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