]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-mtn.el
3b59c61eb782bbe0e194c99c50be5aebd06392b7
[gnu-emacs] / lisp / vc / vc-mtn.el
1 ;;; vc-mtn.el --- VC backend for Monotone -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: vc
7 ;; Package: vc
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;;
27
28 ;;; TODO:
29
30 ;; - The `previous-version' VC method needs to be supported, 'D' in
31 ;; log-view-mode uses it.
32
33 ;;; Code:
34
35 (eval-when-compile (require 'vc))
36
37 (defgroup vc-mtn nil
38 "VC Monotone (mtn) backend."
39 :version "24.1"
40 :group 'vc)
41
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)
51
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)
62
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)
68
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)
72
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)))
76
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.")
82
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))))
88
89 (defun vc-mtn-revision-granularity () 'repository)
90 (defun vc-mtn-checkout-model (_files) 'implicit)
91
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))))
100
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)))
104
105 (defun vc-mtn-registered (file)
106 (let ((root (vc-mtn-root file)))
107 (when root
108 (vc-mtn-state file))))
109
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
116 files flags)))
117
118 (defun vc-mtn-state (file)
119 ;; If `mtn' fails or returns status>0, or if the search files, just
120 ;; return nil.
121 (ignore-errors
122 (with-temp-buffer
123 (vc-mtn-command t 0 file "status")
124 (goto-char (point-min))
125 (re-search-forward
126 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
127 (cond ((match-end 1) 'edited)
128 ((match-end 2) 'added)
129 (t 'up-to-date)))))
130
131 (defun vc-mtn-after-dir-status (update-function)
132 (let (result)
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)))
140
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))
144
145 (defun vc-mtn-dir-status-files (dir _files update-function)
146 (vc-mtn-command (current-buffer) 'async dir "status")
147 (vc-run-delayed
148 (vc-mtn-after-dir-status update-function)))
149
150 (defun vc-mtn-working-revision (file)
151 ;; If `mtn' fails or returns status>0, or if the search fails, just
152 ;; return nil.
153 (ignore-errors
154 (with-temp-buffer
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 \\(.*\\)")
158 (match-string 2))))
159
160 (defun vc-mtn-workfile-branch (file)
161 ;; If `mtn' fails or returns status>0, or if the search files, just
162 ;; return nil.
163 (ignore-errors
164 (with-temp-buffer
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 \\(.*\\)")
168 (match-string 1))))
169
170 ;; Mode-line rewrite code copied from vc-arch.el.
171
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))
176 :version "22.2"
177 :group 'vc-mtn)
178
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)))
182 (if branch
183 (progn
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))))
187 (format "Mtn%c%s"
188 (pcase (vc-state file)
189 ((or `up-to-date `needs-update) ?-)
190 (`added ?@)
191 (_ ?:))
192 branch))
193 "")))
194
195 (defun vc-mtn-register (files &optional _comment)
196 (vc-mtn-command nil 0 files "add"))
197
198 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
199
200 (declare-function log-edit-extract-headers "log-edit" (headers string))
201
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")
206 ("Date" . "--date"))
207 comment))))
208
209 (defun vc-mtn-find-revision (file rev buffer)
210 ;; null rev means latest revision
211 (if rev
212 (vc-mtn-command buffer 0 file "cat" "-r" rev)
213 (vc-mtn-command buffer 0 file "cat")))
214
215 ;; (defun vc-mtn-checkout (file &optional rev)
216 ;; )
217
218 (defun vc-mtn-revert (file &optional contents-done)
219 (unless contents-done
220 (vc-mtn-command nil 0 file "revert")))
221
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"
228 (append
229 (when start-revision (list "--from" (format "%s" start-revision)))
230 (when limit (list "--last" (format "%s" limit))))))
231
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)
236
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))))))
250
251 ;; (defun vc-mtn-show-log-entry (revision)
252 ;; )
253
254 (autoload 'vc-switches "vc")
255
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*")
259 1 ; bug#21969
260 files "diff"
261 (append
262 (vc-switches 'mtn 'diff)
263 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
264
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)))))
269
270 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
271
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 "\\)"))
276
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)))
281 (if (not year)
282 ;; Look for the date on a previous line.
283 (save-excursion
284 (get-text-property (1- (previous-single-property-change
285 (point) 'vc-mtn-time nil (point-min)))
286 'vc-mtn-time))
287 (let ((time (vc-annotate-convert-time
288 (encode-time 0 0 0
289 (string-to-number (match-string 4))
290 (string-to-number (match-string 3))
291 (string-to-number year)
292 t))))
293 (let ((inhibit-read-only t)
294 (inhibit-modification-hooks t))
295 (put-text-property (match-beginning 0) (match-end 0)
296 'vc-mtn-time time))
297 time)))))
298
299 (defun vc-mtn-annotate-extract-revision-at-line ()
300 (save-excursion
301 (when (or (looking-at vc-mtn-annotate-full-re)
302 (re-search-backward vc-mtn-annotate-full-re nil t))
303 (match-string 1))))
304
305 ;;; Revision completion.
306
307 (defun vc-mtn-list-tags ()
308 (with-temp-buffer
309 (vc-mtn-command t 0 nil "list" "tags")
310 (goto-char (point-min))
311 (let ((tags ()))
312 (while (re-search-forward "^[^ ]+" nil t)
313 (push (match-string 0) tags))
314 tags)))
315
316 (defun vc-mtn-list-branches ()
317 (with-temp-buffer
318 (vc-mtn-command t 0 nil "list" "branches")
319 (goto-char (point-min))
320 (let ((branches ()))
321 (while (re-search-forward "^.+" nil t)
322 (push (match-string 0) branches))
323 branches)))
324
325 (defun vc-mtn-list-revision-ids (prefix)
326 (with-temp-buffer
327 (vc-mtn-command t 0 nil "complete" "revision" prefix)
328 (goto-char (point-min))
329 (let ((ids ()))
330 (while (re-search-forward "^.+" nil t)
331 (push (match-string 0) ids))
332 ids)))
333
334 (defun vc-mtn-revision-completion-table (_files)
335 ;; What about using `files'?!? --Stef
336 (lambda (string pred action)
337 (cond
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))
343 pred action))
344 ;; "Tag" selectors.
345 ((string-match "\\`t:" string)
346 (complete-with-action action
347 (mapcar (lambda (tag) (concat "t:" tag))
348 (vc-mtn-list-tags))
349 string pred))
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))
356 string pred)))
357 ;; "ID" selectors.
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))))
363 string pred))
364 (t
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.
370 "u:" "w:"
371 ;; Selector functions.
372 "difference(" "lca(" "max(" "ancestors("
373 "descendants(" "parents(" "children("
374 "pick(")
375 string pred)))))
376
377
378
379 (provide 'vc-mtn)
380
381 ;;; vc-mtn.el ends here