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