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