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