1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is a mercurial version control backend
36 ;; 1) Implement the rest of the vc interface. See the comment at the
37 ;; beginning of vc.el. The current status is:
39 ;; FUNCTION NAME STATUS
41 ;; * revision-granularity OK
42 ;; STATE-QUERYING FUNCTIONS
43 ;; * registered (file) OK
45 ;; - state-heuristic (file) NOT NEEDED
46 ;; - dir-status (dir update-function) OK
47 ;; - dir-status-files (dir files ds uf) OK
48 ;; - dir-extra-headers (dir) OK
49 ;; - dir-printer (fileinfo) OK
50 ;; * working-revision (file) OK
51 ;; - latest-on-branch-p (file) ??
52 ;; * checkout-model (files) OK
53 ;; - workfile-unchanged-p (file) OK
54 ;; - mode-line-string (file) NOT NEEDED
55 ;; STATE-CHANGING FUNCTIONS
56 ;; * register (files &optional rev comment) OK
57 ;; * create-repo () OK
58 ;; - init-revision () NOT NEEDED
59 ;; - responsible-p (file) OK
60 ;; - could-register (file) OK
61 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
62 ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
63 ;; * checkin (files rev comment) OK
64 ;; * find-revision (file rev buffer) OK
65 ;; * checkout (file &optional editable rev) OK
66 ;; * revert (file &optional contents-done) OK
67 ;; - rollback (files) ?? PROBABLY NOT NEEDED
68 ;; - merge (file rev1 rev2) NEEDED
69 ;; - merge-news (file) NEEDED
70 ;; - steal-lock (file &optional revision) NOT NEEDED
72 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
73 ;; - log-view-mode () OK
74 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
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-tag (dir name branchp) NEEDED
85 ;; - retrieve-tag (dir name update) NEEDED
87 ;; - make-version-backups-p (file) ??
88 ;; - repository-hostname (dirname) ??
89 ;; - previous-revision (file rev) OK
90 ;; - next-revision (file rev) OK
91 ;; - check-headers () ??
92 ;; - clear-headers () ??
93 ;; - delete-file (file) TEST IT
94 ;; - rename-file (old new) OK
95 ;; - find-file-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" :value
("") string
))
127 (defcustom vc-hg-diff-switches t
; Hg doesn't support common args like -u
128 "String or list of strings specifying switches for Hg diff under VC.
129 If nil, use the value of `vc-diff-switches'. If t, use no switches."
130 :type
'(choice (const :tag
"Unspecified" nil
)
131 (const :tag
"None" t
)
132 (string :tag
"Argument String")
133 (repeat :tag
"Argument List" :value
("") string
))
137 (defcustom vc-hg-program
"hg"
138 "Name of the Mercurial executable (excluding any arguments)."
142 ;;; Properties of the backend
144 (defvar vc-hg-history nil
)
146 (defun vc-hg-revision-granularity () 'repository
)
147 (defun vc-hg-checkout-model (files) 'implicit
)
149 ;;; State querying functions
151 ;;;###autoload (defun vc-hg-registered (file)
152 ;;;###autoload "Return non-nil if FILE is registered with hg."
153 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
154 ;;;###autoload (progn
155 ;;;###autoload (load "vc-hg")
156 ;;;###autoload (vc-hg-registered file))))
158 ;; Modeled after the similar function in vc-bzr.el
159 (defun vc-hg-registered (file)
160 "Return non-nil if FILE is registered with hg."
161 (when (vc-hg-root file
) ; short cut
162 (let ((state (vc-hg-state file
))) ; expensive
163 (and state
(not (memq state
'(ignored unregistered
)))))))
165 (defun vc-hg-state (file)
166 "Hg-specific version of `vc-state'."
169 (default-directory (file-name-directory file
))
171 (with-output-to-string
176 ;; Ignore all errors.
177 (let ((process-environment
178 ;; Avoid localization of messages so we
179 ;; can parse the output.
180 (append (list "TERM=dumb" "LANGUAGE=C")
181 process-environment
)))
183 vc-hg-program nil t nil
184 "--config" "alias.status=status"
185 "--config" "defaults.status="
186 "status" "-A" (file-relative-name file
)))
187 ;; Some problem happened. E.g. We can't find an `hg'
191 (when (null (string-match ".*: No such file or directory$" out
))
192 (let ((state (aref out
0)))
194 ((eq state ?
=) 'up-to-date
)
195 ((eq state ?A
) 'added
)
196 ((eq state ?M
) 'edited
)
197 ((eq state ?I
) 'ignored
)
198 ((eq state ?R
) 'removed
)
199 ((eq state ?
!) 'missing
)
200 ((eq state ??
) 'unregistered
)
201 ((eq state ?C
) 'up-to-date
) ;; Older mercurials use this
202 (t 'up-to-date
)))))))
204 (defun vc-hg-working-revision (file)
205 "Hg-specific version of `vc-working-revision'."
208 (default-directory (file-name-directory file
))
209 ;; Avoid localization of messages so we can parse the output.
210 (avoid-local-env (append (list "TERM=dumb" "LANGUAGE=C")
211 process-environment
))
213 (with-output-to-string
218 (let ((process-environment avoid-local-env
))
219 ;; Ignore all errors.
221 vc-hg-program nil t nil
222 "--config" "alias.parents=parents"
223 "--config" "defaults.parents="
224 "parents" "--template" "{rev}" (file-relative-name file
)))
225 ;; Some problem happened. E.g. We can't find an `hg'
230 ;; Check if the file is in the 'added state, the above hg
231 ;; command does not distinguish between 'added and 'unregistered.
234 (let ((process-environment avoid-local-env
))
236 vc-hg-program nil nil nil
237 ;; We use "log" here, if there's a faster command
238 ;; that returns true for an 'added file and false
239 ;; for an 'unregistered one, we could use that.
240 "log" "-l1" (file-relative-name file
)))
241 ;; Some problem happened. E.g. We can't find an `hg'
244 (when (eq 0 status
) "0"))))
246 ;;; History functions
248 (defcustom vc-hg-log-switches nil
249 "String or list of strings specifying switches for hg log under VC."
250 :type
'(choice (const :tag
"None" nil
)
251 (string :tag
"Argument String")
252 (repeat :tag
"Argument List" :value
("") string
))
255 (defun vc-hg-print-log (files buffer
&optional shortlog start-revision limit
)
256 "Get change log associated with FILES."
257 ;; `vc-do-command' creates the buffer, but we need it before running
259 (vc-setup-buffer buffer
)
260 ;; If the buffer exists from a previous invocation it might be
262 (let ((inhibit-read-only t
))
265 (apply 'vc-hg-command buffer
0 files
"log"
267 (when start-revision
(list (format "-r%s:" start-revision
)))
268 (when limit
(list "-l" (format "%s" limit
)))
269 (when shortlog
(list "--style" "compact"))
270 vc-hg-log-switches
)))))
272 (defvar log-view-message-re
)
273 (defvar log-view-file-re
)
274 (defvar log-view-font-lock-keywords
)
275 (defvar log-view-per-file-logs
)
277 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
278 (require 'add-log
) ;; we need the add-log faces
279 (set (make-local-variable 'log-view-file-re
) "\\`a\\`")
280 (set (make-local-variable 'log-view-per-file-logs
) nil
)
281 (set (make-local-variable 'log-view-message-re
)
282 (if (eq vc-log-view-type
'short
)
283 "^\\([0-9]+\\)\\(\\[.*\\]\\)? +\\([0-9a-z]\\{12\\}\\) +\\(\\(?:[0-9]+\\)-\\(?:[0-9]+\\)-\\(?:[0-9]+\\) \\(?:[0-9]+\\):\\(?:[0-9]+\\) \\(?:[-+0-9]+\\)\\) +\\(.*\\)$"
284 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
285 (set (make-local-variable 'log-view-font-lock-keywords
)
286 (if (eq vc-log-view-type
'short
)
287 (append `((,log-view-message-re
288 (1 'log-view-message-face
)
289 (2 'highlight nil lax
)
290 (3 'log-view-message-face
)
292 (5 'change-log-name
))))
294 log-view-font-lock-keywords
297 ;; user: FirstName LastName <foo@bar>
298 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
300 (2 'change-log-email
))
305 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
306 (1 'change-log-email
))
307 ("^date: \\(.+\\)" (1 'change-log-date
))
308 ("^tag: +\\([^ ]+\\)$" (1 'highlight
))
309 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
)))))))
311 (defun vc-hg-diff (files &optional oldvers newvers buffer
)
312 "Get a difference report using hg between two revisions of FILES."
313 (let* ((firstfile (car files
))
314 (working (and firstfile
(vc-working-revision firstfile
))))
315 (when (and (equal oldvers working
) (not newvers
))
317 (when (and (not oldvers
) newvers
)
318 (setq oldvers working
))
319 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil files
"diff"
321 (vc-switches 'hg
'diff
)
324 (list "-r" oldvers
"-r" newvers
)
325 (list "-r" oldvers
)))))))
327 (defun vc-hg-revision-table (files)
328 (let ((default-directory (file-name-directory (car files
))))
330 (vc-hg-command t nil files
"log" "--template" "{rev} ")
332 (buffer-substring-no-properties (point-min) (point-max))))))
334 ;; Modeled after the similar function in vc-cvs.el
335 (defun vc-hg-revision-completion-table (files)
336 (lexical-let ((files files
)
338 (setq table
(lazy-completion-table
339 table
(lambda () (vc-hg-revision-table files
))))
342 (defun vc-hg-annotate-command (file buffer
&optional revision
)
343 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
344 Optional arg REVISION is a revision to annotate from."
345 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" "--follow"
346 (when revision
(concat "-r" revision
))))
348 (declare-function vc-annotate-convert-time
"vc-annotate" (time))
350 ;; The format for one line output by "hg annotate -d -n" looks like this:
351 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
352 ;; i.e: VERSION_NUMBER DATE: CONTENTS
353 ;; If the user has set the "--follow" option, the output looks like:
354 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
355 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
356 (defconst vc-hg-annotate-re
357 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\(.+\\): \\)\\)")
359 (defun vc-hg-annotate-time ()
360 (when (looking-at vc-hg-annotate-re
)
361 (goto-char (match-end 0))
362 (vc-annotate-convert-time
363 (date-to-time (match-string-no-properties 2)))))
365 (defun vc-hg-annotate-extract-revision-at-line ()
368 (when (looking-at vc-hg-annotate-re
)
369 (if (match-beginning 3)
370 (match-string-no-properties 1)
371 (cons (match-string-no-properties 1)
372 (expand-file-name (match-string-no-properties 4)
373 (vc-hg-root default-directory
)))))))
375 (defun vc-hg-previous-revision (file rev
)
376 (let ((newrev (1- (string-to-number rev
))))
378 (number-to-string newrev
))))
380 (defun vc-hg-next-revision (file rev
)
381 (let ((newrev (1+ (string-to-number rev
)))
384 (vc-hg-command t
0 nil
"tip")
385 (goto-char (point-min))
386 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
387 (string-to-number (match-string-no-properties 1)))))
388 ;; We don't want to exceed the maximum possible revision number, ie
390 (when (<= newrev tip-revision
)
391 (number-to-string newrev
))))
393 ;; Modeled after the similar function in vc-bzr.el
394 (defun vc-hg-delete-file (file)
395 "Delete FILE and delete it in the hg repository."
399 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
401 ;; Modeled after the similar function in vc-bzr.el
402 (defun vc-hg-rename-file (old new
)
403 "Rename file from OLD to NEW using `hg mv'."
404 (vc-hg-command nil
0 new
"mv" old
))
406 (defun vc-hg-register (files &optional rev comment
)
407 "Register FILES under hg.
410 (vc-hg-command nil
0 files
"add"))
412 (defun vc-hg-create-repo ()
413 "Create a new Mercurial repository."
414 (vc-hg-command nil
0 nil
"init"))
416 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
418 ;; Modeled after the similar function in vc-bzr.el
419 (defun vc-hg-could-register (file)
420 "Return non-nil if FILE could be registered under hg."
421 (and (vc-hg-responsible-p file
) ; shortcut
424 (vc-hg-command t nil file
"add" "--dry-run"))
425 ;; The command succeeds with no output if file is
429 ;; FIXME: This would remove the file. Is that correct?
430 ;; (defun vc-hg-unregister (file)
431 ;; "Unregister FILE from hg."
432 ;; (vc-hg-command nil nil file "remove"))
434 (declare-function log-edit-extract-headers
"log-edit" (headers string
))
436 (defun vc-hg-checkin (files rev comment
)
437 "Hg-specific version of `vc-backend-checkin'.
439 (apply 'vc-hg-command nil
0 files
440 (nconc (list "commit" "-m")
441 (log-edit-extract-headers '(("Author" .
"--user")
445 (defun vc-hg-find-revision (file rev buffer
)
446 (let ((coding-system-for-read 'binary
)
447 (coding-system-for-write 'binary
))
449 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
450 (vc-hg-command buffer
0 file
"cat"))))
452 ;; Modeled after the similar function in vc-bzr.el
453 (defun vc-hg-checkout (file &optional editable rev
)
454 "Retrieve a revision of FILE.
456 REV is the revision to check out into WORKFILE."
457 (let ((coding-system-for-read 'binary
)
458 (coding-system-for-write 'binary
))
459 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
461 (vc-hg-command t
0 file
"cat" "-r" rev
)
462 (vc-hg-command t
0 file
"cat")))))
464 ;; Modeled after the similar function in vc-bzr.el
465 (defun vc-hg-workfile-unchanged-p (file)
466 (eq 'up-to-date
(vc-hg-state file
)))
468 ;; Modeled after the similar function in vc-bzr.el
469 (defun vc-hg-revert (file &optional contents-done
)
470 (unless contents-done
471 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
473 ;;; Hg specific functionality.
475 (defvar vc-hg-extra-menu-map
476 (let ((map (make-sparse-keymap)))
479 (defun vc-hg-extra-menu () vc-hg-extra-menu-map
)
481 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map
)
483 (defvar log-view-vc-backend
)
485 (defstruct (vc-hg-extra-fileinfo
487 (:constructor vc-hg-create-extra-fileinfo
(rename-state extra-name
))
488 (:conc-name vc-hg-extra-fileinfo-
>))
489 rename-state
;; rename or copy state
490 extra-name
) ;; original name for copies and rename targets, new name for
492 (declare-function vc-default-dir-printer
"vc-dir" (backend fileentry
))
494 (defun vc-hg-dir-printer (info)
495 "Pretty-printer for the vc-dir-fileinfo structure."
496 (let ((extra (vc-dir-fileinfo->extra info
)))
497 (vc-default-dir-printer 'Hg info
)
501 (case (vc-hg-extra-fileinfo->rename-state extra
)
502 ('copied
"copied from")
503 ('renamed-from
"renamed from")
504 ('renamed-to
"renamed to"))
505 (vc-hg-extra-fileinfo->extra-name extra
))
506 'face
'font-lock-comment-face
)))))
508 (defun vc-hg-after-dir-status (update-function)
509 (let ((status-char nil
)
511 (translation '((?
= . up-to-date
)
518 (? . copy-rename-line
)
519 (?? . unregistered
)))
523 (last-line-copy nil
))
524 (goto-char (point-min))
526 (setq translated
(cdr (assoc (char-after) translation
)))
528 (buffer-substring-no-properties (+ (point) 2)
529 (line-end-position)))
530 (cond ((not translated
)
531 (setq last-line-copy nil
))
532 ((eq translated
'up-to-date
)
533 (setq last-line-copy nil
))
534 ((eq translated
'copy-rename-line
)
535 ;; For copied files the output looks like this:
536 ;; A COPIED_FILE_NAME
537 ;; ORIGINAL_FILE_NAME
538 (setf (nth 2 last-added
)
539 (vc-hg-create-extra-fileinfo 'copied file
))
540 (setq last-line-copy t
))
541 ((and last-line-copy
(eq translated
'removed
))
542 ;; For renamed files the output looks like this:
544 ;; ORIGINAL_FILE_NAME
545 ;; R ORIGINAL_FILE_NAME
546 ;; We need to adjust the previous entry to not think it is a copy.
547 (setf (vc-hg-extra-fileinfo->rename-state
(nth 2 last-added
))
549 (push (list file translated
550 (vc-hg-create-extra-fileinfo
551 'renamed-to
(nth 0 last-added
))) result
)
552 (setq last-line-copy nil
))
554 (setq last-added
(list file translated nil
))
555 (push last-added result
)
556 (setq last-line-copy nil
)))
558 (funcall update-function result
)))
560 (defun vc-hg-dir-status (dir update-function
)
561 (vc-hg-command (current-buffer) 'async dir
"status" "-C")
563 `(vc-hg-after-dir-status (quote ,update-function
))))
565 (defun vc-hg-dir-status-files (dir files default-state update-function
)
566 (apply 'vc-hg-command
(current-buffer) 'async dir
"status" "-C" files
)
568 `(vc-hg-after-dir-status (quote ,update-function
))))
570 (defun vc-hg-dir-extra-header (name &rest commands
)
571 (concat (propertize name
'face
'font-lock-type-face
)
574 (apply 'vc-hg-command
(current-buffer) 0 nil commands
)
575 (buffer-substring-no-properties (point-min) (1- (point-max))))
576 'face
'font-lock-variable-name-face
)))
578 (defun vc-hg-dir-extra-headers (dir)
579 "Generate extra status headers for a Mercurial tree."
580 (let ((default-directory dir
))
582 (vc-hg-dir-extra-header "Root : " "root") "\n"
583 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
584 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
585 ;; these change after each commit
586 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
587 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
590 (defun vc-hg-log-incoming (buffer remote-location
)
591 (vc-hg-command buffer
1 nil
"incoming" "-n" (unless (string= remote-location
"")
594 (defun vc-hg-log-outgoing (buffer remote-location
)
595 (vc-hg-command buffer
1 nil
"outgoing" "-n" (unless (string= remote-location
"")
598 (declare-function log-view-get-marked
"log-view" ())
600 ;; XXX maybe also add key bindings for these functions.
603 (let ((marked-list (log-view-get-marked)))
605 (apply #'vc-hg-command
609 (mapcar (lambda (arg) (list "-r" arg
)) marked-list
)))
610 (error "No log entries selected for push"))))
612 (defun vc-hg-pull (prompt)
615 (if (and (called-interactively-p 'interactive
)
616 (setq marked-list
(log-view-get-marked)))
617 (apply #'vc-hg-command
621 (mapcar (lambda (arg) (list "-r" arg
))
623 (let* ((root (vc-hg-root default-directory
))
624 (buffer (format "*vc-hg : %s*" (expand-file-name root
)))
627 ;; Todo: maybe check if we're up-to-date before updating
628 ;; the working copy to the latest state.
630 ;; If necessary, prompt for the exact command.
632 (setq args
(split-string
633 (read-shell-command "Run Hg (like this): " "hg -u"
636 (setq hg-program
(car args
)
639 (apply 'vc-do-async-command buffer root hg-program
642 (defun vc-hg-merge-branch ()
643 "Merge incoming changes into the current Mercurial working directory."
644 (let* ((root (vc-hg-root default-directory
))
645 (buffer (format "*vc-hg : %s*" (expand-file-name root
))))
646 (apply 'vc-do-async-command buffer root
"hg" '("merge"))))
648 ;;; Internal functions
650 (defun vc-hg-command (buffer okstatus file-or-list
&rest flags
)
651 "A wrapper around `vc-do-command' for use in vc-hg.el.
652 The difference to vc-do-command is that this function always invokes `hg',
653 and that it passes `vc-hg-global-switches' to it before FLAGS."
654 (apply 'vc-do-command
(or buffer
"*vc*") okstatus vc-hg-program file-or-list
655 (if (stringp vc-hg-global-switches
)
656 (cons vc-hg-global-switches flags
)
657 (append vc-hg-global-switches
660 (defun vc-hg-root (file)
661 (vc-find-root file
".hg"))
665 ;;; vc-hg.el ends here