Commit | Line | Data |
---|---|---|
b1dc6d44 SM |
1 | ;;; vc-mtn.el --- VC backend for Monotone |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2007-2011 Free Software Foundation, Inc. |
b1dc6d44 SM |
4 | |
5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> | |
9766adfb | 6 | ;; Keywords: vc |
bd78fa1d | 7 | ;; Package: vc |
b1dc6d44 | 8 | |
eb3fa2cf GM |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
b1dc6d44 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b1dc6d44 | 15 | |
eb3fa2cf | 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
b1dc6d44 SM |
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 | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b1dc6d44 SM |
23 | |
24 | ;;; Commentary: | |
25 | ||
f64ab8fb | 26 | ;; |
b1dc6d44 | 27 | |
469ca403 DN |
28 | ;;; TODO: |
29 | ||
30 | ;; - The `previous-version' VC method needs to be supported, 'D' in | |
31 | ;; log-view-mode uses it. | |
32 | ||
b1dc6d44 SM |
33 | ;;; Code: |
34 | ||
35 | (eval-when-compile (require 'cl) (require 'vc)) | |
36 | ||
a857238c GM |
37 | (defcustom vc-mtn-diff-switches t |
38 | "String or list of strings specifying switches for monotone diff under VC. | |
633883e7 | 39 | If nil, use the value of `vc-diff-switches'. If t, use no switches." |
a857238c GM |
40 | :type '(choice (const :tag "Unspecified" nil) |
41 | (const :tag "None" t) | |
42 | (string :tag "Argument String") | |
633883e7 | 43 | (repeat :tag "Argument List" :value ("") string)) |
a857238c GM |
44 | :version "23.1" |
45 | :group 'vc) | |
46 | ||
47 | (define-obsolete-variable-alias 'vc-mtn-command 'vc-mtn-program "23.1") | |
48 | (defcustom vc-mtn-program "mtn" | |
49 | "Name of the monotone executable." | |
50 | :type 'string | |
51 | :group 'vc) | |
52 | ||
b1dc6d44 SM |
53 | ;; Clear up the cache to force vc-call to check again and discover |
54 | ;; new functions when we reload this file. | |
55 | (put 'Mtn 'vc-functions nil) | |
56 | ||
a857238c | 57 | (unless (executable-find vc-mtn-program) |
b1dc6d44 SM |
58 | ;; vc-mtn.el is 100% non-functional without the `mtn' executable. |
59 | (setq vc-handled-backends (delq 'Mtn vc-handled-backends))) | |
60 | ||
61 | ;;;###autoload | |
3adbe224 | 62 | (defconst vc-mtn-admin-dir "_MTN" "Name of the monotone directory.") |
b1dc6d44 | 63 | ;;;###autoload |
3adbe224 GM |
64 | (defconst vc-mtn-admin-format (concat vc-mtn-admin-dir "/format") |
65 | "Name of the monotone directory's format file.") | |
b1dc6d44 SM |
66 | |
67 | ;;;###autoload (defun vc-mtn-registered (file) | |
68 | ;;;###autoload (if (vc-find-root file vc-mtn-admin-format) | |
69 | ;;;###autoload (progn | |
70 | ;;;###autoload (load "vc-mtn") | |
71 | ;;;###autoload (vc-mtn-registered file)))) | |
72 | ||
73 | (defun vc-mtn-revision-granularity () 'repository) | |
70e2f6c7 | 74 | (defun vc-mtn-checkout-model (files) 'implicit) |
b1dc6d44 SM |
75 | |
76 | (defun vc-mtn-root (file) | |
77 | (setq file (if (file-directory-p file) | |
78 | (file-name-as-directory file) | |
79 | (file-name-directory file))) | |
80 | (or (vc-file-getprop file 'vc-mtn-root) | |
81 | (vc-file-setprop file 'vc-mtn-root | |
82 | (vc-find-root file vc-mtn-admin-format)))) | |
83 | ||
84 | ||
85 | (defun vc-mtn-registered (file) | |
86 | (let ((root (vc-mtn-root file))) | |
87 | (when root | |
88 | (vc-mtn-state file)))) | |
89 | ||
90 | (defun vc-mtn-command (buffer okstatus files &rest flags) | |
91 | "A wrapper around `vc-do-command' for use in vc-mtn.el." | |
ce4025c7 SM |
92 | (let ((process-environment |
93 | ;; Avoid localization of messages so we can parse the output. | |
94 | (cons "LC_MESSAGES=C" process-environment))) | |
a857238c GM |
95 | (apply 'vc-do-command (or buffer "*vc*") okstatus vc-mtn-program |
96 | files flags))) | |
b1dc6d44 SM |
97 | |
98 | (defun vc-mtn-state (file) | |
99 | ;; If `mtn' fails or returns status>0, or if the search files, just | |
100 | ;; return nil. | |
101 | (ignore-errors | |
102 | (with-temp-buffer | |
103 | (vc-mtn-command t 0 file "status") | |
104 | (goto-char (point-min)) | |
3281a821 DN |
105 | (re-search-forward |
106 | "^ \\(?:\\(patched\\)\\|\\(added\\) \\(?:.*\\)\\)\\|no changes$") | |
107 | (cond ((match-end 1) 'edited) | |
108 | ((match-end 2) 'added) | |
109 | (t 'up-to-date))))) | |
b1dc6d44 | 110 | |
f5a0b281 DN |
111 | (defun vc-mtn-after-dir-status (update-function) |
112 | (let (result) | |
113 | (goto-char (point-min)) | |
05539fb3 | 114 | (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)" nil t) |
f5a0b281 DN |
115 | (while (re-search-forward |
116 | "^ \\(?:\\(patched \\)\\|\\(added \\)\\)\\(.*\\)$" nil t) | |
117 | (cond ((match-end 1) (push (list (match-string 3) 'edited) result)) | |
118 | ((match-end 2) (push (list (match-string 3) 'added) result)))) | |
119 | (funcall update-function result))) | |
120 | ||
121 | (defun vc-mtn-dir-status (dir update-function) | |
122 | (vc-mtn-command (current-buffer) 'async dir "status") | |
123 | (vc-exec-after | |
124 | `(vc-mtn-after-dir-status (quote ,update-function)))) | |
125 | ||
ac3f4c6f | 126 | (defun vc-mtn-working-revision (file) |
b1dc6d44 SM |
127 | ;; If `mtn' fails or returns status>0, or if the search fails, just |
128 | ;; return nil. | |
129 | (ignore-errors | |
130 | (with-temp-buffer | |
131 | (vc-mtn-command t 0 file "status") | |
132 | (goto-char (point-min)) | |
05539fb3 | 133 | (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)") |
b1dc6d44 SM |
134 | (match-string 2)))) |
135 | ||
136 | (defun vc-mtn-workfile-branch (file) | |
137 | ;; If `mtn' fails or returns status>0, or if the search files, just | |
138 | ;; return nil. | |
139 | (ignore-errors | |
140 | (with-temp-buffer | |
141 | (vc-mtn-command t 0 file "status") | |
142 | (goto-char (point-min)) | |
05539fb3 | 143 | (re-search-forward "\\(?:Current b\\|B\\)ranch: *\\(.*\\)\n?\nChanges against parent \\(.*\\)") |
b1dc6d44 SM |
144 | (match-string 1)))) |
145 | ||
146 | (defun vc-mtn-workfile-unchanged-p (file) | |
147 | (not (eq (vc-mtn-state file) 'edited))) | |
148 | ||
149 | ;; Mode-line rewrite code copied from vc-arch.el. | |
150 | ||
151 | (defcustom vc-mtn-mode-line-rewrite | |
152 | '(("\\`[^:/#]*[:/#]" . "")) ;Drop the host part. | |
153 | "Rewrite rules to shorten Mtn's revision names on the mode-line." | |
154 | :type '(repeat (cons regexp string)) | |
8e788369 | 155 | :version "22.2" |
b1dc6d44 SM |
156 | :group 'vc) |
157 | ||
158 | (defun vc-mtn-mode-line-string (file) | |
159 | "Return string for placement in modeline by `vc-mode-line' for FILE." | |
160 | (let ((branch (vc-mtn-workfile-branch file))) | |
161 | (dolist (rule vc-mtn-mode-line-rewrite) | |
162 | (if (string-match (car rule) branch) | |
163 | (setq branch (replace-match (cdr rule) t nil branch)))) | |
164 | (format "Mtn%c%s" | |
165 | (case (vc-state file) | |
3702367b | 166 | ((up-to-date needs-update) ?-) |
b1dc6d44 SM |
167 | (added ?@) |
168 | (t ?:)) | |
169 | branch))) | |
170 | ||
4a87f93e | 171 | (defun vc-mtn-register (files &optional rev comment) |
b1dc6d44 SM |
172 | (vc-mtn-command nil 0 files "add")) |
173 | ||
174 | (defun vc-mtn-responsible-p (file) (vc-mtn-root file)) | |
175 | (defun vc-mtn-could-register (file) (vc-mtn-root file)) | |
176 | ||
f64ab8fb JB |
177 | (declare-function log-edit-extract-headers "log-edit" (headers string)) |
178 | ||
4624de78 | 179 | (defun vc-mtn-checkin (files rev comment) |
fab43c76 DN |
180 | (apply 'vc-mtn-command nil 0 files |
181 | (nconc (list "commit" "-m") | |
182 | (log-edit-extract-headers '(("Author" . "--author") | |
183 | ("Date" . "--date")) | |
184 | comment)))) | |
b1dc6d44 | 185 | |
ac3f4c6f | 186 | (defun vc-mtn-find-revision (file rev buffer) |
b1dc6d44 SM |
187 | (vc-mtn-command buffer 0 file "cat" "-r" rev)) |
188 | ||
189 | ;; (defun vc-mtn-checkout (file &optional editable rev) | |
190 | ;; ) | |
191 | ||
192 | (defun vc-mtn-revert (file &optional contents-done) | |
193 | (unless contents-done | |
194 | (vc-mtn-command nil 0 file "revert"))) | |
195 | ||
196 | ;; (defun vc-mtn-roolback (files) | |
197 | ;; ) | |
198 | ||
662c5698 | 199 | (defun vc-mtn-print-log (files buffer &optional shortlog start-revision limit) |
6616006b | 200 | (apply 'vc-mtn-command buffer 0 files "log" |
662c5698 | 201 | (append |
6e890faa GM |
202 | (when start-revision (list "--from" (format "%s" start-revision))) |
203 | (when limit (list "--last" (format "%s" limit)))))) | |
b1dc6d44 | 204 | |
97546017 DN |
205 | (defvar log-view-message-re) |
206 | (defvar log-view-file-re) | |
207 | (defvar log-view-font-lock-keywords) | |
469ca403 | 208 | (defvar log-view-per-file-logs) |
97546017 | 209 | |
b1dc6d44 | 210 | (define-derived-mode vc-mtn-log-view-mode log-view-mode "Mtn-Log-View" |
469ca403 DN |
211 | ;; Don't match anything. |
212 | (set (make-local-variable 'log-view-file-re) "\\`a\\`") | |
213 | (set (make-local-variable 'log-view-per-file-logs) nil) | |
b1dc6d44 SM |
214 | ;; TODO: Use a more precise regexp than "[ |/]+" to avoid false positives |
215 | ;; in the ChangeLog text. | |
216 | (set (make-local-variable 'log-view-message-re) | |
217 | "^[ |/]+Revision: \\([0-9a-f]+\\)") | |
218 | (require 'add-log) ;For change-log faces. | |
219 | (set (make-local-variable 'log-view-font-lock-keywords) | |
220 | (append log-view-font-lock-keywords | |
221 | '(("^[ |]+Author: \\(.*\\)" (1 'change-log-email)) | |
222 | ("^[ |]+Date: \\(.*\\)" (1 'change-log-date-face)))))) | |
223 | ||
5b5afd50 | 224 | ;; (defun vc-mtn-show-log-entry (revision) |
b1dc6d44 SM |
225 | ;; ) |
226 | ||
b1dc6d44 | 227 | (defun vc-mtn-diff (files &optional rev1 rev2 buffer) |
a857238c | 228 | "Get a difference report using monotone between two revisions of FILES." |
b1dc6d44 | 229 | (apply 'vc-mtn-command (or buffer "*vc-diff*") 1 files "diff" |
a857238c | 230 | (append |
39ba78ef | 231 | (vc-switches 'mtn 'diff) |
a857238c | 232 | (if rev1 (list "-r" rev1)) (if rev2 (list "-r" rev2))))) |
b1dc6d44 SM |
233 | |
234 | (defun vc-mtn-annotate-command (file buf &optional rev) | |
837b0e99 | 235 | (apply 'vc-mtn-command buf 'async file "annotate" |
b1dc6d44 SM |
236 | (if rev (list "-r" rev)))) |
237 | ||
f8bd9ac6 DN |
238 | (declare-function vc-annotate-convert-time "vc-annotate" (time)) |
239 | ||
b1dc6d44 SM |
240 | (defconst vc-mtn-annotate-full-re |
241 | "^ *\\([0-9a-f]+\\)\\.* by [^ ]+ \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\): ") | |
242 | (defconst vc-mtn-annotate-any-re | |
243 | (concat "^\\(?: +: \\|" vc-mtn-annotate-full-re "\\)")) | |
244 | ||
245 | (defun vc-mtn-annotate-time () | |
246 | (when (looking-at vc-mtn-annotate-any-re) | |
247 | (goto-char (match-end 0)) | |
248 | (let ((year (match-string 2))) | |
249 | (if (not year) | |
250 | ;; Look for the date on a previous line. | |
251 | (save-excursion | |
252 | (get-text-property (1- (previous-single-property-change | |
253 | (point) 'vc-mtn-time nil (point-min))) | |
254 | 'vc-mtn-time)) | |
255 | (let ((time (vc-annotate-convert-time | |
256 | (encode-time 0 0 0 | |
257 | (string-to-number (match-string 4)) | |
258 | (string-to-number (match-string 3)) | |
259 | (string-to-number year) | |
260 | t)))) | |
261 | (let ((inhibit-read-only t) | |
262 | (inhibit-modification-hooks t)) | |
263 | (put-text-property (match-beginning 0) (match-end 0) | |
264 | 'vc-mtn-time time)) | |
265 | time))))) | |
266 | ||
267 | (defun vc-mtn-annotate-extract-revision-at-line () | |
268 | (save-excursion | |
269 | (when (or (looking-at vc-mtn-annotate-full-re) | |
270 | (re-search-backward vc-mtn-annotate-full-re nil t)) | |
271 | (match-string 1)))) | |
272 | ||
273 | ;;; Revision completion. | |
274 | ||
275 | (defun vc-mtn-list-tags () | |
276 | (with-temp-buffer | |
277 | (vc-mtn-command t 0 nil "list" "tags") | |
278 | (goto-char (point-min)) | |
279 | (let ((tags ())) | |
280 | (while (re-search-forward "^[^ ]+" nil t) | |
281 | (push (match-string 0) tags)) | |
282 | tags))) | |
283 | ||
284 | (defun vc-mtn-list-branches () | |
285 | (with-temp-buffer | |
286 | (vc-mtn-command t 0 nil "list" "branches") | |
287 | (goto-char (point-min)) | |
288 | (let ((branches ())) | |
289 | (while (re-search-forward "^.+" nil t) | |
290 | (push (match-string 0) branches)) | |
291 | branches))) | |
292 | ||
293 | (defun vc-mtn-list-revision-ids (prefix) | |
294 | (with-temp-buffer | |
295 | (vc-mtn-command t 0 nil "complete" "revision" prefix) | |
296 | (goto-char (point-min)) | |
297 | (let ((ids ())) | |
298 | (while (re-search-forward "^.+" nil t) | |
299 | (push (match-string 0) ids)) | |
300 | ids))) | |
301 | ||
844b90ae | 302 | (defun vc-mtn-revision-completion-table (files) |
cd1181db | 303 | ;; TODO: Implement completion for selectors |
b1dc6d44 | 304 | ;; TODO: Implement completion for composite selectors. |
844b90ae SM |
305 | (lexical-let ((files files)) |
306 | ;; What about using `files'?!? --Stef | |
b1dc6d44 SM |
307 | (lambda (string pred action) |
308 | (cond | |
309 | ;; "Tag" selectors. | |
310 | ((string-match "\\`t:" string) | |
311 | (complete-with-action action | |
312 | (mapcar (lambda (tag) (concat "t:" tag)) | |
313 | (vc-mtn-list-tags)) | |
314 | string pred)) | |
315 | ;; "Branch" selectors. | |
316 | ((string-match "\\`b:" string) | |
317 | (complete-with-action action | |
318 | (mapcar (lambda (tag) (concat "b:" tag)) | |
319 | (vc-mtn-list-branches)) | |
320 | string pred)) | |
321 | ;; "Head" selectors. Not sure how they differ from "branch" selectors. | |
322 | ((string-match "\\`h:" string) | |
323 | (complete-with-action action | |
324 | (mapcar (lambda (tag) (concat "h:" tag)) | |
325 | (vc-mtn-list-branches)) | |
326 | string pred)) | |
327 | ;; "ID" selectors. | |
328 | ((string-match "\\`i:" string) | |
329 | (complete-with-action action | |
330 | (mapcar (lambda (tag) (concat "i:" tag)) | |
331 | (vc-mtn-list-revision-ids | |
332 | (substring string (match-end 0)))) | |
333 | string pred)) | |
334 | (t | |
335 | (complete-with-action action | |
336 | '("t:" "b:" "h:" "i:" | |
337 | ;; Completion not implemented for these. | |
338 | "a:" "c:" "d:" "e:" "l:") | |
339 | string pred)))))) | |
eb3fa2cf GM |
340 | |
341 | ||
b1dc6d44 SM |
342 | |
343 | (provide 'vc-mtn) | |
344 | ||
b1dc6d44 | 345 | ;;; vc-mtn.el ends here |