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