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