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