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