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 ;; 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 &optional buffer) 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
95 ;; - find-file-not-found-hook () PROBABLY NOT NEEDED
97 ;; 2) 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).
117 ;;; Customization options
119 (defcustom vc-hg-global-switches nil
120 "*Global switches to pass to any Hg command."
121 :type
'(choice (const :tag
"None" nil
)
122 (string :tag
"Argument String")
123 (repeat :tag
"Argument List"
130 ;;; Properties of the backend
132 (defun vc-hg-revision-granularity () 'repository
)
133 (defun vc-hg-checkout-model (files) 'implicit
)
135 ;;; State querying functions
137 ;;;###autoload (defun vc-hg-registered (file)
138 ;;;###autoload "Return non-nil if FILE is registered with hg."
139 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
140 ;;;###autoload (progn
141 ;;;###autoload (load "vc-hg")
142 ;;;###autoload (vc-hg-registered file))))
144 ;; Modeled after the similar function in vc-bzr.el
145 (defun vc-hg-registered (file)
146 "Return non-nil if FILE is registered with hg."
147 (when (vc-hg-root file
) ; short cut
148 (let ((state (vc-hg-state file
))) ; expensive
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-working-revision (file)
183 "Hg-specific version of `vc-working-revision'."
187 (with-output-to-string
192 ;; Ignore all errors.
194 "hg" nil t nil
"--cwd" (file-name-directory file
)
195 "log" "-l1" (file-name-nondirectory file
))
196 ;; Some problem happened. E.g. We can't find an `hg'
200 (if (string-match "changeset: *\\([0-9]*\\)" out
)
204 ;;; History functions
206 (defun vc-hg-print-log (files &optional buffer
)
207 "Get change log associated with FILES."
208 ;; `log-view-mode' needs to have the file names in order to function
209 ;; correctly. "hg log" does not print it, so we insert it here by
212 ;; `vc-do-command' creates the buffer, but we need it before running
214 (vc-setup-buffer buffer
)
215 ;; If the buffer exists from a previous invocation it might be
217 (let ((inhibit-read-only t
))
220 (vc-hg-command buffer
0 files
"log"))))
222 (defvar log-view-message-re
)
223 (defvar log-view-file-re
)
224 (defvar log-view-font-lock-keywords
)
225 (defvar log-view-per-file-logs
)
227 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
228 (require 'add-log
) ;; we need the add-log faces
229 (set (make-local-variable 'log-view-file-re
) "\\`a\\`")
230 (set (make-local-variable 'log-view-per-file-logs
) nil
)
231 (set (make-local-variable 'log-view-message-re
)
232 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
233 (set (make-local-variable 'log-view-font-lock-keywords
)
235 log-view-font-lock-keywords
238 ;; user: FirstName LastName <foo@bar>
239 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
241 (2 'change-log-email
))
246 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
247 (1 'change-log-email
))
248 ("^date: \\(.+\\)" (1 'change-log-date
))
249 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
))))))
251 (defun vc-hg-diff (files &optional oldvers newvers buffer
)
252 "Get a difference report using hg between two revisions of FILES."
253 (let* ((firstfile (car files
))
254 (working (and firstfile
(vc-working-revision firstfile
))))
255 (when (and (equal oldvers working
) (not newvers
))
257 (when (and (not oldvers
) newvers
)
258 (setq oldvers working
))
259 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil
260 (mapcar (lambda (file) (file-name-nondirectory file
)) files
)
261 "--cwd" (or (when firstfile
(file-name-directory firstfile
))
262 (expand-file-name default-directory
))
267 (list "-r" oldvers
"-r" newvers
)
268 (list "-r" oldvers
)))))))
270 (defun vc-hg-revision-table (files)
271 (let ((default-directory (file-name-directory (car files
))))
273 (vc-hg-command t nil files
"log" "--template" "{rev} ")
275 (buffer-substring-no-properties (point-min) (point-max))))))
277 ;; Modeled after the similar function in vc-cvs.el
278 (defun vc-hg-revision-completion-table (files)
279 (lexical-let ((files files
)
281 (setq table
(lazy-completion-table
282 table
(lambda () (vc-hg-revision-table files
))))
285 (defun vc-hg-annotate-command (file buffer
&optional revision
)
286 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
287 Optional arg REVISION is a revision to annotate from."
288 (vc-hg-command buffer
0 file
"annotate" "-d" "-n"
289 (when revision
(concat "-r" revision
)))
290 (with-current-buffer buffer
291 (goto-char (point-min))
292 (re-search-forward "^[ \t]*[0-9]")
293 (delete-region (point-min) (match-beginning 0))))
295 (declare-function vc-annotate-convert-time
"vc-annotate" (time))
297 ;; The format for one line output by "hg annotate -d -n" looks like this:
298 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
299 ;; i.e: VERSION_NUMBER DATE: CONTENTS
300 ;; If the user has set the "--follow" option, the output looks like:
301 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
302 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
303 (defconst vc-hg-annotate-re
304 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)[^:\n]*\\(:[^ \n][^:\n]*\\)*: ")
306 (defun vc-hg-annotate-time ()
307 (when (looking-at vc-hg-annotate-re
)
308 (goto-char (match-end 0))
309 (vc-annotate-convert-time
310 (date-to-time (match-string-no-properties 2)))))
312 (defun vc-hg-annotate-extract-revision-at-line ()
315 (when (looking-at vc-hg-annotate-re
) (match-string-no-properties 1))))
317 (defun vc-hg-previous-revision (file rev
)
318 (let ((newrev (1- (string-to-number rev
))))
320 (number-to-string newrev
))))
322 (defun vc-hg-next-revision (file rev
)
323 (let ((newrev (1+ (string-to-number rev
)))
326 (vc-hg-command t
0 nil
"tip")
327 (goto-char (point-min))
328 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
329 (string-to-number (match-string-no-properties 1)))))
330 ;; We don't want to exceed the maximum possible revision number, ie
332 (when (<= newrev tip-revision
)
333 (number-to-string newrev
))))
335 ;; Modeled after the similar function in vc-bzr.el
336 (defun vc-hg-delete-file (file)
337 "Delete FILE and delete it in the hg repository."
341 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
343 ;; Modeled after the similar function in vc-bzr.el
344 (defun vc-hg-rename-file (old new
)
345 "Rename file from OLD to NEW using `hg mv'."
346 (vc-hg-command nil
0 new
"mv" old
))
348 (defun vc-hg-register (files &optional rev comment
)
349 "Register FILES under hg.
352 (vc-hg-command nil
0 files
"add"))
354 (defun vc-hg-create-repo ()
355 "Create a new Mercurial repository."
356 (vc-hg-command nil
0 nil
"init"))
358 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
360 ;; Modeled after the similar function in vc-bzr.el
361 (defun vc-hg-could-register (file)
362 "Return non-nil if FILE could be registered under hg."
363 (and (vc-hg-responsible-p file
) ; shortcut
366 (vc-hg-command t nil file
"add" "--dry-run"))
367 ;; The command succeeds with no output if file is
371 ;; FIXME: This would remove the file. Is that correct?
372 ;; (defun vc-hg-unregister (file)
373 ;; "Unregister FILE from hg."
374 ;; (vc-hg-command nil nil file "remove"))
376 (defun vc-hg-checkin (files rev comment
)
377 "Hg-specific version of `vc-backend-checkin'.
379 (vc-hg-command nil
0 files
"commit" "-m" comment
))
381 (defun vc-hg-find-revision (file rev buffer
)
382 (let ((coding-system-for-read 'binary
)
383 (coding-system-for-write 'binary
))
385 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
386 (vc-hg-command buffer
0 file
"cat"))))
388 ;; Modeled after the similar function in vc-bzr.el
389 (defun vc-hg-checkout (file &optional editable rev
)
390 "Retrieve a revision of FILE.
392 REV is the revision to check out into WORKFILE."
393 (let ((coding-system-for-read 'binary
)
394 (coding-system-for-write 'binary
))
395 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
397 (vc-hg-command t
0 file
"cat" "-r" rev
)
398 (vc-hg-command t
0 file
"cat")))))
400 ;; Modeled after the similar function in vc-bzr.el
401 (defun vc-hg-workfile-unchanged-p (file)
402 (eq 'up-to-date
(vc-hg-state file
)))
404 ;; Modeled after the similar function in vc-bzr.el
405 (defun vc-hg-revert (file &optional contents-done
)
406 (unless contents-done
407 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
409 ;;; Hg specific functionality.
411 (defvar vc-hg-extra-menu-map
412 (let ((map (make-sparse-keymap)))
413 (define-key map
[incoming] '(menu-item "Show incoming" vc-hg-incoming))
414 (define-key map [outgoing] '(menu-item "Show outgoing" vc-hg-outgoing))
417 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
419 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
421 (define-derived-mode vc-hg-outgoing-mode vc-hg-log-view-mode "Hg-Outgoing")
423 (define-derived-mode vc-hg-incoming-mode vc-hg-log-view-mode "Hg-Incoming")
425 (defstruct (vc-hg-extra-fileinfo
427 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
428 (:conc-name vc-hg-extra-fileinfo->))
429 rename-state ;; rename or copy state
430 extra-name) ;; original name for copies and rename targets, new name for
432 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
434 (defun vc-hg-dir-printer (info)
435 "Pretty-printer for the vc-dir-fileinfo structure."
436 (let ((extra (vc-dir-fileinfo->extra info)))
437 (vc-default-dir-printer 'Hg info)
441 (case (vc-hg-extra-fileinfo->rename-state extra)
442 ('copied "copied from")
443 ('renamed-from "renamed from")
444 ('renamed-to "renamed to"))
445 (vc-hg-extra-fileinfo->extra-name extra))
446 'face 'font-lock-comment-face)))))
448 (defun vc-hg-after-dir-status (update-function)
449 (let ((status-char nil)
451 (translation '((?= . up-to-date)
458 (? . copy-rename-line)
459 (?? . unregistered)))
463 (last-line-copy nil))
464 (goto-char (point-min))
466 (setq translated (cdr (assoc (char-after) translation)))
468 (buffer-substring-no-properties (+ (point) 2)
469 (line-end-position)))
470 (cond ((not translated)
471 (setq last-line-copy nil))
472 ((eq translated 'up-to-date)
473 (setq last-line-copy nil))
474 ((eq translated 'copy-rename-line)
475 ;; For copied files the output looks like this:
476 ;; A COPIED_FILE_NAME
477 ;; ORIGINAL_FILE_NAME
478 (setf (nth 2 last-added)
479 (vc-hg-create-extra-fileinfo 'copied file))
480 (setq last-line-copy t))
481 ((and last-line-copy (eq translated 'removed))
482 ;; For renamed files the output looks like this:
484 ;; ORIGINAL_FILE_NAME
485 ;; R ORIGINAL_FILE_NAME
486 ;; We need to adjust the previous entry to not think it is a copy.
487 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
489 (push (list file translated
490 (vc-hg-create-extra-fileinfo
491 'renamed-to (nth 0 last-added))) result)
492 (setq last-line-copy nil))
494 (setq last-added (list file translated nil))
495 (push last-added result)
496 (setq last-line-copy nil)))
498 (funcall update-function result)))
500 (defun vc-hg-dir-status (dir update-function)
501 (vc-hg-command (current-buffer) 'async dir "status" "-C")
503 `(vc-hg-after-dir-status (quote ,update-function))))
505 (defun vc-hg-dir-status-files (dir files default-state update-function)
506 (apply 'vc-hg-command (current-buffer) 'async dir "status" "-C" files)
508 `(vc-hg-after-dir-status (quote ,update-function))))
510 (defun vc-hg-dir-extra-header (name &rest commands)
511 (concat (propertize name 'face 'font-lock-type-face)
514 (apply 'vc-hg-command (current-buffer) 0 nil commands)
515 (buffer-substring-no-properties (point-min) (1- (point-max))))
516 'face 'font-lock-variable-name-face)))
518 (defun vc-hg-dir-extra-headers (dir)
519 "Generate extra status headers for a Mercurial tree."
520 (let ((default-directory dir))
522 (vc-hg-dir-extra-header "Root : " "root") "\n"
523 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
524 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
525 ;; these change after each commit
526 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
527 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
530 ;; FIXME: this adds another top level menu, instead figure out how to
531 ;; replace the Log-View menu.
532 (easy-menu-define log-view-mode-menu vc-hg-outgoing-mode-map
533 "Hg-outgoing Display Menu"
535 ["Push selected" vc-hg-push]))
537 (easy-menu-define log-view-mode-menu vc-hg-incoming-mode-map
538 "Hg-incoming Display Menu"
540 ["Pull selected" vc-hg-pull]))
542 (defun vc-hg-outgoing ()
544 (let ((bname "*Hg outgoing*"))
545 (vc-hg-command bname 0 nil "outgoing" "-n")
546 (pop-to-buffer bname)
547 (vc-hg-outgoing-mode)))
549 (defun vc-hg-incoming ()
551 (let ((bname "*Hg incoming*"))
552 (vc-hg-command bname 0 nil "incoming" "-n")
553 (pop-to-buffer bname)
554 (vc-hg-incoming-mode)))
556 (declare-function log-view-get-marked "log-view" ())
558 ;; XXX maybe also add key bindings for these functions.
561 (let ((marked-list (log-view-get-marked)))
567 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
568 (error "No log entries selected for push"))))
572 (let ((marked-list (log-view-get-marked)))
578 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
579 (error "No log entries selected for pull"))))
581 ;;; Internal functions
583 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
584 "A wrapper around `vc-do-command' for use in vc-hg.el.
585 The difference to vc-do-command is that this function always invokes `hg',
586 and that it passes `vc-hg-global-switches' to it before FLAGS."
587 (apply 'vc-do-command (or buffer "*vc*") okstatus "hg" file-or-list
588 (if (stringp vc-hg-global-switches)
589 (cons vc-hg-global-switches flags)
590 (append vc-hg-global-switches
593 (defun vc-hg-root (file)
594 (vc-find-root file ".hg"))
598 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
599 ;;; vc-hg.el ends here