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