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