1 ;;; vc-bzr.el --- VC backend for the bzr revision control system
3 ;; Copyright (C) 2006--2008 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com>
8 ;; Version: 2008-01-04 (Bzr revno 25)
9 ;; URL: http://launchpad.net/vc-bzr
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
29 ;; See <URL:http://bazaar-vcs.org/> concerning bzr. See
30 ;; <URL:http://launchpad.net/vc-bzr> for alternate development
31 ;; branches of `vc-bzr'.
33 ;; Load this library to register bzr support in VC. In GNU Emacs
34 ;; 22.1, you will need to register the `Bzr' backend with VC: add
35 ;; these lines to your .emacs::
37 ;; (unless (memq 'Bzr vc-handled-backends)
38 ;; (add-to-list 'vc-handled-backends 'Bzr))
43 ;; When edititing a symlink and *both* the symlink and its target
44 ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
45 ;; symlink, thereby not detecting whether the actual contents
46 ;; (that is, the target contents) are changed.
47 ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
49 ;; For an up-to-date list of bugs, please see:
50 ;; https://bugs.launchpad.net/vc-bzr/+bugs
57 (require 'vc)) ; for vc-exec-after
59 ;; Clear up the cache to force vc-call to check again and discover
60 ;; new functions when we reload this file.
61 (put 'Bzr 'vc-functions nil)
68 (defcustom vc-bzr-program "bzr"
69 "Name of the bzr command (excluding any arguments)."
73 (defcustom vc-bzr-diff-switches nil
74 "String/list of strings specifying extra switches for bzr diff under VC."
75 :type '(choice (const :tag "None" nil)
76 (string :tag "Argument String")
77 (repeat :tag "Argument List" :value ("") string))
80 (defcustom vc-bzr-log-switches nil
81 "String/list of strings specifying extra switches for `bzr log' under VC."
82 :type '(choice (const :tag "None" nil)
83 (string :tag "Argument String")
84 (repeat :tag "Argument List" :value ("") string))
87 ;; since v0.9, bzr supports removing the progress indicators
88 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
89 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args)
90 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
91 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
92 `LC_ALL=C' to the environment."
93 (let ((process-environment
94 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
95 "LC_MESSAGES=C" ; Force English output
96 process-environment)))
97 (apply 'vc-do-command buffer okstatus vc-bzr-program
98 file-or-list bzr-command args)))
101 (defconst vc-bzr-admin-dirname ".bzr"
102 "Name of the directory containing Bzr repository status files.")
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"))
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))))
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)
127 (vc-find-root file vc-bzr-admin-checkout-format-file))))
129 (defun vc-bzr-registered (file)
130 "Return non-nil if FILE is registered with bzr.
132 For speed, this function tries first to parse Bzr internal file
133 `checkout/dirstate', but it may fail if Bzr internal file format
134 has changed. As a safeguard, the `checkout/dirstate' file is
135 only parsed if it contains the string `#bazaar dirstate flat
136 format 3' in the first line.
138 If the `checkout/dirstate' file cannot be parsed, fall back to
139 running `vc-bzr-state'."
140 (lexical-let ((root (vc-bzr-root file)))
141 (when root ; Short cut.
142 ;; This looks at internal files. May break if they change
144 (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root)))
145 (if (not (file-readable-p dirstate))
146 (vc-bzr-state file) ; Expensive.
148 (insert-file-contents dirstate)
149 (goto-char (point-min))
150 (if (not (looking-at "#bazaar dirstate flat format 3"))
151 (vc-bzr-state file) ; Some other unknown format?
152 (let* ((relfile (file-relative-name file root))
153 (reldir (file-name-directory relfile)))
156 (if reldir (regexp-quote (directory-file-name reldir)))
158 (regexp-quote (file-name-nondirectory relfile))
162 (defconst vc-bzr-state-words
163 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
164 "Regexp matching file status words as reported in `bzr' output.")
166 (defun vc-bzr-file-name-relative (filename)
167 "Return file name FILENAME stripped of the initial Bzr repository path."
169 ((filename* (expand-file-name filename))
170 (rootdir (vc-bzr-root filename*)))
172 (file-relative-name filename* rootdir))))
174 (defun vc-bzr-status (file)
175 "Return FILE status according to Bzr.
176 Return value is a cons (STATUS . WARNING), where WARNING is a
177 string or nil, and STATUS is one of the symbols: `added',
178 `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
179 which directly correspond to `bzr status' output, or 'unchanged
180 for files whose copy in the working tree is identical to the one
181 in the branch repository, or nil for files that are not
184 If any error occurred in running `bzr status', then return nil."
186 (let ((ret (condition-case nil
187 (vc-bzr-command "status" t 0 file)
188 (file-error nil))) ; vc-bzr-program not found.
190 ;; the only secure status indication in `bzr status' output
191 ;; is a couple of lines following the pattern::
194 ;; if the file is up-to-date, we get no status report from `bzr',
195 ;; so if the regexp search for the above pattern fails, we consider
196 ;; the file to be up-to-date.
197 (goto-char (point-min))
198 (when (re-search-forward
199 ;; bzr prints paths relative to the repository root.
200 (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
201 (regexp-quote (vc-bzr-file-name-relative file))
202 ;; Bzr appends a '/' to directory names and
203 ;; '*' to executable files
204 (if (file-directory-p file) "/?" "\\*?")
207 (lexical-let ((statusword (match-string 1)))
208 ;; Erase the status text that matched.
209 (delete-region (match-beginning 0) (match-end 0))
211 (intern (replace-regexp-in-string " " "" statusword)))))
213 (goto-char (point-min))
214 (skip-chars-forward " \n\t") ;Throw away spaces.
216 ;; "bzr" will output warnings and informational messages to
217 ;; stderr; due to Emacs' `vc-do-command' (and, it seems,
218 ;; `start-process' itself) limitations, we cannot catch stderr
219 ;; and stdout into different buffers. So, if there's anything
220 ;; left in the buffer after removing the above status
221 ;; keywords, let us just presume that any other message from
222 ;; "bzr" is a user warning, and display it.
223 (unless (eobp) (buffer-substring (point) (point-max))))))))
225 (defun vc-bzr-state (file)
226 (lexical-let ((result (vc-bzr-status file)))
229 (message "Warnings in `bzr' output: %s" (cdr result)))
230 (cdr (assq (car result)
232 (kindchanged . edited)
238 (unchanged . up-to-date)))))))
240 (defun vc-bzr-workfile-unchanged-p (file)
241 (eq 'unchanged (car (vc-bzr-status file))))
243 (defun vc-bzr-workfile-version (file)
245 ((rootdir (vc-bzr-root file))
246 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
248 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
249 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
250 ;; This looks at internal files to avoid forking a bzr process.
251 ;; May break if they change their format.
252 (if (file-exists-p branch-format-file)
254 (insert-file-contents branch-format-file)
255 (goto-char (point-min))
258 (looking-at "Bazaar-NG branch, format 0.0.4")
259 (looking-at "Bazaar-NG branch format 5"))
260 ;; count lines in .bzr/branch/revision-history
261 (insert-file-contents revhistory-file)
262 (number-to-string (count-lines (line-end-position) (point-max))))
263 ((looking-at "Bazaar Branch Format 6 (bzr 0.15)")
264 ;; revno is the first number in .bzr/branch/last-revision
265 (insert-file-contents lastrev-file)
266 (if (re-search-forward "[0-9]+" nil t)
267 (buffer-substring (match-beginning 0) (match-end 0))))))
268 ;; fallback to calling "bzr revno"
270 ((result (vc-bzr-command-discarding-stderr
271 vc-bzr-program "revno" file))
272 (exitcode (car result))
273 (output (cdr result)))
275 ((eq exitcode 0) (substring output 0 -1))
278 (defun vc-bzr-checkout-model (file)
281 (defun vc-bzr-create-repo ()
282 "Create a new Bzr repository."
283 (vc-bzr-command "init" nil 0 nil))
285 (defun vc-bzr-init-version (&optional file)
286 "Always return nil, as Bzr cannot register explicit versions."
289 (defun vc-bzr-register (files &optional rev comment)
290 "Register FILE under bzr.
291 Signal an error unless REV is nil.
293 (if rev (error "Can't register explicit version with bzr"))
294 (vc-bzr-command "add" nil 0 files))
296 ;; Could run `bzr status' in the directory and see if it succeeds, but
297 ;; that's relatively expensive.
298 (defalias 'vc-bzr-responsible-p 'vc-bzr-root
299 "Return non-nil if FILE is (potentially) controlled by bzr.
300 The criterion is that there is a `.bzr' directory in the same
301 or a superior directory.")
303 (defun vc-bzr-could-register (file)
304 "Return non-nil if FILE could be registered under bzr."
305 (and (vc-bzr-responsible-p file) ; shortcut
308 (vc-bzr-command "add" t 0 file "--dry-run")
309 ;; The command succeeds with no output if file is
310 ;; registered (in bzr 0.8).
311 (goto-char (point-min))
312 (looking-at "added "))
315 (defun vc-bzr-unregister (file)
316 "Unregister FILE from bzr."
317 (vc-bzr-command "remove" nil 0 file "--keep"))
319 (defun vc-bzr-checkin (files rev comment)
320 "Check FILE in to bzr with log message COMMENT.
321 REV non-nil gets an error."
322 (if rev (error "Can't check in a specific version with bzr"))
323 (vc-bzr-command "commit" nil 0 files "-m" comment))
325 (defun vc-bzr-find-version (file rev buffer)
326 "Fetch version REV of file FILE and put it into BUFFER."
327 (with-current-buffer buffer
328 (if (and rev (stringp rev) (not (string= rev "")))
329 (vc-bzr-command "cat" t 0 file "-r" rev)
330 (vc-bzr-command "cat" t 0 file))))
332 (defun vc-bzr-checkout (file &optional editable rev destfile)
333 "Checkout revision REV of FILE from bzr to DESTFILE.
334 EDITABLE is ignored."
336 (setq destfile (vc-version-backup-file-name file rev)))
337 (let ((coding-system-for-read 'binary)
338 (coding-system-for-write 'binary))
339 (with-temp-file destfile
340 (if (and rev (stringp rev) (not (string= rev "")))
341 (vc-bzr-command "cat" t 0 file "-r" rev)
342 (vc-bzr-command "cat" t 0 file)))))
344 (defun vc-bzr-revert (file &optional contents-done)
345 (unless contents-done
346 (with-temp-buffer (vc-bzr-command "revert" t 0 file))))
348 (defvar log-view-message-re)
349 (defvar log-view-file-re)
350 (defvar log-view-font-lock-keywords)
351 (defvar log-view-current-tag-function)
353 (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
354 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
356 ;; Don't have file markers, so use impossible regexp.
357 (set (make-local-variable 'log-view-file-re) "\\'\\`")
358 (set (make-local-variable 'log-view-message-re)
359 "^ *-+\n *\\(?:revno: \\([0-9]+\\)\\|merged: .+\\)")
360 (set (make-local-variable 'log-view-font-lock-keywords)
361 ;; log-view-font-lock-keywords is careful to use the buffer-local
362 ;; value of log-view-message-re only since Emacs-23.
363 (append `((,log-view-message-re . 'log-view-message-face))
364 ;; log-view-font-lock-keywords
366 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
368 (2 'change-log-email))
369 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face))))))
371 (defun vc-bzr-print-log (files &optional buffer) ; get buffer arg in Emacs 22
372 "Get bzr change log for FILES into specified BUFFER."
373 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
374 ;; the log display may not what the user wants - but I see no other
375 ;; way of getting the above regexps working.
376 (apply 'vc-bzr-command "log" buffer 0 files
377 (if (stringp vc-bzr-log-switches)
378 (list vc-bzr-log-switches)
379 vc-bzr-log-switches))
380 ;; FIXME: Until Emacs-23, VC was missing a hook to sort out the mode for
381 ;; the buffer, or at least set the regexps right.
382 (unless (fboundp 'vc-default-log-view-mode)
383 (add-hook 'log-view-mode-hook 'vc-bzr-log-view-mode)))
385 (defun vc-bzr-show-log-entry (version)
386 "Find entry for patch name VERSION in bzr change log buffer."
387 (goto-char (point-min))
388 (let (case-fold-search)
389 (if (re-search-forward (concat "^-+\nrevno: " version "$") nil t)
390 (beginning-of-line 0)
391 (goto-char (point-min)))))
393 (autoload 'vc-diff-switches-list "vc" nil nil t)
395 (defun vc-bzr-diff (files &optional rev1 rev2 buffer)
396 "VC bzr backend for diff."
397 (let ((working (vc-workfile-version (if (consp files) (car files) files))))
398 (if (and (equal rev1 working) (not rev2))
400 (if (and (not rev1) rev2)
402 ;; bzr diff produces condition code 1 for some reason.
403 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 1 files
404 "--diff-options" (mapconcat 'identity (vc-diff-switches-list bzr)
408 (list "-r" (format "%s..%s" rev1 rev2))
409 (list "-r" rev1))))))
411 (defalias 'vc-bzr-diff-tree 'vc-bzr-diff)
414 ;; FIXME: vc-{next,previous}-version need fixing in vc.el to deal with
415 ;; straight integer versions.
417 (defun vc-bzr-delete-file (file)
418 "Delete FILE and delete it in the bzr repository."
422 (vc-bzr-command "remove" nil 0 file))
424 (defun vc-bzr-rename-file (old new)
425 "Rename file from OLD to NEW using `bzr mv'."
426 (vc-bzr-command "mv" nil 0 new old))
428 (defvar vc-bzr-annotation-table nil
430 (make-variable-buffer-local 'vc-bzr-annotation-table)
432 (defun vc-bzr-annotate-command (file buffer &optional version)
433 "Prepare BUFFER for `vc-annotate' on FILE.
434 Each line is tagged with the revision number, which has a `help-echo'
435 property containing author and date information."
436 (apply #'vc-bzr-command "annotate" buffer 0 file "--long" "--all"
437 (if version (list "-r" version)))
438 (with-current-buffer buffer
439 ;; Store the tags for the annotated source lines in a hash table
440 ;; to allow saving space by sharing the text properties.
441 (setq vc-bzr-annotation-table (make-hash-table :test 'equal))
442 (goto-char (point-min))
443 (while (re-search-forward "^\\( *[0-9]+\\) +\\(.+\\) +\\([0-9]\\{8\\}\\) |"
445 (let* ((rev (match-string 1))
446 (author (match-string 2))
447 (date (match-string 3))
448 (key (match-string 0))
449 (tag (gethash key vc-bzr-annotation-table)))
451 (setq tag (propertize rev 'help-echo (concat "Author: " author
453 'mouse-face 'highlight))
454 (puthash key tag vc-bzr-annotation-table))
456 (insert tag " |")))))
458 ;; Definition from Emacs 22
459 (unless (fboundp 'vc-annotate-convert-time)
460 (defun vc-annotate-convert-time (time)
461 "Convert a time value to a floating-point number of days.
462 The argument TIME is a list as returned by `current-time' or
463 `encode-time', only the first two elements of that list are considered."
464 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600)))
466 (defun vc-bzr-annotate-time ()
467 (when (re-search-forward "^ *[0-9]+ |" nil t)
468 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
469 (string-match "[0-9]+\\'" prop)
470 (vc-annotate-convert-time
472 (string-to-number (substring (match-string 0 prop) 6 8))
473 (string-to-number (substring (match-string 0 prop) 4 6))
474 (string-to-number (substring (match-string 0 prop) 0 4))
477 (defun vc-bzr-annotate-extract-revision-at-line ()
478 "Return revision for current line of annoation buffer, or nil.
479 Return nil if current line isn't annotated."
482 (if (looking-at " *\\([0-9]+\\) | ")
483 (match-string-no-properties 1))))
485 ;; Not needed for Emacs 22
486 (defun vc-bzr-annotate-difference (point)
487 (let ((next-time (vc-bzr-annotate-time)))
489 (- (vc-annotate-convert-time (current-time)) next-time))))
491 (defun vc-bzr-command-discarding-stderr (command &rest args)
492 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
493 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
494 the (numerical) exit code of the process, and OUTPUT is a string
495 containing whatever the process sent to its standard output
496 stream. Standard error output is discarded."
499 (apply #'call-process command nil (list (current-buffer) nil) nil args)
500 (buffer-substring (point-min) (point-max)))))
502 ;; TODO: it would be nice to mark the conflicted files in VC Dired,
503 ;; and implement a command to run ediff and `bzr resolve' once the
504 ;; changes have been merged.
505 (defun vc-bzr-dir-state (dir &optional localp)
506 "Find the VC state of all files in DIR.
507 Optional argument LOCALP is always ignored."
508 (let ((bzr-root-directory (vc-bzr-root dir))
510 current-bzr-state current-vc-state)
511 ;; Check that DIR is a bzr repository.
512 (unless (file-name-absolute-p bzr-root-directory)
513 (error "Cannot find bzr repository for directory `%s'" dir))
514 ;; `bzr ls --versioned' lists all versioned files;
515 ;; assume they are up-to-date, unless we are given
516 ;; evidence of the contrary.
519 (vc-bzr-command "ls" t 0 nil "--versioned" "--non-recursive")
520 (goto-char (point-min))
522 (eq 0 (forward-line)))
524 (let ((file (expand-file-name
525 (buffer-substring-no-properties
526 (line-beginning-position) (line-end-position))
527 bzr-root-directory)))
528 ;; files are up-to-date unless they appear in the `bzr
529 ;; status' output below
530 (vc-file-setprop file 'vc-state 'up-to-date)
531 ;; XXX: is this correct? what happens if one
532 ;; mixes different SCMs in the same dir?
533 ;; Anyway, we're looking at the output of `bzr ls --versioned',
534 ;; so we know these files are registered with Bzr.
535 (vc-file-setprop file 'vc-backend 'Bzr))))
536 ;; `bzr status' reports on added/modified/renamed and unknown/ignored files
539 (vc-bzr-command "status" t 0 nil)
540 (goto-char (point-min))
542 (eq 0 (forward-line)))
545 ((looking-at "^added")
546 (setq current-vc-state 'edited)
547 (setq current-bzr-state 'added))
548 ((looking-at "^kind changed")
549 (setq current-vc-state 'edited)
550 (setq current-bzr-state 'kindchanged))
551 ((looking-at "^modified")
552 (setq current-vc-state 'edited)
553 (setq current-bzr-state 'modified))
554 ((looking-at "^renamed")
555 (setq current-vc-state 'edited)
556 (setq current-bzr-state 'renamed))
557 ((looking-at "^\\(unknown\\|ignored\\)")
558 (setq current-vc-state nil)
559 (setq current-bzr-state 'not-versioned))
561 ;; file names are indented by two spaces
562 (when current-vc-state
563 (let ((file (expand-file-name
564 (buffer-substring-no-properties
565 (match-end 0) (line-end-position))
566 bzr-root-directory)))
567 (vc-file-setprop file 'vc-state current-vc-state)
568 (vc-file-setprop file 'vc-bzr-state current-bzr-state)
569 (when (eq 'added current-bzr-state)
570 (vc-file-setprop file 'vc-workfile-version "0"))))
571 (when (eq 'not-versioned current-bzr-state)
572 (let ((file (expand-file-name
573 (buffer-substring-no-properties
574 (match-end 0) (line-end-position))
575 bzr-root-directory)))
576 (vc-file-setprop file 'vc-backend 'none)
577 (vc-file-setprop file 'vc-state nil))))
579 ;; skip this part of `bzr status' output
580 (setq current-vc-state nil)
581 (setq current-bzr-state nil)))))))
583 (defun vc-bzr-dired-state-info (file)
584 "Bzr-specific version of `vc-dired-state-info'."
585 (if (eq 'edited (vc-state file))
586 (concat "(" (symbol-name (or (vc-file-getprop file 'vc-bzr-state)
588 ;; else fall back to default vc.el representation
589 (vc-default-dired-state-info 'Bzr file)))
591 (eval-after-load "vc"
592 '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t))
595 ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
596 ;;; vc-bzr.el ends here