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