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