(find-library): Wrap search for library name in condition-case
[emacs.git] / lisp / vc.el
blob35c371a60389d05d851da9b132466a2d339dfea6
1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see below for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Keywords: tools
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 3, 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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Credits:
30 ;; VC was initially designed and implemented by Eric S. Raymond
31 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
32 ;; contributed substantial amounts of work to VC. These include:
34 ;; Per Cederqvist <ceder@lysator.liu.se>
35 ;; Paul Eggert <eggert@twinsun.com>
36 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
37 ;; Martin Lorentzson <martinl@gnu.org>
38 ;; Dave Love <fx@gnu.org>
39 ;; Stefan Monnier <monnier@cs.yale.edu>
40 ;; J.D. Smith <jdsmith@alum.mit.edu>
41 ;; Andre Spiegel <spiegel@gnu.org>
42 ;; Richard Stallman <rms@gnu.org>
43 ;; Thien-Thi Nguyen <ttn@gnu.org>
45 ;; In July 2007 ESR returned and redesigned the mode to cope better
46 ;; with modern version-control systems that do commits by fileset
47 ;; rather than per individual file.
49 ;; Features in the new version:
50 ;; * Key commands (vc-next-action = C-x v v, vc-print-log = C-x v l, vc-revert
51 ;; = C-x v u, vc-rollback = C-x v c, vc-diff = C-x v =, vc-update = C-x v +)
52 ;; now operate on filesets rather than individual files.
53 ;; * The fileset for a command is either (a) all marked files in VC-dired
54 ;; mode, (b) the currently visited file if it's under version control,
55 ;; or (c) the current directory if the visited buffer is not under
56 ;; version control and a wildcarding-enable flag has been set.
58 ;; If you maintain a client of the mode or customize it in your .emacs,
59 ;; note that some backend functions which formerly took single file arguments
60 ;; now take a list of files. These include: register, checkin, print-log,
61 ;; rollback, and diff.
63 ;;; Commentary:
65 ;; This mode is fully documented in the Emacs user's manual.
67 ;; Supported version-control systems presently include CVS, RCS, GNU
68 ;; Arch, Subversion, Bzr, Git, Mercurial, Meta-CVS, Monotone and SCCS
69 ;; (or its free replacement, CSSC).
71 ;; Some features will not work with old RCS versions. Where
72 ;; appropriate, VC finds out which version you have, and allows or
73 ;; disallows those features (stealing locks, for example, works only
74 ;; from 5.6.2 onwards).
75 ;; Even initial checkins will fail if your RCS version is so old that ci
76 ;; doesn't understand -t-; this has been known to happen to people running
77 ;; NExTSTEP 3.0.
79 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
81 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
82 ;; to be installed somewhere on Emacs's path for executables.
84 ;; If your site uses the ChangeLog convention supported by Emacs, the
85 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
86 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
87 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
89 ;; The vc code maintains some internal state in order to reduce expensive
90 ;; version-control operations to a minimum. Some names are only computed
91 ;; once. If you perform version control operations with the backend while
92 ;; vc's back is turned, or move/rename master files while vc is running,
93 ;; vc may get seriously confused. Don't do these things!
95 ;; Developer's notes on some concurrency issues are included at the end of
96 ;; the file.
98 ;; ADDING SUPPORT FOR OTHER BACKENDS
100 ;; VC can use arbitrary version control systems as a backend. To add
101 ;; support for a new backend named SYS, write a library vc-sys.el that
102 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
103 ;; case for the function and library names). VC will use that library if
104 ;; you put the symbol SYS somewhere into the list of
105 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
106 ;; returns non-nil for a file, all SYS-specific versions of VC commands
107 ;; will be available for that file.
109 ;; VC keeps some per-file information in the form of properties (see
110 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
111 ;; do not generally need to be aware of these properties. For example,
112 ;; `vc-sys-working-revision' should compute the working revision and
113 ;; return it; it should not look it up in the property, and it needn't
114 ;; store it there either. However, if a backend-specific function does
115 ;; store a value in a property, that value takes precedence over any
116 ;; value that the generic code might want to set (check for uses of
117 ;; the macro `with-vc-properties' in vc.el).
119 ;; In the list of functions below, each identifier needs to be prepended
120 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
121 ;; `*'), others are optional (`-').
123 ;; BACKEND PROPERTIES
125 ;; * revision-granularity
127 ;; Takes no arguments. Returns either 'file or 'repository. Backends
128 ;; that return 'file have per-file revision numbering; backends
129 ;; that return 'repository have per-repository revision numbering,
130 ;; so a revision level implicitly identifies a changeset
132 ;; STATE-QUERYING FUNCTIONS
134 ;; * registered (file)
136 ;; Return non-nil if FILE is registered in this backend. Both this
137 ;; function as well as `state' should be careful to fail gracefully
138 ;; in the event that the backend executable is absent. It is
139 ;; preferable that this function's body is autoloaded, that way only
140 ;; calling vc-registered does not cause the backend to be loaded
141 ;; (all the vc-FOO-registered functions are called to try to find
142 ;; the controlling backend for FILE.
144 ;; * state (file)
146 ;; Return the current version control state of FILE. For a list of
147 ;; possible values, see `vc-state'. This function should do a full and
148 ;; reliable state computation; it is usually called immediately after
149 ;; C-x v v. If you want to use a faster heuristic when visiting a
150 ;; file, put that into `state-heuristic' below.
152 ;; - state-heuristic (file)
154 ;; If provided, this function is used to estimate the version control
155 ;; state of FILE at visiting time. It should be considerably faster
156 ;; than the implementation of `state'. For a list of possible values,
157 ;; see the doc string of `vc-state'.
159 ;; - dir-state (dir)
161 ;; If provided, this function is used to find the version control
162 ;; state of as many files as possible in DIR, and all subdirectories
163 ;; of DIR, in a fast way; it is used to avoid expensive indivitual
164 ;; vc-state calls. The function should not return anything, but
165 ;; rather store the files' states into the corresponding properties.
166 ;; Two properties are required: `vc-backend' and `vc-state'. (Note:
167 ;; in older versions this method was not required to recurse into
168 ;; subdirectories.)
170 ;; * working-revision (file)
172 ;; Return the working revision of FILE. This is the revision fetched
173 ;; by the last checkout or upate, not necessarily the same thing as the
174 ;; head or tip revision. Should return "0" for a file added but not yet
175 ;; committed.
177 ;; - latest-on-branch-p (file)
179 ;; Return non-nil if the working revision of FILE is the latest revision
180 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
181 ;; The default implementation always returns t, which means that
182 ;; working with non-current revisions is not supported by default.
184 ;; * checkout-model (file)
186 ;; Indicate whether FILE needs to be "checked out" before it can be
187 ;; edited. See `vc-checkout-model' for a list of possible values.
189 ;; - workfile-unchanged-p (file)
191 ;; Return non-nil if FILE is unchanged from the working revision.
192 ;; This function should do a brief comparison of FILE's contents
193 ;; with those of the repository master of the working revision. If
194 ;; the backend does not have such a brief-comparison feature, the
195 ;; default implementation of this function can be used, which
196 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
197 ;; must not run asynchronously in this case, see variable
198 ;; `vc-disable-async-diff'.)
200 ;; - mode-line-string (file)
202 ;; If provided, this function should return the VC-specific mode
203 ;; line string for FILE. The returned string should have a
204 ;; `help-echo' property which is the text to be displayed as a
205 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
206 ;; The default implementation deals well with all states that
207 ;; `vc-state' can return.
209 ;; - dired-state-info (file)
211 ;; Translate the `vc-state' property of FILE into a string that can be
212 ;; used in a vc-dired buffer. The default implementation deals well
213 ;; with all states that `vc-state' can return.
215 ;; STATE-CHANGING FUNCTIONS
217 ;; * create-repo (backend)
219 ;; Create an empty repository in the current directory and initialize
220 ;; it so VC mode can add files to it. For file-oriented systems, this
221 ;; need do no more than create a subdirectory with the right name.
223 ;; * register (files &optional rev comment)
225 ;; Register FILES in this backend. Optionally, an initial revision REV
226 ;; and an initial description of the file, COMMENT, may be specified,
227 ;; but it is not guaranteed that the backend will do anything with this.
228 ;; The implementation should pass the value of vc-register-switches
229 ;; to the backend command. (Note: in older versions of VC, this
230 ;; command took a single file argument and not a list.)
232 ;; - init-revision (file)
234 ;; The initial revision to use when registering FILE if one is not
235 ;; specified by the user. If not provided, the variable
236 ;; vc-default-init-revision is used instead.
238 ;; - responsible-p (file)
240 ;; Return non-nil if this backend considers itself "responsible" for
241 ;; FILE, which can also be a directory. This function is used to find
242 ;; out what backend to use for registration of new files and for things
243 ;; like change log generation. The default implementation always
244 ;; returns nil.
246 ;; - could-register (file)
248 ;; Return non-nil if FILE could be registered under this backend. The
249 ;; default implementation always returns t.
251 ;; - receive-file (file rev)
253 ;; Let this backend "receive" a file that is already registered under
254 ;; another backend. The default implementation simply calls `register'
255 ;; for FILE, but it can be overridden to do something more specific,
256 ;; e.g. keep revision numbers consistent or choose editing modes for
257 ;; FILE that resemble those of the other backend.
259 ;; - unregister (file)
261 ;; Unregister FILE from this backend. This is only needed if this
262 ;; backend may be used as a "more local" backend for temporary editing.
264 ;; * checkin (files rev comment)
266 ;; Commit changes in FILES to this backend. If REV is non-nil, that
267 ;; should become the new revision number (not all backends do
268 ;; anything with it). COMMENT is used as a check-in comment. The
269 ;; implementation should pass the value of vc-checkin-switches to
270 ;; the backend command. (Note: in older versions of VC, this
271 ;; command took a single file argument and not a list.)
273 ;; * find-revision (file rev buffer)
275 ;; Fetch revision REV of file FILE and put it into BUFFER.
276 ;; If REV is the empty string, fetch the head of the trunk.
277 ;; The implementation should pass the value of vc-checkout-switches
278 ;; to the backend command.
280 ;; * checkout (file &optional editable rev)
282 ;; Check out revision REV of FILE into the working area. If EDITABLE
283 ;; is non-nil, FILE should be writable by the user and if locking is
284 ;; used for FILE, a lock should also be set. If REV is non-nil, that
285 ;; is the revision to check out (default is the working revision).
286 ;; If REV is t, that means to check out the head of the current branch;
287 ;; if it is the empty string, check out the head of the trunk.
288 ;; The implementation should pass the value of vc-checkout-switches
289 ;; to the backend command.
291 ;; * revert (file &optional contents-done)
293 ;; Revert FILE back to the working revision. If optional
294 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
295 ;; already been reverted from a version backup, and this function
296 ;; only needs to update the status of FILE within the backend.
298 ;; - rollback (files)
300 ;; Remove the tip revision of each of FILES from the repository. If
301 ;; this function is not provided, trying to cancel a revision is
302 ;; caught as an error. (Most backends don't provide it.) (Also
303 ;; note that older versions of this backend command were called
304 ;; 'cancel-version' and took a single file arg, not a list of
305 ;; files.)
307 ;; - merge (file rev1 rev2)
309 ;; Merge the changes between REV1 and REV2 into the current working file.
311 ;; - merge-news (file)
313 ;; Merge recent changes from the current branch into FILE.
315 ;; - steal-lock (file &optional revision)
317 ;; Steal any lock on the working revision of FILE, or on REVISION if
318 ;; that is provided. This function is only needed if locking is
319 ;; used for files under this backend, and if files can indeed be
320 ;; locked by other users.
322 ;; - modify-change-comment (files rev comment)
324 ;; Modify the change comments associated with the files at the
325 ;; given revision. This is optional, many backends do not support it.
327 ;; HISTORY FUNCTIONS
329 ;; * print-log (files &optional buffer)
331 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
332 ;; if BUFFER is nil. (Note: older versions of this function expected
333 ;; only a single file argument.)
335 ;; - log-view-mode ()
337 ;; Mode to use for the output of print-log. This defaults to
338 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
339 ;; mode of `log-view-mode'.
341 ;; - show-log-entry (revision)
343 ;; If provided, search the log entry for REVISION in the current buffer,
344 ;; and make sure it is displayed in the buffer's window. The default
345 ;; implementation of this function works for RCS-style logs.
347 ;; - wash-log (file)
349 ;; Remove all non-comment information from the output of print-log.
351 ;; - logentry-check ()
353 ;; If defined, this function is run to find out whether the user
354 ;; entered a valid log entry for check-in. The log entry is in the
355 ;; current buffer, and if it is not a valid one, the function should
356 ;; throw an error.
358 ;; - comment-history (file)
360 ;; Return a string containing all log entries that were made for FILE.
361 ;; This is used for transferring a file from one backend to another,
362 ;; retaining comment information. The default implementation of this
363 ;; function does this by calling print-log and then wash-log, and
364 ;; returning the resulting buffer contents as a string.
366 ;; - update-changelog (files)
368 ;; Using recent log entries, create ChangeLog entries for FILES, or for
369 ;; all files at or below the default-directory if FILES is nil. The
370 ;; default implementation runs rcs2log, which handles RCS- and
371 ;; CVS-style logs.
373 ;; * diff (files &optional rev1 rev2 buffer)
375 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
376 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
377 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
378 ;; found in the repository) as the older revision; if REV2 is nil,
379 ;; use the current working-copy contents as the newer revision. This
380 ;; function should pass the value of (vc-switches BACKEND 'diff) to
381 ;; the backend command. It should return a status of either 0 (no
382 ;; differences found), or 1 (either non-empty diff or the diff is
383 ;; run asynchronously).
385 ;; - revision-completion-table (files)
387 ;; Return a completion table for existing revisions of FILES.
388 ;; The default is to not use any completion table.
390 ;; - annotate-command (file buf &optional rev)
392 ;; If this function is provided, it should produce an annotated display
393 ;; of FILE in BUF, relative to revision REV. Annotation means each line
394 ;; of FILE displayed is prefixed with version information associated with
395 ;; its addition (deleted lines leave no history) and that the text of the
396 ;; file is fontified according to age.
398 ;; - annotate-time ()
400 ;; Only required if `annotate-command' is defined for the backend.
401 ;; Return the time of the next line of annotation at or after point,
402 ;; as a floating point fractional number of days. The helper
403 ;; function `vc-annotate-convert-time' may be useful for converting
404 ;; multi-part times as returned by `current-time' and `encode-time'
405 ;; to this format. Return nil if no more lines of annotation appear
406 ;; in the buffer. You can safely assume that point is placed at the
407 ;; beginning of each line, starting at `point-min'. The buffer that
408 ;; point is placed in is the Annotate output, as defined by the
409 ;; relevant backend. This function also affects how much of the line
410 ;; is fontified; where it leaves point is where fontification begins.
412 ;; - annotate-current-time ()
414 ;; Only required if `annotate-command' is defined for the backend,
415 ;; AND you'd like the current time considered to be anything besides
416 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
417 ;; time with hours, minutes, and seconds included. Probably safe to
418 ;; ignore. Return the current-time, in units of fractional days.
420 ;; - annotate-extract-revision-at-line ()
422 ;; Only required if `annotate-command' is defined for the backend.
423 ;; Invoked from a buffer in vc-annotate-mode, return the revision
424 ;; corresponding to the current line, or nil if there is no revision
425 ;; corresponding to the current line.
427 ;; SNAPSHOT SYSTEM
429 ;; - create-snapshot (dir name branchp)
431 ;; Take a snapshot of the current state of files under DIR and name it
432 ;; NAME. This should make sure that files are up-to-date before
433 ;; proceeding with the action. DIR can also be a file and if BRANCHP
434 ;; is specified, NAME should be created as a branch and DIR should be
435 ;; checked out under this new branch. The default implementation does
436 ;; not support branches but does a sanity check, a tree traversal and
437 ;; for each file calls `assign-name'.
439 ;; - assign-name (file name)
441 ;; Give name NAME to the working revision of FILE, assuming it is
442 ;; up-to-date. Only used by the default version of `create-snapshot'.
444 ;; - retrieve-snapshot (dir name update)
446 ;; Retrieve a named snapshot of all registered files at or below DIR.
447 ;; If UPDATE is non-nil, then update buffers of any files in the
448 ;; snapshot that are currently visited. The default implementation
449 ;; does a sanity check whether there aren't any uncommitted changes at
450 ;; or below DIR, and then performs a tree walk, using the `checkout'
451 ;; function to retrieve the corresponding revisions.
453 ;; MISCELLANEOUS
455 ;; - make-version-backups-p (file)
457 ;; Return non-nil if unmodified repository revisions of FILE should be
458 ;; backed up locally. If this is done, VC can perform `diff' and
459 ;; `revert' operations itself, without calling the backend system. The
460 ;; default implementation always returns nil.
462 ;; - repository-hostname (dirname)
464 ;; Return the hostname that the backend will have to contact
465 ;; in order to operate on a file in DIRNAME. If the return value
466 ;; is nil, it means that the repository is local.
467 ;; This function is used in `vc-stay-local-p' which backends can use
468 ;; for their convenience.
470 ;; - previous-revision (file rev)
472 ;; Return the revision number that precedes REV for FILE, or nil if no such
473 ;; revision exists.
475 ;; - next-revision (file rev)
477 ;; Return the revision number that follows REV for FILE, or nil if no such
478 ;; revision exists.
480 ;; - check-headers ()
482 ;; Return non-nil if the current buffer contains any version headers.
484 ;; - clear-headers ()
486 ;; In the current buffer, reset all version headers to their unexpanded
487 ;; form. This function should be provided if the state-querying code
488 ;; for this backend uses the version headers to determine the state of
489 ;; a file. This function will then be called whenever VC changes the
490 ;; version control state in such a way that the headers would give
491 ;; wrong information.
493 ;; - delete-file (file)
495 ;; Delete FILE and mark it as deleted in the repository. If this
496 ;; function is not provided, the command `vc-delete-file' will
497 ;; signal an error.
499 ;; - rename-file (old new)
501 ;; Rename file OLD to NEW, both in the working area and in the
502 ;; repository. If this function is not provided, the renaming
503 ;; will be done by (vc-delete-file old) and (vc-register new).
505 ;; - find-file-hook ()
507 ;; Operation called in current buffer when opening a file. This can
508 ;; be used by the backend to setup some local variables it might need.
510 ;; - find-file-not-found-hook ()
512 ;; Operation called in current buffer when opening a non-existing file.
513 ;; By default, this asks the user if she wants to check out the file.
515 ;; - extra-menu ()
517 ;; Return a menu keymap, the items in the keymap will appear at the
518 ;; end of the Version Control menu. The goal is to allow backends
519 ;; to specify extra menu items that appear in the VC menu. This way
520 ;; you can provide menu entries for functionality that is specific
521 ;; to your backend and which does not map to any of the VC generic
522 ;; concepts.
524 ;;; Todo:
526 ;; - Make vc-checkin avoid reverting the buffer if has not changed
527 ;; after the checkin. Comparing (md5 BUFFER) to (md5 FILE) should
528 ;; be enough.
530 ;; - vc-update/vc-merge should deal with VC systems that don't
531 ;; update/merge on a file basis, but on a whole repository basis.
533 ;; - the backend sometimes knows when a file it opens has been marked
534 ;; by the VCS as having a "conflict". Find a way to pass this info -
535 ;; to VC so that it can turn on smerge-mode when opening such a
536 ;; file.
538 ;; - the *VC-log* buffer needs font-locking.
540 ;; - make it easier to write logs, maybe C-x 4 a should add to the log
541 ;; buffer if there's one instead of the ChangeLog.
543 ;; - make vc-state for all backends return 'unregistered instead of
544 ;; nil for unregistered files, then update vc-next-action.
546 ;; - add a generic mechanism for remembering the current branch names,
547 ;; display the branch name in the mode-line. Replace
548 ;; vc-cvs-sticky-tag with that.
550 ;; - vc-register should register a fileset at a time. The backends
551 ;; already support this, only the front-end needs to be change to
552 ;; handle multiple files at a time.
554 ;; - add a mechanism to for ignoring files.
556 ;; - deal with push/pull operations.
558 ;; - decide if vc-status should replace vc-dired.
560 ;; - vc-status needs a menu, mouse bindings and some color bling.
562 ;; - vc-status needs to show missing files. It probably needs to have
563 ;; another state for those files. The user might want to restore
564 ;; them, or remove them from the VCS. C-x v v might also need
565 ;; adjustments.
567 ;; - "snapshots" should be renamed to "branches", and thoroughly reworked.
570 ;;; Code:
572 (require 'vc-hooks)
573 (require 'ring)
574 (eval-when-compile
575 (require 'cl)
576 (require 'compile)
577 (require 'dired) ; for dired-map-over-marks macro
578 (require 'dired-aux)) ; for dired-kill-{line,tree}
580 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
581 (setq minor-mode-alist
582 (cons '(vc-parent-buffer vc-parent-buffer-name)
583 minor-mode-alist)))
585 ;; General customization
587 (defgroup vc nil
588 "Version-control system in Emacs."
589 :group 'tools)
591 (defcustom vc-suppress-confirm nil
592 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
593 :type 'boolean
594 :group 'vc)
596 (defcustom vc-delete-logbuf-window t
597 "If non-nil, delete the *VC-log* buffer and window after each logical action.
598 If nil, bury that buffer instead.
599 This is most useful if you have multiple windows on a frame and would like to
600 preserve the setting."
601 :type 'boolean
602 :group 'vc)
604 (defcustom vc-initial-comment nil
605 "If non-nil, prompt for initial comment when a file is registered."
606 :type 'boolean
607 :group 'vc)
609 (defcustom vc-default-init-revision "1.1"
610 "A string used as the default revision number when a new file is registered.
611 This can be overridden by giving a prefix argument to \\[vc-register]. This
612 can also be overridden by a particular VC backend."
613 :type 'string
614 :group 'vc
615 :version "20.3")
617 (defcustom vc-command-messages nil
618 "If non-nil, display run messages from back-end commands."
619 :type 'boolean
620 :group 'vc)
622 (defcustom vc-checkin-switches nil
623 "A string or list of strings specifying extra switches for checkin.
624 These are passed to the checkin program by \\[vc-checkin]."
625 :type '(choice (const :tag "None" nil)
626 (string :tag "Argument String")
627 (repeat :tag "Argument List"
628 :value ("")
629 string))
630 :group 'vc)
632 (defcustom vc-checkout-switches nil
633 "A string or list of strings specifying extra switches for checkout.
634 These are passed to the checkout program by \\[vc-checkout]."
635 :type '(choice (const :tag "None" nil)
636 (string :tag "Argument String")
637 (repeat :tag "Argument List"
638 :value ("")
639 string))
640 :group 'vc)
642 (defcustom vc-register-switches nil
643 "A string or list of strings; extra switches for registering a file.
644 These are passed to the checkin program by \\[vc-register]."
645 :type '(choice (const :tag "None" nil)
646 (string :tag "Argument String")
647 (repeat :tag "Argument List"
648 :value ("")
649 string))
650 :group 'vc)
652 (defcustom vc-dired-listing-switches "-al"
653 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
654 :type 'string
655 :group 'vc
656 :version "21.1")
658 (defcustom vc-dired-recurse t
659 "If non-nil, show directory trees recursively in VC Dired."
660 :type 'boolean
661 :group 'vc
662 :version "20.3")
664 (defcustom vc-dired-terse-display t
665 "If non-nil, show only locked or locally modified files in VC Dired."
666 :type 'boolean
667 :group 'vc
668 :version "20.3")
670 (defcustom vc-diff-switches nil
671 "A string or list of strings specifying switches for diff under VC.
672 When running diff under a given BACKEND, VC concatenates the values of
673 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
674 get the switches for that command. Thus, `vc-diff-switches' should
675 contain switches that are specific to version control, but not
676 specific to any particular backend."
677 :type '(choice (const :tag "None" nil)
678 (string :tag "Argument String")
679 (repeat :tag "Argument List"
680 :value ("")
681 string))
682 :group 'vc
683 :version "21.1")
685 (defcustom vc-diff-knows-L nil
686 "*Indicates whether diff understands the -L option.
687 The value is either `yes', `no', or nil. If it is nil, VC tries
688 to use -L and sets this variable to remember whether it worked."
689 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
690 :group 'vc)
692 (defcustom vc-allow-async-revert nil
693 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
694 Enabling this option means that you can confirm a revert operation even
695 if the local changes in the file have not been found and displayed yet."
696 :type '(choice (const :tag "No" nil)
697 (const :tag "Yes" t))
698 :group 'vc
699 :version "22.1")
701 ;;;###autoload
702 (defcustom vc-checkout-hook nil
703 "Normal hook (list of functions) run after checking out a file.
704 See `run-hooks'."
705 :type 'hook
706 :group 'vc
707 :version "21.1")
709 (defcustom vc-annotate-display-mode 'fullscale
710 "Which mode to color the output of \\[vc-annotate] with by default."
711 :type '(choice (const :tag "By Color Map Range" nil)
712 (const :tag "Scale to Oldest" scale)
713 (const :tag "Scale Oldest->Newest" fullscale)
714 (number :tag "Specify Fractional Number of Days"
715 :value "20.5"))
716 :group 'vc)
718 ;;;###autoload
719 (defcustom vc-checkin-hook nil
720 "Normal hook (list of functions) run after commit or file checkin.
721 See also `log-edit-done-hook'."
722 :type 'hook
723 :options '(log-edit-comment-to-change-log)
724 :group 'vc)
726 ;;;###autoload
727 (defcustom vc-before-checkin-hook nil
728 "Normal hook (list of functions) run before a commit or a file checkin.
729 See `run-hooks'."
730 :type 'hook
731 :group 'vc)
733 (defcustom vc-logentry-check-hook nil
734 "Normal hook run by `vc-backend-logentry-check'.
735 Use this to impose your own rules on the entry in addition to any the
736 version control backend imposes itself."
737 :type 'hook
738 :group 'vc)
740 ;; Annotate customization
741 (defcustom vc-annotate-color-map
742 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
743 ;; A custom sorted TTY colormap
744 (let* ((colors
745 (sort
746 (delq nil
747 (mapcar (lambda (x)
748 (if (not (or
749 (string-equal (car x) "white")
750 (string-equal (car x) "black") ))
751 (car x)))
752 (tty-color-alist)))
753 (lambda (a b)
754 (cond
755 ((or (string-equal a "red") (string-equal b "blue")) t)
756 ((or (string-equal b "red") (string-equal a "blue")) nil)
757 ((string-equal a "yellow") t)
758 ((string-equal b "yellow") nil)
759 ((string-equal a "cyan") t)
760 ((string-equal b "cyan") nil)
761 ((string-equal a "green") t)
762 ((string-equal b "green") nil)
763 ((string-equal a "magenta") t)
764 ((string-equal b "magenta") nil)
765 (t (string< a b))))))
766 (date 20.)
767 (delta (/ (- 360. date) (1- (length colors)))))
768 (mapcar (lambda (x)
769 (prog1
770 (cons date x)
771 (setq date (+ date delta)))) colors))
772 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
773 '(( 20. . "#FF3F3F")
774 ( 40. . "#FF6C3F")
775 ( 60. . "#FF993F")
776 ( 80. . "#FFC63F")
777 (100. . "#FFF33F")
778 (120. . "#DDFF3F")
779 (140. . "#B0FF3F")
780 (160. . "#83FF3F")
781 (180. . "#56FF3F")
782 (200. . "#3FFF56")
783 (220. . "#3FFF83")
784 (240. . "#3FFFB0")
785 (260. . "#3FFFDD")
786 (280. . "#3FF3FF")
787 (300. . "#3FC6FF")
788 (320. . "#3F99FF")
789 (340. . "#3F6CFF")
790 (360. . "#3F3FFF")))
791 "Association list of age versus color, for \\[vc-annotate].
792 Ages are given in units of fractional days. Default is eighteen
793 steps using a twenty day increment, from red to blue. For TTY
794 displays with 8 or fewer colors, the default is red to blue with
795 all other colors between (excluding black and white)."
796 :type 'alist
797 :group 'vc)
799 (defcustom vc-annotate-very-old-color "#3F3FFF"
800 "Color for lines older than the current color range in \\[vc-annotate]]."
801 :type 'string
802 :group 'vc)
804 (defcustom vc-annotate-background "black"
805 "Background color for \\[vc-annotate].
806 Default color is used if nil."
807 :type 'string
808 :group 'vc)
810 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
811 "Menu elements for the mode-specific menu of VC-Annotate mode.
812 List of factors, used to expand/compress the time scale. See `vc-annotate'."
813 :type '(repeat number)
814 :group 'vc)
816 (defvar vc-annotate-mode-map
817 (let ((m (make-sparse-keymap)))
818 (define-key m "A" 'vc-annotate-revision-previous-to-line)
819 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
820 (define-key m "J" 'vc-annotate-revision-at-line)
821 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
822 (define-key m "N" 'vc-annotate-next-revision)
823 (define-key m "P" 'vc-annotate-prev-revision)
824 (define-key m "W" 'vc-annotate-working-revision)
825 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
827 "Local keymap used for VC-Annotate mode.")
829 ;; Header-insertion hair
831 (defcustom vc-static-header-alist
832 '(("\\.c\\'" .
833 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
834 "*Associate static header string templates with file types.
835 A \%s in the template is replaced with the first string associated with
836 the file's version control type in `vc-header-alist'."
837 :type '(repeat (cons :format "%v"
838 (regexp :tag "File Type")
839 (string :tag "Header String")))
840 :group 'vc)
842 (defcustom vc-comment-alist
843 '((nroff-mode ".\\\"" ""))
844 "*Special comment delimiters for generating VC headers.
845 Add an entry in this list if you need to override the normal `comment-start'
846 and `comment-end' variables. This will only be necessary if the mode language
847 is sensitive to blank lines."
848 :type '(repeat (list :format "%v"
849 (symbol :tag "Mode")
850 (string :tag "Comment Start")
851 (string :tag "Comment End")))
852 :group 'vc)
854 (defcustom vc-checkout-carefully (= (user-uid) 0)
855 "*Non-nil means be extra-careful in checkout.
856 Verify that the file really is not locked
857 and that its contents match what the master file says."
858 :type 'boolean
859 :group 'vc)
860 (make-obsolete-variable 'vc-checkout-carefully
861 "the corresponding checks are always done now."
862 "21.1")
865 ;; Variables the user doesn't need to know about.
866 (defvar vc-log-operation nil)
867 (defvar vc-log-after-operation-hook nil)
869 ;; In a log entry buffer, this is a local variable
870 ;; that points to the buffer for which it was made
871 ;; (either a file, or a VC dired buffer).
872 (defvar vc-parent-buffer nil)
873 (put 'vc-parent-buffer 'permanent-local t)
874 (defvar vc-parent-buffer-name nil)
875 (put 'vc-parent-buffer-name 'permanent-local t)
877 (defvar vc-disable-async-diff nil
878 "VC sets this to t locally to disable some async diff operations.
879 Backends that offer asynchronous diffs should respect this variable
880 in their implementation of vc-BACKEND-diff.")
882 (defvar vc-log-fileset)
883 (defvar vc-log-revision)
885 (defvar vc-dired-mode nil)
886 (make-variable-buffer-local 'vc-dired-mode)
888 ;; File property caching
890 (defun vc-clear-context ()
891 "Clear all cached file properties."
892 (interactive)
893 (fillarray vc-file-prop-obarray 0))
895 (defmacro with-vc-properties (files form settings)
896 "Execute FORM, then maybe set per-file properties for FILES.
897 SETTINGS is an association list of property/value pairs. After
898 executing FORM, set those properties from SETTINGS that have not yet
899 been updated to their corresponding values."
900 (declare (debug t))
901 `(let ((vc-touched-properties (list t)))
902 ,form
903 (dolist (file ,files)
904 (dolist (setting ,settings)
905 (let ((property (car setting)))
906 (unless (memq property vc-touched-properties)
907 (put (intern file vc-file-prop-obarray)
908 property (cdr setting))))))))
910 ;; Two macros for elisp programming
912 ;;;###autoload
913 (defmacro with-vc-file (file comment &rest body)
914 "Check out a writable copy of FILE if necessary, then execute BODY.
915 Check in FILE with COMMENT (a string) after BODY has been executed.
916 FILE is passed through `expand-file-name'; BODY executed within
917 `save-excursion'. If FILE is not under version control, or you are
918 using a locking version-control system and the file is locked by
919 somebody else, signal error."
920 (declare (debug t) (indent 2))
921 (let ((filevar (make-symbol "file")))
922 `(let ((,filevar (expand-file-name ,file)))
923 (or (vc-backend ,filevar)
924 (error "File not under version control: `%s'" file))
925 (unless (vc-editable-p ,filevar)
926 (let ((state (vc-state ,filevar)))
927 (if (stringp state)
928 (error "`%s' is locking `%s'" state ,filevar)
929 (vc-checkout ,filevar t))))
930 (save-excursion
931 ,@body)
932 (vc-checkin (list ,filevar) nil ,comment))))
934 ;;;###autoload
935 (defmacro edit-vc-file (file comment &rest body)
936 "Edit FILE under version control, executing body.
937 Checkin with COMMENT after executing BODY.
938 This macro uses `with-vc-file', passing args to it.
939 However, before executing BODY, find FILE, and after BODY, save buffer."
940 (declare (debug t) (indent 2))
941 (let ((filevar (make-symbol "file")))
942 `(let ((,filevar (expand-file-name ,file)))
943 (with-vc-file
944 ,filevar ,comment
945 (set-buffer (find-file-noselect ,filevar))
946 ,@body
947 (save-buffer)))))
949 ;; Common command execution logic to be used by backends
951 (defun vc-process-filter (p s)
952 "An alternative output filter for async process P.
953 One difference with the default filter is that this inserts S after markers.
954 Another is that undo information is not kept."
955 (let ((buffer (process-buffer p)))
956 (when (buffer-live-p buffer)
957 (with-current-buffer buffer
958 (save-excursion
959 (let ((buffer-undo-list t)
960 (inhibit-read-only t))
961 (goto-char (process-mark p))
962 (insert s)
963 (set-marker (process-mark p) (point))))))))
965 (defun vc-setup-buffer (&optional buf)
966 "Prepare BUF for executing a VC command and make it current.
967 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
968 (unless buf (setq buf "*vc*"))
969 (let ((camefrom (current-buffer))
970 (olddir default-directory))
971 (set-buffer (get-buffer-create buf))
972 (kill-all-local-variables)
973 (set (make-local-variable 'vc-parent-buffer) camefrom)
974 (set (make-local-variable 'vc-parent-buffer-name)
975 (concat " from " (buffer-name camefrom)))
976 (setq default-directory olddir)
977 (let ((buffer-undo-list t)
978 (inhibit-read-only t))
979 (erase-buffer))))
981 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
983 (defun vc-process-sentinel (p s)
984 (let ((previous (process-get p 'vc-previous-sentinel))
985 (buf (process-buffer p)))
986 ;; Impatient users sometime kill "slow" buffers; check liveness
987 ;; to avoid "error in process sentinel: Selecting deleted buffer".
988 (when (buffer-live-p buf)
989 (if previous (funcall previous p s))
990 (with-current-buffer buf
991 (setq mode-line-process
992 (let ((status (process-status p)))
993 ;; Leave mode-line uncluttered, normally.
994 ;; (Let known any weirdness in-form-ally. ;-) --ttn
995 (unless (eq 'exit status)
996 (format " (%s)" status))))
997 (let (vc-sentinel-movepoint)
998 ;; Normally, we want async code such as sentinels to not move point.
999 (save-excursion
1000 (goto-char (process-mark p))
1001 (let ((cmds (process-get p 'vc-sentinel-commands)))
1002 (process-put p 'vc-sentinel-commands nil)
1003 (dolist (cmd cmds)
1004 ;; Each sentinel may move point and the next one should be run
1005 ;; at that new point. We could get the same result by having
1006 ;; each sentinel read&set process-mark, but since `cmd' needs
1007 ;; to work both for async and sync processes, this would be
1008 ;; difficult to achieve.
1009 (vc-exec-after cmd))))
1010 ;; But sometimes the sentinels really want to move point.
1011 (if vc-sentinel-movepoint
1012 (let ((win (get-buffer-window (current-buffer) 0)))
1013 (if (not win)
1014 (goto-char vc-sentinel-movepoint)
1015 (with-selected-window win
1016 (goto-char vc-sentinel-movepoint))))))))))
1018 (defun vc-exec-after (code)
1019 "Eval CODE when the current buffer's process is done.
1020 If the current buffer has no process, just evaluate CODE.
1021 Else, add CODE to the process' sentinel."
1022 (let ((proc (get-buffer-process (current-buffer))))
1023 (cond
1024 ;; If there's no background process, just execute the code.
1025 ;; We used to explicitly call delete-process on exited processes,
1026 ;; but this led to timing problems causing process output to be
1027 ;; lost. Terminated processes get deleted automatically
1028 ;; anyway. -- cyd
1029 ((or (null proc) (eq (process-status proc) 'exit))
1030 ;; Make sure we've read the process's output before going further.
1031 (if proc (accept-process-output proc))
1032 (eval code))
1033 ;; If a process is running, add CODE to the sentinel
1034 ((eq (process-status proc) 'run)
1035 (setq mode-line-process
1036 ;; Deliberate overstatement, but power law respected.
1037 ;; (The message is ephemeral, so we make it loud.) --ttn
1038 (propertize " (incomplete/in progress)"
1039 'face 'compilation-warning
1040 'help-echo
1041 "A VC command is in progress in this buffer"))
1042 (let ((previous (process-sentinel proc)))
1043 (unless (eq previous 'vc-process-sentinel)
1044 (process-put proc 'vc-previous-sentinel previous))
1045 (set-process-sentinel proc 'vc-process-sentinel))
1046 (process-put proc 'vc-sentinel-commands
1047 (cons code (process-get proc 'vc-sentinel-commands))))
1048 (t (error "Unexpected process state"))))
1049 nil)
1051 (defvar vc-post-command-functions nil
1052 "Hook run at the end of `vc-do-command'.
1053 Each function is called inside the buffer in which the command was run
1054 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
1056 (defvar w32-quote-process-args)
1058 (defun vc-delistify (filelist)
1059 "Smash a FILELIST into a file list string suitable for info messages."
1060 ;; FIXME what about file names with spaces?
1061 (if (not filelist) "." (mapconcat 'identity filelist " ")))
1063 ;;;###autoload
1064 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
1065 "Execute a VC command, notifying user and checking for errors.
1066 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
1067 current buffer if BUFFER is t. If the destination buffer is not
1068 already current, set it up properly and erase it. The command is
1069 considered successful if its exit status does not exceed OKSTATUS (if
1070 OKSTATUS is nil, that means to ignore error status, if it is `async', that
1071 means not to wait for termination of the subprocess; if it is t it means to
1072 ignore all execution errors). FILE-OR-LIST is the name of a working file;
1073 it may be a list of files or be nil (to execute commands that don't expect
1074 a file name or set of files). If an optional list of FLAGS is present,
1075 that is inserted into the command line before the filename."
1076 ;; FIXME: file-relative-name can return a bogus result because
1077 ;; it doesn't look at the actual file-system to see if symlinks
1078 ;; come into play.
1079 (let* ((files
1080 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
1081 (if (listp file-or-list) file-or-list (list file-or-list))))
1082 (full-command
1083 ;; What we're doing here is preparing a version of the command
1084 ;; for display in a debug-progess message. If it's fewer than
1085 ;; 20 characters display the entire command (without trailing
1086 ;; newline). Otherwise display the first 20 followed by an ellipsis.
1087 (concat (if (string= (substring command -1) "\n")
1088 (substring command 0 -1)
1089 command)
1091 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
1092 " " (vc-delistify files))))
1093 (save-current-buffer
1094 (unless (or (eq buffer t)
1095 (and (stringp buffer)
1096 (string= (buffer-name) buffer))
1097 (eq buffer (current-buffer)))
1098 (vc-setup-buffer buffer))
1099 ;; If there's some previous async process still running, just kill it.
1100 (let ((oldproc (get-buffer-process (current-buffer))))
1101 ;; If we wanted to wait for oldproc to finish before doing
1102 ;; something, we'd have used vc-eval-after.
1103 ;; Use `delete-process' rather than `kill-process' because we don't
1104 ;; want any of its output to appear from now on.
1105 (if oldproc (delete-process oldproc)))
1106 (let ((squeezed (remq nil flags))
1107 (inhibit-read-only t)
1108 (status 0))
1109 (when files
1110 (setq squeezed (nconc squeezed files)))
1111 (let ((exec-path (append vc-path exec-path))
1112 ;; Add vc-path to PATH for the execution of this command.
1113 (process-environment
1114 (cons (concat "PATH=" (getenv "PATH")
1115 path-separator
1116 (mapconcat 'identity vc-path path-separator))
1117 process-environment))
1118 (w32-quote-process-args t))
1119 (if (and (eq okstatus 'async) (file-remote-p default-directory))
1120 ;; start-process does not support remote execution
1121 (setq okstatus nil))
1122 (if (eq okstatus 'async)
1123 ;; Run asynchronously.
1124 (let ((proc
1125 (let ((process-connection-type nil))
1126 (apply 'start-file-process command (current-buffer)
1127 command squeezed))))
1128 (if vc-command-messages
1129 (message "Running %s in background..." full-command))
1130 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
1131 (set-process-filter proc 'vc-process-filter)
1132 (vc-exec-after
1133 `(if vc-command-messages
1134 (message "Running %s in background... done" ',full-command))))
1135 ;; Run synchrously
1136 (if vc-command-messages
1137 (message "Running %s in foreground..." full-command))
1138 (let ((buffer-undo-list t))
1139 (setq status (apply 'process-file command nil t nil squeezed)))
1140 (when (and (not (eq t okstatus))
1141 (or (not (integerp status))
1142 (and okstatus (< okstatus status))))
1143 (pop-to-buffer (current-buffer))
1144 (goto-char (point-min))
1145 (shrink-window-if-larger-than-buffer)
1146 (error "Running %s...FAILED (%s)" full-command
1147 (if (integerp status) (format "status %d" status) status))))
1148 ;; We're done. But don't emit a status message if running
1149 ;; asychronously, it would just mislead.
1150 (if (and vc-command-messages (not (eq okstatus 'async)))
1151 (message "Running %s...OK = %d" full-command status)))
1152 (vc-exec-after
1153 `(run-hook-with-args 'vc-post-command-functions
1154 ',command ',file-or-list ',flags))
1155 status))))
1157 (defun vc-position-context (posn)
1158 "Save a bit of the text around POSN in the current buffer.
1159 Used to help us find the corresponding position again later
1160 if markers are destroyed or corrupted."
1161 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
1162 ;; rcs.el mode.
1163 (list posn
1164 (buffer-size)
1165 (buffer-substring posn
1166 (min (point-max) (+ posn 100)))))
1168 (defun vc-find-position-by-context (context)
1169 "Return the position of CONTEXT in the current buffer.
1170 If CONTEXT cannot be found, return nil."
1171 (let ((context-string (nth 2 context)))
1172 (if (equal "" context-string)
1173 (point-max)
1174 (save-excursion
1175 (let ((diff (- (nth 1 context) (buffer-size))))
1176 (if (< diff 0) (setq diff (- diff)))
1177 (goto-char (nth 0 context))
1178 (if (or (search-forward context-string nil t)
1179 ;; Can't use search-backward since the match may continue
1180 ;; after point.
1181 (progn (goto-char (- (point) diff (length context-string)))
1182 ;; goto-char doesn't signal an error at
1183 ;; beginning of buffer like backward-char would
1184 (search-forward context-string nil t)))
1185 ;; to beginning of OSTRING
1186 (- (point) (length context-string))))))))
1188 (defun vc-context-matches-p (posn context)
1189 "Return t if POSN matches CONTEXT, nil otherwise."
1190 (let* ((context-string (nth 2 context))
1191 (len (length context-string))
1192 (end (+ posn len)))
1193 (if (> end (1+ (buffer-size)))
1195 (string= context-string (buffer-substring posn end)))))
1197 (defun vc-buffer-context ()
1198 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1199 Used by `vc-restore-buffer-context' to later restore the context."
1200 (let ((point-context (vc-position-context (point)))
1201 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1202 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1203 (vc-position-context (mark-marker))))
1204 ;; Make the right thing happen in transient-mark-mode.
1205 (mark-active nil)
1206 ;; The new compilation code does not use compilation-error-list any
1207 ;; more, so the code below is now ineffective and might as well
1208 ;; be disabled. -- Stef
1209 ;; ;; We may want to reparse the compilation buffer after revert
1210 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
1211 ;; ;; Construct a list; each elt is nil or a buffer
1212 ;; ;; if that buffer is a compilation output buffer
1213 ;; ;; that contains markers into the current buffer.
1214 ;; (save-current-buffer
1215 ;; (mapcar (lambda (buffer)
1216 ;; (set-buffer buffer)
1217 ;; (let ((errors (or
1218 ;; compilation-old-error-list
1219 ;; compilation-error-list))
1220 ;; (buffer-error-marked-p nil))
1221 ;; (while (and (consp errors)
1222 ;; (not buffer-error-marked-p))
1223 ;; (and (markerp (cdr (car errors)))
1224 ;; (eq buffer
1225 ;; (marker-buffer
1226 ;; (cdr (car errors))))
1227 ;; (setq buffer-error-marked-p t))
1228 ;; (setq errors (cdr errors)))
1229 ;; (if buffer-error-marked-p buffer)))
1230 ;; (buffer-list)))))
1231 (reparse nil))
1232 (list point-context mark-context reparse)))
1234 (defun vc-restore-buffer-context (context)
1235 "Restore point/mark, and reparse any affected compilation buffers.
1236 CONTEXT is that which `vc-buffer-context' returns."
1237 (let ((point-context (nth 0 context))
1238 (mark-context (nth 1 context))
1239 ;; (reparse (nth 2 context))
1241 ;; The new compilation code does not use compilation-error-list any
1242 ;; more, so the code below is now ineffective and might as well
1243 ;; be disabled. -- Stef
1244 ;; ;; Reparse affected compilation buffers.
1245 ;; (while reparse
1246 ;; (if (car reparse)
1247 ;; (with-current-buffer (car reparse)
1248 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
1249 ;; ;; Record the position in the compilation buffer of
1250 ;; ;; the last error next-error went to.
1251 ;; (error-pos (marker-position
1252 ;; (car (car-safe compilation-error-list)))))
1253 ;; ;; Reparse the error messages as far as they were parsed before.
1254 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
1255 ;; ;; Move the pointer up to find the error we were at before
1256 ;; ;; reparsing. Now next-error should properly go to the next one.
1257 ;; (while (and compilation-error-list
1258 ;; (/= error-pos (car (car compilation-error-list))))
1259 ;; (setq compilation-error-list (cdr compilation-error-list))))))
1260 ;; (setq reparse (cdr reparse)))
1262 ;; if necessary, restore point and mark
1263 (if (not (vc-context-matches-p (point) point-context))
1264 (let ((new-point (vc-find-position-by-context point-context)))
1265 (if new-point (goto-char new-point))))
1266 (and mark-active
1267 mark-context
1268 (not (vc-context-matches-p (mark) mark-context))
1269 (let ((new-mark (vc-find-position-by-context mark-context)))
1270 (if new-mark (set-mark new-mark))))))
1272 ;;; Code for deducing what fileset and backend to assume
1274 (defun vc-responsible-backend (file &optional register)
1275 "Return the name of a backend system that is responsible for FILE.
1276 The optional argument REGISTER means that a backend suitable for
1277 registration should be found.
1279 If REGISTER is nil, then if FILE is already registered, return the
1280 backend of FILE. If FILE is not registered, or a directory, then the
1281 first backend in `vc-handled-backends' that declares itself
1282 responsible for FILE is returned. If no backend declares itself
1283 responsible, return the first backend.
1285 If REGISTER is non-nil, return the first responsible backend under
1286 which FILE is not yet registered. If there is no such backend, return
1287 the first backend under which FILE is not yet registered, but could
1288 be registered."
1289 (if (not vc-handled-backends)
1290 (error "No handled backends"))
1291 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1292 (catch 'found
1293 ;; First try: find a responsible backend. If this is for registration,
1294 ;; it must be a backend under which FILE is not yet registered.
1295 (dolist (backend vc-handled-backends)
1296 (and (or (not register)
1297 (not (vc-call-backend backend 'registered file)))
1298 (vc-call-backend backend 'responsible-p file)
1299 (throw 'found backend)))
1300 ;; no responsible backend
1301 (if (not register)
1302 ;; if this is not for registration, the first backend must do
1303 (car vc-handled-backends)
1304 ;; for registration, we need to find a new backend that
1305 ;; could register FILE
1306 (dolist (backend vc-handled-backends)
1307 (and (not (vc-call-backend backend 'registered file))
1308 (vc-call-backend backend 'could-register file)
1309 (throw 'found backend)))
1310 (error "No backend that could register")))))
1312 (defun vc-expand-dirs (file-or-dir-list)
1313 "Expands directories in a file list specification.
1314 Only files already under version control are noticed."
1315 ;; FIXME: Kill this function.
1316 (let ((flattened '()))
1317 (dolist (node file-or-dir-list)
1318 (vc-file-tree-walk
1319 node (lambda (f) (if (vc-backend f) (push f flattened)))))
1320 (nreverse flattened)))
1322 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered)
1323 "Deduce a set of files and a backend to which to apply an operation.
1325 If we're in VC-dired mode, the fileset is the list of marked files.
1326 Otherwise, if we're looking at a buffer visiting a version-controlled file,
1327 the fileset is a singleton containing this file.
1328 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1329 and we're in a dired buffer, select the current directory.
1330 If none of these conditions is met, but ALLOW_UNREGISTERED is in and the
1331 visited file is not registered, return a singletin fileset containing it.
1332 Otherwise, throw an error."
1333 (cond (vc-dired-mode
1334 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1335 (unless marked
1336 (error "No files have been selected."))
1337 ;; All members of the fileset must have the same backend
1338 (let ((firstbackend (vc-backend (car marked))))
1339 (dolist (f (cdr marked))
1340 (unless (eq (vc-backend f) firstbackend)
1341 (error "All members of a fileset must be under the same version-control system."))))
1342 marked))
1343 ((eq major-mode 'vc-status-mode)
1344 (let ((marked (vc-status-marked-files)))
1345 (if marked
1346 marked
1347 (list (vc-status-current-file)))))
1348 ((vc-backend buffer-file-name)
1349 (list buffer-file-name))
1350 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1351 (with-current-buffer vc-parent-buffer
1352 vc-dired-mode)))
1353 (progn
1354 (set-buffer vc-parent-buffer)
1355 (vc-deduce-fileset)))
1356 ;; This is guarded by an enabling arg so users won't potentially
1357 ;; shoot themselves in the foot by modifying a fileset they can't
1358 ;; verify by eyeball. Allow it for nondestructive commands like
1359 ;; making diffs, or possibly for destructive ones that have
1360 ;; confirmation prompts.
1361 ((and allow-directory-wildcard
1362 ;; I think this is a misfeature. For now, I'll leave it in, but
1363 ;; I'll disable it anywhere else than in dired buffers. --Stef
1364 (and (derived-mode-p 'dired-mode)
1365 (equal buffer-file-name nil)
1366 (equal list-buffers-directory default-directory)))
1367 (progn
1368 (message "All version-controlled files below %s selected."
1369 default-directory)
1370 (list default-directory)))
1371 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1372 (list buffer-file-name))
1373 (t (error "No fileset is available here."))))
1375 (defun vc-ensure-vc-buffer ()
1376 "Make sure that the current buffer visits a version-controlled file."
1377 (cond
1378 (vc-dired-mode
1379 (set-buffer (find-file-noselect (dired-get-filename))))
1380 ((eq major-mode 'vc-status-mode)
1381 (set-buffer (find-file-noselect (vc-status-current-file))))
1383 (while (and vc-parent-buffer
1384 (buffer-live-p vc-parent-buffer)
1385 ;; Avoid infinite looping when vc-parent-buffer and
1386 ;; current buffer are the same buffer.
1387 (not (eq vc-parent-buffer (current-buffer))))
1388 (set-buffer vc-parent-buffer))
1389 (if (not buffer-file-name)
1390 (error "Buffer %s is not associated with a file" (buffer-name))
1391 (if (not (vc-backend buffer-file-name))
1392 (error "File %s is not under version control" buffer-file-name))))))
1394 ;;; Support for the C-x v v command. This is where all the single-file-oriented
1395 ;;; code from before the fileset rewrite lives.
1397 (defsubst vc-editable-p (file)
1398 "Return non-nil if FILE can be edited."
1399 (or (eq (vc-checkout-model file) 'implicit)
1400 (memq (vc-state file) '(edited needs-merge))))
1402 (defun vc-revert-buffer-internal (&optional arg no-confirm)
1403 "Revert buffer, keeping point and mark where user expects them.
1404 Try to be clever in the face of changes due to expanded version-control
1405 key words. This is important for typeahead to work as expected.
1406 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1407 (interactive "P")
1408 (widen)
1409 (let ((context (vc-buffer-context)))
1410 ;; Use save-excursion here, because it may be able to restore point
1411 ;; and mark properly even in cases where vc-restore-buffer-context
1412 ;; would fail. However, save-excursion might also get it wrong --
1413 ;; in this case, vc-restore-buffer-context gives it a second try.
1414 (save-excursion
1415 ;; t means don't call normal-mode;
1416 ;; that's to preserve various minor modes.
1417 (revert-buffer arg no-confirm t))
1418 (vc-restore-buffer-context context)))
1420 (defun vc-buffer-sync (&optional not-urgent)
1421 "Make sure the current buffer and its working file are in sync.
1422 NOT-URGENT means it is ok to continue if the user says not to save."
1423 (if (buffer-modified-p)
1424 (if (or vc-suppress-confirm
1425 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1426 (save-buffer)
1427 (unless not-urgent
1428 (error "Aborted")))))
1430 (defvar vc-dired-window-configuration)
1432 (defun vc-compatible-state (p q)
1433 "Controls which states can be in the same commit."
1435 (eq p q)
1436 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1438 ;; Here's the major entry point.
1440 ;;;###autoload
1441 (defun vc-next-action (verbose)
1442 "Do the next logical version control operation on the current fileset.
1443 This requires that all files in the fileset be in the same state.
1445 For locking systems:
1446 If every file is not already registered, this registers each for version
1447 control.
1448 If every file is registered and not locked by anyone, this checks out
1449 a writable and locked file of each ready for editing.
1450 If every file is checked out and locked by the calling user, this
1451 first checks to see if each file has changed since checkout. If not,
1452 it performs a revert on that file.
1453 If every file has been changed, this pops up a buffer for entry
1454 of a log message; when the message has been entered, it checks in the
1455 resulting changes along with the log message as change commentary. If
1456 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1457 read-only copy of each changed file is left in place afterwards.
1458 If the affected file is registered and locked by someone else, you are
1459 given the option to steal the lock(s).
1461 For merging systems:
1462 If every file is not already registered, this registers each one for version
1463 control. This does an add, but not a commit.
1464 If every file is added but not committed, each one is committed.
1465 If every working file is changed, but the corresponding repository file is
1466 unchanged, this pops up a buffer for entry of a log message; when the
1467 message has been entered, it checks in the resulting changes along
1468 with the logmessage as change commentary. A writable file is retained.
1469 If the repository file is changed, you are asked if you want to
1470 merge in the changes into your working copy."
1471 (interactive "P")
1472 (let* ((files (vc-deduce-fileset nil t))
1473 (state (vc-state (car files)))
1474 (model (vc-checkout-model (car files)))
1475 revision)
1476 ;; Verify that the fileset is homogenous
1477 (dolist (file (cdr files))
1478 (unless (vc-compatible-state (vc-state file) state)
1479 (error "Fileset is in a mixed-up state"))
1480 (unless (eq (vc-checkout-model file) model)
1481 (error "Fileset has mixed checkout models")))
1482 ;; Check for buffers in the fileset not matching the on-disk contents.
1483 (dolist (file files)
1484 (let ((visited (get-file-buffer file)))
1485 (when visited
1486 (if vc-dired-mode
1487 (switch-to-buffer-other-window visited)
1488 (set-buffer visited))
1489 ;; Check relation of buffer and file, and make sure
1490 ;; user knows what he's doing. First, finding the file
1491 ;; will check whether the file on disk is newer.
1492 ;; Ignore buffer-read-only during this test, and
1493 ;; preserve find-file-literally.
1494 (let ((buffer-read-only (not (file-writable-p file))))
1495 (find-file-noselect file nil find-file-literally))
1496 (if (not (verify-visited-file-modtime (current-buffer)))
1497 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1498 (write-file buffer-file-name)
1499 (error "Aborted"))
1500 ;; Now, check if we have unsaved changes.
1501 (vc-buffer-sync t)
1502 (when (buffer-modified-p)
1503 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1504 (error "Aborted")))))))
1505 ;; Do the right thing
1506 (cond
1507 ;; Files aren't registered
1508 ((or (not state) ;; RCS uses nil for unregistered files.
1509 (eq state 'unregistered)
1510 (eq state 'ignored))
1511 (mapc 'vc-register files))
1512 ;; Files are up-to-date, or need a merge and user specified a revision
1513 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-patch)))
1514 (cond
1515 (verbose
1516 ;; go to a different revision
1517 (setq revision (read-string "Branch, revision, or backend to move to: "))
1518 (let ((vsym (intern-soft (upcase revision))))
1519 (if (member vsym vc-handled-backends)
1520 (dolist (file files) (vc-transfer-file file vsym))
1521 (dolist (file files)
1522 (vc-checkout file (eq model 'implicit) revision)))))
1523 ((not (eq model 'implicit))
1524 ;; check the files out
1525 (dolist (file files) (vc-checkout file t)))
1527 ;; do nothing
1528 (message "Fileset is up-to-date"))))
1529 ;; Files have local changes
1530 ((vc-compatible-state state 'edited)
1531 (let ((ready-for-commit files))
1532 ;; If files are edited but read-only, give user a chance to correct
1533 (dolist (file files)
1534 (unless (file-writable-p file)
1535 ;; Make the file+buffer read-write.
1536 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1537 (error "Aborted"))
1538 (set-file-modes file (logior (file-modes file) 128))
1539 (let ((visited (get-file-buffer file)))
1540 (when visited
1541 (with-current-buffer visited
1542 (toggle-read-only -1))))))
1543 ;; Allow user to revert files with no changes
1544 (save-excursion
1545 (dolist (file files)
1546 (let ((visited (get-file-buffer file)))
1547 ;; For files with locking, if the file does not contain
1548 ;; any changes, just let go of the lock, i.e. revert.
1549 (when (and (not (eq model 'implicit))
1550 (vc-workfile-unchanged-p file)
1551 ;; If buffer is modified, that means the user just
1552 ;; said no to saving it; in that case, don't revert,
1553 ;; because the user might intend to save after
1554 ;; finishing the log entry and committing.
1555 (not (and visited (buffer-modified-p))))
1556 (vc-revert-file file)
1557 (delete file ready-for-commit)))))
1558 ;; Remaining files need to be committed
1559 (if (not ready-for-commit)
1560 (message "No files remain to be committed")
1561 (if (not verbose)
1562 (vc-checkin ready-for-commit)
1563 (progn
1564 (setq revision (read-string "New revision or backend: "))
1565 (let ((vsym (intern (upcase revision))))
1566 (if (member vsym vc-handled-backends)
1567 (dolist (file files) (vc-transfer-file file vsym))
1568 (vc-checkin ready-for-commit revision))))))))
1569 ;; locked by somebody else (locking VCSes only)
1570 ((stringp state)
1571 ;; In the old days, we computed the revision once and used it on
1572 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1573 ;; computed the revision once (incorrectly, using a free var) and
1574 ;; used it on all files. To fix the free var bug, we can either
1575 ;; use `(car files)' or do what we do here: distribute the
1576 ;; revision computation among `files'. Although this may be
1577 ;; tedious for those backends where a "revision" is a trans-file
1578 ;; concept, it is nonetheless correct for both those and (more
1579 ;; importantly) for those where "revision" is a per-file concept.
1580 ;; If the intersection of the former group and "locking VCSes" is
1581 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1582 ;; pre-computation approach of yore.
1583 (dolist (file files)
1584 (vc-steal-lock
1585 file (if verbose
1586 (read-string (format "%s revision to steal: " file))
1587 (vc-working-revision file))
1588 state)))
1589 ;; needs-patch
1590 ((eq state 'needs-patch)
1591 (dolist (file files)
1592 (if (yes-or-no-p (format
1593 "%s is not up-to-date. Get latest revision? "
1594 (file-name-nondirectory file)))
1595 (vc-checkout file (eq model 'implicit) t)
1596 (when (and (not (eq model 'implicit))
1597 (yes-or-no-p "Lock this revision? "))
1598 (vc-checkout file t)))))
1599 ;; needs-merge
1600 ((eq state 'needs-merge)
1601 (dolist (file files)
1602 (when (yes-or-no-p (format
1603 "%s is not up-to-date. Merge in changes now? "
1604 (file-name-nondirectory file)))
1605 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1607 ;; unlocked-changes
1608 ((eq state 'unlocked-changes)
1609 (dolist (file files)
1610 (if (not (equal buffer-file-name file))
1611 (find-file-other-window file))
1612 (if (save-window-excursion
1613 (vc-diff-internal nil (list file) (vc-working-revision file) nil)
1614 (goto-char (point-min))
1615 (let ((inhibit-read-only t))
1616 (insert
1617 (format "Changes to %s since last lock:\n\n" file)))
1618 (not (beep))
1619 (yes-or-no-p (concat "File has unlocked changes. "
1620 "Claim lock retaining changes? ")))
1621 (progn (vc-call steal-lock file)
1622 (clear-visited-file-modtime)
1623 ;; Must clear any headers here because they wouldn't
1624 ;; show that the file is locked now.
1625 (vc-clear-headers file)
1626 (write-file buffer-file-name)
1627 (vc-mode-line file))
1628 (if (not (yes-or-no-p
1629 "Revert to checked-in revision, instead? "))
1630 (error "Checkout aborted")
1631 (vc-revert-buffer-internal t t)
1632 (vc-checkout file t))))))))
1634 (defun vc-create-repo (backend)
1635 "Create an empty repository in the current directory."
1636 (interactive
1637 (list
1638 (intern
1639 (upcase
1640 (completing-read
1641 "Create repository for: "
1642 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1643 nil t)))))
1644 (vc-call-backend backend 'create-repo))
1646 ;;;###autoload
1647 (defun vc-register (&optional fname set-revision comment)
1648 "Register into a version control system.
1649 If FNAME is given register that file, otherwise register the current file.
1650 With prefix argument SET-REVISION, allow user to specify initial revision
1651 level. If COMMENT is present, use that as an initial comment.
1653 The version control system to use is found by cycling through the list
1654 `vc-handled-backends'. The first backend in that list which declares
1655 itself responsible for the file (usually because other files in that
1656 directory are already registered under that backend) will be used to
1657 register the file. If no backend declares itself responsible, the
1658 first backend that could register the file is used."
1659 (interactive "P")
1660 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1662 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1663 (unless fname (setq fname buffer-file-name))
1664 (when (vc-backend fname)
1665 (if (vc-registered fname)
1666 (error "This file is already registered")
1667 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1668 (error "Aborted"))))
1669 ;; Watch out for new buffers of size 0: the corresponding file
1670 ;; does not exist yet, even though buffer-modified-p is nil.
1671 (when bname
1672 (with-current-buffer bname
1673 (if (and (not (buffer-modified-p))
1674 (zerop (buffer-size))
1675 (not (file-exists-p buffer-file-name)))
1676 (set-buffer-modified-p t))
1677 (vc-buffer-sync)))
1678 (vc-start-entry (list fname)
1679 (if set-revision
1680 (read-string (format "Initial revision level for %s: "
1681 fname))
1682 (vc-call-backend (vc-responsible-backend fname)
1683 'init-revision))
1684 (or comment (not vc-initial-comment))
1686 "Enter initial comment."
1687 (lambda (files rev comment)
1688 (dolist (file files)
1689 (message "Registering %s... " file)
1690 (let ((backend (vc-responsible-backend file t)))
1691 (vc-file-clearprops file)
1692 (vc-call-backend backend 'register (list file) rev comment)
1693 (vc-file-setprop file 'vc-backend backend)
1694 (unless vc-make-backup-files
1695 (make-local-variable 'backup-inhibited)
1696 (setq backup-inhibited t)))
1697 (message "Registering %s... done" file))))))
1699 (defun vc-register-with (backend)
1700 "Register the current file with a specified back end."
1701 (interactive "SBackend: ")
1702 (if (not (member backend vc-handled-backends))
1703 (error "Unknown back end."))
1704 (let ((vc-handled-backends (list backend)))
1705 (call-interactively 'vc-register)))
1707 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
1709 (defun vc-resynch-window (file &optional keep noquery)
1710 "If FILE is in the current buffer, either revert or unvisit it.
1711 The choice between revert (to see expanded keywords) and unvisit depends on
1712 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1713 reverting. NOQUERY should be t *only* if it is known the only
1714 difference between the buffer and the file is due to version control
1715 rather than user editing!"
1716 (and (string= buffer-file-name file)
1717 (if keep
1718 (progn
1719 (vc-revert-buffer-internal t noquery)
1720 ;; TODO: Adjusting view mode might no longer be necessary
1721 ;; after RMS change to files.el of 1999-08-08. Investigate
1722 ;; this when we install the new VC.
1723 (and view-read-only
1724 (if (file-writable-p file)
1725 (and view-mode
1726 (let ((view-old-buffer-read-only nil))
1727 (view-mode-exit)))
1728 (and (not view-mode)
1729 (not (eq (get major-mode 'mode-class) 'special))
1730 (view-mode-enter))))
1731 (vc-mode-line buffer-file-name))
1732 (kill-buffer (current-buffer)))))
1734 (defun vc-resynch-buffer (file &optional keep noquery)
1735 "If FILE is currently visited, resynch its buffer."
1736 (if (string= buffer-file-name file)
1737 (vc-resynch-window file keep noquery)
1738 (let ((buffer (get-file-buffer file)))
1739 (if buffer
1740 (with-current-buffer buffer
1741 (vc-resynch-window file keep noquery)))))
1742 (vc-dired-resynch-file file))
1744 (defun vc-start-entry (files rev comment initial-contents msg action &optional after-hook)
1745 "Accept a comment for an operation on FILES revision REV.
1746 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1747 action on close to ACTION. If COMMENT is a string and
1748 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1749 contents of the log entry buffer. If COMMENT is a string and
1750 INITIAL-CONTENTS is nil, do action immediately as if the user had
1751 entered COMMENT. If COMMENT is t, also do action immediately with an
1752 empty comment. Remember the file's buffer in `vc-parent-buffer'
1753 \(current one if no file). AFTER-HOOK specifies the local value
1754 for `vc-log-after-operation-hook'."
1755 (let ((parent
1756 (if (eq major-mode 'vc-dired-mode)
1757 ;; If we are called from VC dired, the parent buffer is
1758 ;; the current buffer.
1759 (current-buffer)
1760 (if (and files (equal (length files) 1))
1761 (get-file-buffer (car files))
1762 (current-buffer)))))
1763 (when vc-before-checkin-hook
1764 (if files
1765 (with-current-buffer parent
1766 (run-hooks 'vc-before-checkin-hook))
1767 (run-hooks 'vc-before-checkin-hook)))
1768 (if (and comment (not initial-contents))
1769 (set-buffer (get-buffer-create "*VC-log*"))
1770 (pop-to-buffer (get-buffer-create "*VC-log*")))
1771 (set (make-local-variable 'vc-parent-buffer) parent)
1772 (set (make-local-variable 'vc-parent-buffer-name)
1773 (concat " from " (buffer-name vc-parent-buffer)))
1774 ;;(if file (vc-mode-line file))
1775 (vc-log-edit files)
1776 (make-local-variable 'vc-log-after-operation-hook)
1777 (when after-hook
1778 (setq vc-log-after-operation-hook after-hook))
1779 (setq vc-log-operation action)
1780 (setq vc-log-revision rev)
1781 (when comment
1782 (erase-buffer)
1783 (when (stringp comment) (insert comment)))
1784 (if (or (not comment) initial-contents)
1785 (message "%s Type C-c C-c when done" msg)
1786 (vc-finish-logentry (eq comment t)))))
1788 (defun vc-checkout (file &optional writable rev)
1789 "Retrieve a copy of the revision REV of FILE.
1790 If WRITABLE is non-nil, make sure the retrieved file is writable.
1791 REV defaults to the latest revision.
1793 After check-out, runs the normal hook `vc-checkout-hook'."
1794 (and writable
1795 (not rev)
1796 (vc-call make-version-backups-p file)
1797 (vc-up-to-date-p file)
1798 (vc-make-version-backup file))
1799 (with-vc-properties
1800 (list file)
1801 (condition-case err
1802 (vc-call checkout file writable rev)
1803 (file-error
1804 ;; Maybe the backend is not installed ;-(
1805 (when writable
1806 (let ((buf (get-file-buffer file)))
1807 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1808 (signal (car err) (cdr err))))
1809 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1810 (not writable))
1811 (if (vc-call latest-on-branch-p file)
1812 'up-to-date
1813 'needs-patch)
1814 'edited))
1815 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1816 (vc-resynch-buffer file t t)
1817 (run-hooks 'vc-checkout-hook))
1819 (defun vc-steal-lock (file rev owner)
1820 "Steal the lock on FILE."
1821 (let (file-description)
1822 (if rev
1823 (setq file-description (format "%s:%s" file rev))
1824 (setq file-description file))
1825 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1826 file-description owner)))
1827 (error "Steal canceled"))
1828 (message "Stealing lock on %s..." file)
1829 (with-vc-properties
1830 (list file)
1831 (vc-call steal-lock file rev)
1832 `((vc-state . edited)))
1833 (vc-resynch-buffer file t t)
1834 (message "Stealing lock on %s...done" file)
1835 ;; Write mail after actually stealing, because if the stealing
1836 ;; goes wrong, we don't want to send any mail.
1837 (compose-mail owner (format "Stolen lock on %s" file-description))
1838 (setq default-directory (expand-file-name "~/"))
1839 (goto-char (point-max))
1840 (insert
1841 (format "I stole the lock on %s, " file-description)
1842 (current-time-string)
1843 ".\n")
1844 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1846 (defun vc-checkin (files &optional rev comment initial-contents)
1847 "Check in FILES.
1848 The optional argument REV may be a string specifying the new revision
1849 level (if nil increment the current level). COMMENT is a comment
1850 string; if omitted, a buffer is popped up to accept a comment. If
1851 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1852 of the log entry buffer.
1854 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1855 that the version control system supports this mode of operation.
1857 Runs the normal hook `vc-checkin-hook'."
1858 (vc-start-entry
1859 files rev comment initial-contents
1860 "Enter a change comment."
1861 (lambda (files rev comment)
1862 (message "Checking in %s..." (vc-delistify files))
1863 ;; "This log message intentionally left almost blank".
1864 ;; RCS 5.7 gripes about white-space-only comments too.
1865 (or (and comment (string-match "[^\t\n ]" comment))
1866 (setq comment "*** empty log message ***"))
1867 (with-vc-properties
1868 files
1869 ;; We used to change buffers to get local value of vc-checkin-switches,
1870 ;; but 'the' local buffer is not a well-defined concept for filesets.
1871 (progn
1872 (vc-call checkin files rev comment)
1873 (mapc 'vc-delete-automatic-version-backups files))
1874 `((vc-state . up-to-date)
1875 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1876 (vc-working-revision . nil)))
1877 (message "Checking in %s...done" (vc-delistify files)))
1878 'vc-checkin-hook))
1880 (defun vc-finish-logentry (&optional nocomment)
1881 "Complete the operation implied by the current log entry.
1882 Use the contents of the current buffer as a check-in or registration
1883 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1884 the buffer contents as a comment."
1885 (interactive)
1886 ;; Check and record the comment, if any.
1887 (unless nocomment
1888 ;; Comment too long?
1889 (vc-call-backend (or (if vc-log-fileset (vc-backend vc-log-fileset))
1890 (vc-responsible-backend default-directory))
1891 'logentry-check)
1892 (run-hooks 'vc-logentry-check-hook))
1893 ;; Sync parent buffer in case the user modified it while editing the comment.
1894 ;; But not if it is a vc-dired buffer.
1895 (with-current-buffer vc-parent-buffer
1896 (or vc-dired-mode (vc-buffer-sync)))
1897 (if (not vc-log-operation)
1898 (error "No log operation is pending"))
1899 ;; save the parameters held in buffer-local variables
1900 (let ((log-operation vc-log-operation)
1901 (log-fileset vc-log-fileset)
1902 (log-revision vc-log-revision)
1903 (log-entry (buffer-string))
1904 (after-hook vc-log-after-operation-hook)
1905 (tmp-vc-parent-buffer vc-parent-buffer))
1906 (pop-to-buffer vc-parent-buffer)
1907 ;; OK, do it to it
1908 (save-excursion
1909 (funcall log-operation
1910 log-fileset
1911 log-revision
1912 log-entry))
1913 ;; Remove checkin window (after the checkin so that if that fails
1914 ;; we don't zap the *VC-log* buffer and the typing therein).
1915 (let ((logbuf (get-buffer "*VC-log*")))
1916 (cond ((and logbuf vc-delete-logbuf-window)
1917 (delete-windows-on logbuf (selected-frame))
1918 ;; Kill buffer and delete any other dedicated windows/frames.
1919 (kill-buffer logbuf))
1920 (logbuf (pop-to-buffer "*VC-log*")
1921 (bury-buffer)
1922 (pop-to-buffer tmp-vc-parent-buffer))))
1923 ;; Now make sure we see the expanded headers
1924 (if log-fileset
1925 (mapc
1926 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
1927 log-fileset))
1928 (if vc-dired-mode
1929 (dired-move-to-filename))
1930 (run-hooks after-hook 'vc-finish-logentry-hook)))
1932 ;;; Additional entry points for examining version histories
1934 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1935 ;; "List differences for all registered files at and below DIR.
1936 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1937 ;; ;; This implementation does an explicit tree walk, and calls
1938 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1939 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1940 ;; ;; and to call it only for files that are actually changed.
1941 ;; ;; However, this is expensive for some backends, and so it is left
1942 ;; ;; to backend-specific implementations.
1943 ;; (setq default-directory dir)
1944 ;; (vc-file-tree-walk
1945 ;; default-directory
1946 ;; (lambda (f)
1947 ;; (vc-exec-after
1948 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1949 ;; (message "Looking at %s" ',f)
1950 ;; (vc-call-backend ',(vc-backend f)
1951 ;; 'diff (list ',f) ',rev1 ',rev2))))))
1953 (defun vc-coding-system-for-diff (file)
1954 "Return the coding system for reading diff output for FILE."
1955 (or coding-system-for-read
1956 ;; if we already have this file open,
1957 ;; use the buffer's coding system
1958 (let ((buf (find-buffer-visiting file)))
1959 (if buf (with-current-buffer buf
1960 buffer-file-coding-system)))
1961 ;; otherwise, try to find one based on the file name
1962 (car (find-operation-coding-system 'insert-file-contents file))
1963 ;; and a final fallback
1964 'undecided))
1966 (defun vc-switches (backend op)
1967 (let ((switches
1968 (or (if backend
1969 (let ((sym (vc-make-backend-sym
1970 backend (intern (concat (symbol-name op)
1971 "-switches")))))
1972 (if (boundp sym) (symbol-value sym))))
1973 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1974 (if (boundp sym) (symbol-value sym)))
1975 (cond
1976 ((eq op 'diff) diff-switches)))))
1977 (if (stringp switches) (list switches)
1978 ;; If not a list, return nil.
1979 ;; This is so we can set vc-diff-switches to t to override
1980 ;; any switches in diff-switches.
1981 (if (listp switches) switches))))
1983 ;; Old def for compatibility with Emacs-21.[123].
1984 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1985 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
1987 (defun vc-diff-finish (buffer-name verbose)
1988 ;; The empty sync output case has already been handled, so the only
1989 ;; possibility of an empty output is for an async process.
1990 (when (buffer-live-p buffer-name)
1991 (with-current-buffer (get-buffer buffer-name)
1992 (and verbose
1993 (zerop (buffer-size))
1994 (let ((inhibit-read-only t))
1995 (insert "No differences found.\n")))
1996 (goto-char (point-min))
1997 (let ((window (get-buffer-window (current-buffer) t)))
1998 (when window
1999 (shrink-window-if-larger-than-buffer window))))))
2001 (defvar vc-diff-added-files nil
2002 "If non-nil, diff added files by comparing them to /dev/null.")
2004 (defun vc-diff-internal (async files rev1 rev2 &optional verbose)
2005 "Report diffs between two revisions of a fileset.
2006 Diff output goes to the *vc-diff* buffer. The function
2007 returns t if the buffer had changes, nil otherwise."
2008 (let* ((filenames (vc-delistify files))
2009 (rev1-name (or rev1 "working revision"))
2010 (rev2-name (or rev2 "workfile"))
2011 ;; Set coding system based on the first file. It's a kluge,
2012 ;; but the only way to set it for each file included would
2013 ;; be to call the back end separately for each file.
2014 (coding-system-for-read
2015 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
2016 (vc-setup-buffer "*vc-diff*")
2017 (message "Finding changes in %s..." filenames)
2018 ;; Many backends don't handle well the case of a file that has been
2019 ;; added but not yet committed to the repo (notably CVS and Subversion).
2020 ;; Do that work here so the backends don't have to futz with it. --ESR
2022 ;; Actually most backends (including CVS) have options to control the
2023 ;; behavior since which one is better depends on the user and on the
2024 ;; situation). Worse yet: this code does not handle the case where
2025 ;; `file' is a directory which contains added files.
2026 ;; I made it conditional on vc-diff-added-files but it should probably
2027 ;; just be removed (or copied/moved to specific backends). --Stef.
2028 (when vc-diff-added-files
2029 (let ((filtered '()))
2030 (dolist (file files)
2031 (if (or (file-directory-p file)
2032 (not (string= (vc-working-revision file) "0")))
2033 (push file filtered)
2034 ;; This file is added but not yet committed;
2035 ;; there is no master file to diff against.
2036 (if (or rev1 rev2)
2037 (error "No revisions of %s exist" file)
2038 ;; We regard this as "changed".
2039 ;; Diff it against /dev/null.
2040 (apply 'vc-do-command "*vc-diff*"
2041 1 "diff" file
2042 (append (vc-switches nil 'diff) '("/dev/null"))))))
2043 (setq files (nreverse filtered))))
2044 (let ((vc-disable-async-diff (not async)))
2045 (vc-call diff files rev1 rev2 "*vc-diff*"))
2046 (set-buffer "*vc-diff*")
2047 (if (and (zerop (buffer-size))
2048 (not (get-buffer-process (current-buffer))))
2049 ;; Treat this case specially so as not to pop the buffer.
2050 (progn
2051 (message "No changes between %s and %s" rev1-name rev2-name)
2052 nil)
2053 (diff-mode)
2054 ;; Make the *vc-diff* buffer read only, the diff-mode key
2055 ;; bindings are nicer for read only buffers. pcl-cvs does the
2056 ;; same thing.
2057 (setq buffer-read-only t)
2058 (vc-exec-after `(vc-diff-finish ,(buffer-name) ,verbose))
2059 ;; Display the buffer, but at the end because it can change point.
2060 (pop-to-buffer (current-buffer))
2061 ;; In the async case, we return t even if there are no differences
2062 ;; because we don't know that yet.
2063 t)))
2065 ;;;###autoload
2066 (defun vc-version-diff (files rev1 rev2)
2067 "Report diffs between revisions of the fileset in the repository history."
2068 (interactive
2069 (let* ((files (vc-deduce-fileset t))
2070 (first (car files))
2071 (completion-table
2072 (vc-call revision-completion-table files))
2073 (rev1-default nil)
2074 (rev2-default nil))
2075 (cond
2076 ;; someday we may be able to do revision completion on non-singleton
2077 ;; filesets, but not yet.
2078 ((/= (length files) 1)
2079 nil)
2080 ;; if it's a directory, don't supply any revision default
2081 ((file-directory-p first)
2082 nil)
2083 ;; if the file is not up-to-date, use working revision as older revision
2084 ((not (vc-up-to-date-p first))
2085 (setq rev1-default (vc-working-revision first)))
2086 ;; if the file is not locked, use last and previous revisions as defaults
2088 (setq rev1-default (vc-call previous-revision first
2089 (vc-working-revision first)))
2090 (if (string= rev1-default "") (setq rev1-default nil))
2091 (setq rev2-default (vc-working-revision first))))
2092 ;; construct argument list
2093 (let* ((rev1-prompt (if rev1-default
2094 (concat "Older revision (default "
2095 rev1-default "): ")
2096 "Older revision: "))
2097 (rev2-prompt (concat "Newer revision (default "
2098 (or rev2-default "current source") "): "))
2099 (rev1 (if completion-table
2100 (completing-read rev1-prompt completion-table
2101 nil nil nil nil rev1-default)
2102 (read-string rev1-prompt nil nil rev1-default)))
2103 (rev2 (if completion-table
2104 (completing-read rev2-prompt completion-table
2105 nil nil nil nil rev2-default)
2106 (read-string rev2-prompt nil nil rev2-default))))
2107 (if (string= rev1 "") (setq rev1 nil))
2108 (if (string= rev2 "") (setq rev2 nil))
2109 (list files rev1 rev2))))
2110 (if (and (not rev1) rev2)
2111 (error "Not a valid revision range."))
2112 (vc-diff-internal t files rev1 rev2 (interactive-p)))
2114 ;; (defun vc-contains-version-controlled-file (dir)
2115 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
2116 ;; (catch 'found
2117 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
2118 ;; nil))
2120 ;;;###autoload
2121 (defun vc-diff (historic &optional not-urgent)
2122 "Display diffs between file revisions.
2123 Normally this compares the currently selected fileset with their
2124 working revisions. With a prefix argument HISTORIC, it reads two revision
2125 designators specifying which revisions to compare.
2127 If no current fileset is available (that is, we are not in
2128 VC-Dired mode and the visited file of the current buffer is not
2129 under version control) and we're in a Dired buffer, use
2130 the current directory.
2131 The optional argument NOT-URGENT non-nil means it is ok to say no to
2132 saving the buffer."
2133 (interactive (list current-prefix-arg t))
2134 (if historic
2135 (call-interactively 'vc-version-diff)
2136 (let* ((files (vc-deduce-fileset t)))
2137 (if buffer-file-name (vc-buffer-sync not-urgent))
2138 (vc-diff-internal t files nil nil (interactive-p)))))
2141 ;;;###autoload
2142 (defun vc-revision-other-window (rev)
2143 "Visit revision REV of the current file in another window.
2144 If the current file is named `F', the revision is named `F.~REV~'.
2145 If `F.~REV~' already exists, use it instead of checking it out again."
2146 (interactive
2147 (save-current-buffer
2148 (vc-ensure-vc-buffer)
2149 (let ((completion-table
2150 (vc-call revision-completion-table buffer-file-name))
2151 (prompt "Revision to visit (default is working revision): "))
2152 (list
2153 (if completion-table
2154 (completing-read prompt completion-table)
2155 (read-string prompt))))))
2156 (vc-ensure-vc-buffer)
2157 (let* ((file buffer-file-name)
2158 (revision (if (string-equal rev "")
2159 (vc-working-revision file)
2160 rev)))
2161 (switch-to-buffer-other-window (vc-find-revision file revision))))
2163 (defun vc-find-revision (file revision)
2164 "Read REVISION of FILE into a buffer and return the buffer."
2165 (let ((automatic-backup (vc-version-backup-file-name file revision))
2166 (filebuf (or (get-file-buffer file) (current-buffer)))
2167 (filename (vc-version-backup-file-name file revision 'manual)))
2168 (unless (file-exists-p filename)
2169 (if (file-exists-p automatic-backup)
2170 (rename-file automatic-backup filename nil)
2171 (message "Checking out %s..." filename)
2172 (with-current-buffer filebuf
2173 (let ((failed t))
2174 (unwind-protect
2175 (let ((coding-system-for-read 'no-conversion)
2176 (coding-system-for-write 'no-conversion))
2177 (with-temp-file filename
2178 (let ((outbuf (current-buffer)))
2179 ;; Change buffer to get local value of
2180 ;; vc-checkout-switches.
2181 (with-current-buffer filebuf
2182 (vc-call find-revision file revision outbuf))))
2183 (setq failed nil))
2184 (when (and failed (file-exists-p filename))
2185 (delete-file filename))))
2186 (vc-mode-line file))
2187 (message "Checking out %s...done" filename)))
2188 (let ((result-buf (find-file-noselect filename)))
2189 (with-current-buffer result-buf
2190 ;; Set the parent buffer so that things like
2191 ;; C-x v g, C-x v l, ... etc work.
2192 (setq vc-parent-buffer filebuf))
2193 result-buf)))
2195 ;; Header-insertion code
2197 ;;;###autoload
2198 (defun vc-insert-headers ()
2199 "Insert headers into a file for use with a version control system.
2200 Headers desired are inserted at point, and are pulled from
2201 the variable `vc-BACKEND-header'."
2202 (interactive)
2203 (vc-ensure-vc-buffer)
2204 (save-excursion
2205 (save-restriction
2206 (widen)
2207 (if (or (not (vc-check-headers))
2208 (y-or-n-p "Version headers already exist. Insert another set? "))
2209 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2210 (comment-start-vc (or (car delims) comment-start "#"))
2211 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2212 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2213 'header))
2214 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2215 (dolist (s hdstrings)
2216 (insert comment-start-vc "\t" s "\t"
2217 comment-end-vc "\n"))
2218 (if vc-static-header-alist
2219 (dolist (f vc-static-header-alist)
2220 (if (string-match (car f) buffer-file-name)
2221 (insert (format (cdr f) (car hdstrings)))))))))))
2223 (defun vc-clear-headers (&optional file)
2224 "Clear all version headers in the current buffer (or FILE).
2225 The headers are reset to their non-expanded form."
2226 (let* ((filename (or file buffer-file-name))
2227 (visited (find-buffer-visiting filename))
2228 (backend (vc-backend filename)))
2229 (when (vc-find-backend-function backend 'clear-headers)
2230 (if visited
2231 (let ((context (vc-buffer-context)))
2232 ;; save-excursion may be able to relocate point and mark
2233 ;; properly. If it fails, vc-restore-buffer-context
2234 ;; will give it a second try.
2235 (save-excursion
2236 (vc-call-backend backend 'clear-headers))
2237 (vc-restore-buffer-context context))
2238 (set-buffer (find-file-noselect filename))
2239 (vc-call-backend backend 'clear-headers)
2240 (kill-buffer filename)))))
2242 (defun vc-modify-change-comment (files rev oldcomment)
2243 "Edit the comment associated with the given files and revision."
2244 (vc-start-entry
2245 files rev oldcomment t
2246 "Enter a replacement change comment."
2247 (lambda (files rev comment)
2248 (vc-call-backend
2249 ;; Less of a kluge than it looks like; log-view mode only passes
2250 ;; this function a singleton list. Arguments left in this form in
2251 ;; case the more general operation ever becomes meaningful.
2252 (vc-responsible-backend (car files))
2253 'modify-change-comment files rev comment))))
2255 ;;;###autoload
2256 (defun vc-merge ()
2257 "Merge changes between two revisions into the current buffer's file.
2258 This asks for two revisions to merge from in the minibuffer. If the
2259 first revision is a branch number, then merge all changes from that
2260 branch. If the first revision is empty, merge news, i.e. recent changes
2261 from the current branch.
2263 See Info node `Merging'."
2264 (interactive)
2265 (vc-ensure-vc-buffer)
2266 (vc-buffer-sync)
2267 (let* ((file buffer-file-name)
2268 (backend (vc-backend file))
2269 (state (vc-state file))
2270 first-revision second-revision status)
2271 (cond
2272 ((stringp state) ;; Locking VCses only
2273 (error "File is locked by %s" state))
2274 ((not (vc-editable-p file))
2275 (if (y-or-n-p
2276 "File must be checked out for merging. Check out now? ")
2277 (vc-checkout file t)
2278 (error "Merge aborted"))))
2279 (setq first-revision
2280 (read-string (concat "Branch or revision to merge from "
2281 "(default news on current branch): ")))
2282 (if (string= first-revision "")
2283 (if (not (vc-find-backend-function backend 'merge-news))
2284 (error "Sorry, merging news is not implemented for %s" backend)
2285 (setq status (vc-call merge-news file)))
2286 (if (not (vc-find-backend-function backend 'merge))
2287 (error "Sorry, merging is not implemented for %s" backend)
2288 (if (not (vc-branch-p first-revision))
2289 (setq second-revision
2290 (read-string "Second revision: "
2291 (concat (vc-branch-part first-revision) ".")))
2292 ;; We want to merge an entire branch. Set revisions
2293 ;; accordingly, so that vc-BACKEND-merge understands us.
2294 (setq second-revision first-revision)
2295 ;; first-revision must be the starting point of the branch
2296 (setq first-revision (vc-branch-part first-revision)))
2297 (setq status (vc-call merge file first-revision second-revision))))
2298 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2300 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2301 (vc-resynch-buffer file t (not (buffer-modified-p)))
2302 (if (zerop status) (message "Merge successful")
2303 (smerge-mode 1)
2304 (message "File contains conflicts.")))
2306 ;;;###autoload
2307 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2309 ;; The VC directory major mode. Coopt Dired for this.
2310 ;; All VC commands get mapped into logical equivalents.
2312 (defvar vc-dired-switches)
2313 (defvar vc-dired-terse-mode)
2315 (defvar vc-dired-mode-map
2316 (let ((map (make-sparse-keymap))
2317 (vmap (make-sparse-keymap)))
2318 (define-key map "\C-xv" vmap)
2319 (define-key map "v" vmap)
2320 (set-keymap-parent vmap vc-prefix-map)
2321 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2322 map))
2324 (define-derived-mode vc-dired-mode dired-mode "Dired under "
2325 "The major mode used in VC directory buffers.
2327 It works like Dired, but lists only files under version control, with
2328 the current VC state of each file being indicated in the place of the
2329 file's link count, owner, group and size. Subdirectories are also
2330 listed, and you may insert them into the buffer as desired, like in
2331 Dired.
2333 All Dired commands operate normally, with the exception of `v', which
2334 is redefined as the version control prefix, so that you can type
2335 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2336 the file named in the current Dired buffer line. `vv' invokes
2337 `vc-next-action' on this file, or on all files currently marked.
2338 There is a special command, `*l', to mark all files currently locked."
2339 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2340 ;; We do it here because dired might not be loaded yet
2341 ;; when vc-dired-mode-map is initialized.
2342 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2343 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2344 ;; The following is slightly modified from files.el,
2345 ;; because file lines look a bit different in vc-dired-mode
2346 ;; (the column before the date does not end in a digit).
2347 ;; albinus: It should be done in the original declaration. Problem
2348 ;; is the optional empty state-info; otherwise ")" would be good
2349 ;; enough as delimeter.
2350 (set (make-local-variable 'directory-listing-before-filename-regexp)
2351 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2352 ;; In some locales, month abbreviations are as short as 2 letters,
2353 ;; and they can be followed by ".".
2354 (month (concat l l "+\\.?"))
2355 (s " ")
2356 (yyyy "[0-9][0-9][0-9][0-9]")
2357 (dd "[ 0-3][0-9]")
2358 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2359 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2360 (zone "[-+][0-2][0-9][0-5][0-9]")
2361 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2362 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2363 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2364 "\\|" yyyy "-" iso-mm-dd "\\)"))
2365 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2366 s "+"
2367 "\\(" HH:MM "\\|" yyyy "\\)"))
2368 (western-comma (concat month s "+" dd "," s "+" yyyy))
2369 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2370 ;; omits the Kanji characters after month and day-of-month.
2371 (mm "[ 0-1]?[0-9]")
2372 (japanese
2373 (concat mm l "?" s dd l "?" s "+"
2374 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2375 ;; the .* below ensures that we find the last match on a line
2376 (concat ".*" s
2377 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2378 s "+")))
2379 (and (boundp 'vc-dired-switches)
2380 vc-dired-switches
2381 (set (make-local-variable 'dired-actual-switches)
2382 vc-dired-switches))
2383 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2384 (let ((backend-name (symbol-name (vc-responsible-backend
2385 default-directory))))
2386 (setq mode-name (concat mode-name backend-name))
2387 ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
2388 (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
2389 (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
2390 (cons backend-name vc-dire-menu-map) 'subdir)))
2391 (setq vc-dired-mode t))
2393 (defun vc-dired-toggle-terse-mode ()
2394 "Toggle terse display in VC Dired."
2395 (interactive)
2396 (if (not vc-dired-mode)
2398 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2399 (if vc-dired-terse-mode
2400 (vc-dired-hook)
2401 (revert-buffer))))
2403 (defun vc-dired-mark-locked ()
2404 "Mark all files currently locked."
2405 (interactive)
2406 (dired-mark-if (let ((f (dired-get-filename nil t)))
2407 (and f
2408 (not (file-directory-p f))
2409 (not (vc-up-to-date-p f))))
2410 "locked file"))
2412 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2414 (defun vc-dired-reformat-line (vc-info)
2415 "Reformat a directory-listing line.
2416 Replace various columns with version control information, VC-INFO.
2417 This code, like dired, assumes UNIX -l format."
2418 (beginning-of-line)
2419 (when (re-search-forward
2420 ;; Match link count, owner, group, size. Group may be missing,
2421 ;; and only the size is present in OS/2 -l format.
2422 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2423 (line-end-position) t)
2424 (replace-match (substring (concat vc-info " ") 0 10)
2425 t t nil 1)))
2427 (defun vc-dired-ignorable-p (filename)
2428 "Should FILENAME be ignored in VC-Dired listings?"
2429 (catch t
2430 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
2431 (dolist (ignorable completion-ignored-extensions)
2432 (let ((ext (substring filename
2433 (- (length filename)
2434 (length ignorable)))))
2435 (if (string= ignorable ext) (throw t t))))
2436 ;; Ignore Makefiles derived from something else
2437 (when (string= (file-name-nondirectory filename) "Makefile")
2438 (let* ((dir (file-name-directory filename))
2439 (peers (directory-files (or dir default-directory))))
2440 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
2441 (throw t t))))
2442 nil))
2444 (defun vc-dired-hook ()
2445 "Reformat the listing according to version control.
2446 Called by dired after any portion of a vc-dired buffer has been read in."
2447 (message "Getting version information... ")
2448 ;; if the backend supports it, get the state
2449 ;; of all files in this directory at once
2450 (let ((backend (vc-responsible-backend default-directory)))
2451 ;; check `backend' can really handle `default-directory'.
2452 (if (and (vc-call-backend backend 'responsible-p default-directory)
2453 (vc-find-backend-function backend 'dir-state))
2454 (vc-call-backend backend 'dir-state default-directory)))
2455 (let (filename
2456 (inhibit-read-only t)
2457 (buffer-undo-list t))
2458 (goto-char (point-min))
2459 (while (not (eobp))
2460 (cond
2461 ;; subdir header line
2462 ((dired-get-subdir)
2463 (forward-line 1)
2464 ;; erase (but don't remove) the "total" line
2465 (delete-region (point) (line-end-position))
2466 (beginning-of-line)
2467 (forward-line 1))
2468 ;; file line
2469 ((setq filename (dired-get-filename nil t))
2470 (cond
2471 ;; subdir
2472 ((file-directory-p filename)
2473 (cond
2474 ((member (file-name-nondirectory filename)
2475 vc-directory-exclusion-list)
2476 (let ((pos (point)))
2477 (dired-kill-tree filename)
2478 (goto-char pos)
2479 (dired-kill-line)))
2480 (vc-dired-terse-mode
2481 ;; Don't show directories in terse mode. Don't use
2482 ;; dired-kill-line to remove it, because in recursive listings,
2483 ;; that would remove the directory contents as well.
2484 (delete-region (line-beginning-position)
2485 (progn (forward-line 1) (point))))
2486 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2487 (dired-kill-line))
2489 (vc-dired-reformat-line nil)
2490 (forward-line 1))))
2491 ;; Try to head off calling the expensive state query -
2492 ;; ignore object files, TeX intermediate files, and so forth.
2493 ((vc-dired-ignorable-p filename)
2494 (dired-kill-line))
2495 ;; Ordinary file -- call the (possibly expensive) state query
2497 ;; First case: unregistered or unknown. (Unknown shouldn't happen here)
2498 ((member (vc-state filename) '(nil unregistered))
2499 (if vc-dired-terse-mode
2500 (dired-kill-line)
2501 (vc-dired-reformat-line "?")
2502 (forward-line 1)))
2503 ;; Either we're in non-terse mode or it's out of date
2504 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
2505 (vc-dired-reformat-line (vc-call dired-state-info filename))
2506 (forward-line 1))
2507 ;; Remaining cases are under version control but uninteresting
2509 (dired-kill-line))))
2510 ;; any other line
2511 (t (forward-line 1))))
2512 (vc-dired-purge))
2513 (message "Getting version information... done")
2514 (save-restriction
2515 (widen)
2516 (cond ((eq (count-lines (point-min) (point-max)) 1)
2517 (goto-char (point-min))
2518 (message "No changes pending under %s" default-directory)))))
2520 (defun vc-dired-purge ()
2521 "Remove empty subdirs."
2522 (goto-char (point-min))
2523 (while (dired-get-subdir)
2524 (forward-line 2)
2525 (if (dired-get-filename nil t)
2526 (if (not (dired-next-subdir 1 t))
2527 (goto-char (point-max)))
2528 (forward-line -2)
2529 (if (not (string= (dired-current-directory) default-directory))
2530 (dired-do-kill-lines t "")
2531 ;; We cannot remove the top level directory.
2532 ;; Just make it look a little nicer.
2533 (forward-line 1)
2534 (or (eobp) (kill-line))
2535 (if (not (dired-next-subdir 1 t))
2536 (goto-char (point-max))))))
2537 (goto-char (point-min)))
2539 (defun vc-dired-buffers-for-dir (dir)
2540 "Return a list of all vc-dired buffers that currently display DIR."
2541 (let (result)
2542 ;; Check whether dired is loaded.
2543 (when (fboundp 'dired-buffers-for-dir)
2544 (dolist (buffer (dired-buffers-for-dir dir))
2545 (with-current-buffer buffer
2546 (if vc-dired-mode
2547 (push buffer result)))))
2548 (nreverse result)))
2550 (defun vc-dired-resynch-file (file)
2551 "Update the entries for FILE in any VC Dired buffers that list it."
2552 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2553 (when buffers
2554 (mapcar (lambda (buffer)
2555 (with-current-buffer buffer
2556 (if (dired-goto-file file)
2557 ;; bind vc-dired-terse-mode to nil so that
2558 ;; files won't vanish when they are checked in
2559 (let ((vc-dired-terse-mode nil))
2560 (dired-do-redisplay 1)))))
2561 buffers))))
2563 ;;;###autoload
2564 (defun vc-directory (dir read-switches)
2565 "Create a buffer in VC Dired Mode for directory DIR.
2567 See Info node `VC Dired Mode'.
2569 With prefix arg READ-SWITCHES, specify a value to override
2570 `dired-listing-switches' when generating the listing."
2571 (interactive "DDired under VC (directory): \nP")
2572 (let ((vc-dired-switches (concat vc-dired-listing-switches
2573 (if vc-dired-recurse "R" ""))))
2574 (if (eq (string-match tramp-file-name-regexp dir) 0)
2575 (error "Sorry, vc-directory does not work over Tramp"))
2576 (if read-switches
2577 (setq vc-dired-switches
2578 (read-string "Dired listing switches: "
2579 vc-dired-switches)))
2580 (require 'dired)
2581 (require 'dired-aux)
2582 (switch-to-buffer
2583 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2584 vc-dired-switches
2585 'vc-dired-mode))))
2587 ;;; Experimental code for the vc-dired replacement
2588 (require 'ewoc)
2590 (defstruct (vc-status-fileinfo
2591 (:copier nil)
2592 (:constructor vc-status-create-fileinfo (state name &optional marked))
2593 (:conc-name vc-status-fileinfo->))
2594 marked
2595 state
2596 name)
2598 (defvar vc-status nil)
2600 (defun vc-status-headers (backend dir)
2601 (concat
2602 (format "VC backend : %s\n" backend)
2603 "Repository : The repository goes here\n"
2604 (format "Working dir: %s\n" dir)))
2606 (defun vc-status-printer (fileentry)
2607 "Pretty print FILEENTRY."
2608 (insert
2609 ;; If you change this, change vc-status-move-to-goal-column.
2610 (format "%c %-20s %s"
2611 (if (vc-status-fileinfo->marked fileentry) ?* ? )
2612 (vc-status-fileinfo->state fileentry)
2613 (vc-status-fileinfo->name fileentry))))
2615 (defun vc-status-move-to-goal-column ()
2616 (beginning-of-line)
2617 ;; Must be in sync with vc-status-printer.
2618 (forward-char 25))
2620 ;;;###autoload
2621 (defun vc-status (dir)
2622 "Show the VC status for DIR."
2623 (interactive "DVC status for directory: ")
2624 (vc-setup-buffer "*vc-status*")
2625 (switch-to-buffer "*vc-status*")
2626 (cd dir)
2627 (vc-status-mode))
2629 (defvar vc-status-mode-map
2630 (let ((map (make-keymap)))
2631 (suppress-keymap map)
2632 ;; Marking.
2633 (define-key map "m" 'vc-status-mark-file)
2634 (define-key map "M" 'vc-status-mark-all-files)
2635 (define-key map "u" 'vc-status-unmark-file)
2636 (define-key map "\C-?" 'vc-status-unmark-file-up)
2637 (define-key map "\M-\C-?" 'vc-status-unmark-all-files)
2638 ;; Movement.
2639 (define-key map "n" 'vc-status-next-line)
2640 (define-key map " " 'vc-status-next-line)
2641 (define-key map "\t" 'vc-status-next-line)
2642 (define-key map "p" 'vc-status-previous-line)
2643 (define-key map [backtab] 'vc-status-previous-line)
2644 ;; VC commands.
2645 (define-key map "=" 'vc-diff)
2646 (define-key map "a" 'vc-status-register)
2647 ;; Can't be "g" (as in vc map), so "A" for "Annotate".
2648 (define-key map "A" 'vc-annotate)
2649 ;; vc-print-log uses the current buffer, not a file.
2650 ;; (define-key map "l" 'vc-status-print-log)
2651 ;; The remainder.
2652 (define-key map "f" 'vc-status-find-file)
2653 (define-key map "o" 'vc-status-find-file-other-window)
2654 (define-key map "q" 'bury-buffer)
2655 (define-key map "g" 'vc-status-refresh)
2656 map)
2657 "Keymap for VC status")
2659 (defun vc-status-mode ()
2660 "Major mode for VC status.
2661 \\{vc-status-mode-map}"
2662 (setq mode-name "*VC Status*")
2663 (setq major-mode 'vc-status-mode)
2664 (setq buffer-read-only t)
2665 (use-local-map vc-status-mode-map)
2666 (let ((buffer-read-only nil)
2667 (backend (vc-responsible-backend default-directory))
2668 entries)
2669 (erase-buffer)
2670 (set (make-local-variable 'vc-status)
2671 (ewoc-create #'vc-status-printer
2672 (vc-status-headers backend default-directory)))
2673 (vc-status-refresh)))
2675 (put 'vc-status-mode 'mode-class 'special)
2677 (defun vc-update-vc-status-buffer (entries buffer)
2678 (with-current-buffer buffer
2679 (dolist (entry entries)
2680 (ewoc-enter-last vc-status
2681 (vc-status-create-fileinfo (cdr entry) (car entry))))
2682 (ewoc-goto-node vc-status (ewoc-nth vc-status 0))))
2684 (defun vc-status-refresh ()
2685 "Refresh the contents of the VC status buffer."
2686 (interactive)
2687 ;; This is not very efficient; ewoc could use a new function here.
2688 (ewoc-filter vc-status (lambda (node) nil))
2689 (let ((backend (vc-responsible-backend default-directory)))
2690 ;; Call the dir-status backend function. dir-status is supposed to
2691 ;; be asynchronous. It should compute the results and call the
2692 ;; function passed as a an arg to update the vc-status buffer with
2693 ;; the results.
2694 (vc-call-backend
2695 backend 'dir-status default-directory
2696 #'vc-update-vc-status-buffer (current-buffer))))
2698 (defun vc-status-next-line (arg)
2699 "Go to the next line.
2700 If a prefix argument is given, move by that many lines."
2701 (interactive "p")
2702 (ewoc-goto-next vc-status arg)
2703 (vc-status-move-to-goal-column))
2705 (defun vc-status-previous-line (arg)
2706 "Go to the previous line.
2707 If a prefix argument is given, move by that many lines."
2708 (interactive "p")
2709 (ewoc-goto-prev vc-status arg)
2710 (vc-status-move-to-goal-column))
2712 (defun vc-status-mark-file ()
2713 "Mark the current file and move to the next line."
2714 (interactive)
2715 (let* ((crt (ewoc-locate vc-status))
2716 (file (ewoc-data crt)))
2717 (setf (vc-status-fileinfo->marked file) t)
2718 (ewoc-invalidate vc-status crt)
2719 (vc-status-next-line 1)))
2721 (defun vc-status-mark-all-files ()
2722 "Mark all files."
2723 (interactive)
2724 (ewoc-map
2725 (lambda (file)
2726 (unless (vc-status-fileinfo->marked file)
2727 (setf (vc-status-fileinfo->marked file) t)
2729 vc-status))
2731 (defun vc-status-unmark-file ()
2732 "Unmark the current file and move to the next line."
2733 (interactive)
2734 (let* ((crt (ewoc-locate vc-status))
2735 (file (ewoc-data crt)))
2736 (setf (vc-status-fileinfo->marked file) nil)
2737 (ewoc-invalidate vc-status crt)
2738 (vc-status-next-line 1)))
2740 (defun vc-status-unmark-file-up ()
2741 "Move to the previous line and unmark the file."
2742 (interactive)
2743 ;; If we're on the first line, we won't move up, but we will still
2744 ;; remove the mark. This seems a bit odd but it is what buffer-menu
2745 ;; does.
2746 (let* ((prev (ewoc-goto-prev vc-status 1))
2747 (file (ewoc-data prev)))
2748 (setf (vc-status-fileinfo->marked file) nil)
2749 (ewoc-invalidate vc-status prev)
2750 (vc-status-move-to-goal-column)))
2752 (defun vc-status-unmark-all-files ()
2753 "Unmark all files."
2754 (interactive)
2755 (ewoc-map
2756 (lambda (file)
2757 (when (vc-status-fileinfo->marked file)
2758 (setf (vc-status-fileinfo->marked file) nil)
2760 vc-status))
2762 (defun vc-status-register ()
2763 "Register the marked files, or the current file if no marks."
2764 (interactive)
2765 (let ((files (or (vc-status-marked-files)
2766 (list (vc-status-current-file)))))
2767 (dolist (file files)
2768 (vc-register file))))
2770 (defun vc-status-find-file ()
2771 "Find the file on the current line."
2772 (interactive)
2773 (find-file (vc-status-current-file)))
2775 (defun vc-status-find-file-other-window ()
2776 "Find the file on the current line, in another window."
2777 (interactive)
2778 (find-file-other-window (vc-status-current-file)))
2780 (defun vc-status-current-file ()
2781 (let ((node (ewoc-locate vc-status)))
2782 (unless node
2783 (error "No file available."))
2784 (expand-file-name (vc-status-fileinfo->name (ewoc-data node)))))
2786 (defun vc-status-marked-files ()
2787 "Return the list of marked files"
2788 (mapcar
2789 (lambda (elem)
2790 (expand-file-name (vc-status-fileinfo->name elem)))
2791 (ewoc-collect
2792 vc-status
2793 (lambda (crt) (vc-status-fileinfo->marked crt)))))
2795 ;;; End experimental code.
2797 ;; Named-configuration entry points
2799 (defun vc-snapshot-precondition (dir)
2800 "Scan the tree below DIR, looking for files not up-to-date.
2801 If any file is not up-to-date, return the name of the first such file.
2802 \(This means, neither snapshot creation nor retrieval is allowed.\)
2803 If one or more of the files are currently visited, return `visited'.
2804 Otherwise, return nil."
2805 (let ((status nil))
2806 (catch 'vc-locked-example
2807 (vc-file-tree-walk
2809 (lambda (f)
2810 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2811 (if (get-file-buffer f) (setq status 'visited)))))
2812 status)))
2814 ;;;###autoload
2815 (defun vc-create-snapshot (dir name branchp)
2816 "Descending recursively from DIR, make a snapshot called NAME.
2817 For each registered file, the working revision becomes part of
2818 the named configuration. If the prefix argument BRANCHP is
2819 given, the snapshot is made as a new branch and the files are
2820 checked out in that new branch."
2821 (interactive
2822 (list (read-file-name "Directory: " default-directory default-directory t)
2823 (read-string "New snapshot name: ")
2824 current-prefix-arg))
2825 (message "Making %s... " (if branchp "branch" "snapshot"))
2826 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2827 (vc-call-backend (vc-responsible-backend dir)
2828 'create-snapshot dir name branchp)
2829 (message "Making %s... done" (if branchp "branch" "snapshot")))
2831 ;;;###autoload
2832 (defun vc-retrieve-snapshot (dir name)
2833 "Descending recursively from DIR, retrieve the snapshot called NAME.
2834 If NAME is empty, it refers to the latest revisions.
2835 If locking is used for the files in DIR, then there must not be any
2836 locked files at or below DIR (but if NAME is empty, locked files are
2837 allowed and simply skipped)."
2838 (interactive
2839 (list (read-file-name "Directory: " default-directory default-directory t)
2840 (read-string "Snapshot name to retrieve (default latest revisions): ")))
2841 (let ((update (yes-or-no-p "Update any affected buffers? "))
2842 (msg (if (or (not name) (string= name ""))
2843 (format "Updating %s... " (abbreviate-file-name dir))
2844 (format "Retrieving snapshot into %s... "
2845 (abbreviate-file-name dir)))))
2846 (message "%s" msg)
2847 (vc-call-backend (vc-responsible-backend dir)
2848 'retrieve-snapshot dir name update)
2849 (message "%s" (concat msg "done"))))
2851 ;; Miscellaneous other entry points
2853 ;;;###autoload
2854 (defun vc-print-log (&optional working-revision)
2855 "List the change log of the current fileset in a window.
2856 If WORKING-REVISION is non-nil, leave the point at that revision."
2857 (interactive)
2858 (let* ((files (vc-deduce-fileset))
2859 (backend (vc-backend files))
2860 (working-revision (or working-revision (vc-working-revision (car files)))))
2861 ;; Don't switch to the output buffer before running the command,
2862 ;; so that any buffer-local settings in the vc-controlled
2863 ;; buffer can be accessed by the command.
2864 (vc-call-backend backend 'print-log files "*vc-change-log*")
2865 (pop-to-buffer "*vc-change-log*")
2866 (vc-exec-after
2867 `(let ((inhibit-read-only t))
2868 (vc-call-backend ',backend 'log-view-mode)
2869 (goto-char (point-max)) (forward-line -1)
2870 (while (looking-at "=*\n")
2871 (delete-char (- (match-end 0) (match-beginning 0)))
2872 (forward-line -1))
2873 (goto-char (point-min))
2874 (if (looking-at "[\b\t\n\v\f\r ]+")
2875 (delete-char (- (match-end 0) (match-beginning 0))))
2876 (shrink-window-if-larger-than-buffer)
2877 ;; move point to the log entry for the working revision
2878 (vc-call-backend ',backend 'show-log-entry ',working-revision)
2879 (setq vc-sentinel-movepoint (point))
2880 (set-buffer-modified-p nil)))))
2882 ;;;###autoload
2883 (defun vc-revert ()
2884 "Revert working copies of the selected fileset to their repository contents.
2885 This asks for confirmation if the buffer contents are not identical
2886 to the working revision (except for keyword expansion)."
2887 (interactive)
2888 (let* ((files (vc-deduce-fileset)))
2889 ;; If any of the files is visited by the current buffer, make
2890 ;; sure buffer is saved. If the user says `no', abort since
2891 ;; we cannot show the changes and ask for confirmation to
2892 ;; discard them.
2893 (if (or (not files) (memq (buffer-file-name) files))
2894 (vc-buffer-sync nil))
2895 (dolist (file files)
2896 (let ((buf (get-file-buffer file)))
2897 (if (and buf (buffer-modified-p buf))
2898 (error "Please kill or save all modified buffers before reverting.")))
2899 (if (vc-up-to-date-p file)
2900 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2901 (error "Revert canceled"))))
2902 (if (vc-diff-internal vc-allow-async-revert files nil nil)
2903 (progn
2904 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
2905 (error "Revert canceled"))
2906 (delete-windows-on "*vc-diff*")
2907 (kill-buffer "*vc-diff*")))
2908 (dolist (file files)
2909 (progn
2910 (message "Reverting %s..." (vc-delistify files))
2911 (vc-revert-file file)
2912 (message "Reverting %s...done" (vc-delistify files))))))
2914 ;;;###autoload
2915 (defun vc-rollback ()
2916 "Roll back (remove) the most recent changeset committed to the repository.
2917 This may be either a file-level or a repository-level operation,
2918 depending on the underlying version-control system."
2919 (interactive)
2920 (let* ((files (vc-deduce-fileset))
2921 (backend (vc-backend files))
2922 (granularity (vc-call-backend backend 'revision-granularity)))
2923 (unless (vc-find-backend-function backend 'rollback)
2924 (error "Rollback is not supported in %s" backend))
2925 (if (and (not (eq granularity 'repository)) (/= (length files) 1))
2926 (error "Rollback requires a singleton fileset or repository versioning"))
2927 (if (not (vc-call latest-on-branch-p (car files)))
2928 (error "Rollback is only possible at the tip revision."))
2929 ;; If any of the files is visited by the current buffer, make
2930 ;; sure buffer is saved. If the user says `no', abort since
2931 ;; we cannot show the changes and ask for confirmation to
2932 ;; discard them.
2933 (if (or (not files) (memq (buffer-file-name) files))
2934 (vc-buffer-sync nil))
2935 (dolist (file files)
2936 (if (buffer-modified-p (get-file-buffer file))
2937 (error "Please kill or save all modified buffers before rollback."))
2938 (if (not (vc-up-to-date-p file))
2939 (error "Please revert all modified workfiles before rollback.")))
2940 ;; Accumulate changes associated with the fileset
2941 (vc-setup-buffer "*vc-diff*")
2942 (not-modified)
2943 (message "Finding changes...")
2944 (let* ((tip (vc-working-revision (car files)))
2945 (previous (vc-call previous-revision (car files) tip)))
2946 (vc-diff-internal nil files previous tip))
2947 ;; Display changes
2948 (unless (yes-or-no-p "Discard these revisions? ")
2949 (error "Rollback canceled"))
2950 (delete-windows-on "*vc-diff*")
2951 (kill-buffer"*vc-diff*")
2952 ;; Do the actual reversions
2953 (message "Rolling back %s..." (vc-delistify files))
2954 (with-vc-properties
2955 files
2956 (vc-call-backend backend 'rollback files)
2957 `((vc-state . ,'up-to-date)
2958 (vc-checkout-time . , (nth 5 (file-attributes file)))
2959 (vc-working-revision . nil)))
2960 (dolist (f files) (vc-resynch-buffer f t t))
2961 (message "Rolling back %s...done" (vc-delistify files))))
2963 ;;;###autoload
2964 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2966 ;;;###autoload
2967 (defun vc-update ()
2968 "Update the current fileset's files to their tip revisions.
2969 For each one that contains no changes, and is not locked, then this simply
2970 replaces the work file with the latest revision on its branch. If the file
2971 contains changes, and the backend supports merging news, then any recent
2972 changes from the current branch are merged into the working file."
2973 (interactive)
2974 (dolist (file (vc-deduce-fileset))
2975 (if (buffer-modified-p (get-file-buffer file))
2976 (error "Please kill or save all modified buffers before updating."))
2977 (if (vc-up-to-date-p file)
2978 (vc-checkout file nil "")
2979 (if (eq (vc-checkout-model file) 'locking)
2980 (if (eq (vc-state file) 'edited)
2981 (error "%s"
2982 (substitute-command-keys
2983 "File is locked--type \\[vc-revert] to discard changes"))
2984 (error "Unexpected file state (%s) -- type %s"
2985 (vc-state file)
2986 (substitute-command-keys
2987 "\\[vc-next-action] to correct")))
2988 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
2989 (error "Sorry, merging news is not implemented for %s"
2990 (vc-backend file))
2991 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))))
2993 (defun vc-version-backup-file (file &optional rev)
2994 "Return name of backup file for revision REV of FILE.
2995 If version backups should be used for FILE, and there exists
2996 such a backup for REV or the working revision of file, return
2997 its name; otherwise return nil."
2998 (when (vc-call make-version-backups-p file)
2999 (let ((backup-file (vc-version-backup-file-name file rev)))
3000 (if (file-exists-p backup-file)
3001 backup-file
3002 ;; there is no automatic backup, but maybe the user made one manually
3003 (setq backup-file (vc-version-backup-file-name file rev 'manual))
3004 (if (file-exists-p backup-file)
3005 backup-file)))))
3007 (defun vc-revert-file (file)
3008 "Revert FILE back to the repository working revision it was based on."
3009 (with-vc-properties
3010 (list file)
3011 (let ((backup-file (vc-version-backup-file file)))
3012 (when backup-file
3013 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
3014 (vc-delete-automatic-version-backups file))
3015 (vc-call revert file backup-file))
3016 `((vc-state . up-to-date)
3017 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
3018 (vc-resynch-buffer file t t))
3020 ;;;###autoload
3021 (defun vc-switch-backend (file backend)
3022 "Make BACKEND the current version control system for FILE.
3023 FILE must already be registered in BACKEND. The change is not
3024 permanent, only for the current session. This function only changes
3025 VC's perspective on FILE, it does not register or unregister it.
3026 By default, this command cycles through the registered backends.
3027 To get a prompt, use a prefix argument."
3028 (interactive
3029 (list
3030 (or buffer-file-name
3031 (error "There is no version-controlled file in this buffer"))
3032 (let ((backend (vc-backend buffer-file-name))
3033 (backends nil))
3034 (unless backend
3035 (error "File %s is not under version control" buffer-file-name))
3036 ;; Find the registered backends.
3037 (dolist (backend vc-handled-backends)
3038 (when (vc-call-backend backend 'registered buffer-file-name)
3039 (push backend backends)))
3040 ;; Find the next backend.
3041 (let ((def (car (delq backend (append (memq backend backends) backends))))
3042 (others (delete backend backends)))
3043 (cond
3044 ((null others) (error "No other backend to switch to"))
3045 (current-prefix-arg
3046 (intern
3047 (upcase
3048 (completing-read
3049 (format "Switch to backend [%s]: " def)
3050 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
3051 nil t nil nil (downcase (symbol-name def))))))
3052 (t def))))))
3053 (unless (eq backend (vc-backend file))
3054 (vc-file-clearprops file)
3055 (vc-file-setprop file 'vc-backend backend)
3056 ;; Force recomputation of the state
3057 (unless (vc-call-backend backend 'registered file)
3058 (vc-file-clearprops file)
3059 (error "%s is not registered in %s" file backend))
3060 (vc-mode-line file)))
3062 ;;;###autoload
3063 (defun vc-transfer-file (file new-backend)
3064 "Transfer FILE to another version control system NEW-BACKEND.
3065 If NEW-BACKEND has a higher precedence than FILE's current backend
3066 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
3067 NEW-BACKEND, using the revision number from the current backend as the
3068 base level. If NEW-BACKEND has a lower precedence than the current
3069 backend, then commit all changes that were made under the current
3070 backend to NEW-BACKEND, and unregister FILE from the current backend.
3071 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
3072 (let* ((old-backend (vc-backend file))
3073 (edited (memq (vc-state file) '(edited needs-merge)))
3074 (registered (vc-call-backend new-backend 'registered file))
3075 (move
3076 (and registered ; Never move if not registered in new-backend yet.
3077 ;; move if new-backend comes later in vc-handled-backends
3078 (or (memq new-backend (memq old-backend vc-handled-backends))
3079 (y-or-n-p "Final transfer? "))))
3080 (comment nil))
3081 (if (eq old-backend new-backend)
3082 (error "%s is the current backend of %s" new-backend file))
3083 (if registered
3084 (set-file-modes file (logior (file-modes file) 128))
3085 ;; `registered' might have switched under us.
3086 (vc-switch-backend file old-backend)
3087 (let* ((rev (vc-working-revision file))
3088 (modified-file (and edited (make-temp-file file)))
3089 (unmodified-file (and modified-file (vc-version-backup-file file))))
3090 ;; Go back to the base unmodified file.
3091 (unwind-protect
3092 (progn
3093 (when modified-file
3094 (copy-file file modified-file 'ok-if-already-exists)
3095 ;; If we have a local copy of the unmodified file, handle that
3096 ;; here and not in vc-revert-file because we don't want to
3097 ;; delete that copy -- it is still useful for OLD-BACKEND.
3098 (if unmodified-file
3099 (copy-file unmodified-file file
3100 'ok-if-already-exists 'keep-date)
3101 (if (y-or-n-p "Get base revision from master? ")
3102 (vc-revert-file file))))
3103 (vc-call-backend new-backend 'receive-file file rev))
3104 (when modified-file
3105 (vc-switch-backend file new-backend)
3106 (unless (eq (vc-checkout-model file) 'implicit)
3107 (vc-checkout file t nil))
3108 (rename-file modified-file file 'ok-if-already-exists)
3109 (vc-file-setprop file 'vc-checkout-time nil)))))
3110 (when move
3111 (vc-switch-backend file old-backend)
3112 (setq comment (vc-call comment-history file))
3113 (vc-call unregister file))
3114 (vc-switch-backend file new-backend)
3115 (when (or move edited)
3116 (vc-file-setprop file 'vc-state 'edited)
3117 (vc-mode-line file)
3118 (vc-checkin file nil comment (stringp comment)))))
3120 (defun vc-rename-master (oldmaster newfile templates)
3121 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
3122 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
3123 (newdir (or (file-name-directory newfile) ""))
3124 (newbase (file-name-nondirectory newfile))
3125 (masters
3126 ;; List of potential master files for `newfile'
3127 (mapcar
3128 (lambda (s) (vc-possible-master s newdir newbase))
3129 templates)))
3130 (if (or (file-symlink-p oldmaster)
3131 (file-symlink-p (file-name-directory oldmaster)))
3132 (error "This is unsafe in the presence of symbolic links"))
3133 (rename-file
3134 oldmaster
3135 (catch 'found
3136 ;; If possible, keep the master file in the same directory.
3137 (dolist (f masters)
3138 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
3139 (throw 'found f)))
3140 ;; If not, just use the first possible place.
3141 (dolist (f masters)
3142 (and f (or (not (setq dir (file-name-directory f)))
3143 (file-directory-p dir))
3144 (throw 'found f)))
3145 (error "New file lacks a version control directory")))))
3147 (defun vc-delete-file (file)
3148 "Delete file and mark it as such in the version control system."
3149 (interactive "fVC delete file: ")
3150 (let ((buf (get-file-buffer file))
3151 (backend (vc-backend file)))
3152 (unless backend
3153 (error "File %s is not under version control"
3154 (file-name-nondirectory file)))
3155 (unless (vc-find-backend-function backend 'delete-file)
3156 (error "Deleting files under %s is not supported in VC" backend))
3157 (if (and buf (buffer-modified-p buf))
3158 (error "Please save files before deleting them"))
3159 (unless (y-or-n-p (format "Really want to delete %s? "
3160 (file-name-nondirectory file)))
3161 (error "Abort!"))
3162 (unless (or (file-directory-p file) (null make-backup-files))
3163 (with-current-buffer (or buf (find-file-noselect file))
3164 (let ((backup-inhibited nil))
3165 (backup-buffer))))
3166 (vc-call delete-file file)
3167 ;; If the backend hasn't deleted the file itself, let's do it for him.
3168 (if (file-exists-p file) (delete-file file))))
3170 ;;;###autoload
3171 (defun vc-rename-file (old new)
3172 "Rename file OLD to NEW, and rename its master file likewise."
3173 (interactive "fVC rename file: \nFRename to: ")
3174 (let ((oldbuf (get-file-buffer old)))
3175 (if (and oldbuf (buffer-modified-p oldbuf))
3176 (error "Please save files before moving them"))
3177 (if (get-file-buffer new)
3178 (error "Already editing new file name"))
3179 (if (file-exists-p new)
3180 (error "New file already exists"))
3181 (let ((state (vc-state old)))
3182 (unless (memq state '(up-to-date edited))
3183 (error "Please %s files before moving them"
3184 (if (stringp state) "check in" "update"))))
3185 (vc-call rename-file old new)
3186 (vc-file-clearprops old)
3187 ;; Move the actual file (unless the backend did it already)
3188 (if (file-exists-p old) (rename-file old new))
3189 ;; ?? Renaming a file might change its contents due to keyword expansion.
3190 ;; We should really check out a new copy if the old copy was precisely equal
3191 ;; to some checked-in revision. However, testing for this is tricky....
3192 (if oldbuf
3193 (with-current-buffer oldbuf
3194 (let ((buffer-read-only buffer-read-only))
3195 (set-visited-file-name new))
3196 (vc-backend new)
3197 (vc-mode-line new)
3198 (set-buffer-modified-p nil)))))
3200 ;;;###autoload
3201 (defun vc-update-change-log (&rest args)
3202 "Find change log file and add entries from recent version control logs.
3203 Normally, find log entries for all registered files in the default
3204 directory.
3206 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
3208 With any numeric prefix arg, find log entries for all currently visited
3209 files that are under version control. This puts all the entries in the
3210 log for the default directory, which may not be appropriate.
3212 From a program, any ARGS are assumed to be filenames for which
3213 log entries should be gathered."
3214 (interactive
3215 (cond ((consp current-prefix-arg) ;C-u
3216 (list buffer-file-name))
3217 (current-prefix-arg ;Numeric argument.
3218 (let ((files nil)
3219 (buffers (buffer-list))
3220 file)
3221 (while buffers
3222 (setq file (buffer-file-name (car buffers)))
3223 (and file (vc-backend file)
3224 (setq files (cons file files)))
3225 (setq buffers (cdr buffers)))
3226 files))
3228 ;; Don't supply any filenames to backend; this means
3229 ;; it should find all relevant files relative to
3230 ;; the default-directory.
3231 nil)))
3232 (dolist (file (or args (list default-directory)))
3233 (if (eq (string-match tramp-file-name-regexp file) 0)
3234 (error "Sorry, vc-update-change-log does not work over Tramp")))
3235 (vc-call-backend (vc-responsible-backend default-directory)
3236 'update-changelog args))
3238 ;;; The default back end. Assumes RCS-like revision numbering.
3240 (defun vc-default-revision-granularity ()
3241 (error "Your backend will not work with this version of VC mode."))
3243 ;; functions that operate on RCS revision numbers. This code should
3244 ;; also be moved into the backends. It stays for now, however, since
3245 ;; it is used in code below.
3246 ;;;###autoload
3247 (defun vc-trunk-p (rev)
3248 "Return t if REV is a revision on the trunk."
3249 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
3251 (defun vc-branch-p (rev)
3252 "Return t if REV is a branch revision."
3253 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
3255 ;;;###autoload
3256 (defun vc-branch-part (rev)
3257 "Return the branch part of a revision number REV."
3258 (let ((index (string-match "\\.[0-9]+\\'" rev)))
3259 (if index
3260 (substring rev 0 index))))
3262 (defun vc-minor-part (rev)
3263 "Return the minor revision number of a revision number REV."
3264 (string-match "[0-9]+\\'" rev)
3265 (substring rev (match-beginning 0) (match-end 0)))
3267 (defun vc-default-previous-revision (backend file rev)
3268 "Return the revision number immediately preceding REV for FILE,
3269 or nil if there is no previous revision. This default
3270 implementation works for MAJOR.MINOR-style revision numbers as
3271 used by RCS and CVS."
3272 (let ((branch (vc-branch-part rev))
3273 (minor-num (string-to-number (vc-minor-part rev))))
3274 (when branch
3275 (if (> minor-num 1)
3276 ;; revision does probably not start a branch or release
3277 (concat branch "." (number-to-string (1- minor-num)))
3278 (if (vc-trunk-p rev)
3279 ;; we are at the beginning of the trunk --
3280 ;; don't know anything to return here
3282 ;; we are at the beginning of a branch --
3283 ;; return revision of starting point
3284 (vc-branch-part branch))))))
3286 (defun vc-default-next-revision (backend file rev)
3287 "Return the revision number immediately following REV for FILE,
3288 or nil if there is no next revision. This default implementation
3289 works for MAJOR.MINOR-style revision numbers as used by RCS
3290 and CVS."
3291 (when (not (string= rev (vc-working-revision file)))
3292 (let ((branch (vc-branch-part rev))
3293 (minor-num (string-to-number (vc-minor-part rev))))
3294 (concat branch "." (number-to-string (1+ minor-num))))))
3296 (defun vc-default-responsible-p (backend file)
3297 "Indicate whether BACKEND is reponsible for FILE.
3298 The default is to return nil always."
3299 nil)
3301 (defun vc-default-could-register (backend file)
3302 "Return non-nil if BACKEND could be used to register FILE.
3303 The default implementation returns t for all files."
3306 (defun vc-default-latest-on-branch-p (backend file)
3307 "Return non-nil if FILE is the latest on its branch.
3308 This default implementation always returns non-nil, which means that
3309 editing non-current revisions is not supported by default."
3312 (defun vc-default-init-revision (backend) vc-default-init-revision)
3314 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
3315 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
3316 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
3317 ;; vc-cvs.el by code using cvs2cl.
3318 (defun vc-update-changelog-rcs2log (files)
3319 "Default implementation of update-changelog.
3320 Uses `rcs2log' which only works for RCS and CVS."
3321 ;; FIXME: We (c|sh)ould add support for cvs2cl
3322 (let ((odefault default-directory)
3323 (changelog (find-change-log))
3324 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
3325 (tempfile (make-temp-file
3326 (expand-file-name "vc"
3327 (or small-temporary-file-directory
3328 temporary-file-directory))))
3329 (login-name (or user-login-name
3330 (format "uid%d" (number-to-string (user-uid)))))
3331 (full-name (or add-log-full-name
3332 (user-full-name)
3333 (user-login-name)
3334 (format "uid%d" (number-to-string (user-uid)))))
3335 (mailing-address (or add-log-mailing-address
3336 user-mail-address)))
3337 (find-file-other-window changelog)
3338 (barf-if-buffer-read-only)
3339 (vc-buffer-sync)
3340 (undo-boundary)
3341 (goto-char (point-min))
3342 (push-mark)
3343 (message "Computing change log entries...")
3344 (message "Computing change log entries... %s"
3345 (unwind-protect
3346 (progn
3347 (setq default-directory odefault)
3348 (if (eq 0 (apply 'call-process
3349 (expand-file-name "rcs2log"
3350 exec-directory)
3351 nil (list t tempfile) nil
3352 "-c" changelog
3353 "-u" (concat login-name
3354 "\t" full-name
3355 "\t" mailing-address)
3356 (mapcar
3357 (lambda (f)
3358 (file-relative-name
3359 (expand-file-name f odefault)))
3360 files)))
3361 "done"
3362 (pop-to-buffer (get-buffer-create "*vc*"))
3363 (erase-buffer)
3364 (insert-file-contents tempfile)
3365 "failed"))
3366 (setq default-directory (file-name-directory changelog))
3367 (delete-file tempfile)))))
3369 (defun vc-default-find-revision (backend file rev buffer)
3370 "Provide the new `find-revision' op based on the old `checkout' op.
3371 This is only for compatibility with old backends. They should be updated
3372 to provide the `find-revision' operation instead."
3373 (let ((tmpfile (make-temp-file (expand-file-name file))))
3374 (unwind-protect
3375 (progn
3376 (vc-call-backend backend 'checkout file nil rev tmpfile)
3377 (with-current-buffer buffer
3378 (insert-file-contents-literally tmpfile)))
3379 (delete-file tmpfile))))
3381 (defun vc-default-dired-state-info (backend file)
3382 (let* ((state (vc-state file))
3383 (statestring
3384 (cond
3385 ((stringp state) (concat "(" state ")"))
3386 ((eq state 'edited) (concat "(" (vc-user-login-name file) ")"))
3387 ((eq state 'needs-merge) "(merge)")
3388 ((eq state 'needs-patch) "(patch)")
3389 ((eq state 'added) "(added)")
3390 ((eq state 'removed) "(removed)")
3391 ((eq state 'ignored) "(ignored)") ;; dired-hook filters this out
3392 ((eq state 'unregistered) "?")
3393 ((eq state 'unlocked-changes) "(stale)")
3394 ((not state) "(unknown)")))
3395 (buffer
3396 (get-file-buffer file))
3397 (modflag
3398 (if (and buffer (buffer-modified-p buffer)) "+" "")))
3399 (concat statestring modflag)))
3401 (defun vc-default-rename-file (backend old new)
3402 (condition-case nil
3403 (add-name-to-file old new)
3404 (error (rename-file old new)))
3405 (vc-delete-file old)
3406 (with-current-buffer (find-file-noselect new)
3407 (vc-register)))
3409 (defalias 'vc-default-logentry-check 'ignore)
3410 (defalias 'vc-default-check-headers 'ignore)
3412 (defun vc-default-log-view-mode (backend) (log-view-mode))
3414 (defun vc-default-show-log-entry (backend rev)
3415 (with-no-warnings
3416 (log-view-goto-rev rev)))
3418 (defun vc-default-comment-history (backend file)
3419 "Return a string with all log entries stored in BACKEND for FILE."
3420 (if (vc-find-backend-function backend 'print-log)
3421 (with-current-buffer "*vc*"
3422 (vc-call print-log (list file))
3423 (vc-call-backend backend 'wash-log)
3424 (buffer-string))))
3426 (defun vc-default-receive-file (backend file rev)
3427 "Let BACKEND receive FILE from another version control system."
3428 (vc-call-backend backend 'register file rev ""))
3430 (defun vc-default-create-snapshot (backend dir name branchp)
3431 (when branchp
3432 (error "VC backend %s does not support module branches" backend))
3433 (let ((result (vc-snapshot-precondition dir)))
3434 (if (stringp result)
3435 (error "File %s is not up-to-date" result)
3436 (vc-file-tree-walk
3438 (lambda (f)
3439 (vc-call assign-name f name))))))
3441 (defun vc-default-retrieve-snapshot (backend dir name update)
3442 (if (string= name "")
3443 (progn
3444 (vc-file-tree-walk
3446 (lambda (f) (and
3447 (vc-up-to-date-p f)
3448 (vc-error-occurred
3449 (vc-call checkout f nil "")
3450 (if update (vc-resynch-buffer f t t)))))))
3451 (let ((result (vc-snapshot-precondition dir)))
3452 (if (stringp result)
3453 (error "File %s is locked" result)
3454 (setq update (and (eq result 'visited) update))
3455 (vc-file-tree-walk
3457 (lambda (f) (vc-error-occurred
3458 (vc-call checkout f nil name)
3459 (if update (vc-resynch-buffer f t t)))))))))
3461 (defun vc-default-revert (backend file contents-done)
3462 (unless contents-done
3463 (let ((rev (vc-working-revision file))
3464 (file-buffer (or (get-file-buffer file) (current-buffer))))
3465 (message "Checking out %s..." file)
3466 (let ((failed t)
3467 (backup-name (car (find-backup-file-name file))))
3468 (when backup-name
3469 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
3470 (unless (file-writable-p file)
3471 (set-file-modes file (logior (file-modes file) 128))))
3472 (unwind-protect
3473 (let ((coding-system-for-read 'no-conversion)
3474 (coding-system-for-write 'no-conversion))
3475 (with-temp-file file
3476 (let ((outbuf (current-buffer)))
3477 ;; Change buffer to get local value of vc-checkout-switches.
3478 (with-current-buffer file-buffer
3479 (let ((default-directory (file-name-directory file)))
3480 (vc-call find-revision file rev outbuf)))))
3481 (setq failed nil))
3482 (when backup-name
3483 (if failed
3484 (rename-file backup-name file 'ok-if-already-exists)
3485 (and (not vc-make-backup-files) (delete-file backup-name))))))
3486 (message "Checking out %s...done" file))))
3488 (defalias 'vc-default-revision-completion-table 'ignore)
3490 (defun vc-check-headers ()
3491 "Check if the current file has any headers in it."
3492 (interactive)
3493 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3495 ;;; Annotate functionality
3497 ;; Declare globally instead of additional parameter to
3498 ;; temp-buffer-show-function (not possible to pass more than one
3499 ;; parameter). The use of annotate-ratio is deprecated in favor of
3500 ;; annotate-mode, which replaces it with the more sensible "span-to
3501 ;; days", along with autoscaling support.
3502 (defvar vc-annotate-ratio nil "Global variable.")
3504 ;; internal buffer-local variables
3505 (defvar vc-annotate-backend nil)
3506 (defvar vc-annotate-parent-file nil)
3507 (defvar vc-annotate-parent-rev nil)
3508 (defvar vc-annotate-parent-display-mode nil)
3510 (defconst vc-annotate-font-lock-keywords
3511 ;; The fontification is done by vc-annotate-lines instead of font-lock.
3512 '((vc-annotate-lines)))
3514 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
3515 "Major mode for output buffers of the `vc-annotate' command.
3517 You can use the mode-specific menu to alter the time-span of the used
3518 colors. See variable `vc-annotate-menu-elements' for customizing the
3519 menu items."
3520 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
3521 ;; it will become a list, to avoid initial annotations being invisible.
3522 (add-to-invisibility-spec 'foo)
3523 (remove-from-invisibility-spec 'foo)
3524 (set (make-local-variable 'truncate-lines) t)
3525 (set (make-local-variable 'font-lock-defaults)
3526 '(vc-annotate-font-lock-keywords t))
3527 (view-mode 1))
3529 (defun vc-annotate-toggle-annotation-visibility ()
3530 "Toggle whether or not the annotation is visible."
3531 (interactive)
3532 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
3533 'remove-from-invisibility-spec
3534 'add-to-invisibility-spec)
3535 'vc-annotate-annotation)
3536 (force-window-update (current-buffer)))
3538 (defun vc-annotate-display-default (ratio)
3539 "Display the output of \\[vc-annotate] using the default color range.
3540 The color range is given by `vc-annotate-color-map', scaled by RATIO.
3541 The current time is used as the offset."
3542 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
3543 (message "Redisplaying annotation...")
3544 (vc-annotate-display ratio)
3545 (message "Redisplaying annotation...done"))
3547 (defun vc-annotate-oldest-in-map (color-map)
3548 "Return the oldest time in the COLOR-MAP."
3549 ;; Since entries should be sorted, we can just use the last one.
3550 (caar (last color-map)))
3552 (defun vc-annotate-get-time-set-line-props ()
3553 (let ((bol (point))
3554 (date (vc-call-backend vc-annotate-backend 'annotate-time))
3555 (inhibit-read-only t))
3556 (assert (>= (point) bol))
3557 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
3558 date))
3560 (defun vc-annotate-display-autoscale (&optional full)
3561 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
3562 Autoscaling means that the map is scaled from the current time to the
3563 oldest annotation in the buffer, or, with prefix argument FULL, to
3564 cover the range from the oldest annotation to the newest."
3565 (interactive "P")
3566 (let ((newest 0.0)
3567 (oldest 999999.) ;Any CVS users at the founding of Rome?
3568 (current (vc-annotate-convert-time (current-time)))
3569 date)
3570 (message "Redisplaying annotation...")
3571 ;; Run through this file and find the oldest and newest dates annotated.
3572 (save-excursion
3573 (goto-char (point-min))
3574 (while (not (eobp))
3575 (when (setq date (vc-annotate-get-time-set-line-props))
3576 (if (> date newest)
3577 (setq newest date))
3578 (if (< date oldest)
3579 (setq oldest date)))
3580 (forward-line 1)))
3581 (vc-annotate-display
3582 (/ (- (if full newest current) oldest)
3583 (vc-annotate-oldest-in-map vc-annotate-color-map))
3584 (if full newest))
3585 (message "Redisplaying annotation...done \(%s\)"
3586 (if full
3587 (format "Spanned from %.1f to %.1f days old"
3588 (- current oldest)
3589 (- current newest))
3590 (format "Spanned to %.1f days old" (- current oldest))))))
3592 ;; Menu -- Using easymenu.el
3593 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
3594 "VC Annotate Display Menu"
3595 `("VC-Annotate"
3596 ["By Color Map Range" (unless (null vc-annotate-display-mode)
3597 (setq vc-annotate-display-mode nil)
3598 (vc-annotate-display-select))
3599 :style toggle :selected (null vc-annotate-display-mode)]
3600 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
3601 (mapcar (lambda (element)
3602 (let ((days (* element oldest-in-map)))
3603 `[,(format "Span %.1f days" days)
3604 (vc-annotate-display-select nil ,days)
3605 :style toggle :selected
3606 (eql vc-annotate-display-mode ,days) ]))
3607 vc-annotate-menu-elements))
3608 ["Span ..."
3609 (vc-annotate-display-select
3610 nil (float (string-to-number (read-string "Span how many days? "))))]
3611 "--"
3612 ["Span to Oldest"
3613 (unless (eq vc-annotate-display-mode 'scale)
3614 (vc-annotate-display-select nil 'scale))
3615 :style toggle :selected
3616 (eq vc-annotate-display-mode 'scale)]
3617 ["Span Oldest->Newest"
3618 (unless (eq vc-annotate-display-mode 'fullscale)
3619 (vc-annotate-display-select nil 'fullscale))
3620 :style toggle :selected
3621 (eq vc-annotate-display-mode 'fullscale)]
3622 "--"
3623 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility]
3624 ["Annotate previous revision" vc-annotate-prev-revision]
3625 ["Annotate next revision" vc-annotate-next-revision]
3626 ["Annotate revision at line" vc-annotate-revision-at-line]
3627 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line]
3628 ["Annotate latest revision" vc-annotate-working-revision]
3629 ["Show log of revision at line" vc-annotate-show-log-revision-at-line]
3630 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line]))
3632 (defun vc-annotate-display-select (&optional buffer mode)
3633 "Highlight the output of \\[vc-annotate].
3634 By default, the current buffer is highlighted, unless overridden by
3635 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
3636 use; you may override this using the second optional arg MODE."
3637 (interactive)
3638 (if mode (setq vc-annotate-display-mode mode))
3639 (pop-to-buffer (or buffer (current-buffer)))
3640 (cond ((null vc-annotate-display-mode)
3641 ;; The ratio is global, thus relative to the global color-map.
3642 (kill-local-variable 'vc-annotate-color-map)
3643 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
3644 ;; One of the auto-scaling modes
3645 ((eq vc-annotate-display-mode 'scale)
3646 (vc-exec-after `(vc-annotate-display-autoscale)))
3647 ((eq vc-annotate-display-mode 'fullscale)
3648 (vc-exec-after `(vc-annotate-display-autoscale t)))
3649 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3650 (vc-annotate-display-default
3651 (/ vc-annotate-display-mode
3652 (vc-annotate-oldest-in-map vc-annotate-color-map))))
3653 (t (error "No such display mode: %s"
3654 vc-annotate-display-mode))))
3656 ;;;###autoload
3657 (defun vc-annotate (file rev &optional display-mode buf)
3658 "Display the edit history of the current file using colors.
3660 This command creates a buffer that shows, for each line of the current
3661 file, when it was last edited and by whom. Additionally, colors are
3662 used to show the age of each line--blue means oldest, red means
3663 youngest, and intermediate colors indicate intermediate ages. By
3664 default, the time scale stretches back one year into the past;
3665 everything that is older than that is shown in blue.
3667 With a prefix argument, this command asks two questions in the
3668 minibuffer. First, you may enter a revision number; then the buffer
3669 displays and annotates that revision instead of the working revision
3670 \(type RET in the minibuffer to leave that default unchanged). Then,
3671 you are prompted for the time span in days which the color range
3672 should cover. For example, a time span of 20 days means that changes
3673 over the past 20 days are shown in red to blue, according to their
3674 age, and everything that is older than that is shown in blue.
3676 Customization variables:
3678 `vc-annotate-menu-elements' customizes the menu elements of the
3679 mode-specific menu. `vc-annotate-color-map' and
3680 `vc-annotate-very-old-color' define the mapping of time to colors.
3681 `vc-annotate-background' specifies the background color."
3682 (interactive
3683 (save-current-buffer
3684 (vc-ensure-vc-buffer)
3685 (list buffer-file-name
3686 (let ((def (vc-working-revision buffer-file-name)))
3687 (if (null current-prefix-arg) def
3688 (read-string
3689 (format "Annotate from revision (default %s): " def)
3690 nil nil def)))
3691 (if (null current-prefix-arg)
3692 vc-annotate-display-mode
3693 (float (string-to-number
3694 (read-string "Annotate span days (default 20): "
3695 nil nil "20")))))))
3696 (vc-ensure-vc-buffer)
3697 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
3698 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
3699 (temp-buffer-show-function 'vc-annotate-display-select)
3700 ;; If BUF is specified, we presume the caller maintains current line,
3701 ;; so we don't need to do it here. This implementation may give
3702 ;; strange results occasionally in the case of REV != WORKFILE-REV.
3703 (current-line (unless buf (line-number-at-pos))))
3704 (message "Annotating...")
3705 ;; If BUF is specified it tells in which buffer we should put the
3706 ;; annotations. This is used when switching annotations to another
3707 ;; revision, so we should update the buffer's name.
3708 (if buf (with-current-buffer buf
3709 (rename-buffer temp-buffer-name t)
3710 ;; In case it had to be uniquified.
3711 (setq temp-buffer-name (buffer-name))))
3712 (with-output-to-temp-buffer temp-buffer-name
3713 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
3714 ;; we must setup the mode first, and then set our local
3715 ;; variables before the show-function is called at the exit of
3716 ;; with-output-to-temp-buffer
3717 (with-current-buffer temp-buffer-name
3718 (if (not (equal major-mode 'vc-annotate-mode))
3719 (vc-annotate-mode))
3720 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
3721 (set (make-local-variable 'vc-annotate-parent-file) file)
3722 (set (make-local-variable 'vc-annotate-parent-rev) rev)
3723 (set (make-local-variable 'vc-annotate-parent-display-mode)
3724 display-mode)))
3726 (with-current-buffer temp-buffer-name
3727 (vc-exec-after
3728 `(progn
3729 ;; Ideally, we'd rather not move point if the user has already
3730 ;; moved it elsewhere, but really point here is not the position
3731 ;; of the user's cursor :-(
3732 (when ,current-line ;(and (bobp))
3733 (goto-line ,current-line)
3734 (setq vc-sentinel-movepoint (point)))
3735 (unless (active-minibuffer-window)
3736 (message "Annotating... done")))))))
3738 (defun vc-annotate-prev-revision (prefix)
3739 "Visit the annotation of the revision previous to this one.
3741 With a numeric prefix argument, annotate the revision that many
3742 revisions previous."
3743 (interactive "p")
3744 (vc-annotate-warp-revision (- 0 prefix)))
3746 (defun vc-annotate-next-revision (prefix)
3747 "Visit the annotation of the revision after this one.
3749 With a numeric prefix argument, annotate the revision that many
3750 revisions after."
3751 (interactive "p")
3752 (vc-annotate-warp-revision prefix))
3754 (defun vc-annotate-working-revision ()
3755 "Visit the annotation of the working revision of this file."
3756 (interactive)
3757 (if (not (equal major-mode 'vc-annotate-mode))
3758 (message "Cannot be invoked outside of a vc annotate buffer")
3759 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
3760 (if (equal warp-rev vc-annotate-parent-rev)
3761 (message "Already at revision %s" warp-rev)
3762 (vc-annotate-warp-revision warp-rev)))))
3764 (defun vc-annotate-extract-revision-at-line ()
3765 "Extract the revision number of the current line."
3766 ;; This function must be invoked from a buffer in vc-annotate-mode
3767 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
3769 (defun vc-annotate-revision-at-line ()
3770 "Visit the annotation of the revision identified in the current line."
3771 (interactive)
3772 (if (not (equal major-mode 'vc-annotate-mode))
3773 (message "Cannot be invoked outside of a vc annotate buffer")
3774 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3775 (if (not rev-at-line)
3776 (message "Cannot extract revision number from the current line")
3777 (if (equal rev-at-line vc-annotate-parent-rev)
3778 (message "Already at revision %s" rev-at-line)
3779 (vc-annotate-warp-revision rev-at-line))))))
3781 (defun vc-annotate-revision-previous-to-line ()
3782 "Visit the annotation of the revision before the revision at line."
3783 (interactive)
3784 (if (not (equal major-mode 'vc-annotate-mode))
3785 (message "Cannot be invoked outside of a vc annotate buffer")
3786 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3787 (prev-rev nil))
3788 (if (not rev-at-line)
3789 (message "Cannot extract revision number from the current line")
3790 (setq prev-rev
3791 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3792 (vc-annotate-warp-revision prev-rev)))))
3794 (defun vc-annotate-show-log-revision-at-line ()
3795 "Visit the log of the revision at line."
3796 (interactive)
3797 (if (not (equal major-mode 'vc-annotate-mode))
3798 (message "Cannot be invoked outside of a vc annotate buffer")
3799 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3800 (if (not rev-at-line)
3801 (message "Cannot extract revision number from the current line")
3802 (vc-print-log rev-at-line)))))
3804 (defun vc-annotate-show-diff-revision-at-line ()
3805 "Visit the diff of the revision at line from its previous revision."
3806 (interactive)
3807 (if (not (equal major-mode 'vc-annotate-mode))
3808 (message "Cannot be invoked outside of a vc annotate buffer")
3809 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3810 (prev-rev nil))
3811 (if (not rev-at-line)
3812 (message "Cannot extract revision number from the current line")
3813 (setq prev-rev
3814 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3815 (if (not prev-rev)
3816 (message "Cannot diff from any revision prior to %s" rev-at-line)
3817 (save-window-excursion
3818 (vc-diff-internal nil (list vc-annotate-parent-file)
3819 prev-rev rev-at-line))
3820 (switch-to-buffer "*vc-diff*"))))))
3822 (defun vc-annotate-warp-revision (revspec)
3823 "Annotate the revision described by REVSPEC.
3825 If REVSPEC is a positive integer, warp that many revisions
3826 forward, if possible, otherwise echo a warning message. If
3827 REVSPEC is a negative integer, warp that many revisions backward,
3828 if possible, otherwise echo a warning message. If REVSPEC is a
3829 string, then it describes a revision number, so warp to that
3830 revision."
3831 (if (not (equal major-mode 'vc-annotate-mode))
3832 (message "Cannot be invoked outside of a vc annotate buffer")
3833 (let* ((buf (current-buffer))
3834 (oldline (line-number-at-pos))
3835 (revspeccopy revspec)
3836 (newrev nil))
3837 (cond
3838 ((and (integerp revspec) (> revspec 0))
3839 (setq newrev vc-annotate-parent-rev)
3840 (while (and (> revspec 0) newrev)
3841 (setq newrev (vc-call next-revision
3842 vc-annotate-parent-file newrev))
3843 (setq revspec (1- revspec)))
3844 (if (not newrev)
3845 (message "Cannot increment %d revisions from revision %s"
3846 revspeccopy vc-annotate-parent-rev)))
3847 ((and (integerp revspec) (< revspec 0))
3848 (setq newrev vc-annotate-parent-rev)
3849 (while (and (< revspec 0) newrev)
3850 (setq newrev (vc-call previous-revision
3851 vc-annotate-parent-file newrev))
3852 (setq revspec (1+ revspec)))
3853 (if (not newrev)
3854 (message "Cannot decrement %d revisions from revision %s"
3855 (- 0 revspeccopy) vc-annotate-parent-rev)))
3856 ((stringp revspec) (setq newrev revspec))
3857 (t (error "Invalid argument to vc-annotate-warp-revision")))
3858 (when newrev
3859 (vc-annotate vc-annotate-parent-file newrev
3860 vc-annotate-parent-display-mode
3861 buf)
3862 (goto-line (min oldline (progn (goto-char (point-max))
3863 (forward-line -1)
3864 (line-number-at-pos))) buf)))))
3866 (defun vc-annotate-compcar (threshold a-list)
3867 "Test successive cons cells of A-LIST against THRESHOLD.
3868 Return the first cons cell with a car that is not less than THRESHOLD,
3869 nil if no such cell exists."
3870 (let ((i 1)
3871 (tmp-cons (car a-list)))
3872 (while (and tmp-cons (< (car tmp-cons) threshold))
3873 (setq tmp-cons (car (nthcdr i a-list)))
3874 (setq i (+ i 1)))
3875 tmp-cons)) ; Return the appropriate value
3877 (defun vc-annotate-convert-time (time)
3878 "Convert a time value to a floating-point number of days.
3879 The argument TIME is a list as returned by `current-time' or
3880 `encode-time', only the first two elements of that list are considered."
3881 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3883 (defun vc-annotate-difference (&optional offset)
3884 "Return the time span in days to the next annotation.
3885 This calls the backend function annotate-time, and returns the
3886 difference in days between the time returned and the current time,
3887 or OFFSET if present."
3888 (let ((next-time (vc-annotate-get-time-set-line-props)))
3889 (if next-time
3890 (- (or offset
3891 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3892 next-time))))
3894 (defun vc-default-annotate-current-time (backend)
3895 "Return the current time, encoded as fractional days."
3896 (vc-annotate-convert-time (current-time)))
3898 (defvar vc-annotate-offset nil)
3900 (defun vc-annotate-display (ratio &optional offset)
3901 "Highlight `vc-annotate' output in the current buffer.
3902 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
3903 The annotations are relative to the current time, unless overridden by OFFSET."
3904 (if (/= ratio 1.0)
3905 (set (make-local-variable 'vc-annotate-color-map)
3906 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
3907 vc-annotate-color-map)))
3908 (set (make-local-variable 'vc-annotate-offset) offset)
3909 (font-lock-mode 1))
3911 (defun vc-annotate-lines (limit)
3912 (while (< (point) limit)
3913 (let ((difference (vc-annotate-difference vc-annotate-offset))
3914 (start (point))
3915 (end (progn (forward-line 1) (point))))
3916 (when difference
3917 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3918 (cons nil vc-annotate-very-old-color)))
3919 ;; substring from index 1 to remove any leading `#' in the name
3920 (face-name (concat "vc-annotate-face-"
3921 (if (string-equal
3922 (substring (cdr color) 0 1) "#")
3923 (substring (cdr color) 1)
3924 (cdr color))))
3925 ;; Make the face if not done.
3926 (face (or (intern-soft face-name)
3927 (let ((tmp-face (make-face (intern face-name))))
3928 (set-face-foreground tmp-face (cdr color))
3929 (if vc-annotate-background
3930 (set-face-background tmp-face
3931 vc-annotate-background))
3932 tmp-face)))) ; Return the face
3933 (put-text-property start end 'face face)))))
3934 ;; Pretend to font-lock there were no matches.
3935 nil)
3938 ;; Set up key bindings for use while editing log messages
3940 (defun vc-log-edit (fileset)
3941 "Set up `log-edit' for use with VC on FILE."
3942 (setq default-directory
3943 (with-current-buffer vc-parent-buffer default-directory))
3944 (log-edit 'vc-finish-logentry
3946 `((log-edit-listfun . (lambda () ',fileset))
3947 (log-edit-diff-function . (lambda () (vc-diff nil)))))
3948 (set (make-local-variable 'vc-log-fileset) fileset)
3949 (make-local-variable 'vc-log-revision)
3950 (set-buffer-modified-p nil)
3951 (setq buffer-file-name nil))
3953 ;; These things should probably be generally available
3955 (defun vc-file-tree-walk (dirname func &rest args)
3956 "Walk recursively through DIRNAME.
3957 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3958 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3959 (message "Traversing directory %s...done" dirname))
3961 (defun vc-file-tree-walk-internal (file func args)
3962 (if (not (file-directory-p file))
3963 (if (vc-backend file) (apply func file args))
3964 (message "Traversing directory %s..." (abbreviate-file-name file))
3965 (let ((dir (file-name-as-directory file)))
3966 (mapcar
3967 (lambda (f) (or
3968 (string-equal f ".")
3969 (string-equal f "..")
3970 (member f vc-directory-exclusion-list)
3971 (let ((dirf (expand-file-name f dir)))
3973 (file-symlink-p dirf) ;; Avoid possible loops.
3974 (vc-file-tree-walk-internal dirf func args)))))
3975 (directory-files dir)))))
3977 (provide 'vc)
3979 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3981 ;; These may be useful to anyone who has to debug or extend the package.
3982 ;; (Note that this information corresponds to versions 5.x. Some of it
3983 ;; might have been invalidated by the additions to support branching
3984 ;; and RCS keyword lookup. AS, 1995/03/24)
3986 ;; A fundamental problem in VC is that there are time windows between
3987 ;; vc-next-action's computations of the file's version-control state and
3988 ;; the actions that change it. This is a window open to lossage in a
3989 ;; multi-user environment; someone else could nip in and change the state
3990 ;; of the master during it.
3992 ;; The performance problem is that rlog/prs calls are very expensive; we want
3993 ;; to avoid them as much as possible.
3995 ;; ANALYSIS:
3997 ;; The performance problem, it turns out, simplifies in practice to the
3998 ;; problem of making vc-state fast. The two other functions that call
3999 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
4000 ;; makes snapshots, the other deletes the calling user's last change in the
4001 ;; master.
4003 ;; The race condition implies that we have to either (a) lock the master
4004 ;; during the entire execution of vc-next-action, or (b) detect and
4005 ;; recover from errors resulting from dispatch on an out-of-date state.
4007 ;; Alternative (a) appears to be infeasible. The problem is that we can't
4008 ;; guarantee that the lock will ever be removed. Suppose a user starts a
4009 ;; checkin, the change message buffer pops up, and the user, having wandered
4010 ;; off to do something else, simply forgets about it?
4012 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
4013 ;; vc-state. Usually, if a file is registered, we can read its locked/
4014 ;; unlocked state and its current owner from its permissions.
4016 ;; This shortcut will fail if someone has manually changed the workfile's
4017 ;; permissions; also if developers are munging the workfile in several
4018 ;; directories, with symlinks to a master (in this latter case, the
4019 ;; permissions shortcut will fail to detect a lock asserted from another
4020 ;; directory).
4022 ;; Note that these cases correspond exactly to the errors which could happen
4023 ;; because of a competing checkin/checkout race in between two instances of
4024 ;; vc-next-action.
4026 ;; For VC's purposes, a workfile/master pair may have the following states:
4028 ;; A. Unregistered. There is a workfile, there is no master.
4030 ;; B. Registered and not locked by anyone.
4032 ;; C. Locked by calling user and unchanged.
4034 ;; D. Locked by the calling user and changed.
4036 ;; E. Locked by someone other than the calling user.
4038 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
4040 ;; VC's idea of state
4041 ;; |
4042 ;; V Actual state RCS action SCCS action Effect
4043 ;; A B C D E
4044 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
4045 ;; B 5 . 6 7 8 co -l get -e checkout
4046 ;; C 9 10 . 11 12 co -u unget; get revert
4047 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
4048 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
4050 ;; All commands take the master file name as a last argument (not shown).
4052 ;; In the discussion below, a "self-race" is a pathological situation in
4053 ;; which VC operations are being attempted simultaneously by two or more
4054 ;; Emacsen running under the same username.
4056 ;; The vc-next-action code has the following windows:
4058 ;; Window P:
4059 ;; Between the check for existence of a master file and the call to
4060 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
4061 ;; never close if the initial-comment feature is on.
4063 ;; Window Q:
4064 ;; Between the call to vc-workfile-unchanged-p in and the immediately
4065 ;; following revert (apparent state C).
4067 ;; Window R:
4068 ;; Between the call to vc-workfile-unchanged-p in and the following
4069 ;; checkin (apparent state D). This window may never close.
4071 ;; Window S:
4072 ;; Between the unlock and the immediately following checkout during a
4073 ;; revert operation (apparent state C). Included in window Q.
4075 ;; Window T:
4076 ;; Between vc-state and the following checkout (apparent state B).
4078 ;; Window U:
4079 ;; Between vc-state and the following revert (apparent state C).
4080 ;; Includes windows Q and S.
4082 ;; Window V:
4083 ;; Between vc-state and the following checkin (apparent state
4084 ;; D). This window may never be closed if the user fails to complete the
4085 ;; checkin message. Includes window R.
4087 ;; Window W:
4088 ;; Between vc-state and the following steal-lock (apparent
4089 ;; state E). This window may never close if the user fails to complete
4090 ;; the steal-lock message. Includes window X.
4092 ;; Window X:
4093 ;; Between the unlock and the immediately following re-lock during a
4094 ;; steal-lock operation (apparent state E). This window may never close
4095 ;; if the user fails to complete the steal-lock message.
4097 ;; Errors:
4099 ;; Apparent state A ---
4101 ;; 1. File looked unregistered but is actually registered and not locked.
4103 ;; Potential cause: someone else's admin during window P, with
4104 ;; caller's admin happening before their checkout.
4106 ;; RCS: Prior to version 5.6.4, ci fails with message
4107 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
4108 ;; ci -i option and the message is "<file>,v: already exists".
4109 ;; SCCS: admin will fail with error (ad19).
4111 ;; We can let these errors be passed up to the user.
4113 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
4115 ;; Potential cause: self-race during window P.
4117 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
4118 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
4119 ;; ci -i option, failing with message "<file>,v: already exists".
4120 ;; SCCS: will fail with error (ad19).
4122 ;; Either of these consequences is acceptable.
4124 ;; 3. File looked unregistered but is actually locked by caller, changed.
4126 ;; Potential cause: self-race during window P.
4128 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
4129 ;; a delta with a null change comment (the -t- switch will be
4130 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
4131 ;; failing with message "<file>,v: already exists".
4132 ;; SCCS: will fail with error (ad19).
4134 ;; 4. File looked unregistered but is locked by someone else.
4136 ;; Potential cause: someone else's admin during window P, with
4137 ;; caller's admin happening *after* their checkout.
4139 ;; RCS: Prior to version 5.6.4, ci fails with a
4140 ;; "no lock set by <user>" message. From 5.6.4 onwards,
4141 ;; VC uses the new ci -i option, failing with message
4142 ;; "<file>,v: already exists".
4143 ;; SCCS: will fail with error (ad19).
4145 ;; We can let these errors be passed up to the user.
4147 ;; Apparent state B ---
4149 ;; 5. File looked registered and not locked, but is actually unregistered.
4151 ;; Potential cause: master file got nuked during window P.
4153 ;; RCS: will fail with "RCS/<file>: No such file or directory"
4154 ;; SCCS: will fail with error ut4.
4156 ;; We can let these errors be passed up to the user.
4158 ;; 6. File looked registered and not locked, but is actually locked by the
4159 ;; calling user and unchanged.
4161 ;; Potential cause: self-race during window T.
4163 ;; RCS: in the same directory as the previous workfile, co -l will fail
4164 ;; with "co error: writable foo exists; checkout aborted". In any other
4165 ;; directory, checkout will succeed.
4166 ;; SCCS: will fail with ge17.
4168 ;; Either of these consequences is acceptable.
4170 ;; 7. File looked registered and not locked, but is actually locked by the
4171 ;; calling user and changed.
4173 ;; As case 6.
4175 ;; 8. File looked registered and not locked, but is actually locked by another
4176 ;; user.
4178 ;; Potential cause: someone else checks it out during window T.
4180 ;; RCS: co error: revision 1.3 already locked by <user>
4181 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
4183 ;; We can let these errors be passed up to the user.
4185 ;; Apparent state C ---
4187 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
4189 ;; As case 5.
4191 ;; 10. File looks locked by calling user and unchanged, but is actually not
4192 ;; locked.
4194 ;; Potential cause: a self-race in window U, or by the revert's
4195 ;; landing during window X of some other user's steal-lock or window S
4196 ;; of another user's revert.
4198 ;; RCS: succeeds, refreshing the file from the identical version in
4199 ;; the master.
4200 ;; SCCS: fails with error ut4 (p file nonexistent).
4202 ;; Either of these consequences is acceptable.
4204 ;; 11. File is locked by calling user. It looks unchanged, but is actually
4205 ;; changed.
4207 ;; Potential cause: the file would have to be touched by a self-race
4208 ;; during window Q.
4210 ;; The revert will succeed, removing whatever changes came with
4211 ;; the touch. It is theoretically possible that work could be lost.
4213 ;; 12. File looks like it's locked by the calling user and unchanged, but
4214 ;; it's actually locked by someone else.
4216 ;; Potential cause: a steal-lock in window V.
4218 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
4219 ;; SCCS: fails with error un2
4221 ;; We can pass these errors up to the user.
4223 ;; Apparent state D ---
4225 ;; 13. File looks like it's locked by the calling user and changed, but it's
4226 ;; actually unregistered.
4228 ;; Potential cause: master file got nuked during window P.
4230 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
4231 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
4232 ;; option, failing with message "no such file or directory".
4233 ;; SCCS: will fail with error ut4.
4235 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
4236 ;; VC may fail to detect the loss of previous version information.
4238 ;; 14. File looks like it's locked by the calling user and changed, but it's
4239 ;; actually unlocked.
4241 ;; Potential cause: self-race in window V, or the checkin happening
4242 ;; during the window X of someone else's steal-lock or window S of
4243 ;; someone else's revert.
4245 ;; RCS: ci will fail with "no lock set by <user>".
4246 ;; SCCS: delta will fail with error ut4.
4248 ;; 15. File looks like it's locked by the calling user and changed, but it's
4249 ;; actually locked by the calling user and unchanged.
4251 ;; Potential cause: another self-race --- a whole checkin/checkout
4252 ;; sequence by the calling user would have to land in window R.
4254 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
4255 ;; RCS: reverts to the file state as of the second user's checkin, leaving
4256 ;; the file unlocked.
4258 ;; It is theoretically possible that work could be lost under RCS.
4260 ;; 16. File looks like it's locked by the calling user and changed, but it's
4261 ;; actually locked by a different user.
4263 ;; RCS: ci error: no lock set by <user>
4264 ;; SCCS: unget will fail with error un2
4266 ;; We can pass these errors up to the user.
4268 ;; Apparent state E ---
4270 ;; 17. File looks like it's locked by some other user, but it's actually
4271 ;; unregistered.
4273 ;; As case 13.
4275 ;; 18. File looks like it's locked by some other user, but it's actually
4276 ;; unlocked.
4278 ;; Potential cause: someone released a lock during window W.
4280 ;; RCS: The calling user will get the lock on the file.
4281 ;; SCCS: unget -n will fail with cm4.
4283 ;; Either of these consequences will be OK.
4285 ;; 19. File looks like it's locked by some other user, but it's actually
4286 ;; locked by the calling user and unchanged.
4288 ;; Potential cause: the other user relinquishing a lock followed by
4289 ;; a self-race, both in window W.
4291 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
4292 ;; the sequence a no-op.
4294 ;; 20. File looks like it's locked by some other user, but it's actually
4295 ;; locked by the calling user and changed.
4297 ;; As case 19.
4299 ;; PROBLEM CASES:
4301 ;; In order of decreasing severity:
4303 ;; Cases 11 and 15 are the only ones that potentially lose work.
4304 ;; They would require a self-race for this to happen.
4306 ;; Case 13 in RCS loses information about previous deltas, retaining
4307 ;; only the information in the current workfile. This can only happen
4308 ;; if the master file gets nuked in window P.
4310 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
4311 ;; no change comment in the master. This would require a self-race in
4312 ;; window P or R respectively.
4314 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
4316 ;; Unfortunately, it appears to me that no recovery is possible in these
4317 ;; cases. They don't yield error messages, so there's no way to tell that
4318 ;; a race condition has occurred.
4320 ;; All other cases don't change either the workfile or the master, and
4321 ;; trigger command errors which the user will see.
4323 ;; Thus, there is no explicit recovery code.
4325 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
4326 ;;; vc.el ends here