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