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