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