1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 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 ;; 1) 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) NOT NEEDED
45 ;; - dir-status (dir update-function) OK
46 ;; - dir-status-files (dir files ds uf) OK
47 ;; - dir-extra-headers (dir) OK
48 ;; - dir-printer (fileinfo) OK
49 ;; * working-revision (file) OK
50 ;; - latest-on-branch-p (file) ??
51 ;; * checkout-model (files) OK
52 ;; - workfile-unchanged-p (file) OK
53 ;; - mode-line-string (file) NOT NEEDED
54 ;; STATE-CHANGING FUNCTIONS
55 ;; * register (files &optional rev comment) OK
56 ;; * create-repo () OK
57 ;; - init-revision () NOT NEEDED
58 ;; - responsible-p (file) OK
59 ;; - could-register (file) OK
60 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
61 ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
62 ;; * checkin (files rev comment) OK
63 ;; * find-revision (file rev buffer) OK
64 ;; * checkout (file &optional editable rev) OK
65 ;; * revert (file &optional contents-done) OK
66 ;; - rollback (files) ?? PROBABLY NOT NEEDED
67 ;; - merge (file rev1 rev2) NEEDED
68 ;; - merge-news (file) NEEDED
69 ;; - steal-lock (file &optional revision) NOT NEEDED
71 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
72 ;; - log-view-mode () OK
73 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
74 ;; - comment-history (file) NOT NEEDED
75 ;; - update-changelog (files) NOT NEEDED
76 ;; * diff (files &optional rev1 rev2 buffer) OK
77 ;; - revision-completion-table (files) OK?
78 ;; - annotate-command (file buf &optional rev) OK
79 ;; - annotate-time () OK
80 ;; - annotate-current-time () NOT NEEDED
81 ;; - annotate-extract-revision-at-line () OK
83 ;; - create-tag (dir name branchp) NEEDED
84 ;; - retrieve-tag (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
96 ;; 2) Implement Stefan Monnier's advice:
97 ;; vc-hg-registered and vc-hg-state
98 ;; Both of those functions should be super extra careful to fail gracefully in
99 ;; unexpected circumstances. The reason this is important is that any error
100 ;; there will prevent the user from even looking at the file :-(
101 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
102 ;; mercurial's control and extracting the current revision should be done
103 ;; without even using `hg' (this way even if you don't have `hg' installed,
104 ;; 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" :value
("") string
))
126 (defcustom vc-hg-diff-switches t
; Hg doesn't support common args like -u
127 "String or list of strings specifying switches for Hg diff under VC.
128 If nil, use the value of `vc-diff-switches'. If t, use no switches."
129 :type
'(choice (const :tag
"Unspecified" nil
)
130 (const :tag
"None" t
)
131 (string :tag
"Argument String")
132 (repeat :tag
"Argument List" :value
("") string
))
137 ;;; Properties of the backend
139 (defun vc-hg-revision-granularity () 'repository
)
140 (defun vc-hg-checkout-model (files) 'implicit
)
142 ;;; State querying functions
144 ;;;###autoload (defun vc-hg-registered (file)
145 ;;;###autoload "Return non-nil if FILE is registered with hg."
146 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
147 ;;;###autoload (progn
148 ;;;###autoload (load "vc-hg")
149 ;;;###autoload (vc-hg-registered file))))
151 ;; Modeled after the similar function in vc-bzr.el
152 (defun vc-hg-registered (file)
153 "Return non-nil if FILE is registered with hg."
154 (when (vc-hg-root file
) ; short cut
155 (let ((state (vc-hg-state file
))) ; expensive
156 (and state
(not (memq state
'(ignored unregistered
)))))))
158 (defun vc-hg-state (file)
159 "Hg-specific version of `vc-state'."
162 (default-directory (file-name-directory file
))
164 (with-output-to-string
169 ;; Ignore all errors.
170 (let ((process-environment
171 ;; Avoid localization of messages so we
172 ;; can parse the output.
173 (append (list "TERM=dumb" "LANGUAGE=C" "HGRCPATH=")
174 process-environment
)))
177 "status" "-A" (file-relative-name file
)))
178 ;; Some problem happened. E.g. We can't find an `hg'
182 (when (null (string-match ".*: No such file or directory$" out
))
183 (let ((state (aref out
0)))
185 ((eq state ?
=) 'up-to-date
)
186 ((eq state ?A
) 'added
)
187 ((eq state ?M
) 'edited
)
188 ((eq state ?I
) 'ignored
)
189 ((eq state ?R
) 'removed
)
190 ((eq state ?
!) 'missing
)
191 ((eq state ??
) 'unregistered
)
192 ((eq state ?C
) 'up-to-date
) ;; Older mercurials use this
193 (t 'up-to-date
)))))))
195 (defun vc-hg-working-revision (file)
196 "Hg-specific version of `vc-working-revision'."
199 (default-directory (file-name-directory file
))
200 ;; Avoid localization of messages so we can parse the output.
201 (avoid-local-env (append (list "TERM=dumb" "LANGUAGE=C" "HGRCPATH=")
202 process-environment
))
204 (with-output-to-string
209 (let ((process-environment avoid-local-env
))
210 ;; Ignore all errors.
213 "parents" "--template" "{rev}" (file-relative-name file
)))
214 ;; Some problem happened. E.g. We can't find an `hg'
219 ;; Check if the file is in the 'added state, the above hg
220 ;; command does not distinguish between 'added and 'unregistered.
223 (let ((process-environment avoid-local-env
))
226 ;; We use "log" here, if there's a faster command
227 ;; that returns true for an 'added file and false
228 ;; for an 'unregistered one, we could use that.
229 "log" "-l1" (file-relative-name file
)))
230 ;; Some problem happened. E.g. We can't find an `hg'
233 (when (eq 0 status
) "0"))))
235 ;;; History functions
237 (defcustom vc-hg-log-switches nil
238 "String or list of strings specifying switches for hg log under VC."
239 :type
'(choice (const :tag
"None" nil
)
240 (string :tag
"Argument String")
241 (repeat :tag
"Argument List" :value
("") string
))
244 (defun vc-hg-print-log (files buffer
&optional shortlog start-revision limit
)
245 "Get change log associated with FILES."
246 ;; `vc-do-command' creates the buffer, but we need it before running
248 (vc-setup-buffer buffer
)
249 ;; If the buffer exists from a previous invocation it might be
251 (let ((inhibit-read-only t
))
254 (apply 'vc-hg-command buffer
0 files
"log"
256 (when start-revision
(list (format "-r%s:" start-revision
)))
257 (when limit
(list "-l" (format "%s" limit
)))
258 (when shortlog
'("--style" "compact"))
259 vc-hg-log-switches
)))))
261 (defvar log-view-message-re
)
262 (defvar log-view-file-re
)
263 (defvar log-view-font-lock-keywords
)
264 (defvar log-view-per-file-logs
)
265 (defvar vc-short-log
)
267 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
268 (require 'add-log
) ;; we need the add-log faces
269 (set (make-local-variable 'log-view-file-re
) "\\`a\\`")
270 (set (make-local-variable 'log-view-per-file-logs
) nil
)
271 (set (make-local-variable 'log-view-message-re
)
273 "^\\([0-9]+\\)\\(?:\\[.*\\]\\)? +\\([0-9a-z]\\{12\\}\\) +\\(\\(?:[0-9]+\\)-\\(?:[0-9]+\\)-\\(?:[0-9]+\\) \\(?:[0-9]+\\):\\(?:[0-9]+\\) \\(?:[-+0-9]+\\)\\) +\\(.*\\)$"
274 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
275 (set (make-local-variable 'log-view-font-lock-keywords
)
277 (append `((,log-view-message-re
278 (1 'log-view-message-face
)
279 (2 'log-view-message-face
)
281 (4 'change-log-name
))))
283 log-view-font-lock-keywords
286 ;; user: FirstName LastName <foo@bar>
287 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
289 (2 'change-log-email
))
294 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
295 (1 'change-log-email
))
296 ("^date: \\(.+\\)" (1 'change-log-date
))
297 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
)))))))
299 (defun vc-hg-diff (files &optional oldvers newvers buffer
)
300 "Get a difference report using hg between two revisions of FILES."
301 (let* ((firstfile (car files
))
302 (working (and firstfile
(vc-working-revision firstfile
))))
303 (when (and (equal oldvers working
) (not newvers
))
305 (when (and (not oldvers
) newvers
)
306 (setq oldvers working
))
307 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil files
"diff"
309 (vc-switches 'hg
'diff
)
312 (list "-r" oldvers
"-r" newvers
)
313 (list "-r" oldvers
)))))))
315 (defun vc-hg-revision-table (files)
316 (let ((default-directory (file-name-directory (car files
))))
318 (vc-hg-command t nil files
"log" "--template" "{rev} ")
320 (buffer-substring-no-properties (point-min) (point-max))))))
322 ;; Modeled after the similar function in vc-cvs.el
323 (defun vc-hg-revision-completion-table (files)
324 (lexical-let ((files files
)
326 (setq table
(lazy-completion-table
327 table
(lambda () (vc-hg-revision-table files
))))
330 (defun vc-hg-annotate-command (file buffer
&optional revision
)
331 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
332 Optional arg REVISION is a revision to annotate from."
333 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" "--follow"
334 (when revision
(concat "-r" revision
))))
336 (declare-function vc-annotate-convert-time
"vc-annotate" (time))
338 ;; The format for one line output by "hg annotate -d -n" looks like this:
339 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
340 ;; i.e: VERSION_NUMBER DATE: CONTENTS
341 ;; If the user has set the "--follow" option, the output looks like:
342 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
343 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
344 (defconst vc-hg-annotate-re
345 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\(.+\\): \\)\\)")
347 (defun vc-hg-annotate-time ()
348 (when (looking-at vc-hg-annotate-re
)
349 (goto-char (match-end 0))
350 (vc-annotate-convert-time
351 (date-to-time (match-string-no-properties 2)))))
353 (defun vc-hg-annotate-extract-revision-at-line ()
356 (when (looking-at vc-hg-annotate-re
)
357 (if (match-beginning 3)
358 (match-string-no-properties 1)
359 (cons (match-string-no-properties 1)
360 (expand-file-name (match-string-no-properties 4)
361 (vc-hg-root default-directory
)))))))
363 (defun vc-hg-previous-revision (file rev
)
364 (let ((newrev (1- (string-to-number rev
))))
366 (number-to-string newrev
))))
368 (defun vc-hg-next-revision (file rev
)
369 (let ((newrev (1+ (string-to-number rev
)))
372 (vc-hg-command t
0 nil
"tip")
373 (goto-char (point-min))
374 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
375 (string-to-number (match-string-no-properties 1)))))
376 ;; We don't want to exceed the maximum possible revision number, ie
378 (when (<= newrev tip-revision
)
379 (number-to-string newrev
))))
381 ;; Modeled after the similar function in vc-bzr.el
382 (defun vc-hg-delete-file (file)
383 "Delete FILE and delete it in the hg repository."
387 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
389 ;; Modeled after the similar function in vc-bzr.el
390 (defun vc-hg-rename-file (old new
)
391 "Rename file from OLD to NEW using `hg mv'."
392 (vc-hg-command nil
0 new
"mv" old
))
394 (defun vc-hg-register (files &optional rev comment
)
395 "Register FILES under hg.
398 (vc-hg-command nil
0 files
"add"))
400 (defun vc-hg-create-repo ()
401 "Create a new Mercurial repository."
402 (vc-hg-command nil
0 nil
"init"))
404 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
406 ;; Modeled after the similar function in vc-bzr.el
407 (defun vc-hg-could-register (file)
408 "Return non-nil if FILE could be registered under hg."
409 (and (vc-hg-responsible-p file
) ; shortcut
412 (vc-hg-command t nil file
"add" "--dry-run"))
413 ;; The command succeeds with no output if file is
417 ;; FIXME: This would remove the file. Is that correct?
418 ;; (defun vc-hg-unregister (file)
419 ;; "Unregister FILE from hg."
420 ;; (vc-hg-command nil nil file "remove"))
422 (defun vc-hg-checkin (files rev comment
)
423 "Hg-specific version of `vc-backend-checkin'.
425 (vc-hg-command nil
0 files
"commit" "-m" comment
))
427 (defun vc-hg-find-revision (file rev buffer
)
428 (let ((coding-system-for-read 'binary
)
429 (coding-system-for-write 'binary
))
431 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
432 (vc-hg-command buffer
0 file
"cat"))))
434 ;; Modeled after the similar function in vc-bzr.el
435 (defun vc-hg-checkout (file &optional editable rev
)
436 "Retrieve a revision of FILE.
438 REV is the revision to check out into WORKFILE."
439 (let ((coding-system-for-read 'binary
)
440 (coding-system-for-write 'binary
))
441 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
443 (vc-hg-command t
0 file
"cat" "-r" rev
)
444 (vc-hg-command t
0 file
"cat")))))
446 ;; Modeled after the similar function in vc-bzr.el
447 (defun vc-hg-workfile-unchanged-p (file)
448 (eq 'up-to-date
(vc-hg-state file
)))
450 ;; Modeled after the similar function in vc-bzr.el
451 (defun vc-hg-revert (file &optional contents-done
)
452 (unless contents-done
453 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
455 ;;; Hg specific functionality.
457 (defvar vc-hg-extra-menu-map
458 (let ((map (make-sparse-keymap)))
459 (define-key map
[incoming] '(menu-item "Show incoming" vc-hg-incoming))
460 (define-key map [outgoing] '(menu-item "Show outgoing" vc-hg-outgoing))
463 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
465 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
467 (defvar log-view-vc-backend)
469 (define-derived-mode vc-hg-outgoing-mode vc-hg-log-view-mode "Hg-Outgoing"
470 "Mode for browsing Hg outgoing changes."
471 (set (make-local-variable 'log-view-vc-backend) 'Hg))
473 (define-derived-mode vc-hg-incoming-mode vc-hg-log-view-mode "Hg-Incoming"
474 "Mode for browsing Hg incoming changes."
475 (set (make-local-variable 'log-view-vc-backend) 'Hg))
477 (defstruct (vc-hg-extra-fileinfo
479 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
480 (:conc-name vc-hg-extra-fileinfo->))
481 rename-state ;; rename or copy state
482 extra-name) ;; original name for copies and rename targets, new name for
484 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
486 (defun vc-hg-dir-printer (info)
487 "Pretty-printer for the vc-dir-fileinfo structure."
488 (let ((extra (vc-dir-fileinfo->extra info)))
489 (vc-default-dir-printer 'Hg info)
493 (case (vc-hg-extra-fileinfo->rename-state extra)
494 ('copied "copied from")
495 ('renamed-from "renamed from")
496 ('renamed-to "renamed to"))
497 (vc-hg-extra-fileinfo->extra-name extra))
498 'face 'font-lock-comment-face)))))
500 (defun vc-hg-after-dir-status (update-function)
501 (let ((status-char nil)
503 (translation '((?= . up-to-date)
510 (? . copy-rename-line)
511 (?? . unregistered)))
515 (last-line-copy nil))
516 (goto-char (point-min))
518 (setq translated (cdr (assoc (char-after) translation)))
520 (buffer-substring-no-properties (+ (point) 2)
521 (line-end-position)))
522 (cond ((not translated)
523 (setq last-line-copy nil))
524 ((eq translated 'up-to-date)
525 (setq last-line-copy nil))
526 ((eq translated 'copy-rename-line)
527 ;; For copied files the output looks like this:
528 ;; A COPIED_FILE_NAME
529 ;; ORIGINAL_FILE_NAME
530 (setf (nth 2 last-added)
531 (vc-hg-create-extra-fileinfo 'copied file))
532 (setq last-line-copy t))
533 ((and last-line-copy (eq translated 'removed))
534 ;; For renamed files the output looks like this:
536 ;; ORIGINAL_FILE_NAME
537 ;; R ORIGINAL_FILE_NAME
538 ;; We need to adjust the previous entry to not think it is a copy.
539 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
541 (push (list file translated
542 (vc-hg-create-extra-fileinfo
543 'renamed-to (nth 0 last-added))) result)
544 (setq last-line-copy nil))
546 (setq last-added (list file translated nil))
547 (push last-added result)
548 (setq last-line-copy nil)))
550 (funcall update-function result)))
552 (defun vc-hg-dir-status (dir update-function)
553 (vc-hg-command (current-buffer) 'async dir "status" "-C")
555 `(vc-hg-after-dir-status (quote ,update-function))))
557 (defun vc-hg-dir-status-files (dir files default-state update-function)
558 (apply 'vc-hg-command (current-buffer) 'async dir "status" "-C" files)
560 `(vc-hg-after-dir-status (quote ,update-function))))
562 (defun vc-hg-dir-extra-header (name &rest commands)
563 (concat (propertize name 'face 'font-lock-type-face)
566 (apply 'vc-hg-command (current-buffer) 0 nil commands)
567 (buffer-substring-no-properties (point-min) (1- (point-max))))
568 'face 'font-lock-variable-name-face)))
570 (defun vc-hg-dir-extra-headers (dir)
571 "Generate extra status headers for a Mercurial tree."
572 (let ((default-directory dir))
574 (vc-hg-dir-extra-header "Root : " "root") "\n"
575 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
576 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
577 ;; these change after each commit
578 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
579 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
582 ;; FIXME: this adds another top level menu, instead figure out how to
583 ;; replace the Log-View menu.
584 (easy-menu-define log-view-mode-menu vc-hg-outgoing-mode-map
585 "Hg-outgoing Display Menu"
587 ["Push selected" vc-hg-push]))
589 (easy-menu-define log-view-mode-menu vc-hg-incoming-mode-map
590 "Hg-incoming Display Menu"
592 ["Pull selected" vc-hg-pull]))
594 (defun vc-hg-outgoing ()
596 (let ((bname "*Hg outgoing*")
598 (vc-hg-command bname 1 nil "outgoing" "-n")
599 (pop-to-buffer bname)
600 (vc-hg-outgoing-mode)))
602 (defun vc-hg-incoming ()
604 (let ((bname "*Hg incoming*")
606 (vc-hg-command bname 0 nil "incoming" "-n")
607 (pop-to-buffer bname)
608 (vc-hg-incoming-mode)))
610 (declare-function log-view-get-marked "log-view" ())
612 ;; XXX maybe also add key bindings for these functions.
615 (let ((marked-list (log-view-get-marked)))
621 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
622 (error "No log entries selected for push"))))
626 (let ((marked-list (log-view-get-marked)))
632 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
633 (error "No log entries selected for pull"))))
635 ;;; Internal functions
637 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
638 "A wrapper around `vc-do-command' for use in vc-hg.el.
639 The difference to vc-do-command is that this function always invokes `hg',
640 and that it passes `vc-hg-global-switches' to it before FLAGS."
641 (apply 'vc-do-command (or buffer "*vc*") okstatus "hg" file-or-list
642 (if (stringp vc-hg-global-switches)
643 (cons vc-hg-global-switches flags)
644 (append vc-hg-global-switches
647 (defun vc-hg-root (file)
648 (vc-find-root file ".hg"))
652 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
653 ;;; vc-hg.el ends here