New backend functions `delete-file' and `repository-hostname'.
[emacs.git] / lisp / vc.el
blob43580f8decb5f15dad967a8caf7d37be16d1e0ad
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>
7 ;; Keywords: tools
9 ;; $Id: vc.el,v 1.351 2003/05/08 17:41:16 monnier 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)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Credits:
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 ;; J.D. Smith <jdsmith@alum.mit.edu>
40 ;; Andre Spiegel <spiegel@gnu.org>
41 ;; Richard Stallman <rms@gnu.org>
42 ;; Thien-Thi Nguyen <ttn@gnu.org>
44 ;;; Commentary:
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
56 ;; NExTSTEP 3.0.
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
73 ;; the file.
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.
106 ;; * state (file)
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'.
121 ;; - dir-state (dir)
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
137 ;; default.
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
151 ;; vc-BACKEND-diff. (Note that vc-BACKEND-diff must not run
152 ;; asynchronously in this case.)
154 ;; - mode-line-string (file)
156 ;; If provided, this function should return the VC-specific mode line
157 ;; string for FILE. The default implementation deals well with all
158 ;; states that `vc-state' can return.
160 ;; - dired-state-info (file)
162 ;; Translate the `vc-state' property of FILE into a string that can be
163 ;; used in a vc-dired buffer. The default implementation deals well
164 ;; with all states that `vc-state' can return.
166 ;; STATE-CHANGING FUNCTIONS
168 ;; * register (file &optional rev comment)
170 ;; Register FILE in this backend. Optionally, an initial revision REV
171 ;; and an initial description of the file, COMMENT, may be specified.
172 ;; The implementation should pass the value of vc-register-switches
173 ;; to the backend command.
175 ;; - init-version (file)
177 ;; The initial version to use when registering FILE if one is not
178 ;; specified by the user. If not provided, the variable
179 ;; vc-default-init-version is used instead.
181 ;; - responsible-p (file)
183 ;; Return non-nil if this backend considers itself "responsible" for
184 ;; FILE, which can also be a directory. This function is used to find
185 ;; out what backend to use for registration of new files and for things
186 ;; like change log generation. The default implementation always
187 ;; returns nil.
189 ;; - could-register (file)
191 ;; Return non-nil if FILE could be registered under this backend. The
192 ;; default implementation always returns t.
194 ;; - receive-file (file rev)
196 ;; Let this backend "receive" a file that is already registered under
197 ;; another backend. The default implementation simply calls `register'
198 ;; for FILE, but it can be overridden to do something more specific,
199 ;; e.g. keep revision numbers consistent or choose editing modes for
200 ;; FILE that resemble those of the other backend.
202 ;; - unregister (file)
204 ;; Unregister FILE from this backend. This is only needed if this
205 ;; backend may be used as a "more local" backend for temporary editing.
207 ;; * checkin (file rev comment)
209 ;; Commit changes in FILE to this backend. If REV is non-nil, that
210 ;; should become the new revision number. COMMENT is used as a
211 ;; check-in comment. The implementation should pass the value of
212 ;; vc-checkin-switches to the backend command.
214 ;; * find-version (file rev buffer)
216 ;; Fetch revision REV of file FILE and put it into BUFFER.
217 ;; If REV is the empty string, fetch the head of the trunk.
218 ;; The implementation should pass the value of vc-checkout-switches
219 ;; to the backend command.
221 ;; * checkout (file &optional editable rev)
223 ;; Check out revision REV of FILE into the working area. If EDITABLE
224 ;; is non-nil, FILE should be writable by the user and if locking is
225 ;; used for FILE, a lock should also be set. If REV is non-nil, that
226 ;; is the revision to check out (default is current workfile version).
227 ;; If REV is t, that means to check out the head of the current branch;
228 ;; if it is the empty string, check out the head of the trunk.
229 ;; The implementation should pass the value of vc-checkout-switches
230 ;; to the backend command.
232 ;; * revert (file &optional contents-done)
234 ;; Revert FILE back to the current workfile version. If optional
235 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
236 ;; already been reverted from a version backup, and this function
237 ;; only needs to update the status of FILE within the backend.
239 ;; - cancel-version (file editable)
241 ;; Cancel the current workfile version of FILE, i.e. remove it from the
242 ;; master. EDITABLE non-nil means that FILE should be writable
243 ;; afterwards, and if locking is used for FILE, then a lock should also
244 ;; be set. If this function is not provided, trying to cancel a
245 ;; version is caught as an error.
247 ;; - merge (file rev1 rev2)
249 ;; Merge the changes between REV1 and REV2 into the current working file.
251 ;; - merge-news (file)
253 ;; Merge recent changes from the current branch into FILE.
255 ;; - steal-lock (file &optional version)
257 ;; Steal any lock on the current workfile version of FILE, or on
258 ;; VERSION if that is provided. This function is only needed if
259 ;; locking is used for files under this backend, and if files can
260 ;; indeed be locked by other users.
262 ;; HISTORY FUNCTIONS
264 ;; * print-log (file)
266 ;; Insert the revision log of FILE into the *vc* buffer.
268 ;; - show-log-entry (version)
270 ;; If provided, search the log entry for VERSION in the current buffer,
271 ;; and make sure it is displayed in the buffer's window. The default
272 ;; implementation of this function works for RCS-style logs.
274 ;; - wash-log (file)
276 ;; Remove all non-comment information from the output of print-log. The
277 ;; default implementation of this function works for RCS-style logs.
279 ;; - logentry-check ()
281 ;; If defined, this function is run to find out whether the user
282 ;; entered a valid log entry for check-in. The log entry is in the
283 ;; current buffer, and if it is not a valid one, the function should
284 ;; throw an error.
286 ;; - comment-history (file)
288 ;; Return a string containing all log entries that were made for FILE.
289 ;; This is used for transferring a file from one backend to another,
290 ;; retaining comment information. The default implementation of this
291 ;; function does this by calling print-log and then wash-log, and
292 ;; returning the resulting buffer contents as a string.
294 ;; - update-changelog (files)
296 ;; Using recent log entries, create ChangeLog entries for FILES, or for
297 ;; all files at or below the default-directory if FILES is nil. The
298 ;; default implementation runs rcs2log, which handles RCS- and
299 ;; CVS-style logs.
301 ;; * diff (file &optional rev1 rev2)
303 ;; Insert the diff for FILE into the *vc-diff* buffer. If REV1 and
304 ;; REV2 are non-nil, report differences from REV1 to REV2. If REV1
305 ;; is nil, use the current workfile version (as found in the
306 ;; repository) as the older version; if REV2 is nil, use the current
307 ;; workfile contents as the newer version. This function should
308 ;; pass the value of (vc-switches BACKEND 'diff) to the backend
309 ;; command. It should return a status of either 0 (no differences
310 ;; found), or 1 (either non-empty diff or the diff is run
311 ;; asynchronously).
313 ;; - diff-tree (dir &optional rev1 rev2)
315 ;; Insert the diff for all files at and below DIR into the *vc-diff*
316 ;; buffer. The meaning of REV1 and REV2 is the same as for
317 ;; vc-BACKEND-diff. The default implementation does an explicit tree
318 ;; walk, calling vc-BACKEND-diff for each individual file.
320 ;; - annotate-command (file buf rev)
322 ;; If this function is provided, it should produce an annotated version
323 ;; of FILE in BUF, relative to version REV. This is currently only
324 ;; implemented for CVS, using the `cvs annotate' command.
326 ;; - annotate-time ()
328 ;; Only required if `annotate-command' is defined for the backend.
329 ;; Return the time of the next line of annotation at or after point,
330 ;; as a floating point fractional number of days. The helper
331 ;; function `vc-annotate-convert-time' may be useful for converting
332 ;; multi-part times as returned by `current-time' and `encode-time'
333 ;; to this format. Return nil if no more lines of annotation appear
334 ;; in the buffer. You can safely assume that point is placed at the
335 ;; beginning of each line, starting at `point-min'. The buffer that
336 ;; point is placed in is the Annotate output, as defined by the
337 ;; relevant backend.
339 ;; - annotate-current-time ()
341 ;; Only required if `annotate-command' is defined for the backend,
342 ;; AND you'd like the current time considered to be anything besides
343 ;; (vs-annotate-convert-time (current-time)) -- i.e. the current
344 ;; time with hours, minutes, and seconds included. Probably safe to
345 ;; ignore. Return the current-time, in units of fractional days.
347 ;; SNAPSHOT SYSTEM
349 ;; - create-snapshot (dir name branchp)
351 ;; Take a snapshot of the current state of files under DIR and name it
352 ;; NAME. This should make sure that files are up-to-date before
353 ;; proceeding with the action. DIR can also be a file and if BRANCHP
354 ;; is specified, NAME should be created as a branch and DIR should be
355 ;; checked out under this new branch. The default implementation does
356 ;; not support branches but does a sanity check, a tree traversal and
357 ;; for each file calls `assign-name'.
359 ;; - assign-name (file name)
361 ;; Give name NAME to the current version of FILE, assuming it is
362 ;; up-to-date. Only used by the default version of `create-snapshot'.
364 ;; - retrieve-snapshot (dir name update)
366 ;; Retrieve a named snapshot of all registered files at or below DIR.
367 ;; If UPDATE is non-nil, then update buffers of any files in the
368 ;; snapshot that are currently visited. The default implementation
369 ;; does a sanity check whether there aren't any uncommitted changes at
370 ;; or below DIR, and then performs a tree walk, using the `checkout'
371 ;; function to retrieve the corresponding versions.
373 ;; MISCELLANEOUS
375 ;; - make-version-backups-p (file)
377 ;; Return non-nil if unmodified repository versions of FILE should be
378 ;; backed up locally. If this is done, VC can perform `diff' and
379 ;; `revert' operations itself, without calling the backend system. The
380 ;; default implementation always returns nil.
382 ;; - repository-hostname (dirname)
384 ;; Return the hostname that the backend will have to contact
385 ;; in order to operate on a file in DIRNAME. If the return value
386 ;; is nil, it is means that the repository is local.
387 ;; This function is used in `vc-stay-local-p' which backends can use
388 ;; for their convenience.
390 ;; - previous-version (file rev)
392 ;; Return the version number that precedes REV for FILE.
394 ;; - check-headers ()
396 ;; Return non-nil if the current buffer contains any version headers.
398 ;; - clear-headers ()
400 ;; In the current buffer, reset all version headers to their unexpanded
401 ;; form. This function should be provided if the state-querying code
402 ;; for this backend uses the version headers to determine the state of
403 ;; a file. This function will then be called whenever VC changes the
404 ;; version control state in such a way that the headers would give
405 ;; wrong information.
407 ;; - delete-file (file)
409 ;; Delete FILE and mark it as deleted in the repository. If this
410 ;; function is not provided, the command `vc-delete-file' will
411 ;; signal an error.
413 ;; - rename-file (old new)
415 ;; Rename file OLD to NEW, both in the working area and in the
416 ;; repository. If this function is not provided, the renaming
417 ;; will be done by (vc-delete-file old) and (vc-register new).
420 ;;; Code:
422 (require 'vc-hooks)
423 (require 'ring)
424 (eval-when-compile
425 (require 'cl)
426 (require 'compile)
427 (require 'dired) ; for dired-map-over-marks macro
428 (require 'dired-aux)) ; for dired-kill-{line,tree}
430 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
431 (setq minor-mode-alist
432 (cons '(vc-parent-buffer vc-parent-buffer-name)
433 minor-mode-alist)))
435 ;; General customization
437 (defgroup vc nil
438 "Version-control system in Emacs."
439 :group 'tools)
441 (defcustom vc-suppress-confirm nil
442 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
443 :type 'boolean
444 :group 'vc)
446 (defcustom vc-delete-logbuf-window t
447 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
448 If nil, bury that buffer instead.
449 This is most useful if you have multiple windows on a frame and would like to
450 preserve the setting."
451 :type 'boolean
452 :group 'vc)
454 (defcustom vc-initial-comment nil
455 "*If non-nil, prompt for initial comment when a file is registered."
456 :type 'boolean
457 :group 'vc)
459 (defcustom vc-default-init-version "1.1"
460 "*A string used as the default version number when a new file is registered.
461 This can be overridden by giving a prefix argument to \\[vc-register]. This
462 can also be overridden by a particular VC backend."
463 :type 'string
464 :group 'vc
465 :version "20.3")
467 (defcustom vc-command-messages nil
468 "*If non-nil, display run messages from back-end commands."
469 :type 'boolean
470 :group 'vc)
472 (defcustom vc-checkin-switches nil
473 "*A string or list of strings specifying extra switches for checkin.
474 These are passed to the checkin program by \\[vc-checkin]."
475 :type '(choice (const :tag "None" nil)
476 (string :tag "Argument String")
477 (repeat :tag "Argument List"
478 :value ("")
479 string))
480 :group 'vc)
482 (defcustom vc-checkout-switches nil
483 "*A string or list of strings specifying extra switches for checkout.
484 These are passed to the checkout program by \\[vc-checkout]."
485 :type '(choice (const :tag "None" nil)
486 (string :tag "Argument String")
487 (repeat :tag "Argument List"
488 :value ("")
489 string))
490 :group 'vc)
492 (defcustom vc-register-switches nil
493 "*A string or list of strings; extra switches for registering a file.
494 These are passed to the checkin program by \\[vc-register]."
495 :type '(choice (const :tag "None" nil)
496 (string :tag "Argument String")
497 (repeat :tag "Argument List"
498 :value ("")
499 string))
500 :group 'vc)
502 (defcustom vc-dired-listing-switches "-al"
503 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
504 :type 'string
505 :group 'vc
506 :version "21.1")
508 (defcustom vc-dired-recurse t
509 "*If non-nil, show directory trees recursively in VC Dired."
510 :type 'boolean
511 :group 'vc
512 :version "20.3")
514 (defcustom vc-dired-terse-display t
515 "*If non-nil, show only locked files in VC Dired."
516 :type 'boolean
517 :group 'vc
518 :version "20.3")
520 (defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS")
521 "*List of directory names to be ignored when walking directory trees."
522 :type '(repeat string)
523 :group 'vc)
525 (defconst vc-maximum-comment-ring-size 32
526 "Maximum number of saved comments in the comment ring.")
528 (defcustom vc-diff-switches nil
529 "*A string or list of strings specifying switches for diff under VC.
530 When running diff under a given BACKEND, VC concatenates the values of
531 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
532 get the switches for that command. Thus, `vc-diff-switches' should
533 contain switches that are specific to version control, but not
534 specific to any particular backend."
535 :type '(choice (const :tag "None" nil)
536 (string :tag "Argument String")
537 (repeat :tag "Argument List"
538 :value ("")
539 string))
540 :group 'vc
541 :version "21.1")
543 ;;;###autoload
544 (defcustom vc-checkout-hook nil
545 "*Normal hook (list of functions) run after checking out a file.
546 See `run-hooks'."
547 :type 'hook
548 :group 'vc
549 :version "21.1")
551 (defcustom vc-annotate-display-mode nil
552 "Which mode to color the output of \\[vc-annotate] with by default."
553 :type '(choice (const :tag "Default" nil)
554 (const :tag "Scale to Oldest" scale)
555 (const :tag "Scale Oldest->Newest" fullscale)
556 (number :tag "Specify Fractional Number of Days"
557 :value "20.5"))
558 :group 'vc)
560 ;;;###autoload
561 (defcustom vc-checkin-hook nil
562 "*Normal hook (list of functions) run after a checkin is done.
563 See `run-hooks'."
564 :type 'hook
565 :options '(vc-comment-to-change-log)
566 :group 'vc)
568 ;;;###autoload
569 (defcustom vc-before-checkin-hook nil
570 "*Normal hook (list of functions) run before a file is checked in.
571 See `run-hooks'."
572 :type 'hook
573 :group 'vc)
575 (defcustom vc-logentry-check-hook nil
576 "*Normal hook run by `vc-backend-logentry-check'.
577 Use this to impose your own rules on the entry in addition to any the
578 version control backend imposes itself."
579 :type 'hook
580 :group 'vc)
582 ;; Annotate customization
583 (defcustom vc-annotate-color-map
584 '(( 20. . "#FF0000")
585 ( 40. . "#FF3800")
586 ( 60. . "#FF7000")
587 ( 80. . "#FFA800")
588 (100. . "#FFE000")
589 (120. . "#E7FF00")
590 (140. . "#AFFF00")
591 (160. . "#77FF00")
592 (180. . "#3FFF00")
593 (200. . "#07FF00")
594 (220. . "#00FF31")
595 (240. . "#00FF69")
596 (260. . "#00FFA1")
597 (280. . "#00FFD9")
598 (300. . "#00EEFF")
599 (320. . "#00B6FF")
600 (340. . "#007EFF"))
601 "*Association list of age versus color, for \\[vc-annotate].
602 Ages are given in units of fractional days. Default is eighteen steps
603 using a twenty day increment."
604 :type 'alist
605 :group 'vc)
607 (defcustom vc-annotate-very-old-color "#0046FF"
608 "*Color for lines older than the current color range in \\[vc-annotate]]."
609 :type 'string
610 :group 'vc)
612 (defcustom vc-annotate-background "black"
613 "*Background color for \\[vc-annotate].
614 Default color is used if nil."
615 :type 'string
616 :group 'vc)
618 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
619 "*Menu elements for the mode-specific menu of VC-Annotate mode.
620 List of factors, used to expand/compress the time scale. See `vc-annotate'."
621 :type '(repeat number)
622 :group 'vc)
624 ;; vc-annotate functionality (CVS only).
625 (defvar vc-annotate-mode nil
626 "Variable indicating if VC-Annotate mode is active.")
628 (defvar vc-annotate-mode-map
629 (let ((m (make-sparse-keymap)))
630 (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
632 "Local keymap used for VC-Annotate mode.")
634 (defvar vc-annotate-mode-menu nil
635 "Local keymap used for VC-Annotate mode's menu bar menu.")
637 ;; Header-insertion hair
639 (defcustom vc-static-header-alist
640 '(("\\.c$" .
641 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
642 "*Associate static header string templates with file types.
643 A \%s in the template is replaced with the first string associated with
644 the file's version control type in `vc-header-alist'."
645 :type '(repeat (cons :format "%v"
646 (regexp :tag "File Type")
647 (string :tag "Header String")))
648 :group 'vc)
650 (defcustom vc-comment-alist
651 '((nroff-mode ".\\\"" ""))
652 "*Special comment delimiters for generating VC headers.
653 Add an entry in this list if you need to override the normal `comment-start'
654 and `comment-end' variables. This will only be necessary if the mode language
655 is sensitive to blank lines."
656 :type '(repeat (list :format "%v"
657 (symbol :tag "Mode")
658 (string :tag "Comment Start")
659 (string :tag "Comment End")))
660 :group 'vc)
662 (defcustom vc-checkout-carefully (= (user-uid) 0)
663 "*Non-nil means be extra-careful in checkout.
664 Verify that the file really is not locked
665 and that its contents match what the master file says."
666 :type 'boolean
667 :group 'vc)
668 (make-obsolete-variable 'vc-checkout-carefully
669 "the corresponding checks are always done now."
670 "21.1")
673 ;; The main keymap
675 ;; Initialization code, to be done just once at load-time
676 (defvar vc-log-mode-map
677 (let ((map (make-sparse-keymap)))
678 (set-keymap-parent map text-mode-map)
679 (define-key map "\M-n" 'vc-next-comment)
680 (define-key map "\M-p" 'vc-previous-comment)
681 (define-key map "\M-r" 'vc-comment-search-reverse)
682 (define-key map "\M-s" 'vc-comment-search-forward)
683 (define-key map "\C-c\C-c" 'vc-finish-logentry)
684 map))
685 ;; Compatibility with old name. Should we bother ?
686 (defvar vc-log-entry-mode vc-log-mode-map)
689 ;; Variables the user doesn't need to know about.
690 (defvar vc-log-operation nil)
691 (defvar vc-log-after-operation-hook nil)
692 (defvar vc-annotate-buffers nil
693 "Alist of current \"Annotate\" buffers and their corresponding backends.
694 The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
695 ;; In a log entry buffer, this is a local variable
696 ;; that points to the buffer for which it was made
697 ;; (either a file, or a VC dired buffer).
698 (defvar vc-parent-buffer nil)
699 (put 'vc-parent-buffer 'permanent-local t)
700 (defvar vc-parent-buffer-name nil)
701 (put 'vc-parent-buffer-name 'permanent-local t)
703 (defvar vc-log-file)
704 (defvar vc-log-version)
706 (defvar vc-dired-mode nil)
707 (make-variable-buffer-local 'vc-dired-mode)
709 (defvar vc-comment-ring (make-ring vc-maximum-comment-ring-size))
710 (defvar vc-comment-ring-index nil)
711 (defvar vc-last-comment-match "")
713 ;; functions that operate on RCS revision numbers. This code should
714 ;; also be moved into the backends. It stays for now, however, since
715 ;; it is used in code below.
716 (defun vc-trunk-p (rev)
717 "Return t if REV is a revision on the trunk."
718 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
720 (defun vc-branch-p (rev)
721 "Return t if REV is a branch revision."
722 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
724 ;;;###autoload
725 (defun vc-branch-part (rev)
726 "Return the branch part of a revision number REV."
727 (let ((index (string-match "\\.[0-9]+\\'" rev)))
728 (if index
729 (substring rev 0 index))))
731 (defun vc-minor-part (rev)
732 "Return the minor version number of a revision number REV."
733 (string-match "[0-9]+\\'" rev)
734 (substring rev (match-beginning 0) (match-end 0)))
736 (defun vc-default-previous-version (backend file rev)
737 "Guess the version number immediately preceding REV for FILE.
738 This default implementation works for <major>.<minor>-style version numbers
739 as used by RCS and CVS."
740 (let ((branch (vc-branch-part rev))
741 (minor-num (string-to-number (vc-minor-part rev))))
742 (when branch
743 (if (> minor-num 1)
744 ;; version does probably not start a branch or release
745 (concat branch "." (number-to-string (1- minor-num)))
746 (if (vc-trunk-p rev)
747 ;; we are at the beginning of the trunk --
748 ;; don't know anything to return here
750 ;; we are at the beginning of a branch --
751 ;; return version of starting point
752 (vc-branch-part branch))))))
754 ;; File property caching
756 (defun vc-clear-context ()
757 "Clear all cached file properties and the comment ring."
758 (interactive)
759 (fillarray vc-file-prop-obarray 0)
760 ;; Note: there is potential for minor lossage here if there is an open
761 ;; log buffer with a nonzero local value of vc-comment-ring-index.
762 (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
764 (defmacro with-vc-properties (file form settings)
765 "Execute FORM, then maybe set per-file properties for FILE.
766 SETTINGS is an association list of property/value pairs. After
767 executing FORM, set those properties from SETTINGS that have not yet
768 been updated to their corresponding values."
769 (declare (debug t))
770 `(let ((vc-touched-properties (list t)))
771 ,form
772 (mapcar (lambda (setting)
773 (let ((property (car setting)))
774 (unless (memq property vc-touched-properties)
775 (put (intern ,file vc-file-prop-obarray)
776 property (cdr setting)))))
777 ,settings)))
779 ;; Random helper functions
781 (defsubst vc-editable-p (file)
782 "Return non-nil if FILE can be edited."
783 (or (eq (vc-checkout-model file) 'implicit)
784 (memq (vc-state file) '(edited needs-merge))))
786 ;; Two macros for elisp programming
787 ;;;###autoload
788 (defmacro with-vc-file (file comment &rest body)
789 "Check out a writable copy of FILE if necessary, then execute BODY.
790 Check in FILE with COMMENT (a string) after BODY has been executed.
791 FILE is passed through `expand-file-name'; BODY executed within
792 `save-excursion'. If FILE is not under version control, or locked by
793 somebody else, signal error."
794 (declare (debug t) (indent 2))
795 (let ((filevar (make-symbol "file")))
796 `(let ((,filevar (expand-file-name ,file)))
797 (or (vc-backend ,filevar)
798 (error (format "File not under version control: `%s'" file)))
799 (unless (vc-editable-p ,filevar)
800 (let ((state (vc-state ,filevar)))
801 (if (stringp state)
802 (error (format "`%s' is locking `%s'" state ,filevar))
803 (vc-checkout ,filevar t))))
804 (save-excursion
805 ,@body)
806 (vc-checkin ,filevar nil ,comment))))
808 ;;;###autoload
809 (defmacro edit-vc-file (file comment &rest body)
810 "Edit FILE under version control, executing body.
811 Checkin with COMMENT after executing BODY.
812 This macro uses `with-vc-file', passing args to it.
813 However, before executing BODY, find FILE, and after BODY, save buffer."
814 (declare (debug t) (indent 2))
815 (let ((filevar (make-symbol "file")))
816 `(let ((,filevar (expand-file-name ,file)))
817 (with-vc-file
818 ,filevar ,comment
819 (set-buffer (find-file-noselect ,filevar))
820 ,@body
821 (save-buffer)))))
823 (defun vc-ensure-vc-buffer ()
824 "Make sure that the current buffer visits a version-controlled file."
825 (if vc-dired-mode
826 (set-buffer (find-file-noselect (dired-get-filename)))
827 (while vc-parent-buffer
828 (pop-to-buffer vc-parent-buffer))
829 (if (not buffer-file-name)
830 (error "Buffer %s is not associated with a file" (buffer-name))
831 (if (not (vc-backend buffer-file-name))
832 (error "File %s is not under version control" buffer-file-name)))))
834 (defun vc-process-filter (p s)
835 "An alternative output filter for async process P.
836 The only difference with the default filter is to insert S after markers."
837 (with-current-buffer (process-buffer p)
838 (save-excursion
839 (let ((inhibit-read-only t))
840 (goto-char (process-mark p))
841 (insert s)
842 (set-marker (process-mark p) (point))))))
844 (defun vc-setup-buffer (&optional buf)
845 "Prepare BUF for executing a VC command and make it current.
846 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
847 (unless buf (setq buf "*vc*"))
848 (let ((camefrom (current-buffer))
849 (olddir default-directory))
850 (set-buffer (get-buffer-create buf))
851 (kill-all-local-variables)
852 (set (make-local-variable 'vc-parent-buffer) camefrom)
853 (set (make-local-variable 'vc-parent-buffer-name)
854 (concat " from " (buffer-name camefrom)))
855 (setq default-directory olddir)
856 (let ((inhibit-read-only t))
857 (erase-buffer))))
859 (defun vc-exec-after (code)
860 "Eval CODE when the current buffer's process is done.
861 If the current buffer has no process, just evaluate CODE.
862 Else, add CODE to the process' sentinel."
863 (let ((proc (get-buffer-process (current-buffer))))
864 (cond
865 ;; If there's no background process, just execute the code.
866 ((null proc) (eval code))
867 ;; If the background process has exited, reap it and try again
868 ((eq (process-status proc) 'exit)
869 (delete-process proc)
870 (vc-exec-after code))
871 ;; If a process is running, add CODE to the sentinel
872 ((eq (process-status proc) 'run)
873 (let ((sentinel (process-sentinel proc)))
874 (set-process-sentinel proc
875 `(lambda (p s)
876 (with-current-buffer ',(current-buffer)
877 (goto-char (process-mark p))
878 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
879 ; (goto-char...)'
880 (car (cdr (cdr ;strip off `lambda (p s)'
881 sentinel))))))
882 (list `(vc-exec-after ',code))))))))
883 (t (error "Unexpected process state"))))
884 nil)
886 (defvar vc-post-command-functions nil
887 "Hook run at the end of `vc-do-command'.
888 Each function is called inside the buffer in which the command was run
889 and is passed 3 arguments: the COMMAND, the FILE and the FLAGS.")
891 (defvar w32-quote-process-args)
892 ;;;###autoload
893 (defun vc-do-command (buffer okstatus command file &rest flags)
894 "Execute a VC command, notifying user and checking for errors.
895 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
896 current buffer if BUFFER is t. If the destination buffer is not
897 already current, set it up properly and erase it. The command is
898 considered successful if its exit status does not exceed OKSTATUS (if
899 OKSTATUS is nil, that means to ignore errors, if it is 'async, that
900 means not to wait for termination of the subprocess). FILE is the
901 name of the working file (may also be nil, to execute commands that
902 don't expect a file name). If an optional list of FLAGS is present,
903 that is inserted into the command line before the filename."
904 (and file (setq file (expand-file-name file)))
905 (if vc-command-messages
906 (message "Running %s on %s..." command file))
907 (save-current-buffer
908 (unless (or (eq buffer t)
909 (and (stringp buffer)
910 (string= (buffer-name) buffer))
911 (eq buffer (current-buffer)))
912 (vc-setup-buffer buffer))
913 (let ((squeezed (remq nil flags))
914 (inhibit-read-only t)
915 (status 0))
916 (when file
917 ;; FIXME: file-relative-name can return a bogus result because
918 ;; it doesn't look at the actual file-system to see if symlinks
919 ;; come into play.
920 (setq squeezed (append squeezed (list (file-relative-name file)))))
921 (let ((exec-path (append vc-path exec-path))
922 ;; Add vc-path to PATH for the execution of this command.
923 (process-environment
924 (cons (concat "PATH=" (getenv "PATH")
925 path-separator
926 (mapconcat 'identity vc-path path-separator))
927 process-environment))
928 (w32-quote-process-args t))
929 (if (eq okstatus 'async)
930 (let ((proc (apply 'start-process command (current-buffer) command
931 squeezed)))
932 (unless (active-minibuffer-window)
933 (message "Running %s in the background..." command))
934 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
935 (set-process-filter proc 'vc-process-filter)
936 (vc-exec-after
937 `(unless (active-minibuffer-window)
938 (message "Running %s in the background... done" ',command))))
939 (setq status (apply 'call-process command nil t nil squeezed))
940 (when (or (not (integerp status)) (and okstatus (< okstatus status)))
941 (pop-to-buffer (current-buffer))
942 (goto-char (point-min))
943 (shrink-window-if-larger-than-buffer)
944 (error "Running %s...FAILED (%s)" command
945 (if (integerp status) (format "status %d" status) status))))
946 (if vc-command-messages
947 (message "Running %s...OK" command)))
948 (vc-exec-after
949 `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
950 status)))
952 (defun vc-position-context (posn)
953 "Save a bit of the text around POSN in the current buffer.
954 Used to help us find the corresponding position again later
955 if markers are destroyed or corrupted."
956 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
957 ;; rcs.el mode.
958 (list posn
959 (buffer-size)
960 (buffer-substring posn
961 (min (point-max) (+ posn 100)))))
963 (defun vc-find-position-by-context (context)
964 "Return the position of CONTEXT in the current buffer.
965 If CONTEXT cannot be found, return nil."
966 (let ((context-string (nth 2 context)))
967 (if (equal "" context-string)
968 (point-max)
969 (save-excursion
970 (let ((diff (- (nth 1 context) (buffer-size))))
971 (if (< diff 0) (setq diff (- diff)))
972 (goto-char (nth 0 context))
973 (if (or (search-forward context-string nil t)
974 ;; Can't use search-backward since the match may continue
975 ;; after point.
976 (progn (goto-char (- (point) diff (length context-string)))
977 ;; goto-char doesn't signal an error at
978 ;; beginning of buffer like backward-char would
979 (search-forward context-string nil t)))
980 ;; to beginning of OSTRING
981 (- (point) (length context-string))))))))
983 (defun vc-context-matches-p (posn context)
984 "Return t if POSN matches CONTEXT, nil otherwise."
985 (let* ((context-string (nth 2 context))
986 (len (length context-string))
987 (end (+ posn len)))
988 (if (> end (1+ (buffer-size)))
990 (string= context-string (buffer-substring posn end)))))
992 (defun vc-buffer-context ()
993 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
994 Used by `vc-restore-buffer-context' to later restore the context."
995 (let ((point-context (vc-position-context (point)))
996 ;; Use mark-marker to avoid confusion in transient-mark-mode.
997 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
998 (vc-position-context (mark-marker))))
999 ;; Make the right thing happen in transient-mark-mode.
1000 (mark-active nil)
1001 ;; We may want to reparse the compilation buffer after revert
1002 (reparse (and (boundp 'compilation-error-list) ;compile loaded
1003 ;; Construct a list; each elt is nil or a buffer
1004 ;; iff that buffer is a compilation output buffer
1005 ;; that contains markers into the current buffer.
1006 (save-current-buffer
1007 (mapcar (lambda (buffer)
1008 (set-buffer buffer)
1009 (let ((errors (or
1010 compilation-old-error-list
1011 compilation-error-list))
1012 (buffer-error-marked-p nil))
1013 (while (and (consp errors)
1014 (not buffer-error-marked-p))
1015 (and (markerp (cdr (car errors)))
1016 (eq buffer
1017 (marker-buffer
1018 (cdr (car errors))))
1019 (setq buffer-error-marked-p t))
1020 (setq errors (cdr errors)))
1021 (if buffer-error-marked-p buffer)))
1022 (buffer-list))))))
1023 (list point-context mark-context reparse)))
1025 (defun vc-restore-buffer-context (context)
1026 "Restore point/mark, and reparse any affected compilation buffers.
1027 CONTEXT is that which `vc-buffer-context' returns."
1028 (let ((point-context (nth 0 context))
1029 (mark-context (nth 1 context))
1030 (reparse (nth 2 context)))
1031 ;; Reparse affected compilation buffers.
1032 (while reparse
1033 (if (car reparse)
1034 (with-current-buffer (car reparse)
1035 (let ((compilation-last-buffer (current-buffer)) ;select buffer
1036 ;; Record the position in the compilation buffer of
1037 ;; the last error next-error went to.
1038 (error-pos (marker-position
1039 (car (car-safe compilation-error-list)))))
1040 ;; Reparse the error messages as far as they were parsed before.
1041 (compile-reinitialize-errors '(4) compilation-parsing-end)
1042 ;; Move the pointer up to find the error we were at before
1043 ;; reparsing. Now next-error should properly go to the next one.
1044 (while (and compilation-error-list
1045 (/= error-pos (car (car compilation-error-list))))
1046 (setq compilation-error-list (cdr compilation-error-list))))))
1047 (setq reparse (cdr reparse)))
1049 ;; if necessary, restore point and mark
1050 (if (not (vc-context-matches-p (point) point-context))
1051 (let ((new-point (vc-find-position-by-context point-context)))
1052 (if new-point (goto-char new-point))))
1053 (and mark-active
1054 mark-context
1055 (not (vc-context-matches-p (mark) mark-context))
1056 (let ((new-mark (vc-find-position-by-context mark-context)))
1057 (if new-mark (set-mark new-mark))))))
1059 (defun vc-revert-buffer1 (&optional arg no-confirm)
1060 "Revert buffer, keeping point and mark where user expects them.
1061 Try to be clever in the face of changes due to expanded version control
1062 key words. This is important for typeahead to work as expected.
1063 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1064 (interactive "P")
1065 (widen)
1066 (let ((context (vc-buffer-context)))
1067 ;; Use save-excursion here, because it may be able to restore point
1068 ;; and mark properly even in cases where vc-restore-buffer-context
1069 ;; would fail. However, save-excursion might also get it wrong --
1070 ;; in this case, vc-restore-buffer-context gives it a second try.
1071 (save-excursion
1072 ;; t means don't call normal-mode;
1073 ;; that's to preserve various minor modes.
1074 (revert-buffer arg no-confirm t))
1075 (vc-restore-buffer-context context)))
1078 (defun vc-buffer-sync (&optional not-urgent)
1079 "Make sure the current buffer and its working file are in sync.
1080 NOT-URGENT means it is ok to continue if the user says not to save."
1081 (if (buffer-modified-p)
1082 (if (or vc-suppress-confirm
1083 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1084 (save-buffer)
1085 (unless not-urgent
1086 (error "Aborted")))))
1088 (defun vc-default-latest-on-branch-p (backend file)
1089 "Return non-nil if FILE is the latest on its branch.
1090 This default implementation always returns non-nil, which means that
1091 editing non-current versions is not supported by default."
1094 (defun vc-recompute-state (file)
1095 "Force a recomputation of the version control state of FILE.
1096 The state is computed using the exact, and possibly expensive
1097 function `vc-BACKEND-state', not the heuristic."
1098 (vc-file-setprop file 'vc-state (vc-call state file)))
1100 (defun vc-next-action-on-file (file verbose &optional comment)
1101 "Do The Right Thing for a given FILE under version control.
1102 If COMMENT is specified, it will be used as an admin or checkin comment.
1103 If VERBOSE is non-nil, query the user rather than using default parameters."
1104 (let ((visited (get-file-buffer file))
1105 state version)
1106 (when visited
1107 (if vc-dired-mode
1108 (switch-to-buffer-other-window visited)
1109 (set-buffer visited))
1110 ;; Check relation of buffer and file, and make sure
1111 ;; user knows what he's doing. First, finding the file
1112 ;; will check whether the file on disk is newer.
1113 ;; Ignore buffer-read-only during this test, and
1114 ;; preserve find-file-literally.
1115 (let ((buffer-read-only (not (file-writable-p file))))
1116 (find-file-noselect file nil find-file-literally))
1117 (if (not (verify-visited-file-modtime (current-buffer)))
1118 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
1119 (write-file buffer-file-name)
1120 (error "Aborted"))
1121 ;; Now, check if we have unsaved changes.
1122 (vc-buffer-sync t)
1123 (if (buffer-modified-p)
1124 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
1125 (error "Aborted")))))
1127 ;; Do the right thing
1128 (if (not (vc-registered file))
1129 (vc-register verbose comment)
1130 (vc-recompute-state file)
1131 (if visited (vc-mode-line file))
1132 (setq state (vc-state file))
1133 (cond
1134 ;; up-to-date
1135 ((or (eq state 'up-to-date)
1136 (and verbose (eq state 'needs-patch)))
1137 (cond
1138 (verbose
1139 ;; go to a different version
1140 (setq version
1141 (read-string "Branch, version, or backend to move to: "))
1142 (let ((vsym (intern-soft (upcase version))))
1143 (if (member vsym vc-handled-backends)
1144 (vc-transfer-file file vsym)
1145 (vc-checkout file (eq (vc-checkout-model file) 'implicit)
1146 version))))
1147 ((not (eq (vc-checkout-model file) 'implicit))
1148 ;; check the file out
1149 (vc-checkout file t))
1151 ;; do nothing
1152 (message "%s is up-to-date" file))))
1154 ;; Abnormal: edited but read-only
1155 ((and visited (eq state 'edited)
1156 buffer-read-only (not (file-writable-p file)))
1157 ;; Make the file+buffer read-write. If the user really wanted to
1158 ;; commit, he'll get a chance to do that next time around, anyway.
1159 (message "File is edited but read-only; making it writable")
1160 (set-file-modes buffer-file-name
1161 (logior (file-modes buffer-file-name) 128))
1162 (toggle-read-only -1))
1164 ;; edited
1165 ((eq state 'edited)
1166 (cond
1167 ;; For files with locking, if the file does not contain
1168 ;; any changes, just let go of the lock, i.e. revert.
1169 ((and (not (eq (vc-checkout-model file) 'implicit))
1170 (vc-workfile-unchanged-p file)
1171 ;; If buffer is modified, that means the user just
1172 ;; said no to saving it; in that case, don't revert,
1173 ;; because the user might intend to save after
1174 ;; finishing the log entry.
1175 (not (and visited (buffer-modified-p))))
1176 ;; DO NOT revert the file without asking the user!
1177 (if (not visited) (find-file-other-window file))
1178 (if (yes-or-no-p "Revert to master version? ")
1179 (vc-revert-buffer)))
1180 (t ;; normal action
1181 (if (not verbose)
1182 (vc-checkin file nil comment)
1183 (setq version (read-string "New version or backend: "))
1184 (let ((vsym (intern (upcase version))))
1185 (if (member vsym vc-handled-backends)
1186 (vc-transfer-file file vsym)
1187 (vc-checkin file version comment)))))))
1189 ;; locked by somebody else
1190 ((stringp state)
1191 (if comment
1192 (error "Sorry, you can't steal the lock on %s this way"
1193 (file-name-nondirectory file)))
1194 (vc-steal-lock file
1195 (if verbose (read-string "Version to steal: ")
1196 (vc-workfile-version file))
1197 state))
1199 ;; needs-patch
1200 ((eq state 'needs-patch)
1201 (if (yes-or-no-p (format
1202 "%s is not up-to-date. Get latest version? "
1203 (file-name-nondirectory file)))
1204 (vc-checkout file (eq (vc-checkout-model file) 'implicit) t)
1205 (if (and (not (eq (vc-checkout-model file) 'implicit))
1206 (yes-or-no-p "Lock this version? "))
1207 (vc-checkout file t)
1208 (error "Aborted"))))
1210 ;; needs-merge
1211 ((eq state 'needs-merge)
1212 (if (yes-or-no-p (format
1213 "%s is not up-to-date. Merge in changes now? "
1214 (file-name-nondirectory file)))
1215 (vc-maybe-resolve-conflicts file (vc-call merge-news file))
1216 (error "Aborted")))
1218 ;; unlocked-changes
1219 ((eq state 'unlocked-changes)
1220 (if (not visited) (find-file-other-window file))
1221 (if (save-window-excursion
1222 (vc-version-diff file (vc-workfile-version file) nil)
1223 (goto-char (point-min))
1224 (let ((inhibit-read-only t))
1225 (insert
1226 (format "Changes to %s since last lock:\n\n" file)))
1227 (not (beep))
1228 (yes-or-no-p (concat "File has unlocked changes. "
1229 "Claim lock retaining changes? ")))
1230 (progn (vc-call steal-lock file)
1231 (clear-visited-file-modtime)
1232 ;; Must clear any headers here because they wouldn't
1233 ;; show that the file is locked now.
1234 (vc-clear-headers file)
1235 (write-file buffer-file-name)
1236 (vc-mode-line file))
1237 (if (not (yes-or-no-p
1238 "Revert to checked-in version, instead? "))
1239 (error "Checkout aborted")
1240 (vc-revert-buffer1 t t)
1241 (vc-checkout file t))))))))
1243 (defvar vc-dired-window-configuration)
1245 (defun vc-next-action-dired (file rev comment)
1246 "Call `vc-next-action-on-file' on all the marked files.
1247 Ignores FILE and REV, but passes on COMMENT."
1248 (let ((dired-buffer (current-buffer)))
1249 (dired-map-over-marks
1250 (let ((file (dired-get-filename)))
1251 (message "Processing %s..." file)
1252 (vc-next-action-on-file file nil comment)
1253 (set-buffer dired-buffer)
1254 (set-window-configuration vc-dired-window-configuration)
1255 (message "Processing %s...done" file))
1256 nil t))
1257 (dired-move-to-filename))
1259 ;; Here's the major entry point.
1261 ;;;###autoload
1262 (defun vc-next-action (verbose)
1263 "Do the next logical version control operation on the current file.
1265 If you call this from within a VC dired buffer with no files marked,
1266 it will operate on the file in the current line.
1268 If you call this from within a VC dired buffer, and one or more
1269 files are marked, it will accept a log message and then operate on
1270 each one. The log message will be used as a comment for any register
1271 or checkin operations, but ignored when doing checkouts. Attempted
1272 lock steals will raise an error.
1274 A prefix argument lets you specify the version number to use.
1276 For RCS and SCCS files:
1277 If the file is not already registered, this registers it for version
1278 control.
1279 If the file is registered and not locked by anyone, this checks out
1280 a writable and locked file ready for editing.
1281 If the file is checked out and locked by the calling user, this
1282 first checks to see if the file has changed since checkout. If not,
1283 it performs a revert.
1284 If the file has been changed, this pops up a buffer for entry
1285 of a log message; when the message has been entered, it checks in the
1286 resulting changes along with the log message as change commentary. If
1287 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1288 read-only copy of the changed file is left in place afterwards.
1289 If the file is registered and locked by someone else, you are given
1290 the option to steal the lock.
1292 For CVS files:
1293 If the file is not already registered, this registers it for version
1294 control. This does a \"cvs add\", but no \"cvs commit\".
1295 If the file is added but not committed, it is committed.
1296 If your working file is changed, but the repository file is
1297 unchanged, this pops up a buffer for entry of a log message; when the
1298 message has been entered, it checks in the resulting changes along
1299 with the logmessage as change commentary. A writable file is retained.
1300 If the repository file is changed, you are asked if you want to
1301 merge in the changes into your working copy."
1303 (interactive "P")
1304 (catch 'nogo
1305 (if vc-dired-mode
1306 (let ((files (dired-get-marked-files)))
1307 (set (make-local-variable 'vc-dired-window-configuration)
1308 (current-window-configuration))
1309 (if (string= ""
1310 (mapconcat
1311 (lambda (f)
1312 (if (not (vc-up-to-date-p f)) "@" ""))
1313 files ""))
1314 (vc-next-action-dired nil nil "dummy")
1315 (vc-start-entry nil nil nil nil
1316 "Enter a change comment for the marked files."
1317 'vc-next-action-dired))
1318 (throw 'nogo nil)))
1319 (while vc-parent-buffer
1320 (pop-to-buffer vc-parent-buffer))
1321 (if buffer-file-name
1322 (vc-next-action-on-file buffer-file-name verbose)
1323 (error "Buffer %s is not associated with a file" (buffer-name)))))
1325 ;; These functions help the vc-next-action entry point
1327 ;;;###autoload
1328 (defun vc-register (&optional set-version comment)
1329 "Register the current file into a version control system.
1330 With prefix argument SET-VERSION, allow user to specify initial version
1331 level. If COMMENT is present, use that as an initial comment.
1333 The version control system to use is found by cycling through the list
1334 `vc-handled-backends'. The first backend in that list which declares
1335 itself responsible for the file (usually because other files in that
1336 directory are already registered under that backend) will be used to
1337 register the file. If no backend declares itself responsible, the
1338 first backend that could register the file is used."
1339 (interactive "P")
1340 (unless buffer-file-name (error "No visited file"))
1341 (when (vc-backend buffer-file-name)
1342 (if (vc-registered buffer-file-name)
1343 (error "This file is already registered")
1344 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1345 (error "Aborted"))))
1346 ;; Watch out for new buffers of size 0: the corresponding file
1347 ;; does not exist yet, even though buffer-modified-p is nil.
1348 (if (and (not (buffer-modified-p))
1349 (zerop (buffer-size))
1350 (not (file-exists-p buffer-file-name)))
1351 (set-buffer-modified-p t))
1352 (vc-buffer-sync)
1354 (vc-start-entry buffer-file-name
1355 (if set-version
1356 (read-string (format "Initial version level for %s: "
1357 (buffer-name)))
1358 (let ((backend (vc-responsible-backend buffer-file-name)))
1359 (if (vc-find-backend-function backend 'init-version)
1360 (vc-call-backend backend 'init-version)
1361 vc-default-init-version)))
1362 (or comment (not vc-initial-comment))
1364 "Enter initial comment."
1365 (lambda (file rev comment)
1366 (message "Registering %s... " file)
1367 (let ((backend (vc-responsible-backend file t)))
1368 (vc-file-clearprops file)
1369 (vc-call-backend backend 'register file rev comment)
1370 (vc-file-setprop file 'vc-backend backend)
1371 (unless vc-make-backup-files
1372 (make-local-variable 'backup-inhibited)
1373 (setq backup-inhibited t)))
1374 (message "Registering %s... done" file))))
1377 (defun vc-responsible-backend (file &optional register)
1378 "Return the name of a backend system that is responsible for FILE.
1379 The optional argument REGISTER means that a backend suitable for
1380 registration should be found.
1382 If REGISTER is nil, then if FILE is already registered, return the
1383 backend of FILE. If FILE is not registered, or a directory, then the
1384 first backend in `vc-handled-backends' that declares itself
1385 responsible for FILE is returned. If no backend declares itself
1386 responsible, return the first backend.
1388 If REGISTER is non-nil, return the first responsible backend under
1389 which FILE is not yet registered. If there is no such backend, return
1390 the first backend under which FILE is not yet registered, but could
1391 be registered."
1392 (if (not vc-handled-backends)
1393 (error "No handled backends"))
1394 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1395 (catch 'found
1396 ;; First try: find a responsible backend. If this is for registration,
1397 ;; it must be a backend under which FILE is not yet registered.
1398 (dolist (backend vc-handled-backends)
1399 (and (or (not register)
1400 (not (vc-call-backend backend 'registered file)))
1401 (vc-call-backend backend 'responsible-p file)
1402 (throw 'found backend)))
1403 ;; no responsible backend
1404 (if (not register)
1405 ;; if this is not for registration, the first backend must do
1406 (car vc-handled-backends)
1407 ;; for registration, we need to find a new backend that
1408 ;; could register FILE
1409 (dolist (backend vc-handled-backends)
1410 (and (not (vc-call-backend backend 'registered file))
1411 (vc-call-backend backend 'could-register file)
1412 (throw 'found backend)))
1413 (error "No backend that could register")))))
1415 (defun vc-default-responsible-p (backend file)
1416 "Indicate whether BACKEND is reponsible for FILE.
1417 The default is to return nil always."
1418 nil)
1420 (defun vc-default-could-register (backend file)
1421 "Return non-nil if BACKEND could be used to register FILE.
1422 The default implementation returns t for all files."
1425 (defun vc-resynch-window (file &optional keep noquery)
1426 "If FILE is in the current buffer, either revert or unvisit it.
1427 The choice between revert (to see expanded keywords) and unvisit depends on
1428 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1429 reverting. NOQUERY should be t *only* if it is known the only
1430 difference between the buffer and the file is due to version control
1431 rather than user editing!"
1432 (and (string= buffer-file-name file)
1433 (if keep
1434 (progn
1435 (vc-revert-buffer1 t noquery)
1436 ;; TODO: Adjusting view mode might no longer be necessary
1437 ;; after RMS change to files.el of 1999-08-08. Investigate
1438 ;; this when we install the new VC.
1439 (and view-read-only
1440 (if (file-writable-p file)
1441 (and view-mode
1442 (let ((view-old-buffer-read-only nil))
1443 (view-mode-exit)))
1444 (and (not view-mode)
1445 (not (eq (get major-mode 'mode-class) 'special))
1446 (view-mode-enter))))
1447 (vc-mode-line buffer-file-name))
1448 (kill-buffer (current-buffer)))))
1450 (defun vc-resynch-buffer (file &optional keep noquery)
1451 "If FILE is currently visited, resynch its buffer."
1452 (if (string= buffer-file-name file)
1453 (vc-resynch-window file keep noquery)
1454 (let ((buffer (get-file-buffer file)))
1455 (if buffer
1456 (with-current-buffer buffer
1457 (vc-resynch-window file keep noquery)))))
1458 (vc-dired-resynch-file file))
1460 (defun vc-start-entry (file rev comment initial-contents msg action &optional after-hook)
1461 "Accept a comment for an operation on FILE revision REV.
1462 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1463 action on close to ACTION. If COMMENT is a string and
1464 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1465 contents of the log entry buffer. If COMMENT is a string and
1466 INITIAL-CONTENTS is nil, do action immediately as if the user had
1467 entered COMMENT. If COMMENT is t, also do action immediately with an
1468 empty comment. Remember the file's buffer in `vc-parent-buffer'
1469 \(current one if no file). AFTER-HOOK specifies the local value
1470 for vc-log-operation-hook."
1471 (let ((parent (or (and file (get-file-buffer file)) (current-buffer))))
1472 (if vc-before-checkin-hook
1473 (if file
1474 (with-current-buffer parent
1475 (run-hooks 'vc-before-checkin-hook))
1476 (run-hooks 'vc-before-checkin-hook)))
1477 (if (and comment (not initial-contents))
1478 (set-buffer (get-buffer-create "*VC-log*"))
1479 (pop-to-buffer (get-buffer-create "*VC-log*")))
1480 (set (make-local-variable 'vc-parent-buffer) parent)
1481 (set (make-local-variable 'vc-parent-buffer-name)
1482 (concat " from " (buffer-name vc-parent-buffer)))
1483 (if file (vc-mode-line file))
1484 (vc-log-edit file)
1485 (make-local-variable 'vc-log-after-operation-hook)
1486 (if after-hook
1487 (setq vc-log-after-operation-hook after-hook))
1488 (setq vc-log-operation action)
1489 (setq vc-log-version rev)
1490 (when comment
1491 (erase-buffer)
1492 (when (stringp comment) (insert comment)))
1493 (if (or (not comment) initial-contents)
1494 (message "%s Type C-c C-c when done" msg)
1495 (vc-finish-logentry (eq comment t)))))
1497 (defun vc-checkout (file &optional writable rev)
1498 "Retrieve a copy of the revision REV of FILE.
1499 If WRITABLE is non-nil, make sure the retrieved file is writable.
1500 REV defaults to the latest revision.
1502 After check-out, runs the normal hook `vc-checkout-hook'."
1503 (and writable
1504 (not rev)
1505 (vc-call make-version-backups-p file)
1506 (vc-up-to-date-p file)
1507 (vc-make-version-backup file))
1508 (with-vc-properties
1509 file
1510 (condition-case err
1511 (vc-call checkout file writable rev)
1512 (file-error
1513 ;; Maybe the backend is not installed ;-(
1514 (when writable
1515 (let ((buf (get-file-buffer file)))
1516 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1517 (signal (car err) (cdr err))))
1518 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1519 (not writable))
1520 (if (vc-call latest-on-branch-p file)
1521 'up-to-date
1522 'needs-patch)
1523 'edited))
1524 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1525 (vc-resynch-buffer file t t)
1526 (run-hooks 'vc-checkout-hook))
1528 (defun vc-steal-lock (file rev owner)
1529 "Steal the lock on FILE."
1530 (let (file-description)
1531 (if rev
1532 (setq file-description (format "%s:%s" file rev))
1533 (setq file-description file))
1534 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1535 file-description owner)))
1536 (error "Steal canceled"))
1537 (message "Stealing lock on %s..." file)
1538 (with-vc-properties
1539 file
1540 (vc-call steal-lock file rev)
1541 `((vc-state . edited)))
1542 (vc-resynch-buffer file t t)
1543 (message "Stealing lock on %s...done" file)
1544 ;; Write mail after actually stealing, because if the stealing
1545 ;; goes wrong, we don't want to send any mail.
1546 (compose-mail owner (format "Stolen lock on %s" file-description))
1547 (setq default-directory (expand-file-name "~/"))
1548 (goto-char (point-max))
1549 (insert
1550 (format "I stole the lock on %s, " file-description)
1551 (current-time-string)
1552 ".\n")
1553 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1555 (defun vc-checkin (file &optional rev comment initial-contents)
1556 "Check in FILE.
1557 The optional argument REV may be a string specifying the new version
1558 level (if nil increment the current level). COMMENT is a comment
1559 string; if omitted, a buffer is popped up to accept a comment. If
1560 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1561 of the log entry buffer.
1563 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1564 that the version control system supports this mode of operation.
1566 Runs the normal hook `vc-checkin-hook'."
1567 (vc-start-entry
1568 file rev comment initial-contents
1569 "Enter a change comment."
1570 (lambda (file rev comment)
1571 (message "Checking in %s..." file)
1572 ;; "This log message intentionally left almost blank".
1573 ;; RCS 5.7 gripes about white-space-only comments too.
1574 (or (and comment (string-match "[^\t\n ]" comment))
1575 (setq comment "*** empty log message ***"))
1576 (with-vc-properties
1577 file
1578 ;; Change buffers to get local value of vc-checkin-switches.
1579 (with-current-buffer (or (get-file-buffer file) (current-buffer))
1580 (progn
1581 (vc-call checkin file rev comment)
1582 (vc-delete-automatic-version-backups file)))
1583 `((vc-state . up-to-date)
1584 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1585 (vc-workfile-version . nil)))
1586 (message "Checking in %s...done" file))
1587 'vc-checkin-hook))
1589 (defun vc-comment-to-change-log (&optional whoami file-name)
1590 "Enter last VC comment into the change log for the current file.
1591 WHOAMI (interactive prefix) non-nil means prompt for user name
1592 and site. FILE-NAME is the name of the change log; if nil, use
1593 `change-log-default-name'.
1595 This may be useful as a `vc-checkin-hook' to update change logs
1596 automatically."
1597 (interactive (if current-prefix-arg
1598 (list current-prefix-arg
1599 (prompt-for-change-log-name))))
1600 ;; Make sure the defvar for add-log-current-defun-function has been executed
1601 ;; before binding it.
1602 (require 'add-log)
1603 (let (;; Extract the comment first so we get any error before doing anything.
1604 (comment (ring-ref vc-comment-ring 0))
1605 ;; Don't let add-change-log-entry insert a defun name.
1606 (add-log-current-defun-function 'ignore)
1607 end)
1608 ;; Call add-log to do half the work.
1609 (add-change-log-entry whoami file-name t t)
1610 ;; Insert the VC comment, leaving point before it.
1611 (setq end (save-excursion (insert comment) (point-marker)))
1612 (if (looking-at "\\s *\\s(")
1613 ;; It starts with an open-paren, as in "(foo): Frobbed."
1614 ;; So remove the ": " add-log inserted.
1615 (delete-char -2))
1616 ;; Canonicalize the white space between the file name and comment.
1617 (just-one-space)
1618 ;; Indent rest of the text the same way add-log indented the first line.
1619 (let ((indentation (current-indentation)))
1620 (save-excursion
1621 (while (< (point) end)
1622 (forward-line 1)
1623 (indent-to indentation))
1624 (setq end (point))))
1625 ;; Fill the inserted text, preserving open-parens at bol.
1626 (let ((paragraph-separate (concat paragraph-separate "\\|\\s *\\s("))
1627 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
1628 (beginning-of-line)
1629 (fill-region (point) end))
1630 ;; Canonicalize the white space at the end of the entry so it is
1631 ;; separated from the next entry by a single blank line.
1632 (skip-syntax-forward " " end)
1633 (delete-char (- (skip-syntax-backward " ")))
1634 (or (eobp) (looking-at "\n\n")
1635 (insert "\n"))))
1637 (defun vc-finish-logentry (&optional nocomment)
1638 "Complete the operation implied by the current log entry.
1639 Use the contents of the current buffer as a check-in or registration
1640 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1641 the buffer contents as a comment, and don't add it to
1642 `vc-comment-ring'."
1643 (interactive)
1644 ;; Check and record the comment, if any.
1645 (unless nocomment
1646 ;; Comment too long?
1647 (vc-call-backend (or (and vc-log-file (vc-backend vc-log-file))
1648 (vc-responsible-backend default-directory))
1649 'logentry-check)
1650 (run-hooks 'vc-logentry-check-hook)
1651 ;; Record the comment in the comment ring
1652 (let ((comment (buffer-string)))
1653 (unless (and (ring-p vc-comment-ring)
1654 (not (ring-empty-p vc-comment-ring))
1655 (equal comment (ring-ref vc-comment-ring 0)))
1656 (ring-insert vc-comment-ring comment))))
1657 ;; Sync parent buffer in case the user modified it while editing the comment.
1658 ;; But not if it is a vc-dired buffer.
1659 (with-current-buffer vc-parent-buffer
1660 (or vc-dired-mode (vc-buffer-sync)))
1661 (if (not vc-log-operation) (error "No log operation is pending"))
1662 ;; save the parameters held in buffer-local variables
1663 (let ((log-operation vc-log-operation)
1664 (log-file vc-log-file)
1665 (log-version vc-log-version)
1666 (log-entry (buffer-string))
1667 (after-hook vc-log-after-operation-hook)
1668 (tmp-vc-parent-buffer vc-parent-buffer))
1669 (pop-to-buffer vc-parent-buffer)
1670 ;; OK, do it to it
1671 (save-excursion
1672 (funcall log-operation
1673 log-file
1674 log-version
1675 log-entry))
1676 ;; Remove checkin window (after the checkin so that if that fails
1677 ;; we don't zap the *VC-log* buffer and the typing therein).
1678 (let ((logbuf (get-buffer "*VC-log*")))
1679 (cond ((and logbuf vc-delete-logbuf-window)
1680 (delete-windows-on logbuf (selected-frame))
1681 ;; Kill buffer and delete any other dedicated windows/frames.
1682 (kill-buffer logbuf))
1683 (logbuf (pop-to-buffer "*VC-log*")
1684 (bury-buffer)
1685 (pop-to-buffer tmp-vc-parent-buffer))))
1686 ;; Now make sure we see the expanded headers
1687 (if log-file
1688 (vc-resynch-buffer log-file vc-keep-workfiles t))
1689 (if vc-dired-mode
1690 (dired-move-to-filename))
1691 (run-hooks after-hook 'vc-finish-logentry-hook)))
1693 ;; Code for access to the comment ring
1695 (defun vc-new-comment-index (stride len)
1696 "Return the comment index STRIDE elements from the current one.
1697 LEN is the length of `vc-comment-ring'."
1698 (mod (cond
1699 (vc-comment-ring-index (+ vc-comment-ring-index stride))
1700 ;; Initialize the index on the first use of this command
1701 ;; so that the first M-p gets index 0, and the first M-n gets
1702 ;; index -1.
1703 ((> stride 0) (1- stride))
1704 (t stride))
1705 len))
1707 (defun vc-previous-comment (arg)
1708 "Cycle backwards through comment history.
1709 With a numeric prefix ARG, go back ARG comments."
1710 (interactive "*p")
1711 (let ((len (ring-length vc-comment-ring)))
1712 (if (<= len 0)
1713 (progn (message "Empty comment ring") (ding))
1714 (erase-buffer)
1715 (setq vc-comment-ring-index (vc-new-comment-index arg len))
1716 (message "Comment %d" (1+ vc-comment-ring-index))
1717 (insert (ring-ref vc-comment-ring vc-comment-ring-index)))))
1719 (defun vc-next-comment (arg)
1720 "Cycle forwards through comment history.
1721 With a numeric prefix ARG, go forward ARG comments."
1722 (interactive "*p")
1723 (vc-previous-comment (- arg)))
1725 (defun vc-comment-search-reverse (str &optional stride)
1726 "Search backwards through comment history for substring match of STR.
1727 If the optional argument STRIDE is present, that is a step-width to use
1728 when going through the comment ring."
1729 ;; Why substring rather than regexp ? -sm
1730 (interactive
1731 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1732 (unless stride (setq stride 1))
1733 (if (string= str "")
1734 (setq str vc-last-comment-match)
1735 (setq vc-last-comment-match str))
1736 (let* ((str (regexp-quote str))
1737 (len (ring-length vc-comment-ring))
1738 (n (vc-new-comment-index stride len)))
1739 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
1740 (not (string-match str (ring-ref vc-comment-ring n))))
1741 (setq n (+ n stride)))
1742 (setq vc-comment-ring-index n)
1743 (vc-previous-comment 0)))
1745 (defun vc-comment-search-forward (str)
1746 "Search forwards through comment history for a substring match of STR."
1747 (interactive
1748 (list (read-string "Comment substring: " nil nil vc-last-comment-match)))
1749 (vc-comment-search-reverse str -1))
1751 ;; Additional entry points for examining version histories
1753 ;;;###autoload
1754 (defun vc-diff (historic &optional not-urgent)
1755 "Display diffs between file versions.
1756 Normally this compares the current file and buffer with the most
1757 recent checked in version of that file. This uses no arguments. With
1758 a prefix argument HISTORIC, it reads the file name to use and two
1759 version designators specifying which versions to compare. The
1760 optional argument NOT-URGENT non-nil means it is ok to say no to
1761 saving the buffer."
1762 (interactive (list current-prefix-arg t))
1763 (if historic
1764 (call-interactively 'vc-version-diff)
1765 (vc-ensure-vc-buffer)
1766 (let ((file buffer-file-name))
1767 (vc-buffer-sync not-urgent)
1768 (if (vc-workfile-unchanged-p buffer-file-name)
1769 (message "No changes to %s since latest version" file)
1770 (vc-version-diff file nil nil)))))
1772 (defun vc-version-diff (file rel1 rel2)
1773 "List the differences between FILE's versions REL1 and REL2.
1774 If REL1 is empty or nil it means to use the current workfile version;
1775 REL2 empty or nil means the current file contents. FILE may also be
1776 a directory, in that case, generate diffs between the correponding
1777 versions of all registered files in or below it."
1778 (interactive
1779 (let ((file (expand-file-name
1780 (read-file-name (if buffer-file-name
1781 "File or dir to diff: (default visited file) "
1782 "File or dir to diff: ")
1783 default-directory buffer-file-name t)))
1784 (rel1-default nil) (rel2-default nil))
1785 ;; compute default versions based on the file state
1786 (cond
1787 ;; if it's a directory, don't supply any version default
1788 ((file-directory-p file)
1789 nil)
1790 ;; if the file is not up-to-date, use current version as older version
1791 ((not (vc-up-to-date-p file))
1792 (setq rel1-default (vc-workfile-version file)))
1793 ;; if the file is not locked, use last and previous version as default
1795 (setq rel1-default (vc-call previous-version file
1796 (vc-workfile-version file)))
1797 (if (string= rel1-default "") (setq rel1-default nil))
1798 (setq rel2-default (vc-workfile-version file))))
1799 ;; construct argument list
1800 (list file
1801 (read-string (if rel1-default
1802 (concat "Older version: (default "
1803 rel1-default ") ")
1804 "Older version: ")
1805 nil nil rel1-default)
1806 (read-string (if rel2-default
1807 (concat "Newer version: (default "
1808 rel2-default ") ")
1809 "Newer version (default: current source): ")
1810 nil nil rel2-default))))
1811 (if (file-directory-p file)
1812 ;; recursive directory diff
1813 (progn
1814 (vc-setup-buffer "*vc-diff*")
1815 (if (string-equal rel1 "") (setq rel1 nil))
1816 (if (string-equal rel2 "") (setq rel2 nil))
1817 (let ((inhibit-read-only t))
1818 (insert "Diffs between "
1819 (or rel1 "last version checked in")
1820 " and "
1821 (or rel2 "current workfile(s)")
1822 ":\n\n"))
1823 (let ((dir (file-name-as-directory file)))
1824 (vc-call-backend (vc-responsible-backend dir)
1825 'diff-tree dir rel1 rel2))
1826 (vc-exec-after `(let ((inhibit-read-only t))
1827 (insert "\nEnd of diffs.\n"))))
1828 ;; single file diff
1829 (vc-diff-internal file rel1 rel2))
1830 (set-buffer "*vc-diff*")
1831 (if (and (zerop (buffer-size))
1832 (not (get-buffer-process (current-buffer))))
1833 (progn
1834 (if rel1
1835 (if rel2
1836 (message "No changes to %s between %s and %s" file rel1 rel2)
1837 (message "No changes to %s since %s" file rel1))
1838 (message "No changes to %s since latest version" file))
1839 nil)
1840 (pop-to-buffer (current-buffer))
1841 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1842 ;; not available. Work around that.
1843 (if (require 'diff-mode nil t) (diff-mode))
1844 (vc-exec-after '(let ((inhibit-read-only t))
1845 (if (eq (buffer-size) 0)
1846 (insert "No differences found.\n"))
1847 (goto-char (point-min))
1848 (shrink-window-if-larger-than-buffer)))
1851 (defun vc-diff-internal (file rel1 rel2)
1852 "Run diff to compare FILE's revisions REL1 and REL2.
1853 Output goes to the current buffer, which is assumed properly set up.
1854 The exit status of the diff command is returned.
1856 This function takes care to set up a proper coding system for diff output.
1857 If both revisions are available as local files, then it also does not
1858 actually call the backend, but performs a local diff."
1859 (if (or (not rel1) (string-equal rel1 ""))
1860 (setq rel1 (vc-workfile-version file)))
1861 (if (string-equal rel2 "")
1862 (setq rel2 nil))
1863 (let ((file-rel1 (vc-version-backup-file file rel1))
1864 (file-rel2 (if (not rel2)
1865 file
1866 (vc-version-backup-file file rel2)))
1867 (coding-system-for-read (vc-coding-system-for-diff file)))
1868 (if (and file-rel1 file-rel2)
1869 (apply 'vc-do-command "*vc-diff*" 1 "diff" nil
1870 (append (vc-switches nil 'diff)
1871 (list (file-relative-name file-rel1)
1872 (file-relative-name file-rel2))))
1873 (vc-call diff file rel1 rel2))))
1876 (defcustom vc-stay-local t
1877 "*Non-nil means use local operations when possible for remote repositories.
1878 This avoids slow queries over the network and instead uses heuristics
1879 and past information to determine the current status of a file.
1881 The value can also be a regular expression or list of regular
1882 expressions to match against the host name of a repository; then VC
1883 only stays local for hosts that match it. Alternatively, the value
1884 can be a list of regular expressions where the first element is the
1885 symbol `except'; then VC always stays local except for hosts matched
1886 by these regular expressions."
1887 :type '(choice (const :tag "Always stay local" t)
1888 (const :tag "Don't stay local" nil)
1889 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
1890 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
1891 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
1892 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
1893 :version "21.4"
1894 :group 'vc)
1896 (defun vc-stay-local-p (file)
1897 "Return non-nil if VC should stay local when handling FILE.
1898 This uses the `repository-hostname' backend operation."
1899 (let* ((backend (vc-backend file))
1900 (sym (vc-make-backend-sym backend 'stay-local))
1901 (stay-local (if (boundp sym) (symbol-value sym) t)))
1902 (if (eq stay-local t) (setq stay-local vc-stay-local))
1903 (if (symbolp stay-local) stay-local
1904 (let ((dirname (if (file-directory-p file)
1905 (directory-file-name file)
1906 (file-name-directory file))))
1907 (eq 'yes
1908 (or (vc-file-getprop dirname 'vc-stay-local-p)
1909 (vc-file-setprop
1910 dirname 'vc-stay-local-p
1911 (let ((hostname (vc-call-backend
1912 backend 'repository-hostname dirname)))
1913 (if (not hostname)
1915 (let ((default t))
1916 (if (eq (car-safe stay-local) 'except)
1917 (setq default nil stay-local (cdr stay-local)))
1918 (when (consp stay-local)
1919 (setq stay-local
1920 (mapconcat 'identity stay-local "\\|")))
1921 (if (if (string-match stay-local hostname)
1922 default (not default))
1923 'yes 'no)))))))))))
1925 (defun vc-switches (backend op)
1926 (let ((switches
1927 (or (if backend
1928 (let ((sym (vc-make-backend-sym
1929 backend (intern (concat (symbol-name op)
1930 "-switches")))))
1931 (if (boundp sym) (symbol-value sym))))
1932 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1933 (if (boundp sym) (symbol-value sym)))
1934 (cond
1935 ((eq op 'diff) diff-switches)))))
1936 (if (stringp switches) (list switches)
1937 ;; If not a list, return nil.
1938 ;; This is so we can set vc-diff-switches to t to override
1939 ;; any switches in diff-switches.
1940 (if (listp switches) switches))))
1942 ;; Old def for compatibility with Emacs-21.[123].
1943 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1944 (make-obsolete 'vc-diff-switches-list 'vc-switches "21.4")
1946 (defun vc-default-diff-tree (backend dir rel1 rel2)
1947 "List differences for all registered files at and below DIR.
1948 The meaning of REL1 and REL2 is the same as for `vc-version-diff'."
1949 ;; This implementation does an explicit tree walk, and calls
1950 ;; vc-BACKEND-diff directly for each file. An optimization
1951 ;; would be to use `vc-diff-internal', so that diffs can be local,
1952 ;; and to call it only for files that are actually changed.
1953 ;; However, this is expensive for some backends, and so it is left
1954 ;; to backend-specific implementations.
1955 (setq default-directory dir)
1956 (vc-file-tree-walk
1957 default-directory
1958 (lambda (f)
1959 (vc-exec-after
1960 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1961 (message "Looking at %s" ',f)
1962 (vc-call-backend ',(vc-backend f)
1963 'diff ',f ',rel1 ',rel2))))))
1965 (defun vc-coding-system-for-diff (file)
1966 "Return the coding system for reading diff output for FILE."
1967 (or coding-system-for-read
1968 ;; if we already have this file open,
1969 ;; use the buffer's coding system
1970 (let ((buf (find-buffer-visiting file)))
1971 (if buf (with-current-buffer buf
1972 buffer-file-coding-system)))
1973 ;; otherwise, try to find one based on the file name
1974 (car (find-operation-coding-system 'insert-file-contents file))
1975 ;; and a final fallback
1976 'undecided))
1978 ;;;###autoload
1979 (defun vc-version-other-window (rev)
1980 "Visit version REV of the current file in another window.
1981 If the current file is named `F', the version is named `F.~REV~'.
1982 If `F.~REV~' already exists, use it instead of checking it out again."
1983 (interactive "sVersion to visit (default is workfile version): ")
1984 (vc-ensure-vc-buffer)
1985 (let* ((file buffer-file-name)
1986 (version (if (string-equal rev "")
1987 (vc-workfile-version file)
1988 rev)))
1989 (switch-to-buffer-other-window (vc-find-version file version))))
1991 (defun vc-find-version (file version)
1992 "Read VERSION of FILE into a buffer and return the buffer."
1993 (let ((automatic-backup (vc-version-backup-file-name file version))
1994 (filebuf (or (get-file-buffer file) (current-buffer)))
1995 (filename (vc-version-backup-file-name file version 'manual)))
1996 (unless (file-exists-p filename)
1997 (if (file-exists-p automatic-backup)
1998 (rename-file automatic-backup filename nil)
1999 (message "Checking out %s..." filename)
2000 (with-current-buffer filebuf
2001 (let ((failed t))
2002 (unwind-protect
2003 (let ((coding-system-for-read 'no-conversion)
2004 (coding-system-for-write 'no-conversion))
2005 (with-temp-file filename
2006 (let ((outbuf (current-buffer)))
2007 ;; Change buffer to get local value of
2008 ;; vc-checkout-switches.
2009 (with-current-buffer filebuf
2010 (vc-call find-version file version outbuf))))
2011 (setq failed nil))
2012 (if (and failed (file-exists-p filename))
2013 (delete-file filename))))
2014 (vc-mode-line file))
2015 (message "Checking out %s...done" filename)))
2016 (find-file-noselect filename)))
2018 (defun vc-default-find-version (backend file rev buffer)
2019 "Provide the new `find-version' op based on the old `checkout' op.
2020 This is only for compatibility with old backends. They should be updated
2021 to provide the `find-version' operation instead."
2022 (let ((tmpfile (make-temp-file (expand-file-name file))))
2023 (unwind-protect
2024 (progn
2025 (vc-call-backend backend 'checkout file nil rev tmpfile)
2026 (with-current-buffer buffer
2027 (insert-file-contents-literally tmpfile)))
2028 (delete-file tmpfile))))
2030 ;; Header-insertion code
2032 ;;;###autoload
2033 (defun vc-insert-headers ()
2034 "Insert headers into a file for use with a version control system.
2035 Headers desired are inserted at point, and are pulled from
2036 the variable `vc-BACKEND-header'."
2037 (interactive)
2038 (vc-ensure-vc-buffer)
2039 (save-excursion
2040 (save-restriction
2041 (widen)
2042 (if (or (not (vc-check-headers))
2043 (y-or-n-p "Version headers already exist. Insert another set? "))
2044 (progn
2045 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2046 (comment-start-vc (or (car delims) comment-start "#"))
2047 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2048 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2049 'header))
2050 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2051 (mapcar (lambda (s)
2052 (insert comment-start-vc "\t" s "\t"
2053 comment-end-vc "\n"))
2054 hdstrings)
2055 (if vc-static-header-alist
2056 (mapcar (lambda (f)
2057 (if (string-match (car f) buffer-file-name)
2058 (insert (format (cdr f) (car hdstrings)))))
2059 vc-static-header-alist))
2061 )))))
2063 (defun vc-clear-headers (&optional file)
2064 "Clear all version headers in the current buffer (or FILE).
2065 The headers are reset to their non-expanded form."
2066 (let* ((filename (or file buffer-file-name))
2067 (visited (find-buffer-visiting filename))
2068 (backend (vc-backend filename)))
2069 (when (vc-find-backend-function backend 'clear-headers)
2070 (if visited
2071 (let ((context (vc-buffer-context)))
2072 ;; save-excursion may be able to relocate point and mark
2073 ;; properly. If it fails, vc-restore-buffer-context
2074 ;; will give it a second try.
2075 (save-excursion
2076 (vc-call-backend backend 'clear-headers))
2077 (vc-restore-buffer-context context))
2078 (set-buffer (find-file-noselect filename))
2079 (vc-call-backend backend 'clear-headers)
2080 (kill-buffer filename)))))
2082 ;;;###autoload
2083 (defun vc-merge ()
2084 "Merge changes between two versions into the current buffer's file.
2085 This asks for two versions to merge from in the minibuffer. If the
2086 first version is a branch number, then merge all changes from that
2087 branch. If the first version is empty, merge news, i.e. recent changes
2088 from the current branch.
2090 See Info node `Merging'."
2091 (interactive)
2092 (vc-ensure-vc-buffer)
2093 (vc-buffer-sync)
2094 (let* ((file buffer-file-name)
2095 (backend (vc-backend file))
2096 (state (vc-state file))
2097 first-version second-version status)
2098 (cond
2099 ((stringp state)
2100 (error "File is locked by %s" state))
2101 ((not (vc-editable-p file))
2102 (if (y-or-n-p
2103 "File must be checked out for merging. Check out now? ")
2104 (vc-checkout file t)
2105 (error "Merge aborted"))))
2106 (setq first-version
2107 (read-string (concat "Branch or version to merge from "
2108 "(default: news on current branch): ")))
2109 (if (string= first-version "")
2110 (if (not (vc-find-backend-function backend 'merge-news))
2111 (error "Sorry, merging news is not implemented for %s" backend)
2112 (setq status (vc-call merge-news file)))
2113 (if (not (vc-find-backend-function backend 'merge))
2114 (error "Sorry, merging is not implemented for %s" backend)
2115 (if (not (vc-branch-p first-version))
2116 (setq second-version
2117 (read-string "Second version: "
2118 (concat (vc-branch-part first-version) ".")))
2119 ;; We want to merge an entire branch. Set versions
2120 ;; accordingly, so that vc-BACKEND-merge understands us.
2121 (setq second-version first-version)
2122 ;; first-version must be the starting point of the branch
2123 (setq first-version (vc-branch-part first-version)))
2124 (setq status (vc-call merge file first-version second-version))))
2125 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2127 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2128 (vc-resynch-buffer file t (not (buffer-modified-p)))
2129 (if (zerop status) (message "Merge successful")
2130 (smerge-mode 1)
2131 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
2132 (vc-resolve-conflicts name-A name-B)
2133 (message "File contains conflict markers"))))
2135 ;;;###autoload
2136 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2138 ;; The VC directory major mode. Coopt Dired for this.
2139 ;; All VC commands get mapped into logical equivalents.
2141 (defvar vc-dired-switches)
2142 (defvar vc-dired-terse-mode)
2144 (defvar vc-dired-mode-map
2145 (let ((map (make-sparse-keymap))
2146 (vmap (make-sparse-keymap)))
2147 (define-key map "\C-xv" vmap)
2148 (define-key map "v" vmap)
2149 (set-keymap-parent vmap vc-prefix-map)
2150 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2151 map))
2153 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
2154 "The major mode used in VC directory buffers.
2156 It works like Dired, but lists only files under version control, with
2157 the current VC state of each file being indicated in the place of the
2158 file's link count, owner, group and size. Subdirectories are also
2159 listed, and you may insert them into the buffer as desired, like in
2160 Dired.
2162 All Dired commands operate normally, with the exception of `v', which
2163 is redefined as the version control prefix, so that you can type
2164 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2165 the file named in the current Dired buffer line. `vv' invokes
2166 `vc-next-action' on this file, or on all files currently marked.
2167 There is a special command, `*l', to mark all files currently locked."
2168 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2169 ;; We do it here because dired might not be loaded yet
2170 ;; when vc-dired-mode-map is initialized.
2171 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2172 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2173 ;; The following is slightly modified from dired.el,
2174 ;; because file lines look a bit different in vc-dired-mode
2175 ;; (the column before the date does not end in a digit).
2176 (set (make-local-variable 'dired-move-to-filename-regexp)
2177 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2178 ;; In some locales, month abbreviations are as short as 2 letters,
2179 ;; and they can be followed by ".".
2180 (month (concat l l "+\\.?"))
2181 (s " ")
2182 (yyyy "[0-9][0-9][0-9][0-9]")
2183 (dd "[ 0-3][0-9]")
2184 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2185 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2186 (zone "[-+][0-2][0-9][0-5][0-9]")
2187 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2188 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2189 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2190 "\\|" yyyy "-" iso-mm-dd "\\)"))
2191 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2192 s "+"
2193 "\\(" HH:MM "\\|" yyyy "\\)"))
2194 (western-comma (concat month s "+" dd "," s "+" yyyy))
2195 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2196 ;; omits the Kanji characters after month and day-of-month.
2197 (mm "[ 0-1]?[0-9]")
2198 (japanese
2199 (concat mm l "?" s dd l "?" s "+"
2200 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2201 ;; the .* below ensures that we find the last match on a line
2202 (concat ".*" s
2203 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2204 s "+")))
2205 (and (boundp 'vc-dired-switches)
2206 vc-dired-switches
2207 (set (make-local-variable 'dired-actual-switches)
2208 vc-dired-switches))
2209 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2210 (setq vc-dired-mode t))
2212 (defun vc-dired-toggle-terse-mode ()
2213 "Toggle terse display in VC Dired."
2214 (interactive)
2215 (if (not vc-dired-mode)
2217 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2218 (if vc-dired-terse-mode
2219 (vc-dired-hook)
2220 (revert-buffer))))
2222 (defun vc-dired-mark-locked ()
2223 "Mark all files currently locked."
2224 (interactive)
2225 (dired-mark-if (let ((f (dired-get-filename nil t)))
2226 (and f
2227 (not (file-directory-p f))
2228 (not (vc-up-to-date-p f))))
2229 "locked file"))
2231 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2233 (defun vc-default-dired-state-info (backend file)
2234 (let ((state (vc-state file)))
2235 (cond
2236 ((stringp state) (concat "(" state ")"))
2237 ((eq state 'edited) (concat "(" (vc-user-login-name) ")"))
2238 ((eq state 'needs-merge) "(merge)")
2239 ((eq state 'needs-patch) "(patch)")
2240 ((eq state 'unlocked-changes) "(stale)"))))
2242 (defun vc-dired-reformat-line (vc-info)
2243 "Reformat a directory-listing line.
2244 Replace various columns with version control information, VC-INFO.
2245 This code, like dired, assumes UNIX -l format."
2246 (beginning-of-line)
2247 (when (re-search-forward
2248 ;; Match link count, owner, group, size. Group may be missing,
2249 ;; and only the size is present in OS/2 -l format.
2250 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2251 (line-end-position) t)
2252 (replace-match (substring (concat vc-info " ") 0 10)
2253 t t nil 1)))
2255 (defun vc-dired-hook ()
2256 "Reformat the listing according to version control.
2257 Called by dired after any portion of a vc-dired buffer has been read in."
2258 (message "Getting version information... ")
2259 (let (subdir filename (buffer-read-only nil))
2260 (goto-char (point-min))
2261 (while (not (eobp))
2262 (cond
2263 ;; subdir header line
2264 ((setq subdir (dired-get-subdir))
2265 ;; if the backend supports it, get the state
2266 ;; of all files in this directory at once
2267 (let ((backend (vc-responsible-backend subdir)))
2268 (if (vc-find-backend-function backend 'dir-state)
2269 (vc-call-backend backend 'dir-state subdir)))
2270 (forward-line 1)
2271 ;; erase (but don't remove) the "total" line
2272 (delete-region (point) (line-end-position))
2273 (beginning-of-line)
2274 (forward-line 1))
2275 ;; file line
2276 ((setq filename (dired-get-filename nil t))
2277 (cond
2278 ;; subdir
2279 ((file-directory-p filename)
2280 (cond
2281 ((member (file-name-nondirectory filename)
2282 vc-directory-exclusion-list)
2283 (let ((pos (point)))
2284 (dired-kill-tree filename)
2285 (goto-char pos)
2286 (dired-kill-line)))
2287 (vc-dired-terse-mode
2288 ;; Don't show directories in terse mode. Don't use
2289 ;; dired-kill-line to remove it, because in recursive listings,
2290 ;; that would remove the directory contents as well.
2291 (delete-region (line-beginning-position)
2292 (progn (forward-line 1) (point))))
2293 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2294 (dired-kill-line))
2296 (vc-dired-reformat-line nil)
2297 (forward-line 1))))
2298 ;; ordinary file
2299 ((and (vc-backend filename)
2300 (not (and vc-dired-terse-mode
2301 (vc-up-to-date-p filename))))
2302 (vc-dired-reformat-line (vc-call dired-state-info filename))
2303 (forward-line 1))
2305 (dired-kill-line))))
2306 ;; any other line
2307 (t (forward-line 1))))
2308 (vc-dired-purge))
2309 (message "Getting version information... done")
2310 (save-restriction
2311 (widen)
2312 (cond ((eq (count-lines (point-min) (point-max)) 1)
2313 (goto-char (point-min))
2314 (message "No files locked under %s" default-directory)))))
2316 (defun vc-dired-purge ()
2317 "Remove empty subdirs."
2318 (goto-char (point-min))
2319 (while (dired-get-subdir)
2320 (forward-line 2)
2321 (if (dired-get-filename nil t)
2322 (if (not (dired-next-subdir 1 t))
2323 (goto-char (point-max)))
2324 (forward-line -2)
2325 (if (not (string= (dired-current-directory) default-directory))
2326 (dired-do-kill-lines t "")
2327 ;; We cannot remove the top level directory.
2328 ;; Just make it look a little nicer.
2329 (forward-line 1)
2330 (kill-line)
2331 (if (not (dired-next-subdir 1 t))
2332 (goto-char (point-max))))))
2333 (goto-char (point-min)))
2335 (defun vc-dired-buffers-for-dir (dir)
2336 "Return a list of all vc-dired buffers that currently display DIR."
2337 (let (result)
2338 ;; Check whether dired is loaded.
2339 (when (fboundp 'dired-buffers-for-dir)
2340 (mapcar (lambda (buffer)
2341 (with-current-buffer buffer
2342 (if vc-dired-mode
2343 (setq result (append result (list buffer))))))
2344 (dired-buffers-for-dir dir)))
2345 result))
2347 (defun vc-dired-resynch-file (file)
2348 "Update the entries for FILE in any VC Dired buffers that list it."
2349 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2350 (when buffers
2351 (mapcar (lambda (buffer)
2352 (with-current-buffer buffer
2353 (if (dired-goto-file file)
2354 ;; bind vc-dired-terse-mode to nil so that
2355 ;; files won't vanish when they are checked in
2356 (let ((vc-dired-terse-mode nil))
2357 (dired-do-redisplay 1)))))
2358 buffers))))
2360 ;;;###autoload
2361 (defun vc-directory (dir read-switches)
2362 "Create a buffer in VC Dired Mode for directory DIR.
2364 See Info node `VC Dired Mode'.
2366 With prefix arg READ-SWITCHES, specify a value to override
2367 `dired-listing-switches' when generating the listing."
2368 (interactive "DDired under VC (directory): \nP")
2369 (let ((vc-dired-switches (concat vc-dired-listing-switches
2370 (if vc-dired-recurse "R" ""))))
2371 (if read-switches
2372 (setq vc-dired-switches
2373 (read-string "Dired listing switches: "
2374 vc-dired-switches)))
2375 (require 'dired)
2376 (require 'dired-aux)
2377 (switch-to-buffer
2378 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2379 vc-dired-switches
2380 'vc-dired-mode))))
2383 ;; Named-configuration entry points
2385 (defun vc-snapshot-precondition (dir)
2386 "Scan the tree below DIR, looking for files not up-to-date.
2387 If any file is not up-to-date, return the name of the first such file.
2388 \(This means, neither snapshot creation nor retrieval is allowed.\)
2389 If one or more of the files are currently visited, return `visited'.
2390 Otherwise, return nil."
2391 (let ((status nil))
2392 (catch 'vc-locked-example
2393 (vc-file-tree-walk
2395 (lambda (f)
2396 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2397 (if (get-file-buffer f) (setq status 'visited)))))
2398 status)))
2400 ;;;###autoload
2401 (defun vc-create-snapshot (dir name branchp)
2402 "Descending recursively from DIR, make a snapshot called NAME.
2403 For each registered file, the version level of its latest version
2404 becomes part of the named configuration. If the prefix argument
2405 BRANCHP is given, the snapshot is made as a new branch and the files
2406 are checked out in that new branch."
2407 (interactive
2408 (list (read-file-name "Directory: " default-directory default-directory t)
2409 (read-string "New snapshot name: ")
2410 current-prefix-arg))
2411 (message "Making %s... " (if branchp "branch" "snapshot"))
2412 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2413 (vc-call-backend (vc-responsible-backend dir)
2414 'create-snapshot dir name branchp)
2415 (message "Making %s... done" (if branchp "branch" "snapshot")))
2417 (defun vc-default-create-snapshot (backend dir name branchp)
2418 (when branchp
2419 (error "VC backend %s does not support module branches" backend))
2420 (let ((result (vc-snapshot-precondition dir)))
2421 (if (stringp result)
2422 (error "File %s is not up-to-date" result)
2423 (vc-file-tree-walk
2425 (lambda (f)
2426 (vc-call assign-name f name))))))
2428 ;;;###autoload
2429 (defun vc-retrieve-snapshot (dir name)
2430 "Descending recursively from DIR, retrieve the snapshot called NAME.
2431 If NAME is empty, it refers to the latest versions.
2432 If locking is used for the files in DIR, then there must not be any
2433 locked files at or below DIR (but if NAME is empty, locked files are
2434 allowed and simply skipped)."
2435 (interactive
2436 (list (read-file-name "Directory: " default-directory default-directory t)
2437 (read-string "Snapshot name to retrieve (default latest versions): ")))
2438 (let ((update (yes-or-no-p "Update any affected buffers? "))
2439 (msg (if (or (not name) (string= name ""))
2440 (format "Updating %s... " (abbreviate-file-name dir))
2441 (format "Retrieving snapshot into %s... "
2442 (abbreviate-file-name dir)))))
2443 (message msg)
2444 (vc-call-backend (vc-responsible-backend dir)
2445 'retrieve-snapshot dir name update)
2446 (message (concat msg "done"))))
2448 (defun vc-default-retrieve-snapshot (backend dir name update)
2449 (if (string= name "")
2450 (progn
2451 (vc-file-tree-walk
2453 (lambda (f) (and
2454 (vc-up-to-date-p f)
2455 (vc-error-occurred
2456 (vc-call checkout f nil "")
2457 (if update (vc-resynch-buffer f t t)))))))
2458 (let ((result (vc-snapshot-precondition dir)))
2459 (if (stringp result)
2460 (error "File %s is locked" result)
2461 (setq update (and (eq result 'visited) update))
2462 (vc-file-tree-walk
2464 (lambda (f) (vc-error-occurred
2465 (vc-call checkout f nil name)
2466 (if update (vc-resynch-buffer f t t)))))))))
2468 ;; Miscellaneous other entry points
2470 ;;;###autoload
2471 (defun vc-print-log ()
2472 "List the change log of the current buffer in a window."
2473 (interactive)
2474 (vc-ensure-vc-buffer)
2475 (let ((file buffer-file-name))
2476 (vc-call print-log file)
2477 (set-buffer "*vc*")
2478 (pop-to-buffer (current-buffer))
2479 (log-view-mode)
2480 (vc-exec-after
2481 `(let ((inhibit-read-only t))
2482 (goto-char (point-max)) (forward-line -1)
2483 (while (looking-at "=*\n")
2484 (delete-char (- (match-end 0) (match-beginning 0)))
2485 (forward-line -1))
2486 (goto-char (point-min))
2487 (if (looking-at "[\b\t\n\v\f\r ]+")
2488 (delete-char (- (match-end 0) (match-beginning 0))))
2489 (shrink-window-if-larger-than-buffer)
2490 ;; move point to the log entry for the current version
2491 (vc-call-backend ',(vc-backend file)
2492 'show-log-entry
2493 ',(vc-workfile-version file))
2494 (set-buffer-modified-p nil)))))
2496 (defun vc-default-show-log-entry (backend rev)
2497 (log-view-goto-rev rev))
2499 (defun vc-default-comment-history (backend file)
2500 "Return a string with all log entries stored in BACKEND for FILE."
2501 (if (vc-find-backend-function backend 'print-log)
2502 (with-current-buffer "*vc*"
2503 (vc-call print-log file)
2504 (vc-call wash-log file)
2505 (buffer-string))))
2507 (defun vc-default-wash-log (backend file)
2508 "Remove all non-comment information from log output.
2509 This default implementation works for RCS logs; backends should override
2510 it if their logs are not in RCS format."
2511 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
2512 "\\(branches: .*;\n\\)?"
2513 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
2514 (goto-char (point-max)) (forward-line -1)
2515 (while (looking-at "=*\n")
2516 (delete-char (- (match-end 0) (match-beginning 0)))
2517 (forward-line -1))
2518 (goto-char (point-min))
2519 (if (looking-at "[\b\t\n\v\f\r ]+")
2520 (delete-char (- (match-end 0) (match-beginning 0))))
2521 (goto-char (point-min))
2522 (re-search-forward separator nil t)
2523 (delete-region (point-min) (point))
2524 (while (re-search-forward separator nil t)
2525 (delete-region (match-beginning 0) (match-end 0)))))
2527 ;;;###autoload
2528 (defun vc-revert-buffer ()
2529 "Revert the current buffer's file to the version it was based on.
2530 This asks for confirmation if the buffer contents are not identical
2531 to that version. This function does not automatically pick up newer
2532 changes found in the master file; use \\[universal-argument] \\[vc-next-action] to do so."
2533 (interactive)
2534 (vc-ensure-vc-buffer)
2535 ;; Make sure buffer is saved. If the user says `no', abort since
2536 ;; we cannot show the changes and ask for confirmation to discard them.
2537 (vc-buffer-sync nil)
2538 (let ((file buffer-file-name)
2539 ;; This operation should always ask for confirmation.
2540 (vc-suppress-confirm nil)
2541 (obuf (current-buffer))
2542 status)
2543 (if (vc-up-to-date-p file)
2544 (unless (yes-or-no-p "File seems up-to-date. Revert anyway? ")
2545 (error "Revert canceled")))
2546 (unless (vc-workfile-unchanged-p file)
2547 ;; vc-diff selects the new window, which is not what we want:
2548 ;; if the new window is on another frame, that'd require the user
2549 ;; moving her mouse to answer the yes-or-no-p question.
2550 (let ((win (save-selected-window
2551 (setq status (vc-diff nil t)) (selected-window))))
2552 (vc-exec-after `(message nil))
2553 (when status
2554 (unwind-protect
2555 (unless (yes-or-no-p "Discard changes? ")
2556 (error "Revert canceled"))
2557 (select-window win)
2558 (if (one-window-p t)
2559 (if (window-dedicated-p (selected-window))
2560 (make-frame-invisible))
2561 (delete-window))))))
2562 (set-buffer obuf)
2563 ;; Do the reverting
2564 (message "Reverting %s..." file)
2565 (vc-revert-file file)
2566 (message "Reverting %s...done" file)))
2568 ;;;###autoload
2569 (defun vc-update ()
2570 "Update the current buffer's file to the latest version on its branch.
2571 If the file contains no changes, and is not locked, then this simply replaces
2572 the working file with the latest version on its branch. If the file contains
2573 changes, and the backend supports merging news, then any recent changes from
2574 the current branch are merged into the working file."
2575 (interactive)
2576 (vc-ensure-vc-buffer)
2577 (vc-buffer-sync nil)
2578 (let ((file buffer-file-name))
2579 (if (vc-up-to-date-p file)
2580 (vc-checkout file nil "")
2581 (if (eq (vc-checkout-model file) 'locking)
2582 (if (eq (vc-state file) 'edited)
2583 (error
2584 (substitute-command-keys
2585 "File is locked--type \\[vc-revert-buffer] to discard changes"))
2586 (error
2587 (substitute-command-keys
2588 "Unexpected file state (%s)--type \\[vc-next-action] to correct")
2589 (vc-state file)))
2590 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
2591 (error "Sorry, merging news is not implemented for %s"
2592 (vc-backend file))
2593 (vc-call merge-news file)
2594 (vc-resynch-window file t t))))))
2596 (defun vc-version-backup-file (file &optional rev)
2597 "Return name of backup file for revision REV of FILE.
2598 If version backups should be used for FILE, and there exists
2599 such a backup for REV or the current workfile version of file,
2600 return its name; otherwise return nil."
2601 (when (vc-call make-version-backups-p file)
2602 (let ((backup-file (vc-version-backup-file-name file rev)))
2603 (if (file-exists-p backup-file)
2604 backup-file
2605 ;; there is no automatic backup, but maybe the user made one manually
2606 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2607 (if (file-exists-p backup-file)
2608 backup-file)))))
2610 (defun vc-revert-file (file)
2611 "Revert FILE back to the version it was based on."
2612 (with-vc-properties
2613 file
2614 (let ((backup-file (vc-version-backup-file file)))
2615 (when backup-file
2616 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2617 (vc-delete-automatic-version-backups file))
2618 (vc-call revert file backup-file))
2619 `((vc-state . up-to-date)
2620 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2621 (vc-resynch-buffer file t t))
2623 ;;;###autoload
2624 (defun vc-cancel-version (norevert)
2625 "Get rid of most recently checked in version of this file.
2626 A prefix argument NOREVERT means do not revert the buffer afterwards."
2627 (interactive "P")
2628 (vc-ensure-vc-buffer)
2629 (let* ((file buffer-file-name)
2630 (backend (vc-backend file))
2631 (target (vc-workfile-version file)))
2632 (cond
2633 ((not (vc-find-backend-function backend 'cancel-version))
2634 (error "Sorry, canceling versions is not supported under %s" backend))
2635 ((not (vc-call latest-on-branch-p file))
2636 (error "This is not the latest version; VC cannot cancel it"))
2637 ((not (vc-up-to-date-p file))
2638 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
2639 (if (null (yes-or-no-p (format "Remove version %s from master? " target)))
2640 (error "Aborted")
2641 (setq norevert (or norevert (not
2642 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2644 (message "Removing last change from %s..." file)
2645 (with-vc-properties
2646 file
2647 (vc-call cancel-version file norevert)
2648 `((vc-state . ,(if norevert 'edited 'up-to-date))
2649 (vc-checkout-time . ,(if norevert
2651 (nth 5 (file-attributes file))))
2652 (vc-workfile-version . nil)))
2653 (message "Removing last change from %s...done" file)
2655 (cond
2656 (norevert ;; clear version headers and mark the buffer modified
2657 (set-visited-file-name file)
2658 (when (not vc-make-backup-files)
2659 ;; inhibit backup for this buffer
2660 (make-local-variable 'backup-inhibited)
2661 (setq backup-inhibited t))
2662 (setq buffer-read-only nil)
2663 (vc-clear-headers)
2664 (vc-mode-line file)
2665 (vc-dired-resynch-file file))
2666 (t ;; revert buffer to file on disk
2667 (vc-resynch-buffer file t t)))
2668 (message "Version %s has been removed from the master" target))))
2670 ;;;###autoload
2671 (defun vc-switch-backend (file backend)
2672 "Make BACKEND the current version control system for FILE.
2673 FILE must already be registered in BACKEND. The change is not
2674 permanent, only for the current session. This function only changes
2675 VC's perspective on FILE, it does not register or unregister it.
2676 By default, this command cycles through the registered backends.
2677 To get a prompt, use a prefix argument."
2678 (interactive
2679 (list
2680 buffer-file-name
2681 (let ((backend (vc-backend buffer-file-name))
2682 (backends nil))
2683 ;; Find the registered backends.
2684 (dolist (backend vc-handled-backends)
2685 (when (vc-call-backend backend 'registered buffer-file-name)
2686 (push backend backends)))
2687 ;; Find the next backend.
2688 (let ((def (car (delq backend (append (memq backend backends) backends))))
2689 (others (delete backend backends)))
2690 (cond
2691 ((null others) (error "No other backend to switch to"))
2692 (current-prefix-arg
2693 (intern
2694 (upcase
2695 (completing-read
2696 (format "Switch to backend [%s]: " def)
2697 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2698 nil t nil nil (downcase (symbol-name def))))))
2699 (t def))))))
2700 (unless (eq backend (vc-backend file))
2701 (vc-file-clearprops file)
2702 (vc-file-setprop file 'vc-backend backend)
2703 ;; Force recomputation of the state
2704 (unless (vc-call-backend backend 'registered file)
2705 (vc-file-clearprops file)
2706 (error "%s is not registered in %s" file backend))
2707 (vc-mode-line file)))
2709 ;;;###autoload
2710 (defun vc-transfer-file (file new-backend)
2711 "Transfer FILE to another version control system NEW-BACKEND.
2712 If NEW-BACKEND has a higher precedence than FILE's current backend
2713 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2714 NEW-BACKEND, using the version number from the current backend as the
2715 base level. If NEW-BACKEND has a lower precedence than the current
2716 backend, then commit all changes that were made under the current
2717 backend to NEW-BACKEND, and unregister FILE from the current backend.
2718 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2719 (let* ((old-backend (vc-backend file))
2720 (edited (memq (vc-state file) '(edited needs-merge)))
2721 (registered (vc-call-backend new-backend 'registered file))
2722 (move
2723 (and registered ; Never move if not registered in new-backend yet.
2724 ;; move if new-backend comes later in vc-handled-backends
2725 (or (memq new-backend (memq old-backend vc-handled-backends))
2726 (y-or-n-p "Final transfer? "))))
2727 (comment nil))
2728 (if (eq old-backend new-backend)
2729 (error "%s is the current backend of %s" new-backend file))
2730 (if registered
2731 (set-file-modes file (logior (file-modes file) 128))
2732 ;; `registered' might have switched under us.
2733 (vc-switch-backend file old-backend)
2734 (let* ((rev (vc-workfile-version file))
2735 (modified-file (and edited (make-temp-file file)))
2736 (unmodified-file (and modified-file (vc-version-backup-file file))))
2737 ;; Go back to the base unmodified file.
2738 (unwind-protect
2739 (progn
2740 (when modified-file
2741 (copy-file file modified-file 'ok-if-already-exists)
2742 ;; If we have a local copy of the unmodified file, handle that
2743 ;; here and not in vc-revert-file because we don't want to
2744 ;; delete that copy -- it is still useful for OLD-BACKEND.
2745 (if unmodified-file
2746 (copy-file unmodified-file file
2747 'ok-if-already-exists 'keep-date)
2748 (if (y-or-n-p "Get base version from master? ")
2749 (vc-revert-file file))))
2750 (vc-call-backend new-backend 'receive-file file rev))
2751 (when modified-file
2752 (vc-switch-backend file new-backend)
2753 (unless (eq (vc-checkout-model file) 'implicit)
2754 (vc-checkout file t nil))
2755 (rename-file modified-file file 'ok-if-already-exists)
2756 (vc-file-setprop file 'vc-checkout-time nil)))))
2757 (when move
2758 (vc-switch-backend file old-backend)
2759 (setq comment (vc-call comment-history file))
2760 (vc-call unregister file))
2761 (vc-switch-backend file new-backend)
2762 (when (or move edited)
2763 (vc-file-setprop file 'vc-state 'edited)
2764 (vc-mode-line file)
2765 (vc-checkin file nil comment (stringp comment)))))
2767 (defun vc-default-unregister (backend file)
2768 "Default implementation of `vc-unregister', signals an error."
2769 (error "Unregistering files is not supported for %s" backend))
2771 (defun vc-default-receive-file (backend file rev)
2772 "Let BACKEND receive FILE from another version control system."
2773 (vc-call-backend backend 'register file rev ""))
2775 (defun vc-rename-master (oldmaster newfile templates)
2776 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2777 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2778 (newdir (or (file-name-directory newfile) ""))
2779 (newbase (file-name-nondirectory newfile))
2780 (masters
2781 ;; List of potential master files for `newfile'
2782 (mapcar
2783 (lambda (s) (vc-possible-master s newdir newbase))
2784 templates)))
2785 (if (or (file-symlink-p oldmaster)
2786 (file-symlink-p (file-name-directory oldmaster)))
2787 (error "This is unsafe in the presence of symbolic links"))
2788 (rename-file
2789 oldmaster
2790 (catch 'found
2791 ;; If possible, keep the master file in the same directory.
2792 (dolist (f masters)
2793 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
2794 (throw 'found f)))
2795 ;; If not, just use the first possible place.
2796 (dolist (f masters)
2797 (and f (or (not (setq dir (file-name-directory f)))
2798 (file-directory-p dir))
2799 (throw 'found f)))
2800 (error "New file lacks a version control directory")))))
2802 (defun vc-delete-file (file)
2803 "Delete file and mark it as such in the version control system."
2804 (interactive "fVC delete file: ")
2805 (let ((buf (get-file-buffer file)))
2806 (unless (vc-find-backend-function backend 'delete-file)
2807 (error "Renaming files under %s is not supported in VC" backend))
2808 (if (and buf (buffer-modified-p buf))
2809 (error "Please save files before deleting them"))
2810 (unless (y-or-n-p (format "Really want to delete %s ? "
2811 (file-name-nondirectory file)))
2812 (error "Abort!"))
2813 (unless (or (file-directory-p file) (null make-backup-files))
2814 (with-current-buffer (or buf (find-file-noselect file))
2815 (let ((backup-inhibited nil))
2816 (backup-buffer))))
2817 (vc-call delete-file file)
2818 ;; If the backend hasn't deleted the file itself, let's do it for him.
2819 (if (file-exists-p file) (delete-file file))))
2821 (defun vc-default-rename-file (backend old new)
2822 (condition-case nil
2823 (add-name-to-file old new)
2824 (error (rename-file old new)))
2825 (vc-delete-file old)
2826 (with-current-buffer (find-file-noselect new)
2827 (vc-register)))
2829 ;;;###autoload
2830 (defun vc-rename-file (old new)
2831 "Rename file OLD to NEW, and rename its master file likewise."
2832 (interactive "fVC rename file: \nFRename to: ")
2833 (let ((oldbuf (get-file-buffer old)))
2834 (if (and oldbuf (buffer-modified-p oldbuf))
2835 (error "Please save files before moving them"))
2836 (if (get-file-buffer new)
2837 (error "Already editing new file name"))
2838 (if (file-exists-p new)
2839 (error "New file already exists"))
2840 (let ((state (vc-state file)))
2841 (unless (memq state '(up-to-date edited))
2842 (error "Please %s files before moving them"
2843 (if (stringp state) "check in" "update"))))
2844 (vc-call rename-file old new)
2845 (vc-file-clearprops old)
2846 ;; Move the actual file (unless the backend did it already)
2847 (if (file-exists-p old) (rename-file old new))
2848 ;; ?? Renaming a file might change its contents due to keyword expansion.
2849 ;; We should really check out a new copy if the old copy was precisely equal
2850 ;; to some checked in version. However, testing for this is tricky....
2851 (if oldbuf
2852 (with-current-buffer oldbuf
2853 (let ((buffer-read-only buffer-read-only))
2854 (set-visited-file-name new))
2855 (vc-backend new)
2856 (vc-mode-line new)
2857 (set-buffer-modified-p nil)))))
2859 ;; Only defined in very recent Emacsen
2860 (defvar small-temporary-file-directory nil)
2862 ;;;###autoload
2863 (defun vc-update-change-log (&rest args)
2864 "Find change log file and add entries from recent version control logs.
2865 Normally, find log entries for all registered files in the default
2866 directory.
2868 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2870 With any numeric prefix arg, find log entries for all currently visited
2871 files that are under version control. This puts all the entries in the
2872 log for the default directory, which may not be appropriate.
2874 From a program, any ARGS are assumed to be filenames for which
2875 log entries should be gathered."
2876 (interactive
2877 (cond ((consp current-prefix-arg) ;C-u
2878 (list buffer-file-name))
2879 (current-prefix-arg ;Numeric argument.
2880 (let ((files nil)
2881 (buffers (buffer-list))
2882 file)
2883 (while buffers
2884 (setq file (buffer-file-name (car buffers)))
2885 (and file (vc-backend file)
2886 (setq files (cons file files)))
2887 (setq buffers (cdr buffers)))
2888 files))
2890 ;; Don't supply any filenames to backend; this means
2891 ;; it should find all relevant files relative to
2892 ;; the default-directory.
2893 nil)))
2894 (vc-call-backend (vc-responsible-backend default-directory)
2895 'update-changelog args))
2897 (defun vc-default-update-changelog (backend files)
2898 "Default implementation of update-changelog.
2899 Uses `rcs2log' which only works for RCS and CVS."
2900 ;; FIXME: We (c|sh)ould add support for cvs2cl
2901 (let ((odefault default-directory)
2902 (changelog (find-change-log))
2903 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2904 (tempfile (make-temp-file
2905 (expand-file-name "vc"
2906 (or small-temporary-file-directory
2907 temporary-file-directory))))
2908 (full-name (or add-log-full-name
2909 (user-full-name)
2910 (user-login-name)
2911 (format "uid%d" (number-to-string (user-uid)))))
2912 (mailing-address (or add-log-mailing-address
2913 user-mail-address)))
2914 (find-file-other-window changelog)
2915 (barf-if-buffer-read-only)
2916 (vc-buffer-sync)
2917 (undo-boundary)
2918 (goto-char (point-min))
2919 (push-mark)
2920 (message "Computing change log entries...")
2921 (message "Computing change log entries... %s"
2922 (unwind-protect
2923 (progn
2924 (setq default-directory odefault)
2925 (if (eq 0 (apply 'call-process
2926 (expand-file-name "rcs2log"
2927 exec-directory)
2928 nil (list t tempfile) nil
2929 "-c" changelog
2930 "-u" (concat (vc-user-login-name)
2931 "\t" full-name
2932 "\t" mailing-address)
2933 (mapcar
2934 (lambda (f)
2935 (file-relative-name
2936 (if (file-name-absolute-p f)
2938 (concat odefault f))))
2939 files)))
2940 "done"
2941 (pop-to-buffer
2942 (set-buffer (get-buffer-create "*vc*")))
2943 (erase-buffer)
2944 (insert-file tempfile)
2945 "failed"))
2946 (setq default-directory (file-name-directory changelog))
2947 (delete-file tempfile)))))
2949 ;; Annotate functionality
2951 ;; Declare globally instead of additional parameter to
2952 ;; temp-buffer-show-function (not possible to pass more than one
2953 ;; parameter). The use of annotate-ratio is deprecated in favor of
2954 ;; annotate-mode, which replaces it with the more sensible "span-to
2955 ;; days", along with autoscaling support.
2956 (defvar vc-annotate-ratio nil "Global variable.")
2957 (defvar vc-annotate-backend nil "Global variable.")
2959 (defconst vc-annotate-font-lock-keywords
2960 ;; The fontification is done by vc-annotate-lines instead of font-lock.
2961 '((vc-annotate-lines)))
2963 (defun vc-annotate-get-backend (buffer)
2964 "Return the backend matching \"Annotate\" buffer BUFFER.
2965 Return nil if no match made. Associations are made based on
2966 `vc-annotate-buffers'."
2967 (cdr (assoc buffer vc-annotate-buffers)))
2969 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2970 "Major mode for output buffers of the `vc-annotate' command.
2972 You can use the mode-specific menu to alter the time-span of the used
2973 colors. See variable `vc-annotate-menu-elements' for customizing the
2974 menu items."
2975 (set (make-local-variable 'truncate-lines) t)
2976 (set (make-local-variable 'font-lock-defaults)
2977 '(vc-annotate-font-lock-keywords t))
2978 (vc-annotate-add-menu))
2980 (defun vc-annotate-display-default (&optional ratio)
2981 "Display the output of \\[vc-annotate] using the default color range.
2982 The color range is given by `vc-annotate-color-map', scaled by RATIO
2983 if present. The current time is used as the offset."
2984 (interactive "e")
2985 (message "Redisplaying annotation...")
2986 (vc-annotate-display
2987 (if ratio (vc-annotate-time-span vc-annotate-color-map ratio)))
2988 (message "Redisplaying annotation...done"))
2990 (defun vc-annotate-display-autoscale (&optional full)
2991 "Highlight the output of \\[vc-annotate]] using an autoscaled color map.
2992 Autoscaling means that the map is scaled from the current time to the
2993 oldest annotation in the buffer, or, with argument FULL non-nil, to
2994 cover the range from the oldest annotation to the newest."
2995 (interactive)
2996 (let ((newest 0.0)
2997 (oldest 999999.) ;Any CVS users at the founding of Rome?
2998 (current (vc-annotate-convert-time (current-time)))
2999 date)
3000 (message "Redisplaying annotation...")
3001 ;; Run through this file and find the oldest and newest dates annotated.
3002 (save-excursion
3003 (goto-char (point-min))
3004 (while (setq date (vc-call-backend vc-annotate-backend 'annotate-time))
3005 (if (> date newest)
3006 (setq newest date))
3007 (if (< date oldest)
3008 (setq oldest date))))
3009 (vc-annotate-display
3010 (vc-annotate-time-span ;return the scaled colormap.
3011 vc-annotate-color-map
3012 (/ (- (if full newest current) oldest)
3013 (vc-annotate-car-last-cons vc-annotate-color-map)))
3014 (if full newest))
3015 (message "Redisplaying annotation...done \(%s\)"
3016 (if full
3017 (format "Spanned from %.1f to %.1f days old"
3018 (- current oldest)
3019 (- current newest))
3020 (format "Spanned to %.1f days old" (- current oldest))))))
3022 ;; Menu -- Using easymenu.el
3023 (defun vc-annotate-add-menu ()
3024 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
3025 (let ((menu-elements vc-annotate-menu-elements)
3026 (menu-def
3027 '("VC-Annotate"
3028 ["Default" (unless (null vc-annotate-display-mode)
3029 (setq vc-annotate-display-mode nil)
3030 (vc-annotate-display-select))
3031 :style toggle :selected (null vc-annotate-display-mode)]))
3032 (oldest-in-map (vc-annotate-car-last-cons vc-annotate-color-map)))
3033 (while menu-elements
3034 (let* ((element (car menu-elements))
3035 (days (* element oldest-in-map)))
3036 (setq menu-elements (cdr menu-elements))
3037 (setq menu-def
3038 (append menu-def
3039 `([,(format "Span %.1f days" days)
3040 (unless (and (numberp vc-annotate-display-mode)
3041 (= vc-annotate-display-mode ,days))
3042 (vc-annotate-display-select nil ,days))
3043 :style toggle :selected
3044 (and (numberp vc-annotate-display-mode)
3045 (= vc-annotate-display-mode ,days)) ])))))
3046 (setq menu-def
3047 (append menu-def
3048 (list
3049 ["Span ..."
3050 (let ((days
3051 (float (string-to-number
3052 (read-string "Span how many days? ")))))
3053 (vc-annotate-display-select nil days)) t])
3054 (list "--")
3055 (list
3056 ["Span to Oldest"
3057 (unless (eq vc-annotate-display-mode 'scale)
3058 (vc-annotate-display-select nil 'scale))
3059 :style toggle :selected
3060 (eq vc-annotate-display-mode 'scale)])
3061 (list
3062 ["Span Oldest->Newest"
3063 (unless (eq vc-annotate-display-mode 'fullscale)
3064 (vc-annotate-display-select nil 'fullscale))
3065 :style toggle :selected
3066 (eq vc-annotate-display-mode 'fullscale)])))
3067 ;; Define the menu
3068 (if (or (featurep 'easymenu) (load "easymenu" t))
3069 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
3070 "VC Annotate Display Menu" menu-def))))
3072 (defun vc-annotate-display-select (&optional buffer mode)
3073 "Highlight the output of \\[vc-annotate].
3074 By default, the current buffer is highlighted, unless overridden by
3075 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
3076 use; you may override this using the second optional arg MODE."
3077 (interactive)
3078 (if mode (setq vc-annotate-display-mode mode))
3079 (when buffer
3080 (set-buffer buffer)
3081 (display-buffer buffer))
3082 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
3083 (vc-annotate-mode))
3084 (cond ((null vc-annotate-display-mode)
3085 (vc-annotate-display-default vc-annotate-ratio))
3086 ;; One of the auto-scaling modes
3087 ((eq vc-annotate-display-mode 'scale)
3088 (vc-annotate-display-autoscale))
3089 ((eq vc-annotate-display-mode 'fullscale)
3090 (vc-annotate-display-autoscale t))
3091 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3092 (vc-annotate-display-default
3093 (/ vc-annotate-display-mode (vc-annotate-car-last-cons
3094 vc-annotate-color-map))))
3095 (t (error "No such display mode: %s"
3096 vc-annotate-display-mode))))
3098 ;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
3099 ;;;; Execute "annotate" on FILE by using `call-process' and insert
3100 ;;;; the contents in BUFFER.
3102 ;;;###autoload
3103 (defun vc-annotate (prefix)
3104 "Display the edit history of the current file using colours.
3106 This command creates a buffer that shows, for each line of the current
3107 file, when it was last edited and by whom. Additionally, colours are
3108 used to show the age of each line--blue means oldest, red means
3109 youngest, and intermediate colours indicate intermediate ages. By
3110 default, the time scale stretches back one year into the past;
3111 everything that is older than that is shown in blue.
3113 With a prefix argument, this command asks two questions in the
3114 minibuffer. First, you may enter a version number; then the buffer
3115 displays and annotates that version instead of the current version
3116 \(type RET in the minibuffer to leave that default unchanged). Then,
3117 you are prompted for the time span in days which the color range
3118 should cover. For example, a time span of 20 days means that changes
3119 over the past 20 days are shown in red to blue, according to their
3120 age, and everything that is older than that is shown in blue.
3122 Customization variables:
3124 `vc-annotate-menu-elements' customizes the menu elements of the
3125 mode-specific menu. `vc-annotate-color-map' and
3126 `vc-annotate-very-old-color' defines the mapping of time to
3127 colors. `vc-annotate-background' specifies the background color."
3128 (interactive "P")
3129 (vc-ensure-vc-buffer)
3130 (let* ((temp-buffer-name (concat "*Annotate " (buffer-name) "*"))
3131 (temp-buffer-show-function 'vc-annotate-display-select)
3132 (rev (vc-workfile-version buffer-file-name))
3133 (vc-annotate-version
3134 (if prefix (read-string
3135 (format "Annotate from version: (default %s) " rev)
3136 nil nil rev)
3137 rev)))
3138 (if prefix
3139 (setq vc-annotate-display-mode
3140 (float (string-to-number
3141 (read-string "Annotate span days: (default 20) "
3142 nil nil "20")))))
3143 (setq vc-annotate-backend (vc-backend buffer-file-name))
3144 (message "Annotating...")
3145 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
3146 (error "Sorry, annotating is not implemented for %s"
3147 vc-annotate-backend))
3148 (with-output-to-temp-buffer temp-buffer-name
3149 (vc-call-backend vc-annotate-backend 'annotate-command
3150 buffer-file-name
3151 (get-buffer temp-buffer-name)
3152 vc-annotate-version))
3153 ;; Don't use the temp-buffer-name until the buffer is created
3154 ;; (only after `with-output-to-temp-buffer'.)
3155 (setq vc-annotate-buffers
3156 (append vc-annotate-buffers
3157 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend))))
3158 (message "Annotating... done")))
3160 (defun vc-annotate-car-last-cons (a-list)
3161 "Return car of last cons in association list A-LIST."
3162 (if (not (eq nil (cdr a-list)))
3163 (vc-annotate-car-last-cons (cdr a-list))
3164 (car (car a-list))))
3166 (defun vc-annotate-time-span (a-list span &optional quantize)
3167 "Apply factor SPAN to the time-span of association list A-LIST.
3168 Return the new alist.
3169 Optionally quantize to the factor of QUANTIZE."
3170 ;; Apply span to each car of every cons
3171 (if (not (eq nil a-list))
3172 (append (list (cons (* (car (car a-list)) span)
3173 (cdr (car a-list))))
3174 (vc-annotate-time-span (nthcdr (or quantize ; optional
3175 1) ; Default to cdr
3176 a-list) span quantize))))
3178 (defun vc-annotate-compcar (threshold a-list)
3179 "Test successive cons cells of A-LIST against THRESHOLD.
3180 Return the first cons cell with a car that is not less than THRESHOLD,
3181 nil if no such cell exists."
3182 (let ((i 1)
3183 (tmp-cons (car a-list)))
3184 (while (and tmp-cons (< (car tmp-cons) threshold))
3185 (setq tmp-cons (car (nthcdr i a-list)))
3186 (setq i (+ i 1)))
3187 tmp-cons)) ; Return the appropriate value
3189 (defun vc-annotate-convert-time (time)
3190 "Convert a time value to a floating-point number of days.
3191 The argument TIME is a list as returned by `current-time' or
3192 `encode-time', only the first two elements of that list are considered."
3193 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3195 (defun vc-annotate-difference (&optional offset)
3196 "Return the time span in days to the next annotation.
3197 This calls the backend function annotate-time, and returns the
3198 difference in days between the time returned and the current time,
3199 or OFFSET if present."
3200 (let ((next-time (vc-call-backend vc-annotate-backend 'annotate-time)))
3201 (if next-time
3202 (- (or offset
3203 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3204 next-time))))
3206 (defun vc-default-annotate-current-time (backend)
3207 "Return the current time, encoded as fractional days."
3208 (vc-annotate-convert-time (current-time)))
3210 (defun vc-annotate-display (&optional color-map offset)
3211 "Highlight `vc-annotate' output in the current buffer.
3212 COLOR-MAP, if present, overrides `vc-annotate-color-map'.
3213 The annotations are relative to the current time, unless overridden by OFFSET."
3214 (if (and color-map (not (eq color-map vc-annotate-color-map)))
3215 (set (make-local-variable 'vc-annotate-color-map) color-map))
3216 (set (make-local-variable 'vc-annotate-offset) offset)
3217 (font-lock-mode 1))
3218 (make-obsolete 'vc-annotate-display 'vc-annotate-display-select "21.4")
3220 (defvar vc-annotate-offset nil)
3222 (defun vc-annotate-lines (limit)
3223 (let (difference)
3224 (while (and (< (point) limit)
3225 (setq difference (vc-annotate-difference vc-annotate-offset)))
3226 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3227 (cons nil vc-annotate-very-old-color)))
3228 ;; substring from index 1 to remove any leading `#' in the name
3229 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
3230 ;; Make the face if not done.
3231 (face (or (intern-soft face-name)
3232 (let ((tmp-face (make-face (intern face-name))))
3233 (set-face-foreground tmp-face (cdr color))
3234 (if vc-annotate-background
3235 (set-face-background tmp-face
3236 vc-annotate-background))
3237 tmp-face))) ; Return the face
3238 (point (point)))
3239 (forward-line 1)
3240 (put-text-property point (point) 'face face)))
3241 ;; Pretend to font-lock there were no matches.
3242 nil))
3244 ;; Collect back-end-dependent stuff here
3246 (defalias 'vc-default-logentry-check 'ignore)
3248 (defun vc-check-headers ()
3249 "Check if the current file has any headers in it."
3250 (interactive)
3251 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3253 (defun vc-default-check-headers (backend)
3254 "Default implementation of check-headers; always returns nil."
3255 nil)
3257 ;; Back-end-dependent stuff ends here.
3259 ;; Set up key bindings for use while editing log messages
3261 (defun vc-log-edit (file)
3262 "Set up `log-edit' for use with VC on FILE."
3263 (setq default-directory
3264 (if file (file-name-directory file)
3265 (with-current-buffer vc-parent-buffer default-directory)))
3266 (log-edit 'vc-finish-logentry nil
3267 (if file `(lambda () ',(list (file-name-nondirectory file)))
3268 ;; If FILE is nil, we were called from vc-dired.
3269 (lambda ()
3270 (with-current-buffer vc-parent-buffer
3271 (dired-get-marked-files t)))))
3272 (set (make-local-variable 'vc-log-file) file)
3273 (make-local-variable 'vc-log-version)
3274 (set-buffer-modified-p nil)
3275 (setq buffer-file-name nil))
3277 ;; These things should probably be generally available
3279 (defun vc-file-tree-walk (dirname func &rest args)
3280 "Walk recursively through DIRNAME.
3281 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3282 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3283 (message "Traversing directory %s...done" dirname))
3285 (defun vc-file-tree-walk-internal (file func args)
3286 (if (not (file-directory-p file))
3287 (if (vc-backend file) (apply func file args))
3288 (message "Traversing directory %s..." (abbreviate-file-name file))
3289 (let ((dir (file-name-as-directory file)))
3290 (mapcar
3291 (lambda (f) (or
3292 (string-equal f ".")
3293 (string-equal f "..")
3294 (member f vc-directory-exclusion-list)
3295 (let ((dirf (expand-file-name f dir)))
3297 (file-symlink-p dirf);; Avoid possible loops
3298 (vc-file-tree-walk-internal dirf func args)))))
3299 (directory-files dir)))))
3301 (provide 'vc)
3303 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3305 ;; These may be useful to anyone who has to debug or extend the package.
3306 ;; (Note that this information corresponds to versions 5.x. Some of it
3307 ;; might have been invalidated by the additions to support branching
3308 ;; and RCS keyword lookup. AS, 1995/03/24)
3310 ;; A fundamental problem in VC is that there are time windows between
3311 ;; vc-next-action's computations of the file's version-control state and
3312 ;; the actions that change it. This is a window open to lossage in a
3313 ;; multi-user environment; someone else could nip in and change the state
3314 ;; of the master during it.
3316 ;; The performance problem is that rlog/prs calls are very expensive; we want
3317 ;; to avoid them as much as possible.
3319 ;; ANALYSIS:
3321 ;; The performance problem, it turns out, simplifies in practice to the
3322 ;; problem of making vc-state fast. The two other functions that call
3323 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3324 ;; makes snapshots, the other deletes the calling user's last change in the
3325 ;; master.
3327 ;; The race condition implies that we have to either (a) lock the master
3328 ;; during the entire execution of vc-next-action, or (b) detect and
3329 ;; recover from errors resulting from dispatch on an out-of-date state.
3331 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3332 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3333 ;; checkin, the change message buffer pops up, and the user, having wandered
3334 ;; off to do something else, simply forgets about it?
3336 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3337 ;; vc-state. Usually, if a file is registered, we can read its locked/
3338 ;; unlocked state and its current owner from its permissions.
3340 ;; This shortcut will fail if someone has manually changed the workfile's
3341 ;; permissions; also if developers are munging the workfile in several
3342 ;; directories, with symlinks to a master (in this latter case, the
3343 ;; permissions shortcut will fail to detect a lock asserted from another
3344 ;; directory).
3346 ;; Note that these cases correspond exactly to the errors which could happen
3347 ;; because of a competing checkin/checkout race in between two instances of
3348 ;; vc-next-action.
3350 ;; For VC's purposes, a workfile/master pair may have the following states:
3352 ;; A. Unregistered. There is a workfile, there is no master.
3354 ;; B. Registered and not locked by anyone.
3356 ;; C. Locked by calling user and unchanged.
3358 ;; D. Locked by the calling user and changed.
3360 ;; E. Locked by someone other than the calling user.
3362 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3364 ;; VC's idea of state
3365 ;; |
3366 ;; V Actual state RCS action SCCS action Effect
3367 ;; A B C D E
3368 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3369 ;; B 5 . 6 7 8 co -l get -e checkout
3370 ;; C 9 10 . 11 12 co -u unget; get revert
3371 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3372 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3374 ;; All commands take the master file name as a last argument (not shown).
3376 ;; In the discussion below, a "self-race" is a pathological situation in
3377 ;; which VC operations are being attempted simultaneously by two or more
3378 ;; Emacsen running under the same username.
3380 ;; The vc-next-action code has the following windows:
3382 ;; Window P:
3383 ;; Between the check for existence of a master file and the call to
3384 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3385 ;; never close if the initial-comment feature is on.
3387 ;; Window Q:
3388 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3389 ;; following revert (apparent state C).
3391 ;; Window R:
3392 ;; Between the call to vc-workfile-unchanged-p in and the following
3393 ;; checkin (apparent state D). This window may never close.
3395 ;; Window S:
3396 ;; Between the unlock and the immediately following checkout during a
3397 ;; revert operation (apparent state C). Included in window Q.
3399 ;; Window T:
3400 ;; Between vc-state and the following checkout (apparent state B).
3402 ;; Window U:
3403 ;; Between vc-state and the following revert (apparent state C).
3404 ;; Includes windows Q and S.
3406 ;; Window V:
3407 ;; Between vc-state and the following checkin (apparent state
3408 ;; D). This window may never be closed if the user fails to complete the
3409 ;; checkin message. Includes window R.
3411 ;; Window W:
3412 ;; Between vc-state and the following steal-lock (apparent
3413 ;; state E). This window may never close if the user fails to complete
3414 ;; the steal-lock message. Includes window X.
3416 ;; Window X:
3417 ;; Between the unlock and the immediately following re-lock during a
3418 ;; steal-lock operation (apparent state E). This window may never close
3419 ;; if the user fails to complete the steal-lock message.
3421 ;; Errors:
3423 ;; Apparent state A ---
3425 ;; 1. File looked unregistered but is actually registered and not locked.
3427 ;; Potential cause: someone else's admin during window P, with
3428 ;; caller's admin happening before their checkout.
3430 ;; RCS: Prior to version 5.6.4, ci fails with message
3431 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3432 ;; ci -i option and the message is "<file>,v: already exists".
3433 ;; SCCS: admin will fail with error (ad19).
3435 ;; We can let these errors be passed up to the user.
3437 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3439 ;; Potential cause: self-race during window P.
3441 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3442 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3443 ;; ci -i option, failing with message "<file>,v: already exists".
3444 ;; SCCS: will fail with error (ad19).
3446 ;; Either of these consequences is acceptable.
3448 ;; 3. File looked unregistered but is actually locked by caller, changed.
3450 ;; Potential cause: self-race during window P.
3452 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3453 ;; a delta with a null change comment (the -t- switch will be
3454 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3455 ;; failing with message "<file>,v: already exists".
3456 ;; SCCS: will fail with error (ad19).
3458 ;; 4. File looked unregistered but is locked by someone else.
3460 ;; Potential cause: someone else's admin during window P, with
3461 ;; caller's admin happening *after* their checkout.
3463 ;; RCS: Prior to version 5.6.4, ci fails with a
3464 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3465 ;; VC uses the new ci -i option, failing with message
3466 ;; "<file>,v: already exists".
3467 ;; SCCS: will fail with error (ad19).
3469 ;; We can let these errors be passed up to the user.
3471 ;; Apparent state B ---
3473 ;; 5. File looked registered and not locked, but is actually unregistered.
3475 ;; Potential cause: master file got nuked during window P.
3477 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3478 ;; SCCS: will fail with error ut4.
3480 ;; We can let these errors be passed up to the user.
3482 ;; 6. File looked registered and not locked, but is actually locked by the
3483 ;; calling user and unchanged.
3485 ;; Potential cause: self-race during window T.
3487 ;; RCS: in the same directory as the previous workfile, co -l will fail
3488 ;; with "co error: writable foo exists; checkout aborted". In any other
3489 ;; directory, checkout will succeed.
3490 ;; SCCS: will fail with ge17.
3492 ;; Either of these consequences is acceptable.
3494 ;; 7. File looked registered and not locked, but is actually locked by the
3495 ;; calling user and changed.
3497 ;; As case 6.
3499 ;; 8. File looked registered and not locked, but is actually locked by another
3500 ;; user.
3502 ;; Potential cause: someone else checks it out during window T.
3504 ;; RCS: co error: revision 1.3 already locked by <user>
3505 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3507 ;; We can let these errors be passed up to the user.
3509 ;; Apparent state C ---
3511 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3513 ;; As case 5.
3515 ;; 10. File looks locked by calling user and unchanged, but is actually not
3516 ;; locked.
3518 ;; Potential cause: a self-race in window U, or by the revert's
3519 ;; landing during window X of some other user's steal-lock or window S
3520 ;; of another user's revert.
3522 ;; RCS: succeeds, refreshing the file from the identical version in
3523 ;; the master.
3524 ;; SCCS: fails with error ut4 (p file nonexistent).
3526 ;; Either of these consequences is acceptable.
3528 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3529 ;; changed.
3531 ;; Potential cause: the file would have to be touched by a self-race
3532 ;; during window Q.
3534 ;; The revert will succeed, removing whatever changes came with
3535 ;; the touch. It is theoretically possible that work could be lost.
3537 ;; 12. File looks like it's locked by the calling user and unchanged, but
3538 ;; it's actually locked by someone else.
3540 ;; Potential cause: a steal-lock in window V.
3542 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3543 ;; SCCS: fails with error un2
3545 ;; We can pass these errors up to the user.
3547 ;; Apparent state D ---
3549 ;; 13. File looks like it's locked by the calling user and changed, but it's
3550 ;; actually unregistered.
3552 ;; Potential cause: master file got nuked during window P.
3554 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3555 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3556 ;; option, failing with message "no such file or directory".
3557 ;; SCCS: will fail with error ut4.
3559 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3560 ;; VC may fail to detect the loss of previous version information.
3562 ;; 14. File looks like it's locked by the calling user and changed, but it's
3563 ;; actually unlocked.
3565 ;; Potential cause: self-race in window V, or the checkin happening
3566 ;; during the window X of someone else's steal-lock or window S of
3567 ;; someone else's revert.
3569 ;; RCS: ci will fail with "no lock set by <user>".
3570 ;; SCCS: delta will fail with error ut4.
3572 ;; 15. File looks like it's locked by the calling user and changed, but it's
3573 ;; actually locked by the calling user and unchanged.
3575 ;; Potential cause: another self-race --- a whole checkin/checkout
3576 ;; sequence by the calling user would have to land in window R.
3578 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3579 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3580 ;; the file unlocked.
3582 ;; It is theoretically possible that work could be lost under RCS.
3584 ;; 16. File looks like it's locked by the calling user and changed, but it's
3585 ;; actually locked by a different user.
3587 ;; RCS: ci error: no lock set by <user>
3588 ;; SCCS: unget will fail with error un2
3590 ;; We can pass these errors up to the user.
3592 ;; Apparent state E ---
3594 ;; 17. File looks like it's locked by some other user, but it's actually
3595 ;; unregistered.
3597 ;; As case 13.
3599 ;; 18. File looks like it's locked by some other user, but it's actually
3600 ;; unlocked.
3602 ;; Potential cause: someone released a lock during window W.
3604 ;; RCS: The calling user will get the lock on the file.
3605 ;; SCCS: unget -n will fail with cm4.
3607 ;; Either of these consequences will be OK.
3609 ;; 19. File looks like it's locked by some other user, but it's actually
3610 ;; locked by the calling user and unchanged.
3612 ;; Potential cause: the other user relinquishing a lock followed by
3613 ;; a self-race, both in window W.
3615 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3616 ;; the sequence a no-op.
3618 ;; 20. File looks like it's locked by some other user, but it's actually
3619 ;; locked by the calling user and changed.
3621 ;; As case 19.
3623 ;; PROBLEM CASES:
3625 ;; In order of decreasing severity:
3627 ;; Cases 11 and 15 are the only ones that potentially lose work.
3628 ;; They would require a self-race for this to happen.
3630 ;; Case 13 in RCS loses information about previous deltas, retaining
3631 ;; only the information in the current workfile. This can only happen
3632 ;; if the master file gets nuked in window P.
3634 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3635 ;; no change comment in the master. This would require a self-race in
3636 ;; window P or R respectively.
3638 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3640 ;; Unfortunately, it appears to me that no recovery is possible in these
3641 ;; cases. They don't yield error messages, so there's no way to tell that
3642 ;; a race condition has occurred.
3644 ;; All other cases don't change either the workfile or the master, and
3645 ;; trigger command errors which the user will see.
3647 ;; Thus, there is no explicit recovery code.
3649 ;;; vc.el ends here