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