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