Nuke arch-tags.
[emacs.git] / lisp / vc / vc.el
blob2f26c1eb50b16f85b4d650c8a665a7baa15fc966
1 ;;; vc.el --- drive a version-control system from within Emacs
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see below for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Keywords: vc tools
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Credits:
28 ;; VC was initially designed and implemented by Eric S. Raymond
29 ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
30 ;; contributed substantial amounts of work to VC. These include:
32 ;; Per Cederqvist <ceder@lysator.liu.se>
33 ;; Paul Eggert <eggert@twinsun.com>
34 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
35 ;; Martin Lorentzson <martinl@gnu.org>
36 ;; Dave Love <fx@gnu.org>
37 ;; Stefan Monnier <monnier@cs.yale.edu>
38 ;; Thien-Thi Nguyen <ttn@gnu.org>
39 ;; Dan Nicolaescu <dann@ics.uci.edu>
40 ;; J.D. Smith <jdsmith@alum.mit.edu>
41 ;; Andre Spiegel <spiegel@gnu.org>
42 ;; Richard Stallman <rms@gnu.org>
44 ;; In July 2007 ESR returned and redesigned the mode to cope better
45 ;; with modern version-control systems that do commits by fileset
46 ;; rather than per individual file.
48 ;; If you maintain a client of the mode or customize it in your .emacs,
49 ;; note that some backend functions which formerly took single file arguments
50 ;; now take a list of files. These include: register, checkin, print-log,
51 ;; rollback, and diff.
53 ;;; Commentary:
55 ;; This mode is fully documented in the Emacs user's manual.
57 ;; Supported version-control systems presently include CVS, RCS, GNU
58 ;; Arch, Subversion, Bzr, Git, Mercurial, Monotone and SCCS
59 ;; (or its free replacement, CSSC).
61 ;; If your site uses the ChangeLog convention supported by Emacs, the
62 ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
63 ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
64 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
66 ;; When using SCCS, RCS, CVS: be careful not to do repo surgery, or
67 ;; operations like registrations and deletions and renames, outside VC
68 ;; while VC is running. The support for these systems was designed
69 ;; when disks were much slower, and the code maintains a lot of
70 ;; internal state in order to reduce expensive operations to a
71 ;; minimum. Thus, if you mess with the repo while VC's back is turned,
72 ;; VC may get seriously confused.
74 ;; When using Subversion or a later system, anything you do outside VC
75 ;; *through the VCS tools* should safely interlock with VC
76 ;; operations. Under these VC does little state caching, because local
77 ;; operations are assumed to be fast. The dividing line is
79 ;; ADDING SUPPORT FOR OTHER BACKENDS
81 ;; VC can use arbitrary version control systems as a backend. To add
82 ;; support for a new backend named SYS, write a library vc-sys.el that
83 ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
84 ;; case for the function and library names). VC will use that library if
85 ;; you put the symbol SYS somewhere into the list of
86 ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
87 ;; returns non-nil for a file, all SYS-specific versions of VC commands
88 ;; will be available for that file.
90 ;; VC keeps some per-file information in the form of properties (see
91 ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
92 ;; do not generally need to be aware of these properties. For example,
93 ;; `vc-sys-working-revision' should compute the working revision and
94 ;; return it; it should not look it up in the property, and it needn't
95 ;; store it there either. However, if a backend-specific function does
96 ;; store a value in a property, that value takes precedence over any
97 ;; value that the generic code might want to set (check for uses of
98 ;; the macro `with-vc-properties' in vc.el).
100 ;; In the list of functions below, each identifier needs to be prepended
101 ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
102 ;; `*'), others are optional (`-').
104 ;; BACKEND PROPERTIES
106 ;; * revision-granularity
108 ;; Takes no arguments. Returns either 'file or 'repository. Backends
109 ;; that return 'file have per-file revision numbering; backends
110 ;; that return 'repository have per-repository revision numbering,
111 ;; so a revision level implicitly identifies a changeset
113 ;; STATE-QUERYING FUNCTIONS
115 ;; * registered (file)
117 ;; Return non-nil if FILE is registered in this backend. Both this
118 ;; function as well as `state' should be careful to fail gracefully
119 ;; in the event that the backend executable is absent. It is
120 ;; preferable that this function's body is autoloaded, that way only
121 ;; calling vc-registered does not cause the backend to be loaded
122 ;; (all the vc-FOO-registered functions are called to try to find
123 ;; the controlling backend for FILE.
125 ;; * state (file)
127 ;; Return the current version control state of FILE. For a list of
128 ;; possible values, see `vc-state'. This function should do a full and
129 ;; reliable state computation; it is usually called immediately after
130 ;; C-x v v. If you want to use a faster heuristic when visiting a
131 ;; file, put that into `state-heuristic' below. Note that under most
132 ;; VCSes this won't be called at all, dir-status is used instead.
134 ;; - state-heuristic (file)
136 ;; If provided, this function is used to estimate the version control
137 ;; state of FILE at visiting time. It should be considerably faster
138 ;; than the implementation of `state'. For a list of possible values,
139 ;; see the doc string of `vc-state'.
141 ;; - dir-status (dir update-function)
143 ;; Produce RESULT: a list of lists of the form (FILE VC-STATE EXTRA)
144 ;; for the files in DIR.
145 ;; EXTRA can be used for backend specific information about FILE.
146 ;; If a command needs to be run to compute this list, it should be
147 ;; run asynchronously using (current-buffer) as the buffer for the
148 ;; command. When RESULT is computed, it should be passed back by
149 ;; doing: (funcall UPDATE-FUNCTION RESULT nil).
150 ;; If the backend uses a process filter, hence it produces partial results,
151 ;; they can be passed back by doing:
152 ;; (funcall UPDATE-FUNCTION RESULT t)
153 ;; and then do a (funcall UPDATE-FUNCTION RESULT nil)
154 ;; when all the results 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', `extra-dir-menu' and `dir-status-files'.
159 ;; - dir-status-files (dir files default-state update-function)
161 ;; This function is identical to dir-status except that it should
162 ;; only report status for the specified FILES. Also it needs to
163 ;; report on all requested files, including up-to-date or ignored
164 ;; files. If not provided, the default is to consider that the files
165 ;; are in DEFAULT-STATE.
167 ;; - dir-extra-headers (dir)
169 ;; Return a string that will be added to the *vc-dir* buffer header.
171 ;; - dir-printer (fileinfo)
173 ;; Pretty print the `vc-dir-fileinfo' FILEINFO.
174 ;; If a backend needs to show more information than the default FILE
175 ;; and STATE in the vc-dir listing, it can store that extra
176 ;; information in `vc-dir-fileinfo->extra'. This function can be
177 ;; used to display that extra information in the *vc-dir* buffer.
179 ;; - status-fileinfo-extra (file)
181 ;; Compute `vc-dir-fileinfo->extra' for FILE.
183 ;; * working-revision (file)
185 ;; Return the working revision of FILE. This is the revision fetched
186 ;; by the last checkout or upate, not necessarily the same thing as the
187 ;; head or tip revision. Should return "0" for a file added but not yet
188 ;; committed.
190 ;; - latest-on-branch-p (file)
192 ;; Return non-nil if the working revision of FILE is the latest revision
193 ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
194 ;; The default implementation always returns t, which means that
195 ;; working with non-current revisions is not supported by default.
197 ;; * checkout-model (files)
199 ;; Indicate whether FILES need to be "checked out" before they can be
200 ;; edited. See `vc-checkout-model' for a list of possible values.
202 ;; - workfile-unchanged-p (file)
204 ;; Return non-nil if FILE is unchanged from the working revision.
205 ;; This function should do a brief comparison of FILE's contents
206 ;; with those of the repository copy of the working revision. If
207 ;; the backend does not have such a brief-comparison feature, the
208 ;; default implementation of this function can be used, which
209 ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
210 ;; must not run asynchronously in this case, see variable
211 ;; `vc-disable-async-diff'.)
213 ;; - mode-line-string (file)
215 ;; If provided, this function should return the VC-specific mode
216 ;; line string for FILE. The returned string should have a
217 ;; `help-echo' property which is the text to be displayed as a
218 ;; tooltip when the mouse hovers over the VC entry on the mode-line.
219 ;; The default implementation deals well with all states that
220 ;; `vc-state' can return.
222 ;; STATE-CHANGING FUNCTIONS
224 ;; * create-repo (backend)
226 ;; Create an empty repository in the current directory and initialize
227 ;; it so VC mode can add files to it. For file-oriented systems, this
228 ;; need do no more than create a subdirectory with the right name.
230 ;; * register (files &optional rev comment)
232 ;; Register FILES in this backend. Optionally, an initial revision REV
233 ;; and an initial description of the file, COMMENT, may be specified,
234 ;; but it is not guaranteed that the backend will do anything with this.
235 ;; The implementation should pass the value of vc-register-switches
236 ;; to the backend command. (Note: in older versions of VC, this
237 ;; command took a single file argument and not a list.)
239 ;; - init-revision (file)
241 ;; The initial revision to use when registering FILE if one is not
242 ;; specified by the user. If not provided, the variable
243 ;; vc-default-init-revision is used instead.
245 ;; - responsible-p (file)
247 ;; Return non-nil if this backend considers itself "responsible" for
248 ;; FILE, which can also be a directory. This function is used to find
249 ;; out what backend to use for registration of new files and for things
250 ;; like change log generation. The default implementation always
251 ;; returns nil.
253 ;; - could-register (file)
255 ;; Return non-nil if FILE could be registered under this backend. The
256 ;; default implementation always returns t.
258 ;; - receive-file (file rev)
260 ;; Let this backend "receive" a file that is already registered under
261 ;; another backend. The default implementation simply calls `register'
262 ;; for FILE, but it can be overridden to do something more specific,
263 ;; e.g. keep revision numbers consistent or choose editing modes for
264 ;; FILE that resemble those of the other backend.
266 ;; - unregister (file)
268 ;; Unregister FILE from this backend. This is only needed if this
269 ;; backend may be used as a "more local" backend for temporary editing.
271 ;; * checkin (files rev comment)
273 ;; Commit changes in FILES to this backend. REV is a historical artifact
274 ;; and should be ignored. COMMENT is used as a check-in comment.
275 ;; The implementation should pass the value of vc-checkin-switches to
276 ;; the backend command.
278 ;; * find-revision (file rev buffer)
280 ;; Fetch revision REV of file FILE and put it into BUFFER.
281 ;; If REV is the empty string, fetch the head of the trunk.
282 ;; The implementation should pass the value of vc-checkout-switches
283 ;; to the backend command.
285 ;; * checkout (file &optional editable rev)
287 ;; Check out revision REV of FILE into the working area. If EDITABLE
288 ;; is non-nil, FILE should be writable by the user and if locking is
289 ;; used for FILE, a lock should also be set. If REV is non-nil, that
290 ;; is the revision to check out (default is the working revision).
291 ;; If REV is t, that means to check out the head of the current branch;
292 ;; if it is the empty string, check out the head of the trunk.
293 ;; The implementation should pass the value of vc-checkout-switches
294 ;; to the backend command.
296 ;; * revert (file &optional contents-done)
298 ;; Revert FILE back to the working revision. If optional
299 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
300 ;; already been reverted from a version backup, and this function
301 ;; only needs to update the status of FILE within the backend.
302 ;; If FILE is in the `added' state it should be returned to the
303 ;; `unregistered' state.
305 ;; - rollback (files)
307 ;; Remove the tip revision of each of FILES from the repository. If
308 ;; this function is not provided, trying to cancel a revision is
309 ;; caught as an error. (Most backends don't provide it.) (Also
310 ;; note that older versions of this backend command were called
311 ;; 'cancel-version' and took a single file arg, not a list of
312 ;; files.)
314 ;; - merge (file rev1 rev2)
316 ;; Merge the changes between REV1 and REV2 into the current working file
317 ;; (for non-distributed VCS).
319 ;; - merge-branch ()
321 ;; Merge another branch into the current one, prompting for a
322 ;; location to merge from.
324 ;; - merge-news (file)
326 ;; Merge recent changes from the current branch into FILE.
327 ;; (for non-distributed VCS).
329 ;; - pull (prompt)
331 ;; Pull "upstream" changes into the current branch (for distributed
332 ;; VCS). If PROMPT is non-nil, or if necessary, prompt for a
333 ;; location to pull from.
335 ;; - steal-lock (file &optional revision)
337 ;; Steal any lock on the working revision of FILE, or on REVISION if
338 ;; that is provided. This function is only needed if locking is
339 ;; used for files under this backend, and if files can indeed be
340 ;; locked by other users.
342 ;; - modify-change-comment (files rev comment)
344 ;; Modify the change comments associated with the files at the
345 ;; given revision. This is optional, many backends do not support it.
347 ;; - mark-resolved (files)
349 ;; Mark conflicts as resolved. Some VC systems need to run a
350 ;; command to mark conflicts as resolved.
352 ;; HISTORY FUNCTIONS
354 ;; * print-log (files buffer &optional shortlog start-revision limit)
356 ;; Insert the revision log for FILES into BUFFER.
357 ;; If SHORTLOG is true insert a short version of the log.
358 ;; If LIMIT is true insert only insert LIMIT log entries. If the
359 ;; backend does not support limiting the number of entries to show
360 ;; it should return `limit-unsupported'.
361 ;; If START-REVISION is given, then show the log starting from the
362 ;; revision. At this point START-REVISION is only required to work
363 ;; in conjunction with LIMIT = 1.
365 ;; * log-outgoing (backend remote-location)
367 ;; Insert in BUFFER the revision log for the changes that will be
368 ;; sent when performing a push operation to REMOTE-LOCATION.
370 ;; * log-incoming (backend remote-location)
372 ;; Insert in BUFFER the revision log for the changes that will be
373 ;; received when performing a pull operation from REMOTE-LOCATION.
375 ;; - log-view-mode ()
377 ;; Mode to use for the output of print-log. This defaults to
378 ;; `log-view-mode' and is expected to be changed (if at all) to a derived
379 ;; mode of `log-view-mode'.
381 ;; - show-log-entry (revision)
383 ;; If provided, search the log entry for REVISION in the current buffer,
384 ;; and make sure it is displayed in the buffer's window. The default
385 ;; implementation of this function works for RCS-style logs.
387 ;; - comment-history (file)
389 ;; Return a string containing all log entries that were made for FILE.
390 ;; This is used for transferring a file from one backend to another,
391 ;; retaining comment information.
393 ;; - update-changelog (files)
395 ;; Using recent log entries, create ChangeLog entries for FILES, or for
396 ;; all files at or below the default-directory if FILES is nil. The
397 ;; default implementation runs rcs2log, which handles RCS- and
398 ;; CVS-style logs.
400 ;; * diff (files &optional rev1 rev2 buffer)
402 ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
403 ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
404 ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
405 ;; found in the repository) as the older revision; if REV2 is nil,
406 ;; use the current working-copy contents as the newer revision. This
407 ;; function should pass the value of (vc-switches BACKEND 'diff) to
408 ;; the backend command. It should return a status of either 0 (no
409 ;; differences found), or 1 (either non-empty diff or the diff is
410 ;; run asynchronously).
412 ;; - revision-completion-table (files)
414 ;; Return a completion table for existing revisions of FILES.
415 ;; The default is to not use any completion table.
417 ;; - annotate-command (file buf &optional rev)
419 ;; If this function is provided, it should produce an annotated display
420 ;; of FILE in BUF, relative to revision REV. Annotation means each line
421 ;; of FILE displayed is prefixed with version information associated with
422 ;; its addition (deleted lines leave no history) and that the text of the
423 ;; file is fontified according to age.
425 ;; - annotate-time ()
427 ;; Only required if `annotate-command' is defined for the backend.
428 ;; Return the time of the next line of annotation at or after point,
429 ;; as a floating point fractional number of days. The helper
430 ;; function `vc-annotate-convert-time' may be useful for converting
431 ;; multi-part times as returned by `current-time' and `encode-time'
432 ;; to this format. Return nil if no more lines of annotation appear
433 ;; in the buffer. You can safely assume that point is placed at the
434 ;; beginning of each line, starting at `point-min'. The buffer that
435 ;; point is placed in is the Annotate output, as defined by the
436 ;; relevant backend. This function also affects how much of the line
437 ;; is fontified; where it leaves point is where fontification begins.
439 ;; - annotate-current-time ()
441 ;; Only required if `annotate-command' is defined for the backend,
442 ;; AND you'd like the current time considered to be anything besides
443 ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
444 ;; time with hours, minutes, and seconds included. Probably safe to
445 ;; ignore. Return the current-time, in units of fractional days.
447 ;; - annotate-extract-revision-at-line ()
449 ;; Only required if `annotate-command' is defined for the backend.
450 ;; Invoked from a buffer in vc-annotate-mode, return the revision
451 ;; corresponding to the current line, or nil if there is no revision
452 ;; corresponding to the current line.
453 ;; If the backend supports annotating through copies and renames,
454 ;; and displays a file name and a revision, then return a cons
455 ;; (REVISION . FILENAME).
457 ;; TAG SYSTEM
459 ;; - create-tag (dir name branchp)
461 ;; Attach the tag NAME to the state of the working copy. This
462 ;; should make sure that files are up-to-date before proceeding with
463 ;; the action. DIR can also be a file and if BRANCHP is specified,
464 ;; NAME should be created as a branch and DIR should be checked out
465 ;; under this new branch. The default implementation does not
466 ;; support branches but does a sanity check, a tree traversal and
467 ;; assigns the tag to each file.
469 ;; - retrieve-tag (dir name update)
471 ;; Retrieve the version tagged by NAME of all registered files at or below DIR.
472 ;; If UPDATE is non-nil, then update buffers of any files in the
473 ;; tag that are currently visited. The default implementation
474 ;; does a sanity check whether there aren't any uncommitted changes at
475 ;; or below DIR, and then performs a tree walk, using the `checkout'
476 ;; function to retrieve the corresponding revisions.
478 ;; MISCELLANEOUS
480 ;; - make-version-backups-p (file)
482 ;; Return non-nil if unmodified repository revisions of FILE should be
483 ;; backed up locally. If this is done, VC can perform `diff' and
484 ;; `revert' operations itself, without calling the backend system. The
485 ;; default implementation always returns nil.
487 ;; - root (file)
488 ;; Return the root of the VC controlled hierarchy for file.
490 ;; - repository-hostname (dirname)
492 ;; Return the hostname that the backend will have to contact
493 ;; in order to operate on a file in DIRNAME. If the return value
494 ;; is nil, it means that the repository is local.
495 ;; This function is used in `vc-stay-local-p' which backends can use
496 ;; for their convenience.
498 ;; - previous-revision (file rev)
500 ;; Return the revision number that precedes REV for FILE, or nil if no such
501 ;; revision exists.
503 ;; - next-revision (file rev)
505 ;; Return the revision number that follows REV for FILE, or nil if no such
506 ;; revision exists.
508 ;; - log-edit-mode ()
510 ;; Turn on the mode used for editing the check in log. This
511 ;; defaults to `log-edit-mode'. If changed, it should use a mode
512 ;; derived from`log-edit-mode'.
514 ;; - check-headers ()
516 ;; Return non-nil if the current buffer contains any version headers.
518 ;; - clear-headers ()
520 ;; In the current buffer, reset all version headers to their unexpanded
521 ;; form. This function should be provided if the state-querying code
522 ;; for this backend uses the version headers to determine the state of
523 ;; a file. This function will then be called whenever VC changes the
524 ;; version control state in such a way that the headers would give
525 ;; wrong information.
527 ;; - delete-file (file)
529 ;; Delete FILE and mark it as deleted in the repository. If this
530 ;; function is not provided, the command `vc-delete-file' will
531 ;; signal an error.
533 ;; - rename-file (old new)
535 ;; Rename file OLD to NEW, both in the working area and in the
536 ;; repository. If this function is not provided, the renaming
537 ;; will be done by (vc-delete-file old) and (vc-register new).
539 ;; - find-file-hook ()
541 ;; Operation called in current buffer when opening a file. This can
542 ;; be used by the backend to setup some local variables it might need.
544 ;; - extra-menu ()
546 ;; Return a menu keymap, the items in the keymap will appear at the
547 ;; end of the Version Control menu. The goal is to allow backends
548 ;; to specify extra menu items that appear in the VC menu. This way
549 ;; you can provide menu entries for functionality that is specific
550 ;; to your backend and which does not map to any of the VC generic
551 ;; concepts.
553 ;; - extra-dir-menu ()
555 ;; Return a menu keymap, the items in the keymap will appear at the
556 ;; end of the VC Status menu. The goal is to allow backends to
557 ;; specify extra menu items that appear in the VC Status menu. This
558 ;; makes it possible to provide menu entries for functionality that
559 ;; is specific to a backend and which does not map to any of the VC
560 ;; generic concepts.
562 ;; - conflicted-files (dir)
564 ;; Return the list of files where conflict resolution is needed in
565 ;; the project that contains DIR.
566 ;; FIXME: what should it do with non-text conflicts?
568 ;;; Todo:
570 ;; - Get rid of the "master file" terminology.
572 ;; - Add key-binding for vc-delete-file.
574 ;;;; New Primitives:
576 ;; - deal with push/pull operations.
578 ;; - add a mechanism for editing the underlying VCS's list of files
579 ;; to be ignored, when that's possible.
581 ;;;; Primitives that need changing:
583 ;; - vc-update/vc-merge should deal with VC systems that don't
584 ;; update/merge on a file basis, but on a whole repository basis.
585 ;; vc-update and vc-merge assume the arguments are always files,
586 ;; they don't deal with directories. Make sure the *vc-dir* buffer
587 ;; is updated after these operations.
588 ;; At least bzr, git and hg should benefit from this.
590 ;;;; Improved branch and tag handling:
592 ;; - add a generic mechanism for remembering the current branch names,
593 ;; display the branch name in the mode-line. Replace
594 ;; vc-cvs-sticky-tag with that.
596 ;;;; Internal cleanups:
598 ;; - backends that care about vc-stay-local should try to take it into
599 ;; account for vc-dir. Is this likely to be useful??? YES!
601 ;; - vc-expand-dirs should take a backend parameter and only look for
602 ;; files managed by that backend.
604 ;; - Another important thing: merge all the status-like backend operations.
605 ;; We should remove dir-status, state, and dir-status-files, and
606 ;; replace them with just `status' which takes a fileset and a continuation
607 ;; (like dir-status) and returns a buffer in which the process(es) are run
608 ;; (or nil if it worked synchronously). Hopefully we can define the old
609 ;; 4 operations in term of this one.
611 ;;;; Other
613 ;; - when a file is in `conflict' state, turn on smerge-mode.
615 ;; - figure out what to do with conflicts that are not caused by the
616 ;; file contents, but by metadata or other causes. Example: File A
617 ;; gets renamed to B in one branch and to C in another and you merge
618 ;; the two branches. Or you locally add file FOO and then pull a
619 ;; change that also adds a new file FOO, ...
621 ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
622 ;; buffer, if one is present, instead of adding to the ChangeLog.
624 ;; - When vc-next-action calls vc-checkin it could pre-fill the
625 ;; *VC-log* buffer with some obvious items: the list of files that
626 ;; were added, the list of files that were removed. If the diff is
627 ;; available, maybe it could even call something like
628 ;; `diff-add-change-log-entries-other-window' to create a detailed
629 ;; skeleton for the log...
631 ;; - most vc-dir backends need more work. They might need to
632 ;; provide custom headers, use the `extra' field and deal with all
633 ;; possible VC states.
635 ;; - add a function that calls vc-dir to `find-directory-functions'.
637 ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
638 ;; files. Now that unregistered and ignored files are shown in
639 ;; vc-dir, it is possible that these commands are called
640 ;; for unregistered/ignored files.
642 ;; - vc-next-action needs work in order to work with multiple
643 ;; backends: `vc-state' returns the state for the default backend,
644 ;; not for the backend in the current *vc-dir* buffer.
646 ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
647 ;; it should work for other async commands done through vc-do-command
648 ;; as well,
650 ;; - vc-dir toolbar needs more icons.
652 ;; - The backends should avoid using `vc-file-setprop' and `vc-file-getprop'.
654 ;;; Code:
656 (require 'vc-hooks)
657 (require 'vc-dispatcher)
659 (eval-when-compile
660 (require 'cl)
661 (require 'dired))
663 (unless (assoc 'vc-parent-buffer minor-mode-alist)
664 (setq minor-mode-alist
665 (cons '(vc-parent-buffer vc-parent-buffer-name)
666 minor-mode-alist)))
668 ;; General customization
670 (defgroup vc nil
671 "Version-control system in Emacs."
672 :group 'tools)
674 (defcustom vc-initial-comment nil
675 "If non-nil, prompt for initial comment when a file is registered."
676 :type 'boolean
677 :group 'vc)
679 (defcustom vc-default-init-revision "1.1"
680 "A string used as the default revision number when a new file is registered.
681 This can be overridden by giving a prefix argument to \\[vc-register]. This
682 can also be overridden by a particular VC backend."
683 :type 'string
684 :group 'vc
685 :version "20.3")
687 (defcustom vc-checkin-switches nil
688 "A string or list of strings specifying extra switches for checkin.
689 These are passed to the checkin program by \\[vc-checkin]."
690 :type '(choice (const :tag "None" nil)
691 (string :tag "Argument String")
692 (repeat :tag "Argument List"
693 :value ("")
694 string))
695 :group 'vc)
697 (defcustom vc-checkout-switches nil
698 "A string or list of strings specifying extra switches for checkout.
699 These are passed to the checkout program by \\[vc-checkout]."
700 :type '(choice (const :tag "None" nil)
701 (string :tag "Argument String")
702 (repeat :tag "Argument List"
703 :value ("")
704 string))
705 :group 'vc)
707 (defcustom vc-register-switches nil
708 "A string or list of strings; extra switches for registering a file.
709 These are passed to the checkin program by \\[vc-register]."
710 :type '(choice (const :tag "None" nil)
711 (string :tag "Argument String")
712 (repeat :tag "Argument List"
713 :value ("")
714 string))
715 :group 'vc)
717 (defcustom vc-diff-switches nil
718 "A string or list of strings specifying switches for diff under VC.
719 When running diff under a given BACKEND, VC uses the first
720 non-nil value of `vc-BACKEND-diff-switches', `vc-diff-switches',
721 and `diff-switches', in that order. Since nil means to check the
722 next variable in the sequence, either of the first two may use
723 the value t to mean no switches at all. `vc-diff-switches'
724 should contain switches that are specific to version control, but
725 not specific to any particular backend."
726 :type '(choice (const :tag "Unspecified" nil)
727 (const :tag "None" t)
728 (string :tag "Argument String")
729 (repeat :tag "Argument List" :value ("") string))
730 :group 'vc
731 :version "21.1")
733 (defcustom vc-diff-knows-L nil
734 "Indicates whether diff understands the -L option.
735 The value is either `yes', `no', or nil. If it is nil, VC tries
736 to use -L and sets this variable to remember whether it worked."
737 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
738 :group 'vc)
740 (defcustom vc-log-show-limit 2000
741 "Limit the number of items shown by the VC log commands.
742 Zero means unlimited.
743 Not all VC backends are able to support this feature."
744 :type 'integer
745 :group 'vc)
747 (defcustom vc-allow-async-revert nil
748 "Specifies whether the diff during \\[vc-revert] may be asynchronous.
749 Enabling this option means that you can confirm a revert operation even
750 if the local changes in the file have not been found and displayed yet."
751 :type '(choice (const :tag "No" nil)
752 (const :tag "Yes" t))
753 :group 'vc
754 :version "22.1")
756 ;;;###autoload
757 (defcustom vc-checkout-hook nil
758 "Normal hook (list of functions) run after checking out a file.
759 See `run-hooks'."
760 :type 'hook
761 :group 'vc
762 :version "21.1")
764 ;;;###autoload
765 (defcustom vc-checkin-hook nil
766 "Normal hook (list of functions) run after commit or file checkin.
767 See also `log-edit-done-hook'."
768 :type 'hook
769 :options '(log-edit-comment-to-change-log)
770 :group 'vc)
772 ;;;###autoload
773 (defcustom vc-before-checkin-hook nil
774 "Normal hook (list of functions) run before a commit or a file checkin.
775 See `run-hooks'."
776 :type 'hook
777 :group 'vc)
779 ;; Header-insertion hair
781 (defcustom vc-static-header-alist
782 '(("\\.c\\'" .
783 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
784 "Associate static header string templates with file types.
785 A \%s in the template is replaced with the first string associated with
786 the file's version control type in `vc-BACKEND-header'."
787 :type '(repeat (cons :format "%v"
788 (regexp :tag "File Type")
789 (string :tag "Header String")))
790 :group 'vc)
792 (defcustom vc-comment-alist
793 '((nroff-mode ".\\\"" ""))
794 "Special comment delimiters for generating VC headers.
795 Add an entry in this list if you need to override the normal `comment-start'
796 and `comment-end' variables. This will only be necessary if the mode language
797 is sensitive to blank lines."
798 :type '(repeat (list :format "%v"
799 (symbol :tag "Mode")
800 (string :tag "Comment Start")
801 (string :tag "Comment End")))
802 :group 'vc)
804 (defcustom vc-checkout-carefully (= (user-uid) 0)
805 "Non-nil means be extra-careful in checkout.
806 Verify that the file really is not locked
807 and that its contents match what the repository version says."
808 :type 'boolean
809 :group 'vc)
810 (make-obsolete-variable 'vc-checkout-carefully
811 "the corresponding checks are always done now."
812 "21.1")
815 ;; Variables users don't need to see
817 (defvar vc-disable-async-diff nil
818 "VC sets this to t locally to disable some async diff operations.
819 Backends that offer asynchronous diffs should respect this variable
820 in their implementation of vc-BACKEND-diff.")
822 ;; File property caching
824 (defun vc-clear-context ()
825 "Clear all cached file properties."
826 (interactive)
827 (fillarray vc-file-prop-obarray 0))
829 (defmacro with-vc-properties (files form settings)
830 "Execute FORM, then maybe set per-file properties for FILES.
831 If any of FILES is actually a directory, then do the same for all
832 buffers for files in that directory.
833 SETTINGS is an association list of property/value pairs. After
834 executing FORM, set those properties from SETTINGS that have not yet
835 been updated to their corresponding values."
836 (declare (debug t))
837 `(let ((vc-touched-properties (list t))
838 (flist nil))
839 (dolist (file ,files)
840 (if (file-directory-p file)
841 (dolist (buffer (buffer-list))
842 (let ((fname (buffer-file-name buffer)))
843 (when (and fname (vc-string-prefix-p file fname))
844 (push fname flist))))
845 (push file flist)))
846 ,form
847 (dolist (file flist)
848 (dolist (setting ,settings)
849 (let ((property (car setting)))
850 (unless (memq property vc-touched-properties)
851 (put (intern file vc-file-prop-obarray)
852 property (cdr setting))))))))
854 ;;; Code for deducing what fileset and backend to assume
856 (defun vc-backend-for-registration (file)
857 "Return a backend that can be used for registering FILE.
859 If no backend declares itself responsible for FILE, then FILE
860 must not be in a version controlled directory, so try to create a
861 repository, prompting for the directory and the VC backend to
862 use."
863 (catch 'found
864 ;; First try: find a responsible backend, it must be a backend
865 ;; under which FILE is not yet registered.
866 (dolist (backend vc-handled-backends)
867 (and (not (vc-call-backend backend 'registered file))
868 (vc-call-backend backend 'responsible-p file)
869 (throw 'found backend)))
870 ;; no responsible backend
871 (let* ((possible-backends
872 (let (pos)
873 (dolist (crt vc-handled-backends)
874 (when (vc-find-backend-function crt 'create-repo)
875 (push crt pos)))
876 pos))
878 (intern
879 ;; Read the VC backend from the user, only
880 ;; complete with the backends that have the
881 ;; 'create-repo method.
882 (completing-read
883 (format "%s is not in a version controlled directory.\nUse VC backend: " file)
884 (mapcar 'symbol-name possible-backends) nil t)))
885 (repo-dir
886 (let ((def-dir (file-name-directory file)))
887 ;; read the directory where to create the
888 ;; repository, make sure it's a parent of
889 ;; file.
890 (read-file-name
891 (format "create %s repository in: " bk)
892 default-directory def-dir t nil
893 (lambda (arg)
894 (message "arg %s" arg)
895 (and (file-directory-p arg)
896 (vc-string-prefix-p (expand-file-name arg) def-dir)))))))
897 (let ((default-directory repo-dir))
898 (vc-call-backend bk 'create-repo))
899 (throw 'found bk))))
901 (defun vc-responsible-backend (file)
902 "Return the name of a backend system that is responsible for FILE.
904 If FILE is already registered, return the
905 backend of FILE. If FILE is not registered, then the
906 first backend in `vc-handled-backends' that declares itself
907 responsible for FILE is returned."
908 (or (and (not (file-directory-p file)) (vc-backend file))
909 (catch 'found
910 ;; First try: find a responsible backend. If this is for registration,
911 ;; it must be a backend under which FILE is not yet registered.
912 (dolist (backend vc-handled-backends)
913 (and (vc-call-backend backend 'responsible-p file)
914 (throw 'found backend))))
915 (error "No VC backend is responsible for %s" file)))
917 (defun vc-expand-dirs (file-or-dir-list)
918 "Expands directories in a file list specification.
919 Within directories, only files already under version control are noticed."
920 (let ((flattened '()))
921 (dolist (node file-or-dir-list)
922 (when (file-directory-p node)
923 (vc-file-tree-walk
924 node (lambda (f) (when (vc-backend f) (push f flattened)))))
925 (unless (file-directory-p node) (push node flattened)))
926 (nreverse flattened)))
928 (defvar vc-dir-backend)
929 (defvar log-view-vc-backend)
930 (defvar diff-vc-backend)
932 (defun vc-deduce-backend ()
933 (cond ((derived-mode-p 'vc-dir-mode) vc-dir-backend)
934 ((derived-mode-p 'log-view-mode) log-view-vc-backend)
935 ((derived-mode-p 'diff-mode) diff-vc-backend)
936 ;; Maybe we could even use comint-mode rather than shell-mode?
937 ((derived-mode-p 'dired-mode 'shell-mode 'compilation-mode)
938 (vc-responsible-backend default-directory))
939 (vc-mode (vc-backend buffer-file-name))))
941 (declare-function vc-dir-current-file "vc-dir" ())
942 (declare-function vc-dir-deduce-fileset "vc-dir" (&optional state-model-only-files))
944 (defun vc-deduce-fileset (&optional observer allow-unregistered
945 state-model-only-files)
946 "Deduce a set of files and a backend to which to apply an operation.
948 Return (BACKEND FILESET FILESET-ONLY-FILES STATE CHECKOUT-MODEL).
949 If we're in VC-dir mode, the fileset is the list of marked files.
950 Otherwise, if we're looking at a buffer visiting a version-controlled file,
951 the fileset is a singleton containing this file.
952 If none of these conditions is met, but ALLOW_UNREGISTERED is on and the
953 visited file is not registered, return a singleton fileset containing it.
954 Otherwise, throw an error.
956 STATE-MODEL-ONLY-FILES if non-nil, means that the caller needs
957 the FILESET-ONLY-FILES STATE and MODEL info. Otherwise, that
958 part may be skipped.
959 BEWARE: this function may change the
960 current buffer."
961 ;; FIXME: OBSERVER is unused. The name is not intuitive and is not
962 ;; documented. It's set to t when called from diff and print-log.
963 (let (backend)
964 (cond
965 ((derived-mode-p 'vc-dir-mode)
966 (vc-dir-deduce-fileset state-model-only-files))
967 ((derived-mode-p 'dired-mode)
968 (if observer
969 (vc-dired-deduce-fileset)
970 (error "State changing VC operations not supported in `dired-mode'")))
971 ((setq backend (vc-backend buffer-file-name))
972 (if state-model-only-files
973 (list backend (list buffer-file-name)
974 (list buffer-file-name)
975 (vc-state buffer-file-name)
976 (vc-checkout-model backend buffer-file-name))
977 (list backend (list buffer-file-name))))
978 ((and (buffer-live-p vc-parent-buffer)
979 ;; FIXME: Why this test? --Stef
980 (or (buffer-file-name vc-parent-buffer)
981 (with-current-buffer vc-parent-buffer
982 (derived-mode-p 'vc-dir-mode))))
983 (progn ;FIXME: Why not `with-current-buffer'? --Stef.
984 (set-buffer vc-parent-buffer)
985 (vc-deduce-fileset observer allow-unregistered state-model-only-files)))
986 ((not buffer-file-name)
987 (error "Buffer %s is not associated with a file" (buffer-name)))
988 ((and allow-unregistered (not (vc-registered buffer-file-name)))
989 (if state-model-only-files
990 (list (vc-backend-for-registration (buffer-file-name))
991 (list buffer-file-name)
992 (list buffer-file-name)
993 (when state-model-only-files 'unregistered)
994 nil)
995 (list (vc-backend-for-registration (buffer-file-name))
996 (list buffer-file-name))))
997 (t (error "No fileset is available here")))))
999 (defun vc-dired-deduce-fileset ()
1000 (let ((backend (vc-responsible-backend default-directory)))
1001 (unless backend (error "Directory not under VC"))
1002 (list backend
1003 (dired-map-over-marks (dired-get-filename nil t) nil))))
1005 (defun vc-ensure-vc-buffer ()
1006 "Make sure that the current buffer visits a version-controlled file."
1007 (cond
1008 ((derived-mode-p 'vc-dir-mode)
1009 (set-buffer (find-file-noselect (vc-dir-current-file))))
1011 (while (and vc-parent-buffer
1012 (buffer-live-p vc-parent-buffer)
1013 ;; Avoid infinite looping when vc-parent-buffer and
1014 ;; current buffer are the same buffer.
1015 (not (eq vc-parent-buffer (current-buffer))))
1016 (set-buffer vc-parent-buffer))
1017 (if (not buffer-file-name)
1018 (error "Buffer %s is not associated with a file" (buffer-name))
1019 (unless (vc-backend buffer-file-name)
1020 (error "File %s is not under version control" buffer-file-name))))))
1022 ;;; Support for the C-x v v command.
1023 ;; This is where all the single-file-oriented code from before the fileset
1024 ;; rewrite lives.
1026 (defsubst vc-editable-p (file)
1027 "Return non-nil if FILE can be edited."
1028 (let ((backend (vc-backend file)))
1029 (and backend
1030 (or (eq (vc-checkout-model backend (list file)) 'implicit)
1031 (memq (vc-state file) '(edited needs-merge conflict))))))
1033 (defun vc-compatible-state (p q)
1034 "Controls which states can be in the same commit."
1036 (eq p q)
1037 (and (member p '(edited added removed)) (member q '(edited added removed)))))
1039 ;; Here's the major entry point.
1041 ;;;###autoload
1042 (defun vc-next-action (verbose)
1043 "Do the next logical version control operation on the current fileset.
1044 This requires that all files in the fileset be in the same state.
1046 For locking systems:
1047 If every file is not already registered, this registers each for version
1048 control.
1049 If every file is registered and not locked by anyone, this checks out
1050 a writable and locked file of each ready for editing.
1051 If every file is checked out and locked by the calling user, this
1052 first checks to see if each file has changed since checkout. If not,
1053 it performs a revert on that file.
1054 If every file has been changed, this pops up a buffer for entry
1055 of a log message; when the message has been entered, it checks in the
1056 resulting changes along with the log message as change commentary. If
1057 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1058 read-only copy of each changed file is left in place afterwards.
1059 If the affected file is registered and locked by someone else, you are
1060 given the option to steal the lock(s).
1062 For merging systems:
1063 If every file is not already registered, this registers each one for version
1064 control. This does an add, but not a commit.
1065 If every file is added but not committed, each one is committed.
1066 If every working file is changed, but the corresponding repository file is
1067 unchanged, this pops up a buffer for entry of a log message; when the
1068 message has been entered, it checks in the resulting changes along
1069 with the logmessage as change commentary. A writable file is retained.
1070 If the repository file is changed, you are asked if you want to
1071 merge in the changes into your working copy."
1072 (interactive "P")
1073 (let* ((vc-fileset (vc-deduce-fileset nil t 'state-model-only-files))
1074 (backend (car vc-fileset))
1075 (files (nth 1 vc-fileset))
1076 (fileset-only-files (nth 2 vc-fileset))
1077 ;; FIXME: We used to call `vc-recompute-state' here.
1078 (state (nth 3 vc-fileset))
1079 ;; The backend should check that the checkout-model is consistent
1080 ;; among all the `files'.
1081 (model (nth 4 vc-fileset)))
1083 ;; Do the right thing
1084 (cond
1085 ((eq state 'missing)
1086 (error "Fileset files are missing, so cannot be operated on"))
1087 ((eq state 'ignored)
1088 (error "Fileset files are ignored by the version-control system"))
1089 ((or (null state) (eq state 'unregistered))
1090 (vc-register nil vc-fileset))
1091 ;; Files are up-to-date, or need a merge and user specified a revision
1092 ((or (eq state 'up-to-date) (and verbose (eq state 'needs-update)))
1093 (cond
1094 (verbose
1095 ;; go to a different revision
1096 (let* ((revision
1097 (read-string "Branch, revision, or backend to move to: "))
1098 (revision-downcase (downcase revision)))
1099 (if (member
1100 revision-downcase
1101 (mapcar (lambda (arg) (downcase (symbol-name arg)))
1102 vc-handled-backends))
1103 (let ((vsym (intern-soft revision-downcase)))
1104 (dolist (file files) (vc-transfer-file file vsym)))
1105 (dolist (file files)
1106 (vc-checkout file (eq model 'implicit) revision)))))
1107 ((not (eq model 'implicit))
1108 ;; check the files out
1109 (dolist (file files) (vc-checkout file t)))
1111 ;; do nothing
1112 (message "Fileset is up-to-date"))))
1113 ;; Files have local changes
1114 ((vc-compatible-state state 'edited)
1115 (let ((ready-for-commit files))
1116 ;; If files are edited but read-only, give user a chance to correct
1117 (dolist (file files)
1118 (unless (file-writable-p file)
1119 ;; Make the file+buffer read-write.
1120 (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue?" file))
1121 (error "Aborted"))
1122 (set-file-modes file (logior (file-modes file) 128))
1123 (let ((visited (get-file-buffer file)))
1124 (when visited
1125 (with-current-buffer visited
1126 (toggle-read-only -1))))))
1127 ;; Allow user to revert files with no changes
1128 (save-excursion
1129 (dolist (file files)
1130 (let ((visited (get-file-buffer file)))
1131 ;; For files with locking, if the file does not contain
1132 ;; any changes, just let go of the lock, i.e. revert.
1133 (when (and (not (eq model 'implicit))
1134 (vc-workfile-unchanged-p file)
1135 ;; If buffer is modified, that means the user just
1136 ;; said no to saving it; in that case, don't revert,
1137 ;; because the user might intend to save after
1138 ;; finishing the log entry and committing.
1139 (not (and visited (buffer-modified-p))))
1140 (vc-revert-file file)
1141 (setq ready-for-commit (delete file ready-for-commit))))))
1142 ;; Remaining files need to be committed
1143 (if (not ready-for-commit)
1144 (message "No files remain to be committed")
1145 (if (not verbose)
1146 (vc-checkin ready-for-commit backend)
1147 (let* ((revision (read-string "New revision or backend: "))
1148 (revision-downcase (downcase revision)))
1149 (if (member
1150 revision-downcase
1151 (mapcar (lambda (arg) (downcase (symbol-name arg)))
1152 vc-handled-backends))
1153 (let ((vsym (intern revision-downcase)))
1154 (dolist (file files) (vc-transfer-file file vsym)))
1155 (vc-checkin ready-for-commit backend revision)))))))
1156 ;; locked by somebody else (locking VCSes only)
1157 ((stringp state)
1158 ;; In the old days, we computed the revision once and used it on
1159 ;; the single file. Then, for the 2007-2008 fileset rewrite, we
1160 ;; computed the revision once (incorrectly, using a free var) and
1161 ;; used it on all files. To fix the free var bug, we can either
1162 ;; use `(car files)' or do what we do here: distribute the
1163 ;; revision computation among `files'. Although this may be
1164 ;; tedious for those backends where a "revision" is a trans-file
1165 ;; concept, it is nonetheless correct for both those and (more
1166 ;; importantly) for those where "revision" is a per-file concept.
1167 ;; If the intersection of the former group and "locking VCSes" is
1168 ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
1169 ;; pre-computation approach of yore.
1170 (dolist (file files)
1171 (vc-steal-lock
1172 file (if verbose
1173 (read-string (format "%s revision to steal: " file))
1174 (vc-working-revision file))
1175 state)))
1176 ;; conflict
1177 ((eq state 'conflict)
1178 ;; FIXME: Is it really the UI we want to provide?
1179 ;; In my experience, the conflicted files should be marked as resolved
1180 ;; one-by-one when saving the file after resolving the conflicts.
1181 ;; I.e. stating explicitly that the conflicts are resolved is done
1182 ;; very rarely.
1183 (vc-mark-resolved backend files))
1184 ;; needs-update
1185 ((eq state 'needs-update)
1186 (dolist (file files)
1187 (if (yes-or-no-p (format
1188 "%s is not up-to-date. Get latest revision? "
1189 (file-name-nondirectory file)))
1190 (vc-checkout file (eq model 'implicit) t)
1191 (when (and (not (eq model 'implicit))
1192 (yes-or-no-p "Lock this revision? "))
1193 (vc-checkout file t)))))
1194 ;; needs-merge
1195 ((eq state 'needs-merge)
1196 (dolist (file files)
1197 (when (yes-or-no-p (format
1198 "%s is not up-to-date. Merge in changes now? "
1199 (file-name-nondirectory file)))
1200 (vc-maybe-resolve-conflicts
1201 file (vc-call-backend backend 'merge-news file)))))
1203 ;; unlocked-changes
1204 ((eq state 'unlocked-changes)
1205 (dolist (file files)
1206 (when (not (equal buffer-file-name file))
1207 (find-file-other-window file))
1208 (if (save-window-excursion
1209 (vc-diff-internal nil
1210 (cons (car vc-fileset) (cons (cadr vc-fileset) (list file)))
1211 (vc-working-revision file) nil)
1212 (goto-char (point-min))
1213 (let ((inhibit-read-only t))
1214 (insert
1215 (format "Changes to %s since last lock:\n\n" file)))
1216 (not (beep))
1217 (yes-or-no-p (concat "File has unlocked changes. "
1218 "Claim lock retaining changes? ")))
1219 (progn (vc-call-backend backend 'steal-lock file)
1220 (clear-visited-file-modtime)
1221 ;; Must clear any headers here because they wouldn't
1222 ;; show that the file is locked now.
1223 (vc-clear-headers file)
1224 (write-file buffer-file-name)
1225 (vc-mode-line file backend))
1226 (if (not (yes-or-no-p
1227 "Revert to checked-in revision, instead? "))
1228 (error "Checkout aborted")
1229 (vc-revert-buffer-internal t t)
1230 (vc-checkout file t)))))
1231 ;; Unknown fileset state
1233 (error "Fileset is in an unknown state %s" state)))))
1235 (defun vc-create-repo (backend)
1236 "Create an empty repository in the current directory."
1237 (interactive
1238 (list
1239 (intern
1240 (upcase
1241 (completing-read
1242 "Create repository for: "
1243 (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
1244 nil t)))))
1245 (vc-call-backend backend 'create-repo))
1247 (declare-function vc-dir-move-to-goal-column "vc-dir" ())
1249 ;;;###autoload
1250 (defun vc-register (&optional set-revision vc-fileset comment)
1251 "Register into a version control system.
1252 If VC-FILESET is given, register the files in that fileset.
1253 Otherwise register the current file.
1254 With prefix argument SET-REVISION, allow user to specify initial revision
1255 level. If COMMENT is present, use that as an initial comment.
1257 The version control system to use is found by cycling through the list
1258 `vc-handled-backends'. The first backend in that list which declares
1259 itself responsible for the file (usually because other files in that
1260 directory are already registered under that backend) will be used to
1261 register the file. If no backend declares itself responsible, the
1262 first backend that could register the file is used."
1263 (interactive "P")
1264 (let* ((fileset-arg (or vc-fileset (vc-deduce-fileset nil t)))
1265 (backend (car fileset-arg))
1266 (files (nth 1 fileset-arg)))
1267 ;; We used to operate on `only-files', but VC wants to provide the
1268 ;; possibility to register directories rather than files only, since
1269 ;; many VCS allow that as well.
1270 (dolist (fname files)
1271 (let ((bname (get-file-buffer fname)))
1272 (unless fname (setq fname buffer-file-name))
1273 (when (vc-backend fname)
1274 (if (vc-registered fname)
1275 (error "This file is already registered")
1276 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1277 (error "Aborted"))))
1278 ;; Watch out for new buffers of size 0: the corresponding file
1279 ;; does not exist yet, even though buffer-modified-p is nil.
1280 (when bname
1281 (with-current-buffer bname
1282 (when (and (not (buffer-modified-p))
1283 (zerop (buffer-size))
1284 (not (file-exists-p buffer-file-name)))
1285 (set-buffer-modified-p t))
1286 (vc-buffer-sync)))))
1287 (message "Registering %s... " files)
1288 (mapc 'vc-file-clearprops files)
1289 (vc-call-backend backend 'register files
1290 (if set-revision
1291 (read-string (format "Initial revision level for %s: " files))
1292 (vc-call-backend backend 'init-revision))
1293 comment)
1294 (mapc
1295 (lambda (file)
1296 (vc-file-setprop file 'vc-backend backend)
1297 ;; FIXME: This is wrong: it should set `backup-inhibited' in all
1298 ;; the buffers visiting files affected by this `vc-register', not
1299 ;; in the current-buffer.
1300 ;; (unless vc-make-backup-files
1301 ;; (make-local-variable 'backup-inhibited)
1302 ;; (setq backup-inhibited t))
1304 (vc-resynch-buffer file vc-keep-workfiles t))
1305 files)
1306 (when (derived-mode-p 'vc-dir-mode)
1307 (vc-dir-move-to-goal-column))
1308 (message "Registering %s... done" files)))
1310 (defun vc-register-with (backend)
1311 "Register the current file with a specified back end."
1312 (interactive "SBackend: ")
1313 (when (not (member backend vc-handled-backends))
1314 (error "Unknown back end"))
1315 (let ((vc-handled-backends (list backend)))
1316 (call-interactively 'vc-register)))
1318 (defun vc-checkout (file &optional writable rev)
1319 "Retrieve a copy of the revision REV of FILE.
1320 If WRITABLE is non-nil, make sure the retrieved file is writable.
1321 REV defaults to the latest revision.
1323 After check-out, runs the normal hook `vc-checkout-hook'."
1324 (and writable
1325 (not rev)
1326 (vc-call make-version-backups-p file)
1327 (vc-up-to-date-p file)
1328 (vc-make-version-backup file))
1329 (let ((backend (vc-backend file)))
1330 (with-vc-properties (list file)
1331 (condition-case err
1332 (vc-call-backend backend 'checkout file writable rev)
1333 (file-error
1334 ;; Maybe the backend is not installed ;-(
1335 (when writable
1336 (let ((buf (get-file-buffer file)))
1337 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1338 (signal (car err) (cdr err))))
1339 `((vc-state . ,(if (or (eq (vc-checkout-model backend (list file)) 'implicit)
1340 (not writable))
1341 (if (vc-call-backend backend 'latest-on-branch-p file)
1342 'up-to-date
1343 'needs-update)
1344 'edited))
1345 (vc-checkout-time . ,(nth 5 (file-attributes file))))))
1346 (vc-resynch-buffer file t t)
1347 (run-hooks 'vc-checkout-hook))
1349 (defun vc-mark-resolved (backend files)
1350 (prog1 (with-vc-properties
1351 files
1352 (vc-call-backend backend 'mark-resolved files)
1353 ;; FIXME: Is this TRTD? Might not be.
1354 `((vc-state . edited)))
1355 (message
1356 (substitute-command-keys
1357 "Conflicts have been resolved in %s. \
1358 Type \\[vc-next-action] to check in changes.")
1359 (if (> (length files) 1)
1360 (format "%d files" (length files))
1361 "this file"))))
1363 (defun vc-steal-lock (file rev owner)
1364 "Steal the lock on FILE."
1365 (let (file-description)
1366 (if rev
1367 (setq file-description (format "%s:%s" file rev))
1368 (setq file-description file))
1369 (when (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1370 file-description owner)))
1371 (error "Steal canceled"))
1372 (message "Stealing lock on %s..." file)
1373 (with-vc-properties
1374 (list file)
1375 (vc-call steal-lock file rev)
1376 `((vc-state . edited)))
1377 (vc-resynch-buffer file t t)
1378 (message "Stealing lock on %s...done" file)
1379 ;; Write mail after actually stealing, because if the stealing
1380 ;; goes wrong, we don't want to send any mail.
1381 (compose-mail owner (format "Stolen lock on %s" file-description))
1382 (setq default-directory (expand-file-name "~/"))
1383 (goto-char (point-max))
1384 (insert
1385 (format "I stole the lock on %s, " file-description)
1386 (current-time-string)
1387 ".\n")
1388 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1390 (defun vc-checkin (files backend &optional rev comment initial-contents)
1391 "Check in FILES.
1392 The optional argument REV may be a string specifying the new revision
1393 level (strongly deprecated). COMMENT is a comment
1394 string; if omitted, a buffer is popped up to accept a comment. If
1395 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1396 of the log entry buffer.
1398 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1399 that the version control system supports this mode of operation.
1401 Runs the normal hooks `vc-before-checkin-hook' and `vc-checkin-hook'."
1402 (when vc-before-checkin-hook
1403 (run-hooks 'vc-before-checkin-hook))
1404 (lexical-let
1405 ((backend backend))
1406 (vc-start-logentry
1407 files comment initial-contents
1408 "Enter a change comment."
1409 "*VC-log*"
1410 (lambda ()
1411 (vc-call-backend backend 'log-edit-mode))
1412 (lexical-let ((rev rev))
1413 (lambda (files comment)
1414 (message "Checking in %s..." (vc-delistify files))
1415 ;; "This log message intentionally left almost blank".
1416 ;; RCS 5.7 gripes about white-space-only comments too.
1417 (or (and comment (string-match "[^\t\n ]" comment))
1418 (setq comment "*** empty log message ***"))
1419 (with-vc-properties
1420 files
1421 ;; We used to change buffers to get local value of
1422 ;; vc-checkin-switches, but 'the' local buffer is
1423 ;; not a well-defined concept for filesets.
1424 (progn
1425 (vc-call-backend backend 'checkin files rev comment)
1426 (mapc 'vc-delete-automatic-version-backups files))
1427 `((vc-state . up-to-date)
1428 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1429 (vc-working-revision . nil)))
1430 (message "Checking in %s...done" (vc-delistify files))))
1431 'vc-checkin-hook)))
1433 ;;; Additional entry points for examining version histories
1435 ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
1436 ;; "List differences for all registered files at and below DIR.
1437 ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
1438 ;; ;; This implementation does an explicit tree walk, and calls
1439 ;; ;; vc-BACKEND-diff directly for each file. An optimization
1440 ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
1441 ;; ;; and to call it only for files that are actually changed.
1442 ;; ;; However, this is expensive for some backends, and so it is left
1443 ;; ;; to backend-specific implementations.
1444 ;; (setq default-directory dir)
1445 ;; (vc-file-tree-walk
1446 ;; default-directory
1447 ;; (lambda (f)
1448 ;; (vc-exec-after
1449 ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1450 ;; (message "Looking at %s" ',f)
1451 ;; (vc-call-backend ',(vc-backend f)
1452 ;; 'diff (list ',f) ',rev1 ',rev2))))))
1454 (defvar vc-coding-system-inherit-eol t
1455 "When non-nil, inherit the EOL format for reading Diff output from the file.
1457 Used in `vc-coding-system-for-diff' to determine the EOL format to use
1458 for reading Diff output for a file. If non-nil, the EOL format is
1459 inherited from the file itself.
1460 Set this variable to nil if your Diff tool might use a different
1461 EOL. Then Emacs will auto-detect the EOL format in Diff output, which
1462 gives better results.") ;; Cf. bug#4451.
1464 (defun vc-coding-system-for-diff (file)
1465 "Return the coding system for reading diff output for FILE."
1466 (or coding-system-for-read
1467 ;; if we already have this file open,
1468 ;; use the buffer's coding system
1469 (let ((buf (find-buffer-visiting file)))
1470 (when buf (with-current-buffer buf
1471 (if vc-coding-system-inherit-eol
1472 buffer-file-coding-system
1473 ;; Don't inherit the EOL part of the coding-system,
1474 ;; because some Diff tools may choose to use
1475 ;; a different one. bug#4451.
1476 (coding-system-base buffer-file-coding-system)))))
1477 ;; otherwise, try to find one based on the file name
1478 (car (find-operation-coding-system 'insert-file-contents file))
1479 ;; and a final fallback
1480 'undecided))
1482 (defun vc-switches (backend op)
1483 "Return a list of vc-BACKEND switches for operation OP.
1484 BACKEND is a symbol such as `CVS', which will be downcased.
1485 OP is a symbol such as `diff'.
1487 In decreasing order of preference, return the value of:
1488 vc-BACKEND-OP-switches (e.g. `vc-cvs-diff-switches');
1489 vc-OP-switches (e.g. `vc-diff-switches'); or, in the case of
1490 diff only, `diff-switches'.
1492 If the chosen value is not a string or a list, return nil.
1493 This is so that you may set, e.g. `vc-svn-diff-switches' to t in order
1494 to override the value of `vc-diff-switches' and `diff-switches'."
1495 (let ((switches
1496 (or (when backend
1497 (let ((sym (vc-make-backend-sym
1498 backend (intern (concat (symbol-name op)
1499 "-switches")))))
1500 (when (boundp sym) (symbol-value sym))))
1501 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1502 (when (boundp sym) (symbol-value sym)))
1503 (cond
1504 ((eq op 'diff) diff-switches)))))
1505 (if (stringp switches) (list switches)
1506 ;; If not a list, return nil.
1507 ;; This is so we can set vc-diff-switches to t to override
1508 ;; any switches in diff-switches.
1509 (when (listp switches) switches))))
1511 ;; Old def for compatibility with Emacs-21.[123].
1512 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1513 (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
1515 (defun vc-diff-finish (buffer messages)
1516 ;; The empty sync output case has already been handled, so the only
1517 ;; possibility of an empty output is for an async process.
1518 (when (buffer-live-p buffer)
1519 (let ((window (get-buffer-window buffer t))
1520 (emptyp (zerop (buffer-size buffer))))
1521 (with-current-buffer buffer
1522 (and messages emptyp
1523 (let ((inhibit-read-only t))
1524 (insert (cdr messages) ".\n")
1525 (message "%s" (cdr messages))))
1526 (goto-char (point-min))
1527 (when window
1528 (shrink-window-if-larger-than-buffer window)))
1529 (when (and messages (not emptyp))
1530 (message "%sdone" (car messages))))))
1532 (defvar vc-diff-added-files nil
1533 "If non-nil, diff added files by comparing them to /dev/null.")
1535 (defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose)
1536 "Report diffs between two revisions of a fileset.
1537 Diff output goes to the *vc-diff* buffer. The function
1538 returns t if the buffer had changes, nil otherwise."
1539 (let* ((files (cadr vc-fileset))
1540 (messages (cons (format "Finding changes in %s..."
1541 (vc-delistify files))
1542 (format "No changes between %s and %s"
1543 (or rev1 "working revision")
1544 (or rev2 "workfile"))))
1545 ;; Set coding system based on the first file. It's a kluge,
1546 ;; but the only way to set it for each file included would
1547 ;; be to call the back end separately for each file.
1548 (coding-system-for-read
1549 (if files (vc-coding-system-for-diff (car files)) 'undecided)))
1550 (vc-setup-buffer "*vc-diff*")
1551 (message "%s" (car messages))
1552 ;; Many backends don't handle well the case of a file that has been
1553 ;; added but not yet committed to the repo (notably CVS and Subversion).
1554 ;; Do that work here so the backends don't have to futz with it. --ESR
1556 ;; Actually most backends (including CVS) have options to control the
1557 ;; behavior since which one is better depends on the user and on the
1558 ;; situation). Worse yet: this code does not handle the case where
1559 ;; `file' is a directory which contains added files.
1560 ;; I made it conditional on vc-diff-added-files but it should probably
1561 ;; just be removed (or copied/moved to specific backends). --Stef.
1562 (when vc-diff-added-files
1563 (let ((filtered '())
1564 process-file-side-effects)
1565 (dolist (file files)
1566 (if (or (file-directory-p file)
1567 (not (string= (vc-working-revision file) "0")))
1568 (push file filtered)
1569 ;; This file is added but not yet committed;
1570 ;; there is no repository version to diff against.
1571 (if (or rev1 rev2)
1572 (error "No revisions of %s exist" file)
1573 ;; We regard this as "changed".
1574 ;; Diff it against /dev/null.
1575 (apply 'vc-do-command "*vc-diff*"
1576 1 "diff" file
1577 (append (vc-switches nil 'diff) '("/dev/null"))))))
1578 (setq files (nreverse filtered))))
1579 (let ((vc-disable-async-diff (not async)))
1580 (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 "*vc-diff*"))
1581 (set-buffer "*vc-diff*")
1582 (if (and (zerop (buffer-size))
1583 (not (get-buffer-process (current-buffer))))
1584 ;; Treat this case specially so as not to pop the buffer.
1585 (progn
1586 (message "%s" (cdr messages))
1587 nil)
1588 (diff-mode)
1589 (set (make-local-variable 'diff-vc-backend) (car vc-fileset))
1590 (set (make-local-variable 'revert-buffer-function)
1591 `(lambda (ignore-auto noconfirm)
1592 (vc-diff-internal ,async ',vc-fileset ,rev1 ,rev2 ,verbose)))
1593 ;; Make the *vc-diff* buffer read only, the diff-mode key
1594 ;; bindings are nicer for read only buffers. pcl-cvs does the
1595 ;; same thing.
1596 (setq buffer-read-only t)
1597 (vc-exec-after `(vc-diff-finish ,(current-buffer) ',(when verbose
1598 messages)))
1599 ;; Display the buffer, but at the end because it can change point.
1600 (pop-to-buffer (current-buffer))
1601 ;; In the async case, we return t even if there are no differences
1602 ;; because we don't know that yet.
1603 t)))
1605 (defun vc-read-revision (prompt &optional files backend default initial-input)
1606 (cond
1607 ((null files)
1608 (let ((vc-fileset (vc-deduce-fileset t))) ;FIXME: why t? --Stef
1609 (setq files (cadr vc-fileset))
1610 (setq backend (car vc-fileset))))
1611 ((null backend) (setq backend (vc-backend (car files)))))
1612 (let ((completion-table
1613 (vc-call-backend backend 'revision-completion-table files)))
1614 (if completion-table
1615 (completing-read prompt completion-table
1616 nil nil initial-input nil default)
1617 (read-string prompt initial-input nil default))))
1619 ;;;###autoload
1620 (defun vc-version-diff (files rev1 rev2)
1621 "Report diffs between revisions of the fileset in the repository history."
1622 (interactive
1623 (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: why t? --Stef
1624 (files (cadr vc-fileset))
1625 (backend (car vc-fileset))
1626 (first (car files))
1627 (rev1-default nil)
1628 (rev2-default nil))
1629 (cond
1630 ;; someday we may be able to do revision completion on non-singleton
1631 ;; filesets, but not yet.
1632 ((/= (length files) 1)
1633 nil)
1634 ;; if it's a directory, don't supply any revision default
1635 ((file-directory-p first)
1636 nil)
1637 ;; if the file is not up-to-date, use working revision as older revision
1638 ((not (vc-up-to-date-p first))
1639 (setq rev1-default (vc-working-revision first)))
1640 ;; if the file is not locked, use last and previous revisions as defaults
1642 (setq rev1-default (vc-call-backend backend 'previous-revision first
1643 (vc-working-revision first)))
1644 (when (string= rev1-default "") (setq rev1-default nil))
1645 (setq rev2-default (vc-working-revision first))))
1646 ;; construct argument list
1647 (let* ((rev1-prompt (if rev1-default
1648 (concat "Older revision (default "
1649 rev1-default "): ")
1650 "Older revision: "))
1651 (rev2-prompt (concat "Newer revision (default "
1652 (or rev2-default "current source") "): "))
1653 (rev1 (vc-read-revision rev1-prompt files backend rev1-default))
1654 (rev2 (vc-read-revision rev2-prompt files backend rev2-default)))
1655 (when (string= rev1 "") (setq rev1 nil))
1656 (when (string= rev2 "") (setq rev2 nil))
1657 (list files rev1 rev2))))
1658 ;; All that was just so we could do argument completion!
1659 (when (and (not rev1) rev2)
1660 (error "Not a valid revision range"))
1661 ;; Yes, it's painful to call (vc-deduce-fileset) again. Alas, the
1662 ;; placement rules for (interactive) don't actually leave us a choice.
1663 (vc-diff-internal t (vc-deduce-fileset t) rev1 rev2
1664 (called-interactively-p 'interactive)))
1666 ;;;###autoload
1667 (defun vc-diff (historic &optional not-urgent)
1668 "Display diffs between file revisions.
1669 Normally this compares the currently selected fileset with their
1670 working revisions. With a prefix argument HISTORIC, it reads two revision
1671 designators specifying which revisions to compare.
1673 The optional argument NOT-URGENT non-nil means it is ok to say no to
1674 saving the buffer."
1675 (interactive (list current-prefix-arg t))
1676 (if historic
1677 (call-interactively 'vc-version-diff)
1678 (when buffer-file-name (vc-buffer-sync not-urgent))
1679 (vc-diff-internal t (vc-deduce-fileset t) nil nil
1680 (called-interactively-p 'interactive))))
1682 ;;;###autoload
1683 (defun vc-root-diff (historic &optional not-urgent)
1684 "Display diffs between VC-controlled whole tree revisions.
1685 Normally, this compares the tree corresponding to the current
1686 fileset with the working revision.
1687 With a prefix argument HISTORIC, prompt for two revision
1688 designators specifying which revisions to compare.
1690 The optional argument NOT-URGENT non-nil means it is ok to say no to
1691 saving the buffer."
1692 (interactive (list current-prefix-arg t))
1693 (if historic
1694 ;; FIXME: this does not work right, `vc-version-diff' ends up
1695 ;; calling `vc-deduce-fileset' to find the files to diff, and
1696 ;; that's not what we want here, we want the diff for the VC root dir.
1697 (call-interactively 'vc-version-diff)
1698 (when buffer-file-name (vc-buffer-sync not-urgent))
1699 (let ((backend (vc-deduce-backend))
1700 rootdir working-revision)
1701 (unless backend
1702 (error "Buffer is not version controlled"))
1703 (setq rootdir (vc-call-backend backend 'root default-directory))
1704 (setq working-revision (vc-working-revision rootdir))
1705 ;; VC diff for the root directory produces output that is
1706 ;; relative to it. Bind default-directory to the root directory
1707 ;; here, this way the *vc-diff* buffer is setup correctly, so
1708 ;; relative file names work.
1709 (let ((default-directory rootdir))
1710 (vc-diff-internal
1711 t (list backend (list rootdir) working-revision) nil nil
1712 (called-interactively-p 'interactive))))))
1714 ;;;###autoload
1715 (defun vc-revision-other-window (rev)
1716 "Visit revision REV of the current file in another window.
1717 If the current file is named `F', the revision is named `F.~REV~'.
1718 If `F.~REV~' already exists, use it instead of checking it out again."
1719 (interactive
1720 (save-current-buffer
1721 (vc-ensure-vc-buffer)
1722 (list
1723 (vc-read-revision "Revision to visit (default is working revision): "
1724 (list buffer-file-name)))))
1725 (vc-ensure-vc-buffer)
1726 (let* ((file buffer-file-name)
1727 (revision (if (string-equal rev "")
1728 (vc-working-revision file)
1729 rev)))
1730 (switch-to-buffer-other-window (vc-find-revision file revision))))
1732 (defun vc-find-revision (file revision &optional backend)
1733 "Read REVISION of FILE into a buffer and return the buffer.
1734 Use BACKEND as the VC backend if specified."
1735 (let ((automatic-backup (vc-version-backup-file-name file revision))
1736 (filebuf (or (get-file-buffer file) (current-buffer)))
1737 (filename (vc-version-backup-file-name file revision 'manual)))
1738 (unless (file-exists-p filename)
1739 (if (file-exists-p automatic-backup)
1740 (rename-file automatic-backup filename nil)
1741 (message "Checking out %s..." filename)
1742 (with-current-buffer filebuf
1743 (let ((failed t))
1744 (unwind-protect
1745 (let ((coding-system-for-read 'no-conversion)
1746 (coding-system-for-write 'no-conversion))
1747 (with-temp-file filename
1748 (let ((outbuf (current-buffer)))
1749 ;; Change buffer to get local value of
1750 ;; vc-checkout-switches.
1751 (with-current-buffer filebuf
1752 (if backend
1753 (vc-call-backend backend 'find-revision file revision outbuf)
1754 (vc-call find-revision file revision outbuf)))))
1755 (setq failed nil))
1756 (when (and failed (file-exists-p filename))
1757 (delete-file filename))))
1758 (vc-mode-line file))
1759 (message "Checking out %s...done" filename)))
1760 (let ((result-buf (find-file-noselect filename)))
1761 (with-current-buffer result-buf
1762 ;; Set the parent buffer so that things like
1763 ;; C-x v g, C-x v l, ... etc work.
1764 (set (make-local-variable 'vc-parent-buffer) filebuf))
1765 result-buf)))
1767 ;; Header-insertion code
1769 ;;;###autoload
1770 (defun vc-insert-headers ()
1771 "Insert headers into a file for use with a version control system.
1772 Headers desired are inserted at point, and are pulled from
1773 the variable `vc-BACKEND-header'."
1774 (interactive)
1775 (vc-ensure-vc-buffer)
1776 (save-excursion
1777 (save-restriction
1778 (widen)
1779 (when (or (not (vc-check-headers))
1780 (y-or-n-p "Version headers already exist. Insert another set? "))
1781 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1782 (comment-start-vc (or (car delims) comment-start "#"))
1783 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1784 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
1785 'header))
1786 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1787 (dolist (s hdstrings)
1788 (insert comment-start-vc "\t" s "\t"
1789 comment-end-vc "\n"))
1790 (when vc-static-header-alist
1791 (dolist (f vc-static-header-alist)
1792 (when (string-match (car f) buffer-file-name)
1793 (insert (format (cdr f) (car hdstrings)))))))))))
1795 (defun vc-clear-headers (&optional file)
1796 "Clear all version headers in the current buffer (or FILE).
1797 The headers are reset to their non-expanded form."
1798 (let* ((filename (or file buffer-file-name))
1799 (visited (find-buffer-visiting filename))
1800 (backend (vc-backend filename)))
1801 (when (vc-find-backend-function backend 'clear-headers)
1802 (if visited
1803 (let ((context (vc-buffer-context)))
1804 ;; save-excursion may be able to relocate point and mark
1805 ;; properly. If it fails, vc-restore-buffer-context
1806 ;; will give it a second try.
1807 (save-excursion
1808 (vc-call-backend backend 'clear-headers))
1809 (vc-restore-buffer-context context))
1810 (set-buffer (find-file-noselect filename))
1811 (vc-call-backend backend 'clear-headers)
1812 (kill-buffer filename)))))
1814 (defun vc-modify-change-comment (files rev oldcomment)
1815 "Edit the comment associated with the given files and revision."
1816 ;; Less of a kluge than it looks like; log-view mode only passes
1817 ;; this function a singleton list. Arguments left in this form in
1818 ;; case the more general operation ever becomes meaningful.
1819 (let ((backend (vc-responsible-backend (car files))))
1820 (vc-start-logentry
1821 files oldcomment t
1822 "Enter a replacement change comment."
1823 "*VC-log*"
1824 (lambda () (vc-call-backend backend 'log-edit-mode))
1825 (lexical-let ((rev rev))
1826 (lambda (files comment)
1827 (vc-call-backend backend
1828 'modify-change-comment files rev comment))))))
1830 ;;;###autoload
1831 (defun vc-merge ()
1832 "Perform a version control merge operation.
1833 On a distributed version control system, this runs a \"merge\"
1834 operation to incorporate changes from another branch onto the
1835 current branch, prompting for an argument list.
1837 On a non-distributed version control system, this merges changes
1838 between two revisions into the current fileset. This asks for
1839 two revisions to merge from in the minibuffer. If the first
1840 revision is a branch number, then merge all changes from that
1841 branch. If the first revision is empty, merge the most recent
1842 changes from the current branch."
1843 (interactive)
1844 (let* ((vc-fileset (vc-deduce-fileset t))
1845 (backend (car vc-fileset))
1846 (files (cadr vc-fileset)))
1847 (cond
1848 ;; If a branch-merge operation is defined, use it.
1849 ((vc-find-backend-function backend 'merge-branch)
1850 (vc-call-backend backend 'merge-branch))
1851 ;; Otherwise, do a per-file merge.
1852 ((vc-find-backend-function backend 'merge)
1853 (vc-buffer-sync)
1854 (dolist (file files)
1855 (let* ((state (vc-state file))
1856 first-revision second-revision status)
1857 (cond
1858 ((stringp state) ;; Locking VCses only
1859 (error "File %s is locked by %s" file state))
1860 ((not (vc-editable-p file))
1861 (vc-checkout file t)))
1862 (setq first-revision
1863 (vc-read-revision
1864 (concat "Merge " file
1865 "from branch or revision "
1866 "(default news on current branch): ")
1867 (list file)
1868 backend))
1869 (cond
1870 ((string= first-revision "")
1871 (setq status (vc-call-backend backend 'merge-news file)))
1873 (if (not (vc-branch-p first-revision))
1874 (setq second-revision
1875 (vc-read-revision
1876 "Second revision: "
1877 (list file) backend nil
1878 ;; FIXME: This is CVS/RCS/SCCS specific.
1879 (concat (vc-branch-part first-revision) ".")))
1880 ;; We want to merge an entire branch. Set revisions
1881 ;; accordingly, so that vc-BACKEND-merge understands us.
1882 (setq second-revision first-revision)
1883 ;; first-revision must be the starting point of the branch
1884 (setq first-revision (vc-branch-part first-revision)))
1885 (setq status (vc-call-backend backend 'merge file
1886 first-revision second-revision))))
1887 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE"))))
1889 (error "Sorry, merging is not implemented for %s" backend)))))
1892 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1893 (vc-resynch-buffer file t (not (buffer-modified-p)))
1894 (if (zerop status) (message "Merge successful")
1895 (smerge-mode 1)
1896 (message "File contains conflicts.")))
1898 ;;;###autoload
1899 (defalias 'vc-resolve-conflicts 'smerge-ediff)
1901 ;; TODO: This is OK but maybe we could integrate it better.
1902 ;; E.g. it could be run semi-automatically (via a prompt?) when saving a file
1903 ;; that was conflicted (i.e. upon mark-resolved).
1904 ;; FIXME: should we add an "other-window" version? Or maybe we should
1905 ;; hook it inside find-file so it automatically works for
1906 ;; find-file-other-window as well. E.g. find-file could use a new
1907 ;; `default-next-file' variable for its default file (M-n), and
1908 ;; we could then set it upon mark-resolve, so C-x C-s C-x C-f M-n would
1909 ;; automatically offer the next conflicted file.
1910 (defun vc-find-conflicted-file ()
1911 "Visit the next conflicted file in the current project."
1912 (interactive)
1913 (let* ((backend (or (if buffer-file-name (vc-backend buffer-file-name))
1914 (vc-responsible-backend default-directory)
1915 (error "No VC backend")))
1916 (files (vc-call-backend backend
1917 'conflicted-files default-directory)))
1918 ;; Don't try and visit the current file.
1919 (if (equal (car files) buffer-file-name) (pop files))
1920 (if (null files)
1921 (message "No more conflicted files")
1922 (find-file (pop files))
1923 (message "%s more conflicted files after this one"
1924 (if files (length files) "No")))))
1926 ;; Named-configuration entry points
1928 (defun vc-tag-precondition (dir)
1929 "Scan the tree below DIR, looking for files not up-to-date.
1930 If any file is not up-to-date, return the name of the first such file.
1931 \(This means, neither tag creation nor retrieval is allowed.\)
1932 If one or more of the files are currently visited, return `visited'.
1933 Otherwise, return nil."
1934 (let ((status nil))
1935 (catch 'vc-locked-example
1936 (vc-file-tree-walk
1938 (lambda (f)
1939 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
1940 (when (get-file-buffer f) (setq status 'visited)))))
1941 status)))
1943 ;;;###autoload
1944 (defun vc-create-tag (dir name branchp)
1945 "Descending recursively from DIR, make a tag called NAME.
1946 For each registered file, the working revision becomes part of
1947 the named configuration. If the prefix argument BRANCHP is
1948 given, the tag is made as a new branch and the files are
1949 checked out in that new branch."
1950 (interactive
1951 (let ((granularity
1952 (vc-call-backend (vc-responsible-backend default-directory)
1953 'revision-granularity)))
1954 (list
1955 (if (eq granularity 'repository)
1956 ;; For VC's that do not work at file level, it's pointless
1957 ;; to ask for a directory, branches are created at repository level.
1958 default-directory
1959 (read-file-name "Directory: " default-directory default-directory t))
1960 (read-string (if current-prefix-arg "New branch name: " "New tag name: "))
1961 current-prefix-arg)))
1962 (message "Making %s... " (if branchp "branch" "tag"))
1963 (when (file-directory-p dir) (setq dir (file-name-as-directory dir)))
1964 (vc-call-backend (vc-responsible-backend dir)
1965 'create-tag dir name branchp)
1966 (vc-resynch-buffer dir t t t)
1967 (message "Making %s... done" (if branchp "branch" "tag")))
1969 ;;;###autoload
1970 (defun vc-retrieve-tag (dir name)
1971 "Descending recursively from DIR, retrieve the tag called NAME.
1972 If NAME is empty, it refers to the latest revisions.
1973 If locking is used for the files in DIR, then there must not be any
1974 locked files at or below DIR (but if NAME is empty, locked files are
1975 allowed and simply skipped)."
1976 (interactive
1977 (let ((granularity
1978 (vc-call-backend (vc-responsible-backend default-directory)
1979 'revision-granularity)))
1980 (list
1981 (if (eq granularity 'repository)
1982 ;; For VC's that do not work at file level, it's pointless
1983 ;; to ask for a directory, branches are created at repository level.
1984 default-directory
1985 (read-file-name "Directory: " default-directory default-directory t))
1986 (read-string "Tag name to retrieve (default latest revisions): "))))
1987 (let ((update (yes-or-no-p "Update any affected buffers? "))
1988 (msg (if (or (not name) (string= name ""))
1989 (format "Updating %s... " (abbreviate-file-name dir))
1990 (format "Retrieving tag into %s... "
1991 (abbreviate-file-name dir)))))
1992 (message "%s" msg)
1993 (vc-call-backend (vc-responsible-backend dir)
1994 'retrieve-tag dir name update)
1995 (vc-resynch-buffer dir t t t)
1996 (message "%s" (concat msg "done"))))
1999 ;; Miscellaneous other entry points
2001 ;; FIXME: this should be a defcustom
2002 ;; FIXME: maybe add another choice:
2003 ;; `root-directory' (or somesuch), which would mean show a short log
2004 ;; for the root directory.
2005 (defvar vc-log-short-style '(directory)
2006 "Whether or not to show a short log.
2007 If it contains `directory' then if the fileset contains a directory show a short log.
2008 If it contains `file' then show short logs for files.
2009 Not all VC backends support short logs!")
2011 (defvar log-view-vc-fileset)
2013 (defun vc-print-log-setup-buttons (working-revision is-start-revision limit pl-return)
2014 (when (and limit (not (eq 'limit-unsupported pl-return))
2015 (not is-start-revision))
2016 (goto-char (point-max))
2017 (lexical-let ((working-revision working-revision)
2018 (limit limit))
2019 (widget-create 'push-button
2020 :notify (lambda (&rest ignore)
2021 (vc-print-log-internal
2022 log-view-vc-backend log-view-vc-fileset
2023 working-revision nil (* 2 limit)))
2024 :help-echo "Show the log again, and double the number of log entries shown"
2025 "Show 2X entries")
2026 (widget-insert " ")
2027 (widget-create 'push-button
2028 :notify (lambda (&rest ignore)
2029 (vc-print-log-internal
2030 log-view-vc-backend log-view-vc-fileset
2031 working-revision nil nil))
2032 :help-echo "Show the log again, showing all entries"
2033 "Show unlimited entries"))
2034 (widget-setup)))
2036 (defun vc-print-log-internal (backend files working-revision
2037 &optional is-start-revision limit)
2038 ;; Don't switch to the output buffer before running the command,
2039 ;; so that any buffer-local settings in the vc-controlled
2040 ;; buffer can be accessed by the command.
2041 (let ((dir-present nil)
2042 (vc-short-log nil)
2043 (buffer-name "*vc-change-log*")
2044 type
2045 pl-return)
2046 (dolist (file files)
2047 (when (file-directory-p file)
2048 (setq dir-present t)))
2049 (setq vc-short-log
2050 (not (null (if dir-present
2051 (memq 'directory vc-log-short-style)
2052 (memq 'file vc-log-short-style)))))
2053 (setq type (if vc-short-log 'short 'long))
2054 (lexical-let
2055 ((working-revision working-revision)
2056 (backend backend)
2057 (limit limit)
2058 (shortlog vc-short-log)
2059 (files files)
2060 (is-start-revision is-start-revision))
2061 (vc-log-internal-common
2062 backend buffer-name files type
2063 (lambda (bk buf type-arg files-arg)
2064 (vc-call-backend bk 'print-log files-arg buf
2065 shortlog (when is-start-revision working-revision) limit))
2066 (lambda (bk files-arg ret)
2067 (vc-print-log-setup-buttons working-revision
2068 is-start-revision limit ret))
2069 (lambda (bk)
2070 (vc-call-backend bk 'show-log-entry working-revision))
2071 (lambda (ignore-auto noconfirm)
2072 (vc-print-log-internal backend files working-revision is-start-revision limit))))))
2074 (defvar vc-log-view-type nil
2075 "Set this to differentiate the different types of logs.")
2076 (put 'vc-log-view-type 'permanent-local t)
2078 (defun vc-log-internal-common (backend
2079 buffer-name
2080 files
2081 type
2082 backend-func
2083 setup-buttons-func
2084 goto-location-func
2085 rev-buff-func)
2086 (let (retval)
2087 (with-current-buffer (get-buffer-create buffer-name)
2088 (set (make-local-variable 'vc-log-view-type) type))
2089 (setq retval (funcall backend-func backend buffer-name type files))
2090 (pop-to-buffer buffer-name)
2091 (let ((inhibit-read-only t))
2092 ;; log-view-mode used to be called with inhibit-read-only bound
2093 ;; to t, so let's keep doing it, just in case.
2094 (vc-call-backend backend 'log-view-mode)
2095 (set (make-local-variable 'log-view-vc-backend) backend)
2096 (set (make-local-variable 'log-view-vc-fileset) files)
2097 (set (make-local-variable 'revert-buffer-function)
2098 rev-buff-func))
2099 (vc-exec-after
2100 `(let ((inhibit-read-only t))
2101 (funcall ',setup-buttons-func ',backend ',files ',retval)
2102 (shrink-window-if-larger-than-buffer)
2103 (funcall ',goto-location-func ',backend)
2104 (setq vc-sentinel-movepoint (point))
2105 (set-buffer-modified-p nil)))))
2107 (defun vc-incoming-outgoing-internal (backend remote-location buffer-name type)
2108 (vc-log-internal-common
2109 backend buffer-name nil type
2110 (lexical-let
2111 ((remote-location remote-location))
2112 (lambda (bk buf type-arg files)
2113 (vc-call-backend bk type-arg buf remote-location)))
2114 (lambda (bk files-arg ret))
2115 (lambda (bk)
2116 (goto-char (point-min)))
2117 (lexical-let
2118 ((backend backend)
2119 (remote-location remote-location)
2120 (buffer-name buffer-name)
2121 (type type))
2122 (lambda (ignore-auto noconfirm)
2123 (vc-incoming-outgoing-internal backend remote-location buffer-name type)))))
2125 ;;;###autoload
2126 (defun vc-print-log (&optional working-revision limit)
2127 "List the change log of the current fileset in a window.
2128 If WORKING-REVISION is non-nil, leave point at that revision.
2129 If LIMIT is non-nil, it should be a number specifying the maximum
2130 number of revisions to show; the default is `vc-log-show-limit'.
2132 When called interactively with a prefix argument, prompt for
2133 WORKING-REVISION and LIMIT."
2134 (interactive
2135 (cond
2136 (current-prefix-arg
2137 (let ((rev (read-from-minibuffer "Log from revision (default: last revision): " nil
2138 nil nil nil))
2139 (lim (string-to-number
2140 (read-from-minibuffer
2141 "Limit display (unlimited: 0): "
2142 (format "%s" vc-log-show-limit)
2143 nil nil nil))))
2144 (when (string= rev "") (setq rev nil))
2145 (when (<= lim 0) (setq lim nil))
2146 (list rev lim)))
2148 (list nil (when (> vc-log-show-limit 0) vc-log-show-limit)))))
2149 (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: Why t? --Stef
2150 (backend (car vc-fileset))
2151 (files (cadr vc-fileset))
2152 (working-revision (or working-revision (vc-working-revision (car files)))))
2153 (vc-print-log-internal backend files working-revision nil limit)))
2155 ;;;###autoload
2156 (defun vc-print-root-log (&optional limit)
2157 "List the change log for the current VC controlled tree in a window.
2158 If LIMIT is non-nil, it should be a number specifying the maximum
2159 number of revisions to show; the default is `vc-log-show-limit'.
2160 When called interactively with a prefix argument, prompt for LIMIT."
2161 (interactive
2162 (cond
2163 (current-prefix-arg
2164 (let ((lim (string-to-number
2165 (read-from-minibuffer
2166 "Limit display (unlimited: 0): "
2167 (format "%s" vc-log-show-limit)
2168 nil nil nil))))
2169 (when (<= lim 0) (setq lim nil))
2170 (list lim)))
2172 (list (when (> vc-log-show-limit 0) vc-log-show-limit)))))
2173 (let ((backend (vc-deduce-backend))
2174 rootdir working-revision)
2175 (unless backend
2176 (error "Buffer is not version controlled"))
2177 (setq rootdir (vc-call-backend backend 'root default-directory))
2178 (setq working-revision (vc-working-revision rootdir))
2179 (vc-print-log-internal backend (list rootdir) working-revision nil limit)))
2181 ;;;###autoload
2182 (defun vc-log-incoming (&optional remote-location)
2183 "Show a log of changes that will be received with a pull operation from REMOTE-LOCATION.
2184 When called interactively with a prefix argument, prompt for REMOTE-LOCATION.."
2185 (interactive
2186 (when current-prefix-arg
2187 (list (read-string "Remote location (empty for default): "))))
2188 (let ((backend (vc-deduce-backend))
2189 rootdir working-revision)
2190 (unless backend
2191 (error "Buffer is not version controlled"))
2192 (vc-incoming-outgoing-internal backend remote-location "*vc-incoming*" 'log-incoming)))
2194 ;;;###autoload
2195 (defun vc-log-outgoing (&optional remote-location)
2196 "Show a log of changes that will be sent with a push operation to REMOTE-LOCATION.
2197 When called interactively with a prefix argument, prompt for REMOTE-LOCATION."
2198 (interactive
2199 (when current-prefix-arg
2200 (list (read-string "Remote location (empty for default): "))))
2201 (let ((backend (vc-deduce-backend))
2202 rootdir working-revision)
2203 (unless backend
2204 (error "Buffer is not version controlled"))
2205 (vc-incoming-outgoing-internal backend remote-location "*vc-outgoing*" 'log-outgoing)))
2207 ;;;###autoload
2208 (defun vc-revert ()
2209 "Revert working copies of the selected fileset to their repository contents.
2210 This asks for confirmation if the buffer contents are not identical
2211 to the working revision (except for keyword expansion)."
2212 (interactive)
2213 (let* ((vc-fileset (vc-deduce-fileset))
2214 (files (cadr vc-fileset)))
2215 ;; If any of the files is visited by the current buffer, make
2216 ;; sure buffer is saved. If the user says `no', abort since
2217 ;; we cannot show the changes and ask for confirmation to
2218 ;; discard them.
2219 (when (or (not files) (memq (buffer-file-name) files))
2220 (vc-buffer-sync nil))
2221 (dolist (file files)
2222 (let ((buf (get-file-buffer file)))
2223 (when (and buf (buffer-modified-p buf))
2224 (error "Please kill or save all modified buffers before reverting")))
2225 (when (vc-up-to-date-p file)
2226 (unless (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
2227 (error "Revert canceled"))))
2228 (when (vc-diff-internal vc-allow-async-revert vc-fileset nil nil)
2229 (unless (yes-or-no-p
2230 (format "Discard changes in %s? "
2231 (let ((str (vc-delistify files))
2232 (nfiles (length files)))
2233 (if (< (length str) 50)
2235 (format "%d file%s" nfiles
2236 (if (= nfiles 1) "" "s"))))))
2237 (error "Revert canceled"))
2238 (delete-windows-on "*vc-diff*")
2239 (kill-buffer "*vc-diff*"))
2240 (dolist (file files)
2241 (message "Reverting %s..." (vc-delistify files))
2242 (vc-revert-file file)
2243 (message "Reverting %s...done" (vc-delistify files)))))
2245 ;;;###autoload
2246 (defun vc-rollback ()
2247 "Roll back (remove) the most recent changeset committed to the repository.
2248 This may be either a file-level or a repository-level operation,
2249 depending on the underlying version-control system."
2250 (interactive)
2251 (let* ((vc-fileset (vc-deduce-fileset))
2252 (backend (car vc-fileset))
2253 (files (cadr vc-fileset))
2254 (granularity (vc-call-backend backend 'revision-granularity)))
2255 (unless (vc-find-backend-function backend 'rollback)
2256 (error "Rollback is not supported in %s" backend))
2257 (when (and (not (eq granularity 'repository)) (/= (length files) 1))
2258 (error "Rollback requires a singleton fileset or repository versioning"))
2259 ;; FIXME: latest-on-branch-p should take the fileset.
2260 (when (not (vc-call-backend backend 'latest-on-branch-p (car files)))
2261 (error "Rollback is only possible at the tip revision"))
2262 ;; If any of the files is visited by the current buffer, make
2263 ;; sure buffer is saved. If the user says `no', abort since
2264 ;; we cannot show the changes and ask for confirmation to
2265 ;; discard them.
2266 (when (or (not files) (memq (buffer-file-name) files))
2267 (vc-buffer-sync nil))
2268 (dolist (file files)
2269 (when (buffer-modified-p (get-file-buffer file))
2270 (error "Please kill or save all modified buffers before rollback"))
2271 (when (not (vc-up-to-date-p file))
2272 (error "Please revert all modified workfiles before rollback")))
2273 ;; Accumulate changes associated with the fileset
2274 (vc-setup-buffer "*vc-diff*")
2275 (not-modified)
2276 (message "Finding changes...")
2277 (let* ((tip (vc-working-revision (car files)))
2278 ;; FIXME: `previous-revision' should take the fileset.
2279 (previous (vc-call-backend backend 'previous-revision
2280 (car files) tip)))
2281 (vc-diff-internal nil vc-fileset previous tip))
2282 ;; Display changes
2283 (unless (yes-or-no-p "Discard these revisions? ")
2284 (error "Rollback canceled"))
2285 (delete-windows-on "*vc-diff*")
2286 (kill-buffer"*vc-diff*")
2287 ;; Do the actual reversions
2288 (message "Rolling back %s..." (vc-delistify files))
2289 (with-vc-properties
2290 files
2291 (vc-call-backend backend 'rollback files)
2292 `((vc-state . ,'up-to-date)
2293 (vc-checkout-time . , (nth 5 (file-attributes file)))
2294 (vc-working-revision . nil)))
2295 (dolist (f files) (vc-resynch-buffer f t t))
2296 (message "Rolling back %s...done" (vc-delistify files))))
2298 ;;;###autoload
2299 (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
2301 ;;;###autoload
2302 (defun vc-update (&optional arg)
2303 "Update the current fileset or branch.
2304 On a distributed version control system, this runs a \"pull\"
2305 operation to update the current branch, prompting for an argument
2306 list if required. Optional prefix ARG forces a prompt.
2308 On a non-distributed version control system, update the current
2309 fileset to the tip revisions. For each unchanged and unlocked
2310 file, this simply replaces the work file with the latest revision
2311 on its branch. If the file contains changes, any changes in the
2312 tip revision are merged into the working file."
2313 (interactive "P")
2314 (let* ((vc-fileset (vc-deduce-fileset t))
2315 (backend (car vc-fileset))
2316 (files (cadr vc-fileset)))
2317 (cond
2318 ;; If a pull operation is defined, use it.
2319 ((vc-find-backend-function backend 'pull)
2320 (vc-call-backend backend 'pull arg))
2321 ;; If VCS has `merge-news' functionality (CVS and SVN), use it.
2322 ((vc-find-backend-function backend 'merge-news)
2323 (save-some-buffers ; save buffers visiting files
2324 nil (lambda ()
2325 (and (buffer-modified-p)
2326 (let ((file (buffer-file-name)))
2327 (and file (member file files))))))
2328 (dolist (file files)
2329 (if (vc-up-to-date-p file)
2330 (vc-checkout file nil t)
2331 (vc-maybe-resolve-conflicts
2332 file (vc-call-backend backend 'merge-news file)))))
2333 ;; For a locking VCS, check out each file.
2334 ((eq (vc-checkout-model backend files) 'locking)
2335 (dolist (file files)
2336 (if (vc-up-to-date-p file)
2337 (vc-checkout file nil t))))
2339 (error "VC update is unsupported for `%s'" backend)))))
2341 ;;;###autoload
2342 (defalias 'vc-pull 'vc-update)
2344 (defun vc-version-backup-file (file &optional rev)
2345 "Return name of backup file for revision REV of FILE.
2346 If version backups should be used for FILE, and there exists
2347 such a backup for REV or the working revision of file, return
2348 its name; otherwise return nil."
2349 (when (vc-call make-version-backups-p file)
2350 (let ((backup-file (vc-version-backup-file-name file rev)))
2351 (if (file-exists-p backup-file)
2352 backup-file
2353 ;; there is no automatic backup, but maybe the user made one manually
2354 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2355 (when (file-exists-p backup-file)
2356 backup-file)))))
2358 (defun vc-revert-file (file)
2359 "Revert FILE back to the repository working revision it was based on."
2360 (with-vc-properties
2361 (list file)
2362 (let ((backup-file (vc-version-backup-file file)))
2363 (when backup-file
2364 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2365 (vc-delete-automatic-version-backups file))
2366 (vc-call revert file backup-file))
2367 `((vc-state . up-to-date)
2368 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2369 (vc-resynch-buffer file t t))
2371 ;;;###autoload
2372 (defun vc-switch-backend (file backend)
2373 "Make BACKEND the current version control system for FILE.
2374 FILE must already be registered in BACKEND. The change is not
2375 permanent, only for the current session. This function only changes
2376 VC's perspective on FILE, it does not register or unregister it.
2377 By default, this command cycles through the registered backends.
2378 To get a prompt, use a prefix argument."
2379 (interactive
2380 (list
2381 (or buffer-file-name
2382 (error "There is no version-controlled file in this buffer"))
2383 (let ((crt-bk (vc-backend buffer-file-name))
2384 (backends nil))
2385 (unless crt-bk
2386 (error "File %s is not under version control" buffer-file-name))
2387 ;; Find the registered backends.
2388 (dolist (crt vc-handled-backends)
2389 (when (and (vc-call-backend crt 'registered buffer-file-name)
2390 (not (eq crt-bk crt)))
2391 (push crt backends)))
2392 ;; Find the next backend.
2393 (let ((def (car backends))
2394 (others backends))
2395 (cond
2396 ((null others) (error "No other backend to switch to"))
2397 (current-prefix-arg
2398 (intern
2399 (upcase
2400 (completing-read
2401 (format "Switch to backend [%s]: " def)
2402 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2403 nil t nil nil (downcase (symbol-name def))))))
2404 (t def))))))
2405 (unless (eq backend (vc-backend file))
2406 (vc-file-clearprops file)
2407 (vc-file-setprop file 'vc-backend backend)
2408 ;; Force recomputation of the state
2409 (unless (vc-call-backend backend 'registered file)
2410 (vc-file-clearprops file)
2411 (error "%s is not registered in %s" file backend))
2412 (vc-mode-line file)))
2414 ;;;###autoload
2415 (defun vc-transfer-file (file new-backend)
2416 "Transfer FILE to another version control system NEW-BACKEND.
2417 If NEW-BACKEND has a higher precedence than FILE's current backend
2418 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2419 NEW-BACKEND, using the revision number from the current backend as the
2420 base level. If NEW-BACKEND has a lower precedence than the current
2421 backend, then commit all changes that were made under the current
2422 backend to NEW-BACKEND, and unregister FILE from the current backend.
2423 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2424 (let* ((old-backend (vc-backend file))
2425 (edited (memq (vc-state file) '(edited needs-merge)))
2426 (registered (vc-call-backend new-backend 'registered file))
2427 (move
2428 (and registered ; Never move if not registered in new-backend yet.
2429 ;; move if new-backend comes later in vc-handled-backends
2430 (or (memq new-backend (memq old-backend vc-handled-backends))
2431 (y-or-n-p "Final transfer? "))))
2432 (comment nil))
2433 (when (eq old-backend new-backend)
2434 (error "%s is the current backend of %s" new-backend file))
2435 (if registered
2436 (set-file-modes file (logior (file-modes file) 128))
2437 ;; `registered' might have switched under us.
2438 (vc-switch-backend file old-backend)
2439 (let* ((rev (vc-working-revision file))
2440 (modified-file (and edited (make-temp-file file)))
2441 (unmodified-file (and modified-file (vc-version-backup-file file))))
2442 ;; Go back to the base unmodified file.
2443 (unwind-protect
2444 (progn
2445 (when modified-file
2446 (copy-file file modified-file 'ok-if-already-exists)
2447 ;; If we have a local copy of the unmodified file, handle that
2448 ;; here and not in vc-revert-file because we don't want to
2449 ;; delete that copy -- it is still useful for OLD-BACKEND.
2450 (if unmodified-file
2451 (copy-file unmodified-file file
2452 'ok-if-already-exists 'keep-date)
2453 (when (y-or-n-p "Get base revision from repository? ")
2454 (vc-revert-file file))))
2455 (vc-call-backend new-backend 'receive-file file rev))
2456 (when modified-file
2457 (vc-switch-backend file new-backend)
2458 (unless (eq (vc-checkout-model new-backend (list file)) 'implicit)
2459 (vc-checkout file t nil))
2460 (rename-file modified-file file 'ok-if-already-exists)
2461 (vc-file-setprop file 'vc-checkout-time nil)))))
2462 (when move
2463 (vc-switch-backend file old-backend)
2464 (setq comment (vc-call-backend old-backend 'comment-history file))
2465 (vc-call-backend old-backend 'unregister file))
2466 (vc-switch-backend file new-backend)
2467 (when (or move edited)
2468 (vc-file-setprop file 'vc-state 'edited)
2469 (vc-mode-line file new-backend)
2470 (vc-checkin file new-backend nil comment (stringp comment)))))
2472 (defun vc-rename-master (oldmaster newfile templates)
2473 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2474 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2475 (newdir (or (file-name-directory newfile) ""))
2476 (newbase (file-name-nondirectory newfile))
2477 (masters
2478 ;; List of potential master files for `newfile'
2479 (mapcar
2480 (lambda (s) (vc-possible-master s newdir newbase))
2481 templates)))
2482 (when (or (file-symlink-p oldmaster)
2483 (file-symlink-p (file-name-directory oldmaster)))
2484 (error "This is unsafe in the presence of symbolic links"))
2485 (rename-file
2486 oldmaster
2487 (catch 'found
2488 ;; If possible, keep the master file in the same directory.
2489 (dolist (f masters)
2490 (when (and f (string= (file-name-directory (expand-file-name f)) dir))
2491 (throw 'found f)))
2492 ;; If not, just use the first possible place.
2493 (dolist (f masters)
2494 (and f (or (not (setq dir (file-name-directory f)))
2495 (file-directory-p dir))
2496 (throw 'found f)))
2497 (error "New file lacks a version control directory")))))
2499 ;;;###autoload
2500 (defun vc-delete-file (file)
2501 "Delete file and mark it as such in the version control system."
2502 (interactive "fVC delete file: ")
2503 (setq file (expand-file-name file))
2504 (let ((buf (get-file-buffer file))
2505 (backend (vc-backend file)))
2506 (unless backend
2507 (error "File %s is not under version control"
2508 (file-name-nondirectory file)))
2509 (unless (vc-find-backend-function backend 'delete-file)
2510 (error "Deleting files under %s is not supported in VC" backend))
2511 (when (and buf (buffer-modified-p buf))
2512 (error "Please save or undo your changes before deleting %s" file))
2513 (let ((state (vc-state file)))
2514 (when (eq state 'edited)
2515 (error "Please commit or undo your changes before deleting %s" file))
2516 (when (eq state 'conflict)
2517 (error "Please resolve the conflicts before deleting %s" file)))
2518 (unless (y-or-n-p (format "Really want to delete %s? "
2519 (file-name-nondirectory file)))
2520 (error "Abort!"))
2521 (unless (or (file-directory-p file) (null make-backup-files)
2522 (not (file-exists-p file)))
2523 (with-current-buffer (or buf (find-file-noselect file))
2524 (let ((backup-inhibited nil))
2525 (backup-buffer))))
2526 ;; Bind `default-directory' so that the command that the backend
2527 ;; runs to remove the file is invoked in the correct context.
2528 (let ((default-directory (file-name-directory file)))
2529 (vc-call-backend backend 'delete-file file))
2530 ;; If the backend hasn't deleted the file itself, let's do it for him.
2531 (when (file-exists-p file) (delete-file file))
2532 ;; Forget what VC knew about the file.
2533 (vc-file-clearprops file)
2534 ;; Make sure the buffer is deleted and the *vc-dir* buffers are
2535 ;; updated after this.
2536 (vc-resynch-buffer file nil t)))
2538 ;;;###autoload
2539 (defun vc-rename-file (old new)
2540 "Rename file OLD to NEW in both work area and repository."
2541 (interactive "fVC rename file: \nFRename to: ")
2542 ;; in CL I would have said (setq new (merge-pathnames new old))
2543 (let ((old-base (file-name-nondirectory old)))
2544 (when (and (not (string= "" old-base))
2545 (string= "" (file-name-nondirectory new)))
2546 (setq new (concat new old-base))))
2547 (let ((oldbuf (get-file-buffer old)))
2548 (when (and oldbuf (buffer-modified-p oldbuf))
2549 (error "Please save files before moving them"))
2550 (when (get-file-buffer new)
2551 (error "Already editing new file name"))
2552 (when (file-exists-p new)
2553 (error "New file already exists"))
2554 (let ((state (vc-state old)))
2555 (unless (memq state '(up-to-date edited))
2556 (error "Please %s files before moving them"
2557 (if (stringp state) "check in" "update"))))
2558 (vc-call rename-file old new)
2559 (vc-file-clearprops old)
2560 ;; Move the actual file (unless the backend did it already)
2561 (when (file-exists-p old) (rename-file old new))
2562 ;; ?? Renaming a file might change its contents due to keyword expansion.
2563 ;; We should really check out a new copy if the old copy was precisely equal
2564 ;; to some checked-in revision. However, testing for this is tricky....
2565 (when oldbuf
2566 (with-current-buffer oldbuf
2567 (let ((buffer-read-only buffer-read-only))
2568 (set-visited-file-name new))
2569 (vc-mode-line new (vc-backend new))
2570 (set-buffer-modified-p nil)))))
2572 ;;;###autoload
2573 (defun vc-update-change-log (&rest args)
2574 "Find change log file and add entries from recent version control logs.
2575 Normally, find log entries for all registered files in the default
2576 directory.
2578 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2580 With any numeric prefix arg, find log entries for all currently visited
2581 files that are under version control. This puts all the entries in the
2582 log for the default directory, which may not be appropriate.
2584 From a program, any ARGS are assumed to be filenames for which
2585 log entries should be gathered."
2586 (interactive
2587 (cond ((consp current-prefix-arg) ;C-u
2588 (list buffer-file-name))
2589 (current-prefix-arg ;Numeric argument.
2590 (let ((files nil)
2591 (buffers (buffer-list))
2592 file)
2593 (while buffers
2594 (setq file (buffer-file-name (car buffers)))
2595 (and file (vc-backend file)
2596 (setq files (cons file files)))
2597 (setq buffers (cdr buffers)))
2598 files))
2600 ;; Don't supply any filenames to backend; this means
2601 ;; it should find all relevant files relative to
2602 ;; the default-directory.
2603 nil)))
2604 (vc-call-backend (vc-responsible-backend default-directory)
2605 'update-changelog args))
2607 ;; functions that operate on RCS revision numbers. This code should
2608 ;; also be moved into the backends. It stays for now, however, since
2609 ;; it is used in code below.
2610 (defun vc-branch-p (rev)
2611 "Return t if REV is a branch revision."
2612 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
2614 ;;;###autoload
2615 (defun vc-branch-part (rev)
2616 "Return the branch part of a revision number REV."
2617 (let ((index (string-match "\\.[0-9]+\\'" rev)))
2618 (when index
2619 (substring rev 0 index))))
2621 (define-obsolete-function-alias
2622 'vc-default-previous-version 'vc-default-previous-revision "23.1")
2624 (defun vc-default-responsible-p (backend file)
2625 "Indicate whether BACKEND is reponsible for FILE.
2626 The default is to return nil always."
2627 nil)
2629 (defun vc-default-could-register (backend file)
2630 "Return non-nil if BACKEND could be used to register FILE.
2631 The default implementation returns t for all files."
2634 (defun vc-default-latest-on-branch-p (backend file)
2635 "Return non-nil if FILE is the latest on its branch.
2636 This default implementation always returns non-nil, which means that
2637 editing non-current revisions is not supported by default."
2640 (defun vc-default-init-revision (backend) vc-default-init-revision)
2642 (defun vc-default-find-revision (backend file rev buffer)
2643 "Provide the new `find-revision' op based on the old `checkout' op.
2644 This is only for compatibility with old backends. They should be updated
2645 to provide the `find-revision' operation instead."
2646 (let ((tmpfile (make-temp-file (expand-file-name file))))
2647 (unwind-protect
2648 (progn
2649 (vc-call-backend backend 'checkout file nil rev tmpfile)
2650 (with-current-buffer buffer
2651 (insert-file-contents-literally tmpfile)))
2652 (delete-file tmpfile))))
2654 (defun vc-default-rename-file (backend old new)
2655 (condition-case nil
2656 (add-name-to-file old new)
2657 (error (rename-file old new)))
2658 (vc-delete-file old)
2659 (with-current-buffer (find-file-noselect new)
2660 (vc-register)))
2662 (defalias 'vc-default-check-headers 'ignore)
2664 (declare-function log-edit-mode "log-edit" ())
2666 (defun vc-default-log-edit-mode (backend) (log-edit-mode))
2668 (defun vc-default-log-view-mode (backend) (log-view-mode))
2670 (defun vc-default-show-log-entry (backend rev)
2671 (with-no-warnings
2672 (log-view-goto-rev rev)))
2674 (defun vc-default-comment-history (backend file)
2675 "Return a string with all log entries stored in BACKEND for FILE."
2676 (when (vc-find-backend-function backend 'print-log)
2677 (with-current-buffer "*vc*"
2678 (vc-call-backend backend 'print-log (list file))
2679 (buffer-string))))
2681 (defun vc-default-receive-file (backend file rev)
2682 "Let BACKEND receive FILE from another version control system."
2683 (vc-call-backend backend 'register (list file) rev ""))
2685 (defun vc-default-retrieve-tag (backend dir name update)
2686 (if (string= name "")
2687 (progn
2688 (vc-file-tree-walk
2690 (lambda (f) (and
2691 (vc-up-to-date-p f)
2692 (vc-error-occurred
2693 (vc-call-backend backend 'checkout f nil "")
2694 (when update (vc-resynch-buffer f t t)))))))
2695 (let ((result (vc-tag-precondition dir)))
2696 (if (stringp result)
2697 (error "File %s is locked" result)
2698 (setq update (and (eq result 'visited) update))
2699 (vc-file-tree-walk
2701 (lambda (f) (vc-error-occurred
2702 (vc-call-backend backend 'checkout f nil name)
2703 (when update (vc-resynch-buffer f t t)))))))))
2705 (defun vc-default-revert (backend file contents-done)
2706 (unless contents-done
2707 (let ((rev (vc-working-revision file))
2708 (file-buffer (or (get-file-buffer file) (current-buffer))))
2709 (message "Checking out %s..." file)
2710 (let ((failed t)
2711 (backup-name (car (find-backup-file-name file))))
2712 (when backup-name
2713 (copy-file file backup-name 'ok-if-already-exists 'keep-date)
2714 (unless (file-writable-p file)
2715 (set-file-modes file (logior (file-modes file) 128))))
2716 (unwind-protect
2717 (let ((coding-system-for-read 'no-conversion)
2718 (coding-system-for-write 'no-conversion))
2719 (with-temp-file file
2720 (let ((outbuf (current-buffer)))
2721 ;; Change buffer to get local value of vc-checkout-switches.
2722 (with-current-buffer file-buffer
2723 (let ((default-directory (file-name-directory file)))
2724 (vc-call-backend backend 'find-revision
2725 file rev outbuf)))))
2726 (setq failed nil))
2727 (when backup-name
2728 (if failed
2729 (rename-file backup-name file 'ok-if-already-exists)
2730 (and (not vc-make-backup-files) (delete-file backup-name))))))
2731 (message "Checking out %s...done" file))))
2733 (defalias 'vc-default-revision-completion-table 'ignore)
2734 (defalias 'vc-default-mark-resolved 'ignore)
2736 (defun vc-default-dir-status-files (backend dir files default-state update-function)
2737 (funcall update-function
2738 (mapcar (lambda (file) (list file default-state)) files)))
2740 (defun vc-check-headers ()
2741 "Check if the current file has any headers in it."
2742 (interactive)
2743 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
2747 ;; These things should probably be generally available
2749 (defun vc-string-prefix-p (prefix string)
2750 (let ((lpref (length prefix)))
2751 (and (>= (length string) lpref)
2752 (eq t (compare-strings prefix nil nil string nil lpref)))))
2754 (defun vc-file-tree-walk (dirname func &rest args)
2755 "Walk recursively through DIRNAME.
2756 Invoke FUNC f ARGS on each VC-managed file f underneath it."
2757 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
2758 (message "Traversing directory %s...done" dirname))
2760 (defun vc-file-tree-walk-internal (file func args)
2761 (if (not (file-directory-p file))
2762 (when (vc-backend file) (apply func file args))
2763 (message "Traversing directory %s..." (abbreviate-file-name file))
2764 (let ((dir (file-name-as-directory file)))
2765 (mapcar
2766 (lambda (f) (or
2767 (string-equal f ".")
2768 (string-equal f "..")
2769 (member f vc-directory-exclusion-list)
2770 (let ((dirf (expand-file-name f dir)))
2772 (file-symlink-p dirf) ;; Avoid possible loops.
2773 (vc-file-tree-walk-internal dirf func args)))))
2774 (directory-files dir)))))
2776 (provide 'vc)
2778 ;;; vc.el ends here