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
42 ;; * revision-granularity OK
43 ;; STATE-QUERYING FUNCTIONS
44 ;; * registered (file) OK
46 ;; - state-heuristic (file) ?? PROBABLY NOT NEEDED
47 ;; - dir-state (dir) OK
48 ;; * working-revision (file) OK
49 ;; - latest-on-branch-p (file) ??
50 ;; * checkout-model (files) OK
51 ;; - workfile-unchanged-p (file) OK
52 ;; - mode-line-string (file) NOT NEEDED
53 ;; - prettify-state-info (file) OK
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 &optional buffer) OK
72 ;; - log-view-mode () OK
73 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
74 ;; - wash-log (file) ??
75 ;; - comment-history (file) NOT NEEDED
76 ;; - update-changelog (files) NOT NEEDED
77 ;; * diff (files &optional rev1 rev2 buffer) OK
78 ;; - revision-completion-table (files) OK?
79 ;; - annotate-command (file buf &optional rev) OK
80 ;; - annotate-time () OK
81 ;; - annotate-current-time () ?? NOT NEEDED
82 ;; - annotate-extract-revision-at-line () OK
84 ;; - create-snapshot (dir name branchp) NEEDED (probably branch?)
85 ;; - assign-name (file name) NOT NEEDED
86 ;; - retrieve-snapshot (dir name update) ?? NEEDED??
88 ;; - make-version-backups-p (file) ??
89 ;; - repository-hostname (dirname) ??
90 ;; - previous-revision (file rev) OK
91 ;; - next-revision (file rev) OK
92 ;; - check-headers () ??
93 ;; - clear-headers () ??
94 ;; - delete-file (file) TEST IT
95 ;; - rename-file (old new) OK
96 ;; - find-file-hook () PROBABLY NOT NEEDED
97 ;; - find-file-not-found-hook () PROBABLY NOT NEEDED
99 ;; Implement Stefan Monnier's advice:
100 ;; vc-hg-registered and vc-hg-state
101 ;; Both of those functions should be super extra careful to fail gracefully in
102 ;; unexpected circumstances. The reason this is important is that any error
103 ;; there will prevent the user from even looking at the file :-(
104 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
105 ;; mercurial's control and extracting the current revision should be done
106 ;; without even using `hg' (this way even if you don't have `hg' installed,
107 ;; Emacs is able to tell you this file is under mercurial's control).
118 ;;; Customization options
120 (defcustom vc-hg-global-switches nil
121 "*Global switches to pass to any Hg command."
122 :type '(choice (const :tag "None" nil)
123 (string :tag "Argument String")
124 (repeat :tag "Argument List"
131 ;;; Properties of the backend
133 (defun vc-hg-revision-granularity () 'repository)
134 (defun vc-hg-checkout-model (files) 'implicit)
136 ;;; State querying functions
138 ;;;###autoload (defun vc-hg-registered (file)
139 ;;;###autoload "Return non-nil if FILE is registered with hg."
140 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
141 ;;;###autoload (progn
142 ;;;###autoload (load "vc-hg")
143 ;;;###autoload (vc-hg-registered file))))
145 ;; Modelled after the similar function in vc-bzr.el
146 (defun vc-hg-registered (file)
147 "Return non-nil if FILE is registered with hg."
148 (when (vc-hg-root file) ; short cut
149 (let ((state (vc-hg-state file))) ; expensive
150 (vc-file-setprop file 'vc-state state)
151 (and state (not (memq state '(ignored unregistered)))))))
153 (defun vc-hg-state (file)
154 "Hg-specific version of `vc-state'."
158 (with-output-to-string
163 ;; Ignore all errors.
165 "hg" nil t nil "--cwd" (file-name-directory file)
166 "status" "-A" (file-name-nondirectory file))
167 ;; Some problem happened. E.g. We can't find an `hg'
171 (when (null (string-match ".*: No such file or directory$" out))
172 (let ((state (aref out 0)))
174 ((eq state ?=) 'up-to-date)
175 ((eq state ?A) 'added)
176 ((eq state ?M) 'edited)
177 ((eq state ?I) 'ignored)
178 ((eq state ?R) 'removed)
179 ((eq state ?!) 'missing)
180 ((eq state ??) 'unregistered)
181 ((eq state ?C) 'up-to-date) ;; Older mercurials use this
182 (t 'up-to-date)))))))
184 (defun vc-hg-dir-state (dir)
186 (buffer-disable-undo) ;; Because these buffers can get huge
187 (vc-hg-command (current-buffer) nil dir "status" "-A")
188 (goto-char (point-min))
189 (let ((status-char nil)
192 (setq status-char (char-after))
195 (buffer-substring-no-properties (+ (point) 2)
196 (line-end-position))))
198 ;; State flag for a clean file is now C, might change to =.
199 ;; The rest of the possible states in "hg status" output:
200 ;; ! = deleted, but still tracked
201 ;; should not show up in VC directory buffers, so don't deal with them
204 ;; Mercurial up to 0.9.5 used C, = is used now.
205 ((or (eq status-char ?=) (eq status-char ?C))
206 (vc-file-setprop file 'vc-backend 'Hg)
207 (vc-file-setprop file 'vc-state 'up-to-date))
209 (vc-file-setprop file 'vc-backend 'Hg)
210 (vc-file-setprop file 'vc-working-revision "0")
211 (vc-file-setprop file 'vc-state 'added))
213 (vc-file-setprop file 'vc-backend 'Hg)
214 (vc-file-setprop file 'vc-state 'removed))
216 (vc-file-setprop file 'vc-backend 'Hg)
217 (vc-file-setprop file 'vc-state 'edited))
219 (vc-file-setprop file 'vc-backend 'Hg)
220 (vc-file-setprop file 'vc-state 'ignored))
222 (vc-file-setprop file 'vc-backend 'none)
223 (vc-file-setprop file 'vc-state 'unregistered))
225 (vc-file-setprop file 'vc-backend 'Hg)
226 (vc-file-setprop file 'vc-state 'missing))
227 (t ;; Presently C, might change to = in 0.9.6
228 (vc-file-setprop file 'vc-backend 'Hg)
229 (vc-file-setprop file 'vc-state 'up-to-date)))
232 (defun vc-hg-working-revision (file)
233 "Hg-specific version of `vc-working-revision'."
237 (with-output-to-string
242 ;; Ignore all errors.
244 "hg" nil t nil "--cwd" (file-name-directory file)
245 "log" "-l1" (file-name-nondirectory file))
246 ;; Some problem happened. E.g. We can't find an `hg'
250 (if (string-match "changeset: *\\([0-9]*\\)" out)
254 ;;; History functions
256 (defun vc-hg-print-log (files &optional buffer)
257 "Get change log associated with FILES."
258 ;; `log-view-mode' needs to have the file names in order to function
259 ;; correctly. "hg log" does not print it, so we insert it here by
262 ;; `vc-do-command' creates the buffer, but we need it before running
264 (vc-setup-buffer buffer)
265 ;; If the buffer exists from a previous invocation it might be
267 (let ((inhibit-read-only t))
268 ;; We need to loop and call "hg log" on each file separately.
269 ;; "hg log" with multiple file arguments mashes all the logs
270 ;; together. Ironically enough, this puts us back near CVS
271 ;; which can't generate proper fileset logs either.
275 (insert "Working file: " file "\n")) ;; Like RCS/CVS.
276 (vc-hg-command buffer 0 file "log"))))
278 (defvar log-view-message-re)
279 (defvar log-view-file-re)
280 (defvar log-view-font-lock-keywords)
282 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
283 (require 'add-log) ;; we need the add-log faces
284 (set (make-local-variable 'log-view-file-re) "^Working file:[ \t]+\\(.+\\)")
285 (set (make-local-variable 'log-view-message-re)
286 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
287 (set (make-local-variable 'log-view-font-lock-keywords)
289 log-view-font-lock-keywords
292 ;; user: FirstName LastName <foo@bar>
293 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
295 (2 'change-log-email))
300 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
301 (1 'change-log-email))
302 ("^date: \\(.+\\)" (1 'change-log-date))
303 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
305 (defun vc-hg-diff (files &optional oldvers newvers buffer)
306 "Get a difference report using hg between two revisions of FILES."
307 (let ((working (vc-working-revision (car files))))
308 (if (and (equal oldvers working) (not newvers))
310 (if (and (not oldvers) newvers)
311 (setq oldvers working))
312 (apply #'vc-hg-command (or buffer "*vc-diff*") nil
313 (mapcar (lambda (file) (file-name-nondirectory file)) files)
314 "--cwd" (file-name-directory (car files))
319 (list "-r" oldvers "-r" newvers)
320 (list "-r" oldvers)))))))
322 (defun vc-hg-revision-table (files)
323 (let ((default-directory (file-name-directory (car files))))
325 (vc-hg-command t nil files "log" "--template" "{rev} ")
327 (buffer-substring-no-properties (point-min) (point-max))))))
329 ;; Modelled after the similar function in vc-cvs.el
330 (defun vc-hg-revision-completion-table (files)
331 (lexical-let ((files files)
333 (setq table (lazy-completion-table
334 table (lambda () (vc-hg-revision-table files))))
337 (defun vc-hg-annotate-command (file buffer &optional revision)
338 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
339 Optional arg REVISION is a revision to annotate from."
340 (vc-hg-command buffer 0 file "annotate" "-d" "-n" (if revision (concat "-r" revision)))
341 (with-current-buffer buffer
342 (goto-char (point-min))
343 (re-search-forward "^[0-9]")
344 (delete-region (point-min) (1- (point)))))
347 ;; The format for one line output by "hg annotate -d -n" looks like this:
348 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
349 ;; i.e: VERSION_NUMBER DATE: CONTENTS
350 (defconst vc-hg-annotate-re "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\): ")
352 (defun vc-hg-annotate-time ()
353 (when (looking-at vc-hg-annotate-re)
354 (goto-char (match-end 0))
355 (vc-annotate-convert-time
356 (date-to-time (match-string-no-properties 2)))))
358 (defun vc-hg-annotate-extract-revision-at-line ()
361 (if (looking-at vc-hg-annotate-re) (match-string-no-properties 1))))
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 ;; Modelled 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 ;; Modelled 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 ;; Modelled 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 ;; XXX 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 ;; Modelled 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 ;; Modelled 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 ;; Modelled 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 (define-derived-mode vc-hg-outgoing-mode vc-hg-log-view-mode "Hg-Outgoing")
469 (define-derived-mode vc-hg-incoming-mode vc-hg-log-view-mode "Hg-Incoming")
471 (defstruct (vc-hg-extra-fileinfo
473 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
474 (:conc-name vc-hg-extra-fileinfo->))
475 rename-state ;; rename or copy state
476 extra-name) ;; original name for copies and rename targets, new name for
478 (defun vc-hg-status-printer (info)
479 "Pretty-printer for the vc-dir-fileinfo structure."
480 (let ((extra (vc-dir-fileinfo->extra info)))
481 (vc-default-status-printer 'Hg info)
485 (case (vc-hg-extra-fileinfo->rename-state extra)
486 ('copied "copied from")
487 ('renamed-from "renamed from")
488 ('renamed-to "renamed to"))
489 (vc-hg-extra-fileinfo->extra-name extra))
490 'face 'font-lock-comment-face)))))
492 (defun vc-hg-after-dir-status (update-function)
493 (let ((status-char nil)
495 (translation '((?= . up-to-date)
502 (? . copy-rename-line)
503 (?? . unregistered)))
507 (last-line-copy nil))
508 (goto-char (point-min))
510 (setq translated (cdr (assoc (char-after) translation)))
512 (buffer-substring-no-properties (+ (point) 2)
513 (line-end-position)))
514 (cond ((not translated)
515 (setq last-line-copy nil))
516 ((eq translated 'up-to-date)
517 (setq last-line-copy nil))
518 ((eq translated 'copy-rename-line)
519 ;; For copied files the output looks like this:
520 ;; A COPIED_FILE_NAME
521 ;; ORIGINAL_FILE_NAME
522 (setf (nth 2 last-added)
523 (vc-hg-create-extra-fileinfo 'copied file))
524 (setq last-line-copy t))
525 ((and last-line-copy (eq translated 'removed))
526 ;; For renamed files the output looks like this:
528 ;; ORIGINAL_FILE_NAME
529 ;; R ORIGINAL_FILE_NAME
530 ;; We need to adjust the previous entry to not think it is a copy.
531 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
533 (push (list file translated
534 (vc-hg-create-extra-fileinfo
535 'renamed-to (nth 0 last-added))) result)
536 (setq last-line-copy nil))
538 (setq last-added (list file translated nil))
539 (push last-added result)
540 (setq last-line-copy nil)))
542 (funcall update-function result)))
544 (defun vc-hg-dir-status (dir update-function)
545 (vc-hg-command (current-buffer) 'async dir "status" "-C")
547 `(vc-hg-after-dir-status (quote ,update-function))))
549 ;; XXX this adds another top level menu, instead figure out how to
550 ;; replace the Log-View menu.
551 (easy-menu-define log-view-mode-menu vc-hg-outgoing-mode-map
552 "Hg-outgoing Display Menu"
554 ["Push selected" vc-hg-push]))
556 (easy-menu-define log-view-mode-menu vc-hg-incoming-mode-map
557 "Hg-incoming Display Menu"
559 ["Pull selected" vc-hg-pull]))
561 (defun vc-hg-outgoing ()
563 (let ((bname "*Hg outgoing*"))
564 (vc-hg-command bname 0 nil "outgoing" "-n")
565 (pop-to-buffer bname)
566 (vc-hg-outgoing-mode)))
568 (defun vc-hg-incoming ()
570 (let ((bname "*Hg incoming*"))
571 (vc-hg-command bname 0 nil "incoming" "-n")
572 (pop-to-buffer bname)
573 (vc-hg-incoming-mode)))
575 (declare-function log-view-get-marked "log-view" ())
577 ;; XXX maybe also add key bindings for these functions.
580 (let ((marked-list (log-view-get-marked)))
586 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
587 (error "No log entries selected for push"))))
591 (let ((marked-list (log-view-get-marked)))
597 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
598 (error "No log entries selected for pull"))))
600 ;;; Internal functions
602 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
603 "A wrapper around `vc-do-command' for use in vc-hg.el.
604 The difference to vc-do-command is that this function always invokes `hg',
605 and that it passes `vc-hg-global-switches' to it before FLAGS."
606 (apply 'vc-do-command buffer okstatus "hg" file-or-list
607 (if (stringp vc-hg-global-switches)
608 (cons vc-hg-global-switches flags)
609 (append vc-hg-global-switches
612 (defun vc-hg-root (file)
613 (vc-find-root file ".hg"))
617 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
618 ;;; vc-hg.el ends here