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 | |
b6e6e09a | 8 | ;; Version: 2007-08-03 |
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")) |
37320a58 SM |
108 | |
109 | ;;;###autoload (defun vc-bzr-registered (file) | |
b6e6e09a | 110 | ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file) |
37320a58 SM |
111 | ;;;###autoload (progn |
112 | ;;;###autoload (load "vc-bzr") | |
113 | ;;;###autoload (vc-bzr-registered file)))) | |
b6e0e86c | 114 | |
a460c94c SM |
115 | (defun vc-bzr-root (file) |
116 | "Return the root directory of the bzr repository containing FILE." | |
117 | ;; Cache technique copied from vc-arch.el. | |
118 | (or (vc-file-getprop file 'bzr-root) | |
119 | (vc-file-setprop | |
120 | file 'bzr-root | |
121 | (vc-find-root file vc-bzr-admin-checkout-format-file)))) | |
b6e0e86c SM |
122 | |
123 | (defun vc-bzr-registered (file) | |
a460c94c SM |
124 | "Return non-nil if FILE is registered with bzr. |
125 | ||
126 | For speed, this function tries first to parse Bzr internal file | |
127 | `checkout/dirstate', but it may fail if Bzr internal file format | |
128 | has changed. As a safeguard, the `checkout/dirstate' file is | |
129 | only parsed if it contains the string `#bazaar dirstate flat | |
130 | format 3' in the first line. | |
131 | ||
132 | If the `checkout/dirstate' file cannot be parsed, fall back to | |
133 | running `vc-bzr-state'." | |
fba500b6 SM |
134 | (lexical-let ((root (vc-bzr-root file))) |
135 | (when root ; Short cut. | |
136 | ;; This looks at internal files. May break if they change | |
137 | ;; their format. | |
138 | (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root))) | |
139 | (if (not (file-readable-p dirstate)) | |
140 | (vc-bzr-state file) ; Expensive. | |
141 | (with-temp-buffer | |
142 | (insert-file-contents dirstate) | |
143 | (goto-char (point-min)) | |
144 | (if (not (looking-at "#bazaar dirstate flat format 3")) | |
145 | (vc-bzr-state file) ; Some other unknown format? | |
146 | (let* ((relfile (file-relative-name file root)) | |
147 | (reldir (file-name-directory relfile))) | |
148 | (re-search-forward | |
149 | (concat "^\0" | |
150 | (if reldir (regexp-quote (directory-file-name reldir))) | |
151 | "\0" | |
152 | (regexp-quote (file-name-nondirectory relfile)) | |
153 | "\0") | |
154 | nil t))))))))) | |
77b5d458 SM |
155 | |
156 | (defconst vc-bzr-state-words | |
33e5d7d4 | 157 | "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown" |
77b5d458 SM |
158 | "Regexp matching file status words as reported in `bzr' output.") |
159 | ||
b6e6e09a SM |
160 | (defun vc-bzr-file-name-relative (filename) |
161 | "Return file name FILENAME stripped of the initial Bzr repository path." | |
162 | (lexical-let* | |
163 | ((filename* (expand-file-name filename)) | |
164 | (rootdir (vc-bzr-root (file-name-directory filename*)))) | |
165 | (and rootdir | |
166 | (file-relative-name filename* rootdir)))) | |
167 | ||
77b5d458 | 168 | ;; FIXME: Also get this in a non-registered sub-directory. |
33e5d7d4 SM |
169 | ;; It already works for me. -- Riccardo |
170 | (defun vc-bzr-status (file) | |
171 | "Return FILE status according to Bzr. | |
172 | Return value is a cons (STATUS . WARNING), where WARNING is a | |
fba500b6 SM |
173 | string or nil, and STATUS is one of the symbols: `added', |
174 | `ignored', `kindchanged', `modified', `removed', `renamed', `unknown', | |
33e5d7d4 SM |
175 | which directly correspond to `bzr status' output, or 'unchanged |
176 | for files whose copy in the working tree is identical to the one | |
177 | in the branch repository, or nil for files that are not | |
178 | registered with Bzr. | |
179 | ||
180 | If any error occurred in running `bzr status', then return nil." | |
77b5d458 | 181 | (with-temp-buffer |
fba500b6 SM |
182 | (let ((ret (condition-case nil |
183 | (vc-bzr-command "status" t 0 file) | |
184 | (file-error nil))) ; vc-bzr-program not found. | |
185 | (status 'unchanged)) | |
186 | ;; the only secure status indication in `bzr status' output | |
187 | ;; is a couple of lines following the pattern:: | |
188 | ;; | <status>: | |
189 | ;; | <file name> | |
190 | ;; if the file is up-to-date, we get no status report from `bzr', | |
191 | ;; so if the regexp search for the above pattern fails, we consider | |
192 | ;; the file to be up-to-date. | |
193 | (goto-char (point-min)) | |
194 | (when (re-search-forward | |
195 | ;; bzr prints paths relative to the repository root. | |
196 | (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+" | |
197 | (regexp-quote (vc-bzr-file-name-relative file)) | |
198 | (if (file-directory-p file) "/?" "") | |
199 | "[ \t\n]*$") | |
200 | nil t) | |
201 | (let ((status (match-string 1))) | |
202 | ;; Erase the status text that matched. | |
203 | (delete-region (match-beginning 0) (match-end 0)) | |
33e5d7d4 | 204 | (setq status |
fba500b6 SM |
205 | (and (equal ret 0) ; Seems redundant. --Stef |
206 | (intern (replace-regexp-in-string " " "" | |
207 | status)))))) | |
208 | (when status | |
209 | (goto-char (point-min)) | |
210 | (skip-chars-forward " \n\t") ;Throw away spaces. | |
211 | (cons status | |
212 | ;; "bzr" will output warnings and informational messages to | |
213 | ;; stderr; due to Emacs' `vc-do-command' (and, it seems, | |
214 | ;; `start-process' itself) limitations, we cannot catch stderr | |
215 | ;; and stdout into different buffers. So, if there's anything | |
216 | ;; left in the buffer after removing the above status | |
217 | ;; keywords, let us just presume that any other message from | |
218 | ;; "bzr" is a user warning, and display it. | |
219 | (unless (eobp) (buffer-substring (point) (point-max)))))))) | |
a0e5e075 | 220 | |
33e5d7d4 SM |
221 | (defun vc-bzr-state (file) |
222 | (lexical-let ((result (vc-bzr-status file))) | |
223 | (when (consp result) | |
224 | (if (cdr result) | |
225 | (message "Warnings in `bzr' output: %s" (cdr result))) | |
226 | (cdr (assq (car result) | |
227 | '((added . edited) | |
fba500b6 | 228 | (kindchanged . edited) |
33e5d7d4 SM |
229 | (renamed . edited) |
230 | (modified . edited) | |
231 | (removed . edited) | |
232 | (ignored . nil) | |
233 | (unknown . nil) | |
234 | (unchanged . up-to-date))))))) | |
b6e0e86c SM |
235 | |
236 | (defun vc-bzr-workfile-unchanged-p (file) | |
33e5d7d4 | 237 | (eq 'unchanged (car (vc-bzr-status file)))) |
b6e0e86c SM |
238 | |
239 | (defun vc-bzr-workfile-version (file) | |
a460c94c SM |
240 | (lexical-let* |
241 | ((rootdir (vc-bzr-root file)) | |
242 | (branch-format-file (concat rootdir "/" vc-bzr-admin-branch-format-file)) | |
243 | (revhistory-file (concat rootdir "/" vc-bzr-admin-revhistory)) | |
244 | (lastrev-file (concat rootdir "/" "branch/last-revision"))) | |
b6e6e09a SM |
245 | ;; Count lines in .bzr/branch/revision-history to avoid forking a |
246 | ;; bzr process. This looks at internal files. May break if they | |
247 | ;; change their format. | |
a460c94c | 248 | (if (file-exists-p branch-format-file) |
fba500b6 | 249 | (with-temp-buffer |
a460c94c SM |
250 | (insert-file-contents branch-format-file) |
251 | (goto-char (point-min)) | |
252 | (cond | |
253 | ((or | |
254 | (looking-at "Bazaar-NG branch, format 0.0.4") | |
255 | (looking-at "Bazaar-NG branch format 5")) | |
256 | ;; count lines in .bzr/branch/revision-history | |
fba500b6 | 257 | (insert-file-contents revhistory-file) |
a460c94c SM |
258 | (number-to-string (count-lines (line-end-position) (point-max)))) |
259 | ((looking-at "Bazaar Branch Format 6 (bzr 0.15)") | |
260 | ;; revno is the first number in .bzr/branch/last-revision | |
261 | (insert-file-contents lastrev-file) | |
262 | (goto-char (line-end-position)) | |
263 | (if (re-search-forward "[0-9]+" nil t) | |
264 | (buffer-substring (match-beginning 0) (match-end 0)))))) | |
265 | ;; fallback to calling "bzr revno" | |
b6e6e09a | 266 | (lexical-let* |
a460c94c SM |
267 | ((result (vc-bzr-command-discarding-stderr |
268 | vc-bzr-program "revno" file)) | |
b6e6e09a SM |
269 | (exitcode (car result)) |
270 | (output (cdr result))) | |
271 | (cond | |
272 | ((eq exitcode 0) (substring output 0 -1)) | |
273 | (t nil)))))) | |
b6e0e86c SM |
274 | |
275 | (defun vc-bzr-checkout-model (file) | |
276 | 'implicit) | |
277 | ||
a6ea7ffc | 278 | (defun vc-bzr-create-repo () |
4211679b | 279 | "Create a new Bzr repository." |
a6ea7ffc DN |
280 | (vc-bzr-command "init" nil 0 nil)) |
281 | ||
8cdd17b4 | 282 | (defun vc-bzr-register (files &optional rev comment) |
b6e0e86c SM |
283 | "Register FILE under bzr. |
284 | Signal an error unless REV is nil. | |
285 | COMMENT is ignored." | |
286 | (if rev (error "Can't register explicit version with bzr")) | |
8cdd17b4 | 287 | (vc-bzr-command "add" nil 0 files)) |
b6e0e86c SM |
288 | |
289 | ;; Could run `bzr status' in the directory and see if it succeeds, but | |
290 | ;; that's relatively expensive. | |
b6e6e09a | 291 | (defalias 'vc-bzr-responsible-p 'vc-bzr-root |
b6e0e86c SM |
292 | "Return non-nil if FILE is (potentially) controlled by bzr. |
293 | The criterion is that there is a `.bzr' directory in the same | |
37320a58 | 294 | or a superior directory.") |
b6e0e86c SM |
295 | |
296 | (defun vc-bzr-could-register (file) | |
297 | "Return non-nil if FILE could be registered under bzr." | |
298 | (and (vc-bzr-responsible-p file) ; shortcut | |
299 | (condition-case () | |
300 | (with-temp-buffer | |
301 | (vc-bzr-command "add" t 0 file "--dry-run") | |
302 | ;; The command succeeds with no output if file is | |
303 | ;; registered (in bzr 0.8). | |
360cf7bc | 304 | (goto-char (point-min)) |
b6e0e86c SM |
305 | (looking-at "added ")) |
306 | (error)))) | |
307 | ||
308 | (defun vc-bzr-unregister (file) | |
309 | "Unregister FILE from bzr." | |
310 | (vc-bzr-command "remove" nil 0 file)) | |
311 | ||
8cdd17b4 | 312 | (defun vc-bzr-checkin (files rev comment) |
b6e0e86c SM |
313 | "Check FILE in to bzr with log message COMMENT. |
314 | REV non-nil gets an error." | |
315 | (if rev (error "Can't check in a specific version with bzr")) | |
8cdd17b4 | 316 | (vc-bzr-command "commit" nil 0 files "-m" comment)) |
b6e0e86c SM |
317 | |
318 | (defun vc-bzr-checkout (file &optional editable rev destfile) | |
319 | "Checkout revision REV of FILE from bzr to DESTFILE. | |
320 | EDITABLE is ignored." | |
321 | (unless destfile | |
322 | (setq destfile (vc-version-backup-file-name file rev))) | |
323 | (let ((coding-system-for-read 'binary) | |
324 | (coding-system-for-write 'binary)) | |
fba500b6 SM |
325 | (with-temp-file destfile |
326 | (if rev | |
327 | (vc-bzr-command "cat" t 0 file "-r" rev) | |
328 | (vc-bzr-command "cat" t 0 file))))) | |
b6e0e86c SM |
329 | |
330 | (defun vc-bzr-revert (file &optional contents-done) | |
331 | (unless contents-done | |
33e5d7d4 | 332 | (with-temp-buffer (vc-bzr-command "revert" t 0 file)))) |
b6e0e86c | 333 | |
37320a58 SM |
334 | (defvar log-view-message-re) |
335 | (defvar log-view-file-re) | |
336 | (defvar log-view-font-lock-keywords) | |
337 | (defvar log-view-current-tag-function) | |
338 | ||
339 | (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View" | |
340 | (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack. | |
b6e0e86c SM |
341 | (require 'add-log) |
342 | ;; Don't have file markers, so use impossible regexp. | |
343 | (set (make-local-variable 'log-view-file-re) "\\'\\`") | |
37320a58 SM |
344 | (set (make-local-variable 'log-view-message-re) |
345 | "^ *-+\n *\\(?:revno: \\([0-9]+\\)\\|merged: .+\\)") | |
b6e0e86c | 346 | (set (make-local-variable 'log-view-font-lock-keywords) |
37320a58 SM |
347 | ;; log-view-font-lock-keywords is careful to use the buffer-local |
348 | ;; value of log-view-message-re only since Emacs-23. | |
349 | (append `((,log-view-message-re . 'log-view-message-face)) | |
350 | ;; log-view-font-lock-keywords | |
351 | '(("^ *committer: \ | |
5ec05779 | 352 | \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]" |
37320a58 SM |
353 | (1 'change-log-name) |
354 | (2 'change-log-email)) | |
355 | ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))) | |
b6e0e86c | 356 | |
8cdd17b4 ER |
357 | (defun vc-bzr-print-log (files &optional buffer) ; get buffer arg in Emacs 22 |
358 | "Get bzr change log for FILES into specified BUFFER." | |
b6e0e86c SM |
359 | ;; Fixme: This might need the locale fixing up if things like `revno' |
360 | ;; got localized, but certainly it shouldn't use LC_ALL=C. | |
8cdd17b4 | 361 | (vc-bzr-command "log" buffer 0 files) |
37320a58 SM |
362 | ;; FIXME: Until Emacs-23, VC was missing a hook to sort out the mode for |
363 | ;; the buffer, or at least set the regexps right. | |
364 | (unless (fboundp 'vc-default-log-view-mode) | |
365 | (add-hook 'log-view-mode-hook 'vc-bzr-log-view-mode))) | |
b6e0e86c SM |
366 | |
367 | (defun vc-bzr-show-log-entry (version) | |
368 | "Find entry for patch name VERSION in bzr change log buffer." | |
369 | (goto-char (point-min)) | |
370 | (let (case-fold-search) | |
371 | (if (re-search-forward (concat "^-+\nrevno: " version "$") nil t) | |
372 | (beginning-of-line 0) | |
373 | (goto-char (point-min))))) | |
374 | ||
b6e0e86c SM |
375 | (autoload 'vc-diff-switches-list "vc" nil nil t) |
376 | ||
8cdd17b4 | 377 | (defun vc-bzr-diff (files &optional rev1 rev2 buffer) |
b6e0e86c | 378 | "VC bzr backend for diff." |
a460c94c | 379 | (let ((working (vc-workfile-version (if (consp files) (car files) files)))) |
b6e0e86c SM |
380 | (if (and (equal rev1 working) (not rev2)) |
381 | (setq rev1 nil)) | |
382 | (if (and (not rev1) rev2) | |
383 | (setq rev1 working)) | |
b6e0e86c | 384 | ;; bzr diff produces condition code 1 for some reason. |
8cdd17b4 | 385 | (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 1 files |
b6e0e86c SM |
386 | "--diff-options" (mapconcat 'identity (vc-diff-switches-list bzr) |
387 | " ") | |
388 | (when rev1 | |
389 | (if rev2 | |
390 | (list "-r" (format "%s..%s" rev1 rev2)) | |
391 | (list "-r" rev1)))))) | |
392 | ||
393 | (defalias 'vc-bzr-diff-tree 'vc-bzr-diff) | |
394 | ||
b6e0e86c | 395 | |
33e5d7d4 | 396 | ;; FIXME: vc-{next,previous}-version need fixing in vc.el to deal with |
b6e0e86c SM |
397 | ;; straight integer versions. |
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 | ||
414 | (defun vc-bzr-annotate-command (file buffer &optional version) | |
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." | |
418 | (apply #'vc-bzr-command "annotate" buffer 0 file "-l" "--all" | |
419 | (if version (list "-r" version))) | |
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)) | |
425 | (while (re-search-forward "^\\( *[0-9]+\\) \\(.+\\) +\\([0-9]\\{8\\}\\) |" | |
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 | |
433 | (save-match-data | |
434 | (string-match " +\\'" author) | |
435 | (setq author (substring author 0 (match-beginning 0)))) | |
436 | (setq tag (propertize rev 'help-echo (concat "Author: " author | |
437 | ", date: " date) | |
438 | 'mouse-face 'highlight)) | |
439 | (puthash key tag vc-bzr-annotation-table)) | |
440 | (replace-match "") | |
441 | (insert tag " |"))))) | |
442 | ||
443 | ;; Definition from Emacs 22 | |
444 | (unless (fboundp 'vc-annotate-convert-time) | |
fba500b6 SM |
445 | (defun vc-annotate-convert-time (time) |
446 | "Convert a time value to a floating-point number of days. | |
b6e0e86c SM |
447 | The argument TIME is a list as returned by `current-time' or |
448 | `encode-time', only the first two elements of that list are considered." | |
fba500b6 | 449 | (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))) |
b6e0e86c SM |
450 | |
451 | (defun vc-bzr-annotate-time () | |
452 | (when (re-search-forward "^ *[0-9]+ |" nil t) | |
453 | (let ((prop (get-text-property (line-beginning-position) 'help-echo))) | |
454 | (string-match "[0-9]+\\'" prop) | |
455 | (vc-annotate-convert-time | |
456 | (encode-time 0 0 0 | |
457 | (string-to-number (substring (match-string 0 prop) 6 8)) | |
458 | (string-to-number (substring (match-string 0 prop) 4 6)) | |
459 | (string-to-number (substring (match-string 0 prop) 0 4)) | |
460 | ))))) | |
461 | ||
462 | (defun vc-bzr-annotate-extract-revision-at-line () | |
463 | "Return revision for current line of annoation buffer, or nil. | |
464 | Return nil if current line isn't annotated." | |
465 | (save-excursion | |
466 | (beginning-of-line) | |
467 | (if (looking-at " *\\([0-9]+\\) | ") | |
468 | (match-string-no-properties 1)))) | |
469 | ||
470 | ;; Not needed for Emacs 22 | |
471 | (defun vc-bzr-annotate-difference (point) | |
472 | (let ((next-time (vc-bzr-annotate-time))) | |
473 | (if next-time | |
474 | (- (vc-annotate-convert-time (current-time)) next-time)))) | |
475 | ||
a460c94c SM |
476 | (defun vc-bzr-command-discarding-stderr (command &rest args) |
477 | "Execute shell command COMMAND (with ARGS); return its output and exitcode. | |
b6e6e09a SM |
478 | Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is |
479 | the (numerical) exit code of the process, and OUTPUT is a string | |
480 | containing whatever the process sent to its standard output | |
481 | stream. Standard error output is discarded." | |
482 | (with-temp-buffer | |
a460c94c | 483 | (cons |
33e5d7d4 | 484 | (apply #'call-process command nil (list (current-buffer) nil) nil args) |
b6e6e09a | 485 | (buffer-substring (point-min) (point-max))))) |
b6e0e86c SM |
486 | |
487 | ;; TODO: it would be nice to mark the conflicted files in VC Dired, | |
488 | ;; and implement a command to run ediff and `bzr resolve' once the | |
489 | ;; changes have been merged. | |
490 | (defun vc-bzr-dir-state (dir &optional localp) | |
491 | "Find the VC state of all files in DIR. | |
492 | Optional argument LOCALP is always ignored." | |
37320a58 SM |
493 | (let ((bzr-root-directory (vc-bzr-root dir)) |
494 | (at-start t) | |
495 | current-bzr-state current-vc-state) | |
496 | ;; Check that DIR is a bzr repository. | |
497 | (unless (file-name-absolute-p bzr-root-directory) | |
b6e0e86c SM |
498 | (error "Cannot find bzr repository for directory `%s'" dir)) |
499 | ;; `bzr ls --versioned' lists all versioned files; | |
500 | ;; assume they are up-to-date, unless we are given | |
501 | ;; evidence of the contrary. | |
37320a58 | 502 | (setq at-start t) |
b6e0e86c | 503 | (with-temp-buffer |
37320a58 | 504 | (vc-bzr-command "ls" t 0 nil "--versioned" "--non-recursive") |
b6e0e86c | 505 | (goto-char (point-min)) |
37320a58 | 506 | (while (or at-start |
b6e0e86c | 507 | (eq 0 (forward-line))) |
37320a58 | 508 | (setq at-start nil) |
b6e0e86c SM |
509 | (let ((file (expand-file-name |
510 | (buffer-substring-no-properties | |
511 | (line-beginning-position) (line-end-position)) | |
512 | bzr-root-directory))) | |
513 | (vc-file-setprop file 'vc-state 'up-to-date) | |
514 | ;; XXX: is this correct? what happens if one | |
515 | ;; mixes different SCMs in the same dir? | |
4211679b | 516 | (vc-file-setprop file 'vc-backend 'Bzr)))) |
b6e0e86c | 517 | ;; `bzr status' reports on added/modified/renamed and unknown/ignored files |
37320a58 | 518 | (setq at-start t) |
b6e0e86c | 519 | (with-temp-buffer |
77b5d458 | 520 | (vc-bzr-command "status" t 0 nil) |
b6e0e86c | 521 | (goto-char (point-min)) |
37320a58 | 522 | (while (or at-start |
b6e0e86c | 523 | (eq 0 (forward-line))) |
37320a58 | 524 | (setq at-start nil) |
b6e0e86c SM |
525 | (cond |
526 | ((looking-at "^added") | |
37320a58 SM |
527 | (setq current-vc-state 'edited) |
528 | (setq current-bzr-state 'added)) | |
33e5d7d4 SM |
529 | ((looking-at "^kind changed") |
530 | (setq current-vc-state 'edited) | |
fba500b6 | 531 | (setq current-bzr-state 'kindchanged)) |
b6e0e86c | 532 | ((looking-at "^modified") |
37320a58 SM |
533 | (setq current-vc-state 'edited) |
534 | (setq current-bzr-state 'modified)) | |
b6e0e86c | 535 | ((looking-at "^renamed") |
37320a58 SM |
536 | (setq current-vc-state 'edited) |
537 | (setq current-bzr-state 'renamed)) | |
b6e0e86c | 538 | ((looking-at "^\\(unknown\\|ignored\\)") |
37320a58 SM |
539 | (setq current-vc-state nil) |
540 | (setq current-bzr-state 'not-versioned)) | |
b6e0e86c SM |
541 | ((looking-at " ") |
542 | ;; file names are indented by two spaces | |
543 | (when current-vc-state | |
544 | (let ((file (expand-file-name | |
545 | (buffer-substring-no-properties | |
546 | (match-end 0) (line-end-position)) | |
547 | bzr-root-directory))) | |
548 | (vc-file-setprop file 'vc-state current-vc-state) | |
549 | (vc-file-setprop file 'vc-bzr-state current-bzr-state) | |
550 | (when (eq 'added current-bzr-state) | |
551 | (vc-file-setprop file 'vc-workfile-version "0")))) | |
552 | (when (eq 'not-versioned current-bzr-state) | |
553 | (let ((file (expand-file-name | |
554 | (buffer-substring-no-properties | |
555 | (match-end 0) (line-end-position)) | |
556 | bzr-root-directory))) | |
557 | (vc-file-setprop file 'vc-backend 'none) | |
558 | (vc-file-setprop file 'vc-state nil)))) | |
559 | (t | |
560 | ;; skip this part of `bzr status' output | |
37320a58 SM |
561 | (setq current-vc-state nil) |
562 | (setq current-bzr-state nil))))))) | |
b6e0e86c SM |
563 | |
564 | (defun vc-bzr-dired-state-info (file) | |
565 | "Bzr-specific version of `vc-dired-state-info'." | |
566 | (if (eq 'edited (vc-state file)) | |
567 | (let ((bzr-state (vc-file-getprop file 'vc-bzr-state))) | |
568 | (if bzr-state | |
569 | (concat "(" (symbol-name bzr-state) ")") | |
570 | ;; else fall back to default vc representation | |
4211679b | 571 | (vc-default-dired-state-info 'Bzr file))))) |
b6e0e86c | 572 | |
b6e0e86c | 573 | (eval-after-load "vc" |
b6e6e09a | 574 | '(add-to-list 'vc-directory-exclusion-list vc-bzr-admin-dirname t)) |
b6e0e86c | 575 | |
b6e0e86c SM |
576 | |
577 | (provide 'vc-bzr) | |
578 | ;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06 | |
579 | ;;; vc-bzr.el ends here |