Merge branch 'master' into comment-cache
[emacs.git] / lisp / vc / vc-hg.el
blob2f9487ca2e71819d80478465074a75a930957ea4
1 ;;; vc-hg.el --- VC backend for the mercurial version control system -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2017 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) OK
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 (declare-function vc-compilation-mode "vc-dispatcher" (backend))
111 ;;; Customization options
113 (defgroup vc-hg nil
114 "VC Mercurial (hg) backend."
115 :version "24.1"
116 :group 'vc)
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)
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)
136 (defcustom vc-hg-annotate-switches '("-u" "--follow")
137 "String or list of strings specifying switches for hg annotate under VC.
138 If nil, use the value of `vc-annotate-switches'. If t, use no
139 switches."
140 :type '(choice (const :tag "Unspecified" nil)
141 (const :tag "None" t)
142 (string :tag "Argument String")
143 (repeat :tag "Argument List" :value ("") string))
144 :version "25.1"
145 :group 'vc-hg)
147 (defcustom vc-hg-program "hg"
148 "Name of the Mercurial executable (excluding any arguments)."
149 :type 'string
150 :group 'vc-hg)
152 (defcustom vc-hg-root-log-format
153 `(,(concat "{rev}:{ifeq(branch, 'default','', '{branch}')}"
154 ":{bookmarks}:{tags}:{author|person}"
155 " {date|shortdate} {desc|firstline}\\n")
156 ,(concat "^\\(?:[+@o x|-]*\\)" ;Graph data.
157 "\\([0-9]+\\):\\([^:]*\\)"
158 ":\\([^:]*\\):\\([^:]*\\):\\(.*?\\)"
159 "[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)")
160 ((1 'log-view-message)
161 (2 'change-log-file)
162 (3 'change-log-list)
163 (4 'change-log-conditionals)
164 (5 'change-log-name)
165 (6 'change-log-date)))
166 "Mercurial log template for `vc-hg-print-log' short format.
167 This should be a list (TEMPLATE REGEXP KEYWORDS), where TEMPLATE
168 is the \"--template\" argument string to pass to Mercurial,
169 REGEXP is a regular expression matching the resulting Mercurial
170 output, and KEYWORDS is a list of `font-lock-keywords' for
171 highlighting the Log View buffer."
172 :type '(list string string (repeat sexp))
173 :group 'vc-hg
174 :version "24.5")
177 ;;; Properties of the backend
179 (defvar vc-hg-history nil)
181 (defun vc-hg-revision-granularity () 'repository)
182 (defun vc-hg-checkout-model (_files) 'implicit)
184 ;;; State querying functions
186 ;;;###autoload (defun vc-hg-registered (file)
187 ;;;###autoload "Return non-nil if FILE is registered with hg."
188 ;;;###autoload (if (vc-find-root file ".hg") ; short cut
189 ;;;###autoload (progn
190 ;;;###autoload (load "vc-hg" nil t)
191 ;;;###autoload (vc-hg-registered file))))
193 ;; Modeled after the similar function in vc-bzr.el
194 (defun vc-hg-registered (file)
195 "Return non-nil if FILE is registered with hg."
196 (when (vc-hg-root file) ; short cut
197 (let ((state (vc-hg-state file))) ; expensive
198 (and state (not (memq state '(ignored unregistered)))))))
200 (defun vc-hg-state (file)
201 "Hg-specific version of `vc-state'."
202 (let ((state (vc-hg-state-fast file)))
203 (if (eq state 'unsupported) (vc-hg-state-slow file) state)))
205 (defun vc-hg-state-slow (file)
206 "Determine status of FILE by running hg."
207 (setq file (expand-file-name file))
208 (let*
209 ((status nil)
210 (default-directory (file-name-directory file))
211 (out
212 (with-output-to-string
213 (with-current-buffer
214 standard-output
215 (setq status
216 (condition-case nil
217 ;; Ignore all errors.
218 (let ((process-environment
219 ;; Avoid localization of messages so we
220 ;; can parse the output. Disable pager.
221 (append
222 (list "TERM=dumb" "LANGUAGE=C" "HGPLAIN=1")
223 process-environment)))
224 (process-file
225 vc-hg-program nil t nil
226 "--config" "alias.status=status"
227 "--config" "defaults.status="
228 "status" "-A" (file-relative-name file)))
229 ;; Some problem happened. E.g. We can't find an `hg'
230 ;; executable.
231 (error nil)))))))
232 (when (and (eq 0 status)
233 (> (length out) 0)
234 (null (string-match ".*: No such file or directory$" out)))
235 (let ((state (aref out 0)))
236 (cond
237 ((eq state ?=) 'up-to-date)
238 ((eq state ?A) 'added)
239 ((eq state ?M) 'edited)
240 ((eq state ?I) 'ignored)
241 ((eq state ?R) 'removed)
242 ((eq state ?!) 'missing)
243 ((eq state ??) 'unregistered)
244 ((eq state ?C) 'up-to-date) ;; Older mercurial versions use this.
245 (t 'up-to-date))))))
247 (defun vc-hg-working-revision (file)
248 "Hg-specific version of `vc-working-revision'."
249 (or (ignore-errors
250 (with-output-to-string
251 (vc-hg-command standard-output 0 file
252 "parent" "--template" "{rev}")))
253 "0"))
255 (defcustom vc-hg-symbolic-revision-styles
256 '(builtin-active-bookmark
257 "{if(bookmarks,sub(' ',',',bookmarks),if(phabdiff,phabdiff,shortest(node,6)))}")
258 "List of ways to present versions symbolically. The version
259 that we use is the first one that successfully produces a
260 non-empty string.
262 Each entry in the list can be either:
264 - The symbol `builtin-active-bookmark', which indicates that we
265 should use the active bookmark if one exists. A template can
266 supply this information as well, but `builtin-active-bookmark' is
267 handled entirely inside Emacs and so is more efficient than using
268 the generic Mercurial mechanism.
270 - A string giving the Mercurial template to supply to \"hg
271 parent\". \"hg help template\" may be useful reading.
273 - A function to call; it should accept two arguments (a revision
274 and an optional path to which to limit history) and produce a
275 string. The function is called with `default-directory' set to
276 within the repository.
278 If no list entry produces a useful revision, return `nil'."
279 :type '(repeat (choice
280 (const :tag "Active bookmark" 'bookmark)
281 (string :tag "Hg template")
282 (function :tag "Custom")))
283 :version "26.1"
284 :group 'vc-hg)
286 (defcustom vc-hg-use-file-version-for-mode-line-version nil
287 "When enabled, the modeline contains revision information for the visited file.
288 When not, the revision in the modeline is for the repository
289 working copy. `nil' is the much faster setting for
290 large repositories."
291 :type 'boolean
292 :version "26.1"
293 :group 'vc-hg)
295 (defun vc-hg--active-bookmark-internal (rev)
296 (when (equal rev ".")
297 (let* ((current-bookmarks-file ".hg/bookmarks.current"))
298 (when (file-exists-p current-bookmarks-file)
299 (ignore-errors
300 (with-temp-buffer
301 (insert-file-contents current-bookmarks-file)
302 (buffer-substring-no-properties
303 (point-min) (point-max))))))))
305 (defun vc-hg--run-log (template rev path)
306 (ignore-errors
307 (with-output-to-string
308 (if path
309 (vc-hg-command
310 standard-output 0 nil
311 "log" "-f" "-l1" "--template" template path)
312 (vc-hg-command
313 standard-output 0 nil
314 "log" "-r" rev "-l1" "--template" template)))))
316 (defun vc-hg--symbolic-revision (rev &optional path)
317 "Make a Mercurial revision human-readable.
318 REV is a Mercurial revision. `default-directory' is assumed to
319 be in the repository root of interest. PATH, if set, is a
320 specific file to query."
321 (let ((symbolic-revision nil)
322 (styles vc-hg-symbolic-revision-styles))
323 (while (and (not symbolic-revision) styles)
324 (let ((style (pop styles)))
325 (setf symbolic-revision
326 (cond ((and (null path) (eq style 'builtin-active-bookmark))
327 (vc-hg--active-bookmark-internal rev))
328 ((stringp style)
329 (vc-hg--run-log style rev path))
330 ((functionp style)
331 (funcall style rev path))))))
332 symbolic-revision))
334 (defun vc-hg-mode-line-string (file)
335 "Hg-specific version of `vc-mode-line-string'."
336 (let* ((backend-name "Hg")
337 (truename (file-truename file))
338 (state (vc-state truename))
339 (state-echo nil)
340 (face nil)
341 (rev (and state
342 (let ((default-directory
343 (expand-file-name (vc-hg-root truename))))
344 (vc-hg--symbolic-revision
346 (and vc-hg-use-file-version-for-mode-line-version
347 truename)))))
348 (rev (or rev "???")))
349 (propertize
350 (cond ((or (eq state 'up-to-date)
351 (eq state 'needs-update))
352 (setq state-echo "Up to date file")
353 (setq face 'vc-up-to-date-state)
354 (concat backend-name "-" rev))
355 ((eq state 'added)
356 (setq state-echo "Locally added file")
357 (setq face 'vc-locally-added-state)
358 (concat backend-name "@" rev))
359 ((eq state 'conflict)
360 (setq state-echo "File contains conflicts after the last merge")
361 (setq face 'vc-conflict-state)
362 (concat backend-name "!" rev))
363 ((eq state 'removed)
364 (setq state-echo "File removed from the VC system")
365 (setq face 'vc-removed-state)
366 (concat backend-name "!" rev))
367 ((eq state 'missing)
368 (setq state-echo "File tracked by the VC system, but missing from the file system")
369 (setq face 'vc-missing-state)
370 (concat backend-name "?" rev))
372 (setq state-echo "Locally modified file")
373 (setq face 'vc-edited-state)
374 (concat backend-name ":" rev)))
375 'face face
376 'help-echo (concat state-echo " under the " backend-name
377 " version control system"))))
379 ;;; History functions
381 (defcustom vc-hg-log-switches nil
382 "String or list of strings specifying switches for hg log under VC."
383 :type '(choice (const :tag "None" nil)
384 (string :tag "Argument String")
385 (repeat :tag "Argument List" :value ("") string))
386 :group 'vc-hg)
388 (autoload 'vc-setup-buffer "vc-dispatcher")
390 (defvar vc-hg-log-graph nil
391 "If non-nil, use `--graph' in the short log output.")
393 (defvar vc-hg-log-format (concat "changeset: {rev}:{node|short}\n"
394 "{tags % 'tag: {tag}\n'}"
395 "{if(parents, 'parents: {parents}\n')}"
396 "user: {author}\n"
397 "Date: {date|date}\n"
398 "summary: {desc|tabindent}\n\n")
399 "Mercurial log template for `vc-hg-print-log' long format.")
401 (defun vc-hg-print-log (files buffer &optional shortlog start-revision limit)
402 "Print commit log associated with FILES into specified BUFFER.
403 If SHORTLOG is non-nil, use a short format based on `vc-hg-root-log-format'.
404 If START-REVISION is non-nil, it is the newest revision to show.
405 If LIMIT is non-nil, show no more than this many entries."
406 ;; `vc-do-command' creates the buffer, but we need it before running
407 ;; the command.
408 (vc-setup-buffer buffer)
409 ;; If the buffer exists from a previous invocation it might be
410 ;; read-only.
411 (let ((inhibit-read-only t))
412 (with-current-buffer
413 buffer
414 (apply 'vc-hg-command buffer 'async files "log"
415 (nconc
416 (when start-revision (list (format "-r%s:0" start-revision)))
417 (when limit (list "-l" (format "%s" limit)))
418 (if shortlog
419 `(,@(if vc-hg-log-graph '("--graph"))
420 "--template"
421 ,(car vc-hg-root-log-format))
422 `("--template" ,vc-hg-log-format))
423 vc-hg-log-switches)))))
425 (defvar log-view-message-re)
426 (defvar log-view-file-re)
427 (defvar log-view-font-lock-keywords)
428 (defvar log-view-per-file-logs)
429 (defvar log-view-expanded-log-entry-function)
431 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
432 (require 'add-log) ;; we need the add-log faces
433 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
434 (set (make-local-variable 'log-view-per-file-logs) nil)
435 (set (make-local-variable 'log-view-message-re)
436 (if (eq vc-log-view-type 'short)
437 (cadr vc-hg-root-log-format)
438 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
439 (set (make-local-variable 'tab-width) 2)
440 ;; Allow expanding short log entries
441 (when (eq vc-log-view-type 'short)
442 (setq truncate-lines t)
443 (set (make-local-variable 'log-view-expanded-log-entry-function)
444 'vc-hg-expanded-log-entry))
445 (set (make-local-variable 'log-view-font-lock-keywords)
446 (if (eq vc-log-view-type 'short)
447 (list (cons (nth 1 vc-hg-root-log-format)
448 (nth 2 vc-hg-root-log-format)))
449 (append
450 log-view-font-lock-keywords
452 ;; Handle the case:
453 ;; user: FirstName LastName <foo@bar>
454 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
455 (1 'change-log-name)
456 (2 'change-log-email))
457 ;; Handle the cases:
458 ;; user: foo@bar
459 ;; and
460 ;; user: foo
461 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
462 (1 'change-log-email))
463 ("^date: \\(.+\\)" (1 'change-log-date))
464 ("^tag: +\\([^ ]+\\)$" (1 'highlight))
465 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
467 (autoload 'vc-switches "vc")
469 (defun vc-hg-diff (files &optional oldvers newvers buffer _async)
470 "Get a difference report using hg between two revisions of FILES."
471 (let* ((firstfile (car files))
472 (working (and firstfile (vc-working-revision firstfile))))
473 (when (and (equal oldvers working) (not newvers))
474 (setq oldvers nil))
475 (when (and (not oldvers) newvers)
476 (setq oldvers working))
477 (apply #'vc-hg-command
478 (or buffer "*vc-diff*")
479 nil ; bug#21969
480 files "diff"
481 (append
482 (vc-switches 'hg 'diff)
483 (when oldvers
484 (if newvers
485 (list "-r" oldvers "-r" newvers)
486 (list "-r" oldvers)))))))
488 (defun vc-hg-expanded-log-entry (revision)
489 (with-temp-buffer
490 (vc-hg-command t nil nil "log" "-r" revision "--template" vc-hg-log-format)
491 (goto-char (point-min))
492 (unless (eobp)
493 ;; Indent the expanded log entry.
494 (indent-region (point-min) (point-max) 2)
495 (goto-char (point-max))
496 (buffer-string))))
498 (defun vc-hg-revision-table (files)
499 (let ((default-directory (file-name-directory (car files))))
500 (with-temp-buffer
501 (vc-hg-command t nil files "log" "--template" "{rev} ")
502 (split-string
503 (buffer-substring-no-properties (point-min) (point-max))))))
505 ;; Modeled after the similar function in vc-cvs.el
506 (defun vc-hg-revision-completion-table (files)
507 (letrec ((table (lazy-completion-table
508 table (lambda () (vc-hg-revision-table files)))))
509 table))
511 (defun vc-hg-annotate-command (file buffer &optional revision)
512 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
513 Optional arg REVISION is a revision to annotate from."
514 (apply #'vc-hg-command buffer 0 file "annotate" "-dq" "-n"
515 (append (vc-switches 'hg 'annotate)
516 (if revision (list (concat "-r" revision))))))
518 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
520 ;; One line printed by "hg annotate -dq -n -u --follow" looks like this:
521 ;; b56girard 114590 2012-03-13 CLOBBER: Lorem ipsum dolor sit
522 ;; i.e. AUTHOR REVISION DATE FILENAME: CONTENTS
523 ;; The user can omit options "-u" and/or "--follow". Then it'll look like:
524 ;; 114590 2012-03-13 CLOBBER:
525 ;; or
526 ;; b56girard 114590 2012-03-13:
527 (defconst vc-hg-annotate-re
528 (concat
529 "^\\(?: *[^ ]+ +\\)?\\([0-9]+\\) " ;User and revision.
530 "\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)" ;Date.
531 "\\(?: +\\([^:]+\\)\\)?:")) ;Filename.
533 (defun vc-hg-annotate-time ()
534 (when (looking-at vc-hg-annotate-re)
535 (goto-char (match-end 0))
536 (vc-annotate-convert-time
537 (let ((str (match-string-no-properties 2)))
538 (encode-time 0 0 0
539 (string-to-number (substring str 6 8))
540 (string-to-number (substring str 4 6))
541 (string-to-number (substring str 0 4)))))))
543 (defun vc-hg-annotate-extract-revision-at-line ()
544 (save-excursion
545 (beginning-of-line)
546 (when (looking-at vc-hg-annotate-re)
547 (if (match-beginning 3)
548 (cons (match-string-no-properties 1)
549 (expand-file-name (match-string-no-properties 3)
550 (vc-hg-root default-directory)))
551 (match-string-no-properties 1)))))
553 ;;; Tag system
555 (defun vc-hg-create-tag (dir name branchp)
556 "Attach the tag NAME to the state of the working copy."
557 (let ((default-directory dir))
558 (and (vc-hg-command nil 0 nil "status")
559 (vc-hg-command nil 0 nil (if branchp "bookmark" "tag") name))))
561 (defun vc-hg-retrieve-tag (dir name _update)
562 "Retrieve the version tagged by NAME of all registered files at or below DIR."
563 (let ((default-directory dir))
564 (vc-hg-command nil 0 nil "update" name)
565 ;; FIXME: update buffers if `update' is true
566 ;; TODO: update *vc-change-log* buffer so can see @ if --graph
569 ;;; Native data structure reading
571 (defcustom vc-hg-parse-hg-data-structures t
572 "If true, try directly parsing Mercurial data structures
573 directly instead of always running Mercurial. We try to be safe
574 against Mercurial data structure format changes and always fall
575 back to running Mercurial directly."
576 :type 'boolean
577 :version "26.1"
578 :group 'vc-hg)
580 (defsubst vc-hg--read-u8 ()
581 "Read and advance over an unsigned byte.
582 Return a fixnum."
583 (prog1 (char-after)
584 (forward-char)))
586 (defsubst vc-hg--read-u32-be ()
587 "Read and advance over a big-endian unsigned 32-bit integer.
588 Return a fixnum; on overflow, result is undefined."
589 ;; Because elisp bytecode has an instruction for multiply and
590 ;; doesn't have one for lsh, it's somewhat counter-intuitively
591 ;; faster to multiply than to shift.
592 (+ (* (vc-hg--read-u8) (* 256 256 256))
593 (* (vc-hg--read-u8) (* 256 256))
594 (* (vc-hg--read-u8) 256)
595 (identity (vc-hg--read-u8))))
597 (defun vc-hg--raw-dirstate-search (dirstate fname)
598 (with-temp-buffer
599 (set-buffer-multibyte nil)
600 (insert-file-contents-literally dirstate)
601 (let* ((result nil)
602 (flen (length fname))
603 (case-fold-search nil)
604 (inhibit-changing-match-data t)
605 ;; Find a conservative bound for the loop below by using
606 ;; Boyer-Moore on the raw dirstate without parsing it; we
607 ;; know we can't possibly find fname _after_ the last place
608 ;; it appears, so we can bail out early if we try to parse
609 ;; past it, which especially helps when the file we're
610 ;; trying to find isn't in dirstate at all. There's no way
611 ;; to similarly bound the starting search position, since
612 ;; the file format is such that we need to parse it from
613 ;; the beginning to find record boundaries.
614 (search-limit
615 (progn
616 (goto-char (point-max))
617 (or (search-backward fname (+ (point-min) 40) t)
618 (point-min)))))
619 ;; 40 is just after the header, which contains the working
620 ;; directory parents
621 (goto-char (+ (point-min) 40))
622 ;; Iterate over all dirstate entries; we might run this loop
623 ;; hundreds of thousands of times, so performance is important
624 ;; here
625 (while (< (point) search-limit)
626 ;; 1+4*4 is the length of the dirstate item header, which we
627 ;; spell as a literal for performance, since the elisp
628 ;; compiler lacks constant propagation
629 (forward-char (1+ (* 3 4)))
630 (let ((this-flen (vc-hg--read-u32-be)))
631 (if (and (or (eq this-flen flen)
632 (and (> this-flen flen)
633 (eq (char-after (+ (point) flen)) 0)))
634 (search-forward fname (+ (point) flen) t))
635 (progn
636 (backward-char (+ flen (1+ (* 4 4))))
637 (setf result
638 (list (vc-hg--read-u8) ; status
639 (vc-hg--read-u32-be) ; mode
640 (vc-hg--read-u32-be) ; size (of file)
641 (vc-hg--read-u32-be) ; mtime
643 (goto-char (point-max)))
644 (forward-char this-flen))))
645 result)))
647 (define-error 'vc-hg-unsupported-syntax "unsupported hgignore syntax")
649 (defconst vc-hg--pcre-c-escapes
650 '((?a . ?\a)
651 (?b . ?\b)
652 (?f . ?\f)
653 (?n . ?\n)
654 (?r . ?\r)
655 (?t . ?\t)
656 (?n . ?\n)
657 (?r . ?\r)
658 (?t . ?\t)
659 (?v . ?\v)))
661 (defconst vc-hg--pcre-metacharacters
662 '(?. ?^ ?$ ?* ?+ ?? ?{ ?\\ ?\[ ?\| ?\())
664 (defconst vc-hg--elisp-metacharacters
665 '(?. ?* ?+ ?? ?\[ ?$ ?\\))
667 (defun vc-hg--escape-for-pcre (c)
668 (if (memq c vc-hg--pcre-metacharacters)
669 (string ?\\ c)
672 (defun vc-hg--parts-to-string (parts)
673 "Build a string from list PARTS. Each element is a character or string."
674 (let ((parts2 nil))
675 (while parts
676 (let* ((partcell (prog1 parts (setf parts (cdr parts))))
677 (part (car partcell)))
678 (if (stringp part)
679 (setf parts2 (nconc (append part nil) parts2))
680 (setcdr partcell parts2)
681 (setf parts2 partcell))))
682 (apply #'string parts2)))
684 (defun vc-hg--pcre-to-elisp-re (pcre prefix)
685 "Transform PCRE, a Mercurial file PCRE, into an elisp RE against PREFIX.
686 PREFIX is the directory name of the directory against which these
687 patterns are rooted. We understand only a subset of PCRE syntax;
688 if we don't understand a construct, we signal
689 `vc-hg-unsupported-syntax'."
690 (cl-assert (string-match "^/\\(.*/\\)?$" prefix))
691 (let ((parts nil)
692 (i 0)
693 (anchored nil)
694 (state 'normal)
695 (pcrelen (length pcre)))
696 (while (< i pcrelen)
697 (let ((c (aref pcre i)))
698 (cond ((eq state 'normal)
699 (cond ((string-match
700 (rx (| "}\\?" (: "(?" (not (any ":")))))
701 pcre i)
702 (signal 'vc-hg-unsupported-syntax (list pcre)))
703 ((eq c ?\\)
704 (setf state 'backslash))
705 ((eq c ?\[)
706 (setf state 'charclass-enter)
707 (push c parts))
708 ((eq c ?^)
709 (if (eq i 0) (setf anchored t)
710 (signal 'vc-hg-unsupported-syntax (list pcre))))
711 ((eq c ?$)
712 ;; Patterns can also match directories exactly,
713 ;; ignoring everything under a matched directory
714 (push "\\(?:$\\|/\\)" parts))
715 ((memq c '(?| ?\( ?\)))
716 (push ?\\ parts)
717 (push c parts))
718 (t (push c parts))))
719 ((eq state 'backslash)
720 (cond ((memq c '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
721 ?A ?b ?B ?d ?D ?s ?S ?w ?W ?Z ?x))
722 (signal 'vc-hg-unsupported-syntax (list pcre)))
723 ((memq c vc-hg--elisp-metacharacters)
724 (push ?\\ parts)
725 (push c parts))
726 (t (push (or (cdr (assq c vc-hg--pcre-c-escapes)) c) parts)))
727 (setf state 'normal))
728 ((eq state 'charclass-enter)
729 (push c parts)
730 (setf state
731 (if (eq c ?\\)
732 'charclass
733 'charclass-backslash)))
734 ((eq state 'charclass-backslash)
735 (if (memq c '(?0 ?x))
736 (signal 'vc-hg-unsupported-syntax (list pcre)))
737 (push (or (cdr (assq c vc-hg--pcre-c-escapes)) c) parts)
738 (setf state 'charclass))
739 ((eq state 'charclass)
740 (push c parts)
741 (cond ((eq c ?\\) (setf state 'charclass-backslash))
742 ((eq c ?\]) (setf state 'normal))))
743 (t (error "invalid state")))
744 (setf i (1+ i))))
745 (unless (eq state 'normal)
746 (signal 'vc-hg-unsupported-syntax (list pcre)))
747 (concat
749 prefix
750 (if anchored "" "\\(?:.*/\\)?")
751 (vc-hg--parts-to-string parts))))
753 (defun vc-hg--glob-to-pcre (glob)
754 "Transform a glob pattern into a Mercurial file pattern regex."
755 (let ((parts nil) (i 0) (n (length glob)) (group 0) c)
756 (cl-macrolet ((peek () '(and (< i n) (aref glob i))))
757 (while (< i n)
758 (setf c (aref glob i))
759 (cl-incf i)
760 (cond ((not (memq c '(?* ?? ?\[ ?\{ ?\} ?, ?\\)))
761 (push (vc-hg--escape-for-pcre c) parts))
762 ((eq c ?*)
763 (cond ((eq (peek) ?*)
764 (cl-incf i)
765 (cond ((eq (peek) ?/)
766 (cl-incf i)
767 (push "(?:.*/)?" parts))
769 (push ".*" parts))))
770 (t (push "[^/]*" parts))))
771 ((eq c ??)
772 (push ?. parts))
773 ((eq c ?\[)
774 (let ((j i))
775 (when (and (< j n) (memq (aref glob j) '(?! ?\])))
776 (cl-incf j))
777 (while (and (< j n) (not (eq (aref glob j) ?\])))
778 (cl-incf j))
779 (cond ((>= j n)
780 (push "\\[" parts))
782 (let ((x (substring glob i j)))
783 (setf x (replace-regexp-in-string
784 "\\\\" "\\\\" x t t))
785 (setf i (1+ j))
786 (cond ((eq (aref x 0) ?!)
787 (setf (aref x 0) ?^))
788 ((eq (aref x 0) ?^)
789 (setf x (concat "\\" x))))
790 (push ?\[ parts)
791 (push x parts)
792 (push ?\] parts))))))
793 ((eq c ?\{)
794 (cl-incf group)
795 (push "(?:" parts))
796 ((eq c ?\})
797 (push ?\) parts)
798 (cl-decf group))
799 ((and (eq c ?,) (> group 0))
800 (push ?| parts))
801 ((eq c ?\\)
802 (if (eq i n)
803 (push "\\\\" parts)
804 (cl-incf i)
805 (push ?\\ parts)
806 (push c parts)))
808 (push (vc-hg--escape-for-pcre c) parts)))))
809 (concat (vc-hg--parts-to-string parts) "$")))
811 (defvar vc-hg--hgignore-patterns)
812 (defvar vc-hg--hgignore-filenames)
814 (defun vc-hg--hgignore-add-pcre (pcre prefix)
815 (push (vc-hg--pcre-to-elisp-re pcre prefix) vc-hg--hgignore-patterns))
817 (defun vc-hg--hgignore-add-glob (glob prefix)
818 (push (vc-hg--pcre-to-elisp-re (vc-hg--glob-to-pcre glob) prefix)
819 vc-hg--hgignore-patterns))
821 (defun vc-hg--hgignore-add-path (path prefix)
822 (let ((parts nil))
823 (dotimes (i (length path))
824 (push (vc-hg--escape-for-pcre (aref path i)) parts))
825 (vc-hg--hgignore-add-pcre
826 (concat "^" (vc-hg--parts-to-string parts) "$")
827 prefix)))
829 (defun vc-hg--slurp-hgignore-1 (hgignore prefix)
830 (let ((default-syntax 'vc-hg--hgignore-add-glob))
831 (with-temp-buffer
832 (let ((attr (file-attributes hgignore)))
833 (when attr (insert-file-contents hgignore))
834 (push (list hgignore (nth 5 attr) (nth 7 attr))
835 vc-hg--hgignore-filenames))
836 (while (not (eobp))
837 ;; This list of pattern-file commands isn't complete, but it
838 ;; should cover the common cases. Remember that we fall back
839 ;; to regular hg commands if we see something we don't like.
840 (save-restriction
841 (narrow-to-region (point) (point-at-eol))
842 (cond ((looking-at "[ \t]*\\(?:#.*\\)?$"))
843 ((looking-at "syntax:[ \t]*re[ \t]*$")
844 (setf default-syntax 'vc-hg--hgignore-add-pcre))
845 ((looking-at "syntax:[ \t]*glob[ \t]*$")
846 (setf default-syntax 'vc-hg--hgignore-add-glob))
847 ((looking-at "path:\\(.+?\\)[ \t]*$")
848 (vc-hg--hgignore-add-path (match-string 1) prefix))
849 ((looking-at "glob:\\(.+?\\)[ \t]*$")
850 (vc-hg--hgignore-add-glob (match-string 1) prefix))
851 ((looking-at "re:\\(.+?\\)[ \t]*$")
852 (vc-hg--hgignore-add-pcre (match-string 1) prefix))
853 ((looking-at "\\(sub\\)?include:\\(.+?\\)[ \t]*$")
854 (let* ((sub (equal (match-string 1) "sub"))
855 (arg (match-string 2))
856 (included-file
857 (if (string-match "^/" arg) arg
858 (concat (file-name-directory hgignore) arg))))
859 (vc-hg--slurp-hgignore-1
860 included-file
861 (if sub (file-name-directory included-file) prefix))))
862 ((looking-at "[a-zA-Z0-9_]*:")
863 (signal 'vc-hg-unsupported-syntax (list (match-string 0))))
864 ((looking-at ".*$")
865 (funcall default-syntax (match-string 0) prefix))))
866 (forward-line 1)))))
868 (cl-defstruct (vc-hg--ignore-patterns
869 (:copier nil)
870 (:constructor vc-hg--ignore-patterns-make))
871 repo
872 ignore-patterns
873 file-sources)
875 (defun vc-hg--slurp-hgignore (repo)
876 "Read hg ignore patterns from REPO.
877 REPO must be the directory name of an hg repository."
878 (cl-assert (string-match "^/\\(.*/\\)?$" repo))
879 (let* ((hgignore (concat repo ".hgignore"))
880 (vc-hg--hgignore-patterns nil)
881 (vc-hg--hgignore-filenames nil))
882 (vc-hg--slurp-hgignore-1 hgignore repo)
883 (vc-hg--ignore-patterns-make
884 :repo repo
885 :ignore-patterns (nreverse vc-hg--hgignore-patterns)
886 :file-sources (nreverse vc-hg--hgignore-filenames))))
888 (defun vc-hg--ignore-patterns-valid-p (hgip)
889 "Return whether the cached ignore patterns in HGIP are still valid"
890 (let ((valid t)
891 (file-sources (vc-hg--ignore-patterns-file-sources hgip)))
892 (while (and file-sources valid)
893 (let* ((fs (pop file-sources))
894 (saved-mtime (nth 1 fs))
895 (saved-size (nth 2 fs))
896 (attr (file-attributes (nth 0 fs)))
897 (current-mtime (nth 5 attr))
898 (current-size (nth 7 attr)))
899 (unless (and (equal saved-mtime current-mtime)
900 (equal saved-size current-size))
901 (setf valid nil))))
902 valid))
904 (defun vc-hg--ignore-patterns-ignored-p (hgip filename)
905 "Test whether the ignore pattern set HGIP says to ignore FILENAME.
906 FILENAME must be the file's true absolute name."
907 (let ((patterns (vc-hg--ignore-patterns-ignore-patterns hgip))
908 (inhibit-changing-match-data t)
909 (ignored nil))
910 (while (and patterns (not ignored))
911 (setf ignored (string-match (pop patterns) filename)))
912 ignored))
914 (defun vc-hg--time-to-fixnum (ts)
915 (+ (* 65536 (car ts)) (cadr ts)))
917 (defvar vc-hg--cached-ignore-patterns nil
918 "Cached pre-parsed hg ignore patterns.")
920 (defun vc-hg--file-ignored-p (repo repo-relative-filename)
921 (let ((hgip vc-hg--cached-ignore-patterns))
922 (unless (and hgip
923 (equal repo (vc-hg--ignore-patterns-repo hgip))
924 (vc-hg--ignore-patterns-valid-p hgip))
925 (setf vc-hg--cached-ignore-patterns nil)
926 (setf hgip (vc-hg--slurp-hgignore repo))
927 (setf vc-hg--cached-ignore-patterns hgip))
928 (vc-hg--ignore-patterns-ignored-p
929 hgip
930 (concat repo repo-relative-filename))))
932 (defun vc-hg--read-repo-requirements (repo)
933 (cl-assert (string-match "^/\\(.*/\\)?$" repo))
934 (let* ((requires-filename (concat repo ".hg/requires")))
935 (and (file-exists-p requires-filename)
936 (with-temp-buffer
937 (set-buffer-multibyte nil)
938 (insert-file-contents-literally requires-filename)
939 (split-string (buffer-substring-no-properties
940 (point-min) (point-max)))))))
942 (defconst vc-hg-supported-requirements
943 '("dotencode"
944 "fncache"
945 "generaldelta"
946 "lz4revlog"
947 "remotefilelog"
948 "revlogv1"
949 "store")
950 "List of Mercurial repository requirements we understand; if a
951 repository requires features not present in this list, we avoid
952 attempting to parse Mercurial data structures.")
954 (defun vc-hg--requirements-understood-p (repo)
955 "Check that we understand the format of the given repository.
956 REPO is the directory name of a Mercurial repository."
957 (null (cl-set-difference (vc-hg--read-repo-requirements repo)
958 vc-hg-supported-requirements
959 :test #'equal)))
961 (defvar vc-hg--dirstate-scan-cache nil
962 "Cache of the last result of `vc-hg--raw-dirstate-search'.
963 Avoids the need to repeatedly scan dirstate on repeated calls to
964 `vc-hg-state', as we see during registration queries.")
966 (defun vc-hg--cached-dirstate-search (dirstate dirstate-attr ascii-fname)
967 (let* ((mtime (nth 5 dirstate-attr))
968 (size (nth 7 dirstate-attr))
969 (cache vc-hg--dirstate-scan-cache)
971 (if (and cache
972 (equal dirstate (pop cache))
973 (equal mtime (pop cache))
974 (equal size (pop cache))
975 (equal ascii-fname (pop cache)))
976 (pop cache)
977 (let ((result (vc-hg--raw-dirstate-search dirstate ascii-fname)))
978 (setf vc-hg--dirstate-scan-cache
979 (list dirstate mtime size ascii-fname result))
980 result))))
982 (defun vc-hg-state-fast (filename)
983 "Like `vc-hg-state', but parse internal data structures directly.
984 Returns one of the usual `vc-state' enumeration values or
985 `unsupported' if we need to take the slow path and run the
986 hg binary."
987 (let* (truename
988 repo
989 dirstate
990 dirstate-attr
991 repo-relative-filename
992 ascii-fname)
993 (if (or
994 ;; Explicit user disable
995 (not vc-hg-parse-hg-data-structures)
996 ;; It'll probably be faster to run hg remotely
997 (file-remote-p filename)
998 (progn
999 (setf truename (file-truename filename))
1000 (file-remote-p truename))
1001 (not (setf repo (vc-hg-root truename)))
1002 ;; dirstate must exist
1003 (not (progn
1004 (setf repo (expand-file-name repo))
1005 (cl-assert (string-match "^/\\(.*/\\)?$" repo))
1006 (setf dirstate (concat repo ".hg/dirstate"))
1007 (setf dirstate-attr (file-attributes dirstate))))
1008 ;; Repository must be in an understood format
1009 (not (vc-hg--requirements-understood-p repo))
1010 ;; Dirstate too small to be valid
1011 (< (nth 7 dirstate-attr) 40)
1012 ;; We want to store 32-bit unsigned values in fixnums
1013 (< most-positive-fixnum 4294967295)
1014 (progn
1015 (setf repo-relative-filename
1016 (file-relative-name truename repo))
1017 (setf ascii-fname
1018 (string-as-unibyte
1019 (let (last-coding-system-used)
1020 (encode-coding-string
1021 repo-relative-filename
1022 'us-ascii t))))
1023 ;; We only try dealing with ASCII filenames
1024 (not (equal ascii-fname repo-relative-filename))))
1025 'unsupported
1026 (let* ((dirstate-entry
1027 (vc-hg--cached-dirstate-search
1028 dirstate dirstate-attr ascii-fname))
1029 (state (car dirstate-entry))
1030 (stat (file-attributes
1031 (concat repo repo-relative-filename))))
1032 (cond ((eq state ?r) 'removed)
1033 ((and (not state) stat)
1034 (condition-case nil
1035 (if (vc-hg--file-ignored-p repo repo-relative-filename)
1036 'ignored
1037 'unregistered)
1038 (vc-hg-unsupported-syntax 'unsupported)))
1039 ((and state (not stat)) 'missing)
1040 ((eq state ?n)
1041 (let ((vc-hg-size (nth 2 dirstate-entry))
1042 (vc-hg-mtime (nth 3 dirstate-entry))
1043 (fs-size (nth 7 stat))
1044 (fs-mtime (vc-hg--time-to-fixnum (nth 5 stat))))
1045 (if (and (eql vc-hg-size fs-size) (eql vc-hg-mtime fs-mtime))
1046 'up-to-date
1047 'edited)))
1048 ((eq state ?a) 'added)
1049 (state 'unsupported))))))
1051 ;;; Miscellaneous
1053 (defun vc-hg-previous-revision (_file rev)
1054 ;; We can't simply decrement by 1, because that revision might be
1055 ;; e.g. on a different branch (bug#22032).
1056 (with-temp-buffer
1057 (and (eq 0
1058 (vc-hg-command t nil nil "id" "-n" "-r" (concat rev "^")))
1059 ;; Trim the trailing newline.
1060 (buffer-substring (point-min) (1- (point-max))))))
1062 (defun vc-hg-next-revision (_file rev)
1063 (let ((newrev (1+ (string-to-number rev)))
1064 (tip-revision
1065 (with-temp-buffer
1066 (vc-hg-command t 0 nil "tip" "--style=default")
1067 (goto-char (point-min))
1068 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
1069 (string-to-number (match-string-no-properties 1)))))
1070 ;; We don't want to exceed the maximum possible revision number, ie
1071 ;; the tip revision.
1072 (when (<= newrev tip-revision)
1073 (number-to-string newrev))))
1075 ;; Modeled after the similar function in vc-bzr.el
1076 (defun vc-hg-delete-file (file)
1077 "Delete FILE and delete it in the hg repository."
1078 (condition-case ()
1079 (delete-file file)
1080 (file-error nil))
1081 (vc-hg-command nil 0 file "remove" "--after" "--force"))
1083 ;; Modeled after the similar function in vc-bzr.el
1084 (defun vc-hg-rename-file (old new)
1085 "Rename file from OLD to NEW using `hg mv'."
1086 (vc-hg-command nil 0 new "mv" old))
1088 (defun vc-hg-register (files &optional _comment)
1089 "Register FILES under hg. COMMENT is ignored."
1090 (vc-hg-command nil 0 files "add"))
1092 (defun vc-hg-create-repo ()
1093 "Create a new Mercurial repository."
1094 (vc-hg-command nil 0 nil "init"))
1096 (defalias 'vc-hg-responsible-p 'vc-hg-root)
1098 (defun vc-hg-unregister (file)
1099 "Unregister FILE from hg."
1100 (vc-hg-command nil 0 file "forget"))
1102 (declare-function log-edit-extract-headers "log-edit" (headers string))
1104 (defun vc-hg-checkin (files comment &optional _rev)
1105 "Hg-specific version of `vc-backend-checkin'.
1106 REV is ignored."
1107 (apply 'vc-hg-command nil 0 files
1108 (nconc (list "commit" "-m")
1109 (log-edit-extract-headers '(("Author" . "--user")
1110 ("Date" . "--date"))
1111 comment))))
1113 (defun vc-hg-find-revision (file rev buffer)
1114 (let ((coding-system-for-read 'binary)
1115 (coding-system-for-write 'binary))
1116 (if rev
1117 (vc-hg-command buffer 0 file "cat" "-r" rev)
1118 (vc-hg-command buffer 0 file "cat"))))
1120 (defun vc-hg-find-ignore-file (file)
1121 "Return the root directory of the repository of FILE."
1122 (expand-file-name ".hgignore"
1123 (vc-hg-root file)))
1125 ;; Modeled after the similar function in vc-bzr.el
1126 (defun vc-hg-checkout (file &optional rev)
1127 "Retrieve a revision of FILE.
1128 EDITABLE is ignored.
1129 REV is the revision to check out into WORKFILE."
1130 (let ((coding-system-for-read 'binary)
1131 (coding-system-for-write 'binary))
1132 (with-current-buffer (or (get-file-buffer file) (current-buffer))
1133 (if rev
1134 (vc-hg-command t 0 file "cat" "-r" rev)
1135 (vc-hg-command t 0 file "cat")))))
1137 (defun vc-hg-resolve-when-done ()
1138 "Call \"hg resolve -m\" if the conflict markers have been removed."
1139 (save-excursion
1140 (goto-char (point-min))
1141 (unless (re-search-forward "^<<<<<<< " nil t)
1142 (vc-hg-command nil 0 buffer-file-name "resolve" "-m")
1143 ;; Remove the hook so that it is not called multiple times.
1144 (remove-hook 'after-save-hook 'vc-hg-resolve-when-done t))))
1146 (defun vc-hg-find-file-hook ()
1147 (when (and buffer-file-name
1148 (file-exists-p (concat buffer-file-name ".orig"))
1149 ;; Hg does not seem to have a "conflict" status, eg
1150 ;; hg http://bz.selenic.com/show_bug.cgi?id=2724
1151 (memq (vc-file-getprop buffer-file-name 'vc-state)
1152 '(edited conflict))
1153 ;; Maybe go on to check that "hg resolve -l" says "U"?
1154 ;; If "hg resolve -l" says there's a conflict but there are no
1155 ;; conflict markers, it's not clear what we should do.
1156 (save-excursion
1157 (goto-char (point-min))
1158 (re-search-forward "^<<<<<<< " nil t)))
1159 ;; Hg may not recognize "conflict" as a state, but we can do better.
1160 (vc-file-setprop buffer-file-name 'vc-state 'conflict)
1161 (smerge-start-session)
1162 (add-hook 'after-save-hook 'vc-hg-resolve-when-done nil t)
1163 (vc-message-unresolved-conflicts buffer-file-name)))
1166 ;; Modeled after the similar function in vc-bzr.el
1167 (defun vc-hg-revert (file &optional contents-done)
1168 (unless contents-done
1169 (with-temp-buffer (vc-hg-command t 0 file "revert"))))
1171 ;;; Hg specific functionality.
1173 (defvar vc-hg-extra-menu-map
1174 (let ((map (make-sparse-keymap)))
1175 map))
1177 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
1179 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
1181 (defvar log-view-vc-backend)
1183 (cl-defstruct (vc-hg-extra-fileinfo
1184 (:copier nil)
1185 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
1186 (:conc-name vc-hg-extra-fileinfo->))
1187 rename-state ;; rename or copy state
1188 extra-name) ;; original name for copies and rename targets, new name for
1190 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
1192 (defun vc-hg-dir-printer (info)
1193 "Pretty-printer for the vc-dir-fileinfo structure."
1194 (let ((extra (vc-dir-fileinfo->extra info)))
1195 (vc-default-dir-printer 'Hg info)
1196 (when extra
1197 (insert (propertize
1198 (format " (%s %s)"
1199 (pcase (vc-hg-extra-fileinfo->rename-state extra)
1200 (`copied "copied from")
1201 (`renamed-from "renamed from")
1202 (`renamed-to "renamed to"))
1203 (vc-hg-extra-fileinfo->extra-name extra))
1204 'face 'font-lock-comment-face)))))
1206 (defun vc-hg-after-dir-status (update-function)
1207 (let ((file nil)
1208 (translation '((?= . up-to-date)
1209 (?C . up-to-date)
1210 (?A . added)
1211 (?R . removed)
1212 (?M . edited)
1213 (?I . ignored)
1214 (?! . missing)
1215 (? . copy-rename-line)
1216 (?? . unregistered)))
1217 (translated nil)
1218 (result nil)
1219 (last-added nil)
1220 (last-line-copy nil))
1221 (goto-char (point-min))
1222 (while (not (eobp))
1223 (setq translated (cdr (assoc (char-after) translation)))
1224 (setq file
1225 (buffer-substring-no-properties (+ (point) 2)
1226 (line-end-position)))
1227 (cond ((not translated)
1228 (setq last-line-copy nil))
1229 ((eq translated 'up-to-date)
1230 (setq last-line-copy nil))
1231 ((eq translated 'copy-rename-line)
1232 ;; For copied files the output looks like this:
1233 ;; A COPIED_FILE_NAME
1234 ;; ORIGINAL_FILE_NAME
1235 (setf (nth 2 last-added)
1236 (vc-hg-create-extra-fileinfo 'copied file))
1237 (setq last-line-copy t))
1238 ((and last-line-copy (eq translated 'removed))
1239 ;; For renamed files the output looks like this:
1240 ;; A NEW_FILE_NAME
1241 ;; ORIGINAL_FILE_NAME
1242 ;; R ORIGINAL_FILE_NAME
1243 ;; We need to adjust the previous entry to not think it is a copy.
1244 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
1245 'renamed-from)
1246 (push (list file translated
1247 (vc-hg-create-extra-fileinfo
1248 'renamed-to (nth 0 last-added))) result)
1249 (setq last-line-copy nil))
1251 (setq last-added (list file translated nil))
1252 (push last-added result)
1253 (setq last-line-copy nil)))
1254 (forward-line))
1255 (funcall update-function result)))
1257 ;; Follows vc-hg-command (or vc-do-async-command), which uses vc-do-command
1258 ;; from vc-dispatcher.
1259 (declare-function vc-exec-after "vc-dispatcher" (code))
1260 ;; Follows vc-exec-after.
1261 (declare-function vc-set-async-update "vc-dispatcher" (process-buffer))
1263 (defun vc-hg-dir-status-files (_dir files update-function)
1264 ;; XXX: We can't pass DIR directly to 'hg status' because that
1265 ;; returns all ignored files if FILES is non-nil (bug#22481).
1266 ;; If honoring DIR ever becomes important, try using '-I DIR/'.
1267 (vc-hg-command (current-buffer) 'async files
1268 "status"
1269 (concat "-mardu" (if files "i"))
1270 "-C")
1271 (vc-run-delayed
1272 (vc-hg-after-dir-status update-function)))
1274 (defun vc-hg-dir-extra-header (name &rest commands)
1275 (concat (propertize name 'face 'font-lock-type-face)
1276 (propertize
1277 (with-temp-buffer
1278 (apply 'vc-hg-command (current-buffer) 0 nil commands)
1279 (buffer-substring-no-properties (point-min) (1- (point-max))))
1280 'face 'font-lock-variable-name-face)))
1282 (defun vc-hg-dir-extra-headers (dir)
1283 "Generate extra status headers for a Mercurial tree."
1284 (let ((default-directory dir))
1285 (concat
1286 (vc-hg-dir-extra-header "Root : " "root") "\n"
1287 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
1288 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
1289 ;; these change after each commit
1290 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
1291 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
1294 (defun vc-hg-log-incoming (buffer remote-location)
1295 (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
1296 remote-location)))
1298 (defun vc-hg-log-outgoing (buffer remote-location)
1299 (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
1300 remote-location)))
1302 (defvar vc-hg-error-regexp-alist nil
1303 ;; 'hg pull' does not list modified files, so, for now, the only
1304 ;; benefit of `vc-compilation-mode' is that one can get rid of
1305 ;; *vc-hg* buffer with 'q' or 'z'.
1306 ;; TODO: call 'hg incoming' before pull/merge to get the list of
1307 ;; modified files
1308 "Value of `compilation-error-regexp-alist' in *vc-hg* buffers.")
1310 (autoload 'vc-do-async-command "vc-dispatcher")
1311 (autoload 'log-view-get-marked "log-view")
1313 (defun vc-hg--pushpull (command prompt &optional obsolete)
1314 "Run COMMAND (a string; either push or pull) on the current Hg branch.
1315 If PROMPT is non-nil, prompt for the Hg command to run.
1316 If OBSOLETE is non-nil, behave like the old versions of the Hg push/pull
1317 commands, which only operated on marked files."
1318 (let (marked-list)
1319 ;; The `vc-hg-pull' and `vc-hg-push' commands existed before the
1320 ;; `pull'/`push' VC actions were implemented.
1321 ;; The following is for backwards compatibility.
1322 (if (and obsolete (setq marked-list (log-view-get-marked)))
1323 (apply #'vc-hg-command
1324 nil 0 nil
1325 command
1326 (apply 'nconc
1327 (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
1328 (let* ((root (vc-hg-root default-directory))
1329 (buffer (format "*vc-hg : %s*" (expand-file-name root)))
1330 (hg-program vc-hg-program)
1331 ;; Fixme: before updating the working copy to the latest
1332 ;; state, should check if it's visiting an old revision.
1333 (args (if (equal command "pull") '("-u"))))
1334 ;; If necessary, prompt for the exact command.
1335 ;; TODO if pushing, prompt if no default push location - cf bzr.
1336 (when prompt
1337 (setq args (split-string
1338 (read-shell-command
1339 (format "Hg %s command: " command)
1340 (format "%s %s%s" hg-program command
1341 (if (not args) ""
1342 (concat " " (mapconcat 'identity args " "))))
1343 'vc-hg-history)
1344 " " t))
1345 (setq hg-program (car args)
1346 command (cadr args)
1347 args (cddr args)))
1348 (apply 'vc-do-async-command buffer root hg-program command args)
1349 (with-current-buffer buffer
1350 (vc-run-delayed
1351 (vc-compilation-mode 'hg)
1352 (setq-local compile-command
1353 (concat hg-program " " command " "
1354 (if args (mapconcat 'identity args " ") "")))))
1355 (vc-set-async-update buffer)))))
1357 (defun vc-hg-pull (prompt)
1358 "Issue a Mercurial pull command.
1359 If called interactively with a set of marked Log View buffers,
1360 call \"hg pull -r REVS\" to pull in the specified revisions REVS.
1362 With a prefix argument or if PROMPT is non-nil, prompt for a
1363 specific Mercurial pull command. The default is \"hg pull -u\",
1364 which fetches changesets from the default remote repository and
1365 then attempts to update the working directory."
1366 (interactive "P")
1367 (vc-hg--pushpull "pull" prompt (called-interactively-p 'interactive)))
1369 (defun vc-hg-push (prompt)
1370 "Push changes from the current Mercurial branch.
1371 Normally, this runs \"hg push\". If PROMPT is non-nil, prompt
1372 for the Hg command to run.
1374 If called interactively with a set of marked Log View buffers,
1375 call \"hg push -r REVS\" to push the specified revisions REVS."
1376 (interactive "P")
1377 (vc-hg--pushpull "push" prompt (called-interactively-p 'interactive)))
1379 (defun vc-hg-merge-branch ()
1380 "Merge incoming changes into the current working directory.
1381 This runs the command \"hg merge\"."
1382 (let* ((root (vc-hg-root default-directory))
1383 (buffer (format "*vc-hg : %s*" (expand-file-name root))))
1384 (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
1385 (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'hg)))
1386 (vc-set-async-update buffer)))
1388 ;;; Internal functions
1390 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
1391 "A wrapper around `vc-do-command' for use in vc-hg.el.
1392 This function differs from vc-do-command in that it invokes
1393 `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
1394 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
1395 (if (stringp vc-hg-global-switches)
1396 (cons vc-hg-global-switches flags)
1397 (append vc-hg-global-switches
1398 flags))))
1400 (defun vc-hg-root (file)
1401 (vc-find-root file ".hg"))
1403 (provide 'vc-hg)
1405 ;;; vc-hg.el ends here