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