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