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