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