Bind [S-tab] to [backtab].
[emacs.git] / lisp / vc-arch.el
blob79056c4c947be0d11dafd0cf6d87b97582e6d6b6
1 ;;; vc-arch.el --- VC backend for the Arch version-control system
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; The home page of the Arch version control system is at
29 ;; http://www.gnuarch.org/
31 ;; This is derived from vc-mcvs.el as follows:
32 ;; - cp vc-mcvs.el vc-arch.el and then M-% mcvs RET arch RET
34 ;; Then of course started the hacking.
36 ;; What has been partly tested:
37 ;; - Open a file.
38 ;; - C-x v = without any prefix arg.
39 ;; - C-x v v to commit a change to a single file.
41 ;; Bugs:
43 ;; - *VC-log*'s initial content lacks the `Summary:' lines.
44 ;; - All files under the tree are considered as "under Arch's control"
45 ;; without regards to =tagging-method and such.
46 ;; - Files are always considered as `edited'.
47 ;; - C-x v l does not work.
48 ;; - C-x v i does not work.
49 ;; - C-x v ~ does not work.
50 ;; - C-x v u does not work.
51 ;; - C-x v s does not work.
52 ;; - C-x v r does not work.
53 ;; - VC-dired does not work.
54 ;; - And more...
56 ;;; Code:
58 (eval-when-compile (require 'vc) (require 'cl))
60 ;;;
61 ;;; Customization options
62 ;;;
64 (defvar vc-arch-command
65 (let ((candidates '("tla")))
66 (while (and candidates (not (executable-find (car candidates))))
67 (setq candidates (cdr candidates)))
68 (or (car candidates) "tla")))
70 ;; Clear up the cache to force vc-call to check again and discover
71 ;; new functions when we reload this file.
72 (put 'Arch 'vc-functions nil)
74 ;;;###autoload (defun vc-arch-registered (file)
75 ;;;###autoload (if (vc-find-root file "{arch}/=tagging-method")
76 ;;;###autoload (progn
77 ;;;###autoload (load "vc-arch")
78 ;;;###autoload (vc-arch-registered file))))
80 (defun vc-arch-add-tagline ()
81 "Add an `arch-tag' to the end of the current file."
82 (interactive)
83 (comment-normalize-vars)
84 (goto-char (point-max))
85 (forward-comment -1)
86 (unless (bolp) (insert "\n"))
87 (let ((beg (point))
88 (idfile (and buffer-file-name
89 (expand-file-name
90 (concat ".arch-ids/"
91 (file-name-nondirectory buffer-file-name)
92 ".id")
93 (file-name-directory buffer-file-name)))))
94 (insert "arch-tag: ")
95 (if (and idfile (file-exists-p idfile))
96 ;; If the file is unreadable, we do want to get an error here.
97 (progn
98 (insert-file-contents idfile)
99 (forward-line 1)
100 (delete-file idfile))
101 (condition-case nil
102 (call-process "uuidgen" nil t)
103 (file-error (insert (format "%s <%s> %s"
104 (current-time-string)
105 user-mail-address
106 (+ (nth 2 (current-time))
107 (buffer-size)))))))
108 (comment-region beg (point))))
110 (defconst vc-arch-tagline-re "^\\W*arch-tag:[ \t]*\\(.*[^ \t\n]\\)")
112 (defun vc-arch-file-source-p (file)
113 "Can return nil, `maybe' or a non-nil value.
114 Only the value `maybe' can be trusted :-(."
115 ;; FIXME: Check the tag and name of parent dirs.
116 (unless (string-match "\\`[,+]" (file-name-nondirectory file))
117 (or (string-match "\\`{arch}/"
118 (file-relative-name file (vc-arch-root file)))
119 (file-exists-p
120 ;; Check the presence of an ID file.
121 (expand-file-name
122 (concat ".arch-ids/" (file-name-nondirectory file) ".id")
123 (file-name-directory file)))
124 ;; Check the presence of a tagline.
125 (with-current-buffer (find-file-noselect file)
126 (save-excursion
127 (goto-char (point-max))
128 (or (re-search-backward vc-arch-tagline-re (- (point) 1000) t)
129 (progn
130 (goto-char (point-min))
131 (re-search-forward vc-arch-tagline-re (+ (point) 1000) t)))))
132 ;; FIXME: check =tagging-method to see whether untagged files might
133 ;; be source or not.
134 (with-current-buffer
135 (find-file-noselect (expand-file-name "{arch}/=tagging-method"
136 (vc-arch-root file)))
137 (let ((untagged-source t)) ;Default is `names'.
138 (save-excursion
139 (goto-char (point-min))
140 (if (re-search-forward "^[ \t]*\\(\\(tagline\\|implicit\\|names\\)\\|explicit\\)" nil t)
141 (setq untagged-source (match-end 2)))
142 (if (re-search-forward "^[ \t]*untagged-source[ \t]+\\(\\(source\\)\\|precious\\|backup\\|junk\\|unrecognized\\)" nil t)
143 (setq untagged-source (match-end 2))))
144 (if untagged-source 'maybe))))))
146 (defun vc-arch-file-id (file)
147 ;; Don't include the kind of ID this is because it seems to be too messy.
148 (let ((idfile (expand-file-name
149 (concat ".arch-ids/" (file-name-nondirectory file) ".id")
150 (file-name-directory file))))
151 (if (file-exists-p idfile)
152 (with-temp-buffer
153 (insert-file-contents idfile)
154 (looking-at ".*[^ \n\t]")
155 (match-string 0)))
156 (with-current-buffer (find-file-noselect file)
157 (save-excursion
158 (goto-char (point-max))
159 (if (or (re-search-backward vc-arch-tagline-re (- (point) 1000) t)
160 (progn
161 (goto-char (point-min))
162 (re-search-forward vc-arch-tagline-re (+ (point) 1000) t)))
163 (match-string 1)
164 (concat "./" (file-relative-name file (vc-arch-root file))))))))
166 (defun vc-arch-tagging-method (file)
167 (with-current-buffer
168 (find-file-noselect
169 (expand-file-name "{arch}/=tagging-method" (vc-arch-root file)))
170 (save-excursion
171 (goto-char (point-min))
172 (if (re-search-forward
173 "^[ \t]*\\(tagline\\|implicit\\|names\\|explicit\\)" nil t)
174 (intern (match-string 1))
175 'names))))
177 (defun vc-arch-root (file)
178 "Return the root directory of a Arch project, if any."
179 (or (vc-file-getprop file 'arch-root)
180 (vc-file-setprop
181 ;; Check the =tagging-method, in case someone naively manually
182 ;; creates a {arch} directory somewhere.
183 file 'arch-root (vc-find-root file "{arch}/=tagging-method"))))
185 (defun vc-arch-register (file &optional rev comment)
186 (if rev (error "Explicit initial revision not supported for Arch"))
187 (let ((tagmet (vc-arch-tagging-method file)))
188 (if (and (memq tagmet '(tagline implicit)) comment-start)
189 (with-current-buffer (find-file-noselect file)
190 (if (buffer-modified-p)
191 (error "Save %s first" (buffer-name)))
192 (vc-arch-add-tagline)
193 (save-buffer))
194 (vc-arch-command nil 0 file "add"))))
196 (defun vc-arch-registered (file)
197 ;; Don't seriously check whether it's source or not. Checking would
198 ;; require running TLA, so it's better to not do it, so it also works if
199 ;; TLA is not installed.
200 (and (vc-arch-root file)
201 (vc-arch-file-source-p file)))
203 (defun vc-arch-default-version (file)
204 (or (vc-file-getprop (vc-arch-root file) 'arch-default-version)
205 (let* ((root (vc-arch-root file))
206 (f (expand-file-name "{arch}/++default-version" root)))
207 (if (file-readable-p f)
208 (vc-file-setprop
209 root 'arch-default-version
210 (with-temp-buffer
211 (insert-file-contents f)
212 ;; Strip the terminating newline.
213 (buffer-substring (point-min) (1- (point-max)))))))))
215 (defun vc-arch-workfile-unchanged-p (file)
216 "Check if FILE is unchanged by diffing against the master version.
217 Return non-nil if FILE is unchanged."
218 nil)
220 (defun vc-arch-state (file)
221 ;; There's no checkout operation and merging is not done from VC
222 ;; so the only operation that's state dependent that VC supports is commit
223 ;; which is only activated if the file is `edited'.
224 (let* ((root (vc-arch-root file))
225 (ver (vc-arch-default-version file))
226 (pat (concat "\\`" (subst-char-in-string ?/ ?% ver)))
227 (dir (expand-file-name ",,inode-sigs/"
228 (expand-file-name "{arch}" root)))
229 (sigfile nil))
230 (dolist (f (if (file-directory-p dir) (directory-files dir t pat)))
231 (if (or (not sigfile) (file-newer-than-file-p f sigfile))
232 (setq sigfile f)))
233 (if (not sigfile)
234 'edited ;We know nothing.
235 (let ((id (vc-arch-file-id file)))
236 (setq id (replace-regexp-in-string "[ \t]" "_" id))
237 (with-current-buffer (find-file-noselect sigfile)
238 (goto-char (point-min))
239 (while (and (search-forward id nil 'move)
240 (save-excursion
241 (goto-char (- (match-beginning 0) 2))
242 ;; For `names', the lines start with `?./foo/bar'.
243 ;; For others there's 2 chars before the ./foo/bar.
244 (or (not (or (bolp) (looking-at "\n?")))
245 ;; Ignore E_ entries used for foo.id files.
246 (looking-at "E_")))))
247 (if (eobp)
248 ;; ID not found.
249 (if (equal (file-name-nondirectory sigfile)
250 (subst-char-in-string
251 ?/ ?% (vc-arch-workfile-version file)))
252 'added
253 ;; Might be `added' or `up-to-date' as well.
254 ;; FIXME: Check in the patch logs to find out.
255 'edited)
256 ;; Found the ID, let's check the inode.
257 (if (not (re-search-forward
258 "\t.*mtime=\\([0-9]+\\):size=\\([0-9]+\\)"
259 (line-end-position) t))
260 ;; Buh? Unexpected format.
261 'edited
262 (let ((ats (file-attributes file)))
263 (if (and (eq (nth 7 ats) (string-to-number (match-string 2)))
264 (equal (format-time-string "%s" (nth 5 ats))
265 (match-string 1)))
266 'up-to-date
267 'edited)))))))))
269 (defun vc-arch-workfile-version (file)
270 (let* ((root (expand-file-name "{arch}" (vc-arch-root file)))
271 (defbranch (vc-arch-default-version file)))
272 (when (and defbranch (string-match "\\`\\(.+@[^/\n]+\\)/\\(\\(\\(.*?\\)\\(?:--.*\\)?\\)--.*\\)\\'" defbranch))
273 (let* ((archive (match-string 1 defbranch))
274 (category (match-string 4 defbranch))
275 (branch (match-string 3 defbranch))
276 (version (match-string 2 defbranch))
277 (sealed nil) (rev-nb 0)
278 (rev nil)
279 logdir tmp)
280 (setq logdir (expand-file-name category root))
281 (setq logdir (expand-file-name branch logdir))
282 (setq logdir (expand-file-name version logdir))
283 (setq logdir (expand-file-name archive logdir))
284 (setq logdir (expand-file-name "patch-log" logdir))
285 (dolist (file (if (file-directory-p logdir) (directory-files logdir)))
286 ;; Revision names go: base-0, patch-N, version-0, versionfix-M.
287 (when (and (eq (aref file 0) ?v) (not sealed))
288 (setq sealed t rev-nb 0))
289 (if (and (string-match "-\\([0-9]+\\)\\'" file)
290 (setq tmp (string-to-number (match-string 1 file)))
291 (or (not sealed) (eq (aref file 0) ?v))
292 (>= tmp rev-nb))
293 (setq rev-nb tmp rev file)))
294 ;; Use "none-000" if the tree hasn't yet been committed on the
295 ;; default branch. We'll then get "Arch:000[branch]" on the mode-line.
296 (concat defbranch "--" (or rev "none-000"))))))
299 (defcustom vc-arch-mode-line-rewrite
300 '(("\\`.*--\\(.*--.*\\)--\\(v?\\).*-\\([0-9]+\\)\\'" . "\\2\\3[\\1]"))
301 "Rewrite rules to shorten Arch's revision names on the mode-line."
302 :type '(repeat (cons regexp string))
303 :group 'vc)
305 (defun vc-arch-mode-line-string (file)
306 "Return string for placement in modeline by `vc-mode-line' for FILE."
307 (let ((rev (vc-workfile-version file)))
308 (dolist (rule vc-arch-mode-line-rewrite)
309 (if (string-match (car rule) rev)
310 (setq rev (replace-match (cdr rule) t nil rev))))
311 (format "Arch%c%s"
312 (case (vc-state file)
313 ((up-to-date needs-patch) ?-)
314 (added ?@)
315 (t ?:))
316 rev)))
318 (defun vc-arch-diff3-rej-p (rej)
319 (let ((attrs (file-attributes rej)))
320 (and attrs (< (nth 7 attrs) 60)
321 (with-temp-buffer
322 (insert-file-contents rej)
323 (goto-char (point-min))
324 (looking-at "Conflicts occured, diff3 conflict markers left in file\\.")))))
326 (defun vc-arch-delete-rej-if-obsolete ()
327 "For use in `after-save-hook'."
328 (save-excursion
329 (let ((rej (concat buffer-file-name ".rej")))
330 (when (and buffer-file-name (vc-arch-diff3-rej-p rej))
331 (if (not (re-search-forward "^<<<<<<< " nil t))
332 ;; The .rej file is obsolete.
333 (condition-case nil (delete-file rej) (error nil)))))))
335 (defun vc-arch-find-file-hook ()
336 (let ((rej (concat buffer-file-name ".rej")))
337 (when (and buffer-file-name (file-exists-p rej))
338 (if (vc-arch-diff3-rej-p rej)
339 (save-excursion
340 (goto-char (point-min))
341 (if (not (re-search-forward "^<<<<<<< " nil t))
342 ;; The .rej file is obsolete.
343 (condition-case nil (delete-file rej) (error nil))
344 (smerge-mode 1)
345 (add-hook 'after-save-hook
346 'vc-arch-delete-rej-if-obsolete nil t)
347 (message "There are unresolved conflicts in this file")))
348 (message "There are unresolved conflicts in %s"
349 (file-name-nondirectory rej))))))
351 (defun vc-arch-find-file-not-found-hook ()
352 ;; Do nothing. We are not sure whether the file is `source' or not,
353 ;; so we shouldn't ask the user whether she wants to check it out.
356 (defun vc-arch-checkout-model (file) 'implicit)
358 (defun vc-arch-checkin (file rev comment)
359 (if rev (error "Committing to a specific revision is unsupported"))
360 (let ((summary (file-relative-name file (vc-arch-root file))))
361 ;; Extract a summary from the comment.
362 (when (or (string-match "\\`Summary:[ \t]*\\(.*[^ \t\n]\\)\\([ \t]*\n\\)*" comment)
363 (string-match "\\`[ \t]*\\(.*[^ \t\n]\\)[ \t]*\\(\n?\\'\\|\n\\([ \t]*\n\\)+\\)" comment))
364 (setq summary (match-string 1 comment))
365 (setq comment (substring comment (match-end 0))))
366 (vc-arch-command nil 0 file "commit" "-s" summary "-L" comment "--"
367 (vc-switches 'Arch 'checkin))))
369 (defun vc-arch-diff (file &optional oldvers newvers buffer)
370 "Get a difference report using Arch between two versions of FILE."
371 (if (and newvers
372 (vc-up-to-date-p file)
373 (equal newvers (vc-workfile-version file)))
374 ;; Newvers is the base revision and the current file is unchanged,
375 ;; so we can diff with the current file.
376 (setq newvers nil))
377 (if newvers
378 (error "Diffing specific revisions not implemented")
379 (let* ((async (and (not vc-disable-async-diff) (fboundp 'start-process)))
380 ;; Run the command from the root dir.
381 (default-directory (vc-arch-root file))
382 (status
383 (vc-arch-command
384 (or buffer "*vc-diff*")
385 (if async 'async 1)
386 nil "file-diffs"
387 ;; Arch does not support the typical flags.
388 ;; (vc-switches 'Arch 'diff)
389 (file-relative-name file)
390 (if (equal oldvers (vc-workfile-version file))
392 oldvers))))
393 (if async 1 status)))) ; async diff, pessimistic assumption.
395 (defun vc-arch-delete-file (file)
396 (vc-arch-command nil 0 file "rm"))
398 (defun vc-arch-rename-file (old new)
399 (vc-arch-command nil 0 new "mv" (file-relative-name old)))
401 (defalias 'vc-arch-responsible-p 'vc-arch-root)
403 (defun vc-arch-command (buffer okstatus file &rest flags)
404 "A wrapper around `vc-do-command' for use in vc-arch.el."
405 (apply 'vc-do-command buffer okstatus vc-arch-command file flags))
407 (defun vc-arch-init-version () nil)
409 (provide 'vc-arch)
411 ;; arch-tag: a35c7c1c-5237-429d-88ef-3d718fd2e704
412 ;;; vc-arch.el ends here