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.30 1993/03/29 15:38:31 eric Exp roland $
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 mode is fully documented in the Emacs user's manual.
30 ;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
31 ;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
32 ;; and Richard Stallman contributed valuable criticism, support, and testing.
34 ;; Supported version-control systems presently include SCCS and RCS;
35 ;; your RCS version should be 5.6.2 or later for proper operation of
36 ;; the lock-breaking code.
38 ;; The RCS code assumes strict locking. You can support the RCS -x option
39 ;; by adding pairs to the vc-master-templates list.
41 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
42 ;; to be installed somewhere on Emacs's path for executables.
44 ;; If your site uses the ChangeLog convention supported by Emacs, the
45 ;; function vc-comment-to-change-log should prove a useful checkin hook.
47 ;; This code depends on call-process passing back the subprocess exit
48 ;; status. Thus, you need Emacs 18.58 or later to run it.
50 ;; The vc code maintains some internal state in order to reduce expensive
51 ;; version-control operations to a minimum. Some names are only computed
52 ;; once. If you perform version control operations with RCS/SCCS/CVS while
53 ;; vc's back is turned, or move/rename master files while vc is running,
54 ;; vc may get seriously confused. Don't do these things!
56 ;; Developer's notes on some concurrency issues are included at the end of
63 ;; General customization
65 (defvar vc-default-back-end nil
66 "*Back-end actually used by this interface; may be SCCS or RCS.
67 The value is only computed when needed to avoid an expensive search.")
68 (defvar vc-diff-options
'("-a" "-c1")
69 "*The command/flags list to be used in constructing diff commands.")
70 (defvar vc-suppress-confirm nil
71 "*If non-nil, reat user as expert; suppress yes-no prompts on some things.")
72 (defvar vc-keep-workfiles t
73 "*If non-nil, don't delete working files after registering changes.")
74 (defvar vc-initial-comment nil
75 "*Prompt for initial comment when a file is registered.")
76 (defvar vc-command-messages nil
77 "*Display run messages from back-end commands.")
78 (defvar vc-mistrust-permissions
'file-symlink-p
79 "*Don't assume that permissions and ownership track version-control status.")
81 (defvar vc-checkin-switches nil
82 "*Extra switches passed to the checkin program by \\[vc-checkin].")
85 (defvar vc-checkin-hook nil
86 "*List of functions called after a vc-checkin is done. See `run-hooks'.")
88 ;; Header-insertion hair
90 (defvar vc-header-alist
91 '((SCCS "\%W\%") (RCS "\$Id\$"))
92 "*Header keywords to be inserted when vc-insert-header is executed.")
93 (defconst vc-static-header-alist
95 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
96 "*Associate static header string templates with file types. A \%s in the
97 template is replaced with the first string associated with the file's
98 verson-control type in vc-header-alist.")
100 (defvar vc-comment-alist
101 '((nroff-mode ".\\\"" ""))
102 "*Special comment delimiters to be used in generating vc headers only.
103 Add an entry in this list if you need to override the normal comment-start
104 and comment-end variables. This will only be necessary if the mode language
105 is sensitive to blank lines.")
107 ;; Variables the user doesn't need to know about.
108 (defvar vc-log-entry-mode nil
)
109 (defvar vc-log-operation nil
)
110 (defvar vc-log-after-operation-hook nil
)
111 (defvar vc-checkout-writeable-buffer-hook
'vc-checkout-writeable-buffer
)
112 (defvar vc-parent-buffer nil
)
115 (defvar vc-log-version
)
117 (defconst vc-name-assoc-file
"VC-names")
119 ;; File property caching
121 (defun vc-file-clearprops (file)
122 ;; clear all properties of a given file
123 (setplist (intern file vc-file-prop-obarray
) nil
))
125 ;; Random helper functions
127 (defun vc-name (file)
128 "Return the master name of a file, nil if it is not registered"
129 (or (vc-file-getprop file
'vc-name
)
130 (vc-file-setprop file
'vc-name
131 (let ((name-and-type (vc-registered file
)))
132 (and name-and-type
(car name-and-type
))))))
134 (defvar vc-binary-assoc nil
)
136 (defun vc-find-binary (name)
137 "Look for a command anywhere on the subprocess-command search path."
138 (or (cdr (assoc name vc-binary-assoc
))
142 (function (lambda (s)
143 (if (and s
(file-exists-p (setq full
(concat s
"/" name
))))
144 (throw 'found nil
))))
147 (setq vc-binary-assoc
(cons (cons name full
) vc-binary-assoc
)))
150 (defun vc-do-command (okstatus command file
&rest flags
)
151 "Execute a version-control command, notifying user and checking for errors.
152 The command is successful if its exit status does not exceed OKSTATUS.
153 Output from COMMAND goes to buffer *vc*. The last argument of the command is
154 the master name of FILE; this is appended to an optional list of FLAGS."
155 (setq file
(expand-file-name file
))
156 (if vc-command-messages
157 (message "Running %s on %s..." command file
))
158 (let ((obuf (current-buffer)) (camefrom (current-buffer))
160 (vc-file (and file
(vc-name file
)))
162 (set-buffer (get-buffer-create "*vc*"))
163 (make-local-variable 'vc-parent-buffer
)
164 (setq vc-parent-buffer camefrom
)
167 ;; This is so that command arguments typed in the *vc* buffer will
168 ;; have reasonable defaults.
169 (setq default-directory
(file-name-directory file
))
172 (function (lambda (s) (and s
(setq squeezed
(append squeezed
(list s
))))))
175 (setq squeezed
(append squeezed
(list vc-file
))))
176 (let ((default-directory (file-name-directory (or file
"./"))))
177 (setq status
(apply 'call-process command nil t nil squeezed
)))
178 (goto-char (point-max))
180 (if (or (not (integerp status
)) (< okstatus status
))
183 (print (cons command squeezed
))
185 (pop-to-buffer "*vc*")
187 (goto-char (point-min))
188 (error "Running %s...FAILED (%s)" command
189 (if (integerp status
)
190 (format "status %d" status
)
193 (if vc-command-messages
194 (message "Running %s...OK" command
))
200 ;;; Save a bit of the text around POSN in the current buffer, to help
201 ;;; us find the corresponding position again later. This works even
202 ;;; if all markers are destroyed or corrupted.
203 (defun vc-position-context (posn)
206 (buffer-substring posn
207 (min (point-max) (+ posn
100)))))
209 ;;; Return the position of CONTEXT in the current buffer, or nil if we
210 ;;; couldn't find it.
211 (defun vc-find-position-by-context (context)
212 (let ((context-string (nth 2 context
)))
213 (if (equal "" context-string
)
216 (let ((diff (- (nth 1 context
) (buffer-size))))
217 (if (< diff
0) (setq diff
(- diff
)))
218 (goto-char (nth 0 context
))
219 (if (or (search-forward context-string nil t
)
220 ;; Can't use search-backward since the match may continue
222 (progn (goto-char (- (point) diff
(length context-string
)))
223 ;; goto-char doesn't signal an error at
224 ;; beginning of buffer like backward-char would
225 (search-forward context-string nil t
)))
226 ;; to beginning of OSTRING
227 (- (point) (length context-string
))))))))
229 (defun vc-revert-buffer1 (&optional arg no-confirm
)
230 ;; Most of this was shamelessly lifted from Sebastian Kremer's rcs.el mode.
231 ;; Revert buffer, try to keep point and mark where user expects them in spite
232 ;; of changes because of expanded version-control key words.
233 ;; This is quite important since otherwise typeahead won't work as expected.
236 (let ((point-context (vc-position-context (point)))
237 ;; Use mark-marker to avoid confusion in transient-mark-mode.
238 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
239 (vc-position-context (mark-marker))))
240 ;; Make the right thing happen in transient-mark-mode.
243 ;; the actual revisit
244 (revert-buffer arg no-confirm
)
246 ;; Restore point and mark
247 (let ((new-point (vc-find-position-by-context point-context
)))
248 (if new-point
(goto-char new-point
)))
250 (let ((new-mark (vc-find-position-by-context mark-context
)))
251 (if new-mark
(set-mark new-mark
))))))
254 (defun vc-buffer-sync ()
255 ;; Make sure the current buffer and its working file are in sync
256 (if (and (buffer-modified-p)
259 (y-or-n-p (format "%s has been modified. Write it out? "
263 (defun vc-workfile-unchanged-p (file)
264 ;; Has the given workfile changed since last checkout?
265 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
266 (lastmod (nth 5 (file-attributes file
))))
268 (equal lastmod checkout-time
)
269 (if (zerop (vc-backend-diff file nil
))
271 (vc-file-setprop file
'vc-checkout-time lastmod
)
274 (vc-file-setprop file
'vc-checkout-time
'(0 .
0))
279 ;; Here's the major entry point.
282 (defun vc-next-action (verbose)
283 "Do the next logical checkin or checkout operation on the current file.
284 If the file is not already registered, this registers it for version
285 control and then retrieves a writeable, locked copy for editing.
286 If the file is registered and not locked by anyone, this checks out
287 a writeable and locked file ready for editing.
288 If the file is checked out and locked by the calling user, this
289 first checks to see if the file has changed since checkout. If not,
290 it performs a revert.
291 If the file has been changed, this pops up a buffer for creation of
292 a log message; when the message has been entered, it checks in the
293 resulting changes along with the log message as change commentary. If
294 the variable vc-keep-workfiles is non-nil (which is its default), a
295 read-only copy of the changed file is left in place afterwards.
296 If the file is registered and locked by someone else, you are given
297 the option to steal the lock."
299 (while vc-parent-buffer
300 (pop-to-buffer vc-parent-buffer
))
303 (do-update owner version
304 (file buffer-file-name
)
305 (vc-file (vc-name buffer-file-name
))
311 ;; if there is no master file corresponding, create one
313 (vc-register verbose
)
314 (if vc-initial-comment
315 (setq vc-log-after-operation-hook
316 'vc-checkout-writeable-buffer-hook
)
317 (vc-checkout-writeable-buffer)))
319 ;; if there is no lock on the file, assert one and get it
320 ((not (setq owner
(vc-locking-user file
)))
321 (vc-checkout-writeable-buffer))
323 ;; a checked-out version exists, but the user may not own the lock
324 ((not (string-equal owner
(user-login-name)))
327 (and verbose
(read-string "Version to steal: "))
330 ;; OK, user owns the lock on the file
333 ;; give luser a chance to save before checking in.
336 ;; Revert if file is unchanged and buffer is too.
337 ;; If buffer is modified, that means the user just said no
338 ;; to saving it; in that case, don't revert,
339 ;; because the user might intend to save
340 ;; after finishing the log entry.
341 (if (and (vc-workfile-unchanged-p file
)
342 (not (buffer-modified-p)))
344 (vc-backend-revert file
)
345 ;; DO NOT revert the file without asking the user!
346 (vc-resynch-window file t nil
))
348 ;; user may want to set nonstandard parameters
350 (setq version
(read-string "New version level: ")))
352 ;; OK, let's do the checkin
353 (vc-checkin file version
))))))
354 (error "There is no file associated with buffer %s" (buffer-name))))
356 ;;; These functions help the vc-next-action entry point
358 (defun vc-checkout-writeable-buffer ()
359 "Retrieve a writeable copy of the latest version of the current buffer's file."
360 (vc-checkout (buffer-file-name) t
)
364 (defun vc-register (&optional override
)
365 "Register the current file into your version-control system."
367 (if (vc-name buffer-file-name
)
368 (error "This file is already registered."))
369 ;; Watch out for new buffers of size 0: the corresponding file
370 ;; does not exist yet, even though buffer-modified-p is nil.
371 (if (and (not (buffer-modified-p))
372 (zerop (buffer-size))
373 (not (file-exists-p buffer-file-name
)))
374 (set-buffer-modified-p t
))
378 (and override
(read-string "Initial version level: ")))
381 (defun vc-resynch-window (file &optional keep noquery
)
382 ;; If the given file is in the current buffer,
383 ;; either revert on it so we see expanded keyworks,
384 ;; or unvisit it (depending on vc-keep-workfiles)
385 ;; NOQUERY if non-nil inhibits confirmation for reverting.
386 ;; NOQUERY should be t *only* if it is known the only difference
387 ;; between the buffer and the file is due to RCS rather than user editing!
388 (and (string= buffer-file-name file
)
391 (vc-revert-buffer1 t noquery
)
392 (vc-mode-line buffer-file-name
))
395 (kill-buffer (current-buffer))))))
398 (defun vc-admin (file rev
)
399 "Check a file into your version-control system.
400 FILE is the unmodified name of the file. REV should be the base version
401 level to check it in under."
402 (if vc-initial-comment
403 (let ((camefrom (current-buffer)))
404 (pop-to-buffer (get-buffer-create "*VC-log*"))
405 (make-local-variable 'vc-parent-buffer
)
406 (setq vc-parent-buffer camefrom
)
408 (narrow-to-region (point-max) (point-max))
409 (vc-mode-line file
(file-name-nondirectory file
))
410 (setq vc-log-operation
'vc-backend-admin
)
411 (setq vc-log-file file
)
412 (setq vc-log-version rev
)
413 (message "Enter initial comment. Type C-c C-c when done."))
415 (vc-backend-admin file rev
)
416 ;; Inhibit query here, since otherwise we always get asked.
417 (vc-resynch-window file vc-keep-workfiles t
))))
419 (defun vc-steal-lock (file rev
&optional owner
)
420 "Steal the lock on the current workfile."
423 (setq owner
(vc-locking-user file
)))
424 (if (not (y-or-n-p (format "Take the lock on %s:%s from %s?" file rev owner
)))
425 (error "Steal cancelled."))
427 (pop-to-buffer (get-buffer-create "*VC-mail*"))
428 (setq default-directory
(expand-file-name "~/"))
429 (auto-save-mode auto-save-default
)
432 (mail-setup owner
(format "%s:%s" file rev
) nil nil nil
433 (list (list 'vc-finish-steal file rev
)))
434 (goto-char (point-max))
436 (format "I stole the lock on %s:%s, " file rev
)
437 (current-time-string)
439 (message "Please explain why you stole the lock. Type C-c C-c when done."))
441 ;; This is called when the notification has been sent.
442 (defun vc-finish-steal (file version
)
443 (vc-backend-steal file version
)
444 (vc-resynch-window file t t
))
446 (defun vc-checkout (file &optional writeable
)
447 "Retrieve a copy of the latest version of the given file."
448 ;; If ftp is on this system and the name matches the ange-ftp format
449 ;; for a remote file, the user is trying something that won't work.
450 (if (and (string-match "^/[^/:]+:" file
) (vc-find-binary "ftp"))
451 (error "Sorry, you can't check out files over FTP"))
452 (vc-backend-checkout file writeable
)
453 (if (string-equal file buffer-file-name
)
454 (vc-resynch-window file t t
))
457 (defun vc-checkin (file &optional rev comment
)
458 "Check in the file specified by FILE.
459 The optional argument REV may be a string specifying the new version level
460 \(if nil increment the current level). The file is either retained with write
461 permissions zeroed, or deleted (according to the value of vc-keep-workfiles).
462 COMMENT is a comment string; if omitted, a buffer is
463 popped up to accept a comment."
464 (let ((camefrom (current-buffer)))
465 (pop-to-buffer (get-buffer-create "*VC-log*"))
466 (make-local-variable 'vc-parent-buffer
)
467 (setq vc-parent-buffer camefrom
))
469 (narrow-to-region (point-max) (point-max))
470 (vc-mode-line file
(file-name-nondirectory file
))
471 (setq vc-log-operation
'vc-backend-checkin
474 vc-log-after-operation-hook
'vc-checkin-hook
)
475 (message "Enter log message. Type C-c C-c when done.")
479 (vc-finish-logentry))))
481 ;;; Here is a checkin hook that may prove useful to sites using the
482 ;;; ChangeLog facility supported by Emacs.
483 (defun vc-comment-to-change-log (&optional file
)
485 Update change log from comments entered into VC for the currently visited file.
486 Optional arg specifies the change log file name; see `find-change-log'.
487 See `vc-update-change-log'."
489 (let ((log (find-change-log file
)))
491 (let ((default-directory (or (file-name-directory log
)
493 (vc-update-change-log
494 (file-relative-name buffer-file-name
))))))
496 (defun vc-finish-logentry ()
497 "Complete the operation implied by the current log entry."
499 (goto-char (point-max))
500 (if (not (bolp)) (newline))
501 ;; Append the contents of the log buffer to the comment ring
503 (set-buffer (get-buffer-create "*VC-comment-ring*"))
504 (goto-char (point-max))
506 (insert-buffer-substring "*VC-log*")
507 (if (and (not (bobp)) (not (= (char-after (1- (point))) ?
\f)))
511 (exchange-point-and-mark)
513 (vc-backend-logentry-check vc-log-file
)
517 (funcall vc-log-operation
521 (error "No log operation is pending."))
522 ;; Return to "parent" buffer of this checkin and remove checkin window
523 (pop-to-buffer (get-file-buffer vc-log-file
))
524 (delete-window (get-buffer-window "*VC-log*"))
525 (bury-buffer "*VC-log*")
526 (bury-buffer "*VC-comment-ring*")
527 ;; Now make sure we see the expanded headers
528 (vc-resynch-window buffer-file-name vc-keep-workfiles t
)
529 (run-hooks vc-log-after-operation-hook
)
532 ;; Code for access to the comment ring
534 (defun vc-next-comment ()
535 "Fill the log buffer with the next message in the msg ring."
539 (set-buffer "*VC-comment-ring*")
541 (if (= (point) (point-max))
542 (goto-char (point-min)))
544 (append-to-buffer "*VC-log*" (point) (1- (mark)))
547 (defun vc-previous-comment ()
548 "Fill the log buffer with the previous message in the msg ring."
552 (set-buffer "*VC-comment-ring*")
553 (if (= (point) (point-min))
554 (goto-char (point-max)))
557 (append-to-buffer "*VC-log*" (point) (1- (mark)))
560 (defun vc-comment-search-backward (regexp)
561 "Fill the log buffer with the last message in the msg ring matching REGEXP."
562 (interactive "sSearch backward for: ")
565 (set-buffer "*VC-comment-ring*")
566 (if (= (point) (point-min))
567 (goto-char (point-max)))
568 (re-search-backward regexp nil t
)
570 (append-to-buffer "*VC-log*" (point) (1- (mark)))
573 (defun vc-comment-search-forward (regexp)
574 "Fill the log buffer with the next message in the msg ring matching REGEXP."
575 (interactive "sSearch forward for: ")
578 (set-buffer "*VC-comment-ring*")
579 (if (= (point) (point-max))
580 (goto-char (point-min)))
581 (re-search-forward regexp nil t
)
583 (append-to-buffer "*VC-log*" (point) (1- (mark)))
586 ;; Additional entry points for examining version histories
589 (defun vc-diff (historic)
590 "Display diffs between file versions."
592 (while vc-parent-buffer
593 (pop-to-buffer vc-parent-buffer
))
595 (call-interactively 'vc-version-diff
)
596 (let ((file buffer-file-name
)
599 (setq unchanged
(vc-workfile-unchanged-p buffer-file-name
))
601 (message "No changes to %s since latest version." file
)
602 (pop-to-buffer "*vc*")
603 (vc-backend-diff file nil
)
605 (goto-char (point-min))
612 (defun vc-version-diff (file rel1 rel2
)
613 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
614 If FILE is a directory, generate diffs between versions for all registered
615 files in or below it."
616 (interactive "FFile or directory to diff: \nsOlder version: \nsNewer version: ")
617 (if (string-equal rel1
"") (setq rel1 nil
))
618 (if (string-equal rel2
"") (setq rel2 nil
))
619 (if (file-directory-p file
)
620 (let ((camefrom (current-buffer)))
621 (set-buffer (get-buffer-create "*vc-status*"))
622 (make-local-variable 'vc-parent-buffer
)
623 (setq vc-parent-buffer camefrom
)
625 (insert "Diffs between "
626 (or rel1
"last version checked in")
628 (or rel2
"current workfile(s)")
630 (set-buffer (get-buffer-create "*vc*"))
632 (function (lambda (f)
633 (message "Looking at %s" f
)
635 (not (file-directory-p f
))
637 (vc-backend-diff f rel1 rel2
)
638 (append-to-buffer "*vc-status*" (point-min) (point-max)))
640 (pop-to-buffer "*vc-status*")
641 (insert "\nEnd of diffs.\n")
642 (goto-char (point-min))
643 (set-buffer-modified-p nil
)
646 (vc-backend-diff file rel1 rel2
)
647 (goto-char (point-min))
648 (if (equal (point-min) (point-max))
649 (message "No changes to %s between %s and %s." file rel1 rel2
)
650 (pop-to-buffer "*vc*")
651 (goto-char (point-min))
657 ;; Header-insertion code
660 (defun vc-insert-headers ()
661 "Insert headers in a file for use with your version-control system.
662 Headers desired are inserted at the start of the buffer, and are pulled from
663 the variable vc-header-alist"
665 (while vc-parent-buffer
666 (pop-to-buffer vc-parent-buffer
))
670 (if (or (not (vc-check-headers))
671 (y-or-n-p "Version headers already exist. Insert another set?"))
673 (let* ((delims (cdr (assq major-mode vc-comment-alist
)))
674 (comment-start-vc (or (car delims
) comment-start
"#"))
675 (comment-end-vc (or (car (cdr delims
)) comment-end
""))
676 (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist
))))
677 (mapcar (function (lambda (s)
678 (insert comment-start-vc
"\t" s
"\t"
679 comment-end-vc
"\n")))
681 (if vc-static-header-alist
682 (mapcar (function (lambda (f)
683 (if (string-match (car f
) buffer-file-name
)
684 (insert (format (cdr f
) (car hdstrings
))))))
685 vc-static-header-alist
))
689 ;; Status-checking functions
692 (defun vc-directory (verbose)
693 "Show version-control status of all files under the current directory."
697 (set-buffer (get-buffer-create "*vc-status*"))
700 (function (lambda (f)
701 (if (vc-registered f
)
702 (let ((user (vc-locking-user f
)))
703 (if (or user verbose
)
706 (concat user
) f
))))))))
707 (setq nonempty
(not (zerop (buffer-size)))))
710 (pop-to-buffer "*vc-status*" t
)
712 (goto-char (point-min)))
713 (message "No files are currently %s under %s"
714 (if verbose
"registered" "locked") default-directory
))
717 ;; Named-configuration support for SCCS
719 (defun vc-add-triple (name file rev
)
721 (find-file (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
))
722 (goto-char (point-max))
723 (insert name
"\t:\t" file
"\t" rev
"\n")
725 (kill-buffer (current-buffer))
728 (defun vc-record-rename (file newname
)
730 (find-file (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
))
731 (goto-char (point-min))
732 (replace-regexp (concat ":" (regexp-quote file
) "$") (concat ":" newname
))
734 (kill-buffer (current-buffer))
737 (defun vc-lookup-triple (file name
)
738 ;; Return the numeric version corresponding to a named snapshot of file
739 ;; If name is nil or a version number string it's just passed through
740 (cond ((null name
) "")
741 ((let ((firstchar (aref name
0)))
742 (and (>= firstchar ?
0) (<= firstchar ?
9)))
746 (concat (vc-backend-subdirectory-name file
) "/" vc-name-assoc-file
)
747 (list (concat name
"\t:\t" file
"\t\\(.+\\)"))))
750 ;; Named-configuration entry points
752 (defun vc-quiescent-p ()
753 ;; Is the current directory ready to be snapshot?
756 (function (lambda (f)
757 (if (and (vc-registered f
) (vc-locking-user f
))
758 (throw 'quiet nil
)))))
762 (defun vc-create-snapshot (name)
763 "Make a snapshot called NAME.
764 The snapshot is made from all registered files at or below the current
765 directory. For each file, the version level of its latest
766 version becomes part of the named configuration."
767 (interactive "sNew snapshot name: ")
768 (if (not (vc-quiescent-p))
769 (error "Can't make a snapshot, locked files are in the way.")
771 (function (lambda (f) (and
773 (vc-backend-assign-name f name
)))))
777 (defun vc-retrieve-snapshot (name)
778 "Retrieve the snapshot called NAME.
779 This function fails if any files are locked at or below the current directory
780 Otherwise, all registered files are checked out (unlocked) at their version
781 levels in the snapshot."
782 (interactive "sSnapshot name to retrieve: ")
783 (if (not (vc-quiescent-p))
784 (error "Can't retrieve a snapshot, locked files are in the way.")
786 (function (lambda (f) (and
788 (vc-error-occurred (vc-backend-checkout f nil name
))))))
791 ;; Miscellaneous other entry points
794 (defun vc-print-log ()
795 "List the change log of the current buffer in a window."
797 (while vc-parent-buffer
798 (pop-to-buffer vc-parent-buffer
))
799 (if (and buffer-file-name
(vc-name buffer-file-name
))
801 (vc-backend-print-log buffer-file-name
)
802 (pop-to-buffer (get-buffer-create "*vc*"))
804 (goto-char (point-min))
806 (error "There is no version-control master associated with this buffer")
811 (defun vc-revert-buffer ()
812 "Revert the current buffer's file back to the latest checked-in version.
813 This asks for confirmation if the buffer contents are not identical
816 (while vc-parent-buffer
817 (pop-to-buffer vc-parent-buffer
))
818 (let ((file buffer-file-name
)
819 (obuf (current-buffer)) (changed (vc-diff nil
)))
820 (if (and changed
(or vc-suppress-confirm
821 (not (yes-or-no-p "Discard changes? "))))
824 (error "Revert cancelled."))
828 (vc-backend-revert file
)
829 (vc-resynch-window file t t
)
834 (defun vc-cancel-version (norevert)
835 "Undo your latest checkin."
837 (while vc-parent-buffer
838 (pop-to-buffer vc-parent-buffer
))
839 (let* ((target (concat (vc-latest-version (buffer-file-name))))
840 (yours (concat (vc-your-latest-version (buffer-file-name))))
841 (prompt (if (string-equal yours target
)
842 "Remove your version %s from master?"
843 "Version %s was not your change. Remove it anyway?")))
844 (if (null (yes-or-no-p (format prompt target
)))
846 (vc-backend-uncheck (buffer-file-name) target
)
848 (vc-mode-line (buffer-file-name))
849 (vc-checkout (buffer-file-name) nil
)))
852 (defun vc-rename-file (old new
)
853 "Rename a file, taking its master files with it."
854 (interactive "fOld name: \nFNew name: ")
855 (let ((oldbuf (get-file-buffer old
)))
856 (if (buffer-modified-p oldbuf
)
857 (error "Please save files before moving them."))
858 (if (get-file-buffer new
)
859 (error "Already editing new file name."))
860 (let ((oldmaster (vc-name old
)))
862 (if (vc-locking-user old
)
863 (error "Please check in files before moving them."))
864 (if (or (file-symlink-p oldmaster
)
865 ;; This had FILE, I changed it to OLD. -- rms.
866 (file-symlink-p (vc-backend-subdirectory-name old
)))
867 (error "This is not a safe thing to do in the presence of symbolic links."))
868 (rename-file oldmaster
(vc-name new
)))
869 (if (or (not oldmaster
) (file-exists-p old
))
870 (rename-file old new
)))
871 ; ?? Renaming a file might change its contents due to keyword expansion.
872 ; We should really check out a new copy if the old copy was precisely equal
873 ; to some checked in version. However, testing for this is tricky....
877 (set-visited-file-name new
)
878 (set-buffer-modified-p nil
))))
879 ;; This had FILE, I changed it to OLD. -- rms.
880 (vc-backend-dispatch old
881 (vc-record-rename old new
)
886 (defun vc-update-change-log (&rest args
)
887 "Find change log file and add entries from recent RCS logs.
888 The mark is left at the end of the text prepended to the change log.
889 With prefix arg of C-u, only find log entries for the current buffer's file.
890 With any numeric prefix arg, find log entries for all files currently visited.
891 From a program, any arguments are passed to the `rcs2log' script."
893 (cond ((consp current-prefix-arg
) ;C-u
894 (list buffer-file-name
))
895 (current-prefix-arg ;Numeric argument.
897 (buffers (buffer-list))
900 (setq file
(buffer-file-name (car buffers
)))
901 (and file
(vc-backend-deduce file
)
902 (setq files
(cons (file-relative-name file
) files
)))
903 (setq buffers
(cdr buffers
)))
905 (find-file-other-window "ChangeLog")
906 (barf-if-buffer-read-only)
909 (goto-char (point-min))
911 (message "Computing change log entries...")
912 (message "Computing change log entries...%s"
913 (if (eq 0 (apply 'call-process
"rcs2log" nil t nil args
))
916 ;; Functions for querying the master and lock files.
918 (defun match-substring (bn)
919 (buffer-substring (match-beginning bn
) (match-end bn
)))
921 (defun vc-parse-buffer (patterns &optional file properties
)
922 ;; Use PATTERNS to parse information out of the current buffer
923 ;; by matching each regular expression in the list and returning \\1.
924 ;; If a regexp has two tag brackets, assume the second is a date
925 ;; field and we want the most recent entry matching the template.
926 ;; If FILE and PROPERTIES are given, the latter must be a list of
927 ;; properties of the same length as PATTERNS; each property is assigned
928 ;; the corresponding value.
929 (mapcar (function (lambda (p)
930 (goto-char (point-min))
931 (if (string-match "\\\\(.*\\\\(" p
)
932 (let ((latest-date "") (latest-val))
933 (while (re-search-forward p nil t
)
934 (let ((date (match-substring 2)))
935 (if (string< latest-date date
)
937 (setq latest-date date
)
939 (match-substring 1))))))
942 (and (re-search-forward p nil t
)
943 (let ((value (match-substring 1)))
945 (vc-file-setprop file
(car properties
) value
))
947 (setq properties
(cdr properties
)))))
951 (defun vc-master-info (file fields
&optional rfile properties
)
952 ;; Search for information in a master file.
953 (if (and file
(file-exists-p file
))
956 (setq buf
(create-file-buffer file
))
959 (insert-file-contents file nil
)
960 (set-buffer-modified-p nil
)
963 (vc-parse-buffer fields rfile properties
)
964 (kill-buffer (current-buffer)))
968 (function (lambda (p) (vc-file-setprop rfile p nil
)))
973 (defun vc-log-info (command file patterns
&optional properties
)
974 ;; Search for information in log program output
975 (if (and file
(file-exists-p file
))
978 (setq buf
(get-buffer-create "*vc*"))
980 (apply 'vc-do-command
0 command file nil
)
981 (set-buffer-modified-p nil
)
983 (vc-parse-buffer patterns file properties
)
984 (kill-buffer (current-buffer))
989 (function (lambda (p) (vc-file-setprop file p nil
)))
994 (defun vc-locking-user (file)
995 "Return the name of the person currently holding a lock on FILE.
996 Return nil if there is no such person."
997 (if (or (not vc-keep-workfiles
)
998 (eq vc-mistrust-permissions
't
)
999 (and vc-mistrust-permissions
1000 (funcall vc-mistrust-permissions
(vc-backend-subdirectory-name file
))))
1001 (vc-true-locking-user file
)
1002 ;; This implementation assumes that any file which is under version
1003 ;; control and has -rw-r--r-- is locked by its owner. This is true
1004 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
1005 ;; We have to be careful not to exclude files with execute bits on;
1006 ;; scripts can be under version control too. The advantage of this
1007 ;; hack is that calls to the very expensive vc-fetch-properties
1008 ;; function only have to be made if (a) the file is locked by someone
1009 ;; other than the current user, or (b) some untoward manipulation
1010 ;; behind vc's back has twiddled the `group' or `other' write bits.
1011 (let ((attributes (file-attributes file
)))
1012 (cond ((string-match ".r-.r-.r-." (nth 8 attributes
))
1014 ((and (= (nth 2 attributes
) (user-uid))
1015 (string-match ".rw.r-.r-." (nth 8 attributes
)))
1018 (vc-true-locking-user file
))))))
1020 (defun vc-true-locking-user (file)
1021 ;; The slow but reliable version
1022 (vc-fetch-properties file
)
1023 (vc-file-getprop file
'vc-locking-user
))
1025 (defun vc-latest-version (file)
1026 ;; Return version level of the latest version of FILE
1027 (vc-fetch-properties file
)
1028 (vc-file-getprop file
'vc-latest-version
))
1030 (defun vc-your-latest-version (file)
1031 ;; Return version level of the latest version of FILE checked in by you
1032 (vc-fetch-properties file
)
1033 (vc-file-getprop file
'vc-your-latest-version
))
1035 ;; Collect back-end-dependent stuff here
1037 ;; Everything eventually funnels through these functions. To implement
1038 ;; support for a new version-control system, add another branch to the
1039 ;; vc-backend-dispatch macro and fill it in in each call. The variable
1040 ;; vc-master-templates in vc-hooks.el will also have to change.
1042 (defmacro vc-backend-dispatch
(f s r
)
1043 "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
1044 (list 'let
(list (list 'type
(list 'vc-backend-deduce f
)))
1046 (list (list 'eq
'type
(quote 'SCCS
)) s
) ;; SCCS
1047 (list (list 'eq
'type
(quote 'RCS
)) r
) ;; RCS
1050 (defun vc-lock-file (file)
1051 ;; Generate lock file name corresponding to FILE
1052 (let ((master (vc-name file
)))
1055 (string-match "\\(.*/\\)s\\.\\(.*\\)" master
)
1057 (substring master
(match-beginning 1) (match-end 1))
1059 (substring master
(match-beginning 2) (match-end 2))))))
1062 (defun vc-fetch-properties (file)
1063 ;; Re-fetch all properties associated with the given file.
1064 ;; Currently these properties are:
1066 ;; vc-locked-version
1067 ;; vc-latest-version
1068 ;; vc-your-latest-version
1069 (vc-backend-dispatch
1073 (vc-master-info (vc-lock-file file
)
1075 "^[^ ]+ [^ ]+ \\([^ ]+\\)"
1078 '(vc-locking-user vc-locked-version
))
1079 (vc-master-info (vc-name file
)
1081 "^\001d D \\([^ ]+\\)"
1082 (concat "^\001d D \\([^ ]+\\) .* "
1083 (regexp-quote (user-login-name)) " ")
1086 '(vc-latest-version vc-your-latest-version
))
1089 (vc-log-info "rlog" file
1091 "^locks: strict\n\t\\([^:]+\\)"
1092 "^locks: strict\n\t[^:]+: \\(.+\\)"
1093 "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
1095 "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
1096 (regexp-quote (user-login-name))
1098 '(vc-locking-user vc-locked-version
1099 vc-latest-version vc-your-latest-version
))
1102 (defun vc-backend-subdirectory-name (&optional file
)
1103 ;; Where the master and lock files for the current directory are kept
1106 (and file
(vc-backend-deduce file
))
1108 (setq vc-default-back-end
(if (vc-find-binary "rcs") 'RCS
'SCCS
)))))
1110 (defun vc-backend-admin (file &optional rev comment
)
1111 ;; Register a file into the version-control system
1112 ;; Automatically retrieves a read-only version of the file with
1113 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1114 ;; it deletes the workfile.
1115 (vc-file-clearprops file
)
1116 (or vc-default-back-end
1117 (setq vc-default-back-end
(if (vc-find-binary "rcs") 'RCS
'SCCS
)))
1118 (message "Registering %s..." file
)
1121 ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end
)
1122 ((file-exists-p "RCS") 'RCS
)
1123 ((file-exists-p "SCCS") 'SCCS
)
1124 (t vc-default-back-end
))))
1125 (cond ((eq backend
'SCCS
)
1126 (vc-do-command 0 "admin" file
;; SCCS
1127 (and rev
(concat "-r" rev
))
1130 (and comment
(concat "-y" comment
))
1132 (car (rassq 'SCCS vc-master-templates
))
1133 (or (file-name-directory file
) "")
1134 (file-name-nondirectory file
)))
1136 (if vc-keep-workfiles
1137 (vc-do-command 0 "get" file
)))
1139 (vc-do-command 0 "ci" file
;; RCS
1140 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
1141 (and comment
(concat "-t-" comment
))
1144 (message "Registering %s...done" file
)
1147 (defun vc-backend-checkout (file &optional writeable rev
)
1148 ;; Retrieve a copy of a saved version into a workfile
1149 (message "Checking out %s..." file
)
1150 (vc-backend-dispatch file
1152 (vc-do-command 0 "get" file
;; SCCS
1154 (and rev
(concat "-r" (vc-lookup-triple file rev
))))
1156 (vc-do-command 0 "co" file
;; RCS
1158 (and rev
(concat "-r" rev
)))
1160 (vc-file-setprop file
'vc-checkout-time
(nth 5 (file-attributes file
)))
1161 (message "Checking out %s...done" file
)
1164 (defun vc-backend-logentry-check (file)
1165 (vc-backend-dispatch file
1166 (if (>= (- (region-end) (region-beginning)) 512) ;; SCCS
1170 "Log must be less than 512 characters. Point is now at char 512.")))
1174 (defun vc-backend-checkin (file &optional rev comment
)
1175 ;; Register changes to FILE as level REV with explanatory COMMENT.
1176 ;; Automatically retrieves a read-only version of the file with
1177 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1178 ;; it deletes the workfile.
1179 (message "Checking in %s..." file
)
1181 ;; Change buffers to get local value of vc-checkin-switches.
1182 (set-buffer (or (get-file-buffer file
) (current-buffer)))
1183 (vc-backend-dispatch file
1185 (apply 'vc-do-command
0 "delta" file
1186 (if rev
(concat "-r" rev
))
1187 (concat "-y" comment
)
1188 vc-checkin-switches
)
1189 (if vc-keep-workfiles
1190 (vc-do-command 0 "get" file
))
1192 (apply 'vc-do-command
0 "ci" file
1193 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
1194 (concat "-m" comment
)
1195 vc-checkin-switches
)
1197 (vc-file-setprop file
'vc-locking-user nil
)
1198 (message "Checking in %s...done" file
)
1201 (defun vc-backend-revert (file)
1202 ;; Revert file to latest checked-in version.
1203 (message "Reverting %s..." file
)
1204 (vc-backend-dispatch
1207 (vc-do-command 0 "unget" file nil
)
1208 (vc-do-command 0 "get" file nil
))
1210 (delete-file file
) ;; RCS
1211 (vc-do-command 0 "co" file
"-u")))
1212 (vc-file-setprop file
'vc-locking-user nil
)
1213 (message "Reverting %s...done" file
)
1216 (defun vc-backend-steal (file &optional rev
)
1217 ;; Steal the lock on the current workfile. Needs RCS 5.6.2 or later for -M.
1218 (message "Stealing lock on %s..." file
)
1220 (vc-do-command 0 "unget" file
"-n" (if rev
(concat "-r" rev
)))
1221 (vc-do-command 0 "get" file
"-g" (if rev
(concat "-r" rev
)))
1224 (vc-do-command 0 "rcs" "-M" (concat "-u" rev
) file
)
1226 (vc-do-command 0 "rcs" (concat "-l" rev
) file
)
1228 (vc-file-setprop file
'vc-locking-user
(user-login-name))
1229 (message "Stealing lock on %s...done" file
)
1232 (defun vc-backend-uncheck (file target
)
1233 ;; Undo the latest checkin. Note: this code will have to get a lot
1234 ;; smarter when we support multiple branches.
1235 (message "Removing last change from %s..." file
)
1236 (vc-backend-dispatch file
1237 (vc-do-command 0 "rmdel" file
(concat "-r" target
))
1238 (vc-do-command 0 "rcs" file
(concat "-o" target
))
1240 (message "Removing last change from %s...done" file
)
1243 (defun vc-backend-print-log (file)
1244 ;; Print change log associated with FILE to buffer *vc*.
1246 (vc-backend-dispatch file
"prs" "rlog")
1250 (defun vc-backend-assign-name (file name
)
1251 ;; Assign to a FILE's latest version a given NAME.
1252 (vc-backend-dispatch file
1253 (vc-add-triple name file
(vc-latest-version file
)) ;; SCCS
1254 (vc-do-command 0 "rcs" file
(concat "-n" name
":")) ;; RCS
1258 (defun vc-backend-diff (file oldvers
&optional newvers
)
1259 ;; Get a difference report between two versions
1260 (if (eq (vc-backend-deduce file
) 'SCCS
)
1261 (setq oldvers
(vc-lookup-triple file oldvers
))
1262 (setq newvers
(vc-lookup-triple file newvers
)))
1263 (apply 'vc-do-command
1
1264 (or (vc-backend-dispatch file
"vcdiff" "rcsdiff")
1265 (error "File %s is not under version control." file
))
1267 (and oldvers
(concat "-r" oldvers
))
1268 (and newvers
(concat "-r" newvers
))
1272 (defun vc-check-headers ()
1273 "Check if the current file has any headers in it."
1276 (goto-char (point-min))
1277 (vc-backend-dispatch buffer-file-name
1278 (re-search-forward "%[MIRLBSDHTEGUYFPQCZWA]%" nil t
) ;; SCCS
1279 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
) ;; RCS
1283 ;; Back-end-dependent stuff ends here.
1285 ;; Set up key bindings for use while editing log messages
1287 (defun vc-log-mode ()
1288 "Minor mode for driving version-control tools.
1289 These bindings are added to the global keymap when you enter this mode:
1290 \\[vc-next-action] perform next logical version-control operation on current file
1291 \\[vc-register] register current file
1292 \\[vc-toggle-read-only] like next-action, but won't register files
1293 \\[vc-insert-headers] insert version-control headers in current file
1294 \\[vc-print-log] display change history of current file
1295 \\[vc-revert-buffer] revert buffer to latest version
1296 \\[vc-cancel-version] undo latest checkin
1297 \\[vc-diff] show diffs between file versions
1298 \\[vc-directory] show all files locked by any user in or below .
1299 \\[vc-update-change-log] add change log entry from recent checkins
1301 While you are entering a change log message for a version, the following
1302 additional bindings will be in effect.
1304 \\[vc-finish-logentry] proceed with check in, ending log message entry
1306 Whenever you do a checkin, your log comment is added to a ring of
1307 saved comments. These can be recalled as follows:
1309 \\[vc-next-comment] replace region with next message in comment ring
1310 \\[vc-previous-comment] replace region with previous message in comment ring
1311 \\[vc-search-comment-reverse] search backward for regexp in the comment ring
1312 \\[vc-search-comment-forward] search backward for regexp in the comment ring
1314 Entry to the change-log submode calls the value of text-mode-hook, then
1315 the value of vc-log-mode-hook.
1317 Global user options:
1318 vc-initial-comment If non-nil, require user to enter a change
1319 comment upon first checkin of the file.
1321 vc-keep-workfiles Non-nil value prevents workfiles from being
1322 deleted when changes are checked in
1324 vc-suppress-confirm Suppresses some confirmation prompts,
1325 notably for reversions.
1327 vc-diff-options A list consisting of the flags
1328 to be used for generating context diffs.
1330 vc-header-alist Which keywords to insert when adding headers
1331 with \\[vc-insert-headers]. Defaults to
1332 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
1334 vc-static-header-alist By default, version headers inserted in C files
1335 get stuffed in a static string area so that
1336 ident(RCS) or what(SCCS) can see them in the
1337 compiled object code. You can override this
1338 by setting this variable to nil, or change
1339 the header template by changing it.
1341 vc-command-messages if non-nil, display run messages from the
1342 actual version-control utilities (this is
1343 intended primarily for people hacking vc
1347 (set-syntax-table text-mode-syntax-table
)
1348 (use-local-map vc-log-entry-mode
)
1349 (setq local-abbrev-table text-mode-abbrev-table
)
1350 (setq major-mode
'vc-log-mode
)
1351 (setq mode-name
"VC-Log")
1352 (make-local-variable 'vc-log-file
)
1353 (make-local-variable 'vc-log-version
)
1354 (set-buffer-modified-p nil
)
1355 (setq buffer-file-name nil
)
1356 (run-hooks 'text-mode-hook
'vc-log-mode-hook
)
1359 ;; Initialization code, to be done just once at load-time
1360 (if vc-log-entry-mode
1362 (setq vc-log-entry-mode
(make-sparse-keymap))
1363 (define-key vc-log-entry-mode
"\M-n" 'vc-next-comment
)
1364 (define-key vc-log-entry-mode
"\M-p" 'vc-previous-comment
)
1365 (define-key vc-log-entry-mode
"\M-r" 'vc-comment-search-backward
)
1366 (define-key vc-log-entry-mode
"\M-s" 'vc-comment-search-forward
)
1367 (define-key vc-log-entry-mode
"\C-c\C-c" 'vc-finish-logentry
)
1370 ;;; These things should probably be generally available
1372 (defun vc-shrink-to-fit ()
1373 "Shrink a window vertically until it's just large enough to contain its text"
1374 (let ((minsize (1+ (count-lines (point-min) (point-max)))))
1375 (if (< minsize
(window-height))
1376 (let ((window-min-height 2))
1377 (shrink-window (- (window-height) minsize
))))))
1379 (defun vc-file-tree-walk (func &rest args
)
1380 "Walk recursively through default directory,
1381 invoking FUNC f ARGS on all non-directory files f underneath it."
1382 (vc-file-tree-walk-internal default-directory func args
)
1383 (message "Traversing directory %s...done" default-directory
))
1385 (defun vc-file-tree-walk-internal (file func args
)
1386 (if (not (file-directory-p file
))
1387 (apply func file args
)
1388 (message "Traversing directory %s..." file
)
1389 (let ((dir (file-name-as-directory file
)))
1393 (string-equal f
".")
1394 (string-equal f
"..")
1395 (let ((dirf (concat dir f
)))
1397 (file-symlink-p dirf
) ;; Avoid possible loops
1398 (vc-file-tree-walk-internal dirf func args
))))))
1399 (directory-files dir
)))))
1403 ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
1405 ;;; These may be useful to anyone who has to debug or extend the package.
1407 ;;; A fundamental problem in VC is that there are time windows between
1408 ;;; vc-next-action's computations of the file's version-control state and
1409 ;;; the actions that change it. This is a window open to lossage in a
1410 ;;; multi-user environment; someone else could nip in and change the state
1411 ;;; of the master during it.
1413 ;;; The performance problem is that rlog/prs calls are very expensive; we want
1414 ;;; to avoid them as much as possible.
1418 ;;; The performance problem, it turns out, simplifies in practice to the
1419 ;;; problem of making vc-locking-user fast. The two other functions that call
1420 ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
1421 ;;; makes snapshots, the other deletes the calling user's last change in the
1424 ;;; The race condition implies that we have to either (a) lock the master
1425 ;;; during the entire execution of vc-next-action, or (b) detect and
1426 ;;; recover from errors resulting from dispatch on an out-of-date state.
1428 ;;; Alternative (a) appears to be unfeasible. The problem is that we can't
1429 ;;; guarantee that the lock will ever be removed. Suppose a user starts a
1430 ;;; checkin, the change message buffer pops up, and the user, having wandered
1431 ;;; off to do something else, simply forgets about it?
1433 ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
1434 ;;; vc-locking-user. Usually, if a file is registered, we can read its locked/
1435 ;;; unlocked state and its current owner from its permissions.
1437 ;;; This shortcut will fail if someone has manually changed the workfile's
1438 ;;; permissions; also if developers are munging the workfile in several
1439 ;;; directories, with symlinks to a master (in this latter case, the
1440 ;;; permissions shortcut will fail to detect a lock asserted from another
1443 ;;; Note that these cases correspond exactly to the errors which could happen
1444 ;;; because of a competing checkin/checkout race in between two instances of
1447 ;;; For VC's purposes, a workfile/master pair may have the following states:
1449 ;;; A. Unregistered. There is a workfile, there is no master.
1451 ;;; B. Registered and not locked by anyone.
1453 ;;; C. Locked by calling user and unchanged.
1455 ;;; D. Locked by the calling user and changed.
1457 ;;; E. Locked by someone other than the calling user.
1459 ;;; This makes for 25 states and 20 error conditions. Here's the matrix:
1461 ;;; VC's idea of state
1463 ;;; V Actual state RCS action SCCS action Effect
1465 ;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
1466 ;;; B 5 . 6 7 8 co -l get -e checkout
1467 ;;; C 9 10 . 11 12 co -u unget; get revert
1468 ;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
1469 ;;; E 17 18 19 20 . rcs -u -M ; rcs -l unget -n ; get -g steal lock
1471 ;;; All commands take the master file name as a last argument (not shown).
1473 ;;; In the discussion below, a "self-race" is a pathological situation in
1474 ;;; which VC operations are being attempted simultaneously by two or more
1475 ;;; Emacsen running under the same username.
1477 ;;; The vc-next-action code has the following windows:
1480 ;;; Between the check for existence of a master file and the call to
1481 ;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
1482 ;;; never close if the initial-comment feature is on.
1485 ;;; Between the call to vc-workfile-unchanged-p in and the immediately
1486 ;;; following revert (apparent state C).
1489 ;;; Between the call to vc-workfile-unchanged-p in and the following
1490 ;;; checkin (apparent state D). This window may never close.
1493 ;;; Between the unlock and the immediately following checkout during a
1494 ;;; revert operation (apparent state C). Included in window Q.
1497 ;;; Between vc-locking-user and the following checkout (apparent state B).
1500 ;;; Between vc-locking-user and the following revert (apparent state C).
1501 ;;; Includes windows Q and S.
1504 ;;; Between vc-locking-user and the following checkin (apparent state
1505 ;;; D). This window may never be closed if the user fails to complete the
1506 ;;; checkin message. Includes window R.
1509 ;;; Between vc-locking-user and the following steal-lock (apparent
1510 ;;; state E). This window may never cloce if the user fails to complete
1511 ;;; the steal-lock message. Includes window X.
1514 ;;; Between the unlock and the immediately following re-lock during a
1515 ;;; steal-lock operation (apparent state E). This window may never cloce
1516 ;;; if the user fails to complete the steal-lock message.
1520 ;;; Apparent state A ---
1522 ;;; 1. File looked unregistered but is actually registered and not locked.
1524 ;;; Potential cause: someone else's admin during window P, with
1525 ;;; caller's admin happening before their checkout.
1527 ;;; RCS: ci will fail with a "no lock set by <user>" message.
1528 ;;; SCCS: admin will fail with error (ad19).
1530 ;;; We can let these errors be passed up to the user.
1532 ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
1534 ;;; Potential cause: self-race during window P.
1536 ;;; RCS: will revert the file to the last saved version and unlock it.
1537 ;;; SCCS: will fail with error (ad19).
1539 ;;; Either of these consequences is acceptable.
1541 ;;; 3. File looked unregistered but is actually locked by caller, changed.
1543 ;;; Potential cause: self-race during window P.
1545 ;;; RCS: will register the caller's workfile as a delta with a
1546 ;;; null change comment (the -t- switch will be ignored).
1547 ;;; SCCS: will fail with error (ad19).
1549 ;;; 4. File looked unregistered but is locked by someone else.
1551 ;;; Potential cause: someone else's admin during window P, with
1552 ;;; caller's admin happening *after* their checkout.
1554 ;;; RCS: will fail with a "no lock set by <user>" message.
1555 ;;; SCCS: will fail with error (ad19).
1557 ;;; We can let these errors be passed up to the user.
1559 ;;; Apparent state B ---
1561 ;;; 5. File looked registered and not locked, but is actually unregistered.
1563 ;;; Potential cause: master file got nuked during window P.
1565 ;;; RCS: will fail with "RCS/<file>: No such file or directory"
1566 ;;; SCCS: will fail with error ut4.
1568 ;;; We can let these errors be passed up to the user.
1570 ;;; 6. File looked registered and not locked, but is actually locked by the
1571 ;;; calling user and unchanged.
1573 ;;; Potential cause: self-race during window T.
1575 ;;; RCS: in the same directory as the previous workfile, co -l will fail
1576 ;;; with "co error: writable foo exists; checkout aborted". In any other
1577 ;;; directory, checkout will succeed.
1578 ;;; SCCS: will fail with ge17.
1580 ;;; Either of these consequences is acceptable.
1582 ;;; 7. File looked registered and not locked, but is actually locked by the
1583 ;;; calling user and changed.
1587 ;;; 8. File looked registered and not locked, but is actually locked by another
1590 ;;; Potential cause: someone else checks it out during window T.
1592 ;;; RCS: co error: revision 1.3 already locked by <user>
1593 ;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
1595 ;;; We can let these errors be passed up to the user.
1597 ;;; Apparent state C ---
1599 ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
1603 ;;; 10. File looks locked by calling user and unchanged, but is actually not
1606 ;;; Potential cause: a self-race in window U, or by the revert's
1607 ;;; landing during window X of some other user's steal-lock or window S
1608 ;;; of another user's revert.
1610 ;;; RCS: succeeds, refreshing the file from the identical version in
1612 ;;; SCCS: fails with error ut4 (p file nonexistent).
1614 ;;; Either of these consequences is acceptable.
1616 ;;; 11. File is locked by calling user. It looks unchanged, but is actually
1619 ;;; Potential cause: the file would have to be touched by a self-race
1620 ;;; during window Q.
1622 ;;; The revert will succeed, removing whatever changes came with
1623 ;;; the touch. It is theoretically possible that work could be lost.
1625 ;;; 12. File looks like it's locked by the calling user and unchanged, but
1626 ;;; it's actually locked by someone else.
1628 ;;; Potential cause: a steal-lock in window V.
1630 ;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
1631 ;;; SCCS: fails with error un2
1633 ;;; We can pass these errors up to the user.
1635 ;;; Apparent state D ---
1637 ;;; 13. File looks like it's locked by the calling user and changed, but it's
1638 ;;; actually unregistered.
1640 ;;; Potential cause: master file got nuked during window P.
1642 ;;; RCS: Checks in the user's version as an initial delta.
1643 ;;; SCCS: will fail with error ut4.
1645 ;;; This case is kind of nasty. It means VC may fail to detect the
1646 ;;; loss of previous version information.
1648 ;;; 14. File looks like it's locked by the calling user and changed, but it's
1649 ;;; actually unlocked.
1651 ;;; Potential cause: self-race in window V, or the checkin happening
1652 ;;; during the window X of someone else's steal-lock or window S of
1653 ;;; someone else's revert.
1655 ;;; RCS: ci will fail with "no lock set by <user>".
1656 ;;; SCCS: delta will fail with error ut4.
1658 ;;; 15. File looks like it's locked by the calling user and changed, but it's
1659 ;;; actually locked by the calling user and unchanged.
1661 ;;; Potential cause: another self-race --- a whole checkin/checkout
1662 ;;; sequence by the calling user would have to land in window R.
1664 ;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
1665 ;;; RCS: reverts to the file state as of the second user's checkin, leaving
1666 ;;; the file unlocked.
1668 ;;; It is theoretically possible that work could be lost under RCS.
1670 ;;; 16. File looks like it's locked by the calling user and changed, but it's
1671 ;;; actually locked by a different user.
1673 ;;; RCS: ci error: no lock set by <user>
1674 ;;; SCCS: unget will fail with error un2
1676 ;;; We can pass these errors up to the user.
1678 ;;; Apparent state E ---
1680 ;;; 17. File looks like it's locked by some other user, but it's actually
1685 ;;; 18. File looks like it's locked by some other user, but it's actually
1688 ;;; Potential cause: someone released a lock during window W.
1690 ;;; RCS: The calling user will get the lock on the file.
1691 ;;; SCCS: unget -n will fail with cm4.
1693 ;;; Either of these consequences will be OK.
1695 ;;; 19. File looks like it's locked by some other user, but it's actually
1696 ;;; locked by the calling user and unchanged.
1698 ;;; Potential cause: the other user relinquishing a lock followed by
1699 ;;; a self-race, both in window W.
1701 ;;; Under both RCS and SCCS, both unlock and lock will succeed, making
1702 ;;; the sequence a no-op.
1704 ;;; 20. File looks like it's locked by some other user, but it's actually
1705 ;;; locked by the calling user and changed.
1711 ;;; In order of decreasing severity:
1713 ;;; Cases 11 and 15 under RCS are the only one that potentially lose work.
1714 ;;; They would require a self-race for this to happen.
1716 ;;; Case 13 in RCS loses information about previous deltas, retaining
1717 ;;; only the information in the current workfile. This can only happen
1718 ;;; if the master file gets nuked in window P.
1720 ;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
1721 ;;; no change comment in the master. This would require a self-race in
1722 ;;; window P or R respectively.
1724 ;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
1726 ;;; Unfortunately, it appears to me that no recovery is possible in these
1727 ;;; cases. They don't yield error messages, so there's no way to tell that
1728 ;;; a race condition has occurred.
1730 ;;; All other cases don't change either the workfile or the master, and
1731 ;;; trigger command errors which the user will see.
1733 ;;; Thus, there is no explicit recovery code.