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