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