1 ;;; vc-arch.el --- VC backend for the Arch version-control system
3 ;; Copyright (C) 2004, 2005, 2006, 2007 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)
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.
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:
38 ;; - C-x v = without any prefix arg.
39 ;; - C-x v v to commit a change to a single file.
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.
58 (eval-when-compile (require 'vc
) (require 'cl
))
61 ;;; Customization options
64 (defvar vc-arch-command
65 (let ((candidates '("tla" "baz")))
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")
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."
83 (comment-normalize-vars)
84 (goto-char (point-max))
86 (skip-chars-forward " \t\n")
88 ((not (bolp)) (insert "\n\n"))
89 ((not (eq ?
\n (char-before (1- (point))))) (insert "\n")))
91 (idfile (and buffer-file-name
94 (file-name-nondirectory buffer-file-name
)
96 (file-name-directory buffer-file-name
)))))
98 (if (and idfile
(file-exists-p idfile
))
99 ;; If the file is unreadable, we do want to get an error here.
101 (insert-file-contents idfile
)
103 (delete-file idfile
))
105 (call-process "uuidgen" nil t
)
106 (file-error (insert (format "%s <%s> %s"
107 (current-time-string)
109 (+ (nth 2 (current-time))
111 (comment-region beg
(point))))
113 (defconst vc-arch-tagline-re
"^\\W*arch-tag:[ \t]*\\(.*[^ \t\n]\\)")
115 (defmacro vc-with-current-file-buffer
(file &rest body
)
116 (declare (indent 2) (debug t
))
117 `(let ((-kill-buf- nil
)
119 (with-current-buffer (or (find-buffer-visiting -file-
)
120 (setq -kill-buf-
(generate-new-buffer " temp")))
121 ;; Avoid find-file-literally since it can do many undesirable extra
122 ;; things (among which, call us back into an infinite loop).
123 (if -kill-buf-
(insert-file-contents -file-
))
126 (if (buffer-live-p -kill-buf-
) (kill-buffer -kill-buf-
))))))
128 (defun vc-arch-file-source-p (file)
129 "Can return nil, `maybe' or a non-nil value.
130 Only the value `maybe' can be trusted :-(."
131 ;; FIXME: Check the tag and name of parent dirs.
132 (unless (string-match "\\`[,+]" (file-name-nondirectory file
))
133 (or (string-match "\\`{arch}/"
134 (file-relative-name file
(vc-arch-root file
)))
136 ;; Check the presence of an ID file.
138 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
139 (file-name-directory file
)))
140 ;; Check the presence of a tagline.
141 (vc-with-current-file-buffer file
143 (goto-char (point-max))
144 (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
146 (goto-char (point-min))
147 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))))
148 ;; FIXME: check =tagging-method to see whether untagged files might
151 (find-file-noselect (expand-file-name "{arch}/=tagging-method"
152 (vc-arch-root file
)))
153 (let ((untagged-source t
)) ;Default is `names'.
155 (goto-char (point-min))
156 (if (re-search-forward "^[ \t]*\\(\\(tagline\\|implicit\\|names\\)\\|explicit\\)" nil t
)
157 (setq untagged-source
(match-end 2)))
158 (if (re-search-forward "^[ \t]*untagged-source[ \t]+\\(\\(source\\)\\|precious\\|backup\\|junk\\|unrecognized\\)" nil t
)
159 (setq untagged-source
(match-end 2))))
160 (if untagged-source
'maybe
))))))
162 (defun vc-arch-file-id (file)
163 ;; Don't include the kind of ID this is because it seems to be too messy.
164 (let ((idfile (expand-file-name
165 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
166 (file-name-directory file
))))
167 (if (file-exists-p idfile
)
169 (insert-file-contents idfile
)
170 (looking-at ".*[^ \n\t]")
172 (with-current-buffer (find-file-noselect file
)
174 (goto-char (point-max))
175 (if (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
177 (goto-char (point-min))
178 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))
180 (concat "./" (file-relative-name file
(vc-arch-root file
)))))))))
182 (defun vc-arch-tagging-method (file)
185 (expand-file-name "{arch}/=tagging-method" (vc-arch-root file
)))
187 (goto-char (point-min))
188 (if (re-search-forward
189 "^[ \t]*\\(tagline\\|implicit\\|names\\|explicit\\)" nil t
)
190 (intern (match-string 1))
193 (defun vc-arch-root (file)
194 "Return the root directory of a Arch project, if any."
195 (or (vc-file-getprop file
'arch-root
)
197 ;; Check the =tagging-method, in case someone naively manually
198 ;; creates a {arch} directory somewhere.
199 file
'arch-root
(vc-find-root file
"{arch}/=tagging-method"))))
201 (defun vc-arch-register (files &optional rev comment
)
202 (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)
211 (vc-arch-command nil
0 files
"add"))
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
)))
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
)
226 root
'arch-default-version
228 (insert-file-contents f
)
229 ;; Strip the terminating newline.
230 (buffer-substring (point-min) (1- (point-max)))))))))
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."
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
)))
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
))
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
)
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_")))))
266 (if (equal (file-name-nondirectory sigfile
)
267 (subst-char-in-string
268 ?
/ ?%
(vc-arch-workfile-version file
)))
270 ;; Might be `added' or `up-to-date' as well.
271 ;; FIXME: Check in the patch logs to find out.
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.
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
))
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)
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 (dolist (file (if (file-directory-p logdir
) (directory-files logdir
)))
303 ;; Revision names go: base-0, patch-N, version-0, versionfix-M.
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
))
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"))))))
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
))
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
))))
329 (case (vc-state file
)
330 ((up-to-date needs-patch
) ?-
)
335 (defun vc-arch-diff3-rej-p (rej)
336 (let ((attrs (file-attributes rej
)))
337 (and attrs
(< (nth 7 attrs
) 60)
339 (insert-file-contents rej
)
340 (goto-char (point-min))
341 (looking-at "Conflicts occured, diff3 conflict markers left in file\\.")))))
343 (defun vc-arch-delete-rej-if-obsolete ()
344 "For use in `after-save-hook'."
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
)))))))
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
)
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
))
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
))))))
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.
373 (defun vc-arch-checkout-model (file) 'implicit
)
375 (defun vc-arch-checkin (files rev comment
)
376 (if rev
(error "Committing to a specific revision is unsupported"))
377 ;; FIXME: This implementation probably only works for singleton filesets
378 (let ((summary (file-relative-name (car file
) (vc-arch-root (car files
)))))
379 ;; Extract a summary from the comment.
380 (when (or (string-match "\\`Summary:[ \t]*\\(.*[^ \t\n]\\)\\([ \t]*\n\\)*" comment
)
381 (string-match "\\`[ \t]*\\(.*[^ \t\n]\\)[ \t]*\\(\n?\\'\\|\n\\([ \t]*\n\\)+\\)" comment
))
382 (setq summary
(match-string 1 comment
))
383 (setq comment
(substring comment
(match-end 0))))
384 (vc-arch-command nil
0 files
"commit" "-s" summary
"-L" comment
"--"
385 (vc-switches 'Arch
'checkin
))))
387 (defun vc-arch-diff (files &optional oldvers newvers buffer
)
388 "Get a difference report using Arch between two versions of FILES."
389 ;; FIXME: This implementation probably only works for singleton filesets
391 (vc-up-to-date-p file
)
392 (equal newvers
(vc-workfile-version (car files
))))
393 ;; Newvers is the base revision and the current file is unchanged,
394 ;; so we can diff with the current file.
397 (error "Diffing specific revisions not implemented")
398 (let* ((async (and (not vc-disable-async-diff
) (fboundp 'start-process
)))
399 ;; Run the command from the root dir.
400 (default-directory (vc-arch-root (car files
)))
403 (or buffer
"*vc-diff*")
406 ;; Arch does not support the typical flags.
407 ;; (vc-switches 'Arch 'diff)
408 (mapcar 'file-relative-name files
)
409 (if (equal oldvers
(vc-workfile-version (car files
)))
412 (if async
1 status
)))) ; async diff, pessimistic assumption.
414 (defun vc-arch-delete-file (file)
415 (vc-arch-command nil
0 file
"rm"))
417 (defun vc-arch-rename-file (old new
)
418 (vc-arch-command nil
0 new
"mv" (file-relative-name old
)))
420 (defalias 'vc-arch-responsible-p
'vc-arch-root
)
422 (defun vc-arch-command (buffer okstatus file
&rest flags
)
423 "A wrapper around `vc-do-command' for use in vc-arch.el."
424 (apply 'vc-do-command buffer okstatus vc-arch-command file flags
))
426 (defun vc-arch-init-version () nil
)
428 ;;; Completion of versions and revisions.
430 (defun vc-arch--version-completion-table (root string
)
434 (when (string-match "/\\([^/]+\\)/\\([^/]+\\)\\'" d
)
435 (concat (match-string 2 d
) "/" (match-string 1 d
))))
436 (let ((default-directory root
))
437 (file-expand-wildcards
439 (if (string-match "/" string
)
440 (concat (substring string
(match-end 0))
441 "*/" (substring string
0 (match-beginning 0)))
442 (concat "*/" string
))
445 (defun vc-arch-revision-completion-table (file)
446 (lexical-let ((file file
))
447 (lambda (string pred action
)
448 ;; FIXME: complete revision patches as well.
449 (let* ((root (expand-file-name "{arch}" (vc-arch-root file
)))
450 (table (vc-arch--version-completion-table root string
)))
451 (complete-with-action action table string pred
)))))
453 ;;; Trimming revision libraries.
455 ;; This code is not directly related to VC and there are many variants of
456 ;; this functionality available as scripts, but I like this version better,
457 ;; so maybe others will like it too.
459 (defun vc-arch-trim-find-least-useful-rev (revs)
460 (let* ((first (pop revs
))
463 ;; We try to give more importance to recent revisions. The idea is
464 ;; that it's OK if checking out a revision 1000-patch-old is ten
465 ;; times slower than checking out a revision 100-patch-old. But at
466 ;; the same time a 2-patch-old rev isn't really ten times more
467 ;; important than a 20-patch-old, so we use an arbitrary constant
468 ;; "100" to reduce this effect for recent revisions. Making this
469 ;; constant a float has the side effect of causing the subsequent
470 ;; computations to be done as floats as well.
471 (max (+ 100.0 (car (or (car (last revs
)) third
))))
472 (cost (lambda () (/ (- (car third
) (car first
)) (- max
(car second
)))))
474 (mincost (funcall cost
)))
478 (setq third
(pop revs
))
479 (when (< (funcall cost
) mincost
)
481 (setq mincost
(funcall cost
))))
484 (defun vc-arch-trim-make-sentinel (revs)
485 (if (null revs
) (lambda (proc msg
) (message "VC-Arch trimming ... done"))
487 (message "VC-Arch trimming %s..." ',(file-name-nondirectory (car revs
)))
488 (rename-file ,(car revs
) ,(concat (car revs
) "*rm*"))
489 (setq proc
(start-process "vc-arch-trim" nil
490 "rm" "-rf" ',(concat (car revs
) "*rm*")))
491 (set-process-sentinel proc
(vc-arch-trim-make-sentinel ',(cdr revs
))))))
493 (defun vc-arch-trim-one-revlib (dir)
494 "Delete half of the revisions in the revision library."
495 (interactive "Ddirectory: ")
500 (when (string-match "-\\([0-9]+\\)\\'" f
)
501 (cons (string-to-number (match-string 1 f
)) f
)))
502 (directory-files dir nil nil
'nosort
)))
506 (dotimes (i (/ (length revs
) 2))
507 (let ((minrev (vc-arch-trim-find-least-useful-rev revs
)))
508 (setq revs
(delq minrev revs
))
509 (push minrev subdirs
)))
510 (funcall (vc-arch-trim-make-sentinel
511 (mapcar (lambda (x) (expand-file-name (cdr x
) dir
)) subdirs
))
514 (defun vc-arch-trim-revlib ()
515 "Delete half of the revisions in the revision library."
517 (let ((rl-dir (with-output-to-string
518 (call-process vc-arch-command nil standard-output nil
519 "my-revision-library"))))
520 (while (string-match "\\(.*\\)\n" rl-dir
)
521 (let ((dir (match-string 1 rl-dir
)))
523 (if (and (file-directory-p dir
) (file-writable-p dir
))
525 (substring rl-dir
(match-end 0))))))
526 (unless (file-writable-p rl-dir
)
527 (error "No writable revlib directory found"))
528 (message "Revlib at %s" rl-dir
)
529 (let* ((archives (directory-files rl-dir
'full
"[^.]\\|..."))
532 (mapcar (lambda (dir)
533 (when (file-directory-p dir
)
534 (directory-files dir
'full
"[^.]\\|...")))
538 (mapcar (lambda (dir)
539 (when (file-directory-p dir
)
540 (directory-files dir
'full
"[^.]\\|...")))
544 (mapcar (lambda (dir)
545 (when (file-directory-p dir
)
546 (directory-files dir
'full
"--.*--")))
548 (mapc 'vc-arch-trim-one-revlib versions
))
551 ;;; Less obvious implementations.
553 (defun vc-arch-find-version (file rev buffer
)
554 (let ((out (make-temp-file "vc-out")))
558 (vc-arch-command (current-buffer) 1 nil
"file-diffs" file rev
)
559 (call-process-region (point-min) (point-max)
560 "patch" nil nil nil
"-R" "-o" out file
))
561 (with-current-buffer buffer
562 (insert-file-contents out
)))
567 ;; arch-tag: a35c7c1c-5237-429d-88ef-3d718fd2e704
568 ;;; vc-arch.el ends here