Stop vc-print-log from jumping to the top
[emacs.git] / lisp / vc / vc.el
blob1a997a4d183e663a4c724e6ff70368ddd1b21697
1 ;;; vc.el --- drive a version-control system from within Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-1998, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: FSF (see below for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Keywords: vc tools
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Credits:
26 ;; VC was initially designed and implemented by Eric S. Raymond
27 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
28 ;; contributed substantial amounts of work to VC. These include:
30 ;; Per Cederqvist <ceder@lysator.liu.se>
31 ;; Paul Eggert <eggert@twinsun.com>
32 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
33 ;; Martin Lorentzson <martinl@gnu.org>
34 ;; Dave Love <fx@gnu.org>
35 ;; Stefan Monnier <monnier@cs.yale.edu>
36 ;; Thien-Thi Nguyen <ttn@gnu.org>
37 ;; Dan Nicolaescu <dann@ics.uci.edu>
38 ;; J.D. Smith <jdsmith@alum.mit.edu>
39 ;; Andre Spiegel <spiegel@gnu.org>
40 ;; Richard Stallman <rms@gnu.org>
42 ;; In July 2007 ESR returned and redesigned the mode to cope better
43 ;; with modern version-control systems that do commits by fileset
44 ;; rather than per individual file.
46 ;; If you maintain a client of the mode or customize it in your .emacs,
47 ;; note that some backend functions which formerly took single file arguments
48 ;; now take a list of files. These include: register, checkin, print-log,
49 ;; and diff.
51 ;;; Commentary:
53 ;; This mode is fully documented in the Emacs user's manual.
55 ;; Supported version-control systems presently include CVS, RCS, SRC,
56 ;; GNU Subversion, Bzr, Git, Mercurial, Monotone and SCCS (or its free
57 ;; replacement, CSSC).
59 ;; If your site uses the ChangeLog convention supported by Emacs, the
60 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
61 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
62 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
64 ;; When using SCCS, RCS, CVS: be careful not to do repo surgery, or
65 ;; operations like registrations and deletions and renames, outside VC
66 ;; while VC is running. The support for these systems was designed
67 ;; when disks were much slower, and the code maintains a lot of
68 ;; internal state in order to reduce expensive operations to a
69 ;; minimum. Thus, if you mess with the repo while VC's back is turned,
70 ;; VC may get seriously confused.
72 ;; When using Subversion or a later system, anything you do outside VC
73 ;; *through the VCS tools* should safely interlock with VC
74 ;; operations. Under these VC does little state caching, because local
75 ;; operations are assumed to be fast.
77 ;; The 'assumed to be fast' category includes SRC, even though it's
78 ;; a wrapper around RCS.
80 ;; ADDING SUPPORT FOR OTHER BACKENDS
82 ;; VC can use arbitrary version control systems as a backend. To add
83 ;; support for a new backend named SYS, write a library vc-sys.el that
84 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
85 ;; case for the function and library names). VC will use that library if
86 ;; you put the symbol SYS somewhere into the list of
87 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
88 ;; returns non-nil for a file, all SYS-specific versions of VC commands
89 ;; will be available for that file.
91 ;; VC keeps some per-file information in the form of properties (see
92 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
93 ;; do not generally need to be aware of these properties. For example,
94 ;; `vc-sys-working-revision' should compute the working revision and
95 ;; return it; it should not look it up in the property, and it needn't
96 ;; store it there either. However, if a backend-specific function does
97 ;; store a value in a property, that value takes precedence over any
98 ;; value that the generic code might want to set (check for uses of
99 ;; the macro `with-vc-properties' in vc.el).
101 ;; In the list of functions below, each identifier needs to be prepended
102 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
103 ;; `*'), others are optional (`-').
105 ;; BACKEND PROPERTIES
107 ;; * revision-granularity
109 ;; Takes no arguments. Returns either 'file or 'repository. Backends
110 ;; that return 'file have per-file revision numbering; backends
111 ;; that return 'repository have per-repository revision numbering,
112 ;; so a revision level implicitly identifies a changeset
114 ;; STATE-QUERYING FUNCTIONS
116 ;; * registered (file)
118 ;; Return non-nil if FILE is registered in this backend. Both this
119 ;; function as well as `state' should be careful to fail gracefully
120 ;; in the event that the backend executable is absent. It is
121 ;; preferable that this function's *body* is autoloaded, that way only
122 ;; calling vc-registered does not cause the backend to be loaded
123 ;; (all the vc-FOO-registered functions are called to try to find
124 ;; the controlling backend for FILE).
126 ;; * state (file)
128 ;; Return the current version control state of FILE. For a list of
129 ;; possible values, see `vc-state'. This function should do a full and
130 ;; reliable state computation; it is usually called immediately after
131 ;; C-x v v.
133 ;; - dir-status-files (dir files update-function)
135 ;; Produce RESULT: a list of lists of the form (FILE VC-STATE EXTRA)
136 ;; for FILES in DIR. If FILES is nil, report on all files in DIR.
137 ;; (It is OK, though possibly inefficient, to ignore the FILES argument
138 ;; and always report on all files in DIR.)
140 ;; If FILES is non-nil, this function should report on all requested
141 ;; files, including up-to-date or ignored files.
143 ;; EXTRA can be used for backend specific information about FILE.
144 ;; If a command needs to be run to compute this list, it should be
145 ;; run asynchronously using (current-buffer) as the buffer for the
146 ;; command.
148 ;; When RESULT is computed, it should be passed back by doing:
149 ;; (funcall UPDATE-FUNCTION RESULT nil). If the backend uses a
150 ;; process filter, hence it produces partial results, they can be
151 ;; passed back by doing: (funcall UPDATE-FUNCTION RESULT t) and then
152 ;; do a (funcall UPDATE-FUNCTION RESULT nil) when all the results
153 ;; have been computed.
155 ;; To provide more backend specific functionality for `vc-dir'
156 ;; the following functions might be needed: `dir-extra-headers',
157 ;; `dir-printer', and `extra-dir-menu'.
159 ;; - dir-extra-headers (dir)
161 ;; Return a string that will be added to the *vc-dir* buffer header.
163 ;; - dir-printer (fileinfo)
165 ;; Pretty print the `vc-dir-fileinfo' FILEINFO.
166 ;; If a backend needs to show more information than the default FILE
167 ;; and STATE in the vc-dir listing, it can store that extra
168 ;; information in `vc-dir-fileinfo->extra'. This function can be
169 ;; used to display that extra information in the *vc-dir* buffer.
171 ;; - status-fileinfo-extra (file)
173 ;; Compute `vc-dir-fileinfo->extra' for FILE.
175 ;; * working-revision (file)
177 ;; Return the working revision of FILE. This is the revision fetched
178 ;; by the last checkout or update, not necessarily the same thing as the
179 ;; head or tip revision. Should return "0" for a file added but not yet
180 ;; committed.
182 ;; * checkout-model (files)
184 ;; Indicate whether FILES need to be "checked out" before they can be
185 ;; edited. See `vc-checkout-model' for a list of possible values.
187 ;; - mode-line-string (file)
189 ;; If provided, this function should return the VC-specific mode
190 ;; line string for FILE. The returned string should have a
191 ;; `help-echo' property which is the text to be displayed as a
192 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
193 ;; The default implementation deals well with all states that
194 ;; `vc-state' can return.
196 ;; STATE-CHANGING FUNCTIONS
198 ;; * create-repo (backend)
200 ;; Create an empty repository in the current directory and initialize
201 ;; it so VC mode can add files to it. For file-oriented systems, this
202 ;; need do no more than create a subdirectory with the right name.
204 ;; * register (files &optional comment)
206 ;; Register FILES in this backend. Optionally, an initial
207 ;; description of the file, COMMENT, may be specified, but it is not
208 ;; guaranteed that the backend will do anything with this. The
209 ;; implementation should pass the value of vc-register-switches to
210 ;; the backend command. (Note: in older versions of VC, this
211 ;; command had an optional revision first argument that was
212 ;; not used; in still older ones it took a single file argument and
213 ;; not a list.)
215 ;; - responsible-p (file)
217 ;; Return non-nil if this backend considers itself "responsible" for
218 ;; FILE, which can also be a directory. This function is used to find
219 ;; out what backend to use for registration of new files and for things
220 ;; like change log generation. The default implementation always
221 ;; returns nil.
223 ;; - receive-file (file rev)
225 ;; Let this backend "receive" a file that is already registered under
226 ;; another backend. The default implementation simply calls `register'
227 ;; for FILE, but it can be overridden to do something more specific,
228 ;; e.g. keep revision numbers consistent or choose editing modes for
229 ;; FILE that resemble those of the other backend.
231 ;; - unregister (file)
233 ;; Unregister FILE from this backend. This is only needed if this
234 ;; backend may be used as a "more local" backend for temporary editing.
236 ;; * checkin (files comment)
238 ;; Commit changes in FILES to this backend. COMMENT is used as a
239 ;; check-in comment. The implementation should pass the value of
240 ;; vc-checkin-switches to the backend command. The revision argument
241 ;; of some older VC versions is no longer supported.
243 ;; * find-revision (file rev buffer)
245 ;; Fetch revision REV of file FILE and put it into BUFFER.
246 ;; If REV is the empty string, fetch the head of the trunk.
247 ;; The implementation should pass the value of vc-checkout-switches
248 ;; to the backend command.
250 ;; * checkout (file &optional rev)
252 ;; Check out revision REV of FILE into the working area. FILE
253 ;; should be writable by the user and if locking is used for FILE, a
254 ;; lock should also be set. If REV is non-nil, that is the revision
255 ;; to check out (default is the working revision). If REV is t,
256 ;; that means to check out the head of the current branch; if it is
257 ;; the empty string, check out the head of the trunk. The
258 ;; implementation should pass the value of vc-checkout-switches to
259 ;; the backend command. The 'editable' argument of older VC versions
260 ;; is gone; all files are checked out editable.
262 ;; * revert (file &optional contents-done)
264 ;; Revert FILE back to the working revision. If optional
265 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
266 ;; already been reverted from a version backup, and this function
267 ;; only needs to update the status of FILE within the backend.
268 ;; If FILE is in the `added' state it should be returned to the
269 ;; `unregistered' state.
271 ;; - merge-file (file rev1 rev2)
273 ;; Merge the changes between REV1 and REV2 into the current working
274 ;; file (for non-distributed VCS). It is expected that with an
275 ;; empty first revision this will behave like the merge-news method.
277 ;; - merge-branch ()
279 ;; Merge another branch into the current one, prompting for a
280 ;; location to merge from.
282 ;; - merge-news (file)
284 ;; Merge recent changes from the current branch into FILE.
285 ;; (for non-distributed VCS).
287 ;; - pull (prompt)
289 ;; Pull "upstream" changes into the current branch (for distributed
290 ;; VCS). If PROMPT is non-nil, or if necessary, prompt for a
291 ;; location to pull from.
293 ;; - steal-lock (file &optional revision)
295 ;; Steal any lock on the working revision of FILE, or on REVISION if
296 ;; that is provided. This function is only needed if locking is
297 ;; used for files under this backend, and if files can indeed be
298 ;; locked by other users.
300 ;; - modify-change-comment (files rev comment)
302 ;; Modify the change comments associated with the files at the
303 ;; given revision. This is optional, many backends do not support it.
305 ;; - mark-resolved (files)
307 ;; Mark conflicts as resolved. Some VC systems need to run a
308 ;; command to mark conflicts as resolved.
310 ;; - find-admin-dir (file)
312 ;; Return the administrative directory of FILE.
314 ;; HISTORY FUNCTIONS
316 ;; * print-log (files buffer &optional shortlog start-revision limit)
318 ;; Insert the revision log for FILES into BUFFER.
319 ;; If SHORTLOG is true insert a short version of the log.
320 ;; If LIMIT is true insert only insert LIMIT log entries. If the
321 ;; backend does not support limiting the number of entries to show
322 ;; it should return `limit-unsupported'.
323 ;; If START-REVISION is given, then show the log starting from that
324 ;; revision ("starting" in the sense of it being the _newest_
325 ;; revision shown, rather than the working revision, which is normally
326 ;; the case). Not all backends support this. At present, this is
327 ;; only ever used with LIMIT = 1 (by vc-annotate-show-log-revision-at-line).
329 ;; * log-outgoing (backend remote-location)
331 ;; Insert in BUFFER the revision log for the changes that will be
332 ;; sent when performing a push operation to REMOTE-LOCATION.
334 ;; * log-incoming (backend remote-location)
336 ;; Insert in BUFFER the revision log for the changes that will be
337 ;; received when performing a pull operation from REMOTE-LOCATION.
339 ;; - log-view-mode ()
341 ;; Mode to use for the output of print-log. This defaults to
342 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
343 ;; mode of `log-view-mode'.
345 ;; - show-log-entry (revision)
347 ;; If provided, search the log entry for REVISION in the current buffer,
348 ;; and make sure it is displayed in the buffer's window. The default
349 ;; implementation of this function works for RCS-style logs.
351 ;; - comment-history (file)
353 ;; Return a string containing all log entries that were made for FILE.
354 ;; This is used for transferring a file from one backend to another,
355 ;; retaining comment information.
357 ;; - update-changelog (files)
359 ;; Using recent log entries, create ChangeLog entries for FILES, or for
360 ;; all files at or below the default-directory if FILES is nil. The
361 ;; default implementation runs rcs2log, which handles RCS- and
362 ;; CVS-style logs.
364 ;; * diff (files &optional rev1 rev2 buffer async)
366 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
367 ;; BUFFER is nil. If ASYNC is non-nil, run asynchronously. If REV1
368 ;; and REV2 are non-nil, report differences from REV1 to REV2. If
369 ;; REV1 is nil, use the working revision (as found in the
370 ;; repository) as the older revision; if REV2 is nil, use the
371 ;; current working-copy contents as the newer revision. This
372 ;; function should pass the value of (vc-switches BACKEND 'diff) to
373 ;; the backend command. It should return a status of either 0 (no
374 ;; differences found), or 1 (either non-empty diff or the diff is
375 ;; run asynchronously).
377 ;; - revision-completion-table (files)
379 ;; Return a completion table for existing revisions of FILES.
380 ;; The default is to not use any completion table.
382 ;; - annotate-command (file buf &optional rev)
384 ;; If this function is provided, it should produce an annotated display
385 ;; of FILE in BUF, relative to revision REV. Annotation means each line
386 ;; of FILE displayed is prefixed with version information associated with
387 ;; its addition (deleted lines leave no history) and that the text of the
388 ;; file is fontified according to age.
390 ;; - annotate-time ()
392 ;; Only required if `annotate-command' is defined for the backend.
393 ;; Return the time of the next line of annotation at or after point,
394 ;; as a floating point fractional number of days. The helper
395 ;; function `vc-annotate-convert-time' may be useful for converting
396 ;; multi-part times as returned by `current-time' and `encode-time'
397 ;; to this format. Return nil if no more lines of annotation appear
398 ;; in the buffer. You can safely assume that point is placed at the
399 ;; beginning of each line, starting at `point-min'. The buffer that
400 ;; point is placed in is the Annotate output, as defined by the
401 ;; relevant backend. This function also affects how much of the line
402 ;; is fontified; where it leaves point is where fontification begins.
404 ;; - annotate-current-time ()
406 ;; Only required if `annotate-command' is defined for the backend,
407 ;; AND you'd like the current time considered to be anything besides
408 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
409 ;; time with hours, minutes, and seconds included. Probably safe to
410 ;; ignore. Return the current-time, in units of fractional days.
412 ;; - annotate-extract-revision-at-line ()
414 ;; Only required if `annotate-command' is defined for the backend.
415 ;; Invoked from a buffer in vc-annotate-mode, return the revision
416 ;; corresponding to the current line, or nil if there is no revision
417 ;; corresponding to the current line.
418 ;; If the backend supports annotating through copies and renames,
419 ;; and displays a file name and a revision, then return a cons
420 ;; (REVISION . FILENAME).
422 ;; - region-history (FILE BUFFER LFROM LTO)
424 ;; Insert into BUFFER the history (log comments and diffs) of the content of
425 ;; FILE between lines LFROM and LTO. This is typically done asynchronously.
427 ;; - region-history-mode ()
429 ;; Major mode to use for the output of `region-history'.
431 ;; TAG SYSTEM
433 ;; - create-tag (dir name branchp)
435 ;; Attach the tag NAME to the state of the working copy. This
436 ;; should make sure that files are up-to-date before proceeding with
437 ;; the action. DIR can also be a file and if BRANCHP is specified,
438 ;; NAME should be created as a branch and DIR should be checked out
439 ;; under this new branch. The default implementation does not
440 ;; support branches but does a sanity check, a tree traversal and
441 ;; assigns the tag to each file.
443 ;; - retrieve-tag (dir name update)
445 ;; Retrieve the version tagged by NAME of all registered files at or below DIR.
446 ;; If UPDATE is non-nil, then update buffers of any files in the
447 ;; tag that are currently visited. The default implementation
448 ;; does a sanity check whether there aren't any uncommitted changes at
449 ;; or below DIR, and then performs a tree walk, using the `checkout'
450 ;; function to retrieve the corresponding revisions.
452 ;; MISCELLANEOUS
454 ;; - make-version-backups-p (file)
456 ;; Return non-nil if unmodified repository revisions of FILE should be
457 ;; backed up locally. If this is done, VC can perform `diff' and
458 ;; `revert' operations itself, without calling the backend system. The
459 ;; default implementation always returns nil.
461 ;; - root (file)
463 ;; Return the root of the VC controlled hierarchy for file.
465 ;; - ignore (file &optional directory)
467 ;; Ignore FILE under the VCS of DIRECTORY (default is `default-directory').
468 ;; FILE is a file wildcard.
469 ;; When called interactively and with a prefix argument, remove FILE
470 ;; from ignored files.
471 ;; When called from Lisp code, if DIRECTORY is non-nil, the
472 ;; repository to use will be deduced by DIRECTORY.
474 ;; - ignore-completion-table
476 ;; Return the completion table for files ignored by the current
477 ;; version control system, e.g., the entries in `.gitignore' and
478 ;; `.bzrignore'.
480 ;; - previous-revision (file rev)
482 ;; Return the revision number that precedes REV for FILE, or nil if no such
483 ;; revision exists.
485 ;; - next-revision (file rev)
487 ;; Return the revision number that follows REV for FILE, or nil if no such
488 ;; revision exists.
490 ;; - log-edit-mode ()
492 ;; Turn on the mode used for editing the check in log. This
493 ;; defaults to `log-edit-mode'. If changed, it should use a mode
494 ;; derived from`log-edit-mode'.
496 ;; - check-headers ()
498 ;; Return non-nil if the current buffer contains any version headers.
500 ;; - delete-file (file)
502 ;; Delete FILE and mark it as deleted in the repository. If this
503 ;; function is not provided, the command `vc-delete-file' will
504 ;; signal an error.
506 ;; - rename-file (old new)
508 ;; Rename file OLD to NEW, both in the working area and in the
509 ;; repository. If this function is not provided, the renaming
510 ;; will be done by (vc-delete-file old) and (vc-register new).
512 ;; - find-file-hook ()
514 ;; Operation called in current buffer when opening a file. This can
515 ;; be used by the backend to setup some local variables it might need.
517 ;; - extra-menu ()
519 ;; Return a menu keymap, the items in the keymap will appear at the
520 ;; end of the Version Control menu. The goal is to allow backends
521 ;; to specify extra menu items that appear in the VC menu. This way
522 ;; you can provide menu entries for functionality that is specific
523 ;; to your backend and which does not map to any of the VC generic
524 ;; concepts.
526 ;; - extra-dir-menu ()
528 ;; Return a menu keymap, the items in the keymap will appear at the
529 ;; end of the VC Status menu. The goal is to allow backends to
530 ;; specify extra menu items that appear in the VC Status menu. This
531 ;; makes it possible to provide menu entries for functionality that
532 ;; is specific to a backend and which does not map to any of the VC
533 ;; generic concepts.
535 ;; - conflicted-files (dir)
537 ;; Return the list of files where conflict resolution is needed in
538 ;; the project that contains DIR.
539 ;; FIXME: what should it do with non-text conflicts?
541 ;;; Changes from the pre-25.1 API:
543 ;; - INCOMPATIBLE CHANGE: The 'editable' optional argument of
544 ;; vc-checkout is gone. The upper level assumes that all files are
545 ;; checked out editable. This moves closer to emulating modern
546 ;; non-locking behavior even on very old VCSes.
548 ;; - INCOMPATIBLE CHANGE: The vc-register function and its backend
549 ;; implementations no longer take a first optional revision
550 ;; argument, since on no system since RCS has setting the initial
551 ;; revision been even possible, let alone sane.
553 ;; INCOMPATIBLE CHANGE: In older versions of the API, vc-diff did
554 ;; not take an async-mode flag as a fourth optional argument. (This
555 ;; change eliminated a particularly ugly global.)
557 ;; - INCOMPATIBLE CHANGE: The backend operation for non-distributed
558 ;; VCSes formerly called "merge" is now "merge-file" (to contrast
559 ;; with merge-branch), and does its own prompting for revisions.
560 ;; (This fixes a layer violation that produced bad behavior under
561 ;; SVN.)
563 ;; - INCOMPATIBLE CHANGE: The old fourth 'default-state' argument of
564 ;; vc-dir-status-files is gone; none of the back ends actually used it.
566 ;; - vc-dir-status is no longer a public method; it has been replaced
567 ;; by vc-dir-status-files.
569 ;; - vc-state-heuristic is no longer a public method (the CVS backend
570 ;; retains it as a private one).
572 ;; - the vc-mistrust-permissions configuration variable is gone; the
573 ;; code no longer relies on permissions except in one corner case where
574 ;; CVS leaves no alternative (which was not gated by this variable). The
575 ;; only affected back ends were SCCS and RCS.
577 ;; - vc-stay-local-p and repository-hostname are no longer part
578 ;; of the public API. The vc-stay-local configuration variable
579 ;; remains but only affects the CVS back end.
581 ;; - The init-revision function and the default-initial-revision
582 ;; variable are gone. These have't made sense on anything shipped
583 ;; since RCS, and using them was a dumb stunt even on RCS.
585 ;; - workfile-unchanged-p is no longer a public back-end method. It
586 ;; was redundant with vc-state and usually implemented with a trivial
587 ;; call to it. A few older back ends retain versions for internal use in
588 ;; their vc-state functions.
590 ;; - could-register is no longer a public method. Only vc-cvs ever used it
592 ;; The vc-keep-workfiles configuration variable is gone. Used only by
593 ;; the RCS and SCCS backends, it was an invitation to shoot self in foot
594 ;; when set to the (non-default) value nil. The original justification
595 ;; for it (saving disk space) is long obsolete.
597 ;; - The rollback method (implemented by RCS and SCCS only) is gone. See
598 ;; the to-do note on uncommit.
600 ;; - latest-on-branch-p is no longer a public method. It was to be used
601 ;; for implementing rollback. RCS keeps its implementation (the only one)
602 ;; for internal use.
605 ;;; Todo:
607 ;;;; New Primitives:
609 ;; - uncommit: undo last checkin, leave changes in place in the workfile,
610 ;; stash the commit comment for re-use.
612 ;; - deal with push operations.
614 ;;;; Primitives that need changing:
616 ;; - vc-update/vc-merge should deal with VC systems that don't do
617 ;; update/merge on a file basis, but on a whole repository basis.
618 ;; vc-update and vc-merge assume the arguments are always files,
619 ;; they don't deal with directories. Make sure the *vc-dir* buffer
620 ;; is updated after these operations.
621 ;; At least bzr, git and hg should benefit from this.
623 ;;;; Improved branch and tag handling:
625 ;; - Make sure the *vc-dir* buffer is updated after merge-branch operations.
627 ;; - add a generic mechanism for remembering the current branch names,
628 ;; display the branch name in the mode-line. Replace
629 ;; vc-cvs-sticky-tag with that.
631 ;; - Add a primitives for switching to a branch (creating it if required.
633 ;; - Add the ability to list tags and branches.
635 ;;;; Unify two different versions of the amend capability
637 ;; - Some back ends (SCCS/RCS/SVN/SRC), have an amend capability that can
638 ;; be invoked from log-view.
640 ;; - The git backend supports amending, but in a different
641 ;; way (press `C-c C-e' in log-edit buffer, when making a new commit).
643 ;; - Second, `log-view-modify-change-comment' doesn't seem to support
644 ;; modern backends at all because `log-view-extract-comment'
645 ;; unconditionally calls `log-view-current-file'. This should be easy to
646 ;; fix.
648 ;; - Third, doing message editing in log-view might be a natural way to go
649 ;; about it, but editing any but the last commit (and even it, if it's
650 ;; been pushed) is a dangerous operation in Git, which we shouldn't make
651 ;; too easy for users to perform.
653 ;; There should be a check that the given comment is not reachable
654 ;; from any of the "remote" refs?
656 ;;;; Other
658 ;; - asynchronous checkin and commit, so you can keep working in other
659 ;; buffers while the repo operation happens.
661 ;; - Direct support for stash/shelve.
663 ;; - when a file is in `conflict' state, turn on smerge-mode.
665 ;; - figure out what to do with conflicts that are not caused by the
666 ;; file contents, but by metadata or other causes. Example: File A
667 ;; gets renamed to B in one branch and to C in another and you merge
668 ;; the two branches. Or you locally add file FOO and then pull a
669 ;; change that also adds a new file FOO, ...
671 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
672 ;; buffer, if one is present, instead of adding to the ChangeLog.
674 ;; - When vc-next-action calls vc-checkin it could pre-fill the
675 ;; *vc-log* buffer with some obvious items: the list of files that
676 ;; were added, the list of files that were removed. If the diff is
677 ;; available, maybe it could even call something like
678 ;; `diff-add-change-log-entries-other-window' to create a detailed
679 ;; skeleton for the log...
681 ;; - most vc-dir backends need more work. They might need to
682 ;; provide custom headers, use the `extra' field and deal with all
683 ;; possible VC states.
685 ;; - add a function that calls vc-dir to `find-directory-functions'.
687 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
688 ;; files. Now that unregistered and ignored files are shown in
689 ;; vc-dir, it is possible that these commands are called
690 ;; for unregistered/ignored files.
692 ;; - vc-next-action needs work in order to work with multiple
693 ;; backends: `vc-state' returns the state for the default backend,
694 ;; not for the backend in the current *vc-dir* buffer.
696 ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
697 ;; it should work for other async commands done through vc-do-command
698 ;; as well,
700 ;; - vc-dir toolbar needs more icons.
702 ;; - The backends should avoid using `vc-file-setprop' and `vc-file-getprop'.
704 ;;; Code:
706 (require 'vc-hooks)
707 (require 'vc-dispatcher)
708 (require 'cl-lib)
710 (declare-function diff-setup-whitespace "diff-mode" ())
712 (eval-when-compile
713 (require 'dired))
715 (declare-function dired-get-filename "dired" (&optional localp noerror))
716 (declare-function dired-move-to-filename "dired" (&optional err eol))
717 (declare-function dired-marker-regexp "dired" ())
719 (unless (assoc 'vc-parent-buffer minor-mode-alist)
720 (setq minor-mode-alist
721 (cons '(vc-parent-buffer vc-parent-buffer-name)
722 minor-mode-alist)))
724 ;; General customization
726 (defgroup vc nil
727 "Emacs interface to version control systems."
728 :group 'tools)
730 (defcustom vc-initial-comment nil
731 "If non-nil, prompt for initial comment when a file is registered."
732 :type 'boolean
733 :group 'vc)
735 (make-obsolete-variable 'vc-initial-comment "it has no effect." "23.2")
737 (defcustom vc-checkin-switches nil
738 "A string or list of strings specifying extra switches for checkin.
739 These are passed to the checkin program by \\[vc-checkin]."
740 :type '(choice (const :tag "None" nil)
741 (string :tag "Argument String")
742 (repeat :tag "Argument List"
743 :value ("")
744 string))
745 :group 'vc)
747 (defcustom vc-checkout-switches nil
748 "A string or list of strings specifying extra switches for checkout.
749 These are passed to the checkout program by \\[vc-checkout]."
750 :type '(choice (const :tag "None" nil)
751 (string :tag "Argument String")
752 (repeat :tag "Argument List"
753 :value ("")
754 string))
755 :group 'vc)
757 (defcustom vc-register-switches nil
758 "A string or list of strings; extra switches for registering a file.
759 These are passed to the checkin program by \\[vc-register]."
760 :type '(choice (const :tag "None" nil)
761 (string :tag "Argument String")
762 (repeat :tag "Argument List"
763 :value ("")
764 string))
765 :group 'vc)
767 (defcustom vc-diff-switches nil
768 "A string or list of strings specifying switches for diff under VC.
769 When running diff under a given BACKEND, VC uses the first
770 non-nil value of `vc-BACKEND-diff-switches', `vc-diff-switches',
771 and `diff-switches', in that order. Since nil means to check the
772 next variable in the sequence, either of the first two may use
773 the value t to mean no switches at all. `vc-diff-switches'
774 should contain switches that are specific to version control, but
775 not specific to any particular backend."
776 :type '(choice (const :tag "Unspecified" nil)
777 (const :tag "None" t)
778 (string :tag "Argument String")
779 (repeat :tag "Argument List" :value ("") string))
780 :group 'vc
781 :version "21.1")
783 (defcustom vc-annotate-switches nil
784 "A string or list of strings specifying switches for annotate under VC.
785 When running annotate under a given BACKEND, VC uses the first
786 non-nil value of `vc-BACKEND-annotate-switches', `vc-annotate-switches',
787 and `annotate-switches', in that order. Since nil means to check the
788 next variable in the sequence, either of the first two may use
789 the value t to mean no switches at all. `vc-annotate-switches'
790 should contain switches that are specific to version control, but
791 not specific to any particular backend.
793 As very few switches (if any) are used across different VC tools,
794 please consider using the specific `vc-BACKEND-annotate-switches'
795 for the backend you use."
796 :type '(choice (const :tag "Unspecified" nil)
797 (const :tag "None" t)
798 (string :tag "Argument String")
799 (repeat :tag "Argument List" :value ("") string))
800 :group 'vc
801 :version "25.1")
803 (defcustom vc-log-show-limit 2000
804 "Limit the number of items shown by the VC log commands.
805 Zero means unlimited.
806 Not all VC backends are able to support this feature."
807 :type 'integer
808 :group 'vc)
810 (defcustom vc-allow-async-revert nil
811 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
812 Enabling this option means that you can confirm a revert operation even
813 if the local changes in the file have not been found and displayed yet."
814 :type '(choice (const :tag "No" nil)
815 (const :tag "Yes" t))
816 :group 'vc
817 :version "22.1")
819 ;;;###autoload
820 (defcustom vc-checkout-hook nil
821 "Normal hook (list of functions) run after checking out a file.
822 See `run-hooks'."
823 :type 'hook
824 :group 'vc
825 :version "21.1")
827 ;;;###autoload
828 (defcustom vc-checkin-hook nil
829 "Normal hook (list of functions) run after commit or file checkin.
830 See also `log-edit-done-hook'."
831 :type 'hook
832 :options '(log-edit-comment-to-change-log)
833 :group 'vc)
835 ;;;###autoload
836 (defcustom vc-before-checkin-hook nil
837 "Normal hook (list of functions) run before a commit or a file checkin.
838 See `run-hooks'."
839 :type 'hook
840 :group 'vc)
842 (defcustom vc-revert-show-diff t
843 "If non-nil, `vc-revert' shows a `vc-diff' buffer before querying."
844 :type 'boolean
845 :group 'vc
846 :version "24.1")
848 ;; Header-insertion hair
850 (defcustom vc-static-header-alist
851 '(("\\.c\\'" .
852 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
853 "Associate static header string templates with file types.
854 A \%s in the template is replaced with the first string associated with
855 the file's version control type in `vc-BACKEND-header'."
856 :type '(repeat (cons :format "%v"
857 (regexp :tag "File Type")
858 (string :tag "Header String")))
859 :group 'vc)
861 (defcustom vc-comment-alist
862 '((nroff-mode ".\\\"" ""))
863 "Special comment delimiters for generating VC headers.
864 Add an entry in this list if you need to override the normal `comment-start'
865 and `comment-end' variables. This will only be necessary if the mode language
866 is sensitive to blank lines."
867 :type '(repeat (list :format "%v"
868 (symbol :tag "Mode")
869 (string :tag "Comment Start")
870 (string :tag "Comment End")))
871 :group 'vc)
874 ;; File property caching
876 (defun vc-clear-context ()
877 "Clear all cached file properties."
878 (interactive)
879 (fillarray vc-file-prop-obarray 0))
881 (defmacro with-vc-properties (files form settings)
882 "Execute FORM, then maybe set per-file properties for FILES.
883 If any of FILES is actually a directory, then do the same for all
884 buffers for files in that directory.
885 SETTINGS is an association list of property/value pairs. After
886 executing FORM, set those properties from SETTINGS that have not yet
887 been updated to their corresponding values."
888 (declare (debug t))
889 `(let ((vc-touched-properties (list t))
890 (flist nil))
891 (dolist (file ,files)
892 (if (file-directory-p file)
893 (dolist (buffer (buffer-list))
894 (let ((fname (buffer-file-name buffer)))
895 (when (and fname (string-prefix-p file fname))
896 (push fname flist))))
897 (push file flist)))
898 ,form
899 (dolist (file flist)
900 (dolist (setting ,settings)
901 (let ((property (car setting)))
902 (unless (memq property vc-touched-properties)
903 (put (intern file vc-file-prop-obarray)
904 property (cdr setting))))))))
906 ;;; Code for deducing what fileset and backend to assume
908 (defun vc-backend-for-registration (file)
909 "Return a backend that can be used for registering FILE.
911 If no backend declares itself responsible for FILE, then FILE
912 must not be in a version controlled directory, so try to create a
913 repository, prompting for the directory and the VC backend to
914 use."
915 (catch 'found
916 ;; First try: find a responsible backend, it must be a backend
917 ;; under which FILE is not yet registered.
918 (dolist (backend vc-handled-backends)
919 (and (not (vc-call-backend backend 'registered file))
920 (vc-call-backend backend 'responsible-p file)
921 (throw 'found backend)))
922 ;; no responsible backend
923 (let* ((possible-backends
924 (let (pos)
925 (dolist (crt vc-handled-backends)
926 (when (vc-find-backend-function crt 'create-repo)
927 (push crt pos)))
928 pos))
930 (intern
931 ;; Read the VC backend from the user, only
932 ;; complete with the backends that have the
933 ;; 'create-repo method.
934 (completing-read
935 (format "%s is not in a version controlled directory.\nUse VC backend: " file)
936 (mapcar 'symbol-name possible-backends) nil t)))
937 (repo-dir
938 (let ((def-dir (file-name-directory file)))
939 ;; read the directory where to create the
940 ;; repository, make sure it's a parent of
941 ;; file.
942 (read-file-name
943 (format "create %s repository in: " bk)
944 default-directory def-dir t nil
945 (lambda (arg)
946 (message "arg %s" arg)
947 (and (file-directory-p arg)
948 (string-prefix-p (expand-file-name arg) def-dir)))))))
949 (let ((default-directory repo-dir))
950 (vc-call-backend bk 'create-repo))
951 (throw 'found bk))))
953 ;;;###autoload
954 (defun vc-responsible-backend (file)
955 "Return the name of a backend system that is responsible for FILE.
957 If FILE is already registered, return the
958 backend of FILE. If FILE is not registered, then the
959 first backend in `vc-handled-backends' that declares itself
960 responsible for FILE is returned."
961 (or (and (not (file-directory-p file)) (vc-backend file))
962 (catch 'found
963 ;; First try: find a responsible backend. If this is for registration,
964 ;; it must be a backend under which FILE is not yet registered.
965 (dolist (backend vc-handled-backends)
966 (and (vc-call-backend backend 'responsible-p file)
967 (throw 'found backend))))
968 (error "No VC backend is responsible for %s" file)))
970 (defun vc-expand-dirs (file-or-dir-list backend)
971 "Expands directories in a file list specification.
972 Within directories, only files already under version control are noticed."
973 (let ((flattened '()))
974 (dolist (node file-or-dir-list)
975 (when (file-directory-p node)
976 (vc-file-tree-walk
977 node (lambda (f) (when (eq (vc-backend f) backend) (push f flattened)))))
978 (unless (file-directory-p node) (push node flattened)))
979 (nreverse flattened)))
981 (defvar vc-dir-backend)
982 (defvar log-view-vc-backend)
983 (defvar log-edit-vc-backend)
984 (defvar diff-vc-backend)
986 (defun vc-deduce-backend ()
987 (cond ((derived-mode-p 'vc-dir-mode) vc-dir-backend)
988 ((derived-mode-p 'log-view-mode) log-view-vc-backend)
989 ((derived-mode-p 'log-edit-mode) log-edit-vc-backend)
990 ((derived-mode-p 'diff-mode) diff-vc-backend)
991 ;; Maybe we could even use comint-mode rather than shell-mode?
992 ((derived-mode-p 'dired-mode 'shell-mode 'compilation-mode)
993 (vc-responsible-backend default-directory))
994 (vc-mode (vc-backend buffer-file-name))))
996 (declare-function vc-dir-current-file "vc-dir" ())
997 (declare-function vc-dir-deduce-fileset "vc-dir" (&optional state-model-only-files))
999 (defun vc-deduce-fileset (&optional observer allow-unregistered
1000 state-model-only-files)
1001 "Deduce a set of files and a backend to which to apply an operation.
1002 Return (BACKEND FILESET FILESET-ONLY-FILES STATE CHECKOUT-MODEL).
1004 If we're in VC-dir mode, FILESET is the list of marked files,
1005 or the directory if no files are marked.
1006 Otherwise, if in a buffer visiting a version-controlled file,
1007 FILESET is a single-file fileset containing that file.
1008 Otherwise, if ALLOW-UNREGISTERED is non-nil and the visited file
1009 is unregistered, FILESET is a single-file fileset containing it.
1010 Otherwise, throw an error.
1012 STATE-MODEL-ONLY-FILES if non-nil, means that the caller needs
1013 the FILESET-ONLY-FILES STATE and MODEL info. Otherwise, that
1014 part may be skipped.
1016 BEWARE: this function may change the current buffer."
1017 ;; FIXME: OBSERVER is unused. The name is not intuitive and is not
1018 ;; documented. It's set to t when called from diff and print-log.
1019 (let (backend)
1020 (cond
1021 ((derived-mode-p 'vc-dir-mode)
1022 (vc-dir-deduce-fileset state-model-only-files))
1023 ((derived-mode-p 'dired-mode)
1024 (if observer
1025 (vc-dired-deduce-fileset)
1026 (error "State changing VC operations not supported in `dired-mode'")))
1027 ((and (derived-mode-p 'log-view-mode)
1028 (setq backend (vc-responsible-backend default-directory)))
1029 (list backend default-directory))
1030 ((setq backend (vc-backend buffer-file-name))
1031 (if state-model-only-files
1032 (list backend (list buffer-file-name)
1033 (list buffer-file-name)
1034 (vc-state buffer-file-name)
1035 (vc-checkout-model backend buffer-file-name))
1036 (list backend (list buffer-file-name))))
1037 ((and (buffer-live-p vc-parent-buffer)
1038 ;; FIXME: Why this test? --Stef
1039 (or (buffer-file-name vc-parent-buffer)
1040 (with-current-buffer vc-parent-buffer
1041 (derived-mode-p 'vc-dir-mode))))
1042 (progn ;FIXME: Why not `with-current-buffer'? --Stef.
1043 (set-buffer vc-parent-buffer)
1044 (vc-deduce-fileset observer allow-unregistered state-model-only-files)))
1045 ((not buffer-file-name)
1046 (error "Buffer %s is not associated with a file" (buffer-name)))
1047 ((and allow-unregistered (not (vc-registered buffer-file-name)))
1048 (if state-model-only-files
1049 (list (vc-backend-for-registration (buffer-file-name))
1050 (list buffer-file-name)
1051 (list buffer-file-name)
1052 (when state-model-only-files 'unregistered)
1053 nil)
1054 (list (vc-backend-for-registration (buffer-file-name))
1055 (list buffer-file-name))))
1056 (t (error "File is not under version control")))))
1058 (defun vc-dired-deduce-fileset ()
1059 (let ((backend (vc-responsible-backend default-directory)))
1060 (unless backend (error "Directory not under VC"))
1061 (list backend
1062 (dired-map-over-marks (dired-get-filename nil t) nil))))
1064 (defun vc-ensure-vc-buffer ()
1065 "Make sure that the current buffer visits a version-controlled file."
1066 (cond
1067 ((derived-mode-p 'vc-dir-mode)
1068 (set-buffer (find-file-noselect (vc-dir-current-file))))
1070 (while (and vc-parent-buffer
1071 (buffer-live-p vc-parent-buffer)
1072 ;; Avoid infinite looping when vc-parent-buffer and
1073 ;; current buffer are the same buffer.
1074 (not (eq vc-parent-buffer (current-buffer))))
1075 (set-buffer vc-parent-buffer))
1076 (if (not buffer-file-name)
1077 (error "Buffer %s is not associated with a file" (buffer-name))
1078 (unless (vc-backend buffer-file-name)
1079 (error "File %s is not under version control" buffer-file-name))))))
1081 ;;; Support for the C-x v v command.
1082 ;; This is where all the single-file-oriented code from before the fileset
1083 ;; rewrite lives.
1085 (defsubst vc-editable-p (file)
1086 "Return non-nil if FILE can be edited."
1087 (let ((backend (vc-backend file)))
1088 (and backend
1089 (or (eq (vc-checkout-model backend (list file)) 'implicit)
1090 (memq (vc-state file) '(edited needs-merge conflict))))))
1092 (defun vc-compatible-state (p q)
1093 "Controls which states can be in the same commit."
1095 (eq p q)
1096 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1098 (defun vc-read-backend (prompt)
1099 (intern
1100 (completing-read prompt (mapcar 'symbol-name vc-handled-backends)
1101 nil 'require-match)))
1103 ;; Here's the major entry point.
1105 ;;;###autoload
1106 (defun vc-next-action (verbose)
1107 "Do the next logical version control operation on the current fileset.
1108 This requires that all files in the current VC fileset be in the
1109 same state. If not, signal an error.
1111 For merging-based version control systems:
1112 If every file in the VC fileset is not registered for version
1113 control, register the fileset (but don't commit).
1114 If every work file in the VC fileset is added or changed, pop
1115 up a *vc-log* buffer to commit the fileset.
1116 For a centralized version control system, if any work file in
1117 the VC fileset is out of date, offer to update the fileset.
1119 For old-style locking-based version control systems, like RCS:
1120 If every file is not registered, register the file(s).
1121 If every file is registered and unlocked, check out (lock)
1122 the file(s) for editing.
1123 If every file is locked by you and has changes, pop up a
1124 *vc-log* buffer to check in the changes. Leave a
1125 read-only copy of each changed file after checking in.
1126 If every file is locked by you and unchanged, unlock them.
1127 If every file is locked by someone else, offer to steal the lock."
1128 (interactive "P")
1129 (let* ((vc-fileset (vc-deduce-fileset nil t 'state-model-only-files))
1130 (backend (car vc-fileset))
1131 (files (nth 1 vc-fileset))
1132 ;; (fileset-only-files (nth 2 vc-fileset))
1133 ;; FIXME: We used to call `vc-recompute-state' here.
1134 (state (nth 3 vc-fileset))
1135 ;; The backend should check that the checkout-model is consistent
1136 ;; among all the `files'.
1137 (model (nth 4 vc-fileset)))
1139 ;; If a buffer has unsaved changes, a checkout would discard those
1140 ;; changes, so treat the buffer as having unlocked changes.
1141 (when (and (not (eq model 'implicit)) (eq state 'up-to-date))
1142 (dolist (file files)
1143 (let ((buffer (get-file-buffer file)))
1144 (and buffer
1145 (buffer-modified-p buffer)
1146 (setq state 'unlocked-changes)))))
1148 ;; Do the right thing.
1149 (cond
1150 ((eq state 'missing)
1151 (error "Fileset files are missing, so cannot be operated on"))
1152 ((eq state 'ignored)
1153 (error "Fileset files are ignored by the version-control system"))
1154 ((or (null state) (eq state 'unregistered))
1155 (vc-register vc-fileset))
1156 ;; Files are up-to-date, or need a merge and user specified a revision
1157 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-update)))
1158 (cond
1159 (verbose
1160 ;; Go to a different revision.
1161 (let* ((revision
1162 ;; FIXME: Provide completion.
1163 (read-string "Branch, revision, or backend to move to: "))
1164 (revision-downcase (downcase revision)))
1165 (if (member
1166 revision-downcase
1167 (mapcar (lambda (arg) (downcase (symbol-name arg)))
1168 vc-handled-backends))
1169 (let ((vsym (intern-soft revision-downcase)))
1170 (dolist (file files) (vc-transfer-file file vsym)))
1171 (dolist (file files)
1172 (vc-checkout file revision)))))
1173 ((not (eq model 'implicit))
1174 ;; check the files out
1175 (dolist (file files) (vc-checkout file)))
1177 ;; do nothing
1178 (message "Fileset is up-to-date"))))
1179 ;; Files have local changes
1180 ((vc-compatible-state state 'edited)
1181 (let ((ready-for-commit files))
1182 ;; CVS, SVN and bzr don't care about read-only (bug#9781).
1183 ;; RCS does, SCCS might (someone should check...).
1184 (when (memq backend '(RCS SCCS))
1185 ;; If files are edited but read-only, give user a chance to correct.
1186 (dolist (file files)
1187 ;; If committing a mix of removed and edited files, the
1188 ;; fileset has state = 'edited. Rather than checking the
1189 ;; state of each individual file in the fileset, it seems
1190 ;; simplest to just check if the file exists. Bug#9781.
1191 (when (and (file-exists-p file) (not (file-writable-p file)))
1192 ;; Make the file-buffer read-write.
1193 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue? " file))
1194 (error "Aborted"))
1195 ;; Maybe we somehow lost permissions on the directory.
1196 (condition-case nil
1197 (set-file-modes file (logior (file-modes file) 128))
1198 (error (error "Unable to make file writable")))
1199 (let ((visited (get-file-buffer file)))
1200 (when visited
1201 (with-current-buffer visited
1202 (read-only-mode -1)))))))
1203 ;; Allow user to revert files with no changes
1204 (save-excursion
1205 (dolist (file files)
1206 (let ((visited (get-file-buffer file)))
1207 ;; For files with locking, if the file does not contain
1208 ;; any changes, just let go of the lock, i.e. revert.
1209 (when (and (not (eq model 'implicit))
1210 (eq state 'up-to-date)
1211 ;; If buffer is modified, that means the user just
1212 ;; said no to saving it; in that case, don't revert,
1213 ;; because the user might intend to save after
1214 ;; finishing the log entry and committing.
1215 (not (and visited (buffer-modified-p))))
1216 (vc-revert-file file)
1217 (setq ready-for-commit (delete file ready-for-commit))))))
1218 ;; Remaining files need to be committed
1219 (if (not ready-for-commit)
1220 (message "No files remain to be committed")
1221 (if (not verbose)
1222 (vc-checkin ready-for-commit backend)
1223 (let ((new-backend (vc-read-backend "New backend: ")))
1224 (if new-backend
1225 (dolist (file files)
1226 (vc-transfer-file file new-backend))))))))
1227 ;; locked by somebody else (locking VCSes only)
1228 ((stringp state)
1229 ;; In the old days, we computed the revision once and used it on
1230 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1231 ;; computed the revision once (incorrectly, using a free var) and
1232 ;; used it on all files. To fix the free var bug, we can either
1233 ;; use `(car files)' or do what we do here: distribute the
1234 ;; revision computation among `files'. Although this may be
1235 ;; tedious for those backends where a "revision" is a trans-file
1236 ;; concept, it is nonetheless correct for both those and (more
1237 ;; importantly) for those where "revision" is a per-file concept.
1238 ;; If the intersection of the former group and "locking VCSes" is
1239 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1240 ;; pre-computation approach of yore.
1241 (dolist (file files)
1242 (vc-steal-lock
1243 file (if verbose
1244 (read-string (format "%s revision to steal: " file))
1245 (vc-working-revision file))
1246 state)))
1247 ;; conflict
1248 ((eq state 'conflict)
1249 ;; FIXME: Is it really the UI we want to provide?
1250 ;; In my experience, the conflicted files should be marked as resolved
1251 ;; one-by-one when saving the file after resolving the conflicts.
1252 ;; I.e. stating explicitly that the conflicts are resolved is done
1253 ;; very rarely.
1254 (vc-mark-resolved backend files))
1255 ;; needs-update
1256 ((eq state 'needs-update)
1257 (dolist (file files)
1258 (if (yes-or-no-p (format
1259 "%s is not up-to-date. Get latest revision? "
1260 (file-name-nondirectory file)))
1261 (vc-checkout file t)
1262 (when (and (not (eq model 'implicit))
1263 (yes-or-no-p "Lock this revision? "))
1264 (vc-checkout file)))))
1265 ;; needs-merge
1266 ((eq state 'needs-merge)
1267 (dolist (file files)
1268 (when (yes-or-no-p (format
1269 "%s is not up-to-date. Merge in changes now? "
1270 (file-name-nondirectory file)))
1271 (vc-maybe-resolve-conflicts
1272 file (vc-call-backend backend 'merge-news file)))))
1274 ;; unlocked-changes
1275 ((eq state 'unlocked-changes)
1276 (dolist (file files)
1277 (when (not (equal buffer-file-name file))
1278 (find-file-other-window file))
1279 (if (save-window-excursion
1280 (vc-diff-internal nil
1281 (cons (car vc-fileset) (cons (cadr vc-fileset) (list file)))
1282 (vc-working-revision file) nil)
1283 (goto-char (point-min))
1284 (let ((inhibit-read-only t))
1285 (insert
1286 (format "Changes to %s since last lock:\n\n" file)))
1287 (not (beep))
1288 (yes-or-no-p (concat "File has unlocked changes. "
1289 "Claim lock retaining changes? ")))
1290 (progn (vc-call-backend backend 'steal-lock file)
1291 (clear-visited-file-modtime)
1292 (write-file buffer-file-name)
1293 (vc-mode-line file backend))
1294 (if (not (yes-or-no-p
1295 "Revert to checked-in revision, instead? "))
1296 (error "Checkout aborted")
1297 (vc-revert-buffer-internal t t)
1298 (vc-checkout file)))))
1299 ;; Unknown fileset state
1301 (error "Fileset is in an unknown state %s" state)))))
1303 (defun vc-create-repo (backend)
1304 "Create an empty repository in the current directory."
1305 (interactive
1306 (list
1307 (intern
1308 (upcase
1309 (completing-read
1310 "Create repository for: "
1311 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1312 nil t)))))
1313 (vc-call-backend backend 'create-repo))
1315 (declare-function vc-dir-move-to-goal-column "vc-dir" ())
1317 ;;;###autoload
1318 (defun vc-register (&optional vc-fileset comment)
1319 "Register into a version control system.
1320 If VC-FILESET is given, register the files in that fileset.
1321 Otherwise register the current file.
1322 If COMMENT is present, use that as an initial comment.
1324 The version control system to use is found by cycling through the list
1325 `vc-handled-backends'. The first backend in that list which declares
1326 itself responsible for the file (usually because other files in that
1327 directory are already registered under that backend) will be used to
1328 register the file. If no backend declares itself responsible, the
1329 first backend that could register the file is used."
1330 (interactive "P")
1331 (let* ((fileset-arg (or vc-fileset (vc-deduce-fileset nil t)))
1332 (backend (car fileset-arg))
1333 (files (nth 1 fileset-arg)))
1334 ;; We used to operate on `only-files', but VC wants to provide the
1335 ;; possibility to register directories rather than files only, since
1336 ;; many VCS allow that as well.
1337 (dolist (fname files)
1338 (let ((bname (get-file-buffer fname)))
1339 (unless fname
1340 (setq fname buffer-file-name))
1341 (when (vc-call-backend backend 'registered fname)
1342 (error "This file is already registered"))
1343 ;; Watch out for new buffers of size 0: the corresponding file
1344 ;; does not exist yet, even though buffer-modified-p is nil.
1345 (when bname
1346 (with-current-buffer bname
1347 (when (and (not (buffer-modified-p))
1348 (zerop (buffer-size))
1349 (not (file-exists-p buffer-file-name)))
1350 (set-buffer-modified-p t))
1351 (vc-buffer-sync)))))
1352 (message "Registering %s... " files)
1353 (mapc 'vc-file-clearprops files)
1354 (vc-call-backend backend 'register files comment)
1355 (mapc
1356 (lambda (file)
1357 (vc-file-setprop file 'vc-backend backend)
1358 ;; FIXME: This is wrong: it should set `backup-inhibited' in all
1359 ;; the buffers visiting files affected by this `vc-register', not
1360 ;; in the current-buffer.
1361 ;; (unless vc-make-backup-files
1362 ;; (make-local-variable 'backup-inhibited)
1363 ;; (setq backup-inhibited t))
1365 (vc-resynch-buffer file t t))
1366 files)
1367 (when (derived-mode-p 'vc-dir-mode)
1368 (vc-dir-move-to-goal-column))
1369 (message "Registering %s... done" files)))
1371 (defun vc-register-with (backend)
1372 "Register the current file with a specified back end."
1373 (interactive "SBackend: ")
1374 (when (not (member backend vc-handled-backends))
1375 (error "Unknown back end"))
1376 (let ((vc-handled-backends (list backend)))
1377 (call-interactively 'vc-register)))
1379 (defun vc-ignore (file &optional directory remove)
1380 "Ignore FILE under the VCS of DIRECTORY.
1382 Normally, FILE is a wildcard specification that matches the files
1383 to be ignored. When REMOVE is non-nil, remove FILE from the list
1384 of ignored files.
1386 DIRECTORY defaults to `default-directory' and is used to
1387 determine the responsible VC backend.
1389 When called interactively, prompt for a FILE to ignore, unless a
1390 prefix argument is given, in which case prompt for a file FILE to
1391 remove from the list of ignored files."
1392 (interactive
1393 (list
1394 (if (not current-prefix-arg)
1395 (read-file-name "File to ignore: ")
1396 (completing-read
1397 "File to remove: "
1398 (vc-call-backend
1399 (or (vc-responsible-backend default-directory)
1400 (error "Unknown backend"))
1401 'ignore-completion-table default-directory)))
1402 nil current-prefix-arg))
1403 (let* ((directory (or directory default-directory))
1404 (backend (or (vc-responsible-backend default-directory)
1405 (error "Unknown backend"))))
1406 (vc-call-backend backend 'ignore file directory remove)))
1408 (defun vc-default-ignore (backend file &optional directory remove)
1409 "Ignore FILE under the VCS of DIRECTORY (default is `default-directory').
1410 FILE is a file wildcard, relative to the root directory of DIRECTORY.
1411 When called from Lisp code, if DIRECTORY is non-nil, the
1412 repository to use will be deduced by DIRECTORY; if REMOVE is
1413 non-nil, remove FILE from ignored files.
1414 Argument BACKEND is the backend you are using."
1415 (let ((ignore
1416 (vc-call-backend backend 'find-ignore-file (or directory default-directory)))
1417 (pattern (file-relative-name
1418 (expand-file-name file) (file-name-directory file))))
1419 (if remove
1420 (vc--remove-regexp pattern ignore)
1421 (vc--add-line pattern ignore))))
1423 (defun vc-default-ignore-completion-table (backend file)
1424 "Return the list of ignored files under BACKEND."
1425 (vc--read-lines
1426 (vc-call-backend backend 'find-ignore-file file)))
1428 (defun vc--read-lines (file)
1429 "Return a list of lines of FILE."
1430 (with-temp-buffer
1431 (insert-file-contents file)
1432 (split-string (buffer-string) "\n" t)))
1434 ;; Subroutine for `vc-git-ignore' and `vc-hg-ignore'.
1435 (defun vc--add-line (string file)
1436 "Add STRING as a line to FILE."
1437 (with-temp-buffer
1438 (insert-file-contents file)
1439 (unless (re-search-forward (concat "^" (regexp-quote string) "$") nil t)
1440 (goto-char (point-max))
1441 (insert (concat "\n" string))
1442 (write-region (point-min) (point-max) file))))
1444 (defun vc--remove-regexp (regexp file)
1445 "Remove all matching for REGEXP in FILE."
1446 (with-temp-buffer
1447 (insert-file-contents file)
1448 (while (re-search-forward regexp nil t)
1449 (replace-match ""))
1450 (write-region (point-min) (point-max) file)))
1452 (defun vc-checkout (file &optional rev)
1453 "Retrieve a copy of the revision REV of FILE.
1454 REV defaults to the latest revision.
1456 After check-out, runs the normal hook `vc-checkout-hook'."
1457 (and (not rev)
1458 (vc-call make-version-backups-p file)
1459 (vc-up-to-date-p file)
1460 (vc-make-version-backup file))
1461 (let ((backend (vc-backend file)))
1462 (with-vc-properties (list file)
1463 (condition-case err
1464 (vc-call-backend backend 'checkout file rev)
1465 (file-error
1466 ;; Maybe the backend is not installed ;-(
1467 (when t
1468 (let ((buf (get-file-buffer file)))
1469 (when buf (with-current-buffer buf (read-only-mode -1)))))
1470 (signal (car err) (cdr err))))
1471 `((vc-state . ,(if (or (eq (vc-checkout-model backend (list file)) 'implicit)
1472 nil)
1473 'up-to-date
1474 'edited))
1475 (vc-checkout-time . ,(nth 5 (file-attributes file))))))
1476 (vc-resynch-buffer file t t)
1477 (run-hooks 'vc-checkout-hook))
1479 (defun vc-mark-resolved (backend files)
1480 (prog1 (with-vc-properties
1481 files
1482 (vc-call-backend backend 'mark-resolved files)
1483 ;; FIXME: Is this TRTD? Might not be.
1484 `((vc-state . edited)))
1485 (message
1486 (substitute-command-keys
1487 "Conflicts have been resolved in %s. \
1488 Type \\[vc-next-action] to check in changes.")
1489 (if (> (length files) 1)
1490 (format "%d files" (length files))
1491 "this file"))))
1493 (defun vc-steal-lock (file rev owner)
1494 "Steal the lock on FILE."
1495 (let (file-description)
1496 (if rev
1497 (setq file-description (format "%s:%s" file rev))
1498 (setq file-description file))
1499 (when (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1500 file-description owner)))
1501 (error "Steal canceled"))
1502 (message "Stealing lock on %s..." file)
1503 (with-vc-properties
1504 (list file)
1505 (vc-call steal-lock file rev)
1506 `((vc-state . edited)))
1507 (vc-resynch-buffer file t t)
1508 (message "Stealing lock on %s...done" file)
1509 ;; Write mail after actually stealing, because if the stealing
1510 ;; goes wrong, we don't want to send any mail.
1511 (compose-mail owner (format "Stolen lock on %s" file-description))
1512 (setq default-directory (expand-file-name "~/"))
1513 (goto-char (point-max))
1514 (insert
1515 (format "I stole the lock on %s, " file-description)
1516 (current-time-string)
1517 ".\n")
1518 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1520 (defun vc-checkin (files backend &optional comment initial-contents)
1521 "Check in FILES. COMMENT is a comment string; if omitted, a
1522 buffer is popped up to accept a comment. If INITIAL-CONTENTS is
1523 non-nil, then COMMENT is used as the initial contents of the log
1524 entry buffer.
1526 Runs the normal hooks `vc-before-checkin-hook' and `vc-checkin-hook'."
1527 (when vc-before-checkin-hook
1528 (run-hooks 'vc-before-checkin-hook))
1529 (vc-start-logentry
1530 files comment initial-contents
1531 "Enter a change comment."
1532 "*vc-log*"
1533 (lambda ()
1534 (vc-call-backend backend 'log-edit-mode))
1535 (lambda (files comment)
1536 (message "Checking in %s..." (vc-delistify files))
1537 ;; "This log message intentionally left almost blank".
1538 ;; RCS 5.7 gripes about white-space-only comments too.
1539 (or (and comment (string-match "[^\t\n ]" comment))
1540 (setq comment "*** empty log message ***"))
1541 (with-vc-properties
1542 files
1543 ;; We used to change buffers to get local value of
1544 ;; vc-checkin-switches, but 'the' local buffer is
1545 ;; not a well-defined concept for filesets.
1546 (progn
1547 (vc-call-backend backend 'checkin files comment)
1548 (mapc 'vc-delete-automatic-version-backups files))
1549 `((vc-state . up-to-date)
1550 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1551 (vc-working-revision . nil)))
1552 (message "Checking in %s...done" (vc-delistify files)))
1553 'vc-checkin-hook
1554 backend))
1556 ;;; Additional entry points for examining version histories
1558 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1559 ;; "List differences for all registered files at and below DIR.
1560 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1561 ;; ;; This implementation does an explicit tree walk, and calls
1562 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1563 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1564 ;; ;; and to call it only for files that are actually changed.
1565 ;; ;; However, this is expensive for some backends, and so it is left
1566 ;; ;; to backend-specific implementations.
1567 ;; (setq default-directory dir)
1568 ;; (vc-file-tree-walk
1569 ;; default-directory
1570 ;; (lambda (f)
1571 ;; (vc-run-delayed
1572 ;; (let ((coding-system-for-read (vc-coding-system-for-diff f)))
1573 ;; (message "Looking at %s" f)
1574 ;; (vc-call-backend (vc-backend f)
1575 ;; 'diff (list f) rev1 rev2))))))
1577 (defvar vc-coding-system-inherit-eol t
1578 "When non-nil, inherit the EOL format for reading Diff output from the file.
1580 Used in `vc-coding-system-for-diff' to determine the EOL format to use
1581 for reading Diff output for a file. If non-nil, the EOL format is
1582 inherited from the file itself.
1583 Set this variable to nil if your Diff tool might use a different
1584 EOL. Then Emacs will auto-detect the EOL format in Diff output, which
1585 gives better results.") ;; Cf. bug#4451.
1587 (defun vc-coding-system-for-diff (file)
1588 "Return the coding system for reading diff output for FILE."
1589 (or coding-system-for-read
1590 ;; if we already have this file open,
1591 ;; use the buffer's coding system
1592 (let ((buf (find-buffer-visiting file)))
1593 (when buf (with-current-buffer buf
1594 (if vc-coding-system-inherit-eol
1595 buffer-file-coding-system
1596 ;; Don't inherit the EOL part of the coding-system,
1597 ;; because some Diff tools may choose to use
1598 ;; a different one. bug#4451.
1599 (coding-system-base buffer-file-coding-system)))))
1600 ;; otherwise, try to find one based on the file name
1601 (car (find-operation-coding-system 'insert-file-contents file))
1602 ;; and a final fallback
1603 'undecided))
1605 (defun vc-switches (backend op)
1606 "Return a list of vc-BACKEND switches for operation OP.
1607 BACKEND is a symbol such as `CVS', which will be downcased.
1608 OP is a symbol such as `diff'.
1610 In decreasing order of preference, return the value of:
1611 vc-BACKEND-OP-switches (e.g. `vc-cvs-diff-switches');
1612 vc-OP-switches (e.g. `vc-diff-switches'); or, in the case of
1613 diff only, `diff-switches'.
1615 If the chosen value is not a string or a list, return nil.
1616 This is so that you may set, e.g. `vc-svn-diff-switches' to t in order
1617 to override the value of `vc-diff-switches' and `diff-switches'."
1618 (let ((switches
1619 (or (when backend
1620 (let ((sym (vc-make-backend-sym
1621 backend (intern (concat (symbol-name op)
1622 "-switches")))))
1623 (when (boundp sym) (symbol-value sym))))
1624 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1625 (when (boundp sym) (symbol-value sym)))
1626 (cond
1627 ((eq op 'diff) diff-switches)))))
1628 (if (stringp switches) (list switches)
1629 ;; If not a list, return nil.
1630 ;; This is so we can set vc-diff-switches to t to override
1631 ;; any switches in diff-switches.
1632 (when (listp switches) switches))))
1634 ;; Old def for compatibility with Emacs-21.[123].
1635 (defmacro vc-diff-switches-list (backend)
1636 (declare (obsolete vc-switches "22.1"))
1637 `(vc-switches ',backend 'diff))
1639 (defun vc-diff-finish (buffer messages)
1640 ;; The empty sync output case has already been handled, so the only
1641 ;; possibility of an empty output is for an async process.
1642 (when (buffer-live-p buffer)
1643 (let ((window (get-buffer-window buffer t))
1644 (emptyp (zerop (buffer-size buffer))))
1645 (with-current-buffer buffer
1646 (and messages emptyp
1647 (let ((inhibit-read-only t))
1648 (insert (cdr messages) ".\n")
1649 (message "%s" (cdr messages))))
1650 (diff-setup-whitespace)
1651 (goto-char (point-min))
1652 (when window
1653 (shrink-window-if-larger-than-buffer window)))
1654 (when (and messages (not emptyp))
1655 (message "%sdone" (car messages))))))
1657 (defvar vc-diff-added-files nil
1658 "If non-nil, diff added files by comparing them to /dev/null.")
1660 (defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose buffer)
1661 "Report diffs between two revisions of a fileset.
1662 Output goes to the buffer BUFFER, which defaults to *vc-diff*.
1663 BUFFER, if non-nil, should be a buffer or a buffer name.
1664 Return t if the buffer had changes, nil otherwise."
1665 (unless buffer
1666 (setq buffer "*vc-diff*"))
1667 (let* ((files (cadr vc-fileset))
1668 (messages (cons (format "Finding changes in %s..."
1669 (vc-delistify files))
1670 (format "No changes between %s and %s"
1671 (or rev1 "working revision")
1672 (or rev2 "workfile"))))
1673 ;; Set coding system based on the first file. It's a kluge,
1674 ;; but the only way to set it for each file included would
1675 ;; be to call the back end separately for each file.
1676 (coding-system-for-read
1677 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
1678 ;; On MS-Windows and MS-DOS, Diff is likely to produce DOS-style
1679 ;; EOLs, which will look ugly if (car files) happens to have Unix
1680 ;; EOLs.
1681 (if (memq system-type '(windows-nt ms-dos))
1682 (setq coding-system-for-read
1683 (coding-system-change-eol-conversion coding-system-for-read
1684 'dos)))
1685 (vc-setup-buffer buffer)
1686 (message "%s" (car messages))
1687 ;; Many backends don't handle well the case of a file that has been
1688 ;; added but not yet committed to the repo (notably CVS and Subversion).
1689 ;; Do that work here so the backends don't have to futz with it. --ESR
1691 ;; Actually most backends (including CVS) have options to control the
1692 ;; behavior since which one is better depends on the user and on the
1693 ;; situation). Worse yet: this code does not handle the case where
1694 ;; `file' is a directory which contains added files.
1695 ;; I made it conditional on vc-diff-added-files but it should probably
1696 ;; just be removed (or copied/moved to specific backends). --Stef.
1697 (when vc-diff-added-files
1698 (let ((filtered '())
1699 process-file-side-effects)
1700 (dolist (file files)
1701 (if (or (file-directory-p file)
1702 (not (string= (vc-working-revision file) "0")))
1703 (push file filtered)
1704 ;; This file is added but not yet committed;
1705 ;; there is no repository version to diff against.
1706 (if (or rev1 rev2)
1707 (error "No revisions of %s exist" file)
1708 ;; We regard this as "changed".
1709 ;; Diff it against /dev/null.
1710 (apply 'vc-do-command buffer
1711 (if async 'async 1) "diff" file
1712 (append (vc-switches nil 'diff) '("/dev/null"))))))
1713 (setq files (nreverse filtered))))
1714 (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 buffer async)
1715 (set-buffer buffer)
1716 (diff-mode)
1717 (set (make-local-variable 'diff-vc-backend) (car vc-fileset))
1718 (set (make-local-variable 'revert-buffer-function)
1719 (lambda (_ignore-auto _noconfirm)
1720 (vc-diff-internal async vc-fileset rev1 rev2 verbose)))
1721 ;; Make the *vc-diff* buffer read only, the diff-mode key
1722 ;; bindings are nicer for read only buffers. pcl-cvs does the
1723 ;; same thing.
1724 (setq buffer-read-only t)
1725 (if (and (zerop (buffer-size))
1726 (not (get-buffer-process (current-buffer))))
1727 ;; Treat this case specially so as not to pop the buffer.
1728 (progn
1729 (message "%s" (cdr messages))
1730 nil)
1731 ;; Display the buffer, but at the end because it can change point.
1732 (pop-to-buffer (current-buffer))
1733 ;; The diff process may finish early, so call `vc-diff-finish'
1734 ;; after `pop-to-buffer'; the former assumes the diff buffer is
1735 ;; shown in some window.
1736 (let ((buf (current-buffer)))
1737 (vc-run-delayed (vc-diff-finish buf (when verbose messages))))
1738 ;; In the async case, we return t even if there are no differences
1739 ;; because we don't know that yet.
1740 t)))
1742 (defun vc-read-revision (prompt &optional files backend default initial-input)
1743 (cond
1744 ((null files)
1745 (let ((vc-fileset (vc-deduce-fileset t))) ;FIXME: why t? --Stef
1746 (setq files (cadr vc-fileset))
1747 (setq backend (car vc-fileset))))
1748 ((null backend) (setq backend (vc-backend (car files)))))
1749 (let ((completion-table
1750 (vc-call-backend backend 'revision-completion-table files)))
1751 (if completion-table
1752 (completing-read prompt completion-table
1753 nil nil initial-input nil default)
1754 (read-string prompt initial-input nil default))))
1756 (defun vc-diff-build-argument-list-internal ()
1757 "Build argument list for calling internal diff functions."
1758 (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: why t? --Stef
1759 (files (cadr vc-fileset))
1760 (backend (car vc-fileset))
1761 (first (car files))
1762 (rev1-default nil)
1763 (rev2-default nil))
1764 (cond
1765 ;; someday we may be able to do revision completion on non-singleton
1766 ;; filesets, but not yet.
1767 ((/= (length files) 1)
1768 nil)
1769 ;; if it's a directory, don't supply any revision default
1770 ((file-directory-p first)
1771 nil)
1772 ;; if the file is not up-to-date, use working revision as older revision
1773 ((not (vc-up-to-date-p first))
1774 (setq rev1-default (vc-working-revision first)))
1775 ;; if the file is not locked, use last revision and current source as defaults
1777 (setq rev1-default (ignore-errors ;If `previous-revision' doesn't work.
1778 (vc-call-backend backend 'previous-revision first
1779 (vc-working-revision first))))
1780 (when (string= rev1-default "") (setq rev1-default nil))))
1781 ;; construct argument list
1782 (let* ((rev1-prompt (if rev1-default
1783 (concat "Older revision (default "
1784 rev1-default "): ")
1785 "Older revision: "))
1786 (rev2-prompt (concat "Newer revision (default "
1787 (or rev2-default "current source") "): "))
1788 (rev1 (vc-read-revision rev1-prompt files backend rev1-default))
1789 (rev2 (vc-read-revision rev2-prompt files backend rev2-default)))
1790 (when (string= rev1 "") (setq rev1 nil))
1791 (when (string= rev2 "") (setq rev2 nil))
1792 (list files rev1 rev2))))
1794 ;;;###autoload
1795 (defun vc-version-diff (_files rev1 rev2)
1796 "Report diffs between revisions of the fileset in the repository history."
1797 (interactive (vc-diff-build-argument-list-internal))
1798 ;; All that was just so we could do argument completion!
1799 (when (and (not rev1) rev2)
1800 (error "Not a valid revision range"))
1801 ;; Yes, it's painful to call (vc-deduce-fileset) again. Alas, the
1802 ;; placement rules for (interactive) don't actually leave us a choice.
1803 (vc-diff-internal t (vc-deduce-fileset t) rev1 rev2
1804 (called-interactively-p 'interactive)))
1806 ;;;###autoload
1807 (defun vc-diff (&optional historic not-urgent)
1808 "Display diffs between file revisions.
1809 Normally this compares the currently selected fileset with their
1810 working revisions. With a prefix argument HISTORIC, it reads two revision
1811 designators specifying which revisions to compare.
1813 The optional argument NOT-URGENT non-nil means it is ok to say no to
1814 saving the buffer."
1815 (interactive (list current-prefix-arg t))
1816 (if historic
1817 (call-interactively 'vc-version-diff)
1818 (when buffer-file-name (vc-buffer-sync not-urgent))
1819 (vc-diff-internal t (vc-deduce-fileset t) nil nil
1820 (called-interactively-p 'interactive))))
1822 (declare-function ediff-load-version-control "ediff" (&optional silent))
1823 (declare-function ediff-vc-internal "ediff-vers"
1824 (rev1 rev2 &optional startup-hooks))
1826 ;;;###autoload
1827 (defun vc-version-ediff (files rev1 rev2)
1828 "Show differences between revisions of the fileset in the
1829 repository history using ediff."
1830 (interactive (vc-diff-build-argument-list-internal))
1831 ;; All that was just so we could do argument completion!
1832 (when (and (not rev1) rev2)
1833 (error "Not a valid revision range"))
1835 (message "%s" (format "Finding changes in %s..." (vc-delistify files)))
1837 ;; Functions ediff-(vc|rcs)-internal use "" instead of nil.
1838 (when (null rev1) (setq rev1 ""))
1839 (when (null rev2) (setq rev2 ""))
1841 (cond
1842 ;; FIXME We only support running ediff on one file for now.
1843 ;; We could spin off an ediff session per file in the file set.
1844 ((= (length files) 1)
1845 (require 'ediff)
1846 (ediff-load-version-control) ; loads ediff-vers
1847 (find-file (car files)) ;FIXME: find-file from Elisp is bad.
1848 (ediff-vc-internal rev1 rev2 nil))
1850 (error "More than one file is not supported"))))
1852 ;;;###autoload
1853 (defun vc-ediff (historic &optional not-urgent)
1854 "Display diffs between file revisions using ediff.
1855 Normally this compares the currently selected fileset with their
1856 working revisions. With a prefix argument HISTORIC, it reads two revision
1857 designators specifying which revisions to compare.
1859 The optional argument NOT-URGENT non-nil means it is ok to say no to
1860 saving the buffer."
1861 (interactive (list current-prefix-arg t))
1862 (if historic
1863 (call-interactively 'vc-version-ediff)
1864 (when buffer-file-name (vc-buffer-sync not-urgent))
1865 (vc-version-ediff (cadr (vc-deduce-fileset t)) nil nil)))
1867 ;;;###autoload
1868 (defun vc-root-diff (historic &optional not-urgent)
1869 "Display diffs between VC-controlled whole tree revisions.
1870 Normally, this compares the tree corresponding to the current
1871 fileset with the working revision.
1872 With a prefix argument HISTORIC, prompt for two revision
1873 designators specifying which revisions to compare.
1875 The optional argument NOT-URGENT non-nil means it is ok to say no to
1876 saving the buffer."
1877 (interactive (list current-prefix-arg t))
1878 (if historic
1879 ;; FIXME: this does not work right, `vc-version-diff' ends up
1880 ;; calling `vc-deduce-fileset' to find the files to diff, and
1881 ;; that's not what we want here, we want the diff for the VC root dir.
1882 (call-interactively 'vc-version-diff)
1883 (when buffer-file-name (vc-buffer-sync not-urgent))
1884 (let ((backend (vc-deduce-backend))
1885 (default-directory default-directory)
1886 rootdir working-revision)
1887 (if backend
1888 (setq rootdir (vc-call-backend backend 'root default-directory))
1889 (setq rootdir (read-directory-name "Directory for VC root-diff: "))
1890 (setq backend (vc-responsible-backend rootdir))
1891 (if backend
1892 (setq default-directory rootdir)
1893 (error "Directory is not version controlled")))
1894 (setq working-revision (vc-working-revision rootdir))
1895 ;; VC diff for the root directory produces output that is
1896 ;; relative to it. Bind default-directory to the root directory
1897 ;; here, this way the *vc-diff* buffer is setup correctly, so
1898 ;; relative file names work.
1899 (let ((default-directory rootdir))
1900 (vc-diff-internal
1901 t (list backend (list rootdir) working-revision) nil nil
1902 (called-interactively-p 'interactive))))))
1904 ;;;###autoload
1905 (defun vc-root-dir ()
1906 "Return the root directory for the current VC tree.
1907 Return nil if the root directory cannot be identified."
1908 (let ((backend (vc-deduce-backend)))
1909 (if backend
1910 (condition-case err
1911 (vc-call-backend backend 'root default-directory)
1912 (vc-not-supported
1913 (unless (eq (cadr err) 'root)
1914 (signal (car err) (cdr err)))
1915 nil)))))
1917 ;;;###autoload
1918 (defun vc-revision-other-window (rev)
1919 "Visit revision REV of the current file in another window.
1920 If the current file is named `F', the revision is named `F.~REV~'.
1921 If `F.~REV~' already exists, use it instead of checking it out again."
1922 (interactive
1923 (save-current-buffer
1924 (vc-ensure-vc-buffer)
1925 (list
1926 (vc-read-revision "Revision to visit (default is working revision): "
1927 (list buffer-file-name)))))
1928 (vc-ensure-vc-buffer)
1929 (let* ((file buffer-file-name)
1930 (revision (if (string-equal rev "")
1931 (vc-working-revision file)
1932 rev)))
1933 (switch-to-buffer-other-window (vc-find-revision file revision))))
1935 (defun vc-find-revision (file revision &optional backend)
1936 "Read REVISION of FILE into a buffer and return the buffer.
1937 Use BACKEND as the VC backend if specified."
1938 (let ((automatic-backup (vc-version-backup-file-name file revision))
1939 (filebuf (or (get-file-buffer file) (current-buffer)))
1940 (filename (vc-version-backup-file-name file revision 'manual)))
1941 (unless (file-exists-p filename)
1942 (if (file-exists-p automatic-backup)
1943 (rename-file automatic-backup filename nil)
1944 (message "Checking out %s..." filename)
1945 (with-current-buffer filebuf
1946 (let ((failed t))
1947 (unwind-protect
1948 (let ((coding-system-for-read 'no-conversion)
1949 (coding-system-for-write 'no-conversion))
1950 (with-temp-file filename
1951 (let ((outbuf (current-buffer)))
1952 ;; Change buffer to get local value of
1953 ;; vc-checkout-switches.
1954 (with-current-buffer filebuf
1955 (if backend
1956 (vc-call-backend backend 'find-revision file revision outbuf)
1957 (vc-call find-revision file revision outbuf)))))
1958 (setq failed nil))
1959 (when (and failed (file-exists-p filename))
1960 (delete-file filename))))
1961 (vc-mode-line file))
1962 (message "Checking out %s...done" filename)))
1963 (let ((result-buf (find-file-noselect filename)))
1964 (with-current-buffer result-buf
1965 ;; Set the parent buffer so that things like
1966 ;; C-x v g, C-x v l, ... etc work.
1967 (set (make-local-variable 'vc-parent-buffer) filebuf))
1968 result-buf)))
1970 ;; Header-insertion code
1972 ;;;###autoload
1973 (defun vc-insert-headers ()
1974 "Insert headers into a file for use with a version control system.
1975 Headers desired are inserted at point, and are pulled from
1976 the variable `vc-BACKEND-header'."
1977 (interactive)
1978 (vc-ensure-vc-buffer)
1979 (save-excursion
1980 (save-restriction
1981 (widen)
1982 (when (or (not (vc-check-headers))
1983 (y-or-n-p "Version headers already exist. Insert another set? "))
1984 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1985 (comment-start-vc (or (car delims) comment-start "#"))
1986 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1987 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
1988 'header))
1989 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1990 (dolist (s hdstrings)
1991 (insert comment-start-vc "\t" s "\t"
1992 comment-end-vc "\n"))
1993 (when vc-static-header-alist
1994 (dolist (f vc-static-header-alist)
1995 (when (string-match (car f) buffer-file-name)
1996 (insert (format (cdr f) (car hdstrings)))))))))))
1998 (defun vc-modify-change-comment (files rev oldcomment)
1999 "Edit the comment associated with the given files and revision."
2000 ;; Less of a kluge than it looks like; log-view mode only passes
2001 ;; this function a singleton list. Arguments left in this form in
2002 ;; case the more general operation ever becomes meaningful.
2003 (let ((backend (vc-responsible-backend (car files))))
2004 (vc-start-logentry
2005 files oldcomment t
2006 "Enter a replacement change comment."
2007 "*vc-log*"
2008 (lambda () (vc-call-backend backend 'log-edit-mode))
2009 (lambda (files comment)
2010 (vc-call-backend backend
2011 'modify-change-comment files rev comment)))))
2013 ;;;###autoload
2014 (defun vc-merge ()
2015 "Perform a version control merge operation.
2016 You must be visiting a version controlled file, or in a `vc-dir' buffer.
2017 On a distributed version control system, this runs a \"merge\"
2018 operation to incorporate changes from another branch onto the
2019 current branch, prompting for an argument list.
2021 On a non-distributed version control system, this merges changes
2022 between two revisions into the current fileset. This asks for
2023 two revisions to merge from in the minibuffer. If the first
2024 revision is a branch number, then merge all changes from that
2025 branch. If the first revision is empty, merge the most recent
2026 changes from the current branch."
2027 (interactive)
2028 (let* ((vc-fileset (vc-deduce-fileset t))
2029 (backend (car vc-fileset))
2030 (files (cadr vc-fileset)))
2031 (cond
2032 ;; If a branch-merge operation is defined, use it.
2033 ((vc-find-backend-function backend 'merge-branch)
2034 (vc-call-backend backend 'merge-branch))
2035 ;; Otherwise, do a per-file merge.
2036 ((vc-find-backend-function backend 'merge)
2037 (vc-buffer-sync)
2038 (dolist (file files)
2039 (let* ((state (vc-state file))
2040 status)
2041 (cond
2042 ((stringp state) ;; Locking VCses only
2043 (error "File %s is locked by %s" file state))
2044 ((not (vc-editable-p file))
2045 (vc-checkout file t)))
2046 (setq status (vc-call-backend backend 'merge-file file))
2047 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE"))))
2049 (error "Sorry, merging is not implemented for %s" backend)))))
2051 (defun vc-maybe-resolve-conflicts (file status &optional _name-A _name-B)
2052 (vc-resynch-buffer file t (not (buffer-modified-p)))
2053 (if (zerop status) (message "Merge successful")
2054 (smerge-mode 1)
2055 (message "File contains conflicts.")))
2057 ;;;###autoload
2058 (defalias 'vc-resolve-conflicts 'smerge-ediff)
2060 ;; TODO: This is OK but maybe we could integrate it better.
2061 ;; E.g. it could be run semi-automatically (via a prompt?) when saving a file
2062 ;; that was conflicted (i.e. upon mark-resolved).
2063 ;; FIXME: should we add an "other-window" version? Or maybe we should
2064 ;; hook it inside find-file so it automatically works for
2065 ;; find-file-other-window as well. E.g. find-file could use a new
2066 ;; `default-next-file' variable for its default file (M-n), and
2067 ;; we could then set it upon mark-resolve, so C-x C-s C-x C-f M-n would
2068 ;; automatically offer the next conflicted file.
2069 (defun vc-find-conflicted-file ()
2070 "Visit the next conflicted file in the current project."
2071 (interactive)
2072 (let* ((backend (or (if buffer-file-name (vc-backend buffer-file-name))
2073 (vc-responsible-backend default-directory)
2074 (error "No VC backend")))
2075 (root (vc-root-dir))
2076 (files (vc-call-backend backend
2077 'conflicted-files (or root default-directory))))
2078 ;; Don't try and visit the current file.
2079 (if (equal (car files) buffer-file-name) (pop files))
2080 (if (null files)
2081 (message "No more conflicted files")
2082 (find-file (pop files))
2083 (message "%s more conflicted files after this one"
2084 (if files (length files) "No")))))
2086 ;; Named-configuration entry points
2088 (defun vc-tag-precondition (dir)
2089 "Scan the tree below DIR, looking for files not up-to-date.
2090 If any file is not up-to-date, return the name of the first such file.
2091 \(This means, neither tag creation nor retrieval is allowed.\)
2092 If one or more of the files are currently visited, return `visited'.
2093 Otherwise, return nil."
2094 (let ((status nil))
2095 (catch 'vc-locked-example
2096 (vc-file-tree-walk
2098 (lambda (f)
2099 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2100 (when (get-file-buffer f) (setq status 'visited)))))
2101 status)))
2103 ;;;###autoload
2104 (defun vc-create-tag (dir name branchp)
2105 "Descending recursively from DIR, make a tag called NAME.
2106 For each registered file, the working revision becomes part of
2107 the named configuration. If the prefix argument BRANCHP is
2108 given, the tag is made as a new branch and the files are
2109 checked out in that new branch."
2110 (interactive
2111 (let ((granularity
2112 (vc-call-backend (vc-responsible-backend default-directory)
2113 'revision-granularity)))
2114 (list
2115 (if (eq granularity 'repository)
2116 ;; For VC's that do not work at file level, it's pointless
2117 ;; to ask for a directory, branches are created at repository level.
2118 default-directory
2119 (read-directory-name "Directory: " default-directory default-directory t))
2120 (read-string (if current-prefix-arg "New branch name: " "New tag name: "))
2121 current-prefix-arg)))
2122 (message "Making %s... " (if branchp "branch" "tag"))
2123 (when (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2124 (vc-call-backend (vc-responsible-backend dir)
2125 'create-tag dir name branchp)
2126 (vc-resynch-buffer dir t t t)
2127 (message "Making %s... done" (if branchp "branch" "tag")))
2129 ;;;###autoload
2130 (defun vc-retrieve-tag (dir name)
2131 "For each file in or below DIR, retrieve their tagged version NAME.
2132 NAME can name a branch, in which case this command will switch to the
2133 named branch in the directory DIR.
2134 Interactively, prompt for DIR only for VCS that works at file level;
2135 otherwise use the default directory of the current buffer.
2136 If NAME is empty, it refers to the latest revisions of the current branch.
2137 If locking is used for the files in DIR, then there must not be any
2138 locked files at or below DIR (but if NAME is empty, locked files are
2139 allowed and simply skipped)."
2140 (interactive
2141 (let ((granularity
2142 (vc-call-backend (vc-responsible-backend default-directory)
2143 'revision-granularity)))
2144 (list
2145 (if (eq granularity 'repository)
2146 ;; For VC's that do not work at file level, it's pointless
2147 ;; to ask for a directory, branches are created at repository level.
2148 default-directory
2149 (read-directory-name "Directory: " default-directory default-directory t))
2150 (read-string "Tag name to retrieve (default latest revisions): "))))
2151 (let ((update (yes-or-no-p "Update any affected buffers? "))
2152 (msg (if (or (not name) (string= name ""))
2153 (format "Updating %s... " (abbreviate-file-name dir))
2154 (format "Retrieving tag into %s... "
2155 (abbreviate-file-name dir)))))
2156 (message "%s" msg)
2157 (vc-call-backend (vc-responsible-backend dir)
2158 'retrieve-tag dir name update)
2159 (vc-resynch-buffer dir t t t)
2160 (message "%s" (concat msg "done"))))
2163 ;; Miscellaneous other entry points
2165 ;; FIXME: this should be a defcustom
2166 ;; FIXME: maybe add another choice:
2167 ;; `root-directory' (or somesuch), which would mean show a short log
2168 ;; for the root directory.
2169 (defvar vc-log-short-style '(directory)
2170 "Whether or not to show a short log.
2171 If it contains `directory' then if the fileset contains a directory show a short log.
2172 If it contains `file' then show short logs for files.
2173 Not all VC backends support short logs!")
2175 (defvar log-view-vc-fileset)
2177 (defun vc-print-log-setup-buttons (working-revision is-start-revision limit pl-return)
2178 "Insert at the end of the current buffer buttons to show more log entries.
2179 In the new log, leave point at WORKING-REVISION (if non-nil).
2180 LIMIT is the number of entries currently shown.
2181 Does nothing if IS-START-REVISION is non-nil, or if LIMIT is nil,
2182 or if PL-RETURN is 'limit-unsupported."
2183 (when (and limit (not (eq 'limit-unsupported pl-return))
2184 (not is-start-revision))
2185 (goto-char (point-max))
2186 (insert "\n")
2187 (insert-text-button "Show 2X entries"
2188 'action (lambda (&rest _ignore)
2189 (vc-print-log-internal
2190 log-view-vc-backend log-view-vc-fileset
2191 working-revision nil (* 2 limit)))
2192 'help-echo "Show the log again, and double the number of log entries shown")
2193 (insert " ")
2194 (insert-text-button "Show unlimited entries"
2195 'action (lambda (&rest _ignore)
2196 (vc-print-log-internal
2197 log-view-vc-backend log-view-vc-fileset
2198 working-revision nil nil))
2199 'help-echo "Show the log again, including all entries")))
2201 (defun vc-print-log-internal (backend files working-revision
2202 &optional is-start-revision limit)
2203 "For specified BACKEND and FILES, show the VC log.
2204 Leave point at WORKING-REVISION, if it is non-nil.
2205 If IS-START-REVISION is non-nil, start the log from WORKING-REVISION
2206 \(not all backends support this); i.e., show only WORKING-REVISION and
2207 earlier revisions. Show up to LIMIT entries (non-nil means unlimited)."
2208 ;; As of 2013/04 the only thing that passes IS-START-REVISION non-nil
2209 ;; is vc-annotate-show-log-revision-at-line, which sets LIMIT = 1.
2211 ;; Don't switch to the output buffer before running the command,
2212 ;; so that any buffer-local settings in the vc-controlled
2213 ;; buffer can be accessed by the command.
2214 (let* ((dir-present (cl-some #'file-directory-p files))
2215 (shortlog (not (null (memq (if dir-present 'directory 'file)
2216 vc-log-short-style))))
2217 (buffer-name "*vc-change-log*")
2218 (type (if shortlog 'short 'long)))
2219 (vc-log-internal-common
2220 backend buffer-name files type
2221 (lambda (bk buf _type-arg files-arg)
2222 (vc-call-backend bk 'print-log files-arg buf shortlog
2223 (when is-start-revision working-revision) limit))
2224 (lambda (_bk _files-arg ret)
2225 (vc-print-log-setup-buttons working-revision
2226 is-start-revision limit ret))
2227 ;; When it's nil, point really shouldn't move (bug#15322).
2228 (when working-revision
2229 (lambda (bk)
2230 (vc-call-backend bk 'show-log-entry working-revision)))
2231 (lambda (_ignore-auto _noconfirm)
2232 (vc-print-log-internal backend files working-revision
2233 is-start-revision limit)))))
2235 (defvar vc-log-view-type nil
2236 "Set this to differentiate the different types of logs.")
2237 (put 'vc-log-view-type 'permanent-local t)
2238 (defvar vc-sentinel-movepoint)
2240 (defun vc-log-internal-common (backend
2241 buffer-name
2242 files
2243 type
2244 backend-func
2245 setup-buttons-func
2246 goto-location-func
2247 rev-buff-func)
2248 (let (retval)
2249 (with-current-buffer (get-buffer-create buffer-name)
2250 (set (make-local-variable 'vc-log-view-type) type))
2251 (setq retval (funcall backend-func backend buffer-name type files))
2252 (with-current-buffer (get-buffer buffer-name)
2253 (let ((inhibit-read-only t))
2254 ;; log-view-mode used to be called with inhibit-read-only bound
2255 ;; to t, so let's keep doing it, just in case.
2256 (vc-call-backend backend 'log-view-mode)
2257 (set (make-local-variable 'log-view-vc-backend) backend)
2258 (set (make-local-variable 'log-view-vc-fileset) files)
2259 (set (make-local-variable 'revert-buffer-function)
2260 rev-buff-func)))
2261 ;; Display after setting up major-mode, so display-buffer-alist can know
2262 ;; the major-mode.
2263 (pop-to-buffer buffer-name)
2264 (vc-run-delayed
2265 (let ((inhibit-read-only t))
2266 (funcall setup-buttons-func backend files retval)
2267 (shrink-window-if-larger-than-buffer)
2268 (when goto-location-func
2269 (funcall goto-location-func backend)
2270 (setq vc-sentinel-movepoint (point)))
2271 (set-buffer-modified-p nil)))))
2273 (defun vc-incoming-outgoing-internal (backend remote-location buffer-name type)
2274 (vc-log-internal-common
2275 backend buffer-name nil type
2276 (lambda (bk buf type-arg _files)
2277 (vc-call-backend bk type-arg buf remote-location))
2278 (lambda (_bk _files-arg _ret) nil)
2279 nil ;; Don't move point.
2280 (lambda (_ignore-auto _noconfirm)
2281 (vc-incoming-outgoing-internal backend remote-location buffer-name type))))
2283 ;;;###autoload
2284 (defun vc-print-log (&optional working-revision limit)
2285 "List the change log of the current fileset in a window.
2286 If WORKING-REVISION is non-nil, leave point at that revision.
2287 If LIMIT is non-nil, it should be a number specifying the maximum
2288 number of revisions to show; the default is `vc-log-show-limit'.
2290 When called interactively with a prefix argument, prompt for
2291 WORKING-REVISION and LIMIT."
2292 (interactive
2293 (cond
2294 (current-prefix-arg
2295 (let ((rev (read-from-minibuffer "Leave point at revision (default: last revision): " nil
2296 nil nil nil))
2297 (lim (string-to-number
2298 (read-from-minibuffer
2299 "Limit display (unlimited: 0): "
2300 (format "%s" vc-log-show-limit)
2301 nil nil nil))))
2302 (when (string= rev "") (setq rev nil))
2303 (when (<= lim 0) (setq lim nil))
2304 (list rev lim)))
2306 (list nil (when (> vc-log-show-limit 0) vc-log-show-limit)))))
2307 (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: Why t? --Stef
2308 (backend (car vc-fileset))
2309 (files (cadr vc-fileset))
2310 ;; (working-revision (or working-revision (vc-working-revision (car files))))
2312 (vc-print-log-internal backend files working-revision nil limit)))
2314 ;;;###autoload
2315 (defun vc-print-root-log (&optional limit)
2316 "List the change log for the current VC controlled tree in a window.
2317 If LIMIT is non-nil, it should be a number specifying the maximum
2318 number of revisions to show; the default is `vc-log-show-limit'.
2319 When called interactively with a prefix argument, prompt for LIMIT."
2320 (interactive
2321 (cond
2322 (current-prefix-arg
2323 (let ((lim (string-to-number
2324 (read-from-minibuffer
2325 "Limit display (unlimited: 0): "
2326 (format "%s" vc-log-show-limit)
2327 nil nil nil))))
2328 (when (<= lim 0) (setq lim nil))
2329 (list lim)))
2331 (list (when (> vc-log-show-limit 0) vc-log-show-limit)))))
2332 (let ((backend (vc-deduce-backend))
2333 (default-directory default-directory)
2334 rootdir)
2335 (if backend
2336 (setq rootdir (vc-call-backend backend 'root default-directory))
2337 (setq rootdir (read-directory-name "Directory for VC root-log: "))
2338 (setq backend (vc-responsible-backend rootdir))
2339 (unless backend
2340 (error "Directory is not version controlled")))
2341 (setq default-directory rootdir)
2342 (vc-print-log-internal backend (list rootdir) nil nil limit)))
2344 ;;;###autoload
2345 (defun vc-log-incoming (&optional remote-location)
2346 "Show a log of changes that will be received with a pull operation from REMOTE-LOCATION.
2347 When called interactively with a prefix argument, prompt for REMOTE-LOCATION."
2348 (interactive
2349 (when current-prefix-arg
2350 (list (read-string "Remote location (empty for default): "))))
2351 (let ((backend (vc-deduce-backend)))
2352 (unless backend
2353 (error "Buffer is not version controlled"))
2354 (vc-incoming-outgoing-internal backend remote-location "*vc-incoming*"
2355 'log-incoming)))
2357 ;;;###autoload
2358 (defun vc-log-outgoing (&optional remote-location)
2359 "Show a log of changes that will be sent with a push operation to REMOTE-LOCATION.
2360 When called interactively with a prefix argument, prompt for REMOTE-LOCATION."
2361 (interactive
2362 (when current-prefix-arg
2363 (list (read-string "Remote location (empty for default): "))))
2364 (let ((backend (vc-deduce-backend)))
2365 (unless backend
2366 (error "Buffer is not version controlled"))
2367 (vc-incoming-outgoing-internal backend remote-location "*vc-outgoing*"
2368 'log-outgoing)))
2370 ;;;###autoload
2371 (defun vc-region-history (from to)
2372 "Show the history of the region FROM..TO."
2373 (interactive "r")
2374 (let* ((lfrom (line-number-at-pos from))
2375 (lto (line-number-at-pos to))
2376 (file buffer-file-name)
2377 (backend (vc-backend file))
2378 (buf (get-buffer-create "*VC-history*")))
2379 (with-current-buffer buf
2380 (setq-local vc-log-view-type 'long))
2381 (vc-call region-history file buf lfrom lto)
2382 (with-current-buffer buf
2383 (vc-call-backend backend 'region-history-mode)
2384 (set (make-local-variable 'log-view-vc-backend) backend)
2385 (set (make-local-variable 'log-view-vc-fileset) file)
2386 (set (make-local-variable 'revert-buffer-function)
2387 (lambda (_ignore-auto _noconfirm)
2388 (with-current-buffer buf
2389 (let ((inhibit-read-only t)) (erase-buffer)))
2390 (vc-call region-history file buf lfrom lto))))
2391 (display-buffer buf)))
2393 ;;;###autoload
2394 (defun vc-revert ()
2395 "Revert working copies of the selected fileset to their repository contents.
2396 This asks for confirmation if the buffer contents are not identical
2397 to the working revision (except for keyword expansion)."
2398 (interactive)
2399 (let* ((vc-fileset (vc-deduce-fileset))
2400 (files (cadr vc-fileset))
2401 (queried nil)
2402 diff-buffer)
2403 ;; If any of the files is visited by the current buffer, make sure
2404 ;; buffer is saved. If the user says `no', abort since we cannot
2405 ;; show the changes and ask for confirmation to discard them.
2406 (when (or (not files) (memq (buffer-file-name) files))
2407 (vc-buffer-sync nil))
2408 (dolist (file files)
2409 (let ((buf (get-file-buffer file)))
2410 (when (and buf (buffer-modified-p buf))
2411 (error "Please kill or save all modified buffers before reverting")))
2412 (when (vc-up-to-date-p file)
2413 (if (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2414 (setq queried t)
2415 (error "Revert canceled"))))
2416 (unwind-protect
2417 (when (if vc-revert-show-diff
2418 (progn
2419 (setq diff-buffer (generate-new-buffer-name "*vc-diff*"))
2420 (vc-diff-internal vc-allow-async-revert vc-fileset
2421 nil nil nil diff-buffer))
2422 ;; Avoid querying the user again.
2423 (null queried))
2424 (unless (yes-or-no-p
2425 (format "Discard changes in %s? "
2426 (let ((str (vc-delistify files))
2427 (nfiles (length files)))
2428 (if (< (length str) 50)
2430 (format "%d file%s" nfiles
2431 (if (= nfiles 1) "" "s"))))))
2432 (error "Revert canceled")))
2433 (when diff-buffer
2434 (quit-windows-on diff-buffer)))
2435 (dolist (file files)
2436 (message "Reverting %s..." (vc-delistify files))
2437 (vc-revert-file file)
2438 (message "Reverting %s...done" (vc-delistify files)))))
2440 ;;;###autoload
2441 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2443 ;;;###autoload
2444 (defun vc-pull (&optional arg)
2445 "Update the current fileset or branch.
2446 You must be visiting a version controlled file, or in a `vc-dir' buffer.
2447 On a distributed version control system, this runs a \"pull\"
2448 operation to update the current branch, prompting for an argument
2449 list if required. Optional prefix ARG forces a prompt.
2451 On a non-distributed version control system, update the current
2452 fileset to the tip revisions. For each unchanged and unlocked
2453 file, this simply replaces the work file with the latest revision
2454 on its branch. If the file contains changes, any changes in the
2455 tip revision are merged into the working file."
2456 (interactive "P")
2457 (let* ((vc-fileset (vc-deduce-fileset t))
2458 (backend (car vc-fileset))
2459 (files (cadr vc-fileset)))
2460 (cond
2461 ;; If a pull operation is defined, use it.
2462 ((vc-find-backend-function backend 'pull)
2463 (vc-call-backend backend 'pull arg))
2464 ;; If VCS has `merge-news' functionality (CVS and SVN), use it.
2465 ((vc-find-backend-function backend 'merge-news)
2466 (save-some-buffers ; save buffers visiting files
2467 nil (lambda ()
2468 (and (buffer-modified-p)
2469 (let ((file (buffer-file-name)))
2470 (and file (member file files))))))
2471 (dolist (file files)
2472 (if (vc-up-to-date-p file)
2473 (vc-checkout file t)
2474 (vc-maybe-resolve-conflicts
2475 file (vc-call-backend backend 'merge-news file)))))
2476 ;; For a locking VCS, check out each file.
2477 ((eq (vc-checkout-model backend files) 'locking)
2478 (dolist (file files)
2479 (if (vc-up-to-date-p file)
2480 (vc-checkout file t))))
2482 (error "VC update is unsupported for `%s'" backend)))))
2484 ;;;###autoload
2485 (defalias 'vc-update 'vc-pull)
2487 (defun vc-version-backup-file (file &optional rev)
2488 "Return name of backup file for revision REV of FILE.
2489 If version backups should be used for FILE, and there exists
2490 such a backup for REV or the working revision of file, return
2491 its name; otherwise return nil."
2492 (when (vc-call make-version-backups-p file)
2493 (let ((backup-file (vc-version-backup-file-name file rev)))
2494 (if (file-exists-p backup-file)
2495 backup-file
2496 ;; there is no automatic backup, but maybe the user made one manually
2497 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2498 (when (file-exists-p backup-file)
2499 backup-file)))))
2501 (defun vc-revert-file (file)
2502 "Revert FILE back to the repository working revision it was based on."
2503 (with-vc-properties
2504 (list file)
2505 (let ((backup-file (vc-version-backup-file file)))
2506 (when backup-file
2507 (copy-file backup-file file 'ok-if-already-exists)
2508 (vc-delete-automatic-version-backups file))
2509 (vc-call revert file backup-file))
2510 `((vc-state . up-to-date)
2511 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2512 (vc-resynch-buffer file t t))
2514 ;;;###autoload
2515 (defun vc-switch-backend (file backend)
2516 "Make BACKEND the current version control system for FILE.
2517 FILE must already be registered in BACKEND. The change is not
2518 permanent, only for the current session. This function only changes
2519 VC's perspective on FILE, it does not register or unregister it.
2520 By default, this command cycles through the registered backends.
2521 To get a prompt, use a prefix argument."
2522 (interactive
2523 (list
2524 (or buffer-file-name
2525 (error "There is no version-controlled file in this buffer"))
2526 (let ((crt-bk (vc-backend buffer-file-name))
2527 (backends nil))
2528 (unless crt-bk
2529 (error "File %s is not under version control" buffer-file-name))
2530 ;; Find the registered backends.
2531 (dolist (crt vc-handled-backends)
2532 (when (and (vc-call-backend crt 'registered buffer-file-name)
2533 (not (eq crt-bk crt)))
2534 (push crt backends)))
2535 ;; Find the next backend.
2536 (let ((def (car backends))
2537 (others backends))
2538 (cond
2539 ((null others) (error "No other backend to switch to"))
2540 (current-prefix-arg
2541 (intern
2542 (upcase
2543 (completing-read
2544 (format "Switch to backend [%s]: " def)
2545 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2546 nil t nil nil (downcase (symbol-name def))))))
2547 (t def))))))
2548 (unless (eq backend (vc-backend file))
2549 (vc-file-clearprops file)
2550 (vc-file-setprop file 'vc-backend backend)
2551 ;; Force recomputation of the state
2552 (unless (vc-call-backend backend 'registered file)
2553 (vc-file-clearprops file)
2554 (error "%s is not registered in %s" file backend))
2555 (vc-mode-line file)))
2557 ;;;###autoload
2558 (defun vc-transfer-file (file new-backend)
2559 "Transfer FILE to another version control system NEW-BACKEND.
2560 If NEW-BACKEND has a higher precedence than FILE's current backend
2561 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2562 NEW-BACKEND, using the revision number from the current backend as the
2563 base level. If NEW-BACKEND has a lower precedence than the current
2564 backend, then commit all changes that were made under the current
2565 backend to NEW-BACKEND, and unregister FILE from the current backend.
2566 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2567 (let* ((old-backend (vc-backend file))
2568 (edited (memq (vc-state file) '(edited needs-merge)))
2569 (registered (vc-call-backend new-backend 'registered file))
2570 (move
2571 (and registered ; Never move if not registered in new-backend yet.
2572 ;; move if new-backend comes later in vc-handled-backends
2573 (or (memq new-backend (memq old-backend vc-handled-backends))
2574 (y-or-n-p "Final transfer? "))))
2575 (comment nil))
2576 (when (eq old-backend new-backend)
2577 (error "%s is the current backend of %s" new-backend file))
2578 (if registered
2579 (set-file-modes file (logior (file-modes file) 128))
2580 ;; `registered' might have switched under us.
2581 (vc-switch-backend file old-backend)
2582 (let* ((rev (vc-working-revision file))
2583 (modified-file (and edited (make-temp-file file)))
2584 (unmodified-file (and modified-file (vc-version-backup-file file))))
2585 ;; Go back to the base unmodified file.
2586 (unwind-protect
2587 (progn
2588 (when modified-file
2589 (copy-file file modified-file 'ok-if-already-exists)
2590 ;; If we have a local copy of the unmodified file, handle that
2591 ;; here and not in vc-revert-file because we don't want to
2592 ;; delete that copy -- it is still useful for OLD-BACKEND.
2593 (if unmodified-file
2594 (copy-file unmodified-file file
2595 'ok-if-already-exists 'keep-date)
2596 (when (y-or-n-p "Get base revision from repository? ")
2597 (vc-revert-file file))))
2598 (vc-call-backend new-backend 'receive-file file rev))
2599 (when modified-file
2600 (vc-switch-backend file new-backend)
2601 (unless (eq (vc-checkout-model new-backend (list file)) 'implicit)
2602 (vc-checkout file))
2603 (rename-file modified-file file 'ok-if-already-exists)
2604 (vc-file-setprop file 'vc-checkout-time nil)))))
2605 (when move
2606 (vc-switch-backend file old-backend)
2607 (setq comment (vc-call-backend old-backend 'comment-history file))
2608 (vc-call-backend old-backend 'unregister file))
2609 (vc-switch-backend file new-backend)
2610 (when (or move edited)
2611 (vc-file-setprop file 'vc-state 'edited)
2612 (vc-mode-line file new-backend)
2613 (vc-checkin file new-backend comment (stringp comment)))))
2615 ;;;###autoload
2616 (defun vc-delete-file (file)
2617 "Delete file and mark it as such in the version control system.
2618 If called interactively, read FILE, defaulting to the current
2619 buffer's file name if it's under version control."
2620 (interactive (list (read-file-name "VC delete file: " nil
2621 (when (vc-backend buffer-file-name)
2622 buffer-file-name) t)))
2623 (setq file (expand-file-name file))
2624 (let ((buf (get-file-buffer file))
2625 (backend (vc-backend file)))
2626 (unless backend
2627 (error "File %s is not under version control"
2628 (file-name-nondirectory file)))
2629 (unless (vc-find-backend-function backend 'delete-file)
2630 (error "Deleting files under %s is not supported in VC" backend))
2631 (when (and buf (buffer-modified-p buf))
2632 (error "Please save or undo your changes before deleting %s" file))
2633 (let ((state (vc-state file)))
2634 (when (eq state 'edited)
2635 (error "Please commit or undo your changes before deleting %s" file))
2636 (when (eq state 'conflict)
2637 (error "Please resolve the conflicts before deleting %s" file)))
2638 (unless (y-or-n-p (format "Really want to delete %s? "
2639 (file-name-nondirectory file)))
2640 (error "Abort!"))
2641 (unless (or (file-directory-p file) (null make-backup-files)
2642 (not (file-exists-p file)))
2643 (with-current-buffer (or buf (find-file-noselect file))
2644 (let ((backup-inhibited nil))
2645 (backup-buffer))))
2646 ;; Bind `default-directory' so that the command that the backend
2647 ;; runs to remove the file is invoked in the correct context.
2648 (let ((default-directory (file-name-directory file)))
2649 (vc-call-backend backend 'delete-file file))
2650 ;; If the backend hasn't deleted the file itself, let's do it for him.
2651 (when (file-exists-p file) (delete-file file))
2652 ;; Forget what VC knew about the file.
2653 (vc-file-clearprops file)
2654 ;; Make sure the buffer is deleted and the *vc-dir* buffers are
2655 ;; updated after this.
2656 (vc-resynch-buffer file nil t)))
2658 ;;;###autoload
2659 (defun vc-rename-file (old new)
2660 "Rename file OLD to NEW in both work area and repository.
2661 If called interactively, read OLD and NEW, defaulting OLD to the
2662 current buffer's file name if it's under version control."
2663 (interactive (list (read-file-name "VC rename file: " nil
2664 (when (vc-backend buffer-file-name)
2665 buffer-file-name) t)
2666 (read-file-name "Rename to: ")))
2667 ;; in CL I would have said (setq new (merge-pathnames new old))
2668 (let ((old-base (file-name-nondirectory old)))
2669 (when (and (not (string= "" old-base))
2670 (string= "" (file-name-nondirectory new)))
2671 (setq new (concat new old-base))))
2672 (let ((oldbuf (get-file-buffer old)))
2673 (when (and oldbuf (buffer-modified-p oldbuf))
2674 (error "Please save files before moving them"))
2675 (when (get-file-buffer new)
2676 (error "Already editing new file name"))
2677 (when (file-exists-p new)
2678 (error "New file already exists"))
2679 (let ((state (vc-state old)))
2680 (unless (memq state '(up-to-date edited))
2681 (error "Please %s files before moving them"
2682 (if (stringp state) "check in" "update"))))
2683 (vc-call rename-file old new)
2684 (vc-file-clearprops old)
2685 ;; Move the actual file (unless the backend did it already)
2686 (when (file-exists-p old) (rename-file old new))
2687 ;; ?? Renaming a file might change its contents due to keyword expansion.
2688 ;; We should really check out a new copy if the old copy was precisely equal
2689 ;; to some checked-in revision. However, testing for this is tricky....
2690 (when oldbuf
2691 (with-current-buffer oldbuf
2692 (let ((buffer-read-only buffer-read-only))
2693 (set-visited-file-name new))
2694 (vc-mode-line new (vc-backend new))
2695 (set-buffer-modified-p nil)))))
2697 ;;;###autoload
2698 (defun vc-update-change-log (&rest args)
2699 "Find change log file and add entries from recent version control logs.
2700 Normally, find log entries for all registered files in the default
2701 directory.
2703 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2705 With any numeric prefix arg, find log entries for all currently visited
2706 files that are under version control. This puts all the entries in the
2707 log for the default directory, which may not be appropriate.
2709 From a program, any ARGS are assumed to be filenames for which
2710 log entries should be gathered."
2711 (interactive
2712 (cond ((consp current-prefix-arg) ;C-u
2713 (list buffer-file-name))
2714 (current-prefix-arg ;Numeric argument.
2715 (let ((files nil))
2716 (dolist (buffer (buffer-list))
2717 (let ((file (buffer-file-name buffer)))
2718 (and file (vc-backend file)
2719 (setq files (cons file files)))))
2720 files))
2722 ;; Don't supply any filenames to backend; this means
2723 ;; it should find all relevant files relative to
2724 ;; the default-directory.
2725 nil)))
2726 (vc-call-backend (vc-responsible-backend default-directory)
2727 'update-changelog args))
2729 ;; functions that operate on RCS revision numbers. This code should
2730 ;; also be moved into the backends. It stays for now, however, since
2731 ;; it is used in code below.
2732 (defun vc-branch-p (rev)
2733 "Return t if REV is a branch revision."
2734 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
2736 ;;;###autoload
2737 (defun vc-branch-part (rev)
2738 "Return the branch part of a revision number REV."
2739 (let ((index (string-match "\\.[0-9]+\\'" rev)))
2740 (when index
2741 (substring rev 0 index))))
2743 (defun vc-default-responsible-p (_backend _file)
2744 "Indicate whether BACKEND is responsible for FILE.
2745 The default is to return nil always."
2746 nil)
2748 (defun vc-default-find-revision (backend file rev buffer)
2749 "Provide the new `find-revision' op based on the old `checkout' op.
2750 This is only for compatibility with old backends. They should be updated
2751 to provide the `find-revision' operation instead."
2752 (let ((tmpfile (make-temp-file (expand-file-name file))))
2753 (unwind-protect
2754 (progn
2755 (vc-call-backend backend 'checkout file nil rev tmpfile)
2756 (with-current-buffer buffer
2757 (insert-file-contents-literally tmpfile)))
2758 (delete-file tmpfile))))
2760 (defun vc-default-rename-file (_backend old new)
2761 (condition-case nil
2762 (add-name-to-file old new)
2763 (error (rename-file old new)))
2764 (vc-delete-file old)
2765 (with-current-buffer (find-file-noselect new)
2766 (vc-register)))
2768 (defalias 'vc-default-check-headers 'ignore)
2770 (declare-function log-edit-mode "log-edit" ())
2772 (defun vc-default-log-edit-mode (_backend) (log-edit-mode))
2774 (defun vc-default-log-view-mode (_backend) (log-view-mode))
2776 (defun vc-default-show-log-entry (_backend rev)
2777 (with-no-warnings
2778 (log-view-goto-rev rev)))
2780 (defun vc-default-comment-history (backend file)
2781 "Return a string with all log entries stored in BACKEND for FILE."
2782 (when (vc-find-backend-function backend 'print-log)
2783 (with-current-buffer "*vc*"
2784 (vc-call-backend backend 'print-log (list file))
2785 (buffer-string))))
2787 (defun vc-default-receive-file (backend file rev)
2788 "Let BACKEND receive FILE from another version control system."
2789 (vc-call-backend backend 'register (list file) rev ""))
2791 (defun vc-default-retrieve-tag (backend dir name update)
2792 (if (string= name "")
2793 (progn
2794 (vc-file-tree-walk
2796 (lambda (f) (and
2797 (vc-up-to-date-p f)
2798 (vc-error-occurred
2799 (vc-call-backend backend 'checkout f nil "")
2800 (when update (vc-resynch-buffer f t t)))))))
2801 (let ((result (vc-tag-precondition dir)))
2802 (if (stringp result)
2803 (error "File %s is locked" result)
2804 (setq update (and (eq result 'visited) update))
2805 (vc-file-tree-walk
2807 (lambda (f) (vc-error-occurred
2808 (vc-call-backend backend 'checkout f nil name)
2809 (when update (vc-resynch-buffer f t t)))))))))
2811 (defun vc-default-revert (backend file contents-done)
2812 (unless contents-done
2813 (let ((rev (vc-working-revision file))
2814 (file-buffer (or (get-file-buffer file) (current-buffer))))
2815 (message "Checking out %s..." file)
2816 (let ((failed t)
2817 (backup-name (car (find-backup-file-name file))))
2818 (when backup-name
2819 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
2820 (unless (file-writable-p file)
2821 (set-file-modes file (logior (file-modes file) 128))))
2822 (unwind-protect
2823 (let ((coding-system-for-read 'no-conversion)
2824 (coding-system-for-write 'no-conversion))
2825 (with-temp-file file
2826 (let ((outbuf (current-buffer)))
2827 ;; Change buffer to get local value of vc-checkout-switches.
2828 (with-current-buffer file-buffer
2829 (let ((default-directory (file-name-directory file)))
2830 (vc-call-backend backend 'find-revision
2831 file rev outbuf)))))
2832 (setq failed nil))
2833 (when backup-name
2834 (if failed
2835 (rename-file backup-name file 'ok-if-already-exists)
2836 (and (not vc-make-backup-files) (delete-file backup-name))))))
2837 (message "Checking out %s...done" file))))
2839 (defalias 'vc-default-revision-completion-table 'ignore)
2840 (defalias 'vc-default-mark-resolved 'ignore)
2842 (defun vc-default-dir-status-files (_backend _dir files update-function)
2843 (funcall update-function
2844 (mapcar (lambda (file) (list file 'up-to-date)) files)))
2846 (defun vc-check-headers ()
2847 "Check if the current file has any headers in it."
2848 (interactive)
2849 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
2853 ;; These things should probably be generally available
2854 (define-obsolete-function-alias 'vc-string-prefix-p 'string-prefix-p "24.3")
2856 (defun vc-file-tree-walk (dirname func &rest args)
2857 "Walk recursively through DIRNAME.
2858 Invoke FUNC f ARGS on each VC-managed file f underneath it."
2859 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
2860 (message "Traversing directory %s...done" dirname))
2862 (defun vc-file-tree-walk-internal (file func args)
2863 (if (not (file-directory-p file))
2864 (when (vc-backend file) (apply func file args))
2865 (message "Traversing directory %s..." (abbreviate-file-name file))
2866 (let ((dir (file-name-as-directory file)))
2867 (mapcar
2868 (lambda (f) (or
2869 (string-equal f ".")
2870 (string-equal f "..")
2871 (member f vc-directory-exclusion-list)
2872 (let ((dirf (expand-file-name f dir)))
2874 (file-symlink-p dirf) ;; Avoid possible loops.
2875 (vc-file-tree-walk-internal dirf func args)))))
2876 (directory-files dir)))))
2878 (provide 'vc)
2880 ;;; vc.el ends here