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