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