]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-bzr.el
Function declaration updates prompted by 'make check-declare'
[gnu-emacs] / lisp / vc / vc-bzr.el
1 ;;; vc-bzr.el --- VC backend for the bzr revision control system -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2006-2015 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Riccardo Murri <riccardo.murri@gmail.com>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: vc tools
9 ;; Created: Sept 2006
10 ;; Package: vc
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; See <URL:http://bazaar.canonical.com/> concerning bzr.
30
31 ;; This library provides bzr support in VC.
32
33 ;; Known bugs
34 ;; ==========
35
36 ;; When editing a symlink and *both* the symlink and its target
37 ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
38 ;; symlink, thereby not detecting whether the actual contents
39 ;; (that is, the target contents) are changed.
40
41 ;;; Properties of the backend
42
43 (defun vc-bzr-revision-granularity () 'repository)
44 (defun vc-bzr-checkout-model (_files) 'implicit)
45
46 ;;; Code:
47
48 (eval-when-compile
49 (require 'cl-lib)
50 (require 'vc-dispatcher)
51 (require 'vc-dir)) ; vc-dir-at-event
52
53 ;; Clear up the cache to force vc-call to check again and discover
54 ;; new functions when we reload this file.
55 (put 'Bzr 'vc-functions nil)
56
57 (defgroup vc-bzr nil
58 "VC Bazaar (bzr) backend."
59 :version "22.2"
60 :group 'vc)
61
62 (defcustom vc-bzr-program "bzr"
63 "Name of the bzr command (excluding any arguments)."
64 :group 'vc-bzr
65 :type 'string)
66
67 (defcustom vc-bzr-diff-switches nil
68 "String or list of strings specifying switches for bzr diff under VC.
69 If nil, use the value of `vc-diff-switches'. If t, use no switches."
70 :type '(choice (const :tag "Unspecified" nil)
71 (const :tag "None" t)
72 (string :tag "Argument String")
73 (repeat :tag "Argument List" :value ("") string))
74 :group 'vc-bzr)
75
76 (defcustom vc-bzr-annotate-switches nil
77 "String or list of strings specifying switches for bzr annotate under VC.
78 If nil, use the value of `vc-annotate-switches'. If t, use no switches."
79 :type '(choice (const :tag "Unspecified" nil)
80 (const :tag "None" t)
81 (string :tag "Argument String")
82 (repeat :tag "Argument List" :value ("") string))
83 :version "25.1"
84 :group 'vc-bzr)
85
86 (defcustom vc-bzr-log-switches nil
87 "String or list of strings specifying switches for bzr log under VC."
88 :type '(choice (const :tag "None" nil)
89 (string :tag "Argument String")
90 (repeat :tag "Argument List" :value ("") string))
91 :group 'vc-bzr)
92
93 (defcustom vc-bzr-status-switches
94 (ignore-errors
95 (with-temp-buffer
96 (call-process vc-bzr-program nil t nil "help" "status")
97 (if (search-backward "--no-classify" nil t)
98 "--no-classify")))
99 "String or list of strings specifying switches for bzr status under VC.
100 The option \"--no-classify\" should be present if your bzr supports it."
101 :type '(choice (const :tag "None" nil)
102 (string :tag "Argument String")
103 (repeat :tag "Argument List" :value ("") string))
104 :group 'vc-bzr
105 :version "24.1")
106
107 ;; since v0.9, bzr supports removing the progress indicators
108 ;; by setting environment variable BZR_PROGRESS_BAR to "none".
109 (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args)
110 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
111 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
112 `LC_MESSAGES=C' to the environment. If BZR-COMMAND is \"status\",
113 prepends `vc-bzr-status-switches' to ARGS."
114 (let ((process-environment
115 `("BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
116 "LC_MESSAGES=C" ; Force English output
117 ,@process-environment)))
118 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-bzr-program
119 file-or-list bzr-command
120 (if (and (string-equal "status" bzr-command)
121 vc-bzr-status-switches)
122 (append (if (stringp vc-bzr-status-switches)
123 (list vc-bzr-status-switches)
124 vc-bzr-status-switches)
125 args)
126 args))))
127
128 (defun vc-bzr-async-command (bzr-command &rest args)
129 "Wrapper round `vc-do-async-command' using `vc-bzr-program' as COMMAND.
130 Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
131 `LC_MESSAGES=C' to the environment.
132 Use the current Bzr root directory as the ROOT argument to
133 `vc-do-async-command', and specify an output buffer named
134 \"*vc-bzr : ROOT*\". Return this buffer."
135 (let* ((process-environment
136 `("BZR_PROGRESS_BAR=none" "LC_MESSAGES=C"
137 ,@process-environment))
138 (root (vc-bzr-root default-directory))
139 (buffer (format "*vc-bzr : %s*" (expand-file-name root))))
140 (apply 'vc-do-async-command buffer root
141 vc-bzr-program bzr-command args)
142 buffer))
143
144 ;;;###autoload
145 (defconst vc-bzr-admin-dirname ".bzr"
146 "Name of the directory containing Bzr repository status files.")
147 ;; Used in the autoloaded vc-bzr-registered; see below.
148 ;;;###autoload
149 (defconst vc-bzr-admin-checkout-format-file
150 (concat vc-bzr-admin-dirname "/checkout/format")
151 "Name of the format file in a .bzr directory.")
152 (defconst vc-bzr-admin-dirstate
153 (concat vc-bzr-admin-dirname "/checkout/dirstate"))
154 (defconst vc-bzr-admin-branch-format-file
155 (concat vc-bzr-admin-dirname "/branch/format"))
156 (defconst vc-bzr-admin-revhistory
157 (concat vc-bzr-admin-dirname "/branch/revision-history"))
158 (defconst vc-bzr-admin-lastrev
159 (concat vc-bzr-admin-dirname "/branch/last-revision"))
160 (defconst vc-bzr-admin-branchconf
161 (concat vc-bzr-admin-dirname "/branch/branch.conf"))
162
163 (defun vc-bzr-root (file)
164 "Return the root directory of the bzr repository containing FILE."
165 ;; Cache technique copied from vc-arch.el.
166 (or (vc-file-getprop file 'bzr-root)
167 (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file)))
168 (when root (vc-file-setprop file 'bzr-root root)))))
169
170 (defun vc-bzr-branch-conf (file)
171 "Return the Bazaar branch settings for file FILE, as an alist.
172 Each element of the returned alist has the form (NAME . VALUE),
173 which are the name and value of a Bazaar setting, as strings.
174
175 The settings are read from the file \".bzr/branch/branch.conf\"
176 in the repository root directory of FILE."
177 (let (settings)
178 (with-temp-buffer
179 (insert-file-contents
180 (expand-file-name vc-bzr-admin-branchconf (vc-bzr-root file)))
181 (while (re-search-forward "^\\([^#=][^=]*?\\) *= *\\(.*\\)$" nil t)
182 (push (cons (match-string 1) (match-string 2)) settings)))
183 settings))
184
185 (defun vc-bzr-sha1 (file)
186 (with-temp-buffer
187 (set-buffer-multibyte nil)
188 (insert-file-contents-literally file)
189 (sha1 (current-buffer))))
190
191 (defun vc-bzr-state-heuristic (file)
192 "Like `vc-bzr-state' but hopefully without running Bzr."
193 ;; `bzr status' could be slow with large histories and pending merges,
194 ;; so this tries to avoid calling it if possible. bzr status is
195 ;; faster now, so this is not as important as it was.
196 ;;
197 ;; This function tries first to parse Bzr internal file
198 ;; `checkout/dirstate', but it may fail if Bzr internal file format
199 ;; has changed. As a safeguard, the `checkout/dirstate' file is
200 ;; only parsed if it contains the string `#bazaar dirstate flat
201 ;; format 3' in the first line.
202 ;; If the `checkout/dirstate' file cannot be parsed, fall back to
203 ;; running `vc-bzr-state'."
204 ;;
205 ;; The format of the dirstate file is explained in bzrlib/dirstate.py
206 ;; in the bzr distribution. Basically:
207 ;; header-line giving the version of the file format in use.
208 ;; a few lines of stuff
209 ;; entries, one per line, with null-separated fields. Each line:
210 ;; entry_key = dirname (may be empty), basename, file-id
211 ;; current = common ( = kind, fingerprint, size, executable )
212 ;; + working ( = packed_stat )
213 ;; parent = common ( as above ) + history ( = rev_id )
214 ;; kinds = (r)elocated, (a)bsent, (d)irectory, (f)ile, (l)ink
215 (let* ((root (vc-bzr-root file))
216 (dirstate (expand-file-name vc-bzr-admin-dirstate root)))
217 (when root ; Short cut.
218 (condition-case err
219 (with-temp-buffer
220 (insert-file-contents dirstate)
221 (goto-char (point-min))
222 (if (not (looking-at "#bazaar dirstate flat format 3"))
223 (vc-bzr-state file) ; Some other unknown format?
224 (let* ((relfile (file-relative-name file root))
225 (reldir (file-name-directory relfile)))
226 (cond
227 ((not
228 (re-search-forward
229 (concat "^\0"
230 (if reldir (regexp-quote
231 (directory-file-name reldir)))
232 "\0"
233 (regexp-quote (file-name-nondirectory relfile))
234 "\0"
235 "[^\0]*\0" ;id?
236 "\\([^\0]*\\)\0" ;"a/f/d", a=removed?
237 "\\([^\0]*\\)\0" ;sha1 (empty if conflicted)?
238 "\\([^\0]*\\)\0" ;size?p
239 ;; y/n. Whether or not the current copy
240 ;; was executable the last time bzr checked?
241 "[^\0]*\0"
242 "[^\0]*\0" ;?
243 ;; Parent information. Absent in a new repo.
244 "\\(?:\\([^\0]*\\)\0" ;"a/f/d" a=added?
245 "\\([^\0]*\\)\0" ;sha1 again?
246 "\\([^\0]*\\)\0" ;size again?
247 ;; y/n. Whether or not the repo thinks
248 ;; the file should be executable?
249 "\\([^\0]*\\)\0"
250 "[^\0]*\0\\)?" ;last revid?
251 ;; There are more fields when merges are pending.
252 )
253 nil t))
254 'unregistered)
255 ;; Apparently the second sha1 is the one we want: when
256 ;; there's a conflict, the first sha1 is absent (and the
257 ;; first size seems to correspond to the file with
258 ;; conflict markers).
259 ((eq (char-after (match-beginning 1)) ?a) 'removed)
260 ;; If there is no parent, this must be a new repo.
261 ;; If file is in dirstate, can only be added (b#8025).
262 ((or (not (match-beginning 4))
263 (eq (char-after (match-beginning 4)) ?a)) 'added)
264 ((or (and (eq (string-to-number (match-string 3))
265 (nth 7 (file-attributes file)))
266 (equal (match-string 5)
267 (save-match-data (vc-bzr-sha1 file)))
268 ;; For a file, does the executable state match?
269 ;; (Bug#7544)
270 (or (not
271 (eq (char-after (match-beginning 1)) ?f))
272 (let ((exe
273 (memq
274 ?x
275 (mapcar
276 'identity
277 (nth 8 (file-attributes file))))))
278 (if (eq (char-after (match-beginning 7))
279 ?y)
280 exe
281 (not exe)))))
282 (and
283 ;; It looks like for lightweight
284 ;; checkouts \2 is empty and we need to
285 ;; look for size in \6.
286 (eq (match-beginning 2) (match-end 2))
287 (eq (string-to-number (match-string 6))
288 (nth 7 (file-attributes file)))
289 (equal (match-string 5)
290 (vc-bzr-sha1 file))))
291 'up-to-date)
292 (t 'edited)))))
293 ;; The dirstate file can't be read, or some other problem.
294 (error
295 (message "Falling back on \"slow\" status detection (%S)" err)
296 (vc-bzr-state file))))))
297
298 ;; This is a cheap approximation that is autoloaded. If it finds a
299 ;; possible match it loads this file and runs the real function.
300 ;; It requires vc-bzr-admin-checkout-format-file to be autoloaded too.
301 ;;;###autoload (defun vc-bzr-registered (file)
302 ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
303 ;;;###autoload (progn
304 ;;;###autoload (load "vc-bzr" nil t)
305 ;;;###autoload (vc-bzr-registered file))))
306
307 (defun vc-bzr-registered (file)
308 "Return non-nil if FILE is registered with bzr."
309 (let ((state (vc-bzr-state-heuristic file)))
310 (not (memq state '(nil unregistered ignored)))))
311
312 (defconst vc-bzr-state-words
313 "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
314 "Regexp matching file status words as reported in `bzr' output.")
315
316 ;; History of Bzr commands.
317 (defvar vc-bzr-history nil)
318
319 (defun vc-bzr-file-name-relative (filename)
320 "Return file name FILENAME stripped of the initial Bzr repository path."
321 (let* ((filename* (expand-file-name filename))
322 (rootdir (vc-bzr-root filename*)))
323 (when rootdir
324 (file-relative-name filename* rootdir))))
325
326 (defvar vc-bzr-error-regexp-alist
327 '(("^\\( M[* ]\\|+N \\|-D \\|\\| \\*\\|R[M ] \\) \\(.+\\)" 2 nil nil 1)
328 ("^C \\(.+\\)" 2)
329 ("^Text conflict in \\(.+\\)" 1 nil nil 2)
330 ("^Using saved parent location: \\(.+\\)" 1 nil nil 0))
331 "Value of `compilation-error-regexp-alist' in *vc-bzr* buffers.")
332
333 ;; To be called via vc-pull from vc.el, which requires vc-dispatcher.
334 (declare-function vc-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))
337
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))))
370
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))))
401
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.
411 ;;
412 ;; ", or nil for files that are not registered with Bzr.
413 ;; If any error occurred in running `bzr status', then return nil."
414 ;;
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))))))))
458
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)))))))
478
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))))
487
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")))
505
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))))))
518
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))))))
596
597 (defun vc-bzr-create-repo ()
598 "Create a new Bzr repository."
599 (vc-bzr-command "init" nil 0 nil))
600
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)))
605
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)))
610
611 (defun vc-bzr-register (files &optional _comment)
612 "Register FILES under bzr. COMMENT is ignored."
613 (vc-bzr-command "add" nil 0 files))
614
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.")
621
622 (defun vc-bzr-unregister (file)
623 "Unregister FILE from bzr."
624 (vc-bzr-command "remove" nil 0 file "--keep"))
625
626 (declare-function log-edit-extract-headers "log-edit" (headers string))
627
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)))))
635
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))))
644
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))))
651
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)))
656
657 (defun vc-bzr-checkout (_file &optional rev)
658 (if rev (error "Operation not supported")
659 ;; Else, there's nothing to do.
660 nil))
661
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"))))
665
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)
672
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)))))))
703
704 (autoload 'vc-setup-buffer "vc-dispatcher")
705
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)))))
753
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)))))
763
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))))
767
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))))
771
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)))
790
791 (autoload 'vc-switches "vc")
792
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)))
811
812
813 ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
814 ;; straight integer revisions.
815
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))
822
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))
829
830 (defvar vc-bzr-annotation-table nil
831 "Internal use.")
832 (make-variable-buffer-local 'vc-bzr-annotation-table)
833
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)))))))
875
876 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
877
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))))))))
888
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))))
896
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)))))
907
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
913
914 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
915
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)))))
925
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)
953 ))
954 (translated nil)
955 (result nil))
956 (goto-char (point-min))
957 ;; Skip a warning message that can occur in some bzr installations.
958 ;; vc-bzr-dir-extra-headers already reports it.
959 ;; Perhaps we should just discard stderr?
960 (and (looking-at "bzr: WARNING: bzrlib version doesn't match")
961 (re-search-forward "^bzr is version" nil t)
962 (forward-line 1))
963 (while (not (eobp))
964 ;; Bzr 2.3.0 added this if there are shelves. (Bug#8170)
965 (unless (looking-at "[0-9]+ shel\\(f\\|ves\\) exists?\\.")
966 (setq status-str
967 (buffer-substring-no-properties (point) (+ (point) 3)))
968 (setq translated (cdr (assoc status-str translation)))
969 (cond
970 ((eq translated 'conflict)
971 ;; For conflicts the file appears twice in the listing: once
972 ;; with the M flag and once with the C flag, so take care
973 ;; not to add it twice to `result'. Ugly.
974 (let* ((file
975 (buffer-substring-no-properties
976 ;;For files with conflicts the format is:
977 ;;C Text conflict in FILENAME
978 ;; Bah.
979 (+ (point) 21) (line-end-position)))
980 (entry (assoc file result)))
981 (when entry
982 (setf (nth 1 entry) 'conflict))))
983 ((eq translated 'renamed)
984 (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t)
985 (let ((new-name (file-relative-name (match-string 2) relative-dir))
986 (old-name (file-relative-name (match-string 1) relative-dir)))
987 (push (list new-name 'edited
988 (vc-bzr-create-extra-fileinfo old-name)) result)))
989 ;; do nothing for non existent files
990 ((eq translated 'not-found))
991 (t
992 (push (list (file-relative-name
993 (buffer-substring-no-properties
994 (+ (point) 4)
995 (line-end-position)) relative-dir)
996 translated) result))))
997 (forward-line))
998 (funcall update-function result)))
999
1000 (defun vc-bzr-dir-status-files (dir files update-function)
1001 "Return a list of conses (file . state) for DIR."
1002 (apply 'vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S" files)
1003 (vc-run-delayed
1004 (vc-bzr-after-dir-status update-function
1005 ;; "bzr status" results are relative to
1006 ;; the bzr root directory, NOT to the
1007 ;; directory "bzr status" was invoked in.
1008 ;; Ugh.
1009 ;; We pass the relative directory here so
1010 ;; that `vc-bzr-after-dir-status' can
1011 ;; frob the results accordingly.
1012 (file-relative-name dir (vc-bzr-root dir)))))
1013
1014 (defvar vc-bzr-shelve-map
1015 (let ((map (make-sparse-keymap)))
1016 ;; Turn off vc-dir marking
1017 (define-key map [mouse-2] 'ignore)
1018
1019 (define-key map [down-mouse-3] 'vc-bzr-shelve-menu)
1020 (define-key map "\C-k" 'vc-bzr-shelve-delete-at-point)
1021 (define-key map "=" 'vc-bzr-shelve-show-at-point)
1022 (define-key map "\C-m" 'vc-bzr-shelve-show-at-point)
1023 (define-key map "A" 'vc-bzr-shelve-apply-and-keep-at-point)
1024 (define-key map "P" 'vc-bzr-shelve-apply-at-point)
1025 (define-key map "S" 'vc-bzr-shelve-snapshot)
1026 map))
1027
1028 (defvar vc-bzr-shelve-menu-map
1029 (let ((map (make-sparse-keymap "Bzr Shelve")))
1030 (define-key map [de]
1031 '(menu-item "Delete Shelf" vc-bzr-shelve-delete-at-point
1032 :help "Delete the current shelf"))
1033 (define-key map [ap]
1034 '(menu-item "Apply and Keep Shelf" vc-bzr-shelve-apply-and-keep-at-point
1035 :help "Apply the current shelf and keep it"))
1036 (define-key map [po]
1037 '(menu-item "Apply and Remove Shelf (Pop)" vc-bzr-shelve-apply-at-point
1038 :help "Apply the current shelf and remove it"))
1039 (define-key map [sh]
1040 '(menu-item "Show Shelve" vc-bzr-shelve-show-at-point
1041 :help "Show the contents of the current shelve"))
1042 map))
1043
1044 (defvar vc-bzr-extra-menu-map
1045 (let ((map (make-sparse-keymap)))
1046 (define-key map [bzr-sn]
1047 '(menu-item "Shelve a Snapshot" vc-bzr-shelve-snapshot
1048 :help "Shelve the current state of the tree and keep the current state"))
1049 (define-key map [bzr-sh]
1050 '(menu-item "Shelve..." vc-bzr-shelve
1051 :help "Shelve changes"))
1052 map))
1053
1054 (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
1055
1056 (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
1057
1058 (defun vc-bzr-dir-extra-headers (dir)
1059 (let*
1060 ((str (with-temp-buffer
1061 (vc-bzr-command "info" t 0 dir)
1062 (buffer-string)))
1063 (shelve (vc-bzr-shelve-list))
1064 (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
1065 (root-dir (vc-bzr-root dir))
1066 (pending-merge
1067 ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
1068 ;; reliable method to detect pending merges, disable this
1069 ;; until a proper solution is implemented.
1070 (and nil
1071 (file-exists-p
1072 (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
1073 (pending-merge-help-echo
1074 (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))
1075 (light-checkout
1076 (when (string-match ".+light checkout root: \\(.+\\)$" str)
1077 (match-string 1 str)))
1078 (light-checkout-branch
1079 (when light-checkout
1080 (when (string-match ".+checkout of branch: \\(.+\\)$" str)
1081 (match-string 1 str)))))
1082 (concat
1083 (propertize "Parent branch : " 'face 'font-lock-type-face)
1084 (propertize
1085 (if (string-match "parent branch: \\(.+\\)$" str)
1086 (match-string 1 str)
1087 "None")
1088 'face 'font-lock-variable-name-face)
1089 "\n"
1090 (when light-checkout
1091 (concat
1092 (propertize "Light checkout root: " 'face 'font-lock-type-face)
1093 (propertize light-checkout 'face 'font-lock-variable-name-face)
1094 "\n"))
1095 (when light-checkout-branch
1096 (concat
1097 (propertize "Checkout of branch : " 'face 'font-lock-type-face)
1098 (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
1099 "\n"))
1100 (when pending-merge
1101 (concat
1102 (propertize "Warning : " 'face 'font-lock-warning-face
1103 'help-echo pending-merge-help-echo)
1104 (propertize "Pending merges, commit recommended before any other action"
1105 'help-echo pending-merge-help-echo
1106 'face 'font-lock-warning-face)
1107 "\n"))
1108 (if shelve
1109 (concat
1110 (propertize "Shelves :\n" 'face 'font-lock-type-face
1111 'help-echo shelve-help-echo)
1112 (mapconcat
1113 (lambda (x)
1114 (propertize x
1115 'face 'font-lock-variable-name-face
1116 'mouse-face 'highlight
1117 '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"
1118 'keymap vc-bzr-shelve-map))
1119 shelve "\n"))
1120 (concat
1121 (propertize "Shelves : " 'face 'font-lock-type-face
1122 'help-echo shelve-help-echo)
1123 (propertize "No shelved changes"
1124 'help-echo shelve-help-echo
1125 'face 'font-lock-variable-name-face))))))
1126
1127 ;; Follows vc-bzr-command, which uses vc-do-command from vc-dispatcher.
1128 (declare-function vc-resynch-buffer "vc-dispatcher"
1129 (file &optional keep noquery reset-vc-info))
1130
1131 (defun vc-bzr-shelve (name)
1132 "Shelve the changes of the selected files."
1133 (interactive "sShelf name: ")
1134 (let ((root (vc-bzr-root default-directory))
1135 (fileset (vc-deduce-fileset)))
1136 (when root
1137 (vc-bzr-command "shelve" nil 0 (nth 1 fileset) "--all" "-m" name)
1138 (vc-resynch-buffer root t t))))
1139
1140 (defun vc-bzr-shelve-show (name)
1141 "Show the contents of shelve NAME."
1142 (interactive "sShelve name: ")
1143 (vc-setup-buffer "*vc-diff*")
1144 ;; FIXME: how can you show the contents of a shelf?
1145 (vc-bzr-command "unshelve" "*vc-diff*" 'async nil "--preview" name)
1146 (set-buffer "*vc-diff*")
1147 (diff-mode)
1148 (setq buffer-read-only t)
1149 (pop-to-buffer (current-buffer)))
1150
1151 (defun vc-bzr-shelve-apply (name)
1152 "Apply shelve NAME and remove it afterwards."
1153 (interactive "sApply (and remove) shelf: ")
1154 (vc-bzr-command "unshelve" nil 0 nil "--apply" name)
1155 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1156
1157 (defun vc-bzr-shelve-apply-and-keep (name)
1158 "Apply shelve NAME and keep it afterwards."
1159 (interactive "sApply (and keep) shelf: ")
1160 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep" name)
1161 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1162
1163 (defun vc-bzr-shelve-snapshot ()
1164 "Create a stash with the current tree state."
1165 (interactive)
1166 (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
1167 (format-time-string "Snapshot on %Y-%m-%d at %H:%M"))
1168 (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep")
1169 (vc-resynch-buffer (vc-bzr-root default-directory) t t))
1170
1171 (defun vc-bzr-shelve-list ()
1172 (with-temp-buffer
1173 (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
1174 (delete
1175 ""
1176 (split-string
1177 (buffer-substring (point-min) (point-max))
1178 "\n"))))
1179
1180 (defun vc-bzr-shelve-get-at-point (point)
1181 (save-excursion
1182 (goto-char point)
1183 (beginning-of-line)
1184 (if (looking-at "^ +\\([0-9]+\\):")
1185 (match-string 1)
1186 (error "Cannot find shelf at point"))))
1187
1188 ;; vc-bzr-shelve-delete-at-point must be called from a vc-dir buffer.
1189 (declare-function vc-dir-refresh "vc-dir" ())
1190
1191 (defun vc-bzr-shelve-delete-at-point ()
1192 (interactive)
1193 (let ((shelve (vc-bzr-shelve-get-at-point (point))))
1194 (when (y-or-n-p (format "Remove shelf %s ? " shelve))
1195 (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
1196 (vc-dir-refresh))))
1197
1198 (defun vc-bzr-shelve-show-at-point ()
1199 (interactive)
1200 (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
1201
1202 (defun vc-bzr-shelve-apply-at-point ()
1203 (interactive)
1204 (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
1205
1206 (defun vc-bzr-shelve-apply-and-keep-at-point ()
1207 (interactive)
1208 (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
1209
1210 (defun vc-bzr-shelve-menu (e)
1211 (interactive "e")
1212 (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
1213
1214 (defun vc-bzr-revision-table (files)
1215 (let ((vc-bzr-revisions '())
1216 (default-directory (file-name-directory (car files))))
1217 (with-temp-buffer
1218 (vc-bzr-command "log" t 0 files "--line")
1219 (let ((start (point-min))
1220 (loglines (buffer-substring-no-properties (point-min) (point-max))))
1221 (while (string-match "^\\([0-9]+\\):" loglines)
1222 (push (match-string 1 loglines) vc-bzr-revisions)
1223 (setq start (+ start (match-end 0)))
1224 (setq loglines (buffer-substring-no-properties start (point-max))))))
1225 vc-bzr-revisions))
1226
1227 (defun vc-bzr-conflicted-files (dir)
1228 (let ((default-directory (vc-bzr-root dir))
1229 (files ()))
1230 (with-temp-buffer
1231 (vc-bzr-command "status" t 0 default-directory)
1232 (goto-char (point-min))
1233 (when (re-search-forward "^conflicts:\n" nil t)
1234 (while (looking-at " \\(?:Text conflict in \\(.*\\)\\|.*\\)\n")
1235 (if (match-end 1)
1236 (push (expand-file-name (match-string 1)) files))
1237 (goto-char (match-end 0)))))
1238 files))
1239
1240 ;;; Revision completion
1241
1242 (eval-and-compile
1243 (defconst vc-bzr-revision-keywords
1244 ;; bzr help revisionspec | sed -ne 's/^\([a-z]*\):$/"\1"/p' | sort -u
1245 '("ancestor" "annotate" "before" "branch" "date" "last" "mainline" "revid"
1246 "revno" "submit" "tag")))
1247
1248 (defun vc-bzr-revision-completion-table (files)
1249 ;; What about using `files'?!? --Stef
1250 (lambda (string pred action)
1251 (cond
1252 ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
1253 string)
1254 (completion-table-with-context (substring string 0 (match-end 0))
1255 (apply-partially
1256 'completion-table-with-predicate
1257 'completion-file-name-table
1258 'file-directory-p t)
1259 (substring string (match-end 0))
1260 pred
1261 action))
1262 ((string-match "\\`\\(before\\):" string)
1263 (completion-table-with-context (substring string 0 (match-end 0))
1264 (vc-bzr-revision-completion-table files)
1265 (substring string (match-end 0))
1266 pred
1267 action))
1268 ((string-match "\\`\\(tag\\):" string)
1269 (let ((prefix (substring string 0 (match-end 0)))
1270 (tag (substring string (match-end 0)))
1271 (table nil)
1272 process-file-side-effects)
1273 (with-temp-buffer
1274 ;; "bzr-1.2 tags" is much faster with --show-ids.
1275 (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
1276 ;; The output is ambiguous, unless we assume that revids do not
1277 ;; contain spaces.
1278 (goto-char (point-min))
1279 (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
1280 (push (match-string-no-properties 1) table)))
1281 (completion-table-with-context prefix table tag pred action)))
1282
1283 ((string-match "\\`annotate:" string)
1284 (completion-table-with-context
1285 (substring string 0 (match-end 0))
1286 (apply-partially #'completion-table-with-terminator '(":" . "\\`a\\`")
1287 #'completion-file-name-table)
1288 (substring string (match-end 0)) pred action))
1289
1290 ((string-match "\\`date:" string)
1291 (completion-table-with-context
1292 (substring string 0 (match-end 0))
1293 '("yesterday" "today" "tomorrow")
1294 (substring string (match-end 0)) pred action))
1295
1296 ((string-match "\\`\\([a-z]+\\):" string)
1297 ;; no actual completion for the remaining keywords.
1298 (completion-table-with-context (substring string 0 (match-end 0))
1299 (if (member (match-string 1 string)
1300 vc-bzr-revision-keywords)
1301 ;; If it's a valid keyword,
1302 ;; use a non-empty table to
1303 ;; indicate it.
1304 '("") nil)
1305 (substring string (match-end 0))
1306 pred
1307 action))
1308 (t
1309 ;; Could use completion-table-with-terminator, except that it
1310 ;; currently doesn't work right w.r.t pcm and doesn't give
1311 ;; the *Completions* output we want.
1312 (complete-with-action action (eval-when-compile
1313 (mapcar (lambda (s) (concat s ":"))
1314 vc-bzr-revision-keywords))
1315 string pred)))))
1316
1317 (provide 'vc-bzr)
1318
1319 ;;; vc-bzr.el ends here