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