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