Merge from gnus--devo--0
[emacs.git] / lisp / vc.el
blob7cf24bcd9539320415d380c00ba59d974179cd77
1 ;;; vc.el --- drive a version-control system from within Emacs
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.
7 ;; Author: FSF (see below for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Keywords: tools
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Credits:
28 ;; VC was initially designed and implemented by Eric S. Raymond
29 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
30 ;; contributed substantial amounts of work to VC. These include:
32 ;; Per Cederqvist <ceder@lysator.liu.se>
33 ;; Paul Eggert <eggert@twinsun.com>
34 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
35 ;; Martin Lorentzson <martinl@gnu.org>
36 ;; Dave Love <fx@gnu.org>
37 ;; Stefan Monnier <monnier@cs.yale.edu>
38 ;; Thien-Thi Nguyen <ttn@gnu.org>
39 ;; Dan Nicolaescu <dann@ics.uci.edu>
40 ;; J.D. Smith <jdsmith@alum.mit.edu>
41 ;; Andre Spiegel <spiegel@gnu.org>
42 ;; Richard Stallman <rms@gnu.org>
44 ;; In July 2007 ESR returned and redesigned the mode to cope better
45 ;; with modern version-control systems that do commits by fileset
46 ;; rather than per individual file.
48 ;; If you maintain a client of the mode or customize it in your .emacs,
49 ;; note that some backend functions which formerly took single file arguments
50 ;; now take a list of files. These include: register, checkin, print-log,
51 ;; rollback, and diff.
53 ;;; Commentary:
55 ;; This mode is fully documented in the Emacs user's manual.
57 ;; Supported version-control systems presently include CVS, RCS, GNU
58 ;; Arch, Subversion, Bzr, Git, Mercurial, Monotone and SCCS
59 ;; (or its free replacement, CSSC).
61 ;; Some features will not work with old RCS versions. Where
62 ;; appropriate, VC finds out which version you have, and allows or
63 ;; disallows those features (stealing locks, for example, works only
64 ;; from 5.6.2 onwards).
65 ;; Even initial checkins will fail if your RCS version is so old that ci
66 ;; doesn't understand -t-; this has been known to happen to people running
67 ;; NExTSTEP 3.0.
69 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
71 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
72 ;; to be installed somewhere on Emacs's path for executables.
74 ;; If your site uses the ChangeLog convention supported by Emacs, the
75 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
76 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
77 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
79 ;; The vc code maintains some internal state in order to reduce expensive
80 ;; version-control operations to a minimum. Some names are only computed
81 ;; once. If you perform version control operations with the backend while
82 ;; vc's back is turned, or move/rename master files while vc is running,
83 ;; vc may get seriously confused. Don't do these things!
85 ;; Developer's notes on some concurrency issues are included at the end of
86 ;; the file.
88 ;; ADDING SUPPORT FOR OTHER BACKENDS
90 ;; VC can use arbitrary version control systems as a backend. To add
91 ;; support for a new backend named SYS, write a library vc-sys.el that
92 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
93 ;; case for the function and library names). VC will use that library if
94 ;; you put the symbol SYS somewhere into the list of
95 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
96 ;; returns non-nil for a file, all SYS-specific versions of VC commands
97 ;; will be available for that file.
99 ;; VC keeps some per-file information in the form of properties (see
100 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
101 ;; do not generally need to be aware of these properties. For example,
102 ;; `vc-sys-working-revision' should compute the working revision and
103 ;; return it; it should not look it up in the property, and it needn't
104 ;; store it there either. However, if a backend-specific function does
105 ;; store a value in a property, that value takes precedence over any
106 ;; value that the generic code might want to set (check for uses of
107 ;; the macro `with-vc-properties' in vc.el).
109 ;; In the list of functions below, each identifier needs to be prepended
110 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
111 ;; `*'), others are optional (`-').
113 ;; BACKEND PROPERTIES
115 ;; * revision-granularity
117 ;; Takes no arguments. Returns either 'file or 'repository. Backends
118 ;; that return 'file have per-file revision numbering; backends
119 ;; that return 'repository have per-repository revision numbering,
120 ;; so a revision level implicitly identifies a changeset
122 ;; STATE-QUERYING FUNCTIONS
124 ;; * registered (file)
126 ;; Return non-nil if FILE is registered in this backend. Both this
127 ;; function as well as `state' should be careful to fail gracefully
128 ;; in the event that the backend executable is absent. It is
129 ;; preferable that this function's body is autoloaded, that way only
130 ;; calling vc-registered does not cause the backend to be loaded
131 ;; (all the vc-FOO-registered functions are called to try to find
132 ;; the controlling backend for FILE.
134 ;; * state (file)
136 ;; Return the current version control state of FILE. For a list of
137 ;; possible values, see `vc-state'. This function should do a full and
138 ;; reliable state computation; it is usually called immediately after
139 ;; C-x v v. If you want to use a faster heuristic when visiting a
140 ;; file, put that into `state-heuristic' below. Note that under most
141 ;; VCSes this won't be called at all, dir-status is used instead.
143 ;; - state-heuristic (file)
145 ;; If provided, this function is used to estimate the version control
146 ;; state of FILE at visiting time. It should be considerably faster
147 ;; than the implementation of `state'. For a list of possible values,
148 ;; see the doc string of `vc-state'.
150 ;; - dir-status (dir update-function)
152 ;; Produce RESULT: a list of lists of the form (FILE VC-STATE EXTRA)
153 ;; for the files in DIR.
154 ;; EXTRA can be used for backend specific information about FILE.
155 ;; If a command needs to be run to compute this list, it should be
156 ;; run asynchronously using (current-buffer) as the buffer for the
157 ;; command. When RESULT is computed, it should be passed back by
158 ;; doing: (funcall UPDATE-FUNCTION RESULT nil).
159 ;; If the backend uses a process filter, hence it produces partial results,
160 ;; they can be passed back by doing:
161 ;; (funcall UPDATE-FUNCTION RESULT t)
162 ;; and then do a (funcall UPDATE-FUNCTION RESULT nil)
163 ;; when all the results have been computed.
164 ;; To provide more backend specific functionality for `vc-dir'
165 ;; the following functions might be needed: `status-extra-headers',
166 ;; `status-printer', `extra-status-menu' and `dir-status-files'.
168 ;; - dir-status-files (dir files default-state update-function)
170 ;; This function is identical to dir-status except that it should
171 ;; only report status for the specified FILES. Also it needs to
172 ;; report on all requested files, including up-to-date or ignored
173 ;; files. If not provided, the default is to consider that the files
174 ;; are in DEFAULT-STATE.
176 ;; - status-extra-headers (dir)
178 ;; Return a string that will be added to the *vc-dir* buffer header.
180 ;; - status-printer (fileinfo)
182 ;; Pretty print the `vc-dir-fileinfo' FILEINFO.
183 ;; If a backend needs to show more information than the default FILE
184 ;; and STATE in the vc-dir listing, it can store that extra
185 ;; information in `vc-dir-fileinfo->extra'. This function can be
186 ;; used to display that extra information in the *vc-dir* buffer.
188 ;; - status-fileinfo-extra (file)
190 ;; Compute `vc-dir-fileinfo->extra' for FILE.
192 ;; * working-revision (file)
194 ;; Return the working revision of FILE. This is the revision fetched
195 ;; by the last checkout or upate, not necessarily the same thing as the
196 ;; head or tip revision. Should return "0" for a file added but not yet
197 ;; committed.
199 ;; - latest-on-branch-p (file)
201 ;; Return non-nil if the working revision of FILE is the latest revision
202 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
203 ;; The default implementation always returns t, which means that
204 ;; working with non-current revisions is not supported by default.
206 ;; * checkout-model (files)
208 ;; Indicate whether FILES need to be "checked out" before they can be
209 ;; edited. See `vc-checkout-model' for a list of possible values.
211 ;; - workfile-unchanged-p (file)
213 ;; Return non-nil if FILE is unchanged from the working revision.
214 ;; This function should do a brief comparison of FILE's contents
215 ;; with those of the repository master of the working revision. If
216 ;; the backend does not have such a brief-comparison feature, the
217 ;; default implementation of this function can be used, which
218 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
219 ;; must not run asynchronously in this case, see variable
220 ;; `vc-disable-async-diff'.)
222 ;; - mode-line-string (file)
224 ;; If provided, this function should return the VC-specific mode
225 ;; line string for FILE. The returned string should have a
226 ;; `help-echo' property which is the text to be displayed as a
227 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
228 ;; The default implementation deals well with all states that
229 ;; `vc-state' can return.
231 ;; - prettify-state-info (file)
233 ;; Translate the `vc-state' property of FILE into a string that can be
234 ;; used in a human-readable buffer. The default implementation deals well
235 ;; with all states that `vc-state' can return.
237 ;; STATE-CHANGING FUNCTIONS
239 ;; * create-repo (backend)
241 ;; Create an empty repository in the current directory and initialize
242 ;; it so VC mode can add files to it. For file-oriented systems, this
243 ;; need do no more than create a subdirectory with the right name.
245 ;; * register (files &optional rev comment)
247 ;; Register FILES in this backend. Optionally, an initial revision REV
248 ;; and an initial description of the file, COMMENT, may be specified,
249 ;; but it is not guaranteed that the backend will do anything with this.
250 ;; The implementation should pass the value of vc-register-switches
251 ;; to the backend command. (Note: in older versions of VC, this
252 ;; command took a single file argument and not a list.)
254 ;; - init-revision (file)
256 ;; The initial revision to use when registering FILE if one is not
257 ;; specified by the user. If not provided, the variable
258 ;; vc-default-init-revision is used instead.
260 ;; - responsible-p (file)
262 ;; Return non-nil if this backend considers itself "responsible" for
263 ;; FILE, which can also be a directory. This function is used to find
264 ;; out what backend to use for registration of new files and for things
265 ;; like change log generation. The default implementation always
266 ;; returns nil.
268 ;; - could-register (file)
270 ;; Return non-nil if FILE could be registered under this backend. The
271 ;; default implementation always returns t.
273 ;; - receive-file (file rev)
275 ;; Let this backend "receive" a file that is already registered under
276 ;; another backend. The default implementation simply calls `register'
277 ;; for FILE, but it can be overridden to do something more specific,
278 ;; e.g. keep revision numbers consistent or choose editing modes for
279 ;; FILE that resemble those of the other backend.
281 ;; - unregister (file)
283 ;; Unregister FILE from this backend. This is only needed if this
284 ;; backend may be used as a "more local" backend for temporary editing.
286 ;; * checkin (files rev comment)
288 ;; Commit changes in FILES to this backend. If REV is non-nil, that
289 ;; should become the new revision number (not all backends do
290 ;; anything with it). COMMENT is used as a check-in comment. The
291 ;; implementation should pass the value of vc-checkin-switches to
292 ;; the backend command. (Note: in older versions of VC, this
293 ;; command took a single file argument and not a list.)
295 ;; * find-revision (file rev buffer)
297 ;; Fetch revision REV of file FILE and put it into BUFFER.
298 ;; If REV is the empty string, fetch the head of the trunk.
299 ;; The implementation should pass the value of vc-checkout-switches
300 ;; to the backend command.
302 ;; * checkout (file &optional editable rev)
304 ;; Check out revision REV of FILE into the working area. If EDITABLE
305 ;; is non-nil, FILE should be writable by the user and if locking is
306 ;; used for FILE, a lock should also be set. If REV is non-nil, that
307 ;; is the revision to check out (default is the working revision).
308 ;; If REV is t, that means to check out the head of the current branch;
309 ;; if it is the empty string, check out the head of the trunk.
310 ;; The implementation should pass the value of vc-checkout-switches
311 ;; to the backend command.
313 ;; * revert (file &optional contents-done)
315 ;; Revert FILE back to the working revision. If optional
316 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
317 ;; already been reverted from a version backup, and this function
318 ;; only needs to update the status of FILE within the backend.
320 ;; - rollback (files)
322 ;; Remove the tip revision of each of FILES from the repository. If
323 ;; this function is not provided, trying to cancel a revision is
324 ;; caught as an error. (Most backends don't provide it.) (Also
325 ;; note that older versions of this backend command were called
326 ;; 'cancel-version' and took a single file arg, not a list of
327 ;; files.)
329 ;; - merge (file rev1 rev2)
331 ;; Merge the changes between REV1 and REV2 into the current working file.
333 ;; - merge-news (file)
335 ;; Merge recent changes from the current branch into FILE.
337 ;; - steal-lock (file &optional revision)
339 ;; Steal any lock on the working revision of FILE, or on REVISION if
340 ;; that is provided. This function is only needed if locking is
341 ;; used for files under this backend, and if files can indeed be
342 ;; locked by other users.
344 ;; - modify-change-comment (files rev comment)
346 ;; Modify the change comments associated with the files at the
347 ;; given revision. This is optional, many backends do not support it.
349 ;; - mark-resolved (files)
351 ;; Mark conflicts as resolved. Some VC systems need to run a
352 ;; command to mark conflicts as resolved.
354 ;; HISTORY FUNCTIONS
356 ;; * print-log (files &optional buffer)
358 ;; Insert the revision log for FILES into BUFFER, or the *vc* buffer
359 ;; if BUFFER is nil. (Note: older versions of this function expected
360 ;; only a single file argument.)
362 ;; - log-view-mode ()
364 ;; Mode to use for the output of print-log. This defaults to
365 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
366 ;; mode of `log-view-mode'.
368 ;; - show-log-entry (revision)
370 ;; If provided, search the log entry for REVISION in the current buffer,
371 ;; and make sure it is displayed in the buffer's window. The default
372 ;; implementation of this function works for RCS-style logs.
374 ;; - comment-history (file)
376 ;; Return a string containing all log entries that were made for FILE.
377 ;; This is used for transferring a file from one backend to another,
378 ;; retaining comment information.
380 ;; - update-changelog (files)
382 ;; Using recent log entries, create ChangeLog entries for FILES, or for
383 ;; all files at or below the default-directory if FILES is nil. The
384 ;; default implementation runs rcs2log, which handles RCS- and
385 ;; CVS-style logs.
387 ;; * diff (files &optional rev1 rev2 buffer)
389 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
390 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
391 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
392 ;; found in the repository) as the older revision; if REV2 is nil,
393 ;; use the current working-copy contents as the newer revision. This
394 ;; function should pass the value of (vc-switches BACKEND 'diff) to
395 ;; the backend command. It should return a status of either 0 (no
396 ;; differences found), or 1 (either non-empty diff or the diff is
397 ;; run asynchronously).
399 ;; - revision-completion-table (files)
401 ;; Return a completion table for existing revisions of FILES.
402 ;; The default is to not use any completion table.
404 ;; - annotate-command (file buf &optional rev)
406 ;; If this function is provided, it should produce an annotated display
407 ;; of FILE in BUF, relative to revision REV. Annotation means each line
408 ;; of FILE displayed is prefixed with version information associated with
409 ;; its addition (deleted lines leave no history) and that the text of the
410 ;; file is fontified according to age.
412 ;; - annotate-time ()
414 ;; Only required if `annotate-command' is defined for the backend.
415 ;; Return the time of the next line of annotation at or after point,
416 ;; as a floating point fractional number of days. The helper
417 ;; function `vc-annotate-convert-time' may be useful for converting
418 ;; multi-part times as returned by `current-time' and `encode-time'
419 ;; to this format. Return nil if no more lines of annotation appear
420 ;; in the buffer. You can safely assume that point is placed at the
421 ;; beginning of each line, starting at `point-min'. The buffer that
422 ;; point is placed in is the Annotate output, as defined by the
423 ;; relevant backend. This function also affects how much of the line
424 ;; is fontified; where it leaves point is where fontification begins.
426 ;; - annotate-current-time ()
428 ;; Only required if `annotate-command' is defined for the backend,
429 ;; AND you'd like the current time considered to be anything besides
430 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
431 ;; time with hours, minutes, and seconds included. Probably safe to
432 ;; ignore. Return the current-time, in units of fractional days.
434 ;; - annotate-extract-revision-at-line ()
436 ;; Only required if `annotate-command' is defined for the backend.
437 ;; Invoked from a buffer in vc-annotate-mode, return the revision
438 ;; corresponding to the current line, or nil if there is no revision
439 ;; corresponding to the current line.
441 ;; TAG SYSTEM
443 ;; - create-tag (dir name branchp)
445 ;; Attach the tag NAME to the state of the working copy. This
446 ;; should make sure that files are up-to-date before proceeding with
447 ;; the action. DIR can also be a file and if BRANCHP is specified,
448 ;; NAME should be created as a branch and DIR should be checked out
449 ;; under this new branch. The default implementation does not
450 ;; support branches but does a sanity check, a tree traversal and
451 ;; assigns the tag to each file.
453 ;; - retrieve-tag (dir name update)
455 ;; Retrieve the version tagged by NAME of all registered files at or below DIR.
456 ;; If UPDATE is non-nil, then update buffers of any files in the
457 ;; tag that are currently visited. The default implementation
458 ;; does a sanity check whether there aren't any uncommitted changes at
459 ;; or below DIR, and then performs a tree walk, using the `checkout'
460 ;; function to retrieve the corresponding revisions.
462 ;; MISCELLANEOUS
464 ;; - root (dir)
466 ;; Return DIR's "root" directory, that is, a parent directory of
467 ;; DIR for which the same backend as used for DIR applies. If no
468 ;; such parent exists, this function should return DIR.
470 ;; - make-version-backups-p (file)
472 ;; Return non-nil if unmodified repository revisions of FILE should be
473 ;; backed up locally. If this is done, VC can perform `diff' and
474 ;; `revert' operations itself, without calling the backend system. The
475 ;; default implementation always returns nil.
477 ;; - repository-hostname (dirname)
479 ;; Return the hostname that the backend will have to contact
480 ;; in order to operate on a file in DIRNAME. If the return value
481 ;; is nil, it means that the repository is local.
482 ;; This function is used in `vc-stay-local-p' which backends can use
483 ;; for their convenience.
485 ;; - previous-revision (file rev)
487 ;; Return the revision number that precedes REV for FILE, or nil if no such
488 ;; revision exists.
490 ;; - next-revision (file rev)
492 ;; Return the revision number that follows REV for FILE, or nil if no such
493 ;; revision exists.
495 ;; - check-headers ()
497 ;; Return non-nil if the current buffer contains any version headers.
499 ;; - clear-headers ()
501 ;; In the current buffer, reset all version headers to their unexpanded
502 ;; form. This function should be provided if the state-querying code
503 ;; for this backend uses the version headers to determine the state of
504 ;; a file. This function will then be called whenever VC changes the
505 ;; version control state in such a way that the headers would give
506 ;; wrong information.
508 ;; - delete-file (file)
510 ;; Delete FILE and mark it as deleted in the repository. If this
511 ;; function is not provided, the command `vc-delete-file' will
512 ;; signal an error.
514 ;; - rename-file (old new)
516 ;; Rename file OLD to NEW, both in the working area and in the
517 ;; repository. If this function is not provided, the renaming
518 ;; will be done by (vc-delete-file old) and (vc-register new).
520 ;; - find-file-hook ()
522 ;; Operation called in current buffer when opening a file. This can
523 ;; be used by the backend to setup some local variables it might need.
525 ;; - find-file-not-found-hook ()
527 ;; Operation called in current buffer when opening a non-existing file.
528 ;; By default, this asks the user if she wants to check out the file.
530 ;; - extra-menu ()
532 ;; Return a menu keymap, the items in the keymap will appear at the
533 ;; end of the Version Control menu. The goal is to allow backends
534 ;; to specify extra menu items that appear in the VC menu. This way
535 ;; you can provide menu entries for functionality that is specific
536 ;; to your backend and which does not map to any of the VC generic
537 ;; concepts.
539 ;; - extra-status-menu ()
541 ;; Return a menu keymap, the items in the keymap will appear at the
542 ;; end of the VC Status menu. The goal is to allow backends to
543 ;; specify extra menu items that appear in the VC Status menu. This
544 ;; makes it possible to provide menu entries for functionality that
545 ;; is specific to a backend and which does not map to any of the VC
546 ;; generic concepts.
548 ;;; Todo:
550 ;;;; New Primitives:
552 ;; - deal with push/pull operations.
554 ;; - add a mechanism for editing the underlying VCS's list of files
555 ;; to be ignored, when that's possible.
557 ;;;; Primitives that need changing:
559 ;; - vc-update/vc-merge should deal with VC systems that don't
560 ;; update/merge on a file basis, but on a whole repository basis.
561 ;; vc-update and vc-merge assume the arguments are always files,
562 ;; they don't deal with directories. Make sure the *vc-dir* buffer
563 ;; is updated after these operations.
564 ;; At least bzr, git and hg should benefit from this.
566 ;;;; Improved branch and tag handling:
568 ;; - add a generic mechanism for remembering the current branch names,
569 ;; display the branch name in the mode-line. Replace
570 ;; vc-cvs-sticky-tag with that.
572 ;; - C-x v b does switch to a different backend, but the mode line is not
573 ;; adapted accordingly. Also, it considers RCS and CVS to be the same,
574 ;; which is pretty confusing.
576 ;; - vc-create-tag and vc-retrieve-tag should update the
577 ;; buffers that might be visiting the affected files.
579 ;;;; Default Behavior:
581 ;; - do not default to RCS anymore when the current directory is not
582 ;; controlled by any VCS and the user does C-x v v
584 ;; - vc-responsible-backend should not return RCS if no backend
585 ;; declares itself responsible.
587 ;;;; Internal cleanups:
589 ;; - backends that care about vc-stay-local should try to take it into
590 ;; account for vc-dir. Is this likely to be useful???
592 ;; - vc-expand-dirs should take a backend parameter and only look for
593 ;; files managed by that backend.
595 ;; - Another important thing: merge all the status-like backend operations.
596 ;; We should remove dir-status, state, and dir-status-files, and
597 ;; replace them with just `status' which takes a fileset and a continuation
598 ;; (like dir-status) and returns a buffer in which the process(es) are run
599 ;; (or nil if it worked synchronously). Hopefully we can define the old
600 ;; 4 operations in term of this one.
602 ;;;; Other
604 ;; - when a file is in `conflict' state, turn on smerge-mode.
606 ;; - figure out what to do with conflicts that are not caused by the
607 ;; file contents, but by metadata or other causes. Example: File A
608 ;; gets renamed to B in one branch and to C in another and you merge
609 ;; the two branches. Or you locally add file FOO and then pull a
610 ;; change that also adds a new file FOO, ...
612 ;; - C-x v l should insert the file set in the *VC-log* buffer so that
613 ;; log-view can recognize it and use it for its commands.
615 ;; - vc-diff should be able to show the diff for all files in a
616 ;; changeset, especially for VC systems that have per repository
617 ;; version numbers. log-view should take advantage of this.
619 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
620 ;; buffer, if one is present, instead of adding to the ChangeLog.
622 ;; - When vc-next-action calls vc-checkin it could pre-fill the
623 ;; *VC-log* buffer with some obvious items: the list of files that
624 ;; were added, the list of files that were removed. If the diff is
625 ;; available, maybe it could even call something like
626 ;; `diff-add-change-log-entries-other-window' to create a detailed
627 ;; skeleton for the log...
629 ;; - most vc-dir backends need more work. They might need to
630 ;; provide custom headers, use the `extra' field and deal with all
631 ;; possible VC states.
633 ;; - add a function that calls vc-dir to `find-directory-functions'.
635 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
636 ;; files. Now that unregistered and ignored files are shown in
637 ;; vc-dir, it is possible that these commands are called
638 ;; for unregistered/ignored files.
640 ;; - Using multiple backends needs work. Given a CVS directory with some
641 ;; files checked into git (but not all), using C-x v l to get a log file
642 ;; from a file only present in git, and then typing RET on some log entry,
643 ;; vc will bombs out because it wants to see the file being in CVS.
644 ;; Those logs should likely use a local variable to hardware the VC they
645 ;; are supposed to work with.
647 ;;;; Problems:
649 ;; - log-view-diff does not work anymore in the case when the log was
650 ;; created from more than one file. The error is:
651 ;; vc-derived-from-dir-mode: Lisp nesting exceeds `max-lisp-eval-depth'.
653 ;; - the vc-dir display is now bogus for git and mercurial.
655 ;; - the CVS vc-dir display is now incorrect from some states.
657 ;; - vc-dir is now broken for RCS and SCCS.
659 ;; - the *vc-dir* buffer is not updated correctly anymore after VC
660 ;; operations that change the file state.
662 ;; - the mouse3 menu for vc-dir does not have a title anymore.
664 ;; - the menu for the *vc-dir* buffer uses the wrong name now.
666 ;;; Code:
668 (require 'vc-hooks)
669 (require 'vc-dispatcher)
670 (require 'tool-bar)
671 (require 'ewoc)
673 (eval-when-compile
674 (require 'cl))
676 (unless (assoc 'vc-parent-buffer minor-mode-alist)
677 (setq minor-mode-alist
678 (cons '(vc-parent-buffer vc-parent-buffer-name)
679 minor-mode-alist)))
681 ;; General customization
683 (defgroup vc nil
684 "Version-control system in Emacs."
685 :group 'tools)
687 (defcustom vc-initial-comment nil
688 "If non-nil, prompt for initial comment when a file is registered."
689 :type 'boolean
690 :group 'vc)
692 (defcustom vc-default-init-revision "1.1"
693 "A string used as the default revision number when a new file is registered.
694 This can be overridden by giving a prefix argument to \\[vc-register]. This
695 can also be overridden by a particular VC backend."
696 :type 'string
697 :group 'vc
698 :version "20.3")
700 (defcustom vc-checkin-switches nil
701 "A string or list of strings specifying extra switches for checkin.
702 These are passed to the checkin program by \\[vc-checkin]."
703 :type '(choice (const :tag "None" nil)
704 (string :tag "Argument String")
705 (repeat :tag "Argument List"
706 :value ("")
707 string))
708 :group 'vc)
710 (defcustom vc-checkout-switches nil
711 "A string or list of strings specifying extra switches for checkout.
712 These are passed to the checkout program by \\[vc-checkout]."
713 :type '(choice (const :tag "None" nil)
714 (string :tag "Argument String")
715 (repeat :tag "Argument List"
716 :value ("")
717 string))
718 :group 'vc)
720 (defcustom vc-register-switches nil
721 "A string or list of strings; extra switches for registering a file.
722 These are passed to the checkin program by \\[vc-register]."
723 :type '(choice (const :tag "None" nil)
724 (string :tag "Argument String")
725 (repeat :tag "Argument List"
726 :value ("")
727 string))
728 :group 'vc)
730 (defcustom vc-diff-switches nil
731 "A string or list of strings specifying switches for diff under VC.
732 When running diff under a given BACKEND, VC concatenates the values of
733 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
734 get the switches for that command. Thus, `vc-diff-switches' should
735 contain switches that are specific to version control, but not
736 specific to any particular backend."
737 :type '(choice (const :tag "None" nil)
738 (string :tag "Argument String")
739 (repeat :tag "Argument List"
740 :value ("")
741 string))
742 :group 'vc
743 :version "21.1")
745 (defcustom vc-diff-knows-L nil
746 "*Indicates whether diff understands the -L option.
747 The value is either `yes', `no', or nil. If it is nil, VC tries
748 to use -L and sets this variable to remember whether it worked."
749 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
750 :group 'vc)
752 (defcustom vc-allow-async-revert nil
753 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
754 Enabling this option means that you can confirm a revert operation even
755 if the local changes in the file have not been found and displayed yet."
756 :type '(choice (const :tag "No" nil)
757 (const :tag "Yes" t))
758 :group 'vc
759 :version "22.1")
761 ;;;###autoload
762 (defcustom vc-checkout-hook nil
763 "Normal hook (list of functions) run after checking out a file.
764 See `run-hooks'."
765 :type 'hook
766 :group 'vc
767 :version "21.1")
769 (defcustom vc-annotate-display-mode 'fullscale
770 "Which mode to color the output of \\[vc-annotate] with by default."
771 :type '(choice (const :tag "By Color Map Range" nil)
772 (const :tag "Scale to Oldest" scale)
773 (const :tag "Scale Oldest->Newest" fullscale)
774 (number :tag "Specify Fractional Number of Days"
775 :value "20.5"))
776 :group 'vc)
778 ;;;###autoload
779 (defcustom vc-checkin-hook nil
780 "Normal hook (list of functions) run after commit or file checkin.
781 See also `log-edit-done-hook'."
782 :type 'hook
783 :options '(log-edit-comment-to-change-log)
784 :group 'vc)
786 ;;;###autoload
787 (defcustom vc-before-checkin-hook nil
788 "Normal hook (list of functions) run before a commit or a file checkin.
789 See `run-hooks'."
790 :type 'hook
791 :group 'vc)
793 ;; Annotate customization
794 (defcustom vc-annotate-color-map
795 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
796 ;; A custom sorted TTY colormap
797 (let* ((colors
798 (sort
799 (delq nil
800 (mapcar (lambda (x)
801 (if (not (or
802 (string-equal (car x) "white")
803 (string-equal (car x) "black") ))
804 (car x)))
805 (tty-color-alist)))
806 (lambda (a b)
807 (cond
808 ((or (string-equal a "red") (string-equal b "blue")) t)
809 ((or (string-equal b "red") (string-equal a "blue")) nil)
810 ((string-equal a "yellow") t)
811 ((string-equal b "yellow") nil)
812 ((string-equal a "cyan") t)
813 ((string-equal b "cyan") nil)
814 ((string-equal a "green") t)
815 ((string-equal b "green") nil)
816 ((string-equal a "magenta") t)
817 ((string-equal b "magenta") nil)
818 (t (string< a b))))))
819 (date 20.)
820 (delta (/ (- 360. date) (1- (length colors)))))
821 (mapcar (lambda (x)
822 (prog1
823 (cons date x)
824 (setq date (+ date delta)))) colors))
825 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
826 '(( 20. . "#FF3F3F")
827 ( 40. . "#FF6C3F")
828 ( 60. . "#FF993F")
829 ( 80. . "#FFC63F")
830 (100. . "#FFF33F")
831 (120. . "#DDFF3F")
832 (140. . "#B0FF3F")
833 (160. . "#83FF3F")
834 (180. . "#56FF3F")
835 (200. . "#3FFF56")
836 (220. . "#3FFF83")
837 (240. . "#3FFFB0")
838 (260. . "#3FFFDD")
839 (280. . "#3FF3FF")
840 (300. . "#3FC6FF")
841 (320. . "#3F99FF")
842 (340. . "#3F6CFF")
843 (360. . "#3F3FFF")))
844 "Association list of age versus color, for \\[vc-annotate].
845 Ages are given in units of fractional days. Default is eighteen
846 steps using a twenty day increment, from red to blue. For TTY
847 displays with 8 or fewer colors, the default is red to blue with
848 all other colors between (excluding black and white)."
849 :type 'alist
850 :group 'vc)
852 (defcustom vc-annotate-very-old-color "#3F3FFF"
853 "Color for lines older than the current color range in \\[vc-annotate]]."
854 :type 'string
855 :group 'vc)
857 (defcustom vc-annotate-background "black"
858 "Background color for \\[vc-annotate].
859 Default color is used if nil."
860 :type '(choice (const :tag "Default background" nil) (color))
861 :group 'vc)
863 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
864 "Menu elements for the mode-specific menu of VC-Annotate mode.
865 List of factors, used to expand/compress the time scale. See `vc-annotate'."
866 :type '(repeat number)
867 :group 'vc)
869 (defvar vc-annotate-mode-map
870 (let ((m (make-sparse-keymap)))
871 (define-key m "A" 'vc-annotate-revision-previous-to-line)
872 (define-key m "D" 'vc-annotate-show-diff-revision-at-line)
873 (define-key m "f" 'vc-annotate-find-revision-at-line)
874 (define-key m "J" 'vc-annotate-revision-at-line)
875 (define-key m "L" 'vc-annotate-show-log-revision-at-line)
876 (define-key m "N" 'vc-annotate-next-revision)
877 (define-key m "P" 'vc-annotate-prev-revision)
878 (define-key m "W" 'vc-annotate-working-revision)
879 (define-key m "V" 'vc-annotate-toggle-annotation-visibility)
881 "Local keymap used for VC-Annotate mode.")
883 ;; Header-insertion hair
885 (defcustom vc-static-header-alist
886 '(("\\.c\\'" .
887 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
888 "*Associate static header string templates with file types.
889 A \%s in the template is replaced with the first string associated with
890 the file's version control type in `vc-header-alist'."
891 :type '(repeat (cons :format "%v"
892 (regexp :tag "File Type")
893 (string :tag "Header String")))
894 :group 'vc)
896 (defcustom vc-comment-alist
897 '((nroff-mode ".\\\"" ""))
898 "*Special comment delimiters for generating VC headers.
899 Add an entry in this list if you need to override the normal `comment-start'
900 and `comment-end' variables. This will only be necessary if the mode language
901 is sensitive to blank lines."
902 :type '(repeat (list :format "%v"
903 (symbol :tag "Mode")
904 (string :tag "Comment Start")
905 (string :tag "Comment End")))
906 :group 'vc)
908 (defcustom vc-checkout-carefully (= (user-uid) 0)
909 "*Non-nil means be extra-careful in checkout.
910 Verify that the file really is not locked
911 and that its contents match what the master file says."
912 :type 'boolean
913 :group 'vc)
914 (make-obsolete-variable 'vc-checkout-carefully
915 "the corresponding checks are always done now."
916 "21.1")
919 ;; Variables users don't need to see
921 (defvar vc-disable-async-diff nil
922 "VC sets this to t locally to disable some async diff operations.
923 Backends that offer asynchronous diffs should respect this variable
924 in their implementation of vc-BACKEND-diff.")
926 ;; File property caching
928 (defun vc-clear-context ()
929 "Clear all cached file properties."
930 (interactive)
931 (fillarray vc-file-prop-obarray 0))
933 (defmacro with-vc-properties (files form settings)
934 "Execute FORM, then maybe set per-file properties for FILES.
935 SETTINGS is an association list of property/value pairs. After
936 executing FORM, set those properties from SETTINGS that have not yet
937 been updated to their corresponding values."
938 (declare (debug t))
939 `(let ((vc-touched-properties (list t)))
940 ,form
941 (dolist (file ,files)
942 (dolist (setting ,settings)
943 (let ((property (car setting)))
944 (unless (memq property vc-touched-properties)
945 (put (intern file vc-file-prop-obarray)
946 property (cdr setting))))))))
948 ;;; Code for deducing what fileset and backend to assume
950 (defun vc-responsible-backend (file &optional register)
951 "Return the name of a backend system that is responsible for FILE.
952 The optional argument REGISTER means that a backend suitable for
953 registration should be found.
955 If REGISTER is nil, then if FILE is already registered, return the
956 backend of FILE. If FILE is not registered, or a directory, then the
957 first backend in `vc-handled-backends' that declares itself
958 responsible for FILE is returned. If no backend declares itself
959 responsible, return the first backend.
961 If REGISTER is non-nil, return the first responsible backend under
962 which FILE is not yet registered. If there is no such backend, return
963 the first backend under which FILE is not yet registered, but could
964 be registered."
965 (when (not vc-handled-backends)
966 (error "No handled backends"))
967 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
968 (catch 'found
969 ;; First try: find a responsible backend. If this is for registration,
970 ;; it must be a backend under which FILE is not yet registered.
971 (dolist (backend vc-handled-backends)
972 (and (or (not register)
973 (not (vc-call-backend backend 'registered file)))
974 (vc-call-backend backend 'responsible-p file)
975 (throw 'found backend)))
976 ;; no responsible backend
977 (if (not register)
978 ;; if this is not for registration, the first backend must do
979 (car vc-handled-backends)
980 ;; for registration, we need to find a new backend that
981 ;; could register FILE
982 (dolist (backend vc-handled-backends)
983 (and (not (vc-call-backend backend 'registered file))
984 (vc-call-backend backend 'could-register file)
985 (throw 'found backend)))
986 (error "No backend that could register")))))
988 (defun vc-expand-dirs (file-or-dir-list)
989 "Expands directories in a file list specification.
990 Within directories, only files already under version control are noticed."
991 (let ((flattened '()))
992 (dolist (node file-or-dir-list)
993 (if (file-directory-p node)
994 (vc-file-tree-walk
995 node (lambda (f) (when (vc-backend f) (push f flattened)))))
996 (push node flattened))
997 (nreverse flattened)))
999 (defun vc-derived-from-dir-mode (&optional buffer)
1000 "Are we in a VC-directory buffer, or do we have one as an ancestor?"
1001 (let ((buffer (or buffer (current-buffer))))
1002 (cond ((derived-mode-p 'vc-dir-mode) t)
1003 (vc-parent-buffer (vc-derived-from-dir-mode vc-parent-buffer))
1004 (t nil))))
1006 (defun vc-deduce-fileset (&optional observer)
1007 "Deduce a set of files and a backend to which to apply an operation and
1008 the common state of the fileset. Return (BACKEND . FILESET)."
1009 (let* ((selection (vc-dispatcher-selection-set observer))
1010 (raw (car selection)) ;; Selection as user made it
1011 (cooked (cdr selection)) ;; Files only
1012 ;; FIXME: Store the backend in a buffer-local variable.
1013 (backend (if (vc-derived-from-dir-mode (current-buffer))
1014 ;; FIXME: this should use vc-dir-backend from
1015 ;; the *vc-dir* buffer.
1016 (vc-responsible-backend default-directory)
1017 (assert (and (= 1 (length raw))
1018 (not (file-directory-p (car raw)))))
1019 (vc-backend (car cooked)))))
1020 (cons backend selection)))
1022 (defun vc-ensure-vc-buffer ()
1023 "Make sure that the current buffer visits a version-controlled file."
1024 (cond
1025 ((vc-dispatcher-browsing)
1026 (set-buffer (find-file-noselect (vc-dir-current-file))))
1028 (while (and vc-parent-buffer
1029 (buffer-live-p vc-parent-buffer)
1030 ;; Avoid infinite looping when vc-parent-buffer and
1031 ;; current buffer are the same buffer.
1032 (not (eq vc-parent-buffer (current-buffer))))
1033 (set-buffer vc-parent-buffer))
1034 (if (not buffer-file-name)
1035 (error "Buffer %s is not associated with a file" (buffer-name))
1036 (unless (vc-backend buffer-file-name)
1037 (error "File %s is not under version control" buffer-file-name))))))
1039 ;;; Support for the C-x v v command.
1040 ;; This is where all the single-file-oriented code from before the fileset
1041 ;; rewrite lives.
1043 (defsubst vc-editable-p (file)
1044 "Return non-nil if FILE can be edited."
1045 (let ((backend (vc-backend file)))
1046 (and backend
1047 (or (eq (vc-checkout-model backend (list file)) 'implicit)
1048 (memq (vc-state file) '(edited needs-merge conflict))))))
1050 (defun vc-compatible-state (p q)
1051 "Controls which states can be in the same commit."
1053 (eq p q)
1054 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1056 ;; Here's the major entry point.
1058 ;;;###autoload
1059 (defun vc-next-action (verbose)
1060 "Do the next logical version control operation on the current fileset.
1061 This requires that all files in the fileset be in the same state.
1063 For locking systems:
1064 If every file is not already registered, this registers each for version
1065 control.
1066 If every file is registered and not locked by anyone, this checks out
1067 a writable and locked file of each ready for editing.
1068 If every file is checked out and locked by the calling user, this
1069 first checks to see if each file has changed since checkout. If not,
1070 it performs a revert on that file.
1071 If every file has been changed, this pops up a buffer for entry
1072 of a log message; when the message has been entered, it checks in the
1073 resulting changes along with the log message as change commentary. If
1074 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1075 read-only copy of each changed file is left in place afterwards.
1076 If the affected file is registered and locked by someone else, you are
1077 given the option to steal the lock(s).
1079 For merging systems:
1080 If every file is not already registered, this registers each one for version
1081 control. This does an add, but not a commit.
1082 If every file is added but not committed, each one is committed.
1083 If every working file is changed, but the corresponding repository file is
1084 unchanged, this pops up a buffer for entry of a log message; when the
1085 message has been entered, it checks in the resulting changes along
1086 with the logmessage as change commentary. A writable file is retained.
1087 If the repository file is changed, you are asked if you want to
1088 merge in the changes into your working copy."
1089 (interactive "P")
1090 (let* ((vc-fileset (vc-deduce-fileset))
1091 (backend (car vc-fileset))
1092 (files (cadr vc-fileset))
1093 (fileset-only-files (cddr vc-fileset))
1094 ;; FIXME: We used to call `vc-recompute-state' here.
1095 (state (vc-state (car fileset-only-files)))
1096 ;; The backend should check that the checkout-model is consistent
1097 ;; among all the `files'.
1098 (model (vc-checkout-model backend files))
1099 revision)
1101 ;; Check that all files are in a consistent state, since we use that
1102 ;; state to decide which operation to perform.
1103 (dolist (file (cdr fileset-only-files))
1104 (unless (vc-compatible-state (vc-state file) state)
1105 (error "%s:%s clashes with %s:%s"
1106 file (vc-state file) (car fileset-only-files) state)))
1108 ;; Do the right thing
1109 (cond
1110 ((eq state 'missing)
1111 (error "Fileset files are missing, so cannot be operated on."))
1112 ((eq state 'ignored)
1113 (error "Fileset files are ignored by the version-control system."))
1114 ((eq state 'unregistered)
1115 (mapc (lambda (arg) (vc-register nil arg)) files))
1116 ;; Files are up-to-date, or need a merge and user specified a revision
1117 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-update)))
1118 (cond
1119 (verbose
1120 ;; go to a different revision
1121 (setq revision (read-string "Branch, revision, or backend to move to: "))
1122 (let ((vsym (intern-soft (upcase revision))))
1123 (if (member vsym vc-handled-backends)
1124 (dolist (file files) (vc-transfer-file file vsym))
1125 (dolist (file files)
1126 (vc-checkout file (eq model 'implicit) revision)))))
1127 ((not (eq model 'implicit))
1128 ;; check the files out
1129 (dolist (file files) (vc-checkout file t)))
1131 ;; do nothing
1132 (message "Fileset is up-to-date"))))
1133 ;; Files have local changes
1134 ((vc-compatible-state state 'edited)
1135 (let ((ready-for-commit files))
1136 ;; If files are edited but read-only, give user a chance to correct
1137 (dolist (file files)
1138 (unless (file-writable-p file)
1139 ;; Make the file+buffer read-write.
1140 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1141 (error "Aborted"))
1142 (set-file-modes file (logior (file-modes file) 128))
1143 (let ((visited (get-file-buffer file)))
1144 (when visited
1145 (with-current-buffer visited
1146 (toggle-read-only -1))))))
1147 ;; Allow user to revert files with no changes
1148 (save-excursion
1149 (dolist (file files)
1150 (let ((visited (get-file-buffer file)))
1151 ;; For files with locking, if the file does not contain
1152 ;; any changes, just let go of the lock, i.e. revert.
1153 (when (and (not (eq model 'implicit))
1154 (vc-workfile-unchanged-p file)
1155 ;; If buffer is modified, that means the user just
1156 ;; said no to saving it; in that case, don't revert,
1157 ;; because the user might intend to save after
1158 ;; finishing the log entry and committing.
1159 (not (and visited (buffer-modified-p))))
1160 (vc-revert-file file)
1161 (delete file ready-for-commit)))))
1162 ;; Remaining files need to be committed
1163 (if (not ready-for-commit)
1164 (message "No files remain to be committed")
1165 (if (not verbose)
1166 (vc-checkin ready-for-commit)
1167 (progn
1168 (setq revision (read-string "New revision or backend: "))
1169 (let ((vsym (intern (upcase revision))))
1170 (if (member vsym vc-handled-backends)
1171 (dolist (file files) (vc-transfer-file file vsym))
1172 (vc-checkin ready-for-commit revision))))))))
1173 ;; locked by somebody else (locking VCSes only)
1174 ((stringp state)
1175 ;; In the old days, we computed the revision once and used it on
1176 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1177 ;; computed the revision once (incorrectly, using a free var) and
1178 ;; used it on all files. To fix the free var bug, we can either
1179 ;; use `(car files)' or do what we do here: distribute the
1180 ;; revision computation among `files'. Although this may be
1181 ;; tedious for those backends where a "revision" is a trans-file
1182 ;; concept, it is nonetheless correct for both those and (more
1183 ;; importantly) for those where "revision" is a per-file concept.
1184 ;; If the intersection of the former group and "locking VCSes" is
1185 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1186 ;; pre-computation approach of yore.
1187 (dolist (file files)
1188 (vc-steal-lock
1189 file (if verbose
1190 (read-string (format "%s revision to steal: " file))
1191 (vc-working-revision file))
1192 state)))
1193 ;; conflict
1194 ((eq state 'conflict)
1195 ;; FIXME: Is it really the UI we want to provide?
1196 ;; In my experience, the conflicted files should be marked as resolved
1197 ;; one-by-one when saving the file after resolving the conflicts.
1198 ;; I.e. stating explicitly that the conflicts are resolved is done
1199 ;; very rarely.
1200 (vc-mark-resolved backend files))
1201 ;; needs-update
1202 ((eq state 'needs-update)
1203 (dolist (file files)
1204 (if (yes-or-no-p (format
1205 "%s is not up-to-date. Get latest revision? "
1206 (file-name-nondirectory file)))
1207 (vc-checkout file (eq model 'implicit) t)
1208 (when (and (not (eq model 'implicit))
1209 (yes-or-no-p "Lock this revision? "))
1210 (vc-checkout file t)))))
1211 ;; needs-merge
1212 ((eq state 'needs-merge)
1213 (dolist (file files)
1214 (when (yes-or-no-p (format
1215 "%s is not up-to-date. Merge in changes now? "
1216 (file-name-nondirectory file)))
1217 (vc-maybe-resolve-conflicts
1218 file (vc-call-backend backend 'merge-news file)))))
1220 ;; unlocked-changes
1221 ((eq state 'unlocked-changes)
1222 (dolist (file files)
1223 (when (not (equal buffer-file-name file))
1224 (find-file-other-window file))
1225 (if (save-window-excursion
1226 (vc-diff-internal nil
1227 (cons (car vc-fileset) (cons (cadr vc-fileset) (list file)))
1228 (vc-working-revision file) nil)
1229 (goto-char (point-min))
1230 (let ((inhibit-read-only t))
1231 (insert
1232 (format "Changes to %s since last lock:\n\n" file)))
1233 (not (beep))
1234 (yes-or-no-p (concat "File has unlocked changes. "
1235 "Claim lock retaining changes? ")))
1236 (progn (vc-call-backend backend 'steal-lock file)
1237 (clear-visited-file-modtime)
1238 ;; Must clear any headers here because they wouldn't
1239 ;; show that the file is locked now.
1240 (vc-clear-headers file)
1241 (write-file buffer-file-name)
1242 (vc-mode-line file))
1243 (if (not (yes-or-no-p
1244 "Revert to checked-in revision, instead? "))
1245 (error "Checkout aborted")
1246 (vc-revert-buffer-internal t t)
1247 (vc-checkout file t)))))
1248 ;; Unknown fileset state
1250 (error "Fileset is in an unknown state %s" state)))))
1252 (defun vc-create-repo (backend)
1253 "Create an empty repository in the current directory."
1254 (interactive
1255 (list
1256 (intern
1257 (upcase
1258 (completing-read
1259 "Create repository for: "
1260 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1261 nil t)))))
1262 (vc-call-backend backend 'create-repo))
1264 ;;;###autoload
1265 (defun vc-register (&optional set-revision fname comment)
1266 "Register into a version control system.
1267 If FNAME is given register that file, otherwise register the current file.
1268 With prefix argument SET-REVISION, allow user to specify initial revision
1269 level. If COMMENT is present, use that as an initial comment.
1271 The version control system to use is found by cycling through the list
1272 `vc-handled-backends'. The first backend in that list which declares
1273 itself responsible for the file (usually because other files in that
1274 directory are already registered under that backend) will be used to
1275 register the file. If no backend declares itself responsible, the
1276 first backend that could register the file is used."
1277 (interactive "P")
1278 (when (and (null fname) (null buffer-file-name)) (error "No visited file"))
1280 (let ((bname (if fname (get-file-buffer fname) (current-buffer))))
1281 (unless fname (setq fname buffer-file-name))
1282 (when (vc-backend fname)
1283 (if (vc-registered fname)
1284 (error "This file is already registered")
1285 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1286 (error "Aborted"))))
1287 ;; Watch out for new buffers of size 0: the corresponding file
1288 ;; does not exist yet, even though buffer-modified-p is nil.
1289 (when bname
1290 (with-current-buffer bname
1291 (when (and (not (buffer-modified-p))
1292 (zerop (buffer-size))
1293 (not (file-exists-p buffer-file-name)))
1294 (set-buffer-modified-p t))
1295 (vc-buffer-sync)))
1296 (vc-start-logentry (list fname)
1297 (if set-revision
1298 (read-string (format "Initial revision level for %s: "
1299 fname))
1300 (vc-call-backend (vc-responsible-backend fname)
1301 'init-revision))
1302 (or comment (not vc-initial-comment))
1304 "Enter initial comment."
1305 "*VC-log*"
1306 (lambda (files rev comment)
1307 (dolist (file files)
1308 (message "Registering %s... " file)
1309 (let ((backend (vc-responsible-backend file t)))
1310 (vc-file-clearprops file)
1311 (vc-call-backend backend 'register (list file) rev comment)
1312 (vc-file-setprop file 'vc-backend backend)
1313 (unless vc-make-backup-files
1314 (make-local-variable 'backup-inhibited)
1315 (setq backup-inhibited t)))
1316 (message "Registering %s... done" file))))))
1318 (defun vc-register-with (backend)
1319 "Register the current file with a specified back end."
1320 (interactive "SBackend: ")
1321 (when (not (member backend vc-handled-backends))
1322 (error "Unknown back end."))
1323 (let ((vc-handled-backends (list backend)))
1324 (call-interactively 'vc-register)))
1326 (defun vc-checkout (file &optional writable rev)
1327 "Retrieve a copy of the revision REV of FILE.
1328 If WRITABLE is non-nil, make sure the retrieved file is writable.
1329 REV defaults to the latest revision.
1331 After check-out, runs the normal hook `vc-checkout-hook'."
1332 (and writable
1333 (not rev)
1334 (vc-call make-version-backups-p file)
1335 (vc-up-to-date-p file)
1336 (vc-make-version-backup file))
1337 (let ((backend (vc-backend file)))
1338 (with-vc-properties (list file)
1339 (condition-case err
1340 (vc-call-backend backend 'checkout file writable rev)
1341 (file-error
1342 ;; Maybe the backend is not installed ;-(
1343 (when writable
1344 (let ((buf (get-file-buffer file)))
1345 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1346 (signal (car err) (cdr err))))
1347 `((vc-state . ,(if (or (eq (vc-checkout-model backend (list file)) 'implicit)
1348 (not writable))
1349 (if (vc-call-backend backend 'latest-on-branch-p file)
1350 'up-to-date
1351 'needs-update)
1352 'edited))
1353 (vc-checkout-time . ,(nth 5 (file-attributes file))))))
1354 (vc-resynch-buffer file t t)
1355 (run-hooks 'vc-checkout-hook))
1357 (defun vc-mark-resolved (backend files)
1358 (with-vc-properties
1359 files
1360 (vc-call-backend backend 'mark-resolved files)
1361 ;; XXX: Is this TRTD? Might not be.
1362 `((vc-state . edited))))
1364 (defun vc-steal-lock (file rev owner)
1365 "Steal the lock on FILE."
1366 (let (file-description)
1367 (if rev
1368 (setq file-description (format "%s:%s" file rev))
1369 (setq file-description file))
1370 (when (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1371 file-description owner)))
1372 (error "Steal canceled"))
1373 (message "Stealing lock on %s..." file)
1374 (with-vc-properties
1375 (list file)
1376 (vc-call steal-lock file rev)
1377 `((vc-state . edited)))
1378 (vc-resynch-buffer file t t)
1379 (message "Stealing lock on %s...done" file)
1380 ;; Write mail after actually stealing, because if the stealing
1381 ;; goes wrong, we don't want to send any mail.
1382 (compose-mail owner (format "Stolen lock on %s" file-description))
1383 (setq default-directory (expand-file-name "~/"))
1384 (goto-char (point-max))
1385 (insert
1386 (format "I stole the lock on %s, " file-description)
1387 (current-time-string)
1388 ".\n")
1389 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1391 (defun vc-checkin (files &optional rev comment initial-contents)
1392 "Check in FILES.
1393 The optional argument REV may be a string specifying the new revision
1394 level (if nil increment the current level). COMMENT is a comment
1395 string; if omitted, a buffer is popped up to accept a comment. If
1396 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1397 of the log entry buffer.
1399 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1400 that the version control system supports this mode of operation.
1402 Runs the normal hooks `vc-before-checkin-hook' and `vc-checkin-hook'."
1403 (when vc-before-checkin-hook
1404 (run-hooks 'vc-before-checkin-hook))
1405 (vc-start-logentry
1406 files rev comment initial-contents
1407 "Enter a change comment."
1408 "*VC-log*"
1409 (lambda (files rev comment)
1410 (message "Checking in %s..." (vc-delistify files))
1411 ;; "This log message intentionally left almost blank".
1412 ;; RCS 5.7 gripes about white-space-only comments too.
1413 (or (and comment (string-match "[^\t\n ]" comment))
1414 (setq comment "*** empty log message ***"))
1415 (with-vc-properties
1416 files
1417 ;; We used to change buffers to get local value of vc-checkin-switches,
1418 ;; but 'the' local buffer is not a well-defined concept for filesets.
1419 (progn
1420 (vc-call checkin files rev comment)
1421 (mapc 'vc-delete-automatic-version-backups files))
1422 `((vc-state . up-to-date)
1423 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1424 (vc-working-revision . nil)))
1425 (message "Checking in %s...done" (vc-delistify files)))
1426 'vc-checkin-hook))
1428 ;;; Additional entry points for examining version histories
1430 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1431 ;; "List differences for all registered files at and below DIR.
1432 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1433 ;; ;; This implementation does an explicit tree walk, and calls
1434 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1435 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1436 ;; ;; and to call it only for files that are actually changed.
1437 ;; ;; However, this is expensive for some backends, and so it is left
1438 ;; ;; to backend-specific implementations.
1439 ;; (setq default-directory dir)
1440 ;; (vc-file-tree-walk
1441 ;; default-directory
1442 ;; (lambda (f)
1443 ;; (vc-exec-after
1444 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1445 ;; (message "Looking at %s" ',f)
1446 ;; (vc-call-backend ',(vc-backend f)
1447 ;; 'diff (list ',f) ',rev1 ',rev2))))))
1449 (defun vc-coding-system-for-diff (file)
1450 "Return the coding system for reading diff output for FILE."
1451 (or coding-system-for-read
1452 ;; if we already have this file open,
1453 ;; use the buffer's coding system
1454 (let ((buf (find-buffer-visiting file)))
1455 (when buf (with-current-buffer buf
1456 buffer-file-coding-system)))
1457 ;; otherwise, try to find one based on the file name
1458 (car (find-operation-coding-system 'insert-file-contents file))
1459 ;; and a final fallback
1460 'undecided))
1462 (defun vc-switches (backend op)
1463 (let ((switches
1464 (or (when backend
1465 (let ((sym (vc-make-backend-sym
1466 backend (intern (concat (symbol-name op)
1467 "-switches")))))
1468 (when (boundp sym) (symbol-value sym))))
1469 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1470 (when (boundp sym) (symbol-value sym)))
1471 (cond
1472 ((eq op 'diff) diff-switches)))))
1473 (if (stringp switches) (list switches)
1474 ;; If not a list, return nil.
1475 ;; This is so we can set vc-diff-switches to t to override
1476 ;; any switches in diff-switches.
1477 (when (listp switches) switches))))
1479 ;; Old def for compatibility with Emacs-21.[123].
1480 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1481 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
1483 (defun vc-diff-finish (buffer messages)
1484 ;; The empty sync output case has already been handled, so the only
1485 ;; possibility of an empty output is for an async process.
1486 (when (buffer-live-p buffer)
1487 (let ((window (get-buffer-window buffer t))
1488 (emptyp (zerop (buffer-size buffer))))
1489 (with-current-buffer buffer
1490 (and messages emptyp
1491 (let ((inhibit-read-only t))
1492 (insert (cdr messages) ".\n")
1493 (message "%s" (cdr messages))))
1494 (goto-char (point-min))
1495 (when window
1496 (shrink-window-if-larger-than-buffer window)))
1497 (when (and messages (not emptyp))
1498 (message "%sdone" (car messages))))))
1500 (defvar vc-diff-added-files nil
1501 "If non-nil, diff added files by comparing them to /dev/null.")
1503 (defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose)
1504 "Report diffs between two revisions of a fileset.
1505 Diff output goes to the *vc-diff* buffer. The function
1506 returns t if the buffer had changes, nil otherwise."
1507 (let* ((files (cadr vc-fileset))
1508 (messages (cons (format "Finding changes in %s..."
1509 (vc-delistify files))
1510 (format "No changes between %s and %s"
1511 (or rev1 "working revision")
1512 (or rev2 "workfile"))))
1513 ;; Set coding system based on the first file. It's a kluge,
1514 ;; but the only way to set it for each file included would
1515 ;; be to call the back end separately for each file.
1516 (coding-system-for-read
1517 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
1518 (vc-setup-buffer "*vc-diff*")
1519 (message "%s" (car messages))
1520 ;; Many backends don't handle well the case of a file that has been
1521 ;; added but not yet committed to the repo (notably CVS and Subversion).
1522 ;; Do that work here so the backends don't have to futz with it. --ESR
1524 ;; Actually most backends (including CVS) have options to control the
1525 ;; behavior since which one is better depends on the user and on the
1526 ;; situation). Worse yet: this code does not handle the case where
1527 ;; `file' is a directory which contains added files.
1528 ;; I made it conditional on vc-diff-added-files but it should probably
1529 ;; just be removed (or copied/moved to specific backends). --Stef.
1530 (when vc-diff-added-files
1531 (let ((filtered '()))
1532 (dolist (file files)
1533 (if (or (file-directory-p file)
1534 (not (string= (vc-working-revision file) "0")))
1535 (push file filtered)
1536 ;; This file is added but not yet committed;
1537 ;; there is no master file to diff against.
1538 (if (or rev1 rev2)
1539 (error "No revisions of %s exist" file)
1540 ;; We regard this as "changed".
1541 ;; Diff it against /dev/null.
1542 (apply 'vc-do-command "*vc-diff*"
1543 1 "diff" file
1544 (append (vc-switches nil 'diff) '("/dev/null"))))))
1545 (setq files (nreverse filtered))))
1546 (let ((vc-disable-async-diff (not async)))
1547 (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 "*vc-diff*"))
1548 (set-buffer "*vc-diff*")
1549 (if (and (zerop (buffer-size))
1550 (not (get-buffer-process (current-buffer))))
1551 ;; Treat this case specially so as not to pop the buffer.
1552 (progn
1553 (message "%s" (cdr messages))
1554 nil)
1555 (diff-mode)
1556 ;; Make the *vc-diff* buffer read only, the diff-mode key
1557 ;; bindings are nicer for read only buffers. pcl-cvs does the
1558 ;; same thing.
1559 (setq buffer-read-only t)
1560 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
1561 messages)))
1562 ;; Display the buffer, but at the end because it can change point.
1563 (pop-to-buffer (current-buffer))
1564 ;; In the async case, we return t even if there are no differences
1565 ;; because we don't know that yet.
1566 t)))
1568 ;;;###autoload
1569 (defun vc-version-diff (files rev1 rev2)
1570 "Report diffs between revisions of the fileset in the repository history."
1571 (interactive
1572 (let* ((vc-fileset (vc-deduce-fileset t))
1573 (files (cadr vc-fileset))
1574 (backend (car vc-fileset))
1575 (first (car files))
1576 (completion-table
1577 (vc-call-backend backend 'revision-completion-table files))
1578 (rev1-default nil)
1579 (rev2-default nil))
1580 (cond
1581 ;; someday we may be able to do revision completion on non-singleton
1582 ;; filesets, but not yet.
1583 ((/= (length files) 1)
1584 nil)
1585 ;; if it's a directory, don't supply any revision default
1586 ((file-directory-p first)
1587 nil)
1588 ;; if the file is not up-to-date, use working revision as older revision
1589 ((not (vc-up-to-date-p first))
1590 (setq rev1-default (vc-working-revision first)))
1591 ;; if the file is not locked, use last and previous revisions as defaults
1593 (setq rev1-default (vc-call-backend backend 'previous-revision first
1594 (vc-working-revision first)))
1595 (when (string= rev1-default "") (setq rev1-default nil))
1596 (setq rev2-default (vc-working-revision first))))
1597 ;; construct argument list
1598 (let* ((rev1-prompt (if rev1-default
1599 (concat "Older revision (default "
1600 rev1-default "): ")
1601 "Older revision: "))
1602 (rev2-prompt (concat "Newer revision (default "
1603 (or rev2-default "current source") "): "))
1604 (rev1 (if completion-table
1605 (completing-read rev1-prompt completion-table
1606 nil nil nil nil rev1-default)
1607 (read-string rev1-prompt nil nil rev1-default)))
1608 (rev2 (if completion-table
1609 (completing-read rev2-prompt completion-table
1610 nil nil nil nil rev2-default)
1611 (read-string rev2-prompt nil nil rev2-default))))
1612 (when (string= rev1 "") (setq rev1 nil))
1613 (when (string= rev2 "") (setq rev2 nil))
1614 (list files rev1 rev2))))
1615 ;; All that was just so we could do argument completion!
1616 (when (and (not rev1) rev2)
1617 (error "Not a valid revision range."))
1618 ;; Yes, it's painful to call (vc-deduce-fileset) again. Alas, the
1619 ;; placement rules for (interactive) don't actually leave us a choice.
1620 (vc-diff-internal t (vc-deduce-fileset) rev1 rev2 (interactive-p)))
1622 ;; (defun vc-contains-version-controlled-file (dir)
1623 ;; "Return t if DIR contains a version-controlled file, nil otherwise."
1624 ;; (catch 'found
1625 ;; (mapc (lambda (f) (and (not (file-directory-p f)) (vc-backend f) (throw 'found 't))) (directory-files dir))
1626 ;; nil))
1628 ;;;###autoload
1629 (defun vc-diff (historic &optional not-urgent)
1630 "Display diffs between file revisions.
1631 Normally this compares the currently selected fileset with their
1632 working revisions. With a prefix argument HISTORIC, it reads two revision
1633 designators specifying which revisions to compare.
1635 The optional argument NOT-URGENT non-nil means it is ok to say no to
1636 saving the buffer."
1637 (interactive (list current-prefix-arg t))
1638 (if historic
1639 (call-interactively 'vc-version-diff)
1640 (when buffer-file-name (vc-buffer-sync not-urgent))
1641 (vc-diff-internal t (vc-deduce-fileset) nil nil (interactive-p))))
1643 ;;;###autoload
1644 (defun vc-revision-other-window (rev)
1645 "Visit revision REV of the current file in another window.
1646 If the current file is named `F', the revision is named `F.~REV~'.
1647 If `F.~REV~' already exists, use it instead of checking it out again."
1648 (interactive
1649 (save-current-buffer
1650 (vc-ensure-vc-buffer)
1651 (let ((completion-table
1652 (vc-call revision-completion-table buffer-file-name))
1653 (prompt "Revision to visit (default is working revision): "))
1654 (list
1655 (if completion-table
1656 (completing-read prompt completion-table)
1657 (read-string prompt))))))
1658 (vc-ensure-vc-buffer)
1659 (let* ((file buffer-file-name)
1660 (revision (if (string-equal rev "")
1661 (vc-working-revision file)
1662 rev)))
1663 (switch-to-buffer-other-window (vc-find-revision file revision))))
1665 (defun vc-find-revision (file revision)
1666 "Read REVISION of FILE into a buffer and return the buffer."
1667 (let ((automatic-backup (vc-version-backup-file-name file revision))
1668 (filebuf (or (get-file-buffer file) (current-buffer)))
1669 (filename (vc-version-backup-file-name file revision 'manual)))
1670 (unless (file-exists-p filename)
1671 (if (file-exists-p automatic-backup)
1672 (rename-file automatic-backup filename nil)
1673 (message "Checking out %s..." filename)
1674 (with-current-buffer filebuf
1675 (let ((failed t))
1676 (unwind-protect
1677 (let ((coding-system-for-read 'no-conversion)
1678 (coding-system-for-write 'no-conversion))
1679 (with-temp-file filename
1680 (let ((outbuf (current-buffer)))
1681 ;; Change buffer to get local value of
1682 ;; vc-checkout-switches.
1683 (with-current-buffer filebuf
1684 (vc-call find-revision file revision outbuf))))
1685 (setq failed nil))
1686 (when (and failed (file-exists-p filename))
1687 (delete-file filename))))
1688 (vc-mode-line file))
1689 (message "Checking out %s...done" filename)))
1690 (let ((result-buf (find-file-noselect filename)))
1691 (with-current-buffer result-buf
1692 ;; Set the parent buffer so that things like
1693 ;; C-x v g, C-x v l, ... etc work.
1694 (set (make-local-variable 'vc-parent-buffer) filebuf))
1695 result-buf)))
1697 ;; Header-insertion code
1699 ;;;###autoload
1700 (defun vc-insert-headers ()
1701 "Insert headers into a file for use with a version control system.
1702 Headers desired are inserted at point, and are pulled from
1703 the variable `vc-BACKEND-header'."
1704 (interactive)
1705 (vc-ensure-vc-buffer)
1706 (save-excursion
1707 (save-restriction
1708 (widen)
1709 (when (or (not (vc-check-headers))
1710 (y-or-n-p "Version headers already exist. Insert another set? "))
1711 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1712 (comment-start-vc (or (car delims) comment-start "#"))
1713 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1714 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
1715 'header))
1716 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1717 (dolist (s hdstrings)
1718 (insert comment-start-vc "\t" s "\t"
1719 comment-end-vc "\n"))
1720 (when vc-static-header-alist
1721 (dolist (f vc-static-header-alist)
1722 (when (string-match (car f) buffer-file-name)
1723 (insert (format (cdr f) (car hdstrings)))))))))))
1725 (defun vc-clear-headers (&optional file)
1726 "Clear all version headers in the current buffer (or FILE).
1727 The headers are reset to their non-expanded form."
1728 (let* ((filename (or file buffer-file-name))
1729 (visited (find-buffer-visiting filename))
1730 (backend (vc-backend filename)))
1731 (when (vc-find-backend-function backend 'clear-headers)
1732 (if visited
1733 (let ((context (vc-buffer-context)))
1734 ;; save-excursion may be able to relocate point and mark
1735 ;; properly. If it fails, vc-restore-buffer-context
1736 ;; will give it a second try.
1737 (save-excursion
1738 (vc-call-backend backend 'clear-headers))
1739 (vc-restore-buffer-context context))
1740 (set-buffer (find-file-noselect filename))
1741 (vc-call-backend backend 'clear-headers)
1742 (kill-buffer filename)))))
1744 (defun vc-modify-change-comment (files rev oldcomment)
1745 "Edit the comment associated with the given files and revision."
1746 (vc-start-logentry
1747 files rev oldcomment t
1748 "Enter a replacement change comment."
1749 "*VC-log*"
1750 (lambda (files rev comment)
1751 (vc-call-backend
1752 ;; Less of a kluge than it looks like; log-view mode only passes
1753 ;; this function a singleton list. Arguments left in this form in
1754 ;; case the more general operation ever becomes meaningful.
1755 (vc-responsible-backend (car files))
1756 'modify-change-comment files rev comment))))
1758 ;;;###autoload
1759 (defun vc-merge ()
1760 "Merge changes between two revisions into the current buffer's file.
1761 This asks for two revisions to merge from in the minibuffer. If the
1762 first revision is a branch number, then merge all changes from that
1763 branch. If the first revision is empty, merge news, i.e. recent changes
1764 from the current branch.
1766 See Info node `Merging'."
1767 (interactive)
1768 (vc-ensure-vc-buffer)
1769 (vc-buffer-sync)
1770 (let* ((file buffer-file-name)
1771 (backend (vc-backend file))
1772 (state (vc-state file))
1773 first-revision second-revision status)
1774 (cond
1775 ((stringp state) ;; Locking VCses only
1776 (error "File is locked by %s" state))
1777 ((not (vc-editable-p file))
1778 (if (y-or-n-p
1779 "File must be checked out for merging. Check out now? ")
1780 (vc-checkout file t)
1781 (error "Merge aborted"))))
1782 (setq first-revision
1783 (read-string (concat "Branch or revision to merge from "
1784 "(default news on current branch): ")))
1785 (if (string= first-revision "")
1786 (setq status (vc-call-backend backend 'merge-news file))
1787 (if (not (vc-find-backend-function backend 'merge))
1788 (error "Sorry, merging is not implemented for %s" backend)
1789 (if (not (vc-branch-p first-revision))
1790 (setq second-revision
1791 (read-string "Second revision: "
1792 (concat (vc-branch-part first-revision) ".")))
1793 ;; We want to merge an entire branch. Set revisions
1794 ;; accordingly, so that vc-BACKEND-merge understands us.
1795 (setq second-revision first-revision)
1796 ;; first-revision must be the starting point of the branch
1797 (setq first-revision (vc-branch-part first-revision)))
1798 (setq status (vc-call-backend backend 'merge file
1799 first-revision second-revision))))
1800 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
1802 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1803 (vc-resynch-buffer file t (not (buffer-modified-p)))
1804 (if (zerop status) (message "Merge successful")
1805 (smerge-mode 1)
1806 (message "File contains conflicts.")))
1808 ;;;###autoload
1809 (defalias 'vc-resolve-conflicts 'smerge-ediff)
1811 ;; VC status implementation
1813 (defun vc-default-status-extra-headers (backend dir)
1814 ;; Be loud by default to remind people to add code to display
1815 ;; backend specific headers.
1816 ;; XXX: change this to return nil before the release.
1817 "Extra : Add backend specific headers here")
1819 (defun vc-dir-headers (backend dir)
1820 "Display the headers in the *VC status* buffer.
1821 It calls the `status-extra-headers' backend method to display backend
1822 specific headers."
1823 (concat
1824 (propertize "VC backend: " 'face 'font-lock-type-face)
1825 (propertize (format "%s\n" backend) 'face 'font-lock-variable-name-face)
1826 (propertize "Working dir: " 'face 'font-lock-type-face)
1827 (propertize (format "%s\n" dir) 'face 'font-lock-variable-name-face)
1828 (vc-call-backend backend 'status-extra-headers dir)
1829 "\n"))
1831 (defun vc-default-status-printer (backend fileentry)
1832 "Pretty print FILEENTRY."
1833 ;; If you change the layout here, change vc-dir-move-to-goal-column.
1834 (let* ((isdir (vc-dir-fileinfo->directory fileentry))
1835 (state (if isdir 'DIRECTORY (vc-dir-fileinfo->state fileentry)))
1836 (filename (vc-dir-fileinfo->name fileentry))
1837 (prettified (if isdir state (vc-call-backend backend 'prettify-state-info filename))))
1838 (insert
1839 (propertize
1840 (format "%c" (if (vc-dir-fileinfo->marked fileentry) ?* ? ))
1841 'face 'font-lock-type-face)
1843 (propertize
1844 (format "%-20s" prettified)
1845 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
1846 ((memq state '(missing conflict)) 'font-lock-warning-face)
1847 (t 'font-lock-variable-name-face))
1848 'mouse-face 'highlight)
1850 (propertize
1851 (format "%s" filename)
1852 'face 'font-lock-function-name-face
1853 'mouse-face 'highlight))))
1855 (defun vc-default-extra-status-menu (backend)
1856 nil)
1858 (defun vc-dir-refresh-files (files default-state)
1859 "Refresh some files in the VC status buffer."
1860 (let ((backend (vc-responsible-backend default-directory))
1861 (status-buffer (current-buffer))
1862 (def-dir default-directory))
1863 (vc-set-mode-line-busy-indicator)
1864 ;; Call the `dir-status-file' backend function.
1865 ;; `dir-status-file' is supposed to be asynchronous.
1866 ;; It should compute the results, and then call the function
1867 ;; passed as an argument in order to update the vc-dir buffer
1868 ;; with the results.
1869 (unless (buffer-live-p vc-dir-process-buffer)
1870 (setq vc-dir-process-buffer
1871 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1872 (lexical-let ((buffer (current-buffer)))
1873 (with-current-buffer vc-dir-process-buffer
1874 (cd def-dir)
1875 (erase-buffer)
1876 (vc-call-backend
1877 backend 'dir-status-files def-dir files default-state
1878 (lambda (entries &optional more-to-come)
1879 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1880 ;; If MORE-TO-COME is true, then more updates will come from
1881 ;; the asynchronous process.
1882 (with-current-buffer buffer
1883 (vc-dir-update entries buffer)
1884 (unless more-to-come
1885 (setq mode-line-process nil)
1886 ;; Remove the ones that haven't been updated at all.
1887 ;; Those not-updated are those whose state is nil because the
1888 ;; file/dir doesn't exist and isn't versioned.
1889 (ewoc-filter vc-ewoc
1890 (lambda (info)
1891 (not (vc-dir-fileinfo->needs-update info))))))))))))
1893 (defun vc-dir-refresh ()
1894 "Refresh the contents of the VC status buffer.
1895 Throw an error if another update process is in progress."
1896 (interactive)
1897 (if (vc-dir-busy)
1898 (error "Another update process is in progress, cannot run two at a time")
1899 (let ((backend (vc-responsible-backend default-directory))
1900 (status-buffer (current-buffer))
1901 (def-dir default-directory))
1902 (vc-set-mode-line-busy-indicator)
1903 ;; Call the `dir-status' backend function.
1904 ;; `dir-status' is supposed to be asynchronous.
1905 ;; It should compute the results, and then call the function
1906 ;; passed as an argument in order to update the vc-dir buffer
1907 ;; with the results.
1909 ;; Create a buffer that can be used by `dir-status' and call
1910 ;; `dir-status' with this buffer as the current buffer. Use
1911 ;; `vc-dir-process-buffer' to remember this buffer, so that
1912 ;; it can be used later to kill the update process in case it
1913 ;; takes too long.
1914 (unless (buffer-live-p vc-dir-process-buffer)
1915 (setq vc-dir-process-buffer
1916 (generate-new-buffer (format " *VC-%s* tmp status" backend))))
1917 ;; set the needs-update flag on all entries
1918 (ewoc-map (lambda (info) (setf (vc-dir-fileinfo->needs-update info) t) nil)
1919 vc-ewoc)
1920 (lexical-let ((buffer (current-buffer)))
1921 (with-current-buffer vc-dir-process-buffer
1922 (cd def-dir)
1923 (erase-buffer)
1924 (vc-call-backend
1925 backend 'dir-status def-dir
1926 (lambda (entries &optional more-to-come)
1927 ;; ENTRIES is a list of (FILE VC_STATE EXTRA) items.
1928 ;; If MORE-TO-COME is true, then more updates will come from
1929 ;; the asynchronous process.
1930 (with-current-buffer buffer
1931 (vc-dir-update entries buffer)
1932 (unless more-to-come
1933 (let ((remaining
1934 (ewoc-collect
1935 vc-ewoc 'vc-dir-fileinfo->needs-update)))
1936 (if remaining
1937 (vc-dir-refresh-files
1938 (mapcar 'vc-dir-fileinfo->name remaining)
1939 'up-to-date)
1940 (setq mode-line-process nil))))))))))))
1942 (defun vc-dir-show-fileentry (file)
1943 "Insert an entry for a specific file into the current VC status listing.
1944 This is typically used if the file is up-to-date (or has been added
1945 outside of VC) and one wants to do some operation on it."
1946 (interactive "fShow file: ")
1947 (vc-dir-update (list (list (file-relative-name file) (vc-state file))) (current-buffer)))
1949 (defun vc-dir-hide-up-to-date ()
1950 "Hide up-to-date items from display."
1951 (interactive)
1952 (ewoc-filter
1953 vc-ewoc
1954 (lambda (crt) (not (eq (vc-dir-fileinfo->state crt) 'up-to-date)))))
1956 (defun vc-dir-register ()
1957 "Register the marked files, or the current file if no marks."
1958 (interactive)
1959 ;; FIXME: Just pass the fileset to vc-register.
1960 (mapc (lambda (arg) (vc-register nil arg))
1961 (or (vc-dir-marked-files) (list (vc-dir-current-file)))))
1963 (defun vc-default-status-fileinfo-extra (backend file)
1964 "Default absence of extra information returned for a file."
1965 nil)
1967 (defvar vc-dir-backend nil
1968 "The backend used by the current *vc-dir* buffer.")
1970 ;; FIXME: Replace these with a more efficient dispatch
1972 (defun vc-generic-status-printer (fileentry)
1973 (vc-call-backend vc-dir-backend 'status-printer fileentry))
1975 (defun vc-generic-state (file)
1976 (vc-call-backend vc-dir-backend 'state file))
1978 (defun vc-generic-status-fileinfo-extra (file)
1979 (vc-call-backend vc-dir-backend 'status-fileinfo-extra file))
1981 (defun vc-dir-extra-menu ()
1982 (vc-call-backend vc-dir-backend 'extra-status-menu))
1984 (defun vc-make-backend-object (file-or-dir)
1985 "Create the backend capability object needed by vc-dispatcher."
1986 (vc-create-client-object
1987 "VC status"
1988 (vc-dir-headers vc-dir-backend file-or-dir)
1989 #'vc-generic-status-printer
1990 #'vc-generic-state
1991 #'vc-generic-status-fileinfo-extra
1992 #'vc-dir-refresh
1993 #'vc-dir-extra-menu))
1995 ;;;###autoload
1996 (defun vc-dir (dir)
1997 "Show the VC status for DIR."
1998 (interactive "DVC status for directory: ")
1999 (pop-to-buffer (vc-dir-prepare-status-buffer "*vc-dir*" dir))
2000 (if (and (derived-mode-p 'vc-dir-mode) (boundp 'client-object))
2001 (vc-dir-refresh)
2002 ;; Otherwise, initialize a new view using the dispatcher layer
2003 (progn
2004 (set (make-local-variable 'vc-dir-backend) (vc-responsible-backend dir))
2005 ;; Build a capability object and hand it to the dispatcher initializer
2006 (vc-dir-mode (vc-make-backend-object dir))
2007 ;; FIXME: Make a derived-mode instead.
2008 ;; Add VC-specific keybindings
2009 (let ((map (current-local-map)))
2010 (define-key map "v" 'vc-diff) ;; C-x v v
2011 (define-key map "=" 'vc-diff) ;; C-x v =
2012 (define-key map "i" 'vc-dir-register) ;; C-x v i
2013 (define-key map "+" 'vc-update) ;; C-x v +
2014 (define-key map "l" 'vc-print-log) ;; C-x v l
2015 ;; More confusing than helpful, probably
2016 ;(define-key map "R" 'vc-revert) ;; u is taken by dispatcher unmark.
2017 ;(define-key map "A" 'vc-annotate) ;; g is taken by dispatcher refresh
2018 (define-key map "x" 'vc-dir-hide-up-to-date))
2020 ;; FIXME: Needs to alter a buffer-local map, otherwise clients may clash
2021 (let ((map vc-dir-menu-map))
2022 ;; VC info details
2023 (define-key map [sepvcdet] '("--"))
2024 (define-key map [remup]
2025 '(menu-item "Hide up-to-date" vc-dir-hide-up-to-date
2026 :help "Hide up-to-date items from display"))
2027 ;; FIXME: This needs a key binding. And maybe a better name
2028 ;; ("Insert" like PCL-CVS uses does not sound that great either)...
2029 (define-key map [ins]
2030 '(menu-item "Show File" vc-dir-show-fileentry
2031 :help "Show a file in the VC status listing even though it might be up to date"))
2032 (define-key map [annotate]
2033 '(menu-item "Annotate" vc-annotate
2034 :help "Display the edit history of the current file using colors"))
2035 (define-key map [diff]
2036 '(menu-item "Compare with Base Version" vc-diff
2037 :help "Compare file set with the base version"))
2038 (define-key map [log]
2039 '(menu-item "Show history" vc-print-log
2040 :help "List the change log of the current file set in a window"))
2041 ;; VC commands.
2042 (define-key map [sepvccmd] '("--"))
2043 (define-key map [update]
2044 '(menu-item "Update to latest version" vc-update
2045 :help "Update the current fileset's files to their tip revisions"))
2046 (define-key map [revert]
2047 '(menu-item "Revert to base version" vc-revert
2048 :help "Revert working copies of the selected fileset to their repository contents."))
2049 (define-key map [next-action]
2050 ;; FIXME: This really really really needs a better name!
2051 ;; And a key binding too.
2052 '(menu-item "Check In/Out" vc-next-action
2053 :help "Do the next logical version control operation on the current fileset"))
2054 (define-key map [register]
2055 '(menu-item "Register" vc-dir-register
2056 :help "Register file set into the version control system"))
2059 ;; Named-configuration entry points
2061 (defun vc-tag-precondition (dir)
2062 "Scan the tree below DIR, looking for files not up-to-date.
2063 If any file is not up-to-date, return the name of the first such file.
2064 \(This means, neither tag creation nor retrieval is allowed.\)
2065 If one or more of the files are currently visited, return `visited'.
2066 Otherwise, return nil."
2067 (let ((status nil))
2068 (catch 'vc-locked-example
2069 (vc-file-tree-walk
2071 (lambda (f)
2072 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2073 (when (get-file-buffer f) (setq status 'visited)))))
2074 status)))
2076 ;;;###autoload
2077 (defun vc-create-tag (dir name branchp)
2078 "Descending recursively from DIR, make a tag called NAME.
2079 For each registered file, the working revision becomes part of
2080 the named configuration. If the prefix argument BRANCHP is
2081 given, the tag is made as a new branch and the files are
2082 checked out in that new branch."
2083 (interactive
2084 (list (read-file-name "Directory: " default-directory default-directory t)
2085 (read-string "New tag name: ")
2086 current-prefix-arg))
2087 (message "Making %s... " (if branchp "branch" "tag"))
2088 (when (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2089 (vc-call-backend (vc-responsible-backend dir)
2090 'create-tag dir name branchp)
2091 (message "Making %s... done" (if branchp "branch" "tag")))
2093 ;;;###autoload
2094 (defun vc-retrieve-tag (dir name)
2095 "Descending recursively from DIR, retrieve the tag called NAME.
2096 If NAME is empty, it refers to the latest revisions.
2097 If locking is used for the files in DIR, then there must not be any
2098 locked files at or below DIR (but if NAME is empty, locked files are
2099 allowed and simply skipped)."
2100 (interactive
2101 (list (read-file-name "Directory: " default-directory default-directory t)
2102 (read-string "Tag name to retrieve (default latest revisions): ")))
2103 (let ((update (yes-or-no-p "Update any affected buffers? "))
2104 (msg (if (or (not name) (string= name ""))
2105 (format "Updating %s... " (abbreviate-file-name dir))
2106 (format "Retrieving tag into %s... "
2107 (abbreviate-file-name dir)))))
2108 (message "%s" msg)
2109 (vc-call-backend (vc-responsible-backend dir)
2110 'retrieve-tag dir name update)
2111 (message "%s" (concat msg "done"))))
2113 ;; Miscellaneous other entry points
2115 ;;;###autoload
2116 (defun vc-print-log (&optional working-revision)
2117 "List the change log of the current fileset in a window.
2118 If WORKING-REVISION is non-nil, leave the point at that revision."
2119 (interactive)
2120 (let* ((vc-fileset (vc-deduce-fileset t))
2121 (backend (car vc-fileset))
2122 (files (cadr vc-fileset))
2123 (working-revision (or working-revision (vc-working-revision (car files)))))
2124 ;; Don't switch to the output buffer before running the command,
2125 ;; so that any buffer-local settings in the vc-controlled
2126 ;; buffer can be accessed by the command.
2127 (vc-call-backend backend 'print-log files "*vc-change-log*")
2128 (pop-to-buffer "*vc-change-log*")
2129 (vc-exec-after
2130 `(let ((inhibit-read-only t))
2131 (vc-call-backend ',backend 'log-view-mode)
2132 (goto-char (point-max)) (forward-line -1)
2133 (while (looking-at "=*\n")
2134 (delete-char (- (match-end 0) (match-beginning 0)))
2135 (forward-line -1))
2136 (goto-char (point-min))
2137 (when (looking-at "[\b\t\n\v\f\r ]+")
2138 (delete-char (- (match-end 0) (match-beginning 0))))
2139 (shrink-window-if-larger-than-buffer)
2140 ;; move point to the log entry for the working revision
2141 (vc-call-backend ',backend 'show-log-entry ',working-revision)
2142 (setq vc-sentinel-movepoint (point))
2143 (set-buffer-modified-p nil)))))
2145 ;;;###autoload
2146 (defun vc-revert ()
2147 "Revert working copies of the selected fileset to their repository contents.
2148 This asks for confirmation if the buffer contents are not identical
2149 to the working revision (except for keyword expansion)."
2150 (interactive)
2151 (let* ((vc-fileset (vc-deduce-fileset))
2152 (files (cadr vc-fileset)))
2153 ;; If any of the files is visited by the current buffer, make
2154 ;; sure buffer is saved. If the user says `no', abort since
2155 ;; we cannot show the changes and ask for confirmation to
2156 ;; discard them.
2157 (when (or (not files) (memq (buffer-file-name) files))
2158 (vc-buffer-sync nil))
2159 (dolist (file files)
2160 (let ((buf (get-file-buffer file)))
2161 (when (and buf (buffer-modified-p buf))
2162 (error "Please kill or save all modified buffers before reverting.")))
2163 (when (vc-up-to-date-p file)
2164 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2165 (error "Revert canceled"))))
2166 (when (vc-diff-internal vc-allow-async-revert vc-fileset nil nil)
2167 (unless (yes-or-no-p (format "Discard changes in %s? " (vc-delistify files)))
2168 (error "Revert canceled"))
2169 (delete-windows-on "*vc-diff*")
2170 (kill-buffer "*vc-diff*"))
2171 (dolist (file files)
2172 (message "Reverting %s..." (vc-delistify files))
2173 (vc-revert-file file)
2174 (message "Reverting %s...done" (vc-delistify files)))))
2176 ;;;###autoload
2177 (defun vc-rollback ()
2178 "Roll back (remove) the most recent changeset committed to the repository.
2179 This may be either a file-level or a repository-level operation,
2180 depending on the underlying version-control system."
2181 (interactive)
2182 (let* ((vc-fileset (vc-deduce-fileset))
2183 (backend (car vc-fileset))
2184 (files (cadr vc-fileset))
2185 (granularity (vc-call-backend backend 'revision-granularity)))
2186 (unless (vc-find-backend-function backend 'rollback)
2187 (error "Rollback is not supported in %s" backend))
2188 (when (and (not (eq granularity 'repository)) (/= (length files) 1))
2189 (error "Rollback requires a singleton fileset or repository versioning"))
2190 ;; FIXME: latest-on-branch-p should take the fileset.
2191 (when (not (vc-call-backend backend 'latest-on-branch-p (car files)))
2192 (error "Rollback is only possible at the tip revision."))
2193 ;; If any of the files is visited by the current buffer, make
2194 ;; sure buffer is saved. If the user says `no', abort since
2195 ;; we cannot show the changes and ask for confirmation to
2196 ;; discard them.
2197 (when (or (not files) (memq (buffer-file-name) files))
2198 (vc-buffer-sync nil))
2199 (dolist (file files)
2200 (when (buffer-modified-p (get-file-buffer file))
2201 (error "Please kill or save all modified buffers before rollback."))
2202 (when (not (vc-up-to-date-p file))
2203 (error "Please revert all modified workfiles before rollback.")))
2204 ;; Accumulate changes associated with the fileset
2205 (vc-setup-buffer "*vc-diff*")
2206 (not-modified)
2207 (message "Finding changes...")
2208 (let* ((tip (vc-working-revision (car files)))
2209 ;; FIXME: `previous-revision' should take the fileset.
2210 (previous (vc-call-backend backend 'previous-revision
2211 (car files) tip)))
2212 (vc-diff-internal nil vc-fileset previous tip))
2213 ;; Display changes
2214 (unless (yes-or-no-p "Discard these revisions? ")
2215 (error "Rollback canceled"))
2216 (delete-windows-on "*vc-diff*")
2217 (kill-buffer"*vc-diff*")
2218 ;; Do the actual reversions
2219 (message "Rolling back %s..." (vc-delistify files))
2220 (with-vc-properties
2221 files
2222 (vc-call-backend backend 'rollback files)
2223 `((vc-state . ,'up-to-date)
2224 (vc-checkout-time . , (nth 5 (file-attributes file)))
2225 (vc-working-revision . nil)))
2226 (dolist (f files) (vc-resynch-buffer f t t))
2227 (message "Rolling back %s...done" (vc-delistify files))))
2229 ;;;###autoload
2230 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2232 ;;;###autoload
2233 (defun vc-update ()
2234 "Update the current fileset's files to their tip revisions.
2235 For each one that contains no changes, and is not locked, then this simply
2236 replaces the work file with the latest revision on its branch. If the file
2237 contains changes, and the backend supports merging news, then any recent
2238 changes from the current branch are merged into the working file."
2239 (interactive)
2240 (let* ((vc-fileset (vc-deduce-fileset))
2241 (backend (car vc-fileset))
2242 (files (cadr vc-fileset)))
2243 (dolist (file files)
2244 (when (let ((buf (get-file-buffer file)))
2245 (and buf (buffer-modified-p buf)))
2246 (error "Please kill or save all modified buffers before updating."))
2247 (if (vc-up-to-date-p file)
2248 (vc-checkout file nil t)
2249 (if (eq (vc-checkout-model backend (list file)) 'locking)
2250 (if (eq (vc-state file) 'edited)
2251 (error "%s"
2252 (substitute-command-keys
2253 "File is locked--type \\[vc-revert] to discard changes"))
2254 (error "Unexpected file state (%s) -- type %s"
2255 (vc-state file)
2256 (substitute-command-keys
2257 "\\[vc-next-action] to correct")))
2258 (vc-maybe-resolve-conflicts
2259 file (vc-call-backend backend 'merge-news file)))))))
2261 (defun vc-version-backup-file (file &optional rev)
2262 "Return name of backup file for revision REV of FILE.
2263 If version backups should be used for FILE, and there exists
2264 such a backup for REV or the working revision of file, return
2265 its name; otherwise return nil."
2266 (when (vc-call make-version-backups-p file)
2267 (let ((backup-file (vc-version-backup-file-name file rev)))
2268 (if (file-exists-p backup-file)
2269 backup-file
2270 ;; there is no automatic backup, but maybe the user made one manually
2271 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2272 (when (file-exists-p backup-file)
2273 backup-file)))))
2275 (defun vc-revert-file (file)
2276 "Revert FILE back to the repository working revision it was based on."
2277 (with-vc-properties
2278 (list file)
2279 (let ((backup-file (vc-version-backup-file file)))
2280 (when backup-file
2281 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2282 (vc-delete-automatic-version-backups file))
2283 (vc-call revert file backup-file))
2284 `((vc-state . up-to-date)
2285 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2286 (vc-resynch-buffer file t t))
2288 ;;;###autoload
2289 (defun vc-switch-backend (file backend)
2290 "Make BACKEND the current version control system for FILE.
2291 FILE must already be registered in BACKEND. The change is not
2292 permanent, only for the current session. This function only changes
2293 VC's perspective on FILE, it does not register or unregister it.
2294 By default, this command cycles through the registered backends.
2295 To get a prompt, use a prefix argument."
2296 (interactive
2297 (list
2298 (or buffer-file-name
2299 (error "There is no version-controlled file in this buffer"))
2300 (let ((backend (vc-backend buffer-file-name))
2301 (backends nil))
2302 (unless backend
2303 (error "File %s is not under version control" buffer-file-name))
2304 ;; Find the registered backends.
2305 (dolist (backend vc-handled-backends)
2306 (when (vc-call-backend backend 'registered buffer-file-name)
2307 (push backend backends)))
2308 ;; Find the next backend.
2309 (let ((def (car (delq backend (append (memq backend backends) backends))))
2310 (others (delete backend backends)))
2311 (cond
2312 ((null others) (error "No other backend to switch to"))
2313 (current-prefix-arg
2314 (intern
2315 (upcase
2316 (completing-read
2317 (format "Switch to backend [%s]: " def)
2318 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2319 nil t nil nil (downcase (symbol-name def))))))
2320 (t def))))))
2321 (unless (eq backend (vc-backend file))
2322 (vc-file-clearprops file)
2323 (vc-file-setprop file 'vc-backend backend)
2324 ;; Force recomputation of the state
2325 (unless (vc-call-backend backend 'registered file)
2326 (vc-file-clearprops file)
2327 (error "%s is not registered in %s" file backend))
2328 (vc-mode-line file)))
2330 ;;;###autoload
2331 (defun vc-transfer-file (file new-backend)
2332 "Transfer FILE to another version control system NEW-BACKEND.
2333 If NEW-BACKEND has a higher precedence than FILE's current backend
2334 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2335 NEW-BACKEND, using the revision number from the current backend as the
2336 base level. If NEW-BACKEND has a lower precedence than the current
2337 backend, then commit all changes that were made under the current
2338 backend to NEW-BACKEND, and unregister FILE from the current backend.
2339 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2340 (let* ((old-backend (vc-backend file))
2341 (edited (memq (vc-state file) '(edited needs-merge)))
2342 (registered (vc-call-backend new-backend 'registered file))
2343 (move
2344 (and registered ; Never move if not registered in new-backend yet.
2345 ;; move if new-backend comes later in vc-handled-backends
2346 (or (memq new-backend (memq old-backend vc-handled-backends))
2347 (y-or-n-p "Final transfer? "))))
2348 (comment nil))
2349 (when (eq old-backend new-backend)
2350 (error "%s is the current backend of %s" new-backend file))
2351 (if registered
2352 (set-file-modes file (logior (file-modes file) 128))
2353 ;; `registered' might have switched under us.
2354 (vc-switch-backend file old-backend)
2355 (let* ((rev (vc-working-revision file))
2356 (modified-file (and edited (make-temp-file file)))
2357 (unmodified-file (and modified-file (vc-version-backup-file file))))
2358 ;; Go back to the base unmodified file.
2359 (unwind-protect
2360 (progn
2361 (when modified-file
2362 (copy-file file modified-file 'ok-if-already-exists)
2363 ;; If we have a local copy of the unmodified file, handle that
2364 ;; here and not in vc-revert-file because we don't want to
2365 ;; delete that copy -- it is still useful for OLD-BACKEND.
2366 (if unmodified-file
2367 (copy-file unmodified-file file
2368 'ok-if-already-exists 'keep-date)
2369 (when (y-or-n-p "Get base revision from master? ")
2370 (vc-revert-file file))))
2371 (vc-call-backend new-backend 'receive-file file rev))
2372 (when modified-file
2373 (vc-switch-backend file new-backend)
2374 (unless (eq (vc-checkout-model new-backend (list file)) 'implicit)
2375 (vc-checkout file t nil))
2376 (rename-file modified-file file 'ok-if-already-exists)
2377 (vc-file-setprop file 'vc-checkout-time nil)))))
2378 (when move
2379 (vc-switch-backend file old-backend)
2380 (setq comment (vc-call-backend old-backend 'comment-history file))
2381 (vc-call-backend old-backend 'unregister file))
2382 (vc-switch-backend file new-backend)
2383 (when (or move edited)
2384 (vc-file-setprop file 'vc-state 'edited)
2385 (vc-mode-line file)
2386 (vc-checkin file nil comment (stringp comment)))))
2388 (defun vc-rename-master (oldmaster newfile templates)
2389 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2390 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2391 (newdir (or (file-name-directory newfile) ""))
2392 (newbase (file-name-nondirectory newfile))
2393 (masters
2394 ;; List of potential master files for `newfile'
2395 (mapcar
2396 (lambda (s) (vc-possible-master s newdir newbase))
2397 templates)))
2398 (when (or (file-symlink-p oldmaster)
2399 (file-symlink-p (file-name-directory oldmaster)))
2400 (error "This is unsafe in the presence of symbolic links"))
2401 (rename-file
2402 oldmaster
2403 (catch 'found
2404 ;; If possible, keep the master file in the same directory.
2405 (dolist (f masters)
2406 (when (and f (string= (file-name-directory (expand-file-name f)) dir))
2407 (throw 'found f)))
2408 ;; If not, just use the first possible place.
2409 (dolist (f masters)
2410 (and f (or (not (setq dir (file-name-directory f)))
2411 (file-directory-p dir))
2412 (throw 'found f)))
2413 (error "New file lacks a version control directory")))))
2415 (defun vc-delete-file (file)
2416 "Delete file and mark it as such in the version control system."
2417 (interactive "fVC delete file: ")
2418 (setq file (expand-file-name file))
2419 (let ((buf (get-file-buffer file))
2420 (backend (vc-backend file)))
2421 (unless backend
2422 (error "File %s is not under version control"
2423 (file-name-nondirectory file)))
2424 (unless (vc-find-backend-function backend 'delete-file)
2425 (error "Deleting files under %s is not supported in VC" backend))
2426 (when (and buf (buffer-modified-p buf))
2427 (error "Please save or undo your changes before deleting %s" file))
2428 (let ((state (vc-state file)))
2429 (when (eq state 'edited)
2430 (error "Please commit or undo your changes before deleting %s" file))
2431 (when (eq state 'conflict)
2432 (error "Please resolve the conflicts before deleting %s" file)))
2433 (unless (y-or-n-p (format "Really want to delete %s? "
2434 (file-name-nondirectory file)))
2435 (error "Abort!"))
2436 (unless (or (file-directory-p file) (null make-backup-files)
2437 (not (file-exists-p file)))
2438 (with-current-buffer (or buf (find-file-noselect file))
2439 (let ((backup-inhibited nil))
2440 (backup-buffer))
2441 ;; If we didn't have a buffer visiting the file before this
2442 ;; command, kill the buffer created by the above
2443 ;; `find-file-noselect' call.
2444 (unless buf (kill-buffer (current-buffer)))))
2445 (vc-call-backend backend 'delete-file file)
2446 ;; If the backend hasn't deleted the file itself, let's do it for him.
2447 (when (file-exists-p file) (delete-file file))
2448 ;; Forget what VC knew about the file.
2449 (vc-file-clearprops file)
2450 (vc-resynch-buffer file buf t)))
2452 ;;;###autoload
2453 (defun vc-rename-file (old new)
2454 "Rename file OLD to NEW, and rename its master file likewise."
2455 (interactive "fVC rename file: \nFRename to: ")
2456 (let ((oldbuf (get-file-buffer old)))
2457 (when (and oldbuf (buffer-modified-p oldbuf))
2458 (error "Please save files before moving them"))
2459 (when (get-file-buffer new)
2460 (error "Already editing new file name"))
2461 (when (file-exists-p new)
2462 (error "New file already exists"))
2463 (let ((state (vc-state old)))
2464 (unless (memq state '(up-to-date edited))
2465 (error "Please %s files before moving them"
2466 (if (stringp state) "check in" "update"))))
2467 (vc-call rename-file old new)
2468 (vc-file-clearprops old)
2469 ;; Move the actual file (unless the backend did it already)
2470 (when (file-exists-p old) (rename-file old new))
2471 ;; ?? Renaming a file might change its contents due to keyword expansion.
2472 ;; We should really check out a new copy if the old copy was precisely equal
2473 ;; to some checked-in revision. However, testing for this is tricky....
2474 (when oldbuf
2475 (with-current-buffer oldbuf
2476 (let ((buffer-read-only buffer-read-only))
2477 (set-visited-file-name new))
2478 (vc-backend new)
2479 (vc-mode-line new)
2480 (set-buffer-modified-p nil)))))
2482 ;;;###autoload
2483 (defun vc-update-change-log (&rest args)
2484 "Find change log file and add entries from recent version control logs.
2485 Normally, find log entries for all registered files in the default
2486 directory.
2488 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2490 With any numeric prefix arg, find log entries for all currently visited
2491 files that are under version control. This puts all the entries in the
2492 log for the default directory, which may not be appropriate.
2494 From a program, any ARGS are assumed to be filenames for which
2495 log entries should be gathered."
2496 (interactive
2497 (cond ((consp current-prefix-arg) ;C-u
2498 (list buffer-file-name))
2499 (current-prefix-arg ;Numeric argument.
2500 (let ((files nil)
2501 (buffers (buffer-list))
2502 file)
2503 (while buffers
2504 (setq file (buffer-file-name (car buffers)))
2505 (and file (vc-backend file)
2506 (setq files (cons file files)))
2507 (setq buffers (cdr buffers)))
2508 files))
2510 ;; Don't supply any filenames to backend; this means
2511 ;; it should find all relevant files relative to
2512 ;; the default-directory.
2513 nil)))
2514 (vc-call-backend (vc-responsible-backend default-directory)
2515 'update-changelog args))
2517 ;;; The default back end. Assumes RCS-like revision numbering.
2519 (defun vc-default-revision-granularity ()
2520 (error "Your backend will not work with this version of VC mode."))
2522 ;; functions that operate on RCS revision numbers. This code should
2523 ;; also be moved into the backends. It stays for now, however, since
2524 ;; it is used in code below.
2525 ;;;###autoload
2526 (defun vc-trunk-p (rev)
2527 "Return t if REV is a revision on the trunk."
2528 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
2530 (defun vc-branch-p (rev)
2531 "Return t if REV is a branch revision."
2532 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
2534 ;;;###autoload
2535 (defun vc-branch-part (rev)
2536 "Return the branch part of a revision number REV."
2537 (let ((index (string-match "\\.[0-9]+\\'" rev)))
2538 (when index
2539 (substring rev 0 index))))
2541 (defun vc-minor-part (rev)
2542 "Return the minor revision number of a revision number REV."
2543 (string-match "[0-9]+\\'" rev)
2544 (substring rev (match-beginning 0) (match-end 0)))
2546 (defun vc-default-previous-revision (backend file rev)
2547 "Return the revision number immediately preceding REV for FILE,
2548 or nil if there is no previous revision. This default
2549 implementation works for MAJOR.MINOR-style revision numbers as
2550 used by RCS and CVS."
2551 (let ((branch (vc-branch-part rev))
2552 (minor-num (string-to-number (vc-minor-part rev))))
2553 (when branch
2554 (if (> minor-num 1)
2555 ;; revision does probably not start a branch or release
2556 (concat branch "." (number-to-string (1- minor-num)))
2557 (if (vc-trunk-p rev)
2558 ;; we are at the beginning of the trunk --
2559 ;; don't know anything to return here
2561 ;; we are at the beginning of a branch --
2562 ;; return revision of starting point
2563 (vc-branch-part branch))))))
2565 (defun vc-default-next-revision (backend file rev)
2566 "Return the revision number immediately following REV for FILE,
2567 or nil if there is no next revision. This default implementation
2568 works for MAJOR.MINOR-style revision numbers as used by RCS
2569 and CVS."
2570 (when (not (string= rev (vc-working-revision file)))
2571 (let ((branch (vc-branch-part rev))
2572 (minor-num (string-to-number (vc-minor-part rev))))
2573 (concat branch "." (number-to-string (1+ minor-num))))))
2575 (defun vc-default-responsible-p (backend file)
2576 "Indicate whether BACKEND is reponsible for FILE.
2577 The default is to return nil always."
2578 nil)
2580 (defun vc-default-could-register (backend file)
2581 "Return non-nil if BACKEND could be used to register FILE.
2582 The default implementation returns t for all files."
2585 (defun vc-default-latest-on-branch-p (backend file)
2586 "Return non-nil if FILE is the latest on its branch.
2587 This default implementation always returns non-nil, which means that
2588 editing non-current revisions is not supported by default."
2591 (defun vc-default-init-revision (backend) vc-default-init-revision)
2593 (defalias 'vc-cvs-update-changelog 'vc-update-changelog-rcs2log)
2594 (defalias 'vc-rcs-update-changelog 'vc-update-changelog-rcs2log)
2595 ;; FIXME: This should probably be moved to vc-rcs.el and replaced in
2596 ;; vc-cvs.el by code using cvs2cl.
2597 (defun vc-update-changelog-rcs2log (files)
2598 "Default implementation of update-changelog.
2599 Uses `rcs2log' which only works for RCS and CVS."
2600 ;; FIXME: We (c|sh)ould add support for cvs2cl
2601 (let ((odefault default-directory)
2602 (changelog (find-change-log))
2603 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2604 (tempfile (make-temp-file
2605 (expand-file-name "vc"
2606 (or small-temporary-file-directory
2607 temporary-file-directory))))
2608 (login-name (or user-login-name
2609 (format "uid%d" (number-to-string (user-uid)))))
2610 (full-name (or add-log-full-name
2611 (user-full-name)
2612 (user-login-name)
2613 (format "uid%d" (number-to-string (user-uid)))))
2614 (mailing-address (or add-log-mailing-address
2615 user-mail-address)))
2616 (find-file-other-window changelog)
2617 (barf-if-buffer-read-only)
2618 (vc-buffer-sync)
2619 (undo-boundary)
2620 (goto-char (point-min))
2621 (push-mark)
2622 (message "Computing change log entries...")
2623 (message "Computing change log entries... %s"
2624 (unwind-protect
2625 (progn
2626 (setq default-directory odefault)
2627 (if (eq 0 (apply 'call-process
2628 (expand-file-name "rcs2log"
2629 exec-directory)
2630 nil (list t tempfile) nil
2631 "-c" changelog
2632 "-u" (concat login-name
2633 "\t" full-name
2634 "\t" mailing-address)
2635 (mapcar
2636 (lambda (f)
2637 (file-relative-name
2638 (expand-file-name f odefault)))
2639 files)))
2640 "done"
2641 (pop-to-buffer (get-buffer-create "*vc*"))
2642 (erase-buffer)
2643 (insert-file-contents tempfile)
2644 "failed"))
2645 (setq default-directory (file-name-directory changelog))
2646 (delete-file tempfile)))))
2648 (defun vc-default-find-revision (backend file rev buffer)
2649 "Provide the new `find-revision' op based on the old `checkout' op.
2650 This is only for compatibility with old backends. They should be updated
2651 to provide the `find-revision' operation instead."
2652 (let ((tmpfile (make-temp-file (expand-file-name file))))
2653 (unwind-protect
2654 (progn
2655 (vc-call-backend backend 'checkout file nil rev tmpfile)
2656 (with-current-buffer buffer
2657 (insert-file-contents-literally tmpfile)))
2658 (delete-file tmpfile))))
2660 (defun vc-default-prettify-state-info (backend file)
2661 (let* ((state (vc-state file))
2662 (statestring
2663 (cond
2664 ((stringp state) (concat "(locked:" state ")"))
2665 ((eq state 'edited) "(modified)")
2666 ((eq state 'needs-merge) "(merge)")
2667 ((eq state 'needs-update) "(update)")
2668 ((eq state 'added) "(added)")
2669 ((eq state 'removed) "(removed)")
2670 ((eq state 'ignored) "(ignored)")
2671 ((eq state 'unregistered) "(unregistered)")
2672 ((eq state 'unlocked-changes) "(stale)")
2673 (t (concat "(unknown:" state ")"))))
2674 (buffer
2675 (get-file-buffer file))
2676 (modflag
2677 (if (and buffer (buffer-modified-p buffer)) "+" "")))
2678 (concat statestring modflag)))
2680 (defun vc-default-rename-file (backend old new)
2681 (condition-case nil
2682 (add-name-to-file old new)
2683 (error (rename-file old new)))
2684 (vc-delete-file old)
2685 (with-current-buffer (find-file-noselect new)
2686 (vc-register)))
2688 (defalias 'vc-default-check-headers 'ignore)
2690 (defun vc-default-log-view-mode (backend) (log-view-mode))
2692 (defun vc-default-show-log-entry (backend rev)
2693 (with-no-warnings
2694 (log-view-goto-rev rev)))
2696 (defun vc-default-comment-history (backend file)
2697 "Return a string with all log entries stored in BACKEND for FILE."
2698 (when (vc-find-backend-function backend 'print-log)
2699 (with-current-buffer "*vc*"
2700 (vc-call-backend backend 'print-log (list file))
2701 (buffer-string))))
2703 (defun vc-default-receive-file (backend file rev)
2704 "Let BACKEND receive FILE from another version control system."
2705 (vc-call-backend backend 'register file rev ""))
2707 (defun vc-default-retrieve-tag (backend dir name update)
2708 (if (string= name "")
2709 (progn
2710 (vc-file-tree-walk
2712 (lambda (f) (and
2713 (vc-up-to-date-p f)
2714 (vc-error-occurred
2715 (vc-call-backend backend 'checkout f nil "")
2716 (when update (vc-resynch-buffer f t t)))))))
2717 (let ((result (vc-tag-precondition dir)))
2718 (if (stringp result)
2719 (error "File %s is locked" result)
2720 (setq update (and (eq result 'visited) update))
2721 (vc-file-tree-walk
2723 (lambda (f) (vc-error-occurred
2724 (vc-call-backend backend 'checkout f nil name)
2725 (when update (vc-resynch-buffer f t t)))))))))
2727 (defun vc-default-revert (backend file contents-done)
2728 (unless contents-done
2729 (let ((rev (vc-working-revision file))
2730 (file-buffer (or (get-file-buffer file) (current-buffer))))
2731 (message "Checking out %s..." file)
2732 (let ((failed t)
2733 (backup-name (car (find-backup-file-name file))))
2734 (when backup-name
2735 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
2736 (unless (file-writable-p file)
2737 (set-file-modes file (logior (file-modes file) 128))))
2738 (unwind-protect
2739 (let ((coding-system-for-read 'no-conversion)
2740 (coding-system-for-write 'no-conversion))
2741 (with-temp-file file
2742 (let ((outbuf (current-buffer)))
2743 ;; Change buffer to get local value of vc-checkout-switches.
2744 (with-current-buffer file-buffer
2745 (let ((default-directory (file-name-directory file)))
2746 (vc-call-backend backend 'find-revision
2747 file rev outbuf)))))
2748 (setq failed nil))
2749 (when backup-name
2750 (if failed
2751 (rename-file backup-name file 'ok-if-already-exists)
2752 (and (not vc-make-backup-files) (delete-file backup-name))))))
2753 (message "Checking out %s...done" file))))
2755 (defalias 'vc-default-revision-completion-table 'ignore)
2757 (defun vc-default-dir-status-files (backend dir files default-state update-function)
2758 (funcall update-function
2759 (mapcar (lambda (file) (list file default-state)) files)))
2761 (defun vc-check-headers ()
2762 "Check if the current file has any headers in it."
2763 (interactive)
2764 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
2766 ;;; Annotate functionality
2768 ;; Declare globally instead of additional parameter to
2769 ;; temp-buffer-show-function (not possible to pass more than one
2770 ;; parameter). The use of annotate-ratio is deprecated in favor of
2771 ;; annotate-mode, which replaces it with the more sensible "span-to
2772 ;; days", along with autoscaling support.
2773 (defvar vc-annotate-ratio nil "Global variable.")
2775 ;; internal buffer-local variables
2776 (defvar vc-annotate-backend nil)
2777 (defvar vc-annotate-parent-file nil)
2778 (defvar vc-annotate-parent-rev nil)
2779 (defvar vc-annotate-parent-display-mode nil)
2781 (defconst vc-annotate-font-lock-keywords
2782 ;; The fontification is done by vc-annotate-lines instead of font-lock.
2783 '((vc-annotate-lines)))
2785 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2786 "Major mode for output buffers of the `vc-annotate' command.
2788 You can use the mode-specific menu to alter the time-span of the used
2789 colors. See variable `vc-annotate-menu-elements' for customizing the
2790 menu items."
2791 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
2792 ;; it will become a list, to avoid initial annotations being invisible.
2793 (add-to-invisibility-spec 'foo)
2794 (remove-from-invisibility-spec 'foo)
2795 (set (make-local-variable 'truncate-lines) t)
2796 (set (make-local-variable 'font-lock-defaults)
2797 '(vc-annotate-font-lock-keywords t))
2798 (view-mode 1))
2800 (defun vc-annotate-toggle-annotation-visibility ()
2801 "Toggle whether or not the annotation is visible."
2802 (interactive)
2803 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
2804 'remove-from-invisibility-spec
2805 'add-to-invisibility-spec)
2806 'vc-annotate-annotation)
2807 (force-window-update (current-buffer)))
2809 (defun vc-annotate-display-default (ratio)
2810 "Display the output of \\[vc-annotate] using the default color range.
2811 The color range is given by `vc-annotate-color-map', scaled by RATIO.
2812 The current time is used as the offset."
2813 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
2814 (message "Redisplaying annotation...")
2815 (vc-annotate-display ratio)
2816 (message "Redisplaying annotation...done"))
2818 (defun vc-annotate-oldest-in-map (color-map)
2819 "Return the oldest time in the COLOR-MAP."
2820 ;; Since entries should be sorted, we can just use the last one.
2821 (caar (last color-map)))
2823 (defun vc-annotate-get-time-set-line-props ()
2824 (let ((bol (point))
2825 (date (vc-call-backend vc-annotate-backend 'annotate-time))
2826 (inhibit-read-only t))
2827 (assert (>= (point) bol))
2828 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
2829 date))
2831 (defun vc-annotate-display-autoscale (&optional full)
2832 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
2833 Autoscaling means that the map is scaled from the current time to the
2834 oldest annotation in the buffer, or, with prefix argument FULL, to
2835 cover the range from the oldest annotation to the newest."
2836 (interactive "P")
2837 (let ((newest 0.0)
2838 (oldest 999999.) ;Any CVS users at the founding of Rome?
2839 (current (vc-annotate-convert-time (current-time)))
2840 date)
2841 (message "Redisplaying annotation...")
2842 ;; Run through this file and find the oldest and newest dates annotated.
2843 (save-excursion
2844 (goto-char (point-min))
2845 (while (not (eobp))
2846 (when (setq date (vc-annotate-get-time-set-line-props))
2847 (when (> date newest)
2848 (setq newest date))
2849 (when (< date oldest)
2850 (setq oldest date)))
2851 (forward-line 1)))
2852 (vc-annotate-display
2853 (/ (- (if full newest current) oldest)
2854 (vc-annotate-oldest-in-map vc-annotate-color-map))
2855 (if full newest))
2856 (message "Redisplaying annotation...done \(%s\)"
2857 (if full
2858 (format "Spanned from %.1f to %.1f days old"
2859 (- current oldest)
2860 (- current newest))
2861 (format "Spanned to %.1f days old" (- current oldest))))))
2863 ;; Menu -- Using easymenu.el
2864 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
2865 "VC Annotate Display Menu"
2866 `("VC-Annotate"
2867 ["By Color Map Range" (unless (null vc-annotate-display-mode)
2868 (setq vc-annotate-display-mode nil)
2869 (vc-annotate-display-select))
2870 :style toggle :selected (null vc-annotate-display-mode)]
2871 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
2872 (mapcar (lambda (element)
2873 (let ((days (* element oldest-in-map)))
2874 `[,(format "Span %.1f days" days)
2875 (vc-annotate-display-select nil ,days)
2876 :style toggle :selected
2877 (eql vc-annotate-display-mode ,days) ]))
2878 vc-annotate-menu-elements))
2879 ["Span ..."
2880 (vc-annotate-display-select
2881 nil (float (string-to-number (read-string "Span how many days? "))))]
2882 "--"
2883 ["Span to Oldest"
2884 (unless (eq vc-annotate-display-mode 'scale)
2885 (vc-annotate-display-select nil 'scale))
2886 :help
2887 "Use an autoscaled color map from the oldest annotation to the current time"
2888 :style toggle :selected
2889 (eq vc-annotate-display-mode 'scale)]
2890 ["Span Oldest->Newest"
2891 (unless (eq vc-annotate-display-mode 'fullscale)
2892 (vc-annotate-display-select nil 'fullscale))
2893 :help
2894 "Use an autoscaled color map from the oldest to the newest annotation"
2895 :style toggle :selected
2896 (eq vc-annotate-display-mode 'fullscale)]
2897 "--"
2898 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
2899 :help
2900 "Toggle whether the annotation is visible or not"]
2901 ["Annotate previous revision" vc-annotate-prev-revision
2902 :help "Visit the annotation of the revision previous to this one"]
2903 ["Annotate next revision" vc-annotate-next-revision
2904 :help "Visit the annotation of the revision after this one"]
2905 ["Annotate revision at line" vc-annotate-revision-at-line
2906 :help
2907 "Visit the annotation of the revision identified in the current line"]
2908 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
2909 :help "Visit the annotation of the revision before the revision at line"]
2910 ["Annotate latest revision" vc-annotate-working-revision
2911 :help "Visit the annotation of the working revision of this file"]
2912 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
2913 :help "Visit the log of the revision at line"]
2914 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
2915 :help
2916 "Visit the diff of the revision at line from its previous revision"]
2917 ["Visit revision at line" vc-annotate-find-revision-at-line
2918 :help "Visit the revision identified in the current line"]))
2920 (defun vc-annotate-display-select (&optional buffer mode)
2921 "Highlight the output of \\[vc-annotate].
2922 By default, the current buffer is highlighted, unless overridden by
2923 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
2924 use; you may override this using the second optional arg MODE."
2925 (interactive)
2926 (when mode (setq vc-annotate-display-mode mode))
2927 (pop-to-buffer (or buffer (current-buffer)))
2928 (cond ((null vc-annotate-display-mode)
2929 ;; The ratio is global, thus relative to the global color-map.
2930 (kill-local-variable 'vc-annotate-color-map)
2931 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
2932 ;; One of the auto-scaling modes
2933 ((eq vc-annotate-display-mode 'scale)
2934 (vc-exec-after `(vc-annotate-display-autoscale)))
2935 ((eq vc-annotate-display-mode 'fullscale)
2936 (vc-exec-after `(vc-annotate-display-autoscale t)))
2937 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
2938 (vc-annotate-display-default
2939 (/ vc-annotate-display-mode
2940 (vc-annotate-oldest-in-map vc-annotate-color-map))))
2941 (t (error "No such display mode: %s"
2942 vc-annotate-display-mode))))
2944 ;;;###autoload
2945 (defun vc-annotate (file rev &optional display-mode buf move-point-to)
2946 "Display the edit history of the current file using colors.
2948 This command creates a buffer that shows, for each line of the current
2949 file, when it was last edited and by whom. Additionally, colors are
2950 used to show the age of each line--blue means oldest, red means
2951 youngest, and intermediate colors indicate intermediate ages. By
2952 default, the time scale stretches back one year into the past;
2953 everything that is older than that is shown in blue.
2955 With a prefix argument, this command asks two questions in the
2956 minibuffer. First, you may enter a revision number; then the buffer
2957 displays and annotates that revision instead of the working revision
2958 \(type RET in the minibuffer to leave that default unchanged). Then,
2959 you are prompted for the time span in days which the color range
2960 should cover. For example, a time span of 20 days means that changes
2961 over the past 20 days are shown in red to blue, according to their
2962 age, and everything that is older than that is shown in blue.
2964 If MOVE-POINT-TO is given, move the point to that line.
2966 Customization variables:
2968 `vc-annotate-menu-elements' customizes the menu elements of the
2969 mode-specific menu. `vc-annotate-color-map' and
2970 `vc-annotate-very-old-color' define the mapping of time to colors.
2971 `vc-annotate-background' specifies the background color."
2972 (interactive
2973 (save-current-buffer
2974 (vc-ensure-vc-buffer)
2975 (list buffer-file-name
2976 (let ((def (vc-working-revision buffer-file-name)))
2977 (if (null current-prefix-arg) def
2978 (read-string
2979 (format "Annotate from revision (default %s): " def)
2980 nil nil def)))
2981 (if (null current-prefix-arg)
2982 vc-annotate-display-mode
2983 (float (string-to-number
2984 (read-string "Annotate span days (default 20): "
2985 nil nil "20")))))))
2986 (vc-ensure-vc-buffer)
2987 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
2988 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
2989 (temp-buffer-show-function 'vc-annotate-display-select)
2990 ;; If BUF is specified, we presume the caller maintains current line,
2991 ;; so we don't need to do it here. This implementation may give
2992 ;; strange results occasionally in the case of REV != WORKFILE-REV.
2993 (current-line (or move-point-to (unless buf (line-number-at-pos)))))
2994 (message "Annotating...")
2995 ;; If BUF is specified it tells in which buffer we should put the
2996 ;; annotations. This is used when switching annotations to another
2997 ;; revision, so we should update the buffer's name.
2998 (when buf (with-current-buffer buf
2999 (rename-buffer temp-buffer-name t)
3000 ;; In case it had to be uniquified.
3001 (setq temp-buffer-name (buffer-name))))
3002 (with-output-to-temp-buffer temp-buffer-name
3003 (let ((backend (vc-backend file)))
3004 (vc-call-backend backend 'annotate-command file
3005 (get-buffer temp-buffer-name) rev)
3006 ;; we must setup the mode first, and then set our local
3007 ;; variables before the show-function is called at the exit of
3008 ;; with-output-to-temp-buffer
3009 (with-current-buffer temp-buffer-name
3010 (unless (equal major-mode 'vc-annotate-mode)
3011 (vc-annotate-mode))
3012 (set (make-local-variable 'vc-annotate-backend) backend)
3013 (set (make-local-variable 'vc-annotate-parent-file) file)
3014 (set (make-local-variable 'vc-annotate-parent-rev) rev)
3015 (set (make-local-variable 'vc-annotate-parent-display-mode)
3016 display-mode))))
3018 (with-current-buffer temp-buffer-name
3019 (vc-exec-after
3020 `(progn
3021 ;; Ideally, we'd rather not move point if the user has already
3022 ;; moved it elsewhere, but really point here is not the position
3023 ;; of the user's cursor :-(
3024 (when ,current-line ;(and (bobp))
3025 (goto-line ,current-line)
3026 (setq vc-sentinel-movepoint (point)))
3027 (unless (active-minibuffer-window)
3028 (message "Annotating... done")))))))
3030 (defun vc-annotate-prev-revision (prefix)
3031 "Visit the annotation of the revision previous to this one.
3033 With a numeric prefix argument, annotate the revision that many
3034 revisions previous."
3035 (interactive "p")
3036 (vc-annotate-warp-revision (- 0 prefix)))
3038 (defun vc-annotate-next-revision (prefix)
3039 "Visit the annotation of the revision after this one.
3041 With a numeric prefix argument, annotate the revision that many
3042 revisions after."
3043 (interactive "p")
3044 (vc-annotate-warp-revision prefix))
3046 (defun vc-annotate-working-revision ()
3047 "Visit the annotation of the working revision of this file."
3048 (interactive)
3049 (if (not (equal major-mode 'vc-annotate-mode))
3050 (message "Cannot be invoked outside of a vc annotate buffer")
3051 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
3052 (if (equal warp-rev vc-annotate-parent-rev)
3053 (message "Already at revision %s" warp-rev)
3054 (vc-annotate-warp-revision warp-rev)))))
3056 (defun vc-annotate-extract-revision-at-line ()
3057 "Extract the revision number of the current line."
3058 ;; This function must be invoked from a buffer in vc-annotate-mode
3059 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
3061 (defun vc-annotate-revision-at-line ()
3062 "Visit the annotation of the revision identified in the current line."
3063 (interactive)
3064 (if (not (equal major-mode 'vc-annotate-mode))
3065 (message "Cannot be invoked outside of a vc annotate buffer")
3066 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3067 (if (not rev-at-line)
3068 (message "Cannot extract revision number from the current line")
3069 (if (equal rev-at-line vc-annotate-parent-rev)
3070 (message "Already at revision %s" rev-at-line)
3071 (vc-annotate-warp-revision rev-at-line))))))
3073 (defun vc-annotate-find-revision-at-line ()
3074 "Visit the revision identified in the current line."
3075 (interactive)
3076 (if (not (equal major-mode 'vc-annotate-mode))
3077 (message "Cannot be invoked outside of a vc annotate buffer")
3078 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3079 (if (not rev-at-line)
3080 (message "Cannot extract revision number from the current line")
3081 (vc-revision-other-window rev-at-line)))))
3083 (defun vc-annotate-revision-previous-to-line ()
3084 "Visit the annotation of the revision before the revision at line."
3085 (interactive)
3086 (if (not (equal major-mode 'vc-annotate-mode))
3087 (message "Cannot be invoked outside of a vc annotate buffer")
3088 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3089 (prev-rev nil))
3090 (if (not rev-at-line)
3091 (message "Cannot extract revision number from the current line")
3092 (setq prev-rev
3093 (vc-call-backend vc-annotate-backend 'previous-revision
3094 vc-annotate-parent-file rev-at-line))
3095 (vc-annotate-warp-revision prev-rev)))))
3097 (defun vc-annotate-show-log-revision-at-line ()
3098 "Visit the log of the revision at line."
3099 (interactive)
3100 (if (not (equal major-mode 'vc-annotate-mode))
3101 (message "Cannot be invoked outside of a vc annotate buffer")
3102 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3103 (if (not rev-at-line)
3104 (message "Cannot extract revision number from the current line")
3105 (vc-print-log rev-at-line)))))
3107 (defun vc-annotate-show-diff-revision-at-line ()
3108 "Visit the diff of the revision at line from its previous revision."
3109 (interactive)
3110 (if (not (equal major-mode 'vc-annotate-mode))
3111 (message "Cannot be invoked outside of a vc annotate buffer")
3112 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3113 (prev-rev nil))
3114 (if (not rev-at-line)
3115 (message "Cannot extract revision number from the current line")
3116 (setq prev-rev
3117 (vc-call-backend vc-annotate-backend 'previous-revision
3118 vc-annotate-parent-file rev-at-line))
3119 (if (not prev-rev)
3120 (message "Cannot diff from any revision prior to %s" rev-at-line)
3121 (save-window-excursion
3122 (vc-diff-internal
3124 ;; The value passed here should follow what
3125 ;; `vc-deduce-fileset' returns.
3126 (cons vc-annotate-backend
3127 (cons (list vc-annotate-parent-file) nil))
3128 prev-rev rev-at-line))
3129 (switch-to-buffer "*vc-diff*"))))))
3131 (defun vc-annotate-warp-revision (revspec)
3132 "Annotate the revision described by REVSPEC.
3134 If REVSPEC is a positive integer, warp that many revisions
3135 forward, if possible, otherwise echo a warning message. If
3136 REVSPEC is a negative integer, warp that many revisions backward,
3137 if possible, otherwise echo a warning message. If REVSPEC is a
3138 string, then it describes a revision number, so warp to that
3139 revision."
3140 (if (not (equal major-mode 'vc-annotate-mode))
3141 (message "Cannot be invoked outside of a vc annotate buffer")
3142 (let* ((buf (current-buffer))
3143 (oldline (line-number-at-pos))
3144 (revspeccopy revspec)
3145 (newrev nil))
3146 (cond
3147 ((and (integerp revspec) (> revspec 0))
3148 (setq newrev vc-annotate-parent-rev)
3149 (while (and (> revspec 0) newrev)
3150 (setq newrev (vc-call-backend vc-annotate-backend 'next-revision
3151 vc-annotate-parent-file newrev))
3152 (setq revspec (1- revspec)))
3153 (unless newrev
3154 (message "Cannot increment %d revisions from revision %s"
3155 revspeccopy vc-annotate-parent-rev)))
3156 ((and (integerp revspec) (< revspec 0))
3157 (setq newrev vc-annotate-parent-rev)
3158 (while (and (< revspec 0) newrev)
3159 (setq newrev (vc-call-backend vc-annotate-backend 'previous-revision
3160 vc-annotate-parent-file newrev))
3161 (setq revspec (1+ revspec)))
3162 (unless newrev
3163 (message "Cannot decrement %d revisions from revision %s"
3164 (- 0 revspeccopy) vc-annotate-parent-rev)))
3165 ((stringp revspec) (setq newrev revspec))
3166 (t (error "Invalid argument to vc-annotate-warp-revision")))
3167 (when newrev
3168 (vc-annotate vc-annotate-parent-file newrev
3169 vc-annotate-parent-display-mode
3171 ;; Pass the current line so that vc-annotate will
3172 ;; place the point in the line.
3173 (min oldline (progn (goto-char (point-max))
3174 (forward-line -1)
3175 (line-number-at-pos))))))))
3177 (defun vc-annotate-compcar (threshold a-list)
3178 "Test successive cons cells of A-LIST against THRESHOLD.
3179 Return the first cons cell with a car that is not less than THRESHOLD,
3180 nil if no such cell exists."
3181 (let ((i 1)
3182 (tmp-cons (car a-list)))
3183 (while (and tmp-cons (< (car tmp-cons) threshold))
3184 (setq tmp-cons (car (nthcdr i a-list)))
3185 (setq i (+ i 1)))
3186 tmp-cons)) ; Return the appropriate value
3188 (defun vc-annotate-convert-time (time)
3189 "Convert a time value to a floating-point number of days.
3190 The argument TIME is a list as returned by `current-time' or
3191 `encode-time', only the first two elements of that list are considered."
3192 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3194 (defun vc-annotate-difference (&optional offset)
3195 "Return the time span in days to the next annotation.
3196 This calls the backend function annotate-time, and returns the
3197 difference in days between the time returned and the current time,
3198 or OFFSET if present."
3199 (let ((next-time (vc-annotate-get-time-set-line-props)))
3200 (when next-time
3201 (- (or offset
3202 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3203 next-time))))
3205 (defun vc-default-annotate-current-time (backend)
3206 "Return the current time, encoded as fractional days."
3207 (vc-annotate-convert-time (current-time)))
3209 (defvar vc-annotate-offset nil)
3211 (defun vc-annotate-display (ratio &optional offset)
3212 "Highlight `vc-annotate' output in the current buffer.
3213 RATIO, is the expansion that should be applied to `vc-annotate-color-map'.
3214 The annotations are relative to the current time, unless overridden by OFFSET."
3215 (when (/= ratio 1.0)
3216 (set (make-local-variable 'vc-annotate-color-map)
3217 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
3218 vc-annotate-color-map)))
3219 (set (make-local-variable 'vc-annotate-offset) offset)
3220 (font-lock-mode 1))
3222 (defun vc-annotate-lines (limit)
3223 (while (< (point) limit)
3224 (let ((difference (vc-annotate-difference vc-annotate-offset))
3225 (start (point))
3226 (end (progn (forward-line 1) (point))))
3227 (when difference
3228 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3229 (cons nil vc-annotate-very-old-color)))
3230 ;; substring from index 1 to remove any leading `#' in the name
3231 (face-name (concat "vc-annotate-face-"
3232 (if (string-equal
3233 (substring (cdr color) 0 1) "#")
3234 (substring (cdr color) 1)
3235 (cdr color))))
3236 ;; Make the face if not done.
3237 (face (or (intern-soft face-name)
3238 (let ((tmp-face (make-face (intern face-name))))
3239 (set-face-foreground tmp-face (cdr color))
3240 (when vc-annotate-background
3241 (set-face-background tmp-face
3242 vc-annotate-background))
3243 tmp-face)))) ; Return the face
3244 (put-text-property start end 'face face)))))
3245 ;; Pretend to font-lock there were no matches.
3246 nil)
3249 ;; These things should probably be generally available
3251 (defun vc-file-tree-walk (dirname func &rest args)
3252 "Walk recursively through DIRNAME.
3253 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3254 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3255 (message "Traversing directory %s...done" dirname))
3257 (defun vc-file-tree-walk-internal (file func args)
3258 (if (not (file-directory-p file))
3259 (when (vc-backend file) (apply func file args))
3260 (message "Traversing directory %s..." (abbreviate-file-name file))
3261 (let ((dir (file-name-as-directory file)))
3262 (mapcar
3263 (lambda (f) (or
3264 (string-equal f ".")
3265 (string-equal f "..")
3266 (member f vc-directory-exclusion-list)
3267 (let ((dirf (expand-file-name f dir)))
3269 (file-symlink-p dirf) ;; Avoid possible loops.
3270 (vc-file-tree-walk-internal dirf func args)))))
3271 (directory-files dir)))))
3273 (provide 'vc)
3275 ;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
3276 ;;; vc.el ends here