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