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