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