1 ;;; vc-arch.el --- VC backend for the Arch version-control system
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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 3, 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 directory listings do not work.
58 (eval-when-compile (require 'vc
) (require 'cl
))
60 ;;; Properties of the backend
62 (defun vc-arch-revision-granularity () 'repository
)
63 (defun vc-arch-checkout-model (files) 'implicit
)
66 ;;; Customization options
69 (defvar vc-arch-command
70 (let ((candidates '("tla" "baz")))
71 (while (and candidates
(not (executable-find (car candidates
))))
72 (setq candidates
(cdr candidates
)))
73 (or (car candidates
) "tla")))
75 ;; Clear up the cache to force vc-call to check again and discover
76 ;; new functions when we reload this file.
77 (put 'Arch
'vc-functions nil
)
79 ;;;###autoload (defun vc-arch-registered (file)
80 ;;;###autoload (if (vc-find-root file "{arch}/=tagging-method")
82 ;;;###autoload (load "vc-arch")
83 ;;;###autoload (vc-arch-registered file))))
85 (defun vc-arch-add-tagline ()
86 "Add an `arch-tag' to the end of the current file."
88 (comment-normalize-vars)
89 (goto-char (point-max))
91 (skip-chars-forward " \t\n")
93 ((not (bolp)) (insert "\n\n"))
94 ((not (eq ?
\n (char-before (1- (point))))) (insert "\n")))
96 (idfile (and buffer-file-name
99 (file-name-nondirectory buffer-file-name
)
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.
106 (insert-file-contents idfile
)
108 (delete-file idfile
))
110 (call-process "uuidgen" nil t
)
111 (file-error (insert (format "%s <%s> %s"
112 (current-time-string)
114 (+ (nth 2 (current-time))
116 (comment-region beg
(point))))
118 (defconst vc-arch-tagline-re
"^\\W*arch-tag:[ \t]*\\(.*[^ \t\n]\\)")
120 (defmacro vc-with-current-file-buffer
(file &rest body
)
121 (declare (indent 2) (debug t
))
122 `(let ((-kill-buf- nil
)
124 (with-current-buffer (or (find-buffer-visiting -file-
)
125 (setq -kill-buf-
(generate-new-buffer " temp")))
126 ;; Avoid find-file-literally since it can do many undesirable extra
127 ;; things (among which, call us back into an infinite loop).
128 (if -kill-buf-
(insert-file-contents -file-
))
131 (if (buffer-live-p -kill-buf-
) (kill-buffer -kill-buf-
))))))
133 (defun vc-arch-file-source-p (file)
134 "Can return nil, `maybe' or a non-nil value.
135 Only the value `maybe' can be trusted :-(."
136 ;; FIXME: Check the tag and name of parent dirs.
137 (unless (string-match "\\`[,+]" (file-name-nondirectory file
))
138 (or (string-match "\\`{arch}/"
139 (file-relative-name file
(vc-arch-root file
)))
141 ;; Check the presence of an ID file.
143 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
144 (file-name-directory file
)))
145 ;; Check the presence of a tagline.
146 (vc-with-current-file-buffer file
148 (goto-char (point-max))
149 (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
151 (goto-char (point-min))
152 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))))
153 ;; FIXME: check =tagging-method to see whether untagged files might
156 (find-file-noselect (expand-file-name "{arch}/=tagging-method"
157 (vc-arch-root file
)))
158 (let ((untagged-source t
)) ;Default is `names'.
160 (goto-char (point-min))
161 (if (re-search-forward "^[ \t]*\\(\\(tagline\\|implicit\\|names\\)\\|explicit\\)" nil t
)
162 (setq untagged-source
(match-end 2)))
163 (if (re-search-forward "^[ \t]*untagged-source[ \t]+\\(\\(source\\)\\|precious\\|backup\\|junk\\|unrecognized\\)" nil t
)
164 (setq untagged-source
(match-end 2))))
165 (if untagged-source
'maybe
))))))
167 (defun vc-arch-file-id (file)
168 ;; Don't include the kind of ID this is because it seems to be too messy.
169 (let ((idfile (expand-file-name
170 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
171 (file-name-directory file
))))
172 (if (file-exists-p idfile
)
174 (insert-file-contents idfile
)
175 (looking-at ".*[^ \n\t]")
177 (with-current-buffer (find-file-noselect file
)
179 (goto-char (point-max))
180 (if (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
182 (goto-char (point-min))
183 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))
185 (concat "./" (file-relative-name file
(vc-arch-root file
)))))))))
187 (defun vc-arch-tagging-method (file)
190 (expand-file-name "{arch}/=tagging-method" (vc-arch-root file
)))
192 (goto-char (point-min))
193 (if (re-search-forward
194 "^[ \t]*\\(tagline\\|implicit\\|names\\|explicit\\)" nil t
)
195 (intern (match-string 1))
198 (defun vc-arch-root (file)
199 "Return the root directory of a Arch project, if any."
200 (or (vc-file-getprop file
'arch-root
)
201 ;; Check the =tagging-method, in case someone naively manually
202 ;; creates a {arch} directory somewhere.
203 (let ((root (vc-find-root file
"{arch}/=tagging-method")))
206 file
'arch-root root
)))))
208 (defun vc-arch-register (files &optional rev comment
)
209 (if rev
(error "Explicit initial revision not supported for Arch"))
211 (let ((tagmet (vc-arch-tagging-method file
)))
212 (if (and (memq tagmet
'(tagline implicit
)) comment-start
)
213 (with-current-buffer (find-file-noselect file
)
214 (if (buffer-modified-p)
215 (error "Save %s first" (buffer-name)))
216 (vc-arch-add-tagline)
218 (vc-arch-command nil
0 files
"add"))
220 (defun vc-arch-registered (file)
221 ;; Don't seriously check whether it's source or not. Checking would
222 ;; require running TLA, so it's better to not do it, so it also works if
223 ;; TLA is not installed.
224 (and (vc-arch-root file
)
225 (vc-arch-file-source-p file
)))
227 (defun vc-arch-default-version (file)
228 (or (vc-file-getprop (vc-arch-root file
) 'arch-default-version
)
229 (let* ((root (vc-arch-root file
))
230 (f (expand-file-name "{arch}/++default-version" root
)))
231 (if (file-readable-p f
)
233 root
'arch-default-version
235 (insert-file-contents f
)
236 ;; Strip the terminating newline.
237 (buffer-substring (point-min) (1- (point-max)))))))))
239 (defun vc-arch-workfile-unchanged-p (file)
240 "Check if FILE is unchanged by diffing against the master version.
241 Return non-nil if FILE is unchanged."
244 (defun vc-arch-state (file)
245 ;; There's no checkout operation and merging is not done from VC
246 ;; so the only operation that's state dependent that VC supports is commit
247 ;; which is only activated if the file is `edited'.
248 (let* ((root (vc-arch-root file
))
249 (ver (vc-arch-default-version file
))
250 (pat (concat "\\`" (subst-char-in-string ?
/ ?% ver
)))
251 (dir (expand-file-name ",,inode-sigs/"
252 (expand-file-name "{arch}" root
)))
254 (dolist (f (if (file-directory-p dir
) (directory-files dir t pat
)))
255 (if (or (not sigfile
) (file-newer-than-file-p f sigfile
))
258 'edited
;We know nothing.
259 (let ((id (vc-arch-file-id file
)))
260 (setq id
(replace-regexp-in-string "[ \t]" "_" id
))
261 (with-current-buffer (find-file-noselect sigfile
)
262 (goto-char (point-min))
263 (while (and (search-forward id nil
'move
)
265 (goto-char (- (match-beginning 0) 2))
266 ;; For `names', the lines start with `?./foo/bar'.
267 ;; For others there's 2 chars before the ./foo/bar.
268 (or (not (or (bolp) (looking-at "\n?")))
269 ;; Ignore E_ entries used for foo.id files.
270 (looking-at "E_")))))
273 (if (equal (file-name-nondirectory sigfile
)
274 (subst-char-in-string
275 ?
/ ?%
(vc-arch-working-revision file
)))
277 ;; Might be `added' or `up-to-date' as well.
278 ;; FIXME: Check in the patch logs to find out.
280 ;; Found the ID, let's check the inode.
281 (if (not (re-search-forward
282 "\t.*mtime=\\([0-9]+\\):size=\\([0-9]+\\)"
283 (line-end-position) t
))
284 ;; Buh? Unexpected format.
286 (let ((ats (file-attributes file
)))
287 (if (and (eq (nth 7 ats
) (string-to-number (match-string 2)))
288 (equal (format-time-string "%s" (nth 5 ats
))
293 (defun vc-arch-working-revision (file)
294 (let* ((root (expand-file-name "{arch}" (vc-arch-root file
)))
295 (defbranch (vc-arch-default-version file
)))
296 (when (and defbranch
(string-match "\\`\\(.+@[^/\n]+\\)/\\(\\(\\(.*?\\)\\(?:--.*\\)?\\)--.*\\)\\'" defbranch
))
297 (let* ((archive (match-string 1 defbranch
))
298 (category (match-string 4 defbranch
))
299 (branch (match-string 3 defbranch
))
300 (version (match-string 2 defbranch
))
301 (sealed nil
) (rev-nb 0)
304 (setq logdir
(expand-file-name category root
))
305 (setq logdir
(expand-file-name branch logdir
))
306 (setq logdir
(expand-file-name version logdir
))
307 (setq logdir
(expand-file-name archive logdir
))
308 (setq logdir
(expand-file-name "patch-log" logdir
))
309 (dolist (file (if (file-directory-p logdir
) (directory-files logdir
)))
310 ;; Revision names go: base-0, patch-N, version-0, versionfix-M.
311 (when (and (eq (aref file
0) ?v
) (not sealed
))
312 (setq sealed t rev-nb
0))
313 (if (and (string-match "-\\([0-9]+\\)\\'" file
)
314 (setq tmp
(string-to-number (match-string 1 file
)))
315 (or (not sealed
) (eq (aref file
0) ?v
))
317 (setq rev-nb tmp rev file
)))
318 ;; Use "none-000" if the tree hasn't yet been committed on the
319 ;; default branch. We'll then get "Arch:000[branch]" on the mode-line.
320 (concat defbranch
"--" (or rev
"none-000"))))))
323 (defcustom vc-arch-mode-line-rewrite
324 '(("\\`.*--\\(.*--.*\\)--\\(v?\\).*-\\([0-9]+\\)\\'" .
"\\2\\3[\\1]"))
325 "Rewrite rules to shorten Arch's revision names on the mode-line."
326 :type
'(repeat (cons regexp string
))
329 (defun vc-arch-mode-line-string (file)
330 "Return string for placement in modeline by `vc-mode-line' for FILE."
331 (let ((rev (vc-working-revision file
)))
332 (dolist (rule vc-arch-mode-line-rewrite
)
333 (if (string-match (car rule
) rev
)
334 (setq rev
(replace-match (cdr rule
) t nil rev
))))
336 (case (vc-state file
)
337 ((up-to-date needs-update
) ?-
)
342 (defun vc-arch-diff3-rej-p (rej)
343 (let ((attrs (file-attributes rej
)))
344 (and attrs
(< (nth 7 attrs
) 60)
346 (insert-file-contents rej
)
347 (goto-char (point-min))
348 (looking-at "Conflicts occured, diff3 conflict markers left in file\\.")))))
350 (defun vc-arch-delete-rej-if-obsolete ()
351 "For use in `after-save-hook'."
353 (let ((rej (concat buffer-file-name
".rej")))
354 (when (and buffer-file-name
(vc-arch-diff3-rej-p rej
))
355 (unless (re-search-forward "^<<<<<<< " nil t
)
356 ;; The .rej file is obsolete.
357 (condition-case nil
(delete-file rej
) (error nil
))
358 ;; Remove the hook so that it is not called multiple times.
359 (remove-hook 'after-save-hook
'vc-arch-delete-rej-if-obsolete t
))))))
361 (defun vc-arch-find-file-hook ()
362 (let ((rej (concat buffer-file-name
".rej")))
363 (when (and buffer-file-name
(file-exists-p rej
))
364 (if (vc-arch-diff3-rej-p rej
)
366 (goto-char (point-min))
367 (if (not (re-search-forward "^<<<<<<< " nil t
))
368 ;; The .rej file is obsolete.
369 (condition-case nil
(delete-file rej
) (error nil
))
371 (add-hook 'after-save-hook
372 'vc-arch-delete-rej-if-obsolete nil t
)
373 (message "There are unresolved conflicts in this file")))
374 (message "There are unresolved conflicts in %s"
375 (file-name-nondirectory rej
))))))
377 (defun vc-arch-checkin (files rev comment
)
378 (if rev
(error "Committing to a specific revision is unsupported"))
379 ;; FIXME: This implementation probably only works for singleton filesets
380 (let ((summary (file-relative-name (car files
) (vc-arch-root (car files
)))))
381 ;; Extract a summary from the comment.
382 (when (or (string-match "\\`Summary:[ \t]*\\(.*[^ \t\n]\\)\\([ \t]*\n\\)*" comment
)
383 (string-match "\\`[ \t]*\\(.*[^ \t\n]\\)[ \t]*\\(\n?\\'\\|\n\\([ \t]*\n\\)+\\)" comment
))
384 (setq summary
(match-string 1 comment
))
385 (setq comment
(substring comment
(match-end 0))))
386 (vc-arch-command nil
0 files
"commit" "-s" summary
"-L" comment
"--"
387 (vc-switches 'Arch
'checkin
))))
389 (defun vc-arch-diff (files &optional oldvers newvers buffer
)
390 "Get a difference report using Arch between two versions of FILES."
391 ;; FIXME: This implementation only works for singleton filesets. To make
392 ;; it work for more cases, we have to either call `file-diffs' manually on
393 ;; each and every `file' in the fileset, or use `changes --diffs' (and
394 ;; variants) and maybe filter the output with `filterdiff' to only include
395 ;; the files in which we're interested.
396 (let ((file (car files
)))
398 (vc-up-to-date-p file
)
399 (equal newvers
(vc-working-revision file
)))
400 ;; Newvers is the base revision and the current file is unchanged,
401 ;; so we can diff with the current file.
404 (error "Diffing specific revisions not implemented")
405 (let* ((async (not vc-disable-async-diff
))
406 ;; Run the command from the root dir.
407 (default-directory (vc-arch-root file
))
410 (or buffer
"*vc-diff*")
413 ;; Arch does not support the typical flags.
414 ;; (vc-switches 'Arch 'diff)
415 (file-relative-name file
)
416 (if (equal oldvers
(vc-working-revision file
))
419 (if async
1 status
))))) ; async diff, pessimistic assumption.
421 (defun vc-arch-delete-file (file)
422 (vc-arch-command nil
0 file
"rm"))
424 (defun vc-arch-rename-file (old new
)
425 (vc-arch-command nil
0 new
"mv" (file-relative-name old
)))
427 (defalias 'vc-arch-responsible-p
'vc-arch-root
)
429 (defun vc-arch-command (buffer okstatus file
&rest flags
)
430 "A wrapper around `vc-do-command' for use in vc-arch.el."
431 (apply 'vc-do-command buffer okstatus vc-arch-command file flags
))
433 (defun vc-arch-init-revision () nil
)
435 ;;; Completion of versions and revisions.
437 (defun vc-arch--version-completion-table (root string
)
441 (when (string-match "/\\([^/]+\\)/\\([^/]+\\)\\'" d
)
442 (concat (match-string 2 d
) "/" (match-string 1 d
))))
443 (let ((default-directory root
))
444 (file-expand-wildcards
446 (if (string-match "/" string
)
447 (concat (substring string
(match-end 0))
448 "*/" (substring string
0 (match-beginning 0)))
449 (concat "*/" string
))
452 (defun vc-arch-revision-completion-table (files)
453 (lexical-let ((files files
))
454 (lambda (string pred action
)
455 ;; FIXME: complete revision patches as well.
456 (let* ((root (expand-file-name "{arch}" (vc-arch-root (car files
))))
457 (table (vc-arch--version-completion-table root string
)))
458 (complete-with-action action table string pred
)))))
460 ;;; Trimming revision libraries.
462 ;; This code is not directly related to VC and there are many variants of
463 ;; this functionality available as scripts, but I like this version better,
464 ;; so maybe others will like it too.
466 (defun vc-arch-trim-find-least-useful-rev (revs)
467 (let* ((first (pop revs
))
470 ;; We try to give more importance to recent revisions. The idea is
471 ;; that it's OK if checking out a revision 1000-patch-old is ten
472 ;; times slower than checking out a revision 100-patch-old. But at
473 ;; the same time a 2-patch-old rev isn't really ten times more
474 ;; important than a 20-patch-old, so we use an arbitrary constant
475 ;; "100" to reduce this effect for recent revisions. Making this
476 ;; constant a float has the side effect of causing the subsequent
477 ;; computations to be done as floats as well.
478 (max (+ 100.0 (car (or (car (last revs
)) third
))))
479 (cost (lambda () (/ (- (car third
) (car first
)) (- max
(car second
)))))
481 (mincost (funcall cost
)))
485 (setq third
(pop revs
))
486 (when (< (funcall cost
) mincost
)
488 (setq mincost
(funcall cost
))))
491 (defun vc-arch-trim-make-sentinel (revs)
492 (if (null revs
) (lambda (proc msg
) (message "VC-Arch trimming ... done"))
494 (message "VC-Arch trimming %s..." ',(file-name-nondirectory (car revs
)))
495 (rename-file ,(car revs
) ,(concat (car revs
) "*rm*"))
496 (setq proc
(start-process "vc-arch-trim" nil
497 "rm" "-rf" ',(concat (car revs
) "*rm*")))
498 (set-process-sentinel proc
(vc-arch-trim-make-sentinel ',(cdr revs
))))))
500 (defun vc-arch-trim-one-revlib (dir)
501 "Delete half of the revisions in the revision library."
502 (interactive "Ddirectory: ")
507 (when (string-match "-\\([0-9]+\\)\\'" f
)
508 (cons (string-to-number (match-string 1 f
)) f
)))
509 (directory-files dir nil nil
'nosort
)))
513 (dotimes (i (/ (length revs
) 2))
514 (let ((minrev (vc-arch-trim-find-least-useful-rev revs
)))
515 (setq revs
(delq minrev revs
))
516 (push minrev subdirs
)))
517 (funcall (vc-arch-trim-make-sentinel
518 (mapcar (lambda (x) (expand-file-name (cdr x
) dir
)) subdirs
))
521 (defun vc-arch-trim-revlib ()
522 "Delete half of the revisions in the revision library."
524 (let ((rl-dir (with-output-to-string
525 (call-process vc-arch-command nil standard-output nil
526 "my-revision-library"))))
527 (while (string-match "\\(.*\\)\n" rl-dir
)
528 (let ((dir (match-string 1 rl-dir
)))
530 (if (and (file-directory-p dir
) (file-writable-p dir
))
532 (substring rl-dir
(match-end 0))))))
533 (unless (file-writable-p rl-dir
)
534 (error "No writable revlib directory found"))
535 (message "Revlib at %s" rl-dir
)
536 (let* ((archives (directory-files rl-dir
'full
"[^.]\\|..."))
539 (mapcar (lambda (dir)
540 (when (file-directory-p dir
)
541 (directory-files dir
'full
"[^.]\\|...")))
545 (mapcar (lambda (dir)
546 (when (file-directory-p dir
)
547 (directory-files dir
'full
"[^.]\\|...")))
551 (mapcar (lambda (dir)
552 (when (file-directory-p dir
)
553 (directory-files dir
'full
"--.*--")))
555 (mapc 'vc-arch-trim-one-revlib versions
))
558 (defvar vc-arch-extra-menu-map
559 (let ((map (make-sparse-keymap)))
560 (define-key map
[add-tagline
]
561 '(menu-item "Add tagline" vc-arch-add-tagline
))
564 (defun vc-arch-extra-menu () vc-arch-extra-menu-map
)
567 ;;; Less obvious implementations.
569 (defun vc-arch-find-revision (file rev buffer
)
570 (let ((out (make-temp-file "vc-out")))
574 (vc-arch-command (current-buffer) 1 nil
"file-diffs" file rev
)
575 (call-process-region (point-min) (point-max)
576 "patch" nil nil nil
"-R" "-o" out file
))
577 (with-current-buffer buffer
578 (insert-file-contents out
)))
583 ;; arch-tag: a35c7c1c-5237-429d-88ef-3d718fd2e704
584 ;;; vc-arch.el ends here