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