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