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