1 ;;; vc-mtn.el --- VC backend for Monotone -*- lexical-binding: t -*-
3 ;; Copyright (C) 2007-2016 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-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
)))
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
122 (defun vc-mtn-state (file)
123 ;; If `mtn' fails or returns status>0, or if the search files, just
127 (vc-mtn-command t
0 file
"status")
128 (goto-char (point-min))
130 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
131 (cond ((match-end 1) 'edited
)
132 ((match-end 2) 'added
)
135 (defun vc-mtn-after-dir-status (update-function)
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")
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
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 \\(.*\\)")
164 (defun vc-mtn-workfile-branch (file)
165 ;; If `mtn' fails or returns status>0, or if the search files, just
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 \\(.*\\)")
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
))
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
)))
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
))))
192 (pcase (vc-state file
)
193 ((or `up-to-date
`needs-update
) ?-
)
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")
213 (defun vc-mtn-find-revision (file rev buffer
)
214 ;; null rev means latest revision
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)
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"
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)
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*")
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)))
286 ;; Look for the date on a previous line.
288 (get-text-property (1- (previous-single-property-change
289 (point) 'vc-mtn-time nil
(point-min)))
291 (let ((time (vc-annotate-convert-time
293 (string-to-number (match-string 4))
294 (string-to-number (match-string 3))
295 (string-to-number year
)
297 (let ((inhibit-read-only t
)
298 (inhibit-modification-hooks t
))
299 (put-text-property (match-beginning 0) (match-end 0)
303 (defun vc-mtn-annotate-extract-revision-at-line ()
305 (when (or (looking-at vc-mtn-annotate-full-re
)
306 (re-search-backward vc-mtn-annotate-full-re nil t
))
309 ;;; Revision completion.
311 (defun vc-mtn-list-tags ()
313 (vc-mtn-command t
0 nil
"list" "tags")
314 (goto-char (point-min))
316 (while (re-search-forward "^[^ ]+" nil t
)
317 (push (match-string 0) tags
))
320 (defun vc-mtn-list-branches ()
322 (vc-mtn-command t
0 nil
"list" "branches")
323 (goto-char (point-min))
325 (while (re-search-forward "^.+" nil t
)
326 (push (match-string 0) branches
))
329 (defun vc-mtn-list-revision-ids (prefix)
331 (vc-mtn-command t
0 nil
"complete" "revision" prefix
)
332 (goto-char (point-min))
334 (while (re-search-forward "^.+" nil t
)
335 (push (match-string 0) ids
))
338 (defun vc-mtn-revision-completion-table (_files)
339 ;; What about using `files'?!? --Stef
340 (lambda (string pred action
)
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))
349 ((string-match "\\`t:" string
)
350 (complete-with-action action
351 (mapcar (lambda (tag) (concat "t:" tag
))
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))
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))))
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.
375 ;; Selector functions.
376 "difference(" "lca(" "max(" "ancestors("
377 "descendants(" "parents(" "children("
385 ;;; vc-mtn.el ends here