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