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