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