Bug fix for vc-dispatcher split.
[bpt/emacs.git] / lisp / vc-rcs.el
CommitLineData
d8aff077
GM
1;;; vc-rcs.el --- support for RCS version-control
2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
409cc4a3 4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
d8aff077
GM
5
6;; Author: FSF (see vc.el for full credits)
7;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8
fa63cb6d 9;; $Id$
d8aff077
GM
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
b4aa6026 15;; the Free Software Foundation; either version 3, or (at your option)
d8aff077
GM
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
d8aff077 27
e8af40ee
PJ
28;;; Commentary:
29
30;; See vc.el
d8aff077 31
0792fdb4
SM
32;; TODO:
33;; - remove call to vc-expand-dirs by implementing our own (which can just
34;; list the RCS subdir instead).
35
d8aff077
GM
36;;; Code:
37
8f98485f
AS
38;;;
39;;; Customization options
40;;;
41
0bc58756 42(eval-when-compile
10489ed7
AS
43 (require 'cl)
44 (require 'vc))
0bc58756 45
d8aff077
GM
46(defcustom vc-rcs-release nil
47 "*The release number of your RCS installation, as a string.
48If nil, VC itself computes this value when it is first needed."
49 :type '(choice (const :tag "Auto" nil)
50 (string :tag "Specified")
51 (const :tag "Unknown" unknown))
52 :group 'vc)
53
54(defcustom vc-rcs-register-switches nil
33c1b7a1
DL
55 "*Extra switches for registering a file in RCS.
56A string or list of strings. These are passed to the checkin program
57by \\[vc-rcs-register]."
d8aff077
GM
58 :type '(choice (const :tag "None" nil)
59 (string :tag "Argument String")
60 (repeat :tag "Argument List"
61 :value ("")
62 string))
33c1b7a1 63 :version "21.1"
d8aff077
GM
64 :group 'vc)
65
10489ed7
AS
66(defcustom vc-rcs-diff-switches nil
67 "*A string or list of strings specifying extra switches for rcsdiff under VC."
68 :type '(choice (const :tag "None" nil)
69 (string :tag "Argument String")
70 (repeat :tag "Argument List"
71 :value ("")
72 string))
73 :version "21.1"
74 :group 'vc)
75
d8aff077
GM
76(defcustom vc-rcs-header (or (cdr (assoc 'RCS vc-header-alist)) '("\$Id\$"))
77 "*Header keywords to be inserted by `vc-insert-headers'."
f0e7c067 78 :type '(repeat string)
33c1b7a1 79 :version "21.1"
d8aff077
GM
80 :group 'vc)
81
82(defcustom vc-rcsdiff-knows-brief nil
83 "*Indicates whether rcsdiff understands the --brief option.
84The value is either `yes', `no', or nil. If it is nil, VC tries
85to use --brief and sets this variable to remember whether it worked."
86 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
87 :group 'vc)
88
89;;;###autoload
90(defcustom vc-rcs-master-templates
91 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s")
92 "*Where to look for RCS master files.
93For a description of possible values, see `vc-check-master-templates'."
94 :type '(choice (const :tag "Use standard RCS file names"
95 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
96 (repeat :tag "User-specified"
97 (choice string
98 function)))
33c1b7a1 99 :version "21.1"
d8aff077
GM
100 :group 'vc)
101
8f98485f 102\f
8cdd17b4
ER
103;;; Properties of the backend
104
70e2f6c7
ER
105(defun vc-rcs-revision-granularity () 'file)
106
107(defun vc-rcs-checkout-model (files)
108 "RCS-specific version of `vc-checkout-model'."
109 (let ((file (if (consp files) (car files) files))
110 result)
111 (when vc-consult-headers
112 (vc-file-setprop file 'vc-checkout-model nil)
113 (vc-rcs-consult-headers file)
114 (setq result (vc-file-getprop file 'vc-checkout-model)))
115 (or result
116 (progn (vc-rcs-fetch-master-state file)
117 (vc-file-getprop file 'vc-checkout-model)))))
8cdd17b4 118
8f98485f
AS
119;;;
120;;; State-querying functions
121;;;
122
e0607aaa
SM
123;; The autoload cookie below places vc-rcs-registered directly into
124;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
125;; every file that is visited.
126;;;###autoload
127(progn
128(defun vc-rcs-registered (f) (vc-default-registered 'RCS f)))
d8aff077
GM
129
130(defun vc-rcs-state (file)
131 "Implementation of `vc-state' for RCS."
15ef1eae 132 (if (not (vc-rcs-registered file))
3702367b
ER
133 'unregistered
134 (or (boundp 'vc-rcs-headers-result)
135 (and vc-consult-headers
136 (vc-rcs-consult-headers file)))
137 (let ((state
138 ;; vc-working-revision might not be known; in that case the
139 ;; property is nil. vc-rcs-fetch-master-state knows how to
140 ;; handle that.
141 (vc-rcs-fetch-master-state file
142 (vc-file-getprop file
143 'vc-working-revision))))
144 (if (not (eq state 'up-to-date))
145 state
146 (if (vc-workfile-unchanged-p file)
147 'up-to-date
70e2f6c7 148 (if (eq (vc-rcs-checkout-model (list file)) 'locking)
3702367b
ER
149 'unlocked-changes
150 'edited))))))
d8aff077
GM
151
152(defun vc-rcs-state-heuristic (file)
153 "State heuristic for RCS."
154 (let (vc-rcs-headers-result)
155 (if (and vc-consult-headers
33c1b7a1 156 (setq vc-rcs-headers-result
d8aff077
GM
157 (vc-rcs-consult-headers file))
158 (eq vc-rcs-headers-result 'rev-and-lock))
159 (let ((state (vc-file-getprop file 'vc-state)))
160 ;; If the headers say that the file is not locked, the
161 ;; permissions can tell us whether locking is used for
162 ;; the file or not.
163 (if (and (eq state 'up-to-date)
164 (not (vc-mistrust-permissions file)))
165 (cond
166 ((string-match ".rw..-..-." (nth 8 (file-attributes file)))
0db2c43c 167 (vc-file-setprop file 'vc-checkout-model 'implicit)
f1180544
JB
168 (setq state
169 (if (vc-rcs-workfile-is-newer file)
170 'edited
0db2c43c 171 'up-to-date)))
d8aff077
GM
172 ((string-match ".r-..-..-." (nth 8 (file-attributes file)))
173 (vc-file-setprop file 'vc-checkout-model 'locking))))
174 state)
175 (if (not (vc-mistrust-permissions file))
b010f887
AS
176 (let* ((attributes (file-attributes file 'string))
177 (owner-name (nth 2 attributes))
d8aff077
GM
178 (permissions (nth 8 attributes)))
179 (cond ((string-match ".r-..-..-." permissions)
180 (vc-file-setprop file 'vc-checkout-model 'locking)
181 'up-to-date)
182 ((string-match ".rw..-..-." permissions)
e0607aaa 183 (if (eq (vc-rcs-checkout-model file) 'locking)
0db2c43c
AS
184 (if (file-ownership-preserved-p file)
185 'edited
b010f887 186 owner-name)
f1180544 187 (if (vc-rcs-workfile-is-newer file)
0db2c43c
AS
188 'edited
189 'up-to-date)))
d8aff077
GM
190 (t
191 ;; Strange permissions. Fall through to
192 ;; expensive state computation.
193 (vc-rcs-state file))))
194 (vc-rcs-state file)))))
195
c1b51374 196(defun vc-rcs-dir-status (dir update-function)
5a9de6d0
ER
197 ;; Doing individual vc-state calls is painful but tgere
198 ;; is no better way in RCS-land.
90e9ca17
DN
199 (let ((flist (vc-expand-dirs (list dir)))
200 (result nil))
201 (dolist (file flist)
202 (let ((state (vc-state file))
203 (frel (file-relative-name file)))
204 (push (list frel state) result)))
c1b51374 205 (funcall update-function result)))
90e9ca17 206
ac3f4c6f
ER
207(defun vc-rcs-working-revision (file)
208 "RCS-specific version of `vc-working-revision'."
d8aff077
GM
209 (or (and vc-consult-headers
210 (vc-rcs-consult-headers file)
ac3f4c6f 211 (vc-file-getprop file 'vc-working-revision))
d8aff077
GM
212 (progn
213 (vc-rcs-fetch-master-state file)
ac3f4c6f 214 (vc-file-getprop file 'vc-working-revision))))
d8aff077 215
8f98485f
AS
216(defun vc-rcs-latest-on-branch-p (file &optional version)
217 "Return non-nil if workfile version of FILE is the latest on its branch.
218When VERSION is given, perform check for that version."
ac3f4c6f 219 (unless version (setq version (vc-working-revision file)))
8f98485f
AS
220 (with-temp-buffer
221 (string= version
7735770b 222 (if (vc-trunk-p version)
8f98485f
AS
223 (progn
224 ;; Compare VERSION to the head version number.
225 (vc-insert-file (vc-name file) "^[0-9]")
226 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
227 ;; If we are not on the trunk, we need to examine the
228 ;; whole current branch.
229 (vc-insert-file (vc-name file) "^desc")
7735770b 230 (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
8f98485f 231
d8aff077 232(defun vc-rcs-workfile-unchanged-p (file)
fa63cb6d 233 "RCS-specific implementation of `vc-workfile-unchanged-p'."
d8aff077
GM
234 ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
235 ;; do a double take and remember the fact for the future
ac3f4c6f 236 (let* ((version (concat "-r" (vc-working-revision file)))
d8aff077
GM
237 (status (if (eq vc-rcsdiff-knows-brief 'no)
238 (vc-do-command nil 1 "rcsdiff" file version)
239 (vc-do-command nil 2 "rcsdiff" file "--brief" version))))
240 (if (eq status 2)
241 (if (not vc-rcsdiff-knows-brief)
242 (setq vc-rcsdiff-knows-brief 'no
243 status (vc-do-command nil 1 "rcsdiff" file version))
244 (error "rcsdiff failed"))
245 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
246 ;; The workfile is unchanged if rcsdiff found no differences.
247 (zerop status)))
248
9d8f43bf
SM
249(defun vc-rcs-find-file-not-found-hook ()
250 (if (yes-or-no-p
251 (format "File %s was lost; check out from version control? "
252 (file-name-nondirectory buffer-file-name)))
253 (save-excursion
254 (require 'vc)
255 (let ((default-directory (file-name-directory buffer-file-name)))
256 (not (vc-error-occurred (vc-checkout buffer-file-name)))))))
d8aff077 257\f
8f98485f
AS
258;;;
259;;; State-changing functions
260;;;
d8aff077 261
8cdd17b4
ER
262(defun vc-rcs-create-repo ()
263 "Create a new RCS repository."
264 ;; RCS is totally file-oriented, so all we have to do is make the directory
265 (make-directory "RCS"))
266
267(defun vc-rcs-register (files &optional rev comment)
268 "Register FILES into the RCS version-control system.
269REV is the optional revision number for the files. COMMENT can be used
270to provide an initial description for each FILES.
d8aff077
GM
271
272`vc-register-switches' and `vc-rcs-register-switches' are passed to
273the RCS command (in that order).
274
275Automatically retrieve a read-only version of the file with keywords
276expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
09607e62 277 (let (subdir name)
8cdd17b4 278 (dolist (file files)
09607e62
GM
279 (and (not (file-exists-p
280 (setq subdir (expand-file-name "RCS"
281 (file-name-directory file)))))
d8aff077
GM
282 (not (directory-files (file-name-directory file)
283 nil ".*,v$" t))
284 (yes-or-no-p "Create RCS subdirectory? ")
285 (make-directory subdir))
286 (apply 'vc-do-command nil 0 "ci" file
287 ;; if available, use the secure registering option
288 (and (vc-rcs-release-p "5.6.4") "-i")
289 (concat (if vc-keep-workfiles "-u" "-r") rev)
290 (and comment (concat "-t-" comment))
3e6bab65 291 (vc-switches 'RCS 'register))
d8aff077
GM
292 ;; parse output to find master file name and workfile version
293 (with-current-buffer "*vc*"
09607e62
GM
294 (goto-char (point-min))
295 (if (not (setq name
296 (if (looking-at (concat "^\\(.*\\) <-- "
297 (file-name-nondirectory file)))
298 (match-string 1))))
299 ;; if we couldn't find the master name,
300 ;; run vc-rcs-registered to get it
301 ;; (will be stored into the vc-name property)
302 (vc-rcs-registered file)
303 (vc-file-setprop file 'vc-name
304 (if (file-name-absolute-p name)
305 name
306 (expand-file-name
307 name
308 (file-name-directory file))))))
309 (vc-file-setprop file 'vc-working-revision
310 (if (re-search-forward
311 "^initial revision: \\([0-9.]+\\).*\n"
312 nil t)
313 (match-string 1))))))
d8aff077 314
8f98485f
AS
315(defun vc-rcs-responsible-p (file)
316 "Return non-nil if RCS thinks it would be responsible for registering FILE."
317 ;; TODO: check for all the patterns in vc-rcs-master-templates
318 (file-directory-p (expand-file-name "RCS" (file-name-directory file))))
319
320(defun vc-rcs-receive-file (file rev)
321 "Implementation of receive-file for RCS."
70e2f6c7 322 (let ((checkout-model (vc-rcs-checkout-model (list file))))
8f98485f
AS
323 (vc-rcs-register file rev "")
324 (when (eq checkout-model 'implicit)
325 (vc-rcs-set-non-strict-locking file))
326 (vc-rcs-set-default-branch file (concat rev ".1"))))
327
0db2c43c
AS
328(defun vc-rcs-unregister (file)
329 "Unregister FILE from RCS.
330If this leaves the RCS subdirectory empty, ask the user
331whether to remove it."
332 (let* ((master (vc-name file))
7849e179
SM
333 (dir (file-name-directory master))
334 (backup-info (find-backup-file-name master)))
335 (if (not backup-info)
336 (delete-file master)
337 (rename-file master (car backup-info) 'ok-if-already-exists)
338 (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
0db2c43c
AS
339 (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
340 ;; check whether RCS dir is empty, i.e. it does not
341 ;; contain any files except "." and ".."
f1180544 342 (not (directory-files dir nil
0db2c43c
AS
343 "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
344 (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
345 (delete-directory dir))))
346
8cdd17b4 347(defun vc-rcs-checkin (files rev comment)
8f98485f 348 "RCS-specific version of `vc-backend-checkin'."
3e6bab65 349 (let ((switches (vc-switches 'RCS 'checkin)))
8cdd17b4
ER
350 ;; Now operate on the files
351 (dolist (file files)
ac3f4c6f 352 (let ((old-version (vc-working-revision file)) new-version
8cdd17b4
ER
353 (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
354 ;; Force branch creation if an appropriate
355 ;; default branch has been set.
356 (and (not rev)
357 default-branch
358 (string-match (concat "^" (regexp-quote old-version) "\\.")
359 default-branch)
360 (setq rev default-branch)
361 (setq switches (cons "-f" switches)))
362 (if (and (not rev) old-version)
363 (setq rev (vc-branch-part old-version)))
364 (apply 'vc-do-command nil 0 "ci" (vc-name file)
365 ;; if available, use the secure check-in option
366 (and (vc-rcs-release-p "5.6.4") "-j")
367 (concat (if vc-keep-workfiles "-u" "-r") rev)
368 (concat "-m" comment)
369 switches)
ac3f4c6f 370 (vc-file-setprop file 'vc-working-revision nil)
8cdd17b4
ER
371
372 ;; determine the new workfile version
373 (set-buffer "*vc*")
374 (goto-char (point-min))
375 (when (or (re-search-forward
376 "new revision: \\([0-9.]+\\);" nil t)
377 (re-search-forward
378 "reverting to previous revision \\([0-9.]+\\)" nil t))
379 (setq new-version (match-string 1))
ac3f4c6f 380 (vc-file-setprop file 'vc-working-revision new-version))
8cdd17b4
ER
381
382 ;; if we got to a different branch, adjust the default
383 ;; branch accordingly
384 (cond
385 ((and old-version new-version
386 (not (string= (vc-branch-part old-version)
387 (vc-branch-part new-version))))
388 (vc-rcs-set-default-branch file
389 (if (vc-trunk-p new-version) nil
390 (vc-branch-part new-version)))
391 ;; If this is an old RCS release, we might have
392 ;; to remove a remaining lock.
393 (if (not (vc-rcs-release-p "5.6.2"))
394 ;; exit status of 1 is also accepted.
395 ;; It means that the lock was removed before.
396 (vc-do-command nil 1 "rcs" (vc-name file)
397 (concat "-u" old-version)))))))))
a7e98271 398
ac3f4c6f 399(defun vc-rcs-find-revision (file rev buffer)
88388365
SM
400 (apply 'vc-do-command
401 buffer 0 "co" (vc-name file)
402 "-q" ;; suppress diagnostic output
403 (concat "-p" rev)
3e6bab65 404 (vc-switches 'RCS 'checkout)))
88388365
SM
405
406(defun vc-rcs-checkout (file &optional editable rev)
407 "Retrieve a copy of a saved version of FILE."
408 (let ((file-buffer (get-file-buffer file))
d8aff077 409 switches)
88388365 410 (message "Checking out %s..." file)
d8aff077
GM
411 (save-excursion
412 ;; Change buffers to get local value of vc-checkout-switches.
413 (if file-buffer (set-buffer file-buffer))
3e6bab65 414 (setq switches (vc-switches 'RCS 'checkout))
d8aff077
GM
415 ;; Save this buffer's default-directory
416 ;; and use save-excursion to make sure it is restored
417 ;; in the same buffer it was saved in.
418 (let ((default-directory default-directory))
419 (save-excursion
420 ;; Adjust the default-directory so that the check-out creates
421 ;; the file in the right place.
88388365
SM
422 (setq default-directory (file-name-directory file))
423 (let (new-version)
424 ;; if we should go to the head of the trunk,
425 ;; clear the default branch first
426 (and rev (string= rev "")
427 (vc-rcs-set-default-branch file nil))
428 ;; now do the checkout
429 (apply 'vc-do-command
430 nil 0 "co" (vc-name file)
431 ;; If locking is not strict, force to overwrite
432 ;; the writable workfile.
70e2f6c7 433 (if (eq (vc-rcs-checkout-model (list file)) 'implicit) "-f")
88388365 434 (if editable "-l")
a3294a80
AS
435 (if (stringp rev)
436 ;; a literal revision was specified
437 (concat "-r" rev)
ac3f4c6f 438 (let ((workrev (vc-working-revision file)))
a3294a80 439 (if workrev
f1180544 440 (concat "-r"
a3294a80
AS
441 (if (not rev)
442 ;; no revision specified:
443 ;; use current workfile version
444 workrev
445 ;; REV is t ...
a207e923 446 (if (not (vc-trunk-p workrev))
a3294a80
AS
447 ;; ... go to head of current branch
448 (vc-branch-part workrev)
449 ;; ... go to head of trunk
450 (vc-rcs-set-default-branch file
451 nil)
452 ""))))))
88388365
SM
453 switches)
454 ;; determine the new workfile version
455 (with-current-buffer "*vc*"
456 (setq new-version
457 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
ac3f4c6f 458 (vc-file-setprop file 'vc-working-revision new-version)
88388365
SM
459 ;; if necessary, adjust the default branch
460 (and rev (not (string= rev ""))
3249f234 461 (vc-rcs-set-default-branch
88388365
SM
462 file
463 (if (vc-rcs-latest-on-branch-p file new-version)
464 (if (vc-trunk-p new-version) nil
465 (vc-branch-part new-version))
466 new-version)))))
467 (message "Checking out %s...done" file)))))
d8aff077 468
8cdd17b4
ER
469(defun vc-rcs-rollback (files)
470 "Roll back, undoing the most recent checkins of FILES."
471 (if (not files)
472 (error "RCS backend doesn't support directory-level rollback."))
473 (dolist (file files)
ac3f4c6f 474 (let* ((discard (vc-working-revision file))
8cdd17b4
ER
475 (previous (if (vc-trunk-p discard) "" (vc-branch-part discard)))
476 (config (current-window-configuration))
477 (done nil))
72c70417 478 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
8cdd17b4
ER
479 discard file)))
480 (error "Aborted"))
481 (message "Removing revision %s from %s." discard file)
482 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-o" discard))
483 ;; Check out the most recent remaining version. If it
484 ;; fails, because the whole branch got deleted, do a
485 ;; double-take and check out the version where the branch
486 ;; started.
487 (while (not done)
488 (condition-case err
489 (progn
490 (vc-do-command nil 0 "co" (vc-name file) "-f"
491 (concat "-u" previous))
492 (setq done t))
493 (error (set-buffer "*vc*")
494 (goto-char (point-min))
495 (if (search-forward "no side branches present for" nil t)
496 (progn (setq previous (vc-branch-part previous))
497 (vc-rcs-set-default-branch file previous)
498 ;; vc-do-command popped up a window with
499 ;; the error message. Get rid of it, by
500 ;; restoring the old window configuration.
501 (set-window-configuration config))
502 ;; No, it was some other error: re-signal it.
503 (signal (car err) (cdr err)))))))))
504
99739bbf 505(defun vc-rcs-revert (file &optional contents-done)
8f98485f
AS
506 "Revert FILE to the version it was based on."
507 (vc-do-command nil 0 "co" (vc-name file) "-f"
f1180544 508 (concat (if (eq (vc-state file) 'edited) "-u" "-r")
ac3f4c6f 509 (vc-working-revision file))))
8f98485f 510
8f98485f
AS
511(defun vc-rcs-merge (file first-version &optional second-version)
512 "Merge changes into current working copy of FILE.
513The changes are between FIRST-VERSION and SECOND-VERSION."
514 (vc-do-command nil 1 "rcsmerge" (vc-name file)
515 "-kk" ; ignore keyword conflicts
516 (concat "-r" first-version)
517 (if second-version (concat "-r" second-version))))
518
519(defun vc-rcs-steal-lock (file &optional rev)
520 "Steal the lock on the current workfile for FILE and revision REV.
521Needs RCS 5.6.2 or later for -M."
7735770b 522 (vc-do-command nil 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
f1180544 523 ;; Do a real checkout after stealing the lock, so that we see
7735770b
AS
524 ;; expanded headers.
525 (vc-do-command nil 0 "co" (vc-name file) "-f" (concat "-l" rev)))
8f98485f 526
9b64a7f0
ER
527(defun vc-rcs-modify-change-comment (files rev comment)
528 "Modify the change comments change on FILES on a specified REV."
529 (dolist (file files)
72c70417 530 (vc-do-command nil 0 "rcs" (vc-name file)
031f1766 531 (concat "-m" rev ":" comment))))
8f98485f
AS
532
533\f
534;;;
535;;; History functions
536;;;
537
8cdd17b4 538(defun vc-rcs-print-log (files &optional buffer)
8f98485f 539 "Get change log associated with FILE."
8cdd17b4 540 (vc-do-command buffer 0 "rlog" (mapcar 'vc-name files)))
8f98485f 541
8cdd17b4
ER
542(defun vc-rcs-diff (files &optional oldvers newvers buffer)
543 "Get a difference report using RCS between two sets of files."
72c70417 544 (apply 'vc-do-command (or buffer "*vc-diff*")
8cdd17b4
ER
545 1 ;; Always go synchronous, the repo is local
546 "rcsdiff" (vc-expand-dirs files)
10489ed7 547 (append (list "-q"
8cdd17b4 548 (and oldvers (concat "-r" oldvers))
10489ed7 549 (and newvers (concat "-r" newvers)))
3e6bab65 550 (vc-switches 'RCS 'diff))))
8f98485f 551
8cdd17b4
ER
552(defun vc-rcs-wash-log ()
553 "Remove all non-comment information from log output."
554 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
555 "\\(branches: .*;\n\\)?"
556 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
557 (goto-char (point-max)) (forward-line -1)
558 (while (looking-at "=*\n")
559 (delete-char (- (match-end 0) (match-beginning 0)))
560 (forward-line -1))
561 (goto-char (point-min))
562 (if (looking-at "[\b\t\n\v\f\r ]+")
563 (delete-char (- (match-end 0) (match-beginning 0))))
564 (goto-char (point-min))
565 (re-search-forward separator nil t)
566 (delete-region (point-min) (point))
567 (while (re-search-forward separator nil t)
568 (delete-region (match-beginning 0) (match-end 0)))))
569
3249f234
TTN
570(defun vc-rcs-annotate-command (file buffer &optional revision)
571 "Annotate FILE, inserting the results in BUFFER.
572Optional arg REVISION is a revision to annotate from."
903d71fb 573 (vc-setup-buffer buffer)
3249f234
TTN
574 ;; Aside from the "head revision on the trunk", the instructions for
575 ;; each revision on the trunk are an ordered list of kill and insert
576 ;; commands necessary to go from the chronologically-following
577 ;; revision to this one. That is, associated with revision N are
578 ;; edits that applied to revision N+1 would result in revision N.
579 ;;
580 ;; On a branch, however, (some) things are inverted: the commands
581 ;; listed are those necessary to go from the chronologically-preceding
582 ;; revision to this one. That is, associated with revision N are
583 ;; edits that applied to revision N-1 would result in revision N.
584 ;;
585 ;; So, to get per-line history info, we apply reverse-chronological
586 ;; edits, starting with the head revision on the trunk, all the way
587 ;; back through the initial revision (typically "1.1" or similar),
588 ;; then apply forward-chronological edits -- keeping track of which
589 ;; revision is associated with each inserted line -- until we reach
590 ;; the desired revision for display (which may be either on the trunk
591 ;; or on a branch).
592 (let* ((tree (with-temp-buffer
593 (insert-file-contents (vc-rcs-registered file))
594 (vc-rcs-parse)))
595 (revisions (cdr (assq 'revisions tree)))
596 ;; The revision N whose instructions we currently are processing.
597 (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
598 ;; Alist from the parse tree for N.
599 (meta (cdr (assoc cur revisions)))
600 ;; Point and temporary string, respectively.
601 p s
602 ;; "Next-branch list". Nil means the desired revision to
603 ;; display lives on the trunk. Non-nil means it lives on a
604 ;; branch, in which case the value is a list of revision pairs
605 ;; (PARENT . CHILD), the first PARENT being on the trunk, that
606 ;; links each series of revisions in the path from the initial
607 ;; revision to the desired revision to display.
608 nbls
609 ;; "Path-accumulate-predicate plus revision/date/author".
610 ;; Until set, forward-chronological edits are not accumulated.
611 ;; Once set, its value (updated every revision) is used for
612 ;; the text property `:vc-rcs-r/d/a' for inserts during
613 ;; processing of forward-chronological instructions for N.
614 ;; See internal func `r/d/a'.
615 prda
616 ;; List of forward-chronological instructions, each of the
617 ;; form: (POS . ACTION), where POS is a buffer position. If
618 ;; ACTION is a string, it is inserted, otherwise it is taken as
619 ;; the number of characters to be deleted.
620 path
621 ;; N+1. When `cur' is "", this is the initial revision.
622 pre)
623 (unless revision
624 (setq revision cur))
625 (unless (assoc revision revisions)
626 (error "No such revision: %s" revision))
627 ;; Find which branches (if any) must be included in the edits.
628 (let ((par revision)
629 bpt kids)
630 (while (setq bpt (vc-branch-part par)
631 par (vc-branch-part bpt))
632 (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
633 ;; A branchpoint may have multiple children. Find the right one.
634 (while (not (string= bpt (vc-branch-part (car kids))))
635 (setq kids (cdr kids)))
636 (push (cons par (car kids)) nbls)))
637 ;; Start with the full text.
638 (set-buffer buffer)
639 (insert (cdr (assq 'text meta)))
640 ;; Apply reverse-chronological edits on the trunk, computing and
641 ;; accumulating forward-chronological edits after some point, for
642 ;; later.
643 (flet ((r/d/a () (vector pre
644 (cdr (assq 'date meta))
645 (cdr (assq 'author meta)))))
646 (while (when (setq pre cur cur (cdr (assq 'next meta)))
647 (not (string= "" cur)))
648 (setq
649 ;; Start accumulating the forward-chronological edits when N+1
650 ;; on the trunk is either the desired revision to display, or
651 ;; the appropriate branchpoint for it. Do this before
652 ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
653 prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
654 (r/d/a))
655 meta (cdr (assoc cur revisions)))
656 ;; Edits in the parse tree specify a line number (in the buffer
657 ;; *BEFORE* editing occurs) to start from, but line numbers
658 ;; change as a result of edits. To DTRT, we apply edits in
659 ;; order of descending buffer position so that edits further
660 ;; down in the buffer occur first w/o corrupting specified
661 ;; buffer positions of edits occurring towards the beginning of
662 ;; the buffer. In this way we avoid using markers. A pleasant
663 ;; property of this approach is ability to push instructions
664 ;; onto `path' directly, w/o need to maintain rev boundaries.
665 (dolist (insn (cdr (assq :insn meta)))
666 (goto-line (pop insn))
667 (setq p (point))
668 (case (pop insn)
669 (k (setq s (buffer-substring-no-properties
670 p (progn (forward-line (car insn))
671 (point))))
672 (when prda
673 (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
674 (delete-region p (point)))
675 (i (setq s (car insn))
676 (when prda
677 (push `(,p . ,(length s)) path))
678 (insert s)))))
679 ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
680 ;; equivalent to pushing an insert instruction (of the entire buffer
681 ;; contents) onto `path' then erasing the buffer, but less wasteful.
682 (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
683 ;; Now apply the forward-chronological edits for the trunk.
684 (dolist (insn path)
685 (goto-char (pop insn))
686 (if (stringp insn)
687 (insert insn)
688 (delete-char insn)))
689 ;; Now apply the forward-chronological edits (directly from the
690 ;; parse-tree) for the branch(es), if necessary. We re-use vars
691 ;; `pre' and `meta' for the sake of internal func `r/d/a'.
692 (while nbls
693 (setq pre (cdr (pop nbls)))
694 (while (progn
695 (setq meta (cdr (assoc pre revisions))
696 prda nil)
697 (dolist (insn (cdr (assq :insn meta)))
698 (goto-line (pop insn))
699 (case (pop insn)
700 (k (delete-region
701 (point) (progn (forward-line (car insn))
702 (point))))
703 (i (insert (propertize
704 (car insn)
705 :vc-rcs-r/d/a
706 (or prda (setq prda (r/d/a))))))))
707 (prog1 (not (string= (if nbls (caar nbls) revision) pre))
708 (setq pre (cdr (assq 'next meta)))))))))
709 ;; Lastly, for each line, insert at bol nicely-formatted history info.
710 ;; We do two passes to collect summary information used to minimize
711 ;; the annotation's usage of screen real-estate: (1) Consider rendered
712 ;; width of revision plus author together as a unit; and (2) Omit
713 ;; author entirely if all authors are the same as the user.
714 (let ((ht (make-hash-table :test 'eq))
715 (me (user-login-name))
716 (maxw 0)
717 (all-me t)
718 rda w a)
719 (goto-char (point-max))
720 (while (not (bobp))
721 (forward-line -1)
722 (setq rda (get-text-property (point) :vc-rcs-r/d/a))
723 (unless (gethash rda ht)
724 (setq a (aref rda 2)
725 all-me (and all-me (string= a me)))
726 (puthash rda (setq w (+ (length (aref rda 0))
727 (length a)))
728 ht)
729 (setq maxw (max w maxw))))
730 (let ((padding (make-string maxw 32)))
731 (flet ((pad (w) (substring-no-properties padding w))
732 (render (rda &rest ls)
733 (propertize
734 (apply 'concat
735 (format-time-string "%Y-%m-%d" (aref rda 1))
736 " "
737 (aref rda 0)
738 ls)
df2e19c2 739 :vc-annotate-prefix t
3249f234
TTN
740 :vc-rcs-r/d/a rda)))
741 (maphash
742 (if all-me
743 (lambda (rda w)
744 (puthash rda (render rda (pad w) ": ") ht))
745 (lambda (rda w)
746 (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
747 ht)))
748 (while (not (eobp))
749 (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
750 (forward-line 1))))
751
752(defun vc-rcs-annotate-current-time ()
753 "Return the current time, based at midnight of the current day, and
754encoded as fractional days."
755 (vc-annotate-convert-time
756 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
757
758(defun vc-rcs-annotate-time ()
759 "Return the time of the next annotation (as fraction of days)
760systime, or nil if there is none. Also, reposition point."
761 (unless (eobp)
53cc5b9c
TTN
762 (prog1 (vc-annotate-convert-time
763 (aref (get-text-property (point) :vc-rcs-r/d/a) 1))
764 (goto-char (next-single-property-change (point) :vc-annotate-prefix)))))
3249f234
TTN
765
766(defun vc-rcs-annotate-extract-revision-at-line ()
767 (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
768
8f98485f
AS
769\f
770;;;
771;;; Snapshot system
772;;;
773
774(defun vc-rcs-assign-name (file name)
775 "Assign to FILE's latest version a given NAME."
776 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-n" name ":")))
777
778\f
779;;;
780;;; Miscellaneous
781;;;
782
783(defun vc-rcs-check-headers ()
784 "Check if the current file has any headers in it."
785 (save-excursion
786 (goto-char (point-min))
787 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
788\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
789
790(defun vc-rcs-clear-headers ()
791 "Implementation of vc-clear-headers for RCS."
792 (let ((case-fold-search nil))
793 (goto-char (point-min))
794 (while (re-search-forward
795 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
796 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
797 nil t)
798 (replace-match "$\\1$"))))
799
800(defun vc-rcs-rename-file (old new)
801 ;; Just move the master file (using vc-rcs-master-templates).
802 (vc-rename-master (vc-name old) new vc-rcs-master-templates))
803
804\f
805;;;
806;;; Internal functions
807;;;
808
b038f9fb
TTN
809(defun vc-rcs-root (dir)
810 (vc-find-root dir "RCS" t))
811
8f98485f
AS
812(defun vc-rcs-workfile-is-newer (file)
813 "Return non-nil if FILE is newer than its RCS master.
814This likely means that FILE has been changed with respect
815to its master version."
816 (let ((file-time (nth 5 (file-attributes file)))
817 (master-time (nth 5 (file-attributes (vc-name file)))))
818 (or (> (nth 0 file-time) (nth 0 master-time))
819 (and (= (nth 0 file-time) (nth 0 master-time))
820 (> (nth 1 file-time) (nth 1 master-time))))))
821
822(defun vc-rcs-find-most-recent-rev (branch)
823 "Find most recent revision on BRANCH."
824 (goto-char (point-min))
825 (let ((latest-rev -1) value)
826 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
827 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
828 nil t)
829 (let ((rev (string-to-number (match-string 2))))
830 (when (< latest-rev rev)
831 (setq latest-rev rev)
832 (setq value (match-string 1)))))
833 (or value
7735770b 834 (vc-branch-part branch))))
8f98485f 835
ac3f4c6f 836(defun vc-rcs-fetch-master-state (file &optional working-revision)
8f98485f
AS
837 "Compute the master file's idea of the state of FILE.
838If a WORKFILE-VERSION is given, compute the state of that version,
839otherwise determine the workfile version based on the master file.
ac3f4c6f 840This function sets the properties `vc-working-revision' and
8f98485f
AS
841`vc-checkout-model' to their correct values, based on the master
842file."
843 (with-temp-buffer
ea28aa35
AS
844 (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
845 (progn (goto-char (point-min))
846 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
847 (error "File %s is not an RCS master file" (vc-name file)))
8f98485f
AS
848 (let ((workfile-is-latest nil)
849 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
850 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
ac3f4c6f 851 (unless working-revision
8f98485f
AS
852 ;; Workfile version not known yet. Determine that first. It
853 ;; is either the head of the trunk, the head of the default
854 ;; branch, or the "default branch" itself, if that is a full
855 ;; revision number.
856 (cond
857 ;; no default branch
858 ((or (not default-branch) (string= "" default-branch))
ac3f4c6f 859 (setq working-revision
8f98485f
AS
860 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
861 (setq workfile-is-latest t))
862 ;; default branch is actually a revision
863 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
864 default-branch)
ac3f4c6f 865 (setq working-revision default-branch))
8f98485f
AS
866 ;; else, search for the head of the default branch
867 (t (vc-insert-file (vc-name file) "^desc")
ac3f4c6f 868 (setq working-revision
8f98485f
AS
869 (vc-rcs-find-most-recent-rev default-branch))
870 (setq workfile-is-latest t)))
ac3f4c6f 871 (vc-file-setprop file 'vc-working-revision working-revision))
8f98485f
AS
872 ;; Check strict locking
873 (goto-char (point-min))
874 (vc-file-setprop file 'vc-checkout-model
875 (if (re-search-forward ";[ \t\n]*strict;" nil t)
876 'locking 'implicit))
877 ;; Compute state of workfile version
878 (goto-char (point-min))
879 (let ((locking-user
880 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
ac3f4c6f 881 (regexp-quote working-revision)
8f98485f
AS
882 "[^0-9.]")
883 1)))
884 (cond
885 ;; not locked
886 ((not locking-user)
887 (if (or workfile-is-latest
ac3f4c6f 888 (vc-rcs-latest-on-branch-p file working-revision))
8f98485f 889 ;; workfile version is latest on branch
036f45fa 890 'up-to-date
8f98485f 891 ;; workfile version is not latest on branch
3702367b 892 'needs-update))
8f98485f
AS
893 ;; locked by the calling user
894 ((and (stringp locking-user)
4f147528 895 (string= locking-user (vc-user-login-name file)))
70e2f6c7 896 (if (or (eq (vc-rcs-checkout-model (list file)) 'locking)
8f98485f 897 workfile-is-latest
ac3f4c6f 898 (vc-rcs-latest-on-branch-p file working-revision))
8f98485f
AS
899 'edited
900 ;; Locking is not used for the file, but the owner does
901 ;; have a lock, and there is a higher version on the current
902 ;; branch. Not sure if this can occur, and if it is right
903 ;; to use `needs-merge' in this case.
904 'needs-merge))
905 ;; locked by somebody else
906 ((stringp locking-user)
907 locking-user)
908 (t
909 (error "Error getting state of RCS file")))))))
910
911(defun vc-rcs-consult-headers (file)
912 "Search for RCS headers in FILE, and set properties accordingly.
913
914Returns: nil if no headers were found
915 'rev if a workfile revision was found
916 'rev-and-lock if revision and lock info was found"
917 (cond
918 ((not (get-file-buffer file)) nil)
919 ((let (status version locking-user)
920 (save-excursion
921 (set-buffer (get-file-buffer file))
922 (goto-char (point-min))
923 (cond
924 ;; search for $Id or $Header
925 ;; -------------------------
926 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
927 ((or (and (search-forward "$Id\ : " nil t)
928 (looking-at "[^ ]+ \\([0-9.]+\\) "))
929 (and (progn (goto-char (point-min))
930 (search-forward "$Header\ : " nil t))
931 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
932 (goto-char (match-end 0))
933 ;; if found, store the revision number ...
934 (setq version (match-string-no-properties 1))
935 ;; ... and check for the locking state
936 (cond
937 ((looking-at
938 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
939 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
940 "[^ ]+ [^ ]+ ")) ; author & state
941 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
942 (cond
943 ;; unlocked revision
944 ((looking-at "\\$")
945 (setq locking-user 'none)
946 (setq status 'rev-and-lock))
947 ;; revision is locked by some user
948 ((looking-at "\\([^ ]+\\) \\$")
949 (setq locking-user (match-string-no-properties 1))
950 (setq status 'rev-and-lock))
951 ;; everything else: false
952 (nil)))
953 ;; unexpected information in
954 ;; keyword string --> quit
955 (nil)))
956 ;; search for $Revision
957 ;; --------------------
958 ((re-search-forward (concat "\\$"
959 "Revision: \\([0-9.]+\\) \\$")
960 nil t)
961 ;; if found, store the revision number ...
962 (setq version (match-string-no-properties 1))
963 ;; and see if there's any lock information
964 (goto-char (point-min))
965 (if (re-search-forward (concat "\\$" "Locker:") nil t)
966 (cond ((looking-at " \\([^ ]+\\) \\$")
967 (setq locking-user (match-string-no-properties 1))
968 (setq status 'rev-and-lock))
969 ((looking-at " *\\$")
970 (setq locking-user 'none)
971 (setq status 'rev-and-lock))
972 (t
973 (setq locking-user 'none)
974 (setq status 'rev-and-lock)))
975 (setq status 'rev)))
976 ;; else: nothing found
977 ;; -------------------
978 (t nil)))
ac3f4c6f 979 (if status (vc-file-setprop file 'vc-working-revision version))
8f98485f
AS
980 (and (eq status 'rev-and-lock)
981 (vc-file-setprop file 'vc-state
982 (cond
983 ((eq locking-user 'none) 'up-to-date)
53cc5b9c 984 ((string= locking-user (vc-user-login-name file))
4f147528 985 'edited)
8f98485f
AS
986 (t locking-user)))
987 ;; If the file has headers, we don't want to query the
988 ;; master file, because that would eliminate all the
989 ;; performance gain the headers brought us. We therefore
990 ;; use a heuristic now to find out whether locking is used
991 ;; for this file. If we trust the file permissions, and the
992 ;; file is not locked, then if the file is read-only we
993 ;; assume that locking is used for the file, otherwise
994 ;; locking is not used.
995 (not (vc-mistrust-permissions file))
996 (vc-up-to-date-p file)
997 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
998 (vc-file-setprop file 'vc-checkout-model 'locking)
999 (vc-file-setprop file 'vc-checkout-model 'implicit)))
1000 status))))
1001
1002(defun vc-release-greater-or-equal (r1 r2)
1003 "Compare release numbers, represented as strings.
1004Release components are assumed cardinal numbers, not decimal fractions
1005\(5.10 is a higher release than 5.9\). Omitted fields are considered
1006lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
1007of the string is found, or a non-numeric component shows up \(5.6.7 is
1008earlier than \"5.6.7 beta\", which is probably not what you want in
1009some cases\). This code is suitable for existing RCS release numbers.
1010CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
1011 (let (v1 v2 i1 i2)
1012 (catch 'done
1013 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
1014 (setq i1 (match-end 0))
1015 (setq v1 (string-to-number (match-string 1 r1)))
1016 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1017 (setq i2 (match-end 0))
1018 (setq v2 (string-to-number (match-string 1 r2)))
1019 (if (> v1 v2) (throw 'done t)
1020 (if (< v1 v2) (throw 'done nil)
1021 (throw 'done
1022 (vc-release-greater-or-equal
1023 (substring r1 i1)
1024 (substring r2 i2)))))))
1025 (throw 'done t)))
1026 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1027 (throw 'done nil))
1028 (throw 'done t)))))
1029
1030(defun vc-rcs-release-p (release)
1031 "Return t if we have RELEASE or better."
1032 (let ((installation (vc-rcs-system-release)))
1033 (if (and installation
1034 (not (eq installation 'unknown)))
1035 (vc-release-greater-or-equal installation release))))
1036
8f98485f
AS
1037(defun vc-rcs-system-release ()
1038 "Return the RCS release installed on this system, as a string.
1039Return symbol UNKNOWN if the release cannot be deducted. The user can
1040override this using variable `vc-rcs-release'.
1041
1042If the user has not set variable `vc-rcs-release' and it is nil,
1043variable `vc-rcs-release' is set to the returned value."
1044 (or vc-rcs-release
1045 (setq vc-rcs-release
1046 (or (and (zerop (vc-do-command nil nil "rcs" nil "-V"))
1047 (with-current-buffer (get-buffer "*vc*")
1048 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
1049 'unknown))))
1050
1051(defun vc-rcs-set-non-strict-locking (file)
1052 (vc-do-command nil 0 "rcs" file "-U")
1053 (vc-file-setprop file 'vc-checkout-model 'implicit)
1054 (set-file-modes file (logior (file-modes file) 128)))
1055
1056(defun vc-rcs-set-default-branch (file branch)
1057 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-b" branch))
1058 (vc-file-setprop file 'vc-rcs-default-branch branch))
1059
3249f234
TTN
1060(defun vc-rcs-parse (&optional buffer)
1061 "Parse current buffer, presumed to be in RCS-style masterfile format.
1062Optional arg BUFFER specifies another buffer to parse. Return an alist
1063of two elements, w/ keys `headers' and `revisions' and values in turn
1064sub-alists. For `headers', the values unless otherwise specified are
1065strings and the keys are:
1066
1067 desc -- description
1068 head -- latest revision
1069 branch -- the branch the \"head revision\" lies on;
1070 absent if the head revision lies on the trunk
1071 access -- ???
1072 symbols -- sub-alist of (SYMBOL . REVISION) elements
1073 locks -- if file is checked out, something like \"ttn:1.7\"
1074 strict -- t if \"strict locking\" is in effect, otherwise nil
1075 comment -- may be absent; typically something like \"# \" or \"; \"
1076 expand -- may be absent; ???
1077
1078For `revisions', the car is REVISION (string), the cdr a sub-alist,
1079with string values (unless otherwise specified) and keys:
1080
1081 date -- a time value (like that returned by `encode-time'); as a
1082 special case, a year value less than 100 is augmented by 1900
1083 author -- username
1084 state -- typically \"Exp\" or \"Rel\"
1085 branches -- list of revisions that begin branches from this revision
1086 next -- on the trunk: the chronologically-preceding revision, or \"\";
1087 on a branch: the chronologically-following revision, or \"\"
1088 log -- change log entry
1089 text -- for the head revision on the trunk, the body of the file;
1090 other revisions have `:insn' instead
1091 :insn -- for non-head revisions, a list of parsed instructions
1092 in one of two forms, in both cases START meaning \"first
1093 go to line START\":
1094 - `(START k COUNT)' -- kill COUNT lines
1095 - `(START i TEXT)' -- insert TEXT (a string)
1096 The list is in descending order by START.
1097
1098The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1099 (setq buffer (get-buffer (or buffer (current-buffer))))
1100 (set-buffer buffer)
1101 ;; An RCS masterfile can be viewed as containing four regular (for the
1102 ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1103 ;; the "description" and (d) the "rev bodies", in that order. In the
1104 ;; returned alist (see docstring), elements from (b) and (d) are
1105 ;; combined pairwise to form the "revisions", while those from (a) and
1106 ;; (c) are simply combined to form the "headers".
1107 ;;
1108 ;; Loosely speaking, each section contains a series of alternating
1109 ;; "tags" and "printed representations". In the (b) and (d), many
1110 ;; such series can appear, and a revision number on a line by itself
1111 ;; precedes the series of tags and printed representations associated
1112 ;; with it.
1113 ;;
1114 ;; In (a) and (b), the printed representations (with the exception of
1115 ;; the `comment' tag in the headers) terminate with a semicolon, which
1116 ;; is NOT part of the "value" finally associated with the tag. All
1117 ;; other printed representations are in "@@-format"; there is an "@",
1118 ;; the middle part (to be translated into the value), another "@" and
1119 ;; a newline. Each "@@" in the middle part indicates the position of
1120 ;; a single "@" (and consequently the requirement of an additional
1121 ;; initial step when translating to the value).
1122 ;;
1123 ;; Parser state includes vars that collect parts of the return value...
1124 (let ((desc nil) (headers nil) (revs nil)
1125 ;; ... as well as vars that support a single-pass, tag-assisted,
1126 ;; minimal-data-copying scan. Basically -- skirting around the
1127 ;; grouping by revision required in (b) and (d) -- we repeatedly
1128 ;; and context-sensitively read a tag (that MUST be present),
1129 ;; determine the bounds of the printed representation, translate
1130 ;; it into a value, and push the tag plus value onto one of the
1131 ;; collection vars. Finally, we return the parse tree
1132 ;; incorporating the values of the collection vars (see "rv").
1133 ;;
1134 ;; A symbol or string to keep track of context (for error messages).
1135 context
1136 ;; A symbol, the current tag.
1137 tok
1138 ;; Region (begin and end buffer positions) of the printed
1139 ;; representation for the current tag.
1140 b e
1141 ;; A list of buffer positions where "@@" can be found within the
1142 ;; printed representation region. For each location, we push two
1143 ;; elements onto the list, 1+ and 2+ the location, respectively,
1144 ;; with the 2+ appearing at the head. In this way, the expression
1145 ;; `(,e ,@@-holes ,b)
1146 ;; describes regions that can be concatenated (in reverse order)
1147 ;; to "de-@@-format" the printed representation as the first step
1148 ;; to translating it into some value. See internal func `gather'.
1149 @-holes)
1150 (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1151 (at (tag) (save-excursion (eq tag (read buffer))))
1152 (to-eol () (buffer-substring-no-properties
1153 (point) (progn (forward-line 1)
1154 (1- (point)))))
1155 (to-semi () (setq b (point)
1156 e (progn (search-forward ";")
1157 (1- (point)))))
1158 (to-one@ () (setq @-holes nil
1159 b (progn (search-forward "@") (point))
1160 e (progn (while (and (search-forward "@")
1161 (= ?@ (char-after))
1162 (progn
1163 (push (point) @-holes)
1164 (forward-char 1)
1165 (push (point) @-holes))))
1166 (1- (point)))))
1167 (tok+val (set-b+e name &optional proc)
1168 (unless (eq name (setq tok (read buffer)))
1169 (error "Missing `%s' while parsing %s" name context))
1170 (sw)
1171 (funcall set-b+e)
1172 (cons tok (if proc
1173 (funcall proc)
1174 (buffer-substring-no-properties b e))))
1175 (k-semi (name &optional proc) (tok+val 'to-semi name proc))
1176 (gather () (let ((pairs `(,e ,@@-holes ,b))
1177 acc)
1178 (while pairs
1179 (push (buffer-substring-no-properties
1180 (cadr pairs) (car pairs))
1181 acc)
1182 (setq pairs (cddr pairs)))
1183 (apply 'concat acc)))
1184 (k-one@ (name &optional later) (tok+val 'to-one@ name
1185 (if later
1186 (lambda () t)
1187 'gather))))
1188 (save-excursion
1189 (goto-char (point-min))
1190 ;; headers
1191 (setq context 'headers)
1192 (flet ((hpush (name &optional proc)
1193 (push (k-semi name proc) headers)))
1194 (hpush 'head)
1195 (when (at 'branch)
1196 (hpush 'branch))
1197 (hpush 'access)
1198 (hpush 'symbols
1199 (lambda ()
1200 (mapcar (lambda (together)
1201 (let ((two (split-string together ":")))
1202 (setcar two (intern (car two)))
1203 (setcdr two (cadr two))
1204 two))
1205 (split-string
1206 (buffer-substring-no-properties b e)))))
1207 (hpush 'locks))
1208 (push `(strict . ,(when (at 'strict)
1209 (search-forward ";")
1210 t))
1211 headers)
1212 (when (at 'comment)
1213 (push (k-one@ 'comment) headers)
1214 (search-forward ";"))
1215 (when (at 'expand)
1216 (push (k-one@ 'expand) headers)
1217 (search-forward ";"))
1218 (setq headers (nreverse headers))
1219 ;; rev headers
1220 (sw) (setq context 'rev-headers)
1221 (while (looking-at "[0-9]")
1222 (push `(,(to-eol)
1223 ,(k-semi 'date
1224 (lambda ()
1225 (let ((ls (mapcar 'string-to-number
1226 (split-string
1227 (buffer-substring-no-properties
1228 b e)
1229 "\\."))))
1230 ;; Hack the year -- verified to be the
1231 ;; same algorithm used in RCS 5.7.
1232 (when (< (car ls) 100)
1233 (setcar ls (+ 1900 (car ls))))
1234 (apply 'encode-time (nreverse ls)))))
1235 ,@(mapcar 'k-semi '(author state))
1236 ,(k-semi 'branches
1237 (lambda ()
1238 (split-string
1239 (buffer-substring-no-properties b e))))
1240 ,(k-semi 'next))
1241 revs)
1242 (sw))
1243 (setq revs (nreverse revs))
1244 ;; desc
1245 (sw) (setq context 'desc
1246 desc (k-one@ 'desc))
1247 ;; rev bodies
1248 (let (acc
1249 ;; Element of `revs' that initially holds only header info.
1250 ;; "Pairwise combination" occurs when we add body info.
1251 rev
1252 ;; Components of the editing commands (aside from the actual
1253 ;; text) that comprise the `text' printed representations
1254 ;; (not including the "head" revision).
1255 cmd start act
1256 ;; Ascending (reversed) `@-holes' which the internal func
1257 ;; `incg' pops to effect incremental gathering.
1258 asc
1259 ;; Function to extract text (for the `a' command), either
1260 ;; `incg' or `buffer-substring-no-properties'. (This is
1261 ;; for speed; strictly speaking, it is sufficient to use
1262 ;; only the former since it behaves identically to the
1263 ;; latter in the absense of "@@".)
1264 sub)
1265 (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
1266 (while (and asc (< (car asc) e))
1267 (push (pop asc) @-holes))
1268 ;; Self-deprecate when work is done.
1269 ;; Folding many dimensions into one.
1270 ;; Thanks B.Mandelbrot, for complex sum.
1271 ;; O beauteous math! --the Unvexed Bum
1272 (unless asc
1273 (setq sub 'buffer-substring-no-properties))
1274 (gather))))
1275 (while (and (sw)
1276 (not (eobp))
1277 (setq context (to-eol)
1278 rev (or (assoc context revs)
1279 (error "Rev `%s' has body but no head"
1280 context))))
1281 (push (k-one@ 'log) (cdr rev))
1282 ;; For rev body `text' tags, delay translation slightly...
1283 (push (k-one@ 'text t) (cdr rev))
1284 ;; ... until we decide which tag and value is appropriate to
1285 ;; collect. For the "head" revision, compute the value of the
1286 ;; `text' printed representation by simple `gather'. For all
1287 ;; other revisions, replace the `text' tag+value with `:insn'
1288 ;; plus value, always scanning in-place.
1289 (if (string= context (cdr (assq 'head headers)))
1290 (setcdr (cadr rev) (gather))
1291 (if @-holes
1292 (setq asc (nreverse @-holes)
1293 sub 'incg)
1294 (setq sub 'buffer-substring-no-properties))
1295 (goto-char b)
1296 (setq acc nil)
1297 (while (< (point) e)
1298 (forward-char 1)
1299 (setq cmd (char-before)
1300 start (read (current-buffer))
1301 act (read (current-buffer)))
1302 (forward-char 1)
1303 (push (case cmd
1304 (?d
1305 ;; `d' means "delete lines".
1306 ;; For Emacs spirit, we use `k' for "kill".
1307 `(,start k ,act))
1308 (?a
1309 ;; `a' means "append after this line" but
1310 ;; internally we normalize it so that START
1311 ;; specifies the actual line for insert, thus
1312 ;; requiring less hair in the realization algs.
1313 ;; For Emacs spirit, we use `i' for "insert".
1314 `(,(1+ start) i
1315 ,(funcall sub (point) (progn (forward-line act)
1316 (point)))))
1317 (t (error "Bad command `%c' in `text' for rev `%s'"
1318 cmd context)))
1319 acc))
1320 (goto-char (1+ e))
1321 (setcar (cdr rev) (cons :insn acc)))))))
1322 ;; rv
1323 `((headers ,desc ,@headers)
1324 (revisions ,@revs)))))
1325
d8aff077
GM
1326(provide 'vc-rcs)
1327
fa63cb6d 1328;; arch-tag: 759b4916-5b0d-431d-b647-b185b8c652cf
d8aff077 1329;;; vc-rcs.el ends here