1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 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
))
138 ;;; Properties of the backend
140 (defun vc-hg-revision-granularity () 'repository
)
141 (defun vc-hg-checkout-model (files) 'implicit
)
143 ;;; State querying functions
145 ;;;###autoload (defun vc-hg-registered (file)
146 ;;;###autoload "Return non-nil if FILE is registered with hg."
147 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
148 ;;;###autoload (progn
149 ;;;###autoload (load "vc-hg")
150 ;;;###autoload (vc-hg-registered file))))
152 ;; Modeled after the similar function in vc-bzr.el
153 (defun vc-hg-registered (file)
154 "Return non-nil if FILE is registered with hg."
155 (when (vc-hg-root file
) ; short cut
156 (let ((state (vc-hg-state file
))) ; expensive
157 (and state
(not (memq state
'(ignored unregistered
)))))))
159 (defun vc-hg-state (file)
160 "Hg-specific version of `vc-state'."
163 (default-directory (file-name-directory file
))
165 (with-output-to-string
170 ;; Ignore all errors.
171 (let ((process-environment
172 ;; Avoid localization of messages so we
173 ;; can parse the output.
174 (append (list "TERM=dumb" "LANGUAGE=C")
175 process-environment
)))
178 "--config" "alias.status=status"
179 "--config" "defaults.status="
180 "status" "-A" (file-relative-name file
)))
181 ;; Some problem happened. E.g. We can't find an `hg'
185 (when (null (string-match ".*: No such file or directory$" out
))
186 (let ((state (aref out
0)))
188 ((eq state ?
=) 'up-to-date
)
189 ((eq state ?A
) 'added
)
190 ((eq state ?M
) 'edited
)
191 ((eq state ?I
) 'ignored
)
192 ((eq state ?R
) 'removed
)
193 ((eq state ?
!) 'missing
)
194 ((eq state ??
) 'unregistered
)
195 ((eq state ?C
) 'up-to-date
) ;; Older mercurials use this
196 (t 'up-to-date
)))))))
198 (defun vc-hg-working-revision (file)
199 "Hg-specific version of `vc-working-revision'."
202 (default-directory (file-name-directory file
))
203 ;; Avoid localization of messages so we can parse the output.
204 (avoid-local-env (append (list "TERM=dumb" "LANGUAGE=C")
205 process-environment
))
207 (with-output-to-string
212 (let ((process-environment avoid-local-env
))
213 ;; Ignore all errors.
216 "--config" "alias.parents=parents"
217 "--config" "defaults.parents="
218 "parents" "--template" "{rev}" (file-relative-name file
)))
219 ;; Some problem happened. E.g. We can't find an `hg'
224 ;; Check if the file is in the 'added state, the above hg
225 ;; command does not distinguish between 'added and 'unregistered.
228 (let ((process-environment avoid-local-env
))
231 ;; We use "log" here, if there's a faster command
232 ;; that returns true for an 'added file and false
233 ;; for an 'unregistered one, we could use that.
234 "log" "-l1" (file-relative-name file
)))
235 ;; Some problem happened. E.g. We can't find an `hg'
238 (when (eq 0 status
) "0"))))
240 ;;; History functions
242 (defcustom vc-hg-log-switches nil
243 "String or list of strings specifying switches for hg log under VC."
244 :type
'(choice (const :tag
"None" nil
)
245 (string :tag
"Argument String")
246 (repeat :tag
"Argument List" :value
("") string
))
249 (defun vc-hg-print-log (files buffer
&optional shortlog start-revision limit
)
250 "Get change log associated with FILES."
251 ;; `vc-do-command' creates the buffer, but we need it before running
253 (vc-setup-buffer buffer
)
254 ;; If the buffer exists from a previous invocation it might be
256 (let ((inhibit-read-only t
))
259 (apply 'vc-hg-command buffer
0 files
"log"
261 (when start-revision
(list (format "-r%s:" start-revision
)))
262 (when limit
(list "-l" (format "%s" limit
)))
263 (when shortlog
(list "--style" "compact"))
264 vc-hg-log-switches
)))))
266 (defvar log-view-message-re
)
267 (defvar log-view-file-re
)
268 (defvar log-view-font-lock-keywords
)
269 (defvar log-view-per-file-logs
)
271 (define-derived-mode vc-hg-log-view-mode log-view-mode
"Hg-Log-View"
272 (require 'add-log
) ;; we need the add-log faces
273 (set (make-local-variable 'log-view-file-re
) "\\`a\\`")
274 (set (make-local-variable 'log-view-per-file-logs
) nil
)
275 (set (make-local-variable 'log-view-message-re
)
276 (if (eq vc-log-view-type
'short
)
277 "^\\([0-9]+\\)\\(\\[.*\\]\\)? +\\([0-9a-z]\\{12\\}\\) +\\(\\(?:[0-9]+\\)-\\(?:[0-9]+\\)-\\(?:[0-9]+\\) \\(?:[0-9]+\\):\\(?:[0-9]+\\) \\(?:[-+0-9]+\\)\\) +\\(.*\\)$"
278 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
279 (set (make-local-variable 'log-view-font-lock-keywords
)
280 (if (eq vc-log-view-type
'short
)
281 (append `((,log-view-message-re
282 (1 'log-view-message-face
)
283 (2 'highlight nil lax
)
284 (3 'log-view-message-face
)
286 (5 'change-log-name
))))
288 log-view-font-lock-keywords
291 ;; user: FirstName LastName <foo@bar>
292 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
294 (2 'change-log-email
))
299 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
300 (1 'change-log-email
))
301 ("^date: \\(.+\\)" (1 'change-log-date
))
302 ("^tag: +\\([^ ]+\\)$" (1 'highlight
))
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* ((firstfile (car files
))
308 (working (and firstfile
(vc-working-revision firstfile
))))
309 (when (and (equal oldvers working
) (not newvers
))
311 (when (and (not oldvers
) newvers
)
312 (setq oldvers working
))
313 (apply #'vc-hg-command
(or buffer
"*vc-diff*") nil files
"diff"
315 (vc-switches 'hg
'diff
)
318 (list "-r" oldvers
"-r" newvers
)
319 (list "-r" oldvers
)))))))
321 (defun vc-hg-revision-table (files)
322 (let ((default-directory (file-name-directory (car files
))))
324 (vc-hg-command t nil files
"log" "--template" "{rev} ")
326 (buffer-substring-no-properties (point-min) (point-max))))))
328 ;; Modeled after the similar function in vc-cvs.el
329 (defun vc-hg-revision-completion-table (files)
330 (lexical-let ((files files
)
332 (setq table
(lazy-completion-table
333 table
(lambda () (vc-hg-revision-table files
))))
336 (defun vc-hg-annotate-command (file buffer
&optional revision
)
337 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
338 Optional arg REVISION is a revision to annotate from."
339 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" "--follow"
340 (when revision
(concat "-r" revision
))))
342 (declare-function vc-annotate-convert-time
"vc-annotate" (time))
344 ;; The format for one line output by "hg annotate -d -n" looks like this:
345 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
346 ;; i.e: VERSION_NUMBER DATE: CONTENTS
347 ;; If the user has set the "--follow" option, the output looks like:
348 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
349 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
350 (defconst vc-hg-annotate-re
351 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\(.+\\): \\)\\)")
353 (defun vc-hg-annotate-time ()
354 (when (looking-at vc-hg-annotate-re
)
355 (goto-char (match-end 0))
356 (vc-annotate-convert-time
357 (date-to-time (match-string-no-properties 2)))))
359 (defun vc-hg-annotate-extract-revision-at-line ()
362 (when (looking-at vc-hg-annotate-re
)
363 (if (match-beginning 3)
364 (match-string-no-properties 1)
365 (cons (match-string-no-properties 1)
366 (expand-file-name (match-string-no-properties 4)
367 (vc-hg-root default-directory
)))))))
369 (defun vc-hg-previous-revision (file rev
)
370 (let ((newrev (1- (string-to-number rev
))))
372 (number-to-string newrev
))))
374 (defun vc-hg-next-revision (file rev
)
375 (let ((newrev (1+ (string-to-number rev
)))
378 (vc-hg-command t
0 nil
"tip")
379 (goto-char (point-min))
380 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
381 (string-to-number (match-string-no-properties 1)))))
382 ;; We don't want to exceed the maximum possible revision number, ie
384 (when (<= newrev tip-revision
)
385 (number-to-string newrev
))))
387 ;; Modeled after the similar function in vc-bzr.el
388 (defun vc-hg-delete-file (file)
389 "Delete FILE and delete it in the hg repository."
393 (vc-hg-command nil
0 file
"remove" "--after" "--force"))
395 ;; Modeled after the similar function in vc-bzr.el
396 (defun vc-hg-rename-file (old new
)
397 "Rename file from OLD to NEW using `hg mv'."
398 (vc-hg-command nil
0 new
"mv" old
))
400 (defun vc-hg-register (files &optional rev comment
)
401 "Register FILES under hg.
404 (vc-hg-command nil
0 files
"add"))
406 (defun vc-hg-create-repo ()
407 "Create a new Mercurial repository."
408 (vc-hg-command nil
0 nil
"init"))
410 (defalias 'vc-hg-responsible-p
'vc-hg-root
)
412 ;; Modeled after the similar function in vc-bzr.el
413 (defun vc-hg-could-register (file)
414 "Return non-nil if FILE could be registered under hg."
415 (and (vc-hg-responsible-p file
) ; shortcut
418 (vc-hg-command t nil file
"add" "--dry-run"))
419 ;; The command succeeds with no output if file is
423 ;; FIXME: This would remove the file. Is that correct?
424 ;; (defun vc-hg-unregister (file)
425 ;; "Unregister FILE from hg."
426 ;; (vc-hg-command nil nil file "remove"))
428 (declare-function log-edit-extract-headers
"log-edit" (headers string
))
430 (defun vc-hg-checkin (files rev comment
)
431 "Hg-specific version of `vc-backend-checkin'.
433 (apply 'vc-hg-command nil
0 files
434 (nconc (list "commit" "-m")
435 (log-edit-extract-headers '(("Author" .
"--user")
439 (defun vc-hg-find-revision (file rev buffer
)
440 (let ((coding-system-for-read 'binary
)
441 (coding-system-for-write 'binary
))
443 (vc-hg-command buffer
0 file
"cat" "-r" rev
)
444 (vc-hg-command buffer
0 file
"cat"))))
446 ;; Modeled after the similar function in vc-bzr.el
447 (defun vc-hg-checkout (file &optional editable rev
)
448 "Retrieve a revision of FILE.
450 REV is the revision to check out into WORKFILE."
451 (let ((coding-system-for-read 'binary
)
452 (coding-system-for-write 'binary
))
453 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
455 (vc-hg-command t
0 file
"cat" "-r" rev
)
456 (vc-hg-command t
0 file
"cat")))))
458 ;; Modeled after the similar function in vc-bzr.el
459 (defun vc-hg-workfile-unchanged-p (file)
460 (eq 'up-to-date
(vc-hg-state file
)))
462 ;; Modeled after the similar function in vc-bzr.el
463 (defun vc-hg-revert (file &optional contents-done
)
464 (unless contents-done
465 (with-temp-buffer (vc-hg-command t
0 file
"revert"))))
467 ;;; Hg specific functionality.
469 (defvar vc-hg-extra-menu-map
470 (let ((map (make-sparse-keymap)))
473 (defun vc-hg-extra-menu () vc-hg-extra-menu-map
)
475 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map
)
477 (defvar log-view-vc-backend
)
479 (defstruct (vc-hg-extra-fileinfo
481 (:constructor vc-hg-create-extra-fileinfo
(rename-state extra-name
))
482 (:conc-name vc-hg-extra-fileinfo-
>))
483 rename-state
;; rename or copy state
484 extra-name
) ;; original name for copies and rename targets, new name for
486 (declare-function vc-default-dir-printer
"vc-dir" (backend fileentry
))
488 (defun vc-hg-dir-printer (info)
489 "Pretty-printer for the vc-dir-fileinfo structure."
490 (let ((extra (vc-dir-fileinfo->extra info
)))
491 (vc-default-dir-printer 'Hg info
)
495 (case (vc-hg-extra-fileinfo->rename-state extra
)
496 ('copied
"copied from")
497 ('renamed-from
"renamed from")
498 ('renamed-to
"renamed to"))
499 (vc-hg-extra-fileinfo->extra-name extra
))
500 'face
'font-lock-comment-face
)))))
502 (defun vc-hg-after-dir-status (update-function)
503 (let ((status-char nil
)
505 (translation '((?
= . up-to-date
)
512 (? . copy-rename-line
)
513 (?? . unregistered
)))
517 (last-line-copy nil
))
518 (goto-char (point-min))
520 (setq translated
(cdr (assoc (char-after) translation
)))
522 (buffer-substring-no-properties (+ (point) 2)
523 (line-end-position)))
524 (cond ((not translated
)
525 (setq last-line-copy nil
))
526 ((eq translated
'up-to-date
)
527 (setq last-line-copy nil
))
528 ((eq translated
'copy-rename-line
)
529 ;; For copied files the output looks like this:
530 ;; A COPIED_FILE_NAME
531 ;; ORIGINAL_FILE_NAME
532 (setf (nth 2 last-added
)
533 (vc-hg-create-extra-fileinfo 'copied file
))
534 (setq last-line-copy t
))
535 ((and last-line-copy
(eq translated
'removed
))
536 ;; For renamed files the output looks like this:
538 ;; ORIGINAL_FILE_NAME
539 ;; R ORIGINAL_FILE_NAME
540 ;; We need to adjust the previous entry to not think it is a copy.
541 (setf (vc-hg-extra-fileinfo->rename-state
(nth 2 last-added
))
543 (push (list file translated
544 (vc-hg-create-extra-fileinfo
545 'renamed-to
(nth 0 last-added
))) result
)
546 (setq last-line-copy nil
))
548 (setq last-added
(list file translated nil
))
549 (push last-added result
)
550 (setq last-line-copy nil
)))
552 (funcall update-function result
)))
554 (defun vc-hg-dir-status (dir update-function
)
555 (vc-hg-command (current-buffer) 'async dir
"status" "-C")
557 `(vc-hg-after-dir-status (quote ,update-function
))))
559 (defun vc-hg-dir-status-files (dir files default-state update-function
)
560 (apply 'vc-hg-command
(current-buffer) 'async dir
"status" "-C" files
)
562 `(vc-hg-after-dir-status (quote ,update-function
))))
564 (defun vc-hg-dir-extra-header (name &rest commands
)
565 (concat (propertize name
'face
'font-lock-type-face
)
568 (apply 'vc-hg-command
(current-buffer) 0 nil commands
)
569 (buffer-substring-no-properties (point-min) (1- (point-max))))
570 'face
'font-lock-variable-name-face
)))
572 (defun vc-hg-dir-extra-headers (dir)
573 "Generate extra status headers for a Mercurial tree."
574 (let ((default-directory dir
))
576 (vc-hg-dir-extra-header "Root : " "root") "\n"
577 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
578 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
579 ;; these change after each commit
580 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
581 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
584 (defun vc-hg-log-incoming (buffer remote-location
)
585 (vc-hg-command buffer
1 nil
"incoming" "-n" (unless (string= remote-location
"")
588 (defun vc-hg-log-outgoing (buffer remote-location
)
589 (vc-hg-command buffer
1 nil
"outgoing" "-n" (unless (string= remote-location
"")
592 (declare-function log-view-get-marked
"log-view" ())
594 ;; XXX maybe also add key bindings for these functions.
597 (let ((marked-list (log-view-get-marked)))
599 (apply #'vc-hg-command
603 (mapcar (lambda (arg) (list "-r" arg
)) marked-list
)))
604 (error "No log entries selected for push"))))
608 (let ((marked-list (log-view-get-marked)))
610 (apply #'vc-hg-command
614 (mapcar (lambda (arg) (list "-r" arg
)) marked-list
)))
615 (error "No log entries selected for pull"))))
617 ;;; Internal functions
619 (defun vc-hg-command (buffer okstatus file-or-list
&rest flags
)
620 "A wrapper around `vc-do-command' for use in vc-hg.el.
621 The difference to vc-do-command is that this function always invokes `hg',
622 and that it passes `vc-hg-global-switches' to it before FLAGS."
623 (apply 'vc-do-command
(or buffer
"*vc*") okstatus
"hg" file-or-list
624 (if (stringp vc-hg-global-switches
)
625 (cons vc-hg-global-switches flags
)
626 (append vc-hg-global-switches
629 (defun vc-hg-root (file)
630 (vc-find-root file
".hg"))
634 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
635 ;;; vc-hg.el ends here