Merge branch 'master' into comment-cache
[emacs.git] / lisp / vc / vc-bzr.el
blob085c05bcc05529ade77e762099d05e30549c2d12
1 ;;; vc-bzr.el --- VC backend for the bzr revision control system -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2017 Free Software Foundation, Inc.
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
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;;; Commentary:
29 ;; See <URL:http://bazaar.canonical.com/> concerning bzr.
31 ;; This library provides bzr support in VC.
33 ;; Known bugs
34 ;; ==========
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.
41 ;;; Properties of the backend
43 (defun vc-bzr-revision-granularity () 'repository)
44 (defun vc-bzr-checkout-model (_files) 'implicit)
46 ;;; Code:
48 (eval-when-compile
49 (require 'cl-lib)
50 (require 'vc-dispatcher)
51 (require 'vc-dir)) ; vc-dir-at-event
53 (declare-function vc-deduce-fileset "vc"
54 (&optional observer allow-unregistered
55 state-model-only-files))
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)
62 (defgroup vc-bzr nil
63 "VC Bazaar (bzr) backend."
64 :version "22.2"
65 :group 'vc)
67 (defcustom vc-bzr-program "bzr"
68 "Name of the bzr command (excluding any arguments)."
69 :group 'vc-bzr
70 :type 'string)
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)
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)
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)
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")
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))))
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))
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"))
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)))))
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.
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))
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))))
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.
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'."
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.
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
280 (mapcar
281 'identity
282 (nth 8 (file-attributes file))))))
283 (if (eq (char-after (match-beginning 7))
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))))))
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))))
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)))))
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.")
321 ;; History of Bzr commands.
322 (defvar vc-bzr-history nil)
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))))
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.")
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))
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
376 (vc-run-delayed
377 (vc-compilation-mode 'bzr)
378 (setq-local compile-command
379 (concat vc-bzr-program " " command " "
380 (if args (mapconcat 'identity args " ") "")))))
381 (vc-set-async-update buf))))
383 (defun vc-bzr-pull (prompt)
384 "Pull changes into the current Bzr branch.
385 Normally, this runs \"bzr pull\". However, if the branch is a
386 bound branch, run \"bzr update\" instead. If there is no default
387 location from which to pull or update, or if PROMPT is non-nil,
388 prompt for the Bzr command to run."
389 (vc-bzr--pushpull "pull" prompt))
391 (defun vc-bzr-push (prompt)
392 "Push changes from the current Bzr branch.
393 Normally, this runs \"bzr push\". If there is no push location,
394 or if PROMPT is non-nil, prompt for the Bzr command to run."
395 (vc-bzr--pushpull "push" prompt))
397 (defun vc-bzr-merge-branch ()
398 "Merge another Bzr branch into the current one.
399 Prompt for the Bzr command to run, providing a pre-defined merge
400 source (an upstream branch or a previous merge source) as a
401 default if it is available."
402 (let* ((branch-conf (vc-bzr-branch-conf default-directory))
403 ;; "bzr merge" without an argument defaults to submit_branch,
404 ;; then parent_location. Extract the specific location and
405 ;; add it explicitly to the command line.
406 (setting nil)
407 (location
408 (cond
409 ((setq setting (assoc "submit_branch" branch-conf))
410 (cdr setting))
411 ((setq setting (assoc "parent_location" branch-conf))
412 (cdr setting))))
413 (cmd
414 (split-string
415 (read-shell-command
416 "Bzr merge command: "
417 (concat vc-bzr-program " merge --pull"
418 (if location (concat " " location) ""))
419 'vc-bzr-history)
420 " " t))
421 (vc-bzr-program (car cmd))
422 (command (cadr cmd))
423 (args (cddr cmd)))
424 (let ((buf (apply 'vc-bzr-async-command command args)))
425 (with-current-buffer buf (vc-run-delayed (vc-compilation-mode 'bzr)))
426 (vc-set-async-update buf))))
428 (defun vc-bzr-status (file)
429 "Return FILE status according to Bzr.
430 Return value is a cons (STATUS . WARNING), where WARNING is a
431 string or nil, and STATUS is one of the symbols: `added',
432 `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
433 which directly correspond to `bzr status' output, or 'unchanged
434 for files whose copy in the working tree is identical to the one
435 in the branch repository (or whose status not be determined)."
436 ;; Doc used to also say the following, but AFAICS, it has never been true.
438 ;; ", or nil for files that are not registered with Bzr.
439 ;; If any error occurred in running `bzr status', then return nil."
441 ;; Rather than returning nil in case of an error, it returns
442 ;; (unchanged . WARNING). FIXME unchanged is not the best status to
443 ;; return in case of error.
444 (with-temp-buffer
445 ;; This is with-demoted-errors without the condition-case-unless-debug
446 ;; annoyance, which makes it fail during ert testing.
447 (condition-case err (vc-bzr-command "status" t 0 file)
448 (error (message "Error: %S" err) nil))
449 (let ((status 'unchanged))
450 ;; the only secure status indication in `bzr status' output
451 ;; is a couple of lines following the pattern::
452 ;; | <status>:
453 ;; | <file name>
454 ;; if the file is up-to-date, we get no status report from `bzr',
455 ;; so if the regexp search for the above pattern fails, we consider
456 ;; the file to be up-to-date.
457 (goto-char (point-min))
458 (when (re-search-forward
459 ;; bzr prints paths relative to the repository root.
460 (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
461 (regexp-quote (vc-bzr-file-name-relative file))
462 ;; Bzr appends a '/' to directory names and
463 ;; '*' to executable files
464 (if (file-directory-p file) "/?" "\\*?")
465 "[ \t\n]*$")
466 nil t)
467 (let ((statusword (match-string 1)))
468 ;; Erase the status text that matched.
469 (delete-region (match-beginning 0) (match-end 0))
470 (setq status
471 (intern (replace-regexp-in-string " " "" statusword)))))
472 (when status
473 (goto-char (point-min))
474 (skip-chars-forward " \n\t") ;Throw away spaces.
475 (cons status
476 ;; "bzr" will output warnings and informational messages to
477 ;; stderr; due to Emacs's `vc-do-command' (and, it seems,
478 ;; `start-process' itself) limitations, we cannot catch stderr
479 ;; and stdout into different buffers. So, if there's anything
480 ;; left in the buffer after removing the above status
481 ;; keywords, let us just presume that any other message from
482 ;; "bzr" is a user warning, and display it.
483 (unless (eobp) (buffer-substring (point) (point-max))))))))
485 (defun vc-bzr-state (file)
486 (let ((result (vc-bzr-status file)))
487 (when (consp result)
488 (let ((warnings (cdr result)))
489 (when warnings
490 ;; bzr 2.3.0 returns info about shelves, which is not really a warning
491 (when (string-match "[0-9]+ shel\\(f\\|ves\\) exists?\\..*?\n" warnings)
492 (setq warnings (replace-match "" nil nil warnings)))
493 (unless (string= warnings "")
494 (message "Warnings in `bzr' output: %s" warnings))))
495 (cdr (assq (car result)
496 '((added . added)
497 (kindchanged . edited)
498 (renamed . edited)
499 (modified . edited)
500 (removed . removed)
501 (ignored . ignored)
502 (unknown . unregistered)
503 (unchanged . up-to-date)))))))
505 (defun vc-bzr-resolve-when-done ()
506 "Call \"bzr resolve\" if the conflict markers have been removed."
507 (save-excursion
508 (goto-char (point-min))
509 (unless (re-search-forward "^<<<<<<< " nil t)
510 (vc-bzr-command "resolve" nil 0 buffer-file-name)
511 ;; Remove the hook so that it is not called multiple times.
512 (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t))))
514 (defun vc-bzr-find-file-hook ()
515 (when (and buffer-file-name
516 ;; FIXME: We should check that "bzr status" says "conflict".
517 (file-exists-p (concat buffer-file-name ".BASE"))
518 (file-exists-p (concat buffer-file-name ".OTHER"))
519 (file-exists-p (concat buffer-file-name ".THIS"))
520 ;; If "bzr status" says there's a conflict but there are no
521 ;; conflict markers, it's not clear what we should do.
522 (save-excursion
523 (goto-char (point-min))
524 (re-search-forward "^<<<<<<< " nil t)))
525 ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
526 ;; but the one in `bzr pull' isn't, so it would be good to provide an
527 ;; elisp function to remerge from the .BASE/OTHER/THIS files.
528 (smerge-start-session)
529 (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t)
530 (vc-message-unresolved-conflicts buffer-file-name)))
532 (defun vc-bzr-version-dirstate (dir)
533 "Try to return as a string the bzr revision ID of directory DIR.
534 This uses the dirstate file's parent revision entry.
535 Returns nil if unable to find this information."
536 (let ((file (expand-file-name ".bzr/checkout/dirstate" dir)))
537 (when (file-readable-p file)
538 (with-temp-buffer
539 (insert-file-contents file)
540 (and (looking-at "#bazaar dirstate flat format 3")
541 (forward-line 3)
542 (looking-at "[0-9]+\0\\([^\0\n]+\\)\0")
543 (match-string 1))))))
545 (defun vc-bzr-working-revision (file)
546 (let* ((rootdir (vc-bzr-root file))
547 (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
548 rootdir))
549 (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
550 (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
551 ;; This looks at internal files to avoid forking a bzr process.
552 ;; May break if they change their format.
553 (if (and (file-exists-p branch-format-file)
554 ;; For lightweight checkouts (obtained with bzr co --lightweight)
555 ;; the branch-format-file does not contain the revision
556 ;; information, we need to look up the branch-format-file
557 ;; in the place where the lightweight checkout comes
558 ;; from. We only do that if it's a local file.
559 (let ((location-fname (expand-file-name
560 (concat vc-bzr-admin-dirname
561 "/branch/location") rootdir)))
562 ;; The existence of this file is how we distinguish
563 ;; lightweight checkouts.
564 (if (file-exists-p location-fname)
565 (with-temp-buffer
566 (insert-file-contents location-fname)
567 ;; If the lightweight checkout points to a
568 ;; location in the local file system, then we can
569 ;; look there for the version information.
570 (when (re-search-forward "file://\\(.+\\)" nil t)
571 (let ((l-c-parent-dir (match-string 1)))
572 (when (and (memq system-type '(ms-dos windows-nt))
573 (string-match-p "^/[[:alpha:]]:"
574 l-c-parent-dir))
575 ;;; The non-Windows code takes a shortcut by using
576 ;;; the host/path separator slash as the start of
577 ;;; the absolute path. That does not work on
578 ;;; Windows, so we must remove it (bug#5345)
579 (setq l-c-parent-dir (substring l-c-parent-dir 1)))
580 (setq branch-format-file
581 (expand-file-name vc-bzr-admin-branch-format-file
582 l-c-parent-dir))
583 (setq lastrev-file
584 (expand-file-name vc-bzr-admin-lastrev
585 l-c-parent-dir))
586 ;; FIXME: maybe it's overkill to check if both these
587 ;; files exist.
588 (and (file-exists-p branch-format-file)
589 (file-exists-p lastrev-file)
590 (equal (vc-bzr-version-dirstate l-c-parent-dir)
591 (vc-bzr-version-dirstate rootdir))))))
592 t)))
593 (with-temp-buffer
594 (insert-file-contents branch-format-file)
595 (goto-char (point-min))
596 (cond
597 ((or
598 (looking-at "Bazaar-NG branch, format 0.0.4")
599 (looking-at "Bazaar-NG branch format 5"))
600 ;; count lines in .bzr/branch/revision-history
601 (insert-file-contents revhistory-file)
602 (number-to-string (count-lines (line-end-position) (point-max))))
603 ((or
604 (looking-at "Bazaar Branch Format 6 (bzr 0.15)")
605 (looking-at "Bazaar Branch Format 7 (needs bzr 1.6)"))
606 ;; revno is the first number in .bzr/branch/last-revision
607 (insert-file-contents lastrev-file)
608 (when (re-search-forward "[0-9]+" nil t)
609 (buffer-substring (match-beginning 0) (match-end 0))))))
610 ;; Fallback to calling "bzr revno --tree".
611 ;; The "--tree" matters for lightweight checkouts not on the same
612 ;; revision as the parent.
613 (let* ((result (vc-bzr-command-discarding-stderr
614 vc-bzr-program "revno" "--tree"
615 (file-relative-name file)))
616 (exitcode (car result))
617 (output (cdr result)))
618 (cond
619 ((and (eq exitcode 0) (not (zerop (length output))))
620 (substring output 0 -1))
621 (t nil))))))
623 (defun vc-bzr-create-repo ()
624 "Create a new Bzr repository."
625 (vc-bzr-command "init" nil 0 nil))
627 (defun vc-bzr-previous-revision (_file rev)
628 (if (string-match "\\`[0-9]+\\'" rev)
629 (number-to-string (1- (string-to-number rev)))
630 (concat "before:" rev)))
632 (defun vc-bzr-next-revision (_file rev)
633 (if (string-match "\\`[0-9]+\\'" rev)
634 (number-to-string (1+ (string-to-number rev)))
635 (error "Don't know how to compute the next revision of %s" rev)))
637 (defun vc-bzr-register (files &optional _comment)
638 "Register FILES under bzr. COMMENT is ignored."
639 (vc-bzr-command "add" nil 0 files))
641 ;; Could run `bzr status' in the directory and see if it succeeds, but
642 ;; that's relatively expensive.
643 (defalias 'vc-bzr-responsible-p 'vc-bzr-root
644 "Return non-nil if FILE is (potentially) controlled by bzr.
645 The criterion is that there is a `.bzr' directory in the same
646 or a superior directory.")
648 (defun vc-bzr-unregister (file)
649 "Unregister FILE from bzr."
650 (vc-bzr-command "remove" nil 0 file "--keep"))
652 (declare-function log-edit-extract-headers "log-edit" (headers string))
654 (defun vc-bzr--sanitize-header (arg)
655 ;; Newlines in --fixes (and probably other fields as well) trigger a nasty
656 ;; Bazaar bug; see https://bugs.launchpad.net/bzr/+bug/1094180.
657 (lambda (str) (list arg
658 (replace-regexp-in-string "\\`[ \t]+\\|[ \t]+\\'"
659 "" (replace-regexp-in-string
660 "\n[ \t]?" " " str)))))
662 (defun vc-bzr-checkin (files comment &optional _rev)
663 "Check FILES in to bzr with log message COMMENT."
664 (apply 'vc-bzr-command "commit" nil 0 files
665 (cons "-m" (log-edit-extract-headers
666 `(("Author" . ,(vc-bzr--sanitize-header "--author"))
667 ("Date" . ,(vc-bzr--sanitize-header "--commit-time"))
668 ("Fixes" . ,(vc-bzr--sanitize-header "--fixes")))
669 comment))))
671 (defun vc-bzr-find-revision (file rev buffer)
672 "Fetch revision REV of file FILE and put it into BUFFER."
673 (with-current-buffer buffer
674 (if (and rev (stringp rev) (not (string= rev "")))
675 (vc-bzr-command "cat" t 0 file "-r" rev)
676 (vc-bzr-command "cat" t 0 file))))
678 (defun vc-bzr-find-ignore-file (file)
679 "Return the root directory of the repository of FILE."
680 (expand-file-name ".bzrignore"
681 (vc-bzr-root file)))
683 (defun vc-bzr-checkout (_file &optional rev)
684 (if rev (error "Operation not supported")
685 ;; Else, there's nothing to do.
686 nil))
688 (defun vc-bzr-revert (file &optional contents-done)
689 (unless contents-done
690 (with-temp-buffer (vc-bzr-command "revert" t 0 file "--no-backup"))))
692 (defvar log-view-message-re)
693 (defvar log-view-file-re)
694 (defvar log-view-font-lock-keywords)
695 (defvar log-view-current-tag-function)
696 (defvar log-view-per-file-logs)
697 (defvar log-view-expanded-log-entry-function)
699 (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
700 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
701 (require 'add-log)
702 (set (make-local-variable 'log-view-per-file-logs) nil)
703 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
704 (set (make-local-variable 'log-view-message-re)
705 (if (eq vc-log-view-type 'short)
706 "^ *\\([0-9.]+\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\( \\[merge\\]\\)?"
707 "^ *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)"))
708 ;; Allow expanding short log entries
709 (when (eq vc-log-view-type 'short)
710 (setq truncate-lines t)
711 (set (make-local-variable 'log-view-expanded-log-entry-function)
712 'vc-bzr-expanded-log-entry))
713 (set (make-local-variable 'log-view-font-lock-keywords)
714 ;; log-view-font-lock-keywords is careful to use the buffer-local
715 ;; value of log-view-message-re only since Emacs-23.
716 (if (eq vc-log-view-type 'short)
717 (append `((,log-view-message-re
718 (1 'log-view-message)
719 (2 'change-log-name)
720 (3 'change-log-date)
721 (4 'change-log-list nil lax))))
722 (append `((,log-view-message-re . 'log-view-message))
723 ;; log-view-font-lock-keywords
724 '(("^ *\\(?:committer\\|author\\): \
725 \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
726 (1 'change-log-name)
727 (2 'change-log-email))
728 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))))
730 (autoload 'vc-setup-buffer "vc-dispatcher")
732 (defun vc-bzr-print-log (files buffer &optional shortlog start-revision limit)
733 "Print commit log associated with FILES into specified BUFFER.
734 If SHORTLOG is non-nil, use --line format.
735 If START-REVISION is non-nil, it is the newest revision to show.
736 If LIMIT is non-nil, show no more than this many entries."
737 ;; `vc-do-command' creates the buffer, but we need it before running
738 ;; the command.
739 (vc-setup-buffer buffer)
740 ;; If the buffer exists from a previous invocation it might be
741 ;; read-only.
742 ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
743 ;; the log display may not what the user wants - but I see no other
744 ;; way of getting the above regexps working.
745 (with-current-buffer buffer
746 (apply 'vc-bzr-command "log" buffer 'async files
747 (append
748 (if shortlog '("--line") '("--long"))
749 ;; The extra complications here when start-revision and limit
750 ;; are set are due to bzr log's --forward argument, which
751 ;; could be enabled via an alias in bazaar.conf.
752 ;; Svn, for example, does not have this problem, because
753 ;; it doesn't have --forward. Instead, you can use
754 ;; svn --log -r HEAD:0 or -r 0:HEAD as you prefer.
755 ;; Bzr, however, insists in -r X..Y that X come before Y.
756 (if start-revision
757 (list (format
758 (if (and limit (= limit 1))
759 ;; This means we don't have to use --no-aliases.
760 ;; Is -c any different to -r in this case?
761 "-r%s"
762 "-r..%s") start-revision)))
763 (when limit (list "-l" (format "%s" limit)))
764 ;; There is no sensible way to combine --limit and --forward,
765 ;; and it breaks the meaning of START-REVISION as the
766 ;; _newest_ revision. See bug#14168.
767 ;; Eg bzr log --forward -r ..100 --limit 50 prints
768 ;; revisions 1-50 rather than 50-100. There
769 ;; seems no way in general to get bzr to print revisions
770 ;; 50-100 in --forward order in that case.
771 ;; FIXME There may be other alias stuff we want to keep.
772 ;; Is there a way to just suppress --forward?
773 ;; As of 2013/4 the only caller uses limit = 1, so it does
774 ;; not matter much.
775 (and start-revision limit (> limit 1) '("--no-aliases"))
776 (if (stringp vc-bzr-log-switches)
777 (list vc-bzr-log-switches)
778 vc-bzr-log-switches)))))
780 (defun vc-bzr-expanded-log-entry (revision)
781 (with-temp-buffer
782 (apply 'vc-bzr-command "log" t nil nil
783 (list "--long" (format "-r%s" revision)))
784 (goto-char (point-min))
785 (when (looking-at "^-+\n")
786 ;; Indent the expanded log entry.
787 (indent-region (match-end 0) (point-max) 2)
788 (buffer-substring (match-end 0) (point-max)))))
790 (defun vc-bzr-log-incoming (buffer remote-location)
791 (apply 'vc-bzr-command "missing" buffer 'async nil
792 (list "--theirs-only" (unless (string= remote-location "") remote-location))))
794 (defun vc-bzr-log-outgoing (buffer remote-location)
795 (apply 'vc-bzr-command "missing" buffer 'async nil
796 (list "--mine-only" (unless (string= remote-location "") remote-location))))
798 (defun vc-bzr-show-log-entry (revision)
799 "Find entry for patch name REVISION in bzr change log buffer."
800 (goto-char (point-min))
801 (when revision
802 (let (case-fold-search
803 found)
804 (if (re-search-forward
805 ;; "revno:" can appear either at the beginning of a line,
806 ;; or indented.
807 (concat "^[ ]*-+\n[ ]*revno: "
808 ;; The revision can contain ".", quote it so that it
809 ;; does not interfere with regexp matching.
810 (regexp-quote revision) "$") nil t)
811 (progn
812 (beginning-of-line 0)
813 (setq found t))
814 (goto-char (point-min)))
815 found)))
817 (autoload 'vc-switches "vc")
819 (defun vc-bzr-diff (files &optional rev1 rev2 buffer async)
820 "VC bzr backend for diff."
821 (let* ((switches (vc-switches 'bzr 'diff))
822 (args
823 (append
824 ;; Only add --diff-options if there are any diff switches.
825 (unless (zerop (length switches))
826 (list "--diff-options" (mapconcat 'identity switches " ")))
827 ;; This `when' is just an optimization because bzr-1.2 is *much*
828 ;; faster when the revision argument is not given.
829 (when (or rev1 rev2)
830 (list "-r" (format "%s..%s"
831 (or rev1 "revno:-1")
832 (or rev2 "")))))))
833 ;; `bzr diff' exits with code 1 if diff is non-empty.
834 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*")
835 (if async 1 'async) files
836 args)))
839 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
840 ;; straight integer revisions.
842 (defun vc-bzr-delete-file (file)
843 "Delete FILE and delete it in the bzr repository."
844 (condition-case ()
845 (delete-file file)
846 (file-error nil))
847 (vc-bzr-command "remove" nil 0 file))
849 (defun vc-bzr-rename-file (old new)
850 "Rename file from OLD to NEW using `bzr mv'."
851 (setq old (expand-file-name old))
852 (setq new (expand-file-name new))
853 (vc-bzr-command "mv" nil 0 new old)
854 (message "Renamed %s => %s" old new))
856 (defvar vc-bzr-annotation-table nil
857 "Internal use.")
858 (make-variable-buffer-local 'vc-bzr-annotation-table)
860 (defun vc-bzr-annotate-command (file buffer &optional revision)
861 "Prepare BUFFER for `vc-annotate' on FILE.
862 Each line is tagged with the revision number, which has a `help-echo'
863 property containing author and date information."
864 (apply #'vc-bzr-command "annotate" buffer 'async file "--long" "--all"
865 (append (vc-switches 'bzr 'annotate)
866 (if revision (list "-r" revision))))
867 (let ((table (make-hash-table :test 'equal)))
868 (set-process-filter
869 (get-buffer-process buffer)
870 (lambda (proc string)
871 (when (process-buffer proc)
872 (with-current-buffer (process-buffer proc)
873 (setq string (concat (process-get proc :vc-left-over) string))
874 ;; Eg: 102020 Gnus developers 20101020 | regexp."
875 ;; As of bzr 2.2.2, no email address in whoami (which can
876 ;; lead to spaces in the author field) is allowed but discouraged.
877 ;; See bug#7792.
878 (while (string-match "^\\( *[0-9.]+ *\\) \\(.+?\\) +\\([0-9]\\{8\\}\\)\\( |.*\n\\)" string)
879 (let* ((rev (match-string 1 string))
880 (author (match-string 2 string))
881 (date (match-string 3 string))
882 (key (substring string (match-beginning 0)
883 (match-beginning 4)))
884 (line (match-string 4 string))
885 (tag (gethash key table))
886 (inhibit-read-only t))
887 (setq string (substring string (match-end 0)))
888 (unless tag
889 (setq tag
890 (propertize
891 (format "%s %-7.7s" rev author)
892 'help-echo (format "Revision: %d, author: %s, date: %s"
893 (string-to-number rev)
894 author date)
895 'mouse-face 'highlight))
896 (puthash key tag table))
897 (goto-char (process-mark proc))
898 (insert tag line)
899 (move-marker (process-mark proc) (point))))
900 (process-put proc :vc-left-over string)))))))
902 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
904 (defun vc-bzr-annotate-time ()
905 (when (re-search-forward "^ *[0-9.]+ +.+? +|" nil t)
906 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
907 (string-match "[0-9]+\\'" prop)
908 (let ((str (match-string-no-properties 0 prop)))
909 (vc-annotate-convert-time
910 (encode-time 0 0 0
911 (string-to-number (substring str 6 8))
912 (string-to-number (substring str 4 6))
913 (string-to-number (substring str 0 4))))))))
915 (defun vc-bzr-annotate-extract-revision-at-line ()
916 "Return revision for current line of annotation buffer, or nil.
917 Return nil if current line isn't annotated."
918 (save-excursion
919 (beginning-of-line)
920 (if (looking-at "^ *\\([0-9.]+\\) +.* +|")
921 (match-string-no-properties 1))))
923 (defun vc-bzr-command-discarding-stderr (command &rest args)
924 "Execute shell command COMMAND (with ARGS); return its output and exitcode.
925 Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
926 the (numerical) exit code of the process, and OUTPUT is a string
927 containing whatever the process sent to its standard output
928 stream. Standard error output is discarded."
929 (with-temp-buffer
930 (cons
931 (apply #'process-file command nil (list (current-buffer) nil) nil args)
932 (buffer-substring (point-min) (point-max)))))
934 (cl-defstruct (vc-bzr-extra-fileinfo
935 (:copier nil)
936 (:constructor vc-bzr-create-extra-fileinfo (extra-name))
937 (:conc-name vc-bzr-extra-fileinfo->))
938 extra-name) ;; original name for rename targets, new name for
940 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
942 (defun vc-bzr-dir-printer (info)
943 "Pretty-printer for the vc-dir-fileinfo structure."
944 (let ((extra (vc-dir-fileinfo->extra info)))
945 (vc-default-dir-printer 'Bzr info)
946 (when extra
947 (insert (propertize
948 (format " (renamed from %s)"
949 (vc-bzr-extra-fileinfo->extra-name extra))
950 'face 'font-lock-comment-face)))))
952 ;; FIXME: this needs testing, it's probably incomplete.
953 (defun vc-bzr-after-dir-status (update-function relative-dir)
954 (let ((status-str nil)
955 (translation '(("+N " . added)
956 ("-D " . removed)
957 (" M " . edited) ;; file text modified
958 (" *" . edited) ;; execute bit changed
959 (" M*" . edited) ;; text modified + execute bit changed
960 ("I " . ignored)
961 (" D " . missing)
962 ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
963 ("C " . conflict)
964 ("? " . unregistered)
965 ;; No such state, but we need to distinguish this case.
966 ("R " . renamed)
967 ("RM " . renamed)
968 ;; For a non existent file FOO, the output is:
969 ;; bzr: ERROR: Path(s) do not exist: FOO
970 ("bzr" . not-found)
971 ;; If the tree is not up to date, bzr will print this warning:
972 ;; working tree is out of date, run 'bzr update'
973 ;; ignore it.
974 ;; FIXME: maybe this warning can be put in the vc-dir header...
975 ("wor" . not-found)
976 ;; Ignore "P " and "P." for pending patches.
977 ("P " . not-found)
978 ("P. " . not-found)
980 (translated nil)
981 (result nil))
982 (goto-char (point-min))
983 ;; Skip a warning message that can occur in some bzr installations.
984 ;; vc-bzr-dir-extra-headers already reports it.
985 ;; Perhaps we should just discard stderr?
986 (and (looking-at "bzr: WARNING: bzrlib version doesn't match")
987 (re-search-forward "^bzr is version" nil t)
988 (forward-line 1))
989 (while (not (eobp))
990 ;; Bzr 2.3.0 added this if there are shelves. (Bug#8170)
991 (unless (looking-at "[0-9]+ shel\\(f\\|ves\\) exists?\\.")
992 (setq status-str
993 (buffer-substring-no-properties (point) (+ (point) 3)))
994 (setq translated (cdr (assoc status-str translation)))
995 (cond
996 ((eq translated 'conflict)
997 ;; For conflicts the file appears twice in the listing: once
998 ;; with the M flag and once with the C flag, so take care
999 ;; not to add it twice to `result'. Ugly.
1000 (let* ((file
1001 (buffer-substring-no-properties
1002 ;;For files with conflicts the format is:
1003 ;;C Text conflict in FILENAME
1004 ;; Bah.
1005 (+ (point) 21) (line-end-position)))
1006 (entry (assoc file result)))
1007 (when entry
1008 (setf (nth 1 entry) 'conflict))))
1009 ((eq translated 'renamed)
1010 (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t)
1011 (let ((new-name (file-relative-name (match-string 2) relative-dir))
1012 (old-name (file-relative-name (match-string 1) relative-dir)))
1013 (push (list new-name 'edited
1014 (vc-bzr-create-extra-fileinfo old-name)) result)))
1015 ;; do nothing for non existent files
1016 ((eq translated 'not-found))
1018 (push (list (file-relative-name
1019 (buffer-substring-no-properties
1020 (+ (point) 4)
1021 (line-end-position)) relative-dir)
1022 translated) result))))
1023 (forward-line))
1024 (funcall update-function result)))
1026 (defun vc-bzr-dir-status-files (dir files update-function)
1027 "Return a list of conses (file . state) for DIR."
1028 (apply 'vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S" files)
1029 (vc-run-delayed
1030 (vc-bzr-after-dir-status update-function
1031 ;; "bzr status" results are relative to
1032 ;; the bzr root directory, NOT to the
1033 ;; directory "bzr status" was invoked in.
1034 ;; Ugh.
1035 ;; We pass the relative directory here so
1036 ;; that `vc-bzr-after-dir-status' can
1037 ;; frob the results accordingly.
1038 (file-relative-name dir (vc-bzr-root dir)))))
1040 (defvar vc-bzr-shelve-map
1041 (let ((map (make-sparse-keymap)))
1042 ;; Turn off vc-dir marking
1043 (define-key map [mouse-2] 'ignore)
1045 (define-key map [down-mouse-3] 'vc-bzr-shelve-menu)
1046 (define-key map "\C-k" 'vc-bzr-shelve-delete-at-point)
1047 (define-key map "=" 'vc-bzr-shelve-show-at-point)
1048 (define-key map "\C-m" 'vc-bzr-shelve-show-at-point)
1049 (define-key map "A" 'vc-bzr-shelve-apply-and-keep-at-point)
1050 (define-key map "P" 'vc-bzr-shelve-apply-at-point)
1051 (define-key map "S" 'vc-bzr-shelve-snapshot)
1052 map))
1054 (defvar vc-bzr-shelve-menu-map
1055 (let ((map (make-sparse-keymap "Bzr Shelve")))
1056 (define-key map [de]
1057 '(menu-item "Delete Shelf" vc-bzr-shelve-delete-at-point
1058 :help "Delete the current shelf"))
1059 (define-key map [ap]
1060 '(menu-item "Apply and Keep Shelf" vc-bzr-shelve-apply-and-keep-at-point
1061 :help "Apply the current shelf and keep it"))
1062 (define-key map [po]
1063 '(menu-item "Apply and Remove Shelf (Pop)" vc-bzr-shelve-apply-at-point
1064 :help "Apply the current shelf and remove it"))
1065 (define-key map [sh]
1066 '(menu-item "Show Shelve" vc-bzr-shelve-show-at-point
1067 :help "Show the contents of the current shelve"))
1068 map))
1070 (defvar vc-bzr-extra-menu-map
1071 (let ((map (make-sparse-keymap)))
1072 (define-key map [bzr-sn]
1073 '(menu-item "Shelve a Snapshot" vc-bzr-shelve-snapshot
1074 :help "Shelve the current state of the tree and keep the current state"))
1075 (define-key map [bzr-sh]
1076 '(menu-item "Shelve..." vc-bzr-shelve
1077 :help "Shelve changes"))
1078 map))
1080 (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
1082 (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
1084 (defun vc-bzr-dir-extra-headers (dir)
1085 (let*
1086 ((str (with-temp-buffer
1087 (vc-bzr-command "info" t 0 dir)
1088 (buffer-string)))
1089 (shelve (vc-bzr-shelve-list))
1090 (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
1091 (root-dir (vc-bzr-root dir))
1092 (pending-merge
1093 ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
1094 ;; reliable method to detect pending merges, disable this
1095 ;; until a proper solution is implemented.
1096 (and nil
1097 (file-exists-p
1098 (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
1099 (pending-merge-help-echo
1100 (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))
1101 (light-checkout
1102 (when (string-match ".+light checkout root: \\(.+\\)$" str)
1103 (match-string 1 str)))
1104 (light-checkout-branch
1105 (when light-checkout
1106 (when (string-match ".+checkout of branch: \\(.+\\)$" str)
1107 (match-string 1 str)))))
1108 (concat
1109 (propertize "Parent branch : " 'face 'font-lock-type-face)
1110 (propertize
1111 (if (string-match "parent branch: \\(.+\\)$" str)
1112 (match-string 1 str)
1113 "None")
1114 'face 'font-lock-variable-name-face)
1115 "\n"
1116 (when light-checkout
1117 (concat
1118 (propertize "Light checkout root: " 'face 'font-lock-type-face)
1119 (propertize light-checkout 'face 'font-lock-variable-name-face)
1120 "\n"))
1121 (when light-checkout-branch
1122 (concat
1123 (propertize "Checkout of branch : " 'face 'font-lock-type-face)
1124 (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
1125 "\n"))
1126 (when pending-merge
1127 (concat
1128 (propertize "Warning : " 'face 'font-lock-warning-face
1129 'help-echo pending-merge-help-echo)
1130 (propertize "Pending merges, commit recommended before any other action"
1131 'help-echo pending-merge-help-echo
1132 'face 'font-lock-warning-face)
1133 "\n"))
1134 (if shelve
1135 (concat
1136 (propertize "Shelves :\n" 'face 'font-lock-type-face
1137 'help-echo shelve-help-echo)
1138 (mapconcat
1139 (lambda (x)
1140 (propertize x
1141 'face 'font-lock-variable-name-face
1142 'mouse-face 'highlight
1143 '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"
1144 'keymap vc-bzr-shelve-map))
1145 shelve "\n"))
1146 (concat
1147 (propertize "Shelves : " 'face 'font-lock-type-face
1148 'help-echo shelve-help-echo)
1149 (propertize "No shelved changes"
1150 'help-echo shelve-help-echo
1151 'face 'font-lock-variable-name-face))))))
1153 ;; Follows vc-bzr-command, which uses vc-do-command from vc-dispatcher.
1154 (declare-function vc-resynch-buffer "vc-dispatcher"
1155 (file &optional keep noquery reset-vc-info))
1157 (defun vc-bzr-shelve (name)
1158 "Shelve the changes of the selected files."
1159 (interactive "sShelf name: ")
1160 (let ((root (vc-bzr-root default-directory))
1161 (fileset (vc-deduce-fileset)))
1162 (when root
1163 (vc-bzr-command "shelve" nil 0 (nth 1 fileset) "--all" "-m" name)
1164 (vc-resynch-buffer root t t))))
1166 (defun vc-bzr-shelve-show (name)
1167 "Show the contents of shelve NAME."
1168 (interactive "sShelve name: ")
1169 (vc-setup-buffer "*vc-diff*")
1170 ;; FIXME: how can you show the contents of a shelf?
1171 (vc-bzr-command "unshelve" "*vc-diff*" 'async nil "--preview" name)
1172 (set-buffer "*vc-diff*")
1173 (diff-mode)
1174 (setq buffer-read-only t)
1175 (pop-to-buffer (current-buffer)))
1177 (defun vc-bzr-shelve-apply (name)
1178 "Apply shelve NAME and remove it afterwards."
1179 (interactive "sApply (and remove) shelf: ")
1180 (vc-bzr-command "unshelve" nil 0 nil "--apply" name)
1181 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1183 (defun vc-bzr-shelve-apply-and-keep (name)
1184 "Apply shelve NAME and keep it afterwards."
1185 (interactive "sApply (and keep) shelf: ")
1186 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep" name)
1187 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1189 (defun vc-bzr-shelve-snapshot ()
1190 "Create a stash with the current tree state."
1191 (interactive)
1192 (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
1193 (format-time-string "Snapshot on %Y-%m-%d at %H:%M"))
1194 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep")
1195 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1197 (defun vc-bzr-shelve-list ()
1198 (with-temp-buffer
1199 (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
1200 (delete
1202 (split-string
1203 (buffer-substring (point-min) (point-max))
1204 "\n"))))
1206 (defun vc-bzr-shelve-get-at-point (point)
1207 (save-excursion
1208 (goto-char point)
1209 (beginning-of-line)
1210 (if (looking-at "^ +\\([0-9]+\\):")
1211 (match-string 1)
1212 (error "Cannot find shelf at point"))))
1214 ;; vc-bzr-shelve-delete-at-point must be called from a vc-dir buffer.
1215 (declare-function vc-dir-refresh "vc-dir" ())
1217 (defun vc-bzr-shelve-delete-at-point ()
1218 (interactive)
1219 (let ((shelve (vc-bzr-shelve-get-at-point (point))))
1220 (when (y-or-n-p (format "Remove shelf %s ? " shelve))
1221 (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
1222 (vc-dir-refresh))))
1224 (defun vc-bzr-shelve-show-at-point ()
1225 (interactive)
1226 (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
1228 (defun vc-bzr-shelve-apply-at-point ()
1229 (interactive)
1230 (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
1232 (defun vc-bzr-shelve-apply-and-keep-at-point ()
1233 (interactive)
1234 (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
1236 (defun vc-bzr-shelve-menu (e)
1237 (interactive "e")
1238 (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
1240 (defun vc-bzr-revision-table (files)
1241 (let ((vc-bzr-revisions '())
1242 (default-directory (file-name-directory (car files))))
1243 (with-temp-buffer
1244 (vc-bzr-command "log" t 0 files "--line")
1245 (let ((start (point-min))
1246 (loglines (buffer-substring-no-properties (point-min) (point-max))))
1247 (while (string-match "^\\([0-9]+\\):" loglines)
1248 (push (match-string 1 loglines) vc-bzr-revisions)
1249 (setq start (+ start (match-end 0)))
1250 (setq loglines (buffer-substring-no-properties start (point-max))))))
1251 vc-bzr-revisions))
1253 (defun vc-bzr-conflicted-files (dir)
1254 (let ((default-directory (vc-bzr-root dir))
1255 (files ()))
1256 (with-temp-buffer
1257 (vc-bzr-command "status" t 0 default-directory)
1258 (goto-char (point-min))
1259 (when (re-search-forward "^conflicts:\n" nil t)
1260 (while (looking-at " \\(?:Text conflict in \\(.*\\)\\|.*\\)\n")
1261 (if (match-end 1)
1262 (push (expand-file-name (match-string 1)) files))
1263 (goto-char (match-end 0)))))
1264 files))
1266 ;;; Revision completion
1268 (eval-and-compile
1269 (defconst vc-bzr-revision-keywords
1270 ;; bzr help revisionspec | sed -ne 's/^\([a-z]*\):$/"\1"/p' | sort -u
1271 '("ancestor" "annotate" "before" "branch" "date" "last" "mainline" "revid"
1272 "revno" "submit" "tag")))
1274 (defun vc-bzr-revision-completion-table (files)
1275 ;; What about using `files'?!? --Stef
1276 (lambda (string pred action)
1277 (cond
1278 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
1279 string)
1280 (completion-table-with-context (substring string 0 (match-end 0))
1281 (apply-partially
1282 'completion-table-with-predicate
1283 'completion-file-name-table
1284 'file-directory-p t)
1285 (substring string (match-end 0))
1286 pred
1287 action))
1288 ((string-match "\\`\\(before\\):" string)
1289 (completion-table-with-context (substring string 0 (match-end 0))
1290 (vc-bzr-revision-completion-table files)
1291 (substring string (match-end 0))
1292 pred
1293 action))
1294 ((string-match "\\`\\(tag\\):" string)
1295 (let ((prefix (substring string 0 (match-end 0)))
1296 (tag (substring string (match-end 0)))
1297 (table nil)
1298 process-file-side-effects)
1299 (with-temp-buffer
1300 ;; "bzr-1.2 tags" is much faster with --show-ids.
1301 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
1302 ;; The output is ambiguous, unless we assume that revids do not
1303 ;; contain spaces.
1304 (goto-char (point-min))
1305 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
1306 (push (match-string-no-properties 1) table)))
1307 (completion-table-with-context prefix table tag pred action)))
1309 ((string-match "\\`annotate:" string)
1310 (completion-table-with-context
1311 (substring string 0 (match-end 0))
1312 (apply-partially #'completion-table-with-terminator '(":" . "\\`a\\`")
1313 #'completion-file-name-table)
1314 (substring string (match-end 0)) pred action))
1316 ((string-match "\\`date:" string)
1317 (completion-table-with-context
1318 (substring string 0 (match-end 0))
1319 '("yesterday" "today" "tomorrow")
1320 (substring string (match-end 0)) pred action))
1322 ((string-match "\\`\\([a-z]+\\):" string)
1323 ;; no actual completion for the remaining keywords.
1324 (completion-table-with-context (substring string 0 (match-end 0))
1325 (if (member (match-string 1 string)
1326 vc-bzr-revision-keywords)
1327 ;; If it's a valid keyword,
1328 ;; use a non-empty table to
1329 ;; indicate it.
1330 '("") nil)
1331 (substring string (match-end 0))
1332 pred
1333 action))
1335 ;; Could use completion-table-with-terminator, except that it
1336 ;; currently doesn't work right w.r.t pcm and doesn't give
1337 ;; the *Completions* output we want.
1338 (complete-with-action action (eval-when-compile
1339 (mapcar (lambda (s) (concat s ":"))
1340 vc-bzr-revision-keywords))
1341 string pred)))))
1343 (provide 'vc-bzr)
1345 ;;; vc-bzr.el ends here