1 ;;; vc-bzr.el --- VC backend for the bzr revision control system
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Riccardo Murri <riccardo.murri@gmail.com>
10 ;; URL: http://launchpad.net/vc-bzr
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; See <URL:http://bazaar-vcs.org/> concerning bzr. See
31 ;; <URL:http://launchpad.net/vc-bzr> for alternate development
32 ;; branches of `vc-bzr'.
34 ;; Load this library to register bzr support in VC.
39 ;; When editing a symlink and *both* the symlink and its target
40 ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
41 ;; symlink, thereby not detecting whether the actual contents
42 ;; (that is, the target contents) are changed.
43 ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
45 ;; For an up-to-date list of bugs, please see:
46 ;; https://bugs.launchpad.net/vc-bzr/+bugs
48 ;;; Properties of the backend
50 (defun vc-bzr-revision-granularity () 'repository
)
51 (defun vc-bzr-checkout-model (files) 'implicit
)
57 (require 'vc
) ;; for vc-exec-after
60 ;; Clear up the cache to force vc-call to check again and discover
61 ;; new functions when we reload this file.
62 (put 'Bzr
'vc-functions nil
)
69 (defcustom vc-bzr-program
"bzr"
70 "Name of the bzr command (excluding any arguments)."
74 (defcustom vc-bzr-diff-switches nil
75 "String or list of strings specifying switches for bzr diff under VC.
76 If nil, use the value of `vc-diff-switches'. If t, use no switches."
77 :type
'(choice (const :tag
"Unspecified" nil
)
79 (string :tag
"Argument String")
80 (repeat :tag
"Argument List" :value
("") string
))
83 (defcustom vc-bzr-log-switches nil
84 "String or list of strings specifying switches for bzr log under VC."
85 :type
'(choice (const :tag
"None" nil
)
86 (string :tag
"Argument String")
87 (repeat :tag
"Argument List" :value
("") string
))
90 ;; since v0.9, bzr supports removing the progress indicators
91 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
92 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list
&rest args
)
93 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
94 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
95 `LC_MESSAGES=C' to the environment."
96 (let ((process-environment
97 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
98 "LC_MESSAGES=C" ; Force English output
99 process-environment
)))
100 (apply 'vc-do-command
(or buffer
"*vc*") okstatus vc-bzr-program
101 file-or-list bzr-command args
)))
105 (defconst vc-bzr-admin-dirname
".bzr"
106 "Name of the directory containing Bzr repository status files.")
108 (defconst vc-bzr-admin-checkout-format-file
109 (concat vc-bzr-admin-dirname
"/checkout/format"))
110 (defconst vc-bzr-admin-dirstate
111 (concat vc-bzr-admin-dirname
"/checkout/dirstate"))
112 (defconst vc-bzr-admin-branch-format-file
113 (concat vc-bzr-admin-dirname
"/branch/format"))
114 (defconst vc-bzr-admin-revhistory
115 (concat vc-bzr-admin-dirname
"/branch/revision-history"))
116 (defconst vc-bzr-admin-lastrev
117 (concat vc-bzr-admin-dirname
"/branch/last-revision"))
118 (defconst vc-bzr-admin-branchconf
119 (concat vc-bzr-admin-dirname
"/branch/branch.conf"))
121 ;;;###autoload (defun vc-bzr-registered (file)
122 ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
123 ;;;###autoload (progn
124 ;;;###autoload (load "vc-bzr")
125 ;;;###autoload (vc-bzr-registered file))))
127 (defun vc-bzr-root (file)
128 "Return the root directory of the bzr repository containing FILE."
129 ;; Cache technique copied from vc-arch.el.
130 (or (vc-file-getprop file
'bzr-root
)
131 (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file
)))
132 (when root
(vc-file-setprop file
'bzr-root root
)))))
134 (defun vc-bzr--branch-conf (file)
135 "Return the Bzr branch config for file FILE, as a string."
137 (insert-file-contents
138 (expand-file-name vc-bzr-admin-branchconf
(vc-bzr-root file
)))
141 (require 'sha1
) ;For sha1-program
143 (defun vc-bzr-sha1 (file)
145 (set-buffer-multibyte nil
)
146 (let ((prog sha1-program
)
148 process-file-side-effects
)
150 (setq args
(cdr prog
))
151 (setq prog
(car prog
)))
152 (apply 'process-file prog
(file-relative-name file
) t nil args
)
153 (buffer-substring (point-min) (+ (point-min) 40)))))
155 (defun vc-bzr-state-heuristic (file)
156 "Like `vc-bzr-state' but hopefully without running Bzr."
157 ;; `bzr status' was excrutiatingly slow with large histories and
158 ;; pending merges, so try to avoid using it until they fix their
159 ;; performance problems.
160 ;; This function tries first to parse Bzr internal file
161 ;; `checkout/dirstate', but it may fail if Bzr internal file format
162 ;; has changed. As a safeguard, the `checkout/dirstate' file is
163 ;; only parsed if it contains the string `#bazaar dirstate flat
164 ;; format 3' in the first line.
165 ;; If the `checkout/dirstate' file cannot be parsed, fall back to
166 ;; running `vc-bzr-state'."
167 (lexical-let ((root (vc-bzr-root file
)))
168 (when root
; Short cut.
169 ;; This looks at internal files. May break if they change
171 (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root
)))
174 (insert-file-contents dirstate
)
175 (goto-char (point-min))
176 (if (not (looking-at "#bazaar dirstate flat format 3"))
177 (vc-bzr-state file
) ; Some other unknown format?
178 (let* ((relfile (file-relative-name file root
))
179 (reldir (file-name-directory relfile
)))
180 (if (re-search-forward
182 (if reldir
(regexp-quote
183 (directory-file-name reldir
)))
185 (regexp-quote (file-name-nondirectory relfile
))
188 "\\([^\0]*\\)\0" ;"a/f/d", a=removed?
189 "\\([^\0]*\\)\0" ;sha1 (empty if conflicted)?
190 "\\([^\0]*\\)\0" ;size?p
191 "[^\0]*\0" ;"y/n", executable?
193 "\\([^\0]*\\)\0" ;"a/f/d" a=added?
194 "\\([^\0]*\\)\0" ;sha1 again?
195 "\\([^\0]*\\)\0" ;size again?
196 "[^\0]*\0" ;"y/n", executable again?
197 "[^\0]*\0" ;last revid?
198 ;; There are more fields when merges are pending.
201 ;; Apparently the second sha1 is the one we want: when
202 ;; there's a conflict, the first sha1 is absent (and the
203 ;; first size seems to correspond to the file with
204 ;; conflict markers).
206 ((eq (char-after (match-beginning 1)) ?a
) 'removed
)
207 ((eq (char-after (match-beginning 4)) ?a
) 'added
)
208 ((or (and (eq (string-to-number (match-string 3))
209 (nth 7 (file-attributes file
)))
210 (equal (match-string 5)
213 ;; It looks like for lightweight
214 ;; checkouts \2 is empty and we need to
215 ;; look for size in \6.
216 (eq (match-beginning 2) (match-end 2))
217 (eq (string-to-number (match-string 6))
218 (nth 7 (file-attributes file
)))
219 (equal (match-string 5)
220 (vc-bzr-sha1 file
))))
224 ;; Either the dirstate file can't be read, or the sha1
225 ;; executable is missing, or ...
226 ;; In either case, recent versions of Bzr aren't that slow
228 (error (vc-bzr-state file
)))))))
231 (defun vc-bzr-registered (file)
232 "Return non-nil if FILE is registered with bzr."
233 (let ((state (vc-bzr-state-heuristic file
)))
234 (not (memq state
'(nil unregistered ignored
)))))
236 (defconst vc-bzr-state-words
237 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
238 "Regexp matching file status words as reported in `bzr' output.")
240 ;; History of Bzr commands.
241 (defvar vc-bzr-history nil
)
243 (defun vc-bzr-file-name-relative (filename)
244 "Return file name FILENAME stripped of the initial Bzr repository path."
246 ((filename* (expand-file-name filename
))
247 (rootdir (vc-bzr-root filename
*)))
249 (file-relative-name filename
* rootdir
))))
251 (defun vc-bzr-async-command (command args
)
252 "Run Bzr COMMAND asynchronously with ARGS, displaying the result.
253 Send the output to a buffer named \"*vc-bzr : NAME*\", where NAME
254 is the root of the current Bzr branch. Display the buffer in
255 some window, but don't select it."
256 ;; TODO: set up hyperlinks.
257 (let* ((dir default-directory
)
258 (root (vc-bzr-root default-directory
))
259 (buffer (get-buffer-create
260 (format "*vc-bzr : %s*"
261 (expand-file-name root
)))))
262 (with-current-buffer buffer
263 (setq default-directory root
)
264 (goto-char (point-max))
265 (unless (eq (point) (point-min))
267 (insert "Running \"" vc-bzr-program
" " command
)
271 ;; Run bzr in the original working directory.
272 (let ((default-directory dir
))
273 (apply 'vc-bzr-command command t
'async nil args
)))
274 (display-buffer buffer
)))
276 (defun vc-bzr-pull (prompt)
277 "Pull changes into the current Bzr branch.
278 Normally, this runs \"bzr pull\". However, if the branch is a
279 bound branch, run \"bzr update\" instead. If there is no default
280 location from which to pull or update, or if PROMPT is non-nil,
281 prompt for the Bzr command to run."
282 (let* ((vc-bzr-program vc-bzr-program
)
283 (branch-conf (vc-bzr--branch-conf default-directory
))
284 ;; Check whether the branch is bound.
285 (bound (string-match "^bound\\s-*=\\s-*True" branch-conf
))
286 ;; If we need to do a "bzr pull", check for a parent. If it
287 ;; does not exist, bzr will need a pull location.
288 (parent (unless bound
290 "^parent_location\\s-*=\\s-*[^\n[:space:]]+"
292 (command (if bound
"update" "pull"))
294 ;; If necessary, prompt for the exact command.
295 (when (or prompt
(not (or bound parent
)))
296 (setq args
(split-string
298 "Run Bzr (like this): "
299 (concat vc-bzr-program
" " command
)
302 (setq vc-bzr-program
(car args
)
305 (vc-bzr-async-command command args
)))
307 (defun vc-bzr-merge-branch ()
308 "Merge another Bzr branch into the current one.
309 Prompt for the Bzr command to run, providing a pre-defined merge
310 source (an upstream branch or a previous merge source) as a
311 default if it is available."
312 (let* ((branch-conf (vc-bzr--branch-conf default-directory
))
313 ;; "bzr merge" without an argument defaults to submit_branch,
314 ;; then parent_location. We extract the specific location
315 ;; and add it explicitly to the command line.
319 "^submit_branch\\s-*=\\s-*\\(?:file://\\)?\\([^\n[:space:]]+\\)$"
321 (match-string 1 branch-conf
))
323 "^parent_location\\s-*=\\s-*\\(?:file://\\)?\\([^\n[:space:]]+\\)$"
325 (match-string 1 branch-conf
))))
329 "Run Bzr (like this): "
330 (concat vc-bzr-program
" merge --pull"
331 (if location
(concat " " location
) ""))
334 (vc-bzr-program (car cmd
))
337 (vc-bzr-async-command command args
)))
339 (defun vc-bzr-status (file)
340 "Return FILE status according to Bzr.
341 Return value is a cons (STATUS . WARNING), where WARNING is a
342 string or nil, and STATUS is one of the symbols: `added',
343 `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
344 which directly correspond to `bzr status' output, or 'unchanged
345 for files whose copy in the working tree is identical to the one
346 in the branch repository, or nil for files that are not
349 If any error occurred in running `bzr status', then return nil."
351 (let ((ret (condition-case nil
352 (vc-bzr-command "status" t
0 file
)
353 (file-error nil
))) ; vc-bzr-program not found.
355 ;; the only secure status indication in `bzr status' output
356 ;; is a couple of lines following the pattern::
359 ;; if the file is up-to-date, we get no status report from `bzr',
360 ;; so if the regexp search for the above pattern fails, we consider
361 ;; the file to be up-to-date.
362 (goto-char (point-min))
363 (when (re-search-forward
364 ;; bzr prints paths relative to the repository root.
365 (concat "^\\(" vc-bzr-state-words
"\\):[ \t\n]+"
366 (regexp-quote (vc-bzr-file-name-relative file
))
367 ;; Bzr appends a '/' to directory names and
368 ;; '*' to executable files
369 (if (file-directory-p file
) "/?" "\\*?")
372 (lexical-let ((statusword (match-string 1)))
373 ;; Erase the status text that matched.
374 (delete-region (match-beginning 0) (match-end 0))
376 (intern (replace-regexp-in-string " " "" statusword
)))))
378 (goto-char (point-min))
379 (skip-chars-forward " \n\t") ;Throw away spaces.
381 ;; "bzr" will output warnings and informational messages to
382 ;; stderr; due to Emacs' `vc-do-command' (and, it seems,
383 ;; `start-process' itself) limitations, we cannot catch stderr
384 ;; and stdout into different buffers. So, if there's anything
385 ;; left in the buffer after removing the above status
386 ;; keywords, let us just presume that any other message from
387 ;; "bzr" is a user warning, and display it.
388 (unless (eobp) (buffer-substring (point) (point-max))))))))
390 (defun vc-bzr-state (file)
391 (lexical-let ((result (vc-bzr-status file
)))
394 (message "Warnings in `bzr' output: %s" (cdr result
)))
395 (cdr (assq (car result
)
397 (kindchanged . edited
)
402 (unknown . unregistered
)
403 (unchanged . up-to-date
)))))))
405 (defun vc-bzr-resolve-when-done ()
406 "Call \"bzr resolve\" if the conflict markers have been removed."
408 (goto-char (point-min))
409 (unless (re-search-forward "^<<<<<<< " nil t
)
410 (vc-bzr-command "resolve" nil
0 buffer-file-name
)
411 ;; Remove the hook so that it is not called multiple times.
412 (remove-hook 'after-save-hook
'vc-bzr-resolve-when-done t
))))
414 (defun vc-bzr-find-file-hook ()
415 (when (and buffer-file-name
416 ;; FIXME: We should check that "bzr status" says "conflict".
417 (file-exists-p (concat buffer-file-name
".BASE"))
418 (file-exists-p (concat buffer-file-name
".OTHER"))
419 (file-exists-p (concat buffer-file-name
".THIS"))
420 ;; If "bzr status" says there's a conflict but there are no
421 ;; conflict markers, it's not clear what we should do.
423 (goto-char (point-min))
424 (re-search-forward "^<<<<<<< " nil t
)))
425 ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
426 ;; but the one in `bzr pull' isn't, so it would be good to provide an
427 ;; elisp function to remerge from the .BASE/OTHER/THIS files.
428 (smerge-start-session)
429 (add-hook 'after-save-hook
'vc-bzr-resolve-when-done nil t
)
430 (message "There are unresolved conflicts in this file")))
432 (defun vc-bzr-workfile-unchanged-p (file)
433 (eq 'unchanged
(car (vc-bzr-status file
))))
435 (defun vc-bzr-working-revision (file)
436 ;; Together with the code in vc-state-heuristic, this makes it possible
437 ;; to get the initial VC state of a Bzr file even if Bzr is not installed.
439 ((rootdir (vc-bzr-root file
))
440 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
442 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir
))
443 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir
)))
444 ;; This looks at internal files to avoid forking a bzr process.
445 ;; May break if they change their format.
446 (if (and (file-exists-p branch-format-file
)
447 ;; For lightweight checkouts (obtained with bzr checkout --lightweight)
448 ;; the branch-format-file does not contain the revision
449 ;; information, we need to look up the branch-format-file
450 ;; in the place where the lightweight checkout comes
451 ;; from. We only do that if it's a local file.
452 (let ((location-fname (expand-file-name
453 (concat vc-bzr-admin-dirname
454 "/branch/location") rootdir
)))
455 ;; The existence of this file is how we distinguish
456 ;; lightweight checkouts.
457 (if (file-exists-p location-fname
)
459 (insert-file-contents location-fname
)
460 ;; If the lightweight checkout points to a
461 ;; location in the local file system, then we can
462 ;; look there for the version information.
463 (when (re-search-forward "file://\\(.+\\)" nil t
)
464 (let ((l-c-parent-dir (match-string 1)))
465 (when (and (memq system-type
'(ms-dos windows-nt
))
466 (string-match-p "^/[[:alpha:]]:" l-c-parent-dir
))
467 ;;; The non-Windows code takes a shortcut by using the host/path
468 ;;; separator slash as the start of the absolute path. That
469 ;;; does not work on Windows, so we must remove it (bug#5345)
470 (setq l-c-parent-dir
(substring l-c-parent-dir
1)))
471 (setq branch-format-file
472 (expand-file-name vc-bzr-admin-branch-format-file
475 (expand-file-name vc-bzr-admin-lastrev l-c-parent-dir
))
476 ;; FIXME: maybe it's overkill to check if both these files exist.
477 (and (file-exists-p branch-format-file
)
478 (file-exists-p lastrev-file
)))))
481 (insert-file-contents branch-format-file
)
482 (goto-char (point-min))
485 (looking-at "Bazaar-NG branch, format 0.0.4")
486 (looking-at "Bazaar-NG branch format 5"))
487 ;; count lines in .bzr/branch/revision-history
488 (insert-file-contents revhistory-file
)
489 (number-to-string (count-lines (line-end-position) (point-max))))
491 (looking-at "Bazaar Branch Format 6 (bzr 0.15)")
492 (looking-at "Bazaar Branch Format 7 (needs bzr 1.6)"))
493 ;; revno is the first number in .bzr/branch/last-revision
494 (insert-file-contents lastrev-file
)
495 (when (re-search-forward "[0-9]+" nil t
)
496 (buffer-substring (match-beginning 0) (match-end 0))))))
497 ;; fallback to calling "bzr revno"
499 ((result (vc-bzr-command-discarding-stderr
500 vc-bzr-program
"revno" (file-relative-name file
)))
501 (exitcode (car result
))
502 (output (cdr result
)))
504 ((eq exitcode
0) (substring output
0 -
1))
507 (defun vc-bzr-create-repo ()
508 "Create a new Bzr repository."
509 (vc-bzr-command "init" nil
0 nil
))
511 (defun vc-bzr-init-revision (&optional file
)
512 "Always return nil, as Bzr cannot register explicit versions."
515 (defun vc-bzr-previous-revision (file rev
)
516 (if (string-match "\\`[0-9]+\\'" rev
)
517 (number-to-string (1- (string-to-number rev
)))
518 (concat "before:" rev
)))
520 (defun vc-bzr-next-revision (file rev
)
521 (if (string-match "\\`[0-9]+\\'" rev
)
522 (number-to-string (1+ (string-to-number rev
)))
523 (error "Don't know how to compute the next revision of %s" rev
)))
525 (defun vc-bzr-register (files &optional rev comment
)
526 "Register FILE under bzr.
527 Signal an error unless REV is nil.
529 (if rev
(error "Can't register explicit revision with bzr"))
530 (vc-bzr-command "add" nil
0 files
))
532 ;; Could run `bzr status' in the directory and see if it succeeds, but
533 ;; that's relatively expensive.
534 (defalias 'vc-bzr-responsible-p
'vc-bzr-root
535 "Return non-nil if FILE is (potentially) controlled by bzr.
536 The criterion is that there is a `.bzr' directory in the same
537 or a superior directory.")
539 (defun vc-bzr-could-register (file)
540 "Return non-nil if FILE could be registered under bzr."
541 (and (vc-bzr-responsible-p file
) ; shortcut
544 (vc-bzr-command "add" t
0 file
"--dry-run")
545 ;; The command succeeds with no output if file is
546 ;; registered (in bzr 0.8).
547 (goto-char (point-min))
548 (looking-at "added "))
551 (defun vc-bzr-unregister (file)
552 "Unregister FILE from bzr."
553 (vc-bzr-command "remove" nil
0 file
"--keep"))
555 (declare-function log-edit-extract-headers
"log-edit" (headers string
))
557 (defun vc-bzr-checkin (files rev comment
)
558 "Check FILE in to bzr with log message COMMENT.
559 REV non-nil gets an error."
560 (if rev
(error "Can't check in a specific revision with bzr"))
561 (apply 'vc-bzr-command
"commit" nil
0
562 files
(cons "-m" (log-edit-extract-headers '(("Author" .
"--author")
563 ("Date" .
"--commit-time")
564 ("Fixes" .
"--fixes"))
567 (defun vc-bzr-find-revision (file rev buffer
)
568 "Fetch revision REV of file FILE and put it into BUFFER."
569 (with-current-buffer buffer
570 (if (and rev
(stringp rev
) (not (string= rev
"")))
571 (vc-bzr-command "cat" t
0 file
"-r" rev
)
572 (vc-bzr-command "cat" t
0 file
))))
574 (defun vc-bzr-checkout (file &optional editable rev
)
575 (if rev
(error "Operation not supported")
576 ;; Else, there's nothing to do.
579 (defun vc-bzr-revert (file &optional contents-done
)
580 (unless contents-done
581 (with-temp-buffer (vc-bzr-command "revert" t
0 file
))))
583 (defvar log-view-message-re
)
584 (defvar log-view-file-re
)
585 (defvar log-view-font-lock-keywords
)
586 (defvar log-view-current-tag-function
)
587 (defvar log-view-per-file-logs
)
589 (define-derived-mode vc-bzr-log-view-mode log-view-mode
"Bzr-Log-View"
590 (remove-hook 'log-view-mode-hook
'vc-bzr-log-view-mode
) ;Deactivate the hack.
592 (set (make-local-variable 'log-view-per-file-logs
) nil
)
593 (set (make-local-variable 'log-view-file-re
) "\\`a\\`")
594 (set (make-local-variable 'log-view-message-re
)
595 (if (eq vc-log-view-type
'short
)
596 "^ *\\([0-9.]+\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\( \\[merge\\]\\)?"
597 "^ *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)"))
598 (set (make-local-variable 'log-view-font-lock-keywords
)
599 ;; log-view-font-lock-keywords is careful to use the buffer-local
600 ;; value of log-view-message-re only since Emacs-23.
601 (if (eq vc-log-view-type
'short
)
602 (append `((,log-view-message-re
603 (1 'log-view-message-face
)
606 (4 'change-log-list nil lax
))))
607 (append `((,log-view-message-re .
'log-view-message-face
))
608 ;; log-view-font-lock-keywords
609 '(("^ *\\(?:committer\\|author\\): \
610 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
612 (2 'change-log-email
))
613 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face
)))))))
615 (defun vc-bzr-print-log (files buffer
&optional shortlog start-revision limit
)
616 "Get bzr change log for FILES into specified BUFFER."
617 ;; `vc-do-command' creates the buffer, but we need it before running
619 (vc-setup-buffer buffer
)
620 ;; If the buffer exists from a previous invocation it might be
622 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
623 ;; the log display may not what the user wants - but I see no other
624 ;; way of getting the above regexps working.
625 (with-current-buffer buffer
626 (apply 'vc-bzr-command
"log" buffer
'async files
628 (when shortlog
'("--line"))
629 (when start-revision
(list (format "-r..%s" start-revision
)))
630 (when limit
(list "-l" (format "%s" limit
)))
631 (if (stringp vc-bzr-log-switches
)
632 (list vc-bzr-log-switches
)
633 vc-bzr-log-switches
)))))
635 (defun vc-bzr-log-incoming (buffer remote-location
)
636 (apply 'vc-bzr-command
"missing" buffer
'async nil
637 (list "--theirs-only" (unless (string= remote-location
"") remote-location
))))
639 (defun vc-bzr-log-outgoing (buffer remote-location
)
640 (apply 'vc-bzr-command
"missing" buffer
'async nil
641 (list "--mine-only" (unless (string= remote-location
"") remote-location
))))
643 (defun vc-bzr-show-log-entry (revision)
644 "Find entry for patch name REVISION in bzr change log buffer."
645 (goto-char (point-min))
647 (let (case-fold-search
649 (if (re-search-forward
650 ;; "revno:" can appear either at the beginning of a line,
652 (concat "^[ ]*-+\n[ ]*revno: "
653 ;; The revision can contain ".", quote it so that it
654 ;; does not interfere with regexp matching.
655 (regexp-quote revision
) "$") nil t
)
657 (beginning-of-line 0)
659 (goto-char (point-min)))
662 (defun vc-bzr-diff (files &optional rev1 rev2 buffer
)
663 "VC bzr backend for diff."
664 ;; `bzr diff' exits with code 1 if diff is non-empty.
665 (apply #'vc-bzr-command
"diff" (or buffer
"*vc-diff*")
666 (if vc-disable-async-diff
1 'async
) files
667 "--diff-options" (mapconcat 'identity
668 (vc-switches 'bzr
'diff
)
670 ;; This `when' is just an optimization because bzr-1.2 is *much*
671 ;; faster when the revision argument is not given.
673 (list "-r" (format "%s..%s"
678 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
679 ;; straight integer revisions.
681 (defun vc-bzr-delete-file (file)
682 "Delete FILE and delete it in the bzr repository."
686 (vc-bzr-command "remove" nil
0 file
))
688 (defun vc-bzr-rename-file (old new
)
689 "Rename file from OLD to NEW using `bzr mv'."
690 (vc-bzr-command "mv" nil
0 new old
))
692 (defvar vc-bzr-annotation-table nil
694 (make-variable-buffer-local 'vc-bzr-annotation-table
)
696 (defun vc-bzr-annotate-command (file buffer
&optional revision
)
697 "Prepare BUFFER for `vc-annotate' on FILE.
698 Each line is tagged with the revision number, which has a `help-echo'
699 property containing author and date information."
700 (apply #'vc-bzr-command
"annotate" buffer
'async file
"--long" "--all"
701 (if revision
(list "-r" revision
)))
702 (lexical-let ((table (make-hash-table :test
'equal
)))
704 (get-buffer-process buffer
)
705 (lambda (proc string
)
706 (when (process-buffer proc
)
707 (with-current-buffer (process-buffer proc
)
708 (setq string
(concat (process-get proc
:vc-left-over
) string
))
709 (while (string-match "^\\( *[0-9.]+ *\\) \\([^\n ]+\\) +\\([0-9]\\{8\\}\\)\\( |.*\n\\)" string
)
710 (let* ((rev (match-string 1 string
))
711 (author (match-string 2 string
))
712 (date (match-string 3 string
))
713 (key (substring string
(match-beginning 0)
714 (match-beginning 4)))
715 (line (match-string 4 string
))
716 (tag (gethash key table
))
717 (inhibit-read-only t
))
718 (setq string
(substring string
(match-end 0)))
722 (format "%s %-7.7s" rev author
)
723 'help-echo
(format "Revision: %d, author: %s, date: %s"
724 (string-to-number rev
)
726 'mouse-face
'highlight
))
727 (puthash key tag table
))
728 (goto-char (process-mark proc
))
730 (move-marker (process-mark proc
) (point))))
731 (process-put proc
:vc-left-over string
)))))))
733 (declare-function vc-annotate-convert-time
"vc-annotate" (time))
735 (defun vc-bzr-annotate-time ()
736 (when (re-search-forward "^ *[0-9.]+ +[^\n ]* +|" nil t
)
737 (let ((prop (get-text-property (line-beginning-position) 'help-echo
)))
738 (string-match "[0-9]+\\'" prop
)
739 (let ((str (match-string-no-properties 0 prop
)))
740 (vc-annotate-convert-time
742 (string-to-number (substring str
6 8))
743 (string-to-number (substring str
4 6))
744 (string-to-number (substring str
0 4))))))))
746 (defun vc-bzr-annotate-extract-revision-at-line ()
747 "Return revision for current line of annoation buffer, or nil.
748 Return nil if current line isn't annotated."
751 (if (looking-at "^ *\\([0-9.]+\\) +[^\n ]* +|")
752 (match-string-no-properties 1))))
754 (defun vc-bzr-command-discarding-stderr (command &rest args
)
755 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
756 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
757 the (numerical) exit code of the process, and OUTPUT is a string
758 containing whatever the process sent to its standard output
759 stream. Standard error output is discarded."
762 (apply #'process-file command nil
(list (current-buffer) nil
) nil args
)
763 (buffer-substring (point-min) (point-max)))))
765 (defstruct (vc-bzr-extra-fileinfo
767 (:constructor vc-bzr-create-extra-fileinfo
(extra-name))
768 (:conc-name vc-bzr-extra-fileinfo-
>))
769 extra-name
) ;; original name for rename targets, new name for
771 (defun vc-bzr-dir-printer (info)
772 "Pretty-printer for the vc-dir-fileinfo structure."
773 (let ((extra (vc-dir-fileinfo->extra info
)))
774 (vc-default-dir-printer 'Bzr info
)
777 (format " (renamed from %s)"
778 (vc-bzr-extra-fileinfo->extra-name extra
))
779 'face
'font-lock-comment-face
)))))
781 ;; FIXME: this needs testing, it's probably incomplete.
782 (defun vc-bzr-after-dir-status (update-function relative-dir
)
783 (let ((status-str nil
)
784 (translation '(("+N " . added
)
786 (" M " . edited
) ;; file text modified
787 (" *" . edited
) ;; execute bit changed
788 (" M*" . edited
) ;; text modified + execute bit changed
789 ;; FIXME: what about ignored files?
791 ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
793 ("? " . unregistered
)
794 ;; No such state, but we need to distinguish this case.
797 ;; For a non existent file FOO, the output is:
798 ;; bzr: ERROR: Path(s) do not exist: FOO
800 ;; If the tree is not up to date, bzr will print this warning:
801 ;; working tree is out of date, run 'bzr update'
803 ;; FIXME: maybe this warning can be put in the vc-dir header...
805 ;; Ignore "P " and "P." for pending patches.
811 (goto-char (point-min))
814 (buffer-substring-no-properties (point) (+ (point) 3)))
815 (setq translated
(cdr (assoc status-str translation
)))
817 ((eq translated
'conflict
)
818 ;; For conflicts the file appears twice in the listing: once
819 ;; with the M flag and once with the C flag, so take care
820 ;; not to add it twice to `result'. Ugly.
822 (buffer-substring-no-properties
823 ;;For files with conflicts the format is:
824 ;;C Text conflict in FILENAME
826 (+ (point) 21) (line-end-position)))
827 (entry (assoc file result
)))
829 (setf (nth 1 entry
) 'conflict
))))
830 ((eq translated
'renamed
)
831 (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t
)
832 (let ((new-name (file-relative-name (match-string 2) relative-dir
))
833 (old-name (file-relative-name (match-string 1) relative-dir
)))
834 (push (list new-name
'edited
835 (vc-bzr-create-extra-fileinfo old-name
)) result
)))
836 ;; do nothing for non existent files
837 ((eq translated
'not-found
))
839 (push (list (file-relative-name
840 (buffer-substring-no-properties
842 (line-end-position)) relative-dir
)
843 translated
) result
)))
845 (funcall update-function result
)))
847 (defun vc-bzr-dir-status (dir update-function
)
848 "Return a list of conses (file . state) for DIR."
849 (vc-bzr-command "status" (current-buffer) 'async dir
"-v" "-S")
851 `(vc-bzr-after-dir-status (quote ,update-function
)
852 ;; "bzr status" results are relative to
853 ;; the bzr root directory, NOT to the
854 ;; directory "bzr status" was invoked in.
856 ;; We pass the relative directory here so
857 ;; that `vc-bzr-after-dir-status' can
858 ;; frob the results accordingly.
859 (file-relative-name ,dir
(vc-bzr-root ,dir
)))))
861 (defun vc-bzr-dir-status-files (dir files default-state update-function
)
862 "Return a list of conses (file . state) for DIR."
863 (apply 'vc-bzr-command
"status" (current-buffer) 'async dir
"-v" "-S" files
)
865 `(vc-bzr-after-dir-status (quote ,update-function
)
866 (file-relative-name ,dir
(vc-bzr-root ,dir
)))))
868 (defvar vc-bzr-shelve-map
869 (let ((map (make-sparse-keymap)))
870 ;; Turn off vc-dir marking
871 (define-key map
[mouse-2
] 'ignore
)
873 (define-key map
[down-mouse-3
] 'vc-bzr-shelve-menu
)
874 (define-key map
"\C-k" 'vc-bzr-shelve-delete-at-point
)
875 (define-key map
"=" 'vc-bzr-shelve-show-at-point
)
876 (define-key map
"\C-m" 'vc-bzr-shelve-show-at-point
)
877 (define-key map
"A" 'vc-bzr-shelve-apply-and-keep-at-point
)
878 (define-key map
"P" 'vc-bzr-shelve-apply-at-point
)
879 (define-key map
"S" 'vc-bzr-shelve-snapshot
)
882 (defvar vc-bzr-shelve-menu-map
883 (let ((map (make-sparse-keymap "Bzr Shelve")))
885 '(menu-item "Delete shelf" vc-bzr-shelve-delete-at-point
886 :help "Delete the current shelf"))
888 '(menu-item "Apply and keep shelf" vc-bzr-shelve-apply-and-keep-at-point
889 :help "Apply the current shelf and keep it"))
891 '(menu-item "Apply and remove shelf (pop)" vc-bzr-shelve-apply-at-point
892 :help "Apply the current shelf and remove it"))
894 '(menu-item "Show shelve" vc-bzr-shelve-show-at-point
895 :help "Show the contents of the current shelve"))
898 (defvar vc-bzr-extra-menu-map
899 (let ((map (make-sparse-keymap)))
900 (define-key map [bzr-sn]
901 '(menu-item "Shelve a snapshot" vc-bzr-shelve-snapshot
902 :help "Shelve the current state of the tree and keep the current state"))
903 (define-key map [bzr-sh]
904 '(menu-item "Shelve..." vc-bzr-shelve
905 :help "Shelve changes"))
908 (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
910 (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
912 (defun vc-bzr-dir-extra-headers (dir)
914 ((str (with-temp-buffer
915 (vc-bzr-command "info" t 0 dir)
917 (shelve (vc-bzr-shelve-list))
918 (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
919 (root-dir (vc-bzr-root dir))
921 ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
922 ;; reliable method to detect pending merges, disable this
923 ;; until a proper solution is implemented.
926 (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
927 (pending-merge-help-echo
928 (format "A merge has been performed.\nA commit from the top-level directory (%s)\nis required before being able to check in anything else" root-dir))
930 (when (string-match ".+light checkout root: \\(.+\\)$" str)
931 (match-string 1 str)))
932 (light-checkout-branch
934 (when (string-match ".+checkout of branch: \\(.+\\)$" str)
935 (match-string 1 str)))))
937 (propertize "Parent branch : " 'face 'font-lock-type-face)
939 (if (string-match "parent branch: \\(.+\\)$" str)
942 'face 'font-lock-variable-name-face)
946 (propertize "Light checkout root: " 'face 'font-lock-type-face)
947 (propertize light-checkout 'face 'font-lock-variable-name-face)
949 (when light-checkout-branch
951 (propertize "Checkout of branch : " 'face 'font-lock-type-face)
952 (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
956 (propertize "Warning : " 'face 'font-lock-warning-face
957 'help-echo pending-merge-help-echo)
958 (propertize "Pending merges, commit recommended before any other action"
959 'help-echo pending-merge-help-echo
960 'face 'font-lock-warning-face)
964 (propertize "Shelves :\n" 'face 'font-lock-type-face
965 'help-echo shelve-help-echo)
969 'face 'font-lock-variable-name-face
970 'mouse-face 'highlight
971 'help-echo "mouse-3: Show shelve menu\nA: Apply and keep shelf\nP: Apply and remove shelf (pop)\nS: Snapshot to a shelf\nC-k: Delete shelf"
972 'keymap vc-bzr-shelve-map))
975 (propertize "Shelves : " 'face 'font-lock-type-face
976 'help-echo shelve-help-echo)
977 (propertize "No shelved changes"
978 'help-echo shelve-help-echo
979 'face 'font-lock-variable-name-face))))))
981 (defun vc-bzr-shelve (name)
983 (interactive "sShelf name: ")
984 (let ((root (vc-bzr-root default-directory)))
986 (vc-bzr-command "shelve" nil 0 nil "--all" "-m" name)
987 (vc-resynch-buffer root t t))))
989 (defun vc-bzr-shelve-show (name)
990 "Show the contents of shelve NAME."
991 (interactive "sShelve name: ")
992 (vc-setup-buffer "*vc-diff*")
993 ;; FIXME: how can you show the contents of a shelf?
994 (vc-bzr-command "unshelve" "*vc-diff*" 'async nil "--preview" name)
995 (set-buffer "*vc-diff*")
997 (setq buffer-read-only t)
998 (pop-to-buffer (current-buffer)))
1000 (defun vc-bzr-shelve-apply (name)
1001 "Apply shelve NAME and remove it afterwards."
1002 (interactive "sApply (and remove) shelf: ")
1003 (vc-bzr-command "unshelve" nil 0 nil "--apply" name)
1004 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1006 (defun vc-bzr-shelve-apply-and-keep (name)
1007 "Apply shelve NAME and keep it afterwards."
1008 (interactive "sApply (and keep) shelf: ")
1009 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep" name)
1010 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1012 (defun vc-bzr-shelve-snapshot ()
1013 "Create a stash with the current tree state."
1015 (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
1016 (let ((ct (current-time)))
1018 (format-time-string "Snapshot on %Y-%m-%d" ct)
1019 (format-time-string " at %H:%M" ct))))
1020 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep")
1021 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1023 (defun vc-bzr-shelve-list ()
1025 (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
1029 (buffer-substring (point-min) (point-max))
1032 (defun vc-bzr-shelve-get-at-point (point)
1036 (if (looking-at "^ +\\([0-9]+\\):")
1038 (error "Cannot find shelf at point"))))
1040 (defun vc-bzr-shelve-delete-at-point ()
1042 (let ((shelve (vc-bzr-shelve-get-at-point (point))))
1043 (when (y-or-n-p (format "Remove shelf %s ?" shelve))
1044 (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
1047 (defun vc-bzr-shelve-show-at-point ()
1049 (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
1051 (defun vc-bzr-shelve-apply-at-point ()
1053 (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
1055 (defun vc-bzr-shelve-apply-and-keep-at-point ()
1057 (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
1059 (defun vc-bzr-shelve-menu (e)
1061 (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
1063 (defun vc-bzr-revision-table (files)
1064 (let ((vc-bzr-revisions '())
1065 (default-directory (file-name-directory (car files))))
1067 (vc-bzr-command "log" t 0 files "--line")
1068 (let ((start (point-min))
1069 (loglines (buffer-substring-no-properties (point-min) (point-max))))
1070 (while (string-match "^\\([0-9]+\\):" loglines)
1071 (push (match-string 1 loglines) vc-bzr-revisions)
1072 (setq start (+ start (match-end 0)))
1073 (setq loglines (buffer-substring-no-properties start (point-max))))))
1076 (defun vc-bzr-conflicted-files (dir)
1077 (let ((default-directory (vc-bzr-root dir))
1080 (vc-bzr-command "status" t 0 default-directory)
1081 (goto-char (point-min))
1082 (when (re-search-forward "^conflicts:\n" nil t)
1083 (while (looking-at " \\(?:Text conflict in \\(.*\\)\\|.*\\)\n")
1085 (push (expand-file-name (match-string 1)) files))
1086 (goto-char (match-end 0)))))
1089 ;;; Revision completion
1092 (defconst vc-bzr-revision-keywords
1093 '("revno" "revid" "last" "before"
1094 "tag" "date" "ancestor" "branch" "submit")))
1096 (defun vc-bzr-revision-completion-table (files)
1097 (lexical-let ((files files))
1098 ;; What about using `files'?!? --Stef
1099 (lambda (string pred action)
1101 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
1103 (completion-table-with-context (substring string 0 (match-end 0))
1105 'completion-table-with-predicate
1106 'completion-file-name-table
1107 'file-directory-p t)
1108 (substring string (match-end 0))
1111 ((string-match "\\`\\(before\\):" string)
1112 (completion-table-with-context (substring string 0 (match-end 0))
1113 (vc-bzr-revision-completion-table files)
1114 (substring string (match-end 0))
1117 ((string-match "\\`\\(tag\\):" string)
1118 (let ((prefix (substring string 0 (match-end 0)))
1119 (tag (substring string (match-end 0)))
1121 process-file-side-effects)
1123 ;; "bzr-1.2 tags" is much faster with --show-ids.
1124 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
1125 ;; The output is ambiguous, unless we assume that revids do not
1127 (goto-char (point-min))
1128 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
1129 (push (match-string-no-properties 1) table)))
1130 (completion-table-with-context prefix table tag pred action)))
1132 ((string-match "\\`\\([a-z]+\\):" string)
1133 ;; no actual completion for the remaining keywords.
1134 (completion-table-with-context (substring string 0 (match-end 0))
1135 (if (member (match-string 1 string)
1136 vc-bzr-revision-keywords)
1137 ;; If it's a valid keyword,
1138 ;; use a non-empty table to
1141 (substring string (match-end 0))
1145 ;; Could use completion-table-with-terminator, except that it
1146 ;; currently doesn't work right w.r.t pcm and doesn't give
1147 ;; the *Completions* output we want.
1148 (complete-with-action action (eval-when-compile
1149 (mapcar (lambda (s) (concat s ":"))
1150 vc-bzr-revision-keywords))
1153 (eval-after-load "vc"
1154 '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t))
1157 ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
1158 ;;; vc-bzr.el ends here