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