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