Remove obsolete files
[emacs.git] / lisp / vc-hg.el
blob663609d39b34212dc6f3b1fec2733352503c3a1a
1 ;;; vc-hg.el --- VC backend for the mercurial version control system
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Ivan Kanis
6 ;; Keywords: tools
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/>.
23 ;;; Commentary:
25 ;; This is a mercurial version control backend
27 ;;; Thanks:
29 ;;; Bugs:
31 ;;; Installation:
33 ;;; Todo:
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
39 ;; BACKEND PROPERTIES
40 ;; * revision-granularity OK
41 ;; STATE-QUERYING FUNCTIONS
42 ;; * registered (file) OK
43 ;; * state (file) OK
44 ;; - state-heuristic (file) NOT NEEDED
45 ;; * working-revision (file) OK
46 ;; - latest-on-branch-p (file) ??
47 ;; * checkout-model (files) OK
48 ;; - workfile-unchanged-p (file) OK
49 ;; - mode-line-string (file) NOT NEEDED
50 ;; - prettify-state-info (file) OK
51 ;; STATE-CHANGING FUNCTIONS
52 ;; * register (files &optional rev comment) OK
53 ;; * create-repo () OK
54 ;; - init-revision () NOT NEEDED
55 ;; - responsible-p (file) OK
56 ;; - could-register (file) OK
57 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
58 ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
59 ;; * checkin (files rev comment) OK
60 ;; * find-revision (file rev buffer) OK
61 ;; * checkout (file &optional editable rev) OK
62 ;; * revert (file &optional contents-done) OK
63 ;; - rollback (files) ?? PROBABLY NOT NEEDED
64 ;; - merge (file rev1 rev2) NEEDED
65 ;; - merge-news (file) NEEDED
66 ;; - steal-lock (file &optional revision) NOT NEEDED
67 ;; HISTORY FUNCTIONS
68 ;; * print-log (files &optional buffer) OK
69 ;; - log-view-mode () OK
70 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
71 ;; - comment-history (file) NOT NEEDED
72 ;; - update-changelog (files) NOT NEEDED
73 ;; * diff (files &optional rev1 rev2 buffer) OK
74 ;; - revision-completion-table (files) OK?
75 ;; - annotate-command (file buf &optional rev) OK
76 ;; - annotate-time () OK
77 ;; - annotate-current-time () NOT NEEDED
78 ;; - annotate-extract-revision-at-line () OK
79 ;; TAG SYSTEM
80 ;; - create-tag (dir name branchp) NEEDED
81 ;; - retrieve-tag (dir name update) NEEDED
82 ;; MISCELLANEOUS
83 ;; - make-version-backups-p (file) ??
84 ;; - repository-hostname (dirname) ??
85 ;; - previous-revision (file rev) OK
86 ;; - next-revision (file rev) OK
87 ;; - check-headers () ??
88 ;; - clear-headers () ??
89 ;; - delete-file (file) TEST IT
90 ;; - rename-file (old new) OK
91 ;; - find-file-hook () PROBABLY NOT NEEDED
92 ;; - find-file-not-found-hook () PROBABLY NOT NEEDED
94 ;; 2) Implement Stefan Monnier's advice:
95 ;; vc-hg-registered and vc-hg-state
96 ;; Both of those functions should be super extra careful to fail gracefully in
97 ;; unexpected circumstances. The reason this is important is that any error
98 ;; there will prevent the user from even looking at the file :-(
99 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
100 ;; mercurial's control and extracting the current revision should be done
101 ;; without even using `hg' (this way even if you don't have `hg' installed,
102 ;; Emacs is able to tell you this file is under mercurial's control).
104 ;;; History:
107 ;;; Code:
109 (eval-when-compile
110 (require 'cl)
111 (require 'vc))
113 ;;; Customization options
115 (defcustom vc-hg-global-switches nil
116 "*Global switches to pass to any Hg command."
117 :type '(choice (const :tag "None" nil)
118 (string :tag "Argument String")
119 (repeat :tag "Argument List"
120 :value ("")
121 string))
122 :version "22.2"
123 :group 'vc)
126 ;;; Properties of the backend
128 (defun vc-hg-revision-granularity () 'repository)
129 (defun vc-hg-checkout-model (files) 'implicit)
131 ;;; State querying functions
133 ;;;###autoload (defun vc-hg-registered (file)
134 ;;;###autoload "Return non-nil if FILE is registered with hg."
135 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
136 ;;;###autoload (progn
137 ;;;###autoload (load "vc-hg")
138 ;;;###autoload (vc-hg-registered file))))
140 ;; Modelled after the similar function in vc-bzr.el
141 (defun vc-hg-registered (file)
142 "Return non-nil if FILE is registered with hg."
143 (when (vc-hg-root file) ; short cut
144 (let ((state (vc-hg-state file))) ; expensive
145 (vc-file-setprop file 'vc-state state)
146 (and state (not (memq state '(ignored unregistered)))))))
148 (defun vc-hg-state (file)
149 "Hg-specific version of `vc-state'."
150 (let*
151 ((status nil)
152 (out
153 (with-output-to-string
154 (with-current-buffer
155 standard-output
156 (setq status
157 (condition-case nil
158 ;; Ignore all errors.
159 (call-process
160 "hg" nil t nil "--cwd" (file-name-directory file)
161 "status" "-A" (file-name-nondirectory file))
162 ;; Some problem happened. E.g. We can't find an `hg'
163 ;; executable.
164 (error nil)))))))
165 (when (eq 0 status)
166 (when (null (string-match ".*: No such file or directory$" out))
167 (let ((state (aref out 0)))
168 (cond
169 ((eq state ?=) 'up-to-date)
170 ((eq state ?A) 'added)
171 ((eq state ?M) 'edited)
172 ((eq state ?I) 'ignored)
173 ((eq state ?R) 'removed)
174 ((eq state ?!) 'missing)
175 ((eq state ??) 'unregistered)
176 ((eq state ?C) 'up-to-date) ;; Older mercurials use this
177 (t 'up-to-date)))))))
179 (defun vc-hg-working-revision (file)
180 "Hg-specific version of `vc-working-revision'."
181 (let*
182 ((status nil)
183 (out
184 (with-output-to-string
185 (with-current-buffer
186 standard-output
187 (setq status
188 (condition-case nil
189 ;; Ignore all errors.
190 (call-process
191 "hg" nil t nil "--cwd" (file-name-directory file)
192 "log" "-l1" (file-name-nondirectory file))
193 ;; Some problem happened. E.g. We can't find an `hg'
194 ;; executable.
195 (error nil)))))))
196 (when (eq 0 status)
197 (if (string-match "changeset: *\\([0-9]*\\)" out)
198 (match-string 1 out)
199 "0"))))
201 ;;; History functions
203 (defun vc-hg-print-log (files &optional buffer)
204 "Get change log associated with FILES."
205 ;; `log-view-mode' needs to have the file names in order to function
206 ;; correctly. "hg log" does not print it, so we insert it here by
207 ;; hand.
209 ;; `vc-do-command' creates the buffer, but we need it before running
210 ;; the command.
211 (vc-setup-buffer buffer)
212 ;; If the buffer exists from a previous invocation it might be
213 ;; read-only.
214 (let ((inhibit-read-only t))
215 (with-current-buffer
216 buffer
217 (vc-hg-command buffer 0 files "log"))))
219 (defvar log-view-message-re)
220 (defvar log-view-file-re)
221 (defvar log-view-font-lock-keywords)
222 (defvar log-view-per-file-logs)
224 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
225 (require 'add-log) ;; we need the add-log faces
226 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
227 (set (make-local-variable 'log-view-per-file-logs) nil)
228 (set (make-local-variable 'log-view-message-re)
229 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)")
230 (set (make-local-variable 'log-view-font-lock-keywords)
231 (append
232 log-view-font-lock-keywords
234 ;; Handle the case:
235 ;; user: FirstName LastName <foo@bar>
236 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
237 (1 'change-log-name)
238 (2 'change-log-email))
239 ;; Handle the cases:
240 ;; user: foo@bar
241 ;; and
242 ;; user: foo
243 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
244 (1 'change-log-email))
245 ("^date: \\(.+\\)" (1 'change-log-date))
246 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
248 (defun vc-hg-diff (files &optional oldvers newvers buffer)
249 "Get a difference report using hg between two revisions of FILES."
250 (let* ((firstfile (car files))
251 (working (and firstfile (vc-working-revision firstfile))))
252 (when (and (equal oldvers working) (not newvers))
253 (setq oldvers nil))
254 (when (and (not oldvers) newvers)
255 (setq oldvers working))
256 (apply #'vc-hg-command (or buffer "*vc-diff*") nil
257 (mapcar (lambda (file) (file-name-nondirectory file)) files)
258 "--cwd" (or (when firstfile (file-name-directory firstfile))
259 (expand-file-name default-directory))
260 "diff"
261 (append
262 (when oldvers
263 (if newvers
264 (list "-r" oldvers "-r" newvers)
265 (list "-r" oldvers)))))))
267 (defun vc-hg-revision-table (files)
268 (let ((default-directory (file-name-directory (car files))))
269 (with-temp-buffer
270 (vc-hg-command t nil files "log" "--template" "{rev} ")
271 (split-string
272 (buffer-substring-no-properties (point-min) (point-max))))))
274 ;; Modelled after the similar function in vc-cvs.el
275 (defun vc-hg-revision-completion-table (files)
276 (lexical-let ((files files)
277 table)
278 (setq table (lazy-completion-table
279 table (lambda () (vc-hg-revision-table files))))
280 table))
282 (defun vc-hg-annotate-command (file buffer &optional revision)
283 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
284 Optional arg REVISION is a revision to annotate from."
285 (vc-hg-command buffer 0 file "annotate" "-d" "-n"
286 (when revision (concat "-r" revision)))
287 (with-current-buffer buffer
288 (goto-char (point-min))
289 (re-search-forward "^[ \t]*[0-9]")
290 (delete-region (point-min) (match-beginning 0))))
293 ;; The format for one line output by "hg annotate -d -n" looks like this:
294 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
295 ;; i.e: VERSION_NUMBER DATE: CONTENTS
296 ;; If the user has set the "--follow" option, the output looks like:
297 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
298 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
299 (defconst vc-hg-annotate-re
300 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)[^:\n]*\\(:[^ \n][^:\n]*\\)*: ")
302 (defun vc-hg-annotate-time ()
303 (when (looking-at vc-hg-annotate-re)
304 (goto-char (match-end 0))
305 (vc-annotate-convert-time
306 (date-to-time (match-string-no-properties 2)))))
308 (defun vc-hg-annotate-extract-revision-at-line ()
309 (save-excursion
310 (beginning-of-line)
311 (when (looking-at vc-hg-annotate-re) (match-string-no-properties 1))))
313 (defun vc-hg-previous-revision (file rev)
314 (let ((newrev (1- (string-to-number rev))))
315 (when (>= newrev 0)
316 (number-to-string newrev))))
318 (defun vc-hg-next-revision (file rev)
319 (let ((newrev (1+ (string-to-number rev)))
320 (tip-revision
321 (with-temp-buffer
322 (vc-hg-command t 0 nil "tip")
323 (goto-char (point-min))
324 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
325 (string-to-number (match-string-no-properties 1)))))
326 ;; We don't want to exceed the maximum possible revision number, ie
327 ;; the tip revision.
328 (when (<= newrev tip-revision)
329 (number-to-string newrev))))
331 ;; Modelled after the similar function in vc-bzr.el
332 (defun vc-hg-delete-file (file)
333 "Delete FILE and delete it in the hg repository."
334 (condition-case ()
335 (delete-file file)
336 (file-error nil))
337 (vc-hg-command nil 0 file "remove" "--after" "--force"))
339 ;; Modelled after the similar function in vc-bzr.el
340 (defun vc-hg-rename-file (old new)
341 "Rename file from OLD to NEW using `hg mv'."
342 (vc-hg-command nil 0 new "mv" old))
344 (defun vc-hg-register (files &optional rev comment)
345 "Register FILES under hg.
346 REV is ignored.
347 COMMENT is ignored."
348 (vc-hg-command nil 0 files "add"))
350 (defun vc-hg-create-repo ()
351 "Create a new Mercurial repository."
352 (vc-hg-command nil 0 nil "init"))
354 (defalias 'vc-hg-responsible-p 'vc-hg-root)
356 ;; Modelled after the similar function in vc-bzr.el
357 (defun vc-hg-could-register (file)
358 "Return non-nil if FILE could be registered under hg."
359 (and (vc-hg-responsible-p file) ; shortcut
360 (condition-case ()
361 (with-temp-buffer
362 (vc-hg-command t nil file "add" "--dry-run"))
363 ;; The command succeeds with no output if file is
364 ;; registered.
365 (error))))
367 ;; XXX This would remove the file. Is that correct?
368 ;; (defun vc-hg-unregister (file)
369 ;; "Unregister FILE from hg."
370 ;; (vc-hg-command nil nil file "remove"))
372 (defun vc-hg-checkin (files rev comment)
373 "Hg-specific version of `vc-backend-checkin'.
374 REV is ignored."
375 (vc-hg-command nil 0 files "commit" "-m" comment))
377 (defun vc-hg-find-revision (file rev buffer)
378 (let ((coding-system-for-read 'binary)
379 (coding-system-for-write 'binary))
380 (if rev
381 (vc-hg-command buffer 0 file "cat" "-r" rev)
382 (vc-hg-command buffer 0 file "cat"))))
384 ;; Modelled after the similar function in vc-bzr.el
385 (defun vc-hg-checkout (file &optional editable rev)
386 "Retrieve a revision of FILE.
387 EDITABLE is ignored.
388 REV is the revision to check out into WORKFILE."
389 (let ((coding-system-for-read 'binary)
390 (coding-system-for-write 'binary))
391 (with-current-buffer (or (get-file-buffer file) (current-buffer))
392 (if rev
393 (vc-hg-command t 0 file "cat" "-r" rev)
394 (vc-hg-command t 0 file "cat")))))
396 ;; Modelled after the similar function in vc-bzr.el
397 (defun vc-hg-workfile-unchanged-p (file)
398 (eq 'up-to-date (vc-hg-state file)))
400 ;; Modelled after the similar function in vc-bzr.el
401 (defun vc-hg-revert (file &optional contents-done)
402 (unless contents-done
403 (with-temp-buffer (vc-hg-command t 0 file "revert"))))
405 ;;; Hg specific functionality.
407 (defvar vc-hg-extra-menu-map
408 (let ((map (make-sparse-keymap)))
409 (define-key map [incoming] '(menu-item "Show incoming" vc-hg-incoming))
410 (define-key map [outgoing] '(menu-item "Show outgoing" vc-hg-outgoing))
411 map))
413 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
415 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
417 (define-derived-mode vc-hg-outgoing-mode vc-hg-log-view-mode "Hg-Outgoing")
419 (define-derived-mode vc-hg-incoming-mode vc-hg-log-view-mode "Hg-Incoming")
421 (defstruct (vc-hg-extra-fileinfo
422 (:copier nil)
423 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
424 (:conc-name vc-hg-extra-fileinfo->))
425 rename-state ;; rename or copy state
426 extra-name) ;; original name for copies and rename targets, new name for
428 (defun vc-hg-status-printer (info)
429 "Pretty-printer for the vc-dir-fileinfo structure."
430 (let ((extra (vc-dir-fileinfo->extra info)))
431 (vc-default-status-printer 'Hg info)
432 (when extra
433 (insert (propertize
434 (format " (%s %s)"
435 (case (vc-hg-extra-fileinfo->rename-state extra)
436 ('copied "copied from")
437 ('renamed-from "renamed from")
438 ('renamed-to "renamed to"))
439 (vc-hg-extra-fileinfo->extra-name extra))
440 'face 'font-lock-comment-face)))))
442 (defun vc-hg-after-dir-status (update-function)
443 (let ((status-char nil)
444 (file nil)
445 (translation '((?= . up-to-date)
446 (?C . up-to-date)
447 (?A . added)
448 (?R . removed)
449 (?M . edited)
450 (?I . ignored)
451 (?! . missing)
452 (? . copy-rename-line)
453 (?? . unregistered)))
454 (translated nil)
455 (result nil)
456 (last-added nil)
457 (last-line-copy nil))
458 (goto-char (point-min))
459 (while (not (eobp))
460 (setq translated (cdr (assoc (char-after) translation)))
461 (setq file
462 (buffer-substring-no-properties (+ (point) 2)
463 (line-end-position)))
464 (cond ((not translated)
465 (setq last-line-copy nil))
466 ((eq translated 'up-to-date)
467 (setq last-line-copy nil))
468 ((eq translated 'copy-rename-line)
469 ;; For copied files the output looks like this:
470 ;; A COPIED_FILE_NAME
471 ;; ORIGINAL_FILE_NAME
472 (setf (nth 2 last-added)
473 (vc-hg-create-extra-fileinfo 'copied file))
474 (setq last-line-copy t))
475 ((and last-line-copy (eq translated 'removed))
476 ;; For renamed files the output looks like this:
477 ;; A NEW_FILE_NAME
478 ;; ORIGINAL_FILE_NAME
479 ;; R ORIGINAL_FILE_NAME
480 ;; We need to adjust the previous entry to not think it is a copy.
481 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
482 'renamed-from)
483 (push (list file translated
484 (vc-hg-create-extra-fileinfo
485 'renamed-to (nth 0 last-added))) result)
486 (setq last-line-copy nil))
488 (setq last-added (list file translated nil))
489 (push last-added result)
490 (setq last-line-copy nil)))
491 (forward-line))
492 (funcall update-function result)))
494 (defun vc-hg-dir-status (dir update-function)
495 (vc-hg-command (current-buffer) 'async dir "status" "-C")
496 (vc-exec-after
497 `(vc-hg-after-dir-status (quote ,update-function))))
499 (defun vc-hg-status-extra-header (name &rest commands)
500 (concat (propertize name 'face 'font-lock-type-face)
501 (propertize
502 (with-temp-buffer
503 (apply 'vc-hg-command (current-buffer) 0 nil commands)
504 (buffer-substring-no-properties (point-min) (1- (point-max))))
505 'face 'font-lock-variable-name-face)))
507 (defun vc-hg-status-extra-headers (dir)
508 "Generate extra status headers for a Mercurial tree."
509 (let ((default-directory dir))
510 (concat
511 (vc-hg-status-extra-header "Root : " "root") "\n"
512 (vc-hg-status-extra-header "Branch : " "id" "-b") "\n"
513 (vc-hg-status-extra-header "Tags : " "id" "-t") ; "\n"
514 ;; these change after each commit
515 ;; (vc-hg-status-extra-header "Local num : " "id" "-n") "\n"
516 ;; (vc-hg-status-extra-header "Global id : " "id" "-i")
519 ;; XXX this adds another top level menu, instead figure out how to
520 ;; replace the Log-View menu.
521 (easy-menu-define log-view-mode-menu vc-hg-outgoing-mode-map
522 "Hg-outgoing Display Menu"
523 `("Hg-outgoing"
524 ["Push selected" vc-hg-push]))
526 (easy-menu-define log-view-mode-menu vc-hg-incoming-mode-map
527 "Hg-incoming Display Menu"
528 `("Hg-incoming"
529 ["Pull selected" vc-hg-pull]))
531 (defun vc-hg-outgoing ()
532 (interactive)
533 (let ((bname "*Hg outgoing*"))
534 (vc-hg-command bname 0 nil "outgoing" "-n")
535 (pop-to-buffer bname)
536 (vc-hg-outgoing-mode)))
538 (defun vc-hg-incoming ()
539 (interactive)
540 (let ((bname "*Hg incoming*"))
541 (vc-hg-command bname 0 nil "incoming" "-n")
542 (pop-to-buffer bname)
543 (vc-hg-incoming-mode)))
545 (declare-function log-view-get-marked "log-view" ())
547 ;; XXX maybe also add key bindings for these functions.
548 (defun vc-hg-push ()
549 (interactive)
550 (let ((marked-list (log-view-get-marked)))
551 (if marked-list
552 (vc-hg-command
553 nil 0 nil
554 (cons "push"
555 (apply 'nconc
556 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
557 (error "No log entries selected for push"))))
559 (defun vc-hg-pull ()
560 (interactive)
561 (let ((marked-list (log-view-get-marked)))
562 (if marked-list
563 (vc-hg-command
564 nil 0 nil
565 (cons "pull"
566 (apply 'nconc
567 (mapcar (lambda (arg) (list "-r" arg)) marked-list))))
568 (error "No log entries selected for pull"))))
570 ;;; Internal functions
572 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
573 "A wrapper around `vc-do-command' for use in vc-hg.el.
574 The difference to vc-do-command is that this function always invokes `hg',
575 and that it passes `vc-hg-global-switches' to it before FLAGS."
576 (apply 'vc-do-command (or buffer "*vc*") okstatus "hg" file-or-list
577 (if (stringp vc-hg-global-switches)
578 (cons vc-hg-global-switches flags)
579 (append vc-hg-global-switches
580 flags))))
582 (defun vc-hg-root (file)
583 (vc-find-root file ".hg"))
585 (provide 'vc-hg)
587 ;; arch-tag: bd094dc5-715a-434f-a331-37b9fb7cd954
588 ;;; vc-hg.el ends here