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