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