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