(vc-bzr-diff): Use vc-switches rather than the obsolete vc-diff-switches.
[bpt/emacs.git] / lisp / vc-bzr.el
1 ;;; vc-bzr.el --- VC backend for the bzr revision control system
2
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com>
6 ;; Keywords: tools
7 ;; Created: Sept 2006
8 ;; Version: 2008-01-04 (Bzr revno 25)
9 ;; URL: http://launchpad.net/vc-bzr
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; See <URL:http://bazaar-vcs.org/> concerning bzr. See
29 ;; <URL:http://launchpad.net/vc-bzr> for alternate development
30 ;; branches of `vc-bzr'.
31
32 ;; Load this library to register bzr support in VC.
33
34 ;; Known bugs
35 ;; ==========
36
37 ;; When edititing a symlink and *both* the symlink and its target
38 ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
39 ;; symlink, thereby not detecting whether the actual contents
40 ;; (that is, the target contents) are changed.
41 ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
42
43 ;; For an up-to-date list of bugs, please see:
44 ;; https://bugs.launchpad.net/vc-bzr/+bugs
45
46 ;;; Properties of the backend
47
48 (defun vc-bzr-revision-granularity () 'repository)
49 (defun vc-bzr-checkout-model (files) 'implicit)
50
51 ;;; Code:
52
53 (eval-when-compile
54 (require 'cl)
55 (require 'vc) ;; for vc-exec-after
56 (require 'vc-dir))
57
58 ;; Clear up the cache to force vc-call to check again and discover
59 ;; new functions when we reload this file.
60 (put 'Bzr 'vc-functions nil)
61
62 (defgroup vc-bzr nil
63 "VC bzr backend."
64 :version "22.2"
65 :group 'vc)
66
67 (defcustom vc-bzr-program "bzr"
68 "Name of the bzr command (excluding any arguments)."
69 :group 'vc-bzr
70 :type 'string)
71
72 (defcustom vc-bzr-diff-switches nil
73 "String/list of strings specifying extra switches for bzr diff under VC."
74 :type '(choice (const :tag "None" nil)
75 (string :tag "Argument String")
76 (repeat :tag "Argument List" :value ("") string))
77 :group 'vc-bzr)
78
79 (defcustom vc-bzr-log-switches nil
80 "String/list of strings specifying extra switches for `bzr log' under VC."
81 :type '(choice (const :tag "None" nil)
82 (string :tag "Argument String")
83 (repeat :tag "Argument List" :value ("") string))
84 :group 'vc-bzr)
85
86 ;; since v0.9, bzr supports removing the progress indicators
87 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
88 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args)
89 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
90 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
91 `LC_MESSAGES=C' to the environment."
92 (let ((process-environment
93 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
94 "LC_MESSAGES=C" ; Force English output
95 process-environment)))
96 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-bzr-program
97 file-or-list bzr-command args)))
98
99
100 ;;;###autoload
101 (defconst vc-bzr-admin-dirname ".bzr"
102 "Name of the directory containing Bzr repository status files.")
103 ;;;###autoload
104 (defconst vc-bzr-admin-checkout-format-file
105 (concat vc-bzr-admin-dirname "/checkout/format"))
106 (defconst vc-bzr-admin-dirstate
107 (concat vc-bzr-admin-dirname "/checkout/dirstate"))
108 (defconst vc-bzr-admin-branch-format-file
109 (concat vc-bzr-admin-dirname "/branch/format"))
110 (defconst vc-bzr-admin-revhistory
111 (concat vc-bzr-admin-dirname "/branch/revision-history"))
112 (defconst vc-bzr-admin-lastrev
113 (concat vc-bzr-admin-dirname "/branch/last-revision"))
114
115 ;;;###autoload (defun vc-bzr-registered (file)
116 ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
117 ;;;###autoload (progn
118 ;;;###autoload (load "vc-bzr")
119 ;;;###autoload (vc-bzr-registered file))))
120
121 (defun vc-bzr-root (file)
122 "Return the root directory of the bzr repository containing FILE."
123 ;; Cache technique copied from vc-arch.el.
124 (or (vc-file-getprop file 'bzr-root)
125 (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file)))
126 (when root (vc-file-setprop file 'bzr-root root)))))
127
128 (require 'sha1) ;For sha1-program
129
130 (defun vc-bzr-sha1 (file)
131 (with-temp-buffer
132 (set-buffer-multibyte nil)
133 (let ((prog sha1-program)
134 (args nil))
135 (when (consp prog)
136 (setq args (cdr prog))
137 (setq prog (car prog)))
138 (apply 'process-file prog (file-relative-name file) t nil args)
139 (buffer-substring (point-min) (+ (point-min) 40)))))
140
141 (defun vc-bzr-state-heuristic (file)
142 "Like `vc-bzr-state' but hopefully without running Bzr."
143 ;; `bzr status' is excrutiatingly slow with large histories and
144 ;; pending merges, so try to avoid using it until they fix their
145 ;; performance problems.
146 ;; This function tries first to parse Bzr internal file
147 ;; `checkout/dirstate', but it may fail if Bzr internal file format
148 ;; has changed. As a safeguard, the `checkout/dirstate' file is
149 ;; only parsed if it contains the string `#bazaar dirstate flat
150 ;; format 3' in the first line.
151 ;; If the `checkout/dirstate' file cannot be parsed, fall back to
152 ;; running `vc-bzr-state'."
153 (lexical-let ((root (vc-bzr-root file)))
154 (when root ; Short cut.
155 ;; This looks at internal files. May break if they change
156 ;; their format.
157 (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root)))
158 (if (not (file-readable-p dirstate))
159 (vc-bzr-state file) ; Expensive.
160 (with-temp-buffer
161 (insert-file-contents dirstate)
162 (goto-char (point-min))
163 (if (not (looking-at "#bazaar dirstate flat format 3"))
164 (vc-bzr-state file) ; Some other unknown format?
165 (let* ((relfile (file-relative-name file root))
166 (reldir (file-name-directory relfile)))
167 (if (re-search-forward
168 (concat "^\0"
169 (if reldir (regexp-quote
170 (directory-file-name reldir)))
171 "\0"
172 (regexp-quote (file-name-nondirectory relfile))
173 "\0"
174 "[^\0]*\0" ;id?
175 "\\([^\0]*\\)\0" ;"a/f/d", a=removed?
176 "[^\0]*\0" ;sha1 (empty if conflicted)?
177 "\\([^\0]*\\)\0" ;size?
178 "[^\0]*\0" ;"y/n", executable?
179 "[^\0]*\0" ;?
180 "\\([^\0]*\\)\0" ;"a/f/d" a=added?
181 "\\([^\0]*\\)\0" ;sha1 again?
182 "[^\0]*\0" ;size again?
183 "[^\0]*\0" ;"y/n", executable again?
184 "[^\0]*\0" ;last revid?
185 ;; There are more fields when merges are pending.
186 )
187 nil t)
188 ;; Apparently the second sha1 is the one we want: when
189 ;; there's a conflict, the first sha1 is absent (and the
190 ;; first size seems to correspond to the file with
191 ;; conflict markers).
192 (cond
193 ((eq (char-after (match-beginning 1)) ?a) 'removed)
194 ((eq (char-after (match-beginning 3)) ?a) 'added)
195 ((and (eq (string-to-number (match-string 2))
196 (nth 7 (file-attributes file)))
197 (equal (match-string 4)
198 (vc-bzr-sha1 file)))
199 'up-to-date)
200 (t 'edited))
201 'unregistered)))))))))
202
203 (defun vc-bzr-registered (file)
204 "Return non-nil if FILE is registered with bzr."
205 (let ((state (vc-bzr-state-heuristic file)))
206 (not (memq state '(nil unregistered ignored)))))
207
208 (defconst vc-bzr-state-words
209 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
210 "Regexp matching file status words as reported in `bzr' output.")
211
212 (defun vc-bzr-file-name-relative (filename)
213 "Return file name FILENAME stripped of the initial Bzr repository path."
214 (lexical-let*
215 ((filename* (expand-file-name filename))
216 (rootdir (vc-bzr-root filename*)))
217 (when rootdir
218 (file-relative-name filename* rootdir))))
219
220 (defun vc-bzr-status (file)
221 "Return FILE status according to Bzr.
222 Return value is a cons (STATUS . WARNING), where WARNING is a
223 string or nil, and STATUS is one of the symbols: `added',
224 `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
225 which directly correspond to `bzr status' output, or 'unchanged
226 for files whose copy in the working tree is identical to the one
227 in the branch repository, or nil for files that are not
228 registered with Bzr.
229
230 If any error occurred in running `bzr status', then return nil."
231 (with-temp-buffer
232 (let ((ret (condition-case nil
233 (vc-bzr-command "status" t 0 file)
234 (file-error nil))) ; vc-bzr-program not found.
235 (status 'unchanged))
236 ;; the only secure status indication in `bzr status' output
237 ;; is a couple of lines following the pattern::
238 ;; | <status>:
239 ;; | <file name>
240 ;; if the file is up-to-date, we get no status report from `bzr',
241 ;; so if the regexp search for the above pattern fails, we consider
242 ;; the file to be up-to-date.
243 (goto-char (point-min))
244 (when (re-search-forward
245 ;; bzr prints paths relative to the repository root.
246 (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
247 (regexp-quote (vc-bzr-file-name-relative file))
248 ;; Bzr appends a '/' to directory names and
249 ;; '*' to executable files
250 (if (file-directory-p file) "/?" "\\*?")
251 "[ \t\n]*$")
252 nil t)
253 (lexical-let ((statusword (match-string 1)))
254 ;; Erase the status text that matched.
255 (delete-region (match-beginning 0) (match-end 0))
256 (setq status
257 (intern (replace-regexp-in-string " " "" statusword)))))
258 (when status
259 (goto-char (point-min))
260 (skip-chars-forward " \n\t") ;Throw away spaces.
261 (cons status
262 ;; "bzr" will output warnings and informational messages to
263 ;; stderr; due to Emacs' `vc-do-command' (and, it seems,
264 ;; `start-process' itself) limitations, we cannot catch stderr
265 ;; and stdout into different buffers. So, if there's anything
266 ;; left in the buffer after removing the above status
267 ;; keywords, let us just presume that any other message from
268 ;; "bzr" is a user warning, and display it.
269 (unless (eobp) (buffer-substring (point) (point-max))))))))
270
271 (defun vc-bzr-state (file)
272 (lexical-let ((result (vc-bzr-status file)))
273 (when (consp result)
274 (when (cdr result)
275 (message "Warnings in `bzr' output: %s" (cdr result)))
276 (cdr (assq (car result)
277 '((added . added)
278 (kindchanged . edited)
279 (renamed . edited)
280 (modified . edited)
281 (removed . removed)
282 (ignored . ignored)
283 (unknown . unregistered)
284 (unchanged . up-to-date)))))))
285
286 (defun vc-bzr-resolve-when-done ()
287 "Call \"bzr resolve\" if the conflict markers have been removed."
288 (save-excursion
289 (goto-char (point-min))
290 (unless (re-search-forward "^<<<<<<< " nil t)
291 (vc-bzr-command "resolve" nil 0 buffer-file-name)
292 ;; Remove the hook so that it is not called multiple times.
293 (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t))))
294
295 (defun vc-bzr-find-file-hook ()
296 (when (and buffer-file-name
297 ;; FIXME: We should check that "bzr status" says "conflict".
298 (file-exists-p (concat buffer-file-name ".BASE"))
299 (file-exists-p (concat buffer-file-name ".OTHER"))
300 (file-exists-p (concat buffer-file-name ".THIS"))
301 ;; If "bzr status" says there's a conflict but there are no
302 ;; conflict markers, it's not clear what we should do.
303 (save-excursion
304 (goto-char (point-min))
305 (re-search-forward "^<<<<<<< " nil t)))
306 ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
307 ;; but the one in `bzr pull' isn't, so it would be good to provide an
308 ;; elisp function to remerge from the .BASE/OTHER/THIS files.
309 (smerge-start-session)
310 (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t)
311 (message "There are unresolved conflicts in this file")))
312
313 (defun vc-bzr-workfile-unchanged-p (file)
314 (eq 'unchanged (car (vc-bzr-status file))))
315
316 (defun vc-bzr-working-revision (file)
317 ;; Together with the code in vc-state-heuristic, this makes it possible
318 ;; to get the initial VC state of a Bzr file even if Bzr is not installed.
319 (lexical-let*
320 ((rootdir (vc-bzr-root file))
321 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
322 rootdir))
323 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
324 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
325 ;; This looks at internal files to avoid forking a bzr process.
326 ;; May break if they change their format.
327 (if (file-exists-p branch-format-file)
328 (with-temp-buffer
329 (insert-file-contents branch-format-file)
330 (goto-char (point-min))
331 (cond
332 ((or
333 (looking-at "Bazaar-NG branch, format 0.0.4")
334 (looking-at "Bazaar-NG branch format 5"))
335 ;; count lines in .bzr/branch/revision-history
336 (insert-file-contents revhistory-file)
337 (number-to-string (count-lines (line-end-position) (point-max))))
338 ((looking-at "Bazaar Branch Format 6 (bzr 0.15)")
339 ;; revno is the first number in .bzr/branch/last-revision
340 (insert-file-contents lastrev-file)
341 (if (re-search-forward "[0-9]+" nil t)
342 (buffer-substring (match-beginning 0) (match-end 0))))))
343 ;; fallback to calling "bzr revno"
344 (lexical-let*
345 ((result (vc-bzr-command-discarding-stderr
346 vc-bzr-program "revno" (file-relative-name file)))
347 (exitcode (car result))
348 (output (cdr result)))
349 (cond
350 ((eq exitcode 0) (substring output 0 -1))
351 (t nil))))))
352
353 (defun vc-bzr-create-repo ()
354 "Create a new Bzr repository."
355 (vc-bzr-command "init" nil 0 nil))
356
357 (defun vc-bzr-init-revision (&optional file)
358 "Always return nil, as Bzr cannot register explicit versions."
359 nil)
360
361 (defun vc-bzr-previous-revision (file rev)
362 (if (string-match "\\`[0-9]+\\'" rev)
363 (number-to-string (1- (string-to-number rev)))
364 (concat "before:" rev)))
365
366 (defun vc-bzr-next-revision (file rev)
367 (if (string-match "\\`[0-9]+\\'" rev)
368 (number-to-string (1+ (string-to-number rev)))
369 (error "Don't know how to compute the next revision of %s" rev)))
370
371 (defun vc-bzr-register (files &optional rev comment)
372 "Register FILE under bzr.
373 Signal an error unless REV is nil.
374 COMMENT is ignored."
375 (if rev (error "Can't register explicit revision with bzr"))
376 (vc-bzr-command "add" nil 0 files))
377
378 ;; Could run `bzr status' in the directory and see if it succeeds, but
379 ;; that's relatively expensive.
380 (defalias 'vc-bzr-responsible-p 'vc-bzr-root
381 "Return non-nil if FILE is (potentially) controlled by bzr.
382 The criterion is that there is a `.bzr' directory in the same
383 or a superior directory.")
384
385 (defun vc-bzr-could-register (file)
386 "Return non-nil if FILE could be registered under bzr."
387 (and (vc-bzr-responsible-p file) ; shortcut
388 (condition-case ()
389 (with-temp-buffer
390 (vc-bzr-command "add" t 0 file "--dry-run")
391 ;; The command succeeds with no output if file is
392 ;; registered (in bzr 0.8).
393 (goto-char (point-min))
394 (looking-at "added "))
395 (error))))
396
397 (defun vc-bzr-unregister (file)
398 "Unregister FILE from bzr."
399 (vc-bzr-command "remove" nil 0 file "--keep"))
400
401 (defun vc-bzr-checkin (files rev comment)
402 "Check FILE in to bzr with log message COMMENT.
403 REV non-nil gets an error."
404 (if rev (error "Can't check in a specific revision with bzr"))
405 (vc-bzr-command "commit" nil 0 files "-m" comment))
406
407 (defun vc-bzr-find-revision (file rev buffer)
408 "Fetch revision REV of file FILE and put it into BUFFER."
409 (with-current-buffer buffer
410 (if (and rev (stringp rev) (not (string= rev "")))
411 (vc-bzr-command "cat" t 0 file "-r" rev)
412 (vc-bzr-command "cat" t 0 file))))
413
414 (defun vc-bzr-checkout (file &optional editable rev)
415 (if rev (error "Operation not supported")
416 ;; Else, there's nothing to do.
417 nil))
418
419 (defun vc-bzr-revert (file &optional contents-done)
420 (unless contents-done
421 (with-temp-buffer (vc-bzr-command "revert" t 0 file))))
422
423 (defvar log-view-message-re)
424 (defvar log-view-file-re)
425 (defvar log-view-font-lock-keywords)
426 (defvar log-view-current-tag-function)
427 (defvar log-view-per-file-logs)
428
429 (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
430 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
431 (require 'add-log)
432 (set (make-local-variable 'log-view-per-file-logs) nil)
433 (set (make-local-variable 'log-view-file-re) "^Working file:[ \t]+\\(.+\\)")
434 (set (make-local-variable 'log-view-message-re)
435 "^ *-+\n *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)")
436 (set (make-local-variable 'log-view-font-lock-keywords)
437 ;; log-view-font-lock-keywords is careful to use the buffer-local
438 ;; value of log-view-message-re only since Emacs-23.
439 (append `((,log-view-message-re . 'log-view-message-face))
440 ;; log-view-font-lock-keywords
441 '(("^ *committer: \
442 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
443 (1 'change-log-name)
444 (2 'change-log-email))
445 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face))))))
446
447 (defun vc-bzr-print-log (files &optional buffer) ; get buffer arg in Emacs 22
448 "Get bzr change log for FILES into specified BUFFER."
449 ;; `vc-do-command' creates the buffer, but we need it before running
450 ;; the command.
451 (vc-setup-buffer buffer)
452 ;; If the buffer exists from a previous invocation it might be
453 ;; read-only.
454 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
455 ;; the log display may not what the user wants - but I see no other
456 ;; way of getting the above regexps working.
457 (dolist (file files)
458 (vc-exec-after
459 `(let ((inhibit-read-only t))
460 (with-current-buffer buffer
461 ;; Insert the file name so that log-view.el can find it.
462 (insert "Working file: " ',file "\n")) ;; Like RCS/CVS.
463 (apply 'vc-bzr-command "log" ',buffer 'async ',file
464 ',(if (stringp vc-bzr-log-switches)
465 (list vc-bzr-log-switches)
466 vc-bzr-log-switches))))))
467
468 (defun vc-bzr-show-log-entry (revision)
469 "Find entry for patch name REVISION in bzr change log buffer."
470 (goto-char (point-min))
471 (when revision
472 (let (case-fold-search)
473 (if (re-search-forward
474 ;; "revno:" can appear either at the beginning of a line,
475 ;; or indented.
476 (concat "^[ ]*-+\n[ ]*revno: "
477 ;; The revision can contain ".", quote it so that it
478 ;; does not interfere with regexp matching.
479 (regexp-quote revision) "$") nil t)
480 (beginning-of-line 0)
481 (goto-char (point-min))))))
482
483 (defun vc-bzr-diff (files &optional rev1 rev2 buffer)
484 "VC bzr backend for diff."
485 ;; `bzr diff' exits with code 1 if diff is non-empty.
486 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 'async files
487 "--diff-options" (mapconcat 'identity
488 (vc-switches 'bzr 'diff)
489 " ")
490 ;; This `when' is just an optimization because bzr-1.2 is *much*
491 ;; faster when the revision argument is not given.
492 (when (or rev1 rev2)
493 (list "-r" (format "%s..%s"
494 (or rev1 "revno:-1")
495 (or rev2 ""))))))
496
497
498 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
499 ;; straight integer revisions.
500
501 (defun vc-bzr-delete-file (file)
502 "Delete FILE and delete it in the bzr repository."
503 (condition-case ()
504 (delete-file file)
505 (file-error nil))
506 (vc-bzr-command "remove" nil 0 file))
507
508 (defun vc-bzr-rename-file (old new)
509 "Rename file from OLD to NEW using `bzr mv'."
510 (vc-bzr-command "mv" nil 0 new old))
511
512 (defvar vc-bzr-annotation-table nil
513 "Internal use.")
514 (make-variable-buffer-local 'vc-bzr-annotation-table)
515
516 (defun vc-bzr-annotate-command (file buffer &optional revision)
517 "Prepare BUFFER for `vc-annotate' on FILE.
518 Each line is tagged with the revision number, which has a `help-echo'
519 property containing author and date information."
520 (apply #'vc-bzr-command "annotate" buffer 0 file "--long" "--all"
521 (if revision (list "-r" revision)))
522 (with-current-buffer buffer
523 ;; Store the tags for the annotated source lines in a hash table
524 ;; to allow saving space by sharing the text properties.
525 (setq vc-bzr-annotation-table (make-hash-table :test 'equal))
526 (goto-char (point-min))
527 (while (re-search-forward "^\\( *[0-9.]+ *\\) \\([^\n ]+\\) +\\([0-9]\\{8\\}\\) |"
528 nil t)
529 (let* ((rev (match-string 1))
530 (author (match-string 2))
531 (date (match-string 3))
532 (key (match-string 0))
533 (tag (gethash key vc-bzr-annotation-table)))
534 (unless tag
535 (setq tag (propertize rev 'help-echo (concat "Author: " author
536 ", date: " date)
537 'mouse-face 'highlight))
538 (puthash key tag vc-bzr-annotation-table))
539 (replace-match "")
540 (insert tag " |")))))
541
542 (declare-function vc-annotate-convert-time "vc-annotate" (time))
543
544 (defun vc-bzr-annotate-time ()
545 (when (re-search-forward "^ *[0-9.]+ +|" nil t)
546 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
547 (string-match "[0-9]+\\'" prop)
548 (let ((str (match-string-no-properties 0 prop)))
549 (vc-annotate-convert-time
550 (encode-time 0 0 0
551 (string-to-number (substring str 6 8))
552 (string-to-number (substring str 4 6))
553 (string-to-number (substring str 0 4))))))))
554
555 (defun vc-bzr-annotate-extract-revision-at-line ()
556 "Return revision for current line of annoation buffer, or nil.
557 Return nil if current line isn't annotated."
558 (save-excursion
559 (beginning-of-line)
560 (if (looking-at " *\\([0-9.]+\\) *| ")
561 (match-string-no-properties 1))))
562
563 (defun vc-bzr-command-discarding-stderr (command &rest args)
564 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
565 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
566 the (numerical) exit code of the process, and OUTPUT is a string
567 containing whatever the process sent to its standard output
568 stream. Standard error output is discarded."
569 (with-temp-buffer
570 (cons
571 (apply #'process-file command nil (list (current-buffer) nil) nil args)
572 (buffer-substring (point-min) (point-max)))))
573
574 (defun vc-bzr-prettify-state-info (file)
575 "Bzr-specific version of `vc-prettify-state-info'."
576 (if (eq 'edited (vc-state file))
577 (concat "(" (symbol-name (or (vc-file-getprop file 'vc-bzr-state)
578 'edited)) ")")
579 ;; else fall back to default vc.el representation
580 (vc-default-prettify-state-info 'Bzr file)))
581
582 (defstruct (vc-bzr-extra-fileinfo
583 (:copier nil)
584 (:constructor vc-bzr-create-extra-fileinfo (extra-name))
585 (:conc-name vc-bzr-extra-fileinfo->))
586 extra-name) ;; original name for rename targets, new name for
587
588 (defun vc-bzr-status-printer (info)
589 "Pretty-printer for the vc-dir-fileinfo structure."
590 (let ((extra (vc-dir-fileinfo->extra info)))
591 (vc-default-status-printer 'Bzr info)
592 (when extra
593 (insert (propertize
594 (format " (renamed from %s)"
595 (vc-bzr-extra-fileinfo->extra-name extra))
596 'face 'font-lock-comment-face)))))
597
598 ;; FIXME: this needs testing, it's probably incomplete.
599 (defun vc-bzr-after-dir-status (update-function)
600 (let ((status-str nil)
601 (translation '(("+N " . added)
602 ("-D " . removed)
603 (" M " . edited) ;; file text modified
604 (" *" . edited) ;; execute bit changed
605 (" M*" . edited) ;; text modified + execute bit changed
606 ;; FIXME: what about ignored files?
607 (" D " . missing)
608 ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
609 ("C " . conflict)
610 ("? " . unregistered)
611 ("? " . unregistered)
612 ;; No such state, but we need to distinguish this case.
613 ("R " . renamed)
614 ;; Ignore "P " and "P." for pending patches.
615 ))
616 (translated nil)
617 (result nil))
618 (goto-char (point-min))
619 (while (not (eobp))
620 (setq status-str
621 (buffer-substring-no-properties (point) (+ (point) 3)))
622 (setq translated (cdr (assoc status-str translation)))
623 (cond
624 ((eq translated 'conflict)
625 ;; For conflicts the file appears twice in the listing: once
626 ;; with the M flag and once with the C flag, so take care
627 ;; not to add it twice to `result'. Ugly.
628 (let* ((file
629 (buffer-substring-no-properties
630 ;;For files with conflicts the format is:
631 ;;C Text conflict in FILENAME
632 ;; Bah.
633 (+ (point) 21) (line-end-position)))
634 (entry (assoc file result)))
635 (when entry
636 (setf (nth 1 entry) 'conflict))))
637 ((eq translated 'renamed)
638 (re-search-forward "R \\(.*\\) => \\(.*\\)$" (line-end-position) t)
639 (let ((new-name (match-string 2))
640 (old-name (match-string 1)))
641 (push (list new-name 'edited
642 (vc-bzr-create-extra-fileinfo old-name)) result)))
643 (t
644 (push (list (buffer-substring-no-properties
645 (+ (point) 4)
646 (line-end-position))
647 translated) result)))
648 (forward-line))
649 (funcall update-function result)))
650
651 (defun vc-bzr-dir-status (dir update-function)
652 "Return a list of conses (file . state) for DIR."
653 (vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S")
654 (vc-exec-after
655 `(vc-bzr-after-dir-status (quote ,update-function))))
656
657 ;;; Revision completion
658
659 (defun vc-bzr-revision-completion-table (files)
660 (lexical-let ((files files))
661 ;; What about using `files'?!? --Stef
662 (lambda (string pred action)
663 (cond
664 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
665 string)
666 (completion-table-with-context (substring string 0 (match-end 0))
667 ;; FIXME: only allow directories.
668 ;; FIXME: don't allow envvars.
669 'read-file-name-internal
670 (substring string (match-end 0))
671 ;; Dropping `pred'. Maybe we should
672 ;; just stash it in
673 ;; `read-file-name-predicate'?
674 nil
675 action))
676 ((string-match "\\`\\(before\\):" string)
677 (completion-table-with-context (substring string 0 (match-end 0))
678 (vc-bzr-revision-completion-table files)
679 (substring string (match-end 0))
680 pred
681 action))
682 ((string-match "\\`\\(tag\\):" string)
683 (let ((prefix (substring string 0 (match-end 0)))
684 (tag (substring string (match-end 0)))
685 (table nil))
686 (with-temp-buffer
687 ;; "bzr-1.2 tags" is much faster with --show-ids.
688 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
689 ;; The output is ambiguous, unless we assume that revids do not
690 ;; contain spaces.
691 (goto-char (point-min))
692 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
693 (push (match-string-no-properties 1) table)))
694 (completion-table-with-context prefix table tag pred action)))
695
696 ((string-match "\\`\\(revid\\):" string)
697 ;; FIXME: How can I get a list of revision ids?
698 )
699 ((eq (car-safe action) 'boundaries)
700 (list* 'boundaries
701 (string-match "[^:]*\\'" string)
702 (string-match ":" (cdr action))))
703 (t
704 ;; Could use completion-table-with-terminator, except that it
705 ;; currently doesn't work right w.r.t pcm and doesn't give
706 ;; the *Completions* output we want.
707 (complete-with-action action '("revno:" "revid:" "last:" "before:"
708 "tag:" "date:" "ancestor:" "branch:"
709 "submit:")
710 string pred))))))
711
712 (eval-after-load "vc"
713 '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t))
714
715 (provide 'vc-bzr)
716 ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
717 ;;; vc-bzr.el ends here