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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;; The home page of the Arch version control system is at
27 ;; http://www.gnuarch.org/
29 ;; This is derived from vc-mcvs.el as follows:
30 ;; - cp vc-mcvs.el vc-arch.el and then M-% mcvs RET arch RET
32 ;; Then of course started the hacking.
34 ;; What has been partly tested:
36 ;; - C-x v = without any prefix arg.
37 ;; - C-x v v to commit a change to a single file.
41 ;; - *VC-log*'s initial content lacks the `Summary:' lines.
42 ;; - All files under the tree are considered as "under Arch's control"
43 ;; without regards to =tagging-method and such.
44 ;; - Files are always considered as `edited'.
45 ;; - C-x v l does not work.
46 ;; - C-x v i does not work.
47 ;; - C-x v ~ does not work.
48 ;; - C-x v u does not work.
49 ;; - C-x v s does not work.
50 ;; - C-x v r does not work.
51 ;; - VC directory listings do not work.
56 (eval-when-compile (require 'vc
) (require 'cl
))
58 ;;; Properties of the backend
60 (defun vc-arch-revision-granularity () 'repository
)
61 (defun vc-arch-checkout-model (files) 'implicit
)
64 ;;; Customization options
67 (define-obsolete-variable-alias 'vc-arch-command
'vc-arch-program
"23.1")
69 (defcustom vc-arch-program
70 (let ((candidates '("tla" "baz")))
71 (while (and candidates
(not (executable-find (car candidates
))))
72 (setq candidates
(cdr candidates
)))
73 (or (car candidates
) "tla"))
74 "Name of the Arch executable."
78 ;; Clear up the cache to force vc-call to check again and discover
79 ;; new functions when we reload this file.
80 (put 'Arch
'vc-functions nil
)
82 ;;;###autoload (defun vc-arch-registered (file)
83 ;;;###autoload (if (vc-find-root file "{arch}/=tagging-method")
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."
91 (comment-normalize-vars)
92 (goto-char (point-max))
94 (skip-chars-forward " \t\n")
96 ((not (bolp)) (insert "\n\n"))
97 ((not (eq ?
\n (char-before (1- (point))))) (insert "\n")))
99 (idfile (and buffer-file-name
102 (file-name-nondirectory buffer-file-name
)
104 (file-name-directory buffer-file-name
)))))
105 (insert "arch-tag: ")
106 (if (and idfile
(file-exists-p idfile
))
107 ;; If the file is unreadable, we do want to get an error here.
109 (insert-file-contents idfile
)
111 (delete-file idfile
))
113 (call-process "uuidgen" nil t
)
114 (file-error (insert (format "%s <%s> %s"
115 (current-time-string)
117 (+ (nth 2 (current-time))
119 (comment-region beg
(point))))
121 (defconst vc-arch-tagline-re
"^\\W*arch-tag:[ \t]*\\(.*[^ \t\n]\\)")
123 (defmacro vc-with-current-file-buffer
(file &rest body
)
124 (declare (indent 2) (debug t
))
125 `(let ((-kill-buf- nil
)
127 (with-current-buffer (or (find-buffer-visiting -file-
)
128 (setq -kill-buf-
(generate-new-buffer " temp")))
129 ;; Avoid find-file-literally since it can do many undesirable extra
130 ;; things (among which, call us back into an infinite loop).
131 (if -kill-buf-
(insert-file-contents -file-
))
134 (if (buffer-live-p -kill-buf-
) (kill-buffer -kill-buf-
))))))
136 (defun vc-arch-file-source-p (file)
137 "Can return nil, `maybe' or a non-nil value.
138 Only the value `maybe' can be trusted :-(."
139 ;; FIXME: Check the tag and name of parent dirs.
140 (unless (string-match "\\`[,+]" (file-name-nondirectory file
))
141 (or (string-match "\\`{arch}/"
142 (file-relative-name file
(vc-arch-root file
)))
144 ;; Check the presence of an ID file.
146 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
147 (file-name-directory file
)))
148 ;; Check the presence of a tagline.
149 (vc-with-current-file-buffer file
151 (goto-char (point-max))
152 (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
154 (goto-char (point-min))
155 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))))
156 ;; FIXME: check =tagging-method to see whether untagged files might
159 (find-file-noselect (expand-file-name "{arch}/=tagging-method"
160 (vc-arch-root file
)))
161 (let ((untagged-source t
)) ;Default is `names'.
163 (goto-char (point-min))
164 (if (re-search-forward "^[ \t]*\\(\\(tagline\\|implicit\\|names\\)\\|explicit\\)" nil t
)
165 (setq untagged-source
(match-end 2)))
166 (if (re-search-forward "^[ \t]*untagged-source[ \t]+\\(\\(source\\)\\|precious\\|backup\\|junk\\|unrecognized\\)" nil t
)
167 (setq untagged-source
(match-end 2))))
168 (if untagged-source
'maybe
))))))
170 (defun vc-arch-file-id (file)
171 ;; Don't include the kind of ID this is because it seems to be too messy.
172 (let ((idfile (expand-file-name
173 (concat ".arch-ids/" (file-name-nondirectory file
) ".id")
174 (file-name-directory file
))))
175 (if (file-exists-p idfile
)
177 (insert-file-contents idfile
)
178 (looking-at ".*[^ \n\t]")
180 (with-current-buffer (find-file-noselect file
)
182 (goto-char (point-max))
183 (if (or (re-search-backward vc-arch-tagline-re
(- (point) 1000) t
)
185 (goto-char (point-min))
186 (re-search-forward vc-arch-tagline-re
(+ (point) 1000) t
)))
188 (concat "./" (file-relative-name file
(vc-arch-root file
)))))))))
190 (defun vc-arch-tagging-method (file)
193 (expand-file-name "{arch}/=tagging-method" (vc-arch-root file
)))
195 (goto-char (point-min))
196 (if (re-search-forward
197 "^[ \t]*\\(tagline\\|implicit\\|names\\|explicit\\)" nil t
)
198 (intern (match-string 1))
201 (defun vc-arch-root (file)
202 "Return the root directory of a Arch project, if any."
203 (or (vc-file-getprop file
'arch-root
)
204 ;; Check the =tagging-method, in case someone naively manually
205 ;; creates a {arch} directory somewhere.
206 (let ((root (vc-find-root file
"{arch}/=tagging-method")))
209 file
'arch-root root
)))))
211 (defun vc-arch-register (files &optional rev comment
)
212 (if rev
(error "Explicit initial revision not supported for Arch"))
214 (let ((tagmet (vc-arch-tagging-method file
)))
215 (if (and (memq tagmet
'(tagline implicit
)) comment-start
)
216 (with-current-buffer (find-file-noselect file
)
217 (if (buffer-modified-p)
218 (error "Save %s first" (buffer-name)))
219 (vc-arch-add-tagline)
221 (vc-arch-command nil
0 files
"add"))
223 (defun vc-arch-registered (file)
224 ;; Don't seriously check whether it's source or not. Checking would
225 ;; require running TLA, so it's better to not do it, so it also works if
226 ;; TLA is not installed.
227 (and (vc-arch-root file
)
228 (vc-arch-file-source-p file
)))
230 (defun vc-arch-default-version (file)
231 (or (vc-file-getprop (vc-arch-root file
) 'arch-default-version
)
232 (let* ((root (vc-arch-root file
))
233 (f (expand-file-name "{arch}/++default-version" root
)))
234 (if (file-readable-p f
)
236 root
'arch-default-version
238 (insert-file-contents f
)
239 ;; Strip the terminating newline.
240 (buffer-substring (point-min) (1- (point-max)))))))))
242 (defun vc-arch-workfile-unchanged-p (file)
243 "Check if FILE is unchanged by diffing against the master version.
244 Return non-nil if FILE is unchanged."
247 (defun vc-arch-state (file)
248 ;; There's no checkout operation and merging is not done from VC
249 ;; so the only operation that's state dependent that VC supports is commit
250 ;; which is only activated if the file is `edited'.
251 (let* ((root (vc-arch-root file
))
252 (ver (vc-arch-default-version file
))
253 (pat (concat "\\`" (subst-char-in-string ?
/ ?% ver
)))
254 (dir (expand-file-name ",,inode-sigs/"
255 (expand-file-name "{arch}" root
)))
257 (dolist (f (if (file-directory-p dir
) (directory-files dir t pat
)))
258 (if (or (not sigfile
) (file-newer-than-file-p f sigfile
))
261 'edited
;We know nothing.
262 (let ((id (vc-arch-file-id file
)))
263 (setq id
(replace-regexp-in-string "[ \t]" "_" id
))
264 (with-current-buffer (find-file-noselect sigfile
)
265 (goto-char (point-min))
266 (while (and (search-forward id nil
'move
)
268 (goto-char (- (match-beginning 0) 2))
269 ;; For `names', the lines start with `?./foo/bar'.
270 ;; For others there's 2 chars before the ./foo/bar.
271 (or (not (or (bolp) (looking-at "\n?")))
272 ;; Ignore E_ entries used for foo.id files.
273 (looking-at "E_")))))
276 (if (equal (file-name-nondirectory sigfile
)
277 (subst-char-in-string
278 ?
/ ?%
(vc-arch-working-revision file
)))
280 ;; Might be `added' or `up-to-date' as well.
281 ;; FIXME: Check in the patch logs to find out.
283 ;; Found the ID, let's check the inode.
284 (if (not (re-search-forward
285 "\t.*mtime=\\([0-9]+\\):size=\\([0-9]+\\)"
286 (line-end-position) t
))
287 ;; Buh? Unexpected format.
289 (let ((ats (file-attributes file
)))
290 (if (and (eq (nth 7 ats
) (string-to-number (match-string 2)))
291 (equal (format-time-string "%s" (nth 5 ats
))
296 (defun vc-arch-dir-status (dir callback
)
297 "Run 'tla inventory' for DIR and pass results to CALLBACK.
298 CALLBACK expects (ENTRIES &optional MORE-TO-COME); see
300 (let ((default-directory dir
))
301 (vc-arch-command t
'async nil
"changes"))
302 ;; The updating could be done asynchronously.
304 `(vc-arch-after-dir-status ',callback
)))
306 (defun vc-arch-after-dir-status (callback)
307 (let* ((state-map '(("M " . edited
)
308 ("Mb" . edited
) ;binary
310 ("D/" . removed
) ;directory
312 ("A/" . added
) ;directory
314 ("/>" . renamed
) ;directory
315 ("lf" . symlink-to-file
)
316 ("fl" . file-to-symlink
)
317 ("--" . permissions-changed
)
318 ("-/" . permissions-changed
) ;directory
320 (state-map-regexp (regexp-opt (mapcar 'car state-map
) t
))
321 (entry-regexp (concat "^" state-map-regexp
" \\(.*\\)$"))
323 (goto-char (point-min))
324 ;;(message "Got %s" (buffer-string))
325 (while (re-search-forward entry-regexp nil t
)
326 (let* ((state-string (match-string 1))
327 (state (cdr (assoc state-string state-map
)))
328 (filename (match-string 2)))
329 (push (list filename state
) result
)))
331 (funcall callback result nil
)))
333 (defun vc-arch-working-revision (file)
334 (let* ((root (expand-file-name "{arch}" (vc-arch-root file
)))
335 (defbranch (vc-arch-default-version file
)))
336 (when (and defbranch
(string-match "\\`\\(.+@[^/\n]+\\)/\\(\\(\\(.*?\\)\\(?:--.*\\)?\\)--.*\\)\\'" defbranch
))
337 (let* ((archive (match-string 1 defbranch
))
338 (category (match-string 4 defbranch
))
339 (branch (match-string 3 defbranch
))
340 (version (match-string 2 defbranch
))
341 (sealed nil
) (rev-nb 0)
344 (setq logdir
(expand-file-name category root
))
345 (setq logdir
(expand-file-name branch logdir
))
346 (setq logdir
(expand-file-name version logdir
))
347 (setq logdir
(expand-file-name archive logdir
))
348 (setq logdir
(expand-file-name "patch-log" logdir
))
349 (dolist (file (if (file-directory-p logdir
) (directory-files logdir
)))
350 ;; Revision names go: base-0, patch-N, version-0, versionfix-M.
351 (when (and (eq (aref file
0) ?v
) (not sealed
))
352 (setq sealed t rev-nb
0))
353 (if (and (string-match "-\\([0-9]+\\)\\'" file
)
354 (setq tmp
(string-to-number (match-string 1 file
)))
355 (or (not sealed
) (eq (aref file
0) ?v
))
357 (setq rev-nb tmp rev file
)))
358 ;; Use "none-000" if the tree hasn't yet been committed on the
359 ;; default branch. We'll then get "Arch:000[branch]" on the mode-line.
360 (concat defbranch
"--" (or rev
"none-000"))))))
363 (defcustom vc-arch-mode-line-rewrite
364 '(("\\`.*--\\(.*--.*\\)--\\(v?\\).*-\\([0-9]+\\)\\'" .
"\\2\\3[\\1]"))
365 "Rewrite rules to shorten Arch's revision names on the mode-line."
366 :type
'(repeat (cons regexp string
))
369 (defun vc-arch-mode-line-string (file)
370 "Return string for placement in modeline by `vc-mode-line' for FILE."
371 (let ((rev (vc-working-revision file
)))
372 (dolist (rule vc-arch-mode-line-rewrite
)
373 (if (string-match (car rule
) rev
)
374 (setq rev
(replace-match (cdr rule
) t nil rev
))))
376 (case (vc-state file
)
377 ((up-to-date needs-update
) ?-
)
382 (defun vc-arch-diff3-rej-p (rej)
383 (let ((attrs (file-attributes rej
)))
384 (and attrs
(< (nth 7 attrs
) 60)
386 (insert-file-contents rej
)
387 (goto-char (point-min))
388 (looking-at "Conflicts occured, diff3 conflict markers left in file\\.")))))
390 (defun vc-arch-delete-rej-if-obsolete ()
391 "For use in `after-save-hook'."
393 (let ((rej (concat buffer-file-name
".rej")))
394 (when (and buffer-file-name
(vc-arch-diff3-rej-p rej
))
395 (unless (re-search-forward "^<<<<<<< " nil t
)
396 ;; The .rej file is obsolete.
397 (condition-case nil
(delete-file rej
) (error nil
))
398 ;; Remove the hook so that it is not called multiple times.
399 (remove-hook 'after-save-hook
'vc-arch-delete-rej-if-obsolete t
))))))
401 (defun vc-arch-find-file-hook ()
402 (let ((rej (concat buffer-file-name
".rej")))
403 (when (and buffer-file-name
(file-exists-p rej
))
404 (if (vc-arch-diff3-rej-p rej
)
406 (goto-char (point-min))
407 (if (not (re-search-forward "^<<<<<<< " nil t
))
408 ;; The .rej file is obsolete.
409 (condition-case nil
(delete-file rej
) (error nil
))
411 (add-hook 'after-save-hook
412 'vc-arch-delete-rej-if-obsolete nil t
)
413 (message "There are unresolved conflicts in this file")))
414 (message "There are unresolved conflicts in %s"
415 (file-name-nondirectory rej
))))))
417 (defun vc-arch-checkin (files rev comment
)
418 (if rev
(error "Committing to a specific revision is unsupported"))
419 ;; FIXME: This implementation probably only works for singleton filesets
420 (let ((summary (file-relative-name (car files
) (vc-arch-root (car files
)))))
421 ;; Extract a summary from the comment.
422 (when (or (string-match "\\`Summary:[ \t]*\\(.*[^ \t\n]\\)\\([ \t]*\n\\)*" comment
)
423 (string-match "\\`[ \t]*\\(.*[^ \t\n]\\)[ \t]*\\(\n?\\'\\|\n\\([ \t]*\n\\)+\\)" comment
))
424 (setq summary
(match-string 1 comment
))
425 (setq comment
(substring comment
(match-end 0))))
426 (vc-arch-command nil
0 files
"commit" "-s" summary
"-L" comment
"--"
427 (vc-switches 'Arch
'checkin
))))
429 (defun vc-arch-diff (files &optional oldvers newvers buffer
)
430 "Get a difference report using Arch between two versions of FILES."
431 ;; FIXME: This implementation only works for singleton filesets. To make
432 ;; it work for more cases, we have to either call `file-diffs' manually on
433 ;; each and every `file' in the fileset, or use `changes --diffs' (and
434 ;; variants) and maybe filter the output with `filterdiff' to only include
435 ;; the files in which we're interested.
436 (let ((file (car files
)))
438 (vc-up-to-date-p file
)
439 (equal newvers
(vc-working-revision file
)))
440 ;; Newvers is the base revision and the current file is unchanged,
441 ;; so we can diff with the current file.
444 (error "Diffing specific revisions not implemented")
445 (let* ((async (not vc-disable-async-diff
))
446 ;; Run the command from the root dir.
447 (default-directory (vc-arch-root file
))
450 (or buffer
"*vc-diff*")
453 ;; Arch does not support the typical flags.
454 ;; (vc-switches 'Arch 'diff)
455 (file-relative-name file
)
456 (if (equal oldvers
(vc-working-revision file
))
459 (if async
1 status
))))) ; async diff, pessimistic assumption.
461 (defun vc-arch-delete-file (file)
462 (vc-arch-command nil
0 file
"rm"))
464 (defun vc-arch-rename-file (old new
)
465 (vc-arch-command nil
0 new
"mv" (file-relative-name old
)))
467 (defalias 'vc-arch-responsible-p
'vc-arch-root
)
469 (defun vc-arch-command (buffer okstatus file
&rest flags
)
470 "A wrapper around `vc-do-command' for use in vc-arch.el."
471 (apply 'vc-do-command
(or buffer
"*vc*") okstatus vc-arch-program file flags
))
473 (defun vc-arch-init-revision () nil
)
475 ;;; Completion of versions and revisions.
477 (defun vc-arch--version-completion-table (root string
)
481 (when (string-match "/\\([^/]+\\)/\\([^/]+\\)\\'" d
)
482 (concat (match-string 2 d
) "/" (match-string 1 d
))))
483 (let ((default-directory root
))
484 (file-expand-wildcards
486 (if (string-match "/" string
)
487 (concat (substring string
(match-end 0))
488 "*/" (substring string
0 (match-beginning 0)))
489 (concat "*/" string
))
492 (defun vc-arch-revision-completion-table (files)
493 (lexical-let ((files files
))
494 (lambda (string pred action
)
495 ;; FIXME: complete revision patches as well.
496 (let* ((root (expand-file-name "{arch}" (vc-arch-root (car files
))))
497 (table (vc-arch--version-completion-table root string
)))
498 (complete-with-action action table string pred
)))))
500 ;;; Trimming revision libraries.
502 ;; This code is not directly related to VC and there are many variants of
503 ;; this functionality available as scripts, but I like this version better,
504 ;; so maybe others will like it too.
506 (defun vc-arch-trim-find-least-useful-rev (revs)
507 (let* ((first (pop revs
))
510 ;; We try to give more importance to recent revisions. The idea is
511 ;; that it's OK if checking out a revision 1000-patch-old is ten
512 ;; times slower than checking out a revision 100-patch-old. But at
513 ;; the same time a 2-patch-old rev isn't really ten times more
514 ;; important than a 20-patch-old, so we use an arbitrary constant
515 ;; "100" to reduce this effect for recent revisions. Making this
516 ;; constant a float has the side effect of causing the subsequent
517 ;; computations to be done as floats as well.
518 (max (+ 100.0 (car (or (car (last revs
)) third
))))
519 (cost (lambda () (/ (- (car third
) (car first
)) (- max
(car second
)))))
521 (mincost (funcall cost
)))
525 (setq third
(pop revs
))
526 (when (< (funcall cost
) mincost
)
528 (setq mincost
(funcall cost
))))
531 (defun vc-arch-trim-make-sentinel (revs)
532 (if (null revs
) (lambda (proc msg
) (message "VC-Arch trimming ... done"))
533 (lexical-let ((revs revs
))
535 (message "VC-Arch trimming %s..." (file-name-nondirectory (car revs
)))
536 (rename-file (car revs
) (concat (car revs
) "*rm*"))
537 (setq proc
(start-process "vc-arch-trim" nil
538 "rm" "-rf" (concat (car revs
) "*rm*")))
539 (set-process-sentinel proc
(vc-arch-trim-make-sentinel (cdr revs
)))))))
541 (defun vc-arch-trim-one-revlib (dir)
542 "Delete half of the revisions in the revision library."
543 (interactive "Ddirectory: ")
544 (let ((garbage (directory-files dir
'full
"\\`,," 'nosort
)))
546 (funcall (vc-arch-trim-make-sentinel garbage
) nil nil
)))
551 (when (string-match "-\\([0-9]+\\)\\'" f
)
552 (cons (string-to-number (match-string 1 f
)) f
)))
553 (directory-files dir nil nil
'nosort
)))
557 (dotimes (i (/ (length revs
) 2))
558 (let ((minrev (vc-arch-trim-find-least-useful-rev revs
)))
559 (setq revs
(delq minrev revs
))
560 (push minrev subdirs
)))
561 (funcall (vc-arch-trim-make-sentinel
562 (mapcar (lambda (x) (expand-file-name (cdr x
) dir
)) subdirs
))
565 (defun vc-arch-trim-revlib ()
566 "Delete half of the revisions in the revision library."
568 (let ((rl-dir (with-output-to-string
569 (call-process vc-arch-program nil standard-output nil
570 "my-revision-library"))))
571 (while (string-match "\\(.*\\)\n" rl-dir
)
572 (let ((dir (match-string 1 rl-dir
)))
574 (if (and (file-directory-p dir
) (file-writable-p dir
))
576 (substring rl-dir
(match-end 0))))))
577 (unless (file-writable-p rl-dir
)
578 (error "No writable revlib directory found"))
579 (message "Revlib at %s" rl-dir
)
580 (let* ((archives (directory-files rl-dir
'full
"[^.]\\|..."))
583 (mapcar (lambda (dir)
584 (when (file-directory-p dir
)
585 (directory-files dir
'full
"[^.]\\|...")))
589 (mapcar (lambda (dir)
590 (when (file-directory-p dir
)
591 (directory-files dir
'full
"[^.]\\|...")))
595 (mapcar (lambda (dir)
596 (when (file-directory-p dir
)
597 (directory-files dir
'full
"--.*--")))
599 (mapc 'vc-arch-trim-one-revlib versions
))
602 (defvar vc-arch-extra-menu-map
603 (let ((map (make-sparse-keymap)))
604 (define-key map
[add-tagline
]
605 '(menu-item "Add tagline" vc-arch-add-tagline
))
608 (defun vc-arch-extra-menu () vc-arch-extra-menu-map
)
611 ;;; Less obvious implementations.
613 (defun vc-arch-find-revision (file rev buffer
)
614 (let ((out (make-temp-file "vc-out")))
618 (vc-arch-command (current-buffer) 1 nil
"file-diffs" file rev
)
619 (call-process-region (point-min) (point-max)
620 "patch" nil nil nil
"-R" "-o" out file
))
621 (with-current-buffer buffer
622 (insert-file-contents out
)))
627 ;; arch-tag: a35c7c1c-5237-429d-88ef-3d718fd2e704
628 ;;; vc-arch.el ends here