Commit | Line | Data |
---|---|---|
b6e0e86c SM |
1 | ;;; vc-bzr.el --- VC backend for the bzr revision control system |
2 | ||
3 | ;; Copyright (C) 2006, 2007 Free Software Foundation, Inc. | |
4 | ||
b6e0e86c SM |
5 | ;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com> |
6 | ;; Keywords: tools | |
7 | ;; Created: Sept 2006 | |
6e98ad29 | 8 | ;; Version: 2007-09-05 |
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 | ||
b6e0e86c SM |
29 | ;; See <URL:http://bazaar-vcs.org/> concerning bzr. |
30 | ||
77b5d458 SM |
31 | ;; Load this library to register bzr support in VC. It covers basic VC |
32 | ;; functionality, but was only lightly exercised with a few Emacs/bzr | |
33 | ;; version combinations, namely those current on the authors' PCs. | |
34 | ;; See various Fixmes below. | |
b6e0e86c | 35 | |
77b5d458 SM |
36 | |
37 | ;; Known bugs | |
38 | ;; ========== | |
39 | ||
40 | ;; When edititing a symlink and *both* the symlink and its target | |
41 | ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the | |
42 | ;; symlink, thereby not detecting whether the actual contents | |
43 | ;; (that is, the target contents) are changed. | |
44 | ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607 | |
45 | ||
46 | ;; For an up-to-date list of bugs, please see: | |
47 | ;; https://bugs.launchpad.net/vc-bzr/+bugs | |
b6e0e86c SM |
48 | |
49 | ||
50 | ;;; Code: | |
51 | ||
52 | (eval-when-compile | |
37320a58 | 53 | (require 'cl) |
b6e0e86c SM |
54 | (require 'vc)) ; for vc-exec-after |
55 | ||
360cf7bc SM |
56 | ;; Clear up the cache to force vc-call to check again and discover |
57 | ;; new functions when we reload this file. | |
4211679b | 58 | (put 'Bzr 'vc-functions nil) |
360cf7bc | 59 | |
b6e0e86c SM |
60 | (defgroup vc-bzr nil |
61 | "VC bzr backend." | |
fba500b6 | 62 | :version "22.2" |
b6e0e86c SM |
63 | :group 'vc) |
64 | ||
65 | (defcustom vc-bzr-program "bzr" | |
37320a58 | 66 | "Name of the bzr command (excluding any arguments)." |
b6e0e86c SM |
67 | :group 'vc-bzr |
68 | :type 'string) | |
69 | ||
70 | ;; Fixme: there's probably no call for this. | |
71 | (defcustom vc-bzr-program-args nil | |
37320a58 | 72 | "List of global arguments to pass to `vc-bzr-program'." |
b6e0e86c SM |
73 | :group 'vc-bzr |
74 | :type '(repeat string)) | |
75 | ||
76 | (defcustom vc-bzr-diff-switches nil | |
37320a58 | 77 | "String/list of strings specifying extra switches for bzr diff under VC." |
b6e0e86c SM |
78 | :type '(choice (const :tag "None" nil) |
79 | (string :tag "Argument String") | |
80 | (repeat :tag "Argument List" :value ("") string)) | |
81 | :group 'vc-bzr) | |
82 | ||
37320a58 SM |
83 | ;; since v0.9, bzr supports removing the progress indicators |
84 | ;; by setting environment variable BZR_PROGRESS_BAR to "none". | |
8cdd17b4 | 85 | (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args) |
37320a58 | 86 | "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND. |
b6e0e86c | 87 | Invoke the bzr command adding `BZR_PROGRESS_BAR=none' to the environment." |
37320a58 SM |
88 | (let ((process-environment |
89 | (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9) | |
90 | "LC_ALL=C" ; Force English output | |
a460c94c | 91 | process-environment))) |
37320a58 | 92 | (apply 'vc-do-command buffer okstatus vc-bzr-program |
8cdd17b4 | 93 | file-or-list bzr-command (append vc-bzr-program-args args)))) |
b6e0e86c | 94 | |
37320a58 SM |
95 | |
96 | ;;;###autoload | |
b6e6e09a SM |
97 | (defconst vc-bzr-admin-dirname ".bzr" ; FIXME: "_bzr" on w32? |
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) | |
121 | (vc-file-setprop | |
122 | file 'bzr-root | |
123 | (vc-find-root file vc-bzr-admin-checkout-format-file)))) | |
b6e0e86c SM |
124 | |
125 | (defun vc-bzr-registered (file) | |
a460c94c SM |
126 | "Return non-nil if FILE is registered with bzr. |
127 | ||
128 | For speed, this function tries first to parse Bzr internal file | |
129 | `checkout/dirstate', but it may fail if Bzr internal file format | |
130 | has changed. As a safeguard, the `checkout/dirstate' file is | |
131 | only parsed if it contains the string `#bazaar dirstate flat | |
132 | format 3' in the first line. | |
133 | ||
134 | If the `checkout/dirstate' file cannot be parsed, fall back to | |
135 | running `vc-bzr-state'." | |
fba500b6 SM |
136 | (lexical-let ((root (vc-bzr-root file))) |
137 | (when root ; Short cut. | |
138 | ;; This looks at internal files. May break if they change | |
139 | ;; their format. | |
140 | (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root))) | |
141 | (if (not (file-readable-p dirstate)) | |
142 | (vc-bzr-state file) ; Expensive. | |
143 | (with-temp-buffer | |
144 | (insert-file-contents dirstate) | |
145 | (goto-char (point-min)) | |
146 | (if (not (looking-at "#bazaar dirstate flat format 3")) | |
147 | (vc-bzr-state file) ; Some other unknown format? | |
148 | (let* ((relfile (file-relative-name file root)) | |
149 | (reldir (file-name-directory relfile))) | |
150 | (re-search-forward | |
151 | (concat "^\0" | |
152 | (if reldir (regexp-quote (directory-file-name reldir))) | |
153 | "\0" | |
154 | (regexp-quote (file-name-nondirectory relfile)) | |
155 | "\0") | |
156 | nil t))))))))) | |
77b5d458 SM |
157 | |
158 | (defconst vc-bzr-state-words | |
33e5d7d4 | 159 | "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown" |
77b5d458 SM |
160 | "Regexp matching file status words as reported in `bzr' output.") |
161 | ||
b6e6e09a SM |
162 | (defun vc-bzr-file-name-relative (filename) |
163 | "Return file name FILENAME stripped of the initial Bzr repository path." | |
164 | (lexical-let* | |
165 | ((filename* (expand-file-name filename)) | |
166 | (rootdir (vc-bzr-root (file-name-directory filename*)))) | |
6e98ad29 | 167 | (when rootdir |
b6e6e09a SM |
168 | (file-relative-name filename* rootdir)))) |
169 | ||
77b5d458 | 170 | ;; FIXME: Also get this in a non-registered sub-directory. |
33e5d7d4 SM |
171 | ;; It already works for me. -- Riccardo |
172 | (defun vc-bzr-status (file) | |
173 | "Return FILE status according to Bzr. | |
174 | Return value is a cons (STATUS . WARNING), where WARNING is a | |
fba500b6 SM |
175 | string or nil, and STATUS is one of the symbols: `added', |
176 | `ignored', `kindchanged', `modified', `removed', `renamed', `unknown', | |
33e5d7d4 SM |
177 | which directly correspond to `bzr status' output, or 'unchanged |
178 | for files whose copy in the working tree is identical to the one | |
179 | in the branch repository, or nil for files that are not | |
180 | registered with Bzr. | |
181 | ||
182 | If any error occurred in running `bzr status', then return nil." | |
77b5d458 | 183 | (with-temp-buffer |
fba500b6 SM |
184 | (let ((ret (condition-case nil |
185 | (vc-bzr-command "status" t 0 file) | |
186 | (file-error nil))) ; vc-bzr-program not found. | |
187 | (status 'unchanged)) | |
188 | ;; the only secure status indication in `bzr status' output | |
189 | ;; is a couple of lines following the pattern:: | |
190 | ;; | <status>: | |
191 | ;; | <file name> | |
192 | ;; if the file is up-to-date, we get no status report from `bzr', | |
193 | ;; so if the regexp search for the above pattern fails, we consider | |
194 | ;; the file to be up-to-date. | |
195 | (goto-char (point-min)) | |
196 | (when (re-search-forward | |
197 | ;; bzr prints paths relative to the repository root. | |
198 | (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+" | |
199 | (regexp-quote (vc-bzr-file-name-relative file)) | |
200 | (if (file-directory-p file) "/?" "") | |
201 | "[ \t\n]*$") | |
202 | nil t) | |
6e98ad29 | 203 | (lexical-let ((statusword (match-string 1))) |
fba500b6 SM |
204 | ;; Erase the status text that matched. |
205 | (delete-region (match-beginning 0) (match-end 0)) | |
33e5d7d4 | 206 | (setq status |
fba500b6 SM |
207 | (and (equal ret 0) ; Seems redundant. --Stef |
208 | (intern (replace-regexp-in-string " " "" | |
6e98ad29 | 209 | statusword)))))) |
fba500b6 SM |
210 | (when status |
211 | (goto-char (point-min)) | |
212 | (skip-chars-forward " \n\t") ;Throw away spaces. | |
213 | (cons status | |
214 | ;; "bzr" will output warnings and informational messages to | |
215 | ;; stderr; due to Emacs' `vc-do-command' (and, it seems, | |
216 | ;; `start-process' itself) limitations, we cannot catch stderr | |
217 | ;; and stdout into different buffers. So, if there's anything | |
218 | ;; left in the buffer after removing the above status | |
219 | ;; keywords, let us just presume that any other message from | |
220 | ;; "bzr" is a user warning, and display it. | |
221 | (unless (eobp) (buffer-substring (point) (point-max)))))))) | |
a0e5e075 | 222 | |
33e5d7d4 SM |
223 | (defun vc-bzr-state (file) |
224 | (lexical-let ((result (vc-bzr-status file))) | |
225 | (when (consp result) | |
226 | (if (cdr result) | |
227 | (message "Warnings in `bzr' output: %s" (cdr result))) | |
228 | (cdr (assq (car result) | |
229 | '((added . edited) | |
fba500b6 | 230 | (kindchanged . edited) |
33e5d7d4 SM |
231 | (renamed . edited) |
232 | (modified . edited) | |
233 | (removed . edited) | |
234 | (ignored . nil) | |
235 | (unknown . nil) | |
236 | (unchanged . up-to-date))))))) | |
b6e0e86c SM |
237 | |
238 | (defun vc-bzr-workfile-unchanged-p (file) | |
33e5d7d4 | 239 | (eq 'unchanged (car (vc-bzr-status file)))) |
b6e0e86c | 240 | |
ac3f4c6f | 241 | (defun vc-bzr-working-revision (file) |
a460c94c SM |
242 | (lexical-let* |
243 | ((rootdir (vc-bzr-root file)) | |
6e98ad29 SM |
244 | (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file |
245 | rootdir)) | |
246 | (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir)) | |
247 | (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir))) | |
248 | ;; This looks at internal files to avoid forking a bzr process. | |
249 | ;; May break if they change their format. | |
a460c94c | 250 | (if (file-exists-p branch-format-file) |
fba500b6 | 251 | (with-temp-buffer |
a460c94c SM |
252 | (insert-file-contents branch-format-file) |
253 | (goto-char (point-min)) | |
254 | (cond | |
255 | ((or | |
256 | (looking-at "Bazaar-NG branch, format 0.0.4") | |
257 | (looking-at "Bazaar-NG branch format 5")) | |
258 | ;; count lines in .bzr/branch/revision-history | |
fba500b6 | 259 | (insert-file-contents revhistory-file) |
a460c94c SM |
260 | (number-to-string (count-lines (line-end-position) (point-max)))) |
261 | ((looking-at "Bazaar Branch Format 6 (bzr 0.15)") | |
262 | ;; revno is the first number in .bzr/branch/last-revision | |
263 | (insert-file-contents lastrev-file) | |
a460c94c SM |
264 | (if (re-search-forward "[0-9]+" nil t) |
265 | (buffer-substring (match-beginning 0) (match-end 0)))))) | |
266 | ;; fallback to calling "bzr revno" | |
b6e6e09a | 267 | (lexical-let* |
a460c94c SM |
268 | ((result (vc-bzr-command-discarding-stderr |
269 | vc-bzr-program "revno" file)) | |
b6e6e09a SM |
270 | (exitcode (car result)) |
271 | (output (cdr result))) | |
272 | (cond | |
273 | ((eq exitcode 0) (substring output 0 -1)) | |
274 | (t nil)))))) | |
b6e0e86c SM |
275 | |
276 | (defun vc-bzr-checkout-model (file) | |
277 | 'implicit) | |
278 | ||
a6ea7ffc | 279 | (defun vc-bzr-create-repo () |
4211679b | 280 | "Create a new Bzr repository." |
a6ea7ffc DN |
281 | (vc-bzr-command "init" nil 0 nil)) |
282 | ||
8cdd17b4 | 283 | (defun vc-bzr-register (files &optional rev comment) |
b6e0e86c SM |
284 | "Register FILE under bzr. |
285 | Signal an error unless REV is nil. | |
286 | COMMENT is ignored." | |
5b5afd50 | 287 | (if rev (error "Can't register explicit revision with bzr")) |
8cdd17b4 | 288 | (vc-bzr-command "add" nil 0 files)) |
b6e0e86c SM |
289 | |
290 | ;; Could run `bzr status' in the directory and see if it succeeds, but | |
291 | ;; that's relatively expensive. | |
b6e6e09a | 292 | (defalias 'vc-bzr-responsible-p 'vc-bzr-root |
b6e0e86c SM |
293 | "Return non-nil if FILE is (potentially) controlled by bzr. |
294 | The criterion is that there is a `.bzr' directory in the same | |
37320a58 | 295 | or a superior directory.") |
b6e0e86c SM |
296 | |
297 | (defun vc-bzr-could-register (file) | |
298 | "Return non-nil if FILE could be registered under bzr." | |
299 | (and (vc-bzr-responsible-p file) ; shortcut | |
300 | (condition-case () | |
301 | (with-temp-buffer | |
302 | (vc-bzr-command "add" t 0 file "--dry-run") | |
303 | ;; The command succeeds with no output if file is | |
304 | ;; registered (in bzr 0.8). | |
360cf7bc | 305 | (goto-char (point-min)) |
b6e0e86c SM |
306 | (looking-at "added ")) |
307 | (error)))) | |
308 | ||
309 | (defun vc-bzr-unregister (file) | |
310 | "Unregister FILE from bzr." | |
311 | (vc-bzr-command "remove" nil 0 file)) | |
312 | ||
8cdd17b4 | 313 | (defun vc-bzr-checkin (files rev comment) |
b6e0e86c SM |
314 | "Check FILE in to bzr with log message COMMENT. |
315 | REV non-nil gets an error." | |
5b5afd50 | 316 | (if rev (error "Can't check in a specific revision with bzr")) |
8cdd17b4 | 317 | (vc-bzr-command "commit" nil 0 files "-m" comment)) |
b6e0e86c SM |
318 | |
319 | (defun vc-bzr-checkout (file &optional editable rev destfile) | |
320 | "Checkout revision REV of FILE from bzr to DESTFILE. | |
321 | EDITABLE is ignored." | |
322 | (unless destfile | |
323 | (setq destfile (vc-version-backup-file-name file rev))) | |
324 | (let ((coding-system-for-read 'binary) | |
325 | (coding-system-for-write 'binary)) | |
fba500b6 SM |
326 | (with-temp-file destfile |
327 | (if rev | |
328 | (vc-bzr-command "cat" t 0 file "-r" rev) | |
329 | (vc-bzr-command "cat" t 0 file))))) | |
b6e0e86c SM |
330 | |
331 | (defun vc-bzr-revert (file &optional contents-done) | |
332 | (unless contents-done | |
33e5d7d4 | 333 | (with-temp-buffer (vc-bzr-command "revert" t 0 file)))) |
b6e0e86c | 334 | |
37320a58 SM |
335 | (defvar log-view-message-re) |
336 | (defvar log-view-file-re) | |
337 | (defvar log-view-font-lock-keywords) | |
338 | (defvar log-view-current-tag-function) | |
339 | ||
340 | (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View" | |
341 | (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack. | |
b6e0e86c SM |
342 | (require 'add-log) |
343 | ;; Don't have file markers, so use impossible regexp. | |
344 | (set (make-local-variable 'log-view-file-re) "\\'\\`") | |
37320a58 SM |
345 | (set (make-local-variable 'log-view-message-re) |
346 | "^ *-+\n *\\(?:revno: \\([0-9]+\\)\\|merged: .+\\)") | |
b6e0e86c | 347 | (set (make-local-variable 'log-view-font-lock-keywords) |
37320a58 SM |
348 | ;; log-view-font-lock-keywords is careful to use the buffer-local |
349 | ;; value of log-view-message-re only since Emacs-23. | |
350 | (append `((,log-view-message-re . 'log-view-message-face)) | |
351 | ;; log-view-font-lock-keywords | |
352 | '(("^ *committer: \ | |
5ec05779 | 353 | \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]" |
37320a58 SM |
354 | (1 'change-log-name) |
355 | (2 'change-log-email)) | |
356 | ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))) | |
b6e0e86c | 357 | |
8cdd17b4 ER |
358 | (defun vc-bzr-print-log (files &optional buffer) ; get buffer arg in Emacs 22 |
359 | "Get bzr change log for FILES into specified BUFFER." | |
b6e0e86c SM |
360 | ;; Fixme: This might need the locale fixing up if things like `revno' |
361 | ;; got localized, but certainly it shouldn't use LC_ALL=C. | |
8cdd17b4 | 362 | (vc-bzr-command "log" buffer 0 files) |
37320a58 SM |
363 | ;; FIXME: Until Emacs-23, VC was missing a hook to sort out the mode for |
364 | ;; the buffer, or at least set the regexps right. | |
365 | (unless (fboundp 'vc-default-log-view-mode) | |
366 | (add-hook 'log-view-mode-hook 'vc-bzr-log-view-mode))) | |
b6e0e86c | 367 | |
5b5afd50 ER |
368 | (defun vc-bzr-show-log-entry (revision) |
369 | "Find entry for patch name REVISION in bzr change log buffer." | |
b6e0e86c SM |
370 | (goto-char (point-min)) |
371 | (let (case-fold-search) | |
5b5afd50 | 372 | (if (re-search-forward (concat "^-+\nrevno: " revision "$") nil t) |
b6e0e86c SM |
373 | (beginning-of-line 0) |
374 | (goto-char (point-min))))) | |
375 | ||
b6e0e86c SM |
376 | (autoload 'vc-diff-switches-list "vc" nil nil t) |
377 | ||
8cdd17b4 | 378 | (defun vc-bzr-diff (files &optional rev1 rev2 buffer) |
b6e0e86c | 379 | "VC bzr backend for diff." |
ac3f4c6f | 380 | (let ((working (vc-working-revision (if (consp files) (car files) files)))) |
b6e0e86c SM |
381 | (if (and (equal rev1 working) (not rev2)) |
382 | (setq rev1 nil)) | |
383 | (if (and (not rev1) rev2) | |
384 | (setq rev1 working)) | |
b6e0e86c | 385 | ;; bzr diff produces condition code 1 for some reason. |
8cdd17b4 | 386 | (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 1 files |
b6e0e86c SM |
387 | "--diff-options" (mapconcat 'identity (vc-diff-switches-list bzr) |
388 | " ") | |
389 | (when rev1 | |
390 | (if rev2 | |
391 | (list "-r" (format "%s..%s" rev1 rev2)) | |
392 | (list "-r" rev1)))))) | |
393 | ||
b6e0e86c | 394 | |
b6e0e86c | 395 | |
5b5afd50 ER |
396 | ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with |
397 | ;; straight integer revisions. | |
b6e0e86c SM |
398 | |
399 | (defun vc-bzr-delete-file (file) | |
400 | "Delete FILE and delete it in the bzr repository." | |
401 | (condition-case () | |
402 | (delete-file file) | |
403 | (file-error nil)) | |
404 | (vc-bzr-command "remove" nil 0 file)) | |
405 | ||
406 | (defun vc-bzr-rename-file (old new) | |
407 | "Rename file from OLD to NEW using `bzr mv'." | |
408 | (vc-bzr-command "mv" nil 0 new old)) | |
409 | ||
410 | (defvar vc-bzr-annotation-table nil | |
411 | "Internal use.") | |
412 | (make-variable-buffer-local 'vc-bzr-annotation-table) | |
413 | ||
5b5afd50 | 414 | (defun vc-bzr-annotate-command (file buffer &optional revision) |
b6e0e86c SM |
415 | "Prepare BUFFER for `vc-annotate' on FILE. |
416 | Each line is tagged with the revision number, which has a `help-echo' | |
417 | property containing author and date information." | |
6e98ad29 | 418 | (apply #'vc-bzr-command "annotate" buffer 0 file "--long" "--all" |
5b5afd50 | 419 | (if revision (list "-r" revision))) |
b6e0e86c SM |
420 | (with-current-buffer buffer |
421 | ;; Store the tags for the annotated source lines in a hash table | |
422 | ;; to allow saving space by sharing the text properties. | |
423 | (setq vc-bzr-annotation-table (make-hash-table :test 'equal)) | |
424 | (goto-char (point-min)) | |
6e98ad29 | 425 | (while (re-search-forward "^\\( *[0-9]+\\) +\\(.+\\) +\\([0-9]\\{8\\}\\) |" |
b6e0e86c SM |
426 | nil t) |
427 | (let* ((rev (match-string 1)) | |
428 | (author (match-string 2)) | |
429 | (date (match-string 3)) | |
430 | (key (match-string 0)) | |
431 | (tag (gethash key vc-bzr-annotation-table))) | |
432 | (unless tag | |
b6e0e86c SM |
433 | (setq tag (propertize rev 'help-echo (concat "Author: " author |
434 | ", date: " date) | |
435 | 'mouse-face 'highlight)) | |
436 | (puthash key tag vc-bzr-annotation-table)) | |
437 | (replace-match "") | |
438 | (insert tag " |"))))) | |
439 | ||
440 | ;; Definition from Emacs 22 | |
441 | (unless (fboundp 'vc-annotate-convert-time) | |
fba500b6 SM |
442 | (defun vc-annotate-convert-time (time) |
443 | "Convert a time value to a floating-point number of days. | |
b6e0e86c SM |
444 | The argument TIME is a list as returned by `current-time' or |
445 | `encode-time', only the first two elements of that list are considered." | |
fba500b6 | 446 | (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))) |
b6e0e86c SM |
447 | |
448 | (defun vc-bzr-annotate-time () | |
449 | (when (re-search-forward "^ *[0-9]+ |" nil t) | |
450 | (let ((prop (get-text-property (line-beginning-position) 'help-echo))) | |
451 | (string-match "[0-9]+\\'" prop) | |
452 | (vc-annotate-convert-time | |
453 | (encode-time 0 0 0 | |
454 | (string-to-number (substring (match-string 0 prop) 6 8)) | |
455 | (string-to-number (substring (match-string 0 prop) 4 6)) | |
456 | (string-to-number (substring (match-string 0 prop) 0 4)) | |
457 | ))))) | |
458 | ||
459 | (defun vc-bzr-annotate-extract-revision-at-line () | |
460 | "Return revision for current line of annoation buffer, or nil. | |
461 | Return nil if current line isn't annotated." | |
462 | (save-excursion | |
463 | (beginning-of-line) | |
464 | (if (looking-at " *\\([0-9]+\\) | ") | |
465 | (match-string-no-properties 1)))) | |
466 | ||
467 | ;; Not needed for Emacs 22 | |
468 | (defun vc-bzr-annotate-difference (point) | |
469 | (let ((next-time (vc-bzr-annotate-time))) | |
470 | (if next-time | |
471 | (- (vc-annotate-convert-time (current-time)) next-time)))) | |
472 | ||
a460c94c SM |
473 | (defun vc-bzr-command-discarding-stderr (command &rest args) |
474 | "Execute shell command COMMAND (with ARGS); return its output and exitcode. | |
b6e6e09a SM |
475 | Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is |
476 | the (numerical) exit code of the process, and OUTPUT is a string | |
477 | containing whatever the process sent to its standard output | |
478 | stream. Standard error output is discarded." | |
479 | (with-temp-buffer | |
a460c94c | 480 | (cons |
33e5d7d4 | 481 | (apply #'call-process command nil (list (current-buffer) nil) nil args) |
b6e6e09a | 482 | (buffer-substring (point-min) (point-max))))) |
b6e0e86c SM |
483 | |
484 | ;; TODO: it would be nice to mark the conflicted files in VC Dired, | |
485 | ;; and implement a command to run ediff and `bzr resolve' once the | |
486 | ;; changes have been merged. | |
487 | (defun vc-bzr-dir-state (dir &optional localp) | |
cdce374a | 488 | "Find the VC state of all files in DIR and its subdirectories. |
b6e0e86c | 489 | Optional argument LOCALP is always ignored." |
37320a58 SM |
490 | (let ((bzr-root-directory (vc-bzr-root dir)) |
491 | (at-start t) | |
492 | current-bzr-state current-vc-state) | |
493 | ;; Check that DIR is a bzr repository. | |
494 | (unless (file-name-absolute-p bzr-root-directory) | |
b6e0e86c SM |
495 | (error "Cannot find bzr repository for directory `%s'" dir)) |
496 | ;; `bzr ls --versioned' lists all versioned files; | |
497 | ;; assume they are up-to-date, unless we are given | |
498 | ;; evidence of the contrary. | |
37320a58 | 499 | (setq at-start t) |
b6e0e86c | 500 | (with-temp-buffer |
18e1f249 | 501 | (buffer-disable-undo) ;; Because these buffers can get huge |
cdce374a | 502 | (vc-bzr-command "ls" t 0 nil "--versioned") |
b6e0e86c | 503 | (goto-char (point-min)) |
37320a58 | 504 | (while (or at-start |
b6e0e86c | 505 | (eq 0 (forward-line))) |
37320a58 | 506 | (setq at-start nil) |
b6e0e86c SM |
507 | (let ((file (expand-file-name |
508 | (buffer-substring-no-properties | |
509 | (line-beginning-position) (line-end-position)) | |
510 | bzr-root-directory))) | |
511 | (vc-file-setprop file 'vc-state 'up-to-date) | |
512 | ;; XXX: is this correct? what happens if one | |
513 | ;; mixes different SCMs in the same dir? | |
4211679b | 514 | (vc-file-setprop file 'vc-backend 'Bzr)))) |
b6e0e86c | 515 | ;; `bzr status' reports on added/modified/renamed and unknown/ignored files |
37320a58 | 516 | (setq at-start t) |
b6e0e86c | 517 | (with-temp-buffer |
77b5d458 | 518 | (vc-bzr-command "status" t 0 nil) |
b6e0e86c | 519 | (goto-char (point-min)) |
37320a58 | 520 | (while (or at-start |
b6e0e86c | 521 | (eq 0 (forward-line))) |
37320a58 | 522 | (setq at-start nil) |
b6e0e86c SM |
523 | (cond |
524 | ((looking-at "^added") | |
484c1b1f | 525 | (setq current-vc-state 'added) |
37320a58 | 526 | (setq current-bzr-state 'added)) |
33e5d7d4 SM |
527 | ((looking-at "^kind changed") |
528 | (setq current-vc-state 'edited) | |
fba500b6 | 529 | (setq current-bzr-state 'kindchanged)) |
b6e0e86c | 530 | ((looking-at "^modified") |
37320a58 SM |
531 | (setq current-vc-state 'edited) |
532 | (setq current-bzr-state 'modified)) | |
b6e0e86c | 533 | ((looking-at "^renamed") |
37320a58 SM |
534 | (setq current-vc-state 'edited) |
535 | (setq current-bzr-state 'renamed)) | |
722f037f ER |
536 | ((looking-at "^ignored") |
537 | (setq current-vc-state 'ignored) | |
538 | (setq current-bzr-state 'not-versioned)) | |
539 | ((looking-at "^unknown") | |
540 | (setq current-vc-state 'unregistered) | |
37320a58 | 541 | (setq current-bzr-state 'not-versioned)) |
b6e0e86c SM |
542 | ((looking-at " ") |
543 | ;; file names are indented by two spaces | |
544 | (when current-vc-state | |
545 | (let ((file (expand-file-name | |
546 | (buffer-substring-no-properties | |
547 | (match-end 0) (line-end-position)) | |
548 | bzr-root-directory))) | |
549 | (vc-file-setprop file 'vc-state current-vc-state) | |
550 | (vc-file-setprop file 'vc-bzr-state current-bzr-state) | |
551 | (when (eq 'added current-bzr-state) | |
ac3f4c6f | 552 | (vc-file-setprop file 'vc-working-revision "0")))) |
b6e0e86c SM |
553 | (when (eq 'not-versioned current-bzr-state) |
554 | (let ((file (expand-file-name | |
555 | (buffer-substring-no-properties | |
556 | (match-end 0) (line-end-position)) | |
557 | bzr-root-directory))) | |
558 | (vc-file-setprop file 'vc-backend 'none) | |
559 | (vc-file-setprop file 'vc-state nil)))) | |
560 | (t | |
561 | ;; skip this part of `bzr status' output | |
37320a58 SM |
562 | (setq current-vc-state nil) |
563 | (setq current-bzr-state nil))))))) | |
b6e0e86c SM |
564 | |
565 | (defun vc-bzr-dired-state-info (file) | |
566 | "Bzr-specific version of `vc-dired-state-info'." | |
567 | (if (eq 'edited (vc-state file)) | |
568 | (let ((bzr-state (vc-file-getprop file 'vc-bzr-state))) | |
569 | (if bzr-state | |
570 | (concat "(" (symbol-name bzr-state) ")") | |
571 | ;; else fall back to default vc representation | |
4211679b | 572 | (vc-default-dired-state-info 'Bzr file))))) |
b6e0e86c | 573 | |
b6e0e86c | 574 | (eval-after-load "vc" |
b6e6e09a | 575 | '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t)) |
b6e0e86c | 576 | |
b6e0e86c SM |
577 | (provide 'vc-bzr) |
578 | ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06 | |
579 | ;;; vc-bzr.el ends here |