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