Merge branch 'master' into comment-cache
[emacs.git] / lisp / vc / vc-mtn.el
blob0b832aaf390ee578584608bf2d01b1f4fa445846
1 ;;; vc-mtn.el --- VC backend for Monotone -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: vc
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
28 ;;; TODO:
30 ;; - The `previous-version' VC method needs to be supported, 'D' in
31 ;; log-view-mode uses it.
33 ;;; Code:
35 (eval-when-compile (require 'vc))
37 (defgroup vc-mtn nil
38 "VC Monotone (mtn) backend."
39 :version "24.1"
40 :group 'vc)
42 (defcustom vc-mtn-diff-switches t
43 "String or list of strings specifying switches for monotone diff under VC.
44 If nil, use the value of `vc-diff-switches'. If t, use no switches."
45 :type '(choice (const :tag "Unspecified" nil)
46 (const :tag "None" t)
47 (string :tag "Argument String")
48 (repeat :tag "Argument List" :value ("") string))
49 :version "23.1"
50 :group 'vc-mtn)
52 (defcustom vc-mtn-annotate-switches nil
53 "String or list of strings specifying switches for mtn annotate under VC.
54 If nil, use the value of `vc-annotate-switches'. If t, use no
55 switches."
56 :type '(choice (const :tag "Unspecified" nil)
57 (const :tag "None" t)
58 (string :tag "Argument String")
59 (repeat :tag "Argument List" :value ("") string))
60 :version "25.1"
61 :group 'vc-mtn)
63 (define-obsolete-variable-alias 'vc-mtn-command 'vc-mtn-program "23.1")
64 (defcustom vc-mtn-program "mtn"
65 "Name of the monotone executable."
66 :type 'string
67 :group 'vc-mtn)
69 ;; Clear up the cache to force vc-call to check again and discover
70 ;; new functions when we reload this file.
71 (put 'Mtn 'vc-functions nil)
73 (unless (executable-find vc-mtn-program)
74 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
75 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
77 ;;;###autoload
78 (defconst vc-mtn-admin-dir "_MTN" "Name of the monotone directory.")
79 ;;;###autoload
80 (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format")
81 "Name of the monotone directory's format file.")
83 ;;;###autoload (defun vc-mtn-registered (file)
84 ;;;###autoload (if (vc-find-root file vc-mtn-admin-format)
85 ;;;###autoload (progn
86 ;;;###autoload (load "vc-mtn" nil t)
87 ;;;###autoload (vc-mtn-registered file))))
89 (defun vc-mtn-revision-granularity () 'repository)
90 (defun vc-mtn-checkout-model (_files) 'implicit)
92 (defun vc-mtn-root (file)
93 (setq file (expand-file-name file)
94 file (if (file-directory-p file)
95 (file-name-as-directory file)
96 (file-name-directory file)))
97 (or (vc-file-getprop file 'vc-mtn-root)
98 (vc-file-setprop file 'vc-mtn-root
99 (vc-find-root file vc-mtn-admin-format))))
101 (defun vc-mtn-find-admin-dir (file)
102 "Return the administrative directory of FILE."
103 (expand-file-name vc-mtn-admin-dir (vc-mtn-root file)))
105 (defun vc-mtn-find-ignore-file (file)
106 "Return the mtn ignore file that controls FILE."
107 (expand-file-name ".mtnignore" (vc-mtn-root file)))
109 (defun vc-mtn-registered (file)
110 (let ((root (vc-mtn-root file)))
111 (when root
112 (vc-mtn-state file))))
114 (defun vc-mtn-command (buffer okstatus files &rest flags)
115 "A wrapper around `vc-do-command' for use in vc-mtn.el."
116 (let ((process-environment
117 ;; Avoid localization of messages so we can parse the output.
118 (cons "LC_MESSAGES=C" process-environment)))
119 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
120 files flags)))
122 (defun vc-mtn-state (file)
123 ;; If `mtn' fails or returns status>0, or if the search files, just
124 ;; return nil.
125 (ignore-errors
126 (with-temp-buffer
127 (vc-mtn-command t 0 file "status")
128 (goto-char (point-min))
129 (re-search-forward
130 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
131 (cond ((match-end 1) 'edited)
132 ((match-end 2) 'added)
133 (t 'up-to-date)))))
135 (defun vc-mtn-after-dir-status (update-function)
136 (let (result)
137 (goto-char (point-min))
138 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)" nil t)
139 (while (re-search-forward
140 "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t)
141 (cond ((match-end 1) (push (list (match-string 3) 'edited) result))
142 ((match-end 2) (push (list (match-string 3) 'added) result))))
143 (funcall update-function result)))
145 ;; dir-status-files called from vc-dir, which loads vc,
146 ;; which loads vc-dispatcher.
147 (declare-function vc-exec-after "vc-dispatcher" (code))
149 (defun vc-mtn-dir-status-files (dir _files update-function)
150 (vc-mtn-command (current-buffer) 'async dir "status")
151 (vc-run-delayed
152 (vc-mtn-after-dir-status update-function)))
154 (defun vc-mtn-working-revision (file)
155 ;; If `mtn' fails or returns status>0, or if the search fails, just
156 ;; return nil.
157 (ignore-errors
158 (with-temp-buffer
159 (vc-mtn-command t 0 file "status")
160 (goto-char (point-min))
161 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
162 (match-string 2))))
164 (defun vc-mtn-workfile-branch (file)
165 ;; If `mtn' fails or returns status>0, or if the search files, just
166 ;; return nil.
167 (ignore-errors
168 (with-temp-buffer
169 (vc-mtn-command t 0 file "status")
170 (goto-char (point-min))
171 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
172 (match-string 1))))
174 ;; Mode-line rewrite code copied from vc-arch.el.
176 (defcustom vc-mtn-mode-line-rewrite
177 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
178 "Rewrite rules to shorten Mtn's revision names on the mode-line."
179 :type '(repeat (cons regexp string))
180 :version "22.2"
181 :group 'vc-mtn)
183 (defun vc-mtn-mode-line-string (file)
184 "Return a string for `vc-mode-line' to put in the mode line for FILE."
185 (let ((branch (vc-mtn-workfile-branch file)))
186 (if branch
187 (progn
188 (dolist (rule vc-mtn-mode-line-rewrite)
189 (if (string-match (car rule) branch)
190 (setq branch (replace-match (cdr rule) t nil branch))))
191 (format "Mtn%c%s"
192 (pcase (vc-state file)
193 ((or `up-to-date `needs-update) ?-)
194 (`added ?@)
195 (_ ?:))
196 branch))
197 "")))
199 (defun vc-mtn-register (files &optional _comment)
200 (vc-mtn-command nil 0 files "add"))
202 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
204 (declare-function log-edit-extract-headers "log-edit" (headers string))
206 (defun vc-mtn-checkin (files comment &optional _rev)
207 (apply 'vc-mtn-command nil 0 files
208 (nconc (list "commit" "-m")
209 (log-edit-extract-headers '(("Author" . "--author")
210 ("Date" . "--date"))
211 comment))))
213 (defun vc-mtn-find-revision (file rev buffer)
214 ;; null rev means latest revision
215 (if rev
216 (vc-mtn-command buffer 0 file "cat" "-r" rev)
217 (vc-mtn-command buffer 0 file "cat")))
219 ;; (defun vc-mtn-checkout (file &optional rev)
220 ;; )
222 (defun vc-mtn-revert (file &optional contents-done)
223 (unless contents-done
224 (vc-mtn-command nil 0 file "revert")))
226 (defun vc-mtn-print-log (files buffer &optional _shortlog start-revision limit)
227 "Print commit logs associated with FILES into specified BUFFER.
228 _SHORTLOG is ignored.
229 If START-REVISION is non-nil, it is the newest revision to show.
230 If LIMIT is non-nil, show no more than this many entries."
231 (apply 'vc-mtn-command buffer 0 files "log"
232 (append
233 (when start-revision (list "--from" (format "%s" start-revision)))
234 (when limit (list "--last" (format "%s" limit))))))
236 (defvar log-view-message-re)
237 (defvar log-view-file-re)
238 (defvar log-view-font-lock-keywords)
239 (defvar log-view-per-file-logs)
241 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
242 ;; Don't match anything.
243 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
244 (set (make-local-variable 'log-view-per-file-logs) nil)
245 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
246 ;; in the ChangeLog text.
247 (set (make-local-variable 'log-view-message-re)
248 "^[ |/]+Revision: \\([0-9a-f]+\\)")
249 (require 'add-log) ;For change-log faces.
250 (set (make-local-variable 'log-view-font-lock-keywords)
251 (append log-view-font-lock-keywords
252 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
253 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
255 ;; (defun vc-mtn-show-log-entry (revision)
256 ;; )
258 (autoload 'vc-switches "vc")
260 (defun vc-mtn-diff (files &optional rev1 rev2 buffer _async)
261 "Get a difference report using monotone between two revisions of FILES."
262 (apply 'vc-mtn-command (or buffer "*vc-diff*")
263 1 ; bug#21969
264 files "diff"
265 (append
266 (vc-switches 'mtn 'diff)
267 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
269 (defun vc-mtn-annotate-command (file buf &optional rev)
270 (apply #'vc-mtn-command buf 'async file "annotate"
271 (append (vc-switches 'mtn 'annotate)
272 (if rev (list "-r" rev)))))
274 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
276 (defconst vc-mtn-annotate-full-re
277 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
278 (defconst vc-mtn-annotate-any-re
279 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
281 (defun vc-mtn-annotate-time ()
282 (when (looking-at vc-mtn-annotate-any-re)
283 (goto-char (match-end 0))
284 (let ((year (match-string 2)))
285 (if (not year)
286 ;; Look for the date on a previous line.
287 (save-excursion
288 (get-text-property (1- (previous-single-property-change
289 (point) 'vc-mtn-time nil (point-min)))
290 'vc-mtn-time))
291 (let ((time (vc-annotate-convert-time
292 (encode-time 0 0 0
293 (string-to-number (match-string 4))
294 (string-to-number (match-string 3))
295 (string-to-number year)
296 t))))
297 (let ((inhibit-read-only t)
298 (inhibit-modification-hooks t))
299 (put-text-property (match-beginning 0) (match-end 0)
300 'vc-mtn-time time))
301 time)))))
303 (defun vc-mtn-annotate-extract-revision-at-line ()
304 (save-excursion
305 (when (or (looking-at vc-mtn-annotate-full-re)
306 (re-search-backward vc-mtn-annotate-full-re nil t))
307 (match-string 1))))
309 ;;; Revision completion.
311 (defun vc-mtn-list-tags ()
312 (with-temp-buffer
313 (vc-mtn-command t 0 nil "list" "tags")
314 (goto-char (point-min))
315 (let ((tags ()))
316 (while (re-search-forward "^[^ ]+" nil t)
317 (push (match-string 0) tags))
318 tags)))
320 (defun vc-mtn-list-branches ()
321 (with-temp-buffer
322 (vc-mtn-command t 0 nil "list" "branches")
323 (goto-char (point-min))
324 (let ((branches ()))
325 (while (re-search-forward "^.+" nil t)
326 (push (match-string 0) branches))
327 branches)))
329 (defun vc-mtn-list-revision-ids (prefix)
330 (with-temp-buffer
331 (vc-mtn-command t 0 nil "complete" "revision" prefix)
332 (goto-char (point-min))
333 (let ((ids ()))
334 (while (re-search-forward "^.+" nil t)
335 (push (match-string 0) ids))
336 ids)))
338 (defun vc-mtn-revision-completion-table (_files)
339 ;; What about using `files'?!? --Stef
340 (lambda (string pred action)
341 (cond
342 ;; Special chars for composite selectors.
343 ((string-match ".*[^\\]\\(\\\\\\\\\\)*[/|;(]" string)
344 (completion-table-with-context (substring string 0 (match-end 0))
345 (vc-mtn-revision-completion-table nil)
346 (substring string (match-end 0))
347 pred action))
348 ;; "Tag" selectors.
349 ((string-match "\\`t:" string)
350 (complete-with-action action
351 (mapcar (lambda (tag) (concat "t:" tag))
352 (vc-mtn-list-tags))
353 string pred))
354 ;; "Branch" or "Head" selectors.
355 ((string-match "\\`[hb]:" string)
356 (let ((prefix (match-string 0 string)))
357 (complete-with-action action
358 (mapcar (lambda (tag) (concat prefix tag))
359 (vc-mtn-list-branches))
360 string pred)))
361 ;; "ID" selectors.
362 ((string-match "\\`i:" string)
363 (complete-with-action action
364 (mapcar (lambda (tag) (concat "i:" tag))
365 (vc-mtn-list-revision-ids
366 (substring string (match-end 0))))
367 string pred))
369 (complete-with-action action
370 '("t:" "b:" "h:" "i:"
371 ;; Completion not implemented for these.
372 "c:" "a:" "k:" "d:" "m:" "e:" "l:" "i:" "p:"
373 ;; These have no arg to complete.
374 "u:" "w:"
375 ;; Selector functions.
376 "difference(" "lca(" "max(" "ancestors("
377 "descendants(" "parents(" "children("
378 "pick(")
379 string pred)))))
383 (provide 'vc-mtn)
385 ;;; vc-mtn.el ends here