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