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