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