1 ;;; vc-mtn.el --- VC backend for Monotone -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
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/>.
30 ;; - The `previous-version' VC method needs to be supported, 'D' in
31 ;; log-view-mode uses it.
35 (eval-when-compile (require 'vc))
38 "VC Monotone (mtn) backend."
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)
47 (string :tag "Argument String")
48 (repeat :tag "Argument List" :value ("") string))
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
56 :type '(choice (const :tag "Unspecified" nil)
58 (string :tag "Argument String")
59 (repeat :tag "Argument List" :value ("") string))
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."
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)))
78 (defconst vc-mtn-admin-dir "_MTN" "Name of the monotone directory.")
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)
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-registered (file)
106 (let ((root (vc-mtn-root file)))
108 (vc-mtn-state file))))
110 (defun vc-mtn-command (buffer okstatus files &rest flags)
111 "A wrapper around `vc-do-command' for use in vc-mtn.el."
112 (let ((process-environment
113 ;; Avoid localization of messages so we can parse the output.
114 (cons "LC_MESSAGES=C" process-environment)))
115 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
118 (defun vc-mtn-state (file)
119 ;; If `mtn' fails or returns status>0, or if the search files, just
123 (vc-mtn-command t 0 file "status")
124 (goto-char (point-min))
126 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
127 (cond ((match-end 1) 'edited)
128 ((match-end 2) 'added)
131 (defun vc-mtn-after-dir-status (update-function)
133 (goto-char (point-min))
134 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)" nil t)
135 (while (re-search-forward
136 "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t)
137 (cond ((match-end 1) (push (list (match-string 3) 'edited) result))
138 ((match-end 2) (push (list (match-string 3) 'added) result))))
139 (funcall update-function result)))
141 ;; dir-status-files called from vc-dir, which loads vc,
142 ;; which loads vc-dispatcher.
143 (declare-function vc-exec-after "vc-dispatcher" (code))
145 (defun vc-mtn-dir-status-files (dir _files update-function)
146 (vc-mtn-command (current-buffer) 'async dir "status")
148 (vc-mtn-after-dir-status update-function)))
150 (defun vc-mtn-working-revision (file)
151 ;; If `mtn' fails or returns status>0, or if the search fails, just
155 (vc-mtn-command t 0 file "status")
156 (goto-char (point-min))
157 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
160 (defun vc-mtn-workfile-branch (file)
161 ;; If `mtn' fails or returns status>0, or if the search files, just
165 (vc-mtn-command t 0 file "status")
166 (goto-char (point-min))
167 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
170 ;; Mode-line rewrite code copied from vc-arch.el.
172 (defcustom vc-mtn-mode-line-rewrite
173 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
174 "Rewrite rules to shorten Mtn's revision names on the mode-line."
175 :type '(repeat (cons regexp string))
179 (defun vc-mtn-mode-line-string (file)
180 "Return a string for `vc-mode-line' to put in the mode line for FILE."
181 (let ((branch (vc-mtn-workfile-branch file)))
184 (dolist (rule vc-mtn-mode-line-rewrite)
185 (if (string-match (car rule) branch)
186 (setq branch (replace-match (cdr rule) t nil branch))))
188 (pcase (vc-state file)
189 ((or `up-to-date `needs-update) ?-)
195 (defun vc-mtn-register (files &optional _comment)
196 (vc-mtn-command nil 0 files "add"))
198 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
200 (declare-function log-edit-extract-headers "log-edit" (headers string))
202 (defun vc-mtn-checkin (files comment &optional _rev)
203 (apply 'vc-mtn-command nil 0 files
204 (nconc (list "commit" "-m")
205 (log-edit-extract-headers '(("Author" . "--author")
209 (defun vc-mtn-find-revision (file rev buffer)
210 ;; null rev means latest revision
212 (vc-mtn-command buffer 0 file "cat" "-r" rev)
213 (vc-mtn-command buffer 0 file "cat")))
215 ;; (defun vc-mtn-checkout (file &optional rev)
218 (defun vc-mtn-revert (file &optional contents-done)
219 (unless contents-done
220 (vc-mtn-command nil 0 file "revert")))
222 (defun vc-mtn-print-log (files buffer &optional _shortlog start-revision limit)
223 "Print commit logs associated with FILES into specified BUFFER.
224 _SHORTLOG is ignored.
225 If START-REVISION is non-nil, it is the newest revision to show.
226 If LIMIT is non-nil, show no more than this many entries."
227 (apply 'vc-mtn-command buffer 0 files "log"
229 (when start-revision (list "--from" (format "%s" start-revision)))
230 (when limit (list "--last" (format "%s" limit))))))
232 (defvar log-view-message-re)
233 (defvar log-view-file-re)
234 (defvar log-view-font-lock-keywords)
235 (defvar log-view-per-file-logs)
237 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
238 ;; Don't match anything.
239 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
240 (set (make-local-variable 'log-view-per-file-logs) nil)
241 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
242 ;; in the ChangeLog text.
243 (set (make-local-variable 'log-view-message-re)
244 "^[ |/]+Revision: \\([0-9a-f]+\\)")
245 (require 'add-log) ;For change-log faces.
246 (set (make-local-variable 'log-view-font-lock-keywords)
247 (append log-view-font-lock-keywords
248 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
249 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
251 ;; (defun vc-mtn-show-log-entry (revision)
254 (autoload 'vc-switches "vc")
256 (defun vc-mtn-diff (files &optional rev1 rev2 buffer _async)
257 "Get a difference report using monotone between two revisions of FILES."
258 (apply 'vc-mtn-command (or buffer "*vc-diff*")
262 (vc-switches 'mtn 'diff)
263 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
265 (defun vc-mtn-annotate-command (file buf &optional rev)
266 (apply #'vc-mtn-command buf 'async file "annotate"
267 (append (vc-switches 'mtn 'annotate)
268 (if rev (list "-r" rev)))))
270 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
272 (defconst vc-mtn-annotate-full-re
273 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
274 (defconst vc-mtn-annotate-any-re
275 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
277 (defun vc-mtn-annotate-time ()
278 (when (looking-at vc-mtn-annotate-any-re)
279 (goto-char (match-end 0))
280 (let ((year (match-string 2)))
282 ;; Look for the date on a previous line.
284 (get-text-property (1- (previous-single-property-change
285 (point) 'vc-mtn-time nil (point-min)))
287 (let ((time (vc-annotate-convert-time
289 (string-to-number (match-string 4))
290 (string-to-number (match-string 3))
291 (string-to-number year)
293 (let ((inhibit-read-only t)
294 (inhibit-modification-hooks t))
295 (put-text-property (match-beginning 0) (match-end 0)
299 (defun vc-mtn-annotate-extract-revision-at-line ()
301 (when (or (looking-at vc-mtn-annotate-full-re)
302 (re-search-backward vc-mtn-annotate-full-re nil t))
305 ;;; Revision completion.
307 (defun vc-mtn-list-tags ()
309 (vc-mtn-command t 0 nil "list" "tags")
310 (goto-char (point-min))
312 (while (re-search-forward "^[^ ]+" nil t)
313 (push (match-string 0) tags))
316 (defun vc-mtn-list-branches ()
318 (vc-mtn-command t 0 nil "list" "branches")
319 (goto-char (point-min))
321 (while (re-search-forward "^.+" nil t)
322 (push (match-string 0) branches))
325 (defun vc-mtn-list-revision-ids (prefix)
327 (vc-mtn-command t 0 nil "complete" "revision" prefix)
328 (goto-char (point-min))
330 (while (re-search-forward "^.+" nil t)
331 (push (match-string 0) ids))
334 (defun vc-mtn-revision-completion-table (_files)
335 ;; What about using `files'?!? --Stef
336 (lambda (string pred action)
338 ;; Special chars for composite selectors.
339 ((string-match ".*[^\\]\\(\\\\\\\\\\)*[/|;(]" string)
340 (completion-table-with-context (substring string 0 (match-end 0))
341 (vc-mtn-revision-completion-table nil)
342 (substring string (match-end 0))
345 ((string-match "\\`t:" string)
346 (complete-with-action action
347 (mapcar (lambda (tag) (concat "t:" tag))
350 ;; "Branch" or "Head" selectors.
351 ((string-match "\\`[hb]:" string)
352 (let ((prefix (match-string 0 string)))
353 (complete-with-action action
354 (mapcar (lambda (tag) (concat prefix tag))
355 (vc-mtn-list-branches))
358 ((string-match "\\`i:" string)
359 (complete-with-action action
360 (mapcar (lambda (tag) (concat "i:" tag))
361 (vc-mtn-list-revision-ids
362 (substring string (match-end 0))))
365 (complete-with-action action
366 '("t:" "b:" "h:" "i:"
367 ;; Completion not implemented for these.
368 "c:" "a:" "k:" "d:" "m:" "e:" "l:" "i:" "p:"
369 ;; These have no arg to complete.
371 ;; Selector functions.
372 "difference(" "lca(" "max(" "ancestors("
373 "descendants(" "parents(" "children("
381 ;;; vc-mtn.el ends here