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