1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This is a mercurial version control backend
37 ;; Implement the rest of the vc interface. See the comment at the
38 ;; beginning of vc.el. The current status is:
40 ;; FUNCTION NAME STATUS
41 ;; * registered (file) OK
43 ;; - state-heuristic (file) ?? PROBABLY NOT NEEDED
44 ;; - dir-state (dir) OK
45 ;; * workfile-version (file) OK
46 ;; - latest-on-branch-p (file) ??
47 ;; * checkout-model (file) OK
48 ;; - workfile-unchanged-p (file) OK
49 ;; - mode-line-string (file) NOT NEEDED
50 ;; - dired-state-info (file) OK
51 ;; STATE-CHANGING FUNCTIONS
52 ;; * register (file &optional rev comment) OK
53 ;; - init-version () NOT NEEDED
54 ;; - responsible-p (file) OK
55 ;; - could-register (file) OK
56 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
57 ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
58 ;; * checkin (file rev comment) OK
59 ;; * find-version (file rev buffer) OK
60 ;; * checkout (file &optional editable rev) OK
61 ;; * revert (file &optional contents-done) OK
62 ;; - cancel-version (file editable) ?? PROBABLY NOT NEEDED
63 ;; - merge (file rev1 rev2) NEEDED
64 ;; - merge-news (file) NEEDED
65 ;; - steal-lock (file &optional version) NOT NEEDED
67 ;; * print-log (file &optional buffer) OK
68 ;; - log-view-mode () OK
69 ;; - show-log-entry (version) NOT NEEDED, DEFAULT IS GOOD
70 ;; - wash-log (file) ??
71 ;; - logentry-check () NOT NEEDED
72 ;; - comment-history (file) NOT NEEDED
73 ;; - update-changelog (files) NOT NEEDED
74 ;; * diff (file &optional rev1 rev2 buffer) OK
75 ;; - revision-completion-table (file) COMMENTED OUT AS A WORKAROUND FOR A BUG
76 ;; - diff-tree (dir &optional rev1 rev2) TEST IT
77 ;; - annotate-command (file buf &optional rev) OK
78 ;; - annotate-time () OK
79 ;; - annotate-current-time () ?? NOT NEEDED
80 ;; - annotate-extract-revision-at-line () OK
82 ;; - create-snapshot (dir name branchp) NEEDED (probably branch?)
83 ;; - assign-name (file name) NOT NEEDED
84 ;; - retrieve-snapshot (dir name update) ?? NEEDED??
86 ;; - make-version-backups-p (file) ??
87 ;; - repository-hostname (dirname) ??
88 ;; - previous-version (file rev) OK
89 ;; - next-version (file rev) OK
90 ;; - check-headers () ??
91 ;; - clear-headers () ??
92 ;; - delete-file (file) TEST IT
93 ;; - rename-file (old new) OK
94 ;; - find-file-hook () PROBABLY NOT NEEDED
95 ;; - find-file-not-found-hook () PROBABLY NOT NEEDED
97 ;; Implement Stefan Monnier's advice:
98 ;; vc-hg-registered and vc-hg-state
99 ;; Both of those functions should be super extra careful to fail gracefully in
100 ;; unexpected circumstances. The reason this is important is that any error
101 ;; there will prevent the user from even looking at the file :-(
102 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
103 ;; mercurial's control and extracting the current revision should be done
104 ;; without even using `hg' (this way even if you don't have `hg' installed,
105 ;; Emacs is able to tell you this file is under mercurial's control).
116 ;;; Customization options
118 (defcustom vc-hg-global-switches nil
119 "*Global switches to pass to any Hg command."
120 :type
'(choice (const :tag
"None" nil
)
121 (string :tag
"Argument String")
122 (repeat :tag
"Argument List"
128 ;;; State querying functions
130 ;;;###autoload (defun vc-hg-registered (file)
131 ;;;###autoload "Return non-nil if FILE is registered with hg."
132 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
133 ;;;###autoload (progn
134 ;;;###autoload (load "vc-hg")
135 ;;;###autoload (vc-hg-registered file))))
137 ;; Modelled after the similar function in vc-bzr.el
138 (defun vc-hg-registered (file)
139 "Return non-nil if FILE is registered with hg."
140 (when (vc-hg-root file
) ; short cut
141 (vc-file-setprop file
'vc-state
(vc-hg-state file
)))) ; expensive
143 (defun vc-hg-state (file)
144 "Hg-specific version of `vc-state'."
148 (with-output-to-string
153 ;; Ignore all errors.
155 "hg" nil t nil
"--cwd" (file-name-directory file
)
156 "status" (file-name-nondirectory file
))
157 ;; Some problem happened. E.g. We can't find an `hg'
161 (if (eq 0 (length out
)) 'up-to-date
162 (when (null (string-match ".*: No such file or directory$" out
))
163 (let ((state (aref out
0)))
165 ((eq state ?A
) 'edited
)
166 ((eq state ?M
) 'edited
)
169 (t 'up-to-date
))))))))
171 (defun vc-hg-dir-state (dir)
173 (vc-hg-command (current-buffer) nil dir
"status")
174 (goto-char (point-min))
175 (let ((status-char nil
)
178 (setq status-char
(char-after))
181 (buffer-substring-no-properties (+ (point) 2)
182 (line-end-position))))
184 ;; The rest of the possible states in "hg status" output:
186 ;; ! = deleted, but still tracked
188 ;; should not show up in vc-dired, so don't deal with them
191 (vc-file-setprop file
'vc-backend
'Hg
)
192 (vc-file-setprop file
'vc-workfile-version
"0")
193 (vc-file-setprop file
'vc-state
'edited
))
195 (vc-file-setprop file
'vc-backend
'Hg
)
196 (vc-file-setprop file
'vc-state
'edited
))
198 (vc-file-setprop file
'vc-backend
'none
)
199 (vc-file-setprop file
'vc-state
'nil
)))
202 (defun vc-hg-workfile-version (file)
203 "Hg-specific version of `vc-workfile-version'."
207 (with-output-to-string
212 ;; Ignore all errors.
214 "hg" nil t nil
"--cwd" (file-name-directory file
)
215 "log" "-l1" (file-name-nondirectory file
))
216 ;; Some problem happened. E.g. We can't find an `hg'
220 (if (string-match "changeset: *\\([0-9]*\\)" out
)
224 ;;; History functions
226 (defun vc-hg-print-log(file &optional buffer
)
227 "Get change log associated with FILE."
228 ;; `log-view-mode' needs to have the file name in order to function
229 ;; correctly. "hg log" does not print it, so we insert it here by
232 ;; `vc-do-command' creates the buffer, but we need it before running
234 (vc-setup-buffer buffer
)
235 ;; If the buffer exists from a previous invocation it might be
237 (let ((inhibit-read-only t
))
240 (insert "File: " (file-name-nondirectory file
) "\n")))
243 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
246 (defvar log-view-message-re
)
247 (defvar log-view-file-re
)
248 (defvar log-view-font-lock-keywords
)
250 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
251 (require 'add-log
) ;; we need the faces add-log
252 ;; Don't have file markers, so use impossible regexp.
253 (set (make-local-variable 'log-view-file-re
) "^File:[ \t]+\\(.+\\)")
254 (set (make-local-variable 'log-view-message-re
)
255 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
256 (set (make-local-variable 'log-view-font-lock-keywords
)
258 log-view-font-lock-keywords
261 ;; user: FirstName LastName <foo@bar>
262 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
264 (2 'change-log-email
))
269 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
270 (1 'change-log-email
))
271 ("^date: \\(.+\\)" (1 'change-log-date
))
272 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
))))))
274 (defun vc-hg-diff (file &optional oldvers newvers buffer
)
275 "Get a difference report using hg between two versions of FILE."
276 (let ((working (vc-workfile-version file
)))
277 (if (and (equal oldvers working
) (not newvers
))
279 (if (and (not oldvers
) newvers
)
280 (setq oldvers working
))
281 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil
282 (file-name-nondirectory file
)
283 "--cwd" (file-name-directory file
)
288 (list "-r" oldvers
"-r" newvers
)
289 (list "-r" oldvers
)))))))
291 (defun vc-hg-revision-table (file)
292 (let ((default-directory (file-name-directory file
)))
294 (vc-hg-command t nil file
"log" "--template" "{rev} ")
296 (buffer-substring-no-properties (point-min) (point-max))))))
298 ;; Modelled after the similar function in vc-cvs.el
299 (defun vc-hg-revision-completion-table (file)
300 (lexical-let ((file file
)
302 (setq table
(lazy-completion-table
303 table
(lambda () (vc-hg-revision-table file
))))
306 (defalias 'vc-hg-diff-tree
'vc-hg-diff
)
308 (defun vc-hg-annotate-command (file buffer
&optional version
)
309 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
310 Optional arg VERSION is a version to annotate from."
311 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" (if version
(concat "-r" version
)))
312 (with-current-buffer buffer
313 (goto-char (point-min))
314 (re-search-forward "^[ \t]*[0-9]")
315 (delete-region (point-min) (match-beginning 0))))
318 ;; The format for one line output by "hg annotate -d -n" looks like this:
319 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
320 ;; i.e: VERSION_NUMBER DATE: CONTENTS
321 ;; If the user has set the "--follow" option, the output looks like:
322 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
323 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
324 (defconst vc-hg-annotate-re
325 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)[^:\n]*\\(:[^ \n][^:\n]*\\)*: ")
327 (defun vc-hg-annotate-time ()
328 (when (looking-at vc-hg-annotate-re
)
329 (goto-char (match-end 0))
330 (vc-annotate-convert-time
331 (date-to-time (match-string-no-properties 2)))))
333 (defun vc-hg-annotate-extract-revision-at-line ()
336 (when (looking-at vc-hg-annotate-re
) (match-string-no-properties 1))))
338 (defun vc-hg-previous-version (file rev
)
339 (let ((newrev (1- (string-to-number rev
))))
341 (number-to-string newrev
))))
343 (defun vc-hg-next-version (file rev
)
344 (let ((newrev (1+ (string-to-number rev
)))
347 (vc-hg-command t
0 nil
"tip")
348 (goto-char (point-min))
349 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
350 (string-to-number (match-string-no-properties 1)))))
351 ;; We don't want to exceed the maximum possible version number, ie
353 (when (<= newrev tip-version
)
354 (number-to-string newrev
))))
356 ;; Modelled after the similar function in vc-bzr.el
357 (defun vc-hg-delete-file (file)
358 "Delete FILE and delete it in the hg repository."
362 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
364 ;; Modelled after the similar function in vc-bzr.el
365 (defun vc-hg-rename-file (old new
)
366 "Rename file from OLD to NEW using `hg mv'."
367 (vc-hg-command nil
0 new old
"mv"))
369 (defun vc-hg-register (file &optional rev comment
)
370 "Register FILE under hg.
373 (vc-hg-command nil
0 file
"add"))
375 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
377 ;; Modelled after the similar function in vc-bzr.el
378 (defun vc-hg-could-register (file)
379 "Return non-nil if FILE could be registered under hg."
380 (and (vc-hg-responsible-p file
) ; shortcut
383 (vc-hg-command t nil file
"add" "--dry-run"))
384 ;; The command succeeds with no output if file is
388 ;; XXX This would remove the file. Is that correct?
389 ;; (defun vc-hg-unregister (file)
390 ;; "Unregister FILE from hg."
391 ;; (vc-hg-command nil nil file "remove"))
393 (defun vc-hg-checkin (file rev comment
)
394 "Hg-specific version of `vc-backend-checkin'.
396 (vc-hg-command nil
0 file
"commit" "-m" comment
))
398 (defun vc-hg-find-version (file rev buffer
)
399 (let ((coding-system-for-read 'binary
)
400 (coding-system-for-write 'binary
))
402 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
403 (vc-hg-command buffer
0 file
"cat"))))
405 ;; Modelled after the similar function in vc-bzr.el
406 (defun vc-hg-checkout (file &optional editable rev
)
407 "Retrieve a revision of FILE.
409 REV is the revision to check out into WORKFILE."
410 (let ((coding-system-for-read 'binary
)
411 (coding-system-for-write 'binary
))
412 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
414 (vc-hg-command t
0 file
"cat" "-r" rev
)
415 (vc-hg-command t
0 file
"cat")))))
417 (defun vc-hg-checkout-model (file)
420 ;; Modelled after the similar function in vc-bzr.el
421 (defun vc-hg-workfile-unchanged-p (file)
422 (eq 'up-to-date
(vc-hg-state file
)))
424 (defun vc-hg-dired-state-info (file)
425 "Hg-specific version of `vc-dired-state-info'."
426 (let ((hg-state (vc-state file
)))
427 (if (eq hg-state
'edited
)
428 (if (equal (vc-workfile-version file
) "0")
429 "(added)" "(modified)")
430 ;; fall back to the default VC representation
431 (vc-default-dired-state-info 'Hg file
))))
433 ;; Modelled after the similar function in vc-bzr.el
434 (defun vc-hg-revert (file &optional contents-done
)
435 (unless contents-done
436 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
438 ;;; Internal functions
440 (defun vc-hg-command (buffer okstatus file
&rest flags
)
441 "A wrapper around `vc-do-command' for use in vc-hg.el.
442 The difference to vc-do-command is that this function always invokes `hg',
443 and that it passes `vc-hg-global-switches' to it before FLAGS."
444 (apply 'vc-do-command buffer okstatus
"hg" file
445 (if (stringp vc-hg-global-switches
)
446 (cons vc-hg-global-switches flags
)
447 (append vc-hg-global-switches
450 (defun vc-hg-root (file)
451 (vc-find-root file
".hg"))
455 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
456 ;;; vc-hg.el ends here