1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992,93,94,95,96,97,98,2000,2001 Free Software Foundation, Inc.
5 ;; Author: FSF (see below for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; $Id: vc.el,v 1.317 2001/10/29 12:26:15 spiegel Exp $
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)
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 the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; VC was initially designed and implemented by Eric S. Raymond
31 ;; <esr@snark.thyrsus.com>. Over the years, many people have
32 ;; contributed substantial amounts of work to VC. These include:
33 ;; Per Cederqvist <ceder@lysator.liu.se>
34 ;; Paul Eggert <eggert@twinsun.com>
35 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
36 ;; Martin Lorentzson <martinl@gnu.org>
37 ;; Dave Love <fx@gnu.org>
38 ;; Stefan Monnier <monnier@cs.yale.edu>
39 ;; John David Smith <jdsmith@astro.cornell.edu>
40 ;; Andre Spiegel <spiegel@gnu.org>
41 ;; Richard Stallman <rms@gnu.org>
46 ;; This mode is fully documented in the Emacs user's manual.
48 ;; Supported version-control systems presently include SCCS, RCS, and CVS.
50 ;; Some features will not work with old RCS versions. Where
51 ;; appropriate, VC finds out which version you have, and allows or
52 ;; disallows those features (stealing locks, for example, works only
53 ;; from 5.6.2 onwards).
54 ;; Even initial checkins will fail if your RCS version is so old that ci
55 ;; doesn't understand -t-; this has been known to happen to people running
58 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
60 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
61 ;; to be installed somewhere on Emacs's path for executables.
63 ;; If your site uses the ChangeLog convention supported by Emacs, the
64 ;; function vc-comment-to-change-log should prove a useful checkin hook.
66 ;; The vc code maintains some internal state in order to reduce expensive
67 ;; version-control operations to a minimum. Some names are only computed
68 ;; once. If you perform version control operations with RCS/SCCS/CVS while
69 ;; vc's back is turned, or move/rename master files while vc is running,
70 ;; vc may get seriously confused. Don't do these things!
72 ;; Developer's notes on some concurrency issues are included at the end of
75 ;; ADDING SUPPORT FOR OTHER BACKENDS
77 ;; VC can use arbitrary version control systems as a backend. To add
78 ;; support for a new backend named SYS, write a library vc-sys.el that
79 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
80 ;; case for the function and library names). VC will use that library if
81 ;; you put the symbol SYS somewhere into the list of
82 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
83 ;; returns non-nil for a file, all SYS-specific versions of VC commands
84 ;; will be available for that file.
86 ;; VC keeps some per-file information in the form of properties (see
87 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
88 ;; do not generally need to be aware of these properties. For example,
89 ;; `vc-sys-workfile-version' should compute the workfile version and
90 ;; return it; it should not look it up in the property, and it needn't
91 ;; store it there either. However, if a backend-specific function does
92 ;; store a value in a property, that value takes precedence over any
93 ;; value that the generic code might want to set (check for uses of
94 ;; the macro `with-vc-properties' in vc.el).
96 ;; In the list of functions below, each identifier needs to be prepended
97 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
98 ;; `*'), others are optional (`-').
100 ;; STATE-QUERYING FUNCTIONS
102 ;; * registered (file)
104 ;; Return non-nil if FILE is registered in this backend.
108 ;; Return the current version control state of FILE. For a list of
109 ;; possible values, see `vc-state'. This function should do a full and
110 ;; reliable state computation; it is usually called immediately after
111 ;; C-x v v. If you want to use a faster heuristic when visiting a
112 ;; file, put that into `state-heuristic' below.
114 ;; - state-heuristic (file)
116 ;; If provided, this function is used to estimate the version control
117 ;; state of FILE at visiting time. It should be considerably faster
118 ;; than the implementation of `state'. For a list of possible values,
119 ;; see the doc string of `vc-state'.
123 ;; If provided, this function is used to find the version control state
124 ;; of all files in DIR in a fast way. The function should not return
125 ;; anything, but rather store the files' states into the corresponding
126 ;; `vc-state' properties.
128 ;; * workfile-version (file)
130 ;; Return the current workfile version of FILE.
132 ;; - latest-on-branch-p (file)
134 ;; Return non-nil if the current workfile version of FILE is the latest
135 ;; on its branch. The default implementation always returns t, which
136 ;; means that working with non-current versions is not supported by
139 ;; * checkout-model (file)
141 ;; Indicate whether FILE needs to be "checked out" before it can be
142 ;; edited. See `vc-checkout-model' for a list of possible values.
144 ;; - workfile-unchanged-p (file)
146 ;; Return non-nil if FILE is unchanged from its current workfile
147 ;; version. This function should do a brief comparison of FILE's
148 ;; contents with those of the master version. If the backend does not
149 ;; have such a brief-comparison feature, the default implementation of
150 ;; this function can be used, which delegates to a full
153 ;; - mode-line-string (file)
155 ;; If provided, this function should return the VC-specific mode line
156 ;; string for FILE. The default implementation deals well with all
157 ;; states that `vc-state' can return.
159 ;; - dired-state-info (file)
161 ;; Translate the `vc-state' property of FILE into a string that can be
162 ;; used in a vc-dired buffer. The default implementation deals well
163 ;; with all states that `vc-state' can return.
165 ;; STATE-CHANGING FUNCTIONS
167 ;; * register (file &optional rev comment)
169 ;; Register FILE in this backend. Optionally, an initial revision REV
170 ;; and an initial description of the file, COMMENT, may be specified.
171 ;; The implementation should pass the value of vc-register-switches
172 ;; to the backend command.
174 ;; - responsible-p (file)
176 ;; Return non-nil if this backend considers itself "responsible" for
177 ;; FILE, which can also be a directory. This function is used to find
178 ;; out what backend to use for registration of new files and for things
179 ;; like change log generation. The default implementation always
182 ;; - could-register (file)
184 ;; Return non-nil if FILE could be registered under this backend. The
185 ;; default implementation always returns t.
187 ;; - receive-file (file rev)
189 ;; Let this backend "receive" a file that is already registered under
190 ;; another backend. The default implementation simply calls `register'
191 ;; for FILE, but it can be overridden to do something more specific,
192 ;; e.g. keep revision numbers consistent or choose editing modes for
193 ;; FILE that resemble those of the other backend.
195 ;; - unregister (file)
197 ;; Unregister FILE from this backend. This is only needed if this
198 ;; backend may be used as a "more local" backend for temporary editing.
200 ;; * checkin (file rev comment)
202 ;; Commit changes in FILE to this backend. If REV is non-nil, that
203 ;; should become the new revision number. COMMENT is used as a
204 ;; check-in comment. The implementation should pass the value of
205 ;; vc-checkin-switches to the backend command.
207 ;; * checkout (file &optional editable rev destfile)
209 ;; Check out revision REV of FILE into the working area. If EDITABLE
210 ;; is non-nil, FILE should be writable by the user and if locking is
211 ;; used for FILE, a lock should also be set. If REV is non-nil, that
212 ;; is the revision to check out (default is current workfile version);
213 ;; if REV is the empty string, that means to check out the head of the
214 ;; trunk. If optional arg DESTFILE is given, it is an alternate
215 ;; filename to write the contents to. The implementation should
216 ;; pass the value of vc-checkout-switches to the backend command.
218 ;; * revert (file &optional contents-done)
220 ;; Revert FILE back to the current workfile version. If optional
221 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
222 ;; already been reverted from a version backup, and this function
223 ;; only needs to update the status of FILE within the backend.
225 ;; - cancel-version (file editable)
227 ;; Cancel the current workfile version of FILE, i.e. remove it from the
228 ;; master. EDITABLE non-nil means that FILE should be writable
229 ;; afterwards, and if locking is used for FILE, then a lock should also
230 ;; be set. If this function is not provided, trying to cancel a
231 ;; version is caught as an error.
233 ;; - merge (file rev1 rev2)
235 ;; Merge the changes between REV1 and REV2 into the current working file.
237 ;; - merge-news (file)
239 ;; Merge recent changes from the current branch into FILE.
241 ;; - steal-lock (file &optional version)
243 ;; Steal any lock on the current workfile version of FILE, or on
244 ;; VERSION if that is provided. This function is only needed if
245 ;; locking is used for files under this backend, and if files can
246 ;; indeed be locked by other users.
250 ;; * print-log (file)
252 ;; Insert the revision log of FILE into the *vc* buffer.
254 ;; - show-log-entry (version)
256 ;; If provided, search the log entry for VERSION in the current buffer,
257 ;; and make sure it is displayed in the buffer's window. The default
258 ;; implementation of this function works for RCS-style logs.
262 ;; Remove all non-comment information from the output of print-log. The
263 ;; default implementation of this function works for RCS-style logs.
265 ;; - logentry-check ()
267 ;; If defined, this function is run to find out whether the user
268 ;; entered a valid log entry for check-in. The log entry is in the
269 ;; current buffer, and if it is not a valid one, the function should
272 ;; - comment-history (file)
274 ;; Return a string containing all log entries that were made for FILE.
275 ;; This is used for transferring a file from one backend to another,
276 ;; retaining comment information. The default implementation of this
277 ;; function does this by calling print-log and then wash-log, and
278 ;; returning the resulting buffer contents as a string.
280 ;; - update-changelog (files)
282 ;; Using recent log entries, create ChangeLog entries for FILES, or for
283 ;; all files at or below the default-directory if FILES is nil. The
284 ;; default implementation runs rcs2log, which handles RCS- and
287 ;; * diff (file &optional rev1 rev2)
289 ;; Insert the diff for FILE into the *vc-diff* buffer. If REV1 and
290 ;; REV2 are non-nil, report differences from REV1 to REV2. If REV1
291 ;; is nil, use the current workfile version (as found in the
292 ;; repository) as the older version; if REV2 is nil, use the current
293 ;; workfile contents as the newer version. This function should
294 ;; pass the value of (vc-diff-switches-list BACKEND) to the backend
295 ;; command. It should return a status of either 0 (no differences
296 ;; found), or 1 (either non-empty diff or the diff is run
299 ;; - diff-tree (dir &optional rev1 rev2)
301 ;; Insert the diff for all files at and below DIR into the *vc-diff*
302 ;; buffer. The meaning of REV1 and REV2 is the same as for
303 ;; vc-BACKEND-diff. The default implementation does an explicit tree
304 ;; walk, calling vc-BACKEND-diff for each individual file.
306 ;; - annotate-command (file buf rev)
308 ;; If this function is provided, it should produce an annotated version
309 ;; of FILE in BUF, relative to version REV. This is currently only
310 ;; implemented for CVS, using the `cvs annotate' command.
312 ;; - annotate-time ()
314 ;; Only required if `annotate-command' is defined for the backend.
315 ;; Return the time of the next line of annotation at or after point,
316 ;; as a floating point fractional number of days. The helper
317 ;; function `vc-annotate-convert-time' may be useful for converting
318 ;; multi-part times as returned by `current-time' and `encode-time'
319 ;; to this format. Return NIL if no more lines of annotation appear
320 ;; in the buffer. You can safely assume that point is placed at the
321 ;; beginning of each line, starting at `point-min'. The buffer that
322 ;; point is placed in is the Annotate output, as defined by the
325 ;; - annotate-current-time ()
327 ;; Only required if `annotate-command' is defined for the backend,
328 ;; AND you'd like the current time considered to be anything besides
329 ;; (vs-annotate-convert-time (current-time)) -- i.e. the current
330 ;; time with hours, minutes, and seconds included. Probably safe to
331 ;; ignore. Return the current-time, in units of fractional days.
335 ;; - create-snapshot (dir name branchp)
337 ;; Take a snapshot of the current state of files under DIR and name it
338 ;; NAME. This should make sure that files are up-to-date before
339 ;; proceeding with the action. DIR can also be a file and if BRANCHP
340 ;; is specified, NAME should be created as a branch and DIR should be
341 ;; checked out under this new branch. The default implementation does
342 ;; not support branches but does a sanity check, a tree traversal and
343 ;; for each file calls `assign-name'.
345 ;; - assign-name (file name)
347 ;; Give name NAME to the current version of FILE, assuming it is
348 ;; up-to-date. Only used by the default version of `create-snapshot'.
350 ;; - retrieve-snapshot (dir name update)
352 ;; Retrieve a named snapshot of all registered files at or below DIR.
353 ;; If UPDATE is non-nil, then update buffers of any files in the
354 ;; snapshot that are currently visited. The default implementation
355 ;; does a sanity check whether there aren't any uncommitted changes at
356 ;; or below DIR, and then performs a tree walk, using the `checkout'
357 ;; function to retrieve the corresponding versions.
361 ;; - make-version-backups-p (file)
363 ;; Return non-nil if unmodified repository versions of FILE should be
364 ;; backed up locally. If this is done, VC can perform `diff' and
365 ;; `revert' operations itself, without calling the backend system. The
366 ;; default implementation always returns nil.
368 ;; - check-headers ()
370 ;; Return non-nil if the current buffer contains any version headers.
372 ;; - clear-headers ()
374 ;; In the current buffer, reset all version headers to their unexpanded
375 ;; form. This function should be provided if the state-querying code
376 ;; for this backend uses the version headers to determine the state of
377 ;; a file. This function will then be called whenever VC changes the
378 ;; version control state in such a way that the headers would give
379 ;; wrong information.
381 ;; - rename-file (old new)
383 ;; Rename file OLD to NEW, both in the working area and in the
384 ;; repository. If this function is not provided, the command
385 ;; `vc-rename-file' will signal an error.
394 (require 'dired) ; for dired-map-over-marks macro
395 (require 'dired-aux)) ; for dired-kill-{line,tree}
397 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
398 (setq minor-mode-alist
399 (cons '(vc-parent-buffer vc-parent-buffer-name)
402 ;; General customization
405 "Version-control system in Emacs."
408 (defcustom vc-suppress-confirm nil
409 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
413 (defcustom vc-delete-logbuf-window t
414 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
415 If nil, bury that buffer instead.
416 This is most useful if you have multiple windows on a frame and would like to
417 preserve the setting."
421 (defcustom vc-initial-comment nil
422 "*If non-nil, prompt for initial comment when a file is registered."
426 (defcustom vc-default-init-version "1.1"
427 "*A string used as the default version number when a new file is registered.
428 This can be overridden by giving a prefix argument to \\[vc-register]."
433 (defcustom vc-command-messages nil
434 "*If non-nil, display run messages from back-end commands."
438 (defcustom vc-checkin-switches nil
439 "*A string or list of strings specifying extra switches for checkin.
440 These are passed to the checkin program by \\[vc-checkin]."
441 :type '(choice (const :tag "None" nil)
442 (string :tag "Argument String")
443 (repeat :tag "Argument List"
448 (defcustom vc-checkout-switches nil
449 "*A string or list of strings specifying extra switches for checkout.
450 These are passed to the checkout program by \\[vc-checkout]."
451 :type '(choice (const :tag "None" nil)
452 (string :tag "Argument String")
453 (repeat :tag "Argument List"
458 (defcustom vc-register-switches nil
459 "*A string or list of strings; extra switches for registering a file.
460 These are passed to the checkin program by \\[vc-register]."
461 :type '(choice (const :tag "None" nil)
462 (string :tag "Argument String")
463 (repeat :tag "Argument List"
468 (defcustom vc-dired-listing-switches "-al"
469 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
474 (defcustom vc-dired-recurse t
475 "*If non-nil, show directory trees recursively in VC Dired."
480 (defcustom vc-dired-terse-display t
481 "*If non-nil, show only locked files in VC Dired."
486 (defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS")
487 "*List of directory names to be ignored while recursively walking file trees."
488 :type '(repeat string)
491 (defconst vc-maximum-comment-ring-size 32
492 "Maximum number of saved comments in the comment ring.")
494 (defcustom vc-diff-switches nil
495 "*A string or list of strings specifying switches for diff under VC.
496 When running diff under a given BACKEND, VC concatenates the values of
497 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
498 get the switches for that command. Thus, `vc-diff-switches' should
499 contain switches that are specific to version control, but not
500 specific to any particular backend."
501 :type '(choice (const :tag "None" nil)
502 (string :tag "Argument String")
503 (repeat :tag "Argument List"
510 (defcustom vc-checkout-hook nil
511 "*Normal hook (list of functions) run after a file has been checked out.
517 (defcustom vc-annotate-display-mode nil
518 "Which mode to color the annotations with by default."
519 :type '(choice (const :tag "Default" nil)
520 (const :tag "Scale to Oldest" scale)
521 (const :tag "Scale Oldest->Newest" fullscale)
522 (number :tag "Specify Fractional Number of Days"
527 (defcustom vc-checkin-hook nil
528 "*Normal hook (list of functions) run after a checkin is done.
531 :options '(vc-comment-to-change-log)
535 (defcustom vc-before-checkin-hook nil
536 "*Normal hook (list of functions) run before a file gets checked in.
541 (defcustom vc-logentry-check-hook nil
542 "*Normal hook run by `vc-backend-logentry-check'.
543 Use this to impose your own rules on the entry in addition to any the
544 version control backend imposes itself."
548 ;; Annotate customization
549 (defcustom vc-annotate-color-map
567 "*ASSOCIATION list of age versus color, for \\[vc-annotate].
568 Ages are given in units of fractional days. Default is eighteen steps
569 using a twenty day increment."
573 (defcustom vc-annotate-very-old-color "#0046FF"
574 "*Color for lines older than CAR of last cons in `vc-annotate-color-map'."
578 (defcustom vc-annotate-background "black"
579 "*Background color for \\[vc-annotate].
580 Default color is used if nil."
584 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
585 "*Menu elements for the mode-specific menu of VC-Annotate mode.
586 List of factors, used to expand/compress the time scale. See `vc-annotate'."
587 :type '(repeat number)
590 ;; vc-annotate functionality (CVS only).
591 (defvar vc-annotate-mode nil
592 "Variable indicating if VC-Annotate mode is active.")
594 (defvar vc-annotate-mode-map
595 (let ((m (make-sparse-keymap)))
596 (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
598 "Local keymap used for VC-Annotate mode.")
600 (defvar vc-annotate-mode-menu nil
601 "Local keymap used for VC-Annotate mode's menu bar menu.")
603 ;; Header-insertion hair
605 (defcustom vc-static-header-alist
607 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
608 "*Associate static header string templates with file types.
609 A \%s in the template is replaced with the first string associated with
610 the file's version control type in `vc-header-alist'."
611 :type '(repeat (cons :format "%v"
612 (regexp :tag "File Type")
613 (string :tag "Header String")))
616 (defcustom vc-comment-alist
617 '((nroff-mode ".\\\"" ""))
618 "*Special comment delimiters to be used in generating vc headers only.
619 Add an entry in this list if you need to override the normal `comment-start'
620 and `comment-end' variables. This will only be necessary if the mode language
621 is sensitive to blank lines."
622 :type '(repeat (list :format "%v"
624 (string :tag "Comment Start")
625 (string :tag "Comment End")))
628 ;; Default is to be extra careful for super-user.
629 ;; TODO: This variable is no longer used; the corresponding checks
630 ;; are always done now. If that turns out to be fast enough,
631 ;; the variable can be obsoleted.
632 (defcustom vc-checkout-carefully (= (user-uid) 0)
633 "*Non-nil means be extra-careful in checkout.
634 Verify that the file really is not locked
635 and that its contents match what the master file says."
642 ;; Initialization code, to be done just once at load-time
643 (defvar vc-log-mode-map
644 (let ((map (make-sparse-keymap)))
645 (define-key map "\M-n" 'vc-next-comment)
646 (define-key map "\M-p" 'vc-previous-comment)
647 (define-key map "\M-r" 'vc-comment-search-reverse)
648 (define-key map "\M-s" 'vc-comment-search-forward)
649 (define-key map "\C-c\C-c" 'vc-finish-logentry)
651 ;; Compatibility with old name. Should we bother ?
652 (defvar vc-log-entry-mode vc-log-mode-map)
655 ;; Variables the user doesn't need to know about.
656 (defvar vc-log-operation nil)
657 (defvar vc-log-after-operation-hook nil)
658 (defvar vc-annotate-buffers nil
659 "Alist of current \"Annotate\" buffers and their corresponding backends.
660 The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
661 ;; In a log entry buffer, this is a local variable
662 ;; that points to the buffer for which it was made
663 ;; (either a file, or a VC dired buffer).
664 (defvar vc-parent-buffer nil)
665 (put 'vc-parent-buffer 'permanent-local t)
666 (defvar vc-parent-buffer-name nil)
667 (put 'vc-parent-buffer-name 'permanent-local t)
670 (defvar vc-log-version)
672 (defvar vc-dired-mode nil)
673 (make-variable-buffer-local 'vc-dired-mode)
675 (defvar vc-comment-ring (make-ring vc-maximum-comment-ring-size))
676 (defvar vc-comment-ring-index nil)
677 (defvar vc-last-comment-match "")
679 ;; functions that operate on RCS revision numbers. This code should
680 ;; also be moved into the backends. It stays for now, however, since
681 ;; it is used in code below.
682 (defun vc-trunk-p (rev)
683 "Return t if REV is a revision on the trunk."
684 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
686 (defun vc-branch-p (rev)
687 "Return t if REV is a branch revision."
688 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
690 (defun vc-branch-part (rev)
691 "Return the branch part of a revision number REV."
692 (substring rev 0 (string-match "\\.[0-9]+\\'" rev)))
694 (defun vc-minor-part (rev)
695 "Return the minor version number of a revision number REV."
696 (string-match "[0-9]+\\'" rev)
697 (substring rev (match-beginning 0) (match-end 0)))
699 (defun vc-previous-version (rev)
700 "Guess the version number immediately preceding REV."
701 (let ((branch (vc-branch-part rev))
702 (minor-num (string-to-number (vc-minor-part rev))))
704 ;; version does probably not start a branch or release
705 (concat branch "." (number-to-string (1- minor-num)))
707 ;; we are at the beginning of the trunk --
708 ;; don't know anything to return here
710 ;; we are at the beginning of a branch --
711 ;; return version of starting point
712 (vc-branch-part branch)))))
714 ;; File property caching
716 (defun vc-clear-context ()
717 "Clear all cached file properties and the comment ring."
719 (fillarray vc-file-prop-obarray 0)
720 ;; Note: there is potential for minor lossage here if there is an open
721 ;; log buffer with a nonzero local value of vc-comment-ring-index.
722 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
724 (defmacro with-vc-properties (file form settings)
725 "Execute FORM, then set per-file properties for FILE,
726 but only those that have not been set during the execution of FORM.
727 SETTINGS is a list of two-element lists, each of which has the
728 form (PROPERTY . VALUE)."
729 `(let ((vc-touched-properties (list t))
732 (mapcar (lambda (setting)
733 (let ((property (car setting)))
734 (unless (memq property vc-touched-properties)
735 (put (intern filename vc-file-prop-obarray)
736 property (cdr setting)))))
739 ;; Random helper functions
741 (defsubst vc-editable-p (file)
742 (or (eq (vc-checkout-model file) 'implicit)
743 (memq (vc-state file) '(edited needs-merge))))
745 ;; Two macros for elisp programming
747 (defmacro with-vc-file (file comment &rest body)
748 "Check out a writable copy of FILE if necessary and execute the body.
749 Check in FILE with COMMENT (a string) after BODY has been executed.
750 FILE is passed through `expand-file-name'; BODY executed within
751 `save-excursion'. If FILE is not under version control, or locked by
752 somebody else, signal error."
753 `(let ((file (expand-file-name ,file)))
754 (or (vc-backend file)
755 (error (format "File not under version control: `%s'" file)))
756 (unless (vc-editable-p file)
757 (let ((state (vc-state file)))
758 (if (stringp state) (error (format "`%s' is locking `%s'" state file))
759 (vc-checkout file t))))
762 (vc-checkin file nil ,comment)))
763 (put 'with-vc-file 'indent-function 1)
766 (defmacro edit-vc-file (file comment &rest body)
767 "Edit FILE under version control, executing body.
768 Checkin with COMMENT after executing BODY.
769 This macro uses `with-vc-file', passing args to it.
770 However, before executing BODY, find FILE, and after BODY, save buffer."
773 (set-buffer (find-file-noselect ,file))
776 (put 'edit-vc-file 'indent-function 1)
778 (defun vc-ensure-vc-buffer ()
779 "Make sure that the current buffer visits a version-controlled file."
781 (set-buffer (find-file-noselect (dired-get-filename)))
782 (while vc-parent-buffer
783 (pop-to-buffer vc-parent-buffer))
784 (if (not (buffer-file-name))
785 (error "Buffer %s is not associated with a file" (buffer-name))
786 (if (not (vc-backend (buffer-file-name)))
787 (error "File %s is not under version control" (buffer-file-name))))))
789 (defvar vc-binary-assoc nil)
790 (defvar vc-binary-suffixes
791 (if (memq system-type '(ms-dos windows-nt))
792 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
795 (defun vc-process-filter (p s)
796 "An alternative output filter for async process P.
797 The only difference with the default filter is to insert S after markers."
798 (with-current-buffer (process-buffer p)
800 (let ((inhibit-read-only t))
801 (goto-char (process-mark p))
803 (set-marker (process-mark p) (point))))))
805 (defun vc-setup-buffer (&optional buf)
806 "Prepare BUF for executing a VC command and make it the current buffer.
807 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
808 (unless buf (setq buf "*vc*"))
809 (let ((camefrom (current-buffer))
810 (olddir default-directory))
811 (set-buffer (get-buffer-create buf))
812 (kill-all-local-variables)
813 (set (make-local-variable 'vc-parent-buffer) camefrom)
814 (set (make-local-variable 'vc-parent-buffer-name)
815 (concat " from " (buffer-name camefrom)))
816 (setq default-directory olddir)
817 (let ((inhibit-read-only t))
820 (defun vc-exec-after (code)
821 "Eval CODE when the current buffer's process is done.
822 If the current buffer has no process, just evaluate CODE.
823 Else, add CODE to the process' sentinel."
824 (let ((proc (get-buffer-process (current-buffer))))
826 ;; If there's no background process, just execute the code.
827 ((null proc) (eval code))
828 ;; If the background process has exited, reap it and try again
829 ((eq (process-status proc) 'exit)
830 (delete-process proc)
831 (vc-exec-after code))
832 ;; If a process is running, add CODE to the sentinel
833 ((eq (process-status proc) 'run)
834 (let ((sentinel (process-sentinel proc)))
835 (set-process-sentinel proc
837 (with-current-buffer ',(current-buffer)
838 (goto-char (process-mark p))
839 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
841 (car (cdr (cdr ;strip off `lambda (p s)'
843 (list `(vc-exec-after ',code))))))))
844 (t (error "Unexpected process state"))))
847 (defvar vc-post-command-functions nil
848 "Hook run at the end of `vc-do-command'.
849 Each function is called inside the buffer in which the command was run
850 and is passed 3 argument: the COMMAND, the FILE and the FLAGS.")
853 (defun vc-do-command (buffer okstatus command file &rest flags)
854 "Execute a version control command, notifying user and checking for errors.
855 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
856 current buffer if BUFFER is t. If the destination buffer is not
857 already current, set it up properly and erase it. The command is
858 considered successful if its exit status does not exceed OKSTATUS (if
859 OKSTATUS is nil, that means to ignore errors, if it is 'async, that
860 means not to wait for termination of the subprocess). FILE is the
861 name of the working file (may also be nil, to execute commands that
862 don't expect a file name). If an optional list of FLAGS is present,
863 that is inserted into the command line before the filename."
864 (and file (setq file (expand-file-name file)))
865 (if vc-command-messages
866 (message "Running %s on %s..." command file))
868 (unless (or (eq buffer t)
869 (and (stringp buffer)
870 (string= (buffer-name) buffer))
871 (eq buffer (current-buffer)))
872 (vc-setup-buffer buffer))
874 (inhibit-read-only t)
876 (setq squeezed (delq nil (copy-sequence flags)))
878 ;; FIXME: file-relative-name can return a bogus result because
879 ;; it doesn't look at the actual file-system to see if symlinks
881 (setq squeezed (append squeezed (list (file-relative-name file)))))
882 (let ((exec-path (append vc-path exec-path))
883 ;; Add vc-path to PATH for the execution of this command.
885 (cons (concat "PATH=" (getenv "PATH")
887 (mapconcat 'identity vc-path path-separator))
888 process-environment))
889 (w32-quote-process-args t))
890 (if (eq okstatus 'async)
891 (let ((proc (apply 'start-process command (current-buffer) command
893 (unless (active-minibuffer-window)
894 (message "Running %s in the background..." command))
895 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
896 (set-process-filter proc 'vc-process-filter)
898 `(unless (active-minibuffer-window)
899 (message "Running %s in the background... done" ',command))))
900 (setq status (apply 'call-process command nil t nil squeezed))
901 (when (or (not (integerp status)) (and okstatus (< okstatus status)))
902 (pop-to-buffer (current-buffer))
903 (goto-char (point-min))
904 (shrink-window-if-larger-than-buffer)
905 (error "Running %s...FAILED (%s)" command
906 (if (integerp status) (format "status %d" status) status))))
907 (if vc-command-messages
908 (message "Running %s...OK" command)))
910 `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
913 (defun vc-position-context (posn)
914 "Save a bit of the text around POSN in the current buffer.
915 Used to help us find the corresponding position again later
916 if markers are destroyed or corrupted."
917 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
921 (buffer-substring posn
922 (min (point-max) (+ posn 100)))))
924 (defun vc-find-position-by-context (context)
925 "Return the position of CONTEXT in the current buffer, or nil if not found."
926 (let ((context-string (nth 2 context)))
927 (if (equal "" context-string)
930 (let ((diff (- (nth 1 context) (buffer-size))))
931 (if (< diff 0) (setq diff (- diff)))
932 (goto-char (nth 0 context))
933 (if (or (search-forward context-string nil t)
934 ;; Can't use search-backward since the match may continue
936 (progn (goto-char (- (point) diff (length context-string)))
937 ;; goto-char doesn't signal an error at
938 ;; beginning of buffer like backward-char would
939 (search-forward context-string nil t)))
940 ;; to beginning of OSTRING
941 (- (point) (length context-string))))))))
943 (defun vc-context-matches-p (posn context)
944 "Return t if POSN matches CONTEXT, nil otherwise."
945 (let* ((context-string (nth 2 context))
946 (len (length context-string))
948 (if (> end (1+ (buffer-size)))
950 (string= context-string (buffer-substring posn end)))))
952 (defun vc-buffer-context ()
953 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
954 Used by `vc-restore-buffer-context' to later restore the context."
955 (let ((point-context (vc-position-context (point)))
956 ;; Use mark-marker to avoid confusion in transient-mark-mode.
957 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
958 (vc-position-context (mark-marker))))
959 ;; Make the right thing happen in transient-mark-mode.
961 ;; We may want to reparse the compilation buffer after revert
962 (reparse (and (boundp 'compilation-error-list) ;compile loaded
963 (let ((curbuf (current-buffer)))
964 ;; Construct a list; each elt is nil or a buffer
965 ;; iff that buffer is a compilation output buffer
966 ;; that contains markers into the current buffer.
968 (mapcar (lambda (buffer)
971 compilation-old-error-list
972 compilation-error-list))
973 (buffer-error-marked-p nil))
974 (while (and (consp errors)
975 (not buffer-error-marked-p))
976 (and (markerp (cdr (car errors)))
980 (setq buffer-error-marked-p t))
981 (setq errors (cdr errors)))
982 (if buffer-error-marked-p buffer)))
984 (list point-context mark-context reparse)))
986 (defun vc-restore-buffer-context (context)
987 "Restore point/mark, and reparse any affected compilation buffers.
988 CONTEXT is that which `vc-buffer-context' returns."
989 (let ((point-context (nth 0 context))
990 (mark-context (nth 1 context))
991 (reparse (nth 2 context)))
992 ;; Reparse affected compilation buffers.
995 (with-current-buffer (car reparse)
996 (let ((compilation-last-buffer (current-buffer)) ;select buffer
997 ;; Record the position in the compilation buffer of
998 ;; the last error next-error went to.
999 (error-pos (marker-position
1000 (car (car-safe compilation-error-list)))))
1001 ;; Reparse the error messages as far as they were parsed before.
1002 (compile-reinitialize-errors '(4) compilation-parsing-end)
1003 ;; Move the pointer up to find the error we were at before
1004 ;; reparsing. Now next-error should properly go to the next one.
1005 (while (and compilation-error-list
1006 (/= error-pos (car (car compilation-error-list))))
1007 (setq compilation-error-list (cdr compilation-error-list))))))
1008 (setq reparse (cdr reparse)))
1010 ;; if necessary, restore point and mark
1011 (if (not (vc-context-matches-p (point) point-context))
1012 (let ((new-point (vc-find-position-by-context point-context)))
1013 (if new-point (goto-char new-point))))
1016 (not (vc-context-matches-p (mark) mark-context))
1017 (let ((new-mark (vc-find-position-by-context mark-context)))
1018 (if new-mark (set-mark new-mark))))))
1020 (defun vc-revert-buffer1 (&optional arg no-confirm)
1021 "Revert buffer, trying to keep point and mark where user expects them.
1022 Tries to be clever in the face of changes due to expanded version control
1023 key words. This is important for typeahead to work as expected.
1024 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1027 (let ((context (vc-buffer-context)))
1028 ;; Use save-excursion here, because it may be able to restore point
1029 ;; and mark properly even in cases where vc-restore-buffer-context
1030 ;; would fail. However, save-excursion might also get it wrong --
1031 ;; in this case, vc-restore-buffer-context gives it a second try.
1033 ;; t means don't call normal-mode;
1034 ;; that's to preserve various minor modes.
1035 (revert-buffer arg no-confirm t))
1036 (vc-restore-buffer-context context)))
1039 (defun vc-buffer-sync (&optional not-urgent)
1040 "Make sure the current buffer and its working file are in sync.
1041 NOT-URGENT means it is ok to continue if the user says not to save."
1042 (if (buffer-modified-p)
1043 (if (or vc-suppress-confirm
1044 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1047 (error "Aborted")))))
1049 (defun vc-workfile-unchanged-p (file)
1050 "Has FILE changed since last checkout?"
1051 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
1052 (lastmod (nth 5 (file-attributes file))))
1054 (equal checkout-time lastmod)
1055 (let ((unchanged (vc-call workfile-unchanged-p file)))
1056 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
1059 (defun vc-default-workfile-unchanged-p (backend file)
1060 "Default check whether FILE is unchanged: diff against master version."
1061 (zerop (vc-call diff file (vc-workfile-version file))))
1063 (defun vc-default-latest-on-branch-p (backend file)
1064 "Default check whether the current workfile version of FILE is the
1065 latest on its branch."
1068 (defun vc-recompute-state (file)
1069 "Force a recomputation of the version control state of FILE.
1070 The state is computed using the exact, and possibly expensive
1071 function `vc-BACKEND-state', not the heuristic."
1072 (vc-file-setprop file 'vc-state (vc-call state file)))
1074 (defun vc-next-action-on-file (file verbose &optional comment)
1075 "Do The Right Thing for a given version-controlled FILE.
1076 If COMMENT is specified, it will be used as an admin or checkin comment.
1077 If VERBOSE is non-nil, query the user rather than using default parameters."
1078 (let ((visited (get-file-buffer file))
1081 ;; Check relation of buffer and file, and make sure
1082 ;; user knows what he's doing. First, finding the file
1083 ;; will check whether the file on disk is newer.
1085 (find-file-other-window file)
1086 (set-buffer (find-file-noselect file)))
1087 (if (not (verify-visited-file-modtime (current-buffer)))
1088 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
1089 (write-file (buffer-file-name))
1091 ;; Now, check if we have unsaved changes.
1093 (if (buffer-modified-p)
1094 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
1095 (error "Aborted")))))
1097 ;; Do the right thing
1098 (if (not (vc-registered file))
1099 (vc-register verbose comment)
1100 (vc-recompute-state file)
1101 (if visited (vc-mode-line file))
1102 (setq state (vc-state file))
1105 ((or (eq state 'up-to-date)
1106 (and verbose (eq state 'needs-patch)))
1109 ;; go to a different version
1111 (read-string "Branch, version, or backend to move to: "))
1112 (let ((vsym (intern-soft (upcase version))))
1113 (if (member vsym vc-handled-backends)
1114 (vc-transfer-file file vsym)
1115 (vc-checkout file (eq (vc-checkout-model file) 'implicit)
1117 ((not (eq (vc-checkout-model file) 'implicit))
1118 ;; check the file out
1119 (vc-checkout file t))
1122 (message "%s is up-to-date" file))))
1124 ;; Abnormal: edited but read-only
1125 ((and visited (eq state 'edited)
1126 buffer-read-only (not (file-writable-p file)))
1127 ;; Make the file+buffer read-write. If the user really wanted to
1128 ;; commit, he'll get a chance to do that next time around, anyway.
1129 (message "File is edited but read-only; making it writable")
1130 (set-file-modes buffer-file-name
1131 (logior (file-modes buffer-file-name) 128))
1132 (toggle-read-only -1))
1137 ;; For files with locking, if the file does not contain
1138 ;; any changes, just let go of the lock, i.e. revert.
1139 ((and (not (eq (vc-checkout-model file) 'implicit))
1140 (vc-workfile-unchanged-p file)
1141 ;; If buffer is modified, that means the user just
1142 ;; said no to saving it; in that case, don't revert,
1143 ;; because the user might intend to save after
1144 ;; finishing the log entry.
1145 (not (and visited (buffer-modified-p))))
1146 ;; DO NOT revert the file without asking the user!
1147 (if (not visited) (find-file-other-window file))
1148 (if (yes-or-no-p "Revert to master version? ")
1149 (vc-revert-buffer)))
1152 (vc-checkin file nil comment)
1153 (setq version (read-string "New version or backend: "))
1154 (let ((vsym (intern (upcase version))))
1155 (if (member vsym vc-handled-backends)
1156 (vc-transfer-file file vsym)
1157 (vc-checkin file version comment)))))))
1159 ;; locked by somebody else
1162 (error "Sorry, you can't steal the lock on %s this way"
1163 (file-name-nondirectory file)))
1165 (if verbose (read-string "Version to steal: ")
1166 (vc-workfile-version file))
1170 ((eq state 'needs-patch)
1171 (if (yes-or-no-p (format
1172 "%s is not up-to-date. Get latest version? "
1173 (file-name-nondirectory file)))
1174 (vc-checkout file (eq (vc-checkout-model file) 'implicit) "")
1175 (if (and (not (eq (vc-checkout-model file) 'implicit))
1176 (yes-or-no-p "Lock this version? "))
1177 (vc-checkout file t)
1178 (error "Aborted"))))
1181 ((eq state 'needs-merge)
1182 (if (yes-or-no-p (format
1183 "%s is not up-to-date. Merge in changes now? "
1184 (file-name-nondirectory file)))
1185 (vc-maybe-resolve-conflicts file (vc-call merge-news file))
1189 ((eq state 'unlocked-changes)
1190 (if (not visited) (find-file-other-window file))
1191 (if (save-window-excursion
1192 (vc-version-diff file (vc-workfile-version file) nil)
1193 (goto-char (point-min))
1194 (let ((inhibit-read-only t))
1196 (format "Changes to %s since last lock:\n\n" file)))
1198 (yes-or-no-p (concat "File has unlocked changes. "
1199 "Claim lock retaining changes? ")))
1200 (progn (vc-call steal-lock file)
1201 ;; Must clear any headers here because they wouldn't
1202 ;; show that the file is locked now.
1203 (vc-clear-headers file)
1204 (vc-mode-line file))
1205 (if (not (yes-or-no-p
1206 "Revert to checked-in version, instead? "))
1207 (error "Checkout aborted")
1208 (vc-revert-buffer1 t t)
1209 (vc-checkout file t))))))))
1211 (defvar vc-dired-window-configuration)
1213 (defun vc-next-action-dired (file rev comment)
1214 "Call `vc-next-action-on-file' on all the marked files.
1215 Ignores FILE and REV, but passes on COMMENT."
1216 (let ((dired-buffer (current-buffer))
1217 (dired-dir default-directory))
1218 (dired-map-over-marks
1219 (let ((file (dired-get-filename)))
1220 (message "Processing %s..." file)
1221 (vc-next-action-on-file file nil comment)
1222 (set-buffer dired-buffer)
1223 (set-window-configuration vc-dired-window-configuration)
1224 (message "Processing %s...done" file))
1226 (dired-move-to-filename))
1228 ;; Here's the major entry point.
1231 (defun vc-next-action (verbose)
1232 "Do the next logical checkin or checkout operation on the current file.
1234 If you call this from within a VC dired buffer with no files marked,
1235 it will operate on the file in the current line.
1237 If you call this from within a VC dired buffer, and one or more
1238 files are marked, it will accept a log message and then operate on
1239 each one. The log message will be used as a comment for any register
1240 or checkin operations, but ignored when doing checkouts. Attempted
1241 lock steals will raise an error.
1243 A prefix argument lets you specify the version number to use.
1245 For RCS and SCCS files:
1246 If the file is not already registered, this registers it for version
1248 If the file is registered and not locked by anyone, this checks out
1249 a writable and locked file ready for editing.
1250 If the file is checked out and locked by the calling user, this
1251 first checks to see if the file has changed since checkout. If not,
1252 it performs a revert.
1253 If the file has been changed, this pops up a buffer for entry
1254 of a log message; when the message has been entered, it checks in the
1255 resulting changes along with the log message as change commentary. If
1256 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1257 read-only copy of the changed file is left in place afterwards.
1258 If the file is registered and locked by someone else, you are given
1259 the option to steal the lock.
1262 If the file is not already registered, this registers it for version
1263 control. This does a \"cvs add\", but no \"cvs commit\".
1264 If the file is added but not committed, it is committed.
1265 If your working file is changed, but the repository file is
1266 unchanged, this pops up a buffer for entry of a log message; when the
1267 message has been entered, it checks in the resulting changes along
1268 with the logmessage as change commentary. A writable file is retained.
1269 If the repository file is changed, you are asked if you want to
1270 merge in the changes into your working copy."
1275 (let ((files (dired-get-marked-files)))
1276 (set (make-local-variable 'vc-dired-window-configuration)
1277 (current-window-configuration))
1281 (if (not (vc-up-to-date-p f)) "@" ""))
1283 (vc-next-action-dired nil nil "dummy")
1284 (vc-start-entry nil nil nil nil
1285 "Enter a change comment for the marked files."
1286 'vc-next-action-dired))
1288 (while vc-parent-buffer
1289 (pop-to-buffer vc-parent-buffer))
1290 (if buffer-file-name
1291 (vc-next-action-on-file buffer-file-name verbose)
1292 (error "Buffer %s is not associated with a file" (buffer-name)))))
1294 ;; These functions help the vc-next-action entry point
1297 (defun vc-register (&optional set-version comment)
1298 "Register the current file into a version control system.
1299 With prefix argument SET-VERSION, allow user to specify initial version
1300 level. If COMMENT is present, use that as an initial comment.
1302 The version control system to use is found by cycling through the list
1303 `vc-handled-backends'. The first backend in that list which declares
1304 itself responsible for the file (usually because other files in that
1305 directory are already registered under that backend) will be used to
1306 register the file. If no backend declares itself responsible, the
1307 first backend that could register the file is used."
1309 (unless buffer-file-name (error "No visited file"))
1310 (when (vc-backend buffer-file-name)
1311 (if (vc-registered buffer-file-name)
1312 (error "This file is already registered")
1313 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1314 (error "Aborted"))))
1315 ;; Watch out for new buffers of size 0: the corresponding file
1316 ;; does not exist yet, even though buffer-modified-p is nil.
1317 (if (and (not (buffer-modified-p))
1318 (zerop (buffer-size))
1319 (not (file-exists-p buffer-file-name)))
1320 (set-buffer-modified-p t))
1323 (vc-start-entry buffer-file-name
1325 (read-string (format "Initial version level for %s: "
1327 ;; TODO: Use backend-specific init version.
1328 vc-default-init-version)
1329 (or comment (not vc-initial-comment))
1331 "Enter initial comment."
1332 (lambda (file rev comment)
1333 (message "Registering %s... " file)
1334 (let ((backend (vc-responsible-backend file t)))
1335 (vc-file-clearprops file)
1336 (vc-call-backend backend 'register file rev comment)
1337 (vc-file-setprop file 'vc-backend backend)
1338 (unless vc-make-backup-files
1339 (make-local-variable 'backup-inhibited)
1340 (setq backup-inhibited t)))
1341 (message "Registering %s... done" file))))
1344 (defun vc-responsible-backend (file &optional register)
1345 "Return the name of a backend system that is responsible for FILE.
1346 The optional argument REGISTER means that a backend suitable for
1347 registration should be found.
1349 If REGISTER is nil, then if FILE is already registered, return the
1350 backend of FILE. If FILE is not registered, or a directory, then the
1351 first backend in `vc-handled-backends' that declares itself
1352 responsible for FILE is returned. If no backend declares itself
1353 responsible, return the first backend.
1355 If REGISTER is non-nil, return the first responsible backend under
1356 which FILE is not yet registered. If there is no such backend, return
1357 the first backend under which FILE is not yet registered, but could
1359 (if (not vc-handled-backends)
1360 (error "No handled backends"))
1361 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1363 ;; First try: find a responsible backend. If this is for registration,
1364 ;; it must be a backend under which FILE is not yet registered.
1365 (dolist (backend vc-handled-backends)
1366 (and (or (not register)
1367 (not (vc-call-backend backend 'registered file)))
1368 (vc-call-backend backend 'responsible-p file)
1369 (throw 'found backend)))
1370 ;; no responsible backend
1372 ;; if this is not for registration, the first backend must do
1373 (car vc-handled-backends)
1374 ;; for registration, we need to find a new backend that
1375 ;; could register FILE
1376 (dolist (backend vc-handled-backends)
1377 (and (not (vc-call-backend backend 'registered file))
1378 (vc-call-backend backend 'could-register file)
1379 (throw 'found backend)))
1380 (error "No backend that could register")))))
1382 (defun vc-default-responsible-p (backend file)
1383 "Indicate whether BACKEND is reponsible for FILE.
1384 The default is to return nil always."
1387 (defun vc-default-could-register (backend file)
1388 "Return non-nil if BACKEND could be used to register FILE.
1389 The default implementation returns t for all files."
1392 (defun vc-resynch-window (file &optional keep noquery)
1393 "If FILE is in the current buffer, either revert or unvisit it.
1394 The choice between revert (to see expanded keywords) and unvisit depends on
1395 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1396 reverting. NOQUERY should be t *only* if it is known the only
1397 difference between the buffer and the file is due to version control
1398 rather than user editing!"
1399 (and (string= buffer-file-name file)
1402 (vc-revert-buffer1 t noquery)
1403 ;; TODO: Adjusting view mode might no longer be necessary
1404 ;; after RMS change to files.el of 1999-08-08. Investigate
1405 ;; this when we install the new VC.
1407 (if (file-writable-p file)
1409 (let ((view-old-buffer-read-only nil))
1411 (and (not view-mode)
1412 (not (eq (get major-mode 'mode-class) 'special))
1413 (view-mode-enter))))
1414 (vc-mode-line buffer-file-name))
1415 (kill-buffer (current-buffer)))))
1417 (defun vc-resynch-buffer (file &optional keep noquery)
1418 "If FILE is currently visited, resynch its buffer."
1419 (if (string= buffer-file-name file)
1420 (vc-resynch-window file keep noquery)
1421 (let ((buffer (get-file-buffer file)))
1423 (with-current-buffer buffer
1424 (vc-resynch-window file keep noquery)))))
1425 (vc-dired-resynch-file file))
1427 (defun vc-start-entry (file rev comment initial-contents msg action &optional after-hook)
1428 "Accept a comment for an operation on FILE revision REV.
1429 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1430 action on close to ACTION. If COMMENT is a string and
1431 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1432 contents of the log entry buffer. If COMMENT is a string and
1433 INITIAL-CONTENTS is nil, do action immediately as if the user had
1434 entered COMMENT. If COMMENT is t, also do action immediately with an
1435 empty comment. Remember the file's buffer in `vc-parent-buffer'
1436 \(current one if no file). AFTER-HOOK specifies the local value
1437 for vc-log-operation-hook."
1438 (let ((parent (or (and file (get-file-buffer file)) (current-buffer))))
1439 (if vc-before-checkin-hook
1441 (with-current-buffer parent
1442 (run-hooks 'vc-before-checkin-hook))
1443 (run-hooks 'vc-before-checkin-hook)))
1444 (if (and comment (not initial-contents))
1445 (set-buffer (get-buffer-create "*VC-log*"))
1446 (pop-to-buffer (get-buffer-create "*VC-log*")))
1447 (set (make-local-variable 'vc-parent-buffer) parent)
1448 (set (make-local-variable 'vc-parent-buffer-name)
1449 (concat " from " (buffer-name vc-parent-buffer)))
1450 (if file (vc-mode-line file))
1452 (make-local-variable 'vc-log-after-operation-hook)
1454 (setq vc-log-after-operation-hook after-hook))
1455 (setq vc-log-operation action)
1456 (setq vc-log-version rev)
1459 (when (stringp comment) (insert comment)))
1460 (if (or (not comment) initial-contents)
1461 (message "%s Type C-c C-c when done" msg)
1462 (vc-finish-logentry (eq comment t)))))
1464 (defun vc-checkout (file &optional writable rev)
1465 "Retrieve a copy of the revision REV of FILE.
1466 If WRITABLE is non-nil, make sure the retrieved file is writable.
1467 REV defaults to the latest revision.
1469 After check-out, runs the normal hook `vc-checkout-hook'."
1472 (vc-call make-version-backups-p file)
1473 (vc-up-to-date-p file)
1474 (vc-make-version-backup file))
1478 (vc-call checkout file writable rev)
1480 ;; Maybe the backend is not installed ;-(
1482 (let ((buf (get-file-buffer file)))
1483 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1484 (signal (car err) (cdr err))))
1485 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1487 (if (vc-call latest-on-branch-p file)
1491 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1492 (vc-resynch-buffer file t t)
1493 (run-hooks 'vc-checkout-hook))
1495 (defun vc-steal-lock (file rev owner)
1496 "Steal the lock on FILE."
1497 (let (file-description)
1499 (setq file-description (format "%s:%s" file rev))
1500 (setq file-description file))
1501 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1502 file-description owner)))
1503 (error "Steal canceled"))
1504 (compose-mail owner (format "Stolen lock on %s" file-description)
1506 (list (list 'vc-finish-steal file rev)))
1507 (setq default-directory (expand-file-name "~/"))
1508 (goto-char (point-max))
1510 (format "I stole the lock on %s, " file-description)
1511 (current-time-string)
1513 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1515 (defun vc-finish-steal (file version)
1516 ;; This is called when the notification has been sent.
1517 (message "Stealing lock on %s..." file)
1520 (vc-call steal-lock file version)
1521 `((vc-state . edited)))
1522 (vc-resynch-buffer file t t)
1523 (message "Stealing lock on %s...done" file))
1525 (defun vc-checkin (file &optional rev comment initial-contents)
1527 The optional argument REV may be a string specifying the new version
1528 level (if nil increment the current level). COMMENT is a comment
1529 string; if omitted, a buffer is popped up to accept a comment. If
1530 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1531 of the log entry buffer.
1533 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1534 that the version control system supports this mode of operation.
1536 Runs the normal hook `vc-checkin-hook'."
1538 file rev comment initial-contents
1539 "Enter a change comment."
1540 (lambda (file rev comment)
1541 (message "Checking in %s..." file)
1542 ;; "This log message intentionally left almost blank".
1543 ;; RCS 5.7 gripes about white-space-only comments too.
1544 (or (and comment (string-match "[^\t\n ]" comment))
1545 (setq comment "*** empty log message ***"))
1548 ;; Change buffers to get local value of vc-checkin-switches.
1549 (with-current-buffer (or (get-file-buffer file) (current-buffer))
1551 (vc-call checkin file rev comment)
1552 (vc-delete-automatic-version-backups file)))
1553 `((vc-state . up-to-date)
1554 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1555 (vc-workfile-version . nil)))
1556 (message "Checking in %s...done" file))
1559 (defun vc-comment-to-change-log (&optional whoami file-name)
1560 "Enter last VC comment into change log file for current buffer's file.
1561 Optional arg (interactive prefix) non-nil means prompt for user name and site.
1562 Second arg is file name of change log. \
1563 If nil, uses `change-log-default-name'.
1565 May be useful as a `vc-checkin-hook' to update change logs automatically."
1566 (interactive (if current-prefix-arg
1567 (list current-prefix-arg
1568 (prompt-for-change-log-name))))
1569 ;; Make sure the defvar for add-log-current-defun-function has been executed
1570 ;; before binding it.
1572 (let (;; Extract the comment first so we get any error before doing anything.
1573 (comment (ring-ref vc-comment-ring 0))
1574 ;; Don't let add-change-log-entry insert a defun name.
1575 (add-log-current-defun-function 'ignore)
1577 ;; Call add-log to do half the work.
1578 (add-change-log-entry whoami file-name t t)
1579 ;; Insert the VC comment, leaving point before it.
1580 (setq end (save-excursion (insert comment) (point-marker)))
1581 (if (looking-at "\\s *\\s(")
1582 ;; It starts with an open-paren, as in "(foo): Frobbed."
1583 ;; So remove the ": " add-log inserted.
1585 ;; Canonicalize the white space between the file name and comment.
1587 ;; Indent rest of the text the same way add-log indented the first line.
1588 (let ((indentation (current-indentation)))
1590 (while (< (point) end)
1592 (indent-to indentation))
1593 (setq end (point))))
1594 ;; Fill the inserted text, preserving open-parens at bol.
1595 (let ((paragraph-separate (concat paragraph-separate "\\|\\s *\\s("))
1596 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
1598 (fill-region (point) end))
1599 ;; Canonicalize the white space at the end of the entry so it is
1600 ;; separated from the next entry by a single blank line.
1601 (skip-syntax-forward " " end)
1602 (delete-char (- (skip-syntax-backward " ")))
1603 (or (eobp) (looking-at "\n\n")
1606 (defun vc-finish-logentry (&optional nocomment)
1607 "Complete the operation implied by the current log entry."
1609 ;; Check and record the comment, if any.
1611 ;; Comment too long?
1612 (vc-call-backend (or (and vc-log-file (vc-backend vc-log-file))
1613 (vc-responsible-backend default-directory))
1615 (run-hooks 'vc-logentry-check-hook)
1616 ;; Record the comment in the comment ring
1617 (let ((comment (buffer-string)))
1618 (unless (and (ring-p vc-comment-ring)
1619 (not (ring-empty-p vc-comment-ring))
1620 (equal comment (ring-ref vc-comment-ring 0)))
1621 (ring-insert vc-comment-ring comment))))
1622 ;; Sync parent buffer in case the user modified it while editing the comment.
1623 ;; But not if it is a vc-dired buffer.
1624 (with-current-buffer vc-parent-buffer
1625 (or vc-dired-mode (vc-buffer-sync)))
1626 (if (not vc-log-operation) (error "No log operation is pending"))
1627 ;; save the parameters held in buffer-local variables
1628 (let ((log-operation vc-log-operation)
1629 (log-file vc-log-file)
1630 (log-version vc-log-version)
1631 (log-entry (buffer-string))
1632 (after-hook vc-log-after-operation-hook)
1633 (tmp-vc-parent-buffer vc-parent-buffer))
1634 (pop-to-buffer vc-parent-buffer)
1637 (funcall log-operation
1641 ;; Remove checkin window (after the checkin so that if that fails
1642 ;; we don't zap the *VC-log* buffer and the typing therein).
1643 (let ((logbuf (get-buffer "*VC-log*")))
1644 (cond ((and logbuf vc-delete-logbuf-window)
1645 (delete-windows-on logbuf (selected-frame))
1646 ;; Kill buffer and delete any other dedicated windows/frames.
1647 (kill-buffer logbuf))
1648 (logbuf (pop-to-buffer "*VC-log*")
1650 (pop-to-buffer tmp-vc-parent-buffer))))
1651 ;; Now make sure we see the expanded headers
1653 (vc-resynch-buffer log-file vc-keep-workfiles t))
1655 (dired-move-to-filename))
1656 (run-hooks after-hook 'vc-finish-logentry-hook)))
1658 ;; Code for access to the comment ring
1660 (defun vc-new-comment-index (stride len)
1662 (vc-comment-ring-index (+ vc-comment-ring-index stride))
1663 ;; Initialize the index on the first use of this command
1664 ;; so that the first M-p gets index 0, and the first M-n gets
1666 ((> stride 0) (1- stride))
1670 (defun vc-previous-comment (arg)
1671 "Cycle backwards through comment history."
1673 (let ((len (ring-length vc-comment-ring)))
1675 (progn (message "Empty comment ring") (ding))
1677 (setq vc-comment-ring-index (vc-new-comment-index arg len))
1678 (message "Comment %d" (1+ vc-comment-ring-index))
1679 (insert (ring-ref vc-comment-ring vc-comment-ring-index)))))
1681 (defun vc-next-comment (arg)
1682 "Cycle forwards through comment history."
1684 (vc-previous-comment (- arg)))
1686 (defun vc-comment-search-reverse (str &optional stride)
1687 "Search backwards through comment history for substring match."
1688 ;; Why substring rather than regexp ? -sm
1690 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1691 (unless stride (setq stride 1))
1692 (if (string= str "")
1693 (setq str vc-last-comment-match)
1694 (setq vc-last-comment-match str))
1695 (let* ((str (regexp-quote str))
1696 (len (ring-length vc-comment-ring))
1697 (n (vc-new-comment-index stride len)))
1698 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
1699 (not (string-match str (ring-ref vc-comment-ring n))))
1700 (setq n (+ n stride)))
1701 (setq vc-comment-ring-index n)
1702 (vc-previous-comment 0)))
1704 (defun vc-comment-search-forward (str)
1705 "Search forwards through comment history for substring match."
1707 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1708 (vc-comment-search-reverse str -1))
1710 ;; Additional entry points for examining version histories
1713 (defun vc-diff (historic &optional not-urgent)
1714 "Display diffs between file versions.
1715 Normally this compares the current file and buffer with the most recent
1716 checked in version of that file. This uses no arguments.
1717 With a prefix argument, it reads the file name to use
1718 and two version designators specifying which versions to compare."
1719 (interactive (list current-prefix-arg t))
1721 (call-interactively 'vc-version-diff)
1722 (vc-ensure-vc-buffer)
1723 (let ((file buffer-file-name))
1724 (vc-buffer-sync not-urgent)
1725 (if (vc-workfile-unchanged-p buffer-file-name)
1726 (message "No changes to %s since latest version" file)
1727 (vc-version-diff file nil nil)))))
1729 (defun vc-version-diff (file rel1 rel2)
1730 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
1731 If FILE is a directory, generate diffs between versions for all registered
1732 files in or below it."
1734 (let ((file (expand-file-name
1735 (read-file-name (if buffer-file-name
1736 "File or dir to diff: (default visited file) "
1737 "File or dir to diff: ")
1738 default-directory buffer-file-name t)))
1739 (rel1-default nil) (rel2-default nil))
1740 ;; compute default versions based on the file state
1742 ;; if it's a directory, don't supply any version default
1743 ((file-directory-p file)
1745 ;; if the file is not up-to-date, use current version as older version
1746 ((not (vc-up-to-date-p file))
1747 (setq rel1-default (vc-workfile-version file)))
1748 ;; if the file is not locked, use last and previous version as default
1750 (setq rel1-default (vc-previous-version (vc-workfile-version file)))
1751 (if (string= rel1-default "") (setq rel1-default nil))
1752 (setq rel2-default (vc-workfile-version file))))
1753 ;; construct argument list
1755 (read-string (if rel1-default
1756 (concat "Older version: (default "
1759 nil nil rel1-default)
1760 (read-string (if rel2-default
1761 (concat "Newer version: (default "
1763 "Newer version (default: current source): ")
1764 nil nil rel2-default))))
1765 (if (file-directory-p file)
1766 ;; recursive directory diff
1768 (vc-setup-buffer "*vc-diff*")
1769 (if (string-equal rel1 "") (setq rel1 nil))
1770 (if (string-equal rel2 "") (setq rel2 nil))
1771 (let ((inhibit-read-only t))
1772 (insert "Diffs between "
1773 (or rel1 "last version checked in")
1775 (or rel2 "current workfile(s)")
1777 (let ((dir (file-name-as-directory file)))
1778 (vc-call-backend (vc-responsible-backend dir)
1779 'diff-tree dir rel1 rel2))
1780 (vc-exec-after `(let ((inhibit-read-only t))
1781 (insert "\nEnd of diffs.\n"))))
1783 (vc-diff-internal file rel1 rel2))
1784 (set-buffer "*vc-diff*")
1785 (if (and (zerop (buffer-size))
1786 (not (get-buffer-process (current-buffer))))
1790 (message "No changes to %s between %s and %s" file rel1 rel2)
1791 (message "No changes to %s since %s" file rel1))
1792 (message "No changes to %s since latest version" file))
1794 (pop-to-buffer (current-buffer))
1795 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1796 ;; not available. Work around that.
1797 (if (require 'diff-mode nil t) (diff-mode))
1798 (vc-exec-after '(let ((inhibit-read-only t))
1799 (if (eq (buffer-size) 0)
1800 (insert "No differences found.\n"))
1801 (goto-char (point-min))
1802 (shrink-window-if-larger-than-buffer)))
1805 (defun vc-diff-internal (file rel1 rel2)
1806 "Run diff to compare FILE's revisions REL1 and REL2.
1807 Output goes to the current buffer, which is assumed properly set up.
1808 The exit status of the diff command is returned.
1810 This function takes care to set up a proper coding system for diff output.
1811 If both revisions are available as local files, then it also does not
1812 actually call the backend, but performs a local diff."
1813 (if (or (not rel1) (string-equal rel1 ""))
1814 (setq rel1 (vc-workfile-version file)))
1815 (if (string-equal rel2 "")
1817 (let ((file-rel1 (vc-version-backup-file file rel1))
1818 (file-rel2 (if (not rel2)
1820 (vc-version-backup-file file rel2)))
1821 (coding-system-for-read (vc-coding-system-for-diff file)))
1822 (if (and file-rel1 file-rel2)
1823 (apply 'vc-do-command "*vc-diff*" 1 "diff" nil
1824 (append (if (listp diff-switches)
1826 (list diff-switches))
1827 (if (listp vc-diff-switches)
1829 (list vc-diff-switches))
1830 (list (file-relative-name file-rel1)
1831 (file-relative-name file-rel2))))
1832 (vc-call diff file rel1 rel2))))
1834 (defmacro vc-diff-switches-list (backend)
1835 "Make a list of `diff-switches', `vc-diff-switches',
1836 and `vc-BACKEND-diff-switches'."
1838 (if (listp diff-switches) diff-switches (list diff-switches))
1839 (if (listp vc-diff-switches) vc-diff-switches (list vc-diff-switches))
1840 (let ((backend-switches
1841 (eval (intern (concat "vc-" (symbol-name ',backend)
1842 "-diff-switches")))))
1843 (if (listp backend-switches) backend-switches (list backend-switches)))))
1845 (defun vc-default-diff-tree (backend dir rel1 rel2)
1846 "Default implementation for diffing an entire tree at and below DIR.
1847 The meaning of REL1 and REL2 is the same as for `vc-version-diff'."
1848 ;; This implementation does an explicit tree walk, and calls
1849 ;; vc-BACKEND-diff directly for each file. An optimization
1850 ;; would be to use `vc-diff-internal', so that diffs can be local,
1851 ;; and to call it only for files that are actually changed.
1852 ;; However, this is expensive for some backends, and so it is left
1853 ;; to backend-specific implementations.
1854 (setq default-directory dir)
1859 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1860 (message "Looking at %s" ',f)
1861 (vc-call-backend ',(vc-backend f)
1862 'diff ',f ',rel1 ',rel2))))))
1864 (defun vc-coding-system-for-diff (file)
1865 "Return the coding system for reading diff output for FILE."
1866 (or coding-system-for-read
1867 ;; if we already have this file open,
1868 ;; use the buffer's coding system
1869 (let ((buf (find-buffer-visiting file)))
1870 (if buf (with-current-buffer buf
1871 buffer-file-coding-system)))
1872 ;; otherwise, try to find one based on the file name
1873 (car (find-operation-coding-system 'insert-file-contents
1875 ;; and a final fallback
1879 (defun vc-version-other-window (rev)
1880 "Visit version REV of the current buffer in another window.
1881 If the current buffer is named `F', the version is named `F.~REV~'.
1882 If `F.~REV~' already exists, it is used instead of being re-created."
1883 (interactive "sVersion to visit (default is workfile version): ")
1884 (vc-ensure-vc-buffer)
1885 (let* ((file buffer-file-name)
1886 (version (if (string-equal rev "")
1887 (vc-workfile-version file)
1889 (automatic-backup (vc-version-backup-file-name file version))
1890 (manual-backup (vc-version-backup-file-name file version 'manual)))
1891 (unless (file-exists-p manual-backup)
1892 (if (file-exists-p automatic-backup)
1893 (rename-file automatic-backup manual-backup nil)
1894 (vc-call checkout file nil version manual-backup)))
1895 (find-file-other-window manual-backup)))
1897 ;; Header-insertion code
1900 (defun vc-insert-headers ()
1901 "Insert headers in a file for use with your version control system.
1902 Headers desired are inserted at point, and are pulled from
1903 the variable `vc-BACKEND-header'."
1905 (vc-ensure-vc-buffer)
1909 (if (or (not (vc-check-headers))
1910 (y-or-n-p "Version headers already exist. Insert another set? "))
1912 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1913 (comment-start-vc (or (car delims) comment-start "#"))
1914 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1915 (hdsym (vc-make-backend-sym (vc-backend (buffer-file-name))
1917 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1919 (insert comment-start-vc "\t" s "\t"
1920 comment-end-vc "\n"))
1922 (if vc-static-header-alist
1924 (if (string-match (car f) buffer-file-name)
1925 (insert (format (cdr f) (car hdstrings)))))
1926 vc-static-header-alist))
1930 (defun vc-clear-headers (&optional file)
1931 "Clear all version headers in the current buffer (or FILE).
1932 I.e. reset them to the non-expanded form."
1933 (let* ((filename (or file buffer-file-name))
1934 (visited (find-buffer-visiting filename))
1935 (backend (vc-backend filename)))
1936 (when (vc-find-backend-function backend 'clear-headers)
1938 (let ((context (vc-buffer-context)))
1939 ;; save-excursion may be able to relocate point and mark
1940 ;; properly. If it fails, vc-restore-buffer-context
1941 ;; will give it a second try.
1943 (vc-call-backend backend 'clear-headers))
1944 (vc-restore-buffer-context context))
1945 (set-buffer (find-file-noselect filename))
1946 (vc-call-backend backend 'clear-headers)
1947 (kill-buffer filename)))))
1951 "Merge changes between two versions into the current buffer's file.
1952 This asks for two versions to merge from in the minibuffer. If the
1953 first version is a branch number, then merge all changes from that
1954 branch. If the first version is empty, merge news, i.e. recent changes
1955 from the current branch.
1957 See Info node `Merging'."
1959 (vc-ensure-vc-buffer)
1961 (let* ((file buffer-file-name)
1962 (backend (vc-backend file))
1963 (state (vc-state file))
1964 first-version second-version status)
1967 (error "File is locked by %s" state))
1968 ((not (vc-editable-p file))
1970 "File must be checked out for merging. Check out now? ")
1971 (vc-checkout file t)
1972 (error "Merge aborted"))))
1974 (read-string (concat "Branch or version to merge from "
1975 "(default: news on current branch): ")))
1976 (if (string= first-version "")
1977 (if (not (vc-find-backend-function backend 'merge-news))
1978 (error "Sorry, merging news is not implemented for %s" backend)
1979 (setq status (vc-call merge-news file)))
1980 (if (not (vc-find-backend-function backend 'merge))
1981 (error "Sorry, merging is not implemented for %s" backend)
1982 (if (not (vc-branch-p first-version))
1983 (setq second-version
1984 (read-string "Second version: "
1985 (concat (vc-branch-part first-version) ".")))
1986 ;; We want to merge an entire branch. Set versions
1987 ;; accordingly, so that vc-BACKEND-merge understands us.
1988 (setq second-version first-version)
1989 ;; first-version must be the starting point of the branch
1990 (setq first-version (vc-branch-part first-version)))
1991 (setq status (vc-call merge file first-version second-version))))
1992 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
1994 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1995 (vc-resynch-buffer file t (not (buffer-modified-p)))
1996 (if (zerop status) (message "Merge successful")
1997 (if (fboundp 'smerge-mode) (smerge-mode 1))
1998 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
1999 (if (fboundp 'smerge-ediff)
2001 (vc-resolve-conflicts name-A name-B))
2002 (message "File contains conflict markers"))))
2004 (defvar vc-ediff-windows)
2005 (defvar vc-ediff-result)
2007 (defvar ediff-buffer-A)
2008 (defvar ediff-buffer-B)
2009 (defvar ediff-buffer-C)
2010 (require 'ediff-util))
2012 (defun vc-resolve-conflicts (&optional name-A name-B)
2013 "Invoke ediff to resolve conflicts in the current buffer.
2014 The conflicts must be marked with rcsmerge conflict markers."
2016 (vc-ensure-vc-buffer)
2018 (file-name (file-name-nondirectory buffer-file-name))
2019 (your-buffer (generate-new-buffer
2020 (concat "*" file-name
2021 " " (or name-A "WORKFILE") "*")))
2022 (other-buffer (generate-new-buffer
2023 (concat "*" file-name
2024 " " (or name-B "CHECKED-IN") "*")))
2025 (result-buffer (current-buffer)))
2027 (set-buffer your-buffer)
2029 (insert-buffer result-buffer)
2030 (goto-char (point-min))
2031 (while (re-search-forward (concat "^<<<<<<< "
2032 (regexp-quote file-name) "\n") nil t)
2035 (if (not (re-search-forward "^=======\n" nil t))
2036 (error "Malformed conflict marker"))
2038 (let ((start (point)))
2039 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t))
2040 (error "Malformed conflict marker"))
2041 (delete-region start (point))))
2044 (kill-buffer your-buffer)
2045 (kill-buffer other-buffer)
2046 (error "No conflict markers found")))
2047 (set-buffer other-buffer)
2049 (insert-buffer result-buffer)
2050 (goto-char (point-min))
2051 (while (re-search-forward (concat "^<<<<<<< "
2052 (regexp-quote file-name) "\n") nil t)
2053 (let ((start (match-beginning 0)))
2054 (if (not (re-search-forward "^=======\n" nil t))
2055 (error "Malformed conflict marker"))
2056 (delete-region start (point))
2057 (if (not (re-search-forward "^>>>>>>> [0-9.]+\n" nil t))
2058 (error "Malformed conflict marker"))
2059 (replace-match "")))
2060 (let ((config (current-window-configuration))
2061 (ediff-default-variant 'default-B))
2065 (set-buffer (ediff-merge-buffers your-buffer other-buffer))
2067 ;; Ediff is now set up, and we are in the control buffer.
2068 ;; Do a few further adjustments and take precautions for exit.
2070 (make-local-variable 'vc-ediff-windows)
2071 (setq vc-ediff-windows config)
2072 (make-local-variable 'vc-ediff-result)
2073 (setq vc-ediff-result result-buffer)
2074 (make-local-variable 'ediff-quit-hook)
2075 (setq ediff-quit-hook
2077 (let ((buffer-A ediff-buffer-A)
2078 (buffer-B ediff-buffer-B)
2079 (buffer-C ediff-buffer-C)
2080 (result vc-ediff-result)
2081 (windows vc-ediff-windows))
2082 (ediff-cleanup-mess)
2085 (insert-buffer buffer-C)
2086 (kill-buffer buffer-A)
2087 (kill-buffer buffer-B)
2088 (kill-buffer buffer-C)
2089 (set-window-configuration windows)
2090 (message "Conflict resolution finished; you may save the buffer"))))
2091 (message "Please resolve conflicts now; exit ediff when done")
2094 ;; The VC directory major mode. Coopt Dired for this.
2095 ;; All VC commands get mapped into logical equivalents.
2097 (defvar vc-dired-switches)
2098 (defvar vc-dired-terse-mode)
2100 (defvar vc-dired-mode-map
2101 (let ((map (make-sparse-keymap))
2102 (vmap (make-sparse-keymap)))
2103 (define-key map "\C-xv" vmap)
2104 (define-key map "v" vmap)
2105 (set-keymap-parent vmap vc-prefix-map)
2106 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2109 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
2110 "The major mode used in VC directory buffers.
2112 It works like Dired, but lists only files under version control, with
2113 the current VC state of each file being indicated in the place of the
2114 file's link count, owner, group and size. Subdirectories are also
2115 listed, and you may insert them into the buffer as desired, like in
2118 All Dired commands operate normally, with the exception of `v', which
2119 is redefined as the version control prefix, so that you can type
2120 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2121 the file named in the current Dired buffer line. `vv' invokes
2122 `vc-next-action' on this file, or on all files currently marked.
2123 There is a special command, `*l', to mark all files currently locked."
2124 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2125 ;; We do it here because dired might not be loaded yet
2126 ;; when vc-dired-mode-map is initialized.
2127 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2128 (make-local-hook 'dired-after-readin-hook)
2129 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2130 ;; The following is slightly modified from dired.el,
2131 ;; because file lines look a bit different in vc-dired-mode.
2132 (set (make-local-variable 'dired-move-to-filename-regexp)
2134 ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2135 ;; In some locales, month abbreviations are as short as 2 letters,
2136 ;; and they can be padded on the right with spaces.
2137 (month (concat l l "+ *"))
2138 ;; Recognize any non-ASCII character.
2139 ;; The purpose is to match a Kanji character.
2141 ;; (k "[^\x00-\x7f\x80-\xff]")
2143 (yyyy "[0-9][0-9][0-9][0-9]")
2146 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2147 (western (concat "\\(" month s dd "\\|" dd s month "\\)"
2148 s "\\(" HH:MM "\\|" s yyyy"\\|" yyyy s "\\)"))
2149 (japanese (concat mm k s dd k s "\\(" s HH:MM "\\|" yyyy k "\\)")))
2150 ;; the .* below ensures that we find the last match on a line
2151 (concat ".*" s "\\(" western "\\|" japanese "\\)" s)))
2152 (and (boundp 'vc-dired-switches)
2154 (set (make-local-variable 'dired-actual-switches)
2156 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2157 (setq vc-dired-mode t))
2159 (defun vc-dired-toggle-terse-mode ()
2160 "Toggle terse display in VC Dired."
2162 (if (not vc-dired-mode)
2164 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2165 (if vc-dired-terse-mode
2169 (defun vc-dired-mark-locked ()
2170 "Mark all files currently locked."
2172 (dired-mark-if (let ((f (dired-get-filename nil t)))
2174 (not (file-directory-p f))
2175 (not (vc-up-to-date-p f))))
2178 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2180 (defun vc-default-dired-state-info (backend file)
2181 (let ((state (vc-state file)))
2183 ((stringp state) (concat "(" state ")"))
2184 ((eq state 'edited) (concat "(" (vc-user-login-name) ")"))
2185 ((eq state 'needs-merge) "(merge)")
2186 ((eq state 'needs-patch) "(patch)")
2187 ((eq state 'unlocked-changes) "(stale)"))))
2189 (defun vc-dired-reformat-line (x)
2190 "Reformat a directory-listing line.
2191 Replace various columns with version control information.
2192 This code, like dired, assumes UNIX -l format."
2194 (let ((pos (point)) limit perm date-and-file)
2196 (setq limit (point))
2200 (re-search-forward ;; owner and group
2201 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[^ ]+ +[0-9]+\\( .*\\)"
2203 (re-search-forward ;; only owner displayed
2204 "^\\(..[drwxlts-]+ \\) *[0-9]+ [^ ]+ +[0-9]+\\( .*\\)"
2206 (re-search-forward ;; OS/2 -l format, no links, owner, group
2207 "^\\(..[drwxlts-]+ \\) *[0-9]+\\( .*\\)"
2209 (setq perm (match-string 1)
2210 date-and-file (match-string 2))
2211 (setq x (substring (concat x " ") 0 10))
2212 (replace-match (concat perm x date-and-file)))))
2214 (defun vc-dired-hook ()
2215 "Reformat the listing according to version control.
2216 Called by dired after any portion of a vc-dired buffer has been read in."
2217 (message "Getting version information... ")
2218 (let (subdir filename (buffer-read-only nil) cvs-dir)
2219 (goto-char (point-min))
2222 ;; subdir header line
2223 ((setq subdir (dired-get-subdir))
2224 ;; if the backend supports it, get the state
2225 ;; of all files in this directory at once
2226 (let ((backend (vc-responsible-backend subdir)))
2227 (if (vc-find-backend-function backend 'dir-state)
2228 (vc-call-backend backend 'dir-state subdir)))
2230 ;; erase (but don't remove) the "total" line
2231 (delete-region (point) (line-end-position))
2235 ((setq filename (dired-get-filename nil t))
2238 ((file-directory-p filename)
2240 ((member (file-name-nondirectory filename)
2241 vc-directory-exclusion-list)
2242 (let ((pos (point)))
2243 (dired-kill-tree filename)
2246 (vc-dired-terse-mode
2247 ;; Don't show directories in terse mode. Don't use
2248 ;; dired-kill-line to remove it, because in recursive listings,
2249 ;; that would remove the directory contents as well.
2250 (delete-region (line-beginning-position)
2251 (progn (forward-line 1) (point))))
2252 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2255 (vc-dired-reformat-line nil)
2258 ((and (vc-backend filename)
2259 (not (and vc-dired-terse-mode
2260 (vc-up-to-date-p filename))))
2261 (vc-dired-reformat-line (vc-call dired-state-info filename))
2264 (dired-kill-line))))
2266 (t (forward-line 1))))
2268 (message "Getting version information... done")
2271 (cond ((eq (count-lines (point-min) (point-max)) 1)
2272 (goto-char (point-min))
2273 (message "No files locked under %s" default-directory)))))
2275 (defun vc-dired-purge ()
2276 "Remove empty subdirs."
2278 (goto-char (point-min))
2279 (while (setq subdir (dired-get-subdir))
2281 (if (dired-get-filename nil t)
2282 (if (not (dired-next-subdir 1 t))
2283 (goto-char (point-max)))
2285 (if (not (string= (dired-current-directory) default-directory))
2286 (dired-do-kill-lines t "")
2287 ;; We cannot remove the top level directory.
2288 ;; Just make it look a little nicer.
2291 (if (not (dired-next-subdir 1 t))
2292 (goto-char (point-max))))))
2293 (goto-char (point-min))))
2295 (defun vc-dired-buffers-for-dir (dir)
2296 "Return a list of all vc-dired buffers that currently display DIR."
2298 ;; Check whether dired is loaded.
2299 (when (fboundp 'dired-buffers-for-dir)
2300 (mapcar (lambda (buffer)
2301 (with-current-buffer buffer
2303 (setq result (append result (list buffer))))))
2304 (dired-buffers-for-dir dir)))
2307 (defun vc-dired-resynch-file (file)
2308 "Update the entries for FILE in any VC Dired buffers that list it."
2309 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2311 (mapcar (lambda (buffer)
2312 (with-current-buffer buffer
2313 (if (dired-goto-file file)
2314 ;; bind vc-dired-terse-mode to nil so that
2315 ;; files won't vanish when they are checked in
2316 (let ((vc-dired-terse-mode nil))
2317 (dired-do-redisplay 1)))))
2321 (defun vc-directory (dir read-switches)
2322 "Create a buffer in VC Dired Mode for directory DIR.
2324 See Info node `VC Dired Mode'.
2326 With prefix arg READ-SWITCHES, specify a value to override
2327 `dired-listing-switches' when generating the listing."
2328 (interactive "DDired under VC (directory): \nP")
2329 (let ((vc-dired-switches (concat vc-dired-listing-switches
2330 (if vc-dired-recurse "R" ""))))
2332 (setq vc-dired-switches
2333 (read-string "Dired listing switches: "
2334 vc-dired-switches)))
2336 (require 'dired-aux)
2338 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2343 ;; Named-configuration entry points
2345 (defun vc-snapshot-precondition (dir)
2346 "Scan the tree below DIR, looking for non-uptodate files.
2347 If any file is not up-to-date, return the name of the first such file.
2348 \(This means, neither snapshot creation nor retrieval is allowed.\)
2349 If one or more of the files are currently visited, return `visited'.
2350 Otherwise, return nil."
2352 (catch 'vc-locked-example
2356 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2357 (if (get-file-buffer f) (setq status 'visited)))))
2361 (defun vc-create-snapshot (dir name branchp)
2362 "Descending recursively from DIR, make a snapshot called NAME.
2363 For each registered file, the version level of its latest version
2364 becomes part of the named configuration. If the prefix argument
2365 BRANCHP is given, the snapshot is made as a new branch and the files
2366 are checked out in that new branch."
2368 (list (read-file-name "Directory: " default-directory default-directory t)
2369 (read-string "New snapshot name: ")
2370 current-prefix-arg))
2371 (message "Making %s... " (if branchp "branch" "snapshot"))
2372 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2373 (vc-call-backend (vc-responsible-backend dir)
2374 'create-snapshot dir name branchp)
2375 (message "Making %s... done" (if branchp "branch" "snapshot")))
2377 (defun vc-default-create-snapshot (backend dir name branchp)
2379 (error "VC backend %s does not support module branches" backend))
2380 (let ((result (vc-snapshot-precondition dir)))
2381 (if (stringp result)
2382 (error "File %s is not up-to-date" result)
2386 (vc-call assign-name f name))))))
2389 (defun vc-retrieve-snapshot (dir name)
2390 "Descending recursively from DIR, retrieve the snapshot called NAME.
2391 If NAME is empty, it refers to the latest versions.
2392 If locking is used for the files in DIR, then there must not be any
2393 locked files at or below DIR (but if NAME is empty, locked files are
2394 allowed and simply skipped)."
2396 (list (read-file-name "Directory: " default-directory default-directory t)
2397 (read-string "Snapshot name to retrieve (default latest versions): ")))
2398 (let ((update (yes-or-no-p "Update any affected buffers? "))
2399 (msg (if (or (not name) (string= name ""))
2400 (format "Updating %s... " (abbreviate-file-name dir))
2401 (format "Retrieving snapshot into %s... "
2402 (abbreviate-file-name dir)))))
2404 (vc-call-backend (vc-responsible-backend dir)
2405 'retrieve-snapshot dir name update)
2406 (message (concat msg "done"))))
2408 (defun vc-default-retrieve-snapshot (backend dir name update)
2409 (if (string= name "")
2416 (vc-call checkout f nil "")
2417 (if update (vc-resynch-buffer f t t)))))))
2418 (let ((result (vc-snapshot-precondition dir)))
2419 (if (stringp result)
2420 (error "File %s is locked" result)
2421 (setq update (and (eq result 'visited) update))
2424 (lambda (f) (vc-error-occurred
2425 (vc-call checkout f nil name)
2426 (if update (vc-resynch-buffer f t t)))))))))
2428 ;; Miscellaneous other entry points
2431 (defun vc-print-log ()
2432 "List the change log of the current buffer in a window."
2434 (vc-ensure-vc-buffer)
2435 (let ((file buffer-file-name))
2436 (vc-call print-log file)
2438 (pop-to-buffer (current-buffer))
2439 (if (fboundp 'log-view-mode) (log-view-mode))
2442 (goto-char (point-max)) (forward-line -1)
2443 (while (looking-at "=*\n")
2444 (delete-char (- (match-end 0) (match-beginning 0)))
2446 (goto-char (point-min))
2447 (if (looking-at "[\b\t\n\v\f\r ]+")
2448 (delete-char (- (match-end 0) (match-beginning 0))))
2449 (shrink-window-if-larger-than-buffer)
2450 ;; move point to the log entry for the current version
2451 (if (fboundp 'log-view-goto-rev)
2452 (log-view-goto-rev ',(vc-workfile-version file))
2453 (if (vc-find-backend-function ',(vc-backend file) 'show-log-entry)
2454 (vc-call-backend ',(vc-backend file)
2456 ',(vc-workfile-version file))))))))
2458 (defun vc-default-comment-history (backend file)
2459 "Return a string with all log entries that were made under BACKEND for FILE."
2460 (if (vc-find-backend-function backend 'print-log)
2462 (vc-call print-log file)
2463 (vc-call wash-log file)
2466 (defun vc-default-wash-log (backend file)
2467 "Remove all non-comment information from log output.
2468 This default implementation works for RCS logs; backends should override
2469 it if their logs are not in RCS format."
2470 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
2471 "\\(branches: .*;\n\\)?"
2472 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
2473 (goto-char (point-max)) (forward-line -1)
2474 (while (looking-at "=*\n")
2475 (delete-char (- (match-end 0) (match-beginning 0)))
2477 (goto-char (point-min))
2478 (if (looking-at "[\b\t\n\v\f\r ]+")
2479 (delete-char (- (match-end 0) (match-beginning 0))))
2480 (goto-char (point-min))
2481 (re-search-forward separator nil t)
2482 (delete-region (point-min) (point))
2483 (while (re-search-forward separator nil t)
2484 (delete-region (match-beginning 0) (match-end 0)))))
2487 (defun vc-revert-buffer ()
2488 "Revert the current buffer's file back to the version it was based on.
2489 This asks for confirmation if the buffer contents are not identical
2490 to that version. This function does not automatically pick up newer
2491 changes found in the master file; use \\[universal-argument] \\[vc-next-action] to do so."
2493 (vc-ensure-vc-buffer)
2494 (let ((file buffer-file-name)
2495 ;; This operation should always ask for confirmation.
2496 (vc-suppress-confirm nil)
2497 (obuf (current-buffer))
2499 (if (vc-up-to-date-p file)
2500 (unless (yes-or-no-p "File seems up-to-date. Revert anyway? ")
2501 (error "Revert canceled")))
2502 (unless (vc-workfile-unchanged-p file)
2503 ;; vc-diff selects the new window, which is not what we want:
2504 ;; if the new window is on another frame, that'd require the user
2505 ;; moving her mouse to answer the yes-or-no-p question.
2506 (let ((win (save-selected-window
2507 (setq status (vc-diff nil t)) (selected-window))))
2508 (vc-exec-after `(message nil))
2511 (unless (yes-or-no-p "Discard changes? ")
2512 (error "Revert canceled"))
2514 (if (one-window-p t)
2515 (if (window-dedicated-p (selected-window))
2516 (make-frame-invisible))
2517 (delete-window))))))
2520 (message "Reverting %s..." file)
2521 (vc-revert-file file)
2522 (message "Reverting %s...done" file)))
2524 (defun vc-version-backup-file (file &optional rev)
2525 "Return name of backup file for revision REV of FILE.
2526 If version backups should be used for FILE, and there exists
2527 such a backup for REV or the current workfile version of file,
2528 return its name; otherwise return nil."
2529 (when (vc-call make-version-backups-p file)
2530 (let ((backup-file (vc-version-backup-file-name file rev)))
2531 (if (file-exists-p backup-file)
2533 ;; there is no automatic backup, but maybe the user made one manually
2534 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2535 (if (file-exists-p backup-file)
2538 (defun vc-revert-file (file)
2539 "Revert FILE back to the version it was based on."
2542 (let ((backup-file (vc-version-backup-file file)))
2544 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2545 (vc-delete-automatic-version-backups file))
2546 (vc-call revert file backup-file))
2547 `((vc-state . up-to-date)
2548 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2549 (vc-resynch-buffer file t t))
2552 (defun vc-cancel-version (norevert)
2553 "Get rid of most recently checked in version of this file.
2554 A prefix argument NOREVERT means do not revert the buffer afterwards."
2556 (vc-ensure-vc-buffer)
2557 (let* ((file (buffer-file-name))
2558 (backend (vc-backend file))
2559 (target (vc-workfile-version file))
2560 (config (current-window-configuration)) done)
2562 ((not (vc-find-backend-function backend 'cancel-version))
2563 (error "Sorry, canceling versions is not supported under %s" backend))
2564 ((not (vc-call latest-on-branch-p file))
2565 (error "This is not the latest version; VC cannot cancel it"))
2566 ((not (vc-up-to-date-p file))
2567 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
2568 (if (null (yes-or-no-p (format "Remove version %s from master? " target)))
2570 (setq norevert (or norevert (not
2571 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2573 (message "Removing last change from %s..." file)
2576 (vc-call cancel-version file norevert)
2577 `((vc-state . ,(if norevert 'edited 'up-to-date))
2578 (vc-checkout-time . ,(if norevert
2580 (nth 5 (file-attributes file))))
2581 (vc-workfile-version . nil)))
2582 (message "Removing last change from %s...done" file)
2585 (norevert ;; clear version headers and mark the buffer modified
2586 (set-visited-file-name file)
2587 (when (not vc-make-backup-files)
2588 ;; inhibit backup for this buffer
2589 (make-local-variable 'backup-inhibited)
2590 (setq backup-inhibited t))
2591 (setq buffer-read-only nil)
2594 (vc-dired-resynch-file file))
2595 (t ;; revert buffer to file on disk
2596 (vc-resynch-buffer file t t)))
2597 (message "Version %s has been removed from the master" target))))
2600 (defun vc-switch-backend (file backend)
2601 "Make BACKEND the current version control system for FILE.
2602 FILE must already be registered in BACKEND. The change is not
2603 permanent, only for the current session. This function only changes
2604 VC's perspective on FILE, it does not register or unregister it.
2605 By default, this command cycles through the registered backends.
2606 To get a prompt, use a prefix argument."
2610 (let ((backend (vc-backend buffer-file-name))
2612 ;; Find the registered backends.
2613 (dolist (backend vc-handled-backends)
2614 (when (vc-call-backend backend 'registered buffer-file-name)
2615 (push backend backends)))
2616 ;; Find the next backend.
2617 (let ((def (car (delq backend (append (memq backend backends) backends))))
2618 (others (delete backend backends)))
2620 ((null others) (error "No other backend to switch to"))
2625 (format "Switch to backend [%s]: " def)
2626 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2627 nil t nil nil (downcase (symbol-name def))))))
2629 (unless (eq backend (vc-backend file))
2630 (vc-file-clearprops file)
2631 (vc-file-setprop file 'vc-backend backend)
2632 ;; Force recomputation of the state
2633 (unless (vc-call-backend backend 'registered file)
2634 (vc-file-clearprops file)
2635 (error "%s is not registered in %s" file backend))
2636 (vc-mode-line file)))
2639 (defun vc-transfer-file (file new-backend)
2640 "Transfer FILE to another version control system NEW-BACKEND.
2641 If NEW-BACKEND has a higher precedence than FILE's current backend
2642 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2643 NEW-BACKEND, using the version number from the current backend as the
2644 base level. If NEW-BACKEND has a lower precedence than the current
2645 backend, then commit all changes that were made under the current
2646 backend to NEW-BACKEND, and unregister FILE from the current backend.
2647 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2648 (let* ((old-backend (vc-backend file))
2649 (edited (memq (vc-state file) '(edited needs-merge)))
2650 (registered (vc-call-backend new-backend 'registered file))
2652 (and registered ; Never move if not registered in new-backend yet.
2653 ;; move if new-backend comes later in vc-handled-backends
2654 (or (memq new-backend (memq old-backend vc-handled-backends))
2655 (y-or-n-p "Final transfer? "))))
2657 (if (eq old-backend new-backend)
2658 (error "%s is the current backend of %s" new-backend file))
2660 (set-file-modes file (logior (file-modes file) 128))
2661 ;; `registered' might have switched under us.
2662 (vc-switch-backend file old-backend)
2663 (let* ((rev (vc-workfile-version file))
2664 (modified-file (and edited (make-temp-name file)))
2665 (unmodified-file (and modified-file (vc-version-backup-file file))))
2666 ;; Go back to the base unmodified file.
2670 (copy-file file modified-file)
2671 ;; If we have a local copy of the unmodified file, handle that
2672 ;; here and not in vc-revert-file because we don't want to
2673 ;; delete that copy -- it is still useful for OLD-BACKEND.
2675 (copy-file unmodified-file file 'ok-if-already-exists)
2676 (if (y-or-n-p "Get base version from master? ")
2677 (vc-revert-file file))))
2678 (vc-call-backend new-backend 'receive-file file rev))
2680 (vc-switch-backend file new-backend)
2681 (unless (eq (vc-checkout-model file) 'implicit)
2682 (vc-checkout file t nil))
2683 (rename-file modified-file file 'ok-if-already-exists)
2684 (vc-file-setprop file 'vc-checkout-time nil)))))
2686 (vc-switch-backend file old-backend)
2687 (setq comment (vc-call comment-history file))
2688 (vc-call unregister file))
2689 (vc-switch-backend file new-backend)
2690 (when (or move edited)
2691 (vc-file-setprop file 'vc-state 'edited)
2693 (vc-checkin file nil comment (stringp comment)))))
2695 (defun vc-default-unregister (backend file)
2696 "Default implementation of `vc-unregister', signals an error."
2697 (error "Unregistering files is not supported for %s" backend))
2699 (defun vc-default-receive-file (backend file rev)
2700 "Let BACKEND receive FILE from another version control system."
2701 (vc-call-backend backend 'register file rev ""))
2703 (defun vc-rename-master (oldmaster newfile templates)
2704 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2705 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2706 (newdir (or (file-name-directory newfile) ""))
2707 (newbase (file-name-nondirectory newfile))
2709 ;; List of potential master files for `newfile'
2711 (lambda (s) (vc-possible-master s newdir newbase))
2713 (if (or (file-symlink-p oldmaster)
2714 (file-symlink-p (file-name-directory oldmaster)))
2715 (error "This unsafe in the presence of symbolic links"))
2719 ;; If possible, keep the master file in the same directory.
2721 (if (and f (string= (file-name-directory (expand-file-name f))
2725 ;; If not, just use the first possible place.
2728 (or (not (setq dir (file-name-directory f)))
2729 (file-directory-p dir))
2732 (error "New file lacks a version control directory")))))
2735 (defun vc-rename-file (old new)
2736 "Rename file OLD to NEW, and rename its master file likewise."
2737 (interactive "fVC rename file: \nFRename to: ")
2738 (let ((oldbuf (get-file-buffer old))
2739 (backend (vc-backend old)))
2740 (unless (or (null backend) (vc-find-backend-function backend 'rename-file))
2741 (error "Renaming files under %s is not supported in VC" backend))
2742 (if (and oldbuf (buffer-modified-p oldbuf))
2743 (error "Please save files before moving them"))
2744 (if (get-file-buffer new)
2745 (error "Already editing new file name"))
2746 (if (file-exists-p new)
2747 (error "New file already exists"))
2749 (if (and backend (not (vc-up-to-date-p old)))
2750 (error "Please check in files before moving them"))
2751 (vc-call-backend backend 'rename-file old new))
2752 ;; Move the actual file (unless the backend did it already)
2753 (if (or (not backend) (file-exists-p old))
2754 (rename-file old new))
2755 ;; ?? Renaming a file might change its contents due to keyword expansion.
2756 ;; We should really check out a new copy if the old copy was precisely equal
2757 ;; to some checked in version. However, testing for this is tricky....
2759 (with-current-buffer oldbuf
2760 (let ((buffer-read-only buffer-read-only))
2761 (set-visited-file-name new))
2764 (set-buffer-modified-p nil)))))
2766 ;; Only defined in very recent Emacsen
2767 (defvar small-temporary-file-directory nil)
2770 (defun vc-update-change-log (&rest args)
2771 "Find change log file and add entries from recent version control logs.
2772 Normally, find log entries for all registered files in the default
2775 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2777 With any numeric prefix arg, find log entries for all currently visited
2778 files that are under version control. This puts all the entries in the
2779 log for the default directory, which may not be appropriate.
2781 From a program, any ARGS are assumed to be filenames for which
2782 log entries should be gathered."
2784 (cond ((consp current-prefix-arg) ;C-u
2785 (list buffer-file-name))
2786 (current-prefix-arg ;Numeric argument.
2788 (buffers (buffer-list))
2791 (setq file (buffer-file-name (car buffers)))
2792 (and file (vc-backend file)
2793 (setq files (cons file files)))
2794 (setq buffers (cdr buffers)))
2797 ;; Don't supply any filenames to backend; this means
2798 ;; it should find all relevant files relative to
2799 ;; the default-directory.
2801 (vc-call-backend (vc-responsible-backend default-directory)
2802 'update-changelog args))
2804 (defun vc-default-update-changelog (backend files)
2805 "Default implementation of update-changelog.
2806 Uses `rcs2log' which only works for RCS and CVS."
2807 ;; FIXME: We (c|sh)ould add support for cvs2cl
2808 (let ((odefault default-directory)
2809 (changelog (find-change-log))
2810 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2812 (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
2813 (expand-file-name "vc"
2814 (or small-temporary-file-directory
2815 temporary-file-directory))))
2816 (full-name (or add-log-full-name
2819 (format "uid%d" (number-to-string (user-uid)))))
2820 (mailing-address (or add-log-mailing-address
2821 user-mail-address)))
2822 (find-file-other-window changelog)
2823 (barf-if-buffer-read-only)
2826 (goto-char (point-min))
2828 (message "Computing change log entries...")
2829 (message "Computing change log entries... %s"
2832 (setq default-directory odefault)
2833 (if (eq 0 (apply 'call-process
2834 (expand-file-name "rcs2log"
2836 nil (list t tempfile) nil
2838 "-u" (concat (vc-user-login-name)
2840 "\t" mailing-address)
2844 (if (file-name-absolute-p f)
2846 (concat odefault f))))
2850 (set-buffer (get-buffer-create "*vc*")))
2852 (insert-file tempfile)
2854 (setq default-directory (file-name-directory changelog))
2855 (delete-file tempfile)))))
2857 ;; Annotate functionality
2859 ;; Declare globally instead of additional parameter to
2860 ;; temp-buffer-show-function (not possible to pass more than one
2861 ;; parameter). The use of annotate-ratio is deprecated in favor of
2862 ;; annotate-mode, which replaces it with the more sensible "span-to
2863 ;; days", along with autoscaling support.
2864 (defvar vc-annotate-ratio nil "Global variable.")
2865 (defvar vc-annotate-backend nil "Global variable.")
2867 (defun vc-annotate-get-backend (buffer)
2868 "Return the backend matching \"Annotate\" buffer BUFFER.
2869 Return NIL if no match made. Associations are made based on
2870 `vc-annotate-buffers'."
2871 (cdr (assoc buffer vc-annotate-buffers)))
2873 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2874 "Major mode for buffers displaying output from the `annotate' command.
2876 You can use the mode-specific menu to alter the time-span of the used
2877 colors. See variable `vc-annotate-menu-elements' for customizing the
2879 (vc-annotate-add-menu))
2881 (defun vc-annotate-display-default (&optional ratio)
2882 "Use the default color spectrum for VC Annotate mode, scaling the
2883 colormap by RATIO, if present. Use the current time as offset."
2885 (message "Redisplaying annotation...")
2886 (vc-annotate-display
2887 (if ratio (vc-annotate-time-span vc-annotate-color-map ratio)))
2888 (message "Redisplaying annotation...done"))
2890 (defun vc-annotate-display-autoscale (&optional full)
2891 "Re-display annotation using colormap scaled from the current time
2892 to the oldest annotation in the buffer, or, with argument FULL set, to
2893 cover the full time range, from oldest to newest."
2896 (oldest 999999.) ;Any CVS users at the founding of Rome?
2897 (current (vc-annotate-convert-time (current-time)))
2899 (message "Redisplaying annotation...")
2900 ;; Run through this file and find the oldest and newest dates annotated.
2902 (goto-char (point-min))
2903 (while (setq date (vc-call-backend vc-annotate-backend 'annotate-time))
2907 (setq oldest date))))
2908 (vc-annotate-display
2909 (vc-annotate-time-span ;return the scaled colormap.
2910 vc-annotate-color-map
2911 (/ (- (if full newest current) oldest)
2912 (vc-annotate-car-last-cons vc-annotate-color-map)))
2914 (message "Redisplaying annotation...done \(%s\)"
2916 (format "Spanned from %.1f to %.1f days old"
2919 (format "Spanned to %.1f days old" (- current oldest))))))
2921 ;; Menu -- Using easymenu.el
2922 (defun vc-annotate-add-menu ()
2923 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
2924 (let ((menu-elements vc-annotate-menu-elements)
2927 ["Default" (unless (null vc-annotate-display-mode)
2928 (setq vc-annotate-display-mode nil)
2929 (vc-annotate-display-select))
2930 :style toggle :selected (null vc-annotate-display-mode)]))
2931 (oldest-in-map (vc-annotate-car-last-cons vc-annotate-color-map)))
2932 (while menu-elements
2933 (let* ((element (car menu-elements))
2934 (days (* element oldest-in-map)))
2935 (setq menu-elements (cdr menu-elements))
2938 `([,(format "Span %.1f days" days)
2939 (unless (and (numberp vc-annotate-display-mode)
2940 (= vc-annotate-display-mode ,days))
2941 (vc-annotate-display-select nil ,days))
2942 :style toggle :selected
2943 (and (numberp vc-annotate-display-mode)
2944 (= vc-annotate-display-mode ,days)) ])))))
2950 (float (string-to-number
2951 (read-string "Span how many days? ")))))
2952 (vc-annotate-display-select nil days)) t])
2956 (unless (eq vc-annotate-display-mode 'scale)
2957 (vc-annotate-display-select nil 'scale))
2958 :style toggle :selected
2959 (eq vc-annotate-display-mode 'scale)])
2961 ["Span Oldest->Newest"
2962 (unless (eq vc-annotate-display-mode 'fullscale)
2963 (vc-annotate-display-select nil 'fullscale))
2964 :style toggle :selected
2965 (eq vc-annotate-display-mode 'fullscale)])))
2967 (if (or (featurep 'easymenu) (load "easymenu" t))
2968 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
2969 "VC Annotate Display Menu" menu-def))))
2971 (defun vc-annotate-display-select (&optional buffer mode)
2972 "Do the default or chosen annotation display as specified in the
2973 customizable variable `vc-annotate-display-mode'."
2975 (if mode (setq vc-annotate-display-mode mode))
2978 (display-buffer buffer))
2979 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
2981 (cond ((null vc-annotate-display-mode) (vc-annotate-display-default
2983 ((symbolp vc-annotate-display-mode) ; One of the auto-scaling modes
2984 (cond ((eq vc-annotate-display-mode 'scale)
2985 (vc-annotate-display-autoscale))
2986 ((eq vc-annotate-display-mode 'fullscale)
2987 (vc-annotate-display-autoscale t))
2988 (t (error "No such display mode: %s"
2989 vc-annotate-display-mode))))
2990 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
2991 (vc-annotate-display-default
2992 (/ vc-annotate-display-mode (vc-annotate-car-last-cons
2993 vc-annotate-color-map))))
2994 (t (error "Error in display mode select"))))
2996 ;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
2997 ;;;; Execute "annotate" on FILE by using `call-process' and insert
2998 ;;;; the contents in BUFFER.
3001 (defun vc-annotate (prefix)
3002 "Display the edit history of the current file using colours.
3004 This command creates a buffer that shows, for each line of the current
3005 file, when it was last edited and by whom. Additionally, colours are
3006 used to show the age of each line--blue means oldest, red means
3007 youngest, and intermediate colours indicate intermediate ages. By
3008 default, the time scale stretches back one year into the past;
3009 everything that is older than that is shown in blue.
3011 With a prefix argument, this command asks two questions in the
3012 minibuffer. First, you may enter a version number; then the buffer
3013 displays and annotates that version instead of the current version
3014 \(type RET in the minibuffer to leave that default unchanged). Then,
3015 you are prompted for the time span in days which the color range
3016 should cover. For example, a time span of 20 days means that changes
3017 over the past 20 days are shown in red to blue, according to their
3018 age, and everything that is older than that is shown in blue.
3020 Customization variables:
3022 `vc-annotate-menu-elements' customizes the menu elements of the
3023 mode-specific menu. `vc-annotate-color-map' and
3024 `vc-annotate-very-old-color' defines the mapping of time to
3025 colors. `vc-annotate-background' specifies the background color."
3027 (vc-ensure-vc-buffer)
3028 (let* ((temp-buffer-name (concat "*Annotate " (buffer-name) "*"))
3029 (temp-buffer-show-function 'vc-annotate-display-select)
3030 (rev (vc-workfile-version (buffer-file-name)))
3031 (vc-annotate-version
3032 (if prefix (read-string
3033 (format "Annotate from version: (default %s) " rev)
3037 (setq vc-annotate-display-mode
3038 (float (string-to-number
3039 (read-string "Annotate span days: (default 20) "
3041 (setq vc-annotate-backend (vc-backend (buffer-file-name)))
3042 (message "Annotating...")
3043 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
3044 (error "Sorry, annotating is not implemented for %s"
3045 vc-annotate-backend))
3046 (with-output-to-temp-buffer temp-buffer-name
3047 (vc-call-backend vc-annotate-backend 'annotate-command
3048 (file-name-nondirectory (buffer-file-name))
3049 (get-buffer temp-buffer-name)
3050 vc-annotate-version))
3051 ;; Don't use the temp-buffer-name until the buffer is created
3052 ;; (only after `with-output-to-temp-buffer'.)
3053 (setq vc-annotate-buffers
3054 (append vc-annotate-buffers
3055 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend))))
3056 (message "Annotating... done")))
3058 (defun vc-annotate-car-last-cons (a-list)
3059 "Return car of last cons in association list A-LIST."
3060 (if (not (eq nil (cdr a-list)))
3061 (vc-annotate-car-last-cons (cdr a-list))
3062 (car (car a-list))))
3064 (defun vc-annotate-time-span (a-list span &optional quantize)
3065 "Apply factor SPAN to the time-span of association list A-LIST.
3066 Return the new alist.
3067 Optionally quantize to the factor of QUANTIZE."
3068 ;; Apply span to each car of every cons
3069 (if (not (eq nil a-list))
3070 (append (list (cons (* (car (car a-list)) span)
3071 (cdr (car a-list))))
3072 (vc-annotate-time-span (nthcdr (or quantize ; optional
3074 a-list) span quantize))))
3076 (defun vc-annotate-compcar (threshold a-list)
3077 "Test successive cons cells of association list A-LIST against THRESHOLD.
3078 Return the first cons cell which car is not less than THRESHOLD,
3081 (tmp-cons (car a-list)))
3082 (while (and tmp-cons (< (car tmp-cons) threshold))
3083 (setq tmp-cons (car (nthcdr i a-list)))
3085 tmp-cons)) ; Return the appropriate value
3087 (defun vc-annotate-convert-time (time)
3088 "Convert high/low times, as returned by `current-time' and
3089 `encode-time', to a single floating point value in units of days.
3090 TIME is list, only the first two elements of TIME are considered,
3091 comprising the high 16 and low 16 bits of the number of seconds since
3093 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3095 (defun vc-annotate-difference (&optional offset)
3096 "Calculate the difference, in days, from the current time and the
3097 time returned from the backend function annotate-time. If OFFSET is
3098 set, use it as the time base instead of the current time."
3099 (let ((next-time (vc-call-backend vc-annotate-backend 'annotate-time)))
3102 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3105 (defun vc-default-annotate-current-time (backend)
3106 "Return the current time, encoded as fractional days."
3107 (vc-annotate-convert-time (current-time)))
3109 (defun vc-annotate-display (&optional color-map offset)
3110 "Do the VC-Annotate display in BUFFER using COLOR-MAP, and time
3111 offset OFFSET (defaults to the present time). You probably want
3112 `vc-annotate-select' instead, after setting
3113 `vc-annotate-display-mode'"
3115 (goto-char (point-min)) ; Position at the top of the buffer.
3116 ;; Delete old overlays
3119 (if (overlay-get overlay 'vc-annotation)
3120 (delete-overlay overlay)))
3121 (overlays-in (point-min) (point-max)))
3122 (goto-char (point-min)) ; Position at the top of the buffer.
3124 (while (setq difference (vc-annotate-difference offset))
3126 ((color (or (vc-annotate-compcar
3127 difference (or color-map vc-annotate-color-map))
3128 (cons nil vc-annotate-very-old-color)))
3129 ;; substring from index 1 to remove any leading `#' in the name
3130 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
3131 ;; Make the face if not done.
3132 (face (or (intern-soft face-name)
3133 (let ((tmp-face (make-face (intern face-name))))
3134 (set-face-foreground tmp-face (cdr color))
3135 (if vc-annotate-background
3136 (set-face-background tmp-face
3137 vc-annotate-background))
3138 tmp-face))) ; Return the face
3142 (setq overlay (make-overlay point (point)))
3143 (overlay-put overlay 'face face)
3144 (overlay-put overlay 'vc-annotation t))))))
3146 ;; Collect back-end-dependent stuff here
3148 (defalias 'vc-default-logentry-check 'ignore)
3150 (defun vc-check-headers ()
3151 "Check if the current file has any headers in it."
3153 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3155 (defun vc-default-check-headers (backend)
3156 "Default implementation of check-headers; always returns nil."
3159 ;; Back-end-dependent stuff ends here.
3161 ;; Set up key bindings for use while editing log messages
3163 (define-derived-mode vc-log-mode text-mode "VC-Log"
3164 "Major mode for editing VC log entries.
3165 These bindings are added to the global keymap when you enter this mode:
3166 \\[vc-next-action] perform next logical version-control operation on current file
3167 \\[vc-register] register current file
3168 \\[vc-toggle-read-only] like next-action, but won't register files
3169 \\[vc-insert-headers] insert version-control headers in current file
3170 \\[vc-print-log] display change history of current file
3171 \\[vc-revert-buffer] revert buffer to latest version
3172 \\[vc-cancel-version] undo latest checkin
3173 \\[vc-diff] show diffs between file versions
3174 \\[vc-version-other-window] visit old version in another window
3175 \\[vc-directory] show all files locked by any user in or below .
3176 \\[vc-annotate] colorful display of the cvs annotate command
3177 \\[vc-update-change-log] add change log entry from recent checkins
3179 While you are entering a change log message for a version, the following
3180 additional bindings will be in effect.
3182 \\[vc-finish-logentry] proceed with check in, ending log message entry
3184 Whenever you do a checkin, your log comment is added to a ring of
3185 saved comments. These can be recalled as follows:
3187 \\[vc-next-comment] replace region with next message in comment ring
3188 \\[vc-previous-comment] replace region with previous message in comment ring
3189 \\[vc-comment-search-reverse] search backward for regexp in the comment ring
3190 \\[vc-comment-search-forward] search backward for regexp in the comment ring
3192 Entry to the change-log submode calls the value of `text-mode-hook', then
3193 the value of `vc-log-mode-hook'.
3195 Global user options:
3196 `vc-initial-comment' If non-nil, require user to enter a change
3197 comment upon first checkin of the file.
3199 `vc-keep-workfiles' Non-nil value prevents workfiles from being
3200 deleted when changes are checked in
3202 `vc-suppress-confirm' Suppresses some confirmation prompts.
3204 vc-BACKEND-header Which keywords to insert when adding headers
3205 with \\[vc-insert-headers]. Defaults to
3206 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under
3209 `vc-static-header-alist' By default, version headers inserted in C files
3210 get stuffed in a static string area so that
3211 ident(RCS/CVS) or what(SCCS) can see them in
3212 the compiled object code. You can override
3213 this by setting this variable to nil, or change
3214 the header template by changing it.
3216 `vc-command-messages' if non-nil, display run messages from the
3217 actual version-control utilities (this is
3218 intended primarily for people hacking vc
3220 (make-local-variable 'vc-comment-ring-index))
3222 (defun vc-log-edit (file)
3223 "Set up `log-edit' for use with VC on FILE.
3224 If `log-edit' is not available, resort to `vc-log-mode'."
3225 (setq default-directory
3226 (if file (file-name-directory file)
3227 (with-current-buffer vc-parent-buffer default-directory)))
3228 (if (fboundp 'log-edit)
3229 (log-edit 'vc-finish-logentry nil
3230 (if file `(lambda () ',(list (file-name-nondirectory file)))
3231 ;; If FILE is nil, we were called from vc-dired.
3233 (with-current-buffer vc-parent-buffer
3234 (dired-get-marked-files t)))))
3236 (set (make-local-variable 'vc-log-file) file)
3237 (make-local-variable 'vc-log-version)
3238 (set-buffer-modified-p nil)
3239 (setq buffer-file-name nil))
3241 ;; These things should probably be generally available
3243 (defun vc-file-tree-walk (dirname func &rest args)
3244 "Walk recursively through DIRNAME.
3245 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3246 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3247 (message "Traversing directory %s...done" dirname))
3249 (defun vc-file-tree-walk-internal (file func args)
3250 (if (not (file-directory-p file))
3251 (if (vc-backend file) (apply func file args))
3252 (message "Traversing directory %s..." (abbreviate-file-name file))
3253 (let ((dir (file-name-as-directory file)))
3256 (string-equal f ".")
3257 (string-equal f "..")
3258 (member f vc-directory-exclusion-list)
3259 (let ((dirf (expand-file-name f dir)))
3261 (file-symlink-p dirf);; Avoid possible loops
3262 (vc-file-tree-walk-internal dirf func args)))))
3263 (directory-files dir)))))
3267 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3269 ;; These may be useful to anyone who has to debug or extend the package.
3270 ;; (Note that this information corresponds to versions 5.x. Some of it
3271 ;; might have been invalidated by the additions to support branching
3272 ;; and RCS keyword lookup. AS, 1995/03/24)
3274 ;; A fundamental problem in VC is that there are time windows between
3275 ;; vc-next-action's computations of the file's version-control state and
3276 ;; the actions that change it. This is a window open to lossage in a
3277 ;; multi-user environment; someone else could nip in and change the state
3278 ;; of the master during it.
3280 ;; The performance problem is that rlog/prs calls are very expensive; we want
3281 ;; to avoid them as much as possible.
3285 ;; The performance problem, it turns out, simplifies in practice to the
3286 ;; problem of making vc-state fast. The two other functions that call
3287 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3288 ;; makes snapshots, the other deletes the calling user's last change in the
3291 ;; The race condition implies that we have to either (a) lock the master
3292 ;; during the entire execution of vc-next-action, or (b) detect and
3293 ;; recover from errors resulting from dispatch on an out-of-date state.
3295 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3296 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3297 ;; checkin, the change message buffer pops up, and the user, having wandered
3298 ;; off to do something else, simply forgets about it?
3300 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3301 ;; vc-state. Usually, if a file is registered, we can read its locked/
3302 ;; unlocked state and its current owner from its permissions.
3304 ;; This shortcut will fail if someone has manually changed the workfile's
3305 ;; permissions; also if developers are munging the workfile in several
3306 ;; directories, with symlinks to a master (in this latter case, the
3307 ;; permissions shortcut will fail to detect a lock asserted from another
3310 ;; Note that these cases correspond exactly to the errors which could happen
3311 ;; because of a competing checkin/checkout race in between two instances of
3314 ;; For VC's purposes, a workfile/master pair may have the following states:
3316 ;; A. Unregistered. There is a workfile, there is no master.
3318 ;; B. Registered and not locked by anyone.
3320 ;; C. Locked by calling user and unchanged.
3322 ;; D. Locked by the calling user and changed.
3324 ;; E. Locked by someone other than the calling user.
3326 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3328 ;; VC's idea of state
3330 ;; V Actual state RCS action SCCS action Effect
3332 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3333 ;; B 5 . 6 7 8 co -l get -e checkout
3334 ;; C 9 10 . 11 12 co -u unget; get revert
3335 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3336 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3338 ;; All commands take the master file name as a last argument (not shown).
3340 ;; In the discussion below, a "self-race" is a pathological situation in
3341 ;; which VC operations are being attempted simultaneously by two or more
3342 ;; Emacsen running under the same username.
3344 ;; The vc-next-action code has the following windows:
3347 ;; Between the check for existence of a master file and the call to
3348 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3349 ;; never close if the initial-comment feature is on.
3352 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3353 ;; following revert (apparent state C).
3356 ;; Between the call to vc-workfile-unchanged-p in and the following
3357 ;; checkin (apparent state D). This window may never close.
3360 ;; Between the unlock and the immediately following checkout during a
3361 ;; revert operation (apparent state C). Included in window Q.
3364 ;; Between vc-state and the following checkout (apparent state B).
3367 ;; Between vc-state and the following revert (apparent state C).
3368 ;; Includes windows Q and S.
3371 ;; Between vc-state and the following checkin (apparent state
3372 ;; D). This window may never be closed if the user fails to complete the
3373 ;; checkin message. Includes window R.
3376 ;; Between vc-state and the following steal-lock (apparent
3377 ;; state E). This window may never close if the user fails to complete
3378 ;; the steal-lock message. Includes window X.
3381 ;; Between the unlock and the immediately following re-lock during a
3382 ;; steal-lock operation (apparent state E). This window may never close
3383 ;; if the user fails to complete the steal-lock message.
3387 ;; Apparent state A ---
3389 ;; 1. File looked unregistered but is actually registered and not locked.
3391 ;; Potential cause: someone else's admin during window P, with
3392 ;; caller's admin happening before their checkout.
3394 ;; RCS: Prior to version 5.6.4, ci fails with message
3395 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3396 ;; ci -i option and the message is "<file>,v: already exists".
3397 ;; SCCS: admin will fail with error (ad19).
3399 ;; We can let these errors be passed up to the user.
3401 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3403 ;; Potential cause: self-race during window P.
3405 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3406 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3407 ;; ci -i option, failing with message "<file>,v: already exists".
3408 ;; SCCS: will fail with error (ad19).
3410 ;; Either of these consequences is acceptable.
3412 ;; 3. File looked unregistered but is actually locked by caller, changed.
3414 ;; Potential cause: self-race during window P.
3416 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3417 ;; a delta with a null change comment (the -t- switch will be
3418 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3419 ;; failing with message "<file>,v: already exists".
3420 ;; SCCS: will fail with error (ad19).
3422 ;; 4. File looked unregistered but is locked by someone else.
3424 ;; Potential cause: someone else's admin during window P, with
3425 ;; caller's admin happening *after* their checkout.
3427 ;; RCS: Prior to version 5.6.4, ci fails with a
3428 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3429 ;; VC uses the new ci -i option, failing with message
3430 ;; "<file>,v: already exists".
3431 ;; SCCS: will fail with error (ad19).
3433 ;; We can let these errors be passed up to the user.
3435 ;; Apparent state B ---
3437 ;; 5. File looked registered and not locked, but is actually unregistered.
3439 ;; Potential cause: master file got nuked during window P.
3441 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3442 ;; SCCS: will fail with error ut4.
3444 ;; We can let these errors be passed up to the user.
3446 ;; 6. File looked registered and not locked, but is actually locked by the
3447 ;; calling user and unchanged.
3449 ;; Potential cause: self-race during window T.
3451 ;; RCS: in the same directory as the previous workfile, co -l will fail
3452 ;; with "co error: writable foo exists; checkout aborted". In any other
3453 ;; directory, checkout will succeed.
3454 ;; SCCS: will fail with ge17.
3456 ;; Either of these consequences is acceptable.
3458 ;; 7. File looked registered and not locked, but is actually locked by the
3459 ;; calling user and changed.
3463 ;; 8. File looked registered and not locked, but is actually locked by another
3466 ;; Potential cause: someone else checks it out during window T.
3468 ;; RCS: co error: revision 1.3 already locked by <user>
3469 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3471 ;; We can let these errors be passed up to the user.
3473 ;; Apparent state C ---
3475 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3479 ;; 10. File looks locked by calling user and unchanged, but is actually not
3482 ;; Potential cause: a self-race in window U, or by the revert's
3483 ;; landing during window X of some other user's steal-lock or window S
3484 ;; of another user's revert.
3486 ;; RCS: succeeds, refreshing the file from the identical version in
3488 ;; SCCS: fails with error ut4 (p file nonexistent).
3490 ;; Either of these consequences is acceptable.
3492 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3495 ;; Potential cause: the file would have to be touched by a self-race
3498 ;; The revert will succeed, removing whatever changes came with
3499 ;; the touch. It is theoretically possible that work could be lost.
3501 ;; 12. File looks like it's locked by the calling user and unchanged, but
3502 ;; it's actually locked by someone else.
3504 ;; Potential cause: a steal-lock in window V.
3506 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3507 ;; SCCS: fails with error un2
3509 ;; We can pass these errors up to the user.
3511 ;; Apparent state D ---
3513 ;; 13. File looks like it's locked by the calling user and changed, but it's
3514 ;; actually unregistered.
3516 ;; Potential cause: master file got nuked during window P.
3518 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3519 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3520 ;; option, failing with message "no such file or directory".
3521 ;; SCCS: will fail with error ut4.
3523 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3524 ;; VC may fail to detect the loss of previous version information.
3526 ;; 14. File looks like it's locked by the calling user and changed, but it's
3527 ;; actually unlocked.
3529 ;; Potential cause: self-race in window V, or the checkin happening
3530 ;; during the window X of someone else's steal-lock or window S of
3531 ;; someone else's revert.
3533 ;; RCS: ci will fail with "no lock set by <user>".
3534 ;; SCCS: delta will fail with error ut4.
3536 ;; 15. File looks like it's locked by the calling user and changed, but it's
3537 ;; actually locked by the calling user and unchanged.
3539 ;; Potential cause: another self-race --- a whole checkin/checkout
3540 ;; sequence by the calling user would have to land in window R.
3542 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3543 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3544 ;; the file unlocked.
3546 ;; It is theoretically possible that work could be lost under RCS.
3548 ;; 16. File looks like it's locked by the calling user and changed, but it's
3549 ;; actually locked by a different user.
3551 ;; RCS: ci error: no lock set by <user>
3552 ;; SCCS: unget will fail with error un2
3554 ;; We can pass these errors up to the user.
3556 ;; Apparent state E ---
3558 ;; 17. File looks like it's locked by some other user, but it's actually
3563 ;; 18. File looks like it's locked by some other user, but it's actually
3566 ;; Potential cause: someone released a lock during window W.
3568 ;; RCS: The calling user will get the lock on the file.
3569 ;; SCCS: unget -n will fail with cm4.
3571 ;; Either of these consequences will be OK.
3573 ;; 19. File looks like it's locked by some other user, but it's actually
3574 ;; locked by the calling user and unchanged.
3576 ;; Potential cause: the other user relinquishing a lock followed by
3577 ;; a self-race, both in window W.
3579 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3580 ;; the sequence a no-op.
3582 ;; 20. File looks like it's locked by some other user, but it's actually
3583 ;; locked by the calling user and changed.
3589 ;; In order of decreasing severity:
3591 ;; Cases 11 and 15 are the only ones that potentially lose work.
3592 ;; They would require a self-race for this to happen.
3594 ;; Case 13 in RCS loses information about previous deltas, retaining
3595 ;; only the information in the current workfile. This can only happen
3596 ;; if the master file gets nuked in window P.
3598 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3599 ;; no change comment in the master. This would require a self-race in
3600 ;; window P or R respectively.
3602 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3604 ;; Unfortunately, it appears to me that no recovery is possible in these
3605 ;; cases. They don't yield error messages, so there's no way to tell that
3606 ;; a race condition has occurred.
3608 ;; All other cases don't change either the workfile or the master, and
3609 ;; trigger command errors which the user will see.
3611 ;; Thus, there is no explicit recovery code.