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