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