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