]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hg.el
Recognize more format variation. Automatically reshow decrypted text.
[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-2015 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 ;; * checkout-model (files) OK
51 ;; - mode-line-string (file) NOT NEEDED
52 ;; STATE-CHANGING FUNCTIONS
53 ;; * register (files &optional rev comment) OK
54 ;; * create-repo () OK
55 ;; - responsible-p (file) OK
56 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
57 ;; - unregister (file) OK
58 ;; * checkin (files rev comment) OK
59 ;; * find-revision (file rev buffer) OK
60 ;; * checkout (file &optional rev) OK
61 ;; * revert (file &optional contents-done) OK
62 ;; - merge (file rev1 rev2) NEEDED
63 ;; - merge-news (file) NEEDED
64 ;; - steal-lock (file &optional revision) NOT NEEDED
65 ;; HISTORY FUNCTIONS
66 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
67 ;; - log-view-mode () OK
68 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
69 ;; - comment-history (file) NOT NEEDED
70 ;; - update-changelog (files) NOT NEEDED
71 ;; * diff (files &optional rev1 rev2 buffer) OK
72 ;; - revision-completion-table (files) OK?
73 ;; - annotate-command (file buf &optional rev) OK
74 ;; - annotate-time () OK
75 ;; - annotate-current-time () NOT NEEDED
76 ;; - annotate-extract-revision-at-line () OK
77 ;; TAG SYSTEM
78 ;; - create-tag (dir name branchp) OK
79 ;; - retrieve-tag (dir name update) OK FIXME UPDATE BUFFERS
80 ;; MISCELLANEOUS
81 ;; - make-version-backups-p (file) ??
82 ;; - previous-revision (file rev) OK
83 ;; - next-revision (file rev) OK
84 ;; - check-headers () ??
85 ;; - delete-file (file) TEST IT
86 ;; - rename-file (old new) OK
87 ;; - find-file-hook () added for bug#10709
88
89 ;; 2) Implement Stefan Monnier's advice:
90 ;; vc-hg-registered and vc-hg-state
91 ;; Both of those functions should be super extra careful to fail gracefully in
92 ;; unexpected circumstances. The reason this is important is that any error
93 ;; there will prevent the user from even looking at the file :-(
94 ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
95 ;; mercurial's control and extracting the current revision should be done
96 ;; without even using `hg' (this way even if you don't have `hg' installed,
97 ;; Emacs is able to tell you this file is under mercurial's control).
98
99 ;;; History:
100 ;;
101
102 ;;; Code:
103
104 (eval-when-compile
105 (require 'cl-lib)
106 (require 'vc)
107 (require 'vc-dir))
108
109 ;;; Customization options
110
111 (defgroup vc-hg nil
112 "VC Mercurial (hg) backend."
113 :version "24.1"
114 :group 'vc)
115
116 (defcustom vc-hg-global-switches nil
117 "Global switches to pass to any Hg command."
118 :type '(choice (const :tag "None" nil)
119 (string :tag "Argument String")
120 (repeat :tag "Argument List" :value ("") string))
121 :version "22.2"
122 :group 'vc-hg)
123
124 (defcustom vc-hg-diff-switches t ; Hg doesn't support common args like -u
125 "String or list of strings specifying switches for Hg diff under VC.
126 If nil, use the value of `vc-diff-switches'. If t, use no switches."
127 :type '(choice (const :tag "Unspecified" nil)
128 (const :tag "None" t)
129 (string :tag "Argument String")
130 (repeat :tag "Argument List" :value ("") string))
131 :version "23.1"
132 :group 'vc-hg)
133
134 (defcustom vc-hg-program "hg"
135 "Name of the Mercurial executable (excluding any arguments)."
136 :type 'string
137 :group 'vc-hg)
138
139 (defcustom vc-hg-root-log-format
140 `(,(concat "{rev}:{ifeq(branch, 'default','', '{branch}')}"
141 ":{bookmarks}:{tags}:{author|person}"
142 " {date|shortdate} {desc|firstline}\\n")
143 ,(concat "^\\(?:[+@o x|-]*\\)" ;Graph data.
144 "\\([0-9]+\\):\\([^:]*\\)"
145 ":\\([^:]*\\):\\([^:]*\\):\\(.*?\\)"
146 "[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)")
147 ((1 'log-view-message-face)
148 (2 'change-log-file)
149 (3 'change-log-list)
150 (4 'change-log-conditionals)
151 (5 'change-log-name)
152 (6 'change-log-date)))
153 "Mercurial log template for `vc-hg-print-log' short format.
154 This should be a list (TEMPLATE REGEXP KEYWORDS), where TEMPLATE
155 is the \"--template\" argument string to pass to Mercurial,
156 REGEXP is a regular expression matching the resulting Mercurial
157 output, and KEYWORDS is a list of `font-lock-keywords' for
158 highlighting the Log View buffer."
159 :type '(list string string (repeat sexp))
160 :group 'vc-hg
161 :version "24.5")
162
163 \f
164 ;;; Properties of the backend
165
166 (defvar vc-hg-history nil)
167
168 (defun vc-hg-revision-granularity () 'repository)
169 (defun vc-hg-checkout-model (_files) 'implicit)
170
171 ;;; State querying functions
172
173 ;;;###autoload (defun vc-hg-registered (file)
174 ;;;###autoload "Return non-nil if FILE is registered with hg."
175 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
176 ;;;###autoload (progn
177 ;;;###autoload (load "vc-hg" nil t)
178 ;;;###autoload (vc-hg-registered file))))
179
180 ;; Modeled after the similar function in vc-bzr.el
181 (defun vc-hg-registered (file)
182 "Return non-nil if FILE is registered with hg."
183 (when (vc-hg-root file) ; short cut
184 (let ((state (vc-hg-state file))) ; expensive
185 (and state (not (memq state '(ignored unregistered)))))))
186
187 (defun vc-hg-state (file)
188 "Hg-specific version of `vc-state'."
189 (setq file (expand-file-name file))
190 (let*
191 ((status nil)
192 (default-directory (file-name-directory file))
193 (out
194 (with-output-to-string
195 (with-current-buffer
196 standard-output
197 (setq status
198 (condition-case nil
199 ;; Ignore all errors.
200 (let ((process-environment
201 ;; Avoid localization of messages so we
202 ;; can parse the output. Disable pager.
203 (append
204 (list "TERM=dumb" "LANGUAGE=C" "HGPLAIN=1")
205 process-environment)))
206 (process-file
207 vc-hg-program nil t nil
208 "--config" "alias.status=status"
209 "--config" "defaults.status="
210 "status" "-A" (file-relative-name file)))
211 ;; Some problem happened. E.g. We can't find an `hg'
212 ;; executable.
213 (error nil)))))))
214 (when (and (eq 0 status)
215 (> (length out) 0)
216 (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 oldvers newvers buffer async)
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"
617 (concat "-mardu" (if files "i"))
618 "-C" files)
619 (vc-run-delayed
620 (vc-hg-after-dir-status update-function)))
621
622 (defun vc-hg-dir-extra-header (name &rest commands)
623 (concat (propertize name 'face 'font-lock-type-face)
624 (propertize
625 (with-temp-buffer
626 (apply 'vc-hg-command (current-buffer) 0 nil commands)
627 (buffer-substring-no-properties (point-min) (1- (point-max))))
628 'face 'font-lock-variable-name-face)))
629
630 (defun vc-hg-dir-extra-headers (dir)
631 "Generate extra status headers for a Mercurial tree."
632 (let ((default-directory dir))
633 (concat
634 (vc-hg-dir-extra-header "Root : " "root") "\n"
635 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
636 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
637 ;; these change after each commit
638 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
639 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
640 )))
641
642 (defun vc-hg-log-incoming (buffer remote-location)
643 (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
644 remote-location)))
645
646 (defun vc-hg-log-outgoing (buffer remote-location)
647 (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
648 remote-location)))
649
650 (declare-function log-view-get-marked "log-view" ())
651
652 ;; XXX maybe also add key bindings for these functions.
653 (defun vc-hg-push ()
654 (interactive)
655 (let ((marked-list (log-view-get-marked)))
656 (if marked-list
657 (apply #'vc-hg-command
658 nil 0 nil
659 "push"
660 (apply 'nconc
661 (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
662 (error "No log entries selected for push"))))
663
664 (defvar vc-hg-error-regexp-alist nil
665 ;; 'hg pull' does not list modified files, so, for now, the only
666 ;; benefit of `vc-compilation-mode' is that one can get rid of
667 ;; *vc-hg* buffer with 'q' or 'z'.
668 ;; TODO: call 'hg incoming' before pull/merge to get the list of
669 ;; modified files
670 "Value of `compilation-error-regexp-alist' in *vc-hg* buffers.")
671
672 (autoload 'vc-do-async-command "vc-dispatcher")
673
674 (defun vc-hg-pull (prompt)
675 "Issue a Mercurial pull command.
676 If called interactively with a set of marked Log View buffers,
677 call \"hg pull -r REVS\" to pull in the specified revisions REVS.
678
679 With a prefix argument or if PROMPT is non-nil, prompt for a
680 specific Mercurial pull command. The default is \"hg pull -u\",
681 which fetches changesets from the default remote repository and
682 then attempts to update the working directory."
683 (interactive "P")
684 (let (marked-list)
685 ;; The `vc-hg-pull' command existed before the `pull' VC action
686 ;; was implemented. Keep it for backward compatibility.
687 (if (and (called-interactively-p 'interactive)
688 (setq marked-list (log-view-get-marked)))
689 (apply #'vc-hg-command
690 nil 0 nil
691 "pull"
692 (apply 'nconc
693 (mapcar (lambda (arg) (list "-r" arg))
694 marked-list)))
695 (let* ((root (vc-hg-root default-directory))
696 (buffer (format "*vc-hg : %s*" (expand-file-name root)))
697 (command "pull")
698 (hg-program vc-hg-program)
699 ;; Fixme: before updating the working copy to the latest
700 ;; state, should check if it's visiting an old revision.
701 (args '("-u")))
702 ;; If necessary, prompt for the exact command.
703 (when prompt
704 (setq args (split-string
705 (read-shell-command "Run Hg (like this): "
706 (format "%s pull -u" hg-program)
707 'vc-hg-history)
708 " " t))
709 (setq hg-program (car args)
710 command (cadr args)
711 args (cddr args)))
712 (apply 'vc-do-async-command buffer root hg-program
713 command args)
714 (with-current-buffer buffer
715 (vc-run-delayed (vc-compilation-mode 'hg)))
716 (vc-set-async-update buffer)))))
717
718 (defun vc-hg-merge-branch ()
719 "Merge incoming changes into the current working directory.
720 This runs the command \"hg merge\"."
721 (let* ((root (vc-hg-root default-directory))
722 (buffer (format "*vc-hg : %s*" (expand-file-name root))))
723 (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
724 (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'hg)))
725 (vc-set-async-update buffer)))
726
727 ;;; Internal functions
728
729 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
730 "A wrapper around `vc-do-command' for use in vc-hg.el.
731 This function differs from vc-do-command in that it invokes
732 `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
733 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
734 (if (stringp vc-hg-global-switches)
735 (cons vc-hg-global-switches flags)
736 (append vc-hg-global-switches
737 flags))))
738
739 (defun vc-hg-root (file)
740 (vc-find-root file ".hg"))
741
742 (provide 'vc-hg)
743
744 ;;; vc-hg.el ends here