99b70b068822e3b1f97b7056356f9163d5adc994
[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 (when (cdr result)
439 (message "Warnings in `bzr' output: %s" (cdr result)))
440 (cdr (assq (car result)
441 '((added . added)
442 (kindchanged . edited)
443 (renamed . edited)
444 (modified . edited)
445 (removed . removed)
446 (ignored . ignored)
447 (unknown . unregistered)
448 (unchanged . up-to-date)))))))
449
450 (defun vc-bzr-resolve-when-done ()
451 "Call \"bzr resolve\" if the conflict markers have been removed."
452 (save-excursion
453 (goto-char (point-min))
454 (unless (re-search-forward "^<<<<<<< " nil t)
455 (vc-bzr-command "resolve" nil 0 buffer-file-name)
456 ;; Remove the hook so that it is not called multiple times.
457 (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t))))
458
459 (defun vc-bzr-find-file-hook ()
460 (when (and buffer-file-name
461 ;; FIXME: We should check that "bzr status" says "conflict".
462 (file-exists-p (concat buffer-file-name ".BASE"))
463 (file-exists-p (concat buffer-file-name ".OTHER"))
464 (file-exists-p (concat buffer-file-name ".THIS"))
465 ;; If "bzr status" says there's a conflict but there are no
466 ;; conflict markers, it's not clear what we should do.
467 (save-excursion
468 (goto-char (point-min))
469 (re-search-forward "^<<<<<<< " nil t)))
470 ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
471 ;; but the one in `bzr pull' isn't, so it would be good to provide an
472 ;; elisp function to remerge from the .BASE/OTHER/THIS files.
473 (smerge-start-session)
474 (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t)
475 (message "There are unresolved conflicts in this file")))
476
477 (defun vc-bzr-workfile-unchanged-p (file)
478 (eq 'unchanged (car (vc-bzr-status file))))
479
480 (defun vc-bzr-working-revision (file)
481 ;; Together with the code in vc-state-heuristic, this makes it possible
482 ;; to get the initial VC state of a Bzr file even if Bzr is not installed.
483 (lexical-let*
484 ((rootdir (vc-bzr-root file))
485 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
486 rootdir))
487 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
488 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
489 ;; This looks at internal files to avoid forking a bzr process.
490 ;; May break if they change their format.
491 (if (and (file-exists-p branch-format-file)
492 ;; For lightweight checkouts (obtained with bzr checkout --lightweight)
493 ;; the branch-format-file does not contain the revision
494 ;; information, we need to look up the branch-format-file
495 ;; in the place where the lightweight checkout comes
496 ;; from. We only do that if it's a local file.
497 (let ((location-fname (expand-file-name
498 (concat vc-bzr-admin-dirname
499 "/branch/location") rootdir)))
500 ;; The existence of this file is how we distinguish
501 ;; lightweight checkouts.
502 (if (file-exists-p location-fname)
503 (with-temp-buffer
504 (insert-file-contents location-fname)
505 ;; If the lightweight checkout points to a
506 ;; location in the local file system, then we can
507 ;; look there for the version information.
508 (when (re-search-forward "file://\\(.+\\)" nil t)
509 (let ((l-c-parent-dir (match-string 1)))
510 (when (and (memq system-type '(ms-dos windows-nt))
511 (string-match-p "^/[[:alpha:]]:" l-c-parent-dir))
512 ;;; The non-Windows code takes a shortcut by using the host/path
513 ;;; separator slash as the start of the absolute path. That
514 ;;; does not work on Windows, so we must remove it (bug#5345)
515 (setq l-c-parent-dir (substring l-c-parent-dir 1)))
516 (setq branch-format-file
517 (expand-file-name vc-bzr-admin-branch-format-file
518 l-c-parent-dir))
519 (setq lastrev-file
520 (expand-file-name vc-bzr-admin-lastrev l-c-parent-dir))
521 ;; FIXME: maybe it's overkill to check if both these files exist.
522 (and (file-exists-p branch-format-file)
523 (file-exists-p lastrev-file)))))
524 t)))
525 (with-temp-buffer
526 (insert-file-contents branch-format-file)
527 (goto-char (point-min))
528 (cond
529 ((or
530 (looking-at "Bazaar-NG branch, format 0.0.4")
531 (looking-at "Bazaar-NG branch format 5"))
532 ;; count lines in .bzr/branch/revision-history
533 (insert-file-contents revhistory-file)
534 (number-to-string (count-lines (line-end-position) (point-max))))
535 ((or
536 (looking-at "Bazaar Branch Format 6 (bzr 0.15)")
537 (looking-at "Bazaar Branch Format 7 (needs bzr 1.6)"))
538 ;; revno is the first number in .bzr/branch/last-revision
539 (insert-file-contents lastrev-file)
540 (when (re-search-forward "[0-9]+" nil t)
541 (buffer-substring (match-beginning 0) (match-end 0))))))
542 ;; fallback to calling "bzr revno"
543 (lexical-let*
544 ((result (vc-bzr-command-discarding-stderr
545 vc-bzr-program "revno" (file-relative-name file)))
546 (exitcode (car result))
547 (output (cdr result)))
548 (cond
549 ((eq exitcode 0) (substring output 0 -1))
550 (t nil))))))
551
552 (defun vc-bzr-create-repo ()
553 "Create a new Bzr repository."
554 (vc-bzr-command "init" nil 0 nil))
555
556 (defun vc-bzr-init-revision (&optional file)
557 "Always return nil, as Bzr cannot register explicit versions."
558 nil)
559
560 (defun vc-bzr-previous-revision (file rev)
561 (if (string-match "\\`[0-9]+\\'" rev)
562 (number-to-string (1- (string-to-number rev)))
563 (concat "before:" rev)))
564
565 (defun vc-bzr-next-revision (file rev)
566 (if (string-match "\\`[0-9]+\\'" rev)
567 (number-to-string (1+ (string-to-number rev)))
568 (error "Don't know how to compute the next revision of %s" rev)))
569
570 (defun vc-bzr-register (files &optional rev comment)
571 "Register FILES under bzr.
572 Signal an error unless REV is nil.
573 COMMENT is ignored."
574 (if rev (error "Can't register explicit revision with bzr"))
575 (vc-bzr-command "add" nil 0 files))
576
577 ;; Could run `bzr status' in the directory and see if it succeeds, but
578 ;; that's relatively expensive.
579 (defalias 'vc-bzr-responsible-p 'vc-bzr-root
580 "Return non-nil if FILE is (potentially) controlled by bzr.
581 The criterion is that there is a `.bzr' directory in the same
582 or a superior directory.")
583
584 (defun vc-bzr-could-register (file)
585 "Return non-nil if FILE could be registered under bzr."
586 (and (vc-bzr-responsible-p file) ; shortcut
587 (condition-case ()
588 (with-temp-buffer
589 (vc-bzr-command "add" t 0 file "--dry-run")
590 ;; The command succeeds with no output if file is
591 ;; registered (in bzr 0.8).
592 (goto-char (point-min))
593 (looking-at "added "))
594 (error))))
595
596 (defun vc-bzr-unregister (file)
597 "Unregister FILE from bzr."
598 (vc-bzr-command "remove" nil 0 file "--keep"))
599
600 (declare-function log-edit-extract-headers "log-edit" (headers string))
601
602 (defun vc-bzr-checkin (files rev comment)
603 "Check FILES in to bzr with log message COMMENT.
604 REV non-nil gets an error."
605 (if rev (error "Can't check in a specific revision with bzr"))
606 (apply 'vc-bzr-command "commit" nil 0
607 files (cons "-m" (log-edit-extract-headers '(("Author" . "--author")
608 ("Date" . "--commit-time")
609 ("Fixes" . "--fixes"))
610 comment))))
611
612 (defun vc-bzr-find-revision (file rev buffer)
613 "Fetch revision REV of file FILE and put it into BUFFER."
614 (with-current-buffer buffer
615 (if (and rev (stringp rev) (not (string= rev "")))
616 (vc-bzr-command "cat" t 0 file "-r" rev)
617 (vc-bzr-command "cat" t 0 file))))
618
619 (defun vc-bzr-checkout (file &optional editable rev)
620 (if rev (error "Operation not supported")
621 ;; Else, there's nothing to do.
622 nil))
623
624 (defun vc-bzr-revert (file &optional contents-done)
625 (unless contents-done
626 (with-temp-buffer (vc-bzr-command "revert" t 0 file))))
627
628 (defvar log-view-message-re)
629 (defvar log-view-file-re)
630 (defvar log-view-font-lock-keywords)
631 (defvar log-view-current-tag-function)
632 (defvar log-view-per-file-logs)
633 (defvar log-view-expanded-log-entry-function)
634
635 (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
636 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
637 (require 'add-log)
638 (set (make-local-variable 'log-view-per-file-logs) nil)
639 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
640 (set (make-local-variable 'log-view-message-re)
641 (if (eq vc-log-view-type 'short)
642 "^ *\\([0-9.]+\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\( \\[merge\\]\\)?"
643 "^ *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)"))
644 ;; Allow expanding short log entries
645 (when (eq vc-log-view-type 'short)
646 (setq truncate-lines t)
647 (set (make-local-variable 'log-view-expanded-log-entry-function)
648 'vc-bzr-expanded-log-entry))
649 (set (make-local-variable 'log-view-font-lock-keywords)
650 ;; log-view-font-lock-keywords is careful to use the buffer-local
651 ;; value of log-view-message-re only since Emacs-23.
652 (if (eq vc-log-view-type 'short)
653 (append `((,log-view-message-re
654 (1 'log-view-message-face)
655 (2 'change-log-name)
656 (3 'change-log-date)
657 (4 'change-log-list nil lax))))
658 (append `((,log-view-message-re . 'log-view-message-face))
659 ;; log-view-font-lock-keywords
660 '(("^ *\\(?:committer\\|author\\): \
661 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
662 (1 'change-log-name)
663 (2 'change-log-email))
664 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))))
665
666 (defun vc-bzr-print-log (files buffer &optional shortlog start-revision limit)
667 "Get bzr change log for FILES into specified BUFFER."
668 ;; `vc-do-command' creates the buffer, but we need it before running
669 ;; the command.
670 (vc-setup-buffer buffer)
671 ;; If the buffer exists from a previous invocation it might be
672 ;; read-only.
673 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
674 ;; the log display may not what the user wants - but I see no other
675 ;; way of getting the above regexps working.
676 (with-current-buffer buffer
677 (apply 'vc-bzr-command "log" buffer 'async files
678 (append
679 (when shortlog '("--line"))
680 (when start-revision (list (format "-r..%s" start-revision)))
681 (when limit (list "-l" (format "%s" limit)))
682 (if (stringp vc-bzr-log-switches)
683 (list vc-bzr-log-switches)
684 vc-bzr-log-switches)))))
685
686 (defun vc-bzr-expanded-log-entry (revision)
687 (with-temp-buffer
688 (apply 'vc-bzr-command "log" t nil nil
689 (list (format "-r%s" revision)))
690 (goto-char (point-min))
691 (when (looking-at "^-+\n")
692 ;; Indent the expanded log entry.
693 (indent-region (match-end 0) (point-max) 2)
694 (buffer-substring (match-end 0) (point-max)))))
695
696 (defun vc-bzr-log-incoming (buffer remote-location)
697 (apply 'vc-bzr-command "missing" buffer 'async nil
698 (list "--theirs-only" (unless (string= remote-location "") remote-location))))
699
700 (defun vc-bzr-log-outgoing (buffer remote-location)
701 (apply 'vc-bzr-command "missing" buffer 'async nil
702 (list "--mine-only" (unless (string= remote-location "") remote-location))))
703
704 (defun vc-bzr-show-log-entry (revision)
705 "Find entry for patch name REVISION in bzr change log buffer."
706 (goto-char (point-min))
707 (when revision
708 (let (case-fold-search
709 found)
710 (if (re-search-forward
711 ;; "revno:" can appear either at the beginning of a line,
712 ;; or indented.
713 (concat "^[ ]*-+\n[ ]*revno: "
714 ;; The revision can contain ".", quote it so that it
715 ;; does not interfere with regexp matching.
716 (regexp-quote revision) "$") nil t)
717 (progn
718 (beginning-of-line 0)
719 (setq found t))
720 (goto-char (point-min)))
721 found)))
722
723 (defun vc-bzr-diff (files &optional rev1 rev2 buffer)
724 "VC bzr backend for diff."
725 (let* ((switches (vc-switches 'bzr 'diff))
726 (args
727 (append
728 ;; Only add --diff-options if there are any diff switches.
729 (unless (zerop (length switches))
730 (list "--diff-options" (mapconcat 'identity switches " ")))
731 ;; This `when' is just an optimization because bzr-1.2 is *much*
732 ;; faster when the revision argument is not given.
733 (when (or rev1 rev2)
734 (list "-r" (format "%s..%s"
735 (or rev1 "revno:-1")
736 (or rev2 "")))))))
737 ;; `bzr diff' exits with code 1 if diff is non-empty.
738 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*")
739 (if vc-disable-async-diff 1 'async) files
740 args)))
741
742
743 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
744 ;; straight integer revisions.
745
746 (defun vc-bzr-delete-file (file)
747 "Delete FILE and delete it in the bzr repository."
748 (condition-case ()
749 (delete-file file)
750 (file-error nil))
751 (vc-bzr-command "remove" nil 0 file))
752
753 (defun vc-bzr-rename-file (old new)
754 "Rename file from OLD to NEW using `bzr mv'."
755 (vc-bzr-command "mv" nil 0 new old))
756
757 (defvar vc-bzr-annotation-table nil
758 "Internal use.")
759 (make-variable-buffer-local 'vc-bzr-annotation-table)
760
761 (defun vc-bzr-annotate-command (file buffer &optional revision)
762 "Prepare BUFFER for `vc-annotate' on FILE.
763 Each line is tagged with the revision number, which has a `help-echo'
764 property containing author and date information."
765 (apply #'vc-bzr-command "annotate" buffer 'async file "--long" "--all"
766 (if revision (list "-r" revision)))
767 (lexical-let ((table (make-hash-table :test 'equal)))
768 (set-process-filter
769 (get-buffer-process buffer)
770 (lambda (proc string)
771 (when (process-buffer proc)
772 (with-current-buffer (process-buffer proc)
773 (setq string (concat (process-get proc :vc-left-over) string))
774 ;; Eg: 102020 Gnus developers 20101020 | regexp."
775 ;; As of bzr 2.2.2, no email address in whoami (which can
776 ;; lead to spaces in the author field) is allowed but discouraged.
777 ;; See bug#7792.
778 (while (string-match "^\\( *[0-9.]+ *\\) \\(.+?\\) +\\([0-9]\\{8\\}\\)\\( |.*\n\\)" string)
779 (let* ((rev (match-string 1 string))
780 (author (match-string 2 string))
781 (date (match-string 3 string))
782 (key (substring string (match-beginning 0)
783 (match-beginning 4)))
784 (line (match-string 4 string))
785 (tag (gethash key table))
786 (inhibit-read-only t))
787 (setq string (substring string (match-end 0)))
788 (unless tag
789 (setq tag
790 (propertize
791 (format "%s %-7.7s" rev author)
792 'help-echo (format "Revision: %d, author: %s, date: %s"
793 (string-to-number rev)
794 author date)
795 'mouse-face 'highlight))
796 (puthash key tag table))
797 (goto-char (process-mark proc))
798 (insert tag line)
799 (move-marker (process-mark proc) (point))))
800 (process-put proc :vc-left-over string)))))))
801
802 (declare-function vc-annotate-convert-time "vc-annotate" (time))
803
804 (defun vc-bzr-annotate-time ()
805 (when (re-search-forward "^ *[0-9.]+ +.+? +|" nil t)
806 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
807 (string-match "[0-9]+\\'" prop)
808 (let ((str (match-string-no-properties 0 prop)))
809 (vc-annotate-convert-time
810 (encode-time 0 0 0
811 (string-to-number (substring str 6 8))
812 (string-to-number (substring str 4 6))
813 (string-to-number (substring str 0 4))))))))
814
815 (defun vc-bzr-annotate-extract-revision-at-line ()
816 "Return revision for current line of annotation buffer, or nil.
817 Return nil if current line isn't annotated."
818 (save-excursion
819 (beginning-of-line)
820 (if (looking-at "^ *\\([0-9.]+\\) +.* +|")
821 (match-string-no-properties 1))))
822
823 (defun vc-bzr-command-discarding-stderr (command &rest args)
824 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
825 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
826 the (numerical) exit code of the process, and OUTPUT is a string
827 containing whatever the process sent to its standard output
828 stream. Standard error output is discarded."
829 (with-temp-buffer
830 (cons
831 (apply #'process-file command nil (list (current-buffer) nil) nil args)
832 (buffer-substring (point-min) (point-max)))))
833
834 (defstruct (vc-bzr-extra-fileinfo
835 (:copier nil)
836 (:constructor vc-bzr-create-extra-fileinfo (extra-name))
837 (:conc-name vc-bzr-extra-fileinfo->))
838 extra-name) ;; original name for rename targets, new name for
839
840 (defun vc-bzr-dir-printer (info)
841 "Pretty-printer for the vc-dir-fileinfo structure."
842 (let ((extra (vc-dir-fileinfo->extra info)))
843 (vc-default-dir-printer 'Bzr info)
844 (when extra
845 (insert (propertize
846 (format " (renamed from %s)"
847 (vc-bzr-extra-fileinfo->extra-name extra))
848 'face 'font-lock-comment-face)))))
849
850 ;; FIXME: this needs testing, it's probably incomplete.
851 (defun vc-bzr-after-dir-status (update-function relative-dir)
852 (let ((status-str nil)
853 (translation '(("+N " . added)
854 ("-D " . removed)
855 (" M " . edited) ;; file text modified
856 (" *" . edited) ;; execute bit changed
857 (" M*" . edited) ;; text modified + execute bit changed
858 ;; FIXME: what about ignored files?
859 (" D " . missing)
860 ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
861 ("C " . conflict)
862 ("? " . unregistered)
863 ;; No such state, but we need to distinguish this case.
864 ("R " . renamed)
865 ("RM " . renamed)
866 ;; For a non existent file FOO, the output is:
867 ;; bzr: ERROR: Path(s) do not exist: FOO
868 ("bzr" . not-found)
869 ;; If the tree is not up to date, bzr will print this warning:
870 ;; working tree is out of date, run 'bzr update'
871 ;; ignore it.
872 ;; FIXME: maybe this warning can be put in the vc-dir header...
873 ("wor" . not-found)
874 ;; Ignore "P " and "P." for pending patches.
875 ("P " . not-found)
876 ("P. " . not-found)
877 ))
878 (translated nil)
879 (result nil))
880 (goto-char (point-min))
881 (while (not (eobp))
882 (setq status-str
883 (buffer-substring-no-properties (point) (+ (point) 3)))
884 (setq translated (cdr (assoc status-str translation)))
885 (cond
886 ((eq translated 'conflict)
887 ;; For conflicts the file appears twice in the listing: once
888 ;; with the M flag and once with the C flag, so take care
889 ;; not to add it twice to `result'. Ugly.
890 (let* ((file
891 (buffer-substring-no-properties
892 ;;For files with conflicts the format is:
893 ;;C Text conflict in FILENAME
894 ;; Bah.
895 (+ (point) 21) (line-end-position)))
896 (entry (assoc file result)))
897 (when entry
898 (setf (nth 1 entry) 'conflict))))
899 ((eq translated 'renamed)
900 (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t)
901 (let ((new-name (file-relative-name (match-string 2) relative-dir))
902 (old-name (file-relative-name (match-string 1) relative-dir)))
903 (push (list new-name 'edited
904 (vc-bzr-create-extra-fileinfo old-name)) result)))
905 ;; do nothing for non existent files
906 ((eq translated 'not-found))
907 (t
908 (push (list (file-relative-name
909 (buffer-substring-no-properties
910 (+ (point) 4)
911 (line-end-position)) relative-dir)
912 translated) result)))
913 (forward-line))
914 (funcall update-function result)))
915
916 (defun vc-bzr-dir-status (dir update-function)
917 "Return a list of conses (file . state) for DIR."
918 (vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S")
919 (vc-exec-after
920 `(vc-bzr-after-dir-status (quote ,update-function)
921 ;; "bzr status" results are relative to
922 ;; the bzr root directory, NOT to the
923 ;; directory "bzr status" was invoked in.
924 ;; Ugh.
925 ;; We pass the relative directory here so
926 ;; that `vc-bzr-after-dir-status' can
927 ;; frob the results accordingly.
928 (file-relative-name ,dir (vc-bzr-root ,dir)))))
929
930 (defun vc-bzr-dir-status-files (dir files default-state update-function)
931 "Return a list of conses (file . state) for DIR."
932 (apply 'vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S" files)
933 (vc-exec-after
934 `(vc-bzr-after-dir-status (quote ,update-function)
935 (file-relative-name ,dir (vc-bzr-root ,dir)))))
936
937 (defvar vc-bzr-shelve-map
938 (let ((map (make-sparse-keymap)))
939 ;; Turn off vc-dir marking
940 (define-key map [mouse-2] 'ignore)
941
942 (define-key map [down-mouse-3] 'vc-bzr-shelve-menu)
943 (define-key map "\C-k" 'vc-bzr-shelve-delete-at-point)
944 (define-key map "=" 'vc-bzr-shelve-show-at-point)
945 (define-key map "\C-m" 'vc-bzr-shelve-show-at-point)
946 (define-key map "A" 'vc-bzr-shelve-apply-and-keep-at-point)
947 (define-key map "P" 'vc-bzr-shelve-apply-at-point)
948 (define-key map "S" 'vc-bzr-shelve-snapshot)
949 map))
950
951 (defvar vc-bzr-shelve-menu-map
952 (let ((map (make-sparse-keymap "Bzr Shelve")))
953 (define-key map [de]
954 '(menu-item "Delete shelf" vc-bzr-shelve-delete-at-point
955 :help "Delete the current shelf"))
956 (define-key map [ap]
957 '(menu-item "Apply and keep shelf" vc-bzr-shelve-apply-and-keep-at-point
958 :help "Apply the current shelf and keep it"))
959 (define-key map [po]
960 '(menu-item "Apply and remove shelf (pop)" vc-bzr-shelve-apply-at-point
961 :help "Apply the current shelf and remove it"))
962 (define-key map [sh]
963 '(menu-item "Show shelve" vc-bzr-shelve-show-at-point
964 :help "Show the contents of the current shelve"))
965 map))
966
967 (defvar vc-bzr-extra-menu-map
968 (let ((map (make-sparse-keymap)))
969 (define-key map [bzr-sn]
970 '(menu-item "Shelve a snapshot" vc-bzr-shelve-snapshot
971 :help "Shelve the current state of the tree and keep the current state"))
972 (define-key map [bzr-sh]
973 '(menu-item "Shelve..." vc-bzr-shelve
974 :help "Shelve changes"))
975 map))
976
977 (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
978
979 (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
980
981 (defun vc-bzr-dir-extra-headers (dir)
982 (let*
983 ((str (with-temp-buffer
984 (vc-bzr-command "info" t 0 dir)
985 (buffer-string)))
986 (shelve (vc-bzr-shelve-list))
987 (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
988 (root-dir (vc-bzr-root dir))
989 (pending-merge
990 ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
991 ;; reliable method to detect pending merges, disable this
992 ;; until a proper solution is implemented.
993 (and nil
994 (file-exists-p
995 (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
996 (pending-merge-help-echo
997 (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))
998 (light-checkout
999 (when (string-match ".+light checkout root: \\(.+\\)$" str)
1000 (match-string 1 str)))
1001 (light-checkout-branch
1002 (when light-checkout
1003 (when (string-match ".+checkout of branch: \\(.+\\)$" str)
1004 (match-string 1 str)))))
1005 (concat
1006 (propertize "Parent branch : " 'face 'font-lock-type-face)
1007 (propertize
1008 (if (string-match "parent branch: \\(.+\\)$" str)
1009 (match-string 1 str)
1010 "None")
1011 'face 'font-lock-variable-name-face)
1012 "\n"
1013 (when light-checkout
1014 (concat
1015 (propertize "Light checkout root: " 'face 'font-lock-type-face)
1016 (propertize light-checkout 'face 'font-lock-variable-name-face)
1017 "\n"))
1018 (when light-checkout-branch
1019 (concat
1020 (propertize "Checkout of branch : " 'face 'font-lock-type-face)
1021 (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
1022 "\n"))
1023 (when pending-merge
1024 (concat
1025 (propertize "Warning : " 'face 'font-lock-warning-face
1026 'help-echo pending-merge-help-echo)
1027 (propertize "Pending merges, commit recommended before any other action"
1028 'help-echo pending-merge-help-echo
1029 'face 'font-lock-warning-face)
1030 "\n"))
1031 (if shelve
1032 (concat
1033 (propertize "Shelves :\n" 'face 'font-lock-type-face
1034 'help-echo shelve-help-echo)
1035 (mapconcat
1036 (lambda (x)
1037 (propertize x
1038 'face 'font-lock-variable-name-face
1039 'mouse-face 'highlight
1040 '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"
1041 'keymap vc-bzr-shelve-map))
1042 shelve "\n"))
1043 (concat
1044 (propertize "Shelves : " 'face 'font-lock-type-face
1045 'help-echo shelve-help-echo)
1046 (propertize "No shelved changes"
1047 'help-echo shelve-help-echo
1048 'face 'font-lock-variable-name-face))))))
1049
1050 (defun vc-bzr-shelve (name)
1051 "Create a shelve."
1052 (interactive "sShelf name: ")
1053 (let ((root (vc-bzr-root default-directory)))
1054 (when root
1055 (vc-bzr-command "shelve" nil 0 nil "--all" "-m" name)
1056 (vc-resynch-buffer root t t))))
1057
1058 (defun vc-bzr-shelve-show (name)
1059 "Show the contents of shelve NAME."
1060 (interactive "sShelve name: ")
1061 (vc-setup-buffer "*vc-diff*")
1062 ;; FIXME: how can you show the contents of a shelf?
1063 (vc-bzr-command "unshelve" "*vc-diff*" 'async nil "--preview" name)
1064 (set-buffer "*vc-diff*")
1065 (diff-mode)
1066 (setq buffer-read-only t)
1067 (pop-to-buffer (current-buffer)))
1068
1069 (defun vc-bzr-shelve-apply (name)
1070 "Apply shelve NAME and remove it afterwards."
1071 (interactive "sApply (and remove) shelf: ")
1072 (vc-bzr-command "unshelve" nil 0 nil "--apply" name)
1073 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1074
1075 (defun vc-bzr-shelve-apply-and-keep (name)
1076 "Apply shelve NAME and keep it afterwards."
1077 (interactive "sApply (and keep) shelf: ")
1078 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep" name)
1079 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1080
1081 (defun vc-bzr-shelve-snapshot ()
1082 "Create a stash with the current tree state."
1083 (interactive)
1084 (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
1085 (let ((ct (current-time)))
1086 (concat
1087 (format-time-string "Snapshot on %Y-%m-%d" ct)
1088 (format-time-string " at %H:%M" ct))))
1089 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep")
1090 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1091
1092 (defun vc-bzr-shelve-list ()
1093 (with-temp-buffer
1094 (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
1095 (delete
1096 ""
1097 (split-string
1098 (buffer-substring (point-min) (point-max))
1099 "\n"))))
1100
1101 (defun vc-bzr-shelve-get-at-point (point)
1102 (save-excursion
1103 (goto-char point)
1104 (beginning-of-line)
1105 (if (looking-at "^ +\\([0-9]+\\):")
1106 (match-string 1)
1107 (error "Cannot find shelf at point"))))
1108
1109 (defun vc-bzr-shelve-delete-at-point ()
1110 (interactive)
1111 (let ((shelve (vc-bzr-shelve-get-at-point (point))))
1112 (when (y-or-n-p (format "Remove shelf %s ? " shelve))
1113 (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
1114 (vc-dir-refresh))))
1115
1116 (defun vc-bzr-shelve-show-at-point ()
1117 (interactive)
1118 (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
1119
1120 (defun vc-bzr-shelve-apply-at-point ()
1121 (interactive)
1122 (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
1123
1124 (defun vc-bzr-shelve-apply-and-keep-at-point ()
1125 (interactive)
1126 (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
1127
1128 (defun vc-bzr-shelve-menu (e)
1129 (interactive "e")
1130 (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
1131
1132 (defun vc-bzr-revision-table (files)
1133 (let ((vc-bzr-revisions '())
1134 (default-directory (file-name-directory (car files))))
1135 (with-temp-buffer
1136 (vc-bzr-command "log" t 0 files "--line")
1137 (let ((start (point-min))
1138 (loglines (buffer-substring-no-properties (point-min) (point-max))))
1139 (while (string-match "^\\([0-9]+\\):" loglines)
1140 (push (match-string 1 loglines) vc-bzr-revisions)
1141 (setq start (+ start (match-end 0)))
1142 (setq loglines (buffer-substring-no-properties start (point-max))))))
1143 vc-bzr-revisions))
1144
1145 (defun vc-bzr-conflicted-files (dir)
1146 (let ((default-directory (vc-bzr-root dir))
1147 (files ()))
1148 (with-temp-buffer
1149 (vc-bzr-command "status" t 0 default-directory)
1150 (goto-char (point-min))
1151 (when (re-search-forward "^conflicts:\n" nil t)
1152 (while (looking-at " \\(?:Text conflict in \\(.*\\)\\|.*\\)\n")
1153 (if (match-end 1)
1154 (push (expand-file-name (match-string 1)) files))
1155 (goto-char (match-end 0)))))
1156 files))
1157
1158 ;;; Revision completion
1159
1160 (eval-and-compile
1161 (defconst vc-bzr-revision-keywords
1162 '("revno" "revid" "last" "before"
1163 "tag" "date" "ancestor" "branch" "submit")))
1164
1165 (defun vc-bzr-revision-completion-table (files)
1166 (lexical-let ((files files))
1167 ;; What about using `files'?!? --Stef
1168 (lambda (string pred action)
1169 (cond
1170 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
1171 string)
1172 (completion-table-with-context (substring string 0 (match-end 0))
1173 (apply-partially
1174 'completion-table-with-predicate
1175 'completion-file-name-table
1176 'file-directory-p t)
1177 (substring string (match-end 0))
1178 pred
1179 action))
1180 ((string-match "\\`\\(before\\):" string)
1181 (completion-table-with-context (substring string 0 (match-end 0))
1182 (vc-bzr-revision-completion-table files)
1183 (substring string (match-end 0))
1184 pred
1185 action))
1186 ((string-match "\\`\\(tag\\):" string)
1187 (let ((prefix (substring string 0 (match-end 0)))
1188 (tag (substring string (match-end 0)))
1189 (table nil)
1190 process-file-side-effects)
1191 (with-temp-buffer
1192 ;; "bzr-1.2 tags" is much faster with --show-ids.
1193 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
1194 ;; The output is ambiguous, unless we assume that revids do not
1195 ;; contain spaces.
1196 (goto-char (point-min))
1197 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
1198 (push (match-string-no-properties 1) table)))
1199 (completion-table-with-context prefix table tag pred action)))
1200
1201 ((string-match "\\`\\([a-z]+\\):" string)
1202 ;; no actual completion for the remaining keywords.
1203 (completion-table-with-context (substring string 0 (match-end 0))
1204 (if (member (match-string 1 string)
1205 vc-bzr-revision-keywords)
1206 ;; If it's a valid keyword,
1207 ;; use a non-empty table to
1208 ;; indicate it.
1209 '("") nil)
1210 (substring string (match-end 0))
1211 pred
1212 action))
1213 (t
1214 ;; Could use completion-table-with-terminator, except that it
1215 ;; currently doesn't work right w.r.t pcm and doesn't give
1216 ;; the *Completions* output we want.
1217 (complete-with-action action (eval-when-compile
1218 (mapcar (lambda (s) (concat s ":"))
1219 vc-bzr-revision-keywords))
1220 string pred))))))
1221
1222 (provide 'vc-bzr)
1223
1224 ;;; vc-bzr.el ends here