Commit | Line | Data |
---|---|---|
b6e0e86c SM |
1 | ;;; vc-bzr.el --- VC backend for the bzr revision control system |
2 | ||
f4d0cf23 | 3 | ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc. |
b6e0e86c | 4 | |
b6e0e86c SM |
5 | ;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com> |
6 | ;; Keywords: tools | |
7 | ;; Created: Sept 2006 | |
f4d0cf23 | 8 | ;; Version: 2008-01-04 (Bzr revno 25) |
b6e0e86c SM |
9 | ;; URL: http://launchpad.net/vc-bzr |
10 | ||
11 | ;; This file is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
64be3a42 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
b6e0e86c SM |
14 | ;; any later version. |
15 | ||
16 | ;; This file is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
24 | ;; Boston, MA 02110-1301, USA. | |
25 | ||
26 | ||
27 | ;;; Commentary: | |
28 | ||
f4d0cf23 SM |
29 | ;; See <URL:http://bazaar-vcs.org/> concerning bzr. See |
30 | ;; <URL:http://launchpad.net/vc-bzr> for alternate development | |
31 | ;; branches of `vc-bzr'. | |
b6e0e86c | 32 | |
f4d0cf23 | 33 | ;; Load this library to register bzr support in VC. |
77b5d458 SM |
34 | |
35 | ;; Known bugs | |
36 | ;; ========== | |
37 | ||
38 | ;; When edititing a symlink and *both* the symlink and its target | |
39 | ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the | |
40 | ;; symlink, thereby not detecting whether the actual contents | |
41 | ;; (that is, the target contents) are changed. | |
42 | ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607 | |
43 | ||
44 | ;; For an up-to-date list of bugs, please see: | |
45 | ;; https://bugs.launchpad.net/vc-bzr/+bugs | |
b6e0e86c SM |
46 | |
47 | ||
48 | ;;; Code: | |
49 | ||
50 | (eval-when-compile | |
37320a58 | 51 | (require 'cl) |
b6e0e86c SM |
52 | (require 'vc)) ; for vc-exec-after |
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 |
37320a58 | 69 | "String/list of strings specifying extra switches for bzr diff under VC." |
b6e0e86c SM |
70 | :type '(choice (const :tag "None" nil) |
71 | (string :tag "Argument String") | |
72 | (repeat :tag "Argument List" :value ("") string)) | |
73 | :group 'vc-bzr) | |
74 | ||
f4d0cf23 SM |
75 | (defcustom vc-bzr-log-switches nil |
76 | "String/list of strings specifying extra 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 | ||
37320a58 SM |
82 | ;; since v0.9, bzr supports removing the progress indicators |
83 | ;; by setting environment variable BZR_PROGRESS_BAR to "none". | |
8cdd17b4 | 84 | (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args) |
37320a58 | 85 | "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND. |
f4d0cf23 SM |
86 | Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and |
87 | `LC_MESSAGES=C' to the environment." | |
37320a58 SM |
88 | (let ((process-environment |
89 | (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9) | |
f4d0cf23 | 90 | "LC_MESSAGES=C" ; Force English output |
a460c94c | 91 | process-environment))) |
37320a58 | 92 | (apply 'vc-do-command buffer okstatus vc-bzr-program |
f4d0cf23 | 93 | file-or-list bzr-command args))) |
b6e0e86c | 94 | |
37320a58 SM |
95 | |
96 | ;;;###autoload | |
f4d0cf23 | 97 | (defconst vc-bzr-admin-dirname ".bzr" |
b6e6e09a | 98 | "Name of the directory containing Bzr repository status files.") |
a460c94c | 99 | ;;;###autoload |
b6e6e09a SM |
100 | (defconst vc-bzr-admin-checkout-format-file |
101 | (concat vc-bzr-admin-dirname "/checkout/format")) | |
a460c94c | 102 | (defconst vc-bzr-admin-dirstate |
b6e6e09a SM |
103 | (concat vc-bzr-admin-dirname "/checkout/dirstate")) |
104 | (defconst vc-bzr-admin-branch-format-file | |
105 | (concat vc-bzr-admin-dirname "/branch/format")) | |
a460c94c | 106 | (defconst vc-bzr-admin-revhistory |
b6e6e09a | 107 | (concat vc-bzr-admin-dirname "/branch/revision-history")) |
12451866 RF |
108 | (defconst vc-bzr-admin-lastrev |
109 | (concat vc-bzr-admin-dirname "/branch/last-revision")) | |
37320a58 SM |
110 | |
111 | ;;;###autoload (defun vc-bzr-registered (file) | |
b6e6e09a | 112 | ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file) |
37320a58 SM |
113 | ;;;###autoload (progn |
114 | ;;;###autoload (load "vc-bzr") | |
115 | ;;;###autoload (vc-bzr-registered file)))) | |
b6e0e86c | 116 | |
a460c94c SM |
117 | (defun vc-bzr-root (file) |
118 | "Return the root directory of the bzr repository containing FILE." | |
119 | ;; Cache technique copied from vc-arch.el. | |
120 | (or (vc-file-getprop file 'bzr-root) | |
b38f5e6f DN |
121 | (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file))) |
122 | (when root (vc-file-setprop file 'bzr-root root))))) | |
b6e0e86c | 123 | |
82eb83ff | 124 | (require 'sha1) ;For sha1-program |
a460c94c | 125 | |
82eb83ff SM |
126 | (defun vc-bzr-sha1 (file) |
127 | (with-temp-buffer | |
128 | (set-buffer-multibyte nil) | |
129 | (let ((prog sha1-program) | |
130 | (args nil)) | |
131 | (when (consp prog) | |
132 | (setq args (cdr prog)) | |
133 | (setq prog (car prog))) | |
134 | (apply 'call-process prog file t nil args) | |
135 | (buffer-substring (point-min) (+ (point-min) 40))))) | |
136 | ||
137 | (defun vc-bzr-state-heuristic (file) | |
138 | "Like `vc-bzr-state' but hopefully without running Bzr." | |
139 | ;; `bzr status' is excrutiatingly slow with large histories and | |
140 | ;; pending merges, so try to avoid using it until they fix their | |
141 | ;; performance problems. | |
142 | ;; This function tries first to parse Bzr internal file | |
143 | ;; `checkout/dirstate', but it may fail if Bzr internal file format | |
144 | ;; has changed. As a safeguard, the `checkout/dirstate' file is | |
145 | ;; only parsed if it contains the string `#bazaar dirstate flat | |
146 | ;; format 3' in the first line. | |
147 | ;; If the `checkout/dirstate' file cannot be parsed, fall back to | |
148 | ;; running `vc-bzr-state'." | |
fba500b6 SM |
149 | (lexical-let ((root (vc-bzr-root file))) |
150 | (when root ; Short cut. | |
151 | ;; This looks at internal files. May break if they change | |
152 | ;; their format. | |
153 | (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root))) | |
154 | (if (not (file-readable-p dirstate)) | |
155 | (vc-bzr-state file) ; Expensive. | |
156 | (with-temp-buffer | |
157 | (insert-file-contents dirstate) | |
158 | (goto-char (point-min)) | |
159 | (if (not (looking-at "#bazaar dirstate flat format 3")) | |
160 | (vc-bzr-state file) ; Some other unknown format? | |
161 | (let* ((relfile (file-relative-name file root)) | |
162 | (reldir (file-name-directory relfile))) | |
82eb83ff SM |
163 | (if (re-search-forward |
164 | (concat "^\0" | |
165 | (if reldir (regexp-quote | |
166 | (directory-file-name reldir))) | |
167 | "\0" | |
168 | (regexp-quote (file-name-nondirectory relfile)) | |
169 | "\0" | |
769087ce SM |
170 | "[^\0]*\0" ;id? |
171 | "\\([^\0]*\\)\0" ;"a/f/d", a=removed? | |
172 | "[^\0]*\0" ;sha1 (empty if conflicted)? | |
173 | "\\([^\0]*\\)\0" ;size? | |
174 | "[^\0]*\0" ;"y/n", executable? | |
175 | "[^\0]*\0" ;? | |
176 | "\\([^\0]*\\)\0" ;"a/f/d" a=added? | |
177 | "\\([^\0]*\\)\0" ;sha1 again? | |
178 | "[^\0]*\0" ;size again? | |
179 | "[^\0]*\0" ;"y/n", executable again? | |
180 | "[^\0]*\0" ;last revid? | |
181 | ;; There are more fields when merges are pending. | |
182 | ) | |
82eb83ff | 183 | nil t) |
769087ce SM |
184 | ;; Apparently the second sha1 is the one we want: when |
185 | ;; there's a conflict, the first sha1 is absent (and the | |
186 | ;; first size seems to correspond to the file with | |
187 | ;; conflict markers). | |
82eb83ff | 188 | (cond |
769087ce | 189 | ((eq (char-after (match-beginning 1)) ?a) 'removed) |
82eb83ff | 190 | ((eq (char-after (match-beginning 3)) ?a) 'added) |
769087ce | 191 | ((and (eq (string-to-number (match-string 2)) |
82eb83ff | 192 | (nth 7 (file-attributes file))) |
769087ce | 193 | (equal (match-string 4) |
82eb83ff SM |
194 | (vc-bzr-sha1 file))) |
195 | 'up-to-date) | |
196 | (t 'edited)) | |
197 | 'unregistered))))))))) | |
198 | ||
199 | (defun vc-bzr-registered (file) | |
200 | "Return non-nil if FILE is registered with bzr." | |
201 | (let ((state (vc-bzr-state-heuristic file))) | |
202 | (not (memq state '(nil unregistered ignored))))) | |
77b5d458 SM |
203 | |
204 | (defconst vc-bzr-state-words | |
33e5d7d4 | 205 | "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown" |
77b5d458 SM |
206 | "Regexp matching file status words as reported in `bzr' output.") |
207 | ||
b6e6e09a SM |
208 | (defun vc-bzr-file-name-relative (filename) |
209 | "Return file name FILENAME stripped of the initial Bzr repository path." | |
210 | (lexical-let* | |
211 | ((filename* (expand-file-name filename)) | |
f4d0cf23 | 212 | (rootdir (vc-bzr-root filename*))) |
6e98ad29 | 213 | (when rootdir |
b6e6e09a SM |
214 | (file-relative-name filename* rootdir)))) |
215 | ||
33e5d7d4 SM |
216 | (defun vc-bzr-status (file) |
217 | "Return FILE status according to Bzr. | |
218 | Return value is a cons (STATUS . WARNING), where WARNING is a | |
fba500b6 SM |
219 | string or nil, and STATUS is one of the symbols: `added', |
220 | `ignored', `kindchanged', `modified', `removed', `renamed', `unknown', | |
33e5d7d4 SM |
221 | which directly correspond to `bzr status' output, or 'unchanged |
222 | for files whose copy in the working tree is identical to the one | |
223 | in the branch repository, or nil for files that are not | |
224 | registered with Bzr. | |
225 | ||
226 | If any error occurred in running `bzr status', then return nil." | |
77b5d458 | 227 | (with-temp-buffer |
fba500b6 SM |
228 | (let ((ret (condition-case nil |
229 | (vc-bzr-command "status" t 0 file) | |
230 | (file-error nil))) ; vc-bzr-program not found. | |
231 | (status 'unchanged)) | |
232 | ;; the only secure status indication in `bzr status' output | |
233 | ;; is a couple of lines following the pattern:: | |
234 | ;; | <status>: | |
235 | ;; | <file name> | |
236 | ;; if the file is up-to-date, we get no status report from `bzr', | |
237 | ;; so if the regexp search for the above pattern fails, we consider | |
238 | ;; the file to be up-to-date. | |
239 | (goto-char (point-min)) | |
240 | (when (re-search-forward | |
241 | ;; bzr prints paths relative to the repository root. | |
242 | (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+" | |
243 | (regexp-quote (vc-bzr-file-name-relative file)) | |
f4d0cf23 SM |
244 | ;; Bzr appends a '/' to directory names and |
245 | ;; '*' to executable files | |
246 | (if (file-directory-p file) "/?" "\\*?") | |
fba500b6 SM |
247 | "[ \t\n]*$") |
248 | nil t) | |
6e98ad29 | 249 | (lexical-let ((statusword (match-string 1))) |
fba500b6 SM |
250 | ;; Erase the status text that matched. |
251 | (delete-region (match-beginning 0) (match-end 0)) | |
33e5d7d4 | 252 | (setq status |
f4d0cf23 | 253 | (intern (replace-regexp-in-string " " "" statusword))))) |
fba500b6 SM |
254 | (when status |
255 | (goto-char (point-min)) | |
256 | (skip-chars-forward " \n\t") ;Throw away spaces. | |
257 | (cons status | |
258 | ;; "bzr" will output warnings and informational messages to | |
259 | ;; stderr; due to Emacs' `vc-do-command' (and, it seems, | |
260 | ;; `start-process' itself) limitations, we cannot catch stderr | |
261 | ;; and stdout into different buffers. So, if there's anything | |
262 | ;; left in the buffer after removing the above status | |
263 | ;; keywords, let us just presume that any other message from | |
264 | ;; "bzr" is a user warning, and display it. | |
265 | (unless (eobp) (buffer-substring (point) (point-max)))))))) | |
a0e5e075 | 266 | |
33e5d7d4 SM |
267 | (defun vc-bzr-state (file) |
268 | (lexical-let ((result (vc-bzr-status file))) | |
269 | (when (consp result) | |
270 | (if (cdr result) | |
271 | (message "Warnings in `bzr' output: %s" (cdr result))) | |
272 | (cdr (assq (car result) | |
6a3f9bb7 | 273 | '((added . added) |
fba500b6 | 274 | (kindchanged . edited) |
33e5d7d4 SM |
275 | (renamed . edited) |
276 | (modified . edited) | |
54bf3704 DN |
277 | (removed . removed) |
278 | (ignored . unregistered) | |
279 | (unknown . unregistered) | |
33e5d7d4 | 280 | (unchanged . up-to-date))))))) |
b6e0e86c | 281 | |
b0a08954 SM |
282 | (defun vc-bzr-resolve-when-done () |
283 | "Call \"bzr resolve\" if the conflict markers have been removed." | |
284 | (save-excursion | |
285 | (goto-char (point-min)) | |
286 | (unless (re-search-forward "^<<<<<<< " nil t) | |
287 | (vc-bzr-command "resolve" nil 0 buffer-file-name) | |
288 | ;; Remove the hook so that it is not called multiple times. | |
289 | (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t)))) | |
290 | ||
291 | (defun vc-bzr-find-file-hook () | |
292 | (when (and buffer-file-name | |
293 | ;; FIXME: We should check that "bzr status" says "conflict". | |
294 | (file-exists-p (concat buffer-file-name ".BASE")) | |
295 | (file-exists-p (concat buffer-file-name ".OTHER")) | |
296 | (file-exists-p (concat buffer-file-name ".THIS")) | |
297 | ;; If "bzr status" says there's a conflict but there are no | |
298 | ;; conflict markers, it's not clear what we should do. | |
299 | (save-excursion | |
300 | (goto-char (point-min)) | |
301 | (re-search-forward "^<<<<<<< " nil t))) | |
302 | ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable, | |
303 | ;; but the one in `bzr pull' isn't, so it would be good to provide an | |
304 | ;; elisp function to remerge from the .BASE/OTHER/THIS files. | |
305 | (smerge-start-session) | |
306 | (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t) | |
307 | (message "There are unresolved conflicts in this file"))) | |
308 | ||
b6e0e86c | 309 | (defun vc-bzr-workfile-unchanged-p (file) |
33e5d7d4 | 310 | (eq 'unchanged (car (vc-bzr-status file)))) |
b6e0e86c | 311 | |
ac3f4c6f | 312 | (defun vc-bzr-working-revision (file) |
82eb83ff SM |
313 | ;; Together with the code in vc-state-heuristic, this makes it possible |
314 | ;; to get the initial VC state of a Bzr file even if Bzr is not installed. | |
a460c94c SM |
315 | (lexical-let* |
316 | ((rootdir (vc-bzr-root file)) | |
6e98ad29 SM |
317 | (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file |
318 | rootdir)) | |
319 | (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir)) | |
320 | (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir))) | |
321 | ;; This looks at internal files to avoid forking a bzr process. | |
322 | ;; May break if they change their format. | |
a460c94c | 323 | (if (file-exists-p branch-format-file) |
fba500b6 | 324 | (with-temp-buffer |
a460c94c SM |
325 | (insert-file-contents branch-format-file) |
326 | (goto-char (point-min)) | |
327 | (cond | |
328 | ((or | |
329 | (looking-at "Bazaar-NG branch, format 0.0.4") | |
330 | (looking-at "Bazaar-NG branch format 5")) | |
331 | ;; count lines in .bzr/branch/revision-history | |
fba500b6 | 332 | (insert-file-contents revhistory-file) |
a460c94c SM |
333 | (number-to-string (count-lines (line-end-position) (point-max)))) |
334 | ((looking-at "Bazaar Branch Format 6 (bzr 0.15)") | |
335 | ;; revno is the first number in .bzr/branch/last-revision | |
336 | (insert-file-contents lastrev-file) | |
a460c94c SM |
337 | (if (re-search-forward "[0-9]+" nil t) |
338 | (buffer-substring (match-beginning 0) (match-end 0)))))) | |
339 | ;; fallback to calling "bzr revno" | |
b6e6e09a | 340 | (lexical-let* |
a460c94c SM |
341 | ((result (vc-bzr-command-discarding-stderr |
342 | vc-bzr-program "revno" file)) | |
b6e6e09a SM |
343 | (exitcode (car result)) |
344 | (output (cdr result))) | |
345 | (cond | |
346 | ((eq exitcode 0) (substring output 0 -1)) | |
347 | (t nil)))))) | |
b6e0e86c SM |
348 | |
349 | (defun vc-bzr-checkout-model (file) | |
350 | 'implicit) | |
351 | ||
a6ea7ffc | 352 | (defun vc-bzr-create-repo () |
4211679b | 353 | "Create a new Bzr repository." |
a6ea7ffc DN |
354 | (vc-bzr-command "init" nil 0 nil)) |
355 | ||
25a4ea6d | 356 | (defun vc-bzr-init-revision (&optional file) |
f4d0cf23 SM |
357 | "Always return nil, as Bzr cannot register explicit versions." |
358 | nil) | |
359 | ||
8cdd17b4 | 360 | (defun vc-bzr-register (files &optional rev comment) |
b6e0e86c SM |
361 | "Register FILE under bzr. |
362 | Signal an error unless REV is nil. | |
363 | COMMENT is ignored." | |
5b5afd50 | 364 | (if rev (error "Can't register explicit revision with bzr")) |
8cdd17b4 | 365 | (vc-bzr-command "add" nil 0 files)) |
b6e0e86c SM |
366 | |
367 | ;; Could run `bzr status' in the directory and see if it succeeds, but | |
368 | ;; that's relatively expensive. | |
b6e6e09a | 369 | (defalias 'vc-bzr-responsible-p 'vc-bzr-root |
b6e0e86c SM |
370 | "Return non-nil if FILE is (potentially) controlled by bzr. |
371 | The criterion is that there is a `.bzr' directory in the same | |
37320a58 | 372 | or a superior directory.") |
b6e0e86c SM |
373 | |
374 | (defun vc-bzr-could-register (file) | |
375 | "Return non-nil if FILE could be registered under bzr." | |
376 | (and (vc-bzr-responsible-p file) ; shortcut | |
377 | (condition-case () | |
378 | (with-temp-buffer | |
379 | (vc-bzr-command "add" t 0 file "--dry-run") | |
380 | ;; The command succeeds with no output if file is | |
381 | ;; registered (in bzr 0.8). | |
360cf7bc | 382 | (goto-char (point-min)) |
b6e0e86c SM |
383 | (looking-at "added ")) |
384 | (error)))) | |
385 | ||
386 | (defun vc-bzr-unregister (file) | |
387 | "Unregister FILE from bzr." | |
f4d0cf23 | 388 | (vc-bzr-command "remove" nil 0 file "--keep")) |
b6e0e86c | 389 | |
8cdd17b4 | 390 | (defun vc-bzr-checkin (files rev comment) |
b6e0e86c SM |
391 | "Check FILE in to bzr with log message COMMENT. |
392 | REV non-nil gets an error." | |
5b5afd50 | 393 | (if rev (error "Can't check in a specific revision with bzr")) |
8cdd17b4 | 394 | (vc-bzr-command "commit" nil 0 files "-m" comment)) |
b6e0e86c | 395 | |
f4d0cf23 SM |
396 | (defun vc-bzr-find-version (file rev buffer) |
397 | "Fetch version REV of file FILE and put it into BUFFER." | |
398 | (with-current-buffer buffer | |
399 | (if (and rev (stringp rev) (not (string= rev ""))) | |
400 | (vc-bzr-command "cat" t 0 file "-r" rev) | |
401 | (vc-bzr-command "cat" t 0 file)))) | |
402 | ||
b6e0e86c SM |
403 | (defun vc-bzr-checkout (file &optional editable rev destfile) |
404 | "Checkout revision REV of FILE from bzr to DESTFILE. | |
405 | EDITABLE is ignored." | |
406 | (unless destfile | |
407 | (setq destfile (vc-version-backup-file-name file rev))) | |
408 | (let ((coding-system-for-read 'binary) | |
409 | (coding-system-for-write 'binary)) | |
fba500b6 | 410 | (with-temp-file destfile |
f4d0cf23 | 411 | (if (and rev (stringp rev) (not (string= rev ""))) |
fba500b6 SM |
412 | (vc-bzr-command "cat" t 0 file "-r" rev) |
413 | (vc-bzr-command "cat" t 0 file))))) | |
b6e0e86c SM |
414 | |
415 | (defun vc-bzr-revert (file &optional contents-done) | |
416 | (unless contents-done | |
33e5d7d4 | 417 | (with-temp-buffer (vc-bzr-command "revert" t 0 file)))) |
b6e0e86c | 418 | |
37320a58 SM |
419 | (defvar log-view-message-re) |
420 | (defvar log-view-file-re) | |
421 | (defvar log-view-font-lock-keywords) | |
422 | (defvar log-view-current-tag-function) | |
423 | ||
424 | (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View" | |
425 | (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack. | |
b6e0e86c | 426 | (require 'add-log) |
ac51b151 | 427 | (set (make-local-variable 'log-view-file-re) "^Working file:[ \t]+\\(.+\\)") |
37320a58 | 428 | (set (make-local-variable 'log-view-message-re) |
f392f8b4 | 429 | "^ *-+\n *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)") |
b6e0e86c | 430 | (set (make-local-variable 'log-view-font-lock-keywords) |
37320a58 SM |
431 | ;; log-view-font-lock-keywords is careful to use the buffer-local |
432 | ;; value of log-view-message-re only since Emacs-23. | |
433 | (append `((,log-view-message-re . 'log-view-message-face)) | |
434 | ;; log-view-font-lock-keywords | |
435 | '(("^ *committer: \ | |
5ec05779 | 436 | \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]" |
37320a58 SM |
437 | (1 'change-log-name) |
438 | (2 'change-log-email)) | |
439 | ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))) | |
b6e0e86c | 440 | |
8cdd17b4 ER |
441 | (defun vc-bzr-print-log (files &optional buffer) ; get buffer arg in Emacs 22 |
442 | "Get bzr change log for FILES into specified BUFFER." | |
ac51b151 DN |
443 | ;; `vc-do-command' creates the buffer, but we need it before running |
444 | ;; the command. | |
445 | (vc-setup-buffer buffer) | |
446 | ;; If the buffer exists from a previous invocation it might be | |
447 | ;; read-only. | |
ac51b151 DN |
448 | ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so |
449 | ;; the log display may not what the user wants - but I see no other | |
450 | ;; way of getting the above regexps working. | |
451 | (dolist (file files) | |
5a07b0f0 SM |
452 | (vc-exec-after |
453 | `(let ((inhibit-read-only t)) | |
ac51b151 DN |
454 | (with-current-buffer buffer |
455 | ;; Insert the file name so that log-view.el can find it. | |
5a07b0f0 SM |
456 | (insert "Working file: " ',file "\n")) ;; Like RCS/CVS. |
457 | (apply 'vc-bzr-command "log" ',buffer 'async ',file | |
458 | ',(if (stringp vc-bzr-log-switches) | |
ac51b151 | 459 | (list vc-bzr-log-switches) |
5a07b0f0 | 460 | vc-bzr-log-switches)))))) |
b6e0e86c | 461 | |
5b5afd50 ER |
462 | (defun vc-bzr-show-log-entry (revision) |
463 | "Find entry for patch name REVISION in bzr change log buffer." | |
b6e0e86c SM |
464 | (goto-char (point-min)) |
465 | (let (case-fold-search) | |
f392f8b4 DN |
466 | (if (re-search-forward |
467 | ;; "revno:" can appear either at the beginning of a line, or indented. | |
468 | (concat "^[ ]*-+\n[ ]*revno: " | |
469 | ;; The revision can contain ".", quote it so that it | |
470 | ;; does not interfere with regexp matching. | |
471 | (regexp-quote revision) "$") nil t) | |
b6e0e86c SM |
472 | (beginning-of-line 0) |
473 | (goto-char (point-min))))) | |
474 | ||
8cdd17b4 | 475 | (defun vc-bzr-diff (files &optional rev1 rev2 buffer) |
b6e0e86c | 476 | "VC bzr backend for diff." |
39f44442 | 477 | ;; `bzr diff' exits with code 1 if diff is non-empty. |
5a07b0f0 | 478 | (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 'async files |
39f44442 SM |
479 | "--diff-options" (mapconcat 'identity |
480 | (vc-diff-switches-list bzr) | |
59ce725a | 481 | " ") |
39f44442 SM |
482 | ;; This `when' is just an optimization because bzr-1.2 is *much* |
483 | ;; faster when the revision argument is not given. | |
484 | (when (or rev1 rev2) | |
485 | (list "-r" (format "%s..%s" | |
486 | (or rev1 "revno:-1") | |
487 | (or rev2 "")))))) | |
b6e0e86c | 488 | |
b6e0e86c | 489 | |
5b5afd50 ER |
490 | ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with |
491 | ;; straight integer revisions. | |
b6e0e86c SM |
492 | |
493 | (defun vc-bzr-delete-file (file) | |
494 | "Delete FILE and delete it in the bzr repository." | |
495 | (condition-case () | |
496 | (delete-file file) | |
497 | (file-error nil)) | |
498 | (vc-bzr-command "remove" nil 0 file)) | |
499 | ||
500 | (defun vc-bzr-rename-file (old new) | |
501 | "Rename file from OLD to NEW using `bzr mv'." | |
502 | (vc-bzr-command "mv" nil 0 new old)) | |
503 | ||
504 | (defvar vc-bzr-annotation-table nil | |
505 | "Internal use.") | |
506 | (make-variable-buffer-local 'vc-bzr-annotation-table) | |
507 | ||
5b5afd50 | 508 | (defun vc-bzr-annotate-command (file buffer &optional revision) |
b6e0e86c SM |
509 | "Prepare BUFFER for `vc-annotate' on FILE. |
510 | Each line is tagged with the revision number, which has a `help-echo' | |
511 | property containing author and date information." | |
6e98ad29 | 512 | (apply #'vc-bzr-command "annotate" buffer 0 file "--long" "--all" |
a6762235 DN |
513 | (if revision (list "-r" revision))) |
514 | (with-current-buffer buffer | |
515 | ;; Store the tags for the annotated source lines in a hash table | |
516 | ;; to allow saving space by sharing the text properties. | |
517 | (setq vc-bzr-annotation-table (make-hash-table :test 'equal)) | |
518 | (goto-char (point-min)) | |
519 | (while (re-search-forward "^\\( *[0-9]+\\) +\\(.+\\) +\\([0-9]\\{8\\}\\) |" | |
520 | nil t) | |
521 | (let* ((rev (match-string 1)) | |
522 | (author (match-string 2)) | |
523 | (date (match-string 3)) | |
524 | (key (match-string 0)) | |
525 | (tag (gethash key vc-bzr-annotation-table))) | |
526 | (unless tag | |
527 | (setq tag (propertize rev 'help-echo (concat "Author: " author | |
528 | ", date: " date) | |
529 | 'mouse-face 'highlight)) | |
530 | (puthash key tag vc-bzr-annotation-table)) | |
531 | (replace-match "") | |
532 | (insert tag " |"))))) | |
b6e0e86c | 533 | |
b6e0e86c | 534 | (defun vc-bzr-annotate-time () |
a6762235 DN |
535 | (when (re-search-forward "^ *[0-9]+ |" nil t) |
536 | (let ((prop (get-text-property (line-beginning-position) 'help-echo))) | |
537 | (string-match "[0-9]+\\'" prop) | |
b6e0e86c SM |
538 | (vc-annotate-convert-time |
539 | (encode-time 0 0 0 | |
a6762235 DN |
540 | (string-to-number (substring (match-string 0 prop) 6 8)) |
541 | (string-to-number (substring (match-string 0 prop) 4 6)) | |
542 | (string-to-number (substring (match-string 0 prop) 0 4)) | |
543 | ))))) | |
b6e0e86c SM |
544 | |
545 | (defun vc-bzr-annotate-extract-revision-at-line () | |
546 | "Return revision for current line of annoation buffer, or nil. | |
547 | Return nil if current line isn't annotated." | |
548 | (save-excursion | |
549 | (beginning-of-line) | |
a6762235 DN |
550 | (if (looking-at " *\\([0-9]+\\) | ") |
551 | (match-string-no-properties 1)))) | |
b6e0e86c | 552 | |
a460c94c SM |
553 | (defun vc-bzr-command-discarding-stderr (command &rest args) |
554 | "Execute shell command COMMAND (with ARGS); return its output and exitcode. | |
b6e6e09a SM |
555 | Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is |
556 | the (numerical) exit code of the process, and OUTPUT is a string | |
557 | containing whatever the process sent to its standard output | |
558 | stream. Standard error output is discarded." | |
559 | (with-temp-buffer | |
a460c94c | 560 | (cons |
33e5d7d4 | 561 | (apply #'call-process command nil (list (current-buffer) nil) nil args) |
b6e6e09a | 562 | (buffer-substring (point-min) (point-max))))) |
b6e0e86c SM |
563 | |
564 | ;; TODO: it would be nice to mark the conflicted files in VC Dired, | |
565 | ;; and implement a command to run ediff and `bzr resolve' once the | |
566 | ;; changes have been merged. | |
567 | (defun vc-bzr-dir-state (dir &optional localp) | |
cdce374a | 568 | "Find the VC state of all files in DIR and its subdirectories. |
b6e0e86c | 569 | Optional argument LOCALP is always ignored." |
37320a58 SM |
570 | (let ((bzr-root-directory (vc-bzr-root dir)) |
571 | (at-start t) | |
572 | current-bzr-state current-vc-state) | |
573 | ;; Check that DIR is a bzr repository. | |
574 | (unless (file-name-absolute-p bzr-root-directory) | |
b6e0e86c SM |
575 | (error "Cannot find bzr repository for directory `%s'" dir)) |
576 | ;; `bzr ls --versioned' lists all versioned files; | |
577 | ;; assume they are up-to-date, unless we are given | |
578 | ;; evidence of the contrary. | |
37320a58 | 579 | (setq at-start t) |
b6e0e86c | 580 | (with-temp-buffer |
18e1f249 | 581 | (buffer-disable-undo) ;; Because these buffers can get huge |
cdce374a | 582 | (vc-bzr-command "ls" t 0 nil "--versioned") |
b6e0e86c | 583 | (goto-char (point-min)) |
37320a58 | 584 | (while (or at-start |
b6e0e86c | 585 | (eq 0 (forward-line))) |
37320a58 | 586 | (setq at-start nil) |
b6e0e86c SM |
587 | (let ((file (expand-file-name |
588 | (buffer-substring-no-properties | |
589 | (line-beginning-position) (line-end-position)) | |
590 | bzr-root-directory))) | |
f4d0cf23 SM |
591 | ;; files are up-to-date unless they appear in the `bzr |
592 | ;; status' output below | |
b6e0e86c SM |
593 | (vc-file-setprop file 'vc-state 'up-to-date) |
594 | ;; XXX: is this correct? what happens if one | |
595 | ;; mixes different SCMs in the same dir? | |
f4d0cf23 SM |
596 | ;; Anyway, we're looking at the output of `bzr ls --versioned', |
597 | ;; so we know these files are registered with Bzr. | |
4211679b | 598 | (vc-file-setprop file 'vc-backend 'Bzr)))) |
b6e0e86c | 599 | ;; `bzr status' reports on added/modified/renamed and unknown/ignored files |
37320a58 | 600 | (setq at-start t) |
b6e0e86c | 601 | (with-temp-buffer |
77b5d458 | 602 | (vc-bzr-command "status" t 0 nil) |
b6e0e86c | 603 | (goto-char (point-min)) |
37320a58 | 604 | (while (or at-start |
b6e0e86c | 605 | (eq 0 (forward-line))) |
37320a58 | 606 | (setq at-start nil) |
b6e0e86c SM |
607 | (cond |
608 | ((looking-at "^added") | |
484c1b1f | 609 | (setq current-vc-state 'added) |
37320a58 | 610 | (setq current-bzr-state 'added)) |
33e5d7d4 SM |
611 | ((looking-at "^kind changed") |
612 | (setq current-vc-state 'edited) | |
fba500b6 | 613 | (setq current-bzr-state 'kindchanged)) |
b6e0e86c | 614 | ((looking-at "^modified") |
37320a58 SM |
615 | (setq current-vc-state 'edited) |
616 | (setq current-bzr-state 'modified)) | |
b6e0e86c | 617 | ((looking-at "^renamed") |
37320a58 SM |
618 | (setq current-vc-state 'edited) |
619 | (setq current-bzr-state 'renamed)) | |
722f037f ER |
620 | ((looking-at "^ignored") |
621 | (setq current-vc-state 'ignored) | |
622 | (setq current-bzr-state 'not-versioned)) | |
623 | ((looking-at "^unknown") | |
624 | (setq current-vc-state 'unregistered) | |
37320a58 | 625 | (setq current-bzr-state 'not-versioned)) |
b6e0e86c SM |
626 | ((looking-at " ") |
627 | ;; file names are indented by two spaces | |
628 | (when current-vc-state | |
629 | (let ((file (expand-file-name | |
630 | (buffer-substring-no-properties | |
631 | (match-end 0) (line-end-position)) | |
632 | bzr-root-directory))) | |
633 | (vc-file-setprop file 'vc-state current-vc-state) | |
634 | (vc-file-setprop file 'vc-bzr-state current-bzr-state) | |
635 | (when (eq 'added current-bzr-state) | |
ac3f4c6f | 636 | (vc-file-setprop file 'vc-working-revision "0")))) |
b6e0e86c SM |
637 | (when (eq 'not-versioned current-bzr-state) |
638 | (let ((file (expand-file-name | |
639 | (buffer-substring-no-properties | |
640 | (match-end 0) (line-end-position)) | |
641 | bzr-root-directory))) | |
642 | (vc-file-setprop file 'vc-backend 'none) | |
643 | (vc-file-setprop file 'vc-state nil)))) | |
644 | (t | |
645 | ;; skip this part of `bzr status' output | |
37320a58 SM |
646 | (setq current-vc-state nil) |
647 | (setq current-bzr-state nil))))))) | |
b6e0e86c SM |
648 | |
649 | (defun vc-bzr-dired-state-info (file) | |
650 | "Bzr-specific version of `vc-dired-state-info'." | |
651 | (if (eq 'edited (vc-state file)) | |
f4d0cf23 SM |
652 | (concat "(" (symbol-name (or (vc-file-getprop file 'vc-bzr-state) |
653 | 'edited)) ")") | |
654 | ;; else fall back to default vc.el representation | |
655 | (vc-default-dired-state-info 'Bzr file))) | |
b6e0e86c | 656 | |
7ee8e7eb DN |
657 | ;; XXX Experimental function for the vc-dired replacement. |
658 | ;; XXX: this needs testing, it's probably incomplete. | |
659 | (defun vc-bzr-after-dir-status (update-function status-buffer) | |
660 | (let ((status-str nil) | |
661 | (file nil) | |
662 | (translation '(("+N" . added) | |
663 | ("-D" . removed) | |
664 | (" M" . edited) | |
665 | ;; XXX: what about ignored files? | |
49546869 | 666 | (" D" . missing) |
7ee8e7eb DN |
667 | ("? " . unregistered))) |
668 | (translated nil) | |
669 | (result nil)) | |
670 | (goto-char (point-min)) | |
671 | (while (not (eobp)) | |
672 | (setq status-str | |
673 | (buffer-substring-no-properties (point) (+ (point) 2))) | |
674 | (setq file | |
675 | (buffer-substring-no-properties (+ (point) 4) | |
676 | (line-end-position))) | |
677 | (setq translated (assoc status-str translation)) | |
678 | (push (cons file (cdr translated)) result) | |
679 | (forward-line)) | |
680 | ;; Remove the temporary buffer. | |
681 | (kill-buffer (current-buffer)) | |
682 | (funcall update-function result status-buffer))) | |
683 | ||
684 | ;; XXX Experimental function for the vc-dired replacement. | |
685 | ;; XXX This probably needs some further refinement and testing. | |
686 | (defun vc-bzr-dir-status (dir update-function status-buffer) | |
687 | "Return a list of conses (file . state) for DIR." | |
688 | (with-current-buffer | |
689 | (get-buffer-create | |
690 | (expand-file-name " *VC-bzr* tmp status" dir)) | |
691 | (erase-buffer) | |
692 | ;; XXX: Is this the right command to use? | |
693 | (vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S") | |
694 | (vc-exec-after | |
695 | `(vc-bzr-after-dir-status (quote ,update-function) ,status-buffer)) | |
696 | (current-buffer))) | |
697 | ||
39f44442 SM |
698 | ;;; Revision completion |
699 | ||
700 | (defun vc-bzr-complete-with-prefix (prefix action table string pred) | |
701 | (let ((comp (complete-with-action action table string pred))) | |
702 | (if (stringp comp) | |
703 | (concat prefix comp) | |
704 | comp))) | |
705 | ||
706 | (defun vc-bzr-revision-completion-table (files) | |
707 | (lexical-let ((files files)) | |
708 | ;; What about using `files'?!? --Stef | |
709 | (lambda (string pred action) | |
710 | (cond | |
711 | ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):" | |
712 | string) | |
713 | (vc-bzr-complete-with-prefix (substring string 0 (match-end 0)) | |
714 | action | |
715 | 'read-file-name-internal | |
716 | (substring string (match-end 0)) | |
717 | ;; Dropping `pred'. Maybe we should just | |
718 | ;; stash it in `read-file-name-predicate'? | |
719 | nil)) | |
720 | ((string-match "\\`\\(before\\):" string) | |
721 | (vc-bzr-complete-with-prefix (substring string 0 (match-end 0)) | |
722 | action | |
723 | (vc-bzr-revision-completion-table files) | |
724 | (substring string (match-end 0)) | |
725 | pred)) | |
726 | ((string-match "\\`\\(tag\\):" string) | |
727 | (let ((prefix (substring string 0 (match-end 0))) | |
728 | (tag (substring string (match-end 0))) | |
729 | (table nil)) | |
730 | (with-temp-buffer | |
731 | ;; "bzr-1.2 tags" is much faster with --show-ids. | |
732 | (call-process vc-bzr-program nil '(t) nil "tags" "--show-ids") | |
733 | ;; The output is ambiguous, unless we assume that revids do not | |
734 | ;; contain spaces. | |
735 | (goto-char (point-min)) | |
736 | (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t) | |
737 | (push (match-string-no-properties 1) table))) | |
738 | (vc-bzr-complete-with-prefix prefix action table tag pred))) | |
739 | ||
740 | ((string-match "\\`\\(revid\\):" string) | |
741 | ;; FIXME: How can I get a list of revision ids? | |
742 | ) | |
743 | (t | |
744 | (complete-with-action action '("revno:" "revid:" "last:" "before:" | |
745 | "tag:" "date:" "ancestor:" "branch:" | |
746 | "submit:") | |
747 | string pred)))))) | |
748 | ||
b6e0e86c | 749 | (eval-after-load "vc" |
b6e6e09a | 750 | '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t)) |
b6e0e86c | 751 | |
b6e0e86c SM |
752 | (provide 'vc-bzr) |
753 | ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06 | |
754 | ;;; vc-bzr.el ends here |