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