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