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