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