Update copyright year to 2015
[emacs.git] / lisp / vc / vc-mtn.el
blobd783572678c1b7a79dc0f6d6dd80ac1150a94808
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>
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 (define-obsolete-variable-alias 'vc-mtn-command 'vc-mtn-program "23.1")
53 (defcustom vc-mtn-program "mtn"
54 "Name of the monotone executable."
55 :type 'string
56 :group 'vc-mtn)
58 ;; Clear up the cache to force vc-call to check again and discover
59 ;; new functions when we reload this file.
60 (put 'Mtn 'vc-functions nil)
62 (unless (executable-find vc-mtn-program)
63 ;; vc-mtn.el is 100% non-functional without the `mtn' executable.
64 (setq vc-handled-backends (delq 'Mtn vc-handled-backends)))
66 ;;;###autoload
67 (defconst vc-mtn-admin-dir "_MTN" "Name of the monotone directory.")
68 ;;;###autoload
69 (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format")
70 "Name of the monotone directory's format file.")
72 ;;;###autoload (defun vc-mtn-registered (file)
73 ;;;###autoload (if (vc-find-root file vc-mtn-admin-format)
74 ;;;###autoload (progn
75 ;;;###autoload (load "vc-mtn" nil t)
76 ;;;###autoload (vc-mtn-registered file))))
78 (defun vc-mtn-revision-granularity () 'repository)
79 (defun vc-mtn-checkout-model (_files) 'implicit)
81 (defun vc-mtn-root (file)
82 (setq file (expand-file-name file)
83 file (if (file-directory-p file)
84 (file-name-as-directory file)
85 (file-name-directory file)))
86 (or (vc-file-getprop file 'vc-mtn-root)
87 (vc-file-setprop file 'vc-mtn-root
88 (vc-find-root file vc-mtn-admin-format))))
90 (defun vc-mtn-find-admin-dir (file)
91 "Return the administrative directory of FILE."
92 (expand-file-name vc-mtn-admin-dir (vc-mtn-root file)))
94 (defun vc-mtn-registered (file)
95 (let ((root (vc-mtn-root file)))
96 (when root
97 (vc-mtn-state file))))
99 (defun vc-mtn-command (buffer okstatus files &rest flags)
100 "A wrapper around `vc-do-command' for use in vc-mtn.el."
101 (let ((process-environment
102 ;; Avoid localization of messages so we can parse the output.
103 (cons "LC_MESSAGES=C" process-environment)))
104 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program
105 files flags)))
107 (defun vc-mtn-state (file)
108 ;; If `mtn' fails or returns status>0, or if the search files, just
109 ;; return nil.
110 (ignore-errors
111 (with-temp-buffer
112 (vc-mtn-command t 0 file "status")
113 (goto-char (point-min))
114 (re-search-forward
115 "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$")
116 (cond ((match-end 1) 'edited)
117 ((match-end 2) 'added)
118 (t 'up-to-date)))))
120 (defun vc-mtn-after-dir-status (update-function)
121 (let (result)
122 (goto-char (point-min))
123 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)" nil t)
124 (while (re-search-forward
125 "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t)
126 (cond ((match-end 1) (push (list (match-string 3) 'edited) result))
127 ((match-end 2) (push (list (match-string 3) 'added) result))))
128 (funcall update-function result)))
130 ;; dir-status-files called from vc-dir, which loads vc,
131 ;; which loads vc-dispatcher.
132 (declare-function vc-exec-after "vc-dispatcher" (code))
134 (defun vc-mtn-dir-status-files (dir _files update-function)
135 (vc-mtn-command (current-buffer) 'async dir "status")
136 (vc-run-delayed
137 (vc-mtn-after-dir-status update-function)))
139 (defun vc-mtn-working-revision (file)
140 ;; If `mtn' fails or returns status>0, or if the search fails, just
141 ;; return nil.
142 (ignore-errors
143 (with-temp-buffer
144 (vc-mtn-command t 0 file "status")
145 (goto-char (point-min))
146 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
147 (match-string 2))))
149 (defun vc-mtn-workfile-branch (file)
150 ;; If `mtn' fails or returns status>0, or if the search files, just
151 ;; return nil.
152 (ignore-errors
153 (with-temp-buffer
154 (vc-mtn-command t 0 file "status")
155 (goto-char (point-min))
156 (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)")
157 (match-string 1))))
159 ;; Mode-line rewrite code copied from vc-arch.el.
161 (defcustom vc-mtn-mode-line-rewrite
162 '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part.
163 "Rewrite rules to shorten Mtn's revision names on the mode-line."
164 :type '(repeat (cons regexp string))
165 :version "22.2"
166 :group 'vc-mtn)
168 (defun vc-mtn-mode-line-string (file)
169 "Return a string for `vc-mode-line' to put in the mode line for FILE."
170 (let ((branch (vc-mtn-workfile-branch file)))
171 (dolist (rule vc-mtn-mode-line-rewrite)
172 (if (string-match (car rule) branch)
173 (setq branch (replace-match (cdr rule) t nil branch))))
174 (format "Mtn%c%s"
175 (pcase (vc-state file)
176 ((or `up-to-date `needs-update) ?-)
177 (`added ?@)
178 (_ ?:))
179 branch)))
181 (defun vc-mtn-register (files &optional _comment)
182 (vc-mtn-command nil 0 files "add"))
184 (defun vc-mtn-responsible-p (file) (vc-mtn-root file))
186 (declare-function log-edit-extract-headers "log-edit" (headers string))
188 (defun vc-mtn-checkin (files comment)
189 (apply 'vc-mtn-command nil 0 files
190 (nconc (list "commit" "-m")
191 (log-edit-extract-headers '(("Author" . "--author")
192 ("Date" . "--date"))
193 comment))))
195 (defun vc-mtn-find-revision (file rev buffer)
196 (vc-mtn-command buffer 0 file "cat" "-r" rev))
198 ;; (defun vc-mtn-checkout (file &optional rev)
199 ;; )
201 (defun vc-mtn-revert (file &optional contents-done)
202 (unless contents-done
203 (vc-mtn-command nil 0 file "revert")))
205 (defun vc-mtn-print-log (files buffer &optional _shortlog start-revision limit)
206 "Print commit logs associated with FILES into specified BUFFER.
207 _SHORTLOG is ignored.
208 If START-REVISION is non-nil, it is the newest revision to show.
209 If LIMIT is non-nil, show no more than this many entries."
210 (apply 'vc-mtn-command buffer 0 files "log"
211 (append
212 (when start-revision (list "--from" (format "%s" start-revision)))
213 (when limit (list "--last" (format "%s" limit))))))
215 (defvar log-view-message-re)
216 (defvar log-view-file-re)
217 (defvar log-view-font-lock-keywords)
218 (defvar log-view-per-file-logs)
220 (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View"
221 ;; Don't match anything.
222 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
223 (set (make-local-variable 'log-view-per-file-logs) nil)
224 ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives
225 ;; in the ChangeLog text.
226 (set (make-local-variable 'log-view-message-re)
227 "^[ |/]+Revision: \\([0-9a-f]+\\)")
228 (require 'add-log) ;For change-log faces.
229 (set (make-local-variable 'log-view-font-lock-keywords)
230 (append log-view-font-lock-keywords
231 '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email))
232 ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face))))))
234 ;; (defun vc-mtn-show-log-entry (revision)
235 ;; )
237 (autoload 'vc-switches "vc")
239 (defun vc-mtn-diff (files &optional rev1 rev2 buffer async)
240 "Get a difference report using monotone between two revisions of FILES."
241 (apply 'vc-mtn-command (or buffer "*vc-diff*")
242 (if async 'async 1)
243 files "diff"
244 (append
245 (vc-switches 'mtn 'diff)
246 (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2)))))
248 (defun vc-mtn-annotate-command (file buf &optional rev)
249 (apply 'vc-mtn-command buf 'async file "annotate"
250 (if rev (list "-r" rev))))
252 (declare-function vc-annotate-convert-time "vc-annotate" (time))
254 (defconst vc-mtn-annotate-full-re
255 "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ")
256 (defconst vc-mtn-annotate-any-re
257 (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)"))
259 (defun vc-mtn-annotate-time ()
260 (when (looking-at vc-mtn-annotate-any-re)
261 (goto-char (match-end 0))
262 (let ((year (match-string 2)))
263 (if (not year)
264 ;; Look for the date on a previous line.
265 (save-excursion
266 (get-text-property (1- (previous-single-property-change
267 (point) 'vc-mtn-time nil (point-min)))
268 'vc-mtn-time))
269 (let ((time (vc-annotate-convert-time
270 (encode-time 0 0 0
271 (string-to-number (match-string 4))
272 (string-to-number (match-string 3))
273 (string-to-number year)
274 t))))
275 (let ((inhibit-read-only t)
276 (inhibit-modification-hooks t))
277 (put-text-property (match-beginning 0) (match-end 0)
278 'vc-mtn-time time))
279 time)))))
281 (defun vc-mtn-annotate-extract-revision-at-line ()
282 (save-excursion
283 (when (or (looking-at vc-mtn-annotate-full-re)
284 (re-search-backward vc-mtn-annotate-full-re nil t))
285 (match-string 1))))
287 ;;; Revision completion.
289 (defun vc-mtn-list-tags ()
290 (with-temp-buffer
291 (vc-mtn-command t 0 nil "list" "tags")
292 (goto-char (point-min))
293 (let ((tags ()))
294 (while (re-search-forward "^[^ ]+" nil t)
295 (push (match-string 0) tags))
296 tags)))
298 (defun vc-mtn-list-branches ()
299 (with-temp-buffer
300 (vc-mtn-command t 0 nil "list" "branches")
301 (goto-char (point-min))
302 (let ((branches ()))
303 (while (re-search-forward "^.+" nil t)
304 (push (match-string 0) branches))
305 branches)))
307 (defun vc-mtn-list-revision-ids (prefix)
308 (with-temp-buffer
309 (vc-mtn-command t 0 nil "complete" "revision" prefix)
310 (goto-char (point-min))
311 (let ((ids ()))
312 (while (re-search-forward "^.+" nil t)
313 (push (match-string 0) ids))
314 ids)))
316 (defun vc-mtn-revision-completion-table (_files)
317 ;; What about using `files'?!? --Stef
318 (lambda (string pred action)
319 (cond
320 ;; Special chars for composite selectors.
321 ((string-match ".*[^\\]\\(\\\\\\\\\\)*[/|;(]" string)
322 (completion-table-with-context (substring string 0 (match-end 0))
323 (vc-mtn-revision-completion-table nil)
324 (substring string (match-end 0))
325 pred action))
326 ;; "Tag" selectors.
327 ((string-match "\\`t:" string)
328 (complete-with-action action
329 (mapcar (lambda (tag) (concat "t:" tag))
330 (vc-mtn-list-tags))
331 string pred))
332 ;; "Branch" or "Head" selectors.
333 ((string-match "\\`[hb]:" string)
334 (let ((prefix (match-string 0 string)))
335 (complete-with-action action
336 (mapcar (lambda (tag) (concat prefix tag))
337 (vc-mtn-list-branches))
338 string pred)))
339 ;; "ID" selectors.
340 ((string-match "\\`i:" string)
341 (complete-with-action action
342 (mapcar (lambda (tag) (concat "i:" tag))
343 (vc-mtn-list-revision-ids
344 (substring string (match-end 0))))
345 string pred))
347 (complete-with-action action
348 '("t:" "b:" "h:" "i:"
349 ;; Completion not implemented for these.
350 "c:" "a:" "k:" "d:" "m:" "e:" "l:" "i:" "p:"
351 ;; These have no arg to complete.
352 "u:" "w:"
353 ;; Selector functions.
354 "difference(" "lca(" "max(" "ancestors("
355 "descendants(" "parents(" "children("
356 "pick(")
357 string pred)))))
361 (provide 'vc-mtn)
363 ;;; vc-mtn.el ends here