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