Merge from mainline.
[bpt/emacs.git] / lisp / vc-bzr.el
1 ;;; vc-bzr.el --- VC backend for the bzr revision control system
2
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Riccardo Murri <riccardo.murri@gmail.com>
7 ;; Keywords: tools
8 ;; Created: Sept 2006
9 ;; Version: 2008-01-04 (Bzr revno 25)
10 ;; URL: http://launchpad.net/vc-bzr
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 editing 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 ;;; Properties of the backend
48
49 (defun vc-bzr-revision-granularity () 'repository)
50 (defun vc-bzr-checkout-model (files) 'implicit)
51
52 ;;; Code:
53
54 (eval-when-compile
55 (require 'cl)
56 (require 'vc) ;; for vc-exec-after
57 (require 'vc-dir))
58
59 ;; Clear up the cache to force vc-call to check again and discover
60 ;; new functions when we reload this file.
61 (put 'Bzr 'vc-functions nil)
62
63 (defgroup vc-bzr nil
64 "VC bzr backend."
65 :version "22.2"
66 :group 'vc)
67
68 (defcustom vc-bzr-program "bzr"
69 "Name of the bzr command (excluding any arguments)."
70 :group 'vc-bzr
71 :type 'string)
72
73 (defcustom vc-bzr-diff-switches nil
74 "String or list of strings specifying switches for bzr diff under VC.
75 If nil, use the value of `vc-diff-switches'. If t, use no switches."
76 :type '(choice (const :tag "Unspecified" nil)
77 (const :tag "None" t)
78 (string :tag "Argument String")
79 (repeat :tag "Argument List" :value ("") string))
80 :group 'vc-bzr)
81
82 (defcustom vc-bzr-log-switches nil
83 "String or list of strings specifying switches for bzr log under VC."
84 :type '(choice (const :tag "None" nil)
85 (string :tag "Argument String")
86 (repeat :tag "Argument List" :value ("") string))
87 :group 'vc-bzr)
88
89 ;; since v0.9, bzr supports removing the progress indicators
90 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
91 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args)
92 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
93 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
94 `LC_MESSAGES=C' to the environment."
95 (let ((process-environment
96 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
97 "LC_MESSAGES=C" ; Force English output
98 process-environment)))
99 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-bzr-program
100 file-or-list bzr-command args)))
101
102
103 ;;;###autoload
104 (defconst vc-bzr-admin-dirname ".bzr"
105 "Name of the directory containing Bzr repository status files.")
106 ;;;###autoload
107 (defconst vc-bzr-admin-checkout-format-file
108 (concat vc-bzr-admin-dirname "/checkout/format"))
109 (defconst vc-bzr-admin-dirstate
110 (concat vc-bzr-admin-dirname "/checkout/dirstate"))
111 (defconst vc-bzr-admin-branch-format-file
112 (concat vc-bzr-admin-dirname "/branch/format"))
113 (defconst vc-bzr-admin-revhistory
114 (concat vc-bzr-admin-dirname "/branch/revision-history"))
115 (defconst vc-bzr-admin-lastrev
116 (concat vc-bzr-admin-dirname "/branch/last-revision"))
117
118 ;;;###autoload (defun vc-bzr-registered (file)
119 ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
120 ;;;###autoload (progn
121 ;;;###autoload (load "vc-bzr")
122 ;;;###autoload (vc-bzr-registered file))))
123
124 (defun vc-bzr-root (file)
125 "Return the root directory of the bzr repository containing FILE."
126 ;; Cache technique copied from vc-arch.el.
127 (or (vc-file-getprop file 'bzr-root)
128 (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file)))
129 (when root (vc-file-setprop file 'bzr-root root)))))
130
131 (require 'sha1) ;For sha1-program
132
133 (defun vc-bzr-sha1 (file)
134 (with-temp-buffer
135 (set-buffer-multibyte nil)
136 (let ((prog sha1-program)
137 (args nil)
138 process-file-side-effects)
139 (when (consp prog)
140 (setq args (cdr prog))
141 (setq prog (car prog)))
142 (apply 'process-file prog (file-relative-name file) t nil args)
143 (buffer-substring (point-min) (+ (point-min) 40)))))
144
145 (defun vc-bzr-state-heuristic (file)
146 "Like `vc-bzr-state' but hopefully without running Bzr."
147 ;; `bzr status' was excrutiatingly slow with large histories and
148 ;; pending merges, so try to avoid using it until they fix their
149 ;; performance problems.
150 ;; This function tries first to parse Bzr internal file
151 ;; `checkout/dirstate', but it may fail if Bzr internal file format
152 ;; has changed. As a safeguard, the `checkout/dirstate' file is
153 ;; only parsed if it contains the string `#bazaar dirstate flat
154 ;; format 3' in the first line.
155 ;; If the `checkout/dirstate' file cannot be parsed, fall back to
156 ;; running `vc-bzr-state'."
157 (lexical-let ((root (vc-bzr-root file)))
158 (when root ; Short cut.
159 ;; This looks at internal files. May break if they change
160 ;; their format.
161 (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root)))
162 (condition-case nil
163 (with-temp-buffer
164 (insert-file-contents dirstate)
165 (goto-char (point-min))
166 (if (not (looking-at "#bazaar dirstate flat format 3"))
167 (vc-bzr-state file) ; Some other unknown format?
168 (let* ((relfile (file-relative-name file root))
169 (reldir (file-name-directory relfile)))
170 (if (re-search-forward
171 (concat "^\0"
172 (if reldir (regexp-quote
173 (directory-file-name reldir)))
174 "\0"
175 (regexp-quote (file-name-nondirectory relfile))
176 "\0"
177 "[^\0]*\0" ;id?
178 "\\([^\0]*\\)\0" ;"a/f/d", a=removed?
179 "\\([^\0]*\\)\0" ;sha1 (empty if conflicted)?
180 "\\([^\0]*\\)\0" ;size?p
181 "[^\0]*\0" ;"y/n", executable?
182 "[^\0]*\0" ;?
183 "\\([^\0]*\\)\0" ;"a/f/d" a=added?
184 "\\([^\0]*\\)\0" ;sha1 again?
185 "\\([^\0]*\\)\0" ;size again?
186 "[^\0]*\0" ;"y/n", executable again?
187 "[^\0]*\0" ;last revid?
188 ;; There are more fields when merges are pending.
189 )
190 nil t)
191 ;; Apparently the second sha1 is the one we want: when
192 ;; there's a conflict, the first sha1 is absent (and the
193 ;; first size seems to correspond to the file with
194 ;; conflict markers).
195 (cond
196 ((eq (char-after (match-beginning 1)) ?a) 'removed)
197 ((eq (char-after (match-beginning 4)) ?a) 'added)
198 ((or (and (eq (string-to-number (match-string 3))
199 (nth 7 (file-attributes file)))
200 (equal (match-string 5)
201 (vc-bzr-sha1 file)))
202 (and
203 ;; It looks like for lightweight
204 ;; checkouts \2 is empty and we need to
205 ;; look for size in \6.
206 (eq (match-beginning 2) (match-end 2))
207 (eq (string-to-number (match-string 6))
208 (nth 7 (file-attributes file)))
209 (equal (match-string 5)
210 (vc-bzr-sha1 file))))
211 'up-to-date)
212 (t 'edited))
213 'unregistered))))
214 ;; Either the dirstate file can't be read, or the sha1
215 ;; executable is missing, or ...
216 ;; In either case, recent versions of Bzr aren't that slow
217 ;; any more.
218 (error (vc-bzr-state file)))))))
219
220
221 (defun vc-bzr-registered (file)
222 "Return non-nil if FILE is registered with bzr."
223 (let ((state (vc-bzr-state-heuristic file)))
224 (not (memq state '(nil unregistered ignored)))))
225
226 (defconst vc-bzr-state-words
227 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
228 "Regexp matching file status words as reported in `bzr' output.")
229
230 (defun vc-bzr-file-name-relative (filename)
231 "Return file name FILENAME stripped of the initial Bzr repository path."
232 (lexical-let*
233 ((filename* (expand-file-name filename))
234 (rootdir (vc-bzr-root filename*)))
235 (when rootdir
236 (file-relative-name filename* rootdir))))
237
238 (defun vc-bzr-status (file)
239 "Return FILE status according to Bzr.
240 Return value is a cons (STATUS . WARNING), where WARNING is a
241 string or nil, and STATUS is one of the symbols: `added',
242 `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
243 which directly correspond to `bzr status' output, or 'unchanged
244 for files whose copy in the working tree is identical to the one
245 in the branch repository, or nil for files that are not
246 registered with Bzr.
247
248 If any error occurred in running `bzr status', then return nil."
249 (with-temp-buffer
250 (let ((ret (condition-case nil
251 (vc-bzr-command "status" t 0 file)
252 (file-error nil))) ; vc-bzr-program not found.
253 (status 'unchanged))
254 ;; the only secure status indication in `bzr status' output
255 ;; is a couple of lines following the pattern::
256 ;; | <status>:
257 ;; | <file name>
258 ;; if the file is up-to-date, we get no status report from `bzr',
259 ;; so if the regexp search for the above pattern fails, we consider
260 ;; the file to be up-to-date.
261 (goto-char (point-min))
262 (when (re-search-forward
263 ;; bzr prints paths relative to the repository root.
264 (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
265 (regexp-quote (vc-bzr-file-name-relative file))
266 ;; Bzr appends a '/' to directory names and
267 ;; '*' to executable files
268 (if (file-directory-p file) "/?" "\\*?")
269 "[ \t\n]*$")
270 nil t)
271 (lexical-let ((statusword (match-string 1)))
272 ;; Erase the status text that matched.
273 (delete-region (match-beginning 0) (match-end 0))
274 (setq status
275 (intern (replace-regexp-in-string " " "" statusword)))))
276 (when status
277 (goto-char (point-min))
278 (skip-chars-forward " \n\t") ;Throw away spaces.
279 (cons status
280 ;; "bzr" will output warnings and informational messages to
281 ;; stderr; due to Emacs' `vc-do-command' (and, it seems,
282 ;; `start-process' itself) limitations, we cannot catch stderr
283 ;; and stdout into different buffers. So, if there's anything
284 ;; left in the buffer after removing the above status
285 ;; keywords, let us just presume that any other message from
286 ;; "bzr" is a user warning, and display it.
287 (unless (eobp) (buffer-substring (point) (point-max))))))))
288
289 (defun vc-bzr-state (file)
290 (lexical-let ((result (vc-bzr-status file)))
291 (when (consp result)
292 (when (cdr result)
293 (message "Warnings in `bzr' output: %s" (cdr result)))
294 (cdr (assq (car result)
295 '((added . added)
296 (kindchanged . edited)
297 (renamed . edited)
298 (modified . edited)
299 (removed . removed)
300 (ignored . ignored)
301 (unknown . unregistered)
302 (unchanged . up-to-date)))))))
303
304 (defun vc-bzr-resolve-when-done ()
305 "Call \"bzr resolve\" if the conflict markers have been removed."
306 (save-excursion
307 (goto-char (point-min))
308 (unless (re-search-forward "^<<<<<<< " nil t)
309 (vc-bzr-command "resolve" nil 0 buffer-file-name)
310 ;; Remove the hook so that it is not called multiple times.
311 (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t))))
312
313 (defun vc-bzr-find-file-hook ()
314 (when (and buffer-file-name
315 ;; FIXME: We should check that "bzr status" says "conflict".
316 (file-exists-p (concat buffer-file-name ".BASE"))
317 (file-exists-p (concat buffer-file-name ".OTHER"))
318 (file-exists-p (concat buffer-file-name ".THIS"))
319 ;; If "bzr status" says there's a conflict but there are no
320 ;; conflict markers, it's not clear what we should do.
321 (save-excursion
322 (goto-char (point-min))
323 (re-search-forward "^<<<<<<< " nil t)))
324 ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
325 ;; but the one in `bzr pull' isn't, so it would be good to provide an
326 ;; elisp function to remerge from the .BASE/OTHER/THIS files.
327 (smerge-start-session)
328 (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t)
329 (message "There are unresolved conflicts in this file")))
330
331 (defun vc-bzr-workfile-unchanged-p (file)
332 (eq 'unchanged (car (vc-bzr-status file))))
333
334 (defun vc-bzr-working-revision (file)
335 ;; Together with the code in vc-state-heuristic, this makes it possible
336 ;; to get the initial VC state of a Bzr file even if Bzr is not installed.
337 (lexical-let*
338 ((rootdir (vc-bzr-root file))
339 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
340 rootdir))
341 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
342 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
343 ;; This looks at internal files to avoid forking a bzr process.
344 ;; May break if they change their format.
345 (if (and (file-exists-p branch-format-file)
346 ;; For lightweight checkouts (obtained with bzr checkout --lightweight)
347 ;; the branch-format-file does not contain the revision
348 ;; information, we need to look up the branch-format-file
349 ;; in the place where the lightweight checkout comes
350 ;; from. We only do that if it's a local file.
351 (let ((location-fname (expand-file-name
352 (concat vc-bzr-admin-dirname
353 "/branch/location") rootdir)))
354 ;; The existence of this file is how we distinguish
355 ;; lightweight checkouts.
356 (if (file-exists-p location-fname)
357 (with-temp-buffer
358 (insert-file-contents location-fname)
359 ;; If the lightweight checkout points to a
360 ;; location in the local file system, then we can
361 ;; look there for the version information.
362 (when (re-search-forward "file://\\(.+\\)" nil t)
363 (let ((l-c-parent-dir (match-string 1)))
364 (when (and (memq system-type '(ms-dos windows-nt))
365 (string-match-p "^/[[:alpha:]]:" l-c-parent-dir))
366 ;;; The non-Windows code takes a shortcut by using the host/path
367 ;;; separator slash as the start of the absolute path. That
368 ;;; does not work on Windows, so we must remove it (bug#5345)
369 (setq l-c-parent-dir (substring l-c-parent-dir 1)))
370 (setq branch-format-file
371 (expand-file-name vc-bzr-admin-branch-format-file
372 l-c-parent-dir))
373 (setq lastrev-file
374 (expand-file-name vc-bzr-admin-lastrev l-c-parent-dir))
375 ;; FIXME: maybe it's overkill to check if both these files exist.
376 (and (file-exists-p branch-format-file)
377 (file-exists-p lastrev-file)))))
378 t)))
379 (with-temp-buffer
380 (insert-file-contents branch-format-file)
381 (goto-char (point-min))
382 (cond
383 ((or
384 (looking-at "Bazaar-NG branch, format 0.0.4")
385 (looking-at "Bazaar-NG branch format 5"))
386 ;; count lines in .bzr/branch/revision-history
387 (insert-file-contents revhistory-file)
388 (number-to-string (count-lines (line-end-position) (point-max))))
389 ((or
390 (looking-at "Bazaar Branch Format 6 (bzr 0.15)")
391 (looking-at "Bazaar Branch Format 7 (needs bzr 1.6)"))
392 ;; revno is the first number in .bzr/branch/last-revision
393 (insert-file-contents lastrev-file)
394 (when (re-search-forward "[0-9]+" nil t)
395 (buffer-substring (match-beginning 0) (match-end 0))))))
396 ;; fallback to calling "bzr revno"
397 (lexical-let*
398 ((result (vc-bzr-command-discarding-stderr
399 vc-bzr-program "revno" (file-relative-name file)))
400 (exitcode (car result))
401 (output (cdr result)))
402 (cond
403 ((eq exitcode 0) (substring output 0 -1))
404 (t nil))))))
405
406 (defun vc-bzr-create-repo ()
407 "Create a new Bzr repository."
408 (vc-bzr-command "init" nil 0 nil))
409
410 (defun vc-bzr-init-revision (&optional file)
411 "Always return nil, as Bzr cannot register explicit versions."
412 nil)
413
414 (defun vc-bzr-previous-revision (file rev)
415 (if (string-match "\\`[0-9]+\\'" rev)
416 (number-to-string (1- (string-to-number rev)))
417 (concat "before:" rev)))
418
419 (defun vc-bzr-next-revision (file rev)
420 (if (string-match "\\`[0-9]+\\'" rev)
421 (number-to-string (1+ (string-to-number rev)))
422 (error "Don't know how to compute the next revision of %s" rev)))
423
424 (defun vc-bzr-register (files &optional rev comment)
425 "Register FILE under bzr.
426 Signal an error unless REV is nil.
427 COMMENT is ignored."
428 (if rev (error "Can't register explicit revision with bzr"))
429 (vc-bzr-command "add" nil 0 files))
430
431 ;; Could run `bzr status' in the directory and see if it succeeds, but
432 ;; that's relatively expensive.
433 (defalias 'vc-bzr-responsible-p 'vc-bzr-root
434 "Return non-nil if FILE is (potentially) controlled by bzr.
435 The criterion is that there is a `.bzr' directory in the same
436 or a superior directory.")
437
438 (defun vc-bzr-could-register (file)
439 "Return non-nil if FILE could be registered under bzr."
440 (and (vc-bzr-responsible-p file) ; shortcut
441 (condition-case ()
442 (with-temp-buffer
443 (vc-bzr-command "add" t 0 file "--dry-run")
444 ;; The command succeeds with no output if file is
445 ;; registered (in bzr 0.8).
446 (goto-char (point-min))
447 (looking-at "added "))
448 (error))))
449
450 (defun vc-bzr-unregister (file)
451 "Unregister FILE from bzr."
452 (vc-bzr-command "remove" nil 0 file "--keep"))
453
454 (defun vc-bzr-checkin (files rev comment)
455 "Check FILE in to bzr with log message COMMENT.
456 REV non-nil gets an error."
457 (if rev (error "Can't check in a specific revision with bzr"))
458 (vc-bzr-command "commit" nil 0 files "-m" comment))
459
460 (defun vc-bzr-find-revision (file rev buffer)
461 "Fetch revision REV of file FILE and put it into BUFFER."
462 (with-current-buffer buffer
463 (if (and rev (stringp rev) (not (string= rev "")))
464 (vc-bzr-command "cat" t 0 file "-r" rev)
465 (vc-bzr-command "cat" t 0 file))))
466
467 (defun vc-bzr-checkout (file &optional editable rev)
468 (if rev (error "Operation not supported")
469 ;; Else, there's nothing to do.
470 nil))
471
472 (defun vc-bzr-revert (file &optional contents-done)
473 (unless contents-done
474 (with-temp-buffer (vc-bzr-command "revert" t 0 file))))
475
476 (defvar log-view-message-re)
477 (defvar log-view-file-re)
478 (defvar log-view-font-lock-keywords)
479 (defvar log-view-current-tag-function)
480 (defvar log-view-per-file-logs)
481 (defvar vc-short-log)
482
483 (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
484 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
485 (require 'add-log)
486 (set (make-local-variable 'log-view-per-file-logs) nil)
487 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
488 (set (make-local-variable 'log-view-message-re)
489 (if vc-short-log
490 "^ *\\([0-9.]+\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\( \\[merge\\]\\)?"
491 "^ *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)"))
492 (set (make-local-variable 'log-view-font-lock-keywords)
493 ;; log-view-font-lock-keywords is careful to use the buffer-local
494 ;; value of log-view-message-re only since Emacs-23.
495 (if vc-short-log
496 (append `((,log-view-message-re
497 (1 'log-view-message-face)
498 (2 'change-log-name)
499 (3 'change-log-date)
500 (4 'change-log-list nil lax))))
501 (append `((,log-view-message-re . 'log-view-message-face))
502 ;; log-view-font-lock-keywords
503 '(("^ *\\(?:committer\\|author\\): \
504 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
505 (1 'change-log-name)
506 (2 'change-log-email))
507 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))))
508
509 (defun vc-bzr-print-log (files buffer &optional shortlog start-revision limit)
510 "Get bzr change log for FILES into specified BUFFER."
511 ;; `vc-do-command' creates the buffer, but we need it before running
512 ;; the command.
513 (vc-setup-buffer buffer)
514 ;; If the buffer exists from a previous invocation it might be
515 ;; read-only.
516 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
517 ;; the log display may not what the user wants - but I see no other
518 ;; way of getting the above regexps working.
519 (with-current-buffer buffer
520 (apply 'vc-bzr-command "log" buffer 'async files
521 (append
522 (when shortlog '("--line"))
523 (when start-revision (list (format "-r..%s" start-revision)))
524 (when limit (list "-l" (format "%s" limit)))
525 (if (stringp vc-bzr-log-switches)
526 (list vc-bzr-log-switches)
527 vc-bzr-log-switches)))))
528
529 (defun vc-bzr-show-log-entry (revision)
530 "Find entry for patch name REVISION in bzr change log buffer."
531 (goto-char (point-min))
532 (when revision
533 (let (case-fold-search
534 found)
535 (if (re-search-forward
536 ;; "revno:" can appear either at the beginning of a line,
537 ;; or indented.
538 (concat "^[ ]*-+\n[ ]*revno: "
539 ;; The revision can contain ".", quote it so that it
540 ;; does not interfere with regexp matching.
541 (regexp-quote revision) "$") nil t)
542 (progn
543 (beginning-of-line 0)
544 (setq found t))
545 (goto-char (point-min)))
546 found)))
547
548 (defun vc-bzr-diff (files &optional rev1 rev2 buffer)
549 "VC bzr backend for diff."
550 ;; `bzr diff' exits with code 1 if diff is non-empty.
551 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*")
552 (if vc-disable-async-diff 1 'async) files
553 "--diff-options" (mapconcat 'identity
554 (vc-switches 'bzr 'diff)
555 " ")
556 ;; This `when' is just an optimization because bzr-1.2 is *much*
557 ;; faster when the revision argument is not given.
558 (when (or rev1 rev2)
559 (list "-r" (format "%s..%s"
560 (or rev1 "revno:-1")
561 (or rev2 ""))))))
562
563
564 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
565 ;; straight integer revisions.
566
567 (defun vc-bzr-delete-file (file)
568 "Delete FILE and delete it in the bzr repository."
569 (condition-case ()
570 (delete-file file)
571 (file-error nil))
572 (vc-bzr-command "remove" nil 0 file))
573
574 (defun vc-bzr-rename-file (old new)
575 "Rename file from OLD to NEW using `bzr mv'."
576 (vc-bzr-command "mv" nil 0 new old))
577
578 (defvar vc-bzr-annotation-table nil
579 "Internal use.")
580 (make-variable-buffer-local 'vc-bzr-annotation-table)
581
582 (defun vc-bzr-annotate-command (file buffer &optional revision)
583 "Prepare BUFFER for `vc-annotate' on FILE.
584 Each line is tagged with the revision number, which has a `help-echo'
585 property containing author and date information."
586 (apply #'vc-bzr-command "annotate" buffer 'async file "--long" "--all"
587 (if revision (list "-r" revision)))
588 (lexical-let ((table (make-hash-table :test 'equal)))
589 (set-process-filter
590 (get-buffer-process buffer)
591 (lambda (proc string)
592 (when (process-buffer proc)
593 (with-current-buffer (process-buffer proc)
594 (setq string (concat (process-get proc :vc-left-over) string))
595 (while (string-match "^\\( *[0-9.]+ *\\) \\([^\n ]+\\) +\\([0-9]\\{8\\}\\)\\( |.*\n\\)" string)
596 (let* ((rev (match-string 1 string))
597 (author (match-string 2 string))
598 (date (match-string 3 string))
599 (key (substring string (match-beginning 0)
600 (match-beginning 4)))
601 (line (match-string 4 string))
602 (tag (gethash key table))
603 (inhibit-read-only t))
604 (setq string (substring string (match-end 0)))
605 (unless tag
606 (setq tag
607 (propertize
608 (format "%s %-7.7s" rev author)
609 'help-echo (format "Revision: %d, author: %s, date: %s"
610 (string-to-number rev)
611 author date)
612 'mouse-face 'highlight))
613 (puthash key tag table))
614 (goto-char (process-mark proc))
615 (insert tag line)
616 (move-marker (process-mark proc) (point))))
617 (process-put proc :vc-left-over string)))))))
618
619 (declare-function vc-annotate-convert-time "vc-annotate" (time))
620
621 (defun vc-bzr-annotate-time ()
622 (when (re-search-forward "^ *[0-9.]+ +[^\n ]* +|" nil t)
623 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
624 (string-match "[0-9]+\\'" prop)
625 (let ((str (match-string-no-properties 0 prop)))
626 (vc-annotate-convert-time
627 (encode-time 0 0 0
628 (string-to-number (substring str 6 8))
629 (string-to-number (substring str 4 6))
630 (string-to-number (substring str 0 4))))))))
631
632 (defun vc-bzr-annotate-extract-revision-at-line ()
633 "Return revision for current line of annoation buffer, or nil.
634 Return nil if current line isn't annotated."
635 (save-excursion
636 (beginning-of-line)
637 (if (looking-at "^ *\\([0-9.]+\\) +[^\n ]* +|")
638 (match-string-no-properties 1))))
639
640 (defun vc-bzr-command-discarding-stderr (command &rest args)
641 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
642 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
643 the (numerical) exit code of the process, and OUTPUT is a string
644 containing whatever the process sent to its standard output
645 stream. Standard error output is discarded."
646 (with-temp-buffer
647 (cons
648 (apply #'process-file command nil (list (current-buffer) nil) nil args)
649 (buffer-substring (point-min) (point-max)))))
650
651 (defstruct (vc-bzr-extra-fileinfo
652 (:copier nil)
653 (:constructor vc-bzr-create-extra-fileinfo (extra-name))
654 (:conc-name vc-bzr-extra-fileinfo->))
655 extra-name) ;; original name for rename targets, new name for
656
657 (defun vc-bzr-dir-printer (info)
658 "Pretty-printer for the vc-dir-fileinfo structure."
659 (let ((extra (vc-dir-fileinfo->extra info)))
660 (vc-default-dir-printer 'Bzr info)
661 (when extra
662 (insert (propertize
663 (format " (renamed from %s)"
664 (vc-bzr-extra-fileinfo->extra-name extra))
665 'face 'font-lock-comment-face)))))
666
667 ;; FIXME: this needs testing, it's probably incomplete.
668 (defun vc-bzr-after-dir-status (update-function relative-dir)
669 (let ((status-str nil)
670 (translation '(("+N " . added)
671 ("-D " . removed)
672 (" M " . edited) ;; file text modified
673 (" *" . edited) ;; execute bit changed
674 (" M*" . edited) ;; text modified + execute bit changed
675 ;; FIXME: what about ignored files?
676 (" D " . missing)
677 ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
678 ("C " . conflict)
679 ("? " . unregistered)
680 ;; No such state, but we need to distinguish this case.
681 ("R " . renamed)
682 ("RM " . renamed)
683 ;; For a non existent file FOO, the output is:
684 ;; bzr: ERROR: Path(s) do not exist: FOO
685 ("bzr" . not-found)
686 ;; If the tree is not up to date, bzr will print this warning:
687 ;; working tree is out of date, run 'bzr update'
688 ;; ignore it.
689 ;; FIXME: maybe this warning can be put in the vc-dir header...
690 ("wor" . not-found)
691 ;; Ignore "P " and "P." for pending patches.
692 ("P " . not-found)
693 ("P. " . not-found)
694 ))
695 (translated nil)
696 (result nil))
697 (goto-char (point-min))
698 (while (not (eobp))
699 (setq status-str
700 (buffer-substring-no-properties (point) (+ (point) 3)))
701 (setq translated (cdr (assoc status-str translation)))
702 (cond
703 ((eq translated 'conflict)
704 ;; For conflicts the file appears twice in the listing: once
705 ;; with the M flag and once with the C flag, so take care
706 ;; not to add it twice to `result'. Ugly.
707 (let* ((file
708 (buffer-substring-no-properties
709 ;;For files with conflicts the format is:
710 ;;C Text conflict in FILENAME
711 ;; Bah.
712 (+ (point) 21) (line-end-position)))
713 (entry (assoc file result)))
714 (when entry
715 (setf (nth 1 entry) 'conflict))))
716 ((eq translated 'renamed)
717 (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t)
718 (let ((new-name (file-relative-name (match-string 2) relative-dir))
719 (old-name (file-relative-name (match-string 1) relative-dir)))
720 (push (list new-name 'edited
721 (vc-bzr-create-extra-fileinfo old-name)) result)))
722 ;; do nothing for non existent files
723 ((eq translated 'not-found))
724 (t
725 (push (list (file-relative-name
726 (buffer-substring-no-properties
727 (+ (point) 4)
728 (line-end-position)) relative-dir)
729 translated) result)))
730 (forward-line))
731 (funcall update-function result)))
732
733 (defun vc-bzr-dir-status (dir update-function)
734 "Return a list of conses (file . state) for DIR."
735 (vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S")
736 (vc-exec-after
737 `(vc-bzr-after-dir-status (quote ,update-function)
738 ;; "bzr status" results are relative to
739 ;; the bzr root directory, NOT to the
740 ;; directory "bzr status" was invoked in.
741 ;; Ugh.
742 ;; We pass the relative directory here so
743 ;; that `vc-bzr-after-dir-status' can
744 ;; frob the results accordingly.
745 (file-relative-name ,dir (vc-bzr-root ,dir)))))
746
747 (defun vc-bzr-dir-status-files (dir files default-state update-function)
748 "Return a list of conses (file . state) for DIR."
749 (apply 'vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S" files)
750 (vc-exec-after
751 `(vc-bzr-after-dir-status (quote ,update-function)
752 (file-relative-name ,dir (vc-bzr-root ,dir)))))
753
754 (defvar vc-bzr-shelve-map
755 (let ((map (make-sparse-keymap)))
756 ;; Turn off vc-dir marking
757 (define-key map [mouse-2] 'ignore)
758
759 (define-key map [down-mouse-3] 'vc-bzr-shelve-menu)
760 (define-key map "\C-k" 'vc-bzr-shelve-delete-at-point)
761 (define-key map "=" 'vc-bzr-shelve-show-at-point)
762 (define-key map "\C-m" 'vc-bzr-shelve-show-at-point)
763 (define-key map "A" 'vc-bzr-shelve-apply-and-keep-at-point)
764 (define-key map "P" 'vc-bzr-shelve-apply-at-point)
765 (define-key map "S" 'vc-bzr-shelve-snapshot)
766 map))
767
768 (defvar vc-bzr-shelve-menu-map
769 (let ((map (make-sparse-keymap "Bzr Shelve")))
770 (define-key map [de]
771 '(menu-item "Delete shelf" vc-bzr-shelve-delete-at-point
772 :help "Delete the current shelf"))
773 (define-key map [ap]
774 '(menu-item "Apply and keep shelf" vc-bzr-shelve-apply-and-keep-at-point
775 :help "Apply the current shelf and keep it"))
776 (define-key map [po]
777 '(menu-item "Apply and remove shelf (pop)" vc-bzr-shelve-apply-at-point
778 :help "Apply the current shelf and remove it"))
779 (define-key map [sh]
780 '(menu-item "Show shelve" vc-bzr-shelve-show-at-point
781 :help "Show the contents of the current shelve"))
782 map))
783
784 (defvar vc-bzr-extra-menu-map
785 (let ((map (make-sparse-keymap)))
786 (define-key map [bzr-sn]
787 '(menu-item "Shelve a snapshot" vc-bzr-shelve-snapshot
788 :help "Shelve the current state of the tree and keep the current state"))
789 (define-key map [bzr-sh]
790 '(menu-item "Shelve..." vc-bzr-shelve
791 :help "Shelve changes"))
792 map))
793
794 (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
795
796 (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
797
798 (defun vc-bzr-dir-extra-headers (dir)
799 (let*
800 ((str (with-temp-buffer
801 (vc-bzr-command "info" t 0 dir)
802 (buffer-string)))
803 (shelve (vc-bzr-shelve-list))
804 (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
805 (root-dir (vc-bzr-root dir))
806 (pending-merge
807 ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
808 ;; reliable method to detect pending merges, disable this
809 ;; until a proper solution is implemented.
810 (and nil
811 (file-exists-p
812 (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
813 (pending-merge-help-echo
814 (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))
815 (light-checkout
816 (when (string-match ".+light checkout root: \\(.+\\)$" str)
817 (match-string 1 str)))
818 (light-checkout-branch
819 (when light-checkout
820 (when (string-match ".+checkout of branch: \\(.+\\)$" str)
821 (match-string 1 str)))))
822 (concat
823 (propertize "Parent branch : " 'face 'font-lock-type-face)
824 (propertize
825 (if (string-match "parent branch: \\(.+\\)$" str)
826 (match-string 1 str)
827 "None")
828 'face 'font-lock-variable-name-face)
829 "\n"
830 (when light-checkout
831 (concat
832 (propertize "Light checkout root: " 'face 'font-lock-type-face)
833 (propertize light-checkout 'face 'font-lock-variable-name-face)
834 "\n"))
835 (when light-checkout-branch
836 (concat
837 (propertize "Checkout of branch : " 'face 'font-lock-type-face)
838 (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
839 "\n"))
840 (when pending-merge
841 (concat
842 (propertize "Warning : " 'face 'font-lock-warning-face
843 'help-echo pending-merge-help-echo)
844 (propertize "Pending merges, commit recommended before any other action"
845 'help-echo pending-merge-help-echo
846 'face 'font-lock-warning-face)
847 "\n"))
848 (if shelve
849 (concat
850 (propertize "Shelves :\n" 'face 'font-lock-type-face
851 'help-echo shelve-help-echo)
852 (mapconcat
853 (lambda (x)
854 (propertize x
855 'face 'font-lock-variable-name-face
856 'mouse-face 'highlight
857 '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"
858 'keymap vc-bzr-shelve-map))
859 shelve "\n"))
860 (concat
861 (propertize "Shelves : " 'face 'font-lock-type-face
862 'help-echo shelve-help-echo)
863 (propertize "No shelved changes"
864 'help-echo shelve-help-echo
865 'face 'font-lock-variable-name-face))))))
866
867 (defun vc-bzr-shelve (name)
868 "Create a shelve."
869 (interactive "sShelf name: ")
870 (let ((root (vc-bzr-root default-directory)))
871 (when root
872 (vc-bzr-command "shelve" nil 0 nil "--all" "-m" name)
873 (vc-resynch-buffer root t t))))
874
875 (defun vc-bzr-shelve-show (name)
876 "Show the contents of shelve NAME."
877 (interactive "sShelve name: ")
878 (vc-setup-buffer "*vc-bzr-shelve*")
879 ;; FIXME: how can you show the contents of a shelf?
880 (vc-bzr-command "unshelve" "*vc-bzr-shelve*" 'async nil "--preview" name)
881 (set-buffer "*vc-bzr-shelve*")
882 (diff-mode)
883 (setq buffer-read-only t)
884 (pop-to-buffer (current-buffer)))
885
886 (defun vc-bzr-shelve-apply (name)
887 "Apply shelve NAME and remove it afterwards."
888 (interactive "sApply (and remove) shelf: ")
889 (vc-bzr-command "unshelve" "*vc-bzr-shelve*" 0 nil "--apply" name)
890 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
891
892 (defun vc-bzr-shelve-apply-and-keep (name)
893 "Apply shelve NAME and keep it afterwards."
894 (interactive "sApply (and keep) shelf: ")
895 (vc-bzr-command "unshelve" "*vc-bzr-shelve*" 0 nil "--apply" "--keep" name)
896 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
897
898 (defun vc-bzr-shelve-snapshot ()
899 "Create a stash with the current tree state."
900 (interactive)
901 (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
902 (let ((ct (current-time)))
903 (concat
904 (format-time-string "Snapshot on %Y-%m-%d" ct)
905 (format-time-string " at %H:%M" ct))))
906 (vc-bzr-command "unshelve" "*vc-bzr-shelve*" 0 nil "--apply" "--keep")
907 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
908
909 (defun vc-bzr-shelve-list ()
910 (with-temp-buffer
911 (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
912 (delete
913 ""
914 (split-string
915 (buffer-substring (point-min) (point-max))
916 "\n"))))
917
918 (defun vc-bzr-shelve-get-at-point (point)
919 (save-excursion
920 (goto-char point)
921 (beginning-of-line)
922 (if (looking-at "^ +\\([0-9]+\\):")
923 (match-string 1)
924 (error "Cannot find shelf at point"))))
925
926 (defun vc-bzr-shelve-delete-at-point ()
927 (interactive)
928 (let ((shelve (vc-bzr-shelve-get-at-point (point))))
929 (when (y-or-n-p (format "Remove shelf %s ?" shelve))
930 (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
931 (vc-dir-refresh))))
932
933 (defun vc-bzr-shelve-show-at-point ()
934 (interactive)
935 (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
936
937 (defun vc-bzr-shelve-apply-at-point ()
938 (interactive)
939 (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
940
941 (defun vc-bzr-shelve-apply-and-keep-at-point ()
942 (interactive)
943 (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
944
945 (defun vc-bzr-shelve-menu (e)
946 (interactive "e")
947 (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
948
949 (defun vc-bzr-revision-table (files)
950 (let ((vc-bzr-revisions '())
951 (default-directory (file-name-directory (car files))))
952 (with-temp-buffer
953 (vc-bzr-command "log" t 0 files "--line")
954 (let ((start (point-min))
955 (loglines (buffer-substring-no-properties (point-min) (point-max))))
956 (while (string-match "^\\([0-9]+\\):" loglines)
957 (push (match-string 1 loglines) vc-bzr-revisions)
958 (setq start (+ start (match-end 0)))
959 (setq loglines (buffer-substring-no-properties start (point-max))))))
960 vc-bzr-revisions))
961
962 ;;; Revision completion
963
964 (eval-and-compile
965 (defconst vc-bzr-revision-keywords
966 '("revno" "revid" "last" "before"
967 "tag" "date" "ancestor" "branch" "submit")))
968
969 (defun vc-bzr-revision-completion-table (files)
970 (lexical-let ((files files))
971 ;; What about using `files'?!? --Stef
972 (lambda (string pred action)
973 (cond
974 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
975 string)
976 (completion-table-with-context (substring string 0 (match-end 0))
977 'completion-file-name-table
978 (substring string (match-end 0))
979 ;; Dropping `pred' for no good reason.
980 'file-directory-p
981 action))
982 ((string-match "\\`\\(before\\):" string)
983 (completion-table-with-context (substring string 0 (match-end 0))
984 (vc-bzr-revision-completion-table files)
985 (substring string (match-end 0))
986 pred
987 action))
988 ((string-match "\\`\\(tag\\):" string)
989 (let ((prefix (substring string 0 (match-end 0)))
990 (tag (substring string (match-end 0)))
991 (table nil)
992 process-file-side-effects)
993 (with-temp-buffer
994 ;; "bzr-1.2 tags" is much faster with --show-ids.
995 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
996 ;; The output is ambiguous, unless we assume that revids do not
997 ;; contain spaces.
998 (goto-char (point-min))
999 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
1000 (push (match-string-no-properties 1) table)))
1001 (completion-table-with-context prefix table tag pred action)))
1002
1003 ((string-match "\\`\\([a-z]+\\):" string)
1004 ;; no actual completion for the remaining keywords.
1005 (completion-table-with-context (substring string 0 (match-end 0))
1006 (if (member (match-string 1 string)
1007 vc-bzr-revision-keywords)
1008 ;; If it's a valid keyword,
1009 ;; use a non-empty table to
1010 ;; indicate it.
1011 '("") nil)
1012 (substring string (match-end 0))
1013 pred
1014 action))
1015 (t
1016 ;; Could use completion-table-with-terminator, except that it
1017 ;; currently doesn't work right w.r.t pcm and doesn't give
1018 ;; the *Completions* output we want.
1019 (complete-with-action action (eval-when-compile
1020 (mapcar (lambda (s) (concat s ":"))
1021 vc-bzr-revision-keywords))
1022 string pred))))))
1023
1024 (eval-after-load "vc"
1025 '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t))
1026
1027 (provide 'vc-bzr)
1028 ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
1029 ;;; vc-bzr.el ends here