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