Put the lower half (the back-end) of NewVC in place. This commit
[bpt/emacs.git] / lisp / vc-bzr.el
CommitLineData
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
5;; NOTE: THIS IS A MODIFIED VERSION OF Dave Love's vc-bzr.el,
6;; which you can find at: http://www.loveshack.ukfsn.org/emacs/vc-bzr.el
7;; I could not get in touch with Dave Love by email, so
8;; I am releasing my changes separately. -- Riccardo
9
10;; Author: Dave Love <fx@gnu.org>, Riccardo Murri <riccardo.murri@gmail.com>
11;; Keywords: tools
12;; Created: Sept 2006
77b5d458 13;; Version: 2007-05-24
b6e0e86c
SM
14;; URL: http://launchpad.net/vc-bzr
15
16;; This file is free software; you can redistribute it and/or modify
17;; it under the terms of the GNU General Public License as published by
18;; the Free Software Foundation; either version 2, or (at your option)
19;; any later version.
20
21;; This file is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
27;; along with GNU Emacs; see the file COPYING. If not, write to the
28;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29;; Boston, MA 02110-1301, USA.
30
31
32;;; Commentary:
33
34;; NOTE: THIS IS A MODIFIED VERSION OF Dave Love's vc-bzr.el,
35;; which you can find at: http://www.loveshack.ukfsn.org/emacs/vc-bzr.el
36
37;; See <URL:http://bazaar-vcs.org/> concerning bzr.
38
77b5d458
SM
39;; Load this library to register bzr support in VC. It covers basic VC
40;; functionality, but was only lightly exercised with a few Emacs/bzr
41;; version combinations, namely those current on the authors' PCs.
42;; See various Fixmes below.
b6e0e86c 43
77b5d458
SM
44
45;; Known bugs
46;; ==========
47
48;; When edititing a symlink and *both* the symlink and its target
49;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
50;; symlink, thereby not detecting whether the actual contents
51;; (that is, the target contents) are changed.
52;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
53
54;; For an up-to-date list of bugs, please see:
55;; https://bugs.launchpad.net/vc-bzr/+bugs
b6e0e86c
SM
56
57
58;;; Code:
59
60(eval-when-compile
37320a58 61 (require 'cl)
b6e0e86c
SM
62 (require 'vc)) ; for vc-exec-after
63
360cf7bc
SM
64;; Clear up the cache to force vc-call to check again and discover
65;; new functions when we reload this file.
66(put 'BZR 'vc-functions nil)
67
b6e0e86c
SM
68(defgroup vc-bzr nil
69 "VC bzr backend."
70;; :version "22"
71 :group 'vc)
72
73(defcustom vc-bzr-program "bzr"
37320a58 74 "Name of the bzr command (excluding any arguments)."
b6e0e86c
SM
75 :group 'vc-bzr
76 :type 'string)
77
78;; Fixme: there's probably no call for this.
79(defcustom vc-bzr-program-args nil
37320a58 80 "List of global arguments to pass to `vc-bzr-program'."
b6e0e86c
SM
81 :group 'vc-bzr
82 :type '(repeat string))
83
84(defcustom vc-bzr-diff-switches nil
37320a58 85 "String/list of strings specifying extra switches for bzr diff under VC."
b6e0e86c
SM
86 :type '(choice (const :tag "None" nil)
87 (string :tag "Argument String")
88 (repeat :tag "Argument List" :value ("") string))
89 :group 'vc-bzr)
90
37320a58
SM
91;; since v0.9, bzr supports removing the progress indicators
92;; by setting environment variable BZR_PROGRESS_BAR to "none".
93(defun vc-bzr-command (bzr-command buffer okstatus file &rest args)
94 "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
b6e0e86c 95Invoke the bzr command adding `BZR_PROGRESS_BAR=none' to the environment."
37320a58
SM
96 (let ((process-environment
97 (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
98 "LC_ALL=C" ; Force English output
99 process-environment))
100 ;; bzr may attempt some kind of user interaction if its stdin/stdout
101 ;; is connected to a PTY; therefore, ask Emacs to use a pipe to
102 ;; communicate with it.
103 ;; This is redundant because vc-do-command does it already. --Stef
104 (process-connection-type nil))
105 (apply 'vc-do-command buffer okstatus vc-bzr-program
106 file bzr-command (append vc-bzr-program-args args))))
b6e0e86c 107
37320a58
SM
108
109;;;###autoload
110(defconst vc-bzr-admin-dirname ".bzr") ; FIXME: "_bzr" on w32?
111
112;;;###autoload (defun vc-bzr-registered (file)
113;;;###autoload (if (vc-find-root file vc-bzr-admin-dirname)
114;;;###autoload (progn
115;;;###autoload (load "vc-bzr")
116;;;###autoload (vc-bzr-registered file))))
b6e0e86c 117
37320a58
SM
118(defun vc-bzr-root-dir (file)
119 "Return the root directory in the hierarchy above FILE.
b6e0e86c 120Return nil if there isn't one."
37320a58 121 (vc-find-root file vc-bzr-admin-dirname))
b6e0e86c
SM
122
123(defun vc-bzr-registered (file)
124 "Return non-nil if FILE is registered with bzr."
37320a58
SM
125 (if (vc-bzr-root-dir file) ; Short cut.
126 (vc-bzr-state file))) ; Expensive.
b6e0e86c 127
77b5d458
SM
128(defun vc-bzr-buffer-nonblank-p (&optional buffer)
129 "Return non-nil if BUFFER contains any non-blank characters."
130 (or (> (buffer-size buffer) 0)
b6e0e86c 131 (save-excursion
77b5d458
SM
132 (set-buffer (or buffer (current-buffer)))
133 (goto-char (point-min))
134 (re-search-forward "[^ \t\n]" (point-max) t))))
135
136(defconst vc-bzr-state-words
137 "added\\|ignored\\|modified\\|removed\\|renamed\\|unknown"
138 "Regexp matching file status words as reported in `bzr' output.")
139
140;; FIXME: Also get this in a non-registered sub-directory.
141(defun vc-bzr-state (file)
142 (with-temp-buffer
143 (cd (file-name-directory file))
144 (let ((ret (vc-bzr-command "status" t 255 file))
145 (state 'up-to-date))
146 ;; the only secure status indication in `bzr status' output
147 ;; is a couple of lines following the pattern::
148 ;; | <status>:
149 ;; | <file name>
150 ;; if the file is up-to-date, we get no status report from `bzr',
151 ;; so if the regexp search for the above pattern fails, we consider
152 ;; the file to be up-to-date.
153 (goto-char (point-min))
154 (when
360cf7bc
SM
155 (re-search-forward
156 (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
77b5d458
SM
157 (file-name-nondirectory file) "[ \t\n]*$")
158 (point-max) t)
159 (let ((start (match-beginning 0))
160 (end (match-end 0)))
161 (goto-char start)
162 (setq state
360cf7bc 163 (cond
77b5d458
SM
164 ((not (equal ret 0)) nil)
165 ((looking-at "added\\|renamed\\|modified\\|removed") 'edited)
166 ((looking-at "unknown\\|ignored") nil)))
167 ;; erase the status text that matched
168 (delete-region start end)))
169 (when (vc-bzr-buffer-nonblank-p)
170 ;; "bzr" will output some warnings and informational messages
171 ;; to the user to stderr; due to Emacs' `vc-do-command' (and,
172 ;; it seems, `start-process' itself), we cannot catch stderr
173 ;; and stdout into different buffers. So, if there's anything
174 ;; left in the buffer after removing the above status
175 ;; keywords, let us just presume that any other message from
176 ;; "bzr" is a user warning, and display it.
360cf7bc 177 (message "Warnings in `bzr' output: %s"
77b5d458 178 (buffer-substring (point-min) (point-max))))
b6e0e86c
SM
179 (when state
180 (vc-file-setprop file 'vc-workfile-version
181 (vc-bzr-workfile-version file))
182 (vc-file-setprop file 'vc-state state))
183 state)))
184
185(defun vc-bzr-workfile-unchanged-p (file)
186 (eq 'up-to-date (vc-bzr-state file)))
187
188(defun vc-bzr-workfile-version (file)
360cf7bc
SM
189 ;; Looks like this could be obtained via counting lines in
190 ;; .bzr/branch/revision-history.
b6e0e86c
SM
191 (with-temp-buffer
192 (vc-bzr-command "revno" t 0 file)
360cf7bc
SM
193 (goto-char (point-min))
194 (buffer-substring (point) (line-end-position))))
b6e0e86c
SM
195
196(defun vc-bzr-checkout-model (file)
197 'implicit)
198
199(defun vc-bzr-register (file &optional rev comment)
200 "Register FILE under bzr.
201Signal an error unless REV is nil.
202COMMENT is ignored."
203 (if rev (error "Can't register explicit version with bzr"))
204 (vc-bzr-command "add" nil 0 file))
205
206;; Could run `bzr status' in the directory and see if it succeeds, but
207;; that's relatively expensive.
37320a58 208(defalias 'vc-bzr-responsible-p 'vc-bzr-root-dir
b6e0e86c
SM
209 "Return non-nil if FILE is (potentially) controlled by bzr.
210The criterion is that there is a `.bzr' directory in the same
37320a58 211or a superior directory.")
b6e0e86c
SM
212
213(defun vc-bzr-could-register (file)
214 "Return non-nil if FILE could be registered under bzr."
215 (and (vc-bzr-responsible-p file) ; shortcut
216 (condition-case ()
217 (with-temp-buffer
218 (vc-bzr-command "add" t 0 file "--dry-run")
219 ;; The command succeeds with no output if file is
220 ;; registered (in bzr 0.8).
360cf7bc 221 (goto-char (point-min))
b6e0e86c
SM
222 (looking-at "added "))
223 (error))))
224
225(defun vc-bzr-unregister (file)
226 "Unregister FILE from bzr."
227 (vc-bzr-command "remove" nil 0 file))
228
229(defun vc-bzr-checkin (file rev comment)
230 "Check FILE in to bzr with log message COMMENT.
231REV non-nil gets an error."
232 (if rev (error "Can't check in a specific version with bzr"))
233 (vc-bzr-command "commit" nil 0 file "-m" comment))
234
235(defun vc-bzr-checkout (file &optional editable rev destfile)
236 "Checkout revision REV of FILE from bzr to DESTFILE.
237EDITABLE is ignored."
238 (unless destfile
239 (setq destfile (vc-version-backup-file-name file rev)))
240 (let ((coding-system-for-read 'binary)
241 (coding-system-for-write 'binary))
242 (with-temp-file destfile
243 (if rev
244 (vc-bzr-command "cat" t 0 file "-r" rev)
245 (vc-bzr-command "cat" t 0 file)))))
246
247(defun vc-bzr-revert (file &optional contents-done)
248 (unless contents-done
249 (with-temp-buffer (vc-bzr-command "revert" t 'async file))))
250
37320a58
SM
251(defvar log-view-message-re)
252(defvar log-view-file-re)
253(defvar log-view-font-lock-keywords)
254(defvar log-view-current-tag-function)
255
256(define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
257 (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
b6e0e86c
SM
258 (require 'add-log)
259 ;; Don't have file markers, so use impossible regexp.
260 (set (make-local-variable 'log-view-file-re) "\\'\\`")
37320a58
SM
261 (set (make-local-variable 'log-view-message-re)
262 "^ *-+\n *\\(?:revno: \\([0-9]+\\)\\|merged: .+\\)")
b6e0e86c 263 (set (make-local-variable 'log-view-font-lock-keywords)
37320a58
SM
264 ;; log-view-font-lock-keywords is careful to use the buffer-local
265 ;; value of log-view-message-re only since Emacs-23.
266 (append `((,log-view-message-re . 'log-view-message-face))
267 ;; log-view-font-lock-keywords
268 '(("^ *committer: \
5ec05779 269\\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
37320a58
SM
270 (1 'change-log-name)
271 (2 'change-log-email))
272 ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face))))))
b6e0e86c
SM
273
274(defun vc-bzr-print-log (file &optional buffer) ; get buffer arg in Emacs 22
275 "Get bzr change log for FILE into specified BUFFER."
b6e0e86c
SM
276 ;; Fixme: This might need the locale fixing up if things like `revno'
277 ;; got localized, but certainly it shouldn't use LC_ALL=C.
278 ;; NB. Can't be async -- see `vc-bzr-post-command-function'.
279 (vc-bzr-command "log" buffer 0 file)
37320a58
SM
280 ;; FIXME: Until Emacs-23, VC was missing a hook to sort out the mode for
281 ;; the buffer, or at least set the regexps right.
282 (unless (fboundp 'vc-default-log-view-mode)
283 (add-hook 'log-view-mode-hook 'vc-bzr-log-view-mode)))
b6e0e86c
SM
284
285(defun vc-bzr-show-log-entry (version)
286 "Find entry for patch name VERSION in bzr change log buffer."
287 (goto-char (point-min))
288 (let (case-fold-search)
289 (if (re-search-forward (concat "^-+\nrevno: " version "$") nil t)
290 (beginning-of-line 0)
291 (goto-char (point-min)))))
292
293;; Fixem: vc-bzr-wash-log
294
295(autoload 'vc-diff-switches-list "vc" nil nil t)
296
297(defun vc-bzr-diff (file &optional rev1 rev2 buffer)
298 "VC bzr backend for diff."
299 (let ((working (vc-workfile-version file)))
300 (if (and (equal rev1 working) (not rev2))
301 (setq rev1 nil))
302 (if (and (not rev1) rev2)
303 (setq rev1 working))
304 ;; NB. Can't be async -- see `vc-bzr-post-command-function'.
305 ;; bzr diff produces condition code 1 for some reason.
306 (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*") 1 file
307 "--diff-options" (mapconcat 'identity (vc-diff-switches-list bzr)
308 " ")
309 (when rev1
310 (if rev2
311 (list "-r" (format "%s..%s" rev1 rev2))
312 (list "-r" rev1))))))
313
314(defalias 'vc-bzr-diff-tree 'vc-bzr-diff)
315
316;; Fixme: implement vc-bzr-dir-state, vc-bzr-dired-state-info
317
318;; Fixme: vc-{next,previous}-version need fixing in vc.el to deal with
319;; straight integer versions.
320
321(defun vc-bzr-delete-file (file)
322 "Delete FILE and delete it in the bzr repository."
323 (condition-case ()
324 (delete-file file)
325 (file-error nil))
326 (vc-bzr-command "remove" nil 0 file))
327
328(defun vc-bzr-rename-file (old new)
329 "Rename file from OLD to NEW using `bzr mv'."
330 (vc-bzr-command "mv" nil 0 new old))
331
332(defvar vc-bzr-annotation-table nil
333 "Internal use.")
334(make-variable-buffer-local 'vc-bzr-annotation-table)
335
336(defun vc-bzr-annotate-command (file buffer &optional version)
337 "Prepare BUFFER for `vc-annotate' on FILE.
338Each line is tagged with the revision number, which has a `help-echo'
339property containing author and date information."
340 (apply #'vc-bzr-command "annotate" buffer 0 file "-l" "--all"
341 (if version (list "-r" version)))
342 (with-current-buffer buffer
343 ;; Store the tags for the annotated source lines in a hash table
344 ;; to allow saving space by sharing the text properties.
345 (setq vc-bzr-annotation-table (make-hash-table :test 'equal))
346 (goto-char (point-min))
347 (while (re-search-forward "^\\( *[0-9]+\\) \\(.+\\) +\\([0-9]\\{8\\}\\) |"
348 nil t)
349 (let* ((rev (match-string 1))
350 (author (match-string 2))
351 (date (match-string 3))
352 (key (match-string 0))
353 (tag (gethash key vc-bzr-annotation-table)))
354 (unless tag
355 (save-match-data
356 (string-match " +\\'" author)
357 (setq author (substring author 0 (match-beginning 0))))
358 (setq tag (propertize rev 'help-echo (concat "Author: " author
359 ", date: " date)
360 'mouse-face 'highlight))
361 (puthash key tag vc-bzr-annotation-table))
362 (replace-match "")
363 (insert tag " |")))))
364
365;; Definition from Emacs 22
366(unless (fboundp 'vc-annotate-convert-time)
367(defun vc-annotate-convert-time (time)
368 "Convert a time value to a floating-point number of days.
369The argument TIME is a list as returned by `current-time' or
370`encode-time', only the first two elements of that list are considered."
371 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600)))
372
373(defun vc-bzr-annotate-time ()
374 (when (re-search-forward "^ *[0-9]+ |" nil t)
375 (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
376 (string-match "[0-9]+\\'" prop)
377 (vc-annotate-convert-time
378 (encode-time 0 0 0
379 (string-to-number (substring (match-string 0 prop) 6 8))
380 (string-to-number (substring (match-string 0 prop) 4 6))
381 (string-to-number (substring (match-string 0 prop) 0 4))
382 )))))
383
384(defun vc-bzr-annotate-extract-revision-at-line ()
385 "Return revision for current line of annoation buffer, or nil.
386Return nil if current line isn't annotated."
387 (save-excursion
388 (beginning-of-line)
389 (if (looking-at " *\\([0-9]+\\) | ")
390 (match-string-no-properties 1))))
391
392;; Not needed for Emacs 22
393(defun vc-bzr-annotate-difference (point)
394 (let ((next-time (vc-bzr-annotate-time)))
395 (if next-time
396 (- (vc-annotate-convert-time (current-time)) next-time))))
397
398;; FIXME: `bzr root' will return the real path to the repository root,
399;; that is, it can differ from the buffer's current directory name
400;; if there are any symbolic links.
401(defun vc-bzr-root (dir)
402 "Return the root directory of the bzr repository containing DIR."
b32ce4c3
MY
403 ;; Cache technique copied from vc-arch.el.
404 (or (vc-file-getprop dir 'bzr-root)
405 (vc-file-setprop
406 dir 'bzr-root
407 (substring
408 (shell-command-to-string (concat vc-bzr-program " root " dir)) 0 -1))))
b6e0e86c
SM
409
410;; TODO: it would be nice to mark the conflicted files in VC Dired,
411;; and implement a command to run ediff and `bzr resolve' once the
412;; changes have been merged.
413(defun vc-bzr-dir-state (dir &optional localp)
414 "Find the VC state of all files in DIR.
415Optional argument LOCALP is always ignored."
37320a58
SM
416 (let ((bzr-root-directory (vc-bzr-root dir))
417 (at-start t)
418 current-bzr-state current-vc-state)
419 ;; Check that DIR is a bzr repository.
420 (unless (file-name-absolute-p bzr-root-directory)
b6e0e86c
SM
421 (error "Cannot find bzr repository for directory `%s'" dir))
422 ;; `bzr ls --versioned' lists all versioned files;
423 ;; assume they are up-to-date, unless we are given
424 ;; evidence of the contrary.
37320a58 425 (setq at-start t)
b6e0e86c 426 (with-temp-buffer
37320a58 427 (vc-bzr-command "ls" t 0 nil "--versioned" "--non-recursive")
b6e0e86c 428 (goto-char (point-min))
37320a58 429 (while (or at-start
b6e0e86c 430 (eq 0 (forward-line)))
37320a58 431 (setq at-start nil)
b6e0e86c
SM
432 (let ((file (expand-file-name
433 (buffer-substring-no-properties
434 (line-beginning-position) (line-end-position))
435 bzr-root-directory)))
436 (vc-file-setprop file 'vc-state 'up-to-date)
437 ;; XXX: is this correct? what happens if one
438 ;; mixes different SCMs in the same dir?
439 (vc-file-setprop file 'vc-backend 'BZR))))
440 ;; `bzr status' reports on added/modified/renamed and unknown/ignored files
37320a58 441 (setq at-start t)
b6e0e86c 442 (with-temp-buffer
77b5d458 443 (vc-bzr-command "status" t 0 nil)
b6e0e86c 444 (goto-char (point-min))
37320a58 445 (while (or at-start
b6e0e86c 446 (eq 0 (forward-line)))
37320a58 447 (setq at-start nil)
b6e0e86c
SM
448 (cond
449 ((looking-at "^added")
37320a58
SM
450 (setq current-vc-state 'edited)
451 (setq current-bzr-state 'added))
b6e0e86c 452 ((looking-at "^modified")
37320a58
SM
453 (setq current-vc-state 'edited)
454 (setq current-bzr-state 'modified))
b6e0e86c 455 ((looking-at "^renamed")
37320a58
SM
456 (setq current-vc-state 'edited)
457 (setq current-bzr-state 'renamed))
b6e0e86c 458 ((looking-at "^\\(unknown\\|ignored\\)")
37320a58
SM
459 (setq current-vc-state nil)
460 (setq current-bzr-state 'not-versioned))
b6e0e86c
SM
461 ((looking-at " ")
462 ;; file names are indented by two spaces
463 (when current-vc-state
464 (let ((file (expand-file-name
465 (buffer-substring-no-properties
466 (match-end 0) (line-end-position))
467 bzr-root-directory)))
468 (vc-file-setprop file 'vc-state current-vc-state)
469 (vc-file-setprop file 'vc-bzr-state current-bzr-state)
470 (when (eq 'added current-bzr-state)
471 (vc-file-setprop file 'vc-workfile-version "0"))))
472 (when (eq 'not-versioned current-bzr-state)
473 (let ((file (expand-file-name
474 (buffer-substring-no-properties
475 (match-end 0) (line-end-position))
476 bzr-root-directory)))
477 (vc-file-setprop file 'vc-backend 'none)
478 (vc-file-setprop file 'vc-state nil))))
479 (t
480 ;; skip this part of `bzr status' output
37320a58
SM
481 (setq current-vc-state nil)
482 (setq current-bzr-state nil)))))))
b6e0e86c
SM
483
484(defun vc-bzr-dired-state-info (file)
485 "Bzr-specific version of `vc-dired-state-info'."
486 (if (eq 'edited (vc-state file))
487 (let ((bzr-state (vc-file-getprop file 'vc-bzr-state)))
488 (if bzr-state
489 (concat "(" (symbol-name bzr-state) ")")
490 ;; else fall back to default vc representation
491 (vc-default-dired-state-info 'BZR file)))))
492
493;; In case of just `(load "vc-bzr")', but that's probably the wrong
494;; way to do it.
495(add-to-list 'vc-handled-backends 'BZR)
496
497(eval-after-load "vc"
498 '(add-to-list 'vc-directory-exclusion-list ".bzr" t))
499
500(defconst vc-bzr-unload-hook
501 (lambda ()
502 (setq vc-handled-backends (delq 'BZR vc-handled-backends))
503 (remove-hook 'vc-post-command-functions 'vc-bzr-post-command-function)))
504
505(provide 'vc-bzr)
506;; arch-tag: 8101bad8-4e92-4e7d-85ae-d8e08b4e7c06
507;;; vc-bzr.el ends here