]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hg.el
Fixes: debbugs:18940
[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 editable 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 (if (file-remote-p file)
215 (process-file
216 "env" nil t nil
217 "HGPLAIN=1" vc-hg-program
218 "--config" "alias.status=status"
219 "--config" "defaults.status="
220 "status" "-A" (file-relative-name file))
221 (process-file
222 vc-hg-program nil t nil
223 "--config" "alias.status=status"
224 "--config" "defaults.status="
225 "status" "-A" (file-relative-name file))))
226 ;; Some problem happened. E.g. We can't find an `hg'
227 ;; executable.
228 (error nil)))))))
229 (when (eq 0 status)
230 (when (null (string-match ".*: No such file or directory$" out))
231 (let ((state (aref out 0)))
232 (cond
233 ((eq state ?=) 'up-to-date)
234 ((eq state ?A) 'added)
235 ((eq state ?M) 'edited)
236 ((eq state ?I) 'ignored)
237 ((eq state ?R) 'removed)
238 ((eq state ?!) 'missing)
239 ((eq state ??) 'unregistered)
240 ((eq state ?C) 'up-to-date) ;; Older mercurial versions use this.
241 (t 'up-to-date)))))))
242
243 (defun vc-hg-working-revision (file)
244 "Hg-specific version of `vc-working-revision'."
245 (or (ignore-errors
246 (with-output-to-string
247 (vc-hg-command standard-output 0 file
248 "parent" "--template" "{rev}")))
249 "0"))
250
251 ;;; History functions
252
253 (defcustom vc-hg-log-switches nil
254 "String or list of strings specifying switches for hg log under VC."
255 :type '(choice (const :tag "None" nil)
256 (string :tag "Argument String")
257 (repeat :tag "Argument List" :value ("") string))
258 :group 'vc-hg)
259
260 (autoload 'vc-setup-buffer "vc-dispatcher")
261
262 (defvar vc-hg-log-graph nil
263 "If non-nil, use `--graph' in the short log output.")
264
265 (defun vc-hg-print-log (files buffer &optional shortlog start-revision limit)
266 "Print commit log associated with FILES into specified BUFFER.
267 If SHORTLOG is non-nil, use a short format based on `vc-hg-root-log-format'.
268 If START-REVISION is non-nil, it is the newest revision to show.
269 If LIMIT is non-nil, show no more than this many entries."
270 ;; `vc-do-command' creates the buffer, but we need it before running
271 ;; the command.
272 (vc-setup-buffer buffer)
273 ;; If the buffer exists from a previous invocation it might be
274 ;; read-only.
275 (let ((inhibit-read-only t))
276 (with-current-buffer
277 buffer
278 (apply 'vc-hg-command buffer 0 files "log"
279 (nconc
280 (when start-revision (list (format "-r%s:0" start-revision)))
281 (when limit (list "-l" (format "%s" limit)))
282 (when shortlog `(,@(if vc-hg-log-graph '("--graph"))
283 "--template"
284 ,(car vc-hg-root-log-format)))
285 vc-hg-log-switches)))))
286
287 (defvar log-view-message-re)
288 (defvar log-view-file-re)
289 (defvar log-view-font-lock-keywords)
290 (defvar log-view-per-file-logs)
291 (defvar log-view-expanded-log-entry-function)
292
293 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
294 (require 'add-log) ;; we need the add-log faces
295 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
296 (set (make-local-variable 'log-view-per-file-logs) nil)
297 (set (make-local-variable 'log-view-message-re)
298 (if (eq vc-log-view-type 'short)
299 (cadr vc-hg-root-log-format)
300 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
301 ;; Allow expanding short log entries
302 (when (eq vc-log-view-type 'short)
303 (setq truncate-lines t)
304 (set (make-local-variable 'log-view-expanded-log-entry-function)
305 'vc-hg-expanded-log-entry))
306 (set (make-local-variable 'log-view-font-lock-keywords)
307 (if (eq vc-log-view-type 'short)
308 (list (cons (nth 1 vc-hg-root-log-format)
309 (nth 2 vc-hg-root-log-format)))
310 (append
311 log-view-font-lock-keywords
312 '(
313 ;; Handle the case:
314 ;; user: FirstName LastName <foo@bar>
315 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
316 (1 'change-log-name)
317 (2 'change-log-email))
318 ;; Handle the cases:
319 ;; user: foo@bar
320 ;; and
321 ;; user: foo
322 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
323 (1 'change-log-email))
324 ("^date: \\(.+\\)" (1 'change-log-date))
325 ("^tag: +\\([^ ]+\\)$" (1 'highlight))
326 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
327
328 (autoload 'vc-switches "vc")
329
330 (defun vc-hg-diff (files &optional oldvers newvers buffer)
331 "Get a difference report using hg between two revisions of FILES."
332 (let* ((firstfile (car files))
333 (working (and firstfile (vc-working-revision firstfile))))
334 (when (and (equal oldvers working) (not newvers))
335 (setq oldvers nil))
336 (when (and (not oldvers) newvers)
337 (setq oldvers working))
338 (apply #'vc-hg-command (or buffer "*vc-diff*") nil files "diff"
339 (append
340 (vc-switches 'hg 'diff)
341 (when oldvers
342 (if newvers
343 (list "-r" oldvers "-r" newvers)
344 (list "-r" oldvers)))))))
345
346 (defun vc-hg-expanded-log-entry (revision)
347 (with-temp-buffer
348 (vc-hg-command t nil nil "log" "-r" revision)
349 (goto-char (point-min))
350 (unless (eobp)
351 ;; Indent the expanded log entry.
352 (indent-region (point-min) (point-max) 2)
353 (goto-char (point-max))
354 (buffer-string))))
355
356 (defun vc-hg-revision-table (files)
357 (let ((default-directory (file-name-directory (car files))))
358 (with-temp-buffer
359 (vc-hg-command t nil files "log" "--template" "{rev} ")
360 (split-string
361 (buffer-substring-no-properties (point-min) (point-max))))))
362
363 ;; Modeled after the similar function in vc-cvs.el
364 (defun vc-hg-revision-completion-table (files)
365 (letrec ((table (lazy-completion-table
366 table (lambda () (vc-hg-revision-table files)))))
367 table))
368
369 (defun vc-hg-annotate-command (file buffer &optional revision)
370 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
371 Optional arg REVISION is a revision to annotate from."
372 (vc-hg-command buffer 0 file "annotate" "-d" "-n" "--follow"
373 (when revision (concat "-r" revision))))
374
375 (declare-function vc-annotate-convert-time "vc-annotate" (time))
376
377 ;; The format for one line output by "hg annotate -d -n" looks like this:
378 ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
379 ;; i.e: VERSION_NUMBER DATE: CONTENTS
380 ;; If the user has set the "--follow" option, the output looks like:
381 ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
382 ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
383 (defconst vc-hg-annotate-re
384 "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\([^:\n]+\\(?::\\(?:[^: \n][^:\n]*\\)?\\)*\\): \\)\\)")
385
386 (defun vc-hg-annotate-time ()
387 (when (looking-at vc-hg-annotate-re)
388 (goto-char (match-end 0))
389 (vc-annotate-convert-time
390 (date-to-time (match-string-no-properties 2)))))
391
392 (defun vc-hg-annotate-extract-revision-at-line ()
393 (save-excursion
394 (beginning-of-line)
395 (when (looking-at vc-hg-annotate-re)
396 (if (match-beginning 3)
397 (match-string-no-properties 1)
398 (cons (match-string-no-properties 1)
399 (expand-file-name (match-string-no-properties 4)
400 (vc-hg-root default-directory)))))))
401
402 ;;; Tag system
403
404 (defun vc-hg-create-tag (dir name branchp)
405 "Attach the tag NAME to the state of the working copy."
406 (let ((default-directory dir))
407 (and (vc-hg-command nil 0 nil "status")
408 (vc-hg-command nil 0 nil (if branchp "bookmark" "tag") name))))
409
410 (defun vc-hg-retrieve-tag (dir name update)
411 "Retrieve the version tagged by NAME of all registered files at or below DIR."
412 (let ((default-directory dir))
413 (vc-hg-command nil 0 nil "update" name)
414 ;; FIXME: update buffers if `update' is true
415 ;; TODO: update *vc-change-log* buffer so can see @ if --graph
416 ))
417
418 ;;; Miscellaneous
419
420 (defun vc-hg-previous-revision (_file rev)
421 (let ((newrev (1- (string-to-number rev))))
422 (when (>= newrev 0)
423 (number-to-string newrev))))
424
425 (defun vc-hg-next-revision (_file rev)
426 (let ((newrev (1+ (string-to-number rev)))
427 (tip-revision
428 (with-temp-buffer
429 (vc-hg-command t 0 nil "tip" "--style=default")
430 (goto-char (point-min))
431 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
432 (string-to-number (match-string-no-properties 1)))))
433 ;; We don't want to exceed the maximum possible revision number, ie
434 ;; the tip revision.
435 (when (<= newrev tip-revision)
436 (number-to-string newrev))))
437
438 ;; Modeled after the similar function in vc-bzr.el
439 (defun vc-hg-delete-file (file)
440 "Delete FILE and delete it in the hg repository."
441 (condition-case ()
442 (delete-file file)
443 (file-error nil))
444 (vc-hg-command nil 0 file "remove" "--after" "--force"))
445
446 ;; Modeled after the similar function in vc-bzr.el
447 (defun vc-hg-rename-file (old new)
448 "Rename file from OLD to NEW using `hg mv'."
449 (vc-hg-command nil 0 new "mv" old))
450
451 (defun vc-hg-register (files &optional _rev _comment)
452 "Register FILES under hg.
453 REV is ignored.
454 COMMENT is ignored."
455 (vc-hg-command nil 0 files "add"))
456
457 (defun vc-hg-create-repo ()
458 "Create a new Mercurial repository."
459 (vc-hg-command nil 0 nil "init"))
460
461 (defalias 'vc-hg-responsible-p 'vc-hg-root)
462
463 ;; Modeled after the similar function in vc-bzr.el
464 (defun vc-hg-could-register (file)
465 "Return non-nil if FILE could be registered under hg."
466 (and (vc-hg-responsible-p file) ; shortcut
467 (condition-case ()
468 (with-temp-buffer
469 (vc-hg-command t nil file "add" "--dry-run"))
470 ;; The command succeeds with no output if file is
471 ;; registered.
472 (error))))
473
474 (defun vc-hg-unregister (file)
475 "Unregister FILE from hg."
476 (vc-hg-command nil 0 file "forget"))
477
478 (declare-function log-edit-extract-headers "log-edit" (headers string))
479
480 (defun vc-hg-checkin (files _rev comment)
481 "Hg-specific version of `vc-backend-checkin'.
482 REV is ignored."
483 (apply 'vc-hg-command nil 0 files
484 (nconc (list "commit" "-m")
485 (log-edit-extract-headers '(("Author" . "--user")
486 ("Date" . "--date"))
487 comment))))
488
489 (defun vc-hg-find-revision (file rev buffer)
490 (let ((coding-system-for-read 'binary)
491 (coding-system-for-write 'binary))
492 (if rev
493 (vc-hg-command buffer 0 file "cat" "-r" rev)
494 (vc-hg-command buffer 0 file "cat"))))
495
496 (defun vc-hg-find-ignore-file (file)
497 "Return the root directory of the repository of FILE."
498 (expand-file-name ".hgignore"
499 (vc-hg-root file)))
500
501 ;; Modeled after the similar function in vc-bzr.el
502 (defun vc-hg-checkout (file &optional _editable rev)
503 "Retrieve a revision of FILE.
504 EDITABLE is ignored.
505 REV is the revision to check out into WORKFILE."
506 (let ((coding-system-for-read 'binary)
507 (coding-system-for-write 'binary))
508 (with-current-buffer (or (get-file-buffer file) (current-buffer))
509 (if rev
510 (vc-hg-command t 0 file "cat" "-r" rev)
511 (vc-hg-command t 0 file "cat")))))
512
513 (defun vc-hg-resolve-when-done ()
514 "Call \"hg resolve -m\" if the conflict markers have been removed."
515 (save-excursion
516 (goto-char (point-min))
517 (unless (re-search-forward "^<<<<<<< " nil t)
518 (vc-hg-command nil 0 buffer-file-name "resolve" "-m")
519 ;; Remove the hook so that it is not called multiple times.
520 (remove-hook 'after-save-hook 'vc-hg-resolve-when-done t))))
521
522 (defun vc-hg-find-file-hook ()
523 (when (and buffer-file-name
524 (file-exists-p (concat buffer-file-name ".orig"))
525 ;; Hg does not seem to have a "conflict" status, eg
526 ;; hg http://bz.selenic.com/show_bug.cgi?id=2724
527 (memq (vc-file-getprop buffer-file-name 'vc-state)
528 '(edited conflict))
529 ;; Maybe go on to check that "hg resolve -l" says "U"?
530 ;; If "hg resolve -l" says there's a conflict but there are no
531 ;; conflict markers, it's not clear what we should do.
532 (save-excursion
533 (goto-char (point-min))
534 (re-search-forward "^<<<<<<< " nil t)))
535 ;; Hg may not recognize "conflict" as a state, but we can do better.
536 (vc-file-setprop buffer-file-name 'vc-state 'conflict)
537 (smerge-start-session)
538 (add-hook 'after-save-hook 'vc-hg-resolve-when-done nil t)
539 (message "There are unresolved conflicts in this file")))
540
541
542 ;; Modeled after the similar function in vc-bzr.el
543 (defun vc-hg-workfile-unchanged-p (file)
544 (eq 'up-to-date (vc-hg-state file)))
545
546 ;; Modeled after the similar function in vc-bzr.el
547 (defun vc-hg-revert (file &optional contents-done)
548 (unless contents-done
549 (with-temp-buffer (vc-hg-command t 0 file "revert"))))
550
551 ;;; Hg specific functionality.
552
553 (defvar vc-hg-extra-menu-map
554 (let ((map (make-sparse-keymap)))
555 map))
556
557 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
558
559 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
560
561 (defvar log-view-vc-backend)
562
563 (cl-defstruct (vc-hg-extra-fileinfo
564 (:copier nil)
565 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
566 (:conc-name vc-hg-extra-fileinfo->))
567 rename-state ;; rename or copy state
568 extra-name) ;; original name for copies and rename targets, new name for
569
570 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
571
572 (defun vc-hg-dir-printer (info)
573 "Pretty-printer for the vc-dir-fileinfo structure."
574 (let ((extra (vc-dir-fileinfo->extra info)))
575 (vc-default-dir-printer 'Hg info)
576 (when extra
577 (insert (propertize
578 (format " (%s %s)"
579 (pcase (vc-hg-extra-fileinfo->rename-state extra)
580 (`copied "copied from")
581 (`renamed-from "renamed from")
582 (`renamed-to "renamed to"))
583 (vc-hg-extra-fileinfo->extra-name extra))
584 'face 'font-lock-comment-face)))))
585
586 (defun vc-hg-after-dir-status (update-function)
587 (let ((file nil)
588 (translation '((?= . up-to-date)
589 (?C . up-to-date)
590 (?A . added)
591 (?R . removed)
592 (?M . edited)
593 (?I . ignored)
594 (?! . missing)
595 (? . copy-rename-line)
596 (?? . unregistered)))
597 (translated nil)
598 (result nil)
599 (last-added nil)
600 (last-line-copy nil))
601 (goto-char (point-min))
602 (while (not (eobp))
603 (setq translated (cdr (assoc (char-after) translation)))
604 (setq file
605 (buffer-substring-no-properties (+ (point) 2)
606 (line-end-position)))
607 (cond ((not translated)
608 (setq last-line-copy nil))
609 ((eq translated 'up-to-date)
610 (setq last-line-copy nil))
611 ((eq translated 'copy-rename-line)
612 ;; For copied files the output looks like this:
613 ;; A COPIED_FILE_NAME
614 ;; ORIGINAL_FILE_NAME
615 (setf (nth 2 last-added)
616 (vc-hg-create-extra-fileinfo 'copied file))
617 (setq last-line-copy t))
618 ((and last-line-copy (eq translated 'removed))
619 ;; For renamed files the output looks like this:
620 ;; A NEW_FILE_NAME
621 ;; ORIGINAL_FILE_NAME
622 ;; R ORIGINAL_FILE_NAME
623 ;; We need to adjust the previous entry to not think it is a copy.
624 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
625 'renamed-from)
626 (push (list file translated
627 (vc-hg-create-extra-fileinfo
628 'renamed-to (nth 0 last-added))) result)
629 (setq last-line-copy nil))
630 (t
631 (setq last-added (list file translated nil))
632 (push last-added result)
633 (setq last-line-copy nil)))
634 (forward-line))
635 (funcall update-function result)))
636
637 ;; Follows vc-hg-command (or vc-do-async-command), which uses vc-do-command
638 ;; from vc-dispatcher.
639 (declare-function vc-exec-after "vc-dispatcher" (code))
640 ;; Follows vc-exec-after.
641 (declare-function vc-set-async-update "vc-dispatcher" (process-buffer))
642
643 (defun vc-hg-dir-status (dir update-function)
644 (vc-hg-command (current-buffer) 'async dir "status" "-C")
645 (vc-run-delayed
646 (vc-hg-after-dir-status update-function)))
647
648 (defun vc-hg-dir-status-files (dir files _default-state update-function)
649 (apply 'vc-hg-command (current-buffer) 'async dir "status" "-C" files)
650 (vc-run-delayed
651 (vc-hg-after-dir-status update-function)))
652
653 (defun vc-hg-dir-extra-header (name &rest commands)
654 (concat (propertize name 'face 'font-lock-type-face)
655 (propertize
656 (with-temp-buffer
657 (apply 'vc-hg-command (current-buffer) 0 nil commands)
658 (buffer-substring-no-properties (point-min) (1- (point-max))))
659 'face 'font-lock-variable-name-face)))
660
661 (defun vc-hg-dir-extra-headers (dir)
662 "Generate extra status headers for a Mercurial tree."
663 (let ((default-directory dir))
664 (concat
665 (vc-hg-dir-extra-header "Root : " "root") "\n"
666 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
667 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
668 ;; these change after each commit
669 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
670 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
671 )))
672
673 (defun vc-hg-log-incoming (buffer remote-location)
674 (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
675 remote-location)))
676
677 (defun vc-hg-log-outgoing (buffer remote-location)
678 (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
679 remote-location)))
680
681 (declare-function log-view-get-marked "log-view" ())
682
683 ;; XXX maybe also add key bindings for these functions.
684 (defun vc-hg-push ()
685 (interactive)
686 (let ((marked-list (log-view-get-marked)))
687 (if marked-list
688 (apply #'vc-hg-command
689 nil 0 nil
690 "push"
691 (apply 'nconc
692 (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
693 (error "No log entries selected for push"))))
694
695 (defvar vc-hg-error-regexp-alist nil
696 ;; 'hg pull' does not list modified files, so, for now, the only
697 ;; benefit of `vc-compilation-mode' is that one can get rid of
698 ;; *vc-hg* buffer with 'q' or 'z'.
699 ;; TODO: call 'hg incoming' before pull/merge to get the list of
700 ;; modified files
701 "Value of `compilation-error-regexp-alist' in *vc-hg* buffers.")
702
703 (autoload 'vc-do-async-command "vc-dispatcher")
704
705 (defun vc-hg-pull (prompt)
706 "Issue a Mercurial pull command.
707 If called interactively with a set of marked Log View buffers,
708 call \"hg pull -r REVS\" to pull in the specified revisions REVS.
709
710 With a prefix argument or if PROMPT is non-nil, prompt for a
711 specific Mercurial pull command. The default is \"hg pull -u\",
712 which fetches changesets from the default remote repository and
713 then attempts to update the working directory."
714 (interactive "P")
715 (let (marked-list)
716 ;; The `vc-hg-pull' command existed before the `pull' VC action
717 ;; was implemented. Keep it for backward compatibility.
718 (if (and (called-interactively-p 'interactive)
719 (setq marked-list (log-view-get-marked)))
720 (apply #'vc-hg-command
721 nil 0 nil
722 "pull"
723 (apply 'nconc
724 (mapcar (lambda (arg) (list "-r" arg))
725 marked-list)))
726 (let* ((root (vc-hg-root default-directory))
727 (buffer (format "*vc-hg : %s*" (expand-file-name root)))
728 (command "pull")
729 (hg-program vc-hg-program)
730 ;; Fixme: before updating the working copy to the latest
731 ;; state, should check if it's visiting an old revision.
732 (args '("-u")))
733 ;; If necessary, prompt for the exact command.
734 (when prompt
735 (setq args (split-string
736 (read-shell-command "Run Hg (like this): "
737 (format "%s pull -u" hg-program)
738 'vc-hg-history)
739 " " t))
740 (setq hg-program (car args)
741 command (cadr args)
742 args (cddr args)))
743 (apply 'vc-do-async-command buffer root hg-program
744 command args)
745 (with-current-buffer buffer
746 (vc-run-delayed (vc-compilation-mode 'hg)))
747 (vc-set-async-update buffer)))))
748
749 (defun vc-hg-merge-branch ()
750 "Merge incoming changes into the current working directory.
751 This runs the command \"hg merge\"."
752 (let* ((root (vc-hg-root default-directory))
753 (buffer (format "*vc-hg : %s*" (expand-file-name root))))
754 (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
755 (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'hg)))
756 (vc-set-async-update buffer)))
757
758 ;;; Internal functions
759
760 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
761 "A wrapper around `vc-do-command' for use in vc-hg.el.
762 This function differs from vc-do-command in that it invokes
763 `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
764 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
765 (if (stringp vc-hg-global-switches)
766 (cons vc-hg-global-switches flags)
767 (append vc-hg-global-switches
768 flags))))
769
770 (defun vc-hg-root (file)
771 (vc-find-root file ".hg"))
772
773 (provide 'vc-hg)
774
775 ;;; vc-hg.el ends here