]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hg.el
Merge branch 'emacs-24'.
[gnu-emacs] / lisp / vc / vc-hg.el
1 ;;; vc-hg.el --- VC backend for the mercurial version control system -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2006-2014 Free Software Foundation, Inc.
4
5 ;; Author: Ivan Kanis
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: vc tools
8 ;; Package: vc
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This is a mercurial version control backend
28
29 ;;; Thanks:
30
31 ;;; Bugs:
32
33 ;;; Installation:
34
35 ;;; Todo:
36
37 ;; 1) Implement the rest of the vc interface. See the comment at the
38 ;; beginning of vc.el. The current status is:
39
40 ;; FUNCTION NAME STATUS
41 ;; BACKEND PROPERTIES
42 ;; * revision-granularity OK
43 ;; STATE-QUERYING FUNCTIONS
44 ;; * registered (file) OK
45 ;; * state (file) OK
46 ;; - state-heuristic (file) NOT NEEDED
47 ;; - dir-status (dir update-function) OK
48 ;; - dir-status-files (dir files ds uf) OK
49 ;; - dir-extra-headers (dir) OK
50 ;; - dir-printer (fileinfo) OK
51 ;; * working-revision (file) OK
52 ;; - latest-on-branch-p (file) ??
53 ;; * checkout-model (files) OK
54 ;; - workfile-unchanged-p (file) OK
55 ;; - mode-line-string (file) NOT NEEDED
56 ;; STATE-CHANGING FUNCTIONS
57 ;; * register (files &optional rev comment) OK
58 ;; * create-repo () OK
59 ;; - init-revision () NOT NEEDED
60 ;; - responsible-p (file) OK
61 ;; - could-register (file) OK
62 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
63 ;; - unregister (file) OK
64 ;; * checkin (files rev comment) OK
65 ;; * find-revision (file rev buffer) OK
66 ;; * checkout (file &optional rev) OK
67 ;; * revert (file &optional contents-done) OK
68 ;; - rollback (files) ?? PROBABLY NOT NEEDED
69 ;; - merge (file rev1 rev2) NEEDED
70 ;; - merge-news (file) NEEDED
71 ;; - steal-lock (file &optional revision) NOT NEEDED
72 ;; HISTORY FUNCTIONS
73 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
74 ;; - log-view-mode () OK
75 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
76 ;; - comment-history (file) NOT NEEDED
77 ;; - update-changelog (files) NOT NEEDED
78 ;; * diff (files &optional rev1 rev2 buffer) OK
79 ;; - revision-completion-table (files) OK?
80 ;; - annotate-command (file buf &optional rev) OK
81 ;; - annotate-time () OK
82 ;; - annotate-current-time () NOT NEEDED
83 ;; - annotate-extract-revision-at-line () OK
84 ;; TAG SYSTEM
85 ;; - create-tag (dir name branchp) OK
86 ;; - retrieve-tag (dir name update) OK FIXME UPDATE BUFFERS
87 ;; MISCELLANEOUS
88 ;; - make-version-backups-p (file) ??
89 ;; - repository-hostname (dirname) ??
90 ;; - previous-revision (file rev) OK
91 ;; - next-revision (file rev) OK
92 ;; - check-headers () ??
93 ;; - clear-headers () ??
94 ;; - delete-file (file) TEST IT
95 ;; - rename-file (old new) OK
96 ;; - find-file-hook () added for bug#10709
97
98 ;; 2) Implement Stefan Monnier's advice:
99 ;; vc-hg-registered and vc-hg-state
100 ;; Both of those functions should be super extra careful to fail gracefully in
101 ;; unexpected circumstances. The reason this is important is that any error
102 ;; there will prevent the user from even looking at the file :-(
103 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
104 ;; mercurial's control and extracting the current revision should be done
105 ;; without even using `hg' (this way even if you don't have `hg' installed,
106 ;; Emacs is able to tell you this file is under mercurial's control).
107
108 ;;; History:
109 ;;
110
111 ;;; Code:
112
113 (eval-when-compile
114 (require 'cl-lib)
115 (require 'vc)
116 (require 'vc-dir))
117
118 ;;; Customization options
119
120 (defgroup vc-hg nil
121 "VC Mercurial (hg) backend."
122 :version "24.1"
123 :group 'vc)
124
125 (defcustom vc-hg-global-switches nil
126 "Global switches to pass to any Hg command."
127 :type '(choice (const :tag "None" nil)
128 (string :tag "Argument String")
129 (repeat :tag "Argument List" :value ("") string))
130 :version "22.2"
131 :group 'vc-hg)
132
133 (defcustom vc-hg-diff-switches t ; Hg doesn't support common args like -u
134 "String or list of strings specifying switches for Hg diff under VC.
135 If nil, use the value of `vc-diff-switches'. If t, use no switches."
136 :type '(choice (const :tag "Unspecified" nil)
137 (const :tag "None" t)
138 (string :tag "Argument String")
139 (repeat :tag "Argument List" :value ("") string))
140 :version "23.1"
141 :group 'vc-hg)
142
143 (defcustom vc-hg-program "hg"
144 "Name of the Mercurial executable (excluding any arguments)."
145 :type 'string
146 :group 'vc-hg)
147
148 (defcustom vc-hg-root-log-format
149 `(,(concat "{rev}:{ifeq(branch, 'default','', '{branch}')}"
150 ":{bookmarks}:{tags}:{author|person}"
151 " {date|shortdate} {desc|firstline}\\n")
152 ,(concat "^\\(?:[+@o x|-]*\\)" ;Graph data.
153 "\\([0-9]+\\):\\([^:]*\\)"
154 ":\\([^:]*\\):\\([^:]*\\):\\(.*?\\)"
155 "[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)")
156 ((1 'log-view-message-face)
157 (2 'change-log-file)
158 (3 'change-log-list)
159 (4 'change-log-conditionals)
160 (5 'change-log-name)
161 (6 'change-log-date)))
162 "Mercurial log template for `vc-hg-print-log' short format.
163 This should be a list (TEMPLATE REGEXP KEYWORDS), where TEMPLATE
164 is the \"--template\" argument string to pass to Mercurial,
165 REGEXP is a regular expression matching the resulting Mercurial
166 output, and KEYWORDS is a list of `font-lock-keywords' for
167 highlighting the Log View buffer."
168 :type '(list string string (repeat sexp))
169 :group 'vc-hg
170 :version "24.5")
171
172 \f
173 ;;; Properties of the backend
174
175 (defvar vc-hg-history nil)
176
177 (defun vc-hg-revision-granularity () 'repository)
178 (defun vc-hg-checkout-model (_files) 'implicit)
179
180 ;;; State querying functions
181
182 ;;;###autoload (defun vc-hg-registered (file)
183 ;;;###autoload "Return non-nil if FILE is registered with hg."
184 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
185 ;;;###autoload (progn
186 ;;;###autoload (load "vc-hg" nil t)
187 ;;;###autoload (vc-hg-registered file))))
188
189 ;; Modeled after the similar function in vc-bzr.el
190 (defun vc-hg-registered (file)
191 "Return non-nil if FILE is registered with hg."
192 (when (vc-hg-root file) ; short cut
193 (let ((state (vc-hg-state file))) ; expensive
194 (and state (not (memq state '(ignored unregistered)))))))
195
196 (defun vc-hg-state (file)
197 "Hg-specific version of `vc-state'."
198 (let*
199 ((status nil)
200 (default-directory (file-name-directory file))
201 (out
202 (with-output-to-string
203 (with-current-buffer
204 standard-output
205 (setq status
206 (condition-case nil
207 ;; Ignore all errors.
208 (let ((process-environment
209 ;; Avoid localization of messages so we
210 ;; can parse the output. Disable pager.
211 (append
212 (list "TERM=dumb" "LANGUAGE=C" "HGPLAIN=1")
213 process-environment)))
214 (process-file
215 vc-hg-program nil t nil
216 "--config" "alias.status=status"
217 "--config" "defaults.status="
218 "status" "-A" (file-relative-name file)))
219 ;; Some problem happened. E.g. We can't find an `hg'
220 ;; executable.
221 (error nil)))))))
222 (when (eq 0 status)
223 (when (null (string-match ".*: No such file or directory$" out))
224 (let ((state (aref out 0)))
225 (cond
226 ((eq state ?=) 'up-to-date)
227 ((eq state ?A) 'added)
228 ((eq state ?M) 'edited)
229 ((eq state ?I) 'ignored)
230 ((eq state ?R) 'removed)
231 ((eq state ?!) 'missing)
232 ((eq state ??) 'unregistered)
233 ((eq state ?C) 'up-to-date) ;; Older mercurial versions use this.
234 (t 'up-to-date)))))))
235
236 (defun vc-hg-working-revision (file)
237 "Hg-specific version of `vc-working-revision'."
238 (or (ignore-errors
239 (with-output-to-string
240 (vc-hg-command standard-output 0 file
241 "parent" "--template" "{rev}")))
242 "0"))
243
244 ;;; History functions
245
246 (defcustom vc-hg-log-switches nil
247 "String or list of strings specifying switches for hg log under VC."
248 :type '(choice (const :tag "None" nil)
249 (string :tag "Argument String")
250 (repeat :tag "Argument List" :value ("") string))
251 :group 'vc-hg)
252
253 (autoload 'vc-setup-buffer "vc-dispatcher")
254
255 (defvar vc-hg-log-graph nil
256 "If non-nil, use `--graph' in the short log output.")
257
258 (defun vc-hg-print-log (files buffer &optional shortlog start-revision limit)
259 "Print commit log associated with FILES into specified BUFFER.
260 If SHORTLOG is non-nil, use a short format based on `vc-hg-root-log-format'.
261 If START-REVISION is non-nil, it is the newest revision to show.
262 If LIMIT is non-nil, show no more than this many entries."
263 ;; `vc-do-command' creates the buffer, but we need it before running
264 ;; the command.
265 (vc-setup-buffer buffer)
266 ;; If the buffer exists from a previous invocation it might be
267 ;; read-only.
268 (let ((inhibit-read-only t))
269 (with-current-buffer
270 buffer
271 (apply 'vc-hg-command buffer 0 files "log"
272 (nconc
273 (when start-revision (list (format "-r%s:0" start-revision)))
274 (when limit (list "-l" (format "%s" limit)))
275 (when shortlog `(,@(if vc-hg-log-graph '("--graph"))
276 "--template"
277 ,(car vc-hg-root-log-format)))
278 vc-hg-log-switches)))))
279
280 (defvar log-view-message-re)
281 (defvar log-view-file-re)
282 (defvar log-view-font-lock-keywords)
283 (defvar log-view-per-file-logs)
284 (defvar log-view-expanded-log-entry-function)
285
286 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
287 (require 'add-log) ;; we need the add-log faces
288 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
289 (set (make-local-variable 'log-view-per-file-logs) nil)
290 (set (make-local-variable 'log-view-message-re)
291 (if (eq vc-log-view-type 'short)
292 (cadr vc-hg-root-log-format)
293 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
294 ;; Allow expanding short log entries
295 (when (eq vc-log-view-type 'short)
296 (setq truncate-lines t)
297 (set (make-local-variable 'log-view-expanded-log-entry-function)
298 'vc-hg-expanded-log-entry))
299 (set (make-local-variable 'log-view-font-lock-keywords)
300 (if (eq vc-log-view-type 'short)
301 (list (cons (nth 1 vc-hg-root-log-format)
302 (nth 2 vc-hg-root-log-format)))
303 (append
304 log-view-font-lock-keywords
305 '(
306 ;; Handle the case:
307 ;; user: FirstName LastName <foo@bar>
308 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
309 (1 'change-log-name)
310 (2 'change-log-email))
311 ;; Handle the cases:
312 ;; user: foo@bar
313 ;; and
314 ;; user: foo
315 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
316 (1 'change-log-email))
317 ("^date: \\(.+\\)" (1 'change-log-date))
318 ("^tag: +\\([^ ]+\\)$" (1 'highlight))
319 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
320
321 (autoload 'vc-switches "vc")
322
323 (defun vc-hg-diff (files &optional oldvers newvers buffer)
324 "Get a difference report using hg between two revisions of FILES."
325 (let* ((firstfile (car files))
326 (working (and firstfile (vc-working-revision firstfile))))
327 (when (and (equal oldvers working) (not newvers))
328 (setq oldvers nil))
329 (when (and (not oldvers) newvers)
330 (setq oldvers working))
331 (apply #'vc-hg-command (or buffer "*vc-diff*") nil files "diff"
332 (append
333 (vc-switches 'hg 'diff)
334 (when oldvers
335 (if newvers
336 (list "-r" oldvers "-r" newvers)
337 (list "-r" oldvers)))))))
338
339 (defun vc-hg-expanded-log-entry (revision)
340 (with-temp-buffer
341 (vc-hg-command t nil nil "log" "-r" revision)
342 (goto-char (point-min))
343 (unless (eobp)
344 ;; Indent the expanded log entry.
345 (indent-region (point-min) (point-max) 2)
346 (goto-char (point-max))
347 (buffer-string))))
348
349 (defun vc-hg-revision-table (files)
350 (let ((default-directory (file-name-directory (car files))))
351 (with-temp-buffer
352 (vc-hg-command t nil files "log" "--template" "{rev} ")
353 (split-string
354 (buffer-substring-no-properties (point-min) (point-max))))))
355
356 ;; Modeled after the similar function in vc-cvs.el
357 (defun vc-hg-revision-completion-table (files)
358 (letrec ((table (lazy-completion-table
359 table (lambda () (vc-hg-revision-table files)))))
360 table))
361
362 (defun vc-hg-annotate-command (file buffer &optional revision)
363 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
364 Optional arg REVISION is a revision to annotate from."
365 (vc-hg-command buffer 0 file "annotate" "-d" "-n" "--follow"
366 (when revision (concat "-r" revision))))
367
368 (declare-function vc-annotate-convert-time "vc-annotate" (time))
369
370 ;; The format for one line output by "hg annotate -d -n" looks like this:
371 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
372 ;; i.e: VERSION_NUMBER DATE: CONTENTS
373 ;; If the user has set the "--follow" option, the output looks like:
374 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
375 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
376 (defconst vc-hg-annotate-re
377 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\([^:\n]+\\(?::\\(?:[^: \n][^:\n]*\\)?\\)*\\): \\)\\)")
378
379 (defun vc-hg-annotate-time ()
380 (when (looking-at vc-hg-annotate-re)
381 (goto-char (match-end 0))
382 (vc-annotate-convert-time
383 (date-to-time (match-string-no-properties 2)))))
384
385 (defun vc-hg-annotate-extract-revision-at-line ()
386 (save-excursion
387 (beginning-of-line)
388 (when (looking-at vc-hg-annotate-re)
389 (if (match-beginning 3)
390 (match-string-no-properties 1)
391 (cons (match-string-no-properties 1)
392 (expand-file-name (match-string-no-properties 4)
393 (vc-hg-root default-directory)))))))
394
395 ;;; Tag system
396
397 (defun vc-hg-create-tag (dir name branchp)
398 "Attach the tag NAME to the state of the working copy."
399 (let ((default-directory dir))
400 (and (vc-hg-command nil 0 nil "status")
401 (vc-hg-command nil 0 nil (if branchp "bookmark" "tag") name))))
402
403 (defun vc-hg-retrieve-tag (dir name _update)
404 "Retrieve the version tagged by NAME of all registered files at or below DIR."
405 (let ((default-directory dir))
406 (vc-hg-command nil 0 nil "update" name)
407 ;; FIXME: update buffers if `update' is true
408 ;; TODO: update *vc-change-log* buffer so can see @ if --graph
409 ))
410
411 ;;; Miscellaneous
412
413 (defun vc-hg-previous-revision (_file rev)
414 (let ((newrev (1- (string-to-number rev))))
415 (when (>= newrev 0)
416 (number-to-string newrev))))
417
418 (defun vc-hg-next-revision (_file rev)
419 (let ((newrev (1+ (string-to-number rev)))
420 (tip-revision
421 (with-temp-buffer
422 (vc-hg-command t 0 nil "tip" "--style=default")
423 (goto-char (point-min))
424 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
425 (string-to-number (match-string-no-properties 1)))))
426 ;; We don't want to exceed the maximum possible revision number, ie
427 ;; the tip revision.
428 (when (<= newrev tip-revision)
429 (number-to-string newrev))))
430
431 ;; Modeled after the similar function in vc-bzr.el
432 (defun vc-hg-delete-file (file)
433 "Delete FILE and delete it in the hg repository."
434 (condition-case ()
435 (delete-file file)
436 (file-error nil))
437 (vc-hg-command nil 0 file "remove" "--after" "--force"))
438
439 ;; Modeled after the similar function in vc-bzr.el
440 (defun vc-hg-rename-file (old new)
441 "Rename file from OLD to NEW using `hg mv'."
442 (vc-hg-command nil 0 new "mv" old))
443
444 (defun vc-hg-register (files &optional _rev _comment)
445 "Register FILES under hg.
446 REV is ignored.
447 COMMENT is ignored."
448 (vc-hg-command nil 0 files "add"))
449
450 (defun vc-hg-create-repo ()
451 "Create a new Mercurial repository."
452 (vc-hg-command nil 0 nil "init"))
453
454 (defalias 'vc-hg-responsible-p 'vc-hg-root)
455
456 ;; Modeled after the similar function in vc-bzr.el
457 (defun vc-hg-could-register (file)
458 "Return non-nil if FILE could be registered under hg."
459 (and (vc-hg-responsible-p file) ; shortcut
460 (condition-case ()
461 (with-temp-buffer
462 (vc-hg-command t nil file "add" "--dry-run"))
463 ;; The command succeeds with no output if file is
464 ;; registered.
465 (error))))
466
467 (defun vc-hg-unregister (file)
468 "Unregister FILE from hg."
469 (vc-hg-command nil 0 file "forget"))
470
471 (declare-function log-edit-extract-headers "log-edit" (headers string))
472
473 (defun vc-hg-checkin (files comment)
474 "Hg-specific version of `vc-backend-checkin'.
475 REV is ignored."
476 (apply 'vc-hg-command nil 0 files
477 (nconc (list "commit" "-m")
478 (log-edit-extract-headers '(("Author" . "--user")
479 ("Date" . "--date"))
480 comment))))
481
482 (defun vc-hg-find-revision (file rev buffer)
483 (let ((coding-system-for-read 'binary)
484 (coding-system-for-write 'binary))
485 (if rev
486 (vc-hg-command buffer 0 file "cat" "-r" rev)
487 (vc-hg-command buffer 0 file "cat"))))
488
489 (defun vc-hg-find-ignore-file (file)
490 "Return the root directory of the repository of FILE."
491 (expand-file-name ".hgignore"
492 (vc-hg-root file)))
493
494 ;; Modeled after the similar function in vc-bzr.el
495 (defun vc-hg-checkout (file &optional rev)
496 "Retrieve a revision of FILE.
497 EDITABLE is ignored.
498 REV is the revision to check out into WORKFILE."
499 (let ((coding-system-for-read 'binary)
500 (coding-system-for-write 'binary))
501 (with-current-buffer (or (get-file-buffer file) (current-buffer))
502 (if rev
503 (vc-hg-command t 0 file "cat" "-r" rev)
504 (vc-hg-command t 0 file "cat")))))
505
506 (defun vc-hg-resolve-when-done ()
507 "Call \"hg resolve -m\" if the conflict markers have been removed."
508 (save-excursion
509 (goto-char (point-min))
510 (unless (re-search-forward "^<<<<<<< " nil t)
511 (vc-hg-command nil 0 buffer-file-name "resolve" "-m")
512 ;; Remove the hook so that it is not called multiple times.
513 (remove-hook 'after-save-hook 'vc-hg-resolve-when-done t))))
514
515 (defun vc-hg-find-file-hook ()
516 (when (and buffer-file-name
517 (file-exists-p (concat buffer-file-name ".orig"))
518 ;; Hg does not seem to have a "conflict" status, eg
519 ;; hg http://bz.selenic.com/show_bug.cgi?id=2724
520 (memq (vc-file-getprop buffer-file-name 'vc-state)
521 '(edited conflict))
522 ;; Maybe go on to check that "hg resolve -l" says "U"?
523 ;; If "hg resolve -l" says there's a conflict but there are no
524 ;; conflict markers, it's not clear what we should do.
525 (save-excursion
526 (goto-char (point-min))
527 (re-search-forward "^<<<<<<< " nil t)))
528 ;; Hg may not recognize "conflict" as a state, but we can do better.
529 (vc-file-setprop buffer-file-name 'vc-state 'conflict)
530 (smerge-start-session)
531 (add-hook 'after-save-hook 'vc-hg-resolve-when-done nil t)
532 (message "There are unresolved conflicts in this file")))
533
534
535 ;; Modeled after the similar function in vc-bzr.el
536 (defun vc-hg-workfile-unchanged-p (file)
537 (eq 'up-to-date (vc-hg-state file)))
538
539 ;; Modeled after the similar function in vc-bzr.el
540 (defun vc-hg-revert (file &optional contents-done)
541 (unless contents-done
542 (with-temp-buffer (vc-hg-command t 0 file "revert"))))
543
544 ;;; Hg specific functionality.
545
546 (defvar vc-hg-extra-menu-map
547 (let ((map (make-sparse-keymap)))
548 map))
549
550 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
551
552 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
553
554 (defvar log-view-vc-backend)
555
556 (cl-defstruct (vc-hg-extra-fileinfo
557 (:copier nil)
558 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
559 (:conc-name vc-hg-extra-fileinfo->))
560 rename-state ;; rename or copy state
561 extra-name) ;; original name for copies and rename targets, new name for
562
563 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
564
565 (defun vc-hg-dir-printer (info)
566 "Pretty-printer for the vc-dir-fileinfo structure."
567 (let ((extra (vc-dir-fileinfo->extra info)))
568 (vc-default-dir-printer 'Hg info)
569 (when extra
570 (insert (propertize
571 (format " (%s %s)"
572 (pcase (vc-hg-extra-fileinfo->rename-state extra)
573 (`copied "copied from")
574 (`renamed-from "renamed from")
575 (`renamed-to "renamed to"))
576 (vc-hg-extra-fileinfo->extra-name extra))
577 'face 'font-lock-comment-face)))))
578
579 (defun vc-hg-after-dir-status (update-function)
580 (let ((file nil)
581 (translation '((?= . up-to-date)
582 (?C . up-to-date)
583 (?A . added)
584 (?R . removed)
585 (?M . edited)
586 (?I . ignored)
587 (?! . missing)
588 (? . copy-rename-line)
589 (?? . unregistered)))
590 (translated nil)
591 (result nil)
592 (last-added nil)
593 (last-line-copy nil))
594 (goto-char (point-min))
595 (while (not (eobp))
596 (setq translated (cdr (assoc (char-after) translation)))
597 (setq file
598 (buffer-substring-no-properties (+ (point) 2)
599 (line-end-position)))
600 (cond ((not translated)
601 (setq last-line-copy nil))
602 ((eq translated 'up-to-date)
603 (setq last-line-copy nil))
604 ((eq translated 'copy-rename-line)
605 ;; For copied files the output looks like this:
606 ;; A COPIED_FILE_NAME
607 ;; ORIGINAL_FILE_NAME
608 (setf (nth 2 last-added)
609 (vc-hg-create-extra-fileinfo 'copied file))
610 (setq last-line-copy t))
611 ((and last-line-copy (eq translated 'removed))
612 ;; For renamed files the output looks like this:
613 ;; A NEW_FILE_NAME
614 ;; ORIGINAL_FILE_NAME
615 ;; R ORIGINAL_FILE_NAME
616 ;; We need to adjust the previous entry to not think it is a copy.
617 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
618 'renamed-from)
619 (push (list file translated
620 (vc-hg-create-extra-fileinfo
621 'renamed-to (nth 0 last-added))) result)
622 (setq last-line-copy nil))
623 (t
624 (setq last-added (list file translated nil))
625 (push last-added result)
626 (setq last-line-copy nil)))
627 (forward-line))
628 (funcall update-function result)))
629
630 ;; Follows vc-hg-command (or vc-do-async-command), which uses vc-do-command
631 ;; from vc-dispatcher.
632 (declare-function vc-exec-after "vc-dispatcher" (code))
633 ;; Follows vc-exec-after.
634 (declare-function vc-set-async-update "vc-dispatcher" (process-buffer))
635
636 (defun vc-hg-dir-status (dir update-function)
637 (vc-hg-command (current-buffer) 'async dir "status" "-C")
638 (vc-run-delayed
639 (vc-hg-after-dir-status update-function)))
640
641 (defun vc-hg-dir-status-files (dir files _default-state update-function)
642 (apply 'vc-hg-command (current-buffer) 'async dir "status" "-mardui" "-C" files)
643 (vc-run-delayed
644 (vc-hg-after-dir-status update-function)))
645
646 (defun vc-hg-dir-extra-header (name &rest commands)
647 (concat (propertize name 'face 'font-lock-type-face)
648 (propertize
649 (with-temp-buffer
650 (apply 'vc-hg-command (current-buffer) 0 nil commands)
651 (buffer-substring-no-properties (point-min) (1- (point-max))))
652 'face 'font-lock-variable-name-face)))
653
654 (defun vc-hg-dir-extra-headers (dir)
655 "Generate extra status headers for a Mercurial tree."
656 (let ((default-directory dir))
657 (concat
658 (vc-hg-dir-extra-header "Root : " "root") "\n"
659 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
660 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
661 ;; these change after each commit
662 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
663 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
664 )))
665
666 (defun vc-hg-log-incoming (buffer remote-location)
667 (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
668 remote-location)))
669
670 (defun vc-hg-log-outgoing (buffer remote-location)
671 (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
672 remote-location)))
673
674 (declare-function log-view-get-marked "log-view" ())
675
676 ;; XXX maybe also add key bindings for these functions.
677 (defun vc-hg-push ()
678 (interactive)
679 (let ((marked-list (log-view-get-marked)))
680 (if marked-list
681 (apply #'vc-hg-command
682 nil 0 nil
683 "push"
684 (apply 'nconc
685 (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
686 (error "No log entries selected for push"))))
687
688 (defvar vc-hg-error-regexp-alist nil
689 ;; 'hg pull' does not list modified files, so, for now, the only
690 ;; benefit of `vc-compilation-mode' is that one can get rid of
691 ;; *vc-hg* buffer with 'q' or 'z'.
692 ;; TODO: call 'hg incoming' before pull/merge to get the list of
693 ;; modified files
694 "Value of `compilation-error-regexp-alist' in *vc-hg* buffers.")
695
696 (autoload 'vc-do-async-command "vc-dispatcher")
697
698 (defun vc-hg-pull (prompt)
699 "Issue a Mercurial pull command.
700 If called interactively with a set of marked Log View buffers,
701 call \"hg pull -r REVS\" to pull in the specified revisions REVS.
702
703 With a prefix argument or if PROMPT is non-nil, prompt for a
704 specific Mercurial pull command. The default is \"hg pull -u\",
705 which fetches changesets from the default remote repository and
706 then attempts to update the working directory."
707 (interactive "P")
708 (let (marked-list)
709 ;; The `vc-hg-pull' command existed before the `pull' VC action
710 ;; was implemented. Keep it for backward compatibility.
711 (if (and (called-interactively-p 'interactive)
712 (setq marked-list (log-view-get-marked)))
713 (apply #'vc-hg-command
714 nil 0 nil
715 "pull"
716 (apply 'nconc
717 (mapcar (lambda (arg) (list "-r" arg))
718 marked-list)))
719 (let* ((root (vc-hg-root default-directory))
720 (buffer (format "*vc-hg : %s*" (expand-file-name root)))
721 (command "pull")
722 (hg-program vc-hg-program)
723 ;; Fixme: before updating the working copy to the latest
724 ;; state, should check if it's visiting an old revision.
725 (args '("-u")))
726 ;; If necessary, prompt for the exact command.
727 (when prompt
728 (setq args (split-string
729 (read-shell-command "Run Hg (like this): "
730 (format "%s pull -u" hg-program)
731 'vc-hg-history)
732 " " t))
733 (setq hg-program (car args)
734 command (cadr args)
735 args (cddr args)))
736 (apply 'vc-do-async-command buffer root hg-program
737 command args)
738 (with-current-buffer buffer
739 (vc-run-delayed (vc-compilation-mode 'hg)))
740 (vc-set-async-update buffer)))))
741
742 (defun vc-hg-merge-branch ()
743 "Merge incoming changes into the current working directory.
744 This runs the command \"hg merge\"."
745 (let* ((root (vc-hg-root default-directory))
746 (buffer (format "*vc-hg : %s*" (expand-file-name root))))
747 (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
748 (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'hg)))
749 (vc-set-async-update buffer)))
750
751 ;;; Internal functions
752
753 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
754 "A wrapper around `vc-do-command' for use in vc-hg.el.
755 This function differs from vc-do-command in that it invokes
756 `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
757 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
758 (if (stringp vc-hg-global-switches)
759 (cons vc-hg-global-switches flags)
760 (append vc-hg-global-switches
761 flags))))
762
763 (defun vc-hg-root (file)
764 (vc-find-root file ".hg"))
765
766 (provide 'vc-hg)
767
768 ;;; vc-hg.el ends here