(vc-finish-logentry): Sync the buffer in case the user modified it while
[emacs/old-mirror.git] / lisp / vc.el
blob3439b354b24a838e6be53c6f52e3db0d2845c467
1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Version: 5.4
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; This mode is fully documented in the Emacs user's manual.
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 ;; If your site uses the ChangeLog convention supported by Emacs, the
43 ;; function vc-comment-to-change-log should prove a useful checkin hook.
45 ;; This code depends on call-process passing back the subprocess exit
46 ;; status. Thus, you need Emacs 18.58 or later to run it. For the
47 ;; vc-directory command to work properly as documented, you need 19.
48 ;; You also need Emacs 19's ring.el.
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
57 ;; the file.
59 ;;; Code:
61 (require 'vc-hooks)
62 (require 'ring)
64 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
65 (setq minor-mode-alist
66 (cons '(vc-parent-buffer vc-parent-buffer-name)
67 minor-mode-alist)))
69 ;; General customization
71 (defvar vc-default-back-end nil
72 "*Back-end actually used by this interface; may be SCCS or RCS.
73 The value is only computed when needed to avoid an expensive search.")
74 (defvar vc-suppress-confirm nil
75 "*If non-nil, treat user as expert; suppress yes-no prompts on some things.")
76 (defvar vc-keep-workfiles t
77 "*If non-nil, don't delete working files after registering changes.")
78 (defvar vc-initial-comment nil
79 "*Prompt for initial comment when a file is registered.")
80 (defvar vc-command-messages nil
81 "*Display run messages from back-end commands.")
82 (defvar vc-mistrust-permissions 'file-symlink-p
83 "*Don't assume that permissions and ownership track version-control status.")
84 (defvar vc-checkin-switches nil
85 "*Extra switches passed to the checkin program by \\[vc-checkin].")
86 (defvar vc-path
87 (if (file-exists-p "/usr/sccs")
88 '("/usr/sccs") nil)
89 "*List of extra directories to search for version control commands.")
91 (defconst vc-maximum-comment-ring-size 32
92 "Maximum number of saved comments in the comment ring.")
94 ;;; This is duplicated in diff.el.
95 (defvar diff-switches "-c"
96 "*A string or list of strings specifying switches to be be passed to diff.")
98 ;;;###autoload
99 (defvar vc-checkin-hook nil
100 "*List of functions called after a checkin is done. See `run-hooks'.")
102 ;; Header-insertion hair
104 (defvar vc-header-alist
105 '((SCCS "\%W\%") (RCS "\$Id\$"))
106 "*Header keywords to be inserted when `vc-insert-headers' is executed.")
107 (defvar vc-static-header-alist
108 '(("\\.c$" .
109 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
110 "*Associate static header string templates with file types. A \%s in the
111 template is replaced with the first string associated with the file's
112 version-control type in `vc-header-alist'.")
114 (defvar vc-comment-alist
115 '((nroff-mode ".\\\"" ""))
116 "*Special comment delimiters to be used in generating vc headers only.
117 Add an entry in this list if you need to override the normal comment-start
118 and comment-end variables. This will only be necessary if the mode language
119 is sensitive to blank lines.")
121 ;; Default is to be extra careful for super-user.
122 (defvar vc-checkout-carefully (= (user-uid) 0)
123 "*Non-nil means be extra-careful in checkout.
124 Verify that the file really is not locked
125 and that its contents match what the master file says.")
127 ;; Variables the user doesn't need to know about.
128 (defvar vc-log-entry-mode nil)
129 (defvar vc-log-operation nil)
130 (defvar vc-log-after-operation-hook nil)
131 (defvar vc-checkout-writable-buffer-hook 'vc-checkout-writable-buffer)
132 (defvar vc-parent-buffer nil)
133 (defvar vc-parent-buffer-name nil)
135 (defvar vc-log-file)
136 (defvar vc-log-version)
138 (defconst vc-name-assoc-file "VC-names")
140 (defvar vc-dired-mode nil)
141 (make-variable-buffer-local 'vc-dired-mode)
143 (defvar vc-comment-ring nil)
144 (defvar vc-comment-ring-index nil)
145 (defvar vc-last-comment-match nil)
147 ;; File property caching
149 (defun vc-file-clearprops (file)
150 ;; clear all properties of a given file
151 (setplist (intern file vc-file-prop-obarray) nil))
153 (defun vc-clear-context ()
154 "Clear all cached file properties and the comment ring."
155 (interactive)
156 (fillarray vc-file-prop-obarray nil)
157 ;; Note: there is potential for minor lossage here if there is an open
158 ;; log buffer with a nonzero local value of vc-comment-ring-index.
159 (setq vc-comment-ring nil))
161 ;; Random helper functions
163 (defun vc-registration-error (file)
164 (if file
165 (error "File %s is not under version control" file)
166 (error "Buffer %s is not associated with a file" (buffer-name))))
168 (defvar vc-binary-assoc nil)
170 (defun vc-find-binary (name)
171 "Look for a command anywhere on the subprocess-command search path."
172 (or (cdr (assoc name vc-binary-assoc))
173 (catch 'found
174 (mapcar
175 (function
176 (lambda (s)
177 (if s
178 (let ((full (concat s "/" name)))
179 (if (file-executable-p full)
180 (progn
181 (setq vc-binary-assoc
182 (cons (cons name full) vc-binary-assoc))
183 (throw 'found full)))))))
184 exec-path)
185 nil)))
187 (defun vc-do-command (okstatus command file &rest flags)
188 "Execute a version-control command, notifying user and checking for errors.
189 The command is successful if its exit status does not exceed OKSTATUS.
190 Output from COMMAND goes to buffer *vc*. The last argument of the command is
191 the master name of FILE; this is appended to an optional list of FLAGS."
192 (setq file (expand-file-name file))
193 (if vc-command-messages
194 (message "Running %s on %s..." command file))
195 (let ((obuf (current-buffer)) (camefrom (current-buffer))
196 (squeezed nil)
197 (vc-file (and file (vc-name file)))
198 status)
199 (set-buffer (get-buffer-create "*vc*"))
200 (set (make-local-variable 'vc-parent-buffer) camefrom)
201 (set (make-local-variable 'vc-parent-buffer-name)
202 (concat " from " (buffer-name camefrom)))
204 (erase-buffer)
206 ;; This is so that command arguments typed in the *vc* buffer will
207 ;; have reasonable defaults.
208 (setq default-directory (file-name-directory file))
210 (mapcar
211 (function (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
212 flags)
213 (if vc-file
214 (setq squeezed (append squeezed (list vc-file))))
215 (let ((default-directory (file-name-directory (or file "./")))
216 (exec-path (if vc-path (append exec-path vc-path) exec-path)))
217 (setq status (apply 'call-process command nil t nil squeezed)))
218 (goto-char (point-max))
219 (forward-line -1)
220 (if (or (not (integerp status)) (< okstatus status))
221 (progn
222 (pop-to-buffer "*vc*")
223 (goto-char (point-min))
224 (shrink-window-if-larger-than-buffer)
225 (error "Running %s...FAILED (%s)" command
226 (if (integerp status)
227 (format "status %d" status)
228 status))
230 (if vc-command-messages
231 (message "Running %s...OK" command))
233 (set-buffer obuf)
234 status)
237 ;;; Save a bit of the text around POSN in the current buffer, to help
238 ;;; us find the corresponding position again later. This works even
239 ;;; if all markers are destroyed or corrupted.
240 (defun vc-position-context (posn)
241 (list posn
242 (buffer-size)
243 (buffer-substring posn
244 (min (point-max) (+ posn 100)))))
246 ;;; Return the position of CONTEXT in the current buffer, or nil if we
247 ;;; couldn't find it.
248 (defun vc-find-position-by-context (context)
249 (let ((context-string (nth 2 context)))
250 (if (equal "" context-string)
251 (point-max)
252 (save-excursion
253 (let ((diff (- (nth 1 context) (buffer-size))))
254 (if (< diff 0) (setq diff (- diff)))
255 (goto-char (nth 0 context))
256 (if (or (search-forward context-string nil t)
257 ;; Can't use search-backward since the match may continue
258 ;; after point.
259 (progn (goto-char (- (point) diff (length context-string)))
260 ;; goto-char doesn't signal an error at
261 ;; beginning of buffer like backward-char would
262 (search-forward context-string nil t)))
263 ;; to beginning of OSTRING
264 (- (point) (length context-string))))))))
266 (defun vc-revert-buffer1 (&optional arg no-confirm)
267 ;; Most of this was shamelessly lifted from Sebastian Kremer's rcs.el mode.
268 ;; Revert buffer, try to keep point and mark where user expects them in spite
269 ;; of changes because of expanded version-control key words.
270 ;; This is quite important since otherwise typeahead won't work as expected.
271 (interactive "P")
272 (widen)
273 (let ((point-context (vc-position-context (point)))
274 ;; Use mark-marker to avoid confusion in transient-mark-mode.
275 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
276 (vc-position-context (mark-marker))))
277 ;; Make the right thing happen in transient-mark-mode.
278 (mark-active nil)
279 ;; We may want to reparse the compilation buffer after revert
280 (reparse (and (boundp 'compilation-error-list) ;compile loaded
281 (let ((curbuf (current-buffer)))
282 ;; Construct a list; each elt is nil or a buffer
283 ;; iff that buffer is a compilation output buffer
284 ;; that contains markers into the current buffer.
285 (save-excursion
286 (mapcar (function
287 (lambda (buffer)
288 (set-buffer buffer)
289 (let ((errors (or
290 compilation-old-error-list
291 compilation-error-list))
292 (buffer-error-marked-p nil))
293 (while (and (consp errors)
294 (not buffer-error-marked-p))
295 (and (markerp (cdr (car errors)))
296 (eq buffer
297 (marker-buffer
298 (cdr (car errors))))
299 (setq buffer-error-marked-p t))
300 (setq errors (cdr errors)))
301 (if buffer-error-marked-p buffer))))
302 (buffer-list)))))))
304 ;; the actual revisit
305 (revert-buffer arg no-confirm)
307 ;; Reparse affected compilation buffers.
308 (while reparse
309 (if (car reparse)
310 (save-excursion
311 (set-buffer (car reparse))
312 (let ((compilation-last-buffer (current-buffer)) ;select buffer
313 ;; Record the position in the compilation buffer of
314 ;; the last error next-error went to.
315 (error-pos (marker-position
316 (car (car-safe compilation-error-list)))))
317 ;; Reparse the error messages as far as they were parsed before.
318 (compile-reinitialize-errors '(4) compilation-parsing-end)
319 ;; Move the pointer up to find the error we were at before
320 ;; reparsing. Now next-error should properly go to the next one.
321 (while (and compilation-error-list
322 (/= error-pos (car (car compilation-error-list))))
323 (setq compilation-error-list (cdr compilation-error-list))))))
324 (setq reparse (cdr reparse)))
326 ;; Restore point and mark
327 (let ((new-point (vc-find-position-by-context point-context)))
328 (if new-point (goto-char new-point)))
329 (if mark-context
330 (let ((new-mark (vc-find-position-by-context mark-context)))
331 (if new-mark (set-mark new-mark))))))
334 (defun vc-buffer-sync (&optional not-urgent)
335 ;; Make sure the current buffer and its working file are in sync
336 ;; NOT-URGENT means it is ok to continue if the user says not to save.
337 (if (buffer-modified-p)
338 (if (or vc-suppress-confirm
339 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
340 (save-buffer)
341 (if not-urgent
343 (error "Aborted")))))
346 (defun vc-workfile-unchanged-p (file &optional want-differences-if-changed)
347 ;; Has the given workfile changed since last checkout?
348 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
349 (lastmod (nth 5 (file-attributes file))))
350 (or (equal checkout-time lastmod)
351 (and (or (not checkout-time) want-differences-if-changed)
352 (let ((unchanged (zerop (vc-backend-diff file nil nil
353 (not want-differences-if-changed)))))
354 ;; 0 stands for an unknown time; it can't match any mod time.
355 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
356 unchanged)))))
358 (defun vc-next-action-on-file (file verbose &optional comment)
359 ;;; If comment is specified, it will be used as an admin or checkin comment.
360 (let (owner version (vc-file (vc-name file)))
361 (cond
363 ;; if there is no master file corresponding, create one
364 ((not vc-file)
365 (vc-register verbose comment)
366 (if vc-initial-comment
367 (setq vc-log-after-operation-hook
368 'vc-checkout-writable-buffer-hook)
369 (vc-checkout-writable-buffer file)))
371 ;; if there is no lock on the file, assert one and get it
372 ((not (setq owner (vc-locking-user file)))
373 (if (and vc-checkout-carefully
374 (not (vc-workfile-unchanged-p file t)))
375 (if (save-window-excursion
376 (pop-to-buffer "*vc*")
377 (goto-char (point-min))
378 (insert-string (format "Changes to %s since last lock:\n\n"
379 file))
380 (not (beep))
381 (yes-or-no-p
382 (concat "File has unlocked changes, "
383 "claim lock retaining changes? ")))
384 (progn (vc-backend-steal file)
385 (vc-mode-line file))
386 (if (not (yes-or-no-p "Revert to checked-in version, instead? "))
387 (error "Checkout aborted.")
388 (vc-revert-buffer1 t t)
389 (vc-checkout-writable-buffer file))
391 (vc-checkout-writable-buffer file)))
393 ;; a checked-out version exists, but the user may not own the lock
394 ((not (string-equal owner (user-login-name)))
395 (if comment
396 (error "Sorry, you can't steal the lock on %s this way" file))
397 (vc-steal-lock
398 file
399 (and verbose (read-string "Version to steal: "))
400 owner))
402 ;; OK, user owns the lock on the file
404 (find-file file)
406 ;; give luser a chance to save before checking in.
407 (vc-buffer-sync)
409 ;; Revert if file is unchanged and buffer is too.
410 ;; If buffer is modified, that means the user just said no
411 ;; to saving it; in that case, don't revert,
412 ;; because the user might intend to save
413 ;; after finishing the log entry.
414 (if (and (vc-workfile-unchanged-p file)
415 (not (buffer-modified-p)))
416 (progn
417 (vc-backend-revert file)
418 ;; DO NOT revert the file without asking the user!
419 (vc-resynch-window file t nil))
421 ;; user may want to set nonstandard parameters
422 (if verbose
423 (setq version (read-string "New version level: ")))
425 ;; OK, let's do the checkin
426 (vc-checkin file version comment)
427 )))))
429 (defun vc-next-action-dired (file rev comment)
430 ;; We've accepted a log comment, now do a vc-next-action using it on all
431 ;; marked files.
432 (set-buffer vc-parent-buffer)
433 (dired-map-over-marks
434 (save-window-excursion
435 (let ((file (dired-get-filename)))
436 (message "Processing %s..." file)
437 (vc-next-action-on-file file nil comment)
438 (message "Processing %s...done" file)))
439 nil t)
442 ;; Here's the major entry point.
444 ;;;###autoload
445 (defun vc-next-action (verbose)
446 "Do the next logical checkin or checkout operation on the current file.
447 If the file is not already registered, this registers it for version
448 control and then retrieves a writable, locked copy for editing.
449 If the file is registered and not locked by anyone, this checks out
450 a writable and locked file ready for editing.
451 If the file is checked out and locked by the calling user, this
452 first checks to see if the file has changed since checkout. If not,
453 it performs a revert.
454 If the file has been changed, this pops up a buffer for entry
455 of a log message; when the message has been entered, it checks in the
456 resulting changes along with the log message as change commentary. If
457 the variable `vc-keep-workfiles' is non-nil (which is its default), a
458 read-only copy of the changed file is left in place afterwards.
459 If the file is registered and locked by someone else, you are given
460 the option to steal the lock.
461 If you call this from within a VC dired buffer with no files marked,
462 it will operate on the file in the current line.
463 If you call this from within a VC dired buffer, and one or more
464 files are marked, it will accept a log message and then operate on
465 each one. The log message will be used as a comment for any register
466 or checkin operations, but ignored when doing checkouts. Attempted
467 lock steals will raise an error.
469 For checkin, a prefix argument lets you specify the version number to use."
470 (interactive "P")
471 (catch 'nogo
472 (if vc-dired-mode
473 (let ((files (dired-get-marked-files)))
474 (if (= (length files) 1)
475 (find-file-other-window (dired-get-filename))
476 (vc-start-entry nil nil nil
477 "Enter a change comment for the marked files."
478 'vc-next-action-dired)
479 (throw 'nogo nil))))
480 (while vc-parent-buffer
481 (pop-to-buffer vc-parent-buffer))
482 (if buffer-file-name
483 (vc-next-action-on-file buffer-file-name verbose)
484 (vc-registration-error nil))))
486 ;;; These functions help the vc-next-action entry point
488 (defun vc-checkout-writable-buffer (&optional file)
489 "Retrieve a writable copy of the latest version of the current buffer's file."
490 (vc-checkout (or file (buffer-file-name)) t)
493 ;;;###autoload
494 (defun vc-register (&optional override comment)
495 "Register the current file into your version-control system."
496 (interactive "P")
497 (if (vc-name buffer-file-name)
498 (error "This file is already registered"))
499 ;; Watch out for new buffers of size 0: the corresponding file
500 ;; does not exist yet, even though buffer-modified-p is nil.
501 (if (and (not (buffer-modified-p))
502 (zerop (buffer-size))
503 (not (file-exists-p buffer-file-name)))
504 (set-buffer-modified-p t))
505 (vc-buffer-sync)
506 (vc-admin
507 buffer-file-name
508 (and override
509 (read-string
510 (format "Initial version level for %s: " buffer-file-name))))
513 (defun vc-resynch-window (file &optional keep noquery)
514 ;; If the given file is in the current buffer,
515 ;; either revert on it so we see expanded keyworks,
516 ;; or unvisit it (depending on vc-keep-workfiles)
517 ;; NOQUERY if non-nil inhibits confirmation for reverting.
518 ;; NOQUERY should be t *only* if it is known the only difference
519 ;; between the buffer and the file is due to RCS rather than user editing!
520 (and (string= buffer-file-name file)
521 (if keep
522 (progn
523 (vc-revert-buffer1 t noquery)
524 (vc-mode-line buffer-file-name))
525 (progn
526 (delete-window)
527 (kill-buffer (current-buffer))))))
529 (defun vc-start-entry (file rev comment msg action)
530 ;; Accept a comment for an operation on FILE revision REV. If COMMENT
531 ;; is nil, pop up a VC-log buffer, emit MSG, and set the
532 ;; action on close to ACTION; otherwise, do action immediately.
533 ;; Remember the file's buffer in parent-buffer (current one if no file).
534 (let ((parent (if file (find-file-noselect file) (current-buffer))))
535 (if comment
536 (set-buffer (get-buffer-create "*VC-log*"))
537 (pop-to-buffer (get-buffer-create "*VC-log*")))
538 (set (make-local-variable 'vc-parent-buffer) parent)
539 (set (make-local-variable 'vc-parent-buffer-name)
540 (concat " from " (buffer-name vc-parent-buffer)))
541 (vc-mode-line (or file " (no file)"))
542 (vc-log-mode)
543 (setq vc-log-operation action)
544 (setq vc-log-file file)
545 (setq vc-log-version rev)
546 (if comment
547 (progn
548 (erase-buffer)
549 (if (eq comment t)
550 (vc-finish-logentry t)
551 (insert comment)
552 (vc-finish-logentry nil)))
553 (message "%s Type C-c C-c when done." msg))))
555 (defun vc-admin (file rev &optional comment)
556 "Check a file into your version-control system.
557 FILE is the unmodified name of the file. REV should be the base version
558 level to check it in under. COMMENT, if specified, is the checkin comment."
559 (vc-start-entry file rev
560 (or comment (not vc-initial-comment))
561 "Enter initial comment." 'vc-backend-admin))
563 (defun vc-checkout (file &optional writable)
564 "Retrieve a copy of the latest version of the given file."
565 ;; If ftp is on this system and the name matches the ange-ftp format
566 ;; for a remote file, the user is trying something that won't work.
567 (if (and (string-match "^/[^/:]+:" file) (vc-find-binary "ftp"))
568 (error "Sorry, you can't check out files over FTP"))
569 (vc-backend-checkout file writable)
570 (if (string-equal file buffer-file-name)
571 (vc-resynch-window file t t))
574 (defun vc-steal-lock (file rev &optional owner)
575 "Steal the lock on the current workfile."
576 (interactive)
577 (if (not owner)
578 (setq owner (vc-locking-user file)))
579 (if (not (y-or-n-p (format "Take the lock on %s:%s from %s? " file rev owner)))
580 (error "Steal cancelled"))
581 (pop-to-buffer (get-buffer-create "*VC-mail*"))
582 (setq default-directory (expand-file-name "~/"))
583 (auto-save-mode auto-save-default)
584 (mail-mode)
585 (erase-buffer)
586 (mail-setup owner (format "%s:%s" file rev) nil nil nil
587 (list (list 'vc-finish-steal file rev)))
588 (goto-char (point-max))
589 (insert
590 (format "I stole the lock on %s:%s, " file rev)
591 (current-time-string)
592 ".\n")
593 (message "Please explain why you stole the lock. Type C-c C-c when done."))
595 ;; This is called when the notification has been sent.
596 (defun vc-finish-steal (file version)
597 (vc-backend-steal file version)
598 (vc-resynch-window file t t))
600 (defun vc-checkin (file &optional rev comment)
601 "Check in the file specified by FILE.
602 The optional argument REV may be a string specifying the new version level
603 \(if nil increment the current level). The file is either retained with write
604 permissions zeroed, or deleted (according to the value of `vc-keep-workfiles').
605 COMMENT is a comment string; if omitted, a buffer is
606 popped up to accept a comment."
607 (setq vc-log-after-operation-hook 'vc-checkin-hook)
608 (vc-start-entry file rev comment "Enter a change comment." 'vc-backend-checkin))
610 ;;; Here is a checkin hook that may prove useful to sites using the
611 ;;; ChangeLog facility supported by Emacs.
612 (defun vc-comment-to-change-log (&optional whoami file-name)
613 "Enter last VC comment into change log file for current buffer's file.
614 Optional arg (interactive prefix) non-nil means prompt for user name and site.
615 Second arg is file name of change log. \
616 If nil, uses `change-log-default-name'."
617 (interactive (if current-prefix-arg
618 (list current-prefix-arg
619 (prompt-for-change-log-name))))
620 (let (;; Extract the comment first so we get any error before doing anything.
621 (comment (ring-ref vc-comment-ring 0))
622 ;; Don't let add-change-log-entry insert a defun name.
623 (add-log-current-defun-function 'ignore)
624 end)
625 ;; Call add-log to do half the work.
626 (add-change-log-entry whoami file-name t t)
627 ;; Insert the VC comment, leaving point before it.
628 (setq end (save-excursion (insert comment) (point-marker)))
629 (if (looking-at "\\s *\\s(")
630 ;; It starts with an open-paren, as in "(foo): Frobbed."
631 ;; So remove the ": " add-log inserted.
632 (delete-char -2))
633 ;; Canonicalize the white space between the file name and comment.
634 (just-one-space)
635 ;; Indent rest of the text the same way add-log indented the first line.
636 (let ((indentation (current-indentation)))
637 (save-excursion
638 (while (< (point) end)
639 (forward-line 1)
640 (indent-to indentation))
641 (setq end (point))))
642 ;; Fill the inserted text, preserving open-parens at bol.
643 (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
644 (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
645 (beginning-of-line)
646 (fill-region (point) end))
647 ;; Canonicalize the white space at the end of the entry so it is
648 ;; separated from the next entry by a single blank line.
649 (skip-syntax-forward " " end)
650 (delete-char (- (skip-syntax-backward " ")))
651 (or (eobp) (looking-at "\n\n")
652 (insert "\n"))))
655 (defun vc-finish-logentry (&optional nocomment)
656 "Complete the operation implied by the current log entry."
657 (interactive)
658 ;; Check and record the comment, if any.
659 (if (not nocomment)
660 (progn
661 (goto-char (point-max))
662 (if (not (bolp))
663 (newline))
664 ;; Comment too long?
665 (vc-backend-logentry-check vc-log-file)
666 ;; Record the comment in the comment ring
667 (if (null vc-comment-ring)
668 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
669 (ring-insert vc-comment-ring (buffer-string))
671 ;; Sync parent buffer in case the user modified it while editing the comment.
672 (save-excursion
673 (set-buffer vc-parent-buffer)
674 (vc-buffer-sync))
675 ;; OK, do it to it
676 (if vc-log-operation
677 (save-excursion
678 (funcall vc-log-operation
679 vc-log-file
680 vc-log-version
681 (buffer-string)))
682 (error "No log operation is pending"))
683 ;; Return to "parent" buffer of this checkin and remove checkin window
684 (pop-to-buffer vc-parent-buffer)
685 (let ((logbuf (get-buffer "*VC-log*")))
686 (delete-windows-on logbuf)
687 (kill-buffer logbuf))
688 ;; Now make sure we see the expanded headers
689 (if buffer-file-name
690 (vc-resynch-window buffer-file-name vc-keep-workfiles t))
691 (run-hooks vc-log-after-operation-hook))
693 ;; Code for access to the comment ring
695 (defun vc-previous-comment (arg)
696 "Cycle backwards through comment history."
697 (interactive "*p")
698 (let ((len (ring-length vc-comment-ring)))
699 (cond ((<= len 0)
700 (message "Empty comment ring")
701 (ding))
703 (erase-buffer)
704 ;; Initialize the index on the first use of this command
705 ;; so that the first M-p gets index 0, and the first M-n gets
706 ;; index -1.
707 (if (null vc-comment-ring-index)
708 (setq vc-comment-ring-index
709 (if (> arg 0) -1
710 (if (< arg 0) 1 0))))
711 (setq vc-comment-ring-index
712 (mod (+ vc-comment-ring-index arg) len))
713 (message "%d" (1+ vc-comment-ring-index))
714 (insert (ring-ref vc-comment-ring vc-comment-ring-index))))))
716 (defun vc-next-comment (arg)
717 "Cycle forwards through comment history."
718 (interactive "*p")
719 (vc-previous-comment (- arg)))
721 (defun vc-comment-search-reverse (str)
722 "Searches backwards through comment history for substring match."
723 (interactive "sComment substring: ")
724 (if (string= str "")
725 (setq str vc-last-comment-match)
726 (setq vc-last-comment-match str))
727 (if (null vc-comment-ring-index)
728 (setq vc-comment-ring-index -1))
729 (let ((str (regexp-quote str))
730 (len (ring-length vc-comment-ring))
731 (n (1+ vc-comment-ring-index)))
732 (while (and (< n len) (not (string-match str (ring-ref vc-comment-ring n))))
733 (setq n (+ n 1)))
734 (cond ((< n len)
735 (vc-previous-comment (- n vc-comment-ring-index)))
736 (t (error "Not found")))))
738 (defun vc-comment-search-forward (str)
739 "Searches forwards through comment history for substring match."
740 (interactive "sComment substring: ")
741 (if (string= str "")
742 (setq str vc-last-comment-match)
743 (setq vc-last-comment-match str))
744 (if (null vc-comment-ring-index)
745 (setq vc-comment-ring-index 0))
746 (let ((str (regexp-quote str))
747 (len (ring-length vc-comment-ring))
748 (n vc-comment-ring-index))
749 (while (and (>= n 0) (not (string-match str (ring-ref vc-comment-ring n))))
750 (setq n (- n 1)))
751 (cond ((>= n 0)
752 (vc-next-comment (- n vc-comment-ring-index)))
753 (t (error "Not found")))))
755 ;; Additional entry points for examining version histories
757 ;;;###autoload
758 (defun vc-diff (historic &optional not-urgent)
759 "Display diffs between file versions.
760 Normally this compares the current file and buffer with the most recent
761 checked in version of that file. This uses no arguments.
762 With a prefix argument, it reads the file name to use
763 and two version designators specifying which versions to compare."
764 (interactive "P")
765 (if vc-dired-mode
766 (set-buffer (find-file-noselect (dired-get-filename))))
767 (while vc-parent-buffer
768 (pop-to-buffer vc-parent-buffer))
769 (if historic
770 (call-interactively 'vc-version-diff)
771 (if (or (null buffer-file-name) (null (vc-name buffer-file-name)))
772 (error
773 "There is no version-control master associated with this buffer"))
774 (let ((file buffer-file-name)
775 unchanged)
776 (or (and file (vc-name file))
777 (vc-registration-error file))
778 (vc-buffer-sync not-urgent)
779 (setq unchanged (vc-workfile-unchanged-p buffer-file-name))
780 (if unchanged
781 (message "No changes to %s since latest version." file)
782 (vc-backend-diff file)
783 ;; Ideally, we'd like at this point to parse the diff so that
784 ;; the buffer effectively goes into compilation mode and we
785 ;; can visit the old and new change locations via next-error.
786 ;; Unfortunately, this is just too painful to do. The basic
787 ;; problem is that the `old' file doesn't exist to be
788 ;; visited. This plays hell with numerous assumptions in
789 ;; the diff.el and compile.el machinery.
790 (pop-to-buffer "*vc*")
791 (pop-to-buffer "*vc*")
792 (if (= 0 (buffer-size))
793 (progn
794 (setq unchanged t)
795 (message "No changes to %s since latest version." file))
796 (goto-char (point-min))
797 (shrink-window-if-larger-than-buffer)))
798 (not unchanged))))
800 (defun vc-version-diff (file rel1 rel2)
801 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
802 If FILE is a directory, generate diffs between versions for all registered
803 files in or below it."
804 (interactive "FFile or directory to diff: \nsOlder version: \nsNewer version: ")
805 (if (string-equal rel1 "") (setq rel1 nil))
806 (if (string-equal rel2 "") (setq rel2 nil))
807 (if (file-directory-p file)
808 (let ((camefrom (current-buffer)))
809 (set-buffer (get-buffer-create "*vc-status*"))
810 (set (make-local-variable 'vc-parent-buffer) camefrom)
811 (set (make-local-variable 'vc-parent-buffer-name)
812 (concat " from " (buffer-name camefrom)))
813 (erase-buffer)
814 (insert "Diffs between "
815 (or rel1 "last version checked in")
816 " and "
817 (or rel2 "current workfile(s)")
818 ":\n\n")
819 (set-buffer (get-buffer-create "*vc*"))
820 (cd file)
821 (vc-file-tree-walk
822 (function (lambda (f)
823 (message "Looking at %s" f)
824 (and
825 (not (file-directory-p f))
826 (vc-registered f)
827 (vc-backend-diff f rel1 rel2)
828 (append-to-buffer "*vc-status*" (point-min) (point-max)))
830 (pop-to-buffer "*vc-status*")
831 (insert "\nEnd of diffs.\n")
832 (goto-char (point-min))
833 (set-buffer-modified-p nil)
835 (if (zerop (vc-backend-diff file rel1 rel2))
836 (message "No changes to %s between %s and %s." file rel1 rel2)
837 (pop-to-buffer "*vc*"))))
839 ;;;###autoload
840 (defun vc-version-other-window (rev)
841 "Visit version REV of the current buffer in another window.
842 If the current buffer is named `F', the version is named `F.~REV~'.
843 If `F.~REV~' already exists, it is used instead of being re-created."
844 (interactive "sVersion to visit (default is latest version): ")
845 (if vc-dired-mode
846 (set-buffer (find-file-noselect (dired-get-filename))))
847 (while vc-parent-buffer
848 (pop-to-buffer vc-parent-buffer))
849 (if (and buffer-file-name (vc-name buffer-file-name))
850 (let* ((version (if (string-equal rev "")
851 (vc-latest-version buffer-file-name)
852 rev))
853 (filename (concat buffer-file-name ".~" version "~")))
854 (or (file-exists-p filename)
855 (vc-backend-checkout buffer-file-name nil version filename))
856 (find-file-other-window filename))
857 (vc-registration-error buffer-file-name)))
859 ;; Header-insertion code
861 ;;;###autoload
862 (defun vc-insert-headers ()
863 "Insert headers in a file for use with your version-control system.
864 Headers desired are inserted at the start of the buffer, and are pulled from
865 the variable `vc-header-alist'."
866 (interactive)
867 (if vc-dired-mode
868 (find-file-other-window (dired-get-filename)))
869 (while vc-parent-buffer
870 (pop-to-buffer vc-parent-buffer))
871 (save-excursion
872 (save-restriction
873 (widen)
874 (if (or (not (vc-check-headers))
875 (y-or-n-p "Version headers already exist. Insert another set? "))
876 (progn
877 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
878 (comment-start-vc (or (car delims) comment-start "#"))
879 (comment-end-vc (or (car (cdr delims)) comment-end ""))
880 (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist))))
881 (mapcar (function (lambda (s)
882 (insert comment-start-vc "\t" s "\t"
883 comment-end-vc "\n")))
884 hdstrings)
885 (if vc-static-header-alist
886 (mapcar (function (lambda (f)
887 (if (string-match (car f) buffer-file-name)
888 (insert (format (cdr f) (car hdstrings))))))
889 vc-static-header-alist))
891 )))))
893 ;; The VC directory submode. Coopt Dired for this.
894 ;; All VC commands get mapped into logical equivalents.
896 (defvar vc-dired-prefix-map (make-sparse-keymap))
897 (define-key vc-dired-prefix-map "\C-xv" vc-prefix-map)
899 (or (not (boundp 'minor-mode-map-alist))
900 (assq 'vc-dired-mode minor-mode-map-alist)
901 (setq minor-mode-map-alist
902 (cons (cons 'vc-dired-mode vc-dired-prefix-map)
903 minor-mode-map-alist)))
905 (defun vc-dired-mode ()
906 "The augmented Dired minor mode used in VC directory buffers.
907 All Dired commands operate normally. Users currently locking listed files
908 are listed in place of the file's owner and group.
909 Keystrokes bound to VC commands will execute as though they had been called
910 on a buffer attached to the file named in the current Dired buffer line."
911 (setq vc-dired-mode t)
912 (setq vc-mode " under VC"))
914 (defun vc-dired-reformat-line (x)
915 ;; Hack a directory-listing line, plugging in locking-user info in
916 ;; place of the user and group info. Should have the beneficial
917 ;; side-effect of shortening the listing line. Each call starts with
918 ;; point immediately following the dired mark area on the line to be
919 ;; hacked.
921 ;; Simplest possible one:
922 ;; (insert (concat x "\t")))
924 ;; This code, like dired, assumes UNIX -l format.
925 (forward-word 1) ;; skip over any extra field due to -ibs options
926 (if x (setq x (concat "(" x ")")))
927 (if (re-search-forward "\\([0-9]+ \\).................\\( .*\\)" nil 0)
928 (let ((rep (substring (concat x " ") 0 9)))
929 (replace-match (concat "\\1" rep "\\2") t)))
932 ;;; Note in Emacs 18 the following defun gets overridden
933 ;;; with the symbol 'vc-directory-18. See below.
934 ;;;###autoload
935 (defun vc-directory (verbose)
936 "Show version-control status of all files under the current directory."
937 (interactive "P")
938 (let (nonempty
939 (dl (length default-directory))
940 (filelist nil) (userlist nil)
941 dired-buf
942 dired-buf-mod-count)
943 (vc-file-tree-walk
944 (function (lambda (f)
945 (if (vc-registered f)
946 (let ((user (vc-locking-user f)))
947 (and (or verbose user)
948 (setq filelist (cons (substring f dl) filelist))
949 (setq userlist (cons user userlist))))))))
950 (save-excursion
951 ;; This uses a semi-documented feature of dired; giving a switch
952 ;; argument forces the buffer to refresh each time.
953 (dired
954 (cons default-directory (nreverse filelist))
955 dired-listing-switches)
956 (setq dired-buf (current-buffer))
957 (setq nonempty (not (zerop (buffer-size)))))
958 (if nonempty
959 (progn
960 (pop-to-buffer dired-buf)
961 (vc-dired-mode)
962 (goto-char (point-min))
963 (setq buffer-read-only nil)
964 (forward-line 1) ;; Skip header line
965 (mapcar
966 (function
967 (lambda (x)
968 (forward-char 2) ;; skip dired's mark area
969 (vc-dired-reformat-line x)
970 (forward-line 1))) ;; go to next line
971 (nreverse userlist))
972 (setq buffer-read-only t)
973 (goto-char (point-min))
975 (message "No files are currently %s under %s"
976 (if verbose "registered" "locked") default-directory))
979 ;; Emacs 18 version
980 (defun vc-directory-18 (verbose)
981 "Show version-control status of all files under the current directory."
982 (interactive "P")
983 (let (nonempty (dir default-directory))
984 (save-excursion
985 (set-buffer (get-buffer-create "*vc-status*"))
986 (erase-buffer)
987 (cd dir)
988 (vc-file-tree-walk
989 (function (lambda (f)
990 (if (vc-registered f)
991 (let ((user (vc-locking-user f)))
992 (if (or user verbose)
993 (insert (format
994 "%s %s\n"
995 (concat user) f))))))))
996 (setq nonempty (not (zerop (buffer-size)))))
997 (if nonempty
998 (progn
999 (pop-to-buffer "*vc-status*" t)
1000 (goto-char (point-min))
1001 (shrink-window-if-larger-than-buffer)))
1002 (message "No files are currently %s under %s"
1003 (if verbose "registered" "locked") default-directory))
1006 (or (boundp 'minor-mode-map-alist)
1007 (fset 'vc-directory 'vc-directory-18))
1009 ; Emacs 18 also lacks these.
1010 (or (boundp 'compilation-old-error-list)
1011 (setq compilation-old-error-list nil))
1013 ;; Named-configuration support for SCCS
1015 (defun vc-add-triple (name file rev)
1016 (save-excursion
1017 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
1018 (goto-char (point-max))
1019 (insert name "\t:\t" file "\t" rev "\n")
1020 (basic-save-buffer)
1021 (kill-buffer (current-buffer))
1024 (defun vc-record-rename (file newname)
1025 (save-excursion
1026 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
1027 (goto-char (point-min))
1028 ;; (replace-regexp (concat ":" (regexp-quote file) "$") (concat ":" newname))
1029 (while (re-search-forward (concat ":" (regexp-quote file) "$") nil t)
1030 (replace-match (concat ":" newname) nil nil))
1031 (basic-save-buffer)
1032 (kill-buffer (current-buffer))
1035 (defun vc-lookup-triple (file name)
1036 ;; Return the numeric version corresponding to a named snapshot of file
1037 ;; If name is nil or a version number string it's just passed through
1038 (cond ((null name) "")
1039 ((let ((firstchar (aref name 0)))
1040 (and (>= firstchar ?0) (<= firstchar ?9)))
1041 name)
1043 (car (vc-master-info
1044 (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file)
1045 (list (concat name "\t:\t" file "\t\\(.+\\)"))))
1048 ;; Named-configuration entry points
1050 (defun vc-locked-example ()
1051 ;; Return an example of why the current directory is not ready to be snapshot
1052 ;; or nil if no such example exists.
1053 (catch 'vc-locked-example
1054 (vc-file-tree-walk
1055 (function (lambda (f)
1056 (if (and (vc-registered f) (vc-locking-user f))
1057 (throw 'vc-locked-example f)))))
1058 nil))
1060 ;;;###autoload
1061 (defun vc-create-snapshot (name)
1062 "Make a snapshot called NAME.
1063 The snapshot is made from all registered files at or below the current
1064 directory. For each file, the version level of its latest
1065 version becomes part of the named configuration."
1066 (interactive "sNew snapshot name: ")
1067 (let ((locked (vc-locked-example)))
1068 (if locked
1069 (error "File %s is locked" locked)
1070 (vc-file-tree-walk
1071 (function (lambda (f) (and
1072 (vc-name f)
1073 (vc-backend-assign-name f name)))))
1076 ;;;###autoload
1077 (defun vc-retrieve-snapshot (name)
1078 "Retrieve the snapshot called NAME.
1079 This function fails if any files are locked at or below the current directory
1080 Otherwise, all registered files are checked out (unlocked) at their version
1081 levels in the snapshot."
1082 (interactive "sSnapshot name to retrieve: ")
1083 (let ((locked (vc-locked-example)))
1084 (if locked
1085 (error "File %s is locked" locked)
1086 (vc-file-tree-walk
1087 (function (lambda (f) (and
1088 (vc-name f)
1089 (vc-error-occurred
1090 (vc-backend-checkout f nil name))))))
1093 ;; Miscellaneous other entry points
1095 ;;;###autoload
1096 (defun vc-print-log ()
1097 "List the change log of the current buffer in a window."
1098 (interactive)
1099 (if vc-dired-mode
1100 (set-buffer (find-file-noselect (dired-get-filename))))
1101 (while vc-parent-buffer
1102 (pop-to-buffer vc-parent-buffer))
1103 (if (and buffer-file-name (vc-name buffer-file-name))
1104 (progn
1105 (vc-backend-print-log buffer-file-name)
1106 (pop-to-buffer (get-buffer-create "*vc*"))
1107 (while (looking-at "=*\n")
1108 (delete-char (- (match-end 0) (match-beginning 0)))
1109 (forward-line -1))
1110 (goto-char (point-min))
1111 (if (looking-at "[\b\t\n\v\f\r ]+")
1112 (delete-char (- (match-end 0) (match-beginning 0))))
1113 (shrink-window-if-larger-than-buffer)
1115 (vc-registration-error buffer-file-name)
1119 ;;;###autoload
1120 (defun vc-revert-buffer ()
1121 "Revert the current buffer's file back to the latest checked-in version.
1122 This asks for confirmation if the buffer contents are not identical
1123 to that version."
1124 (interactive)
1125 (if vc-dired-mode
1126 (find-file-other-window (dired-get-filename)))
1127 (while vc-parent-buffer
1128 (pop-to-buffer vc-parent-buffer))
1129 (let ((file buffer-file-name)
1130 (obuf (current-buffer)) (changed (vc-diff nil t)))
1131 (if (and changed (or vc-suppress-confirm
1132 (not (yes-or-no-p "Discard changes? "))))
1133 (progn
1134 (delete-window)
1135 (error "Revert cancelled"))
1136 (set-buffer obuf))
1137 (if changed
1138 (delete-window))
1139 (vc-backend-revert file)
1140 (vc-resynch-window file t t)
1144 ;;;###autoload
1145 (defun vc-cancel-version (norevert)
1146 "Get rid of most recently checked in version of this file.
1147 A prefix argument means do not revert the buffer afterwards."
1148 (interactive "P")
1149 (if vc-dired-mode
1150 (find-file-other-window (dired-get-filename)))
1151 (while vc-parent-buffer
1152 (pop-to-buffer vc-parent-buffer))
1153 (let* ((target (concat (vc-latest-version (buffer-file-name))))
1154 (yours (concat (vc-your-latest-version (buffer-file-name))))
1155 (prompt (if (string-equal yours target)
1156 "Remove your version %s from master? "
1157 "Version %s was not your change. Remove it anyway? ")))
1158 (if (null (yes-or-no-p (format prompt target)))
1160 (vc-backend-uncheck (buffer-file-name) target)
1161 (if (or norevert
1162 (not (yes-or-no-p "Revert buffer to most recent remaining version? ")))
1163 (vc-mode-line (buffer-file-name))
1164 (vc-checkout (buffer-file-name) nil)))
1167 (defun vc-rename-file (old new)
1168 "Rename file OLD to NEW, and rename its master file likewise."
1169 (interactive "fVC rename file: \nFRename to: ")
1170 (let ((oldbuf (get-file-buffer old)))
1171 (if (and oldbuf (buffer-modified-p oldbuf))
1172 (error "Please save files before moving them"))
1173 (if (get-file-buffer new)
1174 (error "Already editing new file name"))
1175 (if (file-exists-p new)
1176 (error "New file already exists"))
1177 (let ((oldmaster (vc-name old)))
1178 (if oldmaster
1179 (progn
1180 (if (vc-locking-user old)
1181 (error "Please check in files before moving them"))
1182 (if (or (file-symlink-p oldmaster)
1183 ;; This had FILE, I changed it to OLD. -- rms.
1184 (file-symlink-p (vc-backend-subdirectory-name old)))
1185 (error "This is not a safe thing to do in the presence of symbolic links"))
1186 (rename-file
1187 oldmaster
1188 (let ((backend (vc-backend-deduce old))
1189 (newdir (or (file-name-directory new) ""))
1190 (newbase (file-name-nondirectory new)))
1191 (catch 'found
1192 (mapcar
1193 (function
1194 (lambda (s)
1195 (if (eq backend (cdr s))
1196 (let* ((newmaster (format (car s) newdir newbase))
1197 (newmasterdir (file-name-directory newmaster)))
1198 (if (or (not newmasterdir)
1199 (file-directory-p newmasterdir))
1200 (throw 'found newmaster))))))
1201 vc-master-templates)
1202 (error "New file lacks a version control directory"))))))
1203 (if (or (not oldmaster) (file-exists-p old))
1204 (rename-file old new)))
1205 ; ?? Renaming a file might change its contents due to keyword expansion.
1206 ; We should really check out a new copy if the old copy was precisely equal
1207 ; to some checked in version. However, testing for this is tricky....
1208 (if oldbuf
1209 (save-excursion
1210 (set-buffer oldbuf)
1211 (set-visited-file-name new)
1212 (set-buffer-modified-p nil))))
1213 ;; This had FILE, I changed it to OLD. -- rms.
1214 (vc-backend-dispatch old
1215 (vc-record-rename old new)
1216 nil)
1219 ;;;###autoload
1220 (defun vc-update-change-log (&rest args)
1221 "Find change log file and add entries from recent RCS logs.
1222 The mark is left at the end of the text prepended to the change log.
1223 With prefix arg of C-u, only find log entries for the current buffer's file.
1224 With any numeric prefix arg, find log entries for all files currently visited.
1225 Otherwise, find log entries for all registered files in the default directory.
1226 From a program, any arguments are passed to the `rcs2log' script."
1227 (interactive
1228 (cond ((consp current-prefix-arg) ;C-u
1229 (list buffer-file-name))
1230 (current-prefix-arg ;Numeric argument.
1231 (let ((files nil)
1232 (buffers (buffer-list))
1233 file)
1234 (while buffers
1235 (setq file (buffer-file-name (car buffers)))
1236 (and file (vc-backend-deduce file)
1237 (setq files (cons file files)))
1238 (setq buffers (cdr buffers)))
1239 files))
1241 (let ((RCS (concat default-directory "RCS")))
1242 (and (file-directory-p RCS)
1243 (mapcar (function
1244 (lambda (f)
1245 (if (string-match "\\(.*\\),v$" f)
1246 (substring f 0 (match-end 1))
1247 f)))
1248 (directory-files RCS nil "...\\|^[^.]\\|^.[^.]")))))))
1249 (let ((odefault default-directory))
1250 (find-file-other-window (find-change-log))
1251 (barf-if-buffer-read-only)
1252 (vc-buffer-sync)
1253 (undo-boundary)
1254 (goto-char (point-min))
1255 (push-mark)
1256 (message "Computing change log entries...")
1257 (message "Computing change log entries... %s"
1258 (if (or (null args)
1259 (eq 0 (apply 'call-process "rcs2log" nil t nil
1260 (mapcar (function
1261 (lambda (f)
1262 (file-relative-name
1263 (if (file-name-absolute-p f)
1265 (concat odefault f)))))
1266 args))))
1267 "done" "failed"))))
1269 ;; Functions for querying the master and lock files.
1271 (defun vc-match-substring (bn)
1272 (buffer-substring (match-beginning bn) (match-end bn)))
1274 (defun vc-parse-buffer (patterns &optional file properties)
1275 ;; Use PATTERNS to parse information out of the current buffer
1276 ;; by matching each regular expression in the list and returning \\1.
1277 ;; If a regexp has two tag brackets, assume the second is a date
1278 ;; field and we want the most recent entry matching the template.
1279 ;; If FILE and PROPERTIES are given, the latter must be a list of
1280 ;; properties of the same length as PATTERNS; each property is assigned
1281 ;; the corresponding value.
1282 (mapcar (function (lambda (p)
1283 (goto-char (point-min))
1284 (if (string-match "\\\\(.*\\\\(" p)
1285 (let ((latest-date "") (latest-val))
1286 (while (re-search-forward p nil t)
1287 (let ((date (vc-match-substring 2)))
1288 (if (string< latest-date date)
1289 (progn
1290 (setq latest-date date)
1291 (setq latest-val
1292 (vc-match-substring 1))))))
1293 latest-val))
1294 (prog1
1295 (and (re-search-forward p nil t)
1296 (let ((value (vc-match-substring 1)))
1297 (if file
1298 (vc-file-setprop file (car properties) value))
1299 value))
1300 (setq properties (cdr properties)))))
1301 patterns)
1304 (defun vc-master-info (file fields &optional rfile properties)
1305 ;; Search for information in a master file.
1306 (if (and file (file-exists-p file))
1307 (save-excursion
1308 (let ((buf))
1309 (setq buf (create-file-buffer file))
1310 (set-buffer buf))
1311 (erase-buffer)
1312 (insert-file-contents file nil)
1313 (set-buffer-modified-p nil)
1314 (auto-save-mode nil)
1315 (prog1
1316 (vc-parse-buffer fields rfile properties)
1317 (kill-buffer (current-buffer)))
1319 (if rfile
1320 (mapcar
1321 (function (lambda (p) (vc-file-setprop rfile p nil)))
1322 properties))
1326 (defun vc-log-info (command file patterns &optional properties)
1327 ;; Search for information in log program output
1328 (if (and file (file-exists-p file))
1329 (save-excursion
1330 (let ((buf))
1331 (setq buf (get-buffer-create "*vc*"))
1332 (set-buffer buf))
1333 (apply 'vc-do-command 0 command file nil)
1334 (set-buffer-modified-p nil)
1335 (prog1
1336 (vc-parse-buffer patterns file properties)
1337 (kill-buffer (current-buffer))
1340 (if file
1341 (mapcar
1342 (function (lambda (p) (vc-file-setprop file p nil)))
1343 properties))
1347 (defun vc-locking-user (file)
1348 "Return the name of the person currently holding a lock on FILE.
1349 Return nil if there is no such person."
1350 (setq file (expand-file-name file)) ;; ??? Work around bug in 19.0.4
1351 (if (or (not vc-keep-workfiles)
1352 (eq vc-mistrust-permissions 't)
1353 (and vc-mistrust-permissions
1354 (funcall vc-mistrust-permissions (vc-backend-subdirectory-name file))))
1355 (vc-true-locking-user file)
1356 ;; This implementation assumes that any file which is under version
1357 ;; control and has -rw-r--r-- is locked by its owner. This is true
1358 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
1359 ;; We have to be careful not to exclude files with execute bits on;
1360 ;; scripts can be under version control too. Also, we must ignore
1361 ;; the group-read and other-read bits, since paranoid users turn them off.
1362 ;; This hack wins because calls to the very expensive vc-fetch-properties
1363 ;; function only have to be made if (a) the file is locked by someone
1364 ;; other than the current user, or (b) some untoward manipulation
1365 ;; behind vc's back has changed the owner or the `group' or `other'
1366 ;; write bits.
1367 (let ((attributes (file-attributes file)))
1368 (cond ((string-match ".r-..-..-." (nth 8 attributes))
1369 nil)
1370 ((and (= (nth 2 attributes) (user-uid))
1371 (string-match ".rw..-..-." (nth 8 attributes)))
1372 (user-login-name))
1374 (vc-true-locking-user file))))))
1376 (defun vc-true-locking-user (file)
1377 ;; The slow but reliable version
1378 (vc-fetch-properties file)
1379 (vc-file-getprop file 'vc-locking-user))
1381 (defun vc-latest-version (file)
1382 ;; Return version level of the latest version of FILE
1383 (vc-fetch-properties file)
1384 (vc-file-getprop file 'vc-latest-version))
1386 (defun vc-your-latest-version (file)
1387 ;; Return version level of the latest version of FILE checked in by you
1388 (vc-fetch-properties file)
1389 (vc-file-getprop file 'vc-your-latest-version))
1391 ;; Collect back-end-dependent stuff here
1393 ;; Everything eventually funnels through these functions. To implement
1394 ;; support for a new version-control system, add another branch to the
1395 ;; vc-backend-dispatch macro and fill it in in each call. The variable
1396 ;; vc-master-templates in vc-hooks.el will also have to change.
1398 (defmacro vc-backend-dispatch (f s r)
1399 "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
1400 (list 'let (list (list 'type (list 'vc-backend-deduce f)))
1401 (list 'cond
1402 (list (list 'eq 'type (quote 'SCCS)) s) ;; SCCS
1403 (list (list 'eq 'type (quote 'RCS)) r) ;; RCS
1406 (defun vc-lock-file (file)
1407 ;; Generate lock file name corresponding to FILE
1408 (let ((master (vc-name file)))
1409 (and
1410 master
1411 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
1412 (concat
1413 (substring master (match-beginning 1) (match-end 1))
1414 "p."
1415 (substring master (match-beginning 2) (match-end 2))))))
1418 (defun vc-fetch-properties (file)
1419 ;; Re-fetch all properties associated with the given file.
1420 ;; Currently these properties are:
1421 ;; vc-locking-user
1422 ;; vc-locked-version
1423 ;; vc-latest-version
1424 ;; vc-your-latest-version
1425 (vc-backend-dispatch
1426 file
1427 ;; SCCS
1428 (progn
1429 (vc-master-info (vc-lock-file file)
1430 (list
1431 "^[^ ]+ [^ ]+ \\([^ ]+\\)"
1432 "^\\([^ ]+\\)")
1433 file
1434 '(vc-locking-user vc-locked-version))
1435 (vc-master-info (vc-name file)
1436 (list
1437 "^\001d D \\([^ ]+\\)"
1438 (concat "^\001d D \\([^ ]+\\) .* "
1439 (regexp-quote (user-login-name)) " ")
1441 file
1442 '(vc-latest-version vc-your-latest-version))
1444 ;; RCS
1445 (vc-log-info "rlog" file
1446 (list
1447 "^locks: strict\n\t\\([^:]+\\)"
1448 "^locks: strict\n\t[^:]+: \\(.+\\)"
1449 "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
1450 (concat
1451 "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
1452 (regexp-quote (user-login-name))
1453 ";"))
1454 '(vc-locking-user vc-locked-version
1455 vc-latest-version vc-your-latest-version))
1458 (defun vc-backend-subdirectory-name (&optional file)
1459 ;; Where the master and lock files for the current directory are kept
1460 (symbol-name
1462 (and file (vc-backend-deduce file))
1463 vc-default-back-end
1464 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
1466 (defun vc-backend-admin (file &optional rev comment)
1467 ;; Register a file into the version-control system
1468 ;; Automatically retrieves a read-only version of the file with
1469 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1470 ;; it deletes the workfile.
1471 (vc-file-clearprops file)
1472 (or vc-default-back-end
1473 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))
1474 (message "Registering %s..." file)
1475 (let ((backend
1476 (cond
1477 ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end)
1478 ((file-exists-p "RCS") 'RCS)
1479 ((file-exists-p "SCCS") 'SCCS)
1480 (t vc-default-back-end))))
1481 (cond ((eq backend 'SCCS)
1482 (vc-do-command 0 "admin" file ;; SCCS
1483 (and rev (concat "-r" rev))
1484 "-fb"
1485 (concat "-i" file)
1486 (and comment (concat "-y" comment))
1487 (format
1488 (car (rassq 'SCCS vc-master-templates))
1489 (or (file-name-directory file) "")
1490 (file-name-nondirectory file)))
1491 (delete-file file)
1492 (if vc-keep-workfiles
1493 (vc-do-command 0 "get" file)))
1494 ((eq backend 'RCS)
1495 (vc-do-command 0 "ci" file ;; RCS
1496 (concat (if vc-keep-workfiles "-u" "-r") rev)
1497 (and comment (concat "-t-" comment))
1498 file)
1500 (message "Registering %s...done" file)
1503 (defun vc-backend-checkout (file &optional writable rev workfile)
1504 ;; Retrieve a copy of a saved version into a workfile
1505 (let ((filename (or workfile file)))
1506 (message "Checking out %s..." filename)
1507 (vc-backend-dispatch file
1508 (vc-do-command 0 "get" file ;; SCCS
1509 (if writable "-e")
1510 (if workfile (concat "-G" workfile))
1511 (and rev (concat "-r" (vc-lookup-triple file rev))))
1512 (if workfile ;; RCS
1513 ;; RCS doesn't let us check out into arbitrary file names directly.
1514 ;; Use `co -p' and make stdout point to the correct file.
1515 (let ((vc-modes (logior (file-modes (vc-name file))
1516 (if writable 128 0)))
1517 (failed t))
1518 (unwind-protect
1519 (progn
1520 (vc-do-command
1521 0 "/bin/sh" file "-c"
1522 (format "umask %o; exec >\"$1\" || exit; shift; umask %o; exec co \"$@\""
1523 (logand 511 (lognot vc-modes))
1524 (logand 511 (lognot (default-file-modes))))
1525 "" ; dummy argument for shell's $0
1526 filename
1527 (if writable "-l")
1528 (concat "-p" rev))
1529 (setq failed nil))
1530 (and failed (file-exists-p filename) (delete-file filename))))
1531 (vc-do-command 0 "co" file
1532 (if writable "-l")
1533 (and rev (concat "-r" rev))))
1535 (or workfile
1536 (vc-file-setprop file 'vc-checkout-time (nth 5 (file-attributes file))))
1537 (message "Checking out %s...done" filename))
1540 (defun vc-backend-logentry-check (file)
1541 (vc-backend-dispatch file
1542 (if (>= (buffer-size) 512) ;; SCCS
1543 (progn
1544 (goto-char 512)
1545 (error
1546 "Log must be less than 512 characters; point is now at pos 512")))
1547 nil)
1550 (defun vc-backend-checkin (file &optional rev comment)
1551 ;; Register changes to FILE as level REV with explanatory COMMENT.
1552 ;; Automatically retrieves a read-only version of the file with
1553 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1554 ;; it deletes the workfile.
1555 (message "Checking in %s..." file)
1556 (save-excursion
1557 ;; Change buffers to get local value of vc-checkin-switches.
1558 (set-buffer (or (get-file-buffer file) (current-buffer)))
1559 (vc-backend-dispatch file
1560 (progn
1561 (apply 'vc-do-command 0 "delta" file
1562 (if rev (concat "-r" rev))
1563 (concat "-y" comment)
1564 vc-checkin-switches)
1565 (if vc-keep-workfiles
1566 (vc-do-command 0 "get" file))
1568 (apply 'vc-do-command 0 "ci" file
1569 (concat (if vc-keep-workfiles "-u" "-r") rev)
1570 (concat "-m" comment)
1571 vc-checkin-switches)
1573 (vc-file-setprop file 'vc-locking-user nil)
1574 (message "Checking in %s...done" file)
1577 (defun vc-backend-revert (file)
1578 ;; Revert file to latest checked-in version.
1579 (message "Reverting %s..." file)
1580 (vc-backend-dispatch
1581 file
1582 (progn ;; SCCS
1583 (vc-do-command 0 "unget" file nil)
1584 (vc-do-command 0 "get" file nil))
1585 (vc-do-command 0 "co" file "-f" "-u")) ;; RCS. This deletes the work file.
1586 (vc-file-setprop file 'vc-locking-user nil)
1587 (message "Reverting %s...done" file)
1590 (defun vc-backend-steal (file &optional rev)
1591 ;; Steal the lock on the current workfile. Needs RCS 5.6.2 or later for -M.
1592 (message "Stealing lock on %s..." file)
1593 (vc-backend-dispatch file
1594 (progn
1595 (vc-do-command 0 "unget" file "-n" (if rev (concat "-r" rev)))
1596 (vc-do-command 0 "get" file "-g" (if rev (concat "-r" rev)))
1598 (vc-do-command 0 "rcs" file "-M" (concat "-u" rev) (concat "-l" rev)))
1599 (vc-file-setprop file 'vc-locking-user (user-login-name))
1600 (message "Stealing lock on %s...done" file)
1603 (defun vc-backend-uncheck (file target)
1604 ;; Undo the latest checkin. Note: this code will have to get a lot
1605 ;; smarter when we support multiple branches.
1606 (message "Removing last change from %s..." file)
1607 (vc-backend-dispatch file
1608 (vc-do-command 0 "rmdel" file (concat "-r" target))
1609 (vc-do-command 0 "rcs" file (concat "-o" target))
1611 (message "Removing last change from %s...done" file)
1614 (defun vc-backend-print-log (file)
1615 ;; Print change log associated with FILE to buffer *vc*.
1616 (vc-do-command 0
1617 (vc-backend-dispatch file "prs" "rlog")
1618 file)
1621 (defun vc-backend-assign-name (file name)
1622 ;; Assign to a FILE's latest version a given NAME.
1623 (vc-backend-dispatch file
1624 (vc-add-triple name file (vc-latest-version file)) ;; SCCS
1625 (vc-do-command 0 "rcs" file (concat "-n" name ":")) ;; RCS
1629 (defun vc-backend-diff (file &optional oldvers newvers cmp)
1630 ;; Get a difference report between two versions of FILE.
1631 ;; Get only a brief comparison report if CMP, a difference report otherwise.
1632 (if (eq (vc-backend-deduce file) 'SCCS)
1633 (setq oldvers (vc-lookup-triple file oldvers))
1634 (setq newvers (vc-lookup-triple file newvers)))
1635 (let* ((command (or (vc-backend-dispatch file "vcdiff" "rcsdiff")
1636 (vc-registration-error file)))
1637 (options (append (list (and cmp "--brief")
1638 "-q"
1639 (and oldvers (concat "-r" oldvers))
1640 (and newvers (concat "-r" newvers)))
1641 (and (not cmp)
1642 (if (listp diff-switches)
1643 diff-switches
1644 (list diff-switches)))))
1645 (status (apply 'vc-do-command 2 command file options)))
1646 ;; Some RCS versions don't understand "--brief"; work around this.
1647 (if (eq status 2)
1648 (apply 'vc-do-command 1 command file (if cmp options (cdr options)))
1649 status)))
1651 (defun vc-check-headers ()
1652 "Check if the current file has any headers in it."
1653 (interactive)
1654 (save-excursion
1655 (goto-char (point-min))
1656 (vc-backend-dispatch buffer-file-name
1657 (re-search-forward "%[MIRLBSDHTEGUYFPQCZWA]%" nil t) ;; SCCS
1658 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t) ;; RCS
1662 ;; Back-end-dependent stuff ends here.
1664 ;; Set up key bindings for use while editing log messages
1666 (defun vc-log-mode ()
1667 "Minor mode for driving version-control tools.
1668 These bindings are added to the global keymap when you enter this mode:
1669 \\[vc-next-action] perform next logical version-control operation on current file
1670 \\[vc-register] register current file
1671 \\[vc-toggle-read-only] like next-action, but won't register files
1672 \\[vc-insert-headers] insert version-control headers in current file
1673 \\[vc-print-log] display change history of current file
1674 \\[vc-revert-buffer] revert buffer to latest version
1675 \\[vc-cancel-version] undo latest checkin
1676 \\[vc-diff] show diffs between file versions
1677 \\[vc-version-other-window] visit old version in another window
1678 \\[vc-directory] show all files locked by any user in or below .
1679 \\[vc-update-change-log] add change log entry from recent checkins
1681 While you are entering a change log message for a version, the following
1682 additional bindings will be in effect.
1684 \\[vc-finish-logentry] proceed with check in, ending log message entry
1686 Whenever you do a checkin, your log comment is added to a ring of
1687 saved comments. These can be recalled as follows:
1689 \\[vc-next-comment] replace region with next message in comment ring
1690 \\[vc-previous-comment] replace region with previous message in comment ring
1691 \\[vc-comment-search-reverse] search backward for regexp in the comment ring
1692 \\[vc-comment-search-forward] search backward for regexp in the comment ring
1694 Entry to the change-log submode calls the value of text-mode-hook, then
1695 the value of vc-log-mode-hook.
1697 Global user options:
1698 vc-initial-comment If non-nil, require user to enter a change
1699 comment upon first checkin of the file.
1701 vc-keep-workfiles Non-nil value prevents workfiles from being
1702 deleted when changes are checked in
1704 vc-suppress-confirm Suppresses some confirmation prompts,
1705 notably for reversions.
1707 vc-header-alist Which keywords to insert when adding headers
1708 with \\[vc-insert-headers]. Defaults to
1709 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
1711 vc-static-header-alist By default, version headers inserted in C files
1712 get stuffed in a static string area so that
1713 ident(RCS) or what(SCCS) can see them in the
1714 compiled object code. You can override this
1715 by setting this variable to nil, or change
1716 the header template by changing it.
1718 vc-command-messages if non-nil, display run messages from the
1719 actual version-control utilities (this is
1720 intended primarily for people hacking vc
1721 itself).
1723 (interactive)
1724 (set-syntax-table text-mode-syntax-table)
1725 (use-local-map vc-log-entry-mode)
1726 (setq local-abbrev-table text-mode-abbrev-table)
1727 (setq major-mode 'vc-log-mode)
1728 (setq mode-name "VC-Log")
1729 (make-local-variable 'vc-log-file)
1730 (make-local-variable 'vc-log-version)
1731 (make-local-variable 'vc-comment-ring-index)
1732 (set-buffer-modified-p nil)
1733 (setq buffer-file-name nil)
1734 (run-hooks 'text-mode-hook 'vc-log-mode-hook)
1737 ;; Initialization code, to be done just once at load-time
1738 (if vc-log-entry-mode
1740 (setq vc-log-entry-mode (make-sparse-keymap))
1741 (define-key vc-log-entry-mode "\M-n" 'vc-next-comment)
1742 (define-key vc-log-entry-mode "\M-p" 'vc-previous-comment)
1743 (define-key vc-log-entry-mode "\M-r" 'vc-comment-search-reverse)
1744 (define-key vc-log-entry-mode "\M-s" 'vc-comment-search-forward)
1745 (define-key vc-log-entry-mode "\C-c\C-c" 'vc-finish-logentry)
1748 ;;; These things should probably be generally available
1750 (defun vc-file-tree-walk (func &rest args)
1751 "Walk recursively through default directory.
1752 Invoke FUNC f ARGS on each non-directory file f underneath it."
1753 (vc-file-tree-walk-internal default-directory func args)
1754 (message "Traversing directory %s...done" default-directory))
1756 (defun vc-file-tree-walk-internal (file func args)
1757 (if (not (file-directory-p file))
1758 (apply func file args)
1759 (message "Traversing directory %s..." file)
1760 (let ((dir (file-name-as-directory file)))
1761 (mapcar
1762 (function
1763 (lambda (f) (or
1764 (string-equal f ".")
1765 (string-equal f "..")
1766 (let ((dirf (concat dir f)))
1768 (file-symlink-p dirf) ;; Avoid possible loops
1769 (vc-file-tree-walk-internal dirf func args))))))
1770 (directory-files dir)))))
1772 (provide 'vc)
1774 ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
1776 ;;; These may be useful to anyone who has to debug or extend the package.
1777 ;;;
1778 ;;; A fundamental problem in VC is that there are time windows between
1779 ;;; vc-next-action's computations of the file's version-control state and
1780 ;;; the actions that change it. This is a window open to lossage in a
1781 ;;; multi-user environment; someone else could nip in and change the state
1782 ;;; of the master during it.
1783 ;;;
1784 ;;; The performance problem is that rlog/prs calls are very expensive; we want
1785 ;;; to avoid them as much as possible.
1786 ;;;
1787 ;;; ANALYSIS:
1788 ;;;
1789 ;;; The performance problem, it turns out, simplifies in practice to the
1790 ;;; problem of making vc-locking-user fast. The two other functions that call
1791 ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
1792 ;;; makes snapshots, the other deletes the calling user's last change in the
1793 ;;; master.
1794 ;;;
1795 ;;; The race condition implies that we have to either (a) lock the master
1796 ;;; during the entire execution of vc-next-action, or (b) detect and
1797 ;;; recover from errors resulting from dispatch on an out-of-date state.
1798 ;;;
1799 ;;; Alternative (a) appears to be unfeasible. The problem is that we can't
1800 ;;; guarantee that the lock will ever be removed. Suppose a user starts a
1801 ;;; checkin, the change message buffer pops up, and the user, having wandered
1802 ;;; off to do something else, simply forgets about it?
1803 ;;;
1804 ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
1805 ;;; vc-locking-user. Usually, if a file is registered, we can read its locked/
1806 ;;; unlocked state and its current owner from its permissions.
1807 ;;;
1808 ;;; This shortcut will fail if someone has manually changed the workfile's
1809 ;;; permissions; also if developers are munging the workfile in several
1810 ;;; directories, with symlinks to a master (in this latter case, the
1811 ;;; permissions shortcut will fail to detect a lock asserted from another
1812 ;;; directory).
1813 ;;;
1814 ;;; Note that these cases correspond exactly to the errors which could happen
1815 ;;; because of a competing checkin/checkout race in between two instances of
1816 ;;; vc-next-action.
1817 ;;;
1818 ;;; For VC's purposes, a workfile/master pair may have the following states:
1819 ;;;
1820 ;;; A. Unregistered. There is a workfile, there is no master.
1821 ;;;
1822 ;;; B. Registered and not locked by anyone.
1823 ;;;
1824 ;;; C. Locked by calling user and unchanged.
1825 ;;;
1826 ;;; D. Locked by the calling user and changed.
1827 ;;;
1828 ;;; E. Locked by someone other than the calling user.
1829 ;;;
1830 ;;; This makes for 25 states and 20 error conditions. Here's the matrix:
1831 ;;;
1832 ;;; VC's idea of state
1833 ;;; |
1834 ;;; V Actual state RCS action SCCS action Effect
1835 ;;; A B C D E
1836 ;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
1837 ;;; B 5 . 6 7 8 co -l get -e checkout
1838 ;;; C 9 10 . 11 12 co -u unget; get revert
1839 ;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
1840 ;;; E 17 18 19 20 . rcs -u -M ; rcs -l unget -n ; get -g steal lock
1841 ;;;
1842 ;;; All commands take the master file name as a last argument (not shown).
1843 ;;;
1844 ;;; In the discussion below, a "self-race" is a pathological situation in
1845 ;;; which VC operations are being attempted simultaneously by two or more
1846 ;;; Emacsen running under the same username.
1847 ;;;
1848 ;;; The vc-next-action code has the following windows:
1849 ;;;
1850 ;;; Window P:
1851 ;;; Between the check for existence of a master file and the call to
1852 ;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
1853 ;;; never close if the initial-comment feature is on.
1854 ;;;
1855 ;;; Window Q:
1856 ;;; Between the call to vc-workfile-unchanged-p in and the immediately
1857 ;;; following revert (apparent state C).
1858 ;;;
1859 ;;; Window R:
1860 ;;; Between the call to vc-workfile-unchanged-p in and the following
1861 ;;; checkin (apparent state D). This window may never close.
1862 ;;;
1863 ;;; Window S:
1864 ;;; Between the unlock and the immediately following checkout during a
1865 ;;; revert operation (apparent state C). Included in window Q.
1866 ;;;
1867 ;;; Window T:
1868 ;;; Between vc-locking-user and the following checkout (apparent state B).
1869 ;;;
1870 ;;; Window U:
1871 ;;; Between vc-locking-user and the following revert (apparent state C).
1872 ;;; Includes windows Q and S.
1873 ;;;
1874 ;;; Window V:
1875 ;;; Between vc-locking-user and the following checkin (apparent state
1876 ;;; D). This window may never be closed if the user fails to complete the
1877 ;;; checkin message. Includes window R.
1878 ;;;
1879 ;;; Window W:
1880 ;;; Between vc-locking-user and the following steal-lock (apparent
1881 ;;; state E). This window may never close if the user fails to complete
1882 ;;; the steal-lock message. Includes window X.
1883 ;;;
1884 ;;; Window X:
1885 ;;; Between the unlock and the immediately following re-lock during a
1886 ;;; steal-lock operation (apparent state E). This window may never cloce
1887 ;;; if the user fails to complete the steal-lock message.
1888 ;;;
1889 ;;; Errors:
1890 ;;;
1891 ;;; Apparent state A ---
1893 ;;; 1. File looked unregistered but is actually registered and not locked.
1894 ;;;
1895 ;;; Potential cause: someone else's admin during window P, with
1896 ;;; caller's admin happening before their checkout.
1897 ;;;
1898 ;;; RCS: ci will fail with a "no lock set by <user>" message.
1899 ;;; SCCS: admin will fail with error (ad19).
1900 ;;;
1901 ;;; We can let these errors be passed up to the user.
1902 ;;;
1903 ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
1904 ;;;
1905 ;;; Potential cause: self-race during window P.
1906 ;;;
1907 ;;; RCS: will revert the file to the last saved version and unlock it.
1908 ;;; SCCS: will fail with error (ad19).
1909 ;;;
1910 ;;; Either of these consequences is acceptable.
1911 ;;;
1912 ;;; 3. File looked unregistered but is actually locked by caller, changed.
1913 ;;;
1914 ;;; Potential cause: self-race during window P.
1915 ;;;
1916 ;;; RCS: will register the caller's workfile as a delta with a
1917 ;;; null change comment (the -t- switch will be ignored).
1918 ;;; SCCS: will fail with error (ad19).
1919 ;;;
1920 ;;; 4. File looked unregistered but is locked by someone else.
1921 ;;;
1922 ;;; Potential cause: someone else's admin during window P, with
1923 ;;; caller's admin happening *after* their checkout.
1924 ;;;
1925 ;;; RCS: will fail with a "no lock set by <user>" message.
1926 ;;; SCCS: will fail with error (ad19).
1927 ;;;
1928 ;;; We can let these errors be passed up to the user.
1929 ;;;
1930 ;;; Apparent state B ---
1932 ;;; 5. File looked registered and not locked, but is actually unregistered.
1933 ;;;
1934 ;;; Potential cause: master file got nuked during window P.
1935 ;;;
1936 ;;; RCS: will fail with "RCS/<file>: No such file or directory"
1937 ;;; SCCS: will fail with error ut4.
1938 ;;;
1939 ;;; We can let these errors be passed up to the user.
1940 ;;;
1941 ;;; 6. File looked registered and not locked, but is actually locked by the
1942 ;;; calling user and unchanged.
1943 ;;;
1944 ;;; Potential cause: self-race during window T.
1945 ;;;
1946 ;;; RCS: in the same directory as the previous workfile, co -l will fail
1947 ;;; with "co error: writable foo exists; checkout aborted". In any other
1948 ;;; directory, checkout will succeed.
1949 ;;; SCCS: will fail with ge17.
1950 ;;;
1951 ;;; Either of these consequences is acceptable.
1952 ;;;
1953 ;;; 7. File looked registered and not locked, but is actually locked by the
1954 ;;; calling user and changed.
1955 ;;;
1956 ;;; As case 6.
1957 ;;;
1958 ;;; 8. File looked registered and not locked, but is actually locked by another
1959 ;;; user.
1960 ;;;
1961 ;;; Potential cause: someone else checks it out during window T.
1962 ;;;
1963 ;;; RCS: co error: revision 1.3 already locked by <user>
1964 ;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
1965 ;;;
1966 ;;; We can let these errors be passed up to the user.
1967 ;;;
1968 ;;; Apparent state C ---
1970 ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
1971 ;;;
1972 ;;; As case 5.
1973 ;;;
1974 ;;; 10. File looks locked by calling user and unchanged, but is actually not
1975 ;;; locked.
1976 ;;;
1977 ;;; Potential cause: a self-race in window U, or by the revert's
1978 ;;; landing during window X of some other user's steal-lock or window S
1979 ;;; of another user's revert.
1980 ;;;
1981 ;;; RCS: succeeds, refreshing the file from the identical version in
1982 ;;; the master.
1983 ;;; SCCS: fails with error ut4 (p file nonexistent).
1985 ;;; Either of these consequences is acceptable.
1986 ;;;
1987 ;;; 11. File is locked by calling user. It looks unchanged, but is actually
1988 ;;; changed.
1989 ;;;
1990 ;;; Potential cause: the file would have to be touched by a self-race
1991 ;;; during window Q.
1992 ;;;
1993 ;;; The revert will succeed, removing whatever changes came with
1994 ;;; the touch. It is theoretically possible that work could be lost.
1995 ;;;
1996 ;;; 12. File looks like it's locked by the calling user and unchanged, but
1997 ;;; it's actually locked by someone else.
1998 ;;;
1999 ;;; Potential cause: a steal-lock in window V.
2000 ;;;
2001 ;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
2002 ;;; SCCS: fails with error un2
2003 ;;;
2004 ;;; We can pass these errors up to the user.
2005 ;;;
2006 ;;; Apparent state D ---
2008 ;;; 13. File looks like it's locked by the calling user and changed, but it's
2009 ;;; actually unregistered.
2010 ;;;
2011 ;;; Potential cause: master file got nuked during window P.
2012 ;;;
2013 ;;; RCS: Checks in the user's version as an initial delta.
2014 ;;; SCCS: will fail with error ut4.
2016 ;;; This case is kind of nasty. It means VC may fail to detect the
2017 ;;; loss of previous version information.
2018 ;;;
2019 ;;; 14. File looks like it's locked by the calling user and changed, but it's
2020 ;;; actually unlocked.
2021 ;;;
2022 ;;; Potential cause: self-race in window V, or the checkin happening
2023 ;;; during the window X of someone else's steal-lock or window S of
2024 ;;; someone else's revert.
2025 ;;;
2026 ;;; RCS: ci will fail with "no lock set by <user>".
2027 ;;; SCCS: delta will fail with error ut4.
2028 ;;;
2029 ;;; 15. File looks like it's locked by the calling user and changed, but it's
2030 ;;; actually locked by the calling user and unchanged.
2031 ;;;
2032 ;;; Potential cause: another self-race --- a whole checkin/checkout
2033 ;;; sequence by the calling user would have to land in window R.
2034 ;;;
2035 ;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
2036 ;;; RCS: reverts to the file state as of the second user's checkin, leaving
2037 ;;; the file unlocked.
2039 ;;; It is theoretically possible that work could be lost under RCS.
2040 ;;;
2041 ;;; 16. File looks like it's locked by the calling user and changed, but it's
2042 ;;; actually locked by a different user.
2043 ;;;
2044 ;;; RCS: ci error: no lock set by <user>
2045 ;;; SCCS: unget will fail with error un2
2046 ;;;
2047 ;;; We can pass these errors up to the user.
2048 ;;;
2049 ;;; Apparent state E ---
2051 ;;; 17. File looks like it's locked by some other user, but it's actually
2052 ;;; unregistered.
2053 ;;;
2054 ;;; As case 13.
2055 ;;;
2056 ;;; 18. File looks like it's locked by some other user, but it's actually
2057 ;;; unlocked.
2058 ;;;
2059 ;;; Potential cause: someone released a lock during window W.
2060 ;;;
2061 ;;; RCS: The calling user will get the lock on the file.
2062 ;;; SCCS: unget -n will fail with cm4.
2063 ;;;
2064 ;;; Either of these consequences will be OK.
2065 ;;;
2066 ;;; 19. File looks like it's locked by some other user, but it's actually
2067 ;;; locked by the calling user and unchanged.
2068 ;;;
2069 ;;; Potential cause: the other user relinquishing a lock followed by
2070 ;;; a self-race, both in window W.
2071 ;;;
2072 ;;; Under both RCS and SCCS, both unlock and lock will succeed, making
2073 ;;; the sequence a no-op.
2074 ;;;
2075 ;;; 20. File looks like it's locked by some other user, but it's actually
2076 ;;; locked by the calling user and changed.
2077 ;;;
2078 ;;; As case 19.
2079 ;;;
2080 ;;; PROBLEM CASES:
2081 ;;;
2082 ;;; In order of decreasing severity:
2083 ;;;
2084 ;;; Cases 11 and 15 under RCS are the only one that potentially lose work.
2085 ;;; They would require a self-race for this to happen.
2086 ;;;
2087 ;;; Case 13 in RCS loses information about previous deltas, retaining
2088 ;;; only the information in the current workfile. This can only happen
2089 ;;; if the master file gets nuked in window P.
2090 ;;;
2091 ;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
2092 ;;; no change comment in the master. This would require a self-race in
2093 ;;; window P or R respectively.
2094 ;;;
2095 ;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
2096 ;;;
2097 ;;; Unfortunately, it appears to me that no recovery is possible in these
2098 ;;; cases. They don't yield error messages, so there's no way to tell that
2099 ;;; a race condition has occurred.
2100 ;;;
2101 ;;; All other cases don't change either the workfile or the master, and
2102 ;;; trigger command errors which the user will see.
2103 ;;;
2104 ;;; Thus, there is no explicit recovery code.
2106 ;;; vc.el ends here