]> code.delx.au - gnu-emacs/blob - lisp/vc-rcs.el
(vc-rcs-diff): Use consistent parameter format for
[gnu-emacs] / lisp / vc-rcs.el
1 ;;; vc-rcs.el --- support for RCS version-control
2
3 ;; Copyright (C) 1992,93,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
8 ;; $Id: vc-rcs.el,v 1.22 2002/01/05 17:21:06 spiegel Exp $
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; See vc.el
30
31 ;;; Code:
32
33 ;;;
34 ;;; Customization options
35 ;;;
36
37 (eval-when-compile
38 (require 'cl)
39 (require 'vc))
40
41 (defcustom vc-rcs-release nil
42 "*The release number of your RCS installation, as a string.
43 If nil, VC itself computes this value when it is first needed."
44 :type '(choice (const :tag "Auto" nil)
45 (string :tag "Specified")
46 (const :tag "Unknown" unknown))
47 :group 'vc)
48
49 (defcustom vc-rcs-register-switches nil
50 "*Extra switches for registering a file in RCS.
51 A string or list of strings. These are passed to the checkin program
52 by \\[vc-rcs-register]."
53 :type '(choice (const :tag "None" nil)
54 (string :tag "Argument String")
55 (repeat :tag "Argument List"
56 :value ("")
57 string))
58 :version "21.1"
59 :group 'vc)
60
61 (defcustom vc-rcs-checkin-switches nil
62 "*A string or list of strings specifying extra switches for RCS checkin.
63 These are passed to the checkin program by \\[vc-rcs-checkin]."
64 :type '(choice (const :tag "None" nil)
65 (string :tag "Argument String")
66 (repeat :tag "Argument List"
67 :value ("")
68 string))
69 :version "21.1"
70 :group 'vc)
71
72 (defcustom vc-rcs-checkout-switches nil
73 "*A string or list of strings specifying extra switches for RCS checkout.
74 These are passed to the checkout program by \\[vc-rcs-checkout]."
75 :type '(choice (const :tag "None" nil)
76 (string :tag "Argument String")
77 (repeat :tag "Argument List"
78 :value ("")
79 string))
80 :version "21.1"
81 :group 'vc)
82
83 (defcustom vc-rcs-diff-switches nil
84 "*A string or list of strings specifying extra switches for rcsdiff under VC."
85 :type '(choice (const :tag "None" nil)
86 (string :tag "Argument String")
87 (repeat :tag "Argument List"
88 :value ("")
89 string))
90 :version "21.1"
91 :group 'vc)
92
93 (defcustom vc-rcs-header (or (cdr (assoc 'RCS vc-header-alist)) '("\$Id\$"))
94 "*Header keywords to be inserted by `vc-insert-headers'."
95 :type '(repeat string)
96 :version "21.1"
97 :group 'vc)
98
99 (defcustom vc-rcsdiff-knows-brief nil
100 "*Indicates whether rcsdiff understands the --brief option.
101 The value is either `yes', `no', or nil. If it is nil, VC tries
102 to use --brief and sets this variable to remember whether it worked."
103 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
104 :group 'vc)
105
106 ;;;###autoload
107 (defcustom vc-rcs-master-templates
108 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s")
109 "*Where to look for RCS master files.
110 For a description of possible values, see `vc-check-master-templates'."
111 :type '(choice (const :tag "Use standard RCS file names"
112 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
113 (repeat :tag "User-specified"
114 (choice string
115 function)))
116 :version "21.1"
117 :group 'vc)
118
119 \f
120 ;;;
121 ;;; State-querying functions
122 ;;;
123
124 ;;;###autoload
125 (progn (defun vc-rcs-registered (f) (vc-default-registered 'RCS f)))
126
127 (defun vc-rcs-state (file)
128 "Implementation of `vc-state' for RCS."
129 (or (boundp 'vc-rcs-headers-result)
130 (and vc-consult-headers
131 (vc-rcs-consult-headers file)))
132 (let ((state
133 ;; vc-workfile-version might not be known; in that case the
134 ;; property is nil. vc-rcs-fetch-master-state knows how to
135 ;; handle that.
136 (vc-rcs-fetch-master-state file
137 (vc-file-getprop file
138 'vc-workfile-version))))
139 (if (not (eq state 'up-to-date))
140 state
141 (require 'vc)
142 (if (vc-workfile-unchanged-p file)
143 'up-to-date
144 (if (eq (vc-checkout-model file) 'locking)
145 'unlocked-changes
146 'edited)))))
147
148 (defun vc-rcs-state-heuristic (file)
149 "State heuristic for RCS."
150 (let (vc-rcs-headers-result)
151 (if (and vc-consult-headers
152 (setq vc-rcs-headers-result
153 (vc-rcs-consult-headers file))
154 (eq vc-rcs-headers-result 'rev-and-lock))
155 (let ((state (vc-file-getprop file 'vc-state)))
156 ;; If the headers say that the file is not locked, the
157 ;; permissions can tell us whether locking is used for
158 ;; the file or not.
159 (if (and (eq state 'up-to-date)
160 (not (vc-mistrust-permissions file)))
161 (cond
162 ((string-match ".rw..-..-." (nth 8 (file-attributes file)))
163 (vc-file-setprop file 'vc-checkout-model 'implicit)
164 (setq state
165 (if (vc-rcs-workfile-is-newer file)
166 'edited
167 'up-to-date)))
168 ((string-match ".r-..-..-." (nth 8 (file-attributes file)))
169 (vc-file-setprop file 'vc-checkout-model 'locking))))
170 state)
171 (if (not (vc-mistrust-permissions file))
172 (let* ((attributes (file-attributes file))
173 (owner-uid (nth 2 attributes))
174 (permissions (nth 8 attributes)))
175 (cond ((string-match ".r-..-..-." permissions)
176 (vc-file-setprop file 'vc-checkout-model 'locking)
177 'up-to-date)
178 ((string-match ".rw..-..-." permissions)
179 (if (eq (vc-checkout-model file) 'locking)
180 (if (file-ownership-preserved-p file)
181 'edited
182 (vc-user-login-name owner-uid))
183 (if (vc-rcs-workfile-is-newer file)
184 'edited
185 'up-to-date)))
186 (t
187 ;; Strange permissions. Fall through to
188 ;; expensive state computation.
189 (vc-rcs-state file))))
190 (vc-rcs-state file)))))
191
192 (defun vc-rcs-workfile-version (file)
193 "RCS-specific version of `vc-workfile-version'."
194 (or (and vc-consult-headers
195 (vc-rcs-consult-headers file)
196 (vc-file-getprop file 'vc-workfile-version))
197 (progn
198 (vc-rcs-fetch-master-state file)
199 (vc-file-getprop file 'vc-workfile-version))))
200
201 (defun vc-rcs-latest-on-branch-p (file &optional version)
202 "Return non-nil if workfile version of FILE is the latest on its branch.
203 When VERSION is given, perform check for that version."
204 (unless version (setq version (vc-workfile-version file)))
205 (with-temp-buffer
206 (string= version
207 (if (vc-trunk-p version)
208 (progn
209 ;; Compare VERSION to the head version number.
210 (vc-insert-file (vc-name file) "^[0-9]")
211 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
212 ;; If we are not on the trunk, we need to examine the
213 ;; whole current branch.
214 (vc-insert-file (vc-name file) "^desc")
215 (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
216
217 (defun vc-rcs-checkout-model (file)
218 "RCS-specific version of `vc-checkout-model'."
219 (vc-rcs-consult-headers file)
220 (or (vc-file-getprop file 'vc-checkout-model)
221 (progn (vc-rcs-fetch-master-state file)
222 (vc-file-getprop file 'vc-checkout-model))))
223
224 (defun vc-rcs-workfile-unchanged-p (file)
225 "RCS-specific implementation of vc-workfile-unchanged-p."
226 ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
227 ;; do a double take and remember the fact for the future
228 (let* ((version (concat "-r" (vc-workfile-version file)))
229 (status (if (eq vc-rcsdiff-knows-brief 'no)
230 (vc-do-command nil 1 "rcsdiff" file version)
231 (vc-do-command nil 2 "rcsdiff" file "--brief" version))))
232 (if (eq status 2)
233 (if (not vc-rcsdiff-knows-brief)
234 (setq vc-rcsdiff-knows-brief 'no
235 status (vc-do-command nil 1 "rcsdiff" file version))
236 (error "rcsdiff failed"))
237 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
238 ;; The workfile is unchanged if rcsdiff found no differences.
239 (zerop status)))
240
241 \f
242 ;;;
243 ;;; State-changing functions
244 ;;;
245
246 (defun vc-rcs-register (file &optional rev comment)
247 "Register FILE into the RCS version-control system.
248 REV is the optional revision number for the file. COMMENT can be used
249 to provide an initial description of FILE.
250
251 `vc-register-switches' and `vc-rcs-register-switches' are passed to
252 the RCS command (in that order).
253
254 Automatically retrieve a read-only version of the file with keywords
255 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
256 (let ((subdir (expand-file-name "RCS" (file-name-directory file)))
257 (switches (list
258 (if (stringp vc-register-switches)
259 (list vc-register-switches)
260 vc-register-switches)
261 (if (stringp vc-rcs-register-switches)
262 (list vc-rcs-register-switches)
263 vc-rcs-register-switches))))
264
265 (and (not (file-exists-p subdir))
266 (not (directory-files (file-name-directory file)
267 nil ".*,v$" t))
268 (yes-or-no-p "Create RCS subdirectory? ")
269 (make-directory subdir))
270 (apply 'vc-do-command nil 0 "ci" file
271 ;; if available, use the secure registering option
272 (and (vc-rcs-release-p "5.6.4") "-i")
273 (concat (if vc-keep-workfiles "-u" "-r") rev)
274 (and comment (concat "-t-" comment))
275 switches)
276 ;; parse output to find master file name and workfile version
277 (with-current-buffer "*vc*"
278 (goto-char (point-min))
279 (let ((name (if (looking-at (concat "^\\(.*\\) <-- "
280 (file-name-nondirectory file)))
281 (match-string 1))))
282 (if (not name)
283 ;; if we couldn't find the master name,
284 ;; run vc-rcs-registered to get it
285 ;; (will be stored into the vc-name property)
286 (vc-rcs-registered file)
287 (vc-file-setprop file 'vc-name
288 (if (file-name-absolute-p name)
289 name
290 (expand-file-name
291 name
292 (file-name-directory file))))))
293 (vc-file-setprop file 'vc-workfile-version
294 (if (re-search-forward
295 "^initial revision: \\([0-9.]+\\).*\n"
296 nil t)
297 (match-string 1))))))
298
299 (defun vc-rcs-responsible-p (file)
300 "Return non-nil if RCS thinks it would be responsible for registering FILE."
301 ;; TODO: check for all the patterns in vc-rcs-master-templates
302 (file-directory-p (expand-file-name "RCS" (file-name-directory file))))
303
304 (defun vc-rcs-receive-file (file rev)
305 "Implementation of receive-file for RCS."
306 (let ((checkout-model (vc-checkout-model file)))
307 (vc-rcs-register file rev "")
308 (when (eq checkout-model 'implicit)
309 (vc-rcs-set-non-strict-locking file))
310 (vc-rcs-set-default-branch file (concat rev ".1"))))
311
312 (defun vc-rcs-unregister (file)
313 "Unregister FILE from RCS.
314 If this leaves the RCS subdirectory empty, ask the user
315 whether to remove it."
316 (let* ((master (vc-name file))
317 (dir (file-name-directory master))
318 (backup-info (find-backup-file-name master)))
319 (if (not backup-info)
320 (delete-file master)
321 (rename-file master (car backup-info) 'ok-if-already-exists)
322 (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
323 (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
324 ;; check whether RCS dir is empty, i.e. it does not
325 ;; contain any files except "." and ".."
326 (not (directory-files dir nil
327 "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
328 (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
329 (delete-directory dir))))
330
331 (defun vc-rcs-checkin (file rev comment)
332 "RCS-specific version of `vc-backend-checkin'."
333 (let ((switches (if (stringp vc-checkin-switches)
334 (list vc-checkin-switches)
335 vc-checkin-switches)))
336 (let ((old-version (vc-workfile-version file)) new-version
337 (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
338 ;; Force branch creation if an appropriate
339 ;; default branch has been set.
340 (and (not rev)
341 default-branch
342 (string-match (concat "^" (regexp-quote old-version) "\\.")
343 default-branch)
344 (setq rev default-branch)
345 (setq switches (cons "-f" switches)))
346 (apply 'vc-do-command nil 0 "ci" (vc-name file)
347 ;; if available, use the secure check-in option
348 (and (vc-rcs-release-p "5.6.4") "-j")
349 (concat (if vc-keep-workfiles "-u" "-r") rev)
350 (concat "-m" comment)
351 switches)
352 (vc-file-setprop file 'vc-workfile-version nil)
353
354 ;; determine the new workfile version
355 (set-buffer "*vc*")
356 (goto-char (point-min))
357 (when (or (re-search-forward
358 "new revision: \\([0-9.]+\\);" nil t)
359 (re-search-forward
360 "reverting to previous revision \\([0-9.]+\\)" nil t))
361 (setq new-version (match-string 1))
362 (vc-file-setprop file 'vc-workfile-version new-version))
363
364 ;; if we got to a different branch, adjust the default
365 ;; branch accordingly
366 (cond
367 ((and old-version new-version
368 (not (string= (vc-branch-part old-version)
369 (vc-branch-part new-version))))
370 (vc-rcs-set-default-branch file
371 (if (vc-trunk-p new-version) nil
372 (vc-branch-part new-version)))
373 ;; If this is an old RCS release, we might have
374 ;; to remove a remaining lock.
375 (if (not (vc-rcs-release-p "5.6.2"))
376 ;; exit status of 1 is also accepted.
377 ;; It means that the lock was removed before.
378 (vc-do-command nil 1 "rcs" (vc-name file)
379 (concat "-u" old-version))))))))
380
381 (defun vc-rcs-checkout (file &optional editable rev workfile)
382 "Retrieve a copy of a saved version of FILE into a workfile."
383 (let ((filename (or workfile file))
384 (file-buffer (get-file-buffer file))
385 switches)
386 (message "Checking out %s..." filename)
387 (save-excursion
388 ;; Change buffers to get local value of vc-checkout-switches.
389 (if file-buffer (set-buffer file-buffer))
390 (setq switches (if (stringp vc-checkout-switches)
391 (list vc-checkout-switches)
392 vc-checkout-switches))
393 ;; Save this buffer's default-directory
394 ;; and use save-excursion to make sure it is restored
395 ;; in the same buffer it was saved in.
396 (let ((default-directory default-directory))
397 (save-excursion
398 ;; Adjust the default-directory so that the check-out creates
399 ;; the file in the right place.
400 (setq default-directory (file-name-directory filename))
401 (if workfile ;; RCS
402 ;; RCS can't check out into arbitrary file names directly.
403 ;; Use `co -p' and make stdout point to the correct file.
404 (let ((vc-modes (logior (file-modes (vc-name file))
405 (if editable 128 0)))
406 (failed t))
407 (unwind-protect
408 (progn
409 (let ((coding-system-for-read 'no-conversion)
410 (coding-system-for-write 'no-conversion))
411 (with-temp-file filename
412 (apply 'vc-do-command
413 (current-buffer) 0 "co" (vc-name file)
414 "-q" ;; suppress diagnostic output
415 (if editable "-l")
416 (concat "-p" rev)
417 switches)))
418 (set-file-modes filename
419 (logior (file-modes (vc-name file))
420 (if editable 128 0)))
421 (setq failed nil))
422 (and failed (file-exists-p filename)
423 (delete-file filename))))
424 (let (new-version)
425 ;; if we should go to the head of the trunk,
426 ;; clear the default branch first
427 (and rev (string= rev "")
428 (vc-rcs-set-default-branch file nil))
429 ;; now do the checkout
430 (apply 'vc-do-command
431 nil 0 "co" (vc-name file)
432 ;; If locking is not strict, force to overwrite
433 ;; the writable workfile.
434 (if (eq (vc-checkout-model file) 'implicit) "-f")
435 (if editable "-l")
436 (if rev (concat "-r" rev)
437 ;; if no explicit revision was specified,
438 ;; check out that of the working file
439 (let ((workrev (vc-workfile-version file)))
440 (if workrev (concat "-r" workrev)
441 nil)))
442 switches)
443 ;; determine the new workfile version
444 (with-current-buffer "*vc*"
445 (setq new-version
446 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
447 (vc-file-setprop file 'vc-workfile-version new-version)
448 ;; if necessary, adjust the default branch
449 (and rev (not (string= rev ""))
450 (vc-rcs-set-default-branch
451 file
452 (if (vc-rcs-latest-on-branch-p file new-version)
453 (if (vc-trunk-p new-version) nil
454 (vc-branch-part new-version))
455 new-version))))))
456 (message "Checking out %s...done" filename)))))
457
458 (defun vc-rcs-revert (file &optional contents-done)
459 "Revert FILE to the version it was based on."
460 (vc-do-command nil 0 "co" (vc-name file) "-f"
461 (concat "-u" (vc-workfile-version file))))
462
463 (defun vc-rcs-cancel-version (file editable)
464 "Undo the most recent checkin of FILE.
465 EDITABLE non-nil means previous version should be locked."
466 (let* ((target (vc-workfile-version file))
467 (previous (if (vc-trunk-p target) "" (vc-branch-part target)))
468 (config (current-window-configuration))
469 (done nil))
470 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-o" target))
471 ;; Check out the most recent remaining version. If it fails, because
472 ;; the whole branch got deleted, do a double-take and check out the
473 ;; version where the branch started.
474 (while (not done)
475 (condition-case err
476 (progn
477 (vc-do-command nil 0 "co" (vc-name file) "-f"
478 (concat (if editable "-l" "-u") previous))
479 (setq done t))
480 (error (set-buffer "*vc*")
481 (goto-char (point-min))
482 (if (search-forward "no side branches present for" nil t)
483 (progn (setq previous (vc-branch-part previous))
484 (vc-rcs-set-default-branch file previous)
485 ;; vc-do-command popped up a window with
486 ;; the error message. Get rid of it, by
487 ;; restoring the old window configuration.
488 (set-window-configuration config))
489 ;; No, it was some other error: re-signal it.
490 (signal (car err) (cdr err))))))))
491
492 (defun vc-rcs-merge (file first-version &optional second-version)
493 "Merge changes into current working copy of FILE.
494 The changes are between FIRST-VERSION and SECOND-VERSION."
495 (vc-do-command nil 1 "rcsmerge" (vc-name file)
496 "-kk" ; ignore keyword conflicts
497 (concat "-r" first-version)
498 (if second-version (concat "-r" second-version))))
499
500 (defun vc-rcs-steal-lock (file &optional rev)
501 "Steal the lock on the current workfile for FILE and revision REV.
502 Needs RCS 5.6.2 or later for -M."
503 (vc-do-command nil 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
504 ;; Do a real checkout after stealing the lock, so that we see
505 ;; expanded headers.
506 (vc-do-command nil 0 "co" (vc-name file) "-f" (concat "-l" rev)))
507
508
509 \f
510 ;;;
511 ;;; History functions
512 ;;;
513
514 (defun vc-rcs-print-log (file)
515 "Get change log associated with FILE."
516 (vc-do-command nil 0 "rlog" (vc-name file)))
517
518 (defun vc-rcs-show-log-entry (version)
519 (when (re-search-forward
520 ;; also match some context, for safety
521 (concat "----\nrevision " version
522 "\\(\tlocked by:.*\n\\|\n\\)date: ") nil t)
523 ;; set the display window so that
524 ;; the whole log entry is displayed
525 (let (start end lines)
526 (beginning-of-line) (forward-line -1) (setq start (point))
527 (if (not (re-search-forward "^----*\nrevision" nil t))
528 (setq end (point-max))
529 (beginning-of-line) (forward-line -1) (setq end (point)))
530 (setq lines (count-lines start end))
531 (cond
532 ;; if the global information and this log entry fit
533 ;; into the window, display from the beginning
534 ((< (count-lines (point-min) end) (window-height))
535 (goto-char (point-min))
536 (recenter 0)
537 (goto-char start))
538 ;; if the whole entry fits into the window,
539 ;; display it centered
540 ((< (1+ lines) (window-height))
541 (goto-char start)
542 (recenter (1- (- (/ (window-height) 2) (/ lines 2)))))
543 ;; otherwise (the entry is too large for the window),
544 ;; display from the start
545 (t
546 (goto-char start)
547 (recenter 0))))))
548
549 (defun vc-rcs-diff (file &optional oldvers newvers)
550 "Get a difference report using RCS between two versions of FILE."
551 (if (not oldvers) (setq oldvers (vc-workfile-version file)))
552 (apply 'vc-do-command "*vc-diff*" 1 "rcsdiff" file
553 (append (list "-q"
554 (concat "-r" oldvers)
555 (and newvers (concat "-r" newvers)))
556 (vc-diff-switches-list 'RCS))))
557
558 \f
559 ;;;
560 ;;; Snapshot system
561 ;;;
562
563 (defun vc-rcs-assign-name (file name)
564 "Assign to FILE's latest version a given NAME."
565 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-n" name ":")))
566
567 \f
568 ;;;
569 ;;; Miscellaneous
570 ;;;
571
572 (defun vc-rcs-check-headers ()
573 "Check if the current file has any headers in it."
574 (save-excursion
575 (goto-char (point-min))
576 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
577 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
578
579 (defun vc-rcs-clear-headers ()
580 "Implementation of vc-clear-headers for RCS."
581 (let ((case-fold-search nil))
582 (goto-char (point-min))
583 (while (re-search-forward
584 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
585 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
586 nil t)
587 (replace-match "$\\1$"))))
588
589 (defun vc-rcs-rename-file (old new)
590 ;; Just move the master file (using vc-rcs-master-templates).
591 (vc-rename-master (vc-name old) new vc-rcs-master-templates))
592
593 \f
594 ;;;
595 ;;; Internal functions
596 ;;;
597
598 (defun vc-rcs-workfile-is-newer (file)
599 "Return non-nil if FILE is newer than its RCS master.
600 This likely means that FILE has been changed with respect
601 to its master version."
602 (let ((file-time (nth 5 (file-attributes file)))
603 (master-time (nth 5 (file-attributes (vc-name file)))))
604 (or (> (nth 0 file-time) (nth 0 master-time))
605 (and (= (nth 0 file-time) (nth 0 master-time))
606 (> (nth 1 file-time) (nth 1 master-time))))))
607
608 (defun vc-rcs-find-most-recent-rev (branch)
609 "Find most recent revision on BRANCH."
610 (goto-char (point-min))
611 (let ((latest-rev -1) value)
612 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
613 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
614 nil t)
615 (let ((rev (string-to-number (match-string 2))))
616 (when (< latest-rev rev)
617 (setq latest-rev rev)
618 (setq value (match-string 1)))))
619 (or value
620 (vc-branch-part branch))))
621
622 (defun vc-rcs-fetch-master-state (file &optional workfile-version)
623 "Compute the master file's idea of the state of FILE.
624 If a WORKFILE-VERSION is given, compute the state of that version,
625 otherwise determine the workfile version based on the master file.
626 This function sets the properties `vc-workfile-version' and
627 `vc-checkout-model' to their correct values, based on the master
628 file."
629 (with-temp-buffer
630 (vc-insert-file (vc-name file) "^[0-9]")
631 (let ((workfile-is-latest nil)
632 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
633 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
634 (unless workfile-version
635 ;; Workfile version not known yet. Determine that first. It
636 ;; is either the head of the trunk, the head of the default
637 ;; branch, or the "default branch" itself, if that is a full
638 ;; revision number.
639 (cond
640 ;; no default branch
641 ((or (not default-branch) (string= "" default-branch))
642 (setq workfile-version
643 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
644 (setq workfile-is-latest t))
645 ;; default branch is actually a revision
646 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
647 default-branch)
648 (setq workfile-version default-branch))
649 ;; else, search for the head of the default branch
650 (t (vc-insert-file (vc-name file) "^desc")
651 (setq workfile-version
652 (vc-rcs-find-most-recent-rev default-branch))
653 (setq workfile-is-latest t)))
654 (vc-file-setprop file 'vc-workfile-version workfile-version))
655 ;; Check strict locking
656 (goto-char (point-min))
657 (vc-file-setprop file 'vc-checkout-model
658 (if (re-search-forward ";[ \t\n]*strict;" nil t)
659 'locking 'implicit))
660 ;; Compute state of workfile version
661 (goto-char (point-min))
662 (let ((locking-user
663 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
664 (regexp-quote workfile-version)
665 "[^0-9.]")
666 1)))
667 (cond
668 ;; not locked
669 ((not locking-user)
670 (if (or workfile-is-latest
671 (vc-rcs-latest-on-branch-p file workfile-version))
672 ;; workfile version is latest on branch
673 'up-to-date
674 ;; workfile version is not latest on branch
675 'needs-patch))
676 ;; locked by the calling user
677 ((and (stringp locking-user)
678 (string= locking-user (vc-user-login-name)))
679 (if (or (eq (vc-checkout-model file) 'locking)
680 workfile-is-latest
681 (vc-rcs-latest-on-branch-p file workfile-version))
682 'edited
683 ;; Locking is not used for the file, but the owner does
684 ;; have a lock, and there is a higher version on the current
685 ;; branch. Not sure if this can occur, and if it is right
686 ;; to use `needs-merge' in this case.
687 'needs-merge))
688 ;; locked by somebody else
689 ((stringp locking-user)
690 locking-user)
691 (t
692 (error "Error getting state of RCS file")))))))
693
694 (defun vc-rcs-consult-headers (file)
695 "Search for RCS headers in FILE, and set properties accordingly.
696
697 Returns: nil if no headers were found
698 'rev if a workfile revision was found
699 'rev-and-lock if revision and lock info was found"
700 (cond
701 ((not (get-file-buffer file)) nil)
702 ((let (status version locking-user)
703 (save-excursion
704 (set-buffer (get-file-buffer file))
705 (goto-char (point-min))
706 (cond
707 ;; search for $Id or $Header
708 ;; -------------------------
709 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
710 ((or (and (search-forward "$Id\ : " nil t)
711 (looking-at "[^ ]+ \\([0-9.]+\\) "))
712 (and (progn (goto-char (point-min))
713 (search-forward "$Header\ : " nil t))
714 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
715 (goto-char (match-end 0))
716 ;; if found, store the revision number ...
717 (setq version (match-string-no-properties 1))
718 ;; ... and check for the locking state
719 (cond
720 ((looking-at
721 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
722 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
723 "[^ ]+ [^ ]+ ")) ; author & state
724 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
725 (cond
726 ;; unlocked revision
727 ((looking-at "\\$")
728 (setq locking-user 'none)
729 (setq status 'rev-and-lock))
730 ;; revision is locked by some user
731 ((looking-at "\\([^ ]+\\) \\$")
732 (setq locking-user (match-string-no-properties 1))
733 (setq status 'rev-and-lock))
734 ;; everything else: false
735 (nil)))
736 ;; unexpected information in
737 ;; keyword string --> quit
738 (nil)))
739 ;; search for $Revision
740 ;; --------------------
741 ((re-search-forward (concat "\\$"
742 "Revision: \\([0-9.]+\\) \\$")
743 nil t)
744 ;; if found, store the revision number ...
745 (setq version (match-string-no-properties 1))
746 ;; and see if there's any lock information
747 (goto-char (point-min))
748 (if (re-search-forward (concat "\\$" "Locker:") nil t)
749 (cond ((looking-at " \\([^ ]+\\) \\$")
750 (setq locking-user (match-string-no-properties 1))
751 (setq status 'rev-and-lock))
752 ((looking-at " *\\$")
753 (setq locking-user 'none)
754 (setq status 'rev-and-lock))
755 (t
756 (setq locking-user 'none)
757 (setq status 'rev-and-lock)))
758 (setq status 'rev)))
759 ;; else: nothing found
760 ;; -------------------
761 (t nil)))
762 (if status (vc-file-setprop file 'vc-workfile-version version))
763 (and (eq status 'rev-and-lock)
764 (vc-file-setprop file 'vc-state
765 (cond
766 ((eq locking-user 'none) 'up-to-date)
767 ((string= locking-user (vc-user-login-name)) 'edited)
768 (t locking-user)))
769 ;; If the file has headers, we don't want to query the
770 ;; master file, because that would eliminate all the
771 ;; performance gain the headers brought us. We therefore
772 ;; use a heuristic now to find out whether locking is used
773 ;; for this file. If we trust the file permissions, and the
774 ;; file is not locked, then if the file is read-only we
775 ;; assume that locking is used for the file, otherwise
776 ;; locking is not used.
777 (not (vc-mistrust-permissions file))
778 (vc-up-to-date-p file)
779 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
780 (vc-file-setprop file 'vc-checkout-model 'locking)
781 (vc-file-setprop file 'vc-checkout-model 'implicit)))
782 status))))
783
784 (defun vc-release-greater-or-equal (r1 r2)
785 "Compare release numbers, represented as strings.
786 Release components are assumed cardinal numbers, not decimal fractions
787 \(5.10 is a higher release than 5.9\). Omitted fields are considered
788 lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
789 of the string is found, or a non-numeric component shows up \(5.6.7 is
790 earlier than \"5.6.7 beta\", which is probably not what you want in
791 some cases\). This code is suitable for existing RCS release numbers.
792 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
793 (let (v1 v2 i1 i2)
794 (catch 'done
795 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
796 (setq i1 (match-end 0))
797 (setq v1 (string-to-number (match-string 1 r1)))
798 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
799 (setq i2 (match-end 0))
800 (setq v2 (string-to-number (match-string 1 r2)))
801 (if (> v1 v2) (throw 'done t)
802 (if (< v1 v2) (throw 'done nil)
803 (throw 'done
804 (vc-release-greater-or-equal
805 (substring r1 i1)
806 (substring r2 i2)))))))
807 (throw 'done t)))
808 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
809 (throw 'done nil))
810 (throw 'done t)))))
811
812 (defun vc-rcs-release-p (release)
813 "Return t if we have RELEASE or better."
814 (let ((installation (vc-rcs-system-release)))
815 (if (and installation
816 (not (eq installation 'unknown)))
817 (vc-release-greater-or-equal installation release))))
818
819
820 (defun vc-rcs-system-release ()
821 "Return the RCS release installed on this system, as a string.
822 Return symbol UNKNOWN if the release cannot be deducted. The user can
823 override this using variable `vc-rcs-release'.
824
825 If the user has not set variable `vc-rcs-release' and it is nil,
826 variable `vc-rcs-release' is set to the returned value."
827 (or vc-rcs-release
828 (setq vc-rcs-release
829 (or (and (zerop (vc-do-command nil nil "rcs" nil "-V"))
830 (with-current-buffer (get-buffer "*vc*")
831 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
832 'unknown))))
833
834 (defun vc-rcs-set-non-strict-locking (file)
835 (vc-do-command nil 0 "rcs" file "-U")
836 (vc-file-setprop file 'vc-checkout-model 'implicit)
837 (set-file-modes file (logior (file-modes file) 128)))
838
839 (defun vc-rcs-set-default-branch (file branch)
840 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-b" branch))
841 (vc-file-setprop file 'vc-rcs-default-branch branch))
842
843 (provide 'vc-rcs)
844
845 ;;; vc-rcs.el ends here