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