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 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is a mercurial version control backend
35 ;; Implement the rest of the vc interface. See the comment at the
36 ;; beginning of vc.el. The current status is:
38 ;; FUNCTION NAME STATUS
40 ;; * revision-granularity OK
41 ;; STATE-QUERYING FUNCTIONS
42 ;; * registered (file) OK
44 ;; - state-heuristic (file) ?? PROBABLY NOT NEEDED
45 ;; - dir-state (dir) OK
46 ;; * working-revision (file) OK
47 ;; - latest-on-branch-p (file) ??
48 ;; * checkout-model (files) OK
49 ;; - workfile-unchanged-p (file) OK
50 ;; - mode-line-string (file) NOT NEEDED
51 ;; - prettify-state-info (file) OK
52 ;; STATE-CHANGING FUNCTIONS
53 ;; * register (files &optional rev comment) OK
54 ;; * create-repo () OK
55 ;; - init-revision () NOT NEEDED
56 ;; - responsible-p (file) OK
57 ;; - could-register (file) OK
58 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
59 ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
60 ;; * checkin (files rev comment) OK
61 ;; * find-revision (file rev buffer) OK
62 ;; * checkout (file &optional editable rev) OK
63 ;; * revert (file &optional contents-done) OK
64 ;; - rollback (files) ?? PROBABLY NOT NEEDED
65 ;; - merge (file rev1 rev2) NEEDED
66 ;; - merge-news (file) NEEDED
67 ;; - steal-lock (file &optional revision) NOT NEEDED
69 ;; * print-log (files &optional buffer) OK
70 ;; - log-view-mode () OK
71 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
72 ;; - wash-log (file) ??
73 ;; - comment-history (file) NOT NEEDED
74 ;; - update-changelog (files) NOT NEEDED
75 ;; * diff (files &optional rev1 rev2 buffer) OK
76 ;; - revision-completion-table (files) OK?
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-revision (file rev) OK
89 ;; - next-revision (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"
129 ;;; Properties of the backend
131 (defun vc-hg-revision-granularity () 'repository
)
132 (defun vc-hg-checkout-model (files) 'implicit
)
134 ;;; State querying functions
136 ;;;###autoload (defun vc-hg-registered (file)
137 ;;;###autoload "Return non-nil if FILE is registered with hg."
138 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
139 ;;;###autoload (progn
140 ;;;###autoload (load "vc-hg")
141 ;;;###autoload (vc-hg-registered file))))
143 ;; Modelled after the similar function in vc-bzr.el
144 (defun vc-hg-registered (file)
145 "Return non-nil if FILE is registered with hg."
146 (when (vc-hg-root file
) ; short cut
147 (let ((state (vc-hg-state file
))) ; expensive
148 (vc-file-setprop file
'vc-state state
)
149 (and state
(not (memq state
'(ignored unregistered
)))))))
151 (defun vc-hg-state (file)
152 "Hg-specific version of `vc-state'."
156 (with-output-to-string
161 ;; Ignore all errors.
163 "hg" nil t nil
"--cwd" (file-name-directory file
)
164 "status" "-A" (file-name-nondirectory file
))
165 ;; Some problem happened. E.g. We can't find an `hg'
169 (when (null (string-match ".*: No such file or directory$" out
))
170 (let ((state (aref out
0)))
172 ((eq state ?
=) 'up-to-date
)
173 ((eq state ?A
) 'added
)
174 ((eq state ?M
) 'edited
)
175 ((eq state ?I
) 'ignored
)
176 ((eq state ?R
) 'removed
)
177 ((eq state ?
!) 'missing
)
178 ((eq state ??
) 'unregistered
)
179 ((eq state ?C
) 'up-to-date
) ;; Older mercurials use this
180 (t 'up-to-date
)))))))
182 (defun vc-hg-dir-state (dir)
184 (buffer-disable-undo) ;; Because these buffers can get huge
185 (vc-hg-command (current-buffer) nil dir
"status" "-A")
186 (goto-char (point-min))
187 (let ((status-char nil
)
190 (setq status-char
(char-after))
193 (buffer-substring-no-properties (+ (point) 2)
194 (line-end-position))))
196 ;; State flag for a clean file is now C, might change to =.
197 ;; The rest of the possible states in "hg status" output:
198 ;; ! = deleted, but still tracked
199 ;; should not show up in VC directory buffers, so don't deal with them
202 ;; Mercurial up to 0.9.5 used C, = is used now.
203 ((or (eq status-char ?
=) (eq status-char ?C
))
204 (vc-file-setprop file
'vc-backend
'Hg
)
205 (vc-file-setprop file
'vc-state
'up-to-date
))
207 (vc-file-setprop file
'vc-backend
'Hg
)
208 (vc-file-setprop file
'vc-working-revision
"0")
209 (vc-file-setprop file
'vc-state
'added
))
211 (vc-file-setprop file
'vc-backend
'Hg
)
212 (vc-file-setprop file
'vc-state
'removed
))
214 (vc-file-setprop file
'vc-backend
'Hg
)
215 (vc-file-setprop file
'vc-state
'edited
))
217 (vc-file-setprop file
'vc-backend
'Hg
)
218 (vc-file-setprop file
'vc-state
'ignored
))
220 (vc-file-setprop file
'vc-backend
'none
)
221 (vc-file-setprop file
'vc-state
'unregistered
))
223 (vc-file-setprop file
'vc-backend
'Hg
)
224 (vc-file-setprop file
'vc-state
'missing
))
225 (t ;; Presently C, might change to = in 0.9.6
226 (vc-file-setprop file
'vc-backend
'Hg
)
227 (vc-file-setprop file
'vc-state
'up-to-date
)))
230 (defun vc-hg-working-revision (file)
231 "Hg-specific version of `vc-working-revision'."
235 (with-output-to-string
240 ;; Ignore all errors.
242 "hg" nil t nil
"--cwd" (file-name-directory file
)
243 "log" "-l1" (file-name-nondirectory file
))
244 ;; Some problem happened. E.g. We can't find an `hg'
248 (if (string-match "changeset: *\\([0-9]*\\)" out
)
252 ;;; History functions
254 (defun vc-hg-print-log (files &optional buffer
)
255 "Get change log associated with FILES."
256 ;; `log-view-mode' needs to have the file names in order to function
257 ;; correctly. "hg log" does not print it, so we insert it here by
260 ;; `vc-do-command' creates the buffer, but we need it before running
262 (vc-setup-buffer buffer
)
263 ;; If the buffer exists from a previous invocation it might be
265 (let ((inhibit-read-only t
))
266 ;; We need to loop and call "hg log" on each file separately.
267 ;; "hg log" with multiple file arguments mashes all the logs
268 ;; together. Ironically enough, this puts us back near CVS
269 ;; which can't generate proper fileset logs either.
273 (insert "Working file: " file
"\n")) ;; Like RCS/CVS.
274 (vc-hg-command buffer
0 file
"log"))))
276 (defvar log-view-message-re
)
277 (defvar log-view-file-re
)
278 (defvar log-view-font-lock-keywords
)
280 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
281 (require 'add-log
) ;; we need the add-log faces
282 (set (make-local-variable 'log-view-file-re
) "^Working file:[ \t]+\\(.+\\)")
283 (set (make-local-variable 'log-view-message-re
)
284 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
285 (set (make-local-variable 'log-view-font-lock-keywords
)
287 log-view-font-lock-keywords
290 ;; user: FirstName LastName <foo@bar>
291 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
293 (2 'change-log-email
))
298 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
299 (1 'change-log-email
))
300 ("^date: \\(.+\\)" (1 'change-log-date
))
301 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
))))))
303 (defun vc-hg-diff (files &optional oldvers newvers buffer
)
304 "Get a difference report using hg between two revisions of FILES."
305 (let ((working (vc-working-revision (car files
))))
306 (if (and (equal oldvers working
) (not newvers
))
308 (if (and (not oldvers
) newvers
)
309 (setq oldvers working
))
310 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil
311 (mapcar (lambda (file) (file-name-nondirectory file
)) files
)
312 "--cwd" (file-name-directory (car files
))
317 (list "-r" oldvers
"-r" newvers
)
318 (list "-r" oldvers
)))))))
320 (defun vc-hg-revision-table (files)
321 (let ((default-directory (file-name-directory (car files
))))
323 (vc-hg-command t nil files
"log" "--template" "{rev} ")
325 (buffer-substring-no-properties (point-min) (point-max))))))
327 ;; Modelled after the similar function in vc-cvs.el
328 (defun vc-hg-revision-completion-table (files)
329 (lexical-let ((files files
)
331 (setq table
(lazy-completion-table
332 table
(lambda () (vc-hg-revision-table files
))))
335 (defun vc-hg-annotate-command (file buffer
&optional revision
)
336 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
337 Optional arg REVISION is a revision to annotate from."
338 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" (if revision
(concat "-r" revision
)))
339 (with-current-buffer buffer
340 (goto-char (point-min))
341 (re-search-forward "^[0-9]")
342 (delete-region (point-min) (1- (point)))))
345 ;; The format for one line output by "hg annotate -d -n" looks like this:
346 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
347 ;; i.e: VERSION_NUMBER DATE: CONTENTS
348 (defconst vc-hg-annotate-re
"^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\): ")
350 (defun vc-hg-annotate-time ()
351 (when (looking-at vc-hg-annotate-re
)
352 (goto-char (match-end 0))
353 (vc-annotate-convert-time
354 (date-to-time (match-string-no-properties 2)))))
356 (defun vc-hg-annotate-extract-revision-at-line ()
359 (if (looking-at vc-hg-annotate-re
) (match-string-no-properties 1))))
361 (defun vc-hg-previous-revision (file rev
)
362 (let ((newrev (1- (string-to-number rev
))))
364 (number-to-string newrev
))))
366 (defun vc-hg-next-revision (file rev
)
367 (let ((newrev (1+ (string-to-number rev
)))
370 (vc-hg-command t
0 nil
"tip")
371 (goto-char (point-min))
372 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
373 (string-to-number (match-string-no-properties 1)))))
374 ;; We don't want to exceed the maximum possible revision number, ie
376 (when (<= newrev tip-revision
)
377 (number-to-string newrev
))))
379 ;; Modelled after the similar function in vc-bzr.el
380 (defun vc-hg-delete-file (file)
381 "Delete FILE and delete it in the hg repository."
385 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
387 ;; Modelled after the similar function in vc-bzr.el
388 (defun vc-hg-rename-file (old new
)
389 "Rename file from OLD to NEW using `hg mv'."
390 (vc-hg-command nil
0 new
"mv" old
))
392 (defun vc-hg-register (files &optional rev comment
)
393 "Register FILES under hg.
396 (vc-hg-command nil
0 files
"add"))
398 (defun vc-hg-create-repo ()
399 "Create a new Mercurial repository."
400 (vc-hg-command nil
0 nil
"init"))
402 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
404 ;; Modelled after the similar function in vc-bzr.el
405 (defun vc-hg-could-register (file)
406 "Return non-nil if FILE could be registered under hg."
407 (and (vc-hg-responsible-p file
) ; shortcut
410 (vc-hg-command t nil file
"add" "--dry-run"))
411 ;; The command succeeds with no output if file is
415 ;; XXX This would remove the file. Is that correct?
416 ;; (defun vc-hg-unregister (file)
417 ;; "Unregister FILE from hg."
418 ;; (vc-hg-command nil nil file "remove"))
420 (defun vc-hg-checkin (files rev comment
)
421 "Hg-specific version of `vc-backend-checkin'.
423 (vc-hg-command nil
0 files
"commit" "-m" comment
))
425 (defun vc-hg-find-revision (file rev buffer
)
426 (let ((coding-system-for-read 'binary
)
427 (coding-system-for-write 'binary
))
429 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
430 (vc-hg-command buffer
0 file
"cat"))))
432 ;; Modelled after the similar function in vc-bzr.el
433 (defun vc-hg-checkout (file &optional editable rev
)
434 "Retrieve a revision of FILE.
436 REV is the revision to check out into WORKFILE."
437 (let ((coding-system-for-read 'binary
)
438 (coding-system-for-write 'binary
))
439 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
441 (vc-hg-command t
0 file
"cat" "-r" rev
)
442 (vc-hg-command t
0 file
"cat")))))
444 ;; Modelled after the similar function in vc-bzr.el
445 (defun vc-hg-workfile-unchanged-p (file)
446 (eq 'up-to-date
(vc-hg-state file
)))
448 ;; Modelled after the similar function in vc-bzr.el
449 (defun vc-hg-revert (file &optional contents-done
)
450 (unless contents-done
451 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
453 ;;; Hg specific functionality.
455 (defvar vc-hg-extra-menu-map
456 (let ((map (make-sparse-keymap)))
457 (define-key map
[incoming] '(menu-item "Show incoming" vc-hg-incoming))
458 (define-key map [outgoing] '(menu-item "Show outgoing" vc-hg-outgoing))
461 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
463 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
465 (define-derived-mode vc-hg-outgoing-mode vc-hg-log-view-mode "Hg-Outgoing")
467 (define-derived-mode vc-hg-incoming-mode vc-hg-log-view-mode "Hg-Incoming")
469 (defstruct (vc-hg-extra-fileinfo
471 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
472 (:conc-name vc-hg-extra-fileinfo->))
473 rename-state ;; rename or copy state
474 extra-name) ;; original name for copies and rename targets, new name for
476 (defun vc-hg-status-printer (info)
477 "Pretty-printer for the vc-dir-fileinfo structure."
478 (let ((extra (vc-dir-fileinfo->extra info)))
479 (vc-default-status-printer 'Hg info)
483 (case (vc-hg-extra-fileinfo->rename-state extra)
484 ('copied "copied from")
485 ('renamed-from "renamed from")
486 ('renamed-to "renamed to"))
487 (vc-hg-extra-fileinfo->extra-name extra))
488 'face 'font-lock-comment-face)))))
490 (defun vc-hg-after-dir-status (update-function)
491 (let ((status-char nil)
493 (translation '((?= . up-to-date)
500 (? . copy-rename-line)
501 (?? . unregistered)))
505 (last-line-copy nil))
506 (goto-char (point-min))
508 (setq translated (cdr (assoc (char-after) translation)))
510 (buffer-substring-no-properties (+ (point) 2)
511 (line-end-position)))
512 (cond ((not translated)
513 (setq last-line-copy nil))
514 ((eq translated 'up-to-date)
515 (setq last-line-copy nil))
516 ((eq translated 'copy-rename-line)
517 ;; For copied files the output looks like this:
518 ;; A COPIED_FILE_NAME
519 ;; ORIGINAL_FILE_NAME
520 (setf (nth 2 last-added)
521 (vc-hg-create-extra-fileinfo 'copied file))
522 (setq last-line-copy t))
523 ((and last-line-copy (eq translated 'removed))
524 ;; For renamed files the output looks like this:
526 ;; ORIGINAL_FILE_NAME
527 ;; R ORIGINAL_FILE_NAME
528 ;; We need to adjust the previous entry to not think it is a copy.
529 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
531 (push (list file translated
532 (vc-hg-create-extra-fileinfo
533 'renamed-to (nth 0 last-added))) result)
534 (setq last-line-copy nil))
536 (setq last-added (list file translated nil))
537 (push last-added result)
538 (setq last-line-copy nil)))
540 (funcall update-function result)))
542 (defun vc-hg-dir-status (dir update-function)
543 (vc-hg-command (current-buffer) 'async dir "status" "-C")
545 `(vc-hg-after-dir-status (quote ,update-function))))
547 ;; XXX this adds another top level menu, instead figure out how to
548 ;; replace the Log-View menu.
549 (easy-menu-define log-view-mode-menu vc-hg-outgoing-mode-map
550 "Hg-outgoing Display Menu"
552 ["Push selected" vc-hg-push]))
554 (easy-menu-define log-view-mode-menu vc-hg-incoming-mode-map
555 "Hg-incoming Display Menu"
557 ["Pull selected" vc-hg-pull]))
559 (defun vc-hg-outgoing ()
561 (let ((bname "*Hg outgoing*"))
562 (vc-hg-command bname 0 nil "outgoing" "-n")
563 (pop-to-buffer bname)
564 (vc-hg-outgoing-mode)))
566 (defun vc-hg-incoming ()
568 (let ((bname "*Hg incoming*"))
569 (vc-hg-command bname 0 nil "incoming" "-n")
570 (pop-to-buffer bname)
571 (vc-hg-incoming-mode)))
573 (declare-function log-view-get-marked "log-view" ())
575 ;; XXX maybe also add key bindings for these functions.
578 (let ((marked-list (log-view-get-marked)))
584 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
585 (error "No log entries selected for push"))))
589 (let ((marked-list (log-view-get-marked)))
595 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
596 (error "No log entries selected for pull"))))
598 ;;; Internal functions
600 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
601 "A wrapper around `vc-do-command' for use in vc-hg.el.
602 The difference to vc-do-command is that this function always invokes `hg',
603 and that it passes `vc-hg-global-switches' to it before FLAGS."
604 (apply 'vc-do-command buffer okstatus "hg" file-or-list
605 (if (stringp vc-hg-global-switches)
606 (cons vc-hg-global-switches flags)
607 (append vc-hg-global-switches
610 (defun vc-hg-root (file)
611 (vc-find-root file ".hg"))
615 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
616 ;;; vc-hg.el ends here