]> code.delx.au - gnu-emacs/blob - lisp/vc-rcs.el
(uniquify-get-proposed-name): Don't use directory-sep-char.
[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.28 2002/10/08 15:33:18 monnier 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-find-version (file rev buffer)
359 (apply 'vc-do-command
360 buffer 0 "co" (vc-name file)
361 "-q" ;; suppress diagnostic output
362 (concat "-p" rev)
363 (if (stringp vc-checkout-switches)
364 (list vc-checkout-switches)
365 vc-checkout-switches)))
366
367 (defun vc-rcs-checkout (file &optional editable rev)
368 "Retrieve a copy of a saved version of FILE."
369 (let ((file-buffer (get-file-buffer file))
370 switches)
371 (message "Checking out %s..." file)
372 (save-excursion
373 ;; Change buffers to get local value of vc-checkout-switches.
374 (if file-buffer (set-buffer file-buffer))
375 (setq switches (if (stringp vc-checkout-switches)
376 (list vc-checkout-switches)
377 vc-checkout-switches))
378 ;; Save this buffer's default-directory
379 ;; and use save-excursion to make sure it is restored
380 ;; in the same buffer it was saved in.
381 (let ((default-directory default-directory))
382 (save-excursion
383 ;; Adjust the default-directory so that the check-out creates
384 ;; the file in the right place.
385 (setq default-directory (file-name-directory file))
386 (let (new-version)
387 ;; if we should go to the head of the trunk,
388 ;; clear the default branch first
389 (and rev (string= rev "")
390 (vc-rcs-set-default-branch file nil))
391 ;; now do the checkout
392 (apply 'vc-do-command
393 nil 0 "co" (vc-name file)
394 ;; If locking is not strict, force to overwrite
395 ;; the writable workfile.
396 (if (eq (vc-checkout-model file) 'implicit) "-f")
397 (if editable "-l")
398 (if rev (concat "-r" rev)
399 ;; if no explicit revision was specified,
400 ;; check out that of the working file
401 (let ((workrev (vc-workfile-version file)))
402 (if workrev (concat "-r" workrev)
403 nil)))
404 switches)
405 ;; determine the new workfile version
406 (with-current-buffer "*vc*"
407 (setq new-version
408 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
409 (vc-file-setprop file 'vc-workfile-version new-version)
410 ;; if necessary, adjust the default branch
411 (and rev (not (string= rev ""))
412 (vc-rcs-set-default-branch
413 file
414 (if (vc-rcs-latest-on-branch-p file new-version)
415 (if (vc-trunk-p new-version) nil
416 (vc-branch-part new-version))
417 new-version)))))
418 (message "Checking out %s...done" file)))))
419
420 (defun vc-rcs-revert (file &optional contents-done)
421 "Revert FILE to the version it was based on."
422 (vc-do-command nil 0 "co" (vc-name file) "-f"
423 (concat "-u" (vc-workfile-version file))))
424
425 (defun vc-rcs-cancel-version (file editable)
426 "Undo the most recent checkin of FILE.
427 EDITABLE non-nil means previous version should be locked."
428 (let* ((target (vc-workfile-version file))
429 (previous (if (vc-trunk-p target) "" (vc-branch-part target)))
430 (config (current-window-configuration))
431 (done nil))
432 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-o" target))
433 ;; Check out the most recent remaining version. If it fails, because
434 ;; the whole branch got deleted, do a double-take and check out the
435 ;; version where the branch started.
436 (while (not done)
437 (condition-case err
438 (progn
439 (vc-do-command nil 0 "co" (vc-name file) "-f"
440 (concat (if editable "-l" "-u") previous))
441 (setq done t))
442 (error (set-buffer "*vc*")
443 (goto-char (point-min))
444 (if (search-forward "no side branches present for" nil t)
445 (progn (setq previous (vc-branch-part previous))
446 (vc-rcs-set-default-branch file previous)
447 ;; vc-do-command popped up a window with
448 ;; the error message. Get rid of it, by
449 ;; restoring the old window configuration.
450 (set-window-configuration config))
451 ;; No, it was some other error: re-signal it.
452 (signal (car err) (cdr err))))))))
453
454 (defun vc-rcs-merge (file first-version &optional second-version)
455 "Merge changes into current working copy of FILE.
456 The changes are between FIRST-VERSION and SECOND-VERSION."
457 (vc-do-command nil 1 "rcsmerge" (vc-name file)
458 "-kk" ; ignore keyword conflicts
459 (concat "-r" first-version)
460 (if second-version (concat "-r" second-version))))
461
462 (defun vc-rcs-steal-lock (file &optional rev)
463 "Steal the lock on the current workfile for FILE and revision REV.
464 Needs RCS 5.6.2 or later for -M."
465 (vc-do-command nil 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
466 ;; Do a real checkout after stealing the lock, so that we see
467 ;; expanded headers.
468 (vc-do-command nil 0 "co" (vc-name file) "-f" (concat "-l" rev)))
469
470
471 \f
472 ;;;
473 ;;; History functions
474 ;;;
475
476 (defun vc-rcs-print-log (file)
477 "Get change log associated with FILE."
478 (vc-do-command nil 0 "rlog" (vc-name file)))
479
480 (defun vc-rcs-diff (file &optional oldvers newvers)
481 "Get a difference report using RCS between two versions of FILE."
482 (if (not oldvers) (setq oldvers (vc-workfile-version file)))
483 (apply 'vc-do-command "*vc-diff*" 1 "rcsdiff" file
484 (append (list "-q"
485 (concat "-r" oldvers)
486 (and newvers (concat "-r" newvers)))
487 (vc-diff-switches-list 'RCS))))
488
489 \f
490 ;;;
491 ;;; Snapshot system
492 ;;;
493
494 (defun vc-rcs-assign-name (file name)
495 "Assign to FILE's latest version a given NAME."
496 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-n" name ":")))
497
498 \f
499 ;;;
500 ;;; Miscellaneous
501 ;;;
502
503 (defun vc-rcs-check-headers ()
504 "Check if the current file has any headers in it."
505 (save-excursion
506 (goto-char (point-min))
507 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
508 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
509
510 (defun vc-rcs-clear-headers ()
511 "Implementation of vc-clear-headers for RCS."
512 (let ((case-fold-search nil))
513 (goto-char (point-min))
514 (while (re-search-forward
515 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
516 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
517 nil t)
518 (replace-match "$\\1$"))))
519
520 (defun vc-rcs-rename-file (old new)
521 ;; Just move the master file (using vc-rcs-master-templates).
522 (vc-rename-master (vc-name old) new vc-rcs-master-templates))
523
524 \f
525 ;;;
526 ;;; Internal functions
527 ;;;
528
529 (defun vc-rcs-workfile-is-newer (file)
530 "Return non-nil if FILE is newer than its RCS master.
531 This likely means that FILE has been changed with respect
532 to its master version."
533 (let ((file-time (nth 5 (file-attributes file)))
534 (master-time (nth 5 (file-attributes (vc-name file)))))
535 (or (> (nth 0 file-time) (nth 0 master-time))
536 (and (= (nth 0 file-time) (nth 0 master-time))
537 (> (nth 1 file-time) (nth 1 master-time))))))
538
539 (defun vc-rcs-find-most-recent-rev (branch)
540 "Find most recent revision on BRANCH."
541 (goto-char (point-min))
542 (let ((latest-rev -1) value)
543 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
544 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
545 nil t)
546 (let ((rev (string-to-number (match-string 2))))
547 (when (< latest-rev rev)
548 (setq latest-rev rev)
549 (setq value (match-string 1)))))
550 (or value
551 (vc-branch-part branch))))
552
553 (defun vc-rcs-fetch-master-state (file &optional workfile-version)
554 "Compute the master file's idea of the state of FILE.
555 If a WORKFILE-VERSION is given, compute the state of that version,
556 otherwise determine the workfile version based on the master file.
557 This function sets the properties `vc-workfile-version' and
558 `vc-checkout-model' to their correct values, based on the master
559 file."
560 (with-temp-buffer
561 (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
562 (progn (goto-char (point-min))
563 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
564 (error "File %s is not an RCS master file" (vc-name file)))
565 (let ((workfile-is-latest nil)
566 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
567 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
568 (unless workfile-version
569 ;; Workfile version not known yet. Determine that first. It
570 ;; is either the head of the trunk, the head of the default
571 ;; branch, or the "default branch" itself, if that is a full
572 ;; revision number.
573 (cond
574 ;; no default branch
575 ((or (not default-branch) (string= "" default-branch))
576 (setq workfile-version
577 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
578 (setq workfile-is-latest t))
579 ;; default branch is actually a revision
580 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
581 default-branch)
582 (setq workfile-version default-branch))
583 ;; else, search for the head of the default branch
584 (t (vc-insert-file (vc-name file) "^desc")
585 (setq workfile-version
586 (vc-rcs-find-most-recent-rev default-branch))
587 (setq workfile-is-latest t)))
588 (vc-file-setprop file 'vc-workfile-version workfile-version))
589 ;; Check strict locking
590 (goto-char (point-min))
591 (vc-file-setprop file 'vc-checkout-model
592 (if (re-search-forward ";[ \t\n]*strict;" nil t)
593 'locking 'implicit))
594 ;; Compute state of workfile version
595 (goto-char (point-min))
596 (let ((locking-user
597 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
598 (regexp-quote workfile-version)
599 "[^0-9.]")
600 1)))
601 (cond
602 ;; not locked
603 ((not locking-user)
604 (if (or workfile-is-latest
605 (vc-rcs-latest-on-branch-p file workfile-version))
606 ;; workfile version is latest on branch
607 'up-to-date
608 ;; workfile version is not latest on branch
609 'needs-patch))
610 ;; locked by the calling user
611 ((and (stringp locking-user)
612 (string= locking-user (vc-user-login-name)))
613 (if (or (eq (vc-checkout-model file) 'locking)
614 workfile-is-latest
615 (vc-rcs-latest-on-branch-p file workfile-version))
616 'edited
617 ;; Locking is not used for the file, but the owner does
618 ;; have a lock, and there is a higher version on the current
619 ;; branch. Not sure if this can occur, and if it is right
620 ;; to use `needs-merge' in this case.
621 'needs-merge))
622 ;; locked by somebody else
623 ((stringp locking-user)
624 locking-user)
625 (t
626 (error "Error getting state of RCS file")))))))
627
628 (defun vc-rcs-consult-headers (file)
629 "Search for RCS headers in FILE, and set properties accordingly.
630
631 Returns: nil if no headers were found
632 'rev if a workfile revision was found
633 'rev-and-lock if revision and lock info was found"
634 (cond
635 ((not (get-file-buffer file)) nil)
636 ((let (status version locking-user)
637 (save-excursion
638 (set-buffer (get-file-buffer file))
639 (goto-char (point-min))
640 (cond
641 ;; search for $Id or $Header
642 ;; -------------------------
643 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
644 ((or (and (search-forward "$Id\ : " nil t)
645 (looking-at "[^ ]+ \\([0-9.]+\\) "))
646 (and (progn (goto-char (point-min))
647 (search-forward "$Header\ : " nil t))
648 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
649 (goto-char (match-end 0))
650 ;; if found, store the revision number ...
651 (setq version (match-string-no-properties 1))
652 ;; ... and check for the locking state
653 (cond
654 ((looking-at
655 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
656 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
657 "[^ ]+ [^ ]+ ")) ; author & state
658 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
659 (cond
660 ;; unlocked revision
661 ((looking-at "\\$")
662 (setq locking-user 'none)
663 (setq status 'rev-and-lock))
664 ;; revision is locked by some user
665 ((looking-at "\\([^ ]+\\) \\$")
666 (setq locking-user (match-string-no-properties 1))
667 (setq status 'rev-and-lock))
668 ;; everything else: false
669 (nil)))
670 ;; unexpected information in
671 ;; keyword string --> quit
672 (nil)))
673 ;; search for $Revision
674 ;; --------------------
675 ((re-search-forward (concat "\\$"
676 "Revision: \\([0-9.]+\\) \\$")
677 nil t)
678 ;; if found, store the revision number ...
679 (setq version (match-string-no-properties 1))
680 ;; and see if there's any lock information
681 (goto-char (point-min))
682 (if (re-search-forward (concat "\\$" "Locker:") nil t)
683 (cond ((looking-at " \\([^ ]+\\) \\$")
684 (setq locking-user (match-string-no-properties 1))
685 (setq status 'rev-and-lock))
686 ((looking-at " *\\$")
687 (setq locking-user 'none)
688 (setq status 'rev-and-lock))
689 (t
690 (setq locking-user 'none)
691 (setq status 'rev-and-lock)))
692 (setq status 'rev)))
693 ;; else: nothing found
694 ;; -------------------
695 (t nil)))
696 (if status (vc-file-setprop file 'vc-workfile-version version))
697 (and (eq status 'rev-and-lock)
698 (vc-file-setprop file 'vc-state
699 (cond
700 ((eq locking-user 'none) 'up-to-date)
701 ((string= locking-user (vc-user-login-name)) 'edited)
702 (t locking-user)))
703 ;; If the file has headers, we don't want to query the
704 ;; master file, because that would eliminate all the
705 ;; performance gain the headers brought us. We therefore
706 ;; use a heuristic now to find out whether locking is used
707 ;; for this file. If we trust the file permissions, and the
708 ;; file is not locked, then if the file is read-only we
709 ;; assume that locking is used for the file, otherwise
710 ;; locking is not used.
711 (not (vc-mistrust-permissions file))
712 (vc-up-to-date-p file)
713 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
714 (vc-file-setprop file 'vc-checkout-model 'locking)
715 (vc-file-setprop file 'vc-checkout-model 'implicit)))
716 status))))
717
718 (defun vc-release-greater-or-equal (r1 r2)
719 "Compare release numbers, represented as strings.
720 Release components are assumed cardinal numbers, not decimal fractions
721 \(5.10 is a higher release than 5.9\). Omitted fields are considered
722 lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
723 of the string is found, or a non-numeric component shows up \(5.6.7 is
724 earlier than \"5.6.7 beta\", which is probably not what you want in
725 some cases\). This code is suitable for existing RCS release numbers.
726 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
727 (let (v1 v2 i1 i2)
728 (catch 'done
729 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
730 (setq i1 (match-end 0))
731 (setq v1 (string-to-number (match-string 1 r1)))
732 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
733 (setq i2 (match-end 0))
734 (setq v2 (string-to-number (match-string 1 r2)))
735 (if (> v1 v2) (throw 'done t)
736 (if (< v1 v2) (throw 'done nil)
737 (throw 'done
738 (vc-release-greater-or-equal
739 (substring r1 i1)
740 (substring r2 i2)))))))
741 (throw 'done t)))
742 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
743 (throw 'done nil))
744 (throw 'done t)))))
745
746 (defun vc-rcs-release-p (release)
747 "Return t if we have RELEASE or better."
748 (let ((installation (vc-rcs-system-release)))
749 (if (and installation
750 (not (eq installation 'unknown)))
751 (vc-release-greater-or-equal installation release))))
752
753
754 (defun vc-rcs-system-release ()
755 "Return the RCS release installed on this system, as a string.
756 Return symbol UNKNOWN if the release cannot be deducted. The user can
757 override this using variable `vc-rcs-release'.
758
759 If the user has not set variable `vc-rcs-release' and it is nil,
760 variable `vc-rcs-release' is set to the returned value."
761 (or vc-rcs-release
762 (setq vc-rcs-release
763 (or (and (zerop (vc-do-command nil nil "rcs" nil "-V"))
764 (with-current-buffer (get-buffer "*vc*")
765 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
766 'unknown))))
767
768 (defun vc-rcs-set-non-strict-locking (file)
769 (vc-do-command nil 0 "rcs" file "-U")
770 (vc-file-setprop file 'vc-checkout-model 'implicit)
771 (set-file-modes file (logior (file-modes file) 128)))
772
773 (defun vc-rcs-set-default-branch (file branch)
774 (vc-do-command nil 0 "rcs" (vc-name file) (concat "-b" branch))
775 (vc-file-setprop file 'vc-rcs-default-branch branch))
776
777 (provide 'vc-rcs)
778
779 ;;; vc-rcs.el ends here