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