1 ;;; vc-bzr.el --- VC backend for the bzr revision control system
3 ;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com>
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.
31 ;; Load this library to register bzr support in VC. It covers basic VC
32 ;; functionality, but was only lightly exercised with a few Emacs/bzr
33 ;; version combinations, namely those current on the authors' PCs.
34 ;; See various Fixmes below.
40 ;; When edititing a symlink and *both* the symlink and its target
41 ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
42 ;; symlink, thereby not detecting whether the actual contents
43 ;; (that is, the target contents) are changed.
44 ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
46 ;; For an up-to-date list of bugs, please see:
47 ;; https://bugs.launchpad.net/vc-bzr/+bugs
54 (require 'vc
)) ; for vc-exec-after
56 ;; Clear up the cache to force vc-call to check again and discover
57 ;; new functions when we reload this file.
58 (put 'Bzr
'vc-functions nil
)
65 (defcustom vc-bzr-program
"bzr"
66 "Name of the bzr command (excluding any arguments)."
70 ;; Fixme: there's probably no call for this.
71 (defcustom vc-bzr-program-args nil
72 "List of global arguments to pass to `vc-bzr-program'."
74 :type
'(repeat string
))
76 (defcustom vc-bzr-diff-switches nil
77 "String/list of strings specifying extra switches for bzr diff under VC."
78 :type
'(choice (const :tag
"None" nil
)
79 (string :tag
"Argument String")
80 (repeat :tag
"Argument List" :value
("") string
))
83 ;; since v0.9, bzr supports removing the progress indicators
84 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
85 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list
&rest args
)
86 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
87 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' to the environment."
88 (let ((process-environment
89 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
90 "LC_ALL=C" ; Force English output
91 process-environment
)))
92 (apply 'vc-do-command buffer okstatus vc-bzr-program
93 file-or-list bzr-command
(append vc-bzr-program-args args
))))
96 (defconst vc-bzr-admin-dirname
".bzr" ; FIXME: "_bzr" on w32?
97 "Name of the directory containing Bzr repository status files.")
99 (defconst vc-bzr-admin-checkout-format-file
100 (concat vc-bzr-admin-dirname
"/checkout/format"))
101 (defconst vc-bzr-admin-dirstate
102 (concat vc-bzr-admin-dirname
"/checkout/dirstate"))
103 (defconst vc-bzr-admin-branch-format-file
104 (concat vc-bzr-admin-dirname
"/branch/format"))
105 (defconst vc-bzr-admin-revhistory
106 (concat vc-bzr-admin-dirname
"/branch/revision-history"))
108 ;;;###autoload (defun vc-bzr-registered (file)
109 ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
110 ;;;###autoload (progn
111 ;;;###autoload (load "vc-bzr")
112 ;;;###autoload (vc-bzr-registered file))))
114 (defun vc-bzr-root (file)
115 "Return the root directory of the bzr repository containing FILE."
116 ;; Cache technique copied from vc-arch.el.
117 (or (vc-file-getprop file
'bzr-root
)
120 (vc-find-root file vc-bzr-admin-checkout-format-file
))))
122 (defun vc-bzr-registered (file)
123 "Return non-nil if FILE is registered with bzr.
125 For speed, this function tries first to parse Bzr internal file
126 `checkout/dirstate', but it may fail if Bzr internal file format
127 has changed. As a safeguard, the `checkout/dirstate' file is
128 only parsed if it contains the string `#bazaar dirstate flat
129 format 3' in the first line.
131 If the `checkout/dirstate' file cannot be parsed, fall back to
132 running `vc-bzr-state'."
134 (lexical-let ((root (vc-bzr-root file
)))
135 (and root
; Short cut.
136 ;; This looks at internal files. May break if they change
139 ((dirstate-file (expand-file-name vc-bzr-admin-dirstate root
)))
140 (if (file-exists-p dirstate-file
)
142 (insert-file-contents dirstate-file
)
143 (goto-char (point-min))
144 (when (looking-at "#bazaar dirstate flat format 3")
145 (let* ((relfile (file-relative-name file root
))
146 (reldir (file-name-directory relfile
)))
149 (if reldir
(regexp-quote (directory-file-name reldir
)))
151 (regexp-quote (file-name-nondirectory relfile
))
155 (vc-bzr-state file
))) ; Expensive.
156 (file-error nil
))) ; vc-bzr-program not found
158 (defun vc-bzr-buffer-nonblank-p (&optional buffer
)
159 "Return non-nil if BUFFER contains any non-blank characters."
160 (or (> (buffer-size buffer
) 0)
162 (set-buffer (or buffer
(current-buffer)))
163 (goto-char (point-min))
164 (re-search-forward "[^ \t\n]" (point-max) t
))))
166 (defconst vc-bzr-state-words
167 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
168 "Regexp matching file status words as reported in `bzr' output.")
170 (defun vc-bzr-file-name-relative (filename)
171 "Return file name FILENAME stripped of the initial Bzr repository path."
173 ((filename* (expand-file-name filename
))
174 (rootdir (vc-bzr-root (file-name-directory filename
*))))
176 (file-relative-name filename
* rootdir
))))
178 ;; FIXME: Also get this in a non-registered sub-directory.
179 ;; It already works for me. -- Riccardo
180 (defun vc-bzr-status (file)
181 "Return FILE status according to Bzr.
182 Return value is a cons (STATUS . WARNING), where WARNING is a
183 string or nil, and STATUS is one of the symbols: 'added,
184 'ignored, 'kindchange, 'modified, 'removed, 'renamed, 'unknown,
185 which directly correspond to `bzr status' output, or 'unchanged
186 for files whose copy in the working tree is identical to the one
187 in the branch repository, or nil for files that are not
190 If any error occurred in running `bzr status', then return nil."
193 (let ((ret (vc-bzr-command "status" t
0 file
))
195 ;; the only secure status indication in `bzr status' output
196 ;; is a couple of lines following the pattern::
199 ;; if the file is up-to-date, we get no status report from `bzr',
200 ;; so if the regexp search for the above pattern fails, we consider
201 ;; the file to be up-to-date.
202 (goto-char (point-min))
205 ;; bzr prints paths relative to the repository root
206 (concat "^\\(" vc-bzr-state-words
"\\):[ \t\n]+"
207 (regexp-quote (vc-bzr-file-name-relative file
))
208 (if (file-directory-p file
) "/?" "")
211 (let ((start (match-beginning 0))
216 ((not (equal ret
0)) nil
)
217 ((looking-at "added") 'added
)
218 ((looking-at "kind changed") 'kindchange
)
219 ((looking-at "renamed") 'renamed
)
220 ((looking-at "modified") 'modified
)
221 ((looking-at "removed") 'removed
)
222 ((looking-at "ignored") 'ignored
)
223 ((looking-at "unknown") 'unknown
)))
224 ;; erase the status text that matched
225 (delete-region start end
)))
228 ;; "bzr" will output warnings and informational messages to
229 ;; stderr; due to Emacs' `vc-do-command' (and, it seems,
230 ;; `start-process' itself) limitations, we cannot catch stderr
231 ;; and stdout into different buffers. So, if there's anything
232 ;; left in the buffer after removing the above status
233 ;; keywords, let us just presume that any other message from
234 ;; "bzr" is a user warning, and display it.
235 (if (vc-bzr-buffer-nonblank-p)
236 (buffer-substring (point-min) (point-max)))))))
237 (file-error nil
))) ; vc-bzr-program not found
239 (defun vc-bzr-state (file)
240 (lexical-let ((result (vc-bzr-status file
)))
243 (message "Warnings in `bzr' output: %s" (cdr result
)))
244 (cdr (assq (car result
)
246 (kindchange . edited
)
252 (unchanged . up-to-date
)))))))
254 (defun vc-bzr-workfile-unchanged-p (file)
255 (eq 'unchanged
(car (vc-bzr-status file
))))
257 (defun vc-bzr-workfile-version (file)
259 ((rootdir (vc-bzr-root file
))
260 (branch-format-file (concat rootdir
"/" vc-bzr-admin-branch-format-file
))
261 (revhistory-file (concat rootdir
"/" vc-bzr-admin-revhistory
))
262 (lastrev-file (concat rootdir
"/" "branch/last-revision")))
263 ;; Count lines in .bzr/branch/revision-history to avoid forking a
264 ;; bzr process. This looks at internal files. May break if they
265 ;; change their format.
266 (if (file-exists-p branch-format-file
)
268 (insert-file-contents branch-format-file
)
269 (goto-char (point-min))
272 (looking-at "Bazaar-NG branch, format 0.0.4")
273 (looking-at "Bazaar-NG branch format 5"))
274 ;; count lines in .bzr/branch/revision-history
275 (insert-file-contents revhistory-file
)
276 (number-to-string (count-lines (line-end-position) (point-max))))
277 ((looking-at "Bazaar Branch Format 6 (bzr 0.15)")
278 ;; revno is the first number in .bzr/branch/last-revision
279 (insert-file-contents lastrev-file
)
280 (goto-char (line-end-position))
281 (if (re-search-forward "[0-9]+" nil t
)
282 (buffer-substring (match-beginning 0) (match-end 0))))))
283 ;; fallback to calling "bzr revno"
285 ((result (vc-bzr-command-discarding-stderr
286 vc-bzr-program
"revno" file
))
287 (exitcode (car result
))
288 (output (cdr result
)))
290 ((eq exitcode
0) (substring output
0 -
1))
293 (defun vc-bzr-checkout-model (file)
296 (defun vc-bzr-create-repo ()
297 "Create a new Bzr repository."
298 (vc-bzr-command "init" nil
0 nil
))
300 (defun vc-bzr-register (files &optional rev comment
)
301 "Register FILE under bzr.
302 Signal an error unless REV is nil.
304 (if rev
(error "Can't register explicit version with bzr"))
305 (vc-bzr-command "add" nil
0 files
))
307 ;; Could run `bzr status' in the directory and see if it succeeds, but
308 ;; that's relatively expensive.
309 (defalias 'vc-bzr-responsible-p
'vc-bzr-root
310 "Return non-nil if FILE is (potentially) controlled by bzr.
311 The criterion is that there is a `.bzr' directory in the same
312 or a superior directory.")
314 (defun vc-bzr-could-register (file)
315 "Return non-nil if FILE could be registered under bzr."
316 (and (vc-bzr-responsible-p file
) ; shortcut
319 (vc-bzr-command "add" t
0 file
"--dry-run")
320 ;; The command succeeds with no output if file is
321 ;; registered (in bzr 0.8).
322 (goto-char (point-min))
323 (looking-at "added "))
326 (defun vc-bzr-unregister (file)
327 "Unregister FILE from bzr."
328 (vc-bzr-command "remove" nil
0 file
))
330 (defun vc-bzr-checkin (files rev comment
)
331 "Check FILE in to bzr with log message COMMENT.
332 REV non-nil gets an error."
333 (if rev
(error "Can't check in a specific version with bzr"))
334 (vc-bzr-command "commit" nil
0 files
"-m" comment
))
336 (defun vc-bzr-checkout (file &optional editable rev destfile
)
337 "Checkout revision REV of FILE from bzr to DESTFILE.
338 EDITABLE is ignored."
340 (setq destfile
(vc-version-backup-file-name file rev
)))
341 (let ((coding-system-for-read 'binary
)
342 (coding-system-for-write 'binary
))
343 (with-temp-file destfile
345 (vc-bzr-command "cat" t
0 file
"-r" rev
)
346 (vc-bzr-command "cat" t
0 file
)))))
348 (defun vc-bzr-revert (file &optional contents-done
)
349 (unless contents-done
350 (with-temp-buffer (vc-bzr-command "revert" t
0 file
))))
352 (defvar log-view-message-re
)
353 (defvar log-view-file-re
)
354 (defvar log-view-font-lock-keywords
)
355 (defvar log-view-current-tag-function
)
357 (define-derived-mode vc-bzr-log-view-mode log-view-mode
"Bzr-Log-View"
358 (remove-hook 'log-view-mode-hook
'vc-bzr-log-view-mode
) ;Deactivate the hack.
360 ;; Don't have file markers, so use impossible regexp.
361 (set (make-local-variable 'log-view-file-re
) "\\'\\`")
362 (set (make-local-variable 'log-view-message-re
)
363 "^ *-+\n *\\(?:revno: \\([0-9]+\\)\\|merged: .+\\)")
364 (set (make-local-variable 'log-view-font-lock-keywords
)
365 ;; log-view-font-lock-keywords is careful to use the buffer-local
366 ;; value of log-view-message-re only since Emacs-23.
367 (append `((,log-view-message-re .
'log-view-message-face
))
368 ;; log-view-font-lock-keywords
370 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
372 (2 'change-log-email
))
373 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face
))))))
375 (defun vc-bzr-print-log (files &optional buffer
) ; get buffer arg in Emacs 22
376 "Get bzr change log for FILES into specified BUFFER."
377 ;; Fixme: This might need the locale fixing up if things like `revno'
378 ;; got localized, but certainly it shouldn't use LC_ALL=C.
379 ;; NB. Can't be async -- see `vc-bzr-post-command-function'.
380 (vc-bzr-command "log" buffer
0 files
)
381 ;; FIXME: Until Emacs-23, VC was missing a hook to sort out the mode for
382 ;; the buffer, or at least set the regexps right.
383 (unless (fboundp 'vc-default-log-view-mode
)
384 (add-hook 'log-view-mode-hook
'vc-bzr-log-view-mode
)))
386 (defun vc-bzr-show-log-entry (version)
387 "Find entry for patch name VERSION in bzr change log buffer."
388 (goto-char (point-min))
389 (let (case-fold-search)
390 (if (re-search-forward (concat "^-+\nrevno: " version
"$") nil t
)
391 (beginning-of-line 0)
392 (goto-char (point-min)))))
394 (autoload 'vc-diff-switches-list
"vc" nil nil t
)
396 (defun vc-bzr-diff (files &optional rev1 rev2 buffer
)
397 "VC bzr backend for diff."
398 (let ((working (vc-workfile-version (if (consp files
) (car files
) files
))))
399 (if (and (equal rev1 working
) (not rev2
))
401 (if (and (not rev1
) rev2
)
403 ;; NB. Can't be async -- see `vc-bzr-post-command-function'.
404 ;; bzr diff produces condition code 1 for some reason.
405 (apply #'vc-bzr-command
"diff" (or buffer
"*vc-diff*") 1 files
406 "--diff-options" (mapconcat 'identity
(vc-diff-switches-list bzr
)
410 (list "-r" (format "%s..%s" rev1 rev2
))
411 (list "-r" rev1
))))))
413 (defalias 'vc-bzr-diff-tree
'vc-bzr-diff
)
416 ;; FIXME: vc-{next,previous}-version need fixing in vc.el to deal with
417 ;; straight integer versions.
419 (defun vc-bzr-delete-file (file)
420 "Delete FILE and delete it in the bzr repository."
424 (vc-bzr-command "remove" nil
0 file
))
426 (defun vc-bzr-rename-file (old new
)
427 "Rename file from OLD to NEW using `bzr mv'."
428 (vc-bzr-command "mv" nil
0 new old
))
430 (defvar vc-bzr-annotation-table nil
432 (make-variable-buffer-local 'vc-bzr-annotation-table
)
434 (defun vc-bzr-annotate-command (file buffer
&optional version
)
435 "Prepare BUFFER for `vc-annotate' on FILE.
436 Each line is tagged with the revision number, which has a `help-echo'
437 property containing author and date information."
438 (apply #'vc-bzr-command
"annotate" buffer
0 file
"-l" "--all"
439 (if version
(list "-r" version
)))
440 (with-current-buffer buffer
441 ;; Store the tags for the annotated source lines in a hash table
442 ;; to allow saving space by sharing the text properties.
443 (setq vc-bzr-annotation-table
(make-hash-table :test
'equal
))
444 (goto-char (point-min))
445 (while (re-search-forward "^\\( *[0-9]+\\) \\(.+\\) +\\([0-9]\\{8\\}\\) |"
447 (let* ((rev (match-string 1))
448 (author (match-string 2))
449 (date (match-string 3))
450 (key (match-string 0))
451 (tag (gethash key vc-bzr-annotation-table
)))
454 (string-match " +\\'" author
)
455 (setq author
(substring author
0 (match-beginning 0))))
456 (setq tag
(propertize rev
'help-echo
(concat "Author: " author
458 'mouse-face
'highlight
))
459 (puthash key tag vc-bzr-annotation-table
))
461 (insert tag
" |")))))
463 ;; Definition from Emacs 22
464 (unless (fboundp 'vc-annotate-convert-time
)
465 (defun vc-annotate-convert-time (time)
466 "Convert a time value to a floating-point number of days.
467 The argument TIME is a list as returned by `current-time' or
468 `encode-time', only the first two elements of that list are considered."
469 (/ (+ (* (float (car time
)) (lsh 1 16)) (cadr time
)) 24 3600)))
471 (defun vc-bzr-annotate-time ()
472 (when (re-search-forward "^ *[0-9]+ |" nil t
)
473 (let ((prop (get-text-property (line-beginning-position) 'help-echo
)))
474 (string-match "[0-9]+\\'" prop
)
475 (vc-annotate-convert-time
477 (string-to-number (substring (match-string 0 prop
) 6 8))
478 (string-to-number (substring (match-string 0 prop
) 4 6))
479 (string-to-number (substring (match-string 0 prop
) 0 4))
482 (defun vc-bzr-annotate-extract-revision-at-line ()
483 "Return revision for current line of annoation buffer, or nil.
484 Return nil if current line isn't annotated."
487 (if (looking-at " *\\([0-9]+\\) | ")
488 (match-string-no-properties 1))))
490 ;; Not needed for Emacs 22
491 (defun vc-bzr-annotate-difference (point)
492 (let ((next-time (vc-bzr-annotate-time)))
494 (- (vc-annotate-convert-time (current-time)) next-time
))))
496 (defun vc-bzr-command-discarding-stderr (command &rest args
)
497 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
498 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
499 the (numerical) exit code of the process, and OUTPUT is a string
500 containing whatever the process sent to its standard output
501 stream. Standard error output is discarded."
504 (apply #'call-process command nil
(list (current-buffer) nil
) nil args
)
505 (buffer-substring (point-min) (point-max)))))
507 ;; TODO: it would be nice to mark the conflicted files in VC Dired,
508 ;; and implement a command to run ediff and `bzr resolve' once the
509 ;; changes have been merged.
510 (defun vc-bzr-dir-state (dir &optional localp
)
511 "Find the VC state of all files in DIR.
512 Optional argument LOCALP is always ignored."
513 (let ((bzr-root-directory (vc-bzr-root dir
))
515 current-bzr-state current-vc-state
)
516 ;; Check that DIR is a bzr repository.
517 (unless (file-name-absolute-p bzr-root-directory
)
518 (error "Cannot find bzr repository for directory `%s'" dir
))
519 ;; `bzr ls --versioned' lists all versioned files;
520 ;; assume they are up-to-date, unless we are given
521 ;; evidence of the contrary.
524 (vc-bzr-command "ls" t
0 nil
"--versioned" "--non-recursive")
525 (goto-char (point-min))
527 (eq 0 (forward-line)))
529 (let ((file (expand-file-name
530 (buffer-substring-no-properties
531 (line-beginning-position) (line-end-position))
532 bzr-root-directory
)))
533 (vc-file-setprop file
'vc-state
'up-to-date
)
534 ;; XXX: is this correct? what happens if one
535 ;; mixes different SCMs in the same dir?
536 (vc-file-setprop file
'vc-backend
'Bzr
))))
537 ;; `bzr status' reports on added/modified/renamed and unknown/ignored files
540 (vc-bzr-command "status" t
0 nil
)
541 (goto-char (point-min))
543 (eq 0 (forward-line)))
546 ((looking-at "^added")
547 (setq current-vc-state
'edited
)
548 (setq current-bzr-state
'added
))
549 ((looking-at "^kind changed")
550 (setq current-vc-state
'edited
)
551 (setq current-bzr-state
'kindchange
))
552 ((looking-at "^modified")
553 (setq current-vc-state
'edited
)
554 (setq current-bzr-state
'modified
))
555 ((looking-at "^renamed")
556 (setq current-vc-state
'edited
)
557 (setq current-bzr-state
'renamed
))
558 ((looking-at "^\\(unknown\\|ignored\\)")
559 (setq current-vc-state nil
)
560 (setq current-bzr-state
'not-versioned
))
562 ;; file names are indented by two spaces
563 (when current-vc-state
564 (let ((file (expand-file-name
565 (buffer-substring-no-properties
566 (match-end 0) (line-end-position))
567 bzr-root-directory
)))
568 (vc-file-setprop file
'vc-state current-vc-state
)
569 (vc-file-setprop file
'vc-bzr-state current-bzr-state
)
570 (when (eq 'added current-bzr-state
)
571 (vc-file-setprop file
'vc-workfile-version
"0"))))
572 (when (eq 'not-versioned current-bzr-state
)
573 (let ((file (expand-file-name
574 (buffer-substring-no-properties
575 (match-end 0) (line-end-position))
576 bzr-root-directory
)))
577 (vc-file-setprop file
'vc-backend
'none
)
578 (vc-file-setprop file
'vc-state nil
))))
580 ;; skip this part of `bzr status' output
581 (setq current-vc-state nil
)
582 (setq current-bzr-state nil
)))))))
584 (defun vc-bzr-dired-state-info (file)
585 "Bzr-specific version of `vc-dired-state-info'."
586 (if (eq 'edited
(vc-state file
))
587 (let ((bzr-state (vc-file-getprop file
'vc-bzr-state
)))
589 (concat "(" (symbol-name bzr-state
) ")")
590 ;; else fall back to default vc representation
591 (vc-default-dired-state-info 'Bzr file
)))))
593 ;; In case of just `(load "vc-bzr")', but that's probably the wrong
595 (add-to-list 'vc-handled-backends
'Bzr
)
597 (eval-after-load "vc"
598 '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t
))
600 (defconst vc-bzr-unload-hook
602 (setq vc-handled-backends
(delq 'Bzr vc-handled-backends
))
603 (remove-hook 'vc-post-command-functions
'vc-bzr-post-command-function
)))
606 ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
607 ;;; vc-bzr.el ends here