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