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