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