1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; $Id: vc.el,v 1.22 1993/03/07 18:20:54 eggert Exp eric $
10 ;; This file is part of GNU Emacs.
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)
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.
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
29 ;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
30 ;; and Richard Stallman contributed valuable criticism, support, and testing.
32 ;; Supported version-control systems presently include SCCS and RCS;
33 ;; your RCS version should be 5.6.2 or later for proper operation of
34 ;; the lock-breaking code.
36 ;; The RCS code assumes strict locking. You can support the RCS -x option
37 ;; by adding pairs to the vc-master-templates list.
39 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
40 ;; to be installed somewhere on Emacs's path for executables.
42 ;; This code depends on call-process passing back the subprocess exit
43 ;; status. Thus, you need Emacs 18.58 or later to run it.
45 ;; The vc code maintains some internal state in order to reduce expensive
46 ;; version-control operations to a minimum. Some names are only computed
47 ;; once. If you perform version control operations with RCS/SCCS/CVS while
48 ;; vc's back is turned, or move/rename master files while vc is running,
49 ;; vc may get seriously confused. Don't do these things!
51 ;; Developer's notes on some concurrency issues are included at the end of
58 ;; General customization
60 (defvar vc-default-back-end nil
61 "*Back-end actually used by this interface; may be SCCS or RCS.
62 The value is only computed when needed to avoid an expensive search.")
63 (defvar vc-diff-options
'("-a" "-c1")
64 "*The command/flags list to be used in constructing diff commands.")
65 (defvar vc-suppress-confirm nil
66 "*If non-nil, reat user as expert; suppress yes-no prompts on some things.")
67 (defvar vc-keep-workfiles t
68 "*If non-nil, don't delete working files after registering changes.")
69 (defvar vc-initial-comment nil
70 "*Prompt for initial comment when a file is registered.")
71 (defvar vc-command-messages nil
72 "*Display run messages from back-end commands.")
73 (defvar vc-mistrust-permissions
'file-symlink-p
74 "*Don't assume that permissions and ownership track version-control status.")
76 (defvar vc-checkin-switches nil
77 "*Extra switches passed to the checkin program by \\[vc-checkin].")
80 (defvar vc-checkin-hook nil
81 "*List of functions called after a vc-checkin is done. See `run-hooks'.")
83 ;; Header-insertion hair
85 (defvar vc-header-alist
86 '((SCCS "\%W\%") (RCS "\$Id\$"))
87 "*Header keywords to be inserted when vc-insert-header is executed.")
88 (defconst vc-static-header-alist
90 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
91 "*Associate static header string templates with file types. A \%s in the
92 template is replaced with the first string associated with the file's
93 verson-control type in vc-header-strings.")
94 (defvar vc-comment-alist
95 '((nroff-mode ".\\\"" ""))
96 "*Special comment delimiters to be used in generating vc headers only.
97 Add an entry in this list if you need to override the normal comment-start
98 and comment-end variables. This will only be necessary if the mode language
99 is sensitive to blank lines.")
101 ;; Variables the user doesn't need to know about.
102 (defvar vc-log-entry-mode nil
)
103 (defvar vc-log-operation nil
)
104 (defvar vc-log-after-operation-hook nil
)
105 (defvar vc-checkout-writeable-buffer-hook
'vc-checkout-writeable-buffer
)
108 (defvar vc-log-version
)
110 (defconst vc-name-assoc-file
"VC-names")
112 (defmacro vc-error-occurred
(&rest body
)
113 (list 'condition-case nil
(cons 'progn
(append body
'(nil))) '(error t
)))
115 ;; File property caching
117 (defun vc-file-clearprops (file)
118 ;; clear all properties of a given file
119 (setplist (intern file vc-file-prop-obarray
) nil
))
121 ;; Random helper functions
123 (defun vc-name (file)
124 "Return the master name of a file, nil if it is not registered"
125 (or (vc-file-getprop file
'vc-name
)
126 (vc-file-setprop file
'vc-name
127 (let ((name-and-type (vc-registered file
)))
128 (and name-and-type
(car name-and-type
))))))
130 (defvar vc-binary-assoc nil
)
132 (defun vc-find-binary (name)
133 "Look for a command anywhere on the subprocess-command search path."
134 (or (cdr (assoc name vc-binary-assoc
))
138 (function (lambda (s)
139 (if (and s
(file-exists-p (setq full
(concat s
"/" name
))))
140 (throw 'found nil
))))
143 (setq vc-binary-assoc
(cons (cons name full
) vc-binary-assoc
)))
146 (defun vc-do-command (okstatus command file
&rest flags
)
147 "Execute a version-control command, notifying user and checking for errors.
148 The command is successful if its exit status does not exceed OKSTATUS.
149 Output from COMMAND goes to buffer *vc*. The last argument of the command is
150 the master name of FILE; this is appended to an optional list of FLAGS."
151 (setq file
(expand-file-name file
))
152 (if vc-command-messages
153 (message "Running %s on %s..." command file
))
154 (let ((obuf (current-buffer))
156 (vc-file (and file
(vc-name file
)))
158 (set-buffer (get-buffer-create "*vc*"))
161 ;; This is so that command arguments typed in the *vc* buffer will
162 ;; have reasonable defaults.
163 (setq default-directory
(file-name-directory file
))
166 (function (lambda (s) (and s
(setq squeezed
(append squeezed
(list s
))))))
169 (setq squeezed
(append squeezed
(list vc-file
))))
171 ((default-directory (file-name-directory (or file
"./"))))
172 (setq status
(apply 'call-process command nil t nil squeezed
))
174 (goto-char (point-max))
176 (if (or (not (integerp status
)) (< okstatus status
))
179 (print (cons command squeezed
))
181 (pop-to-buffer "*vc*")
183 (goto-char (point-min))
184 (error "Running %s...FAILED (%s)" command
185 (if (integerp status
)
186 (format "status %d" status
)
189 (if vc-command-messages
190 (message "Running %s...OK" command
))
196 ;;; Save a bit of the text around POSN in the current buffer, to help
197 ;;; us find the corresponding position again later. This works even
198 ;;; if all markers are destroyed or corrupted.
199 (defun vc-position-context (posn)
202 (buffer-substring posn
203 (min (point-max) (+ posn
100)))))
205 ;;; Return the position of CONTEXT in the current buffer, or nil if we
206 ;;; couldn't find it.
207 (defun vc-find-position-by-context (context)
208 (let ((context-string (nth 2 context
)))
209 (if (equal "" context-string
)
212 (let ((diff (- (nth 1 context
) (buffer-size))))
213 (if (< diff
0) (setq diff
(- diff
)))
214 (goto-char (nth 0 context
))
215 (if (or (search-forward context-string nil t
)
216 ;; Can't use search-backward since the match may continue
218 (progn (goto-char (- (point) diff
(length context-string
)))
219 ;; goto-char doesn't signal an error at
220 ;; beginning of buffer like backward-char would
221 (search-forward context-string nil t
)))
222 ;; to beginning of OSTRING
223 (- (point) (length context-string
))))))))
225 (defun vc-revert-buffer1 (&optional arg no-confirm
)
226 ;; This code was shamelessly lifted from Sebastian Kremer's rcs.el mode.
227 ;; Revert buffer, try to keep point and mark where user expects them in spite
228 ;; of changes because of expanded version-control key words.
229 ;; This is quite important since otherwise typeahead won't work as expected.
232 (let ((point-context (vc-position-context (point)))
233 ;; Use mark-marker to avoid confusion in transient-mark-mode.
234 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
235 (vc-position-context (mark-marker))))
236 ;; Make the right thing happen in transient-mark-mode.
238 (revert-buffer arg no-confirm
)
239 (let ((new-point (vc-find-position-by-context point-context
)))
240 (if new-point
(goto-char new-point
)))
242 (let ((new-mark (vc-find-position-by-context mark-context
)))
243 (if new-mark
(set-mark new-mark
))))))
246 (defun vc-buffer-sync ()
247 ;; Make sure the current buffer and its working file are in sync
248 (if (and (buffer-modified-p)
251 (y-or-n-p (format "%s has been modified. Write it out? "
255 (defun vc-workfile-unchanged-p (file)
256 ;; Has the given workfile changed since last checkout?
257 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
258 (lastmod (nth 5 (file-attributes file
))))
260 (equal lastmod checkout-time
)
261 (if (zerop (vc-backend-diff file nil
))
263 (vc-file-setprop file
'vc-checkout-time lastmod
)
266 (vc-file-setprop file
'vc-checkout-time
'(0 .
0))
271 ;; Here's the major entry point.
274 (defun vc-next-action (verbose)
275 "Do the next logical checkin or checkout operation on the current file.
276 If the file is not already registered, this registers it for version
277 control and then retrieves a writeable, locked copy for editing.
278 If the file is registered and not locked by anyone, this checks out
279 a writeable and locked file ready for editing.
280 If the file is checked out and locked by the calling user, this
281 first checks to see if the file has changed since checkout. If not,
282 it performs a revert.
283 If the file has been changed, this pops up a buffer for creation of
284 a log message; when the message has been entered, it checks in the
285 resulting changes along with the log message as change commentary. If
286 the variable vc-keep-workfiles is non-nil (which is its default), a
287 read-only copy of the changed file is left in place afterwards.
288 If the file is registered and locked by someone else, you are given
289 the option to steal the lock."
293 (do-update owner version
294 (file buffer-file-name
)
295 (vc-file (vc-name buffer-file-name
))
301 ;; if there is no master file corresponding, create one
303 (vc-register verbose
)
304 (if vc-initial-comment
305 (setq vc-log-after-operation-hook
306 'vc-checkout-writeable-buffer-hook
)
307 (vc-checkout-writeable-buffer)))
309 ;; if there is no lock on the file, assert one and get it
310 ((not (setq owner
(vc-locking-user file
)))
311 (vc-checkout-writeable-buffer))
313 ;; a checked-out version exists, but the user may not own the lock
314 ((not (string-equal owner
(user-login-name)))
317 (and verbose
(read-string "Version to steal: "))
320 ;; OK, user owns the lock on the file
323 ;; give luser a chance to save before checking in.
326 ;; Revert if file is unchanged and buffer is too.
327 ;; If buffer is modified, that means the user just said no
328 ;; to saving it; in that case, don't revert,
329 ;; because the user might intend to save
330 ;; after finishing the log entry.
331 (if (and (vc-workfile-unchanged-p file
)
332 (not (buffer-modified-p)))
334 (vc-backend-revert file
)
335 ;; DO NOT revert the file without asking the user!
336 (vc-resynch-window file t nil
))
338 ;; user may want to set nonstandard parameters
340 (setq version
(read-string "New version level: ")))
342 ;; OK, let's do the checkin
343 (vc-checkin file version
))))))
344 (error "There is no file associated with buffer %s" (buffer-name))))
346 ;;; These functions help the vc-next-action entry point
348 (defun vc-checkout-writeable-buffer ()
349 "Retrieve a writeable copy of the latest version of the current buffer's file."
350 (vc-checkout buffer-file-name t
)
354 (defun vc-register (&optional override
)
355 "Register the current file into your version-control system."
357 (if (vc-name buffer-file-name
)
358 (error "This file is already registered."))
359 ;; Watch out for new buffers of size 0: the corresponding file
360 ;; does not exist yet, even though buffer-modified-p is nil.
361 (if (and (not (buffer-modified-p))
362 (zerop (buffer-size))
363 (not (file-exists-p buffer-file-name
)))
364 (set-buffer-modified-p t
))
368 (and override
(read-string "Initial version level: ")))
371 (defun vc-resynch-window (file &optional keep noquery
)
372 ;; If the given file is in the current buffer,
373 ;; either revert on it so we see expanded keyworks,
374 ;; or unvisit it (depending on vc-keep-workfiles)
375 ;; NOQUERY if non-nil inhibits confirmation for reverting.
376 ;; NOQUERY should be t *only* if it is known the only difference
377 ;; between the buffer and the file is due to RCS rather than user editing!
378 (and (string= buffer-file-name file
)
381 (vc-revert-buffer1 t noquery
)
382 (vc-mode-line buffer-file-name
))
385 (kill-buffer (current-buffer))))))
388 (defun vc-admin (file rev
)
389 "Check a file into your version-control system.
390 FILE is the unmodified name of the file. REV should be the base version
391 level to check it in under."
392 (if vc-initial-comment
394 (pop-to-buffer (get-buffer-create "*VC-log*"))
396 (narrow-to-region (point-max) (point-max))
397 (vc-mode-line file
(file-name-nondirectory file
))
398 (setq vc-log-operation
'vc-backend-admin
)
399 (setq vc-log-file file
)
400 (setq vc-log-version rev
)
401 (message "Enter initial comment. Type C-c C-c when done."))
403 (vc-backend-admin file rev
)
404 ;; Inhibit query here, since otherwise we always get asked.
405 (vc-resynch-window file vc-keep-workfiles t
))))
407 (defun vc-steal-lock (file rev
&optional owner
)
408 "Steal the lock on the current workfile."
411 (setq owner
(vc-locking-user file
)))
412 (if (not (y-or-n-p (format "Take the lock on %s:%s from %s?" file rev owner
)))
413 (error "Steal cancelled."))
415 (pop-to-buffer (get-buffer-create "*VC-mail*"))
416 (setq default-directory
(expand-file-name "~/"))
417 (auto-save-mode auto-save-default
)
420 (mail-sketup owner
(format "%s:%s" file rev
) nil nil nil
421 (list (list 'vc-finish-steal file rev
)))
422 (goto-char (point-max))
424 (format "I stole the lock on %s:%s, " file rev
)
425 (current-time-string)
427 (message "Please explain why you stole the lock. Type C-c C-c when done."))
429 ;; This is called when the notification has been sent.
430 (defun vc-finish-steal (file version
)
431 (vc-backend-steal file version
)
432 (vc-resynch-window file t t
))
434 (defun vc-checkout (file &optional writeable
)
435 "Retrieve a copy of the latest version of the given file."
436 (vc-backend-checkout file writeable
)
437 (if (string-equal file buffer-file-name
)
438 (vc-resynch-window file t t
))
441 (defun vc-checkin (file &optional rev comment
)
442 "Check in the file specified by FILE.
443 The optional argument REV may be a string specifying the new version level
444 \(if nil increment the current level). The file is either retained with write
445 permissions zeroed, or deleted (according to the value of vc-keep-workfiles).
446 COMMENT is a comment string; if omitted, a buffer is
447 popped up to accept a comment."
448 (pop-to-buffer (get-buffer-create "*VC-log*"))
450 (narrow-to-region (point-max) (point-max))
451 (vc-mode-line file
(file-name-nondirectory file
))
452 (setq vc-log-operation
'vc-backend-checkin
455 vc-log-after-operation-hook
'vc-checkin-hook
)
456 (message "Enter log message. Type C-c C-c when done.")
460 (vc-finish-logentry))))
462 (defun vc-finish-logentry ()
463 "Complete the operation implied by the current log entry."
465 (goto-char (point-max))
466 (if (not (bolp)) (newline))
467 ;; delimit current page
470 (goto-char (point-max))
471 (if (and (not (bobp)) (not (= (char-after (1- (point))) ?
\f)))
472 (insert-char ?
\f 1)))
477 (vc-backend-logentry-check vc-log-file
)
480 (funcall vc-log-operation
483 (buffer-substring (region-beginning) (1- (region-end))))
484 (error "No log operation is pending."))
485 ;; Return to "parent" buffer of this checkin and remove checkin window
486 (pop-to-buffer (get-file-buffer vc-log-file
))
487 (delete-window (get-buffer-window "*VC-log*"))
488 (bury-buffer "*VC-log*")
489 ;; Now make sure we see the expanded headers
490 (vc-resynch-window buffer-file-name vc-keep-workfiles t
)
491 (run-hooks vc-log-after-operation-hook
)
494 ;; Code for access to the comment ring
496 (defun vc-next-comment ()
497 "Fill the log buffer with the next message in the msg ring."
501 (if (= (point) (point-max))
502 (goto-char (point-min)))
506 (defun vc-previous-comment ()
507 "Fill the log buffer with the previous message in the msg ring."
510 (if (= (point) (point-min))
511 (goto-char (point-max)))
516 (defun vc-comment-search-backward (regexp)
517 "Fill the log buffer with the last message in the msg ring matching REGEXP."
518 (interactive "sSearch backward for: ")
520 (if (= (point) (point-min))
521 (goto-char (point-max)))
522 (re-search-backward regexp nil t
)
526 (defun vc-comment-search-forward (regexp)
527 "Fill the log buffer with the next message in the msg ring matching REGEXP."
528 (interactive "sSearch forward for: ")
530 (if (= (point) (point-min))
531 (goto-char (point-max)))
532 (re-search-forward regexp nil t
)
536 ;; Additional entry points for examining version histories
539 (defun vc-diff (historic)
540 "Display diffs between file versions."
543 (call-interactively 'vc-version-diff
)
544 (let ((file buffer-file-name
)
547 (setq unchanged
(vc-workfile-unchanged-p buffer-file-name
))
549 (message "No changes to %s since latest version." file
)
550 (pop-to-buffer "*vc*")
551 (vc-backend-diff file nil
)
552 (goto-char (point-min))
559 (defun vc-version-diff (file rel1 rel2
)
560 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
561 If FILE is a directory, generate diffs between versions for all registered
562 files in or below it."
563 (interactive "FFile or directory to diff: \nsOlder version: \nsNewer version: ")
564 (if (string-equal rel1
"") (setq rel1 nil
))
565 (if (string-equal rel2
"") (setq rel2 nil
))
566 (if (file-directory-p file
)
568 (set-buffer (get-buffer-create "*vc-status*"))
570 (insert "Diffs between "
571 (or rel1
"last version checked in")
573 (or rel2
"current workfile(s)")
575 (set-buffer (get-buffer-create "*vc*"))
577 (function (lambda (f)
578 (message (format "Looking at %s" f
))
580 (not (file-directory-p f
))
582 (vc-backend-diff f rel1 rel2
)
583 (append-to-buffer "*vc-status*" (point-min) (point-max)))
585 (pop-to-buffer "*vc-status*")
586 (insert "\nEnd of diffs.\n")
587 (goto-char (point-min))
588 (set-buffer-modified-p nil
)
591 (vc-backend-diff file rel1 rel2
)
592 (goto-char (point-min))
593 (if (equal (point-min) (point-max))
594 (message "No changes to %s between %s and %s." file rel1 rel2
)
595 (pop-to-buffer "*vc*")
596 (goto-char (point-min))
602 ;; Header-insertion code
605 (defun vc-insert-headers ()
606 "Insert headers in a file for use with your version-control system.
607 Headers desired are inserted at the start of the buffer, and are pulled from
608 the variable vc-header-strings"
613 (if (or (not (vc-check-headers))
614 (y-or-n-p "Version headers already exist. Insert another set?"))
616 (let* ((delims (cdr (assq major-mode vc-comment-alist
)))
617 (comment-start-vc (or (car delims
) comment-start
"#"))
618 (comment-end-vc (or (car (cdr delims
)) comment-end
""))
619 (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist
))))
620 (mapcar (function (lambda (s)
621 (insert comment-start-vc
"\t" s
"\t"
622 comment-end-vc
"\n")))
624 (if vc-static-header-alist
625 (mapcar (function (lambda (f)
626 (if (string-match (car f
) buffer-file-name
)
627 (insert (format (cdr f
) (car hdstrings
))))))
628 vc-static-header-alist
))
632 ;; Status-checking functions
635 (defun vc-directory (verbose)
636 "Show version-control status of all files under the current directory."
640 (set-buffer (get-buffer-create "*vc-status*"))
643 (function (lambda (f)
644 (if (vc-registered f
)
645 (let ((user (vc-locking-user f
)))
646 (if (or user verbose
)
649 (concat user
) f
))))))))
650 (setq nonempty
(not (zerop (buffer-size)))))
653 (pop-to-buffer "*vc-status*" t
)
655 (goto-char (point-min)))
656 (message "No files are currently %s under %s"
657 (if verbose
"registered" "locked") default-directory
))
660 ;; Named-configuration support for SCCS
662 (defun vc-add-triple (name file rev
)
664 (find-file (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
))
665 (goto-char (point-max))
666 (insert name
"\t:\t" file
"\t" rev
"\n")
668 (kill-buffer (current-buffer))
671 (defun vc-record-rename (file newname
)
673 (find-file (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
))
674 (goto-char (point-min))
675 (replace-regexp (concat ":" (regexp-quote file
) "$") (concat ":" newname
))
677 (kill-buffer (current-buffer))
680 (defun vc-lookup-triple (file name
)
683 (let ((firstchar (aref name
0)))
684 (and (>= firstchar ?
0) (<= firstchar ?
9) name
))
686 (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
)
687 (list (concat name
"\t:\t" file
"\t\\(.+\\)"))))
690 ;; Named-configuration entry points
692 (defun vc-quiescent-p ()
693 ;; Is the current directory ready to be snapshot?
696 (function (lambda (f)
697 (if (and (vc-registered f
) (vc-locking-user f
))
698 (throw 'quiet nil
)))))
702 (defun vc-create-snapshot (name)
703 "Make a snapshot called NAME.
704 The snapshot is made from all registered files at or below the current
705 directory. For each file, the version level of its latest
706 version becomes part of the named configuration."
707 (interactive "sNew snapshot name: ")
708 (if (not (vc-quiescent-p))
709 (error "Can't make a snapshot, locked files are in the way.")
711 (function (lambda (f) (and
713 (vc-backend-assign-name f name
)))))
717 (defun vc-retrieve-snapshot (name)
718 "Retrieve the snapshot called NAME.
719 This function fails if any files are locked at or below the current directory
720 Otherwise, all registered files are checked out (unlocked) at their version
721 levels in the snapshot."
722 (interactive "sSnapshot name to retrieve: ")
723 (if (not (vc-quiescent-p))
724 (error "Can't retrieve a snapshot, locked files are in the way.")
726 (function (lambda (f) (and
728 (vc-error-occurred (vc-backend-checkout f nil name
))))))
731 ;; Miscellaneous other entry points
734 (defun vc-print-log ()
735 "List the change log of the current buffer in a window."
737 (if (and buffer-file-name
(vc-name buffer-file-name
))
739 (vc-backend-print-log buffer-file-name
)
740 (pop-to-buffer (get-buffer-create "*vc*"))
741 (goto-char (point-min))
743 (error "There is no version-control master associated with this buffer")
748 (defun vc-revert-buffer ()
749 "Revert the current buffer's file back to the latest checked-in version.
750 This asks for confirmation if the buffer contents are not identical
753 (let ((file buffer-file-name
)
754 (obuf (current-buffer)) (changed (vc-diff nil
)))
755 (if (and changed
(or vc-suppress-confirm
756 (not (yes-or-no-p "Discard changes? "))))
759 (error "Revert cancelled."))
763 (vc-backend-revert file
)
764 (vc-resynch-window file t t
)
769 (defun vc-cancel-version (norevert)
770 "Undo your latest checkin."
772 (let ((target (vc-your-latest-version (buffer-file-name))))
774 (error "You didn't check in the last change."))
775 (and (yes-or-no-p (format "Remove version %s from master? " target
))
776 (vc-backend-uncheck (buffer-file-name) target
)))
778 (vc-mode-line (buffer-file-name))
779 (vc-checkout (buffer-file-name) nil
))
782 (defun vc-rename-file (old new
)
783 "Rename a file, taking its master files with it."
784 (interactive "fOld name: \nFNew name: ")
785 (let ((oldbuf (get-file-buffer old
)))
786 (if (buffer-modified-p oldbuf
)
787 (error "Please save files before moving them."))
788 (if (get-file-buffer new
)
789 (error "Already editing new file name."))
790 (let ((oldmaster (vc-name old
)))
792 (if (vc-locking-user old
)
793 (error "Please check in files before moving them."))
794 (if (or (file-symlink-p oldmaster
)
795 ;; This had FILE, I changed it to OLD. -- rms.
796 (file-symlink-p (vc-backend-subdirectory-name old
)))
797 (error "This is not a safe thing to do in the presence of symbolic links."))
798 (rename-file oldmaster
(vc-name new
)))
799 (if (or (not oldmaster
) (file-exists-p old
))
800 (rename-file old new
)))
801 ; ?? Renaming a file might change its contents due to keyword expansion.
802 ; We should really check out a new copy if the old copy was precisely equal
803 ; to some checked in version. However, testing for this is tricky....
807 (set-visited-file-name new
)
808 (set-buffer-modified-p nil
))))
809 ;; This had FILE, I changed it to OLD. -- rms.
810 (vc-backend-dispatch old
811 (vc-record-rename old new
)
816 (defun vc-update-change-log (&rest args
)
817 "Find change log file and add entries from recent RCS logs.
818 The mark is left at the end of the text prepended to the change log.
819 With prefix arg of C-u, only find log entries for the current buffer's file.
820 With any numeric prefix arg, find log entries for all files currently visited.
821 From a program, any arguments are passed to the `rcs2log' script."
823 (cond ((consp current-prefix-arg
) ;C-u
824 (list buffer-file-name
))
825 (current-prefix-arg ;Numeric argument.
827 (buffers (buffer-list))
830 (setq file
(buffer-file-name (car buffers
)))
831 (and file
(vc-backend-deduce file
)
832 (setq files
(cons (file-relative-name file
) files
)))
833 (setq buffers
(cdr buffers
)))
835 (find-file-other-window "ChangeLog")
836 (barf-if-buffer-read-only)
839 (goto-char (point-min))
841 (message "Computing change log entries...")
842 (message "Computing change log entries... %s"
843 (if (eq 0 (apply 'call-process
"rcs2log" nil t nil args
))
846 ;; Functions for querying the master and lock files.
848 (defun match-substring (bn)
849 (buffer-substring (match-beginning bn
) (match-end bn
)))
851 (defun vc-parse-buffer (patterns &optional file properties
)
852 ;; Use PATTERNS to parse information out of the current buffer
853 ;; by matching each regular expression in the list and returning \\1.
854 ;; If a regexp has two tag brackets, assume the second is a date
855 ;; field and we want the most recent entry matching the template.
856 ;; If FILE and PROPERTIES are given, the latter must be a list of
857 ;; properties of the same length as PATTERNS; each property is assigned
858 ;; the corresponding value.
859 (mapcar (function (lambda (p)
860 (goto-char (point-min))
861 (if (string-match "\\\\(.*\\\\(" p
)
862 (let ((latest-date "") (latest-val))
863 (while (re-search-forward p nil t
)
864 (let ((date (match-substring 2)))
865 (if (string< latest-date date
)
867 (setq latest-date date
)
869 (match-substring 1))))))
872 (and (re-search-forward p nil t
)
873 (let ((value (match-substring 1)))
875 (vc-file-setprop file
(car properties
) value
))
877 (setq properties
(cdr properties
)))))
881 (defun vc-master-info (file fields
&optional rfile properties
)
882 ;; Search for information in a master file.
883 (if (and file
(file-exists-p file
))
886 (setq buf
(create-file-buffer file
))
889 (insert-file-contents file nil
)
890 (set-buffer-modified-p nil
)
893 (vc-parse-buffer fields rfile properties
)
894 (kill-buffer (current-buffer)))
898 (function (lambda (p) (vc-file-setprop rfile p nil
)))
903 (defun vc-log-info (command file patterns
&optional properties
)
904 ;; Search for information in log program output
905 (if (and file
(file-exists-p file
))
908 (setq buf
(get-buffer-create "*vc*"))
910 (apply 'vc-do-command
0 command file nil
)
911 (set-buffer-modified-p nil
)
913 (vc-parse-buffer patterns file properties
)
914 (kill-buffer (current-buffer))
919 (function (lambda (p) (vc-file-setprop file p nil
)))
924 (defun vc-locking-user (file)
925 "Return the name of the person currently holding a lock on FILE.
926 Return nil if there is no such person."
927 (if (or (not vc-keep-workfiles
)
928 (eq vc-mistrust-permissions
't
)
929 (and vc-mistrust-permissions
930 (funcall vc-mistrust-permissions
(vc-backend-subdirectory-name file
))))
931 (vc-true-locking-user file
)
932 ;; This implementation assumes that any file which is under version
933 ;; control and has -rw-r--r-- is locked by its owner. This is true
934 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
935 ;; We have to be careful not to exclude files with execute bits on;
936 ;; scripts can be under version control too. The advantage of this
937 ;; hack is that calls to the very expensive vc-fetch-properties
938 ;; function only have to be made if (a) the file is locked by someone
939 ;; other than the current user, or (b) some untoward manipulation
940 ;; behind vc's back has twiddled the `group' or `other' write bits.
941 (let ((attributes (file-attributes file
)))
942 (cond ((string-match ".r-.r-.r-." (nth 8 attributes
))
944 ((and (= (nth 2 attributes
) (user-uid))
945 (string-match ".rw.r-.r-." (nth 8 attributes
)))
948 (vc-true-locking-user file
))))))
950 (defun vc-true-locking-user (file)
951 ;; The slow but reliable version
952 (vc-fetch-properties file
)
953 (vc-file-getprop file
'vc-locking-user
))
955 (defun vc-latest-version (file)
956 ;; Return version level of the latest version of FILE
957 (vc-fetch-properties file
)
958 (vc-file-getprop file
'vc-latest-version
))
960 (defun vc-your-latest-version (file)
961 ;; Return version level of the latest version of FILE checked in by you
962 (vc-fetch-properties file
)
963 (vc-file-getprop file
'vc-your-latest-version
))
965 ;; Collect back-end-dependent stuff here
967 ;; Everything eventually funnels through these functions. To implement
968 ;; support for a new version-control system, add another branch to the
969 ;; vc-backend-dispatch macro (in vc-hooks.el) and fill it in in each call.
971 (defmacro vc-backend-dispatch
(f s r
)
972 "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
973 (list 'let
(list (list 'type
(list 'vc-backend-deduce f
)))
975 (list (list 'eq
'type
(quote 'SCCS
)) s
) ;; SCCS
976 (list (list 'eq
'type
(quote 'RCS
)) r
) ;; RCS
979 (defun vc-lock-file (file)
980 ;; Generate lock file name corresponding to FILE
981 (let ((master (vc-name file
)))
984 (string-match "\\(.*/\\)s\\.\\(.*\\)" master
)
986 (substring master
(match-beginning 1) (match-end 1))
988 (substring master
(match-beginning 2) (match-end 2))))))
991 (defun vc-fetch-properties (file)
992 ;; Re-fetch all properties associated with the given file.
993 ;; Currently these properties are:
997 ;; vc-your-latest-version
1002 (vc-master-info (vc-lock-file file
)
1004 "^[^ ]+ [^ ]+ \\([^ ]+\\)"
1007 '(vc-locking-user vc-locked-version
))
1008 (vc-master-info (vc-name file
)
1010 "^\001d D \\([^ ]+\\)"
1011 (concat "^\001d D \\([^ ]+\\) .* "
1012 (regexp-quote (user-login-name)) " ")
1015 '(vc-latest-version vc-your-latest-version
))
1018 (vc-log-info "rlog" file
1020 "^locks: strict\n\t\\([^:]+\\)"
1021 "^locks: strict\n\t[^:]+: \\(.+\\)"
1022 "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
1024 "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
1025 (regexp-quote (user-login-name))
1027 '(vc-locking-user vc-locked-version
1028 vc-latest-version vc-your-latest-version
))
1031 (defun vc-backend-subdirectory-name (&optional file
)
1032 ;; Where the master and lock files for the current directory are kept
1035 (and file
(vc-backend-deduce file
))
1037 (setq vc-default-back-end
(if (vc-find-binary "rcs") 'RCS
'SCCS
)))))
1039 (defun vc-backend-admin (file &optional rev comment
)
1040 ;; Register a file into the version-control system
1041 ;; Automatically retrieves a read-only version of the file with
1042 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1043 ;; it deletes the workfile.
1044 (vc-file-clearprops file
)
1045 (or vc-default-back-end
1046 (setq vc-default-back-end
(if (vc-find-binary "rcs") 'RCS
'SCCS
)))
1047 (message "Registering %s..." file
)
1050 ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end
)
1051 ((file-exists-p "RCS") 'RCS
)
1052 ((file-exists-p "SCCS") 'SCCS
)
1053 (t vc-default-back-end
))))
1054 (cond ((eq backend
'SCCS
)
1055 (vc-do-command 0 "admin" file
;; SCCS
1056 (and rev
(concat "-r" rev
))
1059 (and comment
(concat "-y" comment
))
1061 (car (rassq 'SCCS vc-master-templates
))
1062 (or (file-name-directory file
) "")
1063 (file-name-nondirectory file
)))
1065 (if vc-keep-workfiles
1066 (vc-do-command 0 "get" file
)))
1068 (vc-do-command 0 "ci" file
;; RCS
1069 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
1070 (and comment
(concat "-t-" comment
))
1073 (message "Registering %s...done" file
)
1076 (defun vc-backend-checkout (file &optional writeable rev
)
1077 ;; Retrieve a copy of a saved version into a workfile
1078 (message "Checking out %s...done" file
)
1079 (vc-backend-dispatch file
1081 (vc-do-command 0 "get" file
;; SCCS
1083 (and rev
(concat "-r" (vc-lookup-triple file rev
))))
1085 (vc-do-command 0 "co" file
;; RCS
1087 (and rev
(concat "-r" rev
)))
1089 (vc-file-setprop file
'vc-checkout-time
(nth 5 (file-attributes file
)))
1090 (message "Checking out %s...done" file
)
1093 (defun vc-backend-logentry-check (file)
1094 (vc-backend-dispatch file
1095 (if (>= (- (region-end) (region-beginning)) 512) ;; SCCS
1097 (message "Reverting %s..." file
)
1100 "Log must be less than 512 characters. Point is now at char 512.")))
1104 (defun vc-backend-checkin (file &optional rev comment
)
1105 ;; Register changes to FILE as level REV with explanatory COMMENT.
1106 ;; Automatically retrieves a read-only version of the file with
1107 (message "Reverting %s...done" file
)
1108 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1109 ;; it deletes the workfile.
1110 (message "Checking in %s..." file
)
1112 (message "Stealing lock on %s..." file
)
1113 ;; Change buffers to get local value of vc-checkin-switches.
1114 (set-buffer (or (get-file-buffer file
) (current-buffer)))
1115 (vc-backend-dispatch file
1117 (apply 'vc-do-command
0 "delta" file
1118 (if rev
(concat "-r" rev
))
1119 (concat "-y" comment
)
1120 vc-checkin-switches
)
1121 (if vc-keep-workfiles
1122 (message "Stealing lock on %s...done" file
)
1123 (vc-do-command 0 "get" file
))
1125 (apply 'vc-do-command
0 "ci" file
1126 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
1127 (concat "-m" comment
)
1128 (message "Removing last change from %s..." file
)
1129 vc-checkin-switches
)
1131 (vc-file-setprop file
'vc-locking-user nil
)
1132 (message "Checking in %s...done" file
)
1135 (defun vc-backend-revert (file)
1136 ;; Revert file to latest checked-in version.
1137 (message "Reverting %s..." file
)
1138 (vc-backend-dispatch
1141 (vc-do-command 0 "unget" file nil
)
1142 (vc-do-command 0 "get" file nil
))
1144 (delete-file file
) ;; RCS
1145 (vc-do-command 0 "co" file
"-u")))
1146 (vc-file-setprop file
'vc-locking-user nil
)
1147 (message "Reverting %s...done" file
)
1150 (defun vc-backend-steal (file &optional rev
)
1151 ;; Steal the lock on the current workfile. Needs RCS 5.6.2 or later for -M.
1152 (message "Stealing lock on %s..." file
)
1154 (vc-do-command 0 "unget" file
"-n" (if rev
(concat "-r" rev
)))
1155 (vc-do-command 0 "get" file
"-g" (if rev
(concat "-r" rev
)))
1158 (vc-do-command 0 "rcs" "-M" (concat "-u" rev
) file
)
1160 (vc-do-command 0 "rcs" (concat "-l" rev
) file
)
1162 (vc-file-setprop file
'vc-locking-user
(user-login-name))
1163 (message "Stealing lock on %s...done" file
)
1166 (defun vc-backend-uncheck (file target
)
1167 ;; Undo the latest checkin. Note: this code will have to get a lot
1168 ;; smarter when we support multiple branches.
1169 (message "Removing last change from %s..." file
)
1170 (vc-backend-dispatch file
1171 (vc-do-command 0 "rmdel" file
(concat "-r" target
))
1172 (vc-do-command 0 "rcs" file
(concat "-o" target
))
1174 (message "Removing last change from %s...done" file
)
1177 (defun vc-backend-print-log (file)
1178 ;; Print change log associated with FILE to buffer *vc*.
1180 (vc-backend-dispatch file
"prs" "rlog")
1184 (defun vc-backend-assign-name (file name
)
1185 ;; Assign to a FILE's latest version a given NAME.
1186 (vc-backend-dispatch file
1187 (vc-add-triple name file
(vc-latest-version file
)) ;; SCCS
1188 (vc-do-command 0 "rcs" file
(concat "-n" name
":")) ;; RCS
1190 (message "Removing last change from %s...done" file
)
1193 (defun vc-backend-diff (file oldvers
&optional newvers
)
1194 ;; Get a difference report between two versions
1195 (apply 'vc-do-command
1
1196 (or (vc-backend-dispatch file
"vcdiff" "rcsdiff")
1197 (error "File %s is not under version control." file
))
1199 (and oldvers
(concat "-r" oldvers
))
1200 (and newvers
(concat "-r" newvers
))
1204 (defun vc-check-headers ()
1205 "Check if the current file has any headers in it."
1208 (goto-char (point-min))
1209 (vc-backend-dispatch buffer-file-name
1210 (re-search-forward "%[MIRLBSDHTEGUYFPQCZWA]%" nil t
) ;; SCCS
1211 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
) ;; RCS
1215 ;; Back-end-dependent stuff ends here.
1217 ;; Set up key bindings for use while editing log messages
1219 (defun vc-log-mode ()
1220 "Minor mode for driving version-control tools.
1221 These bindings are added to the global keymap when you enter this mode:
1222 \\[vc-next-action] perform next logical version-control operation on current file
1223 \\[vc-register] register current file
1224 \\[vc-toggle-read-only] like next-action, but won't register files
1225 \\[vc-insert-headers] insert version-control headers in current file
1226 \\[vc-print-log] display change history of current file
1227 \\[vc-revert-buffer] revert buffer to latest version
1228 \\[vc-cancel-version] undo latest checkin
1229 \\[vc-diff] show diffs between file versions
1230 \\[vc-directory] show all files locked by any user in or below .
1231 \\[vc-update-change-log] add change log entry from recent checkins
1233 While you are entering a change log message for a version, the following
1234 additional bindings will be in effect.
1236 \\[vc-finish-logentry] proceed with check in, ending log message entry
1238 Whenever you do a checkin, your log comment is added to a ring of
1239 saved comments. These can be recalled as follows:
1241 \\[vc-next-comment] replace region with next message in comment ring
1242 \\[vc-previous-comment] replace region with previous message in comment ring
1243 \\[vc-search-comment-reverse] search backward for regexp in the comment ring
1244 \\[vc-search-comment-forward] search backward for regexp in the comment ring
1246 Entry to the change-log submode calls the value of text-mode-hook, then
1247 the value of vc-log-mode-hook.
1249 Global user options:
1250 vc-initial-comment If non-nil, require user to enter a change
1251 comment upon first checkin of the file.
1253 vc-keep-workfiles Non-nil value prevents workfiles from being
1254 deleted when changes are checked in
1256 vc-suppress-confirm Suppresses some confirmation prompts,
1257 notably for reversions.
1259 vc-diff-options A list consisting of the flags
1260 to be used for generating context diffs.
1262 vc-header-strings Which keywords to insert when adding headers
1263 with \\[vc-insert-headers]. Defaults to
1264 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
1266 vc-static-header-alist By default, version headers inserted in C files
1267 get stuffed in a static string area so that
1268 ident(RCS) or what(SCCS) can see them in the
1269 compiled object code. You can override this
1270 by setting this variable to nil, or change
1271 the header template by changing it.
1273 vc-command-messages if non-nil, display run messages from the
1274 actual version-control utilities (this is
1275 intended primarily for people hacking vc
1279 (set-syntax-table text-mode-syntax-table
)
1280 (use-local-map vc-log-entry-mode
)
1281 (setq local-abbrev-table text-mode-abbrev-table
)
1282 (setq major-mode
'vc-log-mode
)
1283 (setq mode-name
"VC-Log")
1284 (make-local-variable 'vc-log-file
)
1285 (make-local-variable 'vc-log-version
)
1286 (set-buffer-modified-p nil
)
1287 (setq buffer-file-name nil
)
1288 (run-hooks 'text-mode-hook
'vc-log-mode-hook
)
1291 ;; Initialization code, to be done just once at load-time
1292 (if vc-log-entry-mode
1294 (setq vc-log-entry-mode
(make-sparse-keymap))
1295 (define-key vc-log-entry-mode
"\M-n" 'vc-next-comment
)
1296 (define-key vc-log-entry-mode
"\M-p" 'vc-previous-comment
)
1297 (define-key vc-log-entry-mode
"\M-r" 'vc-comment-search-backward
)
1298 (define-key vc-log-entry-mode
"\M-s" 'vc-comment-search-forward
)
1299 (define-key vc-log-entry-mode
"\C-c\C-c" 'vc-finish-logentry
)
1302 ;;; These things should probably be generally available
1304 (defun vc-shrink-to-fit ()
1305 "Shrink a window vertically until it's just large enough to contain its text"
1306 (let ((minsize (1+ (count-lines (point-min) (point-max)))))
1307 (if (< minsize
(window-height))
1308 (let ((window-min-height 2))
1309 (shrink-window (- (window-height) minsize
))))))
1311 (defun vc-file-tree-walk (func &rest args
)
1312 "Walk recursively through default directory,
1313 invoking FUNC f ARGS on all non-directory files f underneath it."
1314 (vc-file-tree-walk-internal default-directory func args
)
1315 (message "Traversing directory %s...done" default-directory
))
1317 (defun vc-file-tree-walk-internal (file func args
)
1318 (if (not (file-directory-p file
))
1319 (apply func file args
)
1320 (message "Traversing directory %s..." file
)
1321 (let ((dir (file-name-as-directory file
)))
1325 (string-equal f
".")
1326 (string-equal f
"..")
1327 (let ((dirf (concat dir f
)))
1329 (file-symlink-p dirf
) ;; Avoid possible loops
1330 (vc-file-tree-walk-internal dirf func args
))))))
1331 (directory-files dir
)))))
1335 ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
1337 ;;; These may be useful to anyone who has to debug or extend the package.
1339 ;;; A fundamental problem in VC is that there are time windows between
1340 ;;; vc-next-action's computations of the file's version-control state and
1341 ;;; the actions that change it. This is a window open to lossage in a
1342 ;;; multi-user environment; someone else could nip in and change the state
1343 ;;; of the master during it.
1345 ;;; The performance problem is that rlog/prs calls are very expensive; we want
1346 ;;; to avoid them as much as possible.
1350 ;;; The performance problem, it turns out, simplifies in practice to the
1351 ;;; problem of making vc-locking-user fast. The two other functions that call
1352 ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
1353 ;;; makes snapshots, the other deletes the calling user's last change in the
1356 ;;; The race condition implies that we have to either (a) lock the master
1357 ;;; during the entire execution of vc-next-action, or (b) detect and
1358 ;;; recover from errors resulting from dispatch on an out-of-date state.
1360 ;;; Alternative (a) appears to be unfeasible. The problem is that we can't
1361 ;;; guarantee that the lock will ever be removed. Suppose a user starts a
1362 ;;; checkin, the change message buffer pops up, and the user, having wandered
1363 ;;; off to do something else, simply forgets about it?
1365 ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
1366 ;;; vc-locking-user. Usually, if a file is registered, we can read its locked/
1367 ;;; unlocked state and its current owner from its permissions.
1369 ;;; This shortcut will fail if someone has manually changed the workfile's
1370 ;;; permissions; also if developers are munging the workfile in several
1371 ;;; directories, with symlinks to a master (in this latter case, the
1372 ;;; permissions shortcut will fail to detect a lock asserted from another
1375 ;;; Note that these cases correspond exactly to the errors which could happen
1376 ;;; because of a competing checkin/checkout race in between two instances of
1379 ;;; For VC's purposes, a workfile/master pair may have the following states:
1381 ;;; A. Unregistered. There is a workfile, there is no master.
1383 ;;; B. Registered and not locked by anyone.
1385 ;;; C. Locked by calling user and unchanged.
1387 ;;; D. Locked by the calling user and changed.
1389 ;;; E. Locked by someone other than the calling user.
1391 ;;; This makes for 25 states and 20 error conditions. Here's the matrix:
1393 ;;; VC's idea of state
1395 ;;; V Actual state RCS action SCCS action Effect
1397 ;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
1398 ;;; B 5 . 6 7 8 co -l get -e checkout
1399 ;;; C 9 10 . 11 12 co -u unget; get revert
1400 ;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
1401 ;;; E 17 18 19 20 . rcs -u -M ; rcs -l unget -n ; get -g steal lock
1403 ;;; All commands take the master file name as a last argument (not shown).
1405 ;;; In the discussion below, a "self-race" is a pathological situation in
1406 ;;; which VC operations are being attempted simultaneously by two or more
1407 ;;; Emacsen running under the same username.
1409 ;;; The vc-next-action code has the following windows:
1412 ;;; Between the check for existence of a master file and the call to
1413 ;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
1414 ;;; never close if the initial-comment feature is on.
1417 ;;; Between the call to vc-workfile-unchanged-p in and the immediately
1418 ;;; following revert (apparent state C).
1421 ;;; Between the call to vc-workfile-unchanged-p in and the following
1422 ;;; checkin (apparent state D). This window may never close.
1425 ;;; Between the unlock and the immediately following checkout during a
1426 ;;; revert operation (apparent state C). Included in window Q.
1429 ;;; Between vc-locking-user and the following checkout (apparent state B).
1432 ;;; Between vc-locking-user and the following revert (apparent state C).
1433 ;;; Includes windows Q and S.
1436 ;;; Between vc-locking-user and the following checkin (apparent state
1437 ;;; D). This window may never be closed if the user fails to complete the
1438 ;;; checkin message. Includes window R.
1441 ;;; Between vc-locking-user and the following steal-lock (apparent
1442 ;;; state E). This window may never cloce if the user fails to complete
1443 ;;; the steal-lock message. Includes window X.
1446 ;;; Between the unlock and the immediately following re-lock during a
1447 ;;; steal-lock operation (apparent state E). This window may never cloce
1448 ;;; if the user fails to complete the steal-lock message.
1452 ;;; Apparent state A ---
1454 ;;; 1. File looked unregistered but is actually registered and not locked.
1456 ;;; Potential cause: someone else's admin during window P, with
1457 ;;; caller's admin happening before their checkout.
1459 ;;; RCS: ci will fail with a "no lock set by <user>" message.
1460 ;;; SCCS: admin will fail with error (ad19).
1462 ;;; We can let these errors be passed up to the user.
1464 ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
1466 ;;; Potential cause: self-race during window P.
1468 ;;; RCS: will revert the file to the last saved version and unlock it.
1469 ;;; SCCS: will fail with error (ad19).
1471 ;;; Either of these consequences is acceptable.
1473 ;;; 3. File looked unregistered but is actually locked by caller, changed.
1475 ;;; Potential cause: self-race during window P.
1477 ;;; RCS: will register the caller's workfile as a delta with a
1478 ;;; null change comment (the -t- switch will be ignored).
1479 ;;; SCCS: will fail with error (ad19).
1481 ;;; 4. File looked unregistered but is locked by someone else.
1483 ;;; Potential cause: someone else's admin during window P, with
1484 ;;; caller's admin happening *after* their checkout.
1486 ;;; RCS: will fail with a "no lock set by <user>" message.
1487 ;;; SCCS: will fail with error (ad19).
1489 ;;; We can let these errors be passed up to the user.
1491 ;;; Apparent state B ---
1493 ;;; 5. File looked registered and not locked, but is actually unregistered.
1495 ;;; Potential cause: master file got nuked during window P.
1497 ;;; RCS: will fail with "RCS/<file>: No such file or directory"
1498 ;;; SCCS: will fail with error ut4.
1500 ;;; We can let these errors be passed up to the user.
1502 ;;; 6. File looked registered and not locked, but is actually locked by the
1503 ;;; calling user and unchanged.
1505 ;;; Potential cause: self-race during window T.
1507 ;;; RCS: in the same directory as the previous workfile, co -l will fail
1508 ;;; with "co error: writable foo exists; checkout aborted". In any other
1509 ;;; directory, checkout will succeed.
1510 ;;; SCCS: will fail with ge17.
1512 ;;; Either of these consequences is acceptable.
1514 ;;; 7. File looked registered and not locked, but is actually locked by the
1515 ;;; calling user and changed.
1519 ;;; 8. File looked registered and not locked, but is actually locked by another
1522 ;;; Potential cause: someone else checks it out during window T.
1524 ;;; RCS: co error: revision 1.3 already locked by <user>
1525 ;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
1527 ;;; We can let these errors be passed up to the user.
1529 ;;; Apparent state C ---
1531 ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
1535 ;;; 10. File looks locked by calling user and unchanged, but is actually not
1538 ;;; Potential cause: a self-race in window U, or by the revert's
1539 ;;; landing during window X of some other user's steal-lock or window S
1540 ;;; of another user's revert.
1542 ;;; RCS: succeeds, refreshing the file from the identical version in
1544 ;;; SCCS: fails with error ut4 (p file nonexistent).
1546 ;;; Either of these consequences is acceptable.
1548 ;;; 11. File is locked by calling user. It looks unchanged, but is actually
1551 ;;; Potential cause: the file would have to be touched by a self-race
1552 ;;; during window Q.
1554 ;;; The revert will succeed, removing whatever changes came with
1555 ;;; the touch. It is theoretically possible that work could be lost.
1557 ;;; 12. File looks like it's locked by the calling user and unchanged, but
1558 ;;; it's actually locked by someone else.
1560 ;;; Potential cause: a steal-lock in window V.
1562 ;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
1563 ;;; SCCS: fails with error un2
1565 ;;; We can pass these errors up to the user.
1567 ;;; Apparent state D ---
1569 ;;; 13. File looks like it's locked by the calling user and changed, but it's
1570 ;;; actually unregistered.
1572 ;;; Potential cause: master file got nuked during window P.
1574 ;;; RCS: Checks in the user's version as an initial delta.
1575 ;;; SCCS: will fail with error ut4.
1577 ;;; This case is kind of nasty. It means VC may fail to detect the
1578 ;;; loss of previous version information.
1580 ;;; 14. File looks like it's locked by the calling user and changed, but it's
1581 ;;; actually unlocked.
1583 ;;; Potential cause: self-race in window V, or the checkin happening
1584 ;;; during the window X of someone else's steal-lock or window S of
1585 ;;; someone else's revert.
1587 ;;; RCS: ci will fail with "no lock set by <user>".
1588 ;;; SCCS: delta will fail with error ut4.
1590 ;;; 15. File looks like it's locked by the calling user and changed, but it's
1591 ;;; actually locked by the calling user and unchanged.
1593 ;;; Potential cause: another self-race --- a whole checkin/checkout
1594 ;;; sequence by the calling user would have to land in window R.
1596 ;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
1597 ;;; RCS: reverts to the file state as of the second user's checkin, leaving
1598 ;;; the file unlocked.
1600 ;;; It is theoretically possible that work could be lost under RCS.
1602 ;;; 16. File looks like it's locked by the calling user and changed, but it's
1603 ;;; actually locked by a different user.
1605 ;;; RCS: ci error: no lock set by <user>
1606 ;;; SCCS: unget will fail with error un2
1608 ;;; We can pass these errors up to the user.
1610 ;;; Apparent state E ---
1612 ;;; 17. File looks like it's locked by some other user, but it's actually
1617 ;;; 18. File looks like it's locked by some other user, but it's actually
1620 ;;; Potential cause: someone released a lock during window W.
1622 ;;; RCS: The calling user will get the lock on the file.
1623 ;;; SCCS: unget -n will fail with cm4.
1625 ;;; Either of these consequences will be OK.
1627 ;;; 19. File looks like it's locked by some other user, but it's actually
1628 ;;; locked by the calling user and unchanged.
1630 ;;; Potential cause: the other user relinquishing a lock followed by
1631 ;;; a self-race, both in window W.
1633 ;;; Under both RCS and SCCS, both unlock and lock will succeed, making
1634 ;;; the sequence a no-op.
1636 ;;; 20. File looks like it's locked by some other user, but it's actually
1637 ;;; locked by the calling user and changed.
1643 ;;; In order of decreasing severity:
1645 ;;; Cases 11 and 15 under RCS are the only one that potentially lose work.
1646 ;;; They would require a self-race for this to happen.
1648 ;;; Case 13 in RCS loses information about previous deltas, retaining
1649 ;;; only the information in the current workfile. This can only happen
1650 ;;; if the master file gets nuked in window P.
1652 ;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
1653 ;;; no change comment in the master. This would require a self-race in
1654 ;;; window P or R respectively.
1656 ;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
1658 ;;; Unfortunately, it appears to me that no recovery is possible in these
1659 ;;; cases. They don't yield error messages, so there's no way to tell that
1660 ;;; a race condition has occurred.
1662 ;;; All other cases don't change either the workfile or the master, and
1663 ;;; trigger command errors which the user will see.
1665 ;;; Thus, there is no explicit recovery code.