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