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