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