authors.el: Add some renamed/moved files
[bpt/emacs.git] / lisp / vc / vc-arch.el
CommitLineData
0d42eb3e 1;;; vc-arch.el --- VC backend for the Arch version-control system -*- lexical-binding: t -*-
0f6c7af8 2
ba318903 3;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
0f6c7af8
SM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Stefan Monnier <monnier@gnu.org>
bd78fa1d 7;; Package: vc
0f6c7af8
SM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
0f6c7af8 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
0f6c7af8
SM
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0f6c7af8
SM
23
24;;; Commentary:
25
26;; The home page of the Arch version control system is at
df617e7f 27;;
0f6c7af8 28;; http://www.gnuarch.org/
df617e7f 29;;
0f6c7af8
SM
30;; This is derived from vc-mcvs.el as follows:
31;; - cp vc-mcvs.el vc-arch.el and then M-% mcvs RET arch RET
32;;
33;; Then of course started the hacking.
34;;
35;; What has been partly tested:
df617e7f
SM
36;; - Open a file.
37;; - C-x v = without any prefix arg.
38;; - C-x v v to commit a change to a single file.
0f6c7af8
SM
39
40;; Bugs:
41
ce1438d6 42;; - *vc-log*'s initial content lacks the `Summary:' lines.
0f6c7af8
SM
43;; - All files under the tree are considered as "under Arch's control"
44;; without regards to =tagging-method and such.
45;; - Files are always considered as `edited'.
27fde599 46;; - C-x v l does not work.
0f6c7af8 47;; - C-x v i does not work.
27fde599
SM
48;; - C-x v ~ does not work.
49;; - C-x v u does not work.
50;; - C-x v s does not work.
51;; - C-x v r does not work.
caf37b1f 52;; - VC directory listings do not work.
0f6c7af8
SM
53;; - And more...
54
55;;; Code:
56
a464a6c7 57(eval-when-compile (require 'vc))
0f6c7af8 58
70e2f6c7
ER
59;;; Properties of the backend
60
61(defun vc-arch-revision-granularity () 'repository)
0d42eb3e 62(defun vc-arch-checkout-model (_files) 'implicit)
70e2f6c7 63
0f6c7af8
SM
64;;;
65;;; Customization options
66;;;
67
67b0de11
CY
68(defgroup vc-arch nil
69 "VC Arch backend."
70 :version "24.1"
71 :group 'vc)
72
e98a0cab
GM
73;; It seems Arch diff does not accept many options, so this is not
74;; very useful. It exists mainly so that the VC backends are all
75;; consistent with regards to their treatment of diff switches.
76(defcustom vc-arch-diff-switches t
77 "String or list of strings specifying switches for Arch diff under VC.
78If nil, use the value of `vc-diff-switches'. If t, use no switches."
79 :type '(choice (const :tag "Unspecified" nil)
80 (const :tag "None" t)
81 (string :tag "Argument String")
82 (repeat :tag "Argument List" :value ("") string))
83 :version "23.1"
67b0de11 84 :group 'vc-arch)
e98a0cab 85
e94851d3
GM
86(define-obsolete-variable-alias 'vc-arch-command 'vc-arch-program "23.1")
87
88(defcustom vc-arch-program
b4c4ba41 89 (let ((candidates '("tla" "baz")))
0f6c7af8
SM
90 (while (and candidates (not (executable-find (car candidates))))
91 (setq candidates (cdr candidates)))
e94851d3
GM
92 (or (car candidates) "tla"))
93 "Name of the Arch executable."
94 :type 'string
67b0de11 95 :group 'vc-arch)
0f6c7af8
SM
96
97;; Clear up the cache to force vc-call to check again and discover
98;; new functions when we reload this file.
99(put 'Arch 'vc-functions nil)
100
101;;;###autoload (defun vc-arch-registered (file)
ef9c4df0
SM
102;;;###autoload (if (vc-find-root file "{arch}/=tagging-method")
103;;;###autoload (progn
af314ba0 104;;;###autoload (load "vc-arch" nil t)
ef9c4df0 105;;;###autoload (vc-arch-registered file))))
0f6c7af8 106
df617e7f 107(defun vc-arch-add-tagline ()
0f6c7af8
SM
108 "Add an `arch-tag' to the end of the current file."
109 (interactive)
df617e7f 110 (comment-normalize-vars)
0f6c7af8
SM
111 (goto-char (point-max))
112 (forward-comment -1)
56dada42
SM
113 (skip-chars-forward " \t\n")
114 (cond
115 ((not (bolp)) (insert "\n\n"))
116 ((not (eq ?\n (char-before (1- (point))))) (insert "\n")))
df617e7f
SM
117 (let ((beg (point))
118 (idfile (and buffer-file-name
119 (expand-file-name
120 (concat ".arch-ids/"
121 (file-name-nondirectory buffer-file-name)
122 ".id")
123 (file-name-directory buffer-file-name)))))
0f6c7af8 124 (insert "arch-tag: ")
df617e7f
SM
125 (if (and idfile (file-exists-p idfile))
126 ;; If the file is unreadable, we do want to get an error here.
127 (progn
128 (insert-file-contents idfile)
129 (forward-line 1)
130 (delete-file idfile))
131 (condition-case nil
132 (call-process "uuidgen" nil t)
133 (file-error (insert (format "%s <%s> %s"
134 (current-time-string)
135 user-mail-address
136 (+ (nth 2 (current-time))
137 (buffer-size)))))))
0f6c7af8
SM
138 (comment-region beg (point))))
139
df617e7f
SM
140(defconst vc-arch-tagline-re "^\\W*arch-tag:[ \t]*\\(.*[^ \t\n]\\)")
141
74cb3d21
SM
142(defmacro vc-with-current-file-buffer (file &rest body)
143 (declare (indent 2) (debug t))
144 `(let ((-kill-buf- nil)
145 (-file- ,file))
146 (with-current-buffer (or (find-buffer-visiting -file-)
147 (setq -kill-buf- (generate-new-buffer " temp")))
148 ;; Avoid find-file-literally since it can do many undesirable extra
149 ;; things (among which, call us back into an infinite loop).
150 (if -kill-buf- (insert-file-contents -file-))
151 (unwind-protect
152 (progn ,@body)
153 (if (buffer-live-p -kill-buf-) (kill-buffer -kill-buf-))))))
154
df617e7f
SM
155(defun vc-arch-file-source-p (file)
156 "Can return nil, `maybe' or a non-nil value.
157Only the value `maybe' can be trusted :-(."
158 ;; FIXME: Check the tag and name of parent dirs.
159 (unless (string-match "\\`[,+]" (file-name-nondirectory file))
160 (or (string-match "\\`{arch}/"
161 (file-relative-name file (vc-arch-root file)))
162 (file-exists-p
163 ;; Check the presence of an ID file.
164 (expand-file-name
165 (concat ".arch-ids/" (file-name-nondirectory file) ".id")
166 (file-name-directory file)))
167 ;; Check the presence of a tagline.
74cb3d21 168 (vc-with-current-file-buffer file
df617e7f
SM
169 (save-excursion
170 (goto-char (point-max))
171 (or (re-search-backward vc-arch-tagline-re (- (point) 1000) t)
172 (progn
173 (goto-char (point-min))
174 (re-search-forward vc-arch-tagline-re (+ (point) 1000) t)))))
175 ;; FIXME: check =tagging-method to see whether untagged files might
176 ;; be source or not.
177 (with-current-buffer
178 (find-file-noselect (expand-file-name "{arch}/=tagging-method"
179 (vc-arch-root file)))
180 (let ((untagged-source t)) ;Default is `names'.
181 (save-excursion
182 (goto-char (point-min))
183 (if (re-search-forward "^[ \t]*\\(\\(tagline\\|implicit\\|names\\)\\|explicit\\)" nil t)
184 (setq untagged-source (match-end 2)))
185 (if (re-search-forward "^[ \t]*untagged-source[ \t]+\\(\\(source\\)\\|precious\\|backup\\|junk\\|unrecognized\\)" nil t)
186 (setq untagged-source (match-end 2))))
187 (if untagged-source 'maybe))))))
188
189(defun vc-arch-file-id (file)
190 ;; Don't include the kind of ID this is because it seems to be too messy.
191 (let ((idfile (expand-file-name
192 (concat ".arch-ids/" (file-name-nondirectory file) ".id")
193 (file-name-directory file))))
194 (if (file-exists-p idfile)
195 (with-temp-buffer
196 (insert-file-contents idfile)
197 (looking-at ".*[^ \n\t]")
e56b4640 198 (match-string 0))
df617e7f
SM
199 (with-current-buffer (find-file-noselect file)
200 (save-excursion
201 (goto-char (point-max))
202 (if (or (re-search-backward vc-arch-tagline-re (- (point) 1000) t)
203 (progn
204 (goto-char (point-min))
205 (re-search-forward vc-arch-tagline-re (+ (point) 1000) t)))
206 (match-string 1)
e56b4640 207 (concat "./" (file-relative-name file (vc-arch-root file)))))))))
df617e7f
SM
208
209(defun vc-arch-tagging-method (file)
210 (with-current-buffer
211 (find-file-noselect
212 (expand-file-name "{arch}/=tagging-method" (vc-arch-root file)))
213 (save-excursion
214 (goto-char (point-min))
215 (if (re-search-forward
216 "^[ \t]*\\(tagline\\|implicit\\|names\\|explicit\\)" nil t)
217 (intern (match-string 1))
218 'names))))
219
0f6c7af8 220(defun vc-arch-root (file)
d1f18ec0 221 "Return the root directory of an Arch project, if any."
0f6c7af8 222 (or (vc-file-getprop file 'arch-root)
b38f5e6f
DN
223 ;; Check the =tagging-method, in case someone naively manually
224 ;; creates a {arch} directory somewhere.
225 (let ((root (vc-find-root file "{arch}/=tagging-method")))
226 (when root
227 (vc-file-setprop
228 file 'arch-root root)))))
0f6c7af8 229
34ca0f4c
XF
230(defun vc-arch-find-admin-dir (file)
231 "Return the administrative directory of FILE."
232 (expand-file-name "{arch}" (vc-arch-root file)))
233
0d42eb3e 234(defun vc-arch-register (files &optional rev _comment)
7bbdf1cb 235 (if rev (error "Explicit initial revision not supported for Arch"))
8cdd17b4
ER
236 (dolist (file files)
237 (let ((tagmet (vc-arch-tagging-method file)))
238 (if (and (memq tagmet '(tagline implicit)) comment-start)
239 (with-current-buffer (find-file-noselect file)
240 (if (buffer-modified-p)
241 (error "Save %s first" (buffer-name)))
242 (vc-arch-add-tagline)
243 (save-buffer)))))
244 (vc-arch-command nil 0 files "add"))
df617e7f 245
0f6c7af8 246(defun vc-arch-registered (file)
df617e7f
SM
247 ;; Don't seriously check whether it's source or not. Checking would
248 ;; require running TLA, so it's better to not do it, so it also works if
249 ;; TLA is not installed.
250 (and (vc-arch-root file)
251 (vc-arch-file-source-p file)))
0f6c7af8
SM
252
253(defun vc-arch-default-version (file)
254 (or (vc-file-getprop (vc-arch-root file) 'arch-default-version)
255 (let* ((root (vc-arch-root file))
256 (f (expand-file-name "{arch}/++default-version" root)))
257 (if (file-readable-p f)
258 (vc-file-setprop
259 root 'arch-default-version
260 (with-temp-buffer
261 (insert-file-contents f)
262 ;; Strip the terminating newline.
263 (buffer-substring (point-min) (1- (point-max)))))))))
264
0d42eb3e 265(defun vc-arch-workfile-unchanged-p (_file)
004f9b3f 266 "Stub: arch workfiles are always considered to be in a changed state,"
68eb03ca
SM
267 nil)
268
0f6c7af8
SM
269(defun vc-arch-state (file)
270 ;; There's no checkout operation and merging is not done from VC
271 ;; so the only operation that's state dependent that VC supports is commit
272 ;; which is only activated if the file is `edited'.
df617e7f
SM
273 (let* ((root (vc-arch-root file))
274 (ver (vc-arch-default-version file))
275 (pat (concat "\\`" (subst-char-in-string ?/ ?% ver)))
276 (dir (expand-file-name ",,inode-sigs/"
277 (expand-file-name "{arch}" root)))
278 (sigfile nil))
279 (dolist (f (if (file-directory-p dir) (directory-files dir t pat)))
280 (if (or (not sigfile) (file-newer-than-file-p f sigfile))
281 (setq sigfile f)))
282 (if (not sigfile)
283 'edited ;We know nothing.
284 (let ((id (vc-arch-file-id file)))
285 (setq id (replace-regexp-in-string "[ \t]" "_" id))
286 (with-current-buffer (find-file-noselect sigfile)
287 (goto-char (point-min))
288 (while (and (search-forward id nil 'move)
c9cb3a26
SM
289 (save-excursion
290 (goto-char (- (match-beginning 0) 2))
291 ;; For `names', the lines start with `?./foo/bar'.
292 ;; For others there's 2 chars before the ./foo/bar.
293 (or (not (or (bolp) (looking-at "\n?")))
294 ;; Ignore E_ entries used for foo.id files.
295 (looking-at "E_")))))
df617e7f
SM
296 (if (eobp)
297 ;; ID not found.
298 (if (equal (file-name-nondirectory sigfile)
299 (subst-char-in-string
ac3f4c6f 300 ?/ ?% (vc-arch-working-revision file)))
df617e7f
SM
301 'added
302 ;; Might be `added' or `up-to-date' as well.
303 ;; FIXME: Check in the patch logs to find out.
304 'edited)
305 ;; Found the ID, let's check the inode.
306 (if (not (re-search-forward
307 "\t.*mtime=\\([0-9]+\\):size=\\([0-9]+\\)"
308 (line-end-position) t))
309 ;; Buh? Unexpected format.
310 'edited
311 (let ((ats (file-attributes file)))
f9e66e49 312 (if (and (eq (nth 7 ats) (string-to-number (match-string 2)))
df617e7f
SM
313 (equal (format-time-string "%s" (nth 5 ats))
314 (match-string 1)))
315 'up-to-date
316 'edited)))))))))
7bbdf1cb 317
e658d75c
GM
318;; -dir-status called from vc-dir, which loads vc, which loads vc-dispatcher.
319(declare-function vc-exec-after "vc-dispatcher" (code))
320
b4a7be38
MH
321(defun vc-arch-dir-status (dir callback)
322 "Run 'tla inventory' for DIR and pass results to CALLBACK.
323CALLBACK expects (ENTRIES &optional MORE-TO-COME); see
324`vc-dir-refresh'."
325 (let ((default-directory dir))
326 (vc-arch-command t 'async nil "changes"))
327 ;; The updating could be done asynchronously.
9c750eba
SM
328 (vc-run-delayed
329 (vc-arch-after-dir-status callback)))
b4a7be38
MH
330
331(defun vc-arch-after-dir-status (callback)
332 (let* ((state-map '(("M " . edited)
333 ("Mb" . edited) ;binary
334 ("D " . removed)
335 ("D/" . removed) ;directory
336 ("A " . added)
337 ("A/" . added) ;directory
338 ("=>" . renamed)
339 ("/>" . renamed) ;directory
340 ("lf" . symlink-to-file)
341 ("fl" . file-to-symlink)
342 ("--" . permissions-changed)
343 ("-/" . permissions-changed) ;directory
344 ))
345 (state-map-regexp (regexp-opt (mapcar 'car state-map) t))
346 (entry-regexp (concat "^" state-map-regexp " \\(.*\\)$"))
347 result)
348 (goto-char (point-min))
349 ;;(message "Got %s" (buffer-string))
350 (while (re-search-forward entry-regexp nil t)
351 (let* ((state-string (match-string 1))
352 (state (cdr (assoc state-string state-map)))
353 (filename (match-string 2)))
354 (push (list filename state) result)))
355
356 (funcall callback result nil)))
357
ac3f4c6f 358(defun vc-arch-working-revision (file)
0f6c7af8
SM
359 (let* ((root (expand-file-name "{arch}" (vc-arch-root file)))
360 (defbranch (vc-arch-default-version file)))
c771f3d6 361 (when (and defbranch (string-match "\\`\\(.+@[^/\n]+\\)/\\(\\(\\(.*?\\)\\(?:--.*\\)?\\)--.*\\)\\'" defbranch))
0f6c7af8
SM
362 (let* ((archive (match-string 1 defbranch))
363 (category (match-string 4 defbranch))
364 (branch (match-string 3 defbranch))
365 (version (match-string 2 defbranch))
27fde599 366 (sealed nil) (rev-nb 0)
0f6c7af8
SM
367 (rev nil)
368 logdir tmp)
369 (setq logdir (expand-file-name category root))
370 (setq logdir (expand-file-name branch logdir))
371 (setq logdir (expand-file-name version logdir))
372 (setq logdir (expand-file-name archive logdir))
373 (setq logdir (expand-file-name "patch-log" logdir))
bcabe045
SM
374 (dolist (file (if (file-directory-p logdir) (directory-files logdir)))
375 ;; Revision names go: base-0, patch-N, version-0, versionfix-M.
27fde599
SM
376 (when (and (eq (aref file 0) ?v) (not sealed))
377 (setq sealed t rev-nb 0))
0f6c7af8
SM
378 (if (and (string-match "-\\([0-9]+\\)\\'" file)
379 (setq tmp (string-to-number (match-string 1 file)))
27fde599 380 (or (not sealed) (eq (aref file 0) ?v))
0f6c7af8
SM
381 (>= tmp rev-nb))
382 (setq rev-nb tmp rev file)))
86d660c6
SM
383 ;; Use "none-000" if the tree hasn't yet been committed on the
384 ;; default branch. We'll then get "Arch:000[branch]" on the mode-line.
385 (concat defbranch "--" (or rev "none-000"))))))
0f6c7af8
SM
386
387
388(defcustom vc-arch-mode-line-rewrite
27fde599 389 '(("\\`.*--\\(.*--.*\\)--\\(v?\\).*-\\([0-9]+\\)\\'" . "\\2\\3[\\1]"))
0f6c7af8 390 "Rewrite rules to shorten Arch's revision names on the mode-line."
d30bef64 391 :type '(repeat (cons regexp string))
67b0de11 392 :group 'vc-arch)
0f6c7af8
SM
393
394(defun vc-arch-mode-line-string (file)
37269466 395 "Return a string for `vc-mode-line' to put in the mode line for FILE."
ac3f4c6f 396 (let ((rev (vc-working-revision file)))
0f6c7af8
SM
397 (dolist (rule vc-arch-mode-line-rewrite)
398 (if (string-match (car rule) rev)
399 (setq rev (replace-match (cdr rule) t nil rev))))
400 (format "Arch%c%s"
a464a6c7
SM
401 (pcase (vc-state file)
402 ((or `up-to-date `needs-update) ?-)
403 (`added ?@)
df617e7f 404 (t ?:))
0f6c7af8
SM
405 rev)))
406
407(defun vc-arch-diff3-rej-p (rej)
df617e7f
SM
408 (let ((attrs (file-attributes rej)))
409 (and attrs (< (nth 7 attrs) 60)
410 (with-temp-buffer
411 (insert-file-contents rej)
412 (goto-char (point-min))
e1dbe924 413 (looking-at "Conflicts occurred, diff3 conflict markers left in file\\.")))))
0f6c7af8
SM
414
415(defun vc-arch-delete-rej-if-obsolete ()
7bbdf1cb
SM
416 "For use in `after-save-hook'."
417 (save-excursion
418 (let ((rej (concat buffer-file-name ".rej")))
419 (when (and buffer-file-name (vc-arch-diff3-rej-p rej))
54648b5c
DN
420 (unless (re-search-forward "^<<<<<<< " nil t)
421 ;; The .rej file is obsolete.
422 (condition-case nil (delete-file rej) (error nil))
423 ;; Remove the hook so that it is not called multiple times.
424 (remove-hook 'after-save-hook 'vc-arch-delete-rej-if-obsolete t))))))
0f6c7af8
SM
425
426(defun vc-arch-find-file-hook ()
427 (let ((rej (concat buffer-file-name ".rej")))
428 (when (and buffer-file-name (file-exists-p rej))
429 (if (vc-arch-diff3-rej-p rej)
430 (save-excursion
431 (goto-char (point-min))
7bbdf1cb 432 (if (not (re-search-forward "^<<<<<<< " nil t))
0f6c7af8
SM
433 ;; The .rej file is obsolete.
434 (condition-case nil (delete-file rej) (error nil))
28e4e2b4 435 (smerge-mode 1)
7bbdf1cb 436 (add-hook 'after-save-hook
0f6c7af8
SM
437 'vc-arch-delete-rej-if-obsolete nil t)
438 (message "There are unresolved conflicts in this file")))
439 (message "There are unresolved conflicts in %s"
440 (file-name-nondirectory rej))))))
441
e658d75c
GM
442(autoload 'vc-switches "vc")
443
4624de78 444(defun vc-arch-checkin (files rev comment)
ef5c2eca 445 (if rev (error "Committing to a specific revision is unsupported"))
8cdd17b4 446 ;; FIXME: This implementation probably only works for singleton filesets
98a6dc9f 447 (let ((summary (file-relative-name (car files) (vc-arch-root (car files)))))
27fde599
SM
448 ;; Extract a summary from the comment.
449 (when (or (string-match "\\`Summary:[ \t]*\\(.*[^ \t\n]\\)\\([ \t]*\n\\)*" comment)
450 (string-match "\\`[ \t]*\\(.*[^ \t\n]\\)[ \t]*\\(\n?\\'\\|\n\\([ \t]*\n\\)+\\)" comment))
451 (setq summary (match-string 1 comment))
452 (setq comment (substring comment (match-end 0))))
8cdd17b4 453 (vc-arch-command nil 0 files "commit" "-s" summary "-L" comment "--"
27fde599 454 (vc-switches 'Arch 'checkin))))
0f6c7af8 455
8cdd17b4
ER
456(defun vc-arch-diff (files &optional oldvers newvers buffer)
457 "Get a difference report using Arch between two versions of FILES."
f3b8feb4
SM
458 ;; FIXME: This implementation only works for singleton filesets. To make
459 ;; it work for more cases, we have to either call `file-diffs' manually on
460 ;; each and every `file' in the fileset, or use `changes --diffs' (and
461 ;; variants) and maybe filter the output with `filterdiff' to only include
462 ;; the files in which we're interested.
463 (let ((file (car files)))
464 (if (and newvers
465 (vc-up-to-date-p file)
ac3f4c6f 466 (equal newvers (vc-working-revision file)))
f3b8feb4
SM
467 ;; Newvers is the base revision and the current file is unchanged,
468 ;; so we can diff with the current file.
469 (setq newvers nil))
470 (if newvers
471 (error "Diffing specific revisions not implemented")
9fb5c929
MA
472 (let* (process-file-side-effects
473 (async (not vc-disable-async-diff))
f3b8feb4
SM
474 ;; Run the command from the root dir.
475 (default-directory (vc-arch-root file))
476 (status
477 (vc-arch-command
478 (or buffer "*vc-diff*")
479 (if async 'async 1)
480 nil "file-diffs"
e98a0cab 481 (vc-switches 'Arch 'diff)
f3b8feb4 482 (file-relative-name file)
ac3f4c6f 483 (if (equal oldvers (vc-working-revision file))
f3b8feb4
SM
484 nil
485 oldvers))))
486 (if async 1 status))))) ; async diff, pessimistic assumption.
0f6c7af8
SM
487
488(defun vc-arch-delete-file (file)
489 (vc-arch-command nil 0 file "rm"))
490
491(defun vc-arch-rename-file (old new)
492 (vc-arch-command nil 0 new "mv" (file-relative-name old)))
493
7bbdf1cb
SM
494(defalias 'vc-arch-responsible-p 'vc-arch-root)
495
0f6c7af8
SM
496(defun vc-arch-command (buffer okstatus file &rest flags)
497 "A wrapper around `vc-do-command' for use in vc-arch.el."
e94851d3 498 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-arch-program file flags))
0f6c7af8 499
ac3f4c6f 500(defun vc-arch-init-revision () nil)
7bbdf1cb 501
56dada42
SM
502;;; Completion of versions and revisions.
503
56dada42
SM
504(defun vc-arch--version-completion-table (root string)
505 (delq nil
506 (mapcar
507 (lambda (d)
508 (when (string-match "/\\([^/]+\\)/\\([^/]+\\)\\'" d)
509 (concat (match-string 2 d) "/" (match-string 1 d))))
510 (let ((default-directory root))
511 (file-expand-wildcards
512 (concat "*/*/"
513 (if (string-match "/" string)
514 (concat (substring string (match-end 0))
515 "*/" (substring string 0 (match-beginning 0)))
516 (concat "*/" string))
517 "*"))))))
518
a44d450b 519(defun vc-arch-revision-completion-table (files)
0d42eb3e
SM
520 (lambda (string pred action)
521 ;; FIXME: complete revision patches as well.
522 (let* ((root (expand-file-name "{arch}" (vc-arch-root (car files))))
523 (table (vc-arch--version-completion-table root string)))
524 (complete-with-action action table string pred))))
56dada42
SM
525
526;;; Trimming revision libraries.
527
528;; This code is not directly related to VC and there are many variants of
529;; this functionality available as scripts, but I like this version better,
530;; so maybe others will like it too.
531
532(defun vc-arch-trim-find-least-useful-rev (revs)
533 (let* ((first (pop revs))
534 (second (pop revs))
535 (third (pop revs))
536 ;; We try to give more importance to recent revisions. The idea is
537 ;; that it's OK if checking out a revision 1000-patch-old is ten
538 ;; times slower than checking out a revision 100-patch-old. But at
539 ;; the same time a 2-patch-old rev isn't really ten times more
540 ;; important than a 20-patch-old, so we use an arbitrary constant
541 ;; "100" to reduce this effect for recent revisions. Making this
542 ;; constant a float has the side effect of causing the subsequent
543 ;; computations to be done as floats as well.
544 (max (+ 100.0 (car (or (car (last revs)) third))))
545 (cost (lambda () (/ (- (car third) (car first)) (- max (car second)))))
546 (minrev second)
547 (mincost (funcall cost)))
548 (while revs
549 (setq first second)
550 (setq second third)
551 (setq third (pop revs))
552 (when (< (funcall cost) mincost)
553 (setq minrev second)
554 (setq mincost (funcall cost))))
555 minrev))
556
557(defun vc-arch-trim-make-sentinel (revs)
0d42eb3e
SM
558 (if (null revs) (lambda (_proc _msg) (message "VC-Arch trimming ... done"))
559 (lambda (_proc _msg)
560 (message "VC-Arch trimming %s..." (file-name-nondirectory (car revs)))
561 (rename-file (car revs) (concat (car revs) "*rm*"))
562 (let ((proc (start-process "vc-arch-trim" nil
563 "rm" "-rf" (concat (car revs) "*rm*"))))
e87c4c6e 564 (set-process-sentinel proc (vc-arch-trim-make-sentinel (cdr revs)))))))
56dada42
SM
565
566(defun vc-arch-trim-one-revlib (dir)
567 "Delete half of the revisions in the revision library."
568 (interactive "Ddirectory: ")
e87c4c6e
SM
569 (let ((garbage (directory-files dir 'full "\\`,," 'nosort)))
570 (when garbage
571 (funcall (vc-arch-trim-make-sentinel garbage) nil nil)))
56dada42
SM
572 (let ((revs
573 (sort (delq nil
574 (mapcar
575 (lambda (f)
576 (when (string-match "-\\([0-9]+\\)\\'" f)
577 (cons (string-to-number (match-string 1 f)) f)))
578 (directory-files dir nil nil 'nosort)))
579 'car-less-than-car))
580 (subdirs nil))
581 (when (cddr revs)
0d42eb3e 582 (dotimes (_i (/ (length revs) 2))
56dada42
SM
583 (let ((minrev (vc-arch-trim-find-least-useful-rev revs)))
584 (setq revs (delq minrev revs))
585 (push minrev subdirs)))
586 (funcall (vc-arch-trim-make-sentinel
587 (mapcar (lambda (x) (expand-file-name (cdr x) dir)) subdirs))
588 nil nil))))
589
590(defun vc-arch-trim-revlib ()
591 "Delete half of the revisions in the revision library."
592 (interactive)
593 (let ((rl-dir (with-output-to-string
e94851d3 594 (call-process vc-arch-program nil standard-output nil
56dada42
SM
595 "my-revision-library"))))
596 (while (string-match "\\(.*\\)\n" rl-dir)
597 (let ((dir (match-string 1 rl-dir)))
598 (setq rl-dir
599 (if (and (file-directory-p dir) (file-writable-p dir))
600 dir
601 (substring rl-dir (match-end 0))))))
602 (unless (file-writable-p rl-dir)
603 (error "No writable revlib directory found"))
604 (message "Revlib at %s" rl-dir)
605 (let* ((archives (directory-files rl-dir 'full "[^.]\\|..."))
606 (categories
607 (apply 'append
608 (mapcar (lambda (dir)
609 (when (file-directory-p dir)
610 (directory-files dir 'full "[^.]\\|...")))
611 archives)))
612 (branches
613 (apply 'append
614 (mapcar (lambda (dir)
615 (when (file-directory-p dir)
616 (directory-files dir 'full "[^.]\\|...")))
617 categories)))
618 (versions
619 (apply 'append
620 (mapcar (lambda (dir)
621 (when (file-directory-p dir)
622 (directory-files dir 'full "--.*--")))
623 branches))))
624 (mapc 'vc-arch-trim-one-revlib versions))
625 ))
80d7d79f
SM
626
627(defvar vc-arch-extra-menu-map
628 (let ((map (make-sparse-keymap)))
629 (define-key map [add-tagline]
630 '(menu-item "Add tagline" vc-arch-add-tagline))
631 map))
632
633(defun vc-arch-extra-menu () vc-arch-extra-menu-map)
e94851d3 634
80d7d79f 635
544bdc40
SM
636;;; Less obvious implementations.
637
ac3f4c6f 638(defun vc-arch-find-revision (file rev buffer)
544bdc40
SM
639 (let ((out (make-temp-file "vc-out")))
640 (unwind-protect
641 (progn
642 (with-temp-buffer
643 (vc-arch-command (current-buffer) 1 nil "file-diffs" file rev)
644 (call-process-region (point-min) (point-max)
645 "patch" nil nil nil "-R" "-o" out file))
646 (with-current-buffer buffer
647 (insert-file-contents out)))
648 (delete-file out))))
649
0f6c7af8
SM
650(provide 'vc-arch)
651
0f6c7af8 652;;; vc-arch.el ends here