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