(smerge-apply-resolution-patch): New fun.
[emacs.git] / lisp / vc.el
blobcdd4e2f3ff3edd01e68246b8f954ee61e47db0ad
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 ;; - dir-status (dir update-function status-buffer)
172 ;; Produce RESULT: a list of conses of the form (file . vc-state)
173 ;; for the files in DIR. If a command needs to be run to compute
174 ;; this list, it should be run asynchronously using (current-buffer)
175 ;; as the buffer for the command. When RESULT is computed, it should
176 ;; be passed back by doing: (funcall UPDATE-FUNCTION RESULT STATUS-BUFFER)
177 ;; This function is used by `vc-status', a replacement for `vc-dired'.
178 ;; vc-status is still under development, and is NOT feature
179 ;; complete. As such, the requirements for this function might
180 ;; change. This is a replacement for `dir-state'.
182 ;; - status-extra-headers (dir)
184 ;; Return a string that will be added to the *vc-status* buffer header.
186 ;; - status-printer (fileinfo)
188 ;; Pretty print the `vc-status-fileinfo' FILEINFO.
189 ;; If a backend needs to show more information than the default FILE
190 ;; and STATE in the vc-status listing, it can store that extra
191 ;; information in `vc-status-fileinfo->extra'. This function can be
192 ;; used to display that extra information in the vc-status buffer.
194 ;; * working-revision (file)
196 ;; Return the working revision of FILE. This is the revision fetched
197 ;; by the last checkout or upate, not necessarily the same thing as the
198 ;; head or tip revision. Should return "0" for a file added but not yet
199 ;; committed.
201 ;; - latest-on-branch-p (file)
203 ;; Return non-nil if the working revision of FILE is the latest revision
204 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
205 ;; The default implementation always returns t, which means that
206 ;; working with non-current revisions is not supported by default.
208 ;; * checkout-model (file)
210 ;; Indicate whether FILE needs to be "checked out" before it can be
211 ;; edited. See `vc-checkout-model' for a list of possible values.
213 ;; - workfile-unchanged-p (file)
215 ;; Return non-nil if FILE is unchanged from the working revision.
216 ;; This function should do a brief comparison of FILE's contents
217 ;; with those of the repository master of the working revision. If
218 ;; the backend does not have such a brief-comparison feature, the
219 ;; default implementation of this function can be used, which
220 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
221 ;; must not run asynchronously in this case, see variable
222 ;; `vc-disable-async-diff'.)
224 ;; - mode-line-string (file)
226 ;; If provided, this function should return the VC-specific mode
227 ;; line string for FILE. The returned string should have a
228 ;; `help-echo' property which is the text to be displayed as a
229 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
230 ;; The default implementation deals well with all states that
231 ;; `vc-state' can return.
233 ;; - dired-state-info (file)
235 ;; Translate the `vc-state' property of FILE into a string that can be
236 ;; used in a vc-dired buffer. The default implementation deals well
237 ;; with all states that `vc-state' can return.
239 ;; STATE-CHANGING FUNCTIONS
241 ;; * create-repo (backend)
243 ;; Create an empty repository in the current directory and initialize
244 ;; it so VC mode can add files to it. For file-oriented systems, this
245 ;; need do no more than create a subdirectory with the right name.
247 ;; * register (files &optional rev comment)
249 ;; Register FILES in this backend. Optionally, an initial revision REV
250 ;; and an initial description of the file, COMMENT, may be specified,
251 ;; but it is not guaranteed that the backend will do anything with this.
252 ;; The implementation should pass the value of vc-register-switches
253 ;; to the backend command. (Note: in older versions of VC, this
254 ;; command took a single file argument and not a list.)
256 ;; - init-revision (file)
258 ;; The initial revision to use when registering FILE if one is not
259 ;; specified by the user. If not provided, the variable
260 ;; vc-default-init-revision is used instead.
262 ;; - responsible-p (file)
264 ;; Return non-nil if this backend considers itself "responsible" for
265 ;; FILE, which can also be a directory. This function is used to find
266 ;; out what backend to use for registration of new files and for things
267 ;; like change log generation. The default implementation always
268 ;; returns nil.
270 ;; - could-register (file)
272 ;; Return non-nil if FILE could be registered under this backend. The
273 ;; default implementation always returns t.
275 ;; - receive-file (file rev)
277 ;; Let this backend "receive" a file that is already registered under
278 ;; another backend. The default implementation simply calls `register'
279 ;; for FILE, but it can be overridden to do something more specific,
280 ;; e.g. keep revision numbers consistent or choose editing modes for
281 ;; FILE that resemble those of the other backend.
283 ;; - unregister (file)
285 ;; Unregister FILE from this backend. This is only needed if this
286 ;; backend may be used as a "more local" backend for temporary editing.
288 ;; * checkin (files rev comment)
290 ;; Commit changes in FILES to this backend. If REV is non-nil, that
291 ;; should become the new revision number (not all backends do
292 ;; anything with it). COMMENT is used as a check-in comment. The
293 ;; implementation should pass the value of vc-checkin-switches to
294 ;; the backend command. (Note: in older versions of VC, this
295 ;; command took a single file argument and not a list.)
297 ;; * find-revision (file rev buffer)
299 ;; Fetch revision REV of file FILE and put it into BUFFER.
300 ;; If REV is the empty string, fetch the head of the trunk.
301 ;; The implementation should pass the value of vc-checkout-switches
302 ;; to the backend command.
304 ;; * checkout (file &optional editable rev)
306 ;; Check out revision REV of FILE into the working area. If EDITABLE
307 ;; is non-nil, FILE should be writable by the user and if locking is
308 ;; used for FILE, a lock should also be set. If REV is non-nil, that
309 ;; is the revision to check out (default is the working revision).
310 ;; If REV is t, that means to check out the head of the current branch;
311 ;; if it is the empty string, check out the head of the trunk.
312 ;; The implementation should pass the value of vc-checkout-switches
313 ;; to the backend command.
315 ;; * revert (file &optional contents-done)
317 ;; Revert FILE back to the working revision. If optional
318 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
319 ;; already been reverted from a version backup, and this function
320 ;; only needs to update the status of FILE within the backend.
322 ;; - rollback (files)
324 ;; Remove the tip revision of each of FILES from the repository. If
325 ;; this function is not provided, trying to cancel a revision is
326 ;; caught as an error. (Most backends don't provide it.) (Also
327 ;; note that older versions of this backend command were called
328 ;; 'cancel-version' and took a single file arg, not a list of
329 ;; files.)
331 ;; - merge (file rev1 rev2)
333 ;; Merge the changes between REV1 and REV2 into the current working file.
335 ;; - merge-news (file)
337 ;; Merge recent changes from the current branch into FILE.
339 ;; - steal-lock (file &optional revision)
341 ;; Steal any lock on the working revision of FILE, or on REVISION if
342 ;; that is provided. This function is only needed if locking is
343 ;; used for files under this backend, and if files can indeed be
344 ;; locked by other users.
346 ;; - modify-change-comment (files rev comment)
348 ;; Modify the change comments associated with the files at the
349 ;; given revision. This is optional, many backends do not support it.
351 ;; HISTORY FUNCTIONS
353 ;; * print-log (files &optional buffer)
355 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
356 ;; if BUFFER is nil. (Note: older versions of this function expected
357 ;; only a single file argument.)
359 ;; - log-view-mode ()
361 ;; Mode to use for the output of print-log. This defaults to
362 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
363 ;; mode of `log-view-mode'.
365 ;; - show-log-entry (revision)
367 ;; If provided, search the log entry for REVISION in the current buffer,
368 ;; and make sure it is displayed in the buffer's window. The default
369 ;; implementation of this function works for RCS-style logs.
371 ;; - wash-log (file)
373 ;; Remove all non-comment information from the output of print-log.
375 ;; - logentry-check ()
377 ;; If defined, this function is run to find out whether the user
378 ;; entered a valid log entry for check-in. The log entry is in the
379 ;; current buffer, and if it is not a valid one, the function should
380 ;; throw an error.
382 ;; - comment-history (file)
384 ;; Return a string containing all log entries that were made for FILE.
385 ;; This is used for transferring a file from one backend to another,
386 ;; retaining comment information. The default implementation of this
387 ;; function does this by calling print-log and then wash-log, and
388 ;; returning the resulting buffer contents as a string.
390 ;; - update-changelog (files)
392 ;; Using recent log entries, create ChangeLog entries for FILES, or for
393 ;; all files at or below the default-directory if FILES is nil. The
394 ;; default implementation runs rcs2log, which handles RCS- and
395 ;; CVS-style logs.
397 ;; * diff (files &optional rev1 rev2 buffer)
399 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
400 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
401 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
402 ;; found in the repository) as the older revision; if REV2 is nil,
403 ;; use the current working-copy contents as the newer revision. This
404 ;; function should pass the value of (vc-switches BACKEND 'diff) to
405 ;; the backend command. It should return a status of either 0 (no
406 ;; differences found), or 1 (either non-empty diff or the diff is
407 ;; run asynchronously).
409 ;; - revision-completion-table (files)
411 ;; Return a completion table for existing revisions of FILES.
412 ;; The default is to not use any completion table.
414 ;; - annotate-command (file buf &optional rev)
416 ;; If this function is provided, it should produce an annotated display
417 ;; of FILE in BUF, relative to revision REV. Annotation means each line
418 ;; of FILE displayed is prefixed with version information associated with
419 ;; its addition (deleted lines leave no history) and that the text of the
420 ;; file is fontified according to age.
422 ;; - annotate-time ()
424 ;; Only required if `annotate-command' is defined for the backend.
425 ;; Return the time of the next line of annotation at or after point,
426 ;; as a floating point fractional number of days. The helper
427 ;; function `vc-annotate-convert-time' may be useful for converting
428 ;; multi-part times as returned by `current-time' and `encode-time'
429 ;; to this format. Return nil if no more lines of annotation appear
430 ;; in the buffer. You can safely assume that point is placed at the
431 ;; beginning of each line, starting at `point-min'. The buffer that
432 ;; point is placed in is the Annotate output, as defined by the
433 ;; relevant backend. This function also affects how much of the line
434 ;; is fontified; where it leaves point is where fontification begins.
436 ;; - annotate-current-time ()
438 ;; Only required if `annotate-command' is defined for the backend,
439 ;; AND you'd like the current time considered to be anything besides
440 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
441 ;; time with hours, minutes, and seconds included. Probably safe to
442 ;; ignore. Return the current-time, in units of fractional days.
444 ;; - annotate-extract-revision-at-line ()
446 ;; Only required if `annotate-command' is defined for the backend.
447 ;; Invoked from a buffer in vc-annotate-mode, return the revision
448 ;; corresponding to the current line, or nil if there is no revision
449 ;; corresponding to the current line.
451 ;; SNAPSHOT SYSTEM
453 ;; - create-snapshot (dir name branchp)
455 ;; Take a snapshot of the current state of files under DIR and name it
456 ;; NAME. This should make sure that files are up-to-date before
457 ;; proceeding with the action. DIR can also be a file and if BRANCHP
458 ;; is specified, NAME should be created as a branch and DIR should be
459 ;; checked out under this new branch. The default implementation does
460 ;; not support branches but does a sanity check, a tree traversal and
461 ;; for each file calls `assign-name'.
463 ;; - assign-name (file name)
465 ;; Give name NAME to the working revision of FILE, assuming it is
466 ;; up-to-date. Only used by the default version of `create-snapshot'.
468 ;; - retrieve-snapshot (dir name update)
470 ;; Retrieve a named snapshot of all registered files at or below DIR.
471 ;; If UPDATE is non-nil, then update buffers of any files in the
472 ;; snapshot that are currently visited. The default implementation
473 ;; does a sanity check whether there aren't any uncommitted changes at
474 ;; or below DIR, and then performs a tree walk, using the `checkout'
475 ;; function to retrieve the corresponding revisions.
477 ;; MISCELLANEOUS
479 ;; - root (dir)
481 ;; Return DIR's "root" directory, that is, a parent directory of
482 ;; DIR for which the same backend as used for DIR applies. If no
483 ;; such parent exists, this function should return DIR.
485 ;; - make-version-backups-p (file)
487 ;; Return non-nil if unmodified repository revisions of FILE should be
488 ;; backed up locally. If this is done, VC can perform `diff' and
489 ;; `revert' operations itself, without calling the backend system. The
490 ;; default implementation always returns nil.
492 ;; - repository-hostname (dirname)
494 ;; Return the hostname that the backend will have to contact
495 ;; in order to operate on a file in DIRNAME. If the return value
496 ;; is nil, it means that the repository is local.
497 ;; This function is used in `vc-stay-local-p' which backends can use
498 ;; for their convenience.
500 ;; - previous-revision (file rev)
502 ;; Return the revision number that precedes REV for FILE, or nil if no such
503 ;; revision exists.
505 ;; - next-revision (file rev)
507 ;; Return the revision number that follows REV for FILE, or nil if no such
508 ;; revision exists.
510 ;; - check-headers ()
512 ;; Return non-nil if the current buffer contains any version headers.
514 ;; - clear-headers ()
516 ;; In the current buffer, reset all version headers to their unexpanded
517 ;; form. This function should be provided if the state-querying code
518 ;; for this backend uses the version headers to determine the state of
519 ;; a file. This function will then be called whenever VC changes the
520 ;; version control state in such a way that the headers would give
521 ;; wrong information.
523 ;; - delete-file (file)
525 ;; Delete FILE and mark it as deleted in the repository. If this
526 ;; function is not provided, the command `vc-delete-file' will
527 ;; signal an error.
529 ;; - rename-file (old new)
531 ;; Rename file OLD to NEW, both in the working area and in the
532 ;; repository. If this function is not provided, the renaming
533 ;; will be done by (vc-delete-file old) and (vc-register new).
535 ;; - find-file-hook ()
537 ;; Operation called in current buffer when opening a file. This can
538 ;; be used by the backend to setup some local variables it might need.
540 ;; - find-file-not-found-hook ()
542 ;; Operation called in current buffer when opening a non-existing file.
543 ;; By default, this asks the user if she wants to check out the file.
545 ;; - extra-menu ()
547 ;; Return a menu keymap, the items in the keymap will appear at the
548 ;; end of the Version Control menu. The goal is to allow backends
549 ;; to specify extra menu items that appear in the VC menu. This way
550 ;; you can provide menu entries for functionality that is specific
551 ;; to your backend and which does not map to any of the VC generic
552 ;; concepts.
554 ;; - extra-status-menu ()
556 ;; Return a menu keymap, the items in the keymap will appear at the
557 ;; end of the VC Status menu. The goal is to allow backends to
558 ;; specify extra menu items that appear in the VC Status menu. This
559 ;; makes it possible to provide menu entries for functionality that
560 ;; is specific to a backend and which does not map to any of the VC
561 ;; generic concepts.
563 ;;; Todo:
565 ;; - vc-update/vc-merge should deal with VC systems that don't
566 ;; update/merge on a file basis, but on a whole repository basis.
568 ;; - deal with push/pull operations.
570 ;; - "snapshots" should be renamed to "branches", and thoroughly reworked.
572 ;; - the backend sometimes knows when a file it opens has been marked
573 ;; by the VCS as having a "conflict". Find a way to pass this info -
574 ;; to VC so that it can turn on smerge-mode when opening such a
575 ;; file.
577 ;; - add a generic mechanism for remembering the current branch names,
578 ;; display the branch name in the mode-line. Replace
579 ;; vc-cvs-sticky-tag with that.
581 ;; - the *VC-log* buffer needs font-locking.
583 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
584 ;; buffer, if one is present, instead of adding to the ChangeLog.
586 ;; - make vc-state for all backends return 'unregistered instead of
587 ;; nil for unregistered files, then update vc-next-action.
589 ;; - vc-register should register a fileset at a time. The backends
590 ;; already support this, only the front-end needs to be change to
591 ;; handle multiple files at a time.
593 ;; - add a mechanism for ignoring files.
595 ;; - vc-next-action should do something about 'missing files. Maybe
596 ;; just warn, or offer to checkout.
598 ;; - decide if vc-status should replace vc-dired.
600 ;; - vc-status needs mouse bindings.
602 ;; - vc-status needs more key bindings for VC actions.
604 ;; - vc-status toolbar needs more icons.
606 ;; - vc-status: refresh should not completely wipe out the current
607 ;; contents of the vc-status buffer.
609 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
610 ;; files. Now that unregistered and ignored files are shown in
611 ;; vc-dired/vc-status, it is possible that these commands are called
612 ;; for unregistered/ignored files.
614 ;; - do not default to RCS anymore when the current directory is not
615 ;; controlled by any VCS and the user does C-x v v
618 ;;; Code:
620 (require 'vc-hooks)
621 (require 'tool-bar)
623 (eval-when-compile
624 (require 'cl)
625 (require 'dired) ; for dired-map-over-marks macro
626 (require 'dired-aux)) ; for dired-kill-{line,tree}
628 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
629 (setq minor-mode-alist
630 (cons '(vc-parent-buffer vc-parent-buffer-name)
631 minor-mode-alist)))
633 ;; General customization
635 (defgroup vc nil
636 "Version-control system in Emacs."
637 :group 'tools)
639 (defcustom vc-suppress-confirm nil
640 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
641 :type 'boolean
642 :group 'vc)
644 (defcustom vc-delete-logbuf-window t
645 "If non-nil, delete the *VC-log* buffer and window after each logical action.
646 If nil, bury that buffer instead.
647 This is most useful if you have multiple windows on a frame and would like to
648 preserve the setting."
649 :type 'boolean
650 :group 'vc)
652 (defcustom vc-initial-comment nil
653 "If non-nil, prompt for initial comment when a file is registered."
654 :type 'boolean
655 :group 'vc)
657 (defcustom vc-default-init-revision "1.1"
658 "A string used as the default revision number when a new file is registered.
659 This can be overridden by giving a prefix argument to \\[vc-register]. This
660 can also be overridden by a particular VC backend."
661 :type 'string
662 :group 'vc
663 :version "20.3")
665 (defcustom vc-command-messages nil
666 "If non-nil, display run messages from back-end commands."
667 :type 'boolean
668 :group 'vc)
670 (defcustom vc-checkin-switches nil
671 "A string or list of strings specifying extra switches for checkin.
672 These are passed to the checkin program by \\[vc-checkin]."
673 :type '(choice (const :tag "None" nil)
674 (string :tag "Argument String")
675 (repeat :tag "Argument List"
676 :value ("")
677 string))
678 :group 'vc)
680 (defcustom vc-checkout-switches nil
681 "A string or list of strings specifying extra switches for checkout.
682 These are passed to the checkout program by \\[vc-checkout]."
683 :type '(choice (const :tag "None" nil)
684 (string :tag "Argument String")
685 (repeat :tag "Argument List"
686 :value ("")
687 string))
688 :group 'vc)
690 (defcustom vc-register-switches nil
691 "A string or list of strings; extra switches for registering a file.
692 These are passed to the checkin program by \\[vc-register]."
693 :type '(choice (const :tag "None" nil)
694 (string :tag "Argument String")
695 (repeat :tag "Argument List"
696 :value ("")
697 string))
698 :group 'vc)
700 (defcustom vc-dired-listing-switches "-al"
701 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
702 :type 'string
703 :group 'vc
704 :version "21.1")
706 (defcustom vc-dired-recurse t
707 "If non-nil, show directory trees recursively in VC Dired."
708 :type 'boolean
709 :group 'vc
710 :version "20.3")
712 (defcustom vc-dired-terse-display t
713 "If non-nil, show only locked or locally modified files in VC Dired."
714 :type 'boolean
715 :group 'vc
716 :version "20.3")
718 (defcustom vc-diff-switches nil
719 "A string or list of strings specifying switches for diff under VC.
720 When running diff under a given BACKEND, VC concatenates the values of
721 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
722 get the switches for that command. Thus, `vc-diff-switches' should
723 contain switches that are specific to version control, but not
724 specific to any particular backend."
725 :type '(choice (const :tag "None" nil)
726 (string :tag "Argument String")
727 (repeat :tag "Argument List"
728 :value ("")
729 string))
730 :group 'vc
731 :version "21.1")
733 (defcustom vc-diff-knows-L nil
734 "*Indicates whether diff understands the -L option.
735 The value is either `yes', `no', or nil. If it is nil, VC tries
736 to use -L and sets this variable to remember whether it worked."
737 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
738 :group 'vc)
740 (defcustom vc-allow-async-revert nil
741 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
742 Enabling this option means that you can confirm a revert operation even
743 if the local changes in the file have not been found and displayed yet."
744 :type '(choice (const :tag "No" nil)
745 (const :tag "Yes" t))
746 :group 'vc
747 :version "22.1")
749 ;;;###autoload
750 (defcustom vc-checkout-hook nil
751 "Normal hook (list of functions) run after checking out a file.
752 See `run-hooks'."
753 :type 'hook
754 :group 'vc
755 :version "21.1")
757 (defcustom vc-annotate-display-mode 'fullscale
758 "Which mode to color the output of \\[vc-annotate] with by default."
759 :type '(choice (const :tag "By Color Map Range" nil)
760 (const :tag "Scale to Oldest" scale)
761 (const :tag "Scale Oldest->Newest" fullscale)
762 (number :tag "Specify Fractional Number of Days"
763 :value "20.5"))
764 :group 'vc)
766 ;;;###autoload
767 (defcustom vc-checkin-hook nil
768 "Normal hook (list of functions) run after commit or file checkin.
769 See also `log-edit-done-hook'."
770 :type 'hook
771 :options '(log-edit-comment-to-change-log)
772 :group 'vc)
774 ;;;###autoload
775 (defcustom vc-before-checkin-hook nil
776 "Normal hook (list of functions) run before a commit or a file checkin.
777 See `run-hooks'."
778 :type 'hook
779 :group 'vc)
781 (defcustom vc-logentry-check-hook nil
782 "Normal hook run by `vc-backend-logentry-check'.
783 Use this to impose your own rules on the entry in addition to any the
784 version control backend imposes itself."
785 :type 'hook
786 :group 'vc)
788 ;; Annotate customization
789 (defcustom vc-annotate-color-map
790 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
791 ;; A custom sorted TTY colormap
792 (let* ((colors
793 (sort
794 (delq nil
795 (mapcar (lambda (x)
796 (if (not (or
797 (string-equal (car x) "white")
798 (string-equal (car x) "black") ))
799 (car x)))
800 (tty-color-alist)))
801 (lambda (a b)
802 (cond
803 ((or (string-equal a "red") (string-equal b "blue")) t)
804 ((or (string-equal b "red") (string-equal a "blue")) nil)
805 ((string-equal a "yellow") t)
806 ((string-equal b "yellow") nil)
807 ((string-equal a "cyan") t)
808 ((string-equal b "cyan") nil)
809 ((string-equal a "green") t)
810 ((string-equal b "green") nil)
811 ((string-equal a "magenta") t)
812 ((string-equal b "magenta") nil)
813 (t (string< a b))))))
814 (date 20.)
815 (delta (/ (- 360. date) (1- (length colors)))))
816 (mapcar (lambda (x)
817 (prog1
818 (cons date x)
819 (setq date (+ date delta)))) colors))
820 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
821 '(( 20. . "#FF3F3F")
822 ( 40. . "#FF6C3F")
823 ( 60. . "#FF993F")
824 ( 80. . "#FFC63F")
825 (100. . "#FFF33F")
826 (120. . "#DDFF3F")
827 (140. . "#B0FF3F")
828 (160. . "#83FF3F")
829 (180. . "#56FF3F")
830 (200. . "#3FFF56")
831 (220. . "#3FFF83")
832 (240. . "#3FFFB0")
833 (260. . "#3FFFDD")
834 (280. . "#3FF3FF")
835 (300. . "#3FC6FF")
836 (320. . "#3F99FF")
837 (340. . "#3F6CFF")
838 (360. . "#3F3FFF")))
839 "Association list of age versus color, for \\[vc-annotate].
840 Ages are given in units of fractional days. Default is eighteen
841 steps using a twenty day increment, from red to blue. For TTY
842 displays with 8 or fewer colors, the default is red to blue with
843 all other colors between (excluding black and white)."
844 :type 'alist
845 :group 'vc)
847 (defcustom vc-annotate-very-old-color "#3F3FFF"
848 "Color for lines older than the current color range in \\[vc-annotate]]."
849 :type 'string
850 :group 'vc)
852 (defcustom vc-annotate-background "black"
853 "Background color for \\[vc-annotate].
854 Default color is used if nil."
855 :type '(choice (const :tag "Default background" nil) (color))
856 :group 'vc)
858 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
859 "Menu elements for the mode-specific menu of VC-Annotate mode.
860 List of factors, used to expand/compress the time scale. See `vc-annotate'."
861 :type '(repeat number)
862 :group 'vc)
864 (defvar vc-annotate-mode-map
865 (let ((m (make-sparse-keymap)))
866 (define-key m "A" 'vc-annotate-revision-previous-to-line)
867 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
868 (define-key m "f" 'vc-annotate-find-revision-at-line)
869 (define-key m "J" 'vc-annotate-revision-at-line)
870 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
871 (define-key m "N" 'vc-annotate-next-revision)
872 (define-key m "P" 'vc-annotate-prev-revision)
873 (define-key m "W" 'vc-annotate-working-revision)
874 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
876 "Local keymap used for VC-Annotate mode.")
878 ;; Header-insertion hair
880 (defcustom vc-static-header-alist
881 '(("\\.c\\'" .
882 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
883 "*Associate static header string templates with file types.
884 A \%s in the template is replaced with the first string associated with
885 the file's version control type in `vc-header-alist'."
886 :type '(repeat (cons :format "%v"
887 (regexp :tag "File Type")
888 (string :tag "Header String")))
889 :group 'vc)
891 (defcustom vc-comment-alist
892 '((nroff-mode ".\\\"" ""))
893 "*Special comment delimiters for generating VC headers.
894 Add an entry in this list if you need to override the normal `comment-start'
895 and `comment-end' variables. This will only be necessary if the mode language
896 is sensitive to blank lines."
897 :type '(repeat (list :format "%v"
898 (symbol :tag "Mode")
899 (string :tag "Comment Start")
900 (string :tag "Comment End")))
901 :group 'vc)
903 (defcustom vc-checkout-carefully (= (user-uid) 0)
904 "*Non-nil means be extra-careful in checkout.
905 Verify that the file really is not locked
906 and that its contents match what the master file says."
907 :type 'boolean
908 :group 'vc)
909 (make-obsolete-variable 'vc-checkout-carefully
910 "the corresponding checks are always done now."
911 "21.1")
914 ;; Variables the user doesn't need to know about.
915 (defvar vc-log-operation nil)
916 (defvar vc-log-after-operation-hook nil)
918 ;; In a log entry buffer, this is a local variable
919 ;; that points to the buffer for which it was made
920 ;; (either a file, or a VC dired buffer).
921 (defvar vc-parent-buffer nil)
922 (put 'vc-parent-buffer 'permanent-local t)
923 (defvar vc-parent-buffer-name nil)
924 (put 'vc-parent-buffer-name 'permanent-local t)
926 (defvar vc-disable-async-diff nil
927 "VC sets this to t locally to disable some async diff operations.
928 Backends that offer asynchronous diffs should respect this variable
929 in their implementation of vc-BACKEND-diff.")
931 (defvar vc-log-fileset)
932 (defvar vc-log-revision)
934 (defvar vc-dired-mode nil)
935 (make-variable-buffer-local 'vc-dired-mode)
937 ;; File property caching
939 (defun vc-clear-context ()
940 "Clear all cached file properties."
941 (interactive)
942 (fillarray vc-file-prop-obarray 0))
944 (defmacro with-vc-properties (files form settings)
945 "Execute FORM, then maybe set per-file properties for FILES.
946 SETTINGS is an association list of property/value pairs. After
947 executing FORM, set those properties from SETTINGS that have not yet
948 been updated to their corresponding values."
949 (declare (debug t))
950 `(let ((vc-touched-properties (list t)))
951 ,form
952 (dolist (file ,files)
953 (dolist (setting ,settings)
954 (let ((property (car setting)))
955 (unless (memq property vc-touched-properties)
956 (put (intern file vc-file-prop-obarray)
957 property (cdr setting))))))))
959 ;; Two macros for elisp programming
961 ;;;###autoload
962 (defmacro with-vc-file (file comment &rest body)
963 "Check out a writable copy of FILE if necessary, then execute BODY.
964 Check in FILE with COMMENT (a string) after BODY has been executed.
965 FILE is passed through `expand-file-name'; BODY executed within
966 `save-excursion'. If FILE is not under version control, or you are
967 using a locking version-control system and the file is locked by
968 somebody else, signal error."
969 (declare (debug t) (indent 2))
970 (let ((filevar (make-symbol "file")))
971 `(let ((,filevar (expand-file-name ,file)))
972 (or (vc-backend ,filevar)
973 (error "File not under version control: `%s'" file))
974 (unless (vc-editable-p ,filevar)
975 (let ((state (vc-state ,filevar)))
976 (if (stringp state)
977 (error "`%s' is locking `%s'" state ,filevar)
978 (vc-checkout ,filevar t))))
979 (save-excursion
980 ,@body)
981 (vc-checkin (list ,filevar) nil ,comment))))
983 ;;;###autoload
984 (defmacro edit-vc-file (file comment &rest body)
985 "Edit FILE under version control, executing body.
986 Checkin with COMMENT after executing BODY.
987 This macro uses `with-vc-file', passing args to it.
988 However, before executing BODY, find FILE, and after BODY, save buffer."
989 (declare (debug t) (indent 2))
990 (let ((filevar (make-symbol "file")))
991 `(let ((,filevar (expand-file-name ,file)))
992 (with-vc-file
993 ,filevar ,comment
994 (set-buffer (find-file-noselect ,filevar))
995 ,@body
996 (save-buffer)))))
998 ;; Common command execution logic to be used by backends
1000 (defun vc-process-filter (p s)
1001 "An alternative output filter for async process P.
1002 One difference with the default filter is that this inserts S after markers.
1003 Another is that undo information is not kept."
1004 (let ((buffer (process-buffer p)))
1005 (when (buffer-live-p buffer)
1006 (with-current-buffer buffer
1007 (save-excursion
1008 (let ((buffer-undo-list t)
1009 (inhibit-read-only t))
1010 (goto-char (process-mark p))
1011 (insert s)
1012 (set-marker (process-mark p) (point))))))))
1014 (defun vc-setup-buffer (&optional buf)
1015 "Prepare BUF for executing a VC command and make it current.
1016 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
1017 (unless buf (setq buf "*vc*"))
1018 (let ((camefrom (current-buffer))
1019 (olddir default-directory))
1020 (set-buffer (get-buffer-create buf))
1021 (kill-all-local-variables)
1022 (set (make-local-variable 'vc-parent-buffer) camefrom)
1023 (set (make-local-variable 'vc-parent-buffer-name)
1024 (concat " from " (buffer-name camefrom)))
1025 (setq default-directory olddir)
1026 (let ((buffer-undo-list t)
1027 (inhibit-read-only t))
1028 (erase-buffer))))
1030 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
1032 (defun vc-process-sentinel (p s)
1033 (let ((previous (process-get p 'vc-previous-sentinel))
1034 (buf (process-buffer p)))
1035 ;; Impatient users sometime kill "slow" buffers; check liveness
1036 ;; to avoid "error in process sentinel: Selecting deleted buffer".
1037 (when (buffer-live-p buf)
1038 (if previous (funcall previous p s))
1039 (with-current-buffer buf
1040 (setq mode-line-process
1041 (let ((status (process-status p)))
1042 ;; Leave mode-line uncluttered, normally.
1043 ;; (Let known any weirdness in-form-ally. ;-) --ttn
1044 (unless (eq 'exit status)
1045 (format " (%s)" status))))
1046 (let (vc-sentinel-movepoint)
1047 ;; Normally, we want async code such as sentinels to not move point.
1048 (save-excursion
1049 (goto-char (process-mark p))
1050 (let ((cmds (process-get p 'vc-sentinel-commands)))
1051 (process-put p 'vc-sentinel-commands nil)
1052 (dolist (cmd cmds)
1053 ;; Each sentinel may move point and the next one should be run
1054 ;; at that new point. We could get the same result by having
1055 ;; each sentinel read&set process-mark, but since `cmd' needs
1056 ;; to work both for async and sync processes, this would be
1057 ;; difficult to achieve.
1058 (vc-exec-after cmd))))
1059 ;; But sometimes the sentinels really want to move point.
1060 (if vc-sentinel-movepoint
1061 (let ((win (get-buffer-window (current-buffer) 0)))
1062 (if (not win)
1063 (goto-char vc-sentinel-movepoint)
1064 (with-selected-window win
1065 (goto-char vc-sentinel-movepoint))))))))))
1067 (defun vc-set-mode-line-busy-indicator ()
1068 (setq mode-line-process
1069 (concat " " (propertize "[waiting...]"
1070 'face 'mode-line-emphasis
1071 'help-echo
1072 "A VC command is in progress in this buffer"))))
1074 (defun vc-exec-after (code)
1075 "Eval CODE when the current buffer's process is done.
1076 If the current buffer has no process, just evaluate CODE.
1077 Else, add CODE to the process' sentinel."
1078 (let ((proc (get-buffer-process (current-buffer))))
1079 (cond
1080 ;; If there's no background process, just execute the code.
1081 ;; We used to explicitly call delete-process on exited processes,
1082 ;; but this led to timing problems causing process output to be
1083 ;; lost. Terminated processes get deleted automatically
1084 ;; anyway. -- cyd
1085 ((or (null proc) (eq (process-status proc) 'exit))
1086 ;; Make sure we've read the process's output before going further.
1087 (if proc (accept-process-output proc))
1088 (eval code))
1089 ;; If a process is running, add CODE to the sentinel
1090 ((eq (process-status proc) 'run)
1091 (vc-set-mode-line-busy-indicator)
1092 (let ((previous (process-sentinel proc)))
1093 (unless (eq previous 'vc-process-sentinel)
1094 (process-put proc 'vc-previous-sentinel previous))
1095 (set-process-sentinel proc 'vc-process-sentinel))
1096 (process-put proc 'vc-sentinel-commands
1097 ;; We keep the code fragments in the order given
1098 ;; so that vc-diff-finish's message shows up in
1099 ;; the presence of non-nil vc-command-messages.
1100 (append (process-get proc 'vc-sentinel-commands)
1101 (list code))))
1102 (t (error "Unexpected process state"))))
1103 nil)
1105 (defvar vc-post-command-functions nil
1106 "Hook run at the end of `vc-do-command'.
1107 Each function is called inside the buffer in which the command was run
1108 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
1110 (defvar w32-quote-process-args)
1112 (defun vc-delistify (filelist)
1113 "Smash a FILELIST into a file list string suitable for info messages."
1114 ;; FIXME what about file names with spaces?
1115 (if (not filelist) "." (mapconcat 'identity filelist " ")))
1117 ;;;###autoload
1118 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
1119 "Execute a VC command, notifying user and checking for errors.
1120 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
1121 current buffer if BUFFER is t. If the destination buffer is not
1122 already current, set it up properly and erase it. The command is
1123 considered successful if its exit status does not exceed OKSTATUS (if
1124 OKSTATUS is nil, that means to ignore error status, if it is `async', that
1125 means not to wait for termination of the subprocess; if it is t it means to
1126 ignore all execution errors). FILE-OR-LIST is the name of a working file;
1127 it may be a list of files or be nil (to execute commands that don't expect
1128 a file name or set of files). If an optional list of FLAGS is present,
1129 that is inserted into the command line before the filename."
1130 ;; FIXME: file-relative-name can return a bogus result because
1131 ;; it doesn't look at the actual file-system to see if symlinks
1132 ;; come into play.
1133 (let* ((files
1134 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
1135 (if (listp file-or-list) file-or-list (list file-or-list))))
1136 (full-command
1137 ;; What we're doing here is preparing a version of the command
1138 ;; for display in a debug-progess message. If it's fewer than
1139 ;; 20 characters display the entire command (without trailing
1140 ;; newline). Otherwise display the first 20 followed by an ellipsis.
1141 (concat (if (string= (substring command -1) "\n")
1142 (substring command 0 -1)
1143 command)
1145 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
1146 " " (vc-delistify files))))
1147 (save-current-buffer
1148 (unless (or (eq buffer t)
1149 (and (stringp buffer)
1150 (string= (buffer-name) buffer))
1151 (eq buffer (current-buffer)))
1152 (vc-setup-buffer buffer))
1153 ;; If there's some previous async process still running, just kill it.
1154 (let ((oldproc (get-buffer-process (current-buffer))))
1155 ;; If we wanted to wait for oldproc to finish before doing
1156 ;; something, we'd have used vc-eval-after.
1157 ;; Use `delete-process' rather than `kill-process' because we don't
1158 ;; want any of its output to appear from now on.
1159 (if oldproc (delete-process oldproc)))
1160 (let ((squeezed (remq nil flags))
1161 (inhibit-read-only t)
1162 (status 0))
1163 (when files
1164 (setq squeezed (nconc squeezed files)))
1165 (let ((exec-path (append vc-path exec-path))
1166 ;; Add vc-path to PATH for the execution of this command.
1167 (process-environment
1168 (cons (concat "PATH=" (getenv "PATH")
1169 path-separator
1170 (mapconcat 'identity vc-path path-separator))
1171 process-environment))
1172 (w32-quote-process-args t))
1173 (if (and (eq okstatus 'async) (file-remote-p default-directory))
1174 ;; start-process does not support remote execution
1175 (setq okstatus nil))
1176 (if (eq okstatus 'async)
1177 ;; Run asynchronously.
1178 (let ((proc
1179 (let ((process-connection-type nil))
1180 (apply 'start-file-process command (current-buffer)
1181 command squeezed))))
1182 (if vc-command-messages
1183 (message "Running %s in background..." full-command))
1184 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
1185 (set-process-filter proc 'vc-process-filter)
1186 (vc-exec-after
1187 `(if vc-command-messages
1188 (message "Running %s in background... done" ',full-command))))
1189 ;; Run synchrously
1190 (if vc-command-messages
1191 (message "Running %s in foreground..." full-command))
1192 (let ((buffer-undo-list t))
1193 (setq status (apply 'process-file command nil t nil squeezed)))
1194 (when (and (not (eq t okstatus))
1195 (or (not (integerp status))
1196 (and okstatus (< okstatus status))))
1197 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
1198 (pop-to-buffer (current-buffer))
1199 (goto-char (point-min))
1200 (shrink-window-if-larger-than-buffer))
1201 (error "Running %s...FAILED (%s)" full-command
1202 (if (integerp status) (format "status %d" status) status))))
1203 ;; We're done. But don't emit a status message if running
1204 ;; asychronously, it would just mislead.
1205 (if (and vc-command-messages (not (eq okstatus 'async)))
1206 (message "Running %s...OK = %d" full-command status)))
1207 (vc-exec-after
1208 `(run-hook-with-args 'vc-post-command-functions
1209 ',command ',file-or-list ',flags))
1210 status))))
1212 (defun vc-position-context (posn)
1213 "Save a bit of the text around POSN in the current buffer.
1214 Used to help us find the corresponding position again later
1215 if markers are destroyed or corrupted."
1216 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
1217 ;; rcs.el mode.
1218 (list posn
1219 (buffer-size)
1220 (buffer-substring posn
1221 (min (point-max) (+ posn 100)))))
1223 (defun vc-find-position-by-context (context)
1224 "Return the position of CONTEXT in the current buffer.
1225 If CONTEXT cannot be found, return nil."
1226 (let ((context-string (nth 2 context)))
1227 (if (equal "" context-string)
1228 (point-max)
1229 (save-excursion
1230 (let ((diff (- (nth 1 context) (buffer-size))))
1231 (if (< diff 0) (setq diff (- diff)))
1232 (goto-char (nth 0 context))
1233 (if (or (search-forward context-string nil t)
1234 ;; Can't use search-backward since the match may continue
1235 ;; after point.
1236 (progn (goto-char (- (point) diff (length context-string)))
1237 ;; goto-char doesn't signal an error at
1238 ;; beginning of buffer like backward-char would
1239 (search-forward context-string nil t)))
1240 ;; to beginning of OSTRING
1241 (- (point) (length context-string))))))))
1243 (defun vc-context-matches-p (posn context)
1244 "Return t if POSN matches CONTEXT, nil otherwise."
1245 (let* ((context-string (nth 2 context))
1246 (len (length context-string))
1247 (end (+ posn len)))
1248 (if (> end (1+ (buffer-size)))
1250 (string= context-string (buffer-substring posn end)))))
1252 (defun vc-buffer-context ()
1253 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1254 Used by `vc-restore-buffer-context' to later restore the context."
1255 (let ((point-context (vc-position-context (point)))
1256 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1257 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1258 (vc-position-context (mark-marker))))
1259 ;; Make the right thing happen in transient-mark-mode.
1260 (mark-active nil)
1261 ;; The new compilation code does not use compilation-error-list any
1262 ;; more, so the code below is now ineffective and might as well
1263 ;; be disabled. -- Stef
1264 ;; ;; We may want to reparse the compilation buffer after revert
1265 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
1266 ;; ;; Construct a list; each elt is nil or a buffer
1267 ;; ;; if that buffer is a compilation output buffer
1268 ;; ;; that contains markers into the current buffer.
1269 ;; (save-current-buffer
1270 ;; (mapcar (lambda (buffer)
1271 ;; (set-buffer buffer)
1272 ;; (let ((errors (or
1273 ;; compilation-old-error-list
1274 ;; compilation-error-list))
1275 ;; (buffer-error-marked-p nil))
1276 ;; (while (and (consp errors)
1277 ;; (not buffer-error-marked-p))
1278 ;; (and (markerp (cdr (car errors)))
1279 ;; (eq buffer
1280 ;; (marker-buffer
1281 ;; (cdr (car errors))))
1282 ;; (setq buffer-error-marked-p t))
1283 ;; (setq errors (cdr errors)))
1284 ;; (if buffer-error-marked-p buffer)))
1285 ;; (buffer-list)))))
1286 (reparse nil))
1287 (list point-context mark-context reparse)))
1289 (defun vc-restore-buffer-context (context)
1290 "Restore point/mark, and reparse any affected compilation buffers.
1291 CONTEXT is that which `vc-buffer-context' returns."
1292 (let ((point-context (nth 0 context))
1293 (mark-context (nth 1 context))
1294 ;; (reparse (nth 2 context))
1296 ;; The new compilation code does not use compilation-error-list any
1297 ;; more, so the code below is now ineffective and might as well
1298 ;; be disabled. -- Stef
1299 ;; ;; Reparse affected compilation buffers.
1300 ;; (while reparse
1301 ;; (if (car reparse)
1302 ;; (with-current-buffer (car reparse)
1303 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
1304 ;; ;; Record the position in the compilation buffer of
1305 ;; ;; the last error next-error went to.
1306 ;; (error-pos (marker-position
1307 ;; (car (car-safe compilation-error-list)))))
1308 ;; ;; Reparse the error messages as far as they were parsed before.
1309 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
1310 ;; ;; Move the pointer up to find the error we were at before
1311 ;; ;; reparsing. Now next-error should properly go to the next one.
1312 ;; (while (and compilation-error-list
1313 ;; (/= error-pos (car (car compilation-error-list))))
1314 ;; (setq compilation-error-list (cdr compilation-error-list))))))
1315 ;; (setq reparse (cdr reparse)))
1317 ;; if necessary, restore point and mark
1318 (if (not (vc-context-matches-p (point) point-context))
1319 (let ((new-point (vc-find-position-by-context point-context)))
1320 (if new-point (goto-char new-point))))
1321 (and mark-active
1322 mark-context
1323 (not (vc-context-matches-p (mark) mark-context))
1324 (let ((new-mark (vc-find-position-by-context mark-context)))
1325 (if new-mark (set-mark new-mark))))))
1327 ;;; Code for deducing what fileset and backend to assume
1329 (defun vc-responsible-backend (file &optional register)
1330 "Return the name of a backend system that is responsible for FILE.
1331 The optional argument REGISTER means that a backend suitable for
1332 registration should be found.
1334 If REGISTER is nil, then if FILE is already registered, return the
1335 backend of FILE. If FILE is not registered, or a directory, then the
1336 first backend in `vc-handled-backends' that declares itself
1337 responsible for FILE is returned. If no backend declares itself
1338 responsible, return the first backend.
1340 If REGISTER is non-nil, return the first responsible backend under
1341 which FILE is not yet registered. If there is no such backend, return
1342 the first backend under which FILE is not yet registered, but could
1343 be registered."
1344 (if (not vc-handled-backends)
1345 (error "No handled backends"))
1346 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1347 (catch 'found
1348 ;; First try: find a responsible backend. If this is for registration,
1349 ;; it must be a backend under which FILE is not yet registered.
1350 (dolist (backend vc-handled-backends)
1351 (and (or (not register)
1352 (not (vc-call-backend backend 'registered file)))
1353 (vc-call-backend backend 'responsible-p file)
1354 (throw 'found backend)))
1355 ;; no responsible backend
1356 (if (not register)
1357 ;; if this is not for registration, the first backend must do
1358 (car vc-handled-backends)
1359 ;; for registration, we need to find a new backend that
1360 ;; could register FILE
1361 (dolist (backend vc-handled-backends)
1362 (and (not (vc-call-backend backend 'registered file))
1363 (vc-call-backend backend 'could-register file)
1364 (throw 'found backend)))
1365 (error "No backend that could register")))))
1367 (defun vc-expand-dirs (file-or-dir-list)
1368 "Expands directories in a file list specification.
1369 Only files already under version control are noticed."
1370 ;; FIXME: Kill this function.
1371 (let ((flattened '()))
1372 (dolist (node file-or-dir-list)
1373 (vc-file-tree-walk
1374 node (lambda (f) (if (vc-backend f) (push f flattened)))))
1375 (nreverse flattened)))
1377 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered)
1378 "Deduce a set of files and a backend to which to apply an operation.
1380 If we're in VC-dired mode, the fileset is the list of marked files.
1381 Otherwise, if we're looking at a buffer visiting a version-controlled file,
1382 the fileset is a singleton containing this file.
1383 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1384 and we're in a dired buffer, select the current directory.
1385 If none of these conditions is met, but ALLOW_UNREGISTERED is in and the
1386 visited file is not registered, return a singletin fileset containing it.
1387 Otherwise, throw an error."
1388 (cond (vc-dired-mode
1389 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1390 (unless marked
1391 (error "No files have been selected."))
1392 ;; All members of the fileset must have the same backend
1393 (let ((firstbackend (vc-backend (car marked))))
1394 (dolist (f (cdr marked))
1395 (unless (eq (vc-backend f) firstbackend)
1396 (error "All members of a fileset must be under the same version-control system."))))
1397 marked))
1398 ((eq major-mode 'vc-status-mode)
1399 (let ((marked (vc-status-marked-files)))
1400 (if marked
1401 marked
1402 (list (vc-status-current-file)))))
1403 ((vc-backend buffer-file-name)
1404 (list buffer-file-name))
1405 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1406 (with-current-buffer vc-parent-buffer
1407 (or vc-dired-mode (eq major-mode 'vc-status-mode)))))
1408 (progn
1409 (set-buffer vc-parent-buffer)
1410 (vc-deduce-fileset)))
1411 ;; This is guarded by an enabling arg so users won't potentially
1412 ;; shoot themselves in the foot by modifying a fileset they can't
1413 ;; verify by eyeball. Allow it for nondestructive commands like
1414 ;; making diffs, or possibly for destructive ones that have
1415 ;; confirmation prompts.
1416 ((and allow-directory-wildcard
1417 ;; I think this is a misfeature. For now, I'll leave it in, but
1418 ;; I'll disable it anywhere else than in dired buffers. --Stef
1419 (and (derived-mode-p 'dired-mode)
1420 (equal buffer-file-name nil)
1421 (equal list-buffers-directory default-directory)))
1422 (progn
1423 (message "All version-controlled files below %s selected."
1424 default-directory)
1425 (list default-directory)))
1426 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1427 (list buffer-file-name))
1428 (t (error "No fileset is available here."))))
1430 (defun vc-ensure-vc-buffer ()
1431 "Make sure that the current buffer visits a version-controlled file."
1432 (cond
1433 (vc-dired-mode
1434 (set-buffer (find-file-noselect (dired-get-filename))))
1435 ((eq major-mode 'vc-status-mode)
1436 (set-buffer (find-file-noselect (vc-status-current-file))))
1438 (while (and vc-parent-buffer
1439 (buffer-live-p vc-parent-buffer)
1440 ;; Avoid infinite looping when vc-parent-buffer and
1441 ;; current buffer are the same buffer.
1442 (not (eq vc-parent-buffer (current-buffer))))
1443 (set-buffer vc-parent-buffer))
1444 (if (not buffer-file-name)
1445 (error "Buffer %s is not associated with a file" (buffer-name))
1446 (if (not (vc-backend buffer-file-name))
1447 (error "File %s is not under version control" buffer-file-name))))))
1449 ;;; Support for the C-x v v command. This is where all the single-file-oriented
1450 ;;; code from before the fileset rewrite lives.
1452 (defsubst vc-editable-p (file)
1453 "Return non-nil if FILE can be edited."
1454 (or (eq (vc-checkout-model file) 'implicit)
1455 (memq (vc-state file) '(edited needs-merge))))
1457 (defun vc-revert-buffer-internal (&optional arg no-confirm)
1458 "Revert buffer, keeping point and mark where user expects them.
1459 Try to be clever in the face of changes due to expanded version-control
1460 key words. This is important for typeahead to work as expected.
1461 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1462 (interactive "P")
1463 (widen)
1464 (let ((context (vc-buffer-context)))
1465 ;; Use save-excursion here, because it may be able to restore point
1466 ;; and mark properly even in cases where vc-restore-buffer-context
1467 ;; would fail. However, save-excursion might also get it wrong --
1468 ;; in this case, vc-restore-buffer-context gives it a second try.
1469 (save-excursion
1470 ;; t means don't call normal-mode;
1471 ;; that's to preserve various minor modes.
1472 (revert-buffer arg no-confirm t))
1473 (vc-restore-buffer-context context)))
1475 (defun vc-buffer-sync (&optional not-urgent)
1476 "Make sure the current buffer and its working file are in sync.
1477 NOT-URGENT means it is ok to continue if the user says not to save."
1478 (if (buffer-modified-p)
1479 (if (or vc-suppress-confirm
1480 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1481 (save-buffer)
1482 (unless not-urgent
1483 (error "Aborted")))))
1485 (defvar vc-dired-window-configuration)
1487 (defun vc-compatible-state (p q)
1488 "Controls which states can be in the same commit."
1490 (eq p q)
1491 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1493 ;; Here's the major entry point.
1495 ;;;###autoload
1496 (defun vc-next-action (verbose)
1497 "Do the next logical version control operation on the current fileset.
1498 This requires that all files in the fileset be in the same state.
1500 For locking systems:
1501 If every file is not already registered, this registers each for version
1502 control.
1503 If every file is registered and not locked by anyone, this checks out
1504 a writable and locked file of each ready for editing.
1505 If every file is checked out and locked by the calling user, this
1506 first checks to see if each file has changed since checkout. If not,
1507 it performs a revert on that file.
1508 If every file has been changed, this pops up a buffer for entry
1509 of a log message; when the message has been entered, it checks in the
1510 resulting changes along with the log message as change commentary. If
1511 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1512 read-only copy of each changed file is left in place afterwards.
1513 If the affected file is registered and locked by someone else, you are
1514 given the option to steal the lock(s).
1516 For merging systems:
1517 If every file is not already registered, this registers each one for version
1518 control. This does an add, but not a commit.
1519 If every file is added but not committed, each one is committed.
1520 If every working file is changed, but the corresponding repository file is
1521 unchanged, this pops up a buffer for entry of a log message; when the
1522 message has been entered, it checks in the resulting changes along
1523 with the logmessage as change commentary. A writable file is retained.
1524 If the repository file is changed, you are asked if you want to
1525 merge in the changes into your working copy."
1526 (interactive "P")
1527 (let* ((files (vc-deduce-fileset nil t))
1528 (state (vc-state (car files)))
1529 (model (vc-checkout-model (car files)))
1530 revision)
1531 ;; Verify that the fileset is homogenous
1532 (dolist (file (cdr files))
1533 (unless (vc-compatible-state (vc-state file) state)
1534 (error "Fileset is in a mixed-up state"))
1535 (unless (eq (vc-checkout-model file) model)
1536 (error "Fileset has mixed checkout models")))
1537 ;; Check for buffers in the fileset not matching the on-disk contents.
1538 (dolist (file files)
1539 (let ((visited (get-file-buffer file)))
1540 (when visited
1541 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
1542 (switch-to-buffer-other-window visited)
1543 (set-buffer visited))
1544 ;; Check relation of buffer and file, and make sure
1545 ;; user knows what he's doing. First, finding the file
1546 ;; will check whether the file on disk is newer.
1547 ;; Ignore buffer-read-only during this test, and
1548 ;; preserve find-file-literally.
1549 (let ((buffer-read-only (not (file-writable-p file))))
1550 (find-file-noselect file nil find-file-literally))
1551 (if (not (verify-visited-file-modtime (current-buffer)))
1552 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1553 (write-file buffer-file-name)
1554 (error "Aborted"))
1555 ;; Now, check if we have unsaved changes.
1556 (vc-buffer-sync t)
1557 (when (buffer-modified-p)
1558 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1559 (error "Aborted")))))))
1560 ;; Do the right thing
1561 (cond
1562 ;; Files aren't registered
1563 ((or (not state) ;; RCS uses nil for unregistered files.
1564 (eq state 'unregistered)
1565 (eq state 'ignored))
1566 (mapc 'vc-register files))
1567 ;; Files are up-to-date, or need a merge and user specified a revision
1568 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-patch)))
1569 (cond
1570 (verbose
1571 ;; go to a different revision
1572 (setq revision (read-string "Branch, revision, or backend to move to: "))
1573 (let ((vsym (intern-soft (upcase revision))))
1574 (if (member vsym vc-handled-backends)
1575 (dolist (file files) (vc-transfer-file file vsym))
1576 (dolist (file files)
1577 (vc-checkout file (eq model 'implicit) revision)))))
1578 ((not (eq model 'implicit))
1579 ;; check the files out
1580 (dolist (file files) (vc-checkout file t)))
1582 ;; do nothing
1583 (message "Fileset is up-to-date"))))
1584 ;; Files have local changes
1585 ((vc-compatible-state state 'edited)
1586 (let ((ready-for-commit files))
1587 ;; If files are edited but read-only, give user a chance to correct
1588 (dolist (file files)
1589 (unless (file-writable-p file)
1590 ;; Make the file+buffer read-write.
1591 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1592 (error "Aborted"))
1593 (set-file-modes file (logior (file-modes file) 128))
1594 (let ((visited (get-file-buffer file)))
1595 (when visited
1596 (with-current-buffer visited
1597 (toggle-read-only -1))))))
1598 ;; Allow user to revert files with no changes
1599 (save-excursion
1600 (dolist (file files)
1601 (let ((visited (get-file-buffer file)))
1602 ;; For files with locking, if the file does not contain
1603 ;; any changes, just let go of the lock, i.e. revert.
1604 (when (and (not (eq model 'implicit))
1605 (vc-workfile-unchanged-p file)
1606 ;; If buffer is modified, that means the user just
1607 ;; said no to saving it; in that case, don't revert,
1608 ;; because the user might intend to save after
1609 ;; finishing the log entry and committing.
1610 (not (and visited (buffer-modified-p))))
1611 (vc-revert-file file)
1612 (delete file ready-for-commit)))))
1613 ;; Remaining files need to be committed
1614 (if (not ready-for-commit)
1615 (message "No files remain to be committed")
1616 (if (not verbose)
1617 (vc-checkin ready-for-commit)
1618 (progn
1619 (setq revision (read-string "New revision or backend: "))
1620 (let ((vsym (intern (upcase revision))))
1621 (if (member vsym vc-handled-backends)
1622 (dolist (file files) (vc-transfer-file file vsym))
1623 (vc-checkin ready-for-commit revision))))))))
1624 ;; locked by somebody else (locking VCSes only)
1625 ((stringp state)
1626 ;; In the old days, we computed the revision once and used it on
1627 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1628 ;; computed the revision once (incorrectly, using a free var) and
1629 ;; used it on all files. To fix the free var bug, we can either
1630 ;; use `(car files)' or do what we do here: distribute the
1631 ;; revision computation among `files'. Although this may be
1632 ;; tedious for those backends where a "revision" is a trans-file
1633 ;; concept, it is nonetheless correct for both those and (more
1634 ;; importantly) for those where "revision" is a per-file concept.
1635 ;; If the intersection of the former group and "locking VCSes" is
1636 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1637 ;; pre-computation approach of yore.
1638 (dolist (file files)
1639 (vc-steal-lock
1640 file (if verbose
1641 (read-string (format "%s revision to steal: " file))
1642 (vc-working-revision file))
1643 state)))
1644 ;; needs-patch
1645 ((eq state 'needs-patch)
1646 (dolist (file files)
1647 (if (yes-or-no-p (format
1648 "%s is not up-to-date. Get latest revision? "
1649 (file-name-nondirectory file)))
1650 (vc-checkout file (eq model 'implicit) t)
1651 (when (and (not (eq model 'implicit))
1652 (yes-or-no-p "Lock this revision? "))
1653 (vc-checkout file t)))))
1654 ;; needs-merge
1655 ((eq state 'needs-merge)
1656 (dolist (file files)
1657 (when (yes-or-no-p (format
1658 "%s is not up-to-date. Merge in changes now? "
1659 (file-name-nondirectory file)))
1660 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1662 ;; unlocked-changes
1663 ((eq state 'unlocked-changes)
1664 (dolist (file files)
1665 (if (not (equal buffer-file-name file))
1666 (find-file-other-window file))
1667 (if (save-window-excursion
1668 (vc-diff-internal nil (list file) (vc-working-revision file) nil)
1669 (goto-char (point-min))
1670 (let ((inhibit-read-only t))
1671 (insert
1672 (format "Changes to %s since last lock:\n\n" file)))
1673 (not (beep))
1674 (yes-or-no-p (concat "File has unlocked changes. "
1675 "Claim lock retaining changes? ")))
1676 (progn (vc-call steal-lock file)
1677 (clear-visited-file-modtime)
1678 ;; Must clear any headers here because they wouldn't
1679 ;; show that the file is locked now.
1680 (vc-clear-headers file)
1681 (write-file buffer-file-name)
1682 (vc-mode-line file))
1683 (if (not (yes-or-no-p
1684 "Revert to checked-in revision, instead? "))
1685 (error "Checkout aborted")
1686 (vc-revert-buffer-internal t t)
1687 (vc-checkout file t))))))))
1689 (defun vc-create-repo (backend)
1690 "Create an empty repository in the current directory."
1691 (interactive
1692 (list
1693 (intern
1694 (upcase
1695 (completing-read
1696 "Create repository for: "
1697 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1698 nil t)))))
1699 (vc-call-backend backend 'create-repo))
1701 ;;;###autoload
1702 (defun vc-register (&optional fname set-revision comment)
1703 "Register into a version control system.
1704 If FNAME is given register that file, otherwise register the current file.
1705 With prefix argument SET-REVISION, allow user to specify initial revision
1706 level. If COMMENT is present, use that as an initial comment.
1708 The version control system to use is found by cycling through the list
1709 `vc-handled-backends'. The first backend in that list which declares
1710 itself responsible for the file (usually because other files in that
1711 directory are already registered under that backend) will be used to
1712 register the file. If no backend declares itself responsible, the
1713 first backend that could register the file is used."
1714 (interactive "P")
1715 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1717 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1718 (unless fname (setq fname buffer-file-name))
1719 (when (vc-backend fname)
1720 (if (vc-registered fname)
1721 (error "This file is already registered")
1722 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1723 (error "Aborted"))))
1724 ;; Watch out for new buffers of size 0: the corresponding file
1725 ;; does not exist yet, even though buffer-modified-p is nil.
1726 (when bname
1727 (with-current-buffer bname
1728 (if (and (not (buffer-modified-p))
1729 (zerop (buffer-size))
1730 (not (file-exists-p buffer-file-name)))
1731 (set-buffer-modified-p t))
1732 (vc-buffer-sync)))
1733 (vc-start-entry (list fname)
1734 (if set-revision
1735 (read-string (format "Initial revision level for %s: "
1736 fname))
1737 (vc-call-backend (vc-responsible-backend fname)
1738 'init-revision))
1739 (or comment (not vc-initial-comment))
1741 "Enter initial comment."
1742 (lambda (files rev comment)
1743 (dolist (file files)
1744 (message "Registering %s... " file)
1745 (let ((backend (vc-responsible-backend file t)))
1746 (vc-file-clearprops file)
1747 (vc-call-backend backend 'register (list file) rev comment)
1748 (vc-file-setprop file 'vc-backend backend)
1749 (unless vc-make-backup-files
1750 (make-local-variable 'backup-inhibited)
1751 (setq backup-inhibited t)))
1752 (message "Registering %s... done" file))))))
1754 (defun vc-register-with (backend)
1755 "Register the current file with a specified back end."
1756 (interactive "SBackend: ")
1757 (if (not (member backend vc-handled-backends))
1758 (error "Unknown back end."))
1759 (let ((vc-handled-backends (list backend)))
1760 (call-interactively 'vc-register)))
1762 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
1764 (defun vc-resynch-window (file &optional keep noquery)
1765 "If FILE is in the current buffer, either revert or unvisit it.
1766 The choice between revert (to see expanded keywords) and unvisit depends on
1767 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1768 reverting. NOQUERY should be t *only* if it is known the only
1769 difference between the buffer and the file is due to version control
1770 rather than user editing!"
1771 (and (string= buffer-file-name file)
1772 (if keep
1773 (progn
1774 (vc-revert-buffer-internal t noquery)
1775 ;; TODO: Adjusting view mode might no longer be necessary
1776 ;; after RMS change to files.el of 1999-08-08. Investigate
1777 ;; this when we install the new VC.
1778 (and view-read-only
1779 (if (file-writable-p file)
1780 (and view-mode
1781 (let ((view-old-buffer-read-only nil))
1782 (view-mode-exit)))
1783 (and (not view-mode)
1784 (not (eq (get major-mode 'mode-class) 'special))
1785 (view-mode-enter))))
1786 (vc-mode-line buffer-file-name))
1787 (kill-buffer (current-buffer)))))
1789 (defun vc-resynch-buffer (file &optional keep noquery)
1790 "If FILE is currently visited, resynch its buffer."
1791 (if (string= buffer-file-name file)
1792 (vc-resynch-window file keep noquery)
1793 (let ((buffer (get-file-buffer file)))
1794 (when buffer
1795 (with-current-buffer buffer
1796 (vc-resynch-window file keep noquery)))))
1797 (vc-dired-resynch-file file)
1798 (when (memq 'vc-status-mark-buffer-changed after-save-hook)
1799 (let ((buffer (get-file-buffer file)))
1800 (vc-status-mark-buffer-changed file))))
1803 (defun vc-start-entry (files rev comment initial-contents msg action &optional after-hook)
1804 "Accept a comment for an operation on FILES revision REV.
1805 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1806 action on close to ACTION. If COMMENT is a string and
1807 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1808 contents of the log entry buffer. If COMMENT is a string and
1809 INITIAL-CONTENTS is nil, do action immediately as if the user had
1810 entered COMMENT. If COMMENT is t, also do action immediately with an
1811 empty comment. Remember the file's buffer in `vc-parent-buffer'
1812 \(current one if no file). AFTER-HOOK specifies the local value
1813 for `vc-log-after-operation-hook'."
1814 (let ((parent
1815 (if (or (eq major-mode 'vc-dired-mode) (eq major-mode 'vc-status-mode))
1816 ;; If we are called from VC dired, the parent buffer is
1817 ;; the current buffer.
1818 (current-buffer)
1819 (if (and files (equal (length files) 1))
1820 (get-file-buffer (car files))
1821 (current-buffer)))))
1822 (when vc-before-checkin-hook
1823 (if files
1824 (with-current-buffer parent
1825 (run-hooks 'vc-before-checkin-hook))
1826 (run-hooks 'vc-before-checkin-hook)))
1827 (if (and comment (not initial-contents))
1828 (set-buffer (get-buffer-create "*VC-log*"))
1829 (pop-to-buffer (get-buffer-create "*VC-log*")))
1830 (set (make-local-variable 'vc-parent-buffer) parent)
1831 (set (make-local-variable 'vc-parent-buffer-name)
1832 (concat " from " (buffer-name vc-parent-buffer)))
1833 ;;(if file (vc-mode-line file))
1834 (vc-log-edit files)
1835 (make-local-variable 'vc-log-after-operation-hook)
1836 (when after-hook
1837 (setq vc-log-after-operation-hook after-hook))
1838 (setq vc-log-operation action)
1839 (setq vc-log-revision rev)
1840 (when comment
1841 (erase-buffer)
1842 (when (stringp comment) (insert comment)))
1843 (if (or (not comment) initial-contents)
1844 (message "%s Type C-c C-c when done" msg)
1845 (vc-finish-logentry (eq comment t)))))
1847 (defun vc-checkout (file &optional writable rev)
1848 "Retrieve a copy of the revision REV of FILE.
1849 If WRITABLE is non-nil, make sure the retrieved file is writable.
1850 REV defaults to the latest revision.
1852 After check-out, runs the normal hook `vc-checkout-hook'."
1853 (and writable
1854 (not rev)
1855 (vc-call make-version-backups-p file)
1856 (vc-up-to-date-p file)
1857 (vc-make-version-backup file))
1858 (with-vc-properties
1859 (list file)
1860 (condition-case err
1861 (vc-call checkout file writable rev)
1862 (file-error
1863 ;; Maybe the backend is not installed ;-(
1864 (when writable
1865 (let ((buf (get-file-buffer file)))
1866 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1867 (signal (car err) (cdr err))))
1868 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1869 (not writable))
1870 (if (vc-call latest-on-branch-p file)
1871 'up-to-date
1872 'needs-patch)
1873 'edited))
1874 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1875 (vc-resynch-buffer file t t)
1876 (run-hooks 'vc-checkout-hook))
1878 (defun vc-steal-lock (file rev owner)
1879 "Steal the lock on FILE."
1880 (let (file-description)
1881 (if rev
1882 (setq file-description (format "%s:%s" file rev))
1883 (setq file-description file))
1884 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1885 file-description owner)))
1886 (error "Steal canceled"))
1887 (message "Stealing lock on %s..." file)
1888 (with-vc-properties
1889 (list file)
1890 (vc-call steal-lock file rev)
1891 `((vc-state . edited)))
1892 (vc-resynch-buffer file t t)
1893 (message "Stealing lock on %s...done" file)
1894 ;; Write mail after actually stealing, because if the stealing
1895 ;; goes wrong, we don't want to send any mail.
1896 (compose-mail owner (format "Stolen lock on %s" file-description))
1897 (setq default-directory (expand-file-name "~/"))
1898 (goto-char (point-max))
1899 (insert
1900 (format "I stole the lock on %s, " file-description)
1901 (current-time-string)
1902 ".\n")
1903 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1905 (defun vc-checkin (files &optional rev comment initial-contents)
1906 "Check in FILES.
1907 The optional argument REV may be a string specifying the new revision
1908 level (if nil increment the current level). COMMENT is a comment
1909 string; if omitted, a buffer is popped up to accept a comment. If
1910 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1911 of the log entry buffer.
1913 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1914 that the version control system supports this mode of operation.
1916 Runs the normal hook `vc-checkin-hook'."
1917 (vc-start-entry
1918 files rev comment initial-contents
1919 "Enter a change comment."
1920 (lambda (files rev comment)
1921 (message "Checking in %s..." (vc-delistify files))
1922 ;; "This log message intentionally left almost blank".
1923 ;; RCS 5.7 gripes about white-space-only comments too.
1924 (or (and comment (string-match "[^\t\n ]" comment))
1925 (setq comment "*** empty log message ***"))
1926 (with-vc-properties
1927 files
1928 ;; We used to change buffers to get local value of vc-checkin-switches,
1929 ;; but 'the' local buffer is not a well-defined concept for filesets.
1930 (progn
1931 (vc-call checkin files rev comment)
1932 (mapc 'vc-delete-automatic-version-backups files))
1933 `((vc-state . up-to-date)
1934 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1935 (vc-working-revision . nil)))
1936 (message "Checking in %s...done" (vc-delistify files)))
1937 'vc-checkin-hook))
1939 (defun vc-finish-logentry (&optional nocomment)
1940 "Complete the operation implied by the current log entry.
1941 Use the contents of the current buffer as a check-in or registration
1942 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1943 the buffer contents as a comment."
1944 (interactive)
1945 ;; Check and record the comment, if any.
1946 (unless nocomment
1947 ;; Comment too long?
1948 (vc-call-backend (or (if vc-log-fileset (vc-backend vc-log-fileset))
1949 (vc-responsible-backend default-directory))
1950 'logentry-check)
1951 (run-hooks 'vc-logentry-check-hook))
1952 ;; Sync parent buffer in case the user modified it while editing the comment.
1953 ;; But not if it is a vc-dired buffer.
1954 (with-current-buffer vc-parent-buffer
1955 (or vc-dired-mode (eq major-mode 'vc-status-mode) (vc-buffer-sync)))
1956 (if (not vc-log-operation)
1957 (error "No log operation is pending"))
1958 ;; save the parameters held in buffer-local variables
1959 (let ((log-operation vc-log-operation)
1960 (log-fileset vc-log-fileset)
1961 (log-revision vc-log-revision)
1962 (log-entry (buffer-string))
1963 (after-hook vc-log-after-operation-hook)
1964 (tmp-vc-parent-buffer vc-parent-buffer))
1965 (pop-to-buffer vc-parent-buffer)
1966 ;; OK, do it to it
1967 (save-excursion
1968 (funcall log-operation
1969 log-fileset
1970 log-revision
1971 log-entry))
1972 ;; Remove checkin window (after the checkin so that if that fails
1973 ;; we don't zap the *VC-log* buffer and the typing therein).
1974 (let ((logbuf (get-buffer "*VC-log*")))
1975 (cond ((and logbuf vc-delete-logbuf-window)
1976 (delete-windows-on logbuf (selected-frame))
1977 ;; Kill buffer and delete any other dedicated windows/frames.
1978 (kill-buffer logbuf))
1979 (logbuf (pop-to-buffer "*VC-log*")
1980 (bury-buffer)
1981 (pop-to-buffer tmp-vc-parent-buffer))))
1982 ;; Now make sure we see the expanded headers
1983 (if log-fileset
1984 (mapc
1985 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
1986 log-fileset))
1987 (if (or vc-dired-mode (eq major-mode 'vc-status-mode))
1988 (dired-move-to-filename))
1989 (run-hooks after-hook 'vc-finish-logentry-hook)))
1991 ;;; Additional entry points for examining version histories
1993 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1994 ;; "List differences for all registered files at and below DIR.
1995 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1996 ;; ;; This implementation does an explicit tree walk, and calls
1997 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1998 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1999 ;; ;; and to call it only for files that are actually changed.
2000 ;; ;; However, this is expensive for some backends, and so it is left
2001 ;; ;; to backend-specific implementations.
2002 ;; (setq default-directory dir)
2003 ;; (vc-file-tree-walk
2004 ;; default-directory
2005 ;; (lambda (f)
2006 ;; (vc-exec-after
2007 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
2008 ;; (message "Looking at %s" ',f)
2009 ;; (vc-call-backend ',(vc-backend f)
2010 ;; 'diff (list ',f) ',rev1 ',rev2))))))
2012 (defun vc-coding-system-for-diff (file)
2013 "Return the coding system for reading diff output for FILE."
2014 (or coding-system-for-read
2015 ;; if we already have this file open,
2016 ;; use the buffer's coding system
2017 (let ((buf (find-buffer-visiting file)))
2018 (if buf (with-current-buffer buf
2019 buffer-file-coding-system)))
2020 ;; otherwise, try to find one based on the file name
2021 (car (find-operation-coding-system 'insert-file-contents file))
2022 ;; and a final fallback
2023 'undecided))
2025 (defun vc-switches (backend op)
2026 (let ((switches
2027 (or (if backend
2028 (let ((sym (vc-make-backend-sym
2029 backend (intern (concat (symbol-name op)
2030 "-switches")))))
2031 (if (boundp sym) (symbol-value sym))))
2032 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
2033 (if (boundp sym) (symbol-value sym)))
2034 (cond
2035 ((eq op 'diff) diff-switches)))))
2036 (if (stringp switches) (list switches)
2037 ;; If not a list, return nil.
2038 ;; This is so we can set vc-diff-switches to t to override
2039 ;; any switches in diff-switches.
2040 (if (listp switches) switches))))
2042 ;; Old def for compatibility with Emacs-21.[123].
2043 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
2044 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
2046 (defun vc-diff-finish (buffer messages)
2047 ;; The empty sync output case has already been handled, so the only
2048 ;; possibility of an empty output is for an async process.
2049 (when (buffer-live-p buffer)
2050 (let ((window (get-buffer-window buffer t))
2051 (emptyp (zerop (buffer-size buffer))))
2052 (with-current-buffer buffer
2053 (and messages emptyp
2054 (let ((inhibit-read-only t))
2055 (insert (cdr messages) ".\n")
2056 (message "%s" (cdr messages))))
2057 (goto-char (point-min))
2058 (when window
2059 (shrink-window-if-larger-than-buffer window)))
2060 (when (and messages (not emptyp))
2061 (message "%sdone" (car messages))))))
2063 (defvar vc-diff-added-files nil
2064 "If non-nil, diff added files by comparing them to /dev/null.")
2066 (defun vc-diff-internal (async files rev1 rev2 &optional verbose)
2067 "Report diffs between two revisions of a fileset.
2068 Diff output goes to the *vc-diff* buffer. The function
2069 returns t if the buffer had changes, nil otherwise."
2070 (let* ((messages (cons (format "Finding changes in %s..."
2071 (vc-delistify files))
2072 (format "No changes between %s and %s"
2073 (or rev1 "working revision")
2074 (or rev2 "workfile"))))
2075 ;; Set coding system based on the first file. It's a kluge,
2076 ;; but the only way to set it for each file included would
2077 ;; be to call the back end separately for each file.
2078 (coding-system-for-read
2079 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
2080 (vc-setup-buffer "*vc-diff*")
2081 (message "%s" (car messages))
2082 ;; Many backends don't handle well the case of a file that has been
2083 ;; added but not yet committed to the repo (notably CVS and Subversion).
2084 ;; Do that work here so the backends don't have to futz with it. --ESR
2086 ;; Actually most backends (including CVS) have options to control the
2087 ;; behavior since which one is better depends on the user and on the
2088 ;; situation). Worse yet: this code does not handle the case where
2089 ;; `file' is a directory which contains added files.
2090 ;; I made it conditional on vc-diff-added-files but it should probably
2091 ;; just be removed (or copied/moved to specific backends). --Stef.
2092 (when vc-diff-added-files
2093 (let ((filtered '()))
2094 (dolist (file files)
2095 (if (or (file-directory-p file)
2096 (not (string= (vc-working-revision file) "0")))
2097 (push file filtered)
2098 ;; This file is added but not yet committed;
2099 ;; there is no master file to diff against.
2100 (if (or rev1 rev2)
2101 (error "No revisions of %s exist" file)
2102 ;; We regard this as "changed".
2103 ;; Diff it against /dev/null.
2104 (apply 'vc-do-command "*vc-diff*"
2105 1 "diff" file
2106 (append (vc-switches nil 'diff) '("/dev/null"))))))
2107 (setq files (nreverse filtered))))
2108 (let ((vc-disable-async-diff (not async)))
2109 (vc-call diff files rev1 rev2 "*vc-diff*"))
2110 (set-buffer "*vc-diff*")
2111 (if (and (zerop (buffer-size))
2112 (not (get-buffer-process (current-buffer))))
2113 ;; Treat this case specially so as not to pop the buffer.
2114 (progn
2115 (message "%s" (cdr messages))
2116 nil)
2117 (diff-mode)
2118 ;; Make the *vc-diff* buffer read only, the diff-mode key
2119 ;; bindings are nicer for read only buffers. pcl-cvs does the
2120 ;; same thing.
2121 (setq buffer-read-only t)
2122 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
2123 messages)))
2124 ;; Display the buffer, but at the end because it can change point.
2125 (pop-to-buffer (current-buffer))
2126 ;; In the async case, we return t even if there are no differences
2127 ;; because we don't know that yet.
2128 t)))
2130 ;;;###autoload
2131 (defun vc-version-diff (files rev1 rev2)
2132 "Report diffs between revisions of the fileset in the repository history."
2133 (interactive
2134 (let* ((files (vc-deduce-fileset t))
2135 (first (car files))
2136 (completion-table
2137 (vc-call revision-completion-table files))
2138 (rev1-default nil)
2139 (rev2-default nil))
2140 (cond
2141 ;; someday we may be able to do revision completion on non-singleton
2142 ;; filesets, but not yet.
2143 ((/= (length files) 1)
2144 nil)
2145 ;; if it's a directory, don't supply any revision default
2146 ((file-directory-p first)
2147 nil)
2148 ;; if the file is not up-to-date, use working revision as older revision
2149 ((not (vc-up-to-date-p first))
2150 (setq rev1-default (vc-working-revision first)))
2151 ;; if the file is not locked, use last and previous revisions as defaults
2153 (setq rev1-default (vc-call previous-revision first
2154 (vc-working-revision first)))
2155 (if (string= rev1-default "") (setq rev1-default nil))
2156 (setq rev2-default (vc-working-revision first))))
2157 ;; construct argument list
2158 (let* ((rev1-prompt (if rev1-default
2159 (concat "Older revision (default "
2160 rev1-default "): ")
2161 "Older revision: "))
2162 (rev2-prompt (concat "Newer revision (default "
2163 (or rev2-default "current source") "): "))
2164 (rev1 (if completion-table
2165 (completing-read rev1-prompt completion-table
2166 nil nil nil nil rev1-default)
2167 (read-string rev1-prompt nil nil rev1-default)))
2168 (rev2 (if completion-table
2169 (completing-read rev2-prompt completion-table
2170 nil nil nil nil rev2-default)
2171 (read-string rev2-prompt nil nil rev2-default))))
2172 (if (string= rev1 "") (setq rev1 nil))
2173 (if (string= rev2 "") (setq rev2 nil))
2174 (list files rev1 rev2))))
2175 (if (and (not rev1) rev2)
2176 (error "Not a valid revision range."))
2177 (vc-diff-internal t files rev1 rev2 (interactive-p)))
2179 ;; (defun vc-contains-version-controlled-file (dir)
2180 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
2181 ;; (catch 'found
2182 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
2183 ;; nil))
2185 ;;;###autoload
2186 (defun vc-diff (historic &optional not-urgent)
2187 "Display diffs between file revisions.
2188 Normally this compares the currently selected fileset with their
2189 working revisions. With a prefix argument HISTORIC, it reads two revision
2190 designators specifying which revisions to compare.
2192 If no current fileset is available (that is, we are not in
2193 VC-Dired mode and the visited file of the current buffer is not
2194 under version control) and we're in a Dired buffer, use
2195 the current directory.
2196 The optional argument NOT-URGENT non-nil means it is ok to say no to
2197 saving the buffer."
2198 (interactive (list current-prefix-arg t))
2199 (if historic
2200 (call-interactively 'vc-version-diff)
2201 (let* ((files (vc-deduce-fileset t)))
2202 (if buffer-file-name (vc-buffer-sync not-urgent))
2203 (vc-diff-internal t files nil nil (interactive-p)))))
2206 ;;;###autoload
2207 (defun vc-revision-other-window (rev)
2208 "Visit revision REV of the current file in another window.
2209 If the current file is named `F', the revision is named `F.~REV~'.
2210 If `F.~REV~' already exists, use it instead of checking it out again."
2211 (interactive
2212 (save-current-buffer
2213 (vc-ensure-vc-buffer)
2214 (let ((completion-table
2215 (vc-call revision-completion-table buffer-file-name))
2216 (prompt "Revision to visit (default is working revision): "))
2217 (list
2218 (if completion-table
2219 (completing-read prompt completion-table)
2220 (read-string prompt))))))
2221 (vc-ensure-vc-buffer)
2222 (let* ((file buffer-file-name)
2223 (revision (if (string-equal rev "")
2224 (vc-working-revision file)
2225 rev)))
2226 (switch-to-buffer-other-window (vc-find-revision file revision))))
2228 (defun vc-find-revision (file revision)
2229 "Read REVISION of FILE into a buffer and return the buffer."
2230 (let ((automatic-backup (vc-version-backup-file-name file revision))
2231 (filebuf (or (get-file-buffer file) (current-buffer)))
2232 (filename (vc-version-backup-file-name file revision 'manual)))
2233 (unless (file-exists-p filename)
2234 (if (file-exists-p automatic-backup)
2235 (rename-file automatic-backup filename nil)
2236 (message "Checking out %s..." filename)
2237 (with-current-buffer filebuf
2238 (let ((failed t))
2239 (unwind-protect
2240 (let ((coding-system-for-read 'no-conversion)
2241 (coding-system-for-write 'no-conversion))
2242 (with-temp-file filename
2243 (let ((outbuf (current-buffer)))
2244 ;; Change buffer to get local value of
2245 ;; vc-checkout-switches.
2246 (with-current-buffer filebuf
2247 (vc-call find-revision file revision outbuf))))
2248 (setq failed nil))
2249 (when (and failed (file-exists-p filename))
2250 (delete-file filename))))
2251 (vc-mode-line file))
2252 (message "Checking out %s...done" filename)))
2253 (let ((result-buf (find-file-noselect filename)))
2254 (with-current-buffer result-buf
2255 ;; Set the parent buffer so that things like
2256 ;; C-x v g, C-x v l, ... etc work.
2257 (set (make-local-variable 'vc-parent-buffer) filebuf))
2258 result-buf)))
2260 ;; Header-insertion code
2262 ;;;###autoload
2263 (defun vc-insert-headers ()
2264 "Insert headers into a file for use with a version control system.
2265 Headers desired are inserted at point, and are pulled from
2266 the variable `vc-BACKEND-header'."
2267 (interactive)
2268 (vc-ensure-vc-buffer)
2269 (save-excursion
2270 (save-restriction
2271 (widen)
2272 (if (or (not (vc-check-headers))
2273 (y-or-n-p "Version headers already exist. Insert another set? "))
2274 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2275 (comment-start-vc (or (car delims) comment-start "#"))
2276 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2277 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2278 'header))
2279 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2280 (dolist (s hdstrings)
2281 (insert comment-start-vc "\t" s "\t"
2282 comment-end-vc "\n"))
2283 (if vc-static-header-alist
2284 (dolist (f vc-static-header-alist)
2285 (if (string-match (car f) buffer-file-name)
2286 (insert (format (cdr f) (car hdstrings)))))))))))
2288 (defun vc-clear-headers (&optional file)
2289 "Clear all version headers in the current buffer (or FILE).
2290 The headers are reset to their non-expanded form."
2291 (let* ((filename (or file buffer-file-name))
2292 (visited (find-buffer-visiting filename))
2293 (backend (vc-backend filename)))
2294 (when (vc-find-backend-function backend 'clear-headers)
2295 (if visited
2296 (let ((context (vc-buffer-context)))
2297 ;; save-excursion may be able to relocate point and mark
2298 ;; properly. If it fails, vc-restore-buffer-context
2299 ;; will give it a second try.
2300 (save-excursion
2301 (vc-call-backend backend 'clear-headers))
2302 (vc-restore-buffer-context context))
2303 (set-buffer (find-file-noselect filename))
2304 (vc-call-backend backend 'clear-headers)
2305 (kill-buffer filename)))))
2307 (defun vc-modify-change-comment (files rev oldcomment)
2308 "Edit the comment associated with the given files and revision."
2309 (vc-start-entry
2310 files rev oldcomment t
2311 "Enter a replacement change comment."
2312 (lambda (files rev comment)
2313 (vc-call-backend
2314 ;; Less of a kluge than it looks like; log-view mode only passes
2315 ;; this function a singleton list. Arguments left in this form in
2316 ;; case the more general operation ever becomes meaningful.
2317 (vc-responsible-backend (car files))
2318 'modify-change-comment files rev comment))))
2320 ;;;###autoload
2321 (defun vc-merge ()
2322 "Merge changes between two revisions into the current buffer's file.
2323 This asks for two revisions to merge from in the minibuffer. If the
2324 first revision is a branch number, then merge all changes from that
2325 branch. If the first revision is empty, merge news, i.e. recent changes
2326 from the current branch.
2328 See Info node `Merging'."
2329 (interactive)
2330 (vc-ensure-vc-buffer)
2331 (vc-buffer-sync)
2332 (let* ((file buffer-file-name)
2333 (backend (vc-backend file))
2334 (state (vc-state file))
2335 first-revision second-revision status)
2336 (cond
2337 ((stringp state) ;; Locking VCses only
2338 (error "File is locked by %s" state))
2339 ((not (vc-editable-p file))
2340 (if (y-or-n-p
2341 "File must be checked out for merging. Check out now? ")
2342 (vc-checkout file t)
2343 (error "Merge aborted"))))
2344 (setq first-revision
2345 (read-string (concat "Branch or revision to merge from "
2346 "(default news on current branch): ")))
2347 (if (string= first-revision "")
2348 (if (not (vc-find-backend-function backend 'merge-news))
2349 (error "Sorry, merging news is not implemented for %s" backend)
2350 (setq status (vc-call merge-news file)))
2351 (if (not (vc-find-backend-function backend 'merge))
2352 (error "Sorry, merging is not implemented for %s" backend)
2353 (if (not (vc-branch-p first-revision))
2354 (setq second-revision
2355 (read-string "Second revision: "
2356 (concat (vc-branch-part first-revision) ".")))
2357 ;; We want to merge an entire branch. Set revisions
2358 ;; accordingly, so that vc-BACKEND-merge understands us.
2359 (setq second-revision first-revision)
2360 ;; first-revision must be the starting point of the branch
2361 (setq first-revision (vc-branch-part first-revision)))
2362 (setq status (vc-call merge file first-revision second-revision))))
2363 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2365 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2366 (vc-resynch-buffer file t (not (buffer-modified-p)))
2367 (if (zerop status) (message "Merge successful")
2368 (smerge-mode 1)
2369 (message "File contains conflicts.")))
2371 ;;;###autoload
2372 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2374 ;; The VC directory major mode. Coopt Dired for this.
2375 ;; All VC commands get mapped into logical equivalents.
2377 (defvar vc-dired-switches)
2378 (defvar vc-dired-terse-mode)
2380 (defvar vc-dired-mode-map
2381 (let ((map (make-sparse-keymap))
2382 (vmap (make-sparse-keymap)))
2383 (define-key map "\C-xv" vmap)
2384 (define-key map "v" vmap)
2385 (set-keymap-parent vmap vc-prefix-map)
2386 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2387 map))
2389 (define-derived-mode vc-dired-mode dired-mode "Dired under "
2390 "The major mode used in VC directory buffers.
2392 It works like Dired, but lists only files under version control, with
2393 the current VC state of each file being indicated in the place of the
2394 file's link count, owner, group and size. Subdirectories are also
2395 listed, and you may insert them into the buffer as desired, like in
2396 Dired.
2398 All Dired commands operate normally, with the exception of `v', which
2399 is redefined as the version control prefix, so that you can type
2400 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2401 the file named in the current Dired buffer line. `vv' invokes
2402 `vc-next-action' on this file, or on all files currently marked.
2403 There is a special command, `*l', to mark all files currently locked."
2404 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2405 ;; We do it here because dired might not be loaded yet
2406 ;; when vc-dired-mode-map is initialized.
2407 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2408 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2409 ;; The following is slightly modified from files.el,
2410 ;; because file lines look a bit different in vc-dired-mode
2411 ;; (the column before the date does not end in a digit).
2412 ;; albinus: It should be done in the original declaration. Problem
2413 ;; is the optional empty state-info; otherwise ")" would be good
2414 ;; enough as delimeter.
2415 (set (make-local-variable 'directory-listing-before-filename-regexp)
2416 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2417 ;; In some locales, month abbreviations are as short as 2 letters,
2418 ;; and they can be followed by ".".
2419 (month (concat l l "+\\.?"))
2420 (s " ")
2421 (yyyy "[0-9][0-9][0-9][0-9]")
2422 (dd "[ 0-3][0-9]")
2423 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2424 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2425 (zone "[-+][0-2][0-9][0-5][0-9]")
2426 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2427 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2428 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2429 "\\|" yyyy "-" iso-mm-dd "\\)"))
2430 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2431 s "+"
2432 "\\(" HH:MM "\\|" yyyy "\\)"))
2433 (western-comma (concat month s "+" dd "," s "+" yyyy))
2434 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2435 ;; omits the Kanji characters after month and day-of-month.
2436 (mm "[ 0-1]?[0-9]")
2437 (japanese
2438 (concat mm l "?" s dd l "?" s "+"
2439 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2440 ;; the .* below ensures that we find the last match on a line
2441 (concat ".*" s
2442 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2443 s "+")))
2444 (and (boundp 'vc-dired-switches)
2445 vc-dired-switches
2446 (set (make-local-variable 'dired-actual-switches)
2447 vc-dired-switches))
2448 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2449 (let ((backend-name (symbol-name (vc-responsible-backend
2450 default-directory))))
2451 (setq mode-name (concat mode-name backend-name))
2452 ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
2453 (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
2454 (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
2455 (cons backend-name vc-dire-menu-map) 'subdir)))
2456 (setq vc-dired-mode t))
2458 (defun vc-dired-toggle-terse-mode ()
2459 "Toggle terse display in VC Dired."
2460 (interactive)
2461 (if (not vc-dired-mode)
2463 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2464 (if vc-dired-terse-mode
2465 (vc-dired-hook)
2466 (revert-buffer))))
2468 (defun vc-dired-mark-locked ()
2469 "Mark all files currently locked."
2470 (interactive)
2471 (dired-mark-if (let ((f (dired-get-filename nil t)))
2472 (and f
2473 (not (file-directory-p f))
2474 (not (vc-up-to-date-p f))))
2475 "locked file"))
2477 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2479 (defun vc-dired-reformat-line (vc-info)
2480 "Reformat a directory-listing line.
2481 Replace various columns with version control information, VC-INFO.
2482 This code, like dired, assumes UNIX -l format."
2483 (beginning-of-line)
2484 (when (re-search-forward
2485 ;; Match link count, owner, group, size. Group may be missing,
2486 ;; and only the size is present in OS/2 -l format.
2487 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2488 (line-end-position) t)
2489 (replace-match (substring (concat vc-info " ") 0 10)
2490 t t nil 1)))
2492 (defun vc-dired-ignorable-p (filename)
2493 "Should FILENAME be ignored in VC-Dired listings?"
2494 (catch t
2495 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
2496 (dolist (ignorable completion-ignored-extensions)
2497 (let ((ext (substring filename
2498 (- (length filename)
2499 (length ignorable)))))
2500 (if (string= ignorable ext) (throw t t))))
2501 ;; Ignore Makefiles derived from something else
2502 (when (string= (file-name-nondirectory filename) "Makefile")
2503 (let* ((dir (file-name-directory filename))
2504 (peers (directory-files (or dir default-directory))))
2505 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
2506 (throw t t))))
2507 nil))
2509 (defun vc-dired-hook ()
2510 "Reformat the listing according to version control.
2511 Called by dired after any portion of a vc-dired buffer has been read in."
2512 (message "Getting version information... ")
2513 ;; if the backend supports it, get the state
2514 ;; of all files in this directory at once
2515 (let ((backend (vc-responsible-backend default-directory)))
2516 ;; check `backend' can really handle `default-directory'.
2517 (if (and (vc-call-backend backend 'responsible-p default-directory)
2518 (vc-find-backend-function backend 'dir-state))
2519 (vc-call-backend backend 'dir-state default-directory)))
2520 (let (filename
2521 (inhibit-read-only t)
2522 (buffer-undo-list t))
2523 (goto-char (point-min))
2524 (while (not (eobp))
2525 (cond
2526 ;; subdir header line
2527 ((dired-get-subdir)
2528 (forward-line 1)
2529 ;; erase (but don't remove) the "total" line
2530 (delete-region (point) (line-end-position))
2531 (beginning-of-line)
2532 (forward-line 1))
2533 ;; file line
2534 ((setq filename (dired-get-filename nil t))
2535 (cond
2536 ;; subdir
2537 ((file-directory-p filename)
2538 (cond
2539 ((member (file-name-nondirectory filename)
2540 vc-directory-exclusion-list)
2541 (let ((pos (point)))
2542 (dired-kill-tree filename)
2543 (goto-char pos)
2544 (dired-kill-line)))
2545 (vc-dired-terse-mode
2546 ;; Don't show directories in terse mode. Don't use
2547 ;; dired-kill-line to remove it, because in recursive listings,
2548 ;; that would remove the directory contents as well.
2549 (delete-region (line-beginning-position)
2550 (progn (forward-line 1) (point))))
2551 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2552 (dired-kill-line))
2554 (vc-dired-reformat-line nil)
2555 (forward-line 1))))
2556 ;; Try to head off calling the expensive state query -
2557 ;; ignore object files, TeX intermediate files, and so forth.
2558 ((vc-dired-ignorable-p filename)
2559 (dired-kill-line))
2560 ;; Ordinary file -- call the (possibly expensive) state query
2562 ;; First case: unregistered or unknown. (Unknown shouldn't happen here)
2563 ((member (vc-state filename) '(nil unregistered))
2564 (if vc-dired-terse-mode
2565 (dired-kill-line)
2566 (vc-dired-reformat-line "?")
2567 (forward-line 1)))
2568 ;; Either we're in non-terse mode or it's out of date
2569 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
2570 (vc-dired-reformat-line (vc-call dired-state-info filename))
2571 (forward-line 1))
2572 ;; Remaining cases are under version control but uninteresting
2574 (dired-kill-line))))
2575 ;; any other line
2576 (t (forward-line 1))))
2577 (vc-dired-purge))
2578 (message "Getting version information... done")
2579 (save-restriction
2580 (widen)
2581 (cond ((eq (count-lines (point-min) (point-max)) 1)
2582 (goto-char (point-min))
2583 (message "No changes pending under %s" default-directory)))))
2585 (defun vc-dired-purge ()
2586 "Remove empty subdirs."
2587 (goto-char (point-min))
2588 (while (dired-get-subdir)
2589 (forward-line 2)
2590 (if (dired-get-filename nil t)
2591 (if (not (dired-next-subdir 1 t))
2592 (goto-char (point-max)))
2593 (forward-line -2)
2594 (if (not (string= (dired-current-directory) default-directory))
2595 (dired-do-kill-lines t "")
2596 ;; We cannot remove the top level directory.
2597 ;; Just make it look a little nicer.
2598 (forward-line 1)
2599 (or (eobp) (kill-line))
2600 (if (not (dired-next-subdir 1 t))
2601 (goto-char (point-max))))))
2602 (goto-char (point-min)))
2604 (defun vc-dired-buffers-for-dir (dir)
2605 "Return a list of all vc-dired buffers that currently display DIR."
2606 (let (result)
2607 ;; Check whether dired is loaded.
2608 (when (fboundp 'dired-buffers-for-dir)
2609 (dolist (buffer (dired-buffers-for-dir dir))
2610 (with-current-buffer buffer
2611 (if vc-dired-mode
2612 (push buffer result)))))
2613 (nreverse result)))
2615 (defun vc-dired-resynch-file (file)
2616 "Update the entries for FILE in any VC Dired buffers that list it."
2617 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2618 (when buffers
2619 (mapcar (lambda (buffer)
2620 (with-current-buffer buffer
2621 (if (dired-goto-file file)
2622 ;; bind vc-dired-terse-mode to nil so that
2623 ;; files won't vanish when they are checked in
2624 (let ((vc-dired-terse-mode nil))
2625 (dired-do-redisplay 1)))))
2626 buffers))))
2628 ;;;###autoload
2629 (defun vc-directory (dir read-switches)
2630 "Create a buffer in VC Dired Mode for directory DIR.
2632 See Info node `VC Dired Mode'.
2634 With prefix arg READ-SWITCHES, specify a value to override
2635 `dired-listing-switches' when generating the listing."
2636 (interactive "DDired under VC (directory): \nP")
2637 (let ((vc-dired-switches (concat vc-dired-listing-switches
2638 (if vc-dired-recurse "R" ""))))
2639 (if read-switches
2640 (setq vc-dired-switches
2641 (read-string "Dired listing switches: "
2642 vc-dired-switches)))
2643 (require 'dired)
2644 (require 'dired-aux)
2645 (switch-to-buffer
2646 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2647 vc-dired-switches
2648 'vc-dired-mode))))
2650 ;;; Experimental code for the vc-dired replacement
2651 (require 'ewoc)
2653 (defstruct (vc-status-fileinfo
2654 (:copier nil)
2655 (:constructor
2656 vc-status-create-fileinfo (name state extra &optional marked))
2657 (:conc-name vc-status-fileinfo->))
2658 marked
2659 state
2660 name
2661 ;; For storing backend specific information.
2662 extra)
2664 (defvar vc-status nil)
2666 (defun vc-default-status-extra-headers (backend dir)
2667 "Extra : Add backend specific headers here")
2669 (defun vc-status-headers (backend dir)
2670 (concat
2671 (propertize "VC backend : " 'face 'font-lock-type-face)
2672 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
2673 (propertize "Working dir: " 'face 'font-lock-type-face)
2674 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
2675 (vc-call-backend backend 'status-extra-headers dir)
2676 "\n"))
2678 (defun vc-default-status-printer (backend fileentry)
2679 "Pretty print FILEENTRY."
2680 ;; If you change the layout here, change vc-status-move-to-goal-column.
2681 (let ((state (vc-status-fileinfo->state fileentry)))
2682 (insert
2683 (propertize
2684 (format "%c" (if (vc-status-fileinfo->marked fileentry) ?* ? ))
2685 'face 'font-lock-type-face)
2687 (propertize
2688 (format "%-20s" state)
2689 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
2690 ((eq state 'missing) 'font-lock-warning-face)
2691 (t 'font-lock-variable-name-face))
2692 'mouse-face 'highlight)
2694 (propertize
2695 (format "%s" (vc-status-fileinfo->name fileentry))
2696 'face 'font-lock-function-name-face
2697 'mouse-face 'highlight))))
2699 (defun vc-status-printer (fileentry)
2700 (let ((backend (vc-responsible-backend default-directory)))
2701 (vc-call-backend backend 'status-printer fileentry)))
2703 (defun vc-status-move-to-goal-column ()
2704 (beginning-of-line)
2705 ;; Must be in sync with vc-default-status-printer.
2706 (forward-char 25))
2708 (defun vc-status-prepare-status-buffer (dir &optional create-new)
2709 "Find a *vc-status* buffer showing DIR, or create a new one."
2710 (setq dir (expand-file-name dir))
2711 (let* ((bname "*vc-status*")
2712 ;; Look for another *vc-status* buffer visiting the same directory.
2713 (buf (save-excursion
2714 (unless create-new
2715 (dolist (buffer (buffer-list))
2716 (set-buffer buffer)
2717 (when (and (eq major-mode 'vc-status-mode)
2718 (string= (expand-file-name default-directory) dir))
2719 (return buffer)))))))
2720 (if buf
2722 ;; Create a new *vc-status* buffer.
2723 (with-current-buffer (create-file-buffer bname)
2724 (cd dir)
2725 (vc-setup-buffer (current-buffer))
2726 (current-buffer)))))
2728 ;;;###autoload
2729 (defun vc-status (dir)
2730 "Show the VC status for DIR."
2731 (interactive "DVC status for directory: ")
2732 (switch-to-buffer (vc-status-prepare-status-buffer dir))
2733 (if (eq major-mode 'vc-status-mode)
2734 (vc-status-refresh)
2735 (vc-status-mode)))
2737 (defvar vc-status-menu-map
2738 (let ((map (make-sparse-keymap "VC-status")))
2739 (define-key map [quit]
2740 '(menu-item "Quit" bury-buffer
2741 :help "Quit"))
2742 (define-key map [kill]
2743 '(menu-item "Kill Update Command" vc-status-kill-dir-status-process
2744 :enable vc-status-process-buffer
2745 :help "Kill the command that updates VC status buffer"))
2746 (define-key map [refresh]
2747 '(menu-item "Refresh" vc-status-refresh
2748 :enable (not vc-status-process-buffer)
2749 :help "Refresh the contents of the VC status buffer"))
2750 (define-key map [remup]
2751 '(menu-item "Hide up-to-date" vc-status-hide-up-to-date
2752 :help "Hide up-to-date items from display"))
2753 ;; VC commands.
2754 (define-key map [sepvccmd] '("--"))
2755 (define-key map [annotate]
2756 '(menu-item "Annotate" vc-annotate
2757 :help "Display the edit history of the current file using colors"))
2758 (define-key map [diff]
2759 '(menu-item "Compare with Base Version" vc-diff
2760 :help "Compare file set with the base version"))
2761 (define-key map [register]
2762 '(menu-item "Register" vc-status-register
2763 :help "Register file set into the version control system"))
2764 (define-key map [update]
2765 '(menu-item "Update to latest version" vc-update
2766 :help "Update the current fileset's files to their tip revisions"))
2767 (define-key map [revert]
2768 '(menu-item "Revert to base version" vc-revert
2769 :help "Revert working copies of the selected fileset to their repository contents."))
2770 (define-key map [log]
2771 '(menu-item "Show history" vc-print-log
2772 :help "List the change log of the current file set in a window"))
2774 ;; Movement.
2775 (define-key map [sepmv] '("--"))
2776 (define-key map [next-line]
2777 '(menu-item "Next line" vc-status-next-line
2778 :help "Go to the next line" :keys "n"))
2779 (define-key map [previous-line]
2780 '(menu-item "Previous line" vc-status-previous-line
2781 :help "Go to the previous line"))
2782 ;; Marking.
2783 (define-key map [sepmrk] '("--"))
2784 (define-key map [unmark-all]
2785 '(menu-item "Unmark All" vc-status-unmark-all-files
2786 :help "Unmark all files that are in the same state as the current file\
2787 \nWith prefix argument unmark all files"))
2788 (define-key map [unmark-previous]
2789 '(menu-item "Unmark previous " vc-status-unmark-file-up
2790 :help "Move to the previous line and unmark the file"))
2792 (define-key map [mark-all]
2793 '(menu-item "Mark All" vc-status-mark-all-files
2794 :help "Mark all files that are in the same state as the current file\
2795 \nWith prefix argument mark all files"))
2796 (define-key map [unmark]
2797 '(menu-item "Unmark" vc-status-unmark
2798 :help "Unmark the current file or all files in the region"))
2800 (define-key map [mark]
2801 '(menu-item "Mark" vc-status-mark
2802 :help "Mark the current file or all files in the region"))
2804 (define-key map [sepopn] '("--"))
2805 (define-key map [open-other]
2806 '(menu-item "Open in other window" vc-status-find-file-other-window
2807 :help "Find the file on the current line, in another window"))
2808 (define-key map [open]
2809 '(menu-item "Open file" vc-status-find-file
2810 :help "Find the file on the current line"))
2811 map)
2812 "Menu for VC status")
2814 (defalias 'vc-status-menu-map vc-status-menu-map)
2816 (defvar vc-status-mode-map
2817 (let ((map (make-keymap)))
2818 (suppress-keymap map)
2819 ;; Marking.
2820 (define-key map "m" 'vc-status-mark)
2821 (define-key map "M" 'vc-status-mark-all-files)
2822 (define-key map "u" 'vc-status-unmark)
2823 (define-key map "\C-?" 'vc-status-unmark-file-up)
2824 (define-key map "\M-\C-?" 'vc-status-unmark-all-files)
2825 ;; Movement.
2826 (define-key map "n" 'vc-status-next-line)
2827 (define-key map " " 'vc-status-next-line)
2828 (define-key map "\t" 'vc-status-next-line)
2829 (define-key map "p" 'vc-status-previous-line)
2830 (define-key map [backtab] 'vc-status-previous-line)
2831 ;; VC commands.
2832 (define-key map "=" 'vc-diff) ;; C-x v =
2833 (define-key map "a" 'vc-status-register)
2834 (define-key map "+" 'vc-update) ;; C-x v +
2835 (define-key map "U" 'vc-revert) ;; u is taken by unmark.
2836 ;; Can't be "g" (as in vc map), so "A" for "Annotate".
2837 (define-key map "A" 'vc-annotate)
2838 (define-key map "l" 'vc-print-log) ;; C-x v l
2839 ;; The remainder.
2840 (define-key map "f" 'vc-status-find-file)
2841 (define-key map "\C-m" 'vc-status-find-file)
2842 (define-key map "o" 'vc-status-find-file-other-window)
2843 (define-key map "x" 'vc-status-hide-up-to-date)
2844 (define-key map "q" 'bury-buffer)
2845 (define-key map "g" 'vc-status-refresh)
2846 (define-key map "\C-c\C-c" 'vc-status-kill-dir-status-process)
2847 ;; Not working yet. Functions like vc-status-find-file need to
2848 ;; find the file from the mouse position, not `point'.
2849 ;; (define-key map [(down-mouse-3)] 'vc-status-menu)
2851 ;; Hook up the menu.
2852 (define-key map [menu-bar vc-status-mode]
2853 '(menu-item
2854 ;; This is used to that VC backends could add backend specific
2855 ;; menu items to vc-status-menu-map.
2856 "VC Status" vc-status-menu-map :filter vc-status-menu-map-filter))
2857 map)
2858 "Keymap for VC status")
2860 (defun vc-default-extra-status-menu (backend)
2861 nil)
2863 (defun vc-status-menu-map-filter (orig-binding)
2864 (when (and (symbolp orig-binding) (fboundp orig-binding))
2865 (setq orig-binding (indirect-function orig-binding)))
2866 (let ((ext-binding
2867 (vc-call-backend (vc-responsible-backend default-directory)
2868 'extra-status-menu)))
2869 (if (null ext-binding)
2870 orig-binding
2871 (append orig-binding
2872 '("----")
2873 ext-binding))))
2875 (defun vc-status-menu (e)
2876 "Popup the VC status menu."
2877 (interactive "e")
2878 (popup-menu vc-status-menu-map e))
2880 (defvar vc-status-tool-bar-map
2881 (let ((map (make-sparse-keymap)))
2882 (tool-bar-local-item-from-menu 'vc-status-find-file "open"
2883 map vc-status-mode-map)
2884 (tool-bar-local-item "bookmark_add"
2885 'vc-status-toggle-mark 'vc-status-toggle-mark map
2886 :help "Toggle mark on current item")
2887 (tool-bar-local-item-from-menu 'vc-status-previous-line "left-arrow"
2888 map vc-status-mode-map
2889 :rtl "right-arrow")
2890 (tool-bar-local-item-from-menu 'vc-status-next-line "right-arrow"
2891 map vc-status-mode-map
2892 :rtl "left-arrow")
2893 (tool-bar-local-item-from-menu 'vc-status-refresh "refresh"
2894 map vc-status-mode-map)
2895 (tool-bar-local-item-from-menu 'nonincremental-search-forward
2896 "search" map)
2897 (tool-bar-local-item-from-menu 'vc-status-kill-dir-status-process "cancel"
2898 map vc-status-mode-map)
2899 (tool-bar-local-item-from-menu 'bury-buffer "exit"
2900 map vc-status-mode-map)
2901 map))
2903 (defvar vc-status-process-buffer nil
2904 "The buffer used for the asynchronous call that computes the VC status.")
2906 (defvar vc-status-crt-marked nil
2907 "The list of marked files before `vc-status-refresh'.")
2909 (defun vc-status-mode ()
2910 "Major mode for VC status.
2911 \\{vc-status-mode-map}"
2912 (setq mode-name "*VC Status*")
2913 (setq major-mode 'vc-status-mode)
2914 (setq buffer-read-only t)
2915 (set (make-local-variable 'vc-status-crt-marked) nil)
2916 (use-local-map vc-status-mode-map)
2917 (set (make-local-variable 'tool-bar-map) vc-status-tool-bar-map)
2918 (let ((buffer-read-only nil)
2919 (backend (vc-responsible-backend default-directory))
2920 entries)
2921 (erase-buffer)
2922 (set (make-local-variable 'vc-status-process-buffer) nil)
2923 (set (make-local-variable 'vc-status)
2924 (ewoc-create #'vc-status-printer
2925 (vc-status-headers backend default-directory)))
2926 (add-hook 'after-save-hook 'vc-status-mark-buffer-changed)
2927 (vc-status-refresh)))
2929 (put 'vc-status-mode 'mode-class 'special)
2931 (defun vc-update-vc-status-buffer (entries buffer)
2932 (with-current-buffer buffer
2933 (when entries
2934 ;; Insert the entries we got into the ewoc.
2935 (dolist (entry entries)
2936 (let* ((file (car entry))
2937 (entrycdr (cdr entry))
2938 (state (if (listp entrycdr) (nth 1 entry)))
2939 (extra (if (listp entrycdr) (nth 2 entry))))
2940 (ewoc-enter-last vc-status
2941 (vc-status-create-fileinfo file state extra))))
2942 ;; If we had marked items before the refresh, try mark them here.
2943 ;; XXX: there should be a better way to do this...
2944 (when vc-status-crt-marked
2945 (ewoc-map
2946 (lambda (arg)
2947 (when (member (vc-status-fileinfo->name arg) vc-status-crt-marked)
2948 (setf (vc-status-fileinfo->marked arg) t)))
2949 vc-status))
2950 (ewoc-goto-node vc-status (ewoc-nth vc-status 0)))
2951 (setq vc-status-process-buffer nil)
2952 ;; We are done, turn off the mode-line "in progress" message.
2953 (setq mode-line-process nil)))
2955 (defun vc-status-add-entry (entry buffer)
2956 ;; Add one ENTRY to the vc-status buffer BUFFER.
2957 ;; This will be used to automatically add files with the "modified"
2958 ;; state when saving them.
2960 ;; ENTRY is (FILENAME . STATE)
2961 (with-current-buffer buffer
2962 (let ((crt (ewoc-nth vc-status 0))
2963 (fname (car entry)))
2964 ;; First try to see if there's already an entry with that name
2965 ;; in the ewoc.
2966 (while (and crt (not (string= (vc-status-fileinfo->name
2967 (ewoc-data crt)) fname)))
2968 (setq crt (ewoc-next vc-status crt)))
2969 (if crt
2970 (progn
2971 ;; Found the file, just update the status.
2972 (setf (vc-status-fileinfo->state (ewoc-data crt)) (cdr entry))
2973 (ewoc-invalidate vc-status crt))
2974 ;; Could not find the file, insert a new entry.
2975 (ewoc-enter-last
2976 ;; XXX: `vc-status-fileinfo->extra' is not set here.
2977 ;; It might need to be.
2978 vc-status
2979 (vc-status-create-fileinfo (car entry) (cdr entry) nil))))))
2981 (defun vc-status-refresh ()
2982 "Refresh the contents of the VC status buffer.
2983 Throw an error if another update process is in progress."
2984 (interactive)
2985 (if vc-status-process-buffer
2986 (error "Another update process is in progress, cannot run two at a time")
2987 ;; We clear the ewoc, but remember the marked files so that we can
2988 ;; mark them again after the refresh is done.
2989 ;; This is not very efficient; ewoc could use a new function here.
2990 (setq vc-status-crt-marked
2991 (mapcar
2992 (lambda (elem)
2993 (vc-status-fileinfo->name elem))
2994 (ewoc-collect
2995 vc-status
2996 (lambda (crt) (vc-status-fileinfo->marked crt)))))
2997 (ewoc-filter vc-status (lambda (node) nil))
2999 (let ((backend (vc-responsible-backend default-directory))
3000 (status-buffer (current-buffer))
3001 (def-dir default-directory))
3002 (vc-set-mode-line-busy-indicator)
3003 ;; Call the `dir-status' backend function.
3004 ;; `dir-status' is supposed to be asynchronous.
3005 ;; It should compute the results, and then call the function
3006 ;; passed as an argument in order to update the vc-status buffer
3007 ;; with the results.
3009 ;; Create a buffer that can be used by `dir-status' and call
3010 ;; `dir-status' with this buffer as the current buffer. Use
3011 ;; `vc-status-process-buffer' to remember this buffer, so that
3012 ;; it can be used later to kill the update process in case it
3013 ;; takes too long.
3014 (setq vc-status-process-buffer
3015 (get-buffer-create
3016 (generate-new-buffer-name (format " *VC-%s* status" backend))))
3017 (with-current-buffer vc-status-process-buffer
3018 (cd def-dir)
3019 (erase-buffer)
3020 (vc-call-backend backend 'dir-status def-dir
3021 #'vc-update-vc-status-buffer status-buffer)))))
3023 (defun vc-status-kill-dir-status-process ()
3024 "Kill the temporary buffer and associated process."
3025 (interactive)
3026 (when (buffer-live-p vc-status-process-buffer)
3027 (let ((proc (get-buffer-process vc-status-process-buffer)))
3028 (when proc (delete-process proc))
3029 (setq vc-status-process-buffer nil)
3030 (setq mode-line-process nil))))
3032 (defun vc-status-next-line (arg)
3033 "Go to the next line.
3034 If a prefix argument is given, move by that many lines."
3035 (interactive "p")
3036 (ewoc-goto-next vc-status arg)
3037 (vc-status-move-to-goal-column))
3039 (defun vc-status-previous-line (arg)
3040 "Go to the previous line.
3041 If a prefix argument is given, move by that many lines."
3042 (interactive "p")
3043 (ewoc-goto-prev vc-status arg)
3044 (vc-status-move-to-goal-column))
3046 (defun vc-status-mark-unmark (mark-unmark-function)
3047 (if (use-region-p)
3048 (let ((firstl (line-number-at-pos (region-beginning)))
3049 (lastl (line-number-at-pos (region-end))))
3050 (save-excursion
3051 (goto-char (region-beginning))
3052 (while (<= (line-number-at-pos) lastl)
3053 (funcall mark-unmark-function))))
3054 (funcall mark-unmark-function)))
3056 (defun vc-status-mark-file ()
3057 ;; Mark the current file and move to the next line.
3058 (let* ((crt (ewoc-locate vc-status))
3059 (file (ewoc-data crt)))
3060 (setf (vc-status-fileinfo->marked file) t)
3061 (ewoc-invalidate vc-status crt)
3062 (vc-status-next-line 1)))
3064 (defun vc-status-mark ()
3065 "Mark the current file or all files in the region.
3066 If the region is active, mark all the files in the region.
3067 Otherwise mark the file on the current line and move to the next
3068 line."
3069 (interactive)
3070 (vc-status-mark-unmark 'vc-status-mark-file))
3073 ;; XXX: Should this take the region into consideration?
3074 (defun vc-status-mark-all-files (arg)
3075 "Mark all files with the same state as the current one.
3076 With a prefix argument mark all files.
3078 The VC commands operate on files that are on the same state.
3079 This command is intended to make it easy to select all files that
3080 share the same state."
3081 (interactive "P")
3082 (if arg
3083 (ewoc-map
3084 (lambda (filearg)
3085 (unless (vc-status-fileinfo->marked filearg)
3086 (setf (vc-status-fileinfo->marked filearg) t)
3088 vc-status)
3089 (let* ((crt (ewoc-locate vc-status))
3090 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3091 (ewoc-map
3092 (lambda (filearg)
3093 (when (and (not (vc-status-fileinfo->marked filearg))
3094 (eq (vc-status-fileinfo->state filearg) crt-state))
3095 (setf (vc-status-fileinfo->marked filearg) t)
3097 vc-status))))
3099 (defun vc-status-unmark-file ()
3100 ;; Unmark the current file and move to the next line.
3101 (let* ((crt (ewoc-locate vc-status))
3102 (file (ewoc-data crt)))
3103 (setf (vc-status-fileinfo->marked file) nil)
3104 (ewoc-invalidate vc-status crt)
3105 (vc-status-next-line 1)))
3107 (defun vc-status-unmark ()
3108 "Unmark the current file or all files in the region.
3109 If the region is active, unmark all the files in the region.
3110 Otherwise mark the file on the current line and move to the next
3111 line."
3112 (interactive)
3113 (vc-status-mark-unmark 'vc-status-unmark-file))
3115 (defun vc-status-unmark-file-up ()
3116 "Move to the previous line and unmark the file."
3117 (interactive)
3118 ;; If we're on the first line, we won't move up, but we will still
3119 ;; remove the mark. This seems a bit odd but it is what buffer-menu
3120 ;; does.
3121 (let* ((prev (ewoc-goto-prev vc-status 1))
3122 (file (ewoc-data prev)))
3123 (setf (vc-status-fileinfo->marked file) nil)
3124 (ewoc-invalidate vc-status prev)
3125 (vc-status-move-to-goal-column)))
3127 (defun vc-status-unmark-all-files (arg)
3128 "Unmark all files with the same state as the current one.
3129 With a prefix argument mark all files.
3131 The VC commands operate on files that are on the same state.
3132 This command is intended to make it easy to deselect all files
3133 that share the same state."
3134 (interactive "P")
3135 (if arg
3136 (ewoc-map
3137 (lambda (filearg)
3138 (when (vc-status-fileinfo->marked filearg)
3139 (setf (vc-status-fileinfo->marked filearg) nil)
3141 vc-status)
3142 (let* ((crt (ewoc-locate vc-status))
3143 (crt-state (vc-status-fileinfo->state (ewoc-data crt))))
3144 (ewoc-map
3145 (lambda (filearg)
3146 (when (and (vc-status-fileinfo->marked filearg)
3147 (eq (vc-status-fileinfo->state filearg) crt-state))
3148 (setf (vc-status-fileinfo->marked filearg) nil)
3150 vc-status))))
3152 (defun vc-status-toggle-mark-file ()
3153 (let* ((crt (ewoc-locate vc-status))
3154 (file (ewoc-data crt)))
3155 (if (vc-status-fileinfo->marked file)
3156 (vc-status-unmark-file)
3157 (vc-status-mark-file))))
3159 (defun vc-status-toggle-mark ()
3160 (interactive)
3161 (vc-status-mark-unmark 'vc-status-toggle-mark-file))
3163 (defun vc-status-register ()
3164 "Register the marked files, or the current file if no marks."
3165 (interactive)
3166 (let ((files (or (vc-status-marked-files)
3167 (list (vc-status-current-file)))))
3168 (dolist (file files)
3169 (vc-register file))))
3171 (defun vc-status-find-file ()
3172 "Find the file on the current line."
3173 (interactive)
3174 (find-file (vc-status-current-file)))
3176 (defun vc-status-find-file-other-window ()
3177 "Find the file on the current line, in another window."
3178 (interactive)
3179 (find-file-other-window (vc-status-current-file)))
3181 (defun vc-status-current-file ()
3182 (let ((node (ewoc-locate vc-status)))
3183 (unless node
3184 (error "No file available."))
3185 (expand-file-name (vc-status-fileinfo->name (ewoc-data node)))))
3187 (defun vc-status-marked-files ()
3188 "Return the list of marked files"
3189 (mapcar
3190 (lambda (elem)
3191 (expand-file-name (vc-status-fileinfo->name elem)))
3192 (ewoc-collect
3193 vc-status
3194 (lambda (crt) (vc-status-fileinfo->marked crt)))))
3196 (defun vc-status-hide-up-to-date ()
3197 "Hide up-to-date items from display."
3198 (interactive)
3199 (ewoc-filter
3200 vc-status
3201 (lambda (crt) (not (eq (vc-status-fileinfo->state crt) 'up-to-date)))))
3203 (defun vc-status-mark-buffer-changed (&optional fname)
3204 (let* ((file (or fname (expand-file-name buffer-file-name)))
3205 (state (and (vc-backend file) (vc-state file)))
3206 (found-vc-status-buf nil))
3207 (save-excursion
3208 (dolist (status-buf (buffer-list))
3209 (set-buffer status-buf)
3210 ;; look for a vc-status buffer that might show this file.
3211 (when (eq major-mode 'vc-status-mode)
3212 (setq found-vc-status-buf t)
3213 (let ((ddir (expand-file-name default-directory)))
3214 ;; This test is cvs-string-prefix-p
3215 (when (eq t (compare-strings file nil (length ddir) ddir nil nil))
3216 (let* ((file-short (substring file (length ddir)))
3217 (entry (cons file-short (if state state 'unregistered))))
3218 (vc-status-add-entry entry status-buf))))))
3219 ;; We didn't find any vc-status buffers, remove the hook, it is
3220 ;; not needed.
3221 (unless found-vc-status-buf (remove-hook 'after-save-hook 'vc-status-mark-buffer-changed)))))
3223 ;;; End experimental code.
3225 ;; Named-configuration entry points
3227 (defun vc-snapshot-precondition (dir)
3228 "Scan the tree below DIR, looking for files not up-to-date.
3229 If any file is not up-to-date, return the name of the first such file.
3230 \(This means, neither snapshot creation nor retrieval is allowed.\)
3231 If one or more of the files are currently visited, return `visited'.
3232 Otherwise, return nil."
3233 (let ((status nil))
3234 (catch 'vc-locked-example
3235 (vc-file-tree-walk
3237 (lambda (f)
3238 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
3239 (if (get-file-buffer f) (setq status 'visited)))))
3240 status)))
3242 ;;;###autoload
3243 (defun vc-create-snapshot (dir name branchp)
3244 "Descending recursively from DIR, make a snapshot called NAME.
3245 For each registered file, the working revision becomes part of
3246 the named configuration. If the prefix argument BRANCHP is
3247 given, the snapshot is made as a new branch and the files are
3248 checked out in that new branch."
3249 (interactive
3250 (list (read-file-name "Directory: " default-directory default-directory t)
3251 (read-string "New snapshot name: ")
3252 current-prefix-arg))
3253 (message "Making %s... " (if branchp "branch" "snapshot"))
3254 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
3255 (vc-call-backend (vc-responsible-backend dir)
3256 'create-snapshot dir name branchp)
3257 (message "Making %s... done" (if branchp "branch" "snapshot")))
3259 ;;;###autoload
3260 (defun vc-retrieve-snapshot (dir name)
3261 "Descending recursively from DIR, retrieve the snapshot called NAME.
3262 If NAME is empty, it refers to the latest revisions.
3263 If locking is used for the files in DIR, then there must not be any
3264 locked files at or below DIR (but if NAME is empty, locked files are
3265 allowed and simply skipped)."
3266 (interactive
3267 (list (read-file-name "Directory: " default-directory default-directory t)
3268 (read-string "Snapshot name to retrieve (default latest revisions): ")))
3269 (let ((update (yes-or-no-p "Update any affected buffers? "))
3270 (msg (if (or (not name) (string= name ""))
3271 (format "Updating %s... " (abbreviate-file-name dir))
3272 (format "Retrieving snapshot into %s... "
3273 (abbreviate-file-name dir)))))
3274 (message "%s" msg)
3275 (vc-call-backend (vc-responsible-backend dir)
3276 'retrieve-snapshot dir name update)
3277 (message "%s" (concat msg "done"))))
3279 ;; Miscellaneous other entry points
3281 ;;;###autoload
3282 (defun vc-print-log (&optional working-revision)
3283 "List the change log of the current fileset in a window.
3284 If WORKING-REVISION is non-nil, leave the point at that revision."
3285 (interactive)
3286 (let* ((files (vc-deduce-fileset))
3287 (backend (vc-backend files))
3288 (working-revision (or working-revision (vc-working-revision (car files)))))
3289 ;; Don't switch to the output buffer before running the command,
3290 ;; so that any buffer-local settings in the vc-controlled
3291 ;; buffer can be accessed by the command.
3292 (vc-call-backend backend 'print-log files "*vc-change-log*")
3293 (pop-to-buffer "*vc-change-log*")
3294 (vc-exec-after
3295 `(let ((inhibit-read-only t))
3296 (vc-call-backend ',backend 'log-view-mode)
3297 (goto-char (point-max)) (forward-line -1)
3298 (while (looking-at "=*\n")
3299 (delete-char (- (match-end 0) (match-beginning 0)))
3300 (forward-line -1))
3301 (goto-char (point-min))
3302 (if (looking-at "[\b\t\n\v\f\r ]+")
3303 (delete-char (- (match-end 0) (match-beginning 0))))
3304 (shrink-window-if-larger-than-buffer)
3305 ;; move point to the log entry for the working revision
3306 (vc-call-backend ',backend 'show-log-entry ',working-revision)
3307 (setq vc-sentinel-movepoint (point))
3308 (set-buffer-modified-p nil)))))
3310 ;;;###autoload
3311 (defun vc-revert ()
3312 "Revert working copies of the selected fileset to their repository contents.
3313 This asks for confirmation if the buffer contents are not identical
3314 to the working revision (except for keyword expansion)."
3315 (interactive)
3316 (let* ((files (vc-deduce-fileset)))
3317 ;; If any of the files is visited by the current buffer, make
3318 ;; sure buffer is saved. If the user says `no', abort since
3319 ;; we cannot show the changes and ask for confirmation to
3320 ;; discard them.
3321 (if (or (not files) (memq (buffer-file-name) files))
3322 (vc-buffer-sync nil))
3323 (dolist (file files)
3324 (let ((buf (get-file-buffer file)))
3325 (if (and buf (buffer-modified-p buf))
3326 (error "Please kill or save all modified buffers before reverting.")))
3327 (if (vc-up-to-date-p file)
3328 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
3329 (error "Revert canceled"))))
3330 (if (vc-diff-internal vc-allow-async-revert files nil nil)
3331 (progn
3332 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
3333 (error "Revert canceled"))
3334 (delete-windows-on "*vc-diff*")
3335 (kill-buffer "*vc-diff*")))
3336 (dolist (file files)
3337 (progn
3338 (message "Reverting %s..." (vc-delistify files))
3339 (vc-revert-file file)
3340 (message "Reverting %s...done" (vc-delistify files))))))
3342 ;;;###autoload
3343 (defun vc-rollback ()
3344 "Roll back (remove) the most recent changeset committed to the repository.
3345 This may be either a file-level or a repository-level operation,
3346 depending on the underlying version-control system."
3347 (interactive)
3348 (let* ((files (vc-deduce-fileset))
3349 (backend (vc-backend files))
3350 (granularity (vc-call-backend backend 'revision-granularity)))
3351 (unless (vc-find-backend-function backend 'rollback)
3352 (error "Rollback is not supported in %s" backend))
3353 (if (and (not (eq granularity 'repository)) (/= (length files) 1))
3354 (error "Rollback requires a singleton fileset or repository versioning"))
3355 (if (not (vc-call latest-on-branch-p (car files)))
3356 (error "Rollback is only possible at the tip revision."))
3357 ;; If any of the files is visited by the current buffer, make
3358 ;; sure buffer is saved. If the user says `no', abort since
3359 ;; we cannot show the changes and ask for confirmation to
3360 ;; discard them.
3361 (if (or (not files) (memq (buffer-file-name) files))
3362 (vc-buffer-sync nil))
3363 (dolist (file files)
3364 (if (buffer-modified-p (get-file-buffer file))
3365 (error "Please kill or save all modified buffers before rollback."))
3366 (if (not (vc-up-to-date-p file))
3367 (error "Please revert all modified workfiles before rollback.")))
3368 ;; Accumulate changes associated with the fileset
3369 (vc-setup-buffer "*vc-diff*")
3370 (not-modified)
3371 (message "Finding changes...")
3372 (let* ((tip (vc-working-revision (car files)))
3373 (previous (vc-call previous-revision (car files) tip)))
3374 (vc-diff-internal nil files previous tip))
3375 ;; Display changes
3376 (unless (yes-or-no-p "Discard these revisions? ")
3377 (error "Rollback canceled"))
3378 (delete-windows-on "*vc-diff*")
3379 (kill-buffer"*vc-diff*")
3380 ;; Do the actual reversions
3381 (message "Rolling back %s..." (vc-delistify files))
3382 (with-vc-properties
3383 files
3384 (vc-call-backend backend 'rollback files)
3385 `((vc-state . ,'up-to-date)
3386 (vc-checkout-time . , (nth 5 (file-attributes file)))
3387 (vc-working-revision . nil)))
3388 (dolist (f files) (vc-resynch-buffer f t t))
3389 (message "Rolling back %s...done" (vc-delistify files))))
3391 ;;;###autoload
3392 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
3394 ;;;###autoload
3395 (defun vc-update ()
3396 "Update the current fileset's files to their tip revisions.
3397 For each one that contains no changes, and is not locked, then this simply
3398 replaces the work file with the latest revision on its branch. If the file
3399 contains changes, and the backend supports merging news, then any recent
3400 changes from the current branch are merged into the working file."
3401 (interactive)
3402 (dolist (file (vc-deduce-fileset))
3403 (when (let ((buf (get-file-buffer file)))
3404 (and buf (buffer-modified-p buf)))
3405 (error "Please kill or save all modified buffers before updating."))
3406 (if (vc-up-to-date-p file)
3407 (vc-checkout file nil t)
3408 (if (eq (vc-checkout-model file) 'locking)
3409 (if (eq (vc-state file) 'edited)
3410 (error "%s"
3411 (substitute-command-keys
3412 "File is locked--type \\[vc-revert] to discard changes"))
3413 (error "Unexpected file state (%s) -- type %s"
3414 (vc-state file)
3415 (substitute-command-keys
3416 "\\[vc-next-action] to correct")))
3417 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
3418 (error "Sorry, merging news is not implemented for %s"
3419 (vc-backend file))
3420 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))))
3422 (defun vc-version-backup-file (file &optional rev)
3423 "Return name of backup file for revision REV of FILE.
3424 If version backups should be used for FILE, and there exists
3425 such a backup for REV or the working revision of file, return
3426 its name; otherwise return nil."
3427 (when (vc-call make-version-backups-p file)
3428 (let ((backup-file (vc-version-backup-file-name file rev)))
3429 (if (file-exists-p backup-file)
3430 backup-file
3431 ;; there is no automatic backup, but maybe the user made one manually
3432 (setq backup-file (vc-version-backup-file-name file rev 'manual))
3433 (if (file-exists-p backup-file)
3434 backup-file)))))
3436 (defun vc-revert-file (file)
3437 "Revert FILE back to the repository working revision it was based on."
3438 (with-vc-properties
3439 (list file)
3440 (let ((backup-file (vc-version-backup-file file)))
3441 (when backup-file
3442 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
3443 (vc-delete-automatic-version-backups file))
3444 (vc-call revert file backup-file))
3445 `((vc-state . up-to-date)
3446 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
3447 (vc-resynch-buffer file t t))
3449 ;;;###autoload
3450 (defun vc-switch-backend (file backend)
3451 "Make BACKEND the current version control system for FILE.
3452 FILE must already be registered in BACKEND. The change is not
3453 permanent, only for the current session. This function only changes
3454 VC's perspective on FILE, it does not register or unregister it.
3455 By default, this command cycles through the registered backends.
3456 To get a prompt, use a prefix argument."
3457 (interactive
3458 (list
3459 (or buffer-file-name
3460 (error "There is no version-controlled file in this buffer"))
3461 (let ((backend (vc-backend buffer-file-name))
3462 (backends nil))
3463 (unless backend
3464 (error "File %s is not under version control" buffer-file-name))
3465 ;; Find the registered backends.
3466 (dolist (backend vc-handled-backends)
3467 (when (vc-call-backend backend 'registered buffer-file-name)
3468 (push backend backends)))
3469 ;; Find the next backend.
3470 (let ((def (car (delq backend (append (memq backend backends) backends))))
3471 (others (delete backend backends)))
3472 (cond
3473 ((null others) (error "No other backend to switch to"))
3474 (current-prefix-arg
3475 (intern
3476 (upcase
3477 (completing-read
3478 (format "Switch to backend [%s]: " def)
3479 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
3480 nil t nil nil (downcase (symbol-name def))))))
3481 (t def))))))
3482 (unless (eq backend (vc-backend file))
3483 (vc-file-clearprops file)
3484 (vc-file-setprop file 'vc-backend backend)
3485 ;; Force recomputation of the state
3486 (unless (vc-call-backend backend 'registered file)
3487 (vc-file-clearprops file)
3488 (error "%s is not registered in %s" file backend))
3489 (vc-mode-line file)))
3491 ;;;###autoload
3492 (defun vc-transfer-file (file new-backend)
3493 "Transfer FILE to another version control system NEW-BACKEND.
3494 If NEW-BACKEND has a higher precedence than FILE's current backend
3495 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
3496 NEW-BACKEND, using the revision number from the current backend as the
3497 base level. If NEW-BACKEND has a lower precedence than the current
3498 backend, then commit all changes that were made under the current
3499 backend to NEW-BACKEND, and unregister FILE from the current backend.
3500 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
3501 (let* ((old-backend (vc-backend file))
3502 (edited (memq (vc-state file) '(edited needs-merge)))
3503 (registered (vc-call-backend new-backend 'registered file))
3504 (move
3505 (and registered ; Never move if not registered in new-backend yet.
3506 ;; move if new-backend comes later in vc-handled-backends
3507 (or (memq new-backend (memq old-backend vc-handled-backends))
3508 (y-or-n-p "Final transfer? "))))
3509 (comment nil))
3510 (if (eq old-backend new-backend)
3511 (error "%s is the current backend of %s" new-backend file))
3512 (if registered
3513 (set-file-modes file (logior (file-modes file) 128))
3514 ;; `registered' might have switched under us.
3515 (vc-switch-backend file old-backend)
3516 (let* ((rev (vc-working-revision file))
3517 (modified-file (and edited (make-temp-file file)))
3518 (unmodified-file (and modified-file (vc-version-backup-file file))))
3519 ;; Go back to the base unmodified file.
3520 (unwind-protect
3521 (progn
3522 (when modified-file
3523 (copy-file file modified-file 'ok-if-already-exists)
3524 ;; If we have a local copy of the unmodified file, handle that
3525 ;; here and not in vc-revert-file because we don't want to
3526 ;; delete that copy -- it is still useful for OLD-BACKEND.
3527 (if unmodified-file
3528 (copy-file unmodified-file file
3529 'ok-if-already-exists 'keep-date)
3530 (if (y-or-n-p "Get base revision from master? ")
3531 (vc-revert-file file))))
3532 (vc-call-backend new-backend 'receive-file file rev))
3533 (when modified-file
3534 (vc-switch-backend file new-backend)
3535 (unless (eq (vc-checkout-model file) 'implicit)
3536 (vc-checkout file t nil))
3537 (rename-file modified-file file 'ok-if-already-exists)
3538 (vc-file-setprop file 'vc-checkout-time nil)))))
3539 (when move
3540 (vc-switch-backend file old-backend)
3541 (setq comment (vc-call comment-history file))
3542 (vc-call unregister file))
3543 (vc-switch-backend file new-backend)
3544 (when (or move edited)
3545 (vc-file-setprop file 'vc-state 'edited)
3546 (vc-mode-line file)
3547 (vc-checkin file nil comment (stringp comment)))))
3549 (defun vc-rename-master (oldmaster newfile templates)
3550 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
3551 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
3552 (newdir (or (file-name-directory newfile) ""))
3553 (newbase (file-name-nondirectory newfile))
3554 (masters
3555 ;; List of potential master files for `newfile'
3556 (mapcar
3557 (lambda (s) (vc-possible-master s newdir newbase))
3558 templates)))
3559 (if (or (file-symlink-p oldmaster)
3560 (file-symlink-p (file-name-directory oldmaster)))
3561 (error "This is unsafe in the presence of symbolic links"))
3562 (rename-file
3563 oldmaster
3564 (catch 'found
3565 ;; If possible, keep the master file in the same directory.
3566 (dolist (f masters)
3567 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
3568 (throw 'found f)))
3569 ;; If not, just use the first possible place.
3570 (dolist (f masters)
3571 (and f (or (not (setq dir (file-name-directory f)))
3572 (file-directory-p dir))
3573 (throw 'found f)))
3574 (error "New file lacks a version control directory")))))
3576 (defun vc-delete-file (file)
3577 "Delete file and mark it as such in the version control system."
3578 (interactive "fVC delete file: ")
3579 (setq file (expand-file-name file))
3580 (let ((buf (get-file-buffer file))
3581 (backend (vc-backend file)))
3582 (unless backend
3583 (error "File %s is not under version control"
3584 (file-name-nondirectory file)))
3585 (unless (vc-find-backend-function backend 'delete-file)
3586 (error "Deleting files under %s is not supported in VC" backend))
3587 (if (and buf (buffer-modified-p buf))
3588 (error "Please save files before deleting them"))
3589 (unless (y-or-n-p (format "Really want to delete %s? "
3590 (file-name-nondirectory file)))
3591 (error "Abort!"))
3592 (unless (or (file-directory-p file) (null make-backup-files))
3593 (with-current-buffer (or buf (find-file-noselect file))
3594 (let ((backup-inhibited nil))
3595 (backup-buffer))
3596 ;; If we didn't have a buffer visiting the file before this
3597 ;; command, kill the buffer created by the above
3598 ;; `find-file-noselect' call.
3599 (unless buf (kill-buffer (current-buffer)))))
3600 (vc-call delete-file file)
3601 ;; If the backend hasn't deleted the file itself, let's do it for him.
3602 (if (file-exists-p file) (delete-file file))
3603 ;; Forget what VC knew about the file.
3604 (vc-file-clearprops file)
3605 (vc-resynch-buffer file buf t)))
3607 ;;;###autoload
3608 (defun vc-rename-file (old new)
3609 "Rename file OLD to NEW, and rename its master file likewise."
3610 (interactive "fVC rename file: \nFRename to: ")
3611 (let ((oldbuf (get-file-buffer old)))
3612 (if (and oldbuf (buffer-modified-p oldbuf))
3613 (error "Please save files before moving them"))
3614 (if (get-file-buffer new)
3615 (error "Already editing new file name"))
3616 (if (file-exists-p new)
3617 (error "New file already exists"))
3618 (let ((state (vc-state old)))
3619 (unless (memq state '(up-to-date edited))
3620 (error "Please %s files before moving them"
3621 (if (stringp state) "check in" "update"))))
3622 (vc-call rename-file old new)
3623 (vc-file-clearprops old)
3624 ;; Move the actual file (unless the backend did it already)
3625 (if (file-exists-p old) (rename-file old new))
3626 ;; ?? Renaming a file might change its contents due to keyword expansion.
3627 ;; We should really check out a new copy if the old copy was precisely equal
3628 ;; to some checked-in revision. However, testing for this is tricky....
3629 (if oldbuf
3630 (with-current-buffer oldbuf
3631 (let ((buffer-read-only buffer-read-only))
3632 (set-visited-file-name new))
3633 (vc-backend new)
3634 (vc-mode-line new)
3635 (set-buffer-modified-p nil)))))
3637 ;;;###autoload
3638 (defun vc-update-change-log (&rest args)
3639 "Find change log file and add entries from recent version control logs.
3640 Normally, find log entries for all registered files in the default
3641 directory.
3643 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
3645 With any numeric prefix arg, find log entries for all currently visited
3646 files that are under version control. This puts all the entries in the
3647 log for the default directory, which may not be appropriate.
3649 From a program, any ARGS are assumed to be filenames for which
3650 log entries should be gathered."
3651 (interactive
3652 (cond ((consp current-prefix-arg) ;C-u
3653 (list buffer-file-name))
3654 (current-prefix-arg ;Numeric argument.
3655 (let ((files nil)
3656 (buffers (buffer-list))
3657 file)
3658 (while buffers
3659 (setq file (buffer-file-name (car buffers)))
3660 (and file (vc-backend file)
3661 (setq files (cons file files)))
3662 (setq buffers (cdr buffers)))
3663 files))
3665 ;; Don't supply any filenames to backend; this means
3666 ;; it should find all relevant files relative to
3667 ;; the default-directory.
3668 nil)))
3669 (vc-call-backend (vc-responsible-backend default-directory)
3670 'update-changelog args))
3672 ;;; The default back end. Assumes RCS-like revision numbering.
3674 (defun vc-default-revision-granularity ()
3675 (error "Your backend will not work with this version of VC mode."))
3677 ;; functions that operate on RCS revision numbers. This code should
3678 ;; also be moved into the backends. It stays for now, however, since
3679 ;; it is used in code below.
3680 ;;;###autoload
3681 (defun vc-trunk-p (rev)
3682 "Return t if REV is a revision on the trunk."
3683 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
3685 (defun vc-branch-p (rev)
3686 "Return t if REV is a branch revision."
3687 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
3689 ;;;###autoload
3690 (defun vc-branch-part (rev)
3691 "Return the branch part of a revision number REV."
3692 (let ((index (string-match "\\.[0-9]+\\'" rev)))
3693 (if index
3694 (substring rev 0 index))))
3696 (defun vc-minor-part (rev)
3697 "Return the minor revision number of a revision number REV."
3698 (string-match "[0-9]+\\'" rev)
3699 (substring rev (match-beginning 0) (match-end 0)))
3701 (defun vc-default-previous-revision (backend file rev)
3702 "Return the revision number immediately preceding REV for FILE,
3703 or nil if there is no previous revision. This default
3704 implementation works for MAJOR.MINOR-style revision numbers as
3705 used by RCS and CVS."
3706 (let ((branch (vc-branch-part rev))
3707 (minor-num (string-to-number (vc-minor-part rev))))
3708 (when branch
3709 (if (> minor-num 1)
3710 ;; revision does probably not start a branch or release
3711 (concat branch "." (number-to-string (1- minor-num)))
3712 (if (vc-trunk-p rev)
3713 ;; we are at the beginning of the trunk --
3714 ;; don't know anything to return here
3716 ;; we are at the beginning of a branch --
3717 ;; return revision of starting point
3718 (vc-branch-part branch))))))
3720 (defun vc-default-next-revision (backend file rev)
3721 "Return the revision number immediately following REV for FILE,
3722 or nil if there is no next revision. This default implementation
3723 works for MAJOR.MINOR-style revision numbers as used by RCS
3724 and CVS."
3725 (when (not (string= rev (vc-working-revision file)))
3726 (let ((branch (vc-branch-part rev))
3727 (minor-num (string-to-number (vc-minor-part rev))))
3728 (concat branch "." (number-to-string (1+ minor-num))))))
3730 (defun vc-default-responsible-p (backend file)
3731 "Indicate whether BACKEND is reponsible for FILE.
3732 The default is to return nil always."
3733 nil)
3735 (defun vc-default-could-register (backend file)
3736 "Return non-nil if BACKEND could be used to register FILE.
3737 The default implementation returns t for all files."
3740 (defun vc-default-latest-on-branch-p (backend file)
3741 "Return non-nil if FILE is the latest on its branch.
3742 This default implementation always returns non-nil, which means that
3743 editing non-current revisions is not supported by default."
3746 (defun vc-default-init-revision (backend) vc-default-init-revision)
3748 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
3749 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
3750 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
3751 ;; vc-cvs.el by code using cvs2cl.
3752 (defun vc-update-changelog-rcs2log (files)
3753 "Default implementation of update-changelog.
3754 Uses `rcs2log' which only works for RCS and CVS."
3755 ;; FIXME: We (c|sh)ould add support for cvs2cl
3756 (let ((odefault default-directory)
3757 (changelog (find-change-log))
3758 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
3759 (tempfile (make-temp-file
3760 (expand-file-name "vc"
3761 (or small-temporary-file-directory
3762 temporary-file-directory))))
3763 (login-name (or user-login-name
3764 (format "uid%d" (number-to-string (user-uid)))))
3765 (full-name (or add-log-full-name
3766 (user-full-name)
3767 (user-login-name)
3768 (format "uid%d" (number-to-string (user-uid)))))
3769 (mailing-address (or add-log-mailing-address
3770 user-mail-address)))
3771 (find-file-other-window changelog)
3772 (barf-if-buffer-read-only)
3773 (vc-buffer-sync)
3774 (undo-boundary)
3775 (goto-char (point-min))
3776 (push-mark)
3777 (message "Computing change log entries...")
3778 (message "Computing change log entries... %s"
3779 (unwind-protect
3780 (progn
3781 (setq default-directory odefault)
3782 (if (eq 0 (apply 'call-process
3783 (expand-file-name "rcs2log"
3784 exec-directory)
3785 nil (list t tempfile) nil
3786 "-c" changelog
3787 "-u" (concat login-name
3788 "\t" full-name
3789 "\t" mailing-address)
3790 (mapcar
3791 (lambda (f)
3792 (file-relative-name
3793 (expand-file-name f odefault)))
3794 files)))
3795 "done"
3796 (pop-to-buffer (get-buffer-create "*vc*"))
3797 (erase-buffer)
3798 (insert-file-contents tempfile)
3799 "failed"))
3800 (setq default-directory (file-name-directory changelog))
3801 (delete-file tempfile)))))
3803 (defun vc-default-find-revision (backend file rev buffer)
3804 "Provide the new `find-revision' op based on the old `checkout' op.
3805 This is only for compatibility with old backends. They should be updated
3806 to provide the `find-revision' operation instead."
3807 (let ((tmpfile (make-temp-file (expand-file-name file))))
3808 (unwind-protect
3809 (progn
3810 (vc-call-backend backend 'checkout file nil rev tmpfile)
3811 (with-current-buffer buffer
3812 (insert-file-contents-literally tmpfile)))
3813 (delete-file tmpfile))))
3815 (defun vc-default-dired-state-info (backend file)
3816 (let* ((state (vc-state file))
3817 (statestring
3818 (cond
3819 ((stringp state) (concat "(" state ")"))
3820 ((eq state 'edited) "(modified)")
3821 ((eq state 'needs-merge) "(merge)")
3822 ((eq state 'needs-patch) "(patch)")
3823 ((eq state 'added) "(added)")
3824 ((eq state 'removed) "(removed)")
3825 ((eq state 'ignored) "(ignored)") ;; dired-hook filters this out
3826 ((eq state 'unregistered) "?")
3827 ((eq state 'unlocked-changes) "(stale)")
3828 ((not state) "(unknown)")))
3829 (buffer
3830 (get-file-buffer file))
3831 (modflag
3832 (if (and buffer (buffer-modified-p buffer)) "+" "")))
3833 (concat statestring modflag)))
3835 (defun vc-default-rename-file (backend old new)
3836 (condition-case nil
3837 (add-name-to-file old new)
3838 (error (rename-file old new)))
3839 (vc-delete-file old)
3840 (with-current-buffer (find-file-noselect new)
3841 (vc-register)))
3843 (defalias 'vc-default-logentry-check 'ignore)
3844 (defalias 'vc-default-check-headers 'ignore)
3846 (defun vc-default-log-view-mode (backend) (log-view-mode))
3848 (defun vc-default-show-log-entry (backend rev)
3849 (with-no-warnings
3850 (log-view-goto-rev rev)))
3852 (defun vc-default-comment-history (backend file)
3853 "Return a string with all log entries stored in BACKEND for FILE."
3854 (if (vc-find-backend-function backend 'print-log)
3855 (with-current-buffer "*vc*"
3856 (vc-call print-log (list file))
3857 (vc-call-backend backend 'wash-log)
3858 (buffer-string))))
3860 (defun vc-default-receive-file (backend file rev)
3861 "Let BACKEND receive FILE from another version control system."
3862 (vc-call-backend backend 'register file rev ""))
3864 (defun vc-default-create-snapshot (backend dir name branchp)
3865 (when branchp
3866 (error "VC backend %s does not support module branches" backend))
3867 (let ((result (vc-snapshot-precondition dir)))
3868 (if (stringp result)
3869 (error "File %s is not up-to-date" result)
3870 (vc-file-tree-walk
3872 (lambda (f)
3873 (vc-call assign-name f name))))))
3875 (defun vc-default-retrieve-snapshot (backend dir name update)
3876 (if (string= name "")
3877 (progn
3878 (vc-file-tree-walk
3880 (lambda (f) (and
3881 (vc-up-to-date-p f)
3882 (vc-error-occurred
3883 (vc-call checkout f nil "")
3884 (if update (vc-resynch-buffer f t t)))))))
3885 (let ((result (vc-snapshot-precondition dir)))
3886 (if (stringp result)
3887 (error "File %s is locked" result)
3888 (setq update (and (eq result 'visited) update))
3889 (vc-file-tree-walk
3891 (lambda (f) (vc-error-occurred
3892 (vc-call checkout f nil name)
3893 (if update (vc-resynch-buffer f t t)))))))))
3895 (defun vc-default-revert (backend file contents-done)
3896 (unless contents-done
3897 (let ((rev (vc-working-revision file))
3898 (file-buffer (or (get-file-buffer file) (current-buffer))))
3899 (message "Checking out %s..." file)
3900 (let ((failed t)
3901 (backup-name (car (find-backup-file-name file))))
3902 (when backup-name
3903 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
3904 (unless (file-writable-p file)
3905 (set-file-modes file (logior (file-modes file) 128))))
3906 (unwind-protect
3907 (let ((coding-system-for-read 'no-conversion)
3908 (coding-system-for-write 'no-conversion))
3909 (with-temp-file file
3910 (let ((outbuf (current-buffer)))
3911 ;; Change buffer to get local value of vc-checkout-switches.
3912 (with-current-buffer file-buffer
3913 (let ((default-directory (file-name-directory file)))
3914 (vc-call find-revision file rev outbuf)))))
3915 (setq failed nil))
3916 (when backup-name
3917 (if failed
3918 (rename-file backup-name file 'ok-if-already-exists)
3919 (and (not vc-make-backup-files) (delete-file backup-name))))))
3920 (message "Checking out %s...done" file))))
3922 (defalias 'vc-default-revision-completion-table 'ignore)
3924 (defun vc-check-headers ()
3925 "Check if the current file has any headers in it."
3926 (interactive)
3927 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3929 ;;; Annotate functionality
3931 ;; Declare globally instead of additional parameter to
3932 ;; temp-buffer-show-function (not possible to pass more than one
3933 ;; parameter). The use of annotate-ratio is deprecated in favor of
3934 ;; annotate-mode, which replaces it with the more sensible "span-to
3935 ;; days", along with autoscaling support.
3936 (defvar vc-annotate-ratio nil "Global variable.")
3938 ;; internal buffer-local variables
3939 (defvar vc-annotate-backend nil)
3940 (defvar vc-annotate-parent-file nil)
3941 (defvar vc-annotate-parent-rev nil)
3942 (defvar vc-annotate-parent-display-mode nil)
3944 (defconst vc-annotate-font-lock-keywords
3945 ;; The fontification is done by vc-annotate-lines instead of font-lock.
3946 '((vc-annotate-lines)))
3948 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
3949 "Major mode for output buffers of the `vc-annotate' command.
3951 You can use the mode-specific menu to alter the time-span of the used
3952 colors. See variable `vc-annotate-menu-elements' for customizing the
3953 menu items."
3954 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
3955 ;; it will become a list, to avoid initial annotations being invisible.
3956 (add-to-invisibility-spec 'foo)
3957 (remove-from-invisibility-spec 'foo)
3958 (set (make-local-variable 'truncate-lines) t)
3959 (set (make-local-variable 'font-lock-defaults)
3960 '(vc-annotate-font-lock-keywords t))
3961 (view-mode 1))
3963 (defun vc-annotate-toggle-annotation-visibility ()
3964 "Toggle whether or not the annotation is visible."
3965 (interactive)
3966 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
3967 'remove-from-invisibility-spec
3968 'add-to-invisibility-spec)
3969 'vc-annotate-annotation)
3970 (force-window-update (current-buffer)))
3972 (defun vc-annotate-display-default (ratio)
3973 "Display the output of \\[vc-annotate] using the default color range.
3974 The color range is given by `vc-annotate-color-map', scaled by RATIO.
3975 The current time is used as the offset."
3976 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
3977 (message "Redisplaying annotation...")
3978 (vc-annotate-display ratio)
3979 (message "Redisplaying annotation...done"))
3981 (defun vc-annotate-oldest-in-map (color-map)
3982 "Return the oldest time in the COLOR-MAP."
3983 ;; Since entries should be sorted, we can just use the last one.
3984 (caar (last color-map)))
3986 (defun vc-annotate-get-time-set-line-props ()
3987 (let ((bol (point))
3988 (date (vc-call-backend vc-annotate-backend 'annotate-time))
3989 (inhibit-read-only t))
3990 (assert (>= (point) bol))
3991 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
3992 date))
3994 (defun vc-annotate-display-autoscale (&optional full)
3995 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
3996 Autoscaling means that the map is scaled from the current time to the
3997 oldest annotation in the buffer, or, with prefix argument FULL, to
3998 cover the range from the oldest annotation to the newest."
3999 (interactive "P")
4000 (let ((newest 0.0)
4001 (oldest 999999.) ;Any CVS users at the founding of Rome?
4002 (current (vc-annotate-convert-time (current-time)))
4003 date)
4004 (message "Redisplaying annotation...")
4005 ;; Run through this file and find the oldest and newest dates annotated.
4006 (save-excursion
4007 (goto-char (point-min))
4008 (while (not (eobp))
4009 (when (setq date (vc-annotate-get-time-set-line-props))
4010 (if (> date newest)
4011 (setq newest date))
4012 (if (< date oldest)
4013 (setq oldest date)))
4014 (forward-line 1)))
4015 (vc-annotate-display
4016 (/ (- (if full newest current) oldest)
4017 (vc-annotate-oldest-in-map vc-annotate-color-map))
4018 (if full newest))
4019 (message "Redisplaying annotation...done \(%s\)"
4020 (if full
4021 (format "Spanned from %.1f to %.1f days old"
4022 (- current oldest)
4023 (- current newest))
4024 (format "Spanned to %.1f days old" (- current oldest))))))
4026 ;; Menu -- Using easymenu.el
4027 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
4028 "VC Annotate Display Menu"
4029 `("VC-Annotate"
4030 ["By Color Map Range" (unless (null vc-annotate-display-mode)
4031 (setq vc-annotate-display-mode nil)
4032 (vc-annotate-display-select))
4033 :style toggle :selected (null vc-annotate-display-mode)]
4034 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
4035 (mapcar (lambda (element)
4036 (let ((days (* element oldest-in-map)))
4037 `[,(format "Span %.1f days" days)
4038 (vc-annotate-display-select nil ,days)
4039 :style toggle :selected
4040 (eql vc-annotate-display-mode ,days) ]))
4041 vc-annotate-menu-elements))
4042 ["Span ..."
4043 (vc-annotate-display-select
4044 nil (float (string-to-number (read-string "Span how many days? "))))]
4045 "--"
4046 ["Span to Oldest"
4047 (unless (eq vc-annotate-display-mode 'scale)
4048 (vc-annotate-display-select nil 'scale))
4049 :help
4050 "Use an autoscaled color map from the oldest annotation to the current time"
4051 :style toggle :selected
4052 (eq vc-annotate-display-mode 'scale)]
4053 ["Span Oldest->Newest"
4054 (unless (eq vc-annotate-display-mode 'fullscale)
4055 (vc-annotate-display-select nil 'fullscale))
4056 :help
4057 "Use an autoscaled color map from the oldest to the newest annotation"
4058 :style toggle :selected
4059 (eq vc-annotate-display-mode 'fullscale)]
4060 "--"
4061 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
4062 :help
4063 "Toggle whether the annotation is visible or not"]
4064 ["Annotate previous revision" vc-annotate-prev-revision
4065 :help "Visit the annotation of the revision previous to this one"]
4066 ["Annotate next revision" vc-annotate-next-revision
4067 :help "Visit the annotation of the revision after this one"]
4068 ["Annotate revision at line" vc-annotate-revision-at-line
4069 :help
4070 "Visit the annotation of the revision identified in the current line"]
4071 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
4072 :help "Visit the annotation of the revision before the revision at line"]
4073 ["Annotate latest revision" vc-annotate-working-revision
4074 :help "Visit the annotation of the working revision of this file"]
4075 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
4076 :help "Visit the log of the revision at line"]
4077 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
4078 :help
4079 "Visit the diff of the revision at line from its previous revision"]
4080 ["Visit revision at line" vc-annotate-find-revision-at-line
4081 :help "Visit the revision identified in the current line"]))
4083 (defun vc-annotate-display-select (&optional buffer mode)
4084 "Highlight the output of \\[vc-annotate].
4085 By default, the current buffer is highlighted, unless overridden by
4086 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
4087 use; you may override this using the second optional arg MODE."
4088 (interactive)
4089 (if mode (setq vc-annotate-display-mode mode))
4090 (pop-to-buffer (or buffer (current-buffer)))
4091 (cond ((null vc-annotate-display-mode)
4092 ;; The ratio is global, thus relative to the global color-map.
4093 (kill-local-variable 'vc-annotate-color-map)
4094 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
4095 ;; One of the auto-scaling modes
4096 ((eq vc-annotate-display-mode 'scale)
4097 (vc-exec-after `(vc-annotate-display-autoscale)))
4098 ((eq vc-annotate-display-mode 'fullscale)
4099 (vc-exec-after `(vc-annotate-display-autoscale t)))
4100 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
4101 (vc-annotate-display-default
4102 (/ vc-annotate-display-mode
4103 (vc-annotate-oldest-in-map vc-annotate-color-map))))
4104 (t (error "No such display mode: %s"
4105 vc-annotate-display-mode))))
4107 ;;;###autoload
4108 (defun vc-annotate (file rev &optional display-mode buf move-point-to)
4109 "Display the edit history of the current file using colors.
4111 This command creates a buffer that shows, for each line of the current
4112 file, when it was last edited and by whom. Additionally, colors are
4113 used to show the age of each line--blue means oldest, red means
4114 youngest, and intermediate colors indicate intermediate ages. By
4115 default, the time scale stretches back one year into the past;
4116 everything that is older than that is shown in blue.
4118 With a prefix argument, this command asks two questions in the
4119 minibuffer. First, you may enter a revision number; then the buffer
4120 displays and annotates that revision instead of the working revision
4121 \(type RET in the minibuffer to leave that default unchanged). Then,
4122 you are prompted for the time span in days which the color range
4123 should cover. For example, a time span of 20 days means that changes
4124 over the past 20 days are shown in red to blue, according to their
4125 age, and everything that is older than that is shown in blue.
4127 If MOVE-POINT-TO is given, move the point to that line.
4129 Customization variables:
4131 `vc-annotate-menu-elements' customizes the menu elements of the
4132 mode-specific menu. `vc-annotate-color-map' and
4133 `vc-annotate-very-old-color' define the mapping of time to colors.
4134 `vc-annotate-background' specifies the background color."
4135 (interactive
4136 (save-current-buffer
4137 (vc-ensure-vc-buffer)
4138 (list buffer-file-name
4139 (let ((def (vc-working-revision buffer-file-name)))
4140 (if (null current-prefix-arg) def
4141 (read-string
4142 (format "Annotate from revision (default %s): " def)
4143 nil nil def)))
4144 (if (null current-prefix-arg)
4145 vc-annotate-display-mode
4146 (float (string-to-number
4147 (read-string "Annotate span days (default 20): "
4148 nil nil "20")))))))
4149 (vc-ensure-vc-buffer)
4150 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
4151 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
4152 (temp-buffer-show-function 'vc-annotate-display-select)
4153 ;; If BUF is specified, we presume the caller maintains current line,
4154 ;; so we don't need to do it here. This implementation may give
4155 ;; strange results occasionally in the case of REV != WORKFILE-REV.
4156 (current-line (or move-point-to (unless buf (line-number-at-pos)))))
4157 (message "Annotating...")
4158 ;; If BUF is specified it tells in which buffer we should put the
4159 ;; annotations. This is used when switching annotations to another
4160 ;; revision, so we should update the buffer's name.
4161 (if buf (with-current-buffer buf
4162 (rename-buffer temp-buffer-name t)
4163 ;; In case it had to be uniquified.
4164 (setq temp-buffer-name (buffer-name))))
4165 (with-output-to-temp-buffer temp-buffer-name
4166 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
4167 ;; we must setup the mode first, and then set our local
4168 ;; variables before the show-function is called at the exit of
4169 ;; with-output-to-temp-buffer
4170 (with-current-buffer temp-buffer-name
4171 (if (not (equal major-mode 'vc-annotate-mode))
4172 (vc-annotate-mode))
4173 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
4174 (set (make-local-variable 'vc-annotate-parent-file) file)
4175 (set (make-local-variable 'vc-annotate-parent-rev) rev)
4176 (set (make-local-variable 'vc-annotate-parent-display-mode)
4177 display-mode)))
4179 (with-current-buffer temp-buffer-name
4180 (vc-exec-after
4181 `(progn
4182 ;; Ideally, we'd rather not move point if the user has already
4183 ;; moved it elsewhere, but really point here is not the position
4184 ;; of the user's cursor :-(
4185 (when ,current-line ;(and (bobp))
4186 (goto-line ,current-line)
4187 (setq vc-sentinel-movepoint (point)))
4188 (unless (active-minibuffer-window)
4189 (message "Annotating... done")))))))
4191 (defun vc-annotate-prev-revision (prefix)
4192 "Visit the annotation of the revision previous to this one.
4194 With a numeric prefix argument, annotate the revision that many
4195 revisions previous."
4196 (interactive "p")
4197 (vc-annotate-warp-revision (- 0 prefix)))
4199 (defun vc-annotate-next-revision (prefix)
4200 "Visit the annotation of the revision after this one.
4202 With a numeric prefix argument, annotate the revision that many
4203 revisions after."
4204 (interactive "p")
4205 (vc-annotate-warp-revision prefix))
4207 (defun vc-annotate-working-revision ()
4208 "Visit the annotation of the working revision of this file."
4209 (interactive)
4210 (if (not (equal major-mode 'vc-annotate-mode))
4211 (message "Cannot be invoked outside of a vc annotate buffer")
4212 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
4213 (if (equal warp-rev vc-annotate-parent-rev)
4214 (message "Already at revision %s" warp-rev)
4215 (vc-annotate-warp-revision warp-rev)))))
4217 (defun vc-annotate-extract-revision-at-line ()
4218 "Extract the revision number of the current line."
4219 ;; This function must be invoked from a buffer in vc-annotate-mode
4220 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
4222 (defun vc-annotate-revision-at-line ()
4223 "Visit the annotation of the revision identified in the current line."
4224 (interactive)
4225 (if (not (equal major-mode 'vc-annotate-mode))
4226 (message "Cannot be invoked outside of a vc annotate buffer")
4227 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4228 (if (not rev-at-line)
4229 (message "Cannot extract revision number from the current line")
4230 (if (equal rev-at-line vc-annotate-parent-rev)
4231 (message "Already at revision %s" rev-at-line)
4232 (vc-annotate-warp-revision rev-at-line))))))
4234 (defun vc-annotate-find-revision-at-line ()
4235 "Visit the revision identified in the current line."
4236 (interactive)
4237 (if (not (equal major-mode 'vc-annotate-mode))
4238 (message "Cannot be invoked outside of a vc annotate buffer")
4239 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4240 (if (not rev-at-line)
4241 (message "Cannot extract revision number from the current line")
4242 (vc-revision-other-window rev-at-line)))))
4244 (defun vc-annotate-revision-previous-to-line ()
4245 "Visit the annotation of the revision before the revision at line."
4246 (interactive)
4247 (if (not (equal major-mode 'vc-annotate-mode))
4248 (message "Cannot be invoked outside of a vc annotate buffer")
4249 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4250 (prev-rev nil))
4251 (if (not rev-at-line)
4252 (message "Cannot extract revision number from the current line")
4253 (setq prev-rev
4254 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4255 (vc-annotate-warp-revision prev-rev)))))
4257 (defun vc-annotate-show-log-revision-at-line ()
4258 "Visit the log of the revision at line."
4259 (interactive)
4260 (if (not (equal major-mode 'vc-annotate-mode))
4261 (message "Cannot be invoked outside of a vc annotate buffer")
4262 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
4263 (if (not rev-at-line)
4264 (message "Cannot extract revision number from the current line")
4265 (vc-print-log rev-at-line)))))
4267 (defun vc-annotate-show-diff-revision-at-line ()
4268 "Visit the diff of the revision at line from its previous revision."
4269 (interactive)
4270 (if (not (equal major-mode 'vc-annotate-mode))
4271 (message "Cannot be invoked outside of a vc annotate buffer")
4272 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
4273 (prev-rev nil))
4274 (if (not rev-at-line)
4275 (message "Cannot extract revision number from the current line")
4276 (setq prev-rev
4277 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
4278 (if (not prev-rev)
4279 (message "Cannot diff from any revision prior to %s" rev-at-line)
4280 (save-window-excursion
4281 (vc-diff-internal nil (list vc-annotate-parent-file)
4282 prev-rev rev-at-line))
4283 (switch-to-buffer "*vc-diff*"))))))
4285 (defun vc-annotate-warp-revision (revspec)
4286 "Annotate the revision described by REVSPEC.
4288 If REVSPEC is a positive integer, warp that many revisions
4289 forward, if possible, otherwise echo a warning message. If
4290 REVSPEC is a negative integer, warp that many revisions backward,
4291 if possible, otherwise echo a warning message. If REVSPEC is a
4292 string, then it describes a revision number, so warp to that
4293 revision."
4294 (if (not (equal major-mode 'vc-annotate-mode))
4295 (message "Cannot be invoked outside of a vc annotate buffer")
4296 (let* ((buf (current-buffer))
4297 (oldline (line-number-at-pos))
4298 (revspeccopy revspec)
4299 (newrev nil))
4300 (cond
4301 ((and (integerp revspec) (> revspec 0))
4302 (setq newrev vc-annotate-parent-rev)
4303 (while (and (> revspec 0) newrev)
4304 (setq newrev (vc-call next-revision
4305 vc-annotate-parent-file newrev))
4306 (setq revspec (1- revspec)))
4307 (if (not newrev)
4308 (message "Cannot increment %d revisions from revision %s"
4309 revspeccopy vc-annotate-parent-rev)))
4310 ((and (integerp revspec) (< revspec 0))
4311 (setq newrev vc-annotate-parent-rev)
4312 (while (and (< revspec 0) newrev)
4313 (setq newrev (vc-call previous-revision
4314 vc-annotate-parent-file newrev))
4315 (setq revspec (1+ revspec)))
4316 (if (not newrev)
4317 (message "Cannot decrement %d revisions from revision %s"
4318 (- 0 revspeccopy) vc-annotate-parent-rev)))
4319 ((stringp revspec) (setq newrev revspec))
4320 (t (error "Invalid argument to vc-annotate-warp-revision")))
4321 (when newrev
4322 (vc-annotate vc-annotate-parent-file newrev
4323 vc-annotate-parent-display-mode
4325 ;; Pass the current line so that vc-annotate will
4326 ;; place the point in the line.
4327 (min oldline (progn (goto-char (point-max))
4328 (forward-line -1)
4329 (line-number-at-pos))))))))
4331 (defun vc-annotate-compcar (threshold a-list)
4332 "Test successive cons cells of A-LIST against THRESHOLD.
4333 Return the first cons cell with a car that is not less than THRESHOLD,
4334 nil if no such cell exists."
4335 (let ((i 1)
4336 (tmp-cons (car a-list)))
4337 (while (and tmp-cons (< (car tmp-cons) threshold))
4338 (setq tmp-cons (car (nthcdr i a-list)))
4339 (setq i (+ i 1)))
4340 tmp-cons)) ; Return the appropriate value
4342 (defun vc-annotate-convert-time (time)
4343 "Convert a time value to a floating-point number of days.
4344 The argument TIME is a list as returned by `current-time' or
4345 `encode-time', only the first two elements of that list are considered."
4346 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
4348 (defun vc-annotate-difference (&optional offset)
4349 "Return the time span in days to the next annotation.
4350 This calls the backend function annotate-time, and returns the
4351 difference in days between the time returned and the current time,
4352 or OFFSET if present."
4353 (let ((next-time (vc-annotate-get-time-set-line-props)))
4354 (if next-time
4355 (- (or offset
4356 (vc-call-backend vc-annotate-backend 'annotate-current-time))
4357 next-time))))
4359 (defun vc-default-annotate-current-time (backend)
4360 "Return the current time, encoded as fractional days."
4361 (vc-annotate-convert-time (current-time)))
4363 (defvar vc-annotate-offset nil)
4365 (defun vc-annotate-display (ratio &optional offset)
4366 "Highlight `vc-annotate' output in the current buffer.
4367 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
4368 The annotations are relative to the current time, unless overridden by OFFSET."
4369 (if (/= ratio 1.0)
4370 (set (make-local-variable 'vc-annotate-color-map)
4371 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
4372 vc-annotate-color-map)))
4373 (set (make-local-variable 'vc-annotate-offset) offset)
4374 (font-lock-mode 1))
4376 (defun vc-annotate-lines (limit)
4377 (while (< (point) limit)
4378 (let ((difference (vc-annotate-difference vc-annotate-offset))
4379 (start (point))
4380 (end (progn (forward-line 1) (point))))
4381 (when difference
4382 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
4383 (cons nil vc-annotate-very-old-color)))
4384 ;; substring from index 1 to remove any leading `#' in the name
4385 (face-name (concat "vc-annotate-face-"
4386 (if (string-equal
4387 (substring (cdr color) 0 1) "#")
4388 (substring (cdr color) 1)
4389 (cdr color))))
4390 ;; Make the face if not done.
4391 (face (or (intern-soft face-name)
4392 (let ((tmp-face (make-face (intern face-name))))
4393 (set-face-foreground tmp-face (cdr color))
4394 (if vc-annotate-background
4395 (set-face-background tmp-face
4396 vc-annotate-background))
4397 tmp-face)))) ; Return the face
4398 (put-text-property start end 'face face)))))
4399 ;; Pretend to font-lock there were no matches.
4400 nil)
4403 ;; Set up key bindings for use while editing log messages
4405 (defun vc-log-edit (fileset)
4406 "Set up `log-edit' for use with VC on FILE."
4407 (setq default-directory
4408 (with-current-buffer vc-parent-buffer default-directory))
4409 (log-edit 'vc-finish-logentry
4411 `((log-edit-listfun . (lambda () ',fileset))
4412 (log-edit-diff-function . (lambda () (vc-diff nil)))))
4413 (set (make-local-variable 'vc-log-fileset) fileset)
4414 (make-local-variable 'vc-log-revision)
4415 (set-buffer-modified-p nil)
4416 (setq buffer-file-name nil))
4418 ;; These things should probably be generally available
4420 (defun vc-file-tree-walk (dirname func &rest args)
4421 "Walk recursively through DIRNAME.
4422 Invoke FUNC f ARGS on each VC-managed file f underneath it."
4423 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
4424 (message "Traversing directory %s...done" dirname))
4426 (defun vc-file-tree-walk-internal (file func args)
4427 (if (not (file-directory-p file))
4428 (if (vc-backend file) (apply func file args))
4429 (message "Traversing directory %s..." (abbreviate-file-name file))
4430 (let ((dir (file-name-as-directory file)))
4431 (mapcar
4432 (lambda (f) (or
4433 (string-equal f ".")
4434 (string-equal f "..")
4435 (member f vc-directory-exclusion-list)
4436 (let ((dirf (expand-file-name f dir)))
4438 (file-symlink-p dirf) ;; Avoid possible loops.
4439 (vc-file-tree-walk-internal dirf func args)))))
4440 (directory-files dir)))))
4442 (provide 'vc)
4444 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
4446 ;; These may be useful to anyone who has to debug or extend the package.
4447 ;; (Note that this information corresponds to versions 5.x. Some of it
4448 ;; might have been invalidated by the additions to support branching
4449 ;; and RCS keyword lookup. AS, 1995/03/24)
4451 ;; A fundamental problem in VC is that there are time windows between
4452 ;; vc-next-action's computations of the file's version-control state and
4453 ;; the actions that change it. This is a window open to lossage in a
4454 ;; multi-user environment; someone else could nip in and change the state
4455 ;; of the master during it.
4457 ;; The performance problem is that rlog/prs calls are very expensive; we want
4458 ;; to avoid them as much as possible.
4460 ;; ANALYSIS:
4462 ;; The performance problem, it turns out, simplifies in practice to the
4463 ;; problem of making vc-state fast. The two other functions that call
4464 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
4465 ;; makes snapshots, the other deletes the calling user's last change in the
4466 ;; master.
4468 ;; The race condition implies that we have to either (a) lock the master
4469 ;; during the entire execution of vc-next-action, or (b) detect and
4470 ;; recover from errors resulting from dispatch on an out-of-date state.
4472 ;; Alternative (a) appears to be infeasible. The problem is that we can't
4473 ;; guarantee that the lock will ever be removed. Suppose a user starts a
4474 ;; checkin, the change message buffer pops up, and the user, having wandered
4475 ;; off to do something else, simply forgets about it?
4477 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
4478 ;; vc-state. Usually, if a file is registered, we can read its locked/
4479 ;; unlocked state and its current owner from its permissions.
4481 ;; This shortcut will fail if someone has manually changed the workfile's
4482 ;; permissions; also if developers are munging the workfile in several
4483 ;; directories, with symlinks to a master (in this latter case, the
4484 ;; permissions shortcut will fail to detect a lock asserted from another
4485 ;; directory).
4487 ;; Note that these cases correspond exactly to the errors which could happen
4488 ;; because of a competing checkin/checkout race in between two instances of
4489 ;; vc-next-action.
4491 ;; For VC's purposes, a workfile/master pair may have the following states:
4493 ;; A. Unregistered. There is a workfile, there is no master.
4495 ;; B. Registered and not locked by anyone.
4497 ;; C. Locked by calling user and unchanged.
4499 ;; D. Locked by the calling user and changed.
4501 ;; E. Locked by someone other than the calling user.
4503 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
4505 ;; VC's idea of state
4506 ;; |
4507 ;; V Actual state RCS action SCCS action Effect
4508 ;; A B C D E
4509 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
4510 ;; B 5 . 6 7 8 co -l get -e checkout
4511 ;; C 9 10 . 11 12 co -u unget; get revert
4512 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
4513 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
4515 ;; All commands take the master file name as a last argument (not shown).
4517 ;; In the discussion below, a "self-race" is a pathological situation in
4518 ;; which VC operations are being attempted simultaneously by two or more
4519 ;; Emacsen running under the same username.
4521 ;; The vc-next-action code has the following windows:
4523 ;; Window P:
4524 ;; Between the check for existence of a master file and the call to
4525 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
4526 ;; never close if the initial-comment feature is on.
4528 ;; Window Q:
4529 ;; Between the call to vc-workfile-unchanged-p in and the immediately
4530 ;; following revert (apparent state C).
4532 ;; Window R:
4533 ;; Between the call to vc-workfile-unchanged-p in and the following
4534 ;; checkin (apparent state D). This window may never close.
4536 ;; Window S:
4537 ;; Between the unlock and the immediately following checkout during a
4538 ;; revert operation (apparent state C). Included in window Q.
4540 ;; Window T:
4541 ;; Between vc-state and the following checkout (apparent state B).
4543 ;; Window U:
4544 ;; Between vc-state and the following revert (apparent state C).
4545 ;; Includes windows Q and S.
4547 ;; Window V:
4548 ;; Between vc-state and the following checkin (apparent state
4549 ;; D). This window may never be closed if the user fails to complete the
4550 ;; checkin message. Includes window R.
4552 ;; Window W:
4553 ;; Between vc-state and the following steal-lock (apparent
4554 ;; state E). This window may never close if the user fails to complete
4555 ;; the steal-lock message. Includes window X.
4557 ;; Window X:
4558 ;; Between the unlock and the immediately following re-lock during a
4559 ;; steal-lock operation (apparent state E). This window may never close
4560 ;; if the user fails to complete the steal-lock message.
4562 ;; Errors:
4564 ;; Apparent state A ---
4566 ;; 1. File looked unregistered but is actually registered and not locked.
4568 ;; Potential cause: someone else's admin during window P, with
4569 ;; caller's admin happening before their checkout.
4571 ;; RCS: Prior to version 5.6.4, ci fails with message
4572 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
4573 ;; ci -i option and the message is "<file>,v: already exists".
4574 ;; SCCS: admin will fail with error (ad19).
4576 ;; We can let these errors be passed up to the user.
4578 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
4580 ;; Potential cause: self-race during window P.
4582 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
4583 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
4584 ;; ci -i option, failing with message "<file>,v: already exists".
4585 ;; SCCS: will fail with error (ad19).
4587 ;; Either of these consequences is acceptable.
4589 ;; 3. File looked unregistered but is actually locked by caller, changed.
4591 ;; Potential cause: self-race during window P.
4593 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
4594 ;; a delta with a null change comment (the -t- switch will be
4595 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
4596 ;; failing with message "<file>,v: already exists".
4597 ;; SCCS: will fail with error (ad19).
4599 ;; 4. File looked unregistered but is locked by someone else.
4601 ;; Potential cause: someone else's admin during window P, with
4602 ;; caller's admin happening *after* their checkout.
4604 ;; RCS: Prior to version 5.6.4, ci fails with a
4605 ;; "no lock set by <user>" message. From 5.6.4 onwards,
4606 ;; VC uses the new ci -i option, failing with message
4607 ;; "<file>,v: already exists".
4608 ;; SCCS: will fail with error (ad19).
4610 ;; We can let these errors be passed up to the user.
4612 ;; Apparent state B ---
4614 ;; 5. File looked registered and not locked, but is actually unregistered.
4616 ;; Potential cause: master file got nuked during window P.
4618 ;; RCS: will fail with "RCS/<file>: No such file or directory"
4619 ;; SCCS: will fail with error ut4.
4621 ;; We can let these errors be passed up to the user.
4623 ;; 6. File looked registered and not locked, but is actually locked by the
4624 ;; calling user and unchanged.
4626 ;; Potential cause: self-race during window T.
4628 ;; RCS: in the same directory as the previous workfile, co -l will fail
4629 ;; with "co error: writable foo exists; checkout aborted". In any other
4630 ;; directory, checkout will succeed.
4631 ;; SCCS: will fail with ge17.
4633 ;; Either of these consequences is acceptable.
4635 ;; 7. File looked registered and not locked, but is actually locked by the
4636 ;; calling user and changed.
4638 ;; As case 6.
4640 ;; 8. File looked registered and not locked, but is actually locked by another
4641 ;; user.
4643 ;; Potential cause: someone else checks it out during window T.
4645 ;; RCS: co error: revision 1.3 already locked by <user>
4646 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
4648 ;; We can let these errors be passed up to the user.
4650 ;; Apparent state C ---
4652 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
4654 ;; As case 5.
4656 ;; 10. File looks locked by calling user and unchanged, but is actually not
4657 ;; locked.
4659 ;; Potential cause: a self-race in window U, or by the revert's
4660 ;; landing during window X of some other user's steal-lock or window S
4661 ;; of another user's revert.
4663 ;; RCS: succeeds, refreshing the file from the identical version in
4664 ;; the master.
4665 ;; SCCS: fails with error ut4 (p file nonexistent).
4667 ;; Either of these consequences is acceptable.
4669 ;; 11. File is locked by calling user. It looks unchanged, but is actually
4670 ;; changed.
4672 ;; Potential cause: the file would have to be touched by a self-race
4673 ;; during window Q.
4675 ;; The revert will succeed, removing whatever changes came with
4676 ;; the touch. It is theoretically possible that work could be lost.
4678 ;; 12. File looks like it's locked by the calling user and unchanged, but
4679 ;; it's actually locked by someone else.
4681 ;; Potential cause: a steal-lock in window V.
4683 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
4684 ;; SCCS: fails with error un2
4686 ;; We can pass these errors up to the user.
4688 ;; Apparent state D ---
4690 ;; 13. File looks like it's locked by the calling user and changed, but it's
4691 ;; actually unregistered.
4693 ;; Potential cause: master file got nuked during window P.
4695 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
4696 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
4697 ;; option, failing with message "no such file or directory".
4698 ;; SCCS: will fail with error ut4.
4700 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
4701 ;; VC may fail to detect the loss of previous version information.
4703 ;; 14. File looks like it's locked by the calling user and changed, but it's
4704 ;; actually unlocked.
4706 ;; Potential cause: self-race in window V, or the checkin happening
4707 ;; during the window X of someone else's steal-lock or window S of
4708 ;; someone else's revert.
4710 ;; RCS: ci will fail with "no lock set by <user>".
4711 ;; SCCS: delta will fail with error ut4.
4713 ;; 15. File looks like it's locked by the calling user and changed, but it's
4714 ;; actually locked by the calling user and unchanged.
4716 ;; Potential cause: another self-race --- a whole checkin/checkout
4717 ;; sequence by the calling user would have to land in window R.
4719 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
4720 ;; RCS: reverts to the file state as of the second user's checkin, leaving
4721 ;; the file unlocked.
4723 ;; It is theoretically possible that work could be lost under RCS.
4725 ;; 16. File looks like it's locked by the calling user and changed, but it's
4726 ;; actually locked by a different user.
4728 ;; RCS: ci error: no lock set by <user>
4729 ;; SCCS: unget will fail with error un2
4731 ;; We can pass these errors up to the user.
4733 ;; Apparent state E ---
4735 ;; 17. File looks like it's locked by some other user, but it's actually
4736 ;; unregistered.
4738 ;; As case 13.
4740 ;; 18. File looks like it's locked by some other user, but it's actually
4741 ;; unlocked.
4743 ;; Potential cause: someone released a lock during window W.
4745 ;; RCS: The calling user will get the lock on the file.
4746 ;; SCCS: unget -n will fail with cm4.
4748 ;; Either of these consequences will be OK.
4750 ;; 19. File looks like it's locked by some other user, but it's actually
4751 ;; locked by the calling user and unchanged.
4753 ;; Potential cause: the other user relinquishing a lock followed by
4754 ;; a self-race, both in window W.
4756 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
4757 ;; the sequence a no-op.
4759 ;; 20. File looks like it's locked by some other user, but it's actually
4760 ;; locked by the calling user and changed.
4762 ;; As case 19.
4764 ;; PROBLEM CASES:
4766 ;; In order of decreasing severity:
4768 ;; Cases 11 and 15 are the only ones that potentially lose work.
4769 ;; They would require a self-race for this to happen.
4771 ;; Case 13 in RCS loses information about previous deltas, retaining
4772 ;; only the information in the current workfile. This can only happen
4773 ;; if the master file gets nuked in window P.
4775 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
4776 ;; no change comment in the master. This would require a self-race in
4777 ;; window P or R respectively.
4779 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
4781 ;; Unfortunately, it appears to me that no recovery is possible in these
4782 ;; cases. They don't yield error messages, so there's no way to tell that
4783 ;; a race condition has occurred.
4785 ;; All other cases don't change either the workfile or the master, and
4786 ;; trigger command errors which the user will see.
4788 ;; Thus, there is no explicit recovery code.
4790 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
4791 ;;; vc.el ends here