]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-mtn.el
Merge branch 'map'
[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 (dolist (rule vc-mtn-mode-line-rewrite)
183 (if (string-match (car rule) branch)
184 (setq branch (replace-match (cdr rule) t nil branch))))
185 (format "Mtn%c%s"
186 (pcase (vc-state file)
187 ((or `up-to-date `needs-update) ?-)
188 (`added ?@)
189 (_ ?:))
190 branch)))
191
192 (defun vc-mtn-register (files &optional _comment)
193 (vc-mtn-command nil 0 files "add"))
194
195 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
196
197 (declare-function log-edit-extract-headers "log-edit" (headers string))
198
199 (defun vc-mtn-checkin (files comment)
200 (apply 'vc-mtn-command nil 0 files
201 (nconc (list "commit" "-m")
202 (log-edit-extract-headers '(("Author" . "--author")
203 ("Date" . "--date"))
204 comment))))
205
206 (defun vc-mtn-find-revision (file rev buffer)
207 (vc-mtn-command buffer 0 file "cat" "-r" rev))
208
209 ;; (defun vc-mtn-checkout (file &optional rev)
210 ;; )
211
212 (defun vc-mtn-revert (file &optional contents-done)
213 (unless contents-done
214 (vc-mtn-command nil 0 file "revert")))
215
216 (defun vc-mtn-print-log (files buffer &optional _shortlog start-revision limit)
217 "Print commit logs associated with FILES into specified BUFFER.
218 _SHORTLOG is ignored.
219 If START-REVISION is non-nil, it is the newest revision to show.
220 If LIMIT is non-nil, show no more than this many entries."
221 (apply 'vc-mtn-command buffer 0 files "log"
222 (append
223 (when start-revision (list "--from" (format "%s" start-revision)))
224 (when limit (list "--last" (format "%s" limit))))))
225
226 (defvar log-view-message-re)
227 (defvar log-view-file-re)
228 (defvar log-view-font-lock-keywords)
229 (defvar log-view-per-file-logs)
230
231 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
232 ;; Don't match anything.
233 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
234 (set (make-local-variable 'log-view-per-file-logs) nil)
235 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
236 ;; in the ChangeLog text.
237 (set (make-local-variable 'log-view-message-re)
238 "^[ |/]+Revision: \\([0-9a-f]+\\)")
239 (require 'add-log) ;For change-log faces.
240 (set (make-local-variable 'log-view-font-lock-keywords)
241 (append log-view-font-lock-keywords
242 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
243 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
244
245 ;; (defun vc-mtn-show-log-entry (revision)
246 ;; )
247
248 (autoload 'vc-switches "vc")
249
250 (defun vc-mtn-diff (files &optional rev1 rev2 buffer async)
251 "Get a difference report using monotone between two revisions of FILES."
252 (apply 'vc-mtn-command (or buffer "*vc-diff*")
253 (if async 'async 1)
254 files "diff"
255 (append
256 (vc-switches 'mtn 'diff)
257 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
258
259 (defun vc-mtn-annotate-command (file buf &optional rev)
260 (apply #'vc-mtn-command buf 'async file "annotate"
261 (append (vc-switches 'mtn 'annotate)
262 (if rev (list "-r" rev)))))
263
264 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
265
266 (defconst vc-mtn-annotate-full-re
267 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
268 (defconst vc-mtn-annotate-any-re
269 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
270
271 (defun vc-mtn-annotate-time ()
272 (when (looking-at vc-mtn-annotate-any-re)
273 (goto-char (match-end 0))
274 (let ((year (match-string 2)))
275 (if (not year)
276 ;; Look for the date on a previous line.
277 (save-excursion
278 (get-text-property (1- (previous-single-property-change
279 (point) 'vc-mtn-time nil (point-min)))
280 'vc-mtn-time))
281 (let ((time (vc-annotate-convert-time
282 (encode-time 0 0 0
283 (string-to-number (match-string 4))
284 (string-to-number (match-string 3))
285 (string-to-number year)
286 t))))
287 (let ((inhibit-read-only t)
288 (inhibit-modification-hooks t))
289 (put-text-property (match-beginning 0) (match-end 0)
290 'vc-mtn-time time))
291 time)))))
292
293 (defun vc-mtn-annotate-extract-revision-at-line ()
294 (save-excursion
295 (when (or (looking-at vc-mtn-annotate-full-re)
296 (re-search-backward vc-mtn-annotate-full-re nil t))
297 (match-string 1))))
298
299 ;;; Revision completion.
300
301 (defun vc-mtn-list-tags ()
302 (with-temp-buffer
303 (vc-mtn-command t 0 nil "list" "tags")
304 (goto-char (point-min))
305 (let ((tags ()))
306 (while (re-search-forward "^[^ ]+" nil t)
307 (push (match-string 0) tags))
308 tags)))
309
310 (defun vc-mtn-list-branches ()
311 (with-temp-buffer
312 (vc-mtn-command t 0 nil "list" "branches")
313 (goto-char (point-min))
314 (let ((branches ()))
315 (while (re-search-forward "^.+" nil t)
316 (push (match-string 0) branches))
317 branches)))
318
319 (defun vc-mtn-list-revision-ids (prefix)
320 (with-temp-buffer
321 (vc-mtn-command t 0 nil "complete" "revision" prefix)
322 (goto-char (point-min))
323 (let ((ids ()))
324 (while (re-search-forward "^.+" nil t)
325 (push (match-string 0) ids))
326 ids)))
327
328 (defun vc-mtn-revision-completion-table (_files)
329 ;; What about using `files'?!? --Stef
330 (lambda (string pred action)
331 (cond
332 ;; Special chars for composite selectors.
333 ((string-match ".*[^\\]\\(\\\\\\\\\\)*[/|;(]" string)
334 (completion-table-with-context (substring string 0 (match-end 0))
335 (vc-mtn-revision-completion-table nil)
336 (substring string (match-end 0))
337 pred action))
338 ;; "Tag" selectors.
339 ((string-match "\\`t:" string)
340 (complete-with-action action
341 (mapcar (lambda (tag) (concat "t:" tag))
342 (vc-mtn-list-tags))
343 string pred))
344 ;; "Branch" or "Head" selectors.
345 ((string-match "\\`[hb]:" string)
346 (let ((prefix (match-string 0 string)))
347 (complete-with-action action
348 (mapcar (lambda (tag) (concat prefix tag))
349 (vc-mtn-list-branches))
350 string pred)))
351 ;; "ID" selectors.
352 ((string-match "\\`i:" string)
353 (complete-with-action action
354 (mapcar (lambda (tag) (concat "i:" tag))
355 (vc-mtn-list-revision-ids
356 (substring string (match-end 0))))
357 string pred))
358 (t
359 (complete-with-action action
360 '("t:" "b:" "h:" "i:"
361 ;; Completion not implemented for these.
362 "c:" "a:" "k:" "d:" "m:" "e:" "l:" "i:" "p:"
363 ;; These have no arg to complete.
364 "u:" "w:"
365 ;; Selector functions.
366 "difference(" "lca(" "max(" "ancestors("
367 "descendants(" "parents(" "children("
368 "pick(")
369 string pred)))))
370
371
372
373 (provide 'vc-mtn)
374
375 ;;; vc-mtn.el ends here