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