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