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