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