#
[bpt/emacs.git] / lisp / vc.el
1 ;;; vc.el --- drive a version-control system from within Emacs
2
3 ;; Copyright (C) 1992,93,94,95,96,97,98,2000,01,2003
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: FSF (see below for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; Keywords: tools
9
10 ;; $Id: vc.el,v 1.364 2004/01/22 23:34:33 uid65624 Exp $
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Credits:
30
31 ;; VC was initially designed and implemented by Eric S. Raymond
32 ;; <esr@snark.thyrsus.com>. Over the years, many people have
33 ;; contributed substantial amounts of work to VC. These include:
34 ;; Per Cederqvist <ceder@lysator.liu.se>
35 ;; Paul Eggert <eggert@twinsun.com>
36 ;; Sebastian Kremer <sk@thp.uni-koeln.de>
37 ;; Martin Lorentzson <martinl@gnu.org>
38 ;; Dave Love <fx@gnu.org>
39 ;; Stefan Monnier <monnier@cs.yale.edu>
40 ;; J.D. Smith <jdsmith@alum.mit.edu>
41 ;; Andre Spiegel <spiegel@gnu.org>
42 ;; Richard Stallman <rms@gnu.org>
43 ;; Thien-Thi Nguyen <ttn@gnu.org>
44
45 ;;; Commentary:
46
47 ;; This mode is fully documented in the Emacs user's manual.
48 ;;
49 ;; Supported version-control systems presently include SCCS, RCS, and CVS.
50 ;;
51 ;; Some features will not work with old RCS versions. Where
52 ;; appropriate, VC finds out which version you have, and allows or
53 ;; disallows those features (stealing locks, for example, works only
54 ;; from 5.6.2 onwards).
55 ;; Even initial checkins will fail if your RCS version is so old that ci
56 ;; doesn't understand -t-; this has been known to happen to people running
57 ;; NExTSTEP 3.0.
58 ;;
59 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
60 ;;
61 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
62 ;; to be installed somewhere on Emacs's path for executables.
63 ;;
64 ;; If your site uses the ChangeLog convention supported by Emacs, the
65 ;; function log-edit-comment-to-change-log could prove a useful checkin hook,
66 ;; although you might prefer to use C-c C-a (i.e. log-edit-insert-changelog)
67 ;; from the commit buffer instead or to set `log-edit-setup-invert'.
68 ;;
69 ;; The vc code maintains some internal state in order to reduce expensive
70 ;; version-control operations to a minimum. Some names are only computed
71 ;; once. If you perform version control operations with RCS/SCCS/CVS while
72 ;; vc's back is turned, or move/rename master files while vc is running,
73 ;; vc may get seriously confused. Don't do these things!
74 ;;
75 ;; Developer's notes on some concurrency issues are included at the end of
76 ;; the file.
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-workfile-version' should compute the workfile version 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 ;; STATE-QUERYING FUNCTIONS
104 ;;
105 ;; * registered (file)
106 ;;
107 ;; Return non-nil if FILE is registered in this backend.
108 ;;
109 ;; * state (file)
110 ;;
111 ;; Return the current version control state of FILE. For a list of
112 ;; possible values, see `vc-state'. This function should do a full and
113 ;; reliable state computation; it is usually called immediately after
114 ;; C-x v v. If you want to use a faster heuristic when visiting a
115 ;; file, put that into `state-heuristic' below.
116 ;;
117 ;; - state-heuristic (file)
118 ;;
119 ;; If provided, this function is used to estimate the version control
120 ;; state of FILE at visiting time. It should be considerably faster
121 ;; than the implementation of `state'. For a list of possible values,
122 ;; see the doc string of `vc-state'.
123 ;;
124 ;; - dir-state (dir)
125 ;;
126 ;; If provided, this function is used to find the version control state
127 ;; of all files in DIR in a fast way. The function should not return
128 ;; anything, but rather store the files' states into the corresponding
129 ;; `vc-state' properties.
130 ;;
131 ;; * workfile-version (file)
132 ;;
133 ;; Return the current workfile version of FILE.
134 ;;
135 ;; - latest-on-branch-p (file)
136 ;;
137 ;; Return non-nil if the current workfile version of FILE is the latest
138 ;; on its branch. The default implementation always returns t, which
139 ;; means that working with non-current versions is not supported by
140 ;; default.
141 ;;
142 ;; * checkout-model (file)
143 ;;
144 ;; Indicate whether FILE needs to be "checked out" before it can be
145 ;; edited. See `vc-checkout-model' for a list of possible values.
146 ;;
147 ;; - workfile-unchanged-p (file)
148 ;;
149 ;; Return non-nil if FILE is unchanged from its current workfile
150 ;; version. This function should do a brief comparison of FILE's
151 ;; contents with those of the master version. If the backend does not
152 ;; have such a brief-comparison feature, the default implementation of
153 ;; this function can be used, which delegates to a full
154 ;; vc-BACKEND-diff. (Note that vc-BACKEND-diff must not run
155 ;; asynchronously in this case.)
156 ;;
157 ;; - mode-line-string (file)
158 ;;
159 ;; If provided, this function should return the VC-specific mode line
160 ;; string for FILE. The default implementation deals well with all
161 ;; states that `vc-state' can return.
162 ;;
163 ;; - dired-state-info (file)
164 ;;
165 ;; Translate the `vc-state' property of FILE into a string that can be
166 ;; used in a vc-dired buffer. The default implementation deals well
167 ;; with all states that `vc-state' can return.
168 ;;
169 ;; STATE-CHANGING FUNCTIONS
170 ;;
171 ;; * register (file &optional rev comment)
172 ;;
173 ;; Register FILE in this backend. Optionally, an initial revision REV
174 ;; and an initial description of the file, COMMENT, may be specified.
175 ;; The implementation should pass the value of vc-register-switches
176 ;; to the backend command.
177 ;;
178 ;; - init-version (file)
179 ;;
180 ;; The initial version to use when registering FILE if one is not
181 ;; specified by the user. If not provided, the variable
182 ;; vc-default-init-version is used instead.
183 ;;
184 ;; - responsible-p (file)
185 ;;
186 ;; Return non-nil if this backend considers itself "responsible" for
187 ;; FILE, which can also be a directory. This function is used to find
188 ;; out what backend to use for registration of new files and for things
189 ;; like change log generation. The default implementation always
190 ;; returns nil.
191 ;;
192 ;; - could-register (file)
193 ;;
194 ;; Return non-nil if FILE could be registered under this backend. The
195 ;; default implementation always returns t.
196 ;;
197 ;; - receive-file (file rev)
198 ;;
199 ;; Let this backend "receive" a file that is already registered under
200 ;; another backend. The default implementation simply calls `register'
201 ;; for FILE, but it can be overridden to do something more specific,
202 ;; e.g. keep revision numbers consistent or choose editing modes for
203 ;; FILE that resemble those of the other backend.
204 ;;
205 ;; - unregister (file)
206 ;;
207 ;; Unregister FILE from this backend. This is only needed if this
208 ;; backend may be used as a "more local" backend for temporary editing.
209 ;;
210 ;; * checkin (file rev comment)
211 ;;
212 ;; Commit changes in FILE to this backend. If REV is non-nil, that
213 ;; should become the new revision number. COMMENT is used as a
214 ;; check-in comment. The implementation should pass the value of
215 ;; vc-checkin-switches to the backend command.
216 ;;
217 ;; * find-version (file rev buffer)
218 ;;
219 ;; Fetch revision REV of file FILE and put it into BUFFER.
220 ;; If REV is the empty string, fetch the head of the trunk.
221 ;; The implementation should pass the value of vc-checkout-switches
222 ;; to the backend command.
223 ;;
224 ;; * checkout (file &optional editable rev)
225 ;;
226 ;; Check out revision REV of FILE into the working area. If EDITABLE
227 ;; is non-nil, FILE should be writable by the user and if locking is
228 ;; used for FILE, a lock should also be set. If REV is non-nil, that
229 ;; is the revision to check out (default is current workfile version).
230 ;; If REV is t, that means to check out the head of the current branch;
231 ;; if it is the empty string, check out the head of the trunk.
232 ;; The implementation should pass the value of vc-checkout-switches
233 ;; to the backend command.
234 ;;
235 ;; * revert (file &optional contents-done)
236 ;;
237 ;; Revert FILE back to the current workfile version. If optional
238 ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
239 ;; already been reverted from a version backup, and this function
240 ;; only needs to update the status of FILE within the backend.
241 ;;
242 ;; - cancel-version (file editable)
243 ;;
244 ;; Cancel the current workfile version of FILE, i.e. remove it from the
245 ;; master. EDITABLE non-nil means that FILE should be writable
246 ;; afterwards, and if locking is used for FILE, then a lock should also
247 ;; be set. If this function is not provided, trying to cancel a
248 ;; version is caught as an error.
249 ;;
250 ;; - merge (file rev1 rev2)
251 ;;
252 ;; Merge the changes between REV1 and REV2 into the current working file.
253 ;;
254 ;; - merge-news (file)
255 ;;
256 ;; Merge recent changes from the current branch into FILE.
257 ;;
258 ;; - steal-lock (file &optional version)
259 ;;
260 ;; Steal any lock on the current workfile version of FILE, or on
261 ;; VERSION if that is provided. This function is only needed if
262 ;; locking is used for files under this backend, and if files can
263 ;; indeed be locked by other users.
264 ;;
265 ;; HISTORY FUNCTIONS
266 ;;
267 ;; * print-log (file)
268 ;;
269 ;; Insert the revision log of FILE into the *vc* buffer.
270 ;;
271 ;; - show-log-entry (version)
272 ;;
273 ;; If provided, search the log entry for VERSION in the current buffer,
274 ;; and make sure it is displayed in the buffer's window. The default
275 ;; implementation of this function works for RCS-style logs.
276 ;;
277 ;; - wash-log (file)
278 ;;
279 ;; Remove all non-comment information from the output of print-log. The
280 ;; default implementation of this function works for RCS-style logs.
281 ;;
282 ;; - logentry-check ()
283 ;;
284 ;; If defined, this function is run to find out whether the user
285 ;; entered a valid log entry for check-in. The log entry is in the
286 ;; current buffer, and if it is not a valid one, the function should
287 ;; throw an error.
288 ;;
289 ;; - comment-history (file)
290 ;;
291 ;; Return a string containing all log entries that were made for FILE.
292 ;; This is used for transferring a file from one backend to another,
293 ;; retaining comment information. The default implementation of this
294 ;; function does this by calling print-log and then wash-log, and
295 ;; returning the resulting buffer contents as a string.
296 ;;
297 ;; - update-changelog (files)
298 ;;
299 ;; Using recent log entries, create ChangeLog entries for FILES, or for
300 ;; all files at or below the default-directory if FILES is nil. The
301 ;; default implementation runs rcs2log, which handles RCS- and
302 ;; CVS-style logs.
303 ;;
304 ;; * diff (file &optional rev1 rev2)
305 ;;
306 ;; Insert the diff for FILE into the *vc-diff* buffer. If REV1 and
307 ;; REV2 are non-nil, report differences from REV1 to REV2. If REV1
308 ;; is nil, use the current workfile version (as found in the
309 ;; repository) as the older version; if REV2 is nil, use the current
310 ;; workfile contents as the newer version. This function should
311 ;; pass the value of (vc-switches BACKEND 'diff) to the backend
312 ;; command. It should return a status of either 0 (no differences
313 ;; found), or 1 (either non-empty diff or the diff is run
314 ;; asynchronously).
315 ;;
316 ;; - diff-tree (dir &optional rev1 rev2)
317 ;;
318 ;; Insert the diff for all files at and below DIR into the *vc-diff*
319 ;; buffer. The meaning of REV1 and REV2 is the same as for
320 ;; vc-BACKEND-diff. The default implementation does an explicit tree
321 ;; walk, calling vc-BACKEND-diff for each individual file.
322 ;;
323 ;; - annotate-command (file buf rev)
324 ;;
325 ;; If this function is provided, it should produce an annotated version
326 ;; of FILE in BUF, relative to version REV. This is currently only
327 ;; implemented for CVS, using the `cvs annotate' command.
328 ;;
329 ;; - annotate-time ()
330 ;;
331 ;; Only required if `annotate-command' is defined for the backend.
332 ;; Return the time of the next line of annotation at or after point,
333 ;; as a floating point fractional number of days. The helper
334 ;; function `vc-annotate-convert-time' may be useful for converting
335 ;; multi-part times as returned by `current-time' and `encode-time'
336 ;; to this format. Return nil if no more lines of annotation appear
337 ;; in the buffer. You can safely assume that point is placed at the
338 ;; beginning of each line, starting at `point-min'. The buffer that
339 ;; point is placed in is the Annotate output, as defined by the
340 ;; relevant backend.
341 ;;
342 ;; - annotate-current-time ()
343 ;;
344 ;; Only required if `annotate-command' is defined for the backend,
345 ;; AND you'd like the current time considered to be anything besides
346 ;; (vs-annotate-convert-time (current-time)) -- i.e. the current
347 ;; time with hours, minutes, and seconds included. Probably safe to
348 ;; ignore. Return the current-time, in units of fractional days.
349 ;;
350 ;; - annotate-extract-revision-at-line ()
351 ;;
352 ;; Only required if `annotate-command' is defined for the backend.
353 ;; Invoked from a buffer in vc-annotate-mode, return the revision
354 ;; corresponding to the current line, or nil if there is no revision
355 ;; corresponding to the current line.
356 ;;
357 ;; SNAPSHOT SYSTEM
358 ;;
359 ;; - create-snapshot (dir name branchp)
360 ;;
361 ;; Take a snapshot of the current state of files under DIR and name it
362 ;; NAME. This should make sure that files are up-to-date before
363 ;; proceeding with the action. DIR can also be a file and if BRANCHP
364 ;; is specified, NAME should be created as a branch and DIR should be
365 ;; checked out under this new branch. The default implementation does
366 ;; not support branches but does a sanity check, a tree traversal and
367 ;; for each file calls `assign-name'.
368 ;;
369 ;; - assign-name (file name)
370 ;;
371 ;; Give name NAME to the current version of FILE, assuming it is
372 ;; up-to-date. Only used by the default version of `create-snapshot'.
373 ;;
374 ;; - retrieve-snapshot (dir name update)
375 ;;
376 ;; Retrieve a named snapshot of all registered files at or below DIR.
377 ;; If UPDATE is non-nil, then update buffers of any files in the
378 ;; snapshot that are currently visited. The default implementation
379 ;; does a sanity check whether there aren't any uncommitted changes at
380 ;; or below DIR, and then performs a tree walk, using the `checkout'
381 ;; function to retrieve the corresponding versions.
382 ;;
383 ;; MISCELLANEOUS
384 ;;
385 ;; - make-version-backups-p (file)
386 ;;
387 ;; Return non-nil if unmodified repository versions of FILE should be
388 ;; backed up locally. If this is done, VC can perform `diff' and
389 ;; `revert' operations itself, without calling the backend system. The
390 ;; default implementation always returns nil.
391 ;;
392 ;; - repository-hostname (dirname)
393 ;;
394 ;; Return the hostname that the backend will have to contact
395 ;; in order to operate on a file in DIRNAME. If the return value
396 ;; is nil, it is means that the repository is local.
397 ;; This function is used in `vc-stay-local-p' which backends can use
398 ;; for their convenience.
399 ;;
400 ;; - previous-version (file rev)
401 ;;
402 ;; Return the version number that precedes REV for FILE, or nil if no such
403 ;; version exists.
404 ;;
405 ;; - next-version (file rev)
406 ;;
407 ;; Return the version number that follows REV for FILE, or nil if no such
408 ;; version exists.
409 ;;
410 ;; - check-headers ()
411 ;;
412 ;; Return non-nil if the current buffer contains any version headers.
413 ;;
414 ;; - clear-headers ()
415 ;;
416 ;; In the current buffer, reset all version headers to their unexpanded
417 ;; form. This function should be provided if the state-querying code
418 ;; for this backend uses the version headers to determine the state of
419 ;; a file. This function will then be called whenever VC changes the
420 ;; version control state in such a way that the headers would give
421 ;; wrong information.
422 ;;
423 ;; - delete-file (file)
424 ;;
425 ;; Delete FILE and mark it as deleted in the repository. If this
426 ;; function is not provided, the command `vc-delete-file' will
427 ;; signal an error.
428 ;;
429 ;; - rename-file (old new)
430 ;;
431 ;; Rename file OLD to NEW, both in the working area and in the
432 ;; repository. If this function is not provided, the renaming
433 ;; will be done by (vc-delete-file old) and (vc-register new).
434 ;;
435
436 ;;; Code:
437
438 (require 'vc-hooks)
439 (require 'ring)
440 (eval-when-compile
441 (require 'cl)
442 (require 'compile)
443 (require 'dired) ; for dired-map-over-marks macro
444 (require 'dired-aux)) ; for dired-kill-{line,tree}
445
446 (if (not (assoc 'vc-parent-buffer minor-mode-alist))
447 (setq minor-mode-alist
448 (cons '(vc-parent-buffer vc-parent-buffer-name)
449 minor-mode-alist)))
450
451 ;; General customization
452
453 (defgroup vc nil
454 "Version-control system in Emacs."
455 :group 'tools)
456
457 (defcustom vc-suppress-confirm nil
458 "*If non-nil, treat user as expert; suppress yes-no prompts on some things."
459 :type 'boolean
460 :group 'vc)
461
462 (defcustom vc-delete-logbuf-window t
463 "*If non-nil, delete the *VC-log* buffer and window after each logical action.
464 If nil, bury that buffer instead.
465 This is most useful if you have multiple windows on a frame and would like to
466 preserve the setting."
467 :type 'boolean
468 :group 'vc)
469
470 (defcustom vc-initial-comment nil
471 "*If non-nil, prompt for initial comment when a file is registered."
472 :type 'boolean
473 :group 'vc)
474
475 (defcustom vc-default-init-version "1.1"
476 "*A string used as the default version number when a new file is registered.
477 This can be overridden by giving a prefix argument to \\[vc-register]. This
478 can also be overridden by a particular VC backend."
479 :type 'string
480 :group 'vc
481 :version "20.3")
482
483 (defcustom vc-command-messages nil
484 "*If non-nil, display run messages from back-end commands."
485 :type 'boolean
486 :group 'vc)
487
488 (defcustom vc-checkin-switches nil
489 "*A string or list of strings specifying extra switches for checkin.
490 These are passed to the checkin program by \\[vc-checkin]."
491 :type '(choice (const :tag "None" nil)
492 (string :tag "Argument String")
493 (repeat :tag "Argument List"
494 :value ("")
495 string))
496 :group 'vc)
497
498 (defcustom vc-checkout-switches nil
499 "*A string or list of strings specifying extra switches for checkout.
500 These are passed to the checkout program by \\[vc-checkout]."
501 :type '(choice (const :tag "None" nil)
502 (string :tag "Argument String")
503 (repeat :tag "Argument List"
504 :value ("")
505 string))
506 :group 'vc)
507
508 (defcustom vc-register-switches nil
509 "*A string or list of strings; extra switches for registering a file.
510 These are passed to the checkin program by \\[vc-register]."
511 :type '(choice (const :tag "None" nil)
512 (string :tag "Argument String")
513 (repeat :tag "Argument List"
514 :value ("")
515 string))
516 :group 'vc)
517
518 (defcustom vc-dired-listing-switches "-al"
519 "*Switches passed to `ls' for vc-dired. MUST contain the `l' option."
520 :type 'string
521 :group 'vc
522 :version "21.1")
523
524 (defcustom vc-dired-recurse t
525 "*If non-nil, show directory trees recursively in VC Dired."
526 :type 'boolean
527 :group 'vc
528 :version "20.3")
529
530 (defcustom vc-dired-terse-display t
531 "*If non-nil, show only locked files in VC Dired."
532 :type 'boolean
533 :group 'vc
534 :version "20.3")
535
536 (defcustom vc-directory-exclusion-list '("SCCS" "RCS" "CVS" "MCVS" ".svn")
537 "*List of directory names to be ignored when walking directory trees."
538 :type '(repeat string)
539 :group 'vc)
540
541 (defcustom vc-diff-switches nil
542 "*A string or list of strings specifying switches for diff under VC.
543 When running diff under a given BACKEND, VC concatenates the values of
544 `diff-switches', `vc-diff-switches', and `vc-BACKEND-diff-switches' to
545 get the switches for that command. Thus, `vc-diff-switches' should
546 contain switches that are specific to version control, but not
547 specific to any particular backend."
548 :type '(choice (const :tag "None" nil)
549 (string :tag "Argument String")
550 (repeat :tag "Argument List"
551 :value ("")
552 string))
553 :group 'vc
554 :version "21.1")
555
556 ;;;###autoload
557 (defcustom vc-checkout-hook nil
558 "*Normal hook (list of functions) run after checking out a file.
559 See `run-hooks'."
560 :type 'hook
561 :group 'vc
562 :version "21.1")
563
564 (defcustom vc-annotate-display-mode nil
565 "Which mode to color the output of \\[vc-annotate] with by default."
566 :type '(choice (const :tag "Default" nil)
567 (const :tag "Scale to Oldest" scale)
568 (const :tag "Scale Oldest->Newest" fullscale)
569 (number :tag "Specify Fractional Number of Days"
570 :value "20.5"))
571 :group 'vc)
572
573 ;;;###autoload
574 (defcustom vc-checkin-hook nil
575 "*Normal hook (list of functions) run after a checkin is done.
576 See also `log-edit-done-hook'."
577 :type 'hook
578 :options '(log-edit-comment-to-change-log)
579 :group 'vc)
580
581 ;;;###autoload
582 (defcustom vc-before-checkin-hook nil
583 "*Normal hook (list of functions) run before a file is checked in.
584 See `run-hooks'."
585 :type 'hook
586 :group 'vc)
587
588 (defcustom vc-logentry-check-hook nil
589 "*Normal hook run by `vc-backend-logentry-check'.
590 Use this to impose your own rules on the entry in addition to any the
591 version control backend imposes itself."
592 :type 'hook
593 :group 'vc)
594
595 ;; Annotate customization
596 (defcustom vc-annotate-color-map
597 '(( 20. . "#FF0000")
598 ( 40. . "#FF3800")
599 ( 60. . "#FF7000")
600 ( 80. . "#FFA800")
601 (100. . "#FFE000")
602 (120. . "#E7FF00")
603 (140. . "#AFFF00")
604 (160. . "#77FF00")
605 (180. . "#3FFF00")
606 (200. . "#07FF00")
607 (220. . "#00FF31")
608 (240. . "#00FF69")
609 (260. . "#00FFA1")
610 (280. . "#00FFD9")
611 (300. . "#00EEFF")
612 (320. . "#00B6FF")
613 (340. . "#007EFF"))
614 "*Association list of age versus color, for \\[vc-annotate].
615 Ages are given in units of fractional days. Default is eighteen steps
616 using a twenty day increment."
617 :type 'alist
618 :group 'vc)
619
620 (defcustom vc-annotate-very-old-color "#0046FF"
621 "*Color for lines older than the current color range in \\[vc-annotate]]."
622 :type 'string
623 :group 'vc)
624
625 (defcustom vc-annotate-background "black"
626 "*Background color for \\[vc-annotate].
627 Default color is used if nil."
628 :type 'string
629 :group 'vc)
630
631 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
632 "*Menu elements for the mode-specific menu of VC-Annotate mode.
633 List of factors, used to expand/compress the time scale. See `vc-annotate'."
634 :type '(repeat number)
635 :group 'vc)
636
637 ;; vc-annotate functionality (CVS only).
638 (defvar vc-annotate-mode nil
639 "Variable indicating if VC-Annotate mode is active.")
640
641 (defvar vc-annotate-mode-map
642 (let ((m (make-sparse-keymap)))
643 (define-key m [menu-bar] (make-sparse-keymap "VC-Annotate"))
644 m)
645 "Local keymap used for VC-Annotate mode.")
646
647 (define-key vc-annotate-mode-map "A" 'vc-annotate-revision-previous-to-line)
648 (define-key vc-annotate-mode-map "D" 'vc-annotate-show-diff-revision-at-line)
649 (define-key vc-annotate-mode-map "J" 'vc-annotate-revision-at-line)
650 (define-key vc-annotate-mode-map "L" 'vc-annotate-show-log-revision-at-line)
651 (define-key vc-annotate-mode-map "N" 'vc-annotate-next-version)
652 (define-key vc-annotate-mode-map "P" 'vc-annotate-prev-version)
653 (define-key vc-annotate-mode-map "W" 'vc-annotate-workfile-version)
654
655 (defvar vc-annotate-mode-menu nil
656 "Local keymap used for VC-Annotate mode's menu bar menu.")
657
658 ;; Header-insertion hair
659
660 (defcustom vc-static-header-alist
661 '(("\\.c$" .
662 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
663 "*Associate static header string templates with file types.
664 A \%s in the template is replaced with the first string associated with
665 the file's version control type in `vc-header-alist'."
666 :type '(repeat (cons :format "%v"
667 (regexp :tag "File Type")
668 (string :tag "Header String")))
669 :group 'vc)
670
671 (defcustom vc-comment-alist
672 '((nroff-mode ".\\\"" ""))
673 "*Special comment delimiters for generating VC headers.
674 Add an entry in this list if you need to override the normal `comment-start'
675 and `comment-end' variables. This will only be necessary if the mode language
676 is sensitive to blank lines."
677 :type '(repeat (list :format "%v"
678 (symbol :tag "Mode")
679 (string :tag "Comment Start")
680 (string :tag "Comment End")))
681 :group 'vc)
682
683 (defcustom vc-checkout-carefully (= (user-uid) 0)
684 "*Non-nil means be extra-careful in checkout.
685 Verify that the file really is not locked
686 and that its contents match what the master file says."
687 :type 'boolean
688 :group 'vc)
689 (make-obsolete-variable 'vc-checkout-carefully
690 "the corresponding checks are always done now."
691 "21.1")
692
693 \f
694 ;; Variables the user doesn't need to know about.
695 (defvar vc-log-operation nil)
696 (defvar vc-log-after-operation-hook nil)
697 (defvar vc-annotate-buffers nil
698 "Alist of current \"Annotate\" buffers and their corresponding backends.
699 The keys are \(BUFFER . BACKEND\). See also `vc-annotate-get-backend'.")
700 ;; In a log entry buffer, this is a local variable
701 ;; that points to the buffer for which it was made
702 ;; (either a file, or a VC dired buffer).
703 (defvar vc-parent-buffer nil)
704 (put 'vc-parent-buffer 'permanent-local t)
705 (defvar vc-parent-buffer-name nil)
706 (put 'vc-parent-buffer-name 'permanent-local t)
707
708 (defvar vc-log-file)
709 (defvar vc-log-version)
710
711 (defvar vc-dired-mode nil)
712 (make-variable-buffer-local 'vc-dired-mode)
713
714 ;; functions that operate on RCS revision numbers. This code should
715 ;; also be moved into the backends. It stays for now, however, since
716 ;; it is used in code below.
717 (defun vc-trunk-p (rev)
718 "Return t if REV is a revision on the trunk."
719 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
720
721 (defun vc-branch-p (rev)
722 "Return t if REV is a branch revision."
723 (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
724
725 ;;;###autoload
726 (defun vc-branch-part (rev)
727 "Return the branch part of a revision number REV."
728 (let ((index (string-match "\\.[0-9]+\\'" rev)))
729 (if index
730 (substring rev 0 index))))
731
732 (defun vc-minor-part (rev)
733 "Return the minor version number of a revision number REV."
734 (string-match "[0-9]+\\'" rev)
735 (substring rev (match-beginning 0) (match-end 0)))
736
737 (defun vc-default-previous-version (backend file rev)
738 "Return the version number immediately preceding REV for FILE,
739 or nil if there is no previous version. This default
740 implementation works for <major>.<minor>-style version numbers as
741 used by RCS and CVS."
742 (let ((branch (vc-branch-part rev))
743 (minor-num (string-to-number (vc-minor-part rev))))
744 (when branch
745 (if (> minor-num 1)
746 ;; version does probably not start a branch or release
747 (concat branch "." (number-to-string (1- minor-num)))
748 (if (vc-trunk-p rev)
749 ;; we are at the beginning of the trunk --
750 ;; don't know anything to return here
751 nil
752 ;; we are at the beginning of a branch --
753 ;; return version of starting point
754 (vc-branch-part branch))))))
755
756 (defun vc-default-next-version (backend file rev)
757 "Return the version number immediately following REV for FILE,
758 or nil if there is no next version. This default implementation
759 works for <major>.<minor>-style version numbers as used by RCS
760 and CVS."
761 (when (not (string= rev (vc-workfile-version file)))
762 (let ((branch (vc-branch-part rev))
763 (minor-num (string-to-number (vc-minor-part rev))))
764 (concat branch "." (number-to-string (1+ minor-num))))))
765
766 ;; File property caching
767
768 (defun vc-clear-context ()
769 "Clear all cached file properties."
770 (interactive)
771 (fillarray vc-file-prop-obarray 0))
772
773 (defmacro with-vc-properties (file form settings)
774 "Execute FORM, then maybe set per-file properties for FILE.
775 SETTINGS is an association list of property/value pairs. After
776 executing FORM, set those properties from SETTINGS that have not yet
777 been updated to their corresponding values."
778 (declare (debug t))
779 `(let ((vc-touched-properties (list t)))
780 ,form
781 (mapcar (lambda (setting)
782 (let ((property (car setting)))
783 (unless (memq property vc-touched-properties)
784 (put (intern ,file vc-file-prop-obarray)
785 property (cdr setting)))))
786 ,settings)))
787
788 ;; Random helper functions
789
790 (defsubst vc-editable-p (file)
791 "Return non-nil if FILE can be edited."
792 (or (eq (vc-checkout-model file) 'implicit)
793 (memq (vc-state file) '(edited needs-merge))))
794
795 ;; Two macros for elisp programming
796 ;;;###autoload
797 (defmacro with-vc-file (file comment &rest body)
798 "Check out a writable copy of FILE if necessary, then execute BODY.
799 Check in FILE with COMMENT (a string) after BODY has been executed.
800 FILE is passed through `expand-file-name'; BODY executed within
801 `save-excursion'. If FILE is not under version control, or locked by
802 somebody else, signal error."
803 (declare (debug t) (indent 2))
804 (let ((filevar (make-symbol "file")))
805 `(let ((,filevar (expand-file-name ,file)))
806 (or (vc-backend ,filevar)
807 (error (format "File not under version control: `%s'" file)))
808 (unless (vc-editable-p ,filevar)
809 (let ((state (vc-state ,filevar)))
810 (if (stringp state)
811 (error (format "`%s' is locking `%s'" state ,filevar))
812 (vc-checkout ,filevar t))))
813 (save-excursion
814 ,@body)
815 (vc-checkin ,filevar nil ,comment))))
816
817 ;;;###autoload
818 (defmacro edit-vc-file (file comment &rest body)
819 "Edit FILE under version control, executing body.
820 Checkin with COMMENT after executing BODY.
821 This macro uses `with-vc-file', passing args to it.
822 However, before executing BODY, find FILE, and after BODY, save buffer."
823 (declare (debug t) (indent 2))
824 (let ((filevar (make-symbol "file")))
825 `(let ((,filevar (expand-file-name ,file)))
826 (with-vc-file
827 ,filevar ,comment
828 (set-buffer (find-file-noselect ,filevar))
829 ,@body
830 (save-buffer)))))
831
832 (defun vc-ensure-vc-buffer ()
833 "Make sure that the current buffer visits a version-controlled file."
834 (if vc-dired-mode
835 (set-buffer (find-file-noselect (dired-get-filename)))
836 (while vc-parent-buffer
837 (pop-to-buffer vc-parent-buffer))
838 (if (not buffer-file-name)
839 (error "Buffer %s is not associated with a file" (buffer-name))
840 (if (not (vc-backend buffer-file-name))
841 (error "File %s is not under version control" buffer-file-name)))))
842
843 (defun vc-process-filter (p s)
844 "An alternative output filter for async process P.
845 The only difference with the default filter is to insert S after markers."
846 (with-current-buffer (process-buffer p)
847 (save-excursion
848 (let ((inhibit-read-only t))
849 (goto-char (process-mark p))
850 (insert s)
851 (set-marker (process-mark p) (point))))))
852
853 (defun vc-setup-buffer (&optional buf)
854 "Prepare BUF for executing a VC command and make it current.
855 BUF defaults to \"*vc*\", can be a string and will be created if necessary."
856 (unless buf (setq buf "*vc*"))
857 (let ((camefrom (current-buffer))
858 (olddir default-directory))
859 (set-buffer (get-buffer-create buf))
860 (kill-all-local-variables)
861 (set (make-local-variable 'vc-parent-buffer) camefrom)
862 (set (make-local-variable 'vc-parent-buffer-name)
863 (concat " from " (buffer-name camefrom)))
864 (setq default-directory olddir)
865 (let ((inhibit-read-only t))
866 (erase-buffer))))
867
868 (defun vc-exec-after (code)
869 "Eval CODE when the current buffer's process is done.
870 If the current buffer has no process, just evaluate CODE.
871 Else, add CODE to the process' sentinel."
872 (let ((proc (get-buffer-process (current-buffer))))
873 (cond
874 ;; If there's no background process, just execute the code.
875 ((null proc) (eval code))
876 ;; If the background process has exited, reap it and try again
877 ((eq (process-status proc) 'exit)
878 (delete-process proc)
879 (vc-exec-after code))
880 ;; If a process is running, add CODE to the sentinel
881 ((eq (process-status proc) 'run)
882 (let ((sentinel (process-sentinel proc)))
883 (set-process-sentinel proc
884 `(lambda (p s)
885 (with-current-buffer ',(current-buffer)
886 (goto-char (process-mark p))
887 ,@(append (cdr (cdr (cdr ;strip off `with-current-buffer buf
888 ; (goto-char...)'
889 (car (cdr (cdr ;strip off `lambda (p s)'
890 sentinel))))))
891 (list `(vc-exec-after ',code))))))))
892 (t (error "Unexpected process state"))))
893 nil)
894
895 (defvar vc-post-command-functions nil
896 "Hook run at the end of `vc-do-command'.
897 Each function is called inside the buffer in which the command was run
898 and is passed 3 arguments: the COMMAND, the FILE and the FLAGS.")
899
900 (defvar w32-quote-process-args)
901 ;;;###autoload
902 (defun vc-do-command (buffer okstatus command file &rest flags)
903 "Execute a VC command, notifying user and checking for errors.
904 Output from COMMAND goes to BUFFER, or *vc* if BUFFER is nil or the
905 current buffer if BUFFER is t. If the destination buffer is not
906 already current, set it up properly and erase it. The command is
907 considered successful if its exit status does not exceed OKSTATUS (if
908 OKSTATUS is nil, that means to ignore errors, if it is 'async, that
909 means not to wait for termination of the subprocess). FILE is the
910 name of the working file (may also be nil, to execute commands that
911 don't expect a file name). If an optional list of FLAGS is present,
912 that is inserted into the command line before the filename."
913 (and file (setq file (expand-file-name file)))
914 (if vc-command-messages
915 (message "Running %s on %s..." command file))
916 (save-current-buffer
917 (unless (or (eq buffer t)
918 (and (stringp buffer)
919 (string= (buffer-name) buffer))
920 (eq buffer (current-buffer)))
921 (vc-setup-buffer buffer))
922 (let ((squeezed (remq nil flags))
923 (inhibit-read-only t)
924 (status 0))
925 (when file
926 ;; FIXME: file-relative-name can return a bogus result because
927 ;; it doesn't look at the actual file-system to see if symlinks
928 ;; come into play.
929 (setq squeezed (append squeezed (list (file-relative-name file)))))
930 (let ((exec-path (append vc-path exec-path))
931 ;; Add vc-path to PATH for the execution of this command.
932 (process-environment
933 (cons (concat "PATH=" (getenv "PATH")
934 path-separator
935 (mapconcat 'identity vc-path path-separator))
936 process-environment))
937 (w32-quote-process-args t))
938 (if (eq okstatus 'async)
939 (let ((proc (apply 'start-process command (current-buffer) command
940 squeezed)))
941 (unless (active-minibuffer-window)
942 (message "Running %s in the background..." command))
943 ;;(set-process-sentinel proc (lambda (p msg) (delete-process p)))
944 (set-process-filter proc 'vc-process-filter)
945 (vc-exec-after
946 `(unless (active-minibuffer-window)
947 (message "Running %s in the background... done" ',command))))
948 (setq status (apply 'call-process command nil t nil squeezed))
949 (when (or (not (integerp status)) (and okstatus (< okstatus status)))
950 (pop-to-buffer (current-buffer))
951 (goto-char (point-min))
952 (shrink-window-if-larger-than-buffer)
953 (error "Running %s...FAILED (%s)" command
954 (if (integerp status) (format "status %d" status) status))))
955 (if vc-command-messages
956 (message "Running %s...OK" command)))
957 (vc-exec-after
958 `(run-hook-with-args 'vc-post-command-functions ',command ',file ',flags))
959 status)))
960
961 (defun vc-position-context (posn)
962 "Save a bit of the text around POSN in the current buffer.
963 Used to help us find the corresponding position again later
964 if markers are destroyed or corrupted."
965 ;; A lot of this was shamelessly lifted from Sebastian Kremer's
966 ;; rcs.el mode.
967 (list posn
968 (buffer-size)
969 (buffer-substring posn
970 (min (point-max) (+ posn 100)))))
971
972 (defun vc-find-position-by-context (context)
973 "Return the position of CONTEXT in the current buffer.
974 If CONTEXT cannot be found, return nil."
975 (let ((context-string (nth 2 context)))
976 (if (equal "" context-string)
977 (point-max)
978 (save-excursion
979 (let ((diff (- (nth 1 context) (buffer-size))))
980 (if (< diff 0) (setq diff (- diff)))
981 (goto-char (nth 0 context))
982 (if (or (search-forward context-string nil t)
983 ;; Can't use search-backward since the match may continue
984 ;; after point.
985 (progn (goto-char (- (point) diff (length context-string)))
986 ;; goto-char doesn't signal an error at
987 ;; beginning of buffer like backward-char would
988 (search-forward context-string nil t)))
989 ;; to beginning of OSTRING
990 (- (point) (length context-string))))))))
991
992 (defun vc-context-matches-p (posn context)
993 "Return t if POSN matches CONTEXT, nil otherwise."
994 (let* ((context-string (nth 2 context))
995 (len (length context-string))
996 (end (+ posn len)))
997 (if (> end (1+ (buffer-size)))
998 nil
999 (string= context-string (buffer-substring posn end)))))
1000
1001 (defun vc-buffer-context ()
1002 "Return a list (POINT-CONTEXT MARK-CONTEXT REPARSE).
1003 Used by `vc-restore-buffer-context' to later restore the context."
1004 (let ((point-context (vc-position-context (point)))
1005 ;; Use mark-marker to avoid confusion in transient-mark-mode.
1006 (mark-context (if (eq (marker-buffer (mark-marker)) (current-buffer))
1007 (vc-position-context (mark-marker))))
1008 ;; Make the right thing happen in transient-mark-mode.
1009 (mark-active nil)
1010 ;; We may want to reparse the compilation buffer after revert
1011 (reparse (and (boundp 'compilation-error-list) ;compile loaded
1012 ;; Construct a list; each elt is nil or a buffer
1013 ;; iff that buffer is a compilation output buffer
1014 ;; that contains markers into the current buffer.
1015 (save-current-buffer
1016 (mapcar (lambda (buffer)
1017 (set-buffer buffer)
1018 (let ((errors (or
1019 compilation-old-error-list
1020 compilation-error-list))
1021 (buffer-error-marked-p nil))
1022 (while (and (consp errors)
1023 (not buffer-error-marked-p))
1024 (and (markerp (cdr (car errors)))
1025 (eq buffer
1026 (marker-buffer
1027 (cdr (car errors))))
1028 (setq buffer-error-marked-p t))
1029 (setq errors (cdr errors)))
1030 (if buffer-error-marked-p buffer)))
1031 (buffer-list))))))
1032 (list point-context mark-context reparse)))
1033
1034 (defun vc-restore-buffer-context (context)
1035 "Restore point/mark, and reparse any affected compilation buffers.
1036 CONTEXT is that which `vc-buffer-context' returns."
1037 (let ((point-context (nth 0 context))
1038 (mark-context (nth 1 context))
1039 (reparse (nth 2 context)))
1040 ;; Reparse affected compilation buffers.
1041 (while reparse
1042 (if (car reparse)
1043 (with-current-buffer (car reparse)
1044 (let ((compilation-last-buffer (current-buffer)) ;select buffer
1045 ;; Record the position in the compilation buffer of
1046 ;; the last error next-error went to.
1047 (error-pos (marker-position
1048 (car (car-safe compilation-error-list)))))
1049 ;; Reparse the error messages as far as they were parsed before.
1050 (compile-reinitialize-errors '(4) compilation-parsing-end)
1051 ;; Move the pointer up to find the error we were at before
1052 ;; reparsing. Now next-error should properly go to the next one.
1053 (while (and compilation-error-list
1054 (/= error-pos (car (car compilation-error-list))))
1055 (setq compilation-error-list (cdr compilation-error-list))))))
1056 (setq reparse (cdr reparse)))
1057
1058 ;; if necessary, restore point and mark
1059 (if (not (vc-context-matches-p (point) point-context))
1060 (let ((new-point (vc-find-position-by-context point-context)))
1061 (if new-point (goto-char new-point))))
1062 (and mark-active
1063 mark-context
1064 (not (vc-context-matches-p (mark) mark-context))
1065 (let ((new-mark (vc-find-position-by-context mark-context)))
1066 (if new-mark (set-mark new-mark))))))
1067
1068 (defun vc-revert-buffer1 (&optional arg no-confirm)
1069 "Revert buffer, keeping point and mark where user expects them.
1070 Try to be clever in the face of changes due to expanded version control
1071 key words. This is important for typeahead to work as expected.
1072 ARG and NO-CONFIRM are passed on to `revert-buffer'."
1073 (interactive "P")
1074 (widen)
1075 (let ((context (vc-buffer-context)))
1076 ;; Use save-excursion here, because it may be able to restore point
1077 ;; and mark properly even in cases where vc-restore-buffer-context
1078 ;; would fail. However, save-excursion might also get it wrong --
1079 ;; in this case, vc-restore-buffer-context gives it a second try.
1080 (save-excursion
1081 ;; t means don't call normal-mode;
1082 ;; that's to preserve various minor modes.
1083 (revert-buffer arg no-confirm t))
1084 (vc-restore-buffer-context context)))
1085
1086
1087 (defun vc-buffer-sync (&optional not-urgent)
1088 "Make sure the current buffer and its working file are in sync.
1089 NOT-URGENT means it is ok to continue if the user says not to save."
1090 (if (buffer-modified-p)
1091 (if (or vc-suppress-confirm
1092 (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
1093 (save-buffer)
1094 (unless not-urgent
1095 (error "Aborted")))))
1096
1097 (defun vc-default-latest-on-branch-p (backend file)
1098 "Return non-nil if FILE is the latest on its branch.
1099 This default implementation always returns non-nil, which means that
1100 editing non-current versions is not supported by default."
1101 t)
1102
1103 (defun vc-recompute-state (file)
1104 "Force a recomputation of the version control state of FILE.
1105 The state is computed using the exact, and possibly expensive
1106 function `vc-BACKEND-state', not the heuristic."
1107 (vc-file-setprop file 'vc-state (vc-call state file)))
1108
1109 (defun vc-next-action-on-file (file verbose &optional comment)
1110 "Do The Right Thing for a given FILE under version control.
1111 If COMMENT is specified, it will be used as an admin or checkin comment.
1112 If VERBOSE is non-nil, query the user rather than using default parameters."
1113 (let ((visited (get-file-buffer file))
1114 state version)
1115 (when visited
1116 (if vc-dired-mode
1117 (switch-to-buffer-other-window visited)
1118 (set-buffer visited))
1119 ;; Check relation of buffer and file, and make sure
1120 ;; user knows what he's doing. First, finding the file
1121 ;; will check whether the file on disk is newer.
1122 ;; Ignore buffer-read-only during this test, and
1123 ;; preserve find-file-literally.
1124 (let ((buffer-read-only (not (file-writable-p file))))
1125 (find-file-noselect file nil find-file-literally))
1126 (if (not (verify-visited-file-modtime (current-buffer)))
1127 (if (yes-or-no-p "Replace file on disk with buffer contents? ")
1128 (write-file buffer-file-name)
1129 (error "Aborted"))
1130 ;; Now, check if we have unsaved changes.
1131 (vc-buffer-sync t)
1132 (if (buffer-modified-p)
1133 (or (y-or-n-p "Operate on disk file, keeping modified buffer? ")
1134 (error "Aborted")))))
1135
1136 ;; Do the right thing
1137 (if (not (vc-registered file))
1138 (vc-register verbose comment)
1139 (vc-recompute-state file)
1140 (if visited (vc-mode-line file))
1141 (setq state (vc-state file))
1142 (cond
1143 ;; up-to-date
1144 ((or (eq state 'up-to-date)
1145 (and verbose (eq state 'needs-patch)))
1146 (cond
1147 (verbose
1148 ;; go to a different version
1149 (setq version
1150 (read-string "Branch, version, or backend to move to: "))
1151 (let ((vsym (intern-soft (upcase version))))
1152 (if (member vsym vc-handled-backends)
1153 (vc-transfer-file file vsym)
1154 (vc-checkout file (eq (vc-checkout-model file) 'implicit)
1155 version))))
1156 ((not (eq (vc-checkout-model file) 'implicit))
1157 ;; check the file out
1158 (vc-checkout file t))
1159 (t
1160 ;; do nothing
1161 (message "%s is up-to-date" file))))
1162
1163 ;; Abnormal: edited but read-only
1164 ((and visited (eq state 'edited)
1165 buffer-read-only (not (file-writable-p file)))
1166 ;; Make the file+buffer read-write. If the user really wanted to
1167 ;; commit, he'll get a chance to do that next time around, anyway.
1168 (message "File is edited but read-only; making it writable")
1169 (set-file-modes buffer-file-name
1170 (logior (file-modes buffer-file-name) 128))
1171 (toggle-read-only -1))
1172
1173 ;; edited
1174 ((eq state 'edited)
1175 (cond
1176 ;; For files with locking, if the file does not contain
1177 ;; any changes, just let go of the lock, i.e. revert.
1178 ((and (not (eq (vc-checkout-model file) 'implicit))
1179 (vc-workfile-unchanged-p file)
1180 ;; If buffer is modified, that means the user just
1181 ;; said no to saving it; in that case, don't revert,
1182 ;; because the user might intend to save after
1183 ;; finishing the log entry.
1184 (not (and visited (buffer-modified-p))))
1185 ;; DO NOT revert the file without asking the user!
1186 (if (not visited) (find-file-other-window file))
1187 (if (yes-or-no-p "Revert to master version? ")
1188 (vc-revert-buffer)))
1189 (t ;; normal action
1190 (if (not verbose)
1191 (vc-checkin file nil comment)
1192 (setq version (read-string "New version or backend: "))
1193 (let ((vsym (intern (upcase version))))
1194 (if (member vsym vc-handled-backends)
1195 (vc-transfer-file file vsym)
1196 (vc-checkin file version comment)))))))
1197
1198 ;; locked by somebody else
1199 ((stringp state)
1200 (if comment
1201 (error "Sorry, you can't steal the lock on %s this way"
1202 (file-name-nondirectory file)))
1203 (vc-steal-lock file
1204 (if verbose (read-string "Version to steal: ")
1205 (vc-workfile-version file))
1206 state))
1207
1208 ;; needs-patch
1209 ((eq state 'needs-patch)
1210 (if (yes-or-no-p (format
1211 "%s is not up-to-date. Get latest version? "
1212 (file-name-nondirectory file)))
1213 (vc-checkout file (eq (vc-checkout-model file) 'implicit) t)
1214 (if (and (not (eq (vc-checkout-model file) 'implicit))
1215 (yes-or-no-p "Lock this version? "))
1216 (vc-checkout file t)
1217 (error "Aborted"))))
1218
1219 ;; needs-merge
1220 ((eq state 'needs-merge)
1221 (if (yes-or-no-p (format
1222 "%s is not up-to-date. Merge in changes now? "
1223 (file-name-nondirectory file)))
1224 (vc-maybe-resolve-conflicts file (vc-call merge-news file))
1225 (error "Aborted")))
1226
1227 ;; unlocked-changes
1228 ((eq state 'unlocked-changes)
1229 (if (not visited) (find-file-other-window file))
1230 (if (save-window-excursion
1231 (vc-version-diff file (vc-workfile-version file) nil)
1232 (goto-char (point-min))
1233 (let ((inhibit-read-only t))
1234 (insert
1235 (format "Changes to %s since last lock:\n\n" file)))
1236 (not (beep))
1237 (yes-or-no-p (concat "File has unlocked changes. "
1238 "Claim lock retaining changes? ")))
1239 (progn (vc-call steal-lock file)
1240 (clear-visited-file-modtime)
1241 ;; Must clear any headers here because they wouldn't
1242 ;; show that the file is locked now.
1243 (vc-clear-headers file)
1244 (write-file buffer-file-name)
1245 (vc-mode-line file))
1246 (if (not (yes-or-no-p
1247 "Revert to checked-in version, instead? "))
1248 (error "Checkout aborted")
1249 (vc-revert-buffer1 t t)
1250 (vc-checkout file t))))))))
1251
1252 (defvar vc-dired-window-configuration)
1253
1254 (defun vc-next-action-dired (file rev comment)
1255 "Call `vc-next-action-on-file' on all the marked files.
1256 Ignores FILE and REV, but passes on COMMENT."
1257 (let ((dired-buffer (current-buffer)))
1258 (dired-map-over-marks
1259 (let ((file (dired-get-filename)))
1260 (message "Processing %s..." file)
1261 (vc-next-action-on-file file nil comment)
1262 (set-buffer dired-buffer)
1263 (set-window-configuration vc-dired-window-configuration)
1264 (message "Processing %s...done" file))
1265 nil t))
1266 (dired-move-to-filename))
1267
1268 ;; Here's the major entry point.
1269
1270 ;;;###autoload
1271 (defun vc-next-action (verbose)
1272 "Do the next logical version control operation on the current file.
1273
1274 If you call this from within a VC dired buffer with no files marked,
1275 it will operate on the file in the current line.
1276
1277 If you call this from within a VC dired buffer, and one or more
1278 files are marked, it will accept a log message and then operate on
1279 each one. The log message will be used as a comment for any register
1280 or checkin operations, but ignored when doing checkouts. Attempted
1281 lock steals will raise an error.
1282
1283 A prefix argument lets you specify the version number to use.
1284
1285 For RCS and SCCS files:
1286 If the file is not already registered, this registers it for version
1287 control.
1288 If the file is registered and not locked by anyone, this checks out
1289 a writable and locked file ready for editing.
1290 If the file is checked out and locked by the calling user, this
1291 first checks to see if the file has changed since checkout. If not,
1292 it performs a revert.
1293 If the file has been changed, this pops up a buffer for entry
1294 of a log message; when the message has been entered, it checks in the
1295 resulting changes along with the log message as change commentary. If
1296 the variable `vc-keep-workfiles' is non-nil (which is its default), a
1297 read-only copy of the changed file is left in place afterwards.
1298 If the file is registered and locked by someone else, you are given
1299 the option to steal the lock.
1300
1301 For CVS files:
1302 If the file is not already registered, this registers it for version
1303 control. This does a \"cvs add\", but no \"cvs commit\".
1304 If the file is added but not committed, it is committed.
1305 If your working file is changed, but the repository file is
1306 unchanged, this pops up a buffer for entry of a log message; when the
1307 message has been entered, it checks in the resulting changes along
1308 with the logmessage as change commentary. A writable file is retained.
1309 If the repository file is changed, you are asked if you want to
1310 merge in the changes into your working copy."
1311
1312 (interactive "P")
1313 (catch 'nogo
1314 (if vc-dired-mode
1315 (let ((files (dired-get-marked-files)))
1316 (set (make-local-variable 'vc-dired-window-configuration)
1317 (current-window-configuration))
1318 (if (string= ""
1319 (mapconcat
1320 (lambda (f)
1321 (if (not (vc-up-to-date-p f)) "@" ""))
1322 files ""))
1323 (vc-next-action-dired nil nil "dummy")
1324 (vc-start-entry nil nil nil nil
1325 "Enter a change comment for the marked files."
1326 'vc-next-action-dired))
1327 (throw 'nogo nil)))
1328 (while vc-parent-buffer
1329 (pop-to-buffer vc-parent-buffer))
1330 (if buffer-file-name
1331 (vc-next-action-on-file buffer-file-name verbose)
1332 (error "Buffer %s is not associated with a file" (buffer-name)))))
1333
1334 ;; These functions help the vc-next-action entry point
1335
1336 ;;;###autoload
1337 (defun vc-register (&optional set-version comment)
1338 "Register the current file into a version control system.
1339 With prefix argument SET-VERSION, allow user to specify initial version
1340 level. If COMMENT is present, use that as an initial comment.
1341
1342 The version control system to use is found by cycling through the list
1343 `vc-handled-backends'. The first backend in that list which declares
1344 itself responsible for the file (usually because other files in that
1345 directory are already registered under that backend) will be used to
1346 register the file. If no backend declares itself responsible, the
1347 first backend that could register the file is used."
1348 (interactive "P")
1349 (unless buffer-file-name (error "No visited file"))
1350 (when (vc-backend buffer-file-name)
1351 (if (vc-registered buffer-file-name)
1352 (error "This file is already registered")
1353 (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
1354 (error "Aborted"))))
1355 ;; Watch out for new buffers of size 0: the corresponding file
1356 ;; does not exist yet, even though buffer-modified-p is nil.
1357 (if (and (not (buffer-modified-p))
1358 (zerop (buffer-size))
1359 (not (file-exists-p buffer-file-name)))
1360 (set-buffer-modified-p t))
1361 (vc-buffer-sync)
1362
1363 (vc-start-entry buffer-file-name
1364 (if set-version
1365 (read-string (format "Initial version level for %s: "
1366 (buffer-name)))
1367 (let ((backend (vc-responsible-backend buffer-file-name)))
1368 (if (vc-find-backend-function backend 'init-version)
1369 (vc-call-backend backend 'init-version)
1370 vc-default-init-version)))
1371 (or comment (not vc-initial-comment))
1372 nil
1373 "Enter initial comment."
1374 (lambda (file rev comment)
1375 (message "Registering %s... " file)
1376 (let ((backend (vc-responsible-backend file t)))
1377 (vc-file-clearprops file)
1378 (vc-call-backend backend 'register file rev comment)
1379 (vc-file-setprop file 'vc-backend backend)
1380 (unless vc-make-backup-files
1381 (make-local-variable 'backup-inhibited)
1382 (setq backup-inhibited t)))
1383 (message "Registering %s... done" file))))
1384
1385
1386 (defun vc-responsible-backend (file &optional register)
1387 "Return the name of a backend system that is responsible for FILE.
1388 The optional argument REGISTER means that a backend suitable for
1389 registration should be found.
1390
1391 If REGISTER is nil, then if FILE is already registered, return the
1392 backend of FILE. If FILE is not registered, or a directory, then the
1393 first backend in `vc-handled-backends' that declares itself
1394 responsible for FILE is returned. If no backend declares itself
1395 responsible, return the first backend.
1396
1397 If REGISTER is non-nil, return the first responsible backend under
1398 which FILE is not yet registered. If there is no such backend, return
1399 the first backend under which FILE is not yet registered, but could
1400 be registered."
1401 (if (not vc-handled-backends)
1402 (error "No handled backends"))
1403 (or (and (not (file-directory-p file)) (not register) (vc-backend file))
1404 (catch 'found
1405 ;; First try: find a responsible backend. If this is for registration,
1406 ;; it must be a backend under which FILE is not yet registered.
1407 (dolist (backend vc-handled-backends)
1408 (and (or (not register)
1409 (not (vc-call-backend backend 'registered file)))
1410 (vc-call-backend backend 'responsible-p file)
1411 (throw 'found backend)))
1412 ;; no responsible backend
1413 (if (not register)
1414 ;; if this is not for registration, the first backend must do
1415 (car vc-handled-backends)
1416 ;; for registration, we need to find a new backend that
1417 ;; could register FILE
1418 (dolist (backend vc-handled-backends)
1419 (and (not (vc-call-backend backend 'registered file))
1420 (vc-call-backend backend 'could-register file)
1421 (throw 'found backend)))
1422 (error "No backend that could register")))))
1423
1424 (defun vc-default-responsible-p (backend file)
1425 "Indicate whether BACKEND is reponsible for FILE.
1426 The default is to return nil always."
1427 nil)
1428
1429 (defun vc-default-could-register (backend file)
1430 "Return non-nil if BACKEND could be used to register FILE.
1431 The default implementation returns t for all files."
1432 t)
1433
1434 (defun vc-resynch-window (file &optional keep noquery)
1435 "If FILE is in the current buffer, either revert or unvisit it.
1436 The choice between revert (to see expanded keywords) and unvisit depends on
1437 `vc-keep-workfiles'. NOQUERY if non-nil inhibits confirmation for
1438 reverting. NOQUERY should be t *only* if it is known the only
1439 difference between the buffer and the file is due to version control
1440 rather than user editing!"
1441 (and (string= buffer-file-name file)
1442 (if keep
1443 (progn
1444 (vc-revert-buffer1 t noquery)
1445 ;; TODO: Adjusting view mode might no longer be necessary
1446 ;; after RMS change to files.el of 1999-08-08. Investigate
1447 ;; this when we install the new VC.
1448 (and view-read-only
1449 (if (file-writable-p file)
1450 (and view-mode
1451 (let ((view-old-buffer-read-only nil))
1452 (view-mode-exit)))
1453 (and (not view-mode)
1454 (not (eq (get major-mode 'mode-class) 'special))
1455 (view-mode-enter))))
1456 (vc-mode-line buffer-file-name))
1457 (kill-buffer (current-buffer)))))
1458
1459 (defun vc-resynch-buffer (file &optional keep noquery)
1460 "If FILE is currently visited, resynch its buffer."
1461 (if (string= buffer-file-name file)
1462 (vc-resynch-window file keep noquery)
1463 (let ((buffer (get-file-buffer file)))
1464 (if buffer
1465 (with-current-buffer buffer
1466 (vc-resynch-window file keep noquery)))))
1467 (vc-dired-resynch-file file))
1468
1469 (defun vc-start-entry (file rev comment initial-contents msg action &optional after-hook)
1470 "Accept a comment for an operation on FILE revision REV.
1471 If COMMENT is nil, pop up a VC-log buffer, emit MSG, and set the
1472 action on close to ACTION. If COMMENT is a string and
1473 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial
1474 contents of the log entry buffer. If COMMENT is a string and
1475 INITIAL-CONTENTS is nil, do action immediately as if the user had
1476 entered COMMENT. If COMMENT is t, also do action immediately with an
1477 empty comment. Remember the file's buffer in `vc-parent-buffer'
1478 \(current one if no file). AFTER-HOOK specifies the local value
1479 for vc-log-operation-hook."
1480 (let ((parent (or (and file (get-file-buffer file)) (current-buffer))))
1481 (if vc-before-checkin-hook
1482 (if file
1483 (with-current-buffer parent
1484 (run-hooks 'vc-before-checkin-hook))
1485 (run-hooks 'vc-before-checkin-hook)))
1486 (if (and comment (not initial-contents))
1487 (set-buffer (get-buffer-create "*VC-log*"))
1488 (pop-to-buffer (get-buffer-create "*VC-log*")))
1489 (set (make-local-variable 'vc-parent-buffer) parent)
1490 (set (make-local-variable 'vc-parent-buffer-name)
1491 (concat " from " (buffer-name vc-parent-buffer)))
1492 (if file (vc-mode-line file))
1493 (vc-log-edit file)
1494 (make-local-variable 'vc-log-after-operation-hook)
1495 (if after-hook
1496 (setq vc-log-after-operation-hook after-hook))
1497 (setq vc-log-operation action)
1498 (setq vc-log-version rev)
1499 (when comment
1500 (erase-buffer)
1501 (when (stringp comment) (insert comment)))
1502 (if (or (not comment) initial-contents)
1503 (message "%s Type C-c C-c when done" msg)
1504 (vc-finish-logentry (eq comment t)))))
1505
1506 (defun vc-checkout (file &optional writable rev)
1507 "Retrieve a copy of the revision REV of FILE.
1508 If WRITABLE is non-nil, make sure the retrieved file is writable.
1509 REV defaults to the latest revision.
1510
1511 After check-out, runs the normal hook `vc-checkout-hook'."
1512 (and writable
1513 (not rev)
1514 (vc-call make-version-backups-p file)
1515 (vc-up-to-date-p file)
1516 (vc-make-version-backup file))
1517 (with-vc-properties
1518 file
1519 (condition-case err
1520 (vc-call checkout file writable rev)
1521 (file-error
1522 ;; Maybe the backend is not installed ;-(
1523 (when writable
1524 (let ((buf (get-file-buffer file)))
1525 (when buf (with-current-buffer buf (toggle-read-only -1)))))
1526 (signal (car err) (cdr err))))
1527 `((vc-state . ,(if (or (eq (vc-checkout-model file) 'implicit)
1528 (not writable))
1529 (if (vc-call latest-on-branch-p file)
1530 'up-to-date
1531 'needs-patch)
1532 'edited))
1533 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
1534 (vc-resynch-buffer file t t)
1535 (run-hooks 'vc-checkout-hook))
1536
1537 (defun vc-steal-lock (file rev owner)
1538 "Steal the lock on FILE."
1539 (let (file-description)
1540 (if rev
1541 (setq file-description (format "%s:%s" file rev))
1542 (setq file-description file))
1543 (if (not (yes-or-no-p (format "Steal the lock on %s from %s? "
1544 file-description owner)))
1545 (error "Steal canceled"))
1546 (message "Stealing lock on %s..." file)
1547 (with-vc-properties
1548 file
1549 (vc-call steal-lock file rev)
1550 `((vc-state . edited)))
1551 (vc-resynch-buffer file t t)
1552 (message "Stealing lock on %s...done" file)
1553 ;; Write mail after actually stealing, because if the stealing
1554 ;; goes wrong, we don't want to send any mail.
1555 (compose-mail owner (format "Stolen lock on %s" file-description))
1556 (setq default-directory (expand-file-name "~/"))
1557 (goto-char (point-max))
1558 (insert
1559 (format "I stole the lock on %s, " file-description)
1560 (current-time-string)
1561 ".\n")
1562 (message "Please explain why you stole the lock. Type C-c C-c when done.")))
1563
1564 (defun vc-checkin (file &optional rev comment initial-contents)
1565 "Check in FILE.
1566 The optional argument REV may be a string specifying the new version
1567 level (if nil increment the current level). COMMENT is a comment
1568 string; if omitted, a buffer is popped up to accept a comment. If
1569 INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
1570 of the log entry buffer.
1571
1572 If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
1573 that the version control system supports this mode of operation.
1574
1575 Runs the normal hook `vc-checkin-hook'."
1576 (vc-start-entry
1577 file rev comment initial-contents
1578 "Enter a change comment."
1579 (lambda (file rev comment)
1580 (message "Checking in %s..." file)
1581 ;; "This log message intentionally left almost blank".
1582 ;; RCS 5.7 gripes about white-space-only comments too.
1583 (or (and comment (string-match "[^\t\n ]" comment))
1584 (setq comment "*** empty log message ***"))
1585 (with-vc-properties
1586 file
1587 ;; Change buffers to get local value of vc-checkin-switches.
1588 (with-current-buffer (or (get-file-buffer file) (current-buffer))
1589 (progn
1590 (vc-call checkin file rev comment)
1591 (vc-delete-automatic-version-backups file)))
1592 `((vc-state . up-to-date)
1593 (vc-checkout-time . ,(nth 5 (file-attributes file)))
1594 (vc-workfile-version . nil)))
1595 (message "Checking in %s...done" file))
1596 'vc-checkin-hook))
1597
1598 (defun vc-finish-logentry (&optional nocomment)
1599 "Complete the operation implied by the current log entry.
1600 Use the contents of the current buffer as a check-in or registration
1601 comment. If the optional arg NOCOMMENT is non-nil, then don't check
1602 the buffer contents as a comment."
1603 (interactive)
1604 ;; Check and record the comment, if any.
1605 (unless nocomment
1606 ;; Comment too long?
1607 (vc-call-backend (or (and vc-log-file (vc-backend vc-log-file))
1608 (vc-responsible-backend default-directory))
1609 'logentry-check)
1610 (run-hooks 'vc-logentry-check-hook))
1611 ;; Sync parent buffer in case the user modified it while editing the comment.
1612 ;; But not if it is a vc-dired buffer.
1613 (with-current-buffer vc-parent-buffer
1614 (or vc-dired-mode (vc-buffer-sync)))
1615 (if (not vc-log-operation) (error "No log operation is pending"))
1616 ;; save the parameters held in buffer-local variables
1617 (let ((log-operation vc-log-operation)
1618 (log-file vc-log-file)
1619 (log-version vc-log-version)
1620 (log-entry (buffer-string))
1621 (after-hook vc-log-after-operation-hook)
1622 (tmp-vc-parent-buffer vc-parent-buffer))
1623 (pop-to-buffer vc-parent-buffer)
1624 ;; OK, do it to it
1625 (save-excursion
1626 (funcall log-operation
1627 log-file
1628 log-version
1629 log-entry))
1630 ;; Remove checkin window (after the checkin so that if that fails
1631 ;; we don't zap the *VC-log* buffer and the typing therein).
1632 (let ((logbuf (get-buffer "*VC-log*")))
1633 (cond ((and logbuf vc-delete-logbuf-window)
1634 (delete-windows-on logbuf (selected-frame))
1635 ;; Kill buffer and delete any other dedicated windows/frames.
1636 (kill-buffer logbuf))
1637 (logbuf (pop-to-buffer "*VC-log*")
1638 (bury-buffer)
1639 (pop-to-buffer tmp-vc-parent-buffer))))
1640 ;; Now make sure we see the expanded headers
1641 (if log-file
1642 (vc-resynch-buffer log-file vc-keep-workfiles t))
1643 (if vc-dired-mode
1644 (dired-move-to-filename))
1645 (run-hooks after-hook 'vc-finish-logentry-hook)))
1646
1647 ;; Code for access to the comment ring
1648
1649 ;; Additional entry points for examining version histories
1650
1651 ;;;###autoload
1652 (defun vc-diff (historic &optional not-urgent)
1653 "Display diffs between file versions.
1654 Normally this compares the current file and buffer with the most
1655 recent checked in version of that file. This uses no arguments. With
1656 a prefix argument HISTORIC, it reads the file name to use and two
1657 version designators specifying which versions to compare. The
1658 optional argument NOT-URGENT non-nil means it is ok to say no to
1659 saving the buffer."
1660 (interactive (list current-prefix-arg t))
1661 (if historic
1662 (call-interactively 'vc-version-diff)
1663 (vc-ensure-vc-buffer)
1664 (let ((file buffer-file-name))
1665 (vc-buffer-sync not-urgent)
1666 (if (vc-workfile-unchanged-p buffer-file-name)
1667 (message "No changes to %s since latest version" file)
1668 (vc-version-diff file nil nil)))))
1669
1670 (defun vc-version-diff (file rel1 rel2)
1671 "List the differences between FILE's versions REL1 and REL2.
1672 If REL1 is empty or nil it means to use the current workfile version;
1673 REL2 empty or nil means the current file contents. FILE may also be
1674 a directory, in that case, generate diffs between the correponding
1675 versions of all registered files in or below it."
1676 (interactive
1677 (let ((file (expand-file-name
1678 (read-file-name (if buffer-file-name
1679 "File or dir to diff: (default visited file) "
1680 "File or dir to diff: ")
1681 default-directory buffer-file-name t)))
1682 (rel1-default nil) (rel2-default nil))
1683 ;; compute default versions based on the file state
1684 (cond
1685 ;; if it's a directory, don't supply any version default
1686 ((file-directory-p file)
1687 nil)
1688 ;; if the file is not up-to-date, use current version as older version
1689 ((not (vc-up-to-date-p file))
1690 (setq rel1-default (vc-workfile-version file)))
1691 ;; if the file is not locked, use last and previous version as default
1692 (t
1693 (setq rel1-default (vc-call previous-version file
1694 (vc-workfile-version file)))
1695 (if (string= rel1-default "") (setq rel1-default nil))
1696 (setq rel2-default (vc-workfile-version file))))
1697 ;; construct argument list
1698 (list file
1699 (read-string (if rel1-default
1700 (concat "Older version: (default "
1701 rel1-default ") ")
1702 "Older version: ")
1703 nil nil rel1-default)
1704 (read-string (if rel2-default
1705 (concat "Newer version: (default "
1706 rel2-default ") ")
1707 "Newer version (default: current source): ")
1708 nil nil rel2-default))))
1709 (if (file-directory-p file)
1710 ;; recursive directory diff
1711 (progn
1712 (vc-setup-buffer "*vc-diff*")
1713 (if (string-equal rel1 "") (setq rel1 nil))
1714 (if (string-equal rel2 "") (setq rel2 nil))
1715 (let ((inhibit-read-only t))
1716 (insert "Diffs between "
1717 (or rel1 "last version checked in")
1718 " and "
1719 (or rel2 "current workfile(s)")
1720 ":\n\n"))
1721 (let ((dir (file-name-as-directory file)))
1722 (vc-call-backend (vc-responsible-backend dir)
1723 'diff-tree dir rel1 rel2))
1724 (vc-exec-after `(let ((inhibit-read-only t))
1725 (insert "\nEnd of diffs.\n"))))
1726 ;; single file diff
1727 (vc-diff-internal file rel1 rel2))
1728 (set-buffer "*vc-diff*")
1729 (if (and (zerop (buffer-size))
1730 (not (get-buffer-process (current-buffer))))
1731 (progn
1732 (if rel1
1733 (if rel2
1734 (message "No changes to %s between %s and %s" file rel1 rel2)
1735 (message "No changes to %s since %s" file rel1))
1736 (message "No changes to %s since latest version" file))
1737 nil)
1738 (pop-to-buffer (current-buffer))
1739 ;; Gnus-5.8.5 sets up an autoload for diff-mode, even if it's
1740 ;; not available. Work around that.
1741 (if (require 'diff-mode nil t) (diff-mode))
1742 (vc-exec-after '(let ((inhibit-read-only t))
1743 (if (eq (buffer-size) 0)
1744 (insert "No differences found.\n"))
1745 (goto-char (point-min))
1746 (shrink-window-if-larger-than-buffer)))
1747 t))
1748
1749 (defun vc-diff-internal (file rel1 rel2)
1750 "Run diff to compare FILE's revisions REL1 and REL2.
1751 Output goes to the current buffer, which is assumed properly set up.
1752 The exit status of the diff command is returned.
1753
1754 This function takes care to set up a proper coding system for diff output.
1755 If both revisions are available as local files, then it also does not
1756 actually call the backend, but performs a local diff."
1757 (if (or (not rel1) (string-equal rel1 ""))
1758 (setq rel1 (vc-workfile-version file)))
1759 (if (string-equal rel2 "")
1760 (setq rel2 nil))
1761 (let ((file-rel1 (vc-version-backup-file file rel1))
1762 (file-rel2 (if (not rel2)
1763 file
1764 (vc-version-backup-file file rel2)))
1765 (coding-system-for-read (vc-coding-system-for-diff file)))
1766 (if (and file-rel1 file-rel2)
1767 (apply 'vc-do-command "*vc-diff*" 1 "diff" nil
1768 (append (vc-switches nil 'diff)
1769 (list (file-relative-name file-rel1)
1770 (file-relative-name file-rel2))))
1771 (vc-call diff file rel1 rel2))))
1772
1773
1774 (defun vc-switches (backend op)
1775 (let ((switches
1776 (or (if backend
1777 (let ((sym (vc-make-backend-sym
1778 backend (intern (concat (symbol-name op)
1779 "-switches")))))
1780 (if (boundp sym) (symbol-value sym))))
1781 (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
1782 (if (boundp sym) (symbol-value sym)))
1783 (cond
1784 ((eq op 'diff) diff-switches)))))
1785 (if (stringp switches) (list switches)
1786 ;; If not a list, return nil.
1787 ;; This is so we can set vc-diff-switches to t to override
1788 ;; any switches in diff-switches.
1789 (if (listp switches) switches))))
1790
1791 ;; Old def for compatibility with Emacs-21.[123].
1792 (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
1793 (make-obsolete 'vc-diff-switches-list 'vc-switches "21.4")
1794
1795 (defun vc-default-diff-tree (backend dir rel1 rel2)
1796 "List differences for all registered files at and below DIR.
1797 The meaning of REL1 and REL2 is the same as for `vc-version-diff'."
1798 ;; This implementation does an explicit tree walk, and calls
1799 ;; vc-BACKEND-diff directly for each file. An optimization
1800 ;; would be to use `vc-diff-internal', so that diffs can be local,
1801 ;; and to call it only for files that are actually changed.
1802 ;; However, this is expensive for some backends, and so it is left
1803 ;; to backend-specific implementations.
1804 (setq default-directory dir)
1805 (vc-file-tree-walk
1806 default-directory
1807 (lambda (f)
1808 (vc-exec-after
1809 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
1810 (message "Looking at %s" ',f)
1811 (vc-call-backend ',(vc-backend f)
1812 'diff ',f ',rel1 ',rel2))))))
1813
1814 (defun vc-coding-system-for-diff (file)
1815 "Return the coding system for reading diff output for FILE."
1816 (or coding-system-for-read
1817 ;; if we already have this file open,
1818 ;; use the buffer's coding system
1819 (let ((buf (find-buffer-visiting file)))
1820 (if buf (with-current-buffer buf
1821 buffer-file-coding-system)))
1822 ;; otherwise, try to find one based on the file name
1823 (car (find-operation-coding-system 'insert-file-contents file))
1824 ;; and a final fallback
1825 'undecided))
1826
1827 ;;;###autoload
1828 (defun vc-version-other-window (rev)
1829 "Visit version REV of the current file in another window.
1830 If the current file is named `F', the version is named `F.~REV~'.
1831 If `F.~REV~' already exists, use it instead of checking it out again."
1832 (interactive "sVersion to visit (default is workfile version): ")
1833 (vc-ensure-vc-buffer)
1834 (let* ((file buffer-file-name)
1835 (version (if (string-equal rev "")
1836 (vc-workfile-version file)
1837 rev)))
1838 (switch-to-buffer-other-window (vc-find-version file version))))
1839
1840 (defun vc-find-version (file version)
1841 "Read VERSION of FILE into a buffer and return the buffer."
1842 (let ((automatic-backup (vc-version-backup-file-name file version))
1843 (filebuf (or (get-file-buffer file) (current-buffer)))
1844 (filename (vc-version-backup-file-name file version 'manual)))
1845 (unless (file-exists-p filename)
1846 (if (file-exists-p automatic-backup)
1847 (rename-file automatic-backup filename nil)
1848 (message "Checking out %s..." filename)
1849 (with-current-buffer filebuf
1850 (let ((failed t))
1851 (unwind-protect
1852 (let ((coding-system-for-read 'no-conversion)
1853 (coding-system-for-write 'no-conversion))
1854 (with-temp-file filename
1855 (let ((outbuf (current-buffer)))
1856 ;; Change buffer to get local value of
1857 ;; vc-checkout-switches.
1858 (with-current-buffer filebuf
1859 (vc-call find-version file version outbuf))))
1860 (setq failed nil))
1861 (if (and failed (file-exists-p filename))
1862 (delete-file filename))))
1863 (vc-mode-line file))
1864 (message "Checking out %s...done" filename)))
1865 (find-file-noselect filename)))
1866
1867 (defun vc-default-find-version (backend file rev buffer)
1868 "Provide the new `find-version' op based on the old `checkout' op.
1869 This is only for compatibility with old backends. They should be updated
1870 to provide the `find-version' operation instead."
1871 (let ((tmpfile (make-temp-file (expand-file-name file))))
1872 (unwind-protect
1873 (progn
1874 (vc-call-backend backend 'checkout file nil rev tmpfile)
1875 (with-current-buffer buffer
1876 (insert-file-contents-literally tmpfile)))
1877 (delete-file tmpfile))))
1878
1879 ;; Header-insertion code
1880
1881 ;;;###autoload
1882 (defun vc-insert-headers ()
1883 "Insert headers into a file for use with a version control system.
1884 Headers desired are inserted at point, and are pulled from
1885 the variable `vc-BACKEND-header'."
1886 (interactive)
1887 (vc-ensure-vc-buffer)
1888 (save-excursion
1889 (save-restriction
1890 (widen)
1891 (if (or (not (vc-check-headers))
1892 (y-or-n-p "Version headers already exist. Insert another set? "))
1893 (progn
1894 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
1895 (comment-start-vc (or (car delims) comment-start "#"))
1896 (comment-end-vc (or (car (cdr delims)) comment-end ""))
1897 (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
1898 'header))
1899 (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
1900 (mapcar (lambda (s)
1901 (insert comment-start-vc "\t" s "\t"
1902 comment-end-vc "\n"))
1903 hdstrings)
1904 (if vc-static-header-alist
1905 (mapcar (lambda (f)
1906 (if (string-match (car f) buffer-file-name)
1907 (insert (format (cdr f) (car hdstrings)))))
1908 vc-static-header-alist))
1909 )
1910 )))))
1911
1912 (defun vc-clear-headers (&optional file)
1913 "Clear all version headers in the current buffer (or FILE).
1914 The headers are reset to their non-expanded form."
1915 (let* ((filename (or file buffer-file-name))
1916 (visited (find-buffer-visiting filename))
1917 (backend (vc-backend filename)))
1918 (when (vc-find-backend-function backend 'clear-headers)
1919 (if visited
1920 (let ((context (vc-buffer-context)))
1921 ;; save-excursion may be able to relocate point and mark
1922 ;; properly. If it fails, vc-restore-buffer-context
1923 ;; will give it a second try.
1924 (save-excursion
1925 (vc-call-backend backend 'clear-headers))
1926 (vc-restore-buffer-context context))
1927 (set-buffer (find-file-noselect filename))
1928 (vc-call-backend backend 'clear-headers)
1929 (kill-buffer filename)))))
1930
1931 ;;;###autoload
1932 (defun vc-merge ()
1933 "Merge changes between two versions into the current buffer's file.
1934 This asks for two versions to merge from in the minibuffer. If the
1935 first version is a branch number, then merge all changes from that
1936 branch. If the first version is empty, merge news, i.e. recent changes
1937 from the current branch.
1938
1939 See Info node `Merging'."
1940 (interactive)
1941 (vc-ensure-vc-buffer)
1942 (vc-buffer-sync)
1943 (let* ((file buffer-file-name)
1944 (backend (vc-backend file))
1945 (state (vc-state file))
1946 first-version second-version status)
1947 (cond
1948 ((stringp state)
1949 (error "File is locked by %s" state))
1950 ((not (vc-editable-p file))
1951 (if (y-or-n-p
1952 "File must be checked out for merging. Check out now? ")
1953 (vc-checkout file t)
1954 (error "Merge aborted"))))
1955 (setq first-version
1956 (read-string (concat "Branch or version to merge from "
1957 "(default: news on current branch): ")))
1958 (if (string= first-version "")
1959 (if (not (vc-find-backend-function backend 'merge-news))
1960 (error "Sorry, merging news is not implemented for %s" backend)
1961 (setq status (vc-call merge-news file)))
1962 (if (not (vc-find-backend-function backend 'merge))
1963 (error "Sorry, merging is not implemented for %s" backend)
1964 (if (not (vc-branch-p first-version))
1965 (setq second-version
1966 (read-string "Second version: "
1967 (concat (vc-branch-part first-version) ".")))
1968 ;; We want to merge an entire branch. Set versions
1969 ;; accordingly, so that vc-BACKEND-merge understands us.
1970 (setq second-version first-version)
1971 ;; first-version must be the starting point of the branch
1972 (setq first-version (vc-branch-part first-version)))
1973 (setq status (vc-call merge file first-version second-version))))
1974 (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE")))
1975
1976 (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
1977 (vc-resynch-buffer file t (not (buffer-modified-p)))
1978 (if (zerop status) (message "Merge successful")
1979 (smerge-mode 1)
1980 (if (y-or-n-p "Conflicts detected. Resolve them now? ")
1981 (vc-resolve-conflicts name-A name-B)
1982 (message "File contains conflict markers"))))
1983
1984 ;;;###autoload
1985 (defalias 'vc-resolve-conflicts 'smerge-ediff)
1986
1987 ;; The VC directory major mode. Coopt Dired for this.
1988 ;; All VC commands get mapped into logical equivalents.
1989
1990 (defvar vc-dired-switches)
1991 (defvar vc-dired-terse-mode)
1992
1993 (defvar vc-dired-mode-map
1994 (let ((map (make-sparse-keymap))
1995 (vmap (make-sparse-keymap)))
1996 (define-key map "\C-xv" vmap)
1997 (define-key map "v" vmap)
1998 (set-keymap-parent vmap vc-prefix-map)
1999 (define-key vmap "t" 'vc-dired-toggle-terse-mode)
2000 map))
2001
2002 (define-derived-mode vc-dired-mode dired-mode "Dired under VC"
2003 "The major mode used in VC directory buffers.
2004
2005 It works like Dired, but lists only files under version control, with
2006 the current VC state of each file being indicated in the place of the
2007 file's link count, owner, group and size. Subdirectories are also
2008 listed, and you may insert them into the buffer as desired, like in
2009 Dired.
2010
2011 All Dired commands operate normally, with the exception of `v', which
2012 is redefined as the version control prefix, so that you can type
2013 `vl', `v=' etc. to invoke `vc-print-log', `vc-diff', and the like on
2014 the file named in the current Dired buffer line. `vv' invokes
2015 `vc-next-action' on this file, or on all files currently marked.
2016 There is a special command, `*l', to mark all files currently locked."
2017 ;; define-derived-mode does it for us in Emacs-21, but not in Emacs-20.
2018 ;; We do it here because dired might not be loaded yet
2019 ;; when vc-dired-mode-map is initialized.
2020 (set-keymap-parent vc-dired-mode-map dired-mode-map)
2021 (add-hook 'dired-after-readin-hook 'vc-dired-hook nil t)
2022 ;; The following is slightly modified from dired.el,
2023 ;; because file lines look a bit different in vc-dired-mode
2024 ;; (the column before the date does not end in a digit).
2025 (set (make-local-variable 'dired-move-to-filename-regexp)
2026 (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
2027 ;; In some locales, month abbreviations are as short as 2 letters,
2028 ;; and they can be followed by ".".
2029 (month (concat l l "+\\.?"))
2030 (s " ")
2031 (yyyy "[0-9][0-9][0-9][0-9]")
2032 (dd "[ 0-3][0-9]")
2033 (HH:MM "[ 0-2][0-9]:[0-5][0-9]")
2034 (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
2035 (zone "[-+][0-2][0-9][0-5][0-9]")
2036 (iso-mm-dd "[01][0-9]-[0-3][0-9]")
2037 (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
2038 (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
2039 "\\|" yyyy "-" iso-mm-dd "\\)"))
2040 (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
2041 s "+"
2042 "\\(" HH:MM "\\|" yyyy "\\)"))
2043 (western-comma (concat month s "+" dd "," s "+" yyyy))
2044 ;; Japanese MS-Windows ls-lisp has one-digit months, and
2045 ;; omits the Kanji characters after month and day-of-month.
2046 (mm "[ 0-1]?[0-9]")
2047 (japanese
2048 (concat mm l "?" s dd l "?" s "+"
2049 "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
2050 ;; the .* below ensures that we find the last match on a line
2051 (concat ".*" s
2052 "\\(" western "\\|" western-comma "\\|" japanese "\\|" iso "\\)"
2053 s "+")))
2054 (and (boundp 'vc-dired-switches)
2055 vc-dired-switches
2056 (set (make-local-variable 'dired-actual-switches)
2057 vc-dired-switches))
2058 (set (make-local-variable 'vc-dired-terse-mode) vc-dired-terse-display)
2059 (setq vc-dired-mode t))
2060
2061 (defun vc-dired-toggle-terse-mode ()
2062 "Toggle terse display in VC Dired."
2063 (interactive)
2064 (if (not vc-dired-mode)
2065 nil
2066 (setq vc-dired-terse-mode (not vc-dired-terse-mode))
2067 (if vc-dired-terse-mode
2068 (vc-dired-hook)
2069 (revert-buffer))))
2070
2071 (defun vc-dired-mark-locked ()
2072 "Mark all files currently locked."
2073 (interactive)
2074 (dired-mark-if (let ((f (dired-get-filename nil t)))
2075 (and f
2076 (not (file-directory-p f))
2077 (not (vc-up-to-date-p f))))
2078 "locked file"))
2079
2080 (define-key vc-dired-mode-map "*l" 'vc-dired-mark-locked)
2081
2082 (defun vc-default-dired-state-info (backend file)
2083 (let ((state (vc-state file)))
2084 (cond
2085 ((stringp state) (concat "(" state ")"))
2086 ((eq state 'edited) (concat "(" (vc-user-login-name) ")"))
2087 ((eq state 'needs-merge) "(merge)")
2088 ((eq state 'needs-patch) "(patch)")
2089 ((eq state 'unlocked-changes) "(stale)"))))
2090
2091 (defun vc-dired-reformat-line (vc-info)
2092 "Reformat a directory-listing line.
2093 Replace various columns with version control information, VC-INFO.
2094 This code, like dired, assumes UNIX -l format."
2095 (beginning-of-line)
2096 (when (re-search-forward
2097 ;; Match link count, owner, group, size. Group may be missing,
2098 ;; and only the size is present in OS/2 -l format.
2099 "^..[drwxlts-]+ \\( *[0-9]+\\( [^ ]+ +\\([^ ]+ +\\)?[0-9]+\\)?\\) "
2100 (line-end-position) t)
2101 (replace-match (substring (concat vc-info " ") 0 10)
2102 t t nil 1)))
2103
2104 (defun vc-dired-hook ()
2105 "Reformat the listing according to version control.
2106 Called by dired after any portion of a vc-dired buffer has been read in."
2107 (message "Getting version information... ")
2108 (let (subdir filename (buffer-read-only nil))
2109 (goto-char (point-min))
2110 (while (not (eobp))
2111 (cond
2112 ;; subdir header line
2113 ((setq subdir (dired-get-subdir))
2114 ;; if the backend supports it, get the state
2115 ;; of all files in this directory at once
2116 (let ((backend (vc-responsible-backend subdir)))
2117 (if (vc-find-backend-function backend 'dir-state)
2118 (vc-call-backend backend 'dir-state subdir)))
2119 (forward-line 1)
2120 ;; erase (but don't remove) the "total" line
2121 (delete-region (point) (line-end-position))
2122 (beginning-of-line)
2123 (forward-line 1))
2124 ;; file line
2125 ((setq filename (dired-get-filename nil t))
2126 (cond
2127 ;; subdir
2128 ((file-directory-p filename)
2129 (cond
2130 ((member (file-name-nondirectory filename)
2131 vc-directory-exclusion-list)
2132 (let ((pos (point)))
2133 (dired-kill-tree filename)
2134 (goto-char pos)
2135 (dired-kill-line)))
2136 (vc-dired-terse-mode
2137 ;; Don't show directories in terse mode. Don't use
2138 ;; dired-kill-line to remove it, because in recursive listings,
2139 ;; that would remove the directory contents as well.
2140 (delete-region (line-beginning-position)
2141 (progn (forward-line 1) (point))))
2142 ((string-match "\\`\\.\\.?\\'" (file-name-nondirectory filename))
2143 (dired-kill-line))
2144 (t
2145 (vc-dired-reformat-line nil)
2146 (forward-line 1))))
2147 ;; ordinary file
2148 ((and (vc-backend filename)
2149 (not (and vc-dired-terse-mode
2150 (vc-up-to-date-p filename))))
2151 (vc-dired-reformat-line (vc-call dired-state-info filename))
2152 (forward-line 1))
2153 (t
2154 (dired-kill-line))))
2155 ;; any other line
2156 (t (forward-line 1))))
2157 (vc-dired-purge))
2158 (message "Getting version information... done")
2159 (save-restriction
2160 (widen)
2161 (cond ((eq (count-lines (point-min) (point-max)) 1)
2162 (goto-char (point-min))
2163 (message "No files locked under %s" default-directory)))))
2164
2165 (defun vc-dired-purge ()
2166 "Remove empty subdirs."
2167 (goto-char (point-min))
2168 (while (dired-get-subdir)
2169 (forward-line 2)
2170 (if (dired-get-filename nil t)
2171 (if (not (dired-next-subdir 1 t))
2172 (goto-char (point-max)))
2173 (forward-line -2)
2174 (if (not (string= (dired-current-directory) default-directory))
2175 (dired-do-kill-lines t "")
2176 ;; We cannot remove the top level directory.
2177 ;; Just make it look a little nicer.
2178 (forward-line 1)
2179 (or (eobp) (kill-line))
2180 (if (not (dired-next-subdir 1 t))
2181 (goto-char (point-max))))))
2182 (goto-char (point-min)))
2183
2184 (defun vc-dired-buffers-for-dir (dir)
2185 "Return a list of all vc-dired buffers that currently display DIR."
2186 (let (result)
2187 ;; Check whether dired is loaded.
2188 (when (fboundp 'dired-buffers-for-dir)
2189 (mapcar (lambda (buffer)
2190 (with-current-buffer buffer
2191 (if vc-dired-mode
2192 (setq result (append result (list buffer))))))
2193 (dired-buffers-for-dir dir)))
2194 result))
2195
2196 (defun vc-dired-resynch-file (file)
2197 "Update the entries for FILE in any VC Dired buffers that list it."
2198 (let ((buffers (vc-dired-buffers-for-dir (file-name-directory file))))
2199 (when buffers
2200 (mapcar (lambda (buffer)
2201 (with-current-buffer buffer
2202 (if (dired-goto-file file)
2203 ;; bind vc-dired-terse-mode to nil so that
2204 ;; files won't vanish when they are checked in
2205 (let ((vc-dired-terse-mode nil))
2206 (dired-do-redisplay 1)))))
2207 buffers))))
2208
2209 ;;;###autoload
2210 (defun vc-directory (dir read-switches)
2211 "Create a buffer in VC Dired Mode for directory DIR.
2212
2213 See Info node `VC Dired Mode'.
2214
2215 With prefix arg READ-SWITCHES, specify a value to override
2216 `dired-listing-switches' when generating the listing."
2217 (interactive "DDired under VC (directory): \nP")
2218 (let ((vc-dired-switches (concat vc-dired-listing-switches
2219 (if vc-dired-recurse "R" ""))))
2220 (if read-switches
2221 (setq vc-dired-switches
2222 (read-string "Dired listing switches: "
2223 vc-dired-switches)))
2224 (require 'dired)
2225 (require 'dired-aux)
2226 (switch-to-buffer
2227 (dired-internal-noselect (expand-file-name (file-name-as-directory dir))
2228 vc-dired-switches
2229 'vc-dired-mode))))
2230
2231
2232 ;; Named-configuration entry points
2233
2234 (defun vc-snapshot-precondition (dir)
2235 "Scan the tree below DIR, looking for files not up-to-date.
2236 If any file is not up-to-date, return the name of the first such file.
2237 \(This means, neither snapshot creation nor retrieval is allowed.\)
2238 If one or more of the files are currently visited, return `visited'.
2239 Otherwise, return nil."
2240 (let ((status nil))
2241 (catch 'vc-locked-example
2242 (vc-file-tree-walk
2243 dir
2244 (lambda (f)
2245 (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
2246 (if (get-file-buffer f) (setq status 'visited)))))
2247 status)))
2248
2249 ;;;###autoload
2250 (defun vc-create-snapshot (dir name branchp)
2251 "Descending recursively from DIR, make a snapshot called NAME.
2252 For each registered file, the version level of its latest version
2253 becomes part of the named configuration. If the prefix argument
2254 BRANCHP is given, the snapshot is made as a new branch and the files
2255 are checked out in that new branch."
2256 (interactive
2257 (list (read-file-name "Directory: " default-directory default-directory t)
2258 (read-string "New snapshot name: ")
2259 current-prefix-arg))
2260 (message "Making %s... " (if branchp "branch" "snapshot"))
2261 (if (file-directory-p dir) (setq dir (file-name-as-directory dir)))
2262 (vc-call-backend (vc-responsible-backend dir)
2263 'create-snapshot dir name branchp)
2264 (message "Making %s... done" (if branchp "branch" "snapshot")))
2265
2266 (defun vc-default-create-snapshot (backend dir name branchp)
2267 (when branchp
2268 (error "VC backend %s does not support module branches" backend))
2269 (let ((result (vc-snapshot-precondition dir)))
2270 (if (stringp result)
2271 (error "File %s is not up-to-date" result)
2272 (vc-file-tree-walk
2273 dir
2274 (lambda (f)
2275 (vc-call assign-name f name))))))
2276
2277 ;;;###autoload
2278 (defun vc-retrieve-snapshot (dir name)
2279 "Descending recursively from DIR, retrieve the snapshot called NAME.
2280 If NAME is empty, it refers to the latest versions.
2281 If locking is used for the files in DIR, then there must not be any
2282 locked files at or below DIR (but if NAME is empty, locked files are
2283 allowed and simply skipped)."
2284 (interactive
2285 (list (read-file-name "Directory: " default-directory default-directory t)
2286 (read-string "Snapshot name to retrieve (default latest versions): ")))
2287 (let ((update (yes-or-no-p "Update any affected buffers? "))
2288 (msg (if (or (not name) (string= name ""))
2289 (format "Updating %s... " (abbreviate-file-name dir))
2290 (format "Retrieving snapshot into %s... "
2291 (abbreviate-file-name dir)))))
2292 (message msg)
2293 (vc-call-backend (vc-responsible-backend dir)
2294 'retrieve-snapshot dir name update)
2295 (message (concat msg "done"))))
2296
2297 (defun vc-default-retrieve-snapshot (backend dir name update)
2298 (if (string= name "")
2299 (progn
2300 (vc-file-tree-walk
2301 dir
2302 (lambda (f) (and
2303 (vc-up-to-date-p f)
2304 (vc-error-occurred
2305 (vc-call checkout f nil "")
2306 (if update (vc-resynch-buffer f t t)))))))
2307 (let ((result (vc-snapshot-precondition dir)))
2308 (if (stringp result)
2309 (error "File %s is locked" result)
2310 (setq update (and (eq result 'visited) update))
2311 (vc-file-tree-walk
2312 dir
2313 (lambda (f) (vc-error-occurred
2314 (vc-call checkout f nil name)
2315 (if update (vc-resynch-buffer f t t)))))))))
2316
2317 ;; Miscellaneous other entry points
2318
2319 ;;;###autoload
2320 (defun vc-print-log (&optional focus-rev)
2321 "List the change log of the current buffer in a window. If
2322 FOCUS-REV is non-nil, leave the point at that revision."
2323 (interactive)
2324 (vc-ensure-vc-buffer)
2325 (let ((file buffer-file-name))
2326 (or focus-rev (setq focus-rev (vc-workfile-version file)))
2327 (vc-call print-log file)
2328 (set-buffer "*vc*")
2329 (pop-to-buffer (current-buffer))
2330 (log-view-mode)
2331 (vc-exec-after
2332 `(let ((inhibit-read-only t))
2333 (goto-char (point-max)) (forward-line -1)
2334 (while (looking-at "=*\n")
2335 (delete-char (- (match-end 0) (match-beginning 0)))
2336 (forward-line -1))
2337 (goto-char (point-min))
2338 (if (looking-at "[\b\t\n\v\f\r ]+")
2339 (delete-char (- (match-end 0) (match-beginning 0))))
2340 (shrink-window-if-larger-than-buffer)
2341 ;; move point to the log entry for the current version
2342 (vc-call-backend ',(vc-backend file)
2343 'show-log-entry
2344 ',focus-rev)
2345 (set-buffer-modified-p nil)))))
2346
2347 (defun vc-default-show-log-entry (backend rev)
2348 (with-no-warnings
2349 (log-view-goto-rev rev)))
2350
2351 (defun vc-default-comment-history (backend file)
2352 "Return a string with all log entries stored in BACKEND for FILE."
2353 (if (vc-find-backend-function backend 'print-log)
2354 (with-current-buffer "*vc*"
2355 (vc-call print-log file)
2356 (vc-call wash-log file)
2357 (buffer-string))))
2358
2359 (defun vc-default-wash-log (backend file)
2360 "Remove all non-comment information from log output.
2361 This default implementation works for RCS logs; backends should override
2362 it if their logs are not in RCS format."
2363 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
2364 "\\(branches: .*;\n\\)?"
2365 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
2366 (goto-char (point-max)) (forward-line -1)
2367 (while (looking-at "=*\n")
2368 (delete-char (- (match-end 0) (match-beginning 0)))
2369 (forward-line -1))
2370 (goto-char (point-min))
2371 (if (looking-at "[\b\t\n\v\f\r ]+")
2372 (delete-char (- (match-end 0) (match-beginning 0))))
2373 (goto-char (point-min))
2374 (re-search-forward separator nil t)
2375 (delete-region (point-min) (point))
2376 (while (re-search-forward separator nil t)
2377 (delete-region (match-beginning 0) (match-end 0)))))
2378
2379 ;;;###autoload
2380 (defun vc-revert-buffer ()
2381 "Revert the current buffer's file to the version it was based on.
2382 This asks for confirmation if the buffer contents are not identical
2383 to that version. This function does not automatically pick up newer
2384 changes found in the master file; use \\[universal-argument] \\[vc-next-action] to do so."
2385 (interactive)
2386 (vc-ensure-vc-buffer)
2387 ;; Make sure buffer is saved. If the user says `no', abort since
2388 ;; we cannot show the changes and ask for confirmation to discard them.
2389 (vc-buffer-sync nil)
2390 (let ((file buffer-file-name)
2391 ;; This operation should always ask for confirmation.
2392 (vc-suppress-confirm nil)
2393 (obuf (current-buffer))
2394 status)
2395 (if (vc-up-to-date-p file)
2396 (unless (yes-or-no-p "File seems up-to-date. Revert anyway? ")
2397 (error "Revert canceled")))
2398 (unless (vc-workfile-unchanged-p file)
2399 ;; vc-diff selects the new window, which is not what we want:
2400 ;; if the new window is on another frame, that'd require the user
2401 ;; moving her mouse to answer the yes-or-no-p question.
2402 (let ((win (save-selected-window
2403 (setq status (vc-diff nil t)) (selected-window))))
2404 (vc-exec-after `(message nil))
2405 (when status
2406 (unwind-protect
2407 (unless (yes-or-no-p "Discard changes? ")
2408 (error "Revert canceled"))
2409 (select-window win)
2410 (if (one-window-p t)
2411 (if (window-dedicated-p (selected-window))
2412 (make-frame-invisible))
2413 (delete-window))))))
2414 (set-buffer obuf)
2415 ;; Do the reverting
2416 (message "Reverting %s..." file)
2417 (vc-revert-file file)
2418 (message "Reverting %s...done" file)))
2419
2420 ;;;###autoload
2421 (defun vc-update ()
2422 "Update the current buffer's file to the latest version on its branch.
2423 If the file contains no changes, and is not locked, then this simply replaces
2424 the working file with the latest version on its branch. If the file contains
2425 changes, and the backend supports merging news, then any recent changes from
2426 the current branch are merged into the working file."
2427 (interactive)
2428 (vc-ensure-vc-buffer)
2429 (vc-buffer-sync nil)
2430 (let ((file buffer-file-name))
2431 (if (vc-up-to-date-p file)
2432 (vc-checkout file nil "")
2433 (if (eq (vc-checkout-model file) 'locking)
2434 (if (eq (vc-state file) 'edited)
2435 (error
2436 (substitute-command-keys
2437 "File is locked--type \\[vc-revert-buffer] to discard changes"))
2438 (error
2439 (substitute-command-keys
2440 "Unexpected file state (%s)--type \\[vc-next-action] to correct")
2441 (vc-state file)))
2442 (if (not (vc-find-backend-function (vc-backend file) 'merge-news))
2443 (error "Sorry, merging news is not implemented for %s"
2444 (vc-backend file))
2445 (vc-call merge-news file)
2446 (vc-resynch-window file t t))))))
2447
2448 (defun vc-version-backup-file (file &optional rev)
2449 "Return name of backup file for revision REV of FILE.
2450 If version backups should be used for FILE, and there exists
2451 such a backup for REV or the current workfile version of file,
2452 return its name; otherwise return nil."
2453 (when (vc-call make-version-backups-p file)
2454 (let ((backup-file (vc-version-backup-file-name file rev)))
2455 (if (file-exists-p backup-file)
2456 backup-file
2457 ;; there is no automatic backup, but maybe the user made one manually
2458 (setq backup-file (vc-version-backup-file-name file rev 'manual))
2459 (if (file-exists-p backup-file)
2460 backup-file)))))
2461
2462 (defun vc-revert-file (file)
2463 "Revert FILE back to the version it was based on."
2464 (with-vc-properties
2465 file
2466 (let ((backup-file (vc-version-backup-file file)))
2467 (when backup-file
2468 (copy-file backup-file file 'ok-if-already-exists 'keep-date)
2469 (vc-delete-automatic-version-backups file))
2470 (vc-call revert file backup-file))
2471 `((vc-state . up-to-date)
2472 (vc-checkout-time . ,(nth 5 (file-attributes file)))))
2473 (vc-resynch-buffer file t t))
2474
2475 ;;;###autoload
2476 (defun vc-cancel-version (norevert)
2477 "Get rid of most recently checked in version of this file.
2478 A prefix argument NOREVERT means do not revert the buffer afterwards."
2479 (interactive "P")
2480 (vc-ensure-vc-buffer)
2481 (let* ((file buffer-file-name)
2482 (backend (vc-backend file))
2483 (target (vc-workfile-version file)))
2484 (cond
2485 ((not (vc-find-backend-function backend 'cancel-version))
2486 (error "Sorry, canceling versions is not supported under %s" backend))
2487 ((not (vc-call latest-on-branch-p file))
2488 (error "This is not the latest version; VC cannot cancel it"))
2489 ((not (vc-up-to-date-p file))
2490 (error (substitute-command-keys "File is not up to date; use \\[vc-revert-buffer] to discard changes"))))
2491 (if (null (yes-or-no-p (format "Remove version %s from master? " target)))
2492 (error "Aborted")
2493 (setq norevert (or norevert (not
2494 (yes-or-no-p "Revert buffer to most recent remaining version? "))))
2495
2496 (message "Removing last change from %s..." file)
2497 (with-vc-properties
2498 file
2499 (vc-call cancel-version file norevert)
2500 `((vc-state . ,(if norevert 'edited 'up-to-date))
2501 (vc-checkout-time . ,(if norevert
2502 0
2503 (nth 5 (file-attributes file))))
2504 (vc-workfile-version . nil)))
2505 (message "Removing last change from %s...done" file)
2506
2507 (cond
2508 (norevert ;; clear version headers and mark the buffer modified
2509 (set-visited-file-name file)
2510 (when (not vc-make-backup-files)
2511 ;; inhibit backup for this buffer
2512 (make-local-variable 'backup-inhibited)
2513 (setq backup-inhibited t))
2514 (setq buffer-read-only nil)
2515 (vc-clear-headers)
2516 (vc-mode-line file)
2517 (vc-dired-resynch-file file))
2518 (t ;; revert buffer to file on disk
2519 (vc-resynch-buffer file t t)))
2520 (message "Version %s has been removed from the master" target))))
2521
2522 ;;;###autoload
2523 (defun vc-switch-backend (file backend)
2524 "Make BACKEND the current version control system for FILE.
2525 FILE must already be registered in BACKEND. The change is not
2526 permanent, only for the current session. This function only changes
2527 VC's perspective on FILE, it does not register or unregister it.
2528 By default, this command cycles through the registered backends.
2529 To get a prompt, use a prefix argument."
2530 (interactive
2531 (list
2532 buffer-file-name
2533 (let ((backend (vc-backend buffer-file-name))
2534 (backends nil))
2535 ;; Find the registered backends.
2536 (dolist (backend vc-handled-backends)
2537 (when (vc-call-backend backend 'registered buffer-file-name)
2538 (push backend backends)))
2539 ;; Find the next backend.
2540 (let ((def (car (delq backend (append (memq backend backends) backends))))
2541 (others (delete backend backends)))
2542 (cond
2543 ((null others) (error "No other backend to switch to"))
2544 (current-prefix-arg
2545 (intern
2546 (upcase
2547 (completing-read
2548 (format "Switch to backend [%s]: " def)
2549 (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
2550 nil t nil nil (downcase (symbol-name def))))))
2551 (t def))))))
2552 (unless (eq backend (vc-backend file))
2553 (vc-file-clearprops file)
2554 (vc-file-setprop file 'vc-backend backend)
2555 ;; Force recomputation of the state
2556 (unless (vc-call-backend backend 'registered file)
2557 (vc-file-clearprops file)
2558 (error "%s is not registered in %s" file backend))
2559 (vc-mode-line file)))
2560
2561 ;;;###autoload
2562 (defun vc-transfer-file (file new-backend)
2563 "Transfer FILE to another version control system NEW-BACKEND.
2564 If NEW-BACKEND has a higher precedence than FILE's current backend
2565 \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
2566 NEW-BACKEND, using the version number from the current backend as the
2567 base level. If NEW-BACKEND has a lower precedence than the current
2568 backend, then commit all changes that were made under the current
2569 backend to NEW-BACKEND, and unregister FILE from the current backend.
2570 \(If FILE is not yet registered under NEW-BACKEND, register it.)"
2571 (let* ((old-backend (vc-backend file))
2572 (edited (memq (vc-state file) '(edited needs-merge)))
2573 (registered (vc-call-backend new-backend 'registered file))
2574 (move
2575 (and registered ; Never move if not registered in new-backend yet.
2576 ;; move if new-backend comes later in vc-handled-backends
2577 (or (memq new-backend (memq old-backend vc-handled-backends))
2578 (y-or-n-p "Final transfer? "))))
2579 (comment nil))
2580 (if (eq old-backend new-backend)
2581 (error "%s is the current backend of %s" new-backend file))
2582 (if registered
2583 (set-file-modes file (logior (file-modes file) 128))
2584 ;; `registered' might have switched under us.
2585 (vc-switch-backend file old-backend)
2586 (let* ((rev (vc-workfile-version file))
2587 (modified-file (and edited (make-temp-file file)))
2588 (unmodified-file (and modified-file (vc-version-backup-file file))))
2589 ;; Go back to the base unmodified file.
2590 (unwind-protect
2591 (progn
2592 (when modified-file
2593 (copy-file file modified-file 'ok-if-already-exists)
2594 ;; If we have a local copy of the unmodified file, handle that
2595 ;; here and not in vc-revert-file because we don't want to
2596 ;; delete that copy -- it is still useful for OLD-BACKEND.
2597 (if unmodified-file
2598 (copy-file unmodified-file file
2599 'ok-if-already-exists 'keep-date)
2600 (if (y-or-n-p "Get base version from master? ")
2601 (vc-revert-file file))))
2602 (vc-call-backend new-backend 'receive-file file rev))
2603 (when modified-file
2604 (vc-switch-backend file new-backend)
2605 (unless (eq (vc-checkout-model file) 'implicit)
2606 (vc-checkout file t nil))
2607 (rename-file modified-file file 'ok-if-already-exists)
2608 (vc-file-setprop file 'vc-checkout-time nil)))))
2609 (when move
2610 (vc-switch-backend file old-backend)
2611 (setq comment (vc-call comment-history file))
2612 (vc-call unregister file))
2613 (vc-switch-backend file new-backend)
2614 (when (or move edited)
2615 (vc-file-setprop file 'vc-state 'edited)
2616 (vc-mode-line file)
2617 (vc-checkin file nil comment (stringp comment)))))
2618
2619 (defun vc-default-unregister (backend file)
2620 "Default implementation of `vc-unregister', signals an error."
2621 (error "Unregistering files is not supported for %s" backend))
2622
2623 (defun vc-default-receive-file (backend file rev)
2624 "Let BACKEND receive FILE from another version control system."
2625 (vc-call-backend backend 'register file rev ""))
2626
2627 (defun vc-rename-master (oldmaster newfile templates)
2628 "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
2629 (let* ((dir (file-name-directory (expand-file-name oldmaster)))
2630 (newdir (or (file-name-directory newfile) ""))
2631 (newbase (file-name-nondirectory newfile))
2632 (masters
2633 ;; List of potential master files for `newfile'
2634 (mapcar
2635 (lambda (s) (vc-possible-master s newdir newbase))
2636 templates)))
2637 (if (or (file-symlink-p oldmaster)
2638 (file-symlink-p (file-name-directory oldmaster)))
2639 (error "This is unsafe in the presence of symbolic links"))
2640 (rename-file
2641 oldmaster
2642 (catch 'found
2643 ;; If possible, keep the master file in the same directory.
2644 (dolist (f masters)
2645 (if (and f (string= (file-name-directory (expand-file-name f)) dir))
2646 (throw 'found f)))
2647 ;; If not, just use the first possible place.
2648 (dolist (f masters)
2649 (and f (or (not (setq dir (file-name-directory f)))
2650 (file-directory-p dir))
2651 (throw 'found f)))
2652 (error "New file lacks a version control directory")))))
2653
2654 (defun vc-delete-file (file)
2655 "Delete file and mark it as such in the version control system."
2656 (interactive "fVC delete file: ")
2657 (let ((buf (get-file-buffer file))
2658 (backend (vc-backend file)))
2659 (unless backend
2660 (error "File %s is not under version control"
2661 (file-name-nondirectory file)))
2662 (unless (vc-find-backend-function backend 'delete-file)
2663 (error "Deleting files under %s is not supported in VC" backend))
2664 (if (and buf (buffer-modified-p buf))
2665 (error "Please save files before deleting them"))
2666 (unless (y-or-n-p (format "Really want to delete %s ? "
2667 (file-name-nondirectory file)))
2668 (error "Abort!"))
2669 (unless (or (file-directory-p file) (null make-backup-files))
2670 (with-current-buffer (or buf (find-file-noselect file))
2671 (let ((backup-inhibited nil))
2672 (backup-buffer))))
2673 (vc-call delete-file file)
2674 ;; If the backend hasn't deleted the file itself, let's do it for him.
2675 (if (file-exists-p file) (delete-file file))))
2676
2677 (defun vc-default-rename-file (backend old new)
2678 (condition-case nil
2679 (add-name-to-file old new)
2680 (error (rename-file old new)))
2681 (vc-delete-file old)
2682 (with-current-buffer (find-file-noselect new)
2683 (vc-register)))
2684
2685 ;;;###autoload
2686 (defun vc-rename-file (old new)
2687 "Rename file OLD to NEW, and rename its master file likewise."
2688 (interactive "fVC rename file: \nFRename to: ")
2689 (let ((oldbuf (get-file-buffer old)))
2690 (if (and oldbuf (buffer-modified-p oldbuf))
2691 (error "Please save files before moving them"))
2692 (if (get-file-buffer new)
2693 (error "Already editing new file name"))
2694 (if (file-exists-p new)
2695 (error "New file already exists"))
2696 (let ((state (vc-state old)))
2697 (unless (memq state '(up-to-date edited))
2698 (error "Please %s files before moving them"
2699 (if (stringp state) "check in" "update"))))
2700 (vc-call rename-file old new)
2701 (vc-file-clearprops old)
2702 ;; Move the actual file (unless the backend did it already)
2703 (if (file-exists-p old) (rename-file old new))
2704 ;; ?? Renaming a file might change its contents due to keyword expansion.
2705 ;; We should really check out a new copy if the old copy was precisely equal
2706 ;; to some checked in version. However, testing for this is tricky....
2707 (if oldbuf
2708 (with-current-buffer oldbuf
2709 (let ((buffer-read-only buffer-read-only))
2710 (set-visited-file-name new))
2711 (vc-backend new)
2712 (vc-mode-line new)
2713 (set-buffer-modified-p nil)))))
2714
2715 ;; Only defined in very recent Emacsen
2716 (defvar small-temporary-file-directory nil)
2717
2718 ;;;###autoload
2719 (defun vc-update-change-log (&rest args)
2720 "Find change log file and add entries from recent version control logs.
2721 Normally, find log entries for all registered files in the default
2722 directory.
2723
2724 With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
2725
2726 With any numeric prefix arg, find log entries for all currently visited
2727 files that are under version control. This puts all the entries in the
2728 log for the default directory, which may not be appropriate.
2729
2730 From a program, any ARGS are assumed to be filenames for which
2731 log entries should be gathered."
2732 (interactive
2733 (cond ((consp current-prefix-arg) ;C-u
2734 (list buffer-file-name))
2735 (current-prefix-arg ;Numeric argument.
2736 (let ((files nil)
2737 (buffers (buffer-list))
2738 file)
2739 (while buffers
2740 (setq file (buffer-file-name (car buffers)))
2741 (and file (vc-backend file)
2742 (setq files (cons file files)))
2743 (setq buffers (cdr buffers)))
2744 files))
2745 (t
2746 ;; Don't supply any filenames to backend; this means
2747 ;; it should find all relevant files relative to
2748 ;; the default-directory.
2749 nil)))
2750 (vc-call-backend (vc-responsible-backend default-directory)
2751 'update-changelog args))
2752
2753 (defun vc-default-update-changelog (backend files)
2754 "Default implementation of update-changelog.
2755 Uses `rcs2log' which only works for RCS and CVS."
2756 ;; FIXME: We (c|sh)ould add support for cvs2cl
2757 (let ((odefault default-directory)
2758 (changelog (find-change-log))
2759 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
2760 (tempfile (make-temp-file
2761 (expand-file-name "vc"
2762 (or small-temporary-file-directory
2763 temporary-file-directory))))
2764 (full-name (or add-log-full-name
2765 (user-full-name)
2766 (user-login-name)
2767 (format "uid%d" (number-to-string (user-uid)))))
2768 (mailing-address (or add-log-mailing-address
2769 user-mail-address)))
2770 (find-file-other-window changelog)
2771 (barf-if-buffer-read-only)
2772 (vc-buffer-sync)
2773 (undo-boundary)
2774 (goto-char (point-min))
2775 (push-mark)
2776 (message "Computing change log entries...")
2777 (message "Computing change log entries... %s"
2778 (unwind-protect
2779 (progn
2780 (setq default-directory odefault)
2781 (if (eq 0 (apply 'call-process
2782 (expand-file-name "rcs2log"
2783 exec-directory)
2784 nil (list t tempfile) nil
2785 "-c" changelog
2786 "-u" (concat (vc-user-login-name)
2787 "\t" full-name
2788 "\t" mailing-address)
2789 (mapcar
2790 (lambda (f)
2791 (file-relative-name
2792 (if (file-name-absolute-p f)
2793 f
2794 (concat odefault f))))
2795 files)))
2796 "done"
2797 (pop-to-buffer
2798 (set-buffer (get-buffer-create "*vc*")))
2799 (erase-buffer)
2800 (insert-file tempfile)
2801 "failed"))
2802 (setq default-directory (file-name-directory changelog))
2803 (delete-file tempfile)))))
2804
2805 ;; Annotate functionality
2806
2807 ;; Declare globally instead of additional parameter to
2808 ;; temp-buffer-show-function (not possible to pass more than one
2809 ;; parameter). The use of annotate-ratio is deprecated in favor of
2810 ;; annotate-mode, which replaces it with the more sensible "span-to
2811 ;; days", along with autoscaling support.
2812 (defvar vc-annotate-ratio nil "Global variable.")
2813 (defvar vc-annotate-backend nil "Global variable.")
2814
2815 ;; internal buffer-local variables
2816 (defvar vc-annotate-parent-file nil)
2817 (defvar vc-annotate-parent-rev nil)
2818 (defvar vc-annotate-parent-display-mode nil)
2819
2820 (defconst vc-annotate-font-lock-keywords
2821 ;; The fontification is done by vc-annotate-lines instead of font-lock.
2822 '((vc-annotate-lines)))
2823
2824 (defun vc-annotate-get-backend (buffer)
2825 "Return the backend matching \"Annotate\" buffer BUFFER.
2826 Return nil if no match made. Associations are made based on
2827 `vc-annotate-buffers'."
2828 (cdr (assoc buffer vc-annotate-buffers)))
2829
2830 (define-derived-mode vc-annotate-mode fundamental-mode "Annotate"
2831 "Major mode for output buffers of the `vc-annotate' command.
2832
2833 You can use the mode-specific menu to alter the time-span of the used
2834 colors. See variable `vc-annotate-menu-elements' for customizing the
2835 menu items."
2836 (set (make-local-variable 'truncate-lines) t)
2837 (set (make-local-variable 'font-lock-defaults)
2838 '(vc-annotate-font-lock-keywords t))
2839 (view-mode 1)
2840 (vc-annotate-add-menu))
2841
2842 (defun vc-annotate-display-default (&optional ratio)
2843 "Display the output of \\[vc-annotate] using the default color range.
2844 The color range is given by `vc-annotate-color-map', scaled by RATIO
2845 if present. The current time is used as the offset."
2846 (interactive "e")
2847 (message "Redisplaying annotation...")
2848 (vc-annotate-display
2849 (if ratio (vc-annotate-time-span vc-annotate-color-map ratio)))
2850 (message "Redisplaying annotation...done"))
2851
2852 (defun vc-annotate-display-autoscale (&optional full)
2853 "Highlight the output of \\[vc-annotate]] using an autoscaled color map.
2854 Autoscaling means that the map is scaled from the current time to the
2855 oldest annotation in the buffer, or, with argument FULL non-nil, to
2856 cover the range from the oldest annotation to the newest."
2857 (interactive)
2858 (let ((newest 0.0)
2859 (oldest 999999.) ;Any CVS users at the founding of Rome?
2860 (current (vc-annotate-convert-time (current-time)))
2861 date)
2862 (message "Redisplaying annotation...")
2863 ;; Run through this file and find the oldest and newest dates annotated.
2864 (save-excursion
2865 (goto-char (point-min))
2866 (while (setq date (vc-call-backend vc-annotate-backend 'annotate-time))
2867 (if (> date newest)
2868 (setq newest date))
2869 (if (< date oldest)
2870 (setq oldest date))))
2871 (vc-annotate-display
2872 (vc-annotate-time-span ;return the scaled colormap.
2873 vc-annotate-color-map
2874 (/ (- (if full newest current) oldest)
2875 (vc-annotate-car-last-cons vc-annotate-color-map)))
2876 (if full newest))
2877 (message "Redisplaying annotation...done \(%s\)"
2878 (if full
2879 (format "Spanned from %.1f to %.1f days old"
2880 (- current oldest)
2881 (- current newest))
2882 (format "Spanned to %.1f days old" (- current oldest))))))
2883
2884 ;; Menu -- Using easymenu.el
2885 (defun vc-annotate-add-menu ()
2886 "Add the menu 'Annotate' to the menu bar in VC-Annotate mode."
2887 (let ((menu-elements vc-annotate-menu-elements)
2888 (menu-def
2889 '("VC-Annotate"
2890 ["Default" (unless (null vc-annotate-display-mode)
2891 (setq vc-annotate-display-mode nil)
2892 (vc-annotate-display-select))
2893 :style toggle :selected (null vc-annotate-display-mode)]))
2894 (oldest-in-map (vc-annotate-car-last-cons vc-annotate-color-map)))
2895 (while menu-elements
2896 (let* ((element (car menu-elements))
2897 (days (* element oldest-in-map)))
2898 (setq menu-elements (cdr menu-elements))
2899 (setq menu-def
2900 (append menu-def
2901 `([,(format "Span %.1f days" days)
2902 (unless (and (numberp vc-annotate-display-mode)
2903 (= vc-annotate-display-mode ,days))
2904 (vc-annotate-display-select nil ,days))
2905 :style toggle :selected
2906 (and (numberp vc-annotate-display-mode)
2907 (= vc-annotate-display-mode ,days)) ])))))
2908 (setq menu-def
2909 (append menu-def
2910 (list
2911 ["Span ..."
2912 (let ((days
2913 (float (string-to-number
2914 (read-string "Span how many days? ")))))
2915 (vc-annotate-display-select nil days)) t])
2916 (list "--")
2917 (list
2918 ["Span to Oldest"
2919 (unless (eq vc-annotate-display-mode 'scale)
2920 (vc-annotate-display-select nil 'scale))
2921 :style toggle :selected
2922 (eq vc-annotate-display-mode 'scale)])
2923 (list
2924 ["Span Oldest->Newest"
2925 (unless (eq vc-annotate-display-mode 'fullscale)
2926 (vc-annotate-display-select nil 'fullscale))
2927 :style toggle :selected
2928 (eq vc-annotate-display-mode 'fullscale)])
2929 (list "--")
2930 (list ["Annotate previous revision"
2931 (call-interactively 'vc-annotate-prev-version)])
2932 (list ["Annotate next revision"
2933 (call-interactively 'vc-annotate-next-version)])
2934 (list ["Annotate revision at line"
2935 (vc-annotate-revision-at-line)])
2936 (list ["Annotate revision previous to line"
2937 (vc-annotate-revision-previous-to-line)])
2938 (list ["Annotate latest revision"
2939 (vc-annotate-workfile-version)])
2940 (list ["Show log of revision at line"
2941 (vc-annotate-show-log-revision-at-line)])
2942 (list ["Show diff of revision at line"
2943 (vc-annotate-show-diff-revision-at-line)])))
2944
2945 ;; Define the menu
2946 (if (or (featurep 'easymenu) (load "easymenu" t))
2947 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
2948 "VC Annotate Display Menu" menu-def))))
2949
2950 (defun vc-annotate-display-select (&optional buffer mode)
2951 "Highlight the output of \\[vc-annotate].
2952 By default, the current buffer is highlighted, unless overridden by
2953 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
2954 use; you may override this using the second optional arg MODE."
2955 (interactive)
2956 (if mode (setq vc-annotate-display-mode mode))
2957 (when buffer
2958 (set-buffer buffer)
2959 (display-buffer buffer))
2960 (if (not vc-annotate-mode) ; Turn on vc-annotate-mode if not done
2961 (vc-annotate-mode))
2962 (cond ((null vc-annotate-display-mode)
2963 (vc-annotate-display-default vc-annotate-ratio))
2964 ;; One of the auto-scaling modes
2965 ((eq vc-annotate-display-mode 'scale)
2966 (vc-annotate-display-autoscale))
2967 ((eq vc-annotate-display-mode 'fullscale)
2968 (vc-annotate-display-autoscale t))
2969 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
2970 (vc-annotate-display-default
2971 (/ vc-annotate-display-mode (vc-annotate-car-last-cons
2972 vc-annotate-color-map))))
2973 (t (error "No such display mode: %s"
2974 vc-annotate-display-mode))))
2975
2976 ;;;; (defun vc-BACKEND-annotate-command (file buffer) ...)
2977 ;;;; Execute "annotate" on FILE by using `call-process' and insert
2978 ;;;; the contents in BUFFER.
2979
2980 ;;;###autoload
2981 (defun vc-annotate (prefix &optional revision display-mode)
2982 "Display the edit history of the current file using colours.
2983
2984 This command creates a buffer that shows, for each line of the current
2985 file, when it was last edited and by whom. Additionally, colours are
2986 used to show the age of each line--blue means oldest, red means
2987 youngest, and intermediate colours indicate intermediate ages. By
2988 default, the time scale stretches back one year into the past;
2989 everything that is older than that is shown in blue.
2990
2991 With a prefix argument, this command asks two questions in the
2992 minibuffer. First, you may enter a version number; then the buffer
2993 displays and annotates that version instead of the current version
2994 \(type RET in the minibuffer to leave that default unchanged). Then,
2995 you are prompted for the time span in days which the color range
2996 should cover. For example, a time span of 20 days means that changes
2997 over the past 20 days are shown in red to blue, according to their
2998 age, and everything that is older than that is shown in blue.
2999
3000 Customization variables:
3001
3002 `vc-annotate-menu-elements' customizes the menu elements of the
3003 mode-specific menu. `vc-annotate-color-map' and
3004 `vc-annotate-very-old-color' defines the mapping of time to
3005 colors. `vc-annotate-background' specifies the background color."
3006 (interactive "P")
3007 (vc-ensure-vc-buffer)
3008 (let* ((temp-buffer-name nil)
3009 (temp-buffer-show-function 'vc-annotate-display-select)
3010 (rev (or revision (vc-workfile-version buffer-file-name)))
3011 (bfn buffer-file-name)
3012 (vc-annotate-version
3013 (if prefix (read-string
3014 (format "Annotate from version: (default %s) " rev)
3015 nil nil rev)
3016 rev)))
3017 (if display-mode
3018 (setq vc-annotate-display-mode display-mode)
3019 (if prefix
3020 (setq vc-annotate-display-mode
3021 (float (string-to-number
3022 (read-string "Annotate span days: (default 20) "
3023 nil nil "20"))))))
3024 (setq temp-buffer-name (format "*Annotate %s (rev %s)*"
3025 (buffer-name) vc-annotate-version))
3026 (setq vc-annotate-backend (vc-backend buffer-file-name))
3027 (message "Annotating...")
3028 (if (not (vc-find-backend-function vc-annotate-backend 'annotate-command))
3029 (error "Sorry, annotating is not implemented for %s"
3030 vc-annotate-backend))
3031 (with-output-to-temp-buffer temp-buffer-name
3032 (vc-call-backend vc-annotate-backend 'annotate-command
3033 buffer-file-name
3034 (get-buffer temp-buffer-name)
3035 vc-annotate-version))
3036 (save-excursion
3037 (set-buffer temp-buffer-name)
3038 (set (make-local-variable 'vc-annotate-parent-file) bfn)
3039 (set (make-local-variable 'vc-annotate-parent-rev) vc-annotate-version)
3040 (set (make-local-variable 'vc-annotate-parent-display-mode)
3041 vc-annotate-display-mode))
3042
3043 ;; Don't use the temp-buffer-name until the buffer is created
3044 ;; (only after `with-output-to-temp-buffer'.)
3045 (setq vc-annotate-buffers
3046 (append vc-annotate-buffers
3047 (list (cons (get-buffer temp-buffer-name) vc-annotate-backend))))
3048 (message "Annotating... done")))
3049
3050 (defun vc-annotate-prev-version (prefix)
3051 "Visit the annotation of the version previous to this one.
3052
3053 With a numeric prefix argument, annotate the version that many
3054 versions previous."
3055 (interactive "p")
3056 (vc-annotate-warp-version (- 0 prefix)))
3057
3058 (defun vc-annotate-next-version (prefix)
3059 "Visit the annotation of the version after this one.
3060
3061 With a numeric prefix argument, annotate the version that many
3062 versions after."
3063 (interactive "p")
3064 (vc-annotate-warp-version prefix))
3065
3066 (defun vc-annotate-workfile-version ()
3067 "Visit the annotation of the workfile version of this file."
3068 (interactive)
3069 (if (not (equal major-mode 'vc-annotate-mode))
3070 (message "Cannot be invoked outside of a vc annotate buffer")
3071 (let ((warp-rev (vc-workfile-version vc-annotate-parent-file)))
3072 (if (equal warp-rev vc-annotate-parent-rev)
3073 (message "Already at version %s" warp-rev)
3074 (vc-annotate-warp-version warp-rev)))))
3075
3076 (defun vc-annotate-extract-revision-at-line ()
3077 "Extract the revision number of the current line."
3078 ;; This function must be invoked from a buffer in vc-annotate-mode
3079 (save-window-excursion
3080 (vc-ensure-vc-buffer)
3081 (setq vc-annotate-backend (vc-backend buffer-file-name)))
3082 (vc-call-backend vc-annotate-backend 'annotate-extract-revision-at-line))
3083
3084 (defun vc-annotate-revision-at-line ()
3085 "Visit the annotation of the version identified in the current line."
3086 (interactive)
3087 (if (not (equal major-mode 'vc-annotate-mode))
3088 (message "Cannot be invoked outside of a vc annotate buffer")
3089 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3090 (if (not rev-at-line)
3091 (message "Cannot extract revision number from the current line")
3092 (if (equal rev-at-line vc-annotate-parent-rev)
3093 (message "Already at version %s" rev-at-line)
3094 (vc-annotate-warp-version rev-at-line))))))
3095
3096 (defun vc-annotate-revision-previous-to-line ()
3097 "Visit the annotation of the version before the version at line."
3098 (interactive)
3099 (if (not (equal major-mode 'vc-annotate-mode))
3100 (message "Cannot be invoked outside of a vc annotate buffer")
3101 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3102 (prev-rev nil))
3103 (if (not rev-at-line)
3104 (message "Cannot extract revision number from the current line")
3105 (setq prev-rev
3106 (vc-call previous-version vc-annotate-parent-file rev-at-line))
3107 (vc-annotate-warp-version prev-rev)))))
3108
3109 (defun vc-annotate-show-log-revision-at-line ()
3110 "Visit the log of the version at line."
3111 (interactive)
3112 (if (not (equal major-mode 'vc-annotate-mode))
3113 (message "Cannot be invoked outside of a vc annotate buffer")
3114 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
3115 (if (not rev-at-line)
3116 (message "Cannot extract revision number from the current line")
3117 (vc-print-log rev-at-line)))))
3118
3119 (defun vc-annotate-show-diff-revision-at-line ()
3120 "Visit the diff of the version at line from its previous version."
3121 (interactive)
3122 (if (not (equal major-mode 'vc-annotate-mode))
3123 (message "Cannot be invoked outside of a vc annotate buffer")
3124 (let ((rev-at-line (vc-annotate-extract-revision-at-line))
3125 (prev-rev nil))
3126 (if (not rev-at-line)
3127 (message "Cannot extract revision number from the current line")
3128 (setq prev-rev
3129 (vc-call previous-version vc-annotate-parent-file rev-at-line))
3130 (if (not prev-rev)
3131 (message "Cannot diff from any version prior to %s" rev-at-line)
3132 (save-window-excursion
3133 (vc-version-diff vc-annotate-parent-file prev-rev rev-at-line))
3134 (switch-to-buffer "*vc-diff*"))))))
3135
3136 (defun vc-annotate-warp-version (revspec)
3137 "Annotate the version described by REVSPEC.
3138
3139 If REVSPEC is a positive integer, warp that many versions
3140 forward, if possible, otherwise echo a warning message. If
3141 REVSPEC is a negative integer, warp that many versions backward,
3142 if possible, otherwise echo a warning message. If REVSPEC is a
3143 string, then it describes a revision number, so warp to that
3144 revision."
3145 (if (not (equal major-mode 'vc-annotate-mode))
3146 (message "Cannot be invoked outside of a vc annotate buffer")
3147 (let* ((oldline (line-at-pos))
3148 (revspeccopy revspec)
3149 (newrev nil))
3150 (cond
3151 ((and (integerp revspec) (> revspec 0))
3152 (setq newrev vc-annotate-parent-rev)
3153 (while (and (> revspec 0) newrev)
3154 (setq newrev (vc-call next-version
3155 vc-annotate-parent-file newrev))
3156 (setq revspec (1- revspec)))
3157 (if (not newrev)
3158 (message "Cannot increment %d versions from version %s"
3159 revspeccopy vc-annotate-parent-rev)))
3160 ((and (integerp revspec) (< revspec 0))
3161 (setq newrev vc-annotate-parent-rev)
3162 (while (and (< revspec 0) newrev)
3163 (setq newrev (vc-call previous-version
3164 vc-annotate-parent-file newrev))
3165 (setq revspec (1+ revspec)))
3166 (if (not newrev)
3167 (message "Cannot decrement %d versions from version %s"
3168 (- 0 revspeccopy) vc-annotate-parent-rev)))
3169 ((stringp revspec) (setq newrev revspec))
3170 (t (error "Invalid argument to vc-annotate-warp-version")))
3171 (when newrev
3172 (save-window-excursion
3173 (find-file vc-annotate-parent-file)
3174 (vc-annotate nil newrev vc-annotate-parent-display-mode))
3175 (kill-buffer (current-buffer)) ;; kill the buffer we started from
3176 (switch-to-buffer (car (car (last vc-annotate-buffers))))
3177 (goto-line (min oldline (progn (goto-char (point-max))
3178 (previous-line)
3179 (line-at-pos))))))))
3180
3181 (defun vc-annotate-car-last-cons (a-list)
3182 "Return car of last cons in association list A-LIST."
3183 (if (not (eq nil (cdr a-list)))
3184 (vc-annotate-car-last-cons (cdr a-list))
3185 (car (car a-list))))
3186
3187 (defun vc-annotate-time-span (a-list span &optional quantize)
3188 "Apply factor SPAN to the time-span of association list A-LIST.
3189 Return the new alist.
3190 Optionally quantize to the factor of QUANTIZE."
3191 ;; Apply span to each car of every cons
3192 (if (not (eq nil a-list))
3193 (append (list (cons (* (car (car a-list)) span)
3194 (cdr (car a-list))))
3195 (vc-annotate-time-span (nthcdr (or quantize ; optional
3196 1) ; Default to cdr
3197 a-list) span quantize))))
3198
3199 (defun vc-annotate-compcar (threshold a-list)
3200 "Test successive cons cells of A-LIST against THRESHOLD.
3201 Return the first cons cell with a car that is not less than THRESHOLD,
3202 nil if no such cell exists."
3203 (let ((i 1)
3204 (tmp-cons (car a-list)))
3205 (while (and tmp-cons (< (car tmp-cons) threshold))
3206 (setq tmp-cons (car (nthcdr i a-list)))
3207 (setq i (+ i 1)))
3208 tmp-cons)) ; Return the appropriate value
3209
3210 (defun vc-annotate-convert-time (time)
3211 "Convert a time value to a floating-point number of days.
3212 The argument TIME is a list as returned by `current-time' or
3213 `encode-time', only the first two elements of that list are considered."
3214 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
3215
3216 (defun vc-annotate-difference (&optional offset)
3217 "Return the time span in days to the next annotation.
3218 This calls the backend function annotate-time, and returns the
3219 difference in days between the time returned and the current time,
3220 or OFFSET if present."
3221 (let ((next-time (vc-call-backend vc-annotate-backend 'annotate-time)))
3222 (if next-time
3223 (- (or offset
3224 (vc-call-backend vc-annotate-backend 'annotate-current-time))
3225 next-time))))
3226
3227 (defun vc-default-annotate-current-time (backend)
3228 "Return the current time, encoded as fractional days."
3229 (vc-annotate-convert-time (current-time)))
3230
3231 (defvar vc-annotate-offset nil)
3232
3233 (defun vc-annotate-display (&optional color-map offset)
3234 "Highlight `vc-annotate' output in the current buffer.
3235 COLOR-MAP, if present, overrides `vc-annotate-color-map'.
3236 The annotations are relative to the current time, unless overridden by OFFSET."
3237 (if (and color-map (not (eq color-map vc-annotate-color-map)))
3238 (set (make-local-variable 'vc-annotate-color-map) color-map))
3239 (set (make-local-variable 'vc-annotate-offset) offset)
3240 (font-lock-mode 1))
3241
3242 (defun vc-annotate-lines (limit)
3243 (let (difference)
3244 (while (and (< (point) limit)
3245 (setq difference (vc-annotate-difference vc-annotate-offset)))
3246 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
3247 (cons nil vc-annotate-very-old-color)))
3248 ;; substring from index 1 to remove any leading `#' in the name
3249 (face-name (concat "vc-annotate-face-" (substring (cdr color) 1)))
3250 ;; Make the face if not done.
3251 (face (or (intern-soft face-name)
3252 (let ((tmp-face (make-face (intern face-name))))
3253 (set-face-foreground tmp-face (cdr color))
3254 (if vc-annotate-background
3255 (set-face-background tmp-face
3256 vc-annotate-background))
3257 tmp-face))) ; Return the face
3258 (point (point)))
3259 (forward-line 1)
3260 (put-text-property point (point) 'face face)))
3261 ;; Pretend to font-lock there were no matches.
3262 nil))
3263 \f
3264 ;; Collect back-end-dependent stuff here
3265
3266 (defalias 'vc-default-logentry-check 'ignore)
3267
3268 (defun vc-check-headers ()
3269 "Check if the current file has any headers in it."
3270 (interactive)
3271 (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
3272
3273 (defun vc-default-check-headers (backend)
3274 "Default implementation of check-headers; always returns nil."
3275 nil)
3276
3277 ;; Back-end-dependent stuff ends here.
3278
3279 ;; Set up key bindings for use while editing log messages
3280
3281 (defun vc-log-edit (file)
3282 "Set up `log-edit' for use with VC on FILE."
3283 (setq default-directory
3284 (if file (file-name-directory file)
3285 (with-current-buffer vc-parent-buffer default-directory)))
3286 (log-edit 'vc-finish-logentry nil
3287 (if file `(lambda () ',(list (file-name-nondirectory file)))
3288 ;; If FILE is nil, we were called from vc-dired.
3289 (lambda ()
3290 (with-current-buffer vc-parent-buffer
3291 (dired-get-marked-files t)))))
3292 (set (make-local-variable 'vc-log-file) file)
3293 (make-local-variable 'vc-log-version)
3294 (set-buffer-modified-p nil)
3295 (setq buffer-file-name nil))
3296
3297 ;; These things should probably be generally available
3298
3299 (defun vc-file-tree-walk (dirname func &rest args)
3300 "Walk recursively through DIRNAME.
3301 Invoke FUNC f ARGS on each VC-managed file f underneath it."
3302 (vc-file-tree-walk-internal (expand-file-name dirname) func args)
3303 (message "Traversing directory %s...done" dirname))
3304
3305 (defun vc-file-tree-walk-internal (file func args)
3306 (if (not (file-directory-p file))
3307 (if (vc-backend file) (apply func file args))
3308 (message "Traversing directory %s..." (abbreviate-file-name file))
3309 (let ((dir (file-name-as-directory file)))
3310 (mapcar
3311 (lambda (f) (or
3312 (string-equal f ".")
3313 (string-equal f "..")
3314 (member f vc-directory-exclusion-list)
3315 (let ((dirf (expand-file-name f dir)))
3316 (or
3317 (file-symlink-p dirf);; Avoid possible loops
3318 (vc-file-tree-walk-internal dirf func args)))))
3319 (directory-files dir)))))
3320
3321 (provide 'vc)
3322
3323 ;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
3324 ;;
3325 ;; These may be useful to anyone who has to debug or extend the package.
3326 ;; (Note that this information corresponds to versions 5.x. Some of it
3327 ;; might have been invalidated by the additions to support branching
3328 ;; and RCS keyword lookup. AS, 1995/03/24)
3329 ;;
3330 ;; A fundamental problem in VC is that there are time windows between
3331 ;; vc-next-action's computations of the file's version-control state and
3332 ;; the actions that change it. This is a window open to lossage in a
3333 ;; multi-user environment; someone else could nip in and change the state
3334 ;; of the master during it.
3335 ;;
3336 ;; The performance problem is that rlog/prs calls are very expensive; we want
3337 ;; to avoid them as much as possible.
3338 ;;
3339 ;; ANALYSIS:
3340 ;;
3341 ;; The performance problem, it turns out, simplifies in practice to the
3342 ;; problem of making vc-state fast. The two other functions that call
3343 ;; prs/rlog will not be so commonly used that the slowdown is a problem; one
3344 ;; makes snapshots, the other deletes the calling user's last change in the
3345 ;; master.
3346 ;;
3347 ;; The race condition implies that we have to either (a) lock the master
3348 ;; during the entire execution of vc-next-action, or (b) detect and
3349 ;; recover from errors resulting from dispatch on an out-of-date state.
3350 ;;
3351 ;; Alternative (a) appears to be infeasible. The problem is that we can't
3352 ;; guarantee that the lock will ever be removed. Suppose a user starts a
3353 ;; checkin, the change message buffer pops up, and the user, having wandered
3354 ;; off to do something else, simply forgets about it?
3355 ;;
3356 ;; Alternative (b), on the other hand, works well with a cheap way to speed up
3357 ;; vc-state. Usually, if a file is registered, we can read its locked/
3358 ;; unlocked state and its current owner from its permissions.
3359 ;;
3360 ;; This shortcut will fail if someone has manually changed the workfile's
3361 ;; permissions; also if developers are munging the workfile in several
3362 ;; directories, with symlinks to a master (in this latter case, the
3363 ;; permissions shortcut will fail to detect a lock asserted from another
3364 ;; directory).
3365 ;;
3366 ;; Note that these cases correspond exactly to the errors which could happen
3367 ;; because of a competing checkin/checkout race in between two instances of
3368 ;; vc-next-action.
3369 ;;
3370 ;; For VC's purposes, a workfile/master pair may have the following states:
3371 ;;
3372 ;; A. Unregistered. There is a workfile, there is no master.
3373 ;;
3374 ;; B. Registered and not locked by anyone.
3375 ;;
3376 ;; C. Locked by calling user and unchanged.
3377 ;;
3378 ;; D. Locked by the calling user and changed.
3379 ;;
3380 ;; E. Locked by someone other than the calling user.
3381 ;;
3382 ;; This makes for 25 states and 20 error conditions. Here's the matrix:
3383 ;;
3384 ;; VC's idea of state
3385 ;; |
3386 ;; V Actual state RCS action SCCS action Effect
3387 ;; A B C D E
3388 ;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
3389 ;; B 5 . 6 7 8 co -l get -e checkout
3390 ;; C 9 10 . 11 12 co -u unget; get revert
3391 ;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
3392 ;; E 17 18 19 20 . rcs -u -M -l unget -n ; get -g steal lock
3393 ;;
3394 ;; All commands take the master file name as a last argument (not shown).
3395 ;;
3396 ;; In the discussion below, a "self-race" is a pathological situation in
3397 ;; which VC operations are being attempted simultaneously by two or more
3398 ;; Emacsen running under the same username.
3399 ;;
3400 ;; The vc-next-action code has the following windows:
3401 ;;
3402 ;; Window P:
3403 ;; Between the check for existence of a master file and the call to
3404 ;; admin/checkin in vc-buffer-admin (apparent state A). This window may
3405 ;; never close if the initial-comment feature is on.
3406 ;;
3407 ;; Window Q:
3408 ;; Between the call to vc-workfile-unchanged-p in and the immediately
3409 ;; following revert (apparent state C).
3410 ;;
3411 ;; Window R:
3412 ;; Between the call to vc-workfile-unchanged-p in and the following
3413 ;; checkin (apparent state D). This window may never close.
3414 ;;
3415 ;; Window S:
3416 ;; Between the unlock and the immediately following checkout during a
3417 ;; revert operation (apparent state C). Included in window Q.
3418 ;;
3419 ;; Window T:
3420 ;; Between vc-state and the following checkout (apparent state B).
3421 ;;
3422 ;; Window U:
3423 ;; Between vc-state and the following revert (apparent state C).
3424 ;; Includes windows Q and S.
3425 ;;
3426 ;; Window V:
3427 ;; Between vc-state and the following checkin (apparent state
3428 ;; D). This window may never be closed if the user fails to complete the
3429 ;; checkin message. Includes window R.
3430 ;;
3431 ;; Window W:
3432 ;; Between vc-state and the following steal-lock (apparent
3433 ;; state E). This window may never close if the user fails to complete
3434 ;; the steal-lock message. Includes window X.
3435 ;;
3436 ;; Window X:
3437 ;; Between the unlock and the immediately following re-lock during a
3438 ;; steal-lock operation (apparent state E). This window may never close
3439 ;; if the user fails to complete the steal-lock message.
3440 ;;
3441 ;; Errors:
3442 ;;
3443 ;; Apparent state A ---
3444 ;;
3445 ;; 1. File looked unregistered but is actually registered and not locked.
3446 ;;
3447 ;; Potential cause: someone else's admin during window P, with
3448 ;; caller's admin happening before their checkout.
3449 ;;
3450 ;; RCS: Prior to version 5.6.4, ci fails with message
3451 ;; "no lock set by <user>". From 5.6.4 onwards, VC uses the new
3452 ;; ci -i option and the message is "<file>,v: already exists".
3453 ;; SCCS: admin will fail with error (ad19).
3454 ;;
3455 ;; We can let these errors be passed up to the user.
3456 ;;
3457 ;; 2. File looked unregistered but is actually locked by caller, unchanged.
3458 ;;
3459 ;; Potential cause: self-race during window P.
3460 ;;
3461 ;; RCS: Prior to version 5.6.4, reverts the file to the last saved
3462 ;; version and unlocks it. From 5.6.4 onwards, VC uses the new
3463 ;; ci -i option, failing with message "<file>,v: already exists".
3464 ;; SCCS: will fail with error (ad19).
3465 ;;
3466 ;; Either of these consequences is acceptable.
3467 ;;
3468 ;; 3. File looked unregistered but is actually locked by caller, changed.
3469 ;;
3470 ;; Potential cause: self-race during window P.
3471 ;;
3472 ;; RCS: Prior to version 5.6.4, VC registers the caller's workfile as
3473 ;; a delta with a null change comment (the -t- switch will be
3474 ;; ignored). From 5.6.4 onwards, VC uses the new ci -i option,
3475 ;; failing with message "<file>,v: already exists".
3476 ;; SCCS: will fail with error (ad19).
3477 ;;
3478 ;; 4. File looked unregistered but is locked by someone else.
3479 ;;;
3480 ;; Potential cause: someone else's admin during window P, with
3481 ;; caller's admin happening *after* their checkout.
3482 ;;
3483 ;; RCS: Prior to version 5.6.4, ci fails with a
3484 ;; "no lock set by <user>" message. From 5.6.4 onwards,
3485 ;; VC uses the new ci -i option, failing with message
3486 ;; "<file>,v: already exists".
3487 ;; SCCS: will fail with error (ad19).
3488 ;;
3489 ;; We can let these errors be passed up to the user.
3490 ;;
3491 ;; Apparent state B ---
3492 ;;
3493 ;; 5. File looked registered and not locked, but is actually unregistered.
3494 ;;
3495 ;; Potential cause: master file got nuked during window P.
3496 ;;
3497 ;; RCS: will fail with "RCS/<file>: No such file or directory"
3498 ;; SCCS: will fail with error ut4.
3499 ;;
3500 ;; We can let these errors be passed up to the user.
3501 ;;
3502 ;; 6. File looked registered and not locked, but is actually locked by the
3503 ;; calling user and unchanged.
3504 ;;
3505 ;; Potential cause: self-race during window T.
3506 ;;
3507 ;; RCS: in the same directory as the previous workfile, co -l will fail
3508 ;; with "co error: writable foo exists; checkout aborted". In any other
3509 ;; directory, checkout will succeed.
3510 ;; SCCS: will fail with ge17.
3511 ;;
3512 ;; Either of these consequences is acceptable.
3513 ;;
3514 ;; 7. File looked registered and not locked, but is actually locked by the
3515 ;; calling user and changed.
3516 ;;
3517 ;; As case 6.
3518 ;;
3519 ;; 8. File looked registered and not locked, but is actually locked by another
3520 ;; user.
3521 ;;
3522 ;; Potential cause: someone else checks it out during window T.
3523 ;;
3524 ;; RCS: co error: revision 1.3 already locked by <user>
3525 ;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
3526 ;;
3527 ;; We can let these errors be passed up to the user.
3528 ;;
3529 ;; Apparent state C ---
3530 ;;
3531 ;; 9. File looks locked by calling user and unchanged, but is unregistered.
3532 ;;
3533 ;; As case 5.
3534 ;;
3535 ;; 10. File looks locked by calling user and unchanged, but is actually not
3536 ;; locked.
3537 ;;
3538 ;; Potential cause: a self-race in window U, or by the revert's
3539 ;; landing during window X of some other user's steal-lock or window S
3540 ;; of another user's revert.
3541 ;;
3542 ;; RCS: succeeds, refreshing the file from the identical version in
3543 ;; the master.
3544 ;; SCCS: fails with error ut4 (p file nonexistent).
3545 ;;
3546 ;; Either of these consequences is acceptable.
3547 ;;
3548 ;; 11. File is locked by calling user. It looks unchanged, but is actually
3549 ;; changed.
3550 ;;
3551 ;; Potential cause: the file would have to be touched by a self-race
3552 ;; during window Q.
3553 ;;
3554 ;; The revert will succeed, removing whatever changes came with
3555 ;; the touch. It is theoretically possible that work could be lost.
3556 ;;
3557 ;; 12. File looks like it's locked by the calling user and unchanged, but
3558 ;; it's actually locked by someone else.
3559 ;;
3560 ;; Potential cause: a steal-lock in window V.
3561 ;;
3562 ;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
3563 ;; SCCS: fails with error un2
3564 ;;
3565 ;; We can pass these errors up to the user.
3566 ;;
3567 ;; Apparent state D ---
3568 ;;
3569 ;; 13. File looks like it's locked by the calling user and changed, but it's
3570 ;; actually unregistered.
3571 ;;
3572 ;; Potential cause: master file got nuked during window P.
3573 ;;
3574 ;; RCS: Prior to version 5.6.4, checks in the user's version as an
3575 ;; initial delta. From 5.6.4 onwards, VC uses the new ci -j
3576 ;; option, failing with message "no such file or directory".
3577 ;; SCCS: will fail with error ut4.
3578 ;;
3579 ;; This case is kind of nasty. Under RCS prior to version 5.6.4,
3580 ;; VC may fail to detect the loss of previous version information.
3581 ;;
3582 ;; 14. File looks like it's locked by the calling user and changed, but it's
3583 ;; actually unlocked.
3584 ;;
3585 ;; Potential cause: self-race in window V, or the checkin happening
3586 ;; during the window X of someone else's steal-lock or window S of
3587 ;; someone else's revert.
3588 ;;
3589 ;; RCS: ci will fail with "no lock set by <user>".
3590 ;; SCCS: delta will fail with error ut4.
3591 ;;
3592 ;; 15. File looks like it's locked by the calling user and changed, but it's
3593 ;; actually locked by the calling user and unchanged.
3594 ;;
3595 ;; Potential cause: another self-race --- a whole checkin/checkout
3596 ;; sequence by the calling user would have to land in window R.
3597 ;;
3598 ;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
3599 ;; RCS: reverts to the file state as of the second user's checkin, leaving
3600 ;; the file unlocked.
3601 ;;
3602 ;; It is theoretically possible that work could be lost under RCS.
3603 ;;
3604 ;; 16. File looks like it's locked by the calling user and changed, but it's
3605 ;; actually locked by a different user.
3606 ;;
3607 ;; RCS: ci error: no lock set by <user>
3608 ;; SCCS: unget will fail with error un2
3609 ;;
3610 ;; We can pass these errors up to the user.
3611 ;;
3612 ;; Apparent state E ---
3613 ;;
3614 ;; 17. File looks like it's locked by some other user, but it's actually
3615 ;; unregistered.
3616 ;;
3617 ;; As case 13.
3618 ;;
3619 ;; 18. File looks like it's locked by some other user, but it's actually
3620 ;; unlocked.
3621 ;;
3622 ;; Potential cause: someone released a lock during window W.
3623 ;;
3624 ;; RCS: The calling user will get the lock on the file.
3625 ;; SCCS: unget -n will fail with cm4.
3626 ;;
3627 ;; Either of these consequences will be OK.
3628 ;;
3629 ;; 19. File looks like it's locked by some other user, but it's actually
3630 ;; locked by the calling user and unchanged.
3631 ;;
3632 ;; Potential cause: the other user relinquishing a lock followed by
3633 ;; a self-race, both in window W.
3634 ;;
3635 ;; Under both RCS and SCCS, both unlock and lock will succeed, making
3636 ;; the sequence a no-op.
3637 ;;
3638 ;; 20. File looks like it's locked by some other user, but it's actually
3639 ;; locked by the calling user and changed.
3640 ;;
3641 ;; As case 19.
3642 ;;
3643 ;; PROBLEM CASES:
3644 ;;
3645 ;; In order of decreasing severity:
3646 ;;
3647 ;; Cases 11 and 15 are the only ones that potentially lose work.
3648 ;; They would require a self-race for this to happen.
3649 ;;
3650 ;; Case 13 in RCS loses information about previous deltas, retaining
3651 ;; only the information in the current workfile. This can only happen
3652 ;; if the master file gets nuked in window P.
3653 ;;
3654 ;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
3655 ;; no change comment in the master. This would require a self-race in
3656 ;; window P or R respectively.
3657 ;;
3658 ;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
3659 ;;
3660 ;; Unfortunately, it appears to me that no recovery is possible in these
3661 ;; cases. They don't yield error messages, so there's no way to tell that
3662 ;; a race condition has occurred.
3663 ;;
3664 ;; All other cases don't change either the workfile or the master, and
3665 ;; trigger command errors which the user will see.
3666 ;;
3667 ;; Thus, there is no explicit recovery code.
3668
3669 ;;; arch-tag: ca82c1de-3091-4e26-af92-460abc6213a6
3670 ;;; vc.el ends here