Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / vc.el
blob4b900087b8308ca1eceeef20ede7fa7079265163
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 ;; - comment-history (file)
406 ;; Return a string containing all log entries that were made for FILE.
407 ;; This is used for transferring a file from one backend to another,
408 ;; retaining comment information. The default implementation of this
409 ;; function does this by calling print-log and then wash-log, and
410 ;; returning the resulting buffer contents as a string.
412 ;; - update-changelog (files)
414 ;; Using recent log entries, create ChangeLog entries for FILES, or for
415 ;; all files at or below the default-directory if FILES is nil. The
416 ;; default implementation runs rcs2log, which handles RCS- and
417 ;; CVS-style logs.
419 ;; * diff (files &optional rev1 rev2 buffer)
421 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
422 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
423 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
424 ;; found in the repository) as the older revision; if REV2 is nil,
425 ;; use the current working-copy contents as the newer revision. This
426 ;; function should pass the value of (vc-switches BACKEND 'diff) to
427 ;; the backend command. It should return a status of either 0 (no
428 ;; differences found), or 1 (either non-empty diff or the diff is
429 ;; run asynchronously).
431 ;; - revision-completion-table (files)
433 ;; Return a completion table for existing revisions of FILES.
434 ;; The default is to not use any completion table.
436 ;; - annotate-command (file buf &optional rev)
438 ;; If this function is provided, it should produce an annotated display
439 ;; of FILE in BUF, relative to revision REV. Annotation means each line
440 ;; of FILE displayed is prefixed with version information associated with
441 ;; its addition (deleted lines leave no history) and that the text of the
442 ;; file is fontified according to age.
444 ;; - annotate-time ()
446 ;; Only required if `annotate-command' is defined for the backend.
447 ;; Return the time of the next line of annotation at or after point,
448 ;; as a floating point fractional number of days. The helper
449 ;; function `vc-annotate-convert-time' may be useful for converting
450 ;; multi-part times as returned by `current-time' and `encode-time'
451 ;; to this format. Return nil if no more lines of annotation appear
452 ;; in the buffer. You can safely assume that point is placed at the
453 ;; beginning of each line, starting at `point-min'. The buffer that
454 ;; point is placed in is the Annotate output, as defined by the
455 ;; relevant backend. This function also affects how much of the line
456 ;; is fontified; where it leaves point is where fontification begins.
458 ;; - annotate-current-time ()
460 ;; Only required if `annotate-command' is defined for the backend,
461 ;; AND you'd like the current time considered to be anything besides
462 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
463 ;; time with hours, minutes, and seconds included. Probably safe to
464 ;; ignore. Return the current-time, in units of fractional days.
466 ;; - annotate-extract-revision-at-line ()
468 ;; Only required if `annotate-command' is defined for the backend.
469 ;; Invoked from a buffer in vc-annotate-mode, return the revision
470 ;; corresponding to the current line, or nil if there is no revision
471 ;; corresponding to the current line.
473 ;; SNAPSHOT SYSTEM
475 ;; - create-snapshot (dir name branchp)
477 ;; Take a snapshot of the current state of files under DIR and name it
478 ;; NAME. This should make sure that files are up-to-date before
479 ;; proceeding with the action. DIR can also be a file and if BRANCHP
480 ;; is specified, NAME should be created as a branch and DIR should be
481 ;; checked out under this new branch. The default implementation does
482 ;; not support branches but does a sanity check, a tree traversal and
483 ;; for each file calls `assign-name'.
485 ;; - assign-name (file name)
487 ;; Give name NAME to the working revision of FILE, assuming it is
488 ;; up-to-date. Only used by the default version of `create-snapshot'.
490 ;; - retrieve-snapshot (dir name update)
492 ;; Retrieve a named snapshot of all registered files at or below DIR.
493 ;; If UPDATE is non-nil, then update buffers of any files in the
494 ;; snapshot that are currently visited. The default implementation
495 ;; does a sanity check whether there aren't any uncommitted changes at
496 ;; or below DIR, and then performs a tree walk, using the `checkout'
497 ;; function to retrieve the corresponding revisions.
499 ;; MISCELLANEOUS
501 ;; - root (dir)
503 ;; Return DIR's "root" directory, that is, a parent directory of
504 ;; DIR for which the same backend as used for DIR applies. If no
505 ;; such parent exists, this function should return DIR.
507 ;; - make-version-backups-p (file)
509 ;; Return non-nil if unmodified repository revisions of FILE should be
510 ;; backed up locally. If this is done, VC can perform `diff' and
511 ;; `revert' operations itself, without calling the backend system. The
512 ;; default implementation always returns nil.
514 ;; - repository-hostname (dirname)
516 ;; Return the hostname that the backend will have to contact
517 ;; in order to operate on a file in DIRNAME. If the return value
518 ;; is nil, it means that the repository is local.
519 ;; This function is used in `vc-stay-local-p' which backends can use
520 ;; for their convenience.
522 ;; - previous-revision (file rev)
524 ;; Return the revision number that precedes REV for FILE, or nil if no such
525 ;; revision exists.
527 ;; - next-revision (file rev)
529 ;; Return the revision number that follows REV for FILE, or nil if no such
530 ;; revision exists.
532 ;; - check-headers ()
534 ;; Return non-nil if the current buffer contains any version headers.
536 ;; - clear-headers ()
538 ;; In the current buffer, reset all version headers to their unexpanded
539 ;; form. This function should be provided if the state-querying code
540 ;; for this backend uses the version headers to determine the state of
541 ;; a file. This function will then be called whenever VC changes the
542 ;; version control state in such a way that the headers would give
543 ;; wrong information.
545 ;; - delete-file (file)
547 ;; Delete FILE and mark it as deleted in the repository. If this
548 ;; function is not provided, the command `vc-delete-file' will
549 ;; signal an error.
551 ;; - rename-file (old new)
553 ;; Rename file OLD to NEW, both in the working area and in the
554 ;; repository. If this function is not provided, the renaming
555 ;; will be done by (vc-delete-file old) and (vc-register new).
557 ;; - find-file-hook ()
559 ;; Operation called in current buffer when opening a file. This can
560 ;; be used by the backend to setup some local variables it might need.
562 ;; - find-file-not-found-hook ()
564 ;; Operation called in current buffer when opening a non-existing file.
565 ;; By default, this asks the user if she wants to check out the file.
567 ;; - extra-menu ()
569 ;; Return a menu keymap, the items in the keymap will appear at the
570 ;; end of the Version Control menu. The goal is to allow backends
571 ;; to specify extra menu items that appear in the VC menu. This way
572 ;; you can provide menu entries for functionality that is specific
573 ;; to your backend and which does not map to any of the VC generic
574 ;; concepts.
576 ;; - extra-status-menu ()
578 ;; Return a menu keymap, the items in the keymap will appear at the
579 ;; end of the VC Status menu. The goal is to allow backends to
580 ;; specify extra menu items that appear in the VC Status menu. This
581 ;; makes it possible to provide menu entries for functionality that
582 ;; is specific to a backend and which does not map to any of the VC
583 ;; generic concepts.
585 ;;; Todo:
587 ;; - vc-update/vc-merge should deal with VC systems that don't
588 ;; update/merge on a file basis, but on a whole repository basis.
590 ;; - deal with push/pull operations.
592 ;; - "snapshots" should be renamed to "branches", and thoroughly reworked.
594 ;; - when a file is in `conflict' state, turn on smerge-mode.
596 ;; - figure out what to do with conflicts that are not caused by the
597 ;; file contents, but by metadata or other causes. Example: File A
598 ;; gets renamed to B in one branch and to C in another and you merge
599 ;; the two branches. Or you locally add file FOO and then pull a
600 ;; change that also adds a new file FOO, ...
602 ;; - add a generic mechanism for remembering the current branch names,
603 ;; display the branch name in the mode-line. Replace
604 ;; vc-cvs-sticky-tag with that.
606 ;; - C-x v b does switch to a different backend, but the mode line is not
607 ;; adapted accordingly. Also, it considers RCS and CVS to be the same,
608 ;; which is pretty confusing.
610 ;; - vc-diff should be able to show the diff for all files in a
611 ;; changeset, especially for VC systems that have per repository
612 ;; version numbers. log-view should take advantage of this.
614 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
615 ;; buffer, if one is present, instead of adding to the ChangeLog.
617 ;; - add a mechanism for editing the underlying VCS's list of files
618 ;; to be ignored, when that's possible.
620 ;; - When vc-next-action calls vc-checkin it could pre-fill the
621 ;; *VC-log* buffer with some obvious items: the list of files that
622 ;; were added, the list of files that were removed. If the diff is
623 ;; available, maybe it could even call something like
624 ;; `diff-add-change-log-entries-other-window' to create a detailed
625 ;; skeleton for the log...
627 ;; - a way to do repository wide log (instead of just per
628 ;; file/fileset) is needed. Doing it per directory might be enough...
630 ;; - most vc-dir backends need more work. They might need to
631 ;; provide custom headers, use the `extra' field and deal with all
632 ;; possible VC states.
634 ;; - add function that calls vc-dir to `find-directory-functions'.
636 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
637 ;; files. Now that unregistered and ignored files are shown in
638 ;; vc-dired/vc-dir, it is possible that these commands are called
639 ;; for unregistered/ignored files.
641 ;; - do not default to RCS anymore when the current directory is not
642 ;; controlled by any VCS and the user does C-x v v
644 ;; - vc-create-snapshot and vc-retrieve-snapshot should update the
645 ;; buffers that might be visiting the affected files.
647 ;; - Using multiple backends needs work. Given a CVS directory with some
648 ;; files checked into git (but not all), using C-x v l to get a log file
649 ;; from a file only present in git, and then typing RET on some log entry,
650 ;; vc will bombs out because it wants to see the file being in CVS.
651 ;; Those logs should likely use a local variable to hardware the VC they
652 ;; are supposed to work with.
654 ;; - Another important thing: merge all the status-like backend operations.
655 ;; We should remove dir-status, state, dir-state, and dir-status-files, and
656 ;; replace them with just `status' which takes a fileset and a continuation
657 ;; (like dir-status) and returns a buffer in which the process(es) are run
658 ;; (or nil if it worked synchronously). Hopefully we can define the old
659 ;; 4 operations in term of this one.
661 ;; - backends that care about vc-stay-local should try to take it into
662 ;; account for vc-dir. Is this likely to be useful???
664 ;; - vc-dir listing needs a footer generated when it's done to make it obvious
665 ;; that it has finished.
668 ;;; Code:
670 (require 'vc-hooks)
671 (require 'vc-dispatcher)
672 (require 'tool-bar)
673 (require 'ewoc)
675 (eval-when-compile
676 (require 'dired)
677 (require 'dired-aux)
678 (require 'cl))
680 (unless (assoc 'vc-parent-buffer minor-mode-alist)
681 (setq minor-mode-alist
682 (cons '(vc-parent-buffer vc-parent-buffer-name)
683 minor-mode-alist)))
685 ;; General customization
687 (defgroup vc nil
688 "Version-control system in Emacs."
689 :group 'tools)
691 (defcustom vc-initial-comment nil
692 "If non-nil, prompt for initial comment when a file is registered."
693 :type 'boolean
694 :group 'vc)
696 (defcustom vc-default-init-revision "1.1"
697 "A string used as the default revision number when a new file is registered.
698 This can be overridden by giving a prefix argument to \\[vc-register]. This
699 can also be overridden by a particular VC backend."
700 :type 'string
701 :group 'vc
702 :version "20.3")
704 (defcustom vc-checkin-switches nil
705 "A string or list of strings specifying extra switches for checkin.
706 These are passed to the checkin program by \\[vc-checkin]."
707 :type '(choice (const :tag "None" nil)
708 (string :tag "Argument String")
709 (repeat :tag "Argument List"
710 :value ("")
711 string))
712 :group 'vc)
714 (defcustom vc-checkout-switches nil
715 "A string or list of strings specifying extra switches for checkout.
716 These are passed to the checkout program by \\[vc-checkout]."
717 :type '(choice (const :tag "None" nil)
718 (string :tag "Argument String")
719 (repeat :tag "Argument List"
720 :value ("")
721 string))
722 :group 'vc)
724 (defcustom vc-register-switches nil
725 "A string or list of strings; extra switches for registering a file.
726 These are passed to the checkin program by \\[vc-register]."
727 :type '(choice (const :tag "None" nil)
728 (string :tag "Argument String")
729 (repeat :tag "Argument List"
730 :value ("")
731 string))
732 :group 'vc)
734 (defcustom vc-diff-switches nil
735 "A string or list of strings specifying switches for diff under VC.
736 When running diff under a given BACKEND, VC concatenates the values of
737 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
738 get the switches for that command. Thus, `vc-diff-switches' should
739 contain switches that are specific to version control, but not
740 specific to any particular backend."
741 :type '(choice (const :tag "None" nil)
742 (string :tag "Argument String")
743 (repeat :tag "Argument List"
744 :value ("")
745 string))
746 :group 'vc
747 :version "21.1")
749 (defcustom vc-diff-knows-L nil
750 "*Indicates whether diff understands the -L option.
751 The value is either `yes', `no', or nil. If it is nil, VC tries
752 to use -L and sets this variable to remember whether it worked."
753 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
754 :group 'vc)
756 (defcustom vc-allow-async-revert nil
757 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
758 Enabling this option means that you can confirm a revert operation even
759 if the local changes in the file have not been found and displayed yet."
760 :type '(choice (const :tag "No" nil)
761 (const :tag "Yes" t))
762 :group 'vc
763 :version "22.1")
765 ;;;###autoload
766 (defcustom vc-checkout-hook nil
767 "Normal hook (list of functions) run after checking out a file.
768 See `run-hooks'."
769 :type 'hook
770 :group 'vc
771 :version "21.1")
773 (defcustom vc-annotate-display-mode 'fullscale
774 "Which mode to color the output of \\[vc-annotate] with by default."
775 :type '(choice (const :tag "By Color Map Range" nil)
776 (const :tag "Scale to Oldest" scale)
777 (const :tag "Scale Oldest->Newest" fullscale)
778 (number :tag "Specify Fractional Number of Days"
779 :value "20.5"))
780 :group 'vc)
782 ;;;###autoload
783 (defcustom vc-checkin-hook nil
784 "Normal hook (list of functions) run after commit or file checkin.
785 See also `log-edit-done-hook'."
786 :type 'hook
787 :options '(log-edit-comment-to-change-log)
788 :group 'vc)
790 ;;;###autoload
791 (defcustom vc-before-checkin-hook nil
792 "Normal hook (list of functions) run before a commit or a file checkin.
793 See `run-hooks'."
794 :type 'hook
795 :group 'vc)
797 ;; Annotate customization
798 (defcustom vc-annotate-color-map
799 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
800 ;; A custom sorted TTY colormap
801 (let* ((colors
802 (sort
803 (delq nil
804 (mapcar (lambda (x)
805 (if (not (or
806 (string-equal (car x) "white")
807 (string-equal (car x) "black") ))
808 (car x)))
809 (tty-color-alist)))
810 (lambda (a b)
811 (cond
812 ((or (string-equal a "red") (string-equal b "blue")) t)
813 ((or (string-equal b "red") (string-equal a "blue")) nil)
814 ((string-equal a "yellow") t)
815 ((string-equal b "yellow") nil)
816 ((string-equal a "cyan") t)
817 ((string-equal b "cyan") nil)
818 ((string-equal a "green") t)
819 ((string-equal b "green") nil)
820 ((string-equal a "magenta") t)
821 ((string-equal b "magenta") nil)
822 (t (string< a b))))))
823 (date 20.)
824 (delta (/ (- 360. date) (1- (length colors)))))
825 (mapcar (lambda (x)
826 (prog1
827 (cons date x)
828 (setq date (+ date delta)))) colors))
829 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
830 '(( 20. . "#FF3F3F")
831 ( 40. . "#FF6C3F")
832 ( 60. . "#FF993F")
833 ( 80. . "#FFC63F")
834 (100. . "#FFF33F")
835 (120. . "#DDFF3F")
836 (140. . "#B0FF3F")
837 (160. . "#83FF3F")
838 (180. . "#56FF3F")
839 (200. . "#3FFF56")
840 (220. . "#3FFF83")
841 (240. . "#3FFFB0")
842 (260. . "#3FFFDD")
843 (280. . "#3FF3FF")
844 (300. . "#3FC6FF")
845 (320. . "#3F99FF")
846 (340. . "#3F6CFF")
847 (360. . "#3F3FFF")))
848 "Association list of age versus color, for \\[vc-annotate].
849 Ages are given in units of fractional days. Default is eighteen
850 steps using a twenty day increment, from red to blue. For TTY
851 displays with 8 or fewer colors, the default is red to blue with
852 all other colors between (excluding black and white)."
853 :type 'alist
854 :group 'vc)
856 (defcustom vc-annotate-very-old-color "#3F3FFF"
857 "Color for lines older than the current color range in \\[vc-annotate]]."
858 :type 'string
859 :group 'vc)
861 (defcustom vc-annotate-background "black"
862 "Background color for \\[vc-annotate].
863 Default color is used if nil."
864 :type '(choice (const :tag "Default background" nil) (color))
865 :group 'vc)
867 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
868 "Menu elements for the mode-specific menu of VC-Annotate mode.
869 List of factors, used to expand/compress the time scale. See `vc-annotate'."
870 :type '(repeat number)
871 :group 'vc)
873 (defvar vc-annotate-mode-map
874 (let ((m (make-sparse-keymap)))
875 (define-key m "A" 'vc-annotate-revision-previous-to-line)
876 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
877 (define-key m "f" 'vc-annotate-find-revision-at-line)
878 (define-key m "J" 'vc-annotate-revision-at-line)
879 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
880 (define-key m "N" 'vc-annotate-next-revision)
881 (define-key m "P" 'vc-annotate-prev-revision)
882 (define-key m "W" 'vc-annotate-working-revision)
883 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
885 "Local keymap used for VC-Annotate mode.")
887 ;; Header-insertion hair
889 (defcustom vc-static-header-alist
890 '(("\\.c\\'" .
891 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
892 "*Associate static header string templates with file types.
893 A \%s in the template is replaced with the first string associated with
894 the file's version control type in `vc-header-alist'."
895 :type '(repeat (cons :format "%v"
896 (regexp :tag "File Type")
897 (string :tag "Header String")))
898 :group 'vc)
900 (defcustom vc-comment-alist
901 '((nroff-mode ".\\\"" ""))
902 "*Special comment delimiters for generating VC headers.
903 Add an entry in this list if you need to override the normal `comment-start'
904 and `comment-end' variables. This will only be necessary if the mode language
905 is sensitive to blank lines."
906 :type '(repeat (list :format "%v"
907 (symbol :tag "Mode")
908 (string :tag "Comment Start")
909 (string :tag "Comment End")))
910 :group 'vc)
912 (defcustom vc-checkout-carefully (= (user-uid) 0)
913 "*Non-nil means be extra-careful in checkout.
914 Verify that the file really is not locked
915 and that its contents match what the master file says."
916 :type 'boolean
917 :group 'vc)
918 (make-obsolete-variable 'vc-checkout-carefully
919 "the corresponding checks are always done now."
920 "21.1")
923 ;; Variables users don't need to see
925 (defvar vc-disable-async-diff nil
926 "VC sets this to t locally to disable some async diff operations.
927 Backends that offer asynchronous diffs should respect this variable
928 in their implementation of vc-BACKEND-diff.")
930 ;; File property caching
932 (defun vc-clear-context ()
933 "Clear all cached file properties."
934 (interactive)
935 (fillarray vc-file-prop-obarray 0))
937 (defmacro with-vc-properties (files form settings)
938 "Execute FORM, then maybe set per-file properties for FILES.
939 SETTINGS is an association list of property/value pairs. After
940 executing FORM, set those properties from SETTINGS that have not yet
941 been updated to their corresponding values."
942 (declare (debug t))
943 `(let ((vc-touched-properties (list t)))
944 ,form
945 (dolist (file ,files)
946 (dolist (setting ,settings)
947 (let ((property (car setting)))
948 (unless (memq property vc-touched-properties)
949 (put (intern file vc-file-prop-obarray)
950 property (cdr setting))))))))
952 ;; Two macros for elisp programming
954 ;;;###autoload
955 (defmacro with-vc-file (file comment &rest body)
956 "Check out a writable copy of FILE if necessary, then execute BODY.
957 Check in FILE with COMMENT (a string) after BODY has been executed.
958 FILE is passed through `expand-file-name'; BODY executed within
959 `save-excursion'. If FILE is not under version control, or you are
960 using a locking version-control system and the file is locked by
961 somebody else, signal error."
962 (declare (debug t) (indent 2))
963 (let ((filevar (make-symbol "file")))
964 `(let ((,filevar (expand-file-name ,file)))
965 (or (vc-backend ,filevar)
966 (error "File not under version control: `%s'" file))
967 (unless (vc-editable-p ,filevar)
968 (let ((state (vc-state ,filevar)))
969 (if (stringp state)
970 (error "`%s' is locking `%s'" state ,filevar)
971 (vc-checkout ,filevar t))))
972 (save-excursion
973 ,@body)
974 (vc-checkin (list ,filevar) nil ,comment))))
976 ;;;###autoload
977 (defmacro edit-vc-file (file comment &rest body)
978 "Edit FILE under version control, executing body.
979 Checkin with COMMENT after executing BODY.
980 This macro uses `with-vc-file', passing args to it.
981 However, before executing BODY, find FILE, and after BODY, save buffer."
982 (declare (debug t) (indent 2))
983 (let ((filevar (make-symbol "file")))
984 `(let ((,filevar (expand-file-name ,file)))
985 (with-vc-file
986 ,filevar ,comment
987 (set-buffer (find-file-noselect ,filevar))
988 ,@body
989 (save-buffer)))))
991 ;;; Code for deducing what fileset and backend to assume
993 (defun vc-responsible-backend (file &optional register)
994 "Return the name of a backend system that is responsible for FILE.
995 The optional argument REGISTER means that a backend suitable for
996 registration should be found.
998 If REGISTER is nil, then if FILE is already registered, return the
999 backend of FILE. If FILE is not registered, or a directory, then the
1000 first backend in `vc-handled-backends' that declares itself
1001 responsible for FILE is returned. If no backend declares itself
1002 responsible, return the first backend.
1004 If REGISTER is non-nil, return the first responsible backend under
1005 which FILE is not yet registered. If there is no such backend, return
1006 the first backend under which FILE is not yet registered, but could
1007 be registered."
1008 (when (not vc-handled-backends)
1009 (error "No handled backends"))
1010 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1011 (catch 'found
1012 ;; First try: find a responsible backend. If this is for registration,
1013 ;; it must be a backend under which FILE is not yet registered.
1014 (dolist (backend vc-handled-backends)
1015 (and (or (not register)
1016 (not (vc-call-backend backend 'registered file)))
1017 (vc-call-backend backend 'responsible-p file)
1018 (throw 'found backend)))
1019 ;; no responsible backend
1020 (if (not register)
1021 ;; if this is not for registration, the first backend must do
1022 (car vc-handled-backends)
1023 ;; for registration, we need to find a new backend that
1024 ;; could register FILE
1025 (dolist (backend vc-handled-backends)
1026 (and (not (vc-call-backend backend 'registered file))
1027 (vc-call-backend backend 'could-register file)
1028 (throw 'found backend)))
1029 (error "No backend that could register")))))
1031 (defun vc-expand-dirs (file-or-dir-list)
1032 "Expands directories in a file list specification.
1033 Only files already under version control are noticed."
1034 ;; FIXME: Kill this function.
1035 (let ((flattened '()))
1036 (dolist (node file-or-dir-list)
1037 (vc-file-tree-walk
1038 node (lambda (f) (when (vc-backend f) (push f flattened)))))
1039 (nreverse flattened)))
1041 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered
1042 include-files-not-directories)
1043 "Deduce a set of files and a backend to which to apply an operation.
1044 Return (BACKEND . FILESET)."
1045 (let* ((fileset (vc-dispatcher-selection-set
1046 #'vc-registered
1047 allow-directory-wildcard
1048 allow-unregistered
1049 include-files-not-directories))
1050 (backend (vc-backend (car fileset))))
1051 ;; All members of the fileset must have the same backend
1052 (dolist (f (cdr fileset))
1053 (unless (eq (vc-backend f) backend)
1054 (error "All members of a fileset must be under the same version-control system.")))
1055 (cons backend fileset)))
1057 (defun vc-ensure-vc-buffer ()
1058 "Make sure that the current buffer visits a version-controlled file."
1059 (cond
1060 (vc-dired-mode
1061 (set-buffer (find-file-noselect (dired-get-filename))))
1062 ((eq major-mode 'vc-dir-mode)
1063 (set-buffer (find-file-noselect (vc-dir-current-file))))
1065 (while (and vc-parent-buffer
1066 (buffer-live-p vc-parent-buffer)
1067 ;; Avoid infinite looping when vc-parent-buffer and
1068 ;; current buffer are the same buffer.
1069 (not (eq vc-parent-buffer (current-buffer))))
1070 (set-buffer vc-parent-buffer))
1071 (if (not buffer-file-name)
1072 (error "Buffer %s is not associated with a file" (buffer-name))
1073 (unless (vc-backend buffer-file-name)
1074 (error "File %s is not under version control" buffer-file-name))))))
1076 ;;; Support for the C-x v v command.
1077 ;; This is where all the single-file-oriented code from before the fileset
1078 ;; rewrite lives.
1080 (defsubst vc-editable-p (file)
1081 "Return non-nil if FILE can be edited."
1082 (let ((backend (vc-backend file)))
1083 (and backend
1084 (or (eq (vc-checkout-model backend (list file)) 'implicit)
1085 (memq (vc-state file) '(edited needs-merge conflict))))))
1087 (defun vc-compatible-state (p q)
1088 "Controls which states can be in the same commit."
1090 (eq p q)
1091 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1093 ;; Here's the major entry point.
1095 ;;;###autoload
1096 (defun vc-next-action (verbose)
1097 "Do the next logical version control operation on the current fileset.
1098 This requires that all files in the fileset be in the same state.
1100 For locking systems:
1101 If every file is not already registered, this registers each for version
1102 control.
1103 If every file is registered and not locked by anyone, this checks out
1104 a writable and locked file of each ready for editing.
1105 If every file is checked out and locked by the calling user, this
1106 first checks to see if each file has changed since checkout. If not,
1107 it performs a revert on that file.
1108 If every file has been changed, this pops up a buffer for entry
1109 of a log message; when the message has been entered, it checks in the
1110 resulting changes along with the log message as change commentary. If
1111 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1112 read-only copy of each changed file is left in place afterwards.
1113 If the affected file is registered and locked by someone else, you are
1114 given the option to steal the lock(s).
1116 For merging systems:
1117 If every file is not already registered, this registers each one for version
1118 control. This does an add, but not a commit.
1119 If every file is added but not committed, each one is committed.
1120 If every working file is changed, but the corresponding repository file is
1121 unchanged, this pops up a buffer for entry of a log message; when the
1122 message has been entered, it checks in the resulting changes along
1123 with the logmessage as change commentary. A writable file is retained.
1124 If the repository file is changed, you are asked if you want to
1125 merge in the changes into your working copy."
1126 (interactive "P")
1127 (let* ((vc-fileset (vc-deduce-fileset nil t))
1128 (vc-fileset-only-files (vc-deduce-fileset nil t t))
1129 (only-files (cdr vc-fileset-only-files))
1130 (backend (car vc-fileset))
1131 (files (cdr vc-fileset))
1132 (state (vc-state (car only-files)))
1133 (model (vc-checkout-model backend files))
1134 revision)
1136 ;; Verify that the fileset is homogeneous
1137 (dolist (file (cdr only-files))
1138 ;; Ignore directories, they are compatible with anything.
1139 (unless (file-directory-p file)
1140 (unless (vc-compatible-state (vc-state file) state)
1141 (error "%s:%s clashes with %s:%s"
1142 file (vc-state file) (car files) state))
1143 (unless (eq (vc-checkout-model backend (list file)) model)
1144 (error "Fileset has mixed checkout models"))))
1145 ;; Do the right thing
1146 (cond
1147 ((eq state 'missing)
1148 (error "Fileset files are missing, so cannot be operated on."))
1149 ;; Files aren't registered
1150 ((or (eq state 'unregistered)
1151 (eq state 'ignored))
1152 (mapc (lambda (arg) (vc-register nil arg)) files))
1153 ;; Files are up-to-date, or need a merge and user specified a revision
1154 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-update)))
1155 (cond
1156 (verbose
1157 ;; go to a different revision
1158 (setq revision (read-string "Branch, revision, or backend to move to: "))
1159 (let ((vsym (intern-soft (upcase revision))))
1160 (if (member vsym vc-handled-backends)
1161 (dolist (file files) (vc-transfer-file file vsym))
1162 (dolist (file files)
1163 (vc-checkout file (eq model 'implicit) revision)))))
1164 ((not (eq model 'implicit))
1165 ;; check the files out
1166 (dolist (file files) (vc-checkout file t)))
1168 ;; do nothing
1169 (message "Fileset is up-to-date"))))
1170 ;; Files have local changes
1171 ((vc-compatible-state state 'edited)
1172 (let ((ready-for-commit files))
1173 ;; If files are edited but read-only, give user a chance to correct
1174 (dolist (file files)
1175 (unless (file-writable-p file)
1176 ;; Make the file+buffer read-write.
1177 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1178 (error "Aborted"))
1179 (set-file-modes file (logior (file-modes file) 128))
1180 (let ((visited (get-file-buffer file)))
1181 (when visited
1182 (with-current-buffer visited
1183 (toggle-read-only -1))))))
1184 ;; Allow user to revert files with no changes
1185 (save-excursion
1186 (dolist (file files)
1187 (let ((visited (get-file-buffer file)))
1188 ;; For files with locking, if the file does not contain
1189 ;; any changes, just let go of the lock, i.e. revert.
1190 (when (and (not (eq model 'implicit))
1191 (vc-workfile-unchanged-p file)
1192 ;; If buffer is modified, that means the user just
1193 ;; said no to saving it; in that case, don't revert,
1194 ;; because the user might intend to save after
1195 ;; finishing the log entry and committing.
1196 (not (and visited (buffer-modified-p))))
1197 (vc-revert-file file)
1198 (delete file ready-for-commit)))))
1199 ;; Remaining files need to be committed
1200 (if (not ready-for-commit)
1201 (message "No files remain to be committed")
1202 (if (not verbose)
1203 (vc-checkin ready-for-commit)
1204 (progn
1205 (setq revision (read-string "New revision or backend: "))
1206 (let ((vsym (intern (upcase revision))))
1207 (if (member vsym vc-handled-backends)
1208 (dolist (file files) (vc-transfer-file file vsym))
1209 (vc-checkin ready-for-commit revision))))))))
1210 ;; locked by somebody else (locking VCSes only)
1211 ((stringp state)
1212 ;; In the old days, we computed the revision once and used it on
1213 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1214 ;; computed the revision once (incorrectly, using a free var) and
1215 ;; used it on all files. To fix the free var bug, we can either
1216 ;; use `(car files)' or do what we do here: distribute the
1217 ;; revision computation among `files'. Although this may be
1218 ;; tedious for those backends where a "revision" is a trans-file
1219 ;; concept, it is nonetheless correct for both those and (more
1220 ;; importantly) for those where "revision" is a per-file concept.
1221 ;; If the intersection of the former group and "locking VCSes" is
1222 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1223 ;; pre-computation approach of yore.
1224 (dolist (file files)
1225 (vc-steal-lock
1226 file (if verbose
1227 (read-string (format "%s revision to steal: " file))
1228 (vc-working-revision file))
1229 state)))
1230 ;; conflict
1231 ((eq state 'conflict)
1232 (vc-mark-resolved files))
1233 ;; needs-update
1234 ((eq state 'needs-update)
1235 (dolist (file files)
1236 (if (yes-or-no-p (format
1237 "%s is not up-to-date. Get latest revision? "
1238 (file-name-nondirectory file)))
1239 (vc-checkout file (eq model 'implicit) t)
1240 (when (and (not (eq model 'implicit))
1241 (yes-or-no-p "Lock this revision? "))
1242 (vc-checkout file t)))))
1243 ;; needs-merge
1244 ((eq state 'needs-merge)
1245 (dolist (file files)
1246 (when (yes-or-no-p (format
1247 "%s is not up-to-date. Merge in changes now? "
1248 (file-name-nondirectory file)))
1249 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1251 ;; unlocked-changes
1252 ((eq state 'unlocked-changes)
1253 (dolist (file files)
1254 (when (not (equal buffer-file-name file))
1255 (find-file-other-window file))
1256 (if (save-window-excursion
1257 (vc-diff-internal nil (cons (car vc-fileset) (list file))
1258 (vc-working-revision file) nil)
1259 (goto-char (point-min))
1260 (let ((inhibit-read-only t))
1261 (insert
1262 (format "Changes to %s since last lock:\n\n" file)))
1263 (not (beep))
1264 (yes-or-no-p (concat "File has unlocked changes. "
1265 "Claim lock retaining changes? ")))
1266 (progn (vc-call steal-lock file)
1267 (clear-visited-file-modtime)
1268 ;; Must clear any headers here because they wouldn't
1269 ;; show that the file is locked now.
1270 (vc-clear-headers file)
1271 (write-file buffer-file-name)
1272 (vc-mode-line file))
1273 (if (not (yes-or-no-p
1274 "Revert to checked-in revision, instead? "))
1275 (error "Checkout aborted")
1276 (vc-revert-buffer-internal t t)
1277 (vc-checkout file t)))))
1278 ;; Unknown fileset state
1280 (error "Fileset is in an unknown state %s" state)))))
1282 (defun vc-create-repo (backend)
1283 "Create an empty repository in the current directory."
1284 (interactive
1285 (list
1286 (intern
1287 (upcase
1288 (completing-read
1289 "Create repository for: "
1290 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1291 nil t)))))
1292 (vc-call-backend backend 'create-repo))
1294 ;;;###autoload
1295 (defun vc-register (&optional set-revision fname comment)
1296 "Register into a version control system.
1297 If FNAME is given register that file, otherwise register the current file.
1298 With prefix argument SET-REVISION, allow user to specify initial revision
1299 level. If COMMENT is present, use that as an initial comment.
1301 The version control system to use is found by cycling through the list
1302 `vc-handled-backends'. The first backend in that list which declares
1303 itself responsible for the file (usually because other files in that
1304 directory are already registered under that backend) will be used to
1305 register the file. If no backend declares itself responsible, the
1306 first backend that could register the file is used."
1307 (interactive "P")
1308 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1310 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1311 (unless fname (setq fname buffer-file-name))
1312 (when (vc-backend fname)
1313 (if (vc-registered fname)
1314 (error "This file is already registered")
1315 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1316 (error "Aborted"))))
1317 ;; Watch out for new buffers of size 0: the corresponding file
1318 ;; does not exist yet, even though buffer-modified-p is nil.
1319 (when bname
1320 (with-current-buffer bname
1321 (when (and (not (buffer-modified-p))
1322 (zerop (buffer-size))
1323 (not (file-exists-p buffer-file-name)))
1324 (set-buffer-modified-p t))
1325 (vc-buffer-sync)))
1326 (vc-start-logentry (list fname)
1327 (if set-revision
1328 (read-string (format "Initial revision level for %s: "
1329 fname))
1330 (vc-call-backend (vc-responsible-backend fname)
1331 'init-revision))
1332 (or comment (not vc-initial-comment))
1334 "Enter initial comment."
1335 (lambda (files rev comment)
1336 (dolist (file files)
1337 (message "Registering %s... " file)
1338 (let ((backend (vc-responsible-backend file t)))
1339 (vc-file-clearprops file)
1340 (vc-call-backend backend 'register (list file) rev comment)
1341 (vc-file-setprop file 'vc-backend backend)
1342 (unless vc-make-backup-files
1343 (make-local-variable 'backup-inhibited)
1344 (setq backup-inhibited t)))
1345 (message "Registering %s... done" file))))))
1347 (defun vc-register-with (backend)
1348 "Register the current file with a specified back end."
1349 (interactive "SBackend: ")
1350 (when (not (member backend vc-handled-backends))
1351 (error "Unknown back end."))
1352 (let ((vc-handled-backends (list backend)))
1353 (call-interactively 'vc-register)))
1355 (defun vc-checkout (file &optional writable rev)
1356 "Retrieve a copy of the revision REV of FILE.
1357 If WRITABLE is non-nil, make sure the retrieved file is writable.
1358 REV defaults to the latest revision.
1360 After check-out, runs the normal hook `vc-checkout-hook'."
1361 (and writable
1362 (not rev)
1363 (vc-call make-version-backups-p file)
1364 (vc-up-to-date-p file)
1365 (vc-make-version-backup file))
1366 (let ((backend (vc-backend file)))
1367 (with-vc-properties (list file)
1368 (condition-case err
1369 (vc-call-backend backend 'checkout file writable rev)
1370 (file-error
1371 ;; Maybe the backend is not installed ;-(
1372 (when writable
1373 (let ((buf (get-file-buffer file)))
1374 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1375 (signal (car err) (cdr err))))
1376 `((vc-state . ,(if (or (eq (vc-checkout-model backend (list file)) 'implicit)
1377 (not writable))
1378 (if (vc-call latest-on-branch-p file)
1379 'up-to-date
1380 'needs-update)
1381 'edited))
1382 (vc-checkout-time . ,(nth 5 (file-attributes file))))))
1383 (vc-resynch-buffer file t t)
1384 (run-hooks 'vc-checkout-hook))
1386 (defun vc-mark-resolved (files)
1387 (with-vc-properties
1388 files
1389 (vc-call mark-resolved files)
1390 ;; XXX: Is this TRTD? Might not be.
1391 `((vc-state . edited))))
1393 (defun vc-steal-lock (file rev owner)
1394 "Steal the lock on FILE."
1395 (let (file-description)
1396 (if rev
1397 (setq file-description (format "%s:%s" file rev))
1398 (setq file-description file))
1399 (when (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1400 file-description owner)))
1401 (error "Steal canceled"))
1402 (message "Stealing lock on %s..." file)
1403 (with-vc-properties
1404 (list file)
1405 (vc-call steal-lock file rev)
1406 `((vc-state . edited)))
1407 (vc-resynch-buffer file t t)
1408 (message "Stealing lock on %s...done" file)
1409 ;; Write mail after actually stealing, because if the stealing
1410 ;; goes wrong, we don't want to send any mail.
1411 (compose-mail owner (format "Stolen lock on %s" file-description))
1412 (setq default-directory (expand-file-name "~/"))
1413 (goto-char (point-max))
1414 (insert
1415 (format "I stole the lock on %s, " file-description)
1416 (current-time-string)
1417 ".\n")
1418 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1420 (defun vc-checkin (files &optional rev comment initial-contents)
1421 "Check in FILES.
1422 The optional argument REV may be a string specifying the new revision
1423 level (if nil increment the current level). COMMENT is a comment
1424 string; if omitted, a buffer is popped up to accept a comment. If
1425 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1426 of the log entry buffer.
1428 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1429 that the version control system supports this mode of operation.
1431 Runs the normal hooks `vc-before-checkin-hook' and `vc-checkin-hook'."
1432 (when vc-before-checkin-hook
1433 (run-hooks 'vc-before-checkin-hook))
1434 (vc-start-logentry
1435 files rev comment initial-contents
1436 "Enter a change comment."
1437 (lambda (files rev comment)
1438 (message "Checking in %s..." (vc-delistify files))
1439 ;; "This log message intentionally left almost blank".
1440 ;; RCS 5.7 gripes about white-space-only comments too.
1441 (or (and comment (string-match "[^\t\n ]" comment))
1442 (setq comment "*** empty log message ***"))
1443 (with-vc-properties
1444 files
1445 ;; We used to change buffers to get local value of vc-checkin-switches,
1446 ;; but 'the' local buffer is not a well-defined concept for filesets.
1447 (progn
1448 (vc-call checkin files rev comment)
1449 (mapc 'vc-delete-automatic-version-backups files))
1450 `((vc-state . up-to-date)
1451 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1452 (vc-working-revision . nil)))
1453 (message "Checking in %s...done" (vc-delistify files)))
1454 'vc-checkin-hook))
1456 ;;; Additional entry points for examining version histories
1458 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1459 ;; "List differences for all registered files at and below DIR.
1460 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1461 ;; ;; This implementation does an explicit tree walk, and calls
1462 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1463 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1464 ;; ;; and to call it only for files that are actually changed.
1465 ;; ;; However, this is expensive for some backends, and so it is left
1466 ;; ;; to backend-specific implementations.
1467 ;; (setq default-directory dir)
1468 ;; (vc-file-tree-walk
1469 ;; default-directory
1470 ;; (lambda (f)
1471 ;; (vc-exec-after
1472 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1473 ;; (message "Looking at %s" ',f)
1474 ;; (vc-call-backend ',(vc-backend f)
1475 ;; 'diff (list ',f) ',rev1 ',rev2))))))
1477 (defun vc-coding-system-for-diff (file)
1478 "Return the coding system for reading diff output for FILE."
1479 (or coding-system-for-read
1480 ;; if we already have this file open,
1481 ;; use the buffer's coding system
1482 (let ((buf (find-buffer-visiting file)))
1483 (when buf (with-current-buffer buf
1484 buffer-file-coding-system)))
1485 ;; otherwise, try to find one based on the file name
1486 (car (find-operation-coding-system 'insert-file-contents file))
1487 ;; and a final fallback
1488 'undecided))
1490 (defun vc-switches (backend op)
1491 (let ((switches
1492 (or (when backend
1493 (let ((sym (vc-make-backend-sym
1494 backend (intern (concat (symbol-name op)
1495 "-switches")))))
1496 (when (boundp sym) (symbol-value sym))))
1497 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1498 (when (boundp sym) (symbol-value sym)))
1499 (cond
1500 ((eq op 'diff) diff-switches)))))
1501 (if (stringp switches) (list switches)
1502 ;; If not a list, return nil.
1503 ;; This is so we can set vc-diff-switches to t to override
1504 ;; any switches in diff-switches.
1505 (when (listp switches) switches))))
1507 ;; Old def for compatibility with Emacs-21.[123].
1508 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1509 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
1511 (defun vc-diff-finish (buffer messages)
1512 ;; The empty sync output case has already been handled, so the only
1513 ;; possibility of an empty output is for an async process.
1514 (when (buffer-live-p buffer)
1515 (let ((window (get-buffer-window buffer t))
1516 (emptyp (zerop (buffer-size buffer))))
1517 (with-current-buffer buffer
1518 (and messages emptyp
1519 (let ((inhibit-read-only t))
1520 (insert (cdr messages) ".\n")
1521 (message "%s" (cdr messages))))
1522 (goto-char (point-min))
1523 (when window
1524 (shrink-window-if-larger-than-buffer window)))
1525 (when (and messages (not emptyp))
1526 (message "%sdone" (car messages))))))
1528 (defvar vc-diff-added-files nil
1529 "If non-nil, diff added files by comparing them to /dev/null.")
1531 (defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose)
1532 "Report diffs between two revisions of a fileset.
1533 Diff output goes to the *vc-diff* buffer. The function
1534 returns t if the buffer had changes, nil otherwise."
1535 (let* ((files (cdr vc-fileset))
1536 (messages (cons (format "Finding changes in %s..."
1537 (vc-delistify files))
1538 (format "No changes between %s and %s"
1539 (or rev1 "working revision")
1540 (or rev2 "workfile"))))
1541 ;; Set coding system based on the first file. It's a kluge,
1542 ;; but the only way to set it for each file included would
1543 ;; be to call the back end separately for each file.
1544 (coding-system-for-read
1545 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
1546 (vc-setup-buffer "*vc-diff*")
1547 (message "%s" (car messages))
1548 ;; Many backends don't handle well the case of a file that has been
1549 ;; added but not yet committed to the repo (notably CVS and Subversion).
1550 ;; Do that work here so the backends don't have to futz with it. --ESR
1552 ;; Actually most backends (including CVS) have options to control the
1553 ;; behavior since which one is better depends on the user and on the
1554 ;; situation). Worse yet: this code does not handle the case where
1555 ;; `file' is a directory which contains added files.
1556 ;; I made it conditional on vc-diff-added-files but it should probably
1557 ;; just be removed (or copied/moved to specific backends). --Stef.
1558 (when vc-diff-added-files
1559 (let ((filtered '()))
1560 (dolist (file files)
1561 (if (or (file-directory-p file)
1562 (not (string= (vc-working-revision file) "0")))
1563 (push file filtered)
1564 ;; This file is added but not yet committed;
1565 ;; there is no master file to diff against.
1566 (if (or rev1 rev2)
1567 (error "No revisions of %s exist" file)
1568 ;; We regard this as "changed".
1569 ;; Diff it against /dev/null.
1570 (apply 'vc-do-command "*vc-diff*"
1571 1 "diff" file
1572 (append (vc-switches nil 'diff) '("/dev/null"))))))
1573 (setq files (nreverse filtered))))
1574 (let ((vc-disable-async-diff (not async)))
1575 (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 "*vc-diff*"))
1576 (set-buffer "*vc-diff*")
1577 (if (and (zerop (buffer-size))
1578 (not (get-buffer-process (current-buffer))))
1579 ;; Treat this case specially so as not to pop the buffer.
1580 (progn
1581 (message "%s" (cdr messages))
1582 nil)
1583 (diff-mode)
1584 ;; Make the *vc-diff* buffer read only, the diff-mode key
1585 ;; bindings are nicer for read only buffers. pcl-cvs does the
1586 ;; same thing.
1587 (setq buffer-read-only t)
1588 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
1589 messages)))
1590 ;; Display the buffer, but at the end because it can change point.
1591 (pop-to-buffer (current-buffer))
1592 ;; In the async case, we return t even if there are no differences
1593 ;; because we don't know that yet.
1594 t)))
1596 ;;;###autoload
1597 (defun vc-version-diff (files rev1 rev2)
1598 "Report diffs between revisions of the fileset in the repository history."
1599 (interactive
1600 (let* ((vc-fileset (vc-deduce-fileset t))
1601 (files (cdr vc-fileset))
1602 (first (car files))
1603 (completion-table
1604 (vc-call revision-completion-table files))
1605 (rev1-default nil)
1606 (rev2-default nil))
1607 (cond
1608 ;; someday we may be able to do revision completion on non-singleton
1609 ;; filesets, but not yet.
1610 ((/= (length files) 1)
1611 nil)
1612 ;; if it's a directory, don't supply any revision default
1613 ((file-directory-p first)
1614 nil)
1615 ;; if the file is not up-to-date, use working revision as older revision
1616 ((not (vc-up-to-date-p first))
1617 (setq rev1-default (vc-working-revision first)))
1618 ;; if the file is not locked, use last and previous revisions as defaults
1620 (setq rev1-default (vc-call previous-revision first
1621 (vc-working-revision first)))
1622 (when (string= rev1-default "") (setq rev1-default nil))
1623 (setq rev2-default (vc-working-revision first))))
1624 ;; construct argument list
1625 (let* ((rev1-prompt (if rev1-default
1626 (concat "Older revision (default "
1627 rev1-default "): ")
1628 "Older revision: "))
1629 (rev2-prompt (concat "Newer revision (default "
1630 (or rev2-default "current source") "): "))
1631 (rev1 (if completion-table
1632 (completing-read rev1-prompt completion-table
1633 nil nil nil nil rev1-default)
1634 (read-string rev1-prompt nil nil rev1-default)))
1635 (rev2 (if completion-table
1636 (completing-read rev2-prompt completion-table
1637 nil nil nil nil rev2-default)
1638 (read-string rev2-prompt nil nil rev2-default))))
1639 (when (string= rev1 "") (setq rev1 nil))
1640 (when (string= rev2 "") (setq rev2 nil))
1641 (list files rev1 rev2))))
1642 (when (and (not rev1) rev2)
1643 (error "Not a valid revision range."))
1644 (vc-diff-internal
1645 t (cons (car (vc-deduce-fileset t)) files) rev1 rev2 (interactive-p)))
1647 ;; (defun vc-contains-version-controlled-file (dir)
1648 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
1649 ;; (catch 'found
1650 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
1651 ;; nil))
1653 ;;;###autoload
1654 (defun vc-diff (historic &optional not-urgent)
1655 "Display diffs between file revisions.
1656 Normally this compares the currently selected fileset with their
1657 working revisions. With a prefix argument HISTORIC, it reads two revision
1658 designators specifying which revisions to compare.
1660 If no current fileset is available (that is, we are not in
1661 VC-Dired mode and the visited file of the current buffer is not
1662 under version control) and we're in a Dired buffer, use
1663 the current directory.
1664 The optional argument NOT-URGENT non-nil means it is ok to say no to
1665 saving the buffer."
1666 (interactive (list current-prefix-arg t))
1667 (if historic
1668 (call-interactively 'vc-version-diff)
1669 (when buffer-file-name (vc-buffer-sync not-urgent))
1670 (vc-diff-internal t (vc-deduce-fileset t) nil nil (interactive-p))))
1673 ;;;###autoload
1674 (defun vc-revision-other-window (rev)
1675 "Visit revision REV of the current file in another window.
1676 If the current file is named `F', the revision is named `F.~REV~'.
1677 If `F.~REV~' already exists, use it instead of checking it out again."
1678 (interactive
1679 (save-current-buffer
1680 (vc-ensure-vc-buffer)
1681 (let ((completion-table
1682 (vc-call revision-completion-table buffer-file-name))
1683 (prompt "Revision to visit (default is working revision): "))
1684 (list
1685 (if completion-table
1686 (completing-read prompt completion-table)
1687 (read-string prompt))))))
1688 (vc-ensure-vc-buffer)
1689 (let* ((file buffer-file-name)
1690 (revision (if (string-equal rev "")
1691 (vc-working-revision file)
1692 rev)))
1693 (switch-to-buffer-other-window (vc-find-revision file revision))))
1695 (defun vc-find-revision (file revision)
1696 "Read REVISION of FILE into a buffer and return the buffer."
1697 (let ((automatic-backup (vc-version-backup-file-name file revision))
1698 (filebuf (or (get-file-buffer file) (current-buffer)))
1699 (filename (vc-version-backup-file-name file revision 'manual)))
1700 (unless (file-exists-p filename)
1701 (if (file-exists-p automatic-backup)
1702 (rename-file automatic-backup filename nil)
1703 (message "Checking out %s..." filename)
1704 (with-current-buffer filebuf
1705 (let ((failed t))
1706 (unwind-protect
1707 (let ((coding-system-for-read 'no-conversion)
1708 (coding-system-for-write 'no-conversion))
1709 (with-temp-file filename
1710 (let ((outbuf (current-buffer)))
1711 ;; Change buffer to get local value of
1712 ;; vc-checkout-switches.
1713 (with-current-buffer filebuf
1714 (vc-call find-revision file revision outbuf))))
1715 (setq failed nil))
1716 (when (and failed (file-exists-p filename))
1717 (delete-file filename))))
1718 (vc-mode-line file))
1719 (message "Checking out %s...done" filename)))
1720 (let ((result-buf (find-file-noselect filename)))
1721 (with-current-buffer result-buf
1722 ;; Set the parent buffer so that things like
1723 ;; C-x v g, C-x v l, ... etc work.
1724 (set (make-local-variable 'vc-parent-buffer) filebuf))
1725 result-buf)))
1727 ;; Header-insertion code
1729 ;;;###autoload
1730 (defun vc-insert-headers ()
1731 "Insert headers into a file for use with a version control system.
1732 Headers desired are inserted at point, and are pulled from
1733 the variable `vc-BACKEND-header'."
1734 (interactive)
1735 (vc-ensure-vc-buffer)
1736 (save-excursion
1737 (save-restriction
1738 (widen)
1739 (when (or (not (vc-check-headers))
1740 (y-or-n-p "Version headers already exist. Insert another set? "))
1741 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1742 (comment-start-vc (or (car delims) comment-start "#"))
1743 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1744 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
1745 'header))
1746 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1747 (dolist (s hdstrings)
1748 (insert comment-start-vc "\t" s "\t"
1749 comment-end-vc "\n"))
1750 (when vc-static-header-alist
1751 (dolist (f vc-static-header-alist)
1752 (when (string-match (car f) buffer-file-name)
1753 (insert (format (cdr f) (car hdstrings)))))))))))
1755 (defun vc-clear-headers (&optional file)
1756 "Clear all version headers in the current buffer (or FILE).
1757 The headers are reset to their non-expanded form."
1758 (let* ((filename (or file buffer-file-name))
1759 (visited (find-buffer-visiting filename))
1760 (backend (vc-backend filename)))
1761 (when (vc-find-backend-function backend 'clear-headers)
1762 (if visited
1763 (let ((context (vc-buffer-context)))
1764 ;; save-excursion may be able to relocate point and mark
1765 ;; properly. If it fails, vc-restore-buffer-context
1766 ;; will give it a second try.
1767 (save-excursion
1768 (vc-call-backend backend 'clear-headers))
1769 (vc-restore-buffer-context context))
1770 (set-buffer (find-file-noselect filename))
1771 (vc-call-backend backend 'clear-headers)
1772 (kill-buffer filename)))))
1774 (defun vc-modify-change-comment (files rev oldcomment)
1775 "Edit the comment associated with the given files and revision."
1776 (vc-start-logentry
1777 files rev oldcomment t
1778 "Enter a replacement change comment."
1779 (lambda (files rev comment)
1780 (vc-call-backend
1781 ;; Less of a kluge than it looks like; log-view mode only passes
1782 ;; this function a singleton list. Arguments left in this form in
1783 ;; case the more general operation ever becomes meaningful.
1784 (vc-responsible-backend (car files))
1785 'modify-change-comment files rev comment))))
1787 ;;;###autoload
1788 (defun vc-merge ()
1789 "Merge changes between two revisions into the current buffer's file.
1790 This asks for two revisions to merge from in the minibuffer. If the
1791 first revision is a branch number, then merge all changes from that
1792 branch. If the first revision is empty, merge news, i.e. recent changes
1793 from the current branch.
1795 See Info node `Merging'."
1796 (interactive)
1797 (vc-ensure-vc-buffer)
1798 (vc-buffer-sync)
1799 (let* ((file buffer-file-name)
1800 (backend (vc-backend file))
1801 (state (vc-state file))
1802 first-revision second-revision status)
1803 (cond
1804 ((stringp state) ;; Locking VCses only
1805 (error "File is locked by %s" state))
1806 ((not (vc-editable-p file))
1807 (if (y-or-n-p
1808 "File must be checked out for merging. Check out now? ")
1809 (vc-checkout file t)
1810 (error "Merge aborted"))))
1811 (setq first-revision
1812 (read-string (concat "Branch or revision to merge from "
1813 "(default news on current branch): ")))
1814 (if (string= first-revision "")
1815 (if (not (vc-find-backend-function backend 'merge-news))
1816 (error "Sorry, merging news is not implemented for %s" backend)
1817 (setq status (vc-call merge-news file)))
1818 (if (not (vc-find-backend-function backend 'merge))
1819 (error "Sorry, merging is not implemented for %s" backend)
1820 (if (not (vc-branch-p first-revision))
1821 (setq second-revision
1822 (read-string "Second revision: "
1823 (concat (vc-branch-part first-revision) ".")))
1824 ;; We want to merge an entire branch. Set revisions
1825 ;; accordingly, so that vc-BACKEND-merge understands us.
1826 (setq second-revision first-revision)
1827 ;; first-revision must be the starting point of the branch
1828 (setq first-revision (vc-branch-part first-revision)))
1829 (setq status (vc-call merge file first-revision second-revision))))
1830 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
1832 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1833 (vc-resynch-buffer file t (not (buffer-modified-p)))
1834 (if (zerop status) (message "Merge successful")
1835 (smerge-mode 1)
1836 (message "File contains conflicts.")))
1838 ;;;###autoload
1839 (defalias 'vc-resolve-conflicts 'smerge-ediff)
1841 ;; VC Dired hook
1842 ;; FIXME: Remove Dired support when vc-dir is ready.
1844 (defun vc-dired-hook ()
1845 "Reformat the listing according to version control.
1846 Called by dired after any portion of a vc-dired buffer has been read in."
1847 (message "Getting version information... ")
1848 ;; if the backend supports it, get the state
1849 ;; of all files in this directory at once
1850 (let ((backend (vc-responsible-backend default-directory)))
1851 ;; check `backend' can really handle `default-directory'.
1852 (if (and (vc-call-backend backend 'responsible-p default-directory)
1853 (vc-find-backend-function backend 'dir-state))
1854 (vc-call-backend backend 'dir-state default-directory)))
1855 (let (filename
1856 (inhibit-read-only t)
1857 (buffer-undo-list t))
1858 (goto-char (point-min))
1859 (while (not (eobp))
1860 (cond
1861 ;; subdir header line
1862 ((dired-get-subdir)
1863 (forward-line 1)
1864 ;; erase (but don't remove) the "total" line
1865 (delete-region (point) (line-end-position))
1866 (beginning-of-line)
1867 (forward-line 1))
1868 ;; file line
1869 ((setq filename (dired-get-filename nil t))
1870 (cond
1871 ;; subdir
1872 ((file-directory-p filename)
1873 (cond
1874 ((member (file-name-nondirectory filename)
1875 vc-directory-exclusion-list)
1876 (let ((pos (point)))
1877 (dired-kill-tree filename)
1878 (goto-char pos)
1879 (dired-kill-line)))
1880 (vc-dired-terse-mode
1881 ;; Don't show directories in terse mode. Don't use
1882 ;; dired-kill-line to remove it, because in recursive listings,
1883 ;; that would remove the directory contents as well.
1884 (delete-region (line-beginning-position)
1885 (progn (forward-line 1) (point))))
1886 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
1887 (dired-kill-line))
1889 (vc-dired-reformat-line nil)
1890 (forward-line 1))))
1891 ;; Try to head off calling the expensive state query -
1892 ;; ignore object files, TeX intermediate files, and so forth.
1893 ((vc-dired-ignorable-p filename)
1894 (dired-kill-line))
1895 ;; Ordinary file -- call the (possibly expensive) state query
1897 ;; First case: unregistered or unknown. (Unknown shouldn't happen here)
1898 ((member (vc-state filename) '(nil unregistered))
1899 (if vc-dired-terse-mode
1900 (dired-kill-line)
1901 (vc-dired-reformat-line "?")
1902 (forward-line 1)))
1903 ;; Either we're in non-terse mode or it's out of date
1904 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
1905 (vc-dired-reformat-line (vc-call prettify-state-info filename))
1906 (forward-line 1))
1907 ;; Remaining cases are under version control but uninteresting
1909 (dired-kill-line))))
1910 ;; any other line
1911 (t (forward-line 1))))
1912 (vc-dired-purge))
1913 (message "Getting version information... done")
1914 (save-restriction
1915 (widen)
1916 (cond ((eq (count-lines (point-min) (point-max)) 1)
1917 (goto-char (point-min))
1918 (message "No changes pending under %s" default-directory)))))
1920 ;; VC status implementation
1922 (defun vc-default-status-extra-headers (backend dir)
1923 ;; Be loud by default to remind people to add coded to display
1924 ;; backend specific headers.
1925 ;; XXX: change this to return nil before the release.
1926 "Extra : Add backend specific headers here")
1928 (defun vc-dir-headers (backend dir)
1929 "Display the headers in the *VC status* buffer.
1930 It calls the `status-extra-headers' backend method to display backend
1931 specific headers."
1932 (concat
1933 (propertize "VC backend : " 'face 'font-lock-type-face)
1934 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
1935 (propertize "Working dir: " 'face 'font-lock-type-face)
1936 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
1937 (vc-call-backend backend 'status-extra-headers dir)
1938 "\n"))
1940 (defun vc-default-status-printer (backend fileentry)
1941 "Pretty print FILEENTRY."
1942 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1943 (let ((state
1944 (if (vc-dir-fileinfo->directory fileentry)
1945 'DIRECTORY
1946 (vc-dir-fileinfo->state fileentry))))
1947 (insert
1948 (propertize
1949 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1950 'face 'font-lock-type-face)
1952 (propertize
1953 (format "%-20s" state)
1954 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1955 ((memq state '(missing conflict)) 'font-lock-warning-face)
1956 (t 'font-lock-variable-name-face))
1957 'mouse-face 'highlight)
1959 (propertize
1960 (format "%s" (vc-dir-fileinfo->name fileentry))
1961 'face 'font-lock-function-name-face
1962 'mouse-face 'highlight))))
1964 (defun vc-default-extra-status-menu (backend)
1965 nil)
1967 ;; This is used to that VC backends could add backend specific menu
1968 ;; items to vc-dir-menu-map.
1969 (defun vc-dir-menu-map-filter (orig-binding)
1970 (when (and (symbolp orig-binding) (fboundp orig-binding))
1971 (setq orig-binding (indirect-function orig-binding)))
1972 (let ((ext-binding
1973 (vc-call-backend (vc-responsible-backend default-directory)
1974 'extra-status-menu)))
1975 (if (null ext-binding)
1976 orig-binding
1977 (append orig-binding
1978 '("----")
1979 ext-binding))))
1981 (defun vc-dir-refresh-files (files default-state)
1982 "Refresh some files in the VC status buffer."
1983 (let ((backend (vc-responsible-backend default-directory))
1984 (status-buffer (current-buffer))
1985 (def-dir default-directory))
1986 (vc-set-mode-line-busy-indicator)
1987 ;; Call the `dir-status-file' backend function.
1988 ;; `dir-status-file' is supposed to be asynchronous.
1989 ;; It should compute the results, and then call the function
1990 ;; passed as an argument in order to update the vc-dir buffer
1991 ;; with the results.
1992 (unless (buffer-live-p vc-dir-process-buffer)
1993 (setq vc-dir-process-buffer
1994 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1995 (lexical-let ((buffer (current-buffer)))
1996 (with-current-buffer vc-dir-process-buffer
1997 (cd def-dir)
1998 (erase-buffer)
1999 (vc-call-backend
2000 backend 'dir-status-files def-dir files default-state
2001 (lambda (entries &optional more-to-come)
2002 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
2003 ;; If MORE-TO-COME is true, then more updates will come from
2004 ;; the asynchronous process.
2005 (with-current-buffer buffer
2006 (vc-dir-update entries buffer)
2007 (unless more-to-come
2008 (setq mode-line-process nil)
2009 ;; Remove the ones that haven't been updated at all.
2010 ;; Those not-updated are those whose state is nil because the
2011 ;; file/dir doesn't exist and isn't versioned.
2012 (ewoc-filter vc-ewoc
2013 (lambda (info)
2014 (not (vc-dir-fileinfo->needs-update info))))))))))))
2016 (defun vc-dir-refresh ()
2017 "Refresh the contents of the VC status buffer.
2018 Throw an error if another update process is in progress."
2019 (interactive)
2020 (if (vc-dir-busy)
2021 (error "Another update process is in progress, cannot run two at a time")
2022 (let ((backend (vc-responsible-backend default-directory))
2023 (status-buffer (current-buffer))
2024 (def-dir default-directory))
2025 (vc-set-mode-line-busy-indicator)
2026 ;; Call the `dir-status' backend function.
2027 ;; `dir-status' is supposed to be asynchronous.
2028 ;; It should compute the results, and then call the function
2029 ;; passed as an argument in order to update the vc-dir buffer
2030 ;; with the results.
2032 ;; Create a buffer that can be used by `dir-status' and call
2033 ;; `dir-status' with this buffer as the current buffer. Use
2034 ;; `vc-dir-process-buffer' to remember this buffer, so that
2035 ;; it can be used later to kill the update process in case it
2036 ;; takes too long.
2037 (unless (buffer-live-p vc-dir-process-buffer)
2038 (setq vc-dir-process-buffer
2039 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
2040 ;; set the needs-update flag on all entries
2041 (ewoc-map (lambda (info) (setf (vc-dir-fileinfo->needs-update info) t) nil)
2042 vc-ewoc)
2043 (lexical-let ((buffer (current-buffer)))
2044 (with-current-buffer vc-dir-process-buffer
2045 (cd def-dir)
2046 (erase-buffer)
2047 (vc-call-backend
2048 backend 'dir-status def-dir
2049 (lambda (entries &optional more-to-come)
2050 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
2051 ;; If MORE-TO-COME is true, then more updates will come from
2052 ;; the asynchronous process.
2053 (with-current-buffer buffer
2054 (vc-dir-update entries buffer)
2055 (unless more-to-come
2056 (let ((remaining
2057 (ewoc-collect
2058 vc-ewoc 'vc-dir-fileinfo->needs-update)))
2059 (if remaining
2060 (vc-dir-refresh-files
2061 (mapcar 'vc-dir-fileinfo->name remaining)
2062 'up-to-date)
2063 (setq mode-line-process nil))))))))))))
2065 (defun vc-dir-show-fileentry (file)
2066 "Insert an entry for a specific file into the current VC status listing.
2067 This is typically used if the file is up-to-date (or has been added
2068 outside of VC) and one wants to do some operation on it."
2069 (interactive "fShow file: ")
2070 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
2072 (defun vc-dir-hide-up-to-date ()
2073 "Hide up-to-date items from display."
2074 (interactive)
2075 (ewoc-filter
2076 vc-ewoc
2077 (lambda (crt) (not (eq (vc-dir-fileinfo->state crt) 'up-to-date)))))
2079 (defun vc-dir-register ()
2080 "Register the marked files, or the current file if no marks."
2081 (interactive)
2082 ;; FIXME: Just pass the fileset to vc-register.
2083 (mapc (lambda (arg) (vc-register nil arg))
2084 (or (vc-dir-marked-files) (list (vc-dir-current-file)))))
2086 (defun vc-default-status-fileinfo-extra (backend file)
2087 "Default absence of extra information returned for a file."
2088 nil)
2090 ;; FIXME: Replace these with a more efficient dispatch
2092 (defun vc-generic-status-printer (fileentry)
2093 (let* ((file (vc-dir-fileinfo->name fileentry))
2094 (backend (vc-responsible-backend file)))
2095 (vc-call-backend backend 'status-printer fileentry)))
2097 (defun vc-generic-state (file)
2098 (let ((backend (vc-responsible-backend file)))
2099 (vc-call-backend backend 'state file)))
2101 (defun vc-generic-status-fileinfo-extra (file)
2102 (let ((backend (vc-responsible-backend file)))
2103 (vc-call-backend backend 'status-fileinfo-extra file)))
2105 (defun vc-generic-dir-headers (dir)
2106 (let ((backend (vc-responsible-backend dir)))
2107 (vc-dir-headers backend dir)))
2109 (defun vc-make-backend-object (file-or-dir)
2110 "Create the backend capability object needed by vc-dispatcher."
2111 (vc-create-client-object
2112 "VC status"
2113 (let ((backend (vc-responsible-backend file-or-dir)))
2114 (vc-dir-headers backend file-or-dir))
2115 #'vc-generic-status-printer
2116 #'vc-generic-state
2117 #'vc-generic-status-fileinfo-extra
2118 #'vc-dir-refresh))
2120 ;;;###autoload
2121 (defun vc-dir (dir)
2122 "Show the VC status for DIR."
2123 (interactive "DVC status for directory: ")
2124 (pop-to-buffer (vc-dir-prepare-status-buffer dir))
2125 (if (and (eq major-mode 'vc-dir-mode) (boundp 'client-object))
2126 (vc-dir-refresh)
2127 ;; Otherwise, initialize a new view using the dispatcher layer
2128 (progn
2129 ;; Build a capability object and hand it to the dispatcher initializer
2130 (vc-dir-mode (vc-make-backend-object dir))
2131 ;; Add VC-specific keybindings
2132 (let ((map (current-local-map)))
2133 (define-key map "=" 'vc-diff) ;; C-x v =
2134 (define-key map "a" 'vc-dir-register)
2135 (define-key map "+" 'vc-update) ;; C-x v +
2136 (define-key map "R" 'vc-revert) ;; u is taken by dispatcher unmark.
2137 (define-key map "A" 'vc-annotate) ;; g is taken by dispatcher referesh
2138 (define-key map "l" 'vc-print-log) ;; C-x v l
2139 (define-key map "x" 'vc-dir-hide-up-to-date)
2140 ))))
2142 ;; Named-configuration entry points
2144 (defun vc-snapshot-precondition (dir)
2145 "Scan the tree below DIR, looking for files not up-to-date.
2146 If any file is not up-to-date, return the name of the first such file.
2147 \(This means, neither snapshot creation nor retrieval is allowed.\)
2148 If one or more of the files are currently visited, return `visited'.
2149 Otherwise, return nil."
2150 (let ((status nil))
2151 (catch 'vc-locked-example
2152 (vc-file-tree-walk
2154 (lambda (f)
2155 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2156 (when (get-file-buffer f) (setq status 'visited)))))
2157 status)))
2159 ;;;###autoload
2160 (defun vc-create-snapshot (dir name branchp)
2161 "Descending recursively from DIR, make a snapshot called NAME.
2162 For each registered file, the working revision becomes part of
2163 the named configuration. If the prefix argument BRANCHP is
2164 given, the snapshot is made as a new branch and the files are
2165 checked out in that new branch."
2166 (interactive
2167 (list (read-file-name "Directory: " default-directory default-directory t)
2168 (read-string "New snapshot name: ")
2169 current-prefix-arg))
2170 (message "Making %s... " (if branchp "branch" "snapshot"))
2171 (when (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2172 (vc-call-backend (vc-responsible-backend dir)
2173 'create-snapshot dir name branchp)
2174 (message "Making %s... done" (if branchp "branch" "snapshot")))
2176 ;;;###autoload
2177 (defun vc-retrieve-snapshot (dir name)
2178 "Descending recursively from DIR, retrieve the snapshot called NAME.
2179 If NAME is empty, it refers to the latest revisions.
2180 If locking is used for the files in DIR, then there must not be any
2181 locked files at or below DIR (but if NAME is empty, locked files are
2182 allowed and simply skipped)."
2183 (interactive
2184 (list (read-file-name "Directory: " default-directory default-directory t)
2185 (read-string "Snapshot name to retrieve (default latest revisions): ")))
2186 (let ((update (yes-or-no-p "Update any affected buffers? "))
2187 (msg (if (or (not name) (string= name ""))
2188 (format "Updating %s... " (abbreviate-file-name dir))
2189 (format "Retrieving snapshot into %s... "
2190 (abbreviate-file-name dir)))))
2191 (message "%s" msg)
2192 (vc-call-backend (vc-responsible-backend dir)
2193 'retrieve-snapshot dir name update)
2194 (message "%s" (concat msg "done"))))
2196 ;; Miscellaneous other entry points
2198 ;;;###autoload
2199 (defun vc-print-log (&optional working-revision)
2200 "List the change log of the current fileset in a window.
2201 If WORKING-REVISION is non-nil, leave the point at that revision."
2202 (interactive)
2203 (let* ((vc-fileset (vc-deduce-fileset))
2204 (files (cdr vc-fileset))
2205 (backend (car vc-fileset))
2206 (working-revision (or working-revision (vc-working-revision (car files)))))
2207 ;; Don't switch to the output buffer before running the command,
2208 ;; so that any buffer-local settings in the vc-controlled
2209 ;; buffer can be accessed by the command.
2210 (vc-call-backend backend 'print-log files "*vc-change-log*")
2211 (pop-to-buffer "*vc-change-log*")
2212 (vc-exec-after
2213 `(let ((inhibit-read-only t))
2214 (vc-call-backend ',backend 'log-view-mode)
2215 (goto-char (point-max)) (forward-line -1)
2216 (while (looking-at "=*\n")
2217 (delete-char (- (match-end 0) (match-beginning 0)))
2218 (forward-line -1))
2219 (goto-char (point-min))
2220 (when (looking-at "[\b\t\n\v\f\r ]+")
2221 (delete-char (- (match-end 0) (match-beginning 0))))
2222 (shrink-window-if-larger-than-buffer)
2223 ;; move point to the log entry for the working revision
2224 (vc-call-backend ',backend 'show-log-entry ',working-revision)
2225 (setq vc-sentinel-movepoint (point))
2226 (set-buffer-modified-p nil)))))
2228 ;;;###autoload
2229 (defun vc-revert ()
2230 "Revert working copies of the selected fileset to their repository contents.
2231 This asks for confirmation if the buffer contents are not identical
2232 to the working revision (except for keyword expansion)."
2233 (interactive)
2234 (let* ((vc-fileset (vc-deduce-fileset))
2235 (files (cdr vc-fileset)))
2236 ;; If any of the files is visited by the current buffer, make
2237 ;; sure buffer is saved. If the user says `no', abort since
2238 ;; we cannot show the changes and ask for confirmation to
2239 ;; discard them.
2240 (when (or (not files) (memq (buffer-file-name) files))
2241 (vc-buffer-sync nil))
2242 (dolist (file files)
2243 (let ((buf (get-file-buffer file)))
2244 (when (and buf (buffer-modified-p buf))
2245 (error "Please kill or save all modified buffers before reverting.")))
2246 (when (vc-up-to-date-p file)
2247 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2248 (error "Revert canceled"))))
2249 (when (vc-diff-internal vc-allow-async-revert vc-fileset nil nil)
2250 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
2251 (error "Revert canceled"))
2252 (delete-windows-on "*vc-diff*")
2253 (kill-buffer "*vc-diff*"))
2254 (dolist (file files)
2255 (message "Reverting %s..." (vc-delistify files))
2256 (vc-revert-file file)
2257 (message "Reverting %s...done" (vc-delistify files)))))
2259 ;;;###autoload
2260 (defun vc-rollback ()
2261 "Roll back (remove) the most recent changeset committed to the repository.
2262 This may be either a file-level or a repository-level operation,
2263 depending on the underlying version-control system."
2264 (interactive)
2265 (let* ((vc-fileset (vc-deduce-fileset))
2266 (files (cdr vc-fileset))
2267 (backend (car vc-fileset))
2268 (granularity (vc-call-backend backend 'revision-granularity)))
2269 (unless (vc-find-backend-function backend 'rollback)
2270 (error "Rollback is not supported in %s" backend))
2271 (when (and (not (eq granularity 'repository)) (/= (length files) 1))
2272 (error "Rollback requires a singleton fileset or repository versioning"))
2273 (when (not (vc-call latest-on-branch-p (car files)))
2274 (error "Rollback is only possible at the tip revision."))
2275 ;; If any of the files is visited by the current buffer, make
2276 ;; sure buffer is saved. If the user says `no', abort since
2277 ;; we cannot show the changes and ask for confirmation to
2278 ;; discard them.
2279 (when (or (not files) (memq (buffer-file-name) files))
2280 (vc-buffer-sync nil))
2281 (dolist (file files)
2282 (when (buffer-modified-p (get-file-buffer file))
2283 (error "Please kill or save all modified buffers before rollback."))
2284 (when (not (vc-up-to-date-p file))
2285 (error "Please revert all modified workfiles before rollback.")))
2286 ;; Accumulate changes associated with the fileset
2287 (vc-setup-buffer "*vc-diff*")
2288 (not-modified)
2289 (message "Finding changes...")
2290 (let* ((tip (vc-working-revision (car files)))
2291 (previous (vc-call previous-revision (car files) tip)))
2292 (vc-diff-internal nil vc-fileset previous tip))
2293 ;; Display changes
2294 (unless (yes-or-no-p "Discard these revisions? ")
2295 (error "Rollback canceled"))
2296 (delete-windows-on "*vc-diff*")
2297 (kill-buffer"*vc-diff*")
2298 ;; Do the actual reversions
2299 (message "Rolling back %s..." (vc-delistify files))
2300 (with-vc-properties
2301 files
2302 (vc-call-backend backend 'rollback files)
2303 `((vc-state . ,'up-to-date)
2304 (vc-checkout-time . , (nth 5 (file-attributes file)))
2305 (vc-working-revision . nil)))
2306 (dolist (f files) (vc-resynch-buffer f t t))
2307 (message "Rolling back %s...done" (vc-delistify files))))
2309 ;;;###autoload
2310 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2312 ;;;###autoload
2313 (defun vc-update ()
2314 "Update the current fileset's files to their tip revisions.
2315 For each one that contains no changes, and is not locked, then this simply
2316 replaces the work file with the latest revision on its branch. If the file
2317 contains changes, and the backend supports merging news, then any recent
2318 changes from the current branch are merged into the working file."
2319 (interactive)
2320 (let* ((vc-fileset (vc-deduce-fileset))
2321 (files (cdr vc-fileset))
2322 (backend (car vc-fileset)))
2323 (dolist (file files)
2324 (when (let ((buf (get-file-buffer file)))
2325 (and buf (buffer-modified-p buf)))
2326 (error "Please kill or save all modified buffers before updating."))
2327 (if (vc-up-to-date-p file)
2328 (vc-checkout file nil t)
2329 (if (eq (vc-checkout-model backend (list file)) 'locking)
2330 (if (eq (vc-state file) 'edited)
2331 (error "%s"
2332 (substitute-command-keys
2333 "File is locked--type \\[vc-revert] to discard changes"))
2334 (error "Unexpected file state (%s) -- type %s"
2335 (vc-state file)
2336 (substitute-command-keys
2337 "\\[vc-next-action] to correct")))
2338 (if (not (vc-find-backend-function backend 'merge-news))
2339 (error "Sorry, merging news is not implemented for %s"
2340 backend)
2341 (vc-maybe-resolve-conflicts file (vc-call merge-news file))))))))
2343 (defun vc-version-backup-file (file &optional rev)
2344 "Return name of backup file for revision REV of FILE.
2345 If version backups should be used for FILE, and there exists
2346 such a backup for REV or the working revision of file, return
2347 its name; otherwise return nil."
2348 (when (vc-call make-version-backups-p file)
2349 (let ((backup-file (vc-version-backup-file-name file rev)))
2350 (if (file-exists-p backup-file)
2351 backup-file
2352 ;; there is no automatic backup, but maybe the user made one manually
2353 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2354 (when (file-exists-p backup-file)
2355 backup-file)))))
2357 (defun vc-revert-file (file)
2358 "Revert FILE back to the repository working revision it was based on."
2359 (with-vc-properties
2360 (list file)
2361 (let ((backup-file (vc-version-backup-file file)))
2362 (when backup-file
2363 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2364 (vc-delete-automatic-version-backups file))
2365 (vc-call revert file backup-file))
2366 `((vc-state . up-to-date)
2367 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2368 (vc-resynch-buffer file t t))
2370 ;;;###autoload
2371 (defun vc-switch-backend (file backend)
2372 "Make BACKEND the current version control system for FILE.
2373 FILE must already be registered in BACKEND. The change is not
2374 permanent, only for the current session. This function only changes
2375 VC's perspective on FILE, it does not register or unregister it.
2376 By default, this command cycles through the registered backends.
2377 To get a prompt, use a prefix argument."
2378 (interactive
2379 (list
2380 (or buffer-file-name
2381 (error "There is no version-controlled file in this buffer"))
2382 (let ((backend (vc-backend buffer-file-name))
2383 (backends nil))
2384 (unless backend
2385 (error "File %s is not under version control" buffer-file-name))
2386 ;; Find the registered backends.
2387 (dolist (backend vc-handled-backends)
2388 (when (vc-call-backend backend 'registered buffer-file-name)
2389 (push backend backends)))
2390 ;; Find the next backend.
2391 (let ((def (car (delq backend (append (memq backend backends) backends))))
2392 (others (delete backend backends)))
2393 (cond
2394 ((null others) (error "No other backend to switch to"))
2395 (current-prefix-arg
2396 (intern
2397 (upcase
2398 (completing-read
2399 (format "Switch to backend [%s]: " def)
2400 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2401 nil t nil nil (downcase (symbol-name def))))))
2402 (t def))))))
2403 (unless (eq backend (vc-backend file))
2404 (vc-file-clearprops file)
2405 (vc-file-setprop file 'vc-backend backend)
2406 ;; Force recomputation of the state
2407 (unless (vc-call-backend backend 'registered file)
2408 (vc-file-clearprops file)
2409 (error "%s is not registered in %s" file backend))
2410 (vc-mode-line file)))
2412 ;;;###autoload
2413 (defun vc-transfer-file (file new-backend)
2414 "Transfer FILE to another version control system NEW-BACKEND.
2415 If NEW-BACKEND has a higher precedence than FILE's current backend
2416 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2417 NEW-BACKEND, using the revision number from the current backend as the
2418 base level. If NEW-BACKEND has a lower precedence than the current
2419 backend, then commit all changes that were made under the current
2420 backend to NEW-BACKEND, and unregister FILE from the current backend.
2421 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2422 (let* ((old-backend (vc-backend file))
2423 (edited (memq (vc-state file) '(edited needs-merge)))
2424 (registered (vc-call-backend new-backend 'registered file))
2425 (move
2426 (and registered ; Never move if not registered in new-backend yet.
2427 ;; move if new-backend comes later in vc-handled-backends
2428 (or (memq new-backend (memq old-backend vc-handled-backends))
2429 (y-or-n-p "Final transfer? "))))
2430 (comment nil))
2431 (when (eq old-backend new-backend)
2432 (error "%s is the current backend of %s" new-backend file))
2433 (if registered
2434 (set-file-modes file (logior (file-modes file) 128))
2435 ;; `registered' might have switched under us.
2436 (vc-switch-backend file old-backend)
2437 (let* ((rev (vc-working-revision file))
2438 (modified-file (and edited (make-temp-file file)))
2439 (unmodified-file (and modified-file (vc-version-backup-file file))))
2440 ;; Go back to the base unmodified file.
2441 (unwind-protect
2442 (progn
2443 (when modified-file
2444 (copy-file file modified-file 'ok-if-already-exists)
2445 ;; If we have a local copy of the unmodified file, handle that
2446 ;; here and not in vc-revert-file because we don't want to
2447 ;; delete that copy -- it is still useful for OLD-BACKEND.
2448 (if unmodified-file
2449 (copy-file unmodified-file file
2450 'ok-if-already-exists 'keep-date)
2451 (when (y-or-n-p "Get base revision from master? ")
2452 (vc-revert-file file))))
2453 (vc-call-backend new-backend 'receive-file file rev))
2454 (when modified-file
2455 (vc-switch-backend file new-backend)
2456 (unless (eq (vc-checkout-model new-backend (list file)) 'implicit)
2457 (vc-checkout file t nil))
2458 (rename-file modified-file file 'ok-if-already-exists)
2459 (vc-file-setprop file 'vc-checkout-time nil)))))
2460 (when move
2461 (vc-switch-backend file old-backend)
2462 (setq comment (vc-call comment-history file))
2463 (vc-call unregister file))
2464 (vc-switch-backend file new-backend)
2465 (when (or move edited)
2466 (vc-file-setprop file 'vc-state 'edited)
2467 (vc-mode-line file)
2468 (vc-checkin file nil comment (stringp comment)))))
2470 (defun vc-rename-master (oldmaster newfile templates)
2471 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2472 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2473 (newdir (or (file-name-directory newfile) ""))
2474 (newbase (file-name-nondirectory newfile))
2475 (masters
2476 ;; List of potential master files for `newfile'
2477 (mapcar
2478 (lambda (s) (vc-possible-master s newdir newbase))
2479 templates)))
2480 (when (or (file-symlink-p oldmaster)
2481 (file-symlink-p (file-name-directory oldmaster)))
2482 (error "This is unsafe in the presence of symbolic links"))
2483 (rename-file
2484 oldmaster
2485 (catch 'found
2486 ;; If possible, keep the master file in the same directory.
2487 (dolist (f masters)
2488 (when (and f (string= (file-name-directory (expand-file-name f)) dir))
2489 (throw 'found f)))
2490 ;; If not, just use the first possible place.
2491 (dolist (f masters)
2492 (and f (or (not (setq dir (file-name-directory f)))
2493 (file-directory-p dir))
2494 (throw 'found f)))
2495 (error "New file lacks a version control directory")))))
2497 (defun vc-delete-file (file)
2498 "Delete file and mark it as such in the version control system."
2499 (interactive "fVC delete file: ")
2500 (setq file (expand-file-name file))
2501 (let ((buf (get-file-buffer file))
2502 (backend (vc-backend file)))
2503 (unless backend
2504 (error "File %s is not under version control"
2505 (file-name-nondirectory file)))
2506 (unless (vc-find-backend-function backend 'delete-file)
2507 (error "Deleting files under %s is not supported in VC" backend))
2508 (when (and buf (buffer-modified-p buf))
2509 (error "Please save or undo your changes before deleting %s" file))
2510 (let ((state (vc-state file)))
2511 (when (eq state 'edited)
2512 (error "Please commit or undo your changes before deleting %s" file))
2513 (when (eq state 'conflict)
2514 (error "Please resolve the conflicts before deleting %s" file)))
2515 (unless (y-or-n-p (format "Really want to delete %s? "
2516 (file-name-nondirectory file)))
2517 (error "Abort!"))
2518 (unless (or (file-directory-p file) (null make-backup-files)
2519 (not (file-exists-p file)))
2520 (with-current-buffer (or buf (find-file-noselect file))
2521 (let ((backup-inhibited nil))
2522 (backup-buffer))
2523 ;; If we didn't have a buffer visiting the file before this
2524 ;; command, kill the buffer created by the above
2525 ;; `find-file-noselect' call.
2526 (unless buf (kill-buffer (current-buffer)))))
2527 (vc-call delete-file file)
2528 ;; If the backend hasn't deleted the file itself, let's do it for him.
2529 (when (file-exists-p file) (delete-file file))
2530 ;; Forget what VC knew about the file.
2531 (vc-file-clearprops file)
2532 (vc-resynch-buffer file buf t)))
2534 ;;;###autoload
2535 (defun vc-rename-file (old new)
2536 "Rename file OLD to NEW, and rename its master file likewise."
2537 (interactive "fVC rename file: \nFRename to: ")
2538 (let ((oldbuf (get-file-buffer old)))
2539 (when (and oldbuf (buffer-modified-p oldbuf))
2540 (error "Please save files before moving them"))
2541 (when (get-file-buffer new)
2542 (error "Already editing new file name"))
2543 (when (file-exists-p new)
2544 (error "New file already exists"))
2545 (let ((state (vc-state old)))
2546 (unless (memq state '(up-to-date edited))
2547 (error "Please %s files before moving them"
2548 (if (stringp state) "check in" "update"))))
2549 (vc-call rename-file old new)
2550 (vc-file-clearprops old)
2551 ;; Move the actual file (unless the backend did it already)
2552 (when (file-exists-p old) (rename-file old new))
2553 ;; ?? Renaming a file might change its contents due to keyword expansion.
2554 ;; We should really check out a new copy if the old copy was precisely equal
2555 ;; to some checked-in revision. However, testing for this is tricky....
2556 (when oldbuf
2557 (with-current-buffer oldbuf
2558 (let ((buffer-read-only buffer-read-only))
2559 (set-visited-file-name new))
2560 (vc-backend new)
2561 (vc-mode-line new)
2562 (set-buffer-modified-p nil)))))
2564 ;;;###autoload
2565 (defun vc-update-change-log (&rest args)
2566 "Find change log file and add entries from recent version control logs.
2567 Normally, find log entries for all registered files in the default
2568 directory.
2570 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2572 With any numeric prefix arg, find log entries for all currently visited
2573 files that are under version control. This puts all the entries in the
2574 log for the default directory, which may not be appropriate.
2576 From a program, any ARGS are assumed to be filenames for which
2577 log entries should be gathered."
2578 (interactive
2579 (cond ((consp current-prefix-arg) ;C-u
2580 (list buffer-file-name))
2581 (current-prefix-arg ;Numeric argument.
2582 (let ((files nil)
2583 (buffers (buffer-list))
2584 file)
2585 (while buffers
2586 (setq file (buffer-file-name (car buffers)))
2587 (and file (vc-backend file)
2588 (setq files (cons file files)))
2589 (setq buffers (cdr buffers)))
2590 files))
2592 ;; Don't supply any filenames to backend; this means
2593 ;; it should find all relevant files relative to
2594 ;; the default-directory.
2595 nil)))
2596 (vc-call-backend (vc-responsible-backend default-directory)
2597 'update-changelog args))
2599 ;;; The default back end. Assumes RCS-like revision numbering.
2601 (defun vc-default-revision-granularity ()
2602 (error "Your backend will not work with this version of VC mode."))
2604 ;; functions that operate on RCS revision numbers. This code should
2605 ;; also be moved into the backends. It stays for now, however, since
2606 ;; it is used in code below.
2607 ;;;###autoload
2608 (defun vc-trunk-p (rev)
2609 "Return t if REV is a revision on the trunk."
2610 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
2612 (defun vc-branch-p (rev)
2613 "Return t if REV is a branch revision."
2614 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
2616 ;;;###autoload
2617 (defun vc-branch-part (rev)
2618 "Return the branch part of a revision number REV."
2619 (let ((index (string-match "\\.[0-9]+\\'" rev)))
2620 (when index
2621 (substring rev 0 index))))
2623 (defun vc-minor-part (rev)
2624 "Return the minor revision number of a revision number REV."
2625 (string-match "[0-9]+\\'" rev)
2626 (substring rev (match-beginning 0) (match-end 0)))
2628 (defun vc-default-previous-revision (backend file rev)
2629 "Return the revision number immediately preceding REV for FILE,
2630 or nil if there is no previous revision. This default
2631 implementation works for MAJOR.MINOR-style revision numbers as
2632 used by RCS and CVS."
2633 (let ((branch (vc-branch-part rev))
2634 (minor-num (string-to-number (vc-minor-part rev))))
2635 (when branch
2636 (if (> minor-num 1)
2637 ;; revision does probably not start a branch or release
2638 (concat branch "." (number-to-string (1- minor-num)))
2639 (if (vc-trunk-p rev)
2640 ;; we are at the beginning of the trunk --
2641 ;; don't know anything to return here
2643 ;; we are at the beginning of a branch --
2644 ;; return revision of starting point
2645 (vc-branch-part branch))))))
2647 (defun vc-default-next-revision (backend file rev)
2648 "Return the revision number immediately following REV for FILE,
2649 or nil if there is no next revision. This default implementation
2650 works for MAJOR.MINOR-style revision numbers as used by RCS
2651 and CVS."
2652 (when (not (string= rev (vc-working-revision file)))
2653 (let ((branch (vc-branch-part rev))
2654 (minor-num (string-to-number (vc-minor-part rev))))
2655 (concat branch "." (number-to-string (1+ minor-num))))))
2657 (defun vc-default-responsible-p (backend file)
2658 "Indicate whether BACKEND is reponsible for FILE.
2659 The default is to return nil always."
2660 nil)
2662 (defun vc-default-could-register (backend file)
2663 "Return non-nil if BACKEND could be used to register FILE.
2664 The default implementation returns t for all files."
2667 (defun vc-default-latest-on-branch-p (backend file)
2668 "Return non-nil if FILE is the latest on its branch.
2669 This default implementation always returns non-nil, which means that
2670 editing non-current revisions is not supported by default."
2673 (defun vc-default-init-revision (backend) vc-default-init-revision)
2675 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
2676 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
2677 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
2678 ;; vc-cvs.el by code using cvs2cl.
2679 (defun vc-update-changelog-rcs2log (files)
2680 "Default implementation of update-changelog.
2681 Uses `rcs2log' which only works for RCS and CVS."
2682 ;; FIXME: We (c|sh)ould add support for cvs2cl
2683 (let ((odefault default-directory)
2684 (changelog (find-change-log))
2685 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2686 (tempfile (make-temp-file
2687 (expand-file-name "vc"
2688 (or small-temporary-file-directory
2689 temporary-file-directory))))
2690 (login-name (or user-login-name
2691 (format "uid%d" (number-to-string (user-uid)))))
2692 (full-name (or add-log-full-name
2693 (user-full-name)
2694 (user-login-name)
2695 (format "uid%d" (number-to-string (user-uid)))))
2696 (mailing-address (or add-log-mailing-address
2697 user-mail-address)))
2698 (find-file-other-window changelog)
2699 (barf-if-buffer-read-only)
2700 (vc-buffer-sync)
2701 (undo-boundary)
2702 (goto-char (point-min))
2703 (push-mark)
2704 (message "Computing change log entries...")
2705 (message "Computing change log entries... %s"
2706 (unwind-protect
2707 (progn
2708 (setq default-directory odefault)
2709 (if (eq 0 (apply 'call-process
2710 (expand-file-name "rcs2log"
2711 exec-directory)
2712 nil (list t tempfile) nil
2713 "-c" changelog
2714 "-u" (concat login-name
2715 "\t" full-name
2716 "\t" mailing-address)
2717 (mapcar
2718 (lambda (f)
2719 (file-relative-name
2720 (expand-file-name f odefault)))
2721 files)))
2722 "done"
2723 (pop-to-buffer (get-buffer-create "*vc*"))
2724 (erase-buffer)
2725 (insert-file-contents tempfile)
2726 "failed"))
2727 (setq default-directory (file-name-directory changelog))
2728 (delete-file tempfile)))))
2730 (defun vc-default-find-revision (backend file rev buffer)
2731 "Provide the new `find-revision' op based on the old `checkout' op.
2732 This is only for compatibility with old backends. They should be updated
2733 to provide the `find-revision' operation instead."
2734 (let ((tmpfile (make-temp-file (expand-file-name file))))
2735 (unwind-protect
2736 (progn
2737 (vc-call-backend backend 'checkout file nil rev tmpfile)
2738 (with-current-buffer buffer
2739 (insert-file-contents-literally tmpfile)))
2740 (delete-file tmpfile))))
2742 (defun vc-default-prettify-state-info (backend file)
2743 (let* ((state (vc-state file))
2744 (statestring
2745 (cond
2746 ((stringp state) (concat "(" state ")"))
2747 ((eq state 'edited) "(modified)")
2748 ((eq state 'needs-merge) "(merge)")
2749 ((eq state 'needs-update) "(update)")
2750 ((eq state 'added) "(added)")
2751 ((eq state 'removed) "(removed)")
2752 ((eq state 'ignored) "(ignored)") ;; dired-hook filters this out
2753 ((eq state 'unregistered) "?")
2754 ((eq state 'unlocked-changes) "(stale)")
2755 ((not state) "(unknown)")))
2756 (buffer
2757 (get-file-buffer file))
2758 (modflag
2759 (if (and buffer (buffer-modified-p buffer)) "+" "")))
2760 (concat statestring modflag)))
2762 (defun vc-default-rename-file (backend old new)
2763 (condition-case nil
2764 (add-name-to-file old new)
2765 (error (rename-file old new)))
2766 (vc-delete-file old)
2767 (with-current-buffer (find-file-noselect new)
2768 (vc-register)))
2770 (defalias 'vc-default-check-headers 'ignore)
2772 (defun vc-default-log-view-mode (backend) (log-view-mode))
2774 (defun vc-default-show-log-entry (backend rev)
2775 (with-no-warnings
2776 (log-view-goto-rev rev)))
2778 (defun vc-default-comment-history (backend file)
2779 "Return a string with all log entries stored in BACKEND for FILE."
2780 (when (vc-find-backend-function backend 'print-log)
2781 (with-current-buffer "*vc*"
2782 (vc-call print-log (list file))
2783 (vc-call-backend backend 'wash-log)
2784 (buffer-string))))
2786 (defun vc-default-receive-file (backend file rev)
2787 "Let BACKEND receive FILE from another version control system."
2788 (vc-call-backend backend 'register file rev ""))
2790 (defun vc-default-create-snapshot (backend dir name branchp)
2791 (when branchp
2792 (error "VC backend %s does not support module branches" backend))
2793 (let ((result (vc-snapshot-precondition dir)))
2794 (if (stringp result)
2795 (error "File %s is not up-to-date" result)
2796 (vc-file-tree-walk
2798 (lambda (f)
2799 (vc-call assign-name f name))))))
2801 (defun vc-default-retrieve-snapshot (backend dir name update)
2802 (if (string= name "")
2803 (progn
2804 (vc-file-tree-walk
2806 (lambda (f) (and
2807 (vc-up-to-date-p f)
2808 (vc-error-occurred
2809 (vc-call checkout f nil "")
2810 (when update (vc-resynch-buffer f t t)))))))
2811 (let ((result (vc-snapshot-precondition dir)))
2812 (if (stringp result)
2813 (error "File %s is locked" result)
2814 (setq update (and (eq result 'visited) update))
2815 (vc-file-tree-walk
2817 (lambda (f) (vc-error-occurred
2818 (vc-call checkout f nil name)
2819 (when update (vc-resynch-buffer f t t)))))))))
2821 (defun vc-default-revert (backend file contents-done)
2822 (unless contents-done
2823 (let ((rev (vc-working-revision file))
2824 (file-buffer (or (get-file-buffer file) (current-buffer))))
2825 (message "Checking out %s..." file)
2826 (let ((failed t)
2827 (backup-name (car (find-backup-file-name file))))
2828 (when backup-name
2829 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
2830 (unless (file-writable-p file)
2831 (set-file-modes file (logior (file-modes file) 128))))
2832 (unwind-protect
2833 (let ((coding-system-for-read 'no-conversion)
2834 (coding-system-for-write 'no-conversion))
2835 (with-temp-file file
2836 (let ((outbuf (current-buffer)))
2837 ;; Change buffer to get local value of vc-checkout-switches.
2838 (with-current-buffer file-buffer
2839 (let ((default-directory (file-name-directory file)))
2840 (vc-call find-revision file rev outbuf)))))
2841 (setq failed nil))
2842 (when backup-name
2843 (if failed
2844 (rename-file backup-name file 'ok-if-already-exists)
2845 (and (not vc-make-backup-files) (delete-file backup-name))))))
2846 (message "Checking out %s...done" file))))
2848 (defalias 'vc-default-revision-completion-table 'ignore)
2850 (defun vc-default-dir-status-files (backend dir files default-state update-function)
2851 (funcall update-function
2852 (mapcar (lambda (file) (list file default-state)) files)))
2854 (defun vc-check-headers ()
2855 "Check if the current file has any headers in it."
2856 (interactive)
2857 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
2859 ;;; Annotate functionality
2861 ;; Declare globally instead of additional parameter to
2862 ;; temp-buffer-show-function (not possible to pass more than one
2863 ;; parameter). The use of annotate-ratio is deprecated in favor of
2864 ;; annotate-mode, which replaces it with the more sensible "span-to
2865 ;; days", along with autoscaling support.
2866 (defvar vc-annotate-ratio nil "Global variable.")
2868 ;; internal buffer-local variables
2869 (defvar vc-annotate-backend nil)
2870 (defvar vc-annotate-parent-file nil)
2871 (defvar vc-annotate-parent-rev nil)
2872 (defvar vc-annotate-parent-display-mode nil)
2874 (defconst vc-annotate-font-lock-keywords
2875 ;; The fontification is done by vc-annotate-lines instead of font-lock.
2876 '((vc-annotate-lines)))
2878 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2879 "Major mode for output buffers of the `vc-annotate' command.
2881 You can use the mode-specific menu to alter the time-span of the used
2882 colors. See variable `vc-annotate-menu-elements' for customizing the
2883 menu items."
2884 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
2885 ;; it will become a list, to avoid initial annotations being invisible.
2886 (add-to-invisibility-spec 'foo)
2887 (remove-from-invisibility-spec 'foo)
2888 (set (make-local-variable 'truncate-lines) t)
2889 (set (make-local-variable 'font-lock-defaults)
2890 '(vc-annotate-font-lock-keywords t))
2891 (view-mode 1))
2893 (defun vc-annotate-toggle-annotation-visibility ()
2894 "Toggle whether or not the annotation is visible."
2895 (interactive)
2896 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
2897 'remove-from-invisibility-spec
2898 'add-to-invisibility-spec)
2899 'vc-annotate-annotation)
2900 (force-window-update (current-buffer)))
2902 (defun vc-annotate-display-default (ratio)
2903 "Display the output of \\[vc-annotate] using the default color range.
2904 The color range is given by `vc-annotate-color-map', scaled by RATIO.
2905 The current time is used as the offset."
2906 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
2907 (message "Redisplaying annotation...")
2908 (vc-annotate-display ratio)
2909 (message "Redisplaying annotation...done"))
2911 (defun vc-annotate-oldest-in-map (color-map)
2912 "Return the oldest time in the COLOR-MAP."
2913 ;; Since entries should be sorted, we can just use the last one.
2914 (caar (last color-map)))
2916 (defun vc-annotate-get-time-set-line-props ()
2917 (let ((bol (point))
2918 (date (vc-call-backend vc-annotate-backend 'annotate-time))
2919 (inhibit-read-only t))
2920 (assert (>= (point) bol))
2921 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
2922 date))
2924 (defun vc-annotate-display-autoscale (&optional full)
2925 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
2926 Autoscaling means that the map is scaled from the current time to the
2927 oldest annotation in the buffer, or, with prefix argument FULL, to
2928 cover the range from the oldest annotation to the newest."
2929 (interactive "P")
2930 (let ((newest 0.0)
2931 (oldest 999999.) ;Any CVS users at the founding of Rome?
2932 (current (vc-annotate-convert-time (current-time)))
2933 date)
2934 (message "Redisplaying annotation...")
2935 ;; Run through this file and find the oldest and newest dates annotated.
2936 (save-excursion
2937 (goto-char (point-min))
2938 (while (not (eobp))
2939 (when (setq date (vc-annotate-get-time-set-line-props))
2940 (when (> date newest)
2941 (setq newest date))
2942 (when (< date oldest)
2943 (setq oldest date)))
2944 (forward-line 1)))
2945 (vc-annotate-display
2946 (/ (- (if full newest current) oldest)
2947 (vc-annotate-oldest-in-map vc-annotate-color-map))
2948 (if full newest))
2949 (message "Redisplaying annotation...done \(%s\)"
2950 (if full
2951 (format "Spanned from %.1f to %.1f days old"
2952 (- current oldest)
2953 (- current newest))
2954 (format "Spanned to %.1f days old" (- current oldest))))))
2956 ;; Menu -- Using easymenu.el
2957 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
2958 "VC Annotate Display Menu"
2959 `("VC-Annotate"
2960 ["By Color Map Range" (unless (null vc-annotate-display-mode)
2961 (setq vc-annotate-display-mode nil)
2962 (vc-annotate-display-select))
2963 :style toggle :selected (null vc-annotate-display-mode)]
2964 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
2965 (mapcar (lambda (element)
2966 (let ((days (* element oldest-in-map)))
2967 `[,(format "Span %.1f days" days)
2968 (vc-annotate-display-select nil ,days)
2969 :style toggle :selected
2970 (eql vc-annotate-display-mode ,days) ]))
2971 vc-annotate-menu-elements))
2972 ["Span ..."
2973 (vc-annotate-display-select
2974 nil (float (string-to-number (read-string "Span how many days? "))))]
2975 "--"
2976 ["Span to Oldest"
2977 (unless (eq vc-annotate-display-mode 'scale)
2978 (vc-annotate-display-select nil 'scale))
2979 :help
2980 "Use an autoscaled color map from the oldest annotation to the current time"
2981 :style toggle :selected
2982 (eq vc-annotate-display-mode 'scale)]
2983 ["Span Oldest->Newest"
2984 (unless (eq vc-annotate-display-mode 'fullscale)
2985 (vc-annotate-display-select nil 'fullscale))
2986 :help
2987 "Use an autoscaled color map from the oldest to the newest annotation"
2988 :style toggle :selected
2989 (eq vc-annotate-display-mode 'fullscale)]
2990 "--"
2991 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
2992 :help
2993 "Toggle whether the annotation is visible or not"]
2994 ["Annotate previous revision" vc-annotate-prev-revision
2995 :help "Visit the annotation of the revision previous to this one"]
2996 ["Annotate next revision" vc-annotate-next-revision
2997 :help "Visit the annotation of the revision after this one"]
2998 ["Annotate revision at line" vc-annotate-revision-at-line
2999 :help
3000 "Visit the annotation of the revision identified in the current line"]
3001 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
3002 :help "Visit the annotation of the revision before the revision at line"]
3003 ["Annotate latest revision" vc-annotate-working-revision
3004 :help "Visit the annotation of the working revision of this file"]
3005 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
3006 :help "Visit the log of the revision at line"]
3007 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
3008 :help
3009 "Visit the diff of the revision at line from its previous revision"]
3010 ["Visit revision at line" vc-annotate-find-revision-at-line
3011 :help "Visit the revision identified in the current line"]))
3013 (defun vc-annotate-display-select (&optional buffer mode)
3014 "Highlight the output of \\[vc-annotate].
3015 By default, the current buffer is highlighted, unless overridden by
3016 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
3017 use; you may override this using the second optional arg MODE."
3018 (interactive)
3019 (when mode (setq vc-annotate-display-mode mode))
3020 (pop-to-buffer (or buffer (current-buffer)))
3021 (cond ((null vc-annotate-display-mode)
3022 ;; The ratio is global, thus relative to the global color-map.
3023 (kill-local-variable 'vc-annotate-color-map)
3024 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
3025 ;; One of the auto-scaling modes
3026 ((eq vc-annotate-display-mode 'scale)
3027 (vc-exec-after `(vc-annotate-display-autoscale)))
3028 ((eq vc-annotate-display-mode 'fullscale)
3029 (vc-exec-after `(vc-annotate-display-autoscale t)))
3030 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3031 (vc-annotate-display-default
3032 (/ vc-annotate-display-mode
3033 (vc-annotate-oldest-in-map vc-annotate-color-map))))
3034 (t (error "No such display mode: %s"
3035 vc-annotate-display-mode))))
3037 ;;;###autoload
3038 (defun vc-annotate (file rev &optional display-mode buf move-point-to)
3039 "Display the edit history of the current file using colors.
3041 This command creates a buffer that shows, for each line of the current
3042 file, when it was last edited and by whom. Additionally, colors are
3043 used to show the age of each line--blue means oldest, red means
3044 youngest, and intermediate colors indicate intermediate ages. By
3045 default, the time scale stretches back one year into the past;
3046 everything that is older than that is shown in blue.
3048 With a prefix argument, this command asks two questions in the
3049 minibuffer. First, you may enter a revision number; then the buffer
3050 displays and annotates that revision instead of the working revision
3051 \(type RET in the minibuffer to leave that default unchanged). Then,
3052 you are prompted for the time span in days which the color range
3053 should cover. For example, a time span of 20 days means that changes
3054 over the past 20 days are shown in red to blue, according to their
3055 age, and everything that is older than that is shown in blue.
3057 If MOVE-POINT-TO is given, move the point to that line.
3059 Customization variables:
3061 `vc-annotate-menu-elements' customizes the menu elements of the
3062 mode-specific menu. `vc-annotate-color-map' and
3063 `vc-annotate-very-old-color' define the mapping of time to colors.
3064 `vc-annotate-background' specifies the background color."
3065 (interactive
3066 (save-current-buffer
3067 (vc-ensure-vc-buffer)
3068 (list buffer-file-name
3069 (let ((def (vc-working-revision buffer-file-name)))
3070 (if (null current-prefix-arg) def
3071 (read-string
3072 (format "Annotate from revision (default %s): " def)
3073 nil nil def)))
3074 (if (null current-prefix-arg)
3075 vc-annotate-display-mode
3076 (float (string-to-number
3077 (read-string "Annotate span days (default 20): "
3078 nil nil "20")))))))
3079 (vc-ensure-vc-buffer)
3080 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
3081 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
3082 (temp-buffer-show-function 'vc-annotate-display-select)
3083 ;; If BUF is specified, we presume the caller maintains current line,
3084 ;; so we don't need to do it here. This implementation may give
3085 ;; strange results occasionally in the case of REV != WORKFILE-REV.
3086 (current-line (or move-point-to (unless buf (line-number-at-pos)))))
3087 (message "Annotating...")
3088 ;; If BUF is specified it tells in which buffer we should put the
3089 ;; annotations. This is used when switching annotations to another
3090 ;; revision, so we should update the buffer's name.
3091 (when buf (with-current-buffer buf
3092 (rename-buffer temp-buffer-name t)
3093 ;; In case it had to be uniquified.
3094 (setq temp-buffer-name (buffer-name))))
3095 (with-output-to-temp-buffer temp-buffer-name
3096 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
3097 ;; we must setup the mode first, and then set our local
3098 ;; variables before the show-function is called at the exit of
3099 ;; with-output-to-temp-buffer
3100 (with-current-buffer temp-buffer-name
3101 (unless (equal major-mode 'vc-annotate-mode)
3102 (vc-annotate-mode))
3103 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
3104 (set (make-local-variable 'vc-annotate-parent-file) file)
3105 (set (make-local-variable 'vc-annotate-parent-rev) rev)
3106 (set (make-local-variable 'vc-annotate-parent-display-mode)
3107 display-mode)))
3109 (with-current-buffer temp-buffer-name
3110 (vc-exec-after
3111 `(progn
3112 ;; Ideally, we'd rather not move point if the user has already
3113 ;; moved it elsewhere, but really point here is not the position
3114 ;; of the user's cursor :-(
3115 (when ,current-line ;(and (bobp))
3116 (goto-line ,current-line)
3117 (setq vc-sentinel-movepoint (point)))
3118 (unless (active-minibuffer-window)
3119 (message "Annotating... done")))))))
3121 (defun vc-annotate-prev-revision (prefix)
3122 "Visit the annotation of the revision previous to this one.
3124 With a numeric prefix argument, annotate the revision that many
3125 revisions previous."
3126 (interactive "p")
3127 (vc-annotate-warp-revision (- 0 prefix)))
3129 (defun vc-annotate-next-revision (prefix)
3130 "Visit the annotation of the revision after this one.
3132 With a numeric prefix argument, annotate the revision that many
3133 revisions after."
3134 (interactive "p")
3135 (vc-annotate-warp-revision prefix))
3137 (defun vc-annotate-working-revision ()
3138 "Visit the annotation of the working revision of this file."
3139 (interactive)
3140 (if (not (equal major-mode 'vc-annotate-mode))
3141 (message "Cannot be invoked outside of a vc annotate buffer")
3142 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
3143 (if (equal warp-rev vc-annotate-parent-rev)
3144 (message "Already at revision %s" warp-rev)
3145 (vc-annotate-warp-revision warp-rev)))))
3147 (defun vc-annotate-extract-revision-at-line ()
3148 "Extract the revision number of the current line."
3149 ;; This function must be invoked from a buffer in vc-annotate-mode
3150 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
3152 (defun vc-annotate-revision-at-line ()
3153 "Visit the annotation of the revision identified in the current line."
3154 (interactive)
3155 (if (not (equal major-mode 'vc-annotate-mode))
3156 (message "Cannot be invoked outside of a vc annotate buffer")
3157 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3158 (if (not rev-at-line)
3159 (message "Cannot extract revision number from the current line")
3160 (if (equal rev-at-line vc-annotate-parent-rev)
3161 (message "Already at revision %s" rev-at-line)
3162 (vc-annotate-warp-revision rev-at-line))))))
3164 (defun vc-annotate-find-revision-at-line ()
3165 "Visit the revision identified in the current line."
3166 (interactive)
3167 (if (not (equal major-mode 'vc-annotate-mode))
3168 (message "Cannot be invoked outside of a vc annotate buffer")
3169 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3170 (if (not rev-at-line)
3171 (message "Cannot extract revision number from the current line")
3172 (vc-revision-other-window rev-at-line)))))
3174 (defun vc-annotate-revision-previous-to-line ()
3175 "Visit the annotation of the revision before the revision at line."
3176 (interactive)
3177 (if (not (equal major-mode 'vc-annotate-mode))
3178 (message "Cannot be invoked outside of a vc annotate buffer")
3179 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3180 (prev-rev nil))
3181 (if (not rev-at-line)
3182 (message "Cannot extract revision number from the current line")
3183 (setq prev-rev
3184 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3185 (vc-annotate-warp-revision prev-rev)))))
3187 (defun vc-annotate-show-log-revision-at-line ()
3188 "Visit the log of the revision at line."
3189 (interactive)
3190 (if (not (equal major-mode 'vc-annotate-mode))
3191 (message "Cannot be invoked outside of a vc annotate buffer")
3192 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3193 (if (not rev-at-line)
3194 (message "Cannot extract revision number from the current line")
3195 (vc-print-log rev-at-line)))))
3197 (defun vc-annotate-show-diff-revision-at-line ()
3198 "Visit the diff of the revision at line from its previous revision."
3199 (interactive)
3200 (if (not (equal major-mode 'vc-annotate-mode))
3201 (message "Cannot be invoked outside of a vc annotate buffer")
3202 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3203 (prev-rev nil))
3204 (if (not rev-at-line)
3205 (message "Cannot extract revision number from the current line")
3206 (setq prev-rev
3207 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3208 (if (not prev-rev)
3209 (message "Cannot diff from any revision prior to %s" rev-at-line)
3210 (save-window-excursion
3211 (vc-diff-internal
3213 (cons (vc-backend vc-annotate-parent-file)
3214 (list vc-annotate-parent-file))
3215 prev-rev rev-at-line))
3216 (switch-to-buffer "*vc-diff*"))))))
3218 (defun vc-annotate-warp-revision (revspec)
3219 "Annotate the revision described by REVSPEC.
3221 If REVSPEC is a positive integer, warp that many revisions
3222 forward, if possible, otherwise echo a warning message. If
3223 REVSPEC is a negative integer, warp that many revisions backward,
3224 if possible, otherwise echo a warning message. If REVSPEC is a
3225 string, then it describes a revision number, so warp to that
3226 revision."
3227 (if (not (equal major-mode 'vc-annotate-mode))
3228 (message "Cannot be invoked outside of a vc annotate buffer")
3229 (let* ((buf (current-buffer))
3230 (oldline (line-number-at-pos))
3231 (revspeccopy revspec)
3232 (newrev nil))
3233 (cond
3234 ((and (integerp revspec) (> revspec 0))
3235 (setq newrev vc-annotate-parent-rev)
3236 (while (and (> revspec 0) newrev)
3237 (setq newrev (vc-call next-revision
3238 vc-annotate-parent-file newrev))
3239 (setq revspec (1- revspec)))
3240 (unless newrev
3241 (message "Cannot increment %d revisions from revision %s"
3242 revspeccopy vc-annotate-parent-rev)))
3243 ((and (integerp revspec) (< revspec 0))
3244 (setq newrev vc-annotate-parent-rev)
3245 (while (and (< revspec 0) newrev)
3246 (setq newrev (vc-call previous-revision
3247 vc-annotate-parent-file newrev))
3248 (setq revspec (1+ revspec)))
3249 (unless newrev
3250 (message "Cannot decrement %d revisions from revision %s"
3251 (- 0 revspeccopy) vc-annotate-parent-rev)))
3252 ((stringp revspec) (setq newrev revspec))
3253 (t (error "Invalid argument to vc-annotate-warp-revision")))
3254 (when newrev
3255 (vc-annotate vc-annotate-parent-file newrev
3256 vc-annotate-parent-display-mode
3258 ;; Pass the current line so that vc-annotate will
3259 ;; place the point in the line.
3260 (min oldline (progn (goto-char (point-max))
3261 (forward-line -1)
3262 (line-number-at-pos))))))))
3264 (defun vc-annotate-compcar (threshold a-list)
3265 "Test successive cons cells of A-LIST against THRESHOLD.
3266 Return the first cons cell with a car that is not less than THRESHOLD,
3267 nil if no such cell exists."
3268 (let ((i 1)
3269 (tmp-cons (car a-list)))
3270 (while (and tmp-cons (< (car tmp-cons) threshold))
3271 (setq tmp-cons (car (nthcdr i a-list)))
3272 (setq i (+ i 1)))
3273 tmp-cons)) ; Return the appropriate value
3275 (defun vc-annotate-convert-time (time)
3276 "Convert a time value to a floating-point number of days.
3277 The argument TIME is a list as returned by `current-time' or
3278 `encode-time', only the first two elements of that list are considered."
3279 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3281 (defun vc-annotate-difference (&optional offset)
3282 "Return the time span in days to the next annotation.
3283 This calls the backend function annotate-time, and returns the
3284 difference in days between the time returned and the current time,
3285 or OFFSET if present."
3286 (let ((next-time (vc-annotate-get-time-set-line-props)))
3287 (when next-time
3288 (- (or offset
3289 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3290 next-time))))
3292 (defun vc-default-annotate-current-time (backend)
3293 "Return the current time, encoded as fractional days."
3294 (vc-annotate-convert-time (current-time)))
3296 (defvar vc-annotate-offset nil)
3298 (defun vc-annotate-display (ratio &optional offset)
3299 "Highlight `vc-annotate' output in the current buffer.
3300 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
3301 The annotations are relative to the current time, unless overridden by OFFSET."
3302 (when (/= ratio 1.0)
3303 (set (make-local-variable 'vc-annotate-color-map)
3304 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
3305 vc-annotate-color-map)))
3306 (set (make-local-variable 'vc-annotate-offset) offset)
3307 (font-lock-mode 1))
3309 (defun vc-annotate-lines (limit)
3310 (while (< (point) limit)
3311 (let ((difference (vc-annotate-difference vc-annotate-offset))
3312 (start (point))
3313 (end (progn (forward-line 1) (point))))
3314 (when difference
3315 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3316 (cons nil vc-annotate-very-old-color)))
3317 ;; substring from index 1 to remove any leading `#' in the name
3318 (face-name (concat "vc-annotate-face-"
3319 (if (string-equal
3320 (substring (cdr color) 0 1) "#")
3321 (substring (cdr color) 1)
3322 (cdr color))))
3323 ;; Make the face if not done.
3324 (face (or (intern-soft face-name)
3325 (let ((tmp-face (make-face (intern face-name))))
3326 (set-face-foreground tmp-face (cdr color))
3327 (when vc-annotate-background
3328 (set-face-background tmp-face
3329 vc-annotate-background))
3330 tmp-face)))) ; Return the face
3331 (put-text-property start end 'face face)))))
3332 ;; Pretend to font-lock there were no matches.
3333 nil)
3336 ;; Set up key bindings for use while editing log messages
3338 (defun vc-log-edit (fileset)
3339 "Set up `log-edit' for use with VC on FILE."
3340 (setq default-directory
3341 (with-current-buffer vc-parent-buffer default-directory))
3342 (log-edit 'vc-finish-logentry
3344 `((log-edit-listfun . (lambda () ',fileset))
3345 (log-edit-diff-function . (lambda () (vc-diff nil)))))
3346 (set (make-local-variable 'vc-log-fileset) fileset)
3347 (make-local-variable 'vc-log-revision)
3348 (set-buffer-modified-p nil)
3349 (setq buffer-file-name nil))
3351 ;; These things should probably be generally available
3353 (defun vc-file-tree-walk (dirname func &rest args)
3354 "Walk recursively through DIRNAME.
3355 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3356 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3357 (message "Traversing directory %s...done" dirname))
3359 (defun vc-file-tree-walk-internal (file func args)
3360 (if (not (file-directory-p file))
3361 (when (vc-backend file) (apply func file args))
3362 (message "Traversing directory %s..." (abbreviate-file-name file))
3363 (let ((dir (file-name-as-directory file)))
3364 (mapcar
3365 (lambda (f) (or
3366 (string-equal f ".")
3367 (string-equal f "..")
3368 (member f vc-directory-exclusion-list)
3369 (let ((dirf (expand-file-name f dir)))
3371 (file-symlink-p dirf) ;; Avoid possible loops.
3372 (vc-file-tree-walk-internal dirf func args)))))
3373 (directory-files dir)))))
3375 (provide 'vc)
3377 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3379 ;; These may be useful to anyone who has to debug or extend the package.
3380 ;; (Note that this information corresponds to versions 5.x. Some of it
3381 ;; might have been invalidated by the additions to support branching
3382 ;; and RCS keyword lookup. AS, 1995/03/24)
3384 ;; A fundamental problem in VC is that there are time windows between
3385 ;; vc-next-action's computations of the file's version-control state and
3386 ;; the actions that change it. This is a window open to lossage in a
3387 ;; multi-user environment; someone else could nip in and change the state
3388 ;; of the master during it.
3390 ;; The performance problem is that rlog/prs calls are very expensive; we want
3391 ;; to avoid them as much as possible.
3393 ;; ANALYSIS:
3395 ;; The performance problem, it turns out, simplifies in practice to the
3396 ;; problem of making vc-state fast. The two other functions that call
3397 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3398 ;; makes snapshots, the other deletes the calling user's last change in the
3399 ;; master.
3401 ;; The race condition implies that we have to either (a) lock the master
3402 ;; during the entire execution of vc-next-action, or (b) detect and
3403 ;; recover from errors resulting from dispatch on an out-of-date state.
3405 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3406 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3407 ;; checkin, the change message buffer pops up, and the user, having wandered
3408 ;; off to do something else, simply forgets about it?
3410 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3411 ;; vc-state. Usually, if a file is registered, we can read its locked/
3412 ;; unlocked state and its current owner from its permissions.
3414 ;; This shortcut will fail if someone has manually changed the workfile's
3415 ;; permissions; also if developers are munging the workfile in several
3416 ;; directories, with symlinks to a master (in this latter case, the
3417 ;; permissions shortcut will fail to detect a lock asserted from another
3418 ;; directory).
3420 ;; Note that these cases correspond exactly to the errors which could happen
3421 ;; because of a competing checkin/checkout race in between two instances of
3422 ;; vc-next-action.
3424 ;; For VC's purposes, a workfile/master pair may have the following states:
3426 ;; A. Unregistered. There is a workfile, there is no master.
3428 ;; B. Registered and not locked by anyone.
3430 ;; C. Locked by calling user and unchanged.
3432 ;; D. Locked by the calling user and changed.
3434 ;; E. Locked by someone other than the calling user.
3436 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3438 ;; VC's idea of state
3439 ;; |
3440 ;; V Actual state RCS action SCCS action Effect
3441 ;; A B C D E
3442 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3443 ;; B 5 . 6 7 8 co -l get -e checkout
3444 ;; C 9 10 . 11 12 co -u unget; get revert
3445 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3446 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3448 ;; All commands take the master file name as a last argument (not shown).
3450 ;; In the discussion below, a "self-race" is a pathological situation in
3451 ;; which VC operations are being attempted simultaneously by two or more
3452 ;; Emacsen running under the same username.
3454 ;; The vc-next-action code has the following windows:
3456 ;; Window P:
3457 ;; Between the check for existence of a master file and the call to
3458 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3459 ;; never close if the initial-comment feature is on.
3461 ;; Window Q:
3462 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3463 ;; following revert (apparent state C).
3465 ;; Window R:
3466 ;; Between the call to vc-workfile-unchanged-p in and the following
3467 ;; checkin (apparent state D). This window may never close.
3469 ;; Window S:
3470 ;; Between the unlock and the immediately following checkout during a
3471 ;; revert operation (apparent state C). Included in window Q.
3473 ;; Window T:
3474 ;; Between vc-state and the following checkout (apparent state B).
3476 ;; Window U:
3477 ;; Between vc-state and the following revert (apparent state C).
3478 ;; Includes windows Q and S.
3480 ;; Window V:
3481 ;; Between vc-state and the following checkin (apparent state
3482 ;; D). This window may never be closed if the user fails to complete the
3483 ;; checkin message. Includes window R.
3485 ;; Window W:
3486 ;; Between vc-state and the following steal-lock (apparent
3487 ;; state E). This window may never close if the user fails to complete
3488 ;; the steal-lock message. Includes window X.
3490 ;; Window X:
3491 ;; Between the unlock and the immediately following re-lock during a
3492 ;; steal-lock operation (apparent state E). This window may never close
3493 ;; if the user fails to complete the steal-lock message.
3495 ;; Errors:
3497 ;; Apparent state A ---
3499 ;; 1. File looked unregistered but is actually registered and not locked.
3501 ;; Potential cause: someone else's admin during window P, with
3502 ;; caller's admin happening before their checkout.
3504 ;; RCS: Prior to version 5.6.4, ci fails with message
3505 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3506 ;; ci -i option and the message is "<file>,v: already exists".
3507 ;; SCCS: admin will fail with error (ad19).
3509 ;; We can let these errors be passed up to the user.
3511 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3513 ;; Potential cause: self-race during window P.
3515 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3516 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3517 ;; ci -i option, failing with message "<file>,v: already exists".
3518 ;; SCCS: will fail with error (ad19).
3520 ;; Either of these consequences is acceptable.
3522 ;; 3. File looked unregistered but is actually locked by caller, changed.
3524 ;; Potential cause: self-race during window P.
3526 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3527 ;; a delta with a null change comment (the -t- switch will be
3528 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3529 ;; failing with message "<file>,v: already exists".
3530 ;; SCCS: will fail with error (ad19).
3532 ;; 4. File looked unregistered but is locked by someone else.
3534 ;; Potential cause: someone else's admin during window P, with
3535 ;; caller's admin happening *after* their checkout.
3537 ;; RCS: Prior to version 5.6.4, ci fails with a
3538 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3539 ;; VC uses the new ci -i option, failing with message
3540 ;; "<file>,v: already exists".
3541 ;; SCCS: will fail with error (ad19).
3543 ;; We can let these errors be passed up to the user.
3545 ;; Apparent state B ---
3547 ;; 5. File looked registered and not locked, but is actually unregistered.
3549 ;; Potential cause: master file got nuked during window P.
3551 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3552 ;; SCCS: will fail with error ut4.
3554 ;; We can let these errors be passed up to the user.
3556 ;; 6. File looked registered and not locked, but is actually locked by the
3557 ;; calling user and unchanged.
3559 ;; Potential cause: self-race during window T.
3561 ;; RCS: in the same directory as the previous workfile, co -l will fail
3562 ;; with "co error: writable foo exists; checkout aborted". In any other
3563 ;; directory, checkout will succeed.
3564 ;; SCCS: will fail with ge17.
3566 ;; Either of these consequences is acceptable.
3568 ;; 7. File looked registered and not locked, but is actually locked by the
3569 ;; calling user and changed.
3571 ;; As case 6.
3573 ;; 8. File looked registered and not locked, but is actually locked by another
3574 ;; user.
3576 ;; Potential cause: someone else checks it out during window T.
3578 ;; RCS: co error: revision 1.3 already locked by <user>
3579 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3581 ;; We can let these errors be passed up to the user.
3583 ;; Apparent state C ---
3585 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3587 ;; As case 5.
3589 ;; 10. File looks locked by calling user and unchanged, but is actually not
3590 ;; locked.
3592 ;; Potential cause: a self-race in window U, or by the revert's
3593 ;; landing during window X of some other user's steal-lock or window S
3594 ;; of another user's revert.
3596 ;; RCS: succeeds, refreshing the file from the identical version in
3597 ;; the master.
3598 ;; SCCS: fails with error ut4 (p file nonexistent).
3600 ;; Either of these consequences is acceptable.
3602 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3603 ;; changed.
3605 ;; Potential cause: the file would have to be touched by a self-race
3606 ;; during window Q.
3608 ;; The revert will succeed, removing whatever changes came with
3609 ;; the touch. It is theoretically possible that work could be lost.
3611 ;; 12. File looks like it's locked by the calling user and unchanged, but
3612 ;; it's actually locked by someone else.
3614 ;; Potential cause: a steal-lock in window V.
3616 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3617 ;; SCCS: fails with error un2
3619 ;; We can pass these errors up to the user.
3621 ;; Apparent state D ---
3623 ;; 13. File looks like it's locked by the calling user and changed, but it's
3624 ;; actually unregistered.
3626 ;; Potential cause: master file got nuked during window P.
3628 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3629 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3630 ;; option, failing with message "no such file or directory".
3631 ;; SCCS: will fail with error ut4.
3633 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3634 ;; VC may fail to detect the loss of previous version information.
3636 ;; 14. File looks like it's locked by the calling user and changed, but it's
3637 ;; actually unlocked.
3639 ;; Potential cause: self-race in window V, or the checkin happening
3640 ;; during the window X of someone else's steal-lock or window S of
3641 ;; someone else's revert.
3643 ;; RCS: ci will fail with "no lock set by <user>".
3644 ;; SCCS: delta will fail with error ut4.
3646 ;; 15. File looks like it's locked by the calling user and changed, but it's
3647 ;; actually locked by the calling user and unchanged.
3649 ;; Potential cause: another self-race --- a whole checkin/checkout
3650 ;; sequence by the calling user would have to land in window R.
3652 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3653 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3654 ;; the file unlocked.
3656 ;; It is theoretically possible that work could be lost under RCS.
3658 ;; 16. File looks like it's locked by the calling user and changed, but it's
3659 ;; actually locked by a different user.
3661 ;; RCS: ci error: no lock set by <user>
3662 ;; SCCS: unget will fail with error un2
3664 ;; We can pass these errors up to the user.
3666 ;; Apparent state E ---
3668 ;; 17. File looks like it's locked by some other user, but it's actually
3669 ;; unregistered.
3671 ;; As case 13.
3673 ;; 18. File looks like it's locked by some other user, but it's actually
3674 ;; unlocked.
3676 ;; Potential cause: someone released a lock during window W.
3678 ;; RCS: The calling user will get the lock on the file.
3679 ;; SCCS: unget -n will fail with cm4.
3681 ;; Either of these consequences will be OK.
3683 ;; 19. File looks like it's locked by some other user, but it's actually
3684 ;; locked by the calling user and unchanged.
3686 ;; Potential cause: the other user relinquishing a lock followed by
3687 ;; a self-race, both in window W.
3689 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3690 ;; the sequence a no-op.
3692 ;; 20. File looks like it's locked by some other user, but it's actually
3693 ;; locked by the calling user and changed.
3695 ;; As case 19.
3697 ;; PROBLEM CASES:
3699 ;; In order of decreasing severity:
3701 ;; Cases 11 and 15 are the only ones that potentially lose work.
3702 ;; They would require a self-race for this to happen.
3704 ;; Case 13 in RCS loses information about previous deltas, retaining
3705 ;; only the information in the current workfile. This can only happen
3706 ;; if the master file gets nuked in window P.
3708 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3709 ;; no change comment in the master. This would require a self-race in
3710 ;; window P or R respectively.
3712 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3714 ;; Unfortunately, it appears to me that no recovery is possible in these
3715 ;; cases. They don't yield error messages, so there's no way to tell that
3716 ;; a race condition has occurred.
3718 ;; All other cases don't change either the workfile or the master, and
3719 ;; trigger command errors which the user will see.
3721 ;; Thus, there is no explicit recovery code.
3723 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
3724 ;;; vc.el ends here