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