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