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