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