]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-hg.el
Use ‘T *restrict’ proto, not ‘T[restrict]’
[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) NOT NEEDED
52 ;; STATE-CHANGING FUNCTIONS
53 ;; * register (files &optional rev comment) OK
54 ;; * create-repo () OK
55 ;; - responsible-p (file) OK
56 ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
57 ;; - unregister (file) OK
58 ;; * checkin (files rev comment) OK
59 ;; * find-revision (file rev buffer) OK
60 ;; * checkout (file &optional rev) OK
61 ;; * revert (file &optional contents-done) OK
62 ;; - merge (file rev1 rev2) NEEDED
63 ;; - merge-news (file) NEEDED
64 ;; - steal-lock (file &optional revision) NOT NEEDED
65 ;; HISTORY FUNCTIONS
66 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
67 ;; - log-view-mode () OK
68 ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
69 ;; - comment-history (file) NOT NEEDED
70 ;; - update-changelog (files) NOT NEEDED
71 ;; * diff (files &optional rev1 rev2 buffer) OK
72 ;; - revision-completion-table (files) OK?
73 ;; - annotate-command (file buf &optional rev) OK
74 ;; - annotate-time () OK
75 ;; - annotate-current-time () NOT NEEDED
76 ;; - annotate-extract-revision-at-line () OK
77 ;; TAG SYSTEM
78 ;; - create-tag (dir name branchp) OK
79 ;; - retrieve-tag (dir name update) OK FIXME UPDATE BUFFERS
80 ;; MISCELLANEOUS
81 ;; - make-version-backups-p (file) ??
82 ;; - previous-revision (file rev) OK
83 ;; - next-revision (file rev) OK
84 ;; - check-headers () ??
85 ;; - delete-file (file) TEST IT
86 ;; - rename-file (old new) OK
87 ;; - find-file-hook () added for bug#10709
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 (setq file (expand-file-name file))
201 (let*
202 ((status nil)
203 (default-directory (file-name-directory file))
204 (out
205 (with-output-to-string
206 (with-current-buffer
207 standard-output
208 (setq status
209 (condition-case nil
210 ;; Ignore all errors.
211 (let ((process-environment
212 ;; Avoid localization of messages so we
213 ;; can parse the output. Disable pager.
214 (append
215 (list "TERM=dumb" "LANGUAGE=C" "HGPLAIN=1")
216 process-environment)))
217 (process-file
218 vc-hg-program nil t nil
219 "--config" "alias.status=status"
220 "--config" "defaults.status="
221 "status" "-A" (file-relative-name file)))
222 ;; Some problem happened. E.g. We can't find an `hg'
223 ;; executable.
224 (error nil)))))))
225 (when (and (eq 0 status)
226 (> (length out) 0)
227 (null (string-match ".*: No such file or directory$" out)))
228 (let ((state (aref out 0)))
229 (cond
230 ((eq state ?=) 'up-to-date)
231 ((eq state ?A) 'added)
232 ((eq state ?M) 'edited)
233 ((eq state ?I) 'ignored)
234 ((eq state ?R) 'removed)
235 ((eq state ?!) 'missing)
236 ((eq state ??) 'unregistered)
237 ((eq state ?C) 'up-to-date) ;; Older mercurial versions use this.
238 (t 'up-to-date))))))
239
240 (defun vc-hg-working-revision (file)
241 "Hg-specific version of `vc-working-revision'."
242 (or (ignore-errors
243 (with-output-to-string
244 (vc-hg-command standard-output 0 file
245 "parent" "--template" "{rev}")))
246 "0"))
247
248 ;;; History functions
249
250 (defcustom vc-hg-log-switches nil
251 "String or list of strings specifying switches for hg log under VC."
252 :type '(choice (const :tag "None" nil)
253 (string :tag "Argument String")
254 (repeat :tag "Argument List" :value ("") string))
255 :group 'vc-hg)
256
257 (autoload 'vc-setup-buffer "vc-dispatcher")
258
259 (defvar vc-hg-log-graph nil
260 "If non-nil, use `--graph' in the short log output.")
261
262 (defvar vc-hg-log-format (concat "changeset: {rev}:{node|short}\n"
263 "{tags % 'tag: {tag}\n'}"
264 "{if(parents, 'parents: {parents}\n')}"
265 "user: {author}\n"
266 "Date: {date|date}\n"
267 "summary: {desc|tabindent}\n\n")
268 "Mercurial log template for `vc-hg-print-log' long format.")
269
270 (defun vc-hg-print-log (files buffer &optional shortlog start-revision limit)
271 "Print commit log associated with FILES into specified BUFFER.
272 If SHORTLOG is non-nil, use a short format based on `vc-hg-root-log-format'.
273 If START-REVISION is non-nil, it is the newest revision to show.
274 If LIMIT is non-nil, show no more than this many entries."
275 ;; `vc-do-command' creates the buffer, but we need it before running
276 ;; the command.
277 (vc-setup-buffer buffer)
278 ;; If the buffer exists from a previous invocation it might be
279 ;; read-only.
280 (let ((inhibit-read-only t))
281 (with-current-buffer
282 buffer
283 (apply 'vc-hg-command buffer 'async files "log"
284 (nconc
285 (when start-revision (list (format "-r%s:0" start-revision)))
286 (when limit (list "-l" (format "%s" limit)))
287 (if shortlog
288 `(,@(if vc-hg-log-graph '("--graph"))
289 "--template"
290 ,(car vc-hg-root-log-format))
291 `("--template" ,vc-hg-log-format))
292 vc-hg-log-switches)))))
293
294 (defvar log-view-message-re)
295 (defvar log-view-file-re)
296 (defvar log-view-font-lock-keywords)
297 (defvar log-view-per-file-logs)
298 (defvar log-view-expanded-log-entry-function)
299
300 (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
301 (require 'add-log) ;; we need the add-log faces
302 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
303 (set (make-local-variable 'log-view-per-file-logs) nil)
304 (set (make-local-variable 'log-view-message-re)
305 (if (eq vc-log-view-type 'short)
306 (cadr vc-hg-root-log-format)
307 "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
308 (set (make-local-variable 'tab-width) 2)
309 ;; Allow expanding short log entries
310 (when (eq vc-log-view-type 'short)
311 (setq truncate-lines t)
312 (set (make-local-variable 'log-view-expanded-log-entry-function)
313 'vc-hg-expanded-log-entry))
314 (set (make-local-variable 'log-view-font-lock-keywords)
315 (if (eq vc-log-view-type 'short)
316 (list (cons (nth 1 vc-hg-root-log-format)
317 (nth 2 vc-hg-root-log-format)))
318 (append
319 log-view-font-lock-keywords
320 '(
321 ;; Handle the case:
322 ;; user: FirstName LastName <foo@bar>
323 ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
324 (1 'change-log-name)
325 (2 'change-log-email))
326 ;; Handle the cases:
327 ;; user: foo@bar
328 ;; and
329 ;; user: foo
330 ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
331 (1 'change-log-email))
332 ("^date: \\(.+\\)" (1 'change-log-date))
333 ("^tag: +\\([^ ]+\\)$" (1 'highlight))
334 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
335
336 (autoload 'vc-switches "vc")
337
338 (defun vc-hg-diff (files &optional oldvers newvers buffer _async)
339 "Get a difference report using hg between two revisions of FILES."
340 (let* ((firstfile (car files))
341 (working (and firstfile (vc-working-revision firstfile))))
342 (when (and (equal oldvers working) (not newvers))
343 (setq oldvers nil))
344 (when (and (not oldvers) newvers)
345 (setq oldvers working))
346 (apply #'vc-hg-command
347 (or buffer "*vc-diff*")
348 nil ; bug#21969
349 files "diff"
350 (append
351 (vc-switches 'hg 'diff)
352 (when oldvers
353 (if newvers
354 (list "-r" oldvers "-r" newvers)
355 (list "-r" oldvers)))))))
356
357 (defun vc-hg-expanded-log-entry (revision)
358 (with-temp-buffer
359 (vc-hg-command t nil nil "log" "-r" revision "--template" vc-hg-log-format)
360 (goto-char (point-min))
361 (unless (eobp)
362 ;; Indent the expanded log entry.
363 (indent-region (point-min) (point-max) 2)
364 (goto-char (point-max))
365 (buffer-string))))
366
367 (defun vc-hg-revision-table (files)
368 (let ((default-directory (file-name-directory (car files))))
369 (with-temp-buffer
370 (vc-hg-command t nil files "log" "--template" "{rev} ")
371 (split-string
372 (buffer-substring-no-properties (point-min) (point-max))))))
373
374 ;; Modeled after the similar function in vc-cvs.el
375 (defun vc-hg-revision-completion-table (files)
376 (letrec ((table (lazy-completion-table
377 table (lambda () (vc-hg-revision-table files)))))
378 table))
379
380 (defun vc-hg-annotate-command (file buffer &optional revision)
381 "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
382 Optional arg REVISION is a revision to annotate from."
383 (apply #'vc-hg-command buffer 0 file "annotate" "-dq" "-n"
384 (append (vc-switches 'hg 'annotate)
385 (if revision (list (concat "-r" revision))))))
386
387 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
388
389 ;; One line printed by "hg annotate -dq -n -u --follow" looks like this:
390 ;; b56girard 114590 2012-03-13 CLOBBER: Lorem ipsum dolor sit
391 ;; i.e. AUTHOR REVISION DATE FILENAME: CONTENTS
392 ;; The user can omit options "-u" and/or "--follow". Then it'll look like:
393 ;; 114590 2012-03-13 CLOBBER:
394 ;; or
395 ;; b56girard 114590 2012-03-13:
396 (defconst vc-hg-annotate-re
397 (concat
398 "^\\(?: *[^ ]+ +\\)?\\([0-9]+\\) " ;User and revision.
399 "\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)" ;Date.
400 "\\(?: +\\([^:]+\\)\\)?:")) ;Filename.
401
402 (defun vc-hg-annotate-time ()
403 (when (looking-at vc-hg-annotate-re)
404 (goto-char (match-end 0))
405 (vc-annotate-convert-time
406 (let ((str (match-string-no-properties 2)))
407 (encode-time 0 0 0
408 (string-to-number (substring str 6 8))
409 (string-to-number (substring str 4 6))
410 (string-to-number (substring str 0 4)))))))
411
412 (defun vc-hg-annotate-extract-revision-at-line ()
413 (save-excursion
414 (beginning-of-line)
415 (when (looking-at vc-hg-annotate-re)
416 (if (match-beginning 3)
417 (cons (match-string-no-properties 1)
418 (expand-file-name (match-string-no-properties 3)
419 (vc-hg-root default-directory)))
420 (match-string-no-properties 1)))))
421
422 ;;; Tag system
423
424 (defun vc-hg-create-tag (dir name branchp)
425 "Attach the tag NAME to the state of the working copy."
426 (let ((default-directory dir))
427 (and (vc-hg-command nil 0 nil "status")
428 (vc-hg-command nil 0 nil (if branchp "bookmark" "tag") name))))
429
430 (defun vc-hg-retrieve-tag (dir name _update)
431 "Retrieve the version tagged by NAME of all registered files at or below DIR."
432 (let ((default-directory dir))
433 (vc-hg-command nil 0 nil "update" name)
434 ;; FIXME: update buffers if `update' is true
435 ;; TODO: update *vc-change-log* buffer so can see @ if --graph
436 ))
437
438 ;;; Miscellaneous
439
440 (defun vc-hg-previous-revision (_file rev)
441 ;; We can't simply decrement by 1, because that revision might be
442 ;; e.g. on a different branch (bug#22032).
443 (with-temp-buffer
444 (and (eq 0
445 (vc-hg-command t nil nil "id" "-n" "-r" (concat rev "^")))
446 ;; Trim the trailing newline.
447 (buffer-substring (point-min) (1- (point-max))))))
448
449 (defun vc-hg-next-revision (_file rev)
450 (let ((newrev (1+ (string-to-number rev)))
451 (tip-revision
452 (with-temp-buffer
453 (vc-hg-command t 0 nil "tip" "--style=default")
454 (goto-char (point-min))
455 (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
456 (string-to-number (match-string-no-properties 1)))))
457 ;; We don't want to exceed the maximum possible revision number, ie
458 ;; the tip revision.
459 (when (<= newrev tip-revision)
460 (number-to-string newrev))))
461
462 ;; Modeled after the similar function in vc-bzr.el
463 (defun vc-hg-delete-file (file)
464 "Delete FILE and delete it in the hg repository."
465 (condition-case ()
466 (delete-file file)
467 (file-error nil))
468 (vc-hg-command nil 0 file "remove" "--after" "--force"))
469
470 ;; Modeled after the similar function in vc-bzr.el
471 (defun vc-hg-rename-file (old new)
472 "Rename file from OLD to NEW using `hg mv'."
473 (vc-hg-command nil 0 new "mv" old))
474
475 (defun vc-hg-register (files &optional _comment)
476 "Register FILES under hg. COMMENT is ignored."
477 (vc-hg-command nil 0 files "add"))
478
479 (defun vc-hg-create-repo ()
480 "Create a new Mercurial repository."
481 (vc-hg-command nil 0 nil "init"))
482
483 (defalias 'vc-hg-responsible-p 'vc-hg-root)
484
485 (defun vc-hg-unregister (file)
486 "Unregister FILE from hg."
487 (vc-hg-command nil 0 file "forget"))
488
489 (declare-function log-edit-extract-headers "log-edit" (headers string))
490
491 (defun vc-hg-checkin (files comment &optional _rev)
492 "Hg-specific version of `vc-backend-checkin'.
493 REV is ignored."
494 (apply 'vc-hg-command nil 0 files
495 (nconc (list "commit" "-m")
496 (log-edit-extract-headers '(("Author" . "--user")
497 ("Date" . "--date"))
498 comment))))
499
500 (defun vc-hg-find-revision (file rev buffer)
501 (let ((coding-system-for-read 'binary)
502 (coding-system-for-write 'binary))
503 (if rev
504 (vc-hg-command buffer 0 file "cat" "-r" rev)
505 (vc-hg-command buffer 0 file "cat"))))
506
507 (defun vc-hg-find-ignore-file (file)
508 "Return the root directory of the repository of FILE."
509 (expand-file-name ".hgignore"
510 (vc-hg-root file)))
511
512 ;; Modeled after the similar function in vc-bzr.el
513 (defun vc-hg-checkout (file &optional rev)
514 "Retrieve a revision of FILE.
515 EDITABLE is ignored.
516 REV is the revision to check out into WORKFILE."
517 (let ((coding-system-for-read 'binary)
518 (coding-system-for-write 'binary))
519 (with-current-buffer (or (get-file-buffer file) (current-buffer))
520 (if rev
521 (vc-hg-command t 0 file "cat" "-r" rev)
522 (vc-hg-command t 0 file "cat")))))
523
524 (defun vc-hg-resolve-when-done ()
525 "Call \"hg resolve -m\" if the conflict markers have been removed."
526 (save-excursion
527 (goto-char (point-min))
528 (unless (re-search-forward "^<<<<<<< " nil t)
529 (vc-hg-command nil 0 buffer-file-name "resolve" "-m")
530 ;; Remove the hook so that it is not called multiple times.
531 (remove-hook 'after-save-hook 'vc-hg-resolve-when-done t))))
532
533 (defun vc-hg-find-file-hook ()
534 (when (and buffer-file-name
535 (file-exists-p (concat buffer-file-name ".orig"))
536 ;; Hg does not seem to have a "conflict" status, eg
537 ;; hg http://bz.selenic.com/show_bug.cgi?id=2724
538 (memq (vc-file-getprop buffer-file-name 'vc-state)
539 '(edited conflict))
540 ;; Maybe go on to check that "hg resolve -l" says "U"?
541 ;; If "hg resolve -l" says there's a conflict but there are no
542 ;; conflict markers, it's not clear what we should do.
543 (save-excursion
544 (goto-char (point-min))
545 (re-search-forward "^<<<<<<< " nil t)))
546 ;; Hg may not recognize "conflict" as a state, but we can do better.
547 (vc-file-setprop buffer-file-name 'vc-state 'conflict)
548 (smerge-start-session)
549 (add-hook 'after-save-hook 'vc-hg-resolve-when-done nil t)
550 (vc-message-unresolved-conflicts buffer-file-name)))
551
552
553 ;; Modeled after the similar function in vc-bzr.el
554 (defun vc-hg-revert (file &optional contents-done)
555 (unless contents-done
556 (with-temp-buffer (vc-hg-command t 0 file "revert"))))
557
558 ;;; Hg specific functionality.
559
560 (defvar vc-hg-extra-menu-map
561 (let ((map (make-sparse-keymap)))
562 map))
563
564 (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
565
566 (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
567
568 (defvar log-view-vc-backend)
569
570 (cl-defstruct (vc-hg-extra-fileinfo
571 (:copier nil)
572 (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
573 (:conc-name vc-hg-extra-fileinfo->))
574 rename-state ;; rename or copy state
575 extra-name) ;; original name for copies and rename targets, new name for
576
577 (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
578
579 (defun vc-hg-dir-printer (info)
580 "Pretty-printer for the vc-dir-fileinfo structure."
581 (let ((extra (vc-dir-fileinfo->extra info)))
582 (vc-default-dir-printer 'Hg info)
583 (when extra
584 (insert (propertize
585 (format " (%s %s)"
586 (pcase (vc-hg-extra-fileinfo->rename-state extra)
587 (`copied "copied from")
588 (`renamed-from "renamed from")
589 (`renamed-to "renamed to"))
590 (vc-hg-extra-fileinfo->extra-name extra))
591 'face 'font-lock-comment-face)))))
592
593 (defun vc-hg-after-dir-status (update-function)
594 (let ((file nil)
595 (translation '((?= . up-to-date)
596 (?C . up-to-date)
597 (?A . added)
598 (?R . removed)
599 (?M . edited)
600 (?I . ignored)
601 (?! . missing)
602 (? . copy-rename-line)
603 (?? . unregistered)))
604 (translated nil)
605 (result nil)
606 (last-added nil)
607 (last-line-copy nil))
608 (goto-char (point-min))
609 (while (not (eobp))
610 (setq translated (cdr (assoc (char-after) translation)))
611 (setq file
612 (buffer-substring-no-properties (+ (point) 2)
613 (line-end-position)))
614 (cond ((not translated)
615 (setq last-line-copy nil))
616 ((eq translated 'up-to-date)
617 (setq last-line-copy nil))
618 ((eq translated 'copy-rename-line)
619 ;; For copied files the output looks like this:
620 ;; A COPIED_FILE_NAME
621 ;; ORIGINAL_FILE_NAME
622 (setf (nth 2 last-added)
623 (vc-hg-create-extra-fileinfo 'copied file))
624 (setq last-line-copy t))
625 ((and last-line-copy (eq translated 'removed))
626 ;; For renamed files the output looks like this:
627 ;; A NEW_FILE_NAME
628 ;; ORIGINAL_FILE_NAME
629 ;; R ORIGINAL_FILE_NAME
630 ;; We need to adjust the previous entry to not think it is a copy.
631 (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
632 'renamed-from)
633 (push (list file translated
634 (vc-hg-create-extra-fileinfo
635 'renamed-to (nth 0 last-added))) result)
636 (setq last-line-copy nil))
637 (t
638 (setq last-added (list file translated nil))
639 (push last-added result)
640 (setq last-line-copy nil)))
641 (forward-line))
642 (funcall update-function result)))
643
644 ;; Follows vc-hg-command (or vc-do-async-command), which uses vc-do-command
645 ;; from vc-dispatcher.
646 (declare-function vc-exec-after "vc-dispatcher" (code))
647 ;; Follows vc-exec-after.
648 (declare-function vc-set-async-update "vc-dispatcher" (process-buffer))
649
650 (defun vc-hg-dir-status-files (_dir files update-function)
651 ;; XXX: We can't pass DIR directly to 'hg status' because that
652 ;; returns all ignored files if FILES is non-nil (bug#22481).
653 ;; If honoring DIR ever becomes important, try using '-I DIR/'.
654 (vc-hg-command (current-buffer) 'async files
655 "status"
656 (concat "-mardu" (if files "i"))
657 "-C")
658 (vc-run-delayed
659 (vc-hg-after-dir-status update-function)))
660
661 (defun vc-hg-dir-extra-header (name &rest commands)
662 (concat (propertize name 'face 'font-lock-type-face)
663 (propertize
664 (with-temp-buffer
665 (apply 'vc-hg-command (current-buffer) 0 nil commands)
666 (buffer-substring-no-properties (point-min) (1- (point-max))))
667 'face 'font-lock-variable-name-face)))
668
669 (defun vc-hg-dir-extra-headers (dir)
670 "Generate extra status headers for a Mercurial tree."
671 (let ((default-directory dir))
672 (concat
673 (vc-hg-dir-extra-header "Root : " "root") "\n"
674 (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
675 (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
676 ;; these change after each commit
677 ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
678 ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
679 )))
680
681 (defun vc-hg-log-incoming (buffer remote-location)
682 (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
683 remote-location)))
684
685 (defun vc-hg-log-outgoing (buffer remote-location)
686 (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
687 remote-location)))
688
689 (defvar vc-hg-error-regexp-alist nil
690 ;; 'hg pull' does not list modified files, so, for now, the only
691 ;; benefit of `vc-compilation-mode' is that one can get rid of
692 ;; *vc-hg* buffer with 'q' or 'z'.
693 ;; TODO: call 'hg incoming' before pull/merge to get the list of
694 ;; modified files
695 "Value of `compilation-error-regexp-alist' in *vc-hg* buffers.")
696
697 (autoload 'vc-do-async-command "vc-dispatcher")
698 (autoload 'log-view-get-marked "log-view")
699
700 (defun vc-hg--pushpull (command prompt &optional obsolete)
701 "Run COMMAND (a string; either push or pull) on the current Hg branch.
702 If PROMPT is non-nil, prompt for the Hg command to run.
703 If OBSOLETE is non-nil, behave like the old versions of the Hg push/pull
704 commands, which only operated on marked files."
705 (let (marked-list)
706 ;; The `vc-hg-pull' and `vc-hg-push' commands existed before the
707 ;; `pull'/`push' VC actions were implemented.
708 ;; The following is for backwards compatibility.
709 (if (and obsolete (setq marked-list (log-view-get-marked)))
710 (apply #'vc-hg-command
711 nil 0 nil
712 command
713 (apply 'nconc
714 (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
715 (let* ((root (vc-hg-root default-directory))
716 (buffer (format "*vc-hg : %s*" (expand-file-name root)))
717 (hg-program vc-hg-program)
718 ;; Fixme: before updating the working copy to the latest
719 ;; state, should check if it's visiting an old revision.
720 (args (if (equal command "pull") '("-u"))))
721 ;; If necessary, prompt for the exact command.
722 ;; TODO if pushing, prompt if no default push location - cf bzr.
723 (when prompt
724 (setq args (split-string
725 (read-shell-command
726 (format "Hg %s command: " command)
727 (format "%s %s%s" hg-program command
728 (if (not args) ""
729 (concat " " (mapconcat 'identity args " "))))
730 'vc-hg-history)
731 " " t))
732 (setq hg-program (car args)
733 command (cadr args)
734 args (cddr args)))
735 (apply 'vc-do-async-command buffer root hg-program command args)
736 (with-current-buffer buffer
737 (vc-run-delayed (vc-compilation-mode 'hg)))
738 (vc-set-async-update buffer)))))
739
740 (defun vc-hg-pull (prompt)
741 "Issue a Mercurial pull command.
742 If called interactively with a set of marked Log View buffers,
743 call \"hg pull -r REVS\" to pull in the specified revisions REVS.
744
745 With a prefix argument or if PROMPT is non-nil, prompt for a
746 specific Mercurial pull command. The default is \"hg pull -u\",
747 which fetches changesets from the default remote repository and
748 then attempts to update the working directory."
749 (interactive "P")
750 (vc-hg--pushpull "pull" prompt (called-interactively-p 'interactive)))
751
752 (defun vc-hg-push (prompt)
753 "Push changes from the current Mercurial branch.
754 Normally, this runs \"hg push\". If PROMPT is non-nil, prompt
755 for the Hg command to run.
756
757 If called interactively with a set of marked Log View buffers,
758 call \"hg push -r REVS\" to push the specified revisions REVS."
759 (interactive "P")
760 (vc-hg--pushpull "push" prompt (called-interactively-p 'interactive)))
761
762 (defun vc-hg-merge-branch ()
763 "Merge incoming changes into the current working directory.
764 This runs the command \"hg merge\"."
765 (let* ((root (vc-hg-root default-directory))
766 (buffer (format "*vc-hg : %s*" (expand-file-name root))))
767 (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
768 (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'hg)))
769 (vc-set-async-update buffer)))
770
771 ;;; Internal functions
772
773 (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
774 "A wrapper around `vc-do-command' for use in vc-hg.el.
775 This function differs from vc-do-command in that it invokes
776 `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
777 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
778 (if (stringp vc-hg-global-switches)
779 (cons vc-hg-global-switches flags)
780 (append vc-hg-global-switches
781 flags))))
782
783 (defun vc-hg-root (file)
784 (vc-find-root file ".hg"))
785
786 (provide 'vc-hg)
787
788 ;;; vc-hg.el ends here