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