* vc-cvs.el, vc-svn.el: Simplify backend dired-state-info
[emacs.git] / lisp / vc.el
blob373d0f501240bf154d6c6f2ae6b288e412193601
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 Free Software Foundation, Inc.
6 ;; Author: FSF (see below for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; Keywords: tools
10 ;; $Id$
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Credits:
31 ;; VC was initially designed and implemented by Eric S. Raymond
32 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
33 ;; contributed substantial amounts of work to VC. These include:
35 ;; Per Cederqvist <ceder@lysator.liu.se>
36 ;; Paul Eggert <eggert@twinsun.com>
37 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
38 ;; Martin Lorentzson <martinl@gnu.org>
39 ;; Dave Love <fx@gnu.org>
40 ;; Stefan Monnier <monnier@cs.yale.edu>
41 ;; J.D. Smith <jdsmith@alum.mit.edu>
42 ;; Andre Spiegel <spiegel@gnu.org>
43 ;; Richard Stallman <rms@gnu.org>
44 ;; Thien-Thi Nguyen <ttn@gnu.org>
46 ;; In July 2007 ESR returned and redesigned the mode to cope better
47 ;; with modern version-control systems that do commits by fileset
48 ;; rather than per individual file.
50 ;; Features in the new version:
51 ;; * Key commands (vc-next-action = C-x v v, vc-print-log = C-x v l, vc-revert
52 ;; = C-x v u, vc-rollback = C-x v c, vc-diff = C-x v =, vc-update = C-x v +)
53 ;; now operate on filesets rather than individual files.
54 ;; * The fileset for a command is either (a) all marked files in VC-dired
55 ;; mode, (b) the currently visited file if it's under version control,
56 ;; or (c) the current directory if the visited buffer is not under
57 ;; version control and a wildcarding-enable flag has been set.
59 ;; If you maintain a client of the mode or customize it in your .emacs,
60 ;; note that some backend functions which formerly took single file arguments
61 ;; now take a list of files. These include: register, checkin, print-log,
62 ;; rollback, and diff.
64 ;;; Commentary:
66 ;; This mode is fully documented in the Emacs user's manual.
68 ;; Supported version-control systems presently include CVS, RCS, GNU
69 ;; Arch, Subversion, Bzr, Git, Mercurial, Meta-CVS, Monotone and SCCS
70 ;; (or its free replacement, CSSC).
72 ;; Some features will not work with old RCS versions. Where
73 ;; appropriate, VC finds out which version you have, and allows or
74 ;; disallows those features (stealing locks, for example, works only
75 ;; from 5.6.2 onwards).
76 ;; Even initial checkins will fail if your RCS version is so old that ci
77 ;; doesn't understand -t-; this has been known to happen to people running
78 ;; NExTSTEP 3.0.
80 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
82 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
83 ;; to be installed somewhere on Emacs's path for executables.
85 ;; If your site uses the ChangeLog convention supported by Emacs, the
86 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
87 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
88 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
90 ;; The vc code maintains some internal state in order to reduce expensive
91 ;; version-control operations to a minimum. Some names are only computed
92 ;; once. If you perform version control operations with the backend while
93 ;; vc's back is turned, or move/rename master files while vc is running,
94 ;; vc may get seriously confused. Don't do these things!
96 ;; Developer's notes on some concurrency issues are included at the end of
97 ;; the file.
99 ;; ADDING SUPPORT FOR OTHER BACKENDS
101 ;; VC can use arbitrary version control systems as a backend. To add
102 ;; support for a new backend named SYS, write a library vc-sys.el that
103 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
104 ;; case for the function and library names). VC will use that library if
105 ;; you put the symbol SYS somewhere into the list of
106 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
107 ;; returns non-nil for a file, all SYS-specific versions of VC commands
108 ;; will be available for that file.
110 ;; VC keeps some per-file information in the form of properties (see
111 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
112 ;; do not generally need to be aware of these properties. For example,
113 ;; `vc-sys-working-revision' should compute the working revision and
114 ;; return it; it should not look it up in the property, and it needn't
115 ;; store it there either. However, if a backend-specific function does
116 ;; store a value in a property, that value takes precedence over any
117 ;; value that the generic code might want to set (check for uses of
118 ;; the macro `with-vc-properties' in vc.el).
120 ;; In the list of functions below, each identifier needs to be prepended
121 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
122 ;; `*'), others are optional (`-').
124 ;; BACKEND PROPERTIES
126 ;; * revision-granularity
128 ;; Takes no arguments. Returns either 'file or 'repository. Backends
129 ;; that return 'file have per-file revision numbering; backends
130 ;; that return 'repository have per-repository revision numbering,
131 ;; so a revision level implicitly identifies a changeset
133 ;; STATE-QUERYING FUNCTIONS
135 ;; * registered (file)
137 ;; Return non-nil if FILE is registered in this backend. Both this
138 ;; function as well as `state' should be careful to fail gracefully
139 ;; in the event that the backend executable is absent. It is
140 ;; preferable that this function's body is autoloaded, that way only
141 ;; calling vc-registered does not cause the backend to be loaded
142 ;; (all the vc-FOO-registered functions are called to try to find
143 ;; the controlling backend for FILE.
145 ;; * state (file)
147 ;; Return the current version control state of FILE. For a list of
148 ;; possible values, see `vc-state'. This function should do a full and
149 ;; reliable state computation; it is usually called immediately after
150 ;; C-x v v. If you want to use a faster heuristic when visiting a
151 ;; file, put that into `state-heuristic' below.
153 ;; - state-heuristic (file)
155 ;; If provided, this function is used to estimate the version control
156 ;; state of FILE at visiting time. It should be considerably faster
157 ;; than the implementation of `state'. For a list of possible values,
158 ;; see the doc string of `vc-state'.
160 ;; - dir-state (dir)
162 ;; If provided, this function is used to find the version control state
163 ;; of all files in DIR, and all subdirecties of DIR, in a fast way.
164 ;; The function should not return anything, but rather store the files'
165 ;; states into the corresponding `vc-state' properties. (Note: in
166 ;; older versions this method was not required to recurse into
167 ;; subdirectories.)
169 ;; * working-revision (file)
171 ;; Return the working revision of FILE. This is the revision fetched
172 ;; by the last checkout or upate, not necessarily the same thing as the
173 ;; head or tip revision. Should return "0" for a file added but not yet
174 ;; committed.
176 ;; - latest-on-branch-p (file)
178 ;; Return non-nil if the working revision of FILE is the latest revision
179 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
180 ;; The default implementation always returns t, which means that
181 ;; working with non-current revisions is not supported by default.
183 ;; * checkout-model (file)
185 ;; Indicate whether FILE needs to be "checked out" before it can be
186 ;; edited. See `vc-checkout-model' for a list of possible values.
188 ;; - workfile-unchanged-p (file)
190 ;; Return non-nil if FILE is unchanged from the working revision.
191 ;; This function should do a brief comparison of FILE's contents
192 ;; with those of the repository master of the working revision. If
193 ;; the backend does not have such a brief-comparison feature, the
194 ;; default implementation of this function can be used, which
195 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
196 ;; must not run asynchronously in this case, see variable
197 ;; `vc-disable-async-diff'.)
199 ;; - mode-line-string (file)
201 ;; If provided, this function should return the VC-specific mode
202 ;; line string for FILE. The returned string should have a
203 ;; `help-echo' property which is the text to be displayed as a
204 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
205 ;; The default implementation deals well with all states that
206 ;; `vc-state' can return.
208 ;; - dired-state-info (file)
210 ;; Translate the `vc-state' property of FILE into a string that can be
211 ;; used in a vc-dired buffer. The default implementation deals well
212 ;; with all states that `vc-state' can return.
214 ;; STATE-CHANGING FUNCTIONS
216 ;; * create-repo (backend)
218 ;; Create an empty repository in the current directory and initialize
219 ;; it so VC mode can add files to it. For file-oriented systems, this
220 ;; need do no more than create a subdirectory with the right name.
222 ;; * register (files &optional rev comment)
224 ;; Register FILES in this backend. Optionally, an initial revision REV
225 ;; and an initial description of the file, COMMENT, may be specified,
226 ;; but it is not guaranteed that the backend will do anything with this.
227 ;; The implementation should pass the value of vc-register-switches
228 ;; to the backend command. (Note: in older versions of VC, this
229 ;; command took a single file argument and not a list.)
231 ;; - init-revision (file)
233 ;; The initial revision to use when registering FILE if one is not
234 ;; specified by the user. If not provided, the variable
235 ;; vc-default-init-revision is used instead.
237 ;; - responsible-p (file)
239 ;; Return non-nil if this backend considers itself "responsible" for
240 ;; FILE, which can also be a directory. This function is used to find
241 ;; out what backend to use for registration of new files and for things
242 ;; like change log generation. The default implementation always
243 ;; returns nil.
245 ;; - could-register (file)
247 ;; Return non-nil if FILE could be registered under this backend. The
248 ;; default implementation always returns t.
250 ;; - receive-file (file rev)
252 ;; Let this backend "receive" a file that is already registered under
253 ;; another backend. The default implementation simply calls `register'
254 ;; for FILE, but it can be overridden to do something more specific,
255 ;; e.g. keep revision numbers consistent or choose editing modes for
256 ;; FILE that resemble those of the other backend.
258 ;; - unregister (file)
260 ;; Unregister FILE from this backend. This is only needed if this
261 ;; backend may be used as a "more local" backend for temporary editing.
263 ;; * checkin (files rev comment)
265 ;; Commit changes in FILES to this backend. If REV is non-nil, that
266 ;; should become the new revision number (not all backends do
267 ;; anything with it). COMMENT is used as a check-in comment. The
268 ;; implementation should pass the value of vc-checkin-switches to
269 ;; the backend command. (Note: in older versions of VC, this
270 ;; command took a single file argument and not a list.)
272 ;; * find-revision (file rev buffer)
274 ;; Fetch revision REV of file FILE and put it into BUFFER.
275 ;; If REV is the empty string, fetch the head of the trunk.
276 ;; The implementation should pass the value of vc-checkout-switches
277 ;; to the backend command.
279 ;; * checkout (file &optional editable rev)
281 ;; Check out revision REV of FILE into the working area. If EDITABLE
282 ;; is non-nil, FILE should be writable by the user and if locking is
283 ;; used for FILE, a lock should also be set. If REV is non-nil, that
284 ;; is the revision to check out (default is the working revision).
285 ;; If REV is t, that means to check out the head of the current branch;
286 ;; if it is the empty string, check out the head of the trunk.
287 ;; The implementation should pass the value of vc-checkout-switches
288 ;; to the backend command.
290 ;; * revert (file &optional contents-done)
292 ;; Revert FILE back to the working revision. If optional
293 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
294 ;; already been reverted from a version backup, and this function
295 ;; only needs to update the status of FILE within the backend.
297 ;; - rollback (files)
299 ;; Remove the tip revision of each of FILES from the repository. If
300 ;; this function is not provided, trying to cancel a revision is
301 ;; caught as an error. (Most backends don't provide it.) (Also
302 ;; note that older versions of this backend command were called
303 ;; 'cancel-version' and took a single file arg, not a list of
304 ;; files.)
306 ;; - merge (file rev1 rev2)
308 ;; Merge the changes between REV1 and REV2 into the current working file.
310 ;; - merge-news (file)
312 ;; Merge recent changes from the current branch into FILE.
314 ;; - steal-lock (file &optional revision)
316 ;; Steal any lock on the working revision of FILE, or on REVISION if
317 ;; that is provided. This function is only needed if locking is
318 ;; used for files under this backend, and if files can indeed be
319 ;; locked by other users.
321 ;; - modify-change-comment (files rev comment)
323 ;; Modify the change comments associated with the files at the
324 ;; given revision. This is optional, many backends do not support it.
326 ;; HISTORY FUNCTIONS
328 ;; * print-log (files &optional buffer)
330 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
331 ;; if BUFFER is nil. (Note: older versions of this function expected
332 ;; only a single file argument.)
334 ;; - log-view-mode ()
336 ;; Mode to use for the output of print-log. This defaults to
337 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
338 ;; mode of `log-view-mode'.
340 ;; - show-log-entry (revision)
342 ;; If provided, search the log entry for REVISION in the current buffer,
343 ;; and make sure it is displayed in the buffer's window. The default
344 ;; implementation of this function works for RCS-style logs.
346 ;; - wash-log (file)
348 ;; Remove all non-comment information from the output of print-log.
350 ;; - logentry-check ()
352 ;; If defined, this function is run to find out whether the user
353 ;; entered a valid log entry for check-in. The log entry is in the
354 ;; current buffer, and if it is not a valid one, the function should
355 ;; throw an error.
357 ;; - comment-history (file)
359 ;; Return a string containing all log entries that were made for FILE.
360 ;; This is used for transferring a file from one backend to another,
361 ;; retaining comment information. The default implementation of this
362 ;; function does this by calling print-log and then wash-log, and
363 ;; returning the resulting buffer contents as a string.
365 ;; - update-changelog (files)
367 ;; Using recent log entries, create ChangeLog entries for FILES, or for
368 ;; all files at or below the default-directory if FILES is nil. The
369 ;; default implementation runs rcs2log, which handles RCS- and
370 ;; CVS-style logs.
372 ;; * diff (files &optional rev1 rev2 buffer)
374 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
375 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
376 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
377 ;; found in the repository) as the older revision; if REV2 is nil,
378 ;; use the current working-copy contents as the newer revision. This
379 ;; function should pass the value of (vc-switches BACKEND 'diff) to
380 ;; the backend command. It should return a status of either 0 (no
381 ;; differences found), or 1 (either non-empty diff or the diff is
382 ;; run asynchronously).
384 ;; - revision-completion-table (files)
386 ;; Return a completion table for existing revisions of FILES.
387 ;; The default is to not use any completion table.
389 ;; - annotate-command (file buf &optional rev)
391 ;; If this function is provided, it should produce an annotated display
392 ;; of FILE in BUF, relative to revision REV. Annotation means each line
393 ;; of FILE displayed is prefixed with version information associated with
394 ;; its addition (deleted lines leave no history) and that the text of the
395 ;; file is fontified according to age.
397 ;; - annotate-time ()
399 ;; Only required if `annotate-command' is defined for the backend.
400 ;; Return the time of the next line of annotation at or after point,
401 ;; as a floating point fractional number of days. The helper
402 ;; function `vc-annotate-convert-time' may be useful for converting
403 ;; multi-part times as returned by `current-time' and `encode-time'
404 ;; to this format. Return nil if no more lines of annotation appear
405 ;; in the buffer. You can safely assume that point is placed at the
406 ;; beginning of each line, starting at `point-min'. The buffer that
407 ;; point is placed in is the Annotate output, as defined by the
408 ;; relevant backend. This function also affects how much of the line
409 ;; is fontified; where it leaves point is where fontification begins.
411 ;; - annotate-current-time ()
413 ;; Only required if `annotate-command' is defined for the backend,
414 ;; AND you'd like the current time considered to be anything besides
415 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
416 ;; time with hours, minutes, and seconds included. Probably safe to
417 ;; ignore. Return the current-time, in units of fractional days.
419 ;; - annotate-extract-revision-at-line ()
421 ;; Only required if `annotate-command' is defined for the backend.
422 ;; Invoked from a buffer in vc-annotate-mode, return the revision
423 ;; corresponding to the current line, or nil if there is no revision
424 ;; corresponding to the current line.
426 ;; SNAPSHOT SYSTEM
428 ;; - create-snapshot (dir name branchp)
430 ;; Take a snapshot of the current state of files under DIR and name it
431 ;; NAME. This should make sure that files are up-to-date before
432 ;; proceeding with the action. DIR can also be a file and if BRANCHP
433 ;; is specified, NAME should be created as a branch and DIR should be
434 ;; checked out under this new branch. The default implementation does
435 ;; not support branches but does a sanity check, a tree traversal and
436 ;; for each file calls `assign-name'.
438 ;; - assign-name (file name)
440 ;; Give name NAME to the working revision of FILE, assuming it is
441 ;; up-to-date. Only used by the default version of `create-snapshot'.
443 ;; - retrieve-snapshot (dir name update)
445 ;; Retrieve a named snapshot of all registered files at or below DIR.
446 ;; If UPDATE is non-nil, then update buffers of any files in the
447 ;; snapshot that are currently visited. The default implementation
448 ;; does a sanity check whether there aren't any uncommitted changes at
449 ;; or below DIR, and then performs a tree walk, using the `checkout'
450 ;; function to retrieve the corresponding revisions.
452 ;; MISCELLANEOUS
454 ;; - make-version-backups-p (file)
456 ;; Return non-nil if unmodified repository revisions of FILE should be
457 ;; backed up locally. If this is done, VC can perform `diff' and
458 ;; `revert' operations itself, without calling the backend system. The
459 ;; default implementation always returns nil.
461 ;; - repository-hostname (dirname)
463 ;; Return the hostname that the backend will have to contact
464 ;; in order to operate on a file in DIRNAME. If the return value
465 ;; is nil, it means that the repository is local.
466 ;; This function is used in `vc-stay-local-p' which backends can use
467 ;; for their convenience.
469 ;; - previous-revision (file rev)
471 ;; Return the revision number that precedes REV for FILE, or nil if no such
472 ;; revision exists.
474 ;; - next-revision (file rev)
476 ;; Return the revision number that follows REV for FILE, or nil if no such
477 ;; revision exists.
479 ;; - check-headers ()
481 ;; Return non-nil if the current buffer contains any version headers.
483 ;; - clear-headers ()
485 ;; In the current buffer, reset all version headers to their unexpanded
486 ;; form. This function should be provided if the state-querying code
487 ;; for this backend uses the version headers to determine the state of
488 ;; a file. This function will then be called whenever VC changes the
489 ;; version control state in such a way that the headers would give
490 ;; wrong information.
492 ;; - delete-file (file)
494 ;; Delete FILE and mark it as deleted in the repository. If this
495 ;; function is not provided, the command `vc-delete-file' will
496 ;; signal an error.
498 ;; - rename-file (old new)
500 ;; Rename file OLD to NEW, both in the working area and in the
501 ;; repository. If this function is not provided, the renaming
502 ;; will be done by (vc-delete-file old) and (vc-register new).
504 ;; - find-file-hook ()
506 ;; Operation called in current buffer when opening a file. This can
507 ;; be used by the backend to setup some local variables it might need.
509 ;; - find-file-not-found-hook ()
511 ;; Operation called in current buffer when opening a non-existing file.
512 ;; By default, this asks the user if she wants to check out the file.
514 ;; - extra-menu ()
516 ;; Return a menu keymap, the items in the keymap will appear at the
517 ;; end of the Version Control menu. The goal is to allow backends
518 ;; to specify extra menu items that appear in the VC menu. This way
519 ;; you can provide menu entries for functionality that is specific
520 ;; to your backend and which does not map to any of the VC generic
521 ;; concepts.
523 ;;; Code:
525 (require 'vc-hooks)
526 (require 'ring)
527 (eval-when-compile
528 (require 'cl)
529 (require 'compile)
530 (require 'dired) ; for dired-map-over-marks macro
531 (require 'dired-aux)) ; for dired-kill-{line,tree}
533 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
534 (setq minor-mode-alist
535 (cons '(vc-parent-buffer vc-parent-buffer-name)
536 minor-mode-alist)))
538 ;; General customization
540 (defgroup vc nil
541 "Version-control system in Emacs."
542 :group 'tools)
544 (defcustom vc-suppress-confirm nil
545 "If non-nil, treat user as expert; suppress yes-no prompts on some things."
546 :type 'boolean
547 :group 'vc)
549 (defcustom vc-delete-logbuf-window t
550 "If non-nil, delete the *VC-log* buffer and window after each logical action.
551 If nil, bury that buffer instead.
552 This is most useful if you have multiple windows on a frame and would like to
553 preserve the setting."
554 :type 'boolean
555 :group 'vc)
557 (defcustom vc-initial-comment nil
558 "If non-nil, prompt for initial comment when a file is registered."
559 :type 'boolean
560 :group 'vc)
562 (defcustom vc-default-init-revision "1.1"
563 "A string used as the default revision number when a new file is registered.
564 This can be overridden by giving a prefix argument to \\[vc-register]. This
565 can also be overridden by a particular VC backend."
566 :type 'string
567 :group 'vc
568 :version "20.3")
570 (defcustom vc-command-messages nil
571 "If non-nil, display run messages from back-end commands."
572 :type 'boolean
573 :group 'vc)
575 (defcustom vc-checkin-switches nil
576 "A string or list of strings specifying extra switches for checkin.
577 These are passed to the checkin program by \\[vc-checkin]."
578 :type '(choice (const :tag "None" nil)
579 (string :tag "Argument String")
580 (repeat :tag "Argument List"
581 :value ("")
582 string))
583 :group 'vc)
585 (defcustom vc-checkout-switches nil
586 "A string or list of strings specifying extra switches for checkout.
587 These are passed to the checkout program by \\[vc-checkout]."
588 :type '(choice (const :tag "None" nil)
589 (string :tag "Argument String")
590 (repeat :tag "Argument List"
591 :value ("")
592 string))
593 :group 'vc)
595 (defcustom vc-register-switches nil
596 "A string or list of strings; extra switches for registering a file.
597 These are passed to the checkin program by \\[vc-register]."
598 :type '(choice (const :tag "None" nil)
599 (string :tag "Argument String")
600 (repeat :tag "Argument List"
601 :value ("")
602 string))
603 :group 'vc)
605 (defcustom vc-dired-listing-switches "-al"
606 "Switches passed to `ls' for vc-dired. MUST contain the `l' option."
607 :type 'string
608 :group 'vc
609 :version "21.1")
611 (defcustom vc-dired-recurse t
612 "If non-nil, show directory trees recursively in VC Dired."
613 :type 'boolean
614 :group 'vc
615 :version "20.3")
617 (defcustom vc-dired-terse-display t
618 "If non-nil, show only locked or locally modified files in VC Dired."
619 :type 'boolean
620 :group 'vc
621 :version "20.3")
623 (defcustom vc-diff-switches nil
624 "A string or list of strings specifying switches for diff under VC.
625 When running diff under a given BACKEND, VC concatenates the values of
626 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
627 get the switches for that command. Thus, `vc-diff-switches' should
628 contain switches that are specific to version control, but not
629 specific to any particular backend."
630 :type '(choice (const :tag "None" nil)
631 (string :tag "Argument String")
632 (repeat :tag "Argument List"
633 :value ("")
634 string))
635 :group 'vc
636 :version "21.1")
638 (defcustom vc-diff-knows-L nil
639 "*Indicates whether diff understands the -L option.
640 The value is either `yes', `no', or nil. If it is nil, VC tries
641 to use -L and sets this variable to remember whether it worked."
642 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
643 :group 'vc)
645 (defcustom vc-allow-async-revert nil
646 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
647 Enabling this option means that you can confirm a revert operation even
648 if the local changes in the file have not been found and displayed yet."
649 :type '(choice (const :tag "No" nil)
650 (const :tag "Yes" t))
651 :group 'vc
652 :version "22.1")
654 ;;;###autoload
655 (defcustom vc-checkout-hook nil
656 "Normal hook (list of functions) run after checking out a file.
657 See `run-hooks'."
658 :type 'hook
659 :group 'vc
660 :version "21.1")
662 (defcustom vc-annotate-display-mode 'fullscale
663 "Which mode to color the output of \\[vc-annotate] with by default."
664 :type '(choice (const :tag "By Color Map Range" nil)
665 (const :tag "Scale to Oldest" scale)
666 (const :tag "Scale Oldest->Newest" fullscale)
667 (number :tag "Specify Fractional Number of Days"
668 :value "20.5"))
669 :group 'vc)
671 ;;;###autoload
672 (defcustom vc-checkin-hook nil
673 "Normal hook (list of functions) run after commit or file checkin.
674 See also `log-edit-done-hook'."
675 :type 'hook
676 :options '(log-edit-comment-to-change-log)
677 :group 'vc)
679 ;;;###autoload
680 (defcustom vc-before-checkin-hook nil
681 "Normal hook (list of functions) run before a commit or a file checkin.
682 See `run-hooks'."
683 :type 'hook
684 :group 'vc)
686 (defcustom vc-logentry-check-hook nil
687 "Normal hook run by `vc-backend-logentry-check'.
688 Use this to impose your own rules on the entry in addition to any the
689 version control backend imposes itself."
690 :type 'hook
691 :group 'vc)
693 ;; Annotate customization
694 (defcustom vc-annotate-color-map
695 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
696 ;; A custom sorted TTY colormap
697 (let* ((colors
698 (sort
699 (delq nil
700 (mapcar (lambda (x)
701 (if (not (or
702 (string-equal (car x) "white")
703 (string-equal (car x) "black") ))
704 (car x)))
705 (tty-color-alist)))
706 (lambda (a b)
707 (cond
708 ((or (string-equal a "red") (string-equal b "blue")) t)
709 ((or (string-equal b "red") (string-equal a "blue")) nil)
710 ((string-equal a "yellow") t)
711 ((string-equal b "yellow") nil)
712 ((string-equal a "cyan") t)
713 ((string-equal b "cyan") nil)
714 ((string-equal a "green") t)
715 ((string-equal b "green") nil)
716 ((string-equal a "magenta") t)
717 ((string-equal b "magenta") nil)
718 (t (string< a b))))))
719 (date 20.)
720 (delta (/ (- 360. date) (1- (length colors)))))
721 (mapcar (lambda (x)
722 (prog1
723 (cons date x)
724 (setq date (+ date delta)))) colors))
725 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
726 '(( 20. . "#FF3F3F")
727 ( 40. . "#FF6C3F")
728 ( 60. . "#FF993F")
729 ( 80. . "#FFC63F")
730 (100. . "#FFF33F")
731 (120. . "#DDFF3F")
732 (140. . "#B0FF3F")
733 (160. . "#83FF3F")
734 (180. . "#56FF3F")
735 (200. . "#3FFF56")
736 (220. . "#3FFF83")
737 (240. . "#3FFFB0")
738 (260. . "#3FFFDD")
739 (280. . "#3FF3FF")
740 (300. . "#3FC6FF")
741 (320. . "#3F99FF")
742 (340. . "#3F6CFF")
743 (360. . "#3F3FFF")))
744 "Association list of age versus color, for \\[vc-annotate].
745 Ages are given in units of fractional days. Default is eighteen
746 steps using a twenty day increment, from red to blue. For TTY
747 displays with 8 or fewer colors, the default is red to blue with
748 all other colors between (excluding black and white)."
749 :type 'alist
750 :group 'vc)
752 (defcustom vc-annotate-very-old-color "#3F3FFF"
753 "Color for lines older than the current color range in \\[vc-annotate]]."
754 :type 'string
755 :group 'vc)
757 (defcustom vc-annotate-background "black"
758 "Background color for \\[vc-annotate].
759 Default color is used if nil."
760 :type 'string
761 :group 'vc)
763 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
764 "Menu elements for the mode-specific menu of VC-Annotate mode.
765 List of factors, used to expand/compress the time scale. See `vc-annotate'."
766 :type '(repeat number)
767 :group 'vc)
769 (defvar vc-annotate-mode-map
770 (let ((m (make-sparse-keymap)))
771 (define-key m "A" 'vc-annotate-revision-previous-to-line)
772 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
773 (define-key m "J" 'vc-annotate-revision-at-line)
774 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
775 (define-key m "N" 'vc-annotate-next-revision)
776 (define-key m "P" 'vc-annotate-prev-revision)
777 (define-key m "W" 'vc-annotate-working-revision)
778 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
780 "Local keymap used for VC-Annotate mode.")
782 ;; Header-insertion hair
784 (defcustom vc-static-header-alist
785 '(("\\.c\\'" .
786 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
787 "*Associate static header string templates with file types.
788 A \%s in the template is replaced with the first string associated with
789 the file's version control type in `vc-header-alist'."
790 :type '(repeat (cons :format "%v"
791 (regexp :tag "File Type")
792 (string :tag "Header String")))
793 :group 'vc)
795 (defcustom vc-comment-alist
796 '((nroff-mode ".\\\"" ""))
797 "*Special comment delimiters for generating VC headers.
798 Add an entry in this list if you need to override the normal `comment-start'
799 and `comment-end' variables. This will only be necessary if the mode language
800 is sensitive to blank lines."
801 :type '(repeat (list :format "%v"
802 (symbol :tag "Mode")
803 (string :tag "Comment Start")
804 (string :tag "Comment End")))
805 :group 'vc)
807 (defcustom vc-checkout-carefully (= (user-uid) 0)
808 "*Non-nil means be extra-careful in checkout.
809 Verify that the file really is not locked
810 and that its contents match what the master file says."
811 :type 'boolean
812 :group 'vc)
813 (make-obsolete-variable 'vc-checkout-carefully
814 "the corresponding checks are always done now."
815 "21.1")
818 ;; Variables the user doesn't need to know about.
819 (defvar vc-log-operation nil)
820 (defvar vc-log-after-operation-hook nil)
822 ;; In a log entry buffer, this is a local variable
823 ;; that points to the buffer for which it was made
824 ;; (either a file, or a VC dired buffer).
825 (defvar vc-parent-buffer nil)
826 (put 'vc-parent-buffer 'permanent-local t)
827 (defvar vc-parent-buffer-name nil)
828 (put 'vc-parent-buffer-name 'permanent-local t)
830 (defvar vc-disable-async-diff nil
831 "VC sets this to t locally to disable some async diff operations.
832 Backends that offer asynchronous diffs should respect this variable
833 in their implementation of vc-BACKEND-diff.")
835 (defvar vc-log-fileset)
836 (defvar vc-log-revision)
838 (defvar vc-dired-mode nil)
839 (make-variable-buffer-local 'vc-dired-mode)
841 ;; File property caching
843 (defun vc-clear-context ()
844 "Clear all cached file properties."
845 (interactive)
846 (fillarray vc-file-prop-obarray 0))
848 (defmacro with-vc-properties (files form settings)
849 "Execute FORM, then maybe set per-file properties for FILES.
850 SETTINGS is an association list of property/value pairs. After
851 executing FORM, set those properties from SETTINGS that have not yet
852 been updated to their corresponding values."
853 (declare (debug t))
854 `(let ((vc-touched-properties (list t)))
855 ,form
856 (dolist (file ,files)
857 (dolist (setting ,settings)
858 (let ((property (car setting)))
859 (unless (memq property vc-touched-properties)
860 (put (intern file vc-file-prop-obarray)
861 property (cdr setting))))))))
863 ;; Two macros for elisp programming
865 ;;;###autoload
866 (defmacro with-vc-file (file comment &rest body)
867 "Check out a writable copy of FILE if necessary, then execute BODY.
868 Check in FILE with COMMENT (a string) after BODY has been executed.
869 FILE is passed through `expand-file-name'; BODY executed within
870 `save-excursion'. If FILE is not under version control, or you are
871 using a locking version-control system and the file is locked by
872 somebody else, signal error."
873 (declare (debug t) (indent 2))
874 (let ((filevar (make-symbol "file")))
875 `(let ((,filevar (expand-file-name ,file)))
876 (or (vc-backend ,filevar)
877 (error "File not under version control: `%s'" file))
878 (unless (vc-editable-p ,filevar)
879 (let ((state (vc-state ,filevar)))
880 (if (stringp state)
881 (error "`%s' is locking `%s'" state ,filevar)
882 (vc-checkout ,filevar t))))
883 (save-excursion
884 ,@body)
885 (vc-checkin (list ,filevar) nil ,comment))))
887 ;;;###autoload
888 (defmacro edit-vc-file (file comment &rest body)
889 "Edit FILE under version control, executing body.
890 Checkin with COMMENT after executing BODY.
891 This macro uses `with-vc-file', passing args to it.
892 However, before executing BODY, find FILE, and after BODY, save buffer."
893 (declare (debug t) (indent 2))
894 (let ((filevar (make-symbol "file")))
895 `(let ((,filevar (expand-file-name ,file)))
896 (with-vc-file
897 ,filevar ,comment
898 (set-buffer (find-file-noselect ,filevar))
899 ,@body
900 (save-buffer)))))
902 ;; Common command execution logic to be used by backends
904 (defun vc-process-filter (p s)
905 "An alternative output filter for async process P.
906 One difference with the default filter is that this inserts S after markers.
907 Another is that undo information is not kept."
908 (with-current-buffer (process-buffer p)
909 (save-excursion
910 (let ((buffer-undo-list t)
911 (inhibit-read-only t))
912 (goto-char (process-mark p))
913 (insert s)
914 (set-marker (process-mark p) (point))))))
916 (defun vc-setup-buffer (&optional buf)
917 "Prepare BUF for executing a VC command and make it current.
918 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
919 (unless buf (setq buf "*vc*"))
920 (let ((camefrom (current-buffer))
921 (olddir default-directory))
922 (set-buffer (get-buffer-create buf))
923 (kill-all-local-variables)
924 (set (make-local-variable 'vc-parent-buffer) camefrom)
925 (set (make-local-variable 'vc-parent-buffer-name)
926 (concat " from " (buffer-name camefrom)))
927 (setq default-directory olddir)
928 (let ((buffer-undo-list t)
929 (inhibit-read-only t))
930 (erase-buffer))))
932 (defvar vc-sentinel-movepoint) ;Dynamically scoped.
934 (defun vc-process-sentinel (p s)
935 (let ((previous (process-get p 'vc-previous-sentinel)))
936 (if previous (funcall previous p s))
937 (with-current-buffer (process-buffer p)
938 (let (vc-sentinel-movepoint)
939 ;; Normally, we want async code such as sentinels to not move point.
940 (save-excursion
941 (goto-char (process-mark p))
942 (let ((cmds (process-get p 'vc-sentinel-commands)))
943 (process-put p 'vc-postprocess nil)
944 (dolist (cmd cmds)
945 ;; Each sentinel may move point and the next one should be run
946 ;; at that new point. We could get the same result by having
947 ;; each sentinel read&set process-mark, but since `cmd' needs
948 ;; to work both for async and sync processes, this would be
949 ;; difficult to achieve.
950 (vc-exec-after cmd))))
951 ;; But sometimes the sentinels really want to move point.
952 (if vc-sentinel-movepoint
953 (let ((win (get-buffer-window (current-buffer) 0)))
954 (if (not win)
955 (goto-char vc-sentinel-movepoint)
956 (with-selected-window win
957 (goto-char vc-sentinel-movepoint)))))))))
959 (defun vc-exec-after (code)
960 "Eval CODE when the current buffer's process is done.
961 If the current buffer has no process, just evaluate CODE.
962 Else, add CODE to the process' sentinel."
963 (let ((proc (get-buffer-process (current-buffer))))
964 (cond
965 ;; If there's no background process, just execute the code.
966 ;; We used to explicitly call delete-process on exited processes,
967 ;; but this led to timing problems causing process output to be
968 ;; lost. Terminated processes get deleted automatically
969 ;; anyway. -- cyd
970 ((or (null proc) (eq (process-status proc) 'exit))
971 ;; Make sure we've read the process's output before going further.
972 (if proc (accept-process-output proc))
973 (eval code))
974 ;; If a process is running, add CODE to the sentinel
975 ((eq (process-status proc) 'run)
976 (let ((previous (process-sentinel proc)))
977 (unless (eq previous 'vc-process-sentinel)
978 (process-put proc 'vc-previous-sentinel previous))
979 (set-process-sentinel proc 'vc-process-sentinel))
980 (process-put proc 'vc-sentinel-commands
981 (cons code (process-get proc 'vc-sentinel-commands))))
982 (t (error "Unexpected process state"))))
983 nil)
985 (defvar vc-post-command-functions nil
986 "Hook run at the end of `vc-do-command'.
987 Each function is called inside the buffer in which the command was run
988 and is passed 3 arguments: the COMMAND, the FILES and the FLAGS.")
990 (defvar w32-quote-process-args)
992 (defun vc-delistify (filelist)
993 "Smash a FILELIST into a file list string suitable for info messages."
994 ;; FIXME what about file names with spaces?
995 (if (not filelist) "." (mapconcat 'identity filelist " ")))
997 ;;;###autoload
998 (defun vc-do-command (buffer okstatus command file-or-list &rest flags)
999 "Execute a VC command, notifying user and checking for errors.
1000 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
1001 current buffer if BUFFER is t. If the destination buffer is not
1002 already current, set it up properly and erase it. The command is
1003 considered successful if its exit status does not exceed OKSTATUS (if
1004 OKSTATUS is nil, that means to ignore error status, if it is `async', that
1005 means not to wait for termination of the subprocess; if it is t it means to
1006 ignore all execution errors). FILE-OR-LIST is the name of a working file;
1007 it may be a list of files or be nil (to execute commands that don't expect
1008 a file name or set of files). If an optional list of FLAGS is present,
1009 that is inserted into the command line before the filename."
1010 ;; FIXME: file-relative-name can return a bogus result because
1011 ;; it doesn't look at the actual file-system to see if symlinks
1012 ;; come into play.
1013 (let* ((files
1014 (mapcar (lambda (f) (file-relative-name (expand-file-name f)))
1015 (if (listp file-or-list) file-or-list (list file-or-list))))
1016 (full-command
1017 ;; What we're doing here is preparing a version of the command
1018 ;; for display in a debug-progess message. If it's fewer than
1019 ;; 20 characters display the entire command (without trailing
1020 ;; newline). Otherwise display the first 20 followed by an ellipsis.
1021 (concat (if (string= (substring command -1) "\n")
1022 (substring command 0 -1)
1023 command)
1025 (vc-delistify (mapcar (lambda (s) (if (> (length s) 20) (concat (substring s 0 2) "...") s)) flags))
1026 " " (vc-delistify files))))
1027 (save-current-buffer
1028 (unless (or (eq buffer t)
1029 (and (stringp buffer)
1030 (string= (buffer-name) buffer))
1031 (eq buffer (current-buffer)))
1032 (vc-setup-buffer buffer))
1033 (let ((squeezed (remq nil flags))
1034 (inhibit-read-only t)
1035 (status 0))
1036 (when files
1037 (setq squeezed (nconc squeezed files)))
1038 (let ((exec-path (append vc-path exec-path))
1039 ;; Add vc-path to PATH for the execution of this command.
1040 (process-environment
1041 (cons (concat "PATH=" (getenv "PATH")
1042 path-separator
1043 (mapconcat 'identity vc-path path-separator))
1044 process-environment))
1045 (w32-quote-process-args t))
1046 (if (and (eq okstatus 'async) (file-remote-p default-directory))
1047 ;; start-process does not support remote execution
1048 (setq okstatus nil))
1049 (if (eq okstatus 'async)
1050 ;; Run asynchronously
1051 (let ((proc
1052 (let ((process-connection-type nil))
1053 (apply 'start-process command (current-buffer) command
1054 squeezed))))
1055 (if vc-command-messages
1056 (message "Running %s in background..." full-command))
1057 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
1058 (set-process-filter proc 'vc-process-filter)
1059 (vc-exec-after
1060 `(if vc-command-messages
1061 (message "Running %s in background... done" ',full-command))))
1062 ;; Run synchrously
1063 (if vc-command-messages
1064 (message "Running %s in foreground..." full-command))
1065 (let ((buffer-undo-list t))
1066 (setq status (apply 'process-file command nil t nil squeezed)))
1067 (when (and (not (eq t okstatus))
1068 (or (not (integerp status))
1069 (and okstatus (< okstatus status))))
1070 (pop-to-buffer (current-buffer))
1071 (goto-char (point-min))
1072 (shrink-window-if-larger-than-buffer)
1073 (error "Running %s...FAILED (%s)" full-command
1074 (if (integerp status) (format "status %d" status) status))))
1075 ;; We're done. But don't emit a status message if running
1076 ;; asychronously, it would just mislead.
1077 (if (and vc-command-messages (not (eq okstatus 'async)))
1078 (message "Running %s...OK = %d" full-command status)))
1079 (vc-exec-after
1080 `(run-hook-with-args 'vc-post-command-functions
1081 ',command ',file-or-list ',flags))
1082 status))))
1084 (defun vc-position-context (posn)
1085 "Save a bit of the text around POSN in the current buffer.
1086 Used to help us find the corresponding position again later
1087 if markers are destroyed or corrupted."
1088 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
1089 ;; rcs.el mode.
1090 (list posn
1091 (buffer-size)
1092 (buffer-substring posn
1093 (min (point-max) (+ posn 100)))))
1095 (defun vc-find-position-by-context (context)
1096 "Return the position of CONTEXT in the current buffer.
1097 If CONTEXT cannot be found, return nil."
1098 (let ((context-string (nth 2 context)))
1099 (if (equal "" context-string)
1100 (point-max)
1101 (save-excursion
1102 (let ((diff (- (nth 1 context) (buffer-size))))
1103 (if (< diff 0) (setq diff (- diff)))
1104 (goto-char (nth 0 context))
1105 (if (or (search-forward context-string nil t)
1106 ;; Can't use search-backward since the match may continue
1107 ;; after point.
1108 (progn (goto-char (- (point) diff (length context-string)))
1109 ;; goto-char doesn't signal an error at
1110 ;; beginning of buffer like backward-char would
1111 (search-forward context-string nil t)))
1112 ;; to beginning of OSTRING
1113 (- (point) (length context-string))))))))
1115 (defun vc-context-matches-p (posn context)
1116 "Return t if POSN matches CONTEXT, nil otherwise."
1117 (let* ((context-string (nth 2 context))
1118 (len (length context-string))
1119 (end (+ posn len)))
1120 (if (> end (1+ (buffer-size)))
1122 (string= context-string (buffer-substring posn end)))))
1124 (defun vc-buffer-context ()
1125 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1126 Used by `vc-restore-buffer-context' to later restore the context."
1127 (let ((point-context (vc-position-context (point)))
1128 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1129 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1130 (vc-position-context (mark-marker))))
1131 ;; Make the right thing happen in transient-mark-mode.
1132 (mark-active nil)
1133 ;; The new compilation code does not use compilation-error-list any
1134 ;; more, so the code below is now ineffective and might as well
1135 ;; be disabled. -- Stef
1136 ;; ;; We may want to reparse the compilation buffer after revert
1137 ;; (reparse (and (boundp 'compilation-error-list) ;compile loaded
1138 ;; ;; Construct a list; each elt is nil or a buffer
1139 ;; ;; if that buffer is a compilation output buffer
1140 ;; ;; that contains markers into the current buffer.
1141 ;; (save-current-buffer
1142 ;; (mapcar (lambda (buffer)
1143 ;; (set-buffer buffer)
1144 ;; (let ((errors (or
1145 ;; compilation-old-error-list
1146 ;; compilation-error-list))
1147 ;; (buffer-error-marked-p nil))
1148 ;; (while (and (consp errors)
1149 ;; (not buffer-error-marked-p))
1150 ;; (and (markerp (cdr (car errors)))
1151 ;; (eq buffer
1152 ;; (marker-buffer
1153 ;; (cdr (car errors))))
1154 ;; (setq buffer-error-marked-p t))
1155 ;; (setq errors (cdr errors)))
1156 ;; (if buffer-error-marked-p buffer)))
1157 ;; (buffer-list)))))
1158 (reparse nil))
1159 (list point-context mark-context reparse)))
1161 (defun vc-restore-buffer-context (context)
1162 "Restore point/mark, and reparse any affected compilation buffers.
1163 CONTEXT is that which `vc-buffer-context' returns."
1164 (let ((point-context (nth 0 context))
1165 (mark-context (nth 1 context))
1166 ;; (reparse (nth 2 context))
1168 ;; The new compilation code does not use compilation-error-list any
1169 ;; more, so the code below is now ineffective and might as well
1170 ;; be disabled. -- Stef
1171 ;; ;; Reparse affected compilation buffers.
1172 ;; (while reparse
1173 ;; (if (car reparse)
1174 ;; (with-current-buffer (car reparse)
1175 ;; (let ((compilation-last-buffer (current-buffer)) ;select buffer
1176 ;; ;; Record the position in the compilation buffer of
1177 ;; ;; the last error next-error went to.
1178 ;; (error-pos (marker-position
1179 ;; (car (car-safe compilation-error-list)))))
1180 ;; ;; Reparse the error messages as far as they were parsed before.
1181 ;; (compile-reinitialize-errors '(4) compilation-parsing-end)
1182 ;; ;; Move the pointer up to find the error we were at before
1183 ;; ;; reparsing. Now next-error should properly go to the next one.
1184 ;; (while (and compilation-error-list
1185 ;; (/= error-pos (car (car compilation-error-list))))
1186 ;; (setq compilation-error-list (cdr compilation-error-list))))))
1187 ;; (setq reparse (cdr reparse)))
1189 ;; if necessary, restore point and mark
1190 (if (not (vc-context-matches-p (point) point-context))
1191 (let ((new-point (vc-find-position-by-context point-context)))
1192 (if new-point (goto-char new-point))))
1193 (and mark-active
1194 mark-context
1195 (not (vc-context-matches-p (mark) mark-context))
1196 (let ((new-mark (vc-find-position-by-context mark-context)))
1197 (if new-mark (set-mark new-mark))))))
1199 ;;; Code for deducing what fileset and backend to assume
1201 (defun vc-responsible-backend (file &optional register)
1202 "Return the name of a backend system that is responsible for FILE.
1203 The optional argument REGISTER means that a backend suitable for
1204 registration should be found.
1206 If REGISTER is nil, then if FILE is already registered, return the
1207 backend of FILE. If FILE is not registered, or a directory, then the
1208 first backend in `vc-handled-backends' that declares itself
1209 responsible for FILE is returned. If no backend declares itself
1210 responsible, return the first backend.
1212 If REGISTER is non-nil, return the first responsible backend under
1213 which FILE is not yet registered. If there is no such backend, return
1214 the first backend under which FILE is not yet registered, but could
1215 be registered."
1216 (if (not vc-handled-backends)
1217 (error "No handled backends"))
1218 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1219 (catch 'found
1220 ;; First try: find a responsible backend. If this is for registration,
1221 ;; it must be a backend under which FILE is not yet registered.
1222 (dolist (backend vc-handled-backends)
1223 (and (or (not register)
1224 (not (vc-call-backend backend 'registered file)))
1225 (vc-call-backend backend 'responsible-p file)
1226 (throw 'found backend)))
1227 ;; no responsible backend
1228 (if (not register)
1229 ;; if this is not for registration, the first backend must do
1230 (car vc-handled-backends)
1231 ;; for registration, we need to find a new backend that
1232 ;; could register FILE
1233 (dolist (backend vc-handled-backends)
1234 (and (not (vc-call-backend backend 'registered file))
1235 (vc-call-backend backend 'could-register file)
1236 (throw 'found backend)))
1237 (error "No backend that could register")))))
1239 (defun vc-expand-dirs (file-or-dir-list)
1240 "Expands directories in a file list specification.
1241 Only files already under version control are noticed."
1242 ;; FIXME: Kill this function.
1243 (let ((flattened '()))
1244 (dolist (node file-or-dir-list)
1245 (vc-file-tree-walk
1246 node (lambda (f) (if (vc-backend f) (push f flattened)))))
1247 (nreverse flattened)))
1249 (defun vc-deduce-fileset (&optional allow-directory-wildcard allow-unregistered)
1250 "Deduce a set of files and a backend to which to apply an operation.
1252 If we're in VC-dired mode, the fileset is the list of marked files.
1253 Otherwise, if we're looking at a buffer visiting a version-controlled file,
1254 the fileset is a singleton containing this file.
1255 If neither of these things is true, but ALLOW-DIRECTORY-WILDCARD is on
1256 and we're in a dired buffer, select the current directory.
1257 If none of these conditions is met, but ALLOW_UNREGISTERED is in and the
1258 visited file is not registered, return a singletin fileset containing it.
1259 Otherwise, throw an error."
1260 (cond (vc-dired-mode
1261 (let ((marked (dired-map-over-marks (dired-get-filename) nil)))
1262 (unless marked
1263 (error "No files have been selected."))
1264 ;; All members of the fileset must have the same backend
1265 (let ((firstbackend (vc-backend (car marked))))
1266 (dolist (f (cdr marked))
1267 (unless (eq (vc-backend f) firstbackend)
1268 (error "All members of a fileset must be under the same version-control system."))))
1269 marked))
1270 ((vc-backend buffer-file-name)
1271 (list buffer-file-name))
1272 ((and vc-parent-buffer (or (buffer-file-name vc-parent-buffer)
1273 (with-current-buffer vc-parent-buffer
1274 vc-dired-mode)))
1275 (progn
1276 (set-buffer vc-parent-buffer)
1277 (vc-deduce-fileset)))
1278 ;; This is guarded by an enabling arg so users won't potentially
1279 ;; shoot themselves in the foot by modifying a fileset they can't
1280 ;; verify by eyeball. Allow it for nondestructive commands like
1281 ;; making diffs, or possibly for destructive ones that have
1282 ;; confirmation prompts.
1283 ((and allow-directory-wildcard
1284 ;; I think this is a misfeature. For now, I'll leave it in, but
1285 ;; I'll disable it anywhere else than in dired buffers. --Stef
1286 (and (derived-mode-p 'dired-mode)
1287 (equal buffer-file-name nil)
1288 (equal list-buffers-directory default-directory)))
1289 (progn
1290 (message "All version-controlled files below %s selected."
1291 default-directory)
1292 (list default-directory)))
1293 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1294 (list buffer-file-name))
1295 (t (error "No fileset is available here."))))
1297 (defun vc-ensure-vc-buffer ()
1298 "Make sure that the current buffer visits a version-controlled file."
1299 (if vc-dired-mode
1300 (set-buffer (find-file-noselect (dired-get-filename)))
1301 (while (and vc-parent-buffer
1302 ;; Avoid infinite looping when vc-parent-buffer and
1303 ;; current buffer are the same buffer.
1304 (not (eq vc-parent-buffer (current-buffer))))
1305 (set-buffer vc-parent-buffer))
1306 (if (not buffer-file-name)
1307 (error "Buffer %s is not associated with a file" (buffer-name))
1308 (if (not (vc-backend buffer-file-name))
1309 (error "File %s is not under version control" buffer-file-name)))))
1311 ;;; Support for the C-x v v command. This is where all the single-file-oriented
1312 ;;; code from before the fileset rewrite lives.
1314 (defsubst vc-editable-p (file)
1315 "Return non-nil if FILE can be edited."
1316 (or (eq (vc-checkout-model file) 'implicit)
1317 (memq (vc-state file) '(edited needs-merge))))
1319 (defun vc-revert-buffer-internal (&optional arg no-confirm)
1320 "Revert buffer, keeping point and mark where user expects them.
1321 Try to be clever in the face of changes due to expanded version-control
1322 key words. This is important for typeahead to work as expected.
1323 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1324 (interactive "P")
1325 (widen)
1326 (let ((context (vc-buffer-context)))
1327 ;; Use save-excursion here, because it may be able to restore point
1328 ;; and mark properly even in cases where vc-restore-buffer-context
1329 ;; would fail. However, save-excursion might also get it wrong --
1330 ;; in this case, vc-restore-buffer-context gives it a second try.
1331 (save-excursion
1332 ;; t means don't call normal-mode;
1333 ;; that's to preserve various minor modes.
1334 (revert-buffer arg no-confirm t))
1335 (vc-restore-buffer-context context)))
1337 (defun vc-buffer-sync (&optional not-urgent)
1338 "Make sure the current buffer and its working file are in sync.
1339 NOT-URGENT means it is ok to continue if the user says not to save."
1340 (if (buffer-modified-p)
1341 (if (or vc-suppress-confirm
1342 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1343 (save-buffer)
1344 (unless not-urgent
1345 (error "Aborted")))))
1347 (defvar vc-dired-window-configuration)
1349 ;; Here's the major entry point.
1351 ;;;###autoload
1352 (defun vc-next-action (verbose)
1353 "Do the next logical version control operation on the current fileset.
1354 This requires that all files in the fileset be in the same state.
1356 For locking systems:
1357 If every file is not already registered, this registers each for version
1358 control.
1359 If every file is registered and not locked by anyone, this checks out
1360 a writable and locked file of each ready for editing.
1361 If every file is checked out and locked by the calling user, this
1362 first checks to see if each file has changed since checkout. If not,
1363 it performs a revert on that file.
1364 If every file has been changed, this pops up a buffer for entry
1365 of a log message; when the message has been entered, it checks in the
1366 resulting changes along with the log message as change commentary. If
1367 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1368 read-only copy of each changed file is left in place afterwards.
1369 If the affected file is registered and locked by someone else, you are
1370 given the option to steal the lock(s).
1372 For merging systems:
1373 If every file is not already registered, this registers each one for version
1374 control. This does an add, but not a commit.
1375 If every file is added but not committed, each one is committed.
1376 If every working file is changed, but the corresponding repository file is
1377 unchanged, this pops up a buffer for entry of a log message; when the
1378 message has been entered, it checks in the resulting changes along
1379 with the logmessage as change commentary. A writable file is retained.
1380 If the repository file is changed, you are asked if you want to
1381 merge in the changes into your working copy."
1382 (interactive "P")
1383 (let* ((files (vc-deduce-fileset nil t))
1384 (state (vc-state (car files)))
1385 (model (vc-checkout-model (car files)))
1386 revision)
1387 ;; Verify that the fileset is homogenous
1388 (dolist (file (cdr files))
1389 (if (not (eq (vc-state file) state))
1390 (error "Fileset is in a mixed-up state"))
1391 (if (not (eq (vc-checkout-model file) model))
1392 (error "Fileset has mixed checkout models")))
1393 ;; Check for buffers in the fileset not matching the on-disk contents.
1394 (dolist (file files)
1395 (let ((visited (get-file-buffer file)))
1396 (when visited
1397 (if vc-dired-mode
1398 (switch-to-buffer-other-window visited)
1399 (set-buffer visited))
1400 ;; Check relation of buffer and file, and make sure
1401 ;; user knows what he's doing. First, finding the file
1402 ;; will check whether the file on disk is newer.
1403 ;; Ignore buffer-read-only during this test, and
1404 ;; preserve find-file-literally.
1405 (let ((buffer-read-only (not (file-writable-p file))))
1406 (find-file-noselect file nil find-file-literally))
1407 (if (not (verify-visited-file-modtime (current-buffer)))
1408 (if (yes-or-no-p (format "Replace %s on disk with buffer contents? " file))
1409 (write-file buffer-file-name)
1410 (error "Aborted"))
1411 ;; Now, check if we have unsaved changes.
1412 (vc-buffer-sync t)
1413 (if (buffer-modified-p)
1414 (or (y-or-n-p (message "Use %s on disk, keeping modified buffer? " file))
1415 (error "Aborted")))))))
1416 ;; Do the right thing
1417 (cond
1418 ;; Files aren't registered
1419 ((not state)
1420 (mapc 'vc-register files))
1421 ;; Files are up-to-date, or need a merge and user specified a revision
1422 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-patch)))
1423 (cond
1424 (verbose
1425 ;; go to a different revision
1426 (setq revision (read-string "Branch, revision, or backend to move to: "))
1427 (let ((vsym (intern-soft (upcase revision))))
1428 (if (member vsym vc-handled-backends)
1429 (dolist (file files) (vc-transfer-file file vsym))
1430 (dolist (file files)
1431 (vc-checkout file (eq model 'implicit) revision)))))
1432 ((not (eq model 'implicit))
1433 ;; check the files out
1434 (dolist (file files) (vc-checkout file t)))
1436 ;; do nothing
1437 (message "Fileset is up-to-date"))))
1438 ;; Files have local changes
1439 ((eq state 'edited)
1440 (let ((ready-for-commit files))
1441 ;; If files are edited but read-only, give user a chance to correct
1442 (dolist (file files)
1443 (if (not (file-writable-p file))
1444 (progn
1445 ;; Make the file+buffer read-write.
1446 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1447 (error "Aborted"))
1448 (set-file-modes file (logior (file-modes file) 128))
1449 (let ((visited (get-file-buffer file)))
1450 (if visited
1451 (with-current-buffer visited
1452 (toggle-read-only -1)))))))
1453 ;; Allow user to revert files with no changes
1454 (save-excursion
1455 (dolist (file files)
1456 (let ((visited (get-file-buffer file)))
1457 ;; For files with locking, if the file does not contain
1458 ;; any changes, just let go of the lock, i.e. revert.
1459 (if (and (not (eq model 'implicit))
1460 (vc-workfile-unchanged-p file)
1461 ;; If buffer is modified, that means the user just
1462 ;; said no to saving it; in that case, don't revert,
1463 ;; because the user might intend to save after
1464 ;; finishing the log entry and committing.
1465 (not (and visited (buffer-modified-p))))
1466 (progn
1467 (vc-revert-file file)
1468 (delete file ready-for-commit))))))
1469 ;; Remaining files need to be committed
1470 (if (not ready-for-commit)
1471 (message "No files remain to be committed")
1472 (if (not verbose)
1473 (vc-checkin ready-for-commit)
1474 (progn
1475 (setq revision (read-string "New revision or backend: "))
1476 (let ((vsym (intern (upcase revision))))
1477 (if (member vsym vc-handled-backends)
1478 (vc-transfer-file file vsym)
1479 (vc-checkin ready-for-commit revision))))))))
1480 ;; locked by somebody else (locking VCSes only)
1481 ((stringp state)
1482 (let ((revision
1483 (if verbose
1484 (read-string "Revision to steal: ")
1485 (vc-working-revision file))))
1486 (dolist (file files) (vc-steal-lock file revision state))))
1487 ;; needs-patch
1488 ((eq state 'needs-patch)
1489 (dolist (file files)
1490 (if (yes-or-no-p (format
1491 "%s is not up-to-date. Get latest revision? "
1492 (file-name-nondirectory file)))
1493 (vc-checkout file (eq model 'implicit) t)
1494 (if (and (not (eq model 'implicit))
1495 (yes-or-no-p "Lock this revision? "))
1496 (vc-checkout file t)))))
1497 ;; needs-merge
1498 ((eq state 'needs-merge)
1499 (dolist (file files)
1500 (if (yes-or-no-p (format
1501 "%s is not up-to-date. Merge in changes now? "
1502 (file-name-nondirectory file)))
1503 (vc-maybe-resolve-conflicts file (vc-call merge-news file)))))
1505 ;; unlocked-changes
1506 ((eq state 'unlocked-changes)
1507 (dolist (file files)
1508 (if (not (equal buffer-file-name file))
1509 (find-file-other-window file))
1510 (if (save-window-excursion
1511 (vc-diff-internal nil (list file) (vc-working-revision file) nil)
1512 (goto-char (point-min))
1513 (let ((inhibit-read-only t))
1514 (insert
1515 (format "Changes to %s since last lock:\n\n" file)))
1516 (not (beep))
1517 (yes-or-no-p (concat "File has unlocked changes. "
1518 "Claim lock retaining changes? ")))
1519 (progn (vc-call steal-lock file)
1520 (clear-visited-file-modtime)
1521 ;; Must clear any headers here because they wouldn't
1522 ;; show that the file is locked now.
1523 (vc-clear-headers file)
1524 (write-file buffer-file-name)
1525 (vc-mode-line file))
1526 (if (not (yes-or-no-p
1527 "Revert to checked-in revision, instead? "))
1528 (error "Checkout aborted")
1529 (vc-revert-buffer-internal t t)
1530 (vc-checkout file t))))))))
1532 (defun vc-create-repo (backend)
1533 "Create an empty repository in the current directory."
1534 (interactive
1535 (list
1536 (intern
1537 (upcase
1538 (completing-read
1539 "Create repository for: "
1540 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1541 nil t)))))
1542 (vc-call-backend backend 'create-repo))
1544 ;;;###autoload
1545 (defun vc-register (&optional fname set-revision comment)
1546 "Register into a version control system.
1547 If FNAME is given register that file, otherwise register the current file.
1548 With prefix argument SET-REVISION, allow user to specify initial revision
1549 level. If COMMENT is present, use that as an initial comment.
1551 The version control system to use is found by cycling through the list
1552 `vc-handled-backends'. The first backend in that list which declares
1553 itself responsible for the file (usually because other files in that
1554 directory are already registered under that backend) will be used to
1555 register the file. If no backend declares itself responsible, the
1556 first backend that could register the file is used."
1557 (interactive "P")
1558 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1560 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1561 (unless fname (setq fname buffer-file-name))
1562 (when (vc-backend fname)
1563 (if (vc-registered fname)
1564 (error "This file is already registered")
1565 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1566 (error "Aborted"))))
1567 ;; Watch out for new buffers of size 0: the corresponding file
1568 ;; does not exist yet, even though buffer-modified-p is nil.
1569 (when bname
1570 (with-current-buffer bname
1571 (if (and (not (buffer-modified-p))
1572 (zerop (buffer-size))
1573 (not (file-exists-p buffer-file-name)))
1574 (set-buffer-modified-p t))
1575 (vc-buffer-sync)))
1576 (vc-start-entry (list fname)
1577 (if set-revision
1578 (read-string (format "Initial revision level for %s: "
1579 fname))
1580 (vc-call-backend (vc-responsible-backend fname)
1581 'init-revision))
1582 (or comment (not vc-initial-comment))
1584 "Enter initial comment."
1585 (lambda (files rev comment)
1586 (dolist (file files)
1587 (message "Registering %s... " file)
1588 (let ((backend (vc-responsible-backend file t)))
1589 (vc-file-clearprops file)
1590 (vc-call-backend backend 'register (list file) rev comment)
1591 (vc-file-setprop file 'vc-backend backend)
1592 (unless vc-make-backup-files
1593 (make-local-variable 'backup-inhibited)
1594 (setq backup-inhibited t)))
1595 (message "Registering %s... done" file))))))
1597 (defun vc-register-with (backend)
1598 "Register the current file with a specified back end."
1599 (interactive "SBackend: ")
1600 (if (not (member backend vc-handled-backends))
1601 (error "Unknown back end."))
1602 (let ((vc-handled-backends (list backend)))
1603 (call-interactively 'vc-register)))
1605 (declare-function view-mode-exit "view" (&optional return-to-alist exit-action all-win))
1607 (defun vc-resynch-window (file &optional keep noquery)
1608 "If FILE is in the current buffer, either revert or unvisit it.
1609 The choice between revert (to see expanded keywords) and unvisit depends on
1610 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1611 reverting. NOQUERY should be t *only* if it is known the only
1612 difference between the buffer and the file is due to version control
1613 rather than user editing!"
1614 (and (string= buffer-file-name file)
1615 (if keep
1616 (progn
1617 (vc-revert-buffer-internal t noquery)
1618 ;; TODO: Adjusting view mode might no longer be necessary
1619 ;; after RMS change to files.el of 1999-08-08. Investigate
1620 ;; this when we install the new VC.
1621 (and view-read-only
1622 (if (file-writable-p file)
1623 (and view-mode
1624 (let ((view-old-buffer-read-only nil))
1625 (view-mode-exit)))
1626 (and (not view-mode)
1627 (not (eq (get major-mode 'mode-class) 'special))
1628 (view-mode-enter))))
1629 (vc-mode-line buffer-file-name))
1630 (kill-buffer (current-buffer)))))
1632 (defun vc-resynch-buffer (file &optional keep noquery)
1633 "If FILE is currently visited, resynch its buffer."
1634 (if (string= buffer-file-name file)
1635 (vc-resynch-window file keep noquery)
1636 (let ((buffer (get-file-buffer file)))
1637 (if buffer
1638 (with-current-buffer buffer
1639 (vc-resynch-window file keep noquery)))))
1640 (vc-dired-resynch-file file))
1642 (defun vc-start-entry (files rev comment initial-contents msg action &optional after-hook)
1643 "Accept a comment for an operation on FILES revision REV.
1644 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1645 action on close to ACTION. If COMMENT is a string and
1646 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1647 contents of the log entry buffer. If COMMENT is a string and
1648 INITIAL-CONTENTS is nil, do action immediately as if the user had
1649 entered COMMENT. If COMMENT is t, also do action immediately with an
1650 empty comment. Remember the file's buffer in `vc-parent-buffer'
1651 \(current one if no file). AFTER-HOOK specifies the local value
1652 for vc-log-operation-hook."
1653 (let ((parent
1654 (if (eq major-mode 'vc-dired-mode)
1655 ;; If we are called from VC dired, the parent buffer is
1656 ;; the current buffer.
1657 (current-buffer)
1658 (if (and files (equal (length files) 1))
1659 (get-file-buffer (car files))
1660 (current-buffer)))))
1661 (when vc-before-checkin-hook
1662 (if files
1663 (with-current-buffer parent
1664 (run-hooks 'vc-before-checkin-hook))
1665 (run-hooks 'vc-before-checkin-hook)))
1666 (if (and comment (not initial-contents))
1667 (set-buffer (get-buffer-create "*VC-log*"))
1668 (pop-to-buffer (get-buffer-create "*VC-log*")))
1669 (set (make-local-variable 'vc-parent-buffer) parent)
1670 (set (make-local-variable 'vc-parent-buffer-name)
1671 (concat " from " (buffer-name vc-parent-buffer)))
1672 ;;(if file (vc-mode-line file))
1673 (vc-log-edit files)
1674 (make-local-variable 'vc-log-after-operation-hook)
1675 (when after-hook
1676 (setq vc-log-after-operation-hook after-hook))
1677 (setq vc-log-operation action)
1678 (setq vc-log-revision rev)
1679 (when comment
1680 (erase-buffer)
1681 (when (stringp comment) (insert comment)))
1682 (if (or (not comment) initial-contents)
1683 (message "%s Type C-c C-c when done" msg)
1684 (vc-finish-logentry (eq comment t)))))
1686 (defun vc-checkout (file &optional writable rev)
1687 "Retrieve a copy of the revision REV of FILE.
1688 If WRITABLE is non-nil, make sure the retrieved file is writable.
1689 REV defaults to the latest revision.
1691 After check-out, runs the normal hook `vc-checkout-hook'."
1692 (and writable
1693 (not rev)
1694 (vc-call make-version-backups-p file)
1695 (vc-up-to-date-p file)
1696 (vc-make-version-backup file))
1697 (with-vc-properties
1698 (list file)
1699 (condition-case err
1700 (vc-call checkout file writable rev)
1701 (file-error
1702 ;; Maybe the backend is not installed ;-(
1703 (when writable
1704 (let ((buf (get-file-buffer file)))
1705 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1706 (signal (car err) (cdr err))))
1707 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1708 (not writable))
1709 (if (vc-call latest-on-branch-p file)
1710 'up-to-date
1711 'needs-patch)
1712 'edited))
1713 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1714 (vc-resynch-buffer file t t)
1715 (run-hooks 'vc-checkout-hook))
1717 (defun vc-steal-lock (file rev owner)
1718 "Steal the lock on FILE."
1719 (let (file-description)
1720 (if rev
1721 (setq file-description (format "%s:%s" file rev))
1722 (setq file-description file))
1723 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1724 file-description owner)))
1725 (error "Steal canceled"))
1726 (message "Stealing lock on %s..." file)
1727 (with-vc-properties
1728 (list file)
1729 (vc-call steal-lock file rev)
1730 `((vc-state . edited)))
1731 (vc-resynch-buffer file t t)
1732 (message "Stealing lock on %s...done" file)
1733 ;; Write mail after actually stealing, because if the stealing
1734 ;; goes wrong, we don't want to send any mail.
1735 (compose-mail owner (format "Stolen lock on %s" file-description))
1736 (setq default-directory (expand-file-name "~/"))
1737 (goto-char (point-max))
1738 (insert
1739 (format "I stole the lock on %s, " file-description)
1740 (current-time-string)
1741 ".\n")
1742 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1744 (defun vc-checkin (files &optional rev comment initial-contents)
1745 "Check in FILES.
1746 The optional argument REV may be a string specifying the new revision
1747 level (if nil increment the current level). COMMENT is a comment
1748 string; if omitted, a buffer is popped up to accept a comment. If
1749 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1750 of the log entry buffer.
1752 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1753 that the version control system supports this mode of operation.
1755 Runs the normal hook `vc-checkin-hook'."
1756 (vc-start-entry
1757 files rev comment initial-contents
1758 "Enter a change comment."
1759 (lambda (files rev comment)
1760 (message "Checking in %s..." (vc-delistify files))
1761 ;; "This log message intentionally left almost blank".
1762 ;; RCS 5.7 gripes about white-space-only comments too.
1763 (or (and comment (string-match "[^\t\n ]" comment))
1764 (setq comment "*** empty log message ***"))
1765 (with-vc-properties
1766 files
1767 ;; We used to change buffers to get local value of vc-checkin-switches,
1768 ;; but 'the' local buffer is not a well-defined concept for filesets.
1769 (progn
1770 (vc-call checkin files rev comment)
1771 (mapc 'vc-delete-automatic-version-backups files))
1772 `((vc-state . up-to-date)
1773 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1774 (vc-working-revision . nil)))
1775 (message "Checking in %s...done" (vc-delistify files)))
1776 'vc-checkin-hook))
1778 (defun vc-finish-logentry (&optional nocomment)
1779 "Complete the operation implied by the current log entry.
1780 Use the contents of the current buffer as a check-in or registration
1781 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1782 the buffer contents as a comment."
1783 (interactive)
1784 ;; Check and record the comment, if any.
1785 (unless nocomment
1786 ;; Comment too long?
1787 (vc-call-backend (or (if vc-log-fileset (vc-backend vc-log-fileset))
1788 (vc-responsible-backend default-directory))
1789 'logentry-check)
1790 (run-hooks 'vc-logentry-check-hook))
1791 ;; Sync parent buffer in case the user modified it while editing the comment.
1792 ;; But not if it is a vc-dired buffer.
1793 (with-current-buffer vc-parent-buffer
1794 (or vc-dired-mode (vc-buffer-sync)))
1795 (if (not vc-log-operation)
1796 (error "No log operation is pending"))
1797 ;; save the parameters held in buffer-local variables
1798 (let ((log-operation vc-log-operation)
1799 (log-fileset vc-log-fileset)
1800 (log-revision vc-log-revision)
1801 (log-entry (buffer-string))
1802 (after-hook vc-log-after-operation-hook)
1803 (tmp-vc-parent-buffer vc-parent-buffer))
1804 (pop-to-buffer vc-parent-buffer)
1805 ;; OK, do it to it
1806 (save-excursion
1807 (funcall log-operation
1808 log-fileset
1809 log-revision
1810 log-entry))
1811 ;; Remove checkin window (after the checkin so that if that fails
1812 ;; we don't zap the *VC-log* buffer and the typing therein).
1813 (let ((logbuf (get-buffer "*VC-log*")))
1814 (cond ((and logbuf vc-delete-logbuf-window)
1815 (delete-windows-on logbuf (selected-frame))
1816 ;; Kill buffer and delete any other dedicated windows/frames.
1817 (kill-buffer logbuf))
1818 (logbuf (pop-to-buffer "*VC-log*")
1819 (bury-buffer)
1820 (pop-to-buffer tmp-vc-parent-buffer))))
1821 ;; Now make sure we see the expanded headers
1822 (if log-fileset
1823 (mapc
1824 (lambda (file) (vc-resynch-buffer file vc-keep-workfiles t))
1825 log-fileset))
1826 (if vc-dired-mode
1827 (dired-move-to-filename))
1828 (run-hooks after-hook 'vc-finish-logentry-hook)))
1830 ;;; Additional entry points for examining version histories
1832 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1833 ;; "List differences for all registered files at and below DIR.
1834 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1835 ;; ;; This implementation does an explicit tree walk, and calls
1836 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1837 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1838 ;; ;; and to call it only for files that are actually changed.
1839 ;; ;; However, this is expensive for some backends, and so it is left
1840 ;; ;; to backend-specific implementations.
1841 ;; (setq default-directory dir)
1842 ;; (vc-file-tree-walk
1843 ;; default-directory
1844 ;; (lambda (f)
1845 ;; (vc-exec-after
1846 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1847 ;; (message "Looking at %s" ',f)
1848 ;; (vc-call-backend ',(vc-backend f)
1849 ;; 'diff (list ',f) ',rev1 ',rev2))))))
1851 (defun vc-coding-system-for-diff (file)
1852 "Return the coding system for reading diff output for FILE."
1853 (or coding-system-for-read
1854 ;; if we already have this file open,
1855 ;; use the buffer's coding system
1856 (let ((buf (find-buffer-visiting file)))
1857 (if buf (with-current-buffer buf
1858 buffer-file-coding-system)))
1859 ;; otherwise, try to find one based on the file name
1860 (car (find-operation-coding-system 'insert-file-contents file))
1861 ;; and a final fallback
1862 'undecided))
1864 (defun vc-switches (backend op)
1865 (let ((switches
1866 (or (if backend
1867 (let ((sym (vc-make-backend-sym
1868 backend (intern (concat (symbol-name op)
1869 "-switches")))))
1870 (if (boundp sym) (symbol-value sym))))
1871 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1872 (if (boundp sym) (symbol-value sym)))
1873 (cond
1874 ((eq op 'diff) diff-switches)))))
1875 (if (stringp switches) (list switches)
1876 ;; If not a list, return nil.
1877 ;; This is so we can set vc-diff-switches to t to override
1878 ;; any switches in diff-switches.
1879 (if (listp switches) switches))))
1881 ;; Old def for compatibility with Emacs-21.[123].
1882 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1883 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
1885 (defun vc-diff-sentinel (verbose rev1-name rev2-name)
1886 ;; The empty sync output case has already been handled, so the only
1887 ;; possibility of an empty output is for an async process, in which case
1888 ;; it's important to insert the "diffs end here" message in the buffer
1889 ;; since the user may miss a message in the echo area.
1890 (when verbose
1891 (let ((inhibit-read-only t))
1892 (if (eq (buffer-size) 0)
1893 (insert "No differences found.\n")
1894 (insert (format "\n\nDiffs between %s and %s end here." rev1-name rev2-name)))))
1895 (goto-char (point-min))
1896 (shrink-window-if-larger-than-buffer))
1898 (defvar vc-diff-added-files nil
1899 "If non-nil, diff added files by comparing them to /dev/null.")
1901 (defun vc-diff-internal (async files rev1 rev2 &optional verbose)
1902 "Report diffs between two revisions of a fileset.
1903 Diff output goes to the *vc-diff* buffer. The function
1904 returns t if the buffer had changes, nil otherwise."
1905 (let* ((filenames (vc-delistify files))
1906 (rev1-name (or rev1 "working revision"))
1907 (rev2-name (or rev2 "workfile"))
1908 ;; Set coding system based on the first file. It's a kluge,
1909 ;; but the only way to set it for each file included would
1910 ;; be to call the back end separately for each file.
1911 (coding-system-for-read
1912 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
1913 (vc-setup-buffer "*vc-diff*")
1914 (message "Finding changes in %s..." filenames)
1915 ;; Many backends don't handle well the case of a file that has been
1916 ;; added but not yet committed to the repo (notably CVS and Subversion).
1917 ;; Do that work here so the backends don't have to futz with it. --ESR
1919 ;; Actually most backends (including CVS) have options to control the
1920 ;; behavior since which one is better depends on the user and on the
1921 ;; situation). Worse yet: this code does not handle the case where
1922 ;; `file' is a directory which contains added files.
1923 ;; I made it conditional on vc-diff-added-files but it should probably
1924 ;; just be removed (or copied/moved to specific backends). --Stef.
1925 (when vc-diff-added-files
1926 (let ((filtered '()))
1927 (dolist (file files)
1928 (if (or (file-directory-p file)
1929 (not (string= (vc-working-revision file) "0")))
1930 (push file filtered)
1931 ;; This file is added but not yet committed;
1932 ;; there is no master file to diff against.
1933 (if (or rev1 rev2)
1934 (error "No revisions of %s exist" file)
1935 ;; We regard this as "changed".
1936 ;; Diff it against /dev/null.
1937 (apply 'vc-do-command "*vc-diff*"
1938 1 "diff" file
1939 (append (vc-switches nil 'diff) '("/dev/null"))))))
1940 (setq files (nreverse filtered))))
1941 (let ((vc-disable-async-diff (not async)))
1942 (vc-call diff files rev1 rev2 "*vc-diff*"))
1943 (set-buffer "*vc-diff*")
1944 (if (and (zerop (buffer-size))
1945 (not (get-buffer-process (current-buffer))))
1946 ;; Treat this case specially so as not to pop the buffer.
1947 (progn
1948 (message "No changes between %s and %s" rev1-name rev2-name)
1949 nil)
1950 (diff-mode)
1951 ;; Make the *vc-diff* buffer read only, the diff-mode key
1952 ;; bindings are nicer for read only buffers. pcl-cvs does the
1953 ;; same thing.
1954 (setq buffer-read-only t)
1955 (vc-exec-after `(vc-diff-sentinel ,verbose ,rev1-name ,rev2-name))
1956 ;; Display the buffer, but at the end because it can change point.
1957 (pop-to-buffer (current-buffer))
1958 ;; In the async case, we return t even if there are no differences
1959 ;; because we don't know that yet.
1960 t)))
1962 ;;;###autoload
1963 (defun vc-version-diff (files rev1 rev2)
1964 "Report diffs between revisions of the fileset in the repository history."
1965 (interactive
1966 (let* ((files (vc-deduce-fileset t))
1967 (first (car files))
1968 (completion-table
1969 (vc-call revision-completion-table files))
1970 (rev1-default nil)
1971 (rev2-default nil))
1972 (cond
1973 ;; someday we may be able to do revision completion on non-singleton
1974 ;; filesets, but not yet.
1975 ((/= (length files) 1)
1976 nil)
1977 ;; if it's a directory, don't supply any revision default
1978 ((file-directory-p first)
1979 nil)
1980 ;; if the file is not up-to-date, use working revision as older revision
1981 ((not (vc-up-to-date-p first))
1982 (setq rev1-default (vc-working-revision first)))
1983 ;; if the file is not locked, use last and previous revisions as defaults
1985 (setq rev1-default (vc-call previous-revision first
1986 (vc-working-revision first)))
1987 (if (string= rev1-default "") (setq rev1-default nil))
1988 (setq rev2-default (vc-working-revision first))))
1989 ;; construct argument list
1990 (let* ((rev1-prompt (if rev1-default
1991 (concat "Older revision (default "
1992 rev1-default "): ")
1993 "Older revision: "))
1994 (rev2-prompt (concat "Newer revision (default "
1995 (or rev2-default "current source") "): "))
1996 (rev1 (if completion-table
1997 (completing-read rev1-prompt completion-table
1998 nil nil nil nil rev1-default)
1999 (read-string rev1-prompt nil nil rev1-default)))
2000 (rev2 (if completion-table
2001 (completing-read rev2-prompt completion-table
2002 nil nil nil nil rev2-default)
2003 (read-string rev2-prompt nil nil rev2-default))))
2004 (if (string= rev1 "") (setq rev1 nil))
2005 (if (string= rev2 "") (setq rev2 nil))
2006 (list files rev1 rev2))))
2007 (if (and (not rev1) rev2)
2008 (error "Not a valid revision range."))
2009 (vc-diff-internal t files rev1 rev2 (interactive-p)))
2011 ;; (defun vc-contains-version-controlled-file (dir)
2012 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
2013 ;; (catch 'found
2014 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
2015 ;; nil))
2017 ;;;###autoload
2018 (defun vc-diff (historic &optional not-urgent)
2019 "Display diffs between file revisions.
2020 Normally this compares the currently selected fileset with their
2021 working revisions. With a prefix argument HISTORIC, it reads two revision
2022 designators specifying which revisions to compare.
2024 If no current fileset is available (that is, we are not in
2025 VC-Dired mode and the visited file of the current buffer is not
2026 under version control) and we're in a Dired buffer, use
2027 the current directory.
2028 The optional argument NOT-URGENT non-nil means it is ok to say no to
2029 saving the buffer."
2030 (interactive (list current-prefix-arg t))
2031 (if historic
2032 (call-interactively 'vc-version-diff)
2033 (let* ((files (vc-deduce-fileset t)))
2034 (if buffer-file-name (vc-buffer-sync not-urgent))
2035 (vc-diff-internal t files nil nil (interactive-p)))))
2038 ;;;###autoload
2039 (defun vc-revision-other-window (rev)
2040 "Visit revision REV of the current file in another window.
2041 If the current file is named `F', the revision is named `F.~REV~'.
2042 If `F.~REV~' already exists, use it instead of checking it out again."
2043 (interactive
2044 (save-current-buffer
2045 (vc-ensure-vc-buffer)
2046 (let ((completion-table
2047 (vc-call revision-completion-table buffer-file-name))
2048 (prompt "Revision to visit (default is working revision): "))
2049 (list
2050 (if completion-table
2051 (completing-read prompt completion-table)
2052 (read-string prompt))))))
2053 (vc-ensure-vc-buffer)
2054 (let* ((file buffer-file-name)
2055 (revision (if (string-equal rev "")
2056 (vc-working-revision file)
2057 rev)))
2058 (switch-to-buffer-other-window (vc-find-revision file revision))))
2060 (defun vc-find-revision (file revision)
2061 "Read REVISION of FILE into a buffer and return the buffer."
2062 (let ((automatic-backup (vc-version-backup-file-name file revision))
2063 (filebuf (or (get-file-buffer file) (current-buffer)))
2064 (filename (vc-version-backup-file-name file revision 'manual)))
2065 (unless (file-exists-p filename)
2066 (if (file-exists-p automatic-backup)
2067 (rename-file automatic-backup filename nil)
2068 (message "Checking out %s..." filename)
2069 (with-current-buffer filebuf
2070 (let ((failed t))
2071 (unwind-protect
2072 (let ((coding-system-for-read 'no-conversion)
2073 (coding-system-for-write 'no-conversion))
2074 (with-temp-file filename
2075 (let ((outbuf (current-buffer)))
2076 ;; Change buffer to get local value of
2077 ;; vc-checkout-switches.
2078 (with-current-buffer filebuf
2079 (vc-call find-revision file revision outbuf))))
2080 (setq failed nil))
2081 (when (and failed (file-exists-p filename))
2082 (delete-file filename))))
2083 (vc-mode-line file))
2084 (message "Checking out %s...done" filename)))
2085 (let ((result-buf (find-file-noselect filename)))
2086 (with-current-buffer result-buf
2087 ;; Set the parent buffer so that things like
2088 ;; C-x v g, C-x v l, ... etc work.
2089 (setq vc-parent-buffer filebuf))
2090 result-buf)))
2092 ;; Header-insertion code
2094 ;;;###autoload
2095 (defun vc-insert-headers ()
2096 "Insert headers into a file for use with a version control system.
2097 Headers desired are inserted at point, and are pulled from
2098 the variable `vc-BACKEND-header'."
2099 (interactive)
2100 (vc-ensure-vc-buffer)
2101 (save-excursion
2102 (save-restriction
2103 (widen)
2104 (if (or (not (vc-check-headers))
2105 (y-or-n-p "Version headers already exist. Insert another set? "))
2106 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
2107 (comment-start-vc (or (car delims) comment-start "#"))
2108 (comment-end-vc (or (car (cdr delims)) comment-end ""))
2109 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
2110 'header))
2111 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
2112 (dolist (s hdstrings)
2113 (insert comment-start-vc "\t" s "\t"
2114 comment-end-vc "\n"))
2115 (if vc-static-header-alist
2116 (dolist (f vc-static-header-alist)
2117 (if (string-match (car f) buffer-file-name)
2118 (insert (format (cdr f) (car hdstrings)))))))))))
2120 (defun vc-clear-headers (&optional file)
2121 "Clear all version headers in the current buffer (or FILE).
2122 The headers are reset to their non-expanded form."
2123 (let* ((filename (or file buffer-file-name))
2124 (visited (find-buffer-visiting filename))
2125 (backend (vc-backend filename)))
2126 (when (vc-find-backend-function backend 'clear-headers)
2127 (if visited
2128 (let ((context (vc-buffer-context)))
2129 ;; save-excursion may be able to relocate point and mark
2130 ;; properly. If it fails, vc-restore-buffer-context
2131 ;; will give it a second try.
2132 (save-excursion
2133 (vc-call-backend backend 'clear-headers))
2134 (vc-restore-buffer-context context))
2135 (set-buffer (find-file-noselect filename))
2136 (vc-call-backend backend 'clear-headers)
2137 (kill-buffer filename)))))
2139 (defun vc-modify-change-comment (files rev oldcomment)
2140 "Edit the comment associated with the given files and revision."
2141 (vc-start-entry
2142 files rev oldcomment t
2143 "Enter a replacement change comment."
2144 (lambda (files rev comment)
2145 (vc-call-backend
2146 ;; Less of a kluge than it looks like; log-view mode only passes
2147 ;; this function a singleton list. Arguments left in this form in
2148 ;; case the more general operation ever becomes meaningful.
2149 (vc-responsible-backend (car files))
2150 'modify-change-comment files rev comment))))
2152 ;;;###autoload
2153 (defun vc-merge ()
2154 "Merge changes between two revisions into the current buffer's file.
2155 This asks for two revisions to merge from in the minibuffer. If the
2156 first revision is a branch number, then merge all changes from that
2157 branch. If the first revision is empty, merge news, i.e. recent changes
2158 from the current branch.
2160 See Info node `Merging'."
2161 (interactive)
2162 (vc-ensure-vc-buffer)
2163 (vc-buffer-sync)
2164 (let* ((file buffer-file-name)
2165 (backend (vc-backend file))
2166 (state (vc-state file))
2167 first-revision second-revision status)
2168 (cond
2169 ((stringp state) ;; Locking VCses only
2170 (error "File is locked by %s" state))
2171 ((not (vc-editable-p file))
2172 (if (y-or-n-p
2173 "File must be checked out for merging. Check out now? ")
2174 (vc-checkout file t)
2175 (error "Merge aborted"))))
2176 (setq first-revision
2177 (read-string (concat "Branch or revision to merge from "
2178 "(default news on current branch): ")))
2179 (if (string= first-revision "")
2180 (if (not (vc-find-backend-function backend 'merge-news))
2181 (error "Sorry, merging news is not implemented for %s" backend)
2182 (setq status (vc-call merge-news file)))
2183 (if (not (vc-find-backend-function backend 'merge))
2184 (error "Sorry, merging is not implemented for %s" backend)
2185 (if (not (vc-branch-p first-revision))
2186 (setq second-revision
2187 (read-string "Second revision: "
2188 (concat (vc-branch-part first-revision) ".")))
2189 ;; We want to merge an entire branch. Set revisions
2190 ;; accordingly, so that vc-BACKEND-merge understands us.
2191 (setq second-revision first-revision)
2192 ;; first-revision must be the starting point of the branch
2193 (setq first-revision (vc-branch-part first-revision)))
2194 (setq status (vc-call merge file first-revision second-revision))))
2195 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
2197 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
2198 (vc-resynch-buffer file t (not (buffer-modified-p)))
2199 (if (zerop status) (message "Merge successful")
2200 (smerge-mode 1)
2201 (message "File contains conflicts.")))
2203 ;;;###autoload
2204 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2206 ;; The VC directory major mode. Coopt Dired for this.
2207 ;; All VC commands get mapped into logical equivalents.
2209 (defvar vc-dired-switches)
2210 (defvar vc-dired-terse-mode)
2212 (defvar vc-dired-mode-map
2213 (let ((map (make-sparse-keymap))
2214 (vmap (make-sparse-keymap)))
2215 (define-key map "\C-xv" vmap)
2216 (define-key map "v" vmap)
2217 (set-keymap-parent vmap vc-prefix-map)
2218 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2219 map))
2221 (define-derived-mode vc-dired-mode dired-mode "Dired under "
2222 "The major mode used in VC directory buffers.
2224 It works like Dired, but lists only files under version control, with
2225 the current VC state of each file being indicated in the place of the
2226 file's link count, owner, group and size. Subdirectories are also
2227 listed, and you may insert them into the buffer as desired, like in
2228 Dired.
2230 All Dired commands operate normally, with the exception of `v', which
2231 is redefined as the version control prefix, so that you can type
2232 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2233 the file named in the current Dired buffer line. `vv' invokes
2234 `vc-next-action' on this file, or on all files currently marked.
2235 There is a special command, `*l', to mark all files currently locked."
2236 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2237 ;; We do it here because dired might not be loaded yet
2238 ;; when vc-dired-mode-map is initialized.
2239 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2240 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2241 ;; The following is slightly modified from files.el,
2242 ;; because file lines look a bit different in vc-dired-mode
2243 ;; (the column before the date does not end in a digit).
2244 ;; albinus: It should be done in the original declaration. Problem
2245 ;; is the optional empty state-info; otherwise ")" would be good
2246 ;; enough as delimeter.
2247 (set (make-local-variable 'directory-listing-before-filename-regexp)
2248 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2249 ;; In some locales, month abbreviations are as short as 2 letters,
2250 ;; and they can be followed by ".".
2251 (month (concat l l "+\\.?"))
2252 (s " ")
2253 (yyyy "[0-9][0-9][0-9][0-9]")
2254 (dd "[ 0-3][0-9]")
2255 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2256 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2257 (zone "[-+][0-2][0-9][0-5][0-9]")
2258 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2259 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2260 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2261 "\\|" yyyy "-" iso-mm-dd "\\)"))
2262 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2263 s "+"
2264 "\\(" HH:MM "\\|" yyyy "\\)"))
2265 (western-comma (concat month s "+" dd "," s "+" yyyy))
2266 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2267 ;; omits the Kanji characters after month and day-of-month.
2268 (mm "[ 0-1]?[0-9]")
2269 (japanese
2270 (concat mm l "?" s dd l "?" s "+"
2271 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2272 ;; the .* below ensures that we find the last match on a line
2273 (concat ".*" s
2274 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2275 s "+")))
2276 (and (boundp 'vc-dired-switches)
2277 vc-dired-switches
2278 (set (make-local-variable 'dired-actual-switches)
2279 vc-dired-switches))
2280 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2281 (let ((backend-name (symbol-name (vc-responsible-backend
2282 default-directory))))
2283 (setq mode-name (concat mode-name backend-name))
2284 ;; Add menu after `vc-dired-mode-map' has `dired-mode-map' as the parent.
2285 (let ((vc-dire-menu-map (copy-keymap vc-menu-map)))
2286 (define-key-after (lookup-key vc-dired-mode-map [menu-bar]) [vc]
2287 (cons backend-name vc-dire-menu-map) 'subdir)))
2288 (setq vc-dired-mode t))
2290 (defun vc-dired-toggle-terse-mode ()
2291 "Toggle terse display in VC Dired."
2292 (interactive)
2293 (if (not vc-dired-mode)
2295 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2296 (if vc-dired-terse-mode
2297 (vc-dired-hook)
2298 (revert-buffer))))
2300 (defun vc-dired-mark-locked ()
2301 "Mark all files currently locked."
2302 (interactive)
2303 (dired-mark-if (let ((f (dired-get-filename nil t)))
2304 (and f
2305 (not (file-directory-p f))
2306 (not (vc-up-to-date-p f))))
2307 "locked file"))
2309 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2311 (defun vc-dired-reformat-line (vc-info)
2312 "Reformat a directory-listing line.
2313 Replace various columns with version control information, VC-INFO.
2314 This code, like dired, assumes UNIX -l format."
2315 (beginning-of-line)
2316 (when (re-search-forward
2317 ;; Match link count, owner, group, size. Group may be missing,
2318 ;; and only the size is present in OS/2 -l format.
2319 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2320 (line-end-position) t)
2321 (replace-match (substring (concat vc-info " ") 0 10)
2322 t t nil 1)))
2324 (defun vc-dired-ignorable-p (filename)
2325 "Should FILENAME be ignored in VC-Dired listings?"
2326 (catch t
2327 ;; Ignore anything that wouldn't be found by completion (.o, .la, etc.)
2328 (dolist (ignorable completion-ignored-extensions)
2329 (let ((ext (substring filename
2330 (- (length filename)
2331 (length ignorable)))))
2332 (if (string= ignorable ext) (throw t t))))
2333 ;; Ignore Makefiles derived from something else
2334 (when (string= (file-name-nondirectory filename) "Makefile")
2335 (let* ((dir (file-name-directory filename))
2336 (peers (directory-files (or dir default-directory))))
2337 (if (or (member "Makefile.in" peers) (member "Makefile.am" peers))
2338 (throw t t))))
2339 nil))
2341 (defun vc-dired-hook ()
2342 "Reformat the listing according to version control.
2343 Called by dired after any portion of a vc-dired buffer has been read in."
2344 (message "Getting version information... ")
2345 ;; if the backend supports it, get the state
2346 ;; of all files in this directory at once
2347 (let ((backend (vc-responsible-backend default-directory)))
2348 ;; check `backend' can really handle `default-directory'.
2349 (if (and (vc-call-backend backend 'responsible-p default-directory)
2350 (vc-find-backend-function backend 'dir-state))
2351 (vc-call-backend backend 'dir-state default-directory)))
2352 (let (filename (inhibit-read-only t))
2353 (goto-char (point-min))
2354 (while (not (eobp))
2355 (cond
2356 ;; subdir header line
2357 ((dired-get-subdir)
2358 (forward-line 1)
2359 ;; erase (but don't remove) the "total" line
2360 (delete-region (point) (line-end-position))
2361 (beginning-of-line)
2362 (forward-line 1))
2363 ;; file line
2364 ((setq filename (dired-get-filename nil t))
2365 (cond
2366 ;; subdir
2367 ((file-directory-p filename)
2368 (cond
2369 ((member (file-name-nondirectory filename)
2370 vc-directory-exclusion-list)
2371 (let ((pos (point)))
2372 (dired-kill-tree filename)
2373 (goto-char pos)
2374 (dired-kill-line)))
2375 (vc-dired-terse-mode
2376 ;; Don't show directories in terse mode. Don't use
2377 ;; dired-kill-line to remove it, because in recursive listings,
2378 ;; that would remove the directory contents as well.
2379 (delete-region (line-beginning-position)
2380 (progn (forward-line 1) (point))))
2381 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2382 (dired-kill-line))
2384 (vc-dired-reformat-line nil)
2385 (forward-line 1))))
2386 ;; try to head off calling the expensive state query -
2387 ;; ignore object files, TeX intermediate files, and so forth.
2388 ((vc-dired-ignorable-p filename)
2389 (dired-kill-line))
2390 ;; ordinary file -- call the (possibly expensive) state query
2392 (let ((backend (vc-backend filename)))
2393 (cond
2394 ;; Not registered
2395 ((not backend)
2396 (if vc-dired-terse-mode
2397 (dired-kill-line)
2398 (vc-dired-reformat-line "?")
2399 (forward-line 1)))
2400 ;; Either we're in non-terse mode or it's out of date
2401 ((not (and vc-dired-terse-mode (vc-up-to-date-p filename)))
2402 (vc-dired-reformat-line (vc-call dired-state-info filename))
2403 (forward-line 1))
2404 ;; Remaining cases are under version control but uninteresting
2406 (dired-kill-line)))))))
2407 ;; any other line
2408 (t (forward-line 1))))
2409 (vc-dired-purge))
2410 (message "Getting version information... done")
2411 (save-restriction
2412 (widen)
2413 (cond ((eq (count-lines (point-min) (point-max)) 1)
2414 (goto-char (point-min))
2415 (message "No changes pending under %s" default-directory)))))
2417 (defun vc-dired-purge ()
2418 "Remove empty subdirs."
2419 (goto-char (point-min))
2420 (while (dired-get-subdir)
2421 (forward-line 2)
2422 (if (dired-get-filename nil t)
2423 (if (not (dired-next-subdir 1 t))
2424 (goto-char (point-max)))
2425 (forward-line -2)
2426 (if (not (string= (dired-current-directory) default-directory))
2427 (dired-do-kill-lines t "")
2428 ;; We cannot remove the top level directory.
2429 ;; Just make it look a little nicer.
2430 (forward-line 1)
2431 (or (eobp) (kill-line))
2432 (if (not (dired-next-subdir 1 t))
2433 (goto-char (point-max))))))
2434 (goto-char (point-min)))
2436 (defun vc-dired-buffers-for-dir (dir)
2437 "Return a list of all vc-dired buffers that currently display DIR."
2438 (let (result)
2439 ;; Check whether dired is loaded.
2440 (when (fboundp 'dired-buffers-for-dir)
2441 (dolist (buffer (dired-buffers-for-dir dir))
2442 (with-current-buffer buffer
2443 (if vc-dired-mode
2444 (push buffer result)))))
2445 (nreverse result)))
2447 (defun vc-dired-resynch-file (file)
2448 "Update the entries for FILE in any VC Dired buffers that list it."
2449 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2450 (when buffers
2451 (mapcar (lambda (buffer)
2452 (with-current-buffer buffer
2453 (if (dired-goto-file file)
2454 ;; bind vc-dired-terse-mode to nil so that
2455 ;; files won't vanish when they are checked in
2456 (let ((vc-dired-terse-mode nil))
2457 (dired-do-redisplay 1)))))
2458 buffers))))
2460 ;;;###autoload
2461 (defun vc-directory (dir read-switches)
2462 "Create a buffer in VC Dired Mode for directory DIR.
2464 See Info node `VC Dired Mode'.
2466 With prefix arg READ-SWITCHES, specify a value to override
2467 `dired-listing-switches' when generating the listing."
2468 (interactive "DDired under VC (directory): \nP")
2469 (let ((vc-dired-switches (concat vc-dired-listing-switches
2470 (if vc-dired-recurse "R" ""))))
2471 (if (eq (string-match tramp-file-name-regexp dir) 0)
2472 (error "Sorry, vc-directory does not work over Tramp"))
2473 (if read-switches
2474 (setq vc-dired-switches
2475 (read-string "Dired listing switches: "
2476 vc-dired-switches)))
2477 (require 'dired)
2478 (require 'dired-aux)
2479 (switch-to-buffer
2480 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2481 vc-dired-switches
2482 'vc-dired-mode))))
2485 ;; Named-configuration entry points
2487 (defun vc-snapshot-precondition (dir)
2488 "Scan the tree below DIR, looking for files not up-to-date.
2489 If any file is not up-to-date, return the name of the first such file.
2490 \(This means, neither snapshot creation nor retrieval is allowed.\)
2491 If one or more of the files are currently visited, return `visited'.
2492 Otherwise, return nil."
2493 (let ((status nil))
2494 (catch 'vc-locked-example
2495 (vc-file-tree-walk
2497 (lambda (f)
2498 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2499 (if (get-file-buffer f) (setq status 'visited)))))
2500 status)))
2502 ;;;###autoload
2503 (defun vc-create-snapshot (dir name branchp)
2504 "Descending recursively from DIR, make a snapshot called NAME.
2505 For each registered file, the working revision becomes part of
2506 the named configuration. If the prefix argument BRANCHP is
2507 given, the snapshot is made as a new branch and the files are
2508 checked out in that new branch."
2509 (interactive
2510 (list (read-file-name "Directory: " default-directory default-directory t)
2511 (read-string "New snapshot name: ")
2512 current-prefix-arg))
2513 (message "Making %s... " (if branchp "branch" "snapshot"))
2514 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2515 (vc-call-backend (vc-responsible-backend dir)
2516 'create-snapshot dir name branchp)
2517 (message "Making %s... done" (if branchp "branch" "snapshot")))
2519 ;;;###autoload
2520 (defun vc-retrieve-snapshot (dir name)
2521 "Descending recursively from DIR, retrieve the snapshot called NAME.
2522 If NAME is empty, it refers to the latest revisions.
2523 If locking is used for the files in DIR, then there must not be any
2524 locked files at or below DIR (but if NAME is empty, locked files are
2525 allowed and simply skipped)."
2526 (interactive
2527 (list (read-file-name "Directory: " default-directory default-directory t)
2528 (read-string "Snapshot name to retrieve (default latest revisions): ")))
2529 (let ((update (yes-or-no-p "Update any affected buffers? "))
2530 (msg (if (or (not name) (string= name ""))
2531 (format "Updating %s... " (abbreviate-file-name dir))
2532 (format "Retrieving snapshot into %s... "
2533 (abbreviate-file-name dir)))))
2534 (message "%s" msg)
2535 (vc-call-backend (vc-responsible-backend dir)
2536 'retrieve-snapshot dir name update)
2537 (message "%s" (concat msg "done"))))
2539 ;; Miscellaneous other entry points
2541 ;;;###autoload
2542 (defun vc-print-log (&optional working-revision)
2543 "List the change log of the current fileset in a window.
2544 If WORKING-REVISION is non-nil, leave the point at that revision."
2545 (interactive)
2546 (let* ((files (vc-deduce-fileset))
2547 (backend (vc-backend files))
2548 (working-revision (or working-revision (vc-working-revision (car files)))))
2549 ;; Don't switch to the output buffer before running the command,
2550 ;; so that any buffer-local settings in the vc-controlled
2551 ;; buffer can be accessed by the command.
2552 (vc-call-backend backend 'print-log files "*vc-change-log*")
2553 (pop-to-buffer "*vc-change-log*")
2554 (vc-exec-after
2555 `(let ((inhibit-read-only t))
2556 (vc-call-backend ',backend 'log-view-mode)
2557 (goto-char (point-max)) (forward-line -1)
2558 (while (looking-at "=*\n")
2559 (delete-char (- (match-end 0) (match-beginning 0)))
2560 (forward-line -1))
2561 (goto-char (point-min))
2562 (if (looking-at "[\b\t\n\v\f\r ]+")
2563 (delete-char (- (match-end 0) (match-beginning 0))))
2564 (shrink-window-if-larger-than-buffer)
2565 ;; move point to the log entry for the working revision
2566 (vc-call-backend ',backend 'show-log-entry ',working-revision)
2567 (setq vc-sentinel-movepoint (point))
2568 (set-buffer-modified-p nil)))))
2570 ;;;###autoload
2571 (defun vc-revert ()
2572 "Revert working copies of the selected fileset to their repository contents.
2573 This asks for confirmation if the buffer contents are not identical
2574 to the working revision (except for keyword expansion)."
2575 (interactive)
2576 (let* ((files (vc-deduce-fileset)))
2577 ;; If any of the files is visited by the current buffer, make
2578 ;; sure buffer is saved. If the user says `no', abort since
2579 ;; we cannot show the changes and ask for confirmation to
2580 ;; discard them.
2581 (if (or (not files) (memq (buffer-file-name) files))
2582 (vc-buffer-sync nil))
2583 (dolist (file files)
2584 (let ((buf (get-file-buffer file)))
2585 (if (and buf (buffer-modified-p buf))
2586 (error "Please kill or save all modified buffers before reverting.")))
2587 (if (vc-up-to-date-p file)
2588 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2589 (error "Revert canceled"))))
2590 (if (vc-diff-internal vc-allow-async-revert files nil nil)
2591 (progn
2592 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
2593 (error "Revert canceled"))
2594 (delete-windows-on "*vc-diff*")
2595 (kill-buffer "*vc-diff*")))
2596 (dolist (file files)
2597 (progn
2598 (message "Reverting %s..." (vc-delistify files))
2599 (vc-revert-file file)
2600 (message "Reverting %s...done" (vc-delistify files))))))
2602 ;;;###autoload
2603 (defun vc-rollback ()
2604 "Roll back (remove) the most recent changeset committed to the repository.
2605 This may be either a file-level or a repository-level operation,
2606 depending on the underlying version-control system."
2607 (interactive)
2608 (let* ((files (vc-deduce-fileset))
2609 (backend (vc-backend files))
2610 (granularity (vc-call-backend backend 'revision-granularity)))
2611 (unless (vc-find-backend-function backend 'rollback)
2612 (error "Rollback is not supported in %s" backend))
2613 (if (and (not (eq granularity 'repository)) (/= (length files) 1))
2614 (error "Rollback requires a singleton fileset or repository versioning"))
2615 (if (not (vc-call latest-on-branch-p (car files)))
2616 (error "Rollback is only possible at the tip revision."))
2617 ;; If any of the files is visited by the current buffer, make
2618 ;; sure buffer is saved. If the user says `no', abort since
2619 ;; we cannot show the changes and ask for confirmation to
2620 ;; discard them.
2621 (if (or (not files) (memq (buffer-file-name) files))
2622 (vc-buffer-sync nil))
2623 (dolist (file files)
2624 (if (buffer-modified-p (get-file-buffer file))
2625 (error "Please kill or save all modified buffers before rollback."))
2626 (if (not (vc-up-to-date-p file))
2627 (error "Please revert all modified workfiles before rollback.")))
2628 ;; Accumulate changes associated with the fileset
2629 (vc-setup-buffer "*vc-diff*")
2630 (not-modified)
2631 (message "Finding changes...")
2632 (let* ((tip (vc-working-revision (car files)))
2633 (previous (vc-call previous-revision (car files) tip)))
2634 (vc-diff-internal nil files previous tip))
2635 ;; Display changes
2636 (unless (yes-or-no-p "Discard these revisions? ")
2637 (error "Rollback canceled"))
2638 (delete-windows-on "*vc-diff*")
2639 (kill-buffer"*vc-diff*")
2640 ;; Do the actual reversions
2641 (message "Rolling back %s..." (vc-delistify files))
2642 (with-vc-properties
2643 files
2644 (vc-call-backend backend 'rollback files)
2645 `((vc-state . ,'up-to-date)
2646 (vc-checkout-time . , (nth 5 (file-attributes file)))
2647 (vc-working-revision . nil)))
2648 (dolist (f files) (vc-resynch-buffer f t t))
2649 (message "Rolling back %s...done" (vc-delistify files))))
2651 ;;;###autoload
2652 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2654 ;;;###autoload
2655 (defun vc-update ()
2656 "Update the current fileset's files to their tip revisions.
2657 For each one that contains no changes, and is not locked, then this simply
2658 replaces the work file with the latest revision on its branch. If the file
2659 contains changes, and the backend supports merging news, then any recent
2660 changes from the current branch are merged into the working file."
2661 (interactive)
2662 (dolist (file (vc-deduce-fileset))
2663 (if (buffer-modified-p (get-file-buffer file))
2664 (error "Please kill or save all modified buffers before updating."))
2665 (if (vc-up-to-date-p file)
2666 (vc-checkout file nil "")
2667 (if (eq (vc-checkout-model file) 'locking)
2668 (if (eq (vc-state file) 'edited)
2669 (error "%s"
2670 (substitute-command-keys
2671 "File is locked--type \\[vc-revert] to discard changes"))
2672 (error "Unexpected file state (%s) -- type %s"
2673 (vc-state file)
2674 (substitute-command-keys
2675 "\\[vc-next-action] to correct")))
2676 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
2677 (error "Sorry, merging news is not implemented for %s"
2678 (vc-backend file))
2679 (vc-call merge-news file)
2680 (vc-resynch-buffer file t t))))))
2682 (defun vc-version-backup-file (file &optional rev)
2683 "Return name of backup file for revision REV of FILE.
2684 If version backups should be used for FILE, and there exists
2685 such a backup for REV or the working revision of file, return
2686 its name; otherwise return nil."
2687 (when (vc-call make-version-backups-p file)
2688 (let ((backup-file (vc-version-backup-file-name file rev)))
2689 (if (file-exists-p backup-file)
2690 backup-file
2691 ;; there is no automatic backup, but maybe the user made one manually
2692 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2693 (if (file-exists-p backup-file)
2694 backup-file)))))
2696 (defun vc-revert-file (file)
2697 "Revert FILE back to the repository working revision it was based on."
2698 (with-vc-properties
2699 (list file)
2700 (let ((backup-file (vc-version-backup-file file)))
2701 (when backup-file
2702 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2703 (vc-delete-automatic-version-backups file))
2704 (vc-call revert file backup-file))
2705 `((vc-state . up-to-date)
2706 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2707 (vc-resynch-buffer file t t))
2709 ;;;###autoload
2710 (defun vc-switch-backend (file backend)
2711 "Make BACKEND the current version control system for FILE.
2712 FILE must already be registered in BACKEND. The change is not
2713 permanent, only for the current session. This function only changes
2714 VC's perspective on FILE, it does not register or unregister it.
2715 By default, this command cycles through the registered backends.
2716 To get a prompt, use a prefix argument."
2717 (interactive
2718 (list
2719 (or buffer-file-name
2720 (error "There is no version-controlled file in this buffer"))
2721 (let ((backend (vc-backend buffer-file-name))
2722 (backends nil))
2723 (unless backend
2724 (error "File %s is not under version control" buffer-file-name))
2725 ;; Find the registered backends.
2726 (dolist (backend vc-handled-backends)
2727 (when (vc-call-backend backend 'registered buffer-file-name)
2728 (push backend backends)))
2729 ;; Find the next backend.
2730 (let ((def (car (delq backend (append (memq backend backends) backends))))
2731 (others (delete backend backends)))
2732 (cond
2733 ((null others) (error "No other backend to switch to"))
2734 (current-prefix-arg
2735 (intern
2736 (upcase
2737 (completing-read
2738 (format "Switch to backend [%s]: " def)
2739 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2740 nil t nil nil (downcase (symbol-name def))))))
2741 (t def))))))
2742 (unless (eq backend (vc-backend file))
2743 (vc-file-clearprops file)
2744 (vc-file-setprop file 'vc-backend backend)
2745 ;; Force recomputation of the state
2746 (unless (vc-call-backend backend 'registered file)
2747 (vc-file-clearprops file)
2748 (error "%s is not registered in %s" file backend))
2749 (vc-mode-line file)))
2751 ;;;###autoload
2752 (defun vc-transfer-file (file new-backend)
2753 "Transfer FILE to another version control system NEW-BACKEND.
2754 If NEW-BACKEND has a higher precedence than FILE's current backend
2755 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2756 NEW-BACKEND, using the revision number from the current backend as the
2757 base level. If NEW-BACKEND has a lower precedence than the current
2758 backend, then commit all changes that were made under the current
2759 backend to NEW-BACKEND, and unregister FILE from the current backend.
2760 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2761 (let* ((old-backend (vc-backend file))
2762 (edited (memq (vc-state file) '(edited needs-merge)))
2763 (registered (vc-call-backend new-backend 'registered file))
2764 (move
2765 (and registered ; Never move if not registered in new-backend yet.
2766 ;; move if new-backend comes later in vc-handled-backends
2767 (or (memq new-backend (memq old-backend vc-handled-backends))
2768 (y-or-n-p "Final transfer? "))))
2769 (comment nil))
2770 (if (eq old-backend new-backend)
2771 (error "%s is the current backend of %s" new-backend file))
2772 (if registered
2773 (set-file-modes file (logior (file-modes file) 128))
2774 ;; `registered' might have switched under us.
2775 (vc-switch-backend file old-backend)
2776 (let* ((rev (vc-working-revision file))
2777 (modified-file (and edited (make-temp-file file)))
2778 (unmodified-file (and modified-file (vc-version-backup-file file))))
2779 ;; Go back to the base unmodified file.
2780 (unwind-protect
2781 (progn
2782 (when modified-file
2783 (copy-file file modified-file 'ok-if-already-exists)
2784 ;; If we have a local copy of the unmodified file, handle that
2785 ;; here and not in vc-revert-file because we don't want to
2786 ;; delete that copy -- it is still useful for OLD-BACKEND.
2787 (if unmodified-file
2788 (copy-file unmodified-file file
2789 'ok-if-already-exists 'keep-date)
2790 (if (y-or-n-p "Get base revision from master? ")
2791 (vc-revert-file file))))
2792 (vc-call-backend new-backend 'receive-file file rev))
2793 (when modified-file
2794 (vc-switch-backend file new-backend)
2795 (unless (eq (vc-checkout-model file) 'implicit)
2796 (vc-checkout file t nil))
2797 (rename-file modified-file file 'ok-if-already-exists)
2798 (vc-file-setprop file 'vc-checkout-time nil)))))
2799 (when move
2800 (vc-switch-backend file old-backend)
2801 (setq comment (vc-call comment-history file))
2802 (vc-call unregister file))
2803 (vc-switch-backend file new-backend)
2804 (when (or move edited)
2805 (vc-file-setprop file 'vc-state 'edited)
2806 (vc-mode-line file)
2807 (vc-checkin file nil comment (stringp comment)))))
2809 (defun vc-rename-master (oldmaster newfile templates)
2810 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2811 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2812 (newdir (or (file-name-directory newfile) ""))
2813 (newbase (file-name-nondirectory newfile))
2814 (masters
2815 ;; List of potential master files for `newfile'
2816 (mapcar
2817 (lambda (s) (vc-possible-master s newdir newbase))
2818 templates)))
2819 (if (or (file-symlink-p oldmaster)
2820 (file-symlink-p (file-name-directory oldmaster)))
2821 (error "This is unsafe in the presence of symbolic links"))
2822 (rename-file
2823 oldmaster
2824 (catch 'found
2825 ;; If possible, keep the master file in the same directory.
2826 (dolist (f masters)
2827 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
2828 (throw 'found f)))
2829 ;; If not, just use the first possible place.
2830 (dolist (f masters)
2831 (and f (or (not (setq dir (file-name-directory f)))
2832 (file-directory-p dir))
2833 (throw 'found f)))
2834 (error "New file lacks a version control directory")))))
2836 (defun vc-delete-file (file)
2837 "Delete file and mark it as such in the version control system."
2838 (interactive "fVC delete file: ")
2839 (let ((buf (get-file-buffer file))
2840 (backend (vc-backend file)))
2841 (unless backend
2842 (error "File %s is not under version control"
2843 (file-name-nondirectory file)))
2844 (unless (vc-find-backend-function backend 'delete-file)
2845 (error "Deleting files under %s is not supported in VC" backend))
2846 (if (and buf (buffer-modified-p buf))
2847 (error "Please save files before deleting them"))
2848 (unless (y-or-n-p (format "Really want to delete %s? "
2849 (file-name-nondirectory file)))
2850 (error "Abort!"))
2851 (unless (or (file-directory-p file) (null make-backup-files))
2852 (with-current-buffer (or buf (find-file-noselect file))
2853 (let ((backup-inhibited nil))
2854 (backup-buffer))))
2855 (vc-call delete-file file)
2856 ;; If the backend hasn't deleted the file itself, let's do it for him.
2857 (if (file-exists-p file) (delete-file file))))
2859 ;;;###autoload
2860 (defun vc-rename-file (old new)
2861 "Rename file OLD to NEW, and rename its master file likewise."
2862 (interactive "fVC rename file: \nFRename to: ")
2863 (let ((oldbuf (get-file-buffer old)))
2864 (if (and oldbuf (buffer-modified-p oldbuf))
2865 (error "Please save files before moving them"))
2866 (if (get-file-buffer new)
2867 (error "Already editing new file name"))
2868 (if (file-exists-p new)
2869 (error "New file already exists"))
2870 (let ((state (vc-state old)))
2871 (unless (memq state '(up-to-date edited))
2872 (error "Please %s files before moving them"
2873 (if (stringp state) "check in" "update"))))
2874 (vc-call rename-file old new)
2875 (vc-file-clearprops old)
2876 ;; Move the actual file (unless the backend did it already)
2877 (if (file-exists-p old) (rename-file old new))
2878 ;; ?? Renaming a file might change its contents due to keyword expansion.
2879 ;; We should really check out a new copy if the old copy was precisely equal
2880 ;; to some checked-in revision. However, testing for this is tricky....
2881 (if oldbuf
2882 (with-current-buffer oldbuf
2883 (let ((buffer-read-only buffer-read-only))
2884 (set-visited-file-name new))
2885 (vc-backend new)
2886 (vc-mode-line new)
2887 (set-buffer-modified-p nil)))))
2889 ;;;###autoload
2890 (defun vc-update-change-log (&rest args)
2891 "Find change log file and add entries from recent version control logs.
2892 Normally, find log entries for all registered files in the default
2893 directory.
2895 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2897 With any numeric prefix arg, find log entries for all currently visited
2898 files that are under version control. This puts all the entries in the
2899 log for the default directory, which may not be appropriate.
2901 From a program, any ARGS are assumed to be filenames for which
2902 log entries should be gathered."
2903 (interactive
2904 (cond ((consp current-prefix-arg) ;C-u
2905 (list buffer-file-name))
2906 (current-prefix-arg ;Numeric argument.
2907 (let ((files nil)
2908 (buffers (buffer-list))
2909 file)
2910 (while buffers
2911 (setq file (buffer-file-name (car buffers)))
2912 (and file (vc-backend file)
2913 (setq files (cons file files)))
2914 (setq buffers (cdr buffers)))
2915 files))
2917 ;; Don't supply any filenames to backend; this means
2918 ;; it should find all relevant files relative to
2919 ;; the default-directory.
2920 nil)))
2921 (dolist (file (or args (list default-directory)))
2922 (if (eq (string-match tramp-file-name-regexp file) 0)
2923 (error "Sorry, vc-update-change-log does not work over Tramp")))
2924 (vc-call-backend (vc-responsible-backend default-directory)
2925 'update-changelog args))
2927 ;;; The default back end. Assumes RCS-like revision numbering.
2929 (defun vc-default-revision-granularity ()
2930 (error "Your backend will not work with this version of VC mode."))
2932 ;; functions that operate on RCS revision numbers. This code should
2933 ;; also be moved into the backends. It stays for now, however, since
2934 ;; it is used in code below.
2935 ;;;###autoload
2936 (defun vc-trunk-p (rev)
2937 "Return t if REV is a revision on the trunk."
2938 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
2940 (defun vc-branch-p (rev)
2941 "Return t if REV is a branch revision."
2942 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
2944 ;;;###autoload
2945 (defun vc-branch-part (rev)
2946 "Return the branch part of a revision number REV."
2947 (let ((index (string-match "\\.[0-9]+\\'" rev)))
2948 (if index
2949 (substring rev 0 index))))
2951 (defun vc-minor-part (rev)
2952 "Return the minor revision number of a revision number REV."
2953 (string-match "[0-9]+\\'" rev)
2954 (substring rev (match-beginning 0) (match-end 0)))
2956 (defun vc-default-previous-revision (backend file rev)
2957 "Return the revision number immediately preceding REV for FILE,
2958 or nil if there is no previous revision. This default
2959 implementation works for MAJOR.MINOR-style revision numbers as
2960 used by RCS and CVS."
2961 (let ((branch (vc-branch-part rev))
2962 (minor-num (string-to-number (vc-minor-part rev))))
2963 (when branch
2964 (if (> minor-num 1)
2965 ;; revision does probably not start a branch or release
2966 (concat branch "." (number-to-string (1- minor-num)))
2967 (if (vc-trunk-p rev)
2968 ;; we are at the beginning of the trunk --
2969 ;; don't know anything to return here
2971 ;; we are at the beginning of a branch --
2972 ;; return revision of starting point
2973 (vc-branch-part branch))))))
2975 (defun vc-default-next-revision (backend file rev)
2976 "Return the revision number immediately following REV for FILE,
2977 or nil if there is no next revision. This default implementation
2978 works for MAJOR.MINOR-style revision numbers as used by RCS
2979 and CVS."
2980 (when (not (string= rev (vc-working-revision file)))
2981 (let ((branch (vc-branch-part rev))
2982 (minor-num (string-to-number (vc-minor-part rev))))
2983 (concat branch "." (number-to-string (1+ minor-num))))))
2985 (defun vc-default-responsible-p (backend file)
2986 "Indicate whether BACKEND is reponsible for FILE.
2987 The default is to return nil always."
2988 nil)
2990 (defun vc-default-could-register (backend file)
2991 "Return non-nil if BACKEND could be used to register FILE.
2992 The default implementation returns t for all files."
2995 (defun vc-default-latest-on-branch-p (backend file)
2996 "Return non-nil if FILE is the latest on its branch.
2997 This default implementation always returns non-nil, which means that
2998 editing non-current revisions is not supported by default."
3001 (defun vc-default-init-revision (backend) vc-default-init-revision)
3003 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
3004 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
3005 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
3006 ;; vc-cvs.el by code using cvs2cl.
3007 (defun vc-update-changelog-rcs2log (files)
3008 "Default implementation of update-changelog.
3009 Uses `rcs2log' which only works for RCS and CVS."
3010 ;; FIXME: We (c|sh)ould add support for cvs2cl
3011 (let ((odefault default-directory)
3012 (changelog (find-change-log))
3013 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
3014 (tempfile (make-temp-file
3015 (expand-file-name "vc"
3016 (or small-temporary-file-directory
3017 temporary-file-directory))))
3018 (login-name (or user-login-name
3019 (format "uid%d" (number-to-string (user-uid)))))
3020 (full-name (or add-log-full-name
3021 (user-full-name)
3022 (user-login-name)
3023 (format "uid%d" (number-to-string (user-uid)))))
3024 (mailing-address (or add-log-mailing-address
3025 user-mail-address)))
3026 (find-file-other-window changelog)
3027 (barf-if-buffer-read-only)
3028 (vc-buffer-sync)
3029 (undo-boundary)
3030 (goto-char (point-min))
3031 (push-mark)
3032 (message "Computing change log entries...")
3033 (message "Computing change log entries... %s"
3034 (unwind-protect
3035 (progn
3036 (setq default-directory odefault)
3037 (if (eq 0 (apply 'call-process
3038 (expand-file-name "rcs2log"
3039 exec-directory)
3040 nil (list t tempfile) nil
3041 "-c" changelog
3042 "-u" (concat login-name
3043 "\t" full-name
3044 "\t" mailing-address)
3045 (mapcar
3046 (lambda (f)
3047 (file-relative-name
3048 (expand-file-name f odefault)))
3049 files)))
3050 "done"
3051 (pop-to-buffer (get-buffer-create "*vc*"))
3052 (erase-buffer)
3053 (insert-file-contents tempfile)
3054 "failed"))
3055 (setq default-directory (file-name-directory changelog))
3056 (delete-file tempfile)))))
3058 (defun vc-default-find-revision (backend file rev buffer)
3059 "Provide the new `find-revision' op based on the old `checkout' op.
3060 This is only for compatibility with old backends. They should be updated
3061 to provide the `find-revision' operation instead."
3062 (let ((tmpfile (make-temp-file (expand-file-name file))))
3063 (unwind-protect
3064 (progn
3065 (vc-call-backend backend 'checkout file nil rev tmpfile)
3066 (with-current-buffer buffer
3067 (insert-file-contents-literally tmpfile)))
3068 (delete-file tmpfile))))
3070 (defun vc-default-dired-state-info (backend file)
3071 (let* ((state (vc-state file))
3072 (statestring
3073 (cond
3074 ((stringp state) (concat "(" state ")"))
3075 ((eq state 'edited) (concat "(" (vc-user-login-name file) ")"))
3076 ((eq state 'needs-merge) "(merge)")
3077 ((eq state 'needs-patch) "(patch)")
3078 ((eq state 'added) "(added)")
3079 ((eq state 'unlocked-changes) "(stale)")))
3080 (buffer
3081 (get-file-buffer file))
3082 (modflag
3083 (if (and buffer (buffer-modified-p buffer)) "+" "")))
3084 (concat statestring modflag)))
3086 (defun vc-default-rename-file (backend old new)
3087 (condition-case nil
3088 (add-name-to-file old new)
3089 (error (rename-file old new)))
3090 (vc-delete-file old)
3091 (with-current-buffer (find-file-noselect new)
3092 (vc-register)))
3094 (defalias 'vc-default-logentry-check 'ignore)
3095 (defalias 'vc-default-check-headers 'ignore)
3097 (defun vc-default-log-view-mode (backend) (log-view-mode))
3099 (defun vc-default-show-log-entry (backend rev)
3100 (with-no-warnings
3101 (log-view-goto-rev rev)))
3103 (defun vc-default-comment-history (backend file)
3104 "Return a string with all log entries stored in BACKEND for FILE."
3105 (if (vc-find-backend-function backend 'print-log)
3106 (with-current-buffer "*vc*"
3107 (vc-call print-log (list file))
3108 (vc-call-backend backend 'wash-log)
3109 (buffer-string))))
3111 (defun vc-default-receive-file (backend file rev)
3112 "Let BACKEND receive FILE from another version control system."
3113 (vc-call-backend backend 'register file rev ""))
3115 (defun vc-default-create-snapshot (backend dir name branchp)
3116 (when branchp
3117 (error "VC backend %s does not support module branches" backend))
3118 (let ((result (vc-snapshot-precondition dir)))
3119 (if (stringp result)
3120 (error "File %s is not up-to-date" result)
3121 (vc-file-tree-walk
3123 (lambda (f)
3124 (vc-call assign-name f name))))))
3126 (defun vc-default-retrieve-snapshot (backend dir name update)
3127 (if (string= name "")
3128 (progn
3129 (vc-file-tree-walk
3131 (lambda (f) (and
3132 (vc-up-to-date-p f)
3133 (vc-error-occurred
3134 (vc-call checkout f nil "")
3135 (if update (vc-resynch-buffer f t t)))))))
3136 (let ((result (vc-snapshot-precondition dir)))
3137 (if (stringp result)
3138 (error "File %s is locked" result)
3139 (setq update (and (eq result 'visited) update))
3140 (vc-file-tree-walk
3142 (lambda (f) (vc-error-occurred
3143 (vc-call checkout f nil name)
3144 (if update (vc-resynch-buffer f t t)))))))))
3146 (defun vc-default-revert (backend file contents-done)
3147 (unless contents-done
3148 (let ((rev (vc-working-revision file))
3149 (file-buffer (or (get-file-buffer file) (current-buffer))))
3150 (message "Checking out %s..." file)
3151 (let ((failed t)
3152 (backup-name (car (find-backup-file-name file))))
3153 (when backup-name
3154 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
3155 (unless (file-writable-p file)
3156 (set-file-modes file (logior (file-modes file) 128))))
3157 (unwind-protect
3158 (let ((coding-system-for-read 'no-conversion)
3159 (coding-system-for-write 'no-conversion))
3160 (with-temp-file file
3161 (let ((outbuf (current-buffer)))
3162 ;; Change buffer to get local value of vc-checkout-switches.
3163 (with-current-buffer file-buffer
3164 (let ((default-directory (file-name-directory file)))
3165 (vc-call find-revision file rev outbuf)))))
3166 (setq failed nil))
3167 (when backup-name
3168 (if failed
3169 (rename-file backup-name file 'ok-if-already-exists)
3170 (and (not vc-make-backup-files) (delete-file backup-name))))))
3171 (message "Checking out %s...done" file))))
3173 (defalias 'vc-default-revision-completion-table 'ignore)
3175 (defun vc-check-headers ()
3176 "Check if the current file has any headers in it."
3177 (interactive)
3178 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3180 ;;; Annotate functionality
3182 ;; Declare globally instead of additional parameter to
3183 ;; temp-buffer-show-function (not possible to pass more than one
3184 ;; parameter). The use of annotate-ratio is deprecated in favor of
3185 ;; annotate-mode, which replaces it with the more sensible "span-to
3186 ;; days", along with autoscaling support.
3187 (defvar vc-annotate-ratio nil "Global variable.")
3189 ;; internal buffer-local variables
3190 (defvar vc-annotate-backend nil)
3191 (defvar vc-annotate-parent-file nil)
3192 (defvar vc-annotate-parent-rev nil)
3193 (defvar vc-annotate-parent-display-mode nil)
3195 (defconst vc-annotate-font-lock-keywords
3196 ;; The fontification is done by vc-annotate-lines instead of font-lock.
3197 '((vc-annotate-lines)))
3199 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
3200 "Major mode for output buffers of the `vc-annotate' command.
3202 You can use the mode-specific menu to alter the time-span of the used
3203 colors. See variable `vc-annotate-menu-elements' for customizing the
3204 menu items."
3205 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
3206 ;; it will become a list, to avoid initial annotations being invisible.
3207 (add-to-invisibility-spec 'foo)
3208 (remove-from-invisibility-spec 'foo)
3209 (set (make-local-variable 'truncate-lines) t)
3210 (set (make-local-variable 'font-lock-defaults)
3211 '(vc-annotate-font-lock-keywords t))
3212 (view-mode 1))
3214 (defun vc-annotate-toggle-annotation-visibility ()
3215 "Toggle whether or not the annotation is visible."
3216 (interactive)
3217 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
3218 'remove-from-invisibility-spec
3219 'add-to-invisibility-spec)
3220 'vc-annotate-annotation)
3221 (force-window-update (current-buffer)))
3223 (defun vc-annotate-display-default (ratio)
3224 "Display the output of \\[vc-annotate] using the default color range.
3225 The color range is given by `vc-annotate-color-map', scaled by RATIO.
3226 The current time is used as the offset."
3227 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
3228 (message "Redisplaying annotation...")
3229 (vc-annotate-display ratio)
3230 (message "Redisplaying annotation...done"))
3232 (defun vc-annotate-oldest-in-map (color-map)
3233 "Return the oldest time in the COLOR-MAP."
3234 ;; Since entries should be sorted, we can just use the last one.
3235 (caar (last color-map)))
3237 (defun vc-annotate-get-time-set-line-props ()
3238 (let ((bol (point))
3239 (date (vc-call-backend vc-annotate-backend 'annotate-time))
3240 (inhibit-read-only t))
3241 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
3242 date))
3244 (defun vc-annotate-display-autoscale (&optional full)
3245 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
3246 Autoscaling means that the map is scaled from the current time to the
3247 oldest annotation in the buffer, or, with prefix argument FULL, to
3248 cover the range from the oldest annotation to the newest."
3249 (interactive "P")
3250 (let ((newest 0.0)
3251 (oldest 999999.) ;Any CVS users at the founding of Rome?
3252 (current (vc-annotate-convert-time (current-time)))
3253 date)
3254 (message "Redisplaying annotation...")
3255 ;; Run through this file and find the oldest and newest dates annotated.
3256 (save-excursion
3257 (goto-char (point-min))
3258 (while (not (eobp))
3259 (when (setq date (vc-annotate-get-time-set-line-props))
3260 (if (> date newest)
3261 (setq newest date))
3262 (if (< date oldest)
3263 (setq oldest date)))
3264 (forward-line 1)))
3265 (vc-annotate-display
3266 (/ (- (if full newest current) oldest)
3267 (vc-annotate-oldest-in-map vc-annotate-color-map))
3268 (if full newest))
3269 (message "Redisplaying annotation...done \(%s\)"
3270 (if full
3271 (format "Spanned from %.1f to %.1f days old"
3272 (- current oldest)
3273 (- current newest))
3274 (format "Spanned to %.1f days old" (- current oldest))))))
3276 ;; Menu -- Using easymenu.el
3277 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
3278 "VC Annotate Display Menu"
3279 `("VC-Annotate"
3280 ["By Color Map Range" (unless (null vc-annotate-display-mode)
3281 (setq vc-annotate-display-mode nil)
3282 (vc-annotate-display-select))
3283 :style toggle :selected (null vc-annotate-display-mode)]
3284 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
3285 (mapcar (lambda (element)
3286 (let ((days (* element oldest-in-map)))
3287 `[,(format "Span %.1f days" days)
3288 (vc-annotate-display-select nil ,days)
3289 :style toggle :selected
3290 (eql vc-annotate-display-mode ,days) ]))
3291 vc-annotate-menu-elements))
3292 ["Span ..."
3293 (vc-annotate-display-select
3294 nil (float (string-to-number (read-string "Span how many days? "))))]
3295 "--"
3296 ["Span to Oldest"
3297 (unless (eq vc-annotate-display-mode 'scale)
3298 (vc-annotate-display-select nil 'scale))
3299 :style toggle :selected
3300 (eq vc-annotate-display-mode 'scale)]
3301 ["Span Oldest->Newest"
3302 (unless (eq vc-annotate-display-mode 'fullscale)
3303 (vc-annotate-display-select nil 'fullscale))
3304 :style toggle :selected
3305 (eq vc-annotate-display-mode 'fullscale)]
3306 "--"
3307 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility]
3308 ["Annotate previous revision" vc-annotate-prev-revision]
3309 ["Annotate next revision" vc-annotate-next-revision]
3310 ["Annotate revision at line" vc-annotate-revision-at-line]
3311 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line]
3312 ["Annotate latest revision" vc-annotate-working-revision]
3313 ["Show log of revision at line" vc-annotate-show-log-revision-at-line]
3314 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line]))
3316 (defun vc-annotate-display-select (&optional buffer mode)
3317 "Highlight the output of \\[vc-annotate].
3318 By default, the current buffer is highlighted, unless overridden by
3319 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
3320 use; you may override this using the second optional arg MODE."
3321 (interactive)
3322 (if mode (setq vc-annotate-display-mode mode))
3323 (pop-to-buffer (or buffer (current-buffer)))
3324 (cond ((null vc-annotate-display-mode)
3325 ;; The ratio is global, thus relative to the global color-map.
3326 (kill-local-variable 'vc-annotate-color-map)
3327 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
3328 ;; One of the auto-scaling modes
3329 ((eq vc-annotate-display-mode 'scale)
3330 (vc-exec-after `(vc-annotate-display-autoscale)))
3331 ((eq vc-annotate-display-mode 'fullscale)
3332 (vc-exec-after `(vc-annotate-display-autoscale t)))
3333 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
3334 (vc-annotate-display-default
3335 (/ vc-annotate-display-mode
3336 (vc-annotate-oldest-in-map vc-annotate-color-map))))
3337 (t (error "No such display mode: %s"
3338 vc-annotate-display-mode))))
3340 ;;;###autoload
3341 (defun vc-annotate (file rev &optional display-mode buf)
3342 "Display the edit history of the current file using colors.
3344 This command creates a buffer that shows, for each line of the current
3345 file, when it was last edited and by whom. Additionally, colors are
3346 used to show the age of each line--blue means oldest, red means
3347 youngest, and intermediate colors indicate intermediate ages. By
3348 default, the time scale stretches back one year into the past;
3349 everything that is older than that is shown in blue.
3351 With a prefix argument, this command asks two questions in the
3352 minibuffer. First, you may enter a revision number; then the buffer
3353 displays and annotates that revision instead of the working revision
3354 \(type RET in the minibuffer to leave that default unchanged). Then,
3355 you are prompted for the time span in days which the color range
3356 should cover. For example, a time span of 20 days means that changes
3357 over the past 20 days are shown in red to blue, according to their
3358 age, and everything that is older than that is shown in blue.
3360 Customization variables:
3362 `vc-annotate-menu-elements' customizes the menu elements of the
3363 mode-specific menu. `vc-annotate-color-map' and
3364 `vc-annotate-very-old-color' define the mapping of time to colors.
3365 `vc-annotate-background' specifies the background color."
3366 (interactive
3367 (save-current-buffer
3368 (vc-ensure-vc-buffer)
3369 (list buffer-file-name
3370 (let ((def (vc-working-revision buffer-file-name)))
3371 (if (null current-prefix-arg) def
3372 (read-string
3373 (format "Annotate from revision (default %s): " def)
3374 nil nil def)))
3375 (if (null current-prefix-arg)
3376 vc-annotate-display-mode
3377 (float (string-to-number
3378 (read-string "Annotate span days (default 20): "
3379 nil nil "20")))))))
3380 (vc-ensure-vc-buffer)
3381 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
3382 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
3383 (temp-buffer-show-function 'vc-annotate-display-select)
3384 ;; If BUF is specified, we presume the caller maintains current line,
3385 ;; so we don't need to do it here. This implementation may give
3386 ;; strange results occasionally in the case of REV != WORKFILE-REV.
3387 (current-line (unless buf (line-number-at-pos))))
3388 (message "Annotating...")
3389 ;; If BUF is specified it tells in which buffer we should put the
3390 ;; annotations. This is used when switching annotations to another
3391 ;; revision, so we should update the buffer's name.
3392 (if buf (with-current-buffer buf
3393 (rename-buffer temp-buffer-name t)
3394 ;; In case it had to be uniquified.
3395 (setq temp-buffer-name (buffer-name))))
3396 (with-output-to-temp-buffer temp-buffer-name
3397 (vc-call annotate-command file (get-buffer temp-buffer-name) rev)
3398 ;; we must setup the mode first, and then set our local
3399 ;; variables before the show-function is called at the exit of
3400 ;; with-output-to-temp-buffer
3401 (with-current-buffer temp-buffer-name
3402 (if (not (equal major-mode 'vc-annotate-mode))
3403 (vc-annotate-mode))
3404 (set (make-local-variable 'vc-annotate-backend) (vc-backend file))
3405 (set (make-local-variable 'vc-annotate-parent-file) file)
3406 (set (make-local-variable 'vc-annotate-parent-rev) rev)
3407 (set (make-local-variable 'vc-annotate-parent-display-mode)
3408 display-mode)))
3410 (with-current-buffer temp-buffer-name
3411 (vc-exec-after
3412 `(progn
3413 ;; Ideally, we'd rather not move point if the user has already
3414 ;; moved it elsewhere, but really point here is not the position
3415 ;; of the user's cursor :-(
3416 (when ,current-line ;(and (bobp))
3417 (goto-line ,current-line)
3418 (setq vc-sentinel-movepoint (point)))
3419 (unless (active-minibuffer-window)
3420 (message "Annotating... done")))))))
3422 (defun vc-annotate-prev-revision (prefix)
3423 "Visit the annotation of the revision previous to this one.
3425 With a numeric prefix argument, annotate the revision that many
3426 revisions previous."
3427 (interactive "p")
3428 (vc-annotate-warp-revision (- 0 prefix)))
3430 (defun vc-annotate-next-revision (prefix)
3431 "Visit the annotation of the revision after this one.
3433 With a numeric prefix argument, annotate the revision that many
3434 revisions after."
3435 (interactive "p")
3436 (vc-annotate-warp-revision prefix))
3438 (defun vc-annotate-working-revision ()
3439 "Visit the annotation of the working revision of this file."
3440 (interactive)
3441 (if (not (equal major-mode 'vc-annotate-mode))
3442 (message "Cannot be invoked outside of a vc annotate buffer")
3443 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
3444 (if (equal warp-rev vc-annotate-parent-rev)
3445 (message "Already at revision %s" warp-rev)
3446 (vc-annotate-warp-revision warp-rev)))))
3448 (defun vc-annotate-extract-revision-at-line ()
3449 "Extract the revision number of the current line."
3450 ;; This function must be invoked from a buffer in vc-annotate-mode
3451 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
3453 (defun vc-annotate-revision-at-line ()
3454 "Visit the annotation of the revision identified in the current line."
3455 (interactive)
3456 (if (not (equal major-mode 'vc-annotate-mode))
3457 (message "Cannot be invoked outside of a vc annotate buffer")
3458 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3459 (if (not rev-at-line)
3460 (message "Cannot extract revision number from the current line")
3461 (if (equal rev-at-line vc-annotate-parent-rev)
3462 (message "Already at revision %s" rev-at-line)
3463 (vc-annotate-warp-revision rev-at-line))))))
3465 (defun vc-annotate-revision-previous-to-line ()
3466 "Visit the annotation of the revision before the revision at line."
3467 (interactive)
3468 (if (not (equal major-mode 'vc-annotate-mode))
3469 (message "Cannot be invoked outside of a vc annotate buffer")
3470 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3471 (prev-rev nil))
3472 (if (not rev-at-line)
3473 (message "Cannot extract revision number from the current line")
3474 (setq prev-rev
3475 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3476 (vc-annotate-warp-revision prev-rev)))))
3478 (defun vc-annotate-show-log-revision-at-line ()
3479 "Visit the log of the revision at line."
3480 (interactive)
3481 (if (not (equal major-mode 'vc-annotate-mode))
3482 (message "Cannot be invoked outside of a vc annotate buffer")
3483 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3484 (if (not rev-at-line)
3485 (message "Cannot extract revision number from the current line")
3486 (vc-print-log rev-at-line)))))
3488 (defun vc-annotate-show-diff-revision-at-line ()
3489 "Visit the diff of the revision at line from its previous revision."
3490 (interactive)
3491 (if (not (equal major-mode 'vc-annotate-mode))
3492 (message "Cannot be invoked outside of a vc annotate buffer")
3493 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3494 (prev-rev nil))
3495 (if (not rev-at-line)
3496 (message "Cannot extract revision number from the current line")
3497 (setq prev-rev
3498 (vc-call previous-revision vc-annotate-parent-file rev-at-line))
3499 (if (not prev-rev)
3500 (message "Cannot diff from any revision prior to %s" rev-at-line)
3501 (save-window-excursion
3502 (vc-diff-internal nil (list vc-annotate-parent-file)
3503 prev-rev rev-at-line))
3504 (switch-to-buffer "*vc-diff*"))))))
3506 (defun vc-annotate-warp-revision (revspec)
3507 "Annotate the revision described by REVSPEC.
3509 If REVSPEC is a positive integer, warp that many revisions
3510 forward, if possible, otherwise echo a warning message. If
3511 REVSPEC is a negative integer, warp that many revisions backward,
3512 if possible, otherwise echo a warning message. If REVSPEC is a
3513 string, then it describes a revision number, so warp to that
3514 revision."
3515 (if (not (equal major-mode 'vc-annotate-mode))
3516 (message "Cannot be invoked outside of a vc annotate buffer")
3517 (let* ((buf (current-buffer))
3518 (oldline (line-number-at-pos))
3519 (revspeccopy revspec)
3520 (newrev nil))
3521 (cond
3522 ((and (integerp revspec) (> revspec 0))
3523 (setq newrev vc-annotate-parent-rev)
3524 (while (and (> revspec 0) newrev)
3525 (setq newrev (vc-call next-revision
3526 vc-annotate-parent-file newrev))
3527 (setq revspec (1- revspec)))
3528 (if (not newrev)
3529 (message "Cannot increment %d revisions from revision %s"
3530 revspeccopy vc-annotate-parent-rev)))
3531 ((and (integerp revspec) (< revspec 0))
3532 (setq newrev vc-annotate-parent-rev)
3533 (while (and (< revspec 0) newrev)
3534 (setq newrev (vc-call previous-revision
3535 vc-annotate-parent-file newrev))
3536 (setq revspec (1+ revspec)))
3537 (if (not newrev)
3538 (message "Cannot decrement %d revisions from revision %s"
3539 (- 0 revspeccopy) vc-annotate-parent-rev)))
3540 ((stringp revspec) (setq newrev revspec))
3541 (t (error "Invalid argument to vc-annotate-warp-revision")))
3542 (when newrev
3543 (vc-annotate vc-annotate-parent-file newrev
3544 vc-annotate-parent-display-mode
3545 buf)
3546 (goto-line (min oldline (progn (goto-char (point-max))
3547 (forward-line -1)
3548 (line-number-at-pos))) buf)))))
3550 (defun vc-annotate-compcar (threshold a-list)
3551 "Test successive cons cells of A-LIST against THRESHOLD.
3552 Return the first cons cell with a car that is not less than THRESHOLD,
3553 nil if no such cell exists."
3554 (let ((i 1)
3555 (tmp-cons (car a-list)))
3556 (while (and tmp-cons (< (car tmp-cons) threshold))
3557 (setq tmp-cons (car (nthcdr i a-list)))
3558 (setq i (+ i 1)))
3559 tmp-cons)) ; Return the appropriate value
3561 (defun vc-annotate-convert-time (time)
3562 "Convert a time value to a floating-point number of days.
3563 The argument TIME is a list as returned by `current-time' or
3564 `encode-time', only the first two elements of that list are considered."
3565 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3567 (defun vc-annotate-difference (&optional offset)
3568 "Return the time span in days to the next annotation.
3569 This calls the backend function annotate-time, and returns the
3570 difference in days between the time returned and the current time,
3571 or OFFSET if present."
3572 (let ((next-time (vc-annotate-get-time-set-line-props)))
3573 (if next-time
3574 (- (or offset
3575 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3576 next-time))))
3578 (defun vc-default-annotate-current-time (backend)
3579 "Return the current time, encoded as fractional days."
3580 (vc-annotate-convert-time (current-time)))
3582 (defvar vc-annotate-offset nil)
3584 (defun vc-annotate-display (ratio &optional offset)
3585 "Highlight `vc-annotate' output in the current buffer.
3586 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
3587 The annotations are relative to the current time, unless overridden by OFFSET."
3588 (if (/= ratio 1.0)
3589 (set (make-local-variable 'vc-annotate-color-map)
3590 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
3591 vc-annotate-color-map)))
3592 (set (make-local-variable 'vc-annotate-offset) offset)
3593 (font-lock-mode 1))
3595 (defun vc-annotate-lines (limit)
3596 (while (< (point) limit)
3597 (let ((difference (vc-annotate-difference vc-annotate-offset))
3598 (start (point))
3599 (end (progn (forward-line 1) (point))))
3600 (when difference
3601 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3602 (cons nil vc-annotate-very-old-color)))
3603 ;; substring from index 1 to remove any leading `#' in the name
3604 (face-name (concat "vc-annotate-face-"
3605 (if (string-equal
3606 (substring (cdr color) 0 1) "#")
3607 (substring (cdr color) 1)
3608 (cdr color))))
3609 ;; Make the face if not done.
3610 (face (or (intern-soft face-name)
3611 (let ((tmp-face (make-face (intern face-name))))
3612 (set-face-foreground tmp-face (cdr color))
3613 (if vc-annotate-background
3614 (set-face-background tmp-face
3615 vc-annotate-background))
3616 tmp-face)))) ; Return the face
3617 (put-text-property start end 'face face)))))
3618 ;; Pretend to font-lock there were no matches.
3619 nil)
3622 ;; Set up key bindings for use while editing log messages
3624 (defun vc-log-edit (fileset)
3625 "Set up `log-edit' for use with VC on FILE."
3626 (setq default-directory
3627 (with-current-buffer vc-parent-buffer default-directory))
3628 (log-edit 'vc-finish-logentry
3630 `((log-edit-listfun . (lambda () ',fileset))
3631 (log-edit-diff-function . (lambda () (vc-diff nil)))))
3632 (set (make-local-variable 'vc-log-fileset) fileset)
3633 (make-local-variable 'vc-log-revision)
3634 (set-buffer-modified-p nil)
3635 (setq buffer-file-name nil))
3637 ;; These things should probably be generally available
3639 (defun vc-file-tree-walk (dirname func &rest args)
3640 "Walk recursively through DIRNAME.
3641 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3642 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3643 (message "Traversing directory %s...done" dirname))
3645 (defun vc-file-tree-walk-internal (file func args)
3646 (if (not (file-directory-p file))
3647 (if (vc-backend file) (apply func file args))
3648 (message "Traversing directory %s..." (abbreviate-file-name file))
3649 (let ((dir (file-name-as-directory file)))
3650 (mapcar
3651 (lambda (f) (or
3652 (string-equal f ".")
3653 (string-equal f "..")
3654 (member f vc-directory-exclusion-list)
3655 (let ((dirf (expand-file-name f dir)))
3657 (file-symlink-p dirf) ;; Avoid possible loops.
3658 (vc-file-tree-walk-internal dirf func args)))))
3659 (directory-files dir)))))
3661 (provide 'vc)
3663 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3665 ;; These may be useful to anyone who has to debug or extend the package.
3666 ;; (Note that this information corresponds to versions 5.x. Some of it
3667 ;; might have been invalidated by the additions to support branching
3668 ;; and RCS keyword lookup. AS, 1995/03/24)
3670 ;; A fundamental problem in VC is that there are time windows between
3671 ;; vc-next-action's computations of the file's version-control state and
3672 ;; the actions that change it. This is a window open to lossage in a
3673 ;; multi-user environment; someone else could nip in and change the state
3674 ;; of the master during it.
3676 ;; The performance problem is that rlog/prs calls are very expensive; we want
3677 ;; to avoid them as much as possible.
3679 ;; ANALYSIS:
3681 ;; The performance problem, it turns out, simplifies in practice to the
3682 ;; problem of making vc-state fast. The two other functions that call
3683 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3684 ;; makes snapshots, the other deletes the calling user's last change in the
3685 ;; master.
3687 ;; The race condition implies that we have to either (a) lock the master
3688 ;; during the entire execution of vc-next-action, or (b) detect and
3689 ;; recover from errors resulting from dispatch on an out-of-date state.
3691 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3692 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3693 ;; checkin, the change message buffer pops up, and the user, having wandered
3694 ;; off to do something else, simply forgets about it?
3696 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3697 ;; vc-state. Usually, if a file is registered, we can read its locked/
3698 ;; unlocked state and its current owner from its permissions.
3700 ;; This shortcut will fail if someone has manually changed the workfile's
3701 ;; permissions; also if developers are munging the workfile in several
3702 ;; directories, with symlinks to a master (in this latter case, the
3703 ;; permissions shortcut will fail to detect a lock asserted from another
3704 ;; directory).
3706 ;; Note that these cases correspond exactly to the errors which could happen
3707 ;; because of a competing checkin/checkout race in between two instances of
3708 ;; vc-next-action.
3710 ;; For VC's purposes, a workfile/master pair may have the following states:
3712 ;; A. Unregistered. There is a workfile, there is no master.
3714 ;; B. Registered and not locked by anyone.
3716 ;; C. Locked by calling user and unchanged.
3718 ;; D. Locked by the calling user and changed.
3720 ;; E. Locked by someone other than the calling user.
3722 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3724 ;; VC's idea of state
3725 ;; |
3726 ;; V Actual state RCS action SCCS action Effect
3727 ;; A B C D E
3728 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3729 ;; B 5 . 6 7 8 co -l get -e checkout
3730 ;; C 9 10 . 11 12 co -u unget; get revert
3731 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3732 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3734 ;; All commands take the master file name as a last argument (not shown).
3736 ;; In the discussion below, a "self-race" is a pathological situation in
3737 ;; which VC operations are being attempted simultaneously by two or more
3738 ;; Emacsen running under the same username.
3740 ;; The vc-next-action code has the following windows:
3742 ;; Window P:
3743 ;; Between the check for existence of a master file and the call to
3744 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3745 ;; never close if the initial-comment feature is on.
3747 ;; Window Q:
3748 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3749 ;; following revert (apparent state C).
3751 ;; Window R:
3752 ;; Between the call to vc-workfile-unchanged-p in and the following
3753 ;; checkin (apparent state D). This window may never close.
3755 ;; Window S:
3756 ;; Between the unlock and the immediately following checkout during a
3757 ;; revert operation (apparent state C). Included in window Q.
3759 ;; Window T:
3760 ;; Between vc-state and the following checkout (apparent state B).
3762 ;; Window U:
3763 ;; Between vc-state and the following revert (apparent state C).
3764 ;; Includes windows Q and S.
3766 ;; Window V:
3767 ;; Between vc-state and the following checkin (apparent state
3768 ;; D). This window may never be closed if the user fails to complete the
3769 ;; checkin message. Includes window R.
3771 ;; Window W:
3772 ;; Between vc-state and the following steal-lock (apparent
3773 ;; state E). This window may never close if the user fails to complete
3774 ;; the steal-lock message. Includes window X.
3776 ;; Window X:
3777 ;; Between the unlock and the immediately following re-lock during a
3778 ;; steal-lock operation (apparent state E). This window may never close
3779 ;; if the user fails to complete the steal-lock message.
3781 ;; Errors:
3783 ;; Apparent state A ---
3785 ;; 1. File looked unregistered but is actually registered and not locked.
3787 ;; Potential cause: someone else's admin during window P, with
3788 ;; caller's admin happening before their checkout.
3790 ;; RCS: Prior to version 5.6.4, ci fails with message
3791 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3792 ;; ci -i option and the message is "<file>,v: already exists".
3793 ;; SCCS: admin will fail with error (ad19).
3795 ;; We can let these errors be passed up to the user.
3797 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3799 ;; Potential cause: self-race during window P.
3801 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3802 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3803 ;; ci -i option, failing with message "<file>,v: already exists".
3804 ;; SCCS: will fail with error (ad19).
3806 ;; Either of these consequences is acceptable.
3808 ;; 3. File looked unregistered but is actually locked by caller, changed.
3810 ;; Potential cause: self-race during window P.
3812 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3813 ;; a delta with a null change comment (the -t- switch will be
3814 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3815 ;; failing with message "<file>,v: already exists".
3816 ;; SCCS: will fail with error (ad19).
3818 ;; 4. File looked unregistered but is locked by someone else.
3820 ;; Potential cause: someone else's admin during window P, with
3821 ;; caller's admin happening *after* their checkout.
3823 ;; RCS: Prior to version 5.6.4, ci fails with a
3824 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3825 ;; VC uses the new ci -i option, failing with message
3826 ;; "<file>,v: already exists".
3827 ;; SCCS: will fail with error (ad19).
3829 ;; We can let these errors be passed up to the user.
3831 ;; Apparent state B ---
3833 ;; 5. File looked registered and not locked, but is actually unregistered.
3835 ;; Potential cause: master file got nuked during window P.
3837 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3838 ;; SCCS: will fail with error ut4.
3840 ;; We can let these errors be passed up to the user.
3842 ;; 6. File looked registered and not locked, but is actually locked by the
3843 ;; calling user and unchanged.
3845 ;; Potential cause: self-race during window T.
3847 ;; RCS: in the same directory as the previous workfile, co -l will fail
3848 ;; with "co error: writable foo exists; checkout aborted". In any other
3849 ;; directory, checkout will succeed.
3850 ;; SCCS: will fail with ge17.
3852 ;; Either of these consequences is acceptable.
3854 ;; 7. File looked registered and not locked, but is actually locked by the
3855 ;; calling user and changed.
3857 ;; As case 6.
3859 ;; 8. File looked registered and not locked, but is actually locked by another
3860 ;; user.
3862 ;; Potential cause: someone else checks it out during window T.
3864 ;; RCS: co error: revision 1.3 already locked by <user>
3865 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3867 ;; We can let these errors be passed up to the user.
3869 ;; Apparent state C ---
3871 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3873 ;; As case 5.
3875 ;; 10. File looks locked by calling user and unchanged, but is actually not
3876 ;; locked.
3878 ;; Potential cause: a self-race in window U, or by the revert's
3879 ;; landing during window X of some other user's steal-lock or window S
3880 ;; of another user's revert.
3882 ;; RCS: succeeds, refreshing the file from the identical version in
3883 ;; the master.
3884 ;; SCCS: fails with error ut4 (p file nonexistent).
3886 ;; Either of these consequences is acceptable.
3888 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3889 ;; changed.
3891 ;; Potential cause: the file would have to be touched by a self-race
3892 ;; during window Q.
3894 ;; The revert will succeed, removing whatever changes came with
3895 ;; the touch. It is theoretically possible that work could be lost.
3897 ;; 12. File looks like it's locked by the calling user and unchanged, but
3898 ;; it's actually locked by someone else.
3900 ;; Potential cause: a steal-lock in window V.
3902 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3903 ;; SCCS: fails with error un2
3905 ;; We can pass these errors up to the user.
3907 ;; Apparent state D ---
3909 ;; 13. File looks like it's locked by the calling user and changed, but it's
3910 ;; actually unregistered.
3912 ;; Potential cause: master file got nuked during window P.
3914 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3915 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3916 ;; option, failing with message "no such file or directory".
3917 ;; SCCS: will fail with error ut4.
3919 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3920 ;; VC may fail to detect the loss of previous version information.
3922 ;; 14. File looks like it's locked by the calling user and changed, but it's
3923 ;; actually unlocked.
3925 ;; Potential cause: self-race in window V, or the checkin happening
3926 ;; during the window X of someone else's steal-lock or window S of
3927 ;; someone else's revert.
3929 ;; RCS: ci will fail with "no lock set by <user>".
3930 ;; SCCS: delta will fail with error ut4.
3932 ;; 15. File looks like it's locked by the calling user and changed, but it's
3933 ;; actually locked by the calling user and unchanged.
3935 ;; Potential cause: another self-race --- a whole checkin/checkout
3936 ;; sequence by the calling user would have to land in window R.
3938 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3939 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3940 ;; the file unlocked.
3942 ;; It is theoretically possible that work could be lost under RCS.
3944 ;; 16. File looks like it's locked by the calling user and changed, but it's
3945 ;; actually locked by a different user.
3947 ;; RCS: ci error: no lock set by <user>
3948 ;; SCCS: unget will fail with error un2
3950 ;; We can pass these errors up to the user.
3952 ;; Apparent state E ---
3954 ;; 17. File looks like it's locked by some other user, but it's actually
3955 ;; unregistered.
3957 ;; As case 13.
3959 ;; 18. File looks like it's locked by some other user, but it's actually
3960 ;; unlocked.
3962 ;; Potential cause: someone released a lock during window W.
3964 ;; RCS: The calling user will get the lock on the file.
3965 ;; SCCS: unget -n will fail with cm4.
3967 ;; Either of these consequences will be OK.
3969 ;; 19. File looks like it's locked by some other user, but it's actually
3970 ;; locked by the calling user and unchanged.
3972 ;; Potential cause: the other user relinquishing a lock followed by
3973 ;; a self-race, both in window W.
3975 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3976 ;; the sequence a no-op.
3978 ;; 20. File looks like it's locked by some other user, but it's actually
3979 ;; locked by the calling user and changed.
3981 ;; As case 19.
3983 ;; PROBLEM CASES:
3985 ;; In order of decreasing severity:
3987 ;; Cases 11 and 15 are the only ones that potentially lose work.
3988 ;; They would require a self-race for this to happen.
3990 ;; Case 13 in RCS loses information about previous deltas, retaining
3991 ;; only the information in the current workfile. This can only happen
3992 ;; if the master file gets nuked in window P.
3994 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3995 ;; no change comment in the master. This would require a self-race in
3996 ;; window P or R respectively.
3998 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
4000 ;; Unfortunately, it appears to me that no recovery is possible in these
4001 ;; cases. They don't yield error messages, so there's no way to tell that
4002 ;; a race condition has occurred.
4004 ;; All other cases don't change either the workfile or the master, and
4005 ;; trigger command errors which the user will see.
4007 ;; Thus, there is no explicit recovery code.
4009 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
4010 ;;; vc.el ends here