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