1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006, 2007 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This is a mercurial version control backend
38 ;; Implement the rest of the vc interface:
42 ;; Implement Stefan Monnier's advice:
43 ;; vc-hg-registered and vc-hg-state
44 ;; Both of those functions should be super extra careful to fail gracefully in
45 ;; unexpected circumstances. The most important such case is when the `hg'
46 ;; executable is not available. The reason this is important is that any error
47 ;; there will prevent the user from even looking at the file :-(
48 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
49 ;; mercurial's control and extracting the current revision should be done
50 ;; without even using `hg' (this way even if you don't have `hg' installed,
51 ;; Emacs is able to tell you this file is under mercurial's control).
61 ;; XXX This should be moved to vc-hooks when we can be sure that vc-state
62 ;; and friends are always harmless.
63 (add-to-list 'vc-handled-backends
'HG
)
65 ;;; Customization options
67 (defcustom vc-hg-global-switches nil
68 "*Global switches to pass to any Hg command."
69 :type
'(choice (const :tag
"None" nil
)
70 (string :tag
"Argument String")
71 (repeat :tag
"Argument List"
77 ;;; State querying functions
79 ;;;###autoload (defun vc-hg-registered (file)
80 ;;;###autoload "Return non-nil if FILE is registered with hg."
81 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
83 ;;;###autoload (load "vc-hg")
84 ;;;###autoload (vc-hg-registered file))))
86 ;; Modelled after the similar function in vc-bzr.el
87 (defun vc-hg-registered (file)
88 "Return non-nil if FILE is registered with hg."
89 (if (vc-find-root file
".hg") ; short cut
90 (vc-hg-state file
))) ; expensive
92 (defun vc-hg-state (file)
93 "Hg-specific version of `vc-state'."
94 (let ((out (vc-hg-internal-status file
)))
95 (if (eq 0 (length out
)) 'up-to-date
96 (let ((state (aref out
0)))
98 ((eq state ?M
) 'edited
)
99 ((eq state ?A
) 'edited
)
100 ((eq state ?P
) 'needs-patch
)
104 (defun vc-hg-workfile-version (file)
105 "Hg-specific version of `vc-workfile-version'."
106 (let ((out (vc-hg-internal-log file
)))
107 (if (string-match "changeset: *\\([0-9]*\\)" out
)
111 ;;; History functions
113 (defun vc-hg-print-log(file &optional buffer
)
114 "Get change log associated with FILE."
115 ;; `log-view-mode' needs to have the file name in order to function
116 ;; correctly. "hg log" does not print it, so we insert it here by
119 ;; `vc-do-command' creates the buffer, but we need it before running
121 (vc-setup-buffer buffer
)
122 ;; If the buffer exists from a previous invocation it might be
124 (let ((inhibit-read-only t
))
127 (insert "File: " (file-name-nondirectory file
) "\n")))
130 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
133 (defvar log-view-message-re
)
134 (defvar log-view-file-re
)
135 (defvar log-view-font-lock-keywords
)
137 (define-derived-mode vc-hg-log-view-mode log-view-mode
"HG-Log-View"
138 (require 'add-log
) ;; we need the faces add-log
139 ;; Don't have file markers, so use impossible regexp.
140 (set (make-local-variable 'log-view-file-re
) "^File:[ \t]+\\(.+\\)")
141 (set (make-local-variable 'log-view-message-re
)
142 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
143 (set (make-local-variable 'log-view-font-lock-keywords
)
145 log-view-font-lock-keywords
148 '(("^user:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
149 (1 'change-log-email
))
151 ;; user: FirstName LastName <foo@bar>
152 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
154 (2 'change-log-email
))
155 ("^date: \\(.+\\)" (1 'change-log-date
))
156 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
))))))
158 (defun vc-hg-diff (file &optional oldvers newvers buffer
)
159 "Get a difference report using hg between two versions of FILE."
160 (let ((working (vc-workfile-version file
)))
161 (if (and (equal oldvers working
) (not newvers
))
163 (if (and (not oldvers
) newvers
)
164 (setq oldvers working
))
165 (apply 'call-process
"hg" nil
(or buffer
"*vc-diff*") nil
166 "--cwd" (file-name-directory file
) "diff"
170 (list "-r" oldvers
"-r" newvers
)
173 (list (file-name-nondirectory file
))))))
175 (defun vc-hg-annotate-command (file buffer
&optional version
)
176 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
177 Optional arg VERSION is a version to annotate from."
178 (vc-hg-command buffer
0 file
"annotate" "-d" "-n" (if version
(concat "-r" version
)))
179 (with-current-buffer buffer
180 (goto-char (point-min))
181 (re-search-forward "^[0-9]")
182 (delete-region (point-min) (1- (point)))))
185 ;; The format for one line output by "hg annotate -d -n" looks like this:
186 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
187 ;; i.e: VERSION_NUMBER DATE: CONTENTS
188 (defconst vc-hg-annotate-re
"^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\): ")
190 (defun vc-hg-annotate-time ()
191 (when (looking-at vc-hg-annotate-re
)
192 (goto-char (match-end 0))
193 (vc-annotate-convert-time
194 (date-to-time (match-string-no-properties 2)))))
196 (defun vc-hg-annotate-extract-revision-at-line ()
199 (if (looking-at vc-hg-annotate-re
) (match-string-no-properties 1))))
201 (defun vc-hg-previous-version (file rev
)
202 (let ((newrev (1- (string-to-number rev
))))
204 (number-to-string newrev
))))
206 (defun vc-hg-register (file &optional rev comment
)
207 "Register FILE under hg.
210 (vc-hg-command nil nil file
"add"))
212 (defun vc-hg-checkin (file rev comment
)
213 "HG-specific version of `vc-backend-checkin'.
215 (vc-hg-command nil nil file
"commit" "-m" comment
))
217 ;; Modelled after the similar function in vc-bzr.el
218 (defun vc-hg-checkout (file &optional editable rev workfile
)
219 "Retrieve a revision of FILE into a WORKFILE.
221 REV is the revision to check out into WORKFILE."
223 (setq workfile
(vc-version-backup-file-name file rev
)))
224 (let ((coding-system-for-read 'binary
)
225 (coding-system-for-write 'binary
))
226 (with-temp-file workfile
228 (vc-hg-command t nil file
"cat" "-r" rev
)
229 (vc-hg-command t nil file
"cat")))))
231 (defun vc-hg-checkout-model (file)
234 ;;; Internal functions
236 (defun vc-hg-command (buffer okstatus file
&rest flags
)
237 "A wrapper around `vc-do-command' for use in vc-hg.el.
238 The difference to vc-do-command is that this function always invokes `hg',
239 and that it passes `vc-hg-global-switches' to it before FLAGS."
240 (apply 'vc-do-command buffer okstatus
"hg" file
241 (if (stringp vc-hg-global-switches
)
242 (cons vc-hg-global-switches flags
)
243 (append vc-hg-global-switches
246 (defun vc-hg-internal-log (file &optional buffer
)
247 "Return log of FILE."
248 (with-output-to-string
252 "hg" nil t nil
"--cwd" (file-name-directory file
)
253 "log" "-l1" (file-name-nondirectory file
)))))
255 (defun vc-hg-internal-status(file)
256 "Return status of FILE."
257 (with-output-to-string
261 "hg" nil t nil
"--cwd" (file-name-directory file
)
262 "status" (file-name-nondirectory file
)))))
266 ;;; vc-hg.el ends here