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