(vc-update-change-log): Use shell-command, not shell-command-on-region.
[bpt/emacs.git] / lisp / vc.el
CommitLineData
594722a8
ER
1;;; vc.el --- drive a version-control system from within Emacs
2
3;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6;; Version: 4.0
7
f35ecf88 8;; $Id: vc.el,v 1.3 1992/08/08 22:58:39 rms Exp roland $
594722a8
ER
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
24;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26;;; Commentary:
27
28;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
29;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
30;; and Richard Stallman contributed valuable criticism, support, and testing.
31;;
32;; Supported version-control systems presently include SCCS and RCS;
33;; your RCS version should be 5.6.2 or later for proper operation of
34;; the lock-breaking code.
35;;
36;; The RCS code assumes strict locking. You can support the RCS -x option
37;; by adding pairs to the vc-master-templates list.
38;;
39;; Proper function of the SCCS diff commands requires the shellscript vcdiff
40;; to be installed somewhere on Emacs's path for executables.
41;;
42;; This code depends on call-process passing back the subprocess exit
43;; status. Thus, you need Emacs 18.58 or later to run it.
44;;
45;; The vc code maintains some internal state in order to reduce expensive
46;; version-control operations to a minimum. Some names are only computed
47;; once. If you perform version control operations with RCS/SCCS/CVS while
48;; vc's back is turned, or move/rename master files while vc is running,
49;; vc may get seriously confused. Don't do these things!
50;;
51;; Developer's notes on some concurrency issues are included at the end of
52;; the file.
53
54;;; Code:
55
56(require 'vc-hooks)
57
58;; General customization
59
60(defvar vc-default-back-end nil
61 "*Back-end actually used by this interface; may be SCCS or RCS.
62The value is only computed when needed to avoid an expensive search.")
63(defvar vc-diff-options '("-a" "-c1")
64 "*The command/flags list to be used in constructing diff commands.")
65(defvar vc-suppress-confirm nil
66 "*If non-nil, reat user as expert; suppress yes-no prompts on some things.")
67(defvar vc-keep-workfiles t
68 "*If non-nil, don't delete working files after registering changes.")
69(defvar vc-initial-comment nil
70 "*Prompt for initial comment when a file is registered.")
71(defvar vc-command-messages nil
72 "*Display run messages from back-end commands.")
73(defvar vc-mistrust-permissions 'file-symlink-p
74 "*Don't assume that permissions and ownership track version-control status.")
75
76;; Header-insertion hair
77
78(defvar vc-header-alist
79 '((SCCS "\%W\%") (RCS "\$Id\$"))
80 "*Header keywords to be inserted when vc-insert-header is executed.")
81(defconst vc-static-header-alist
82 '(("\\.c$" .
83 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
84 "*Associate static header string templates with file types. A \%s in the
85template is replaced with the first string associated with the file's
86verson-control type in vc-header-strings.")
87(defvar vc-comment-alist
88 '((nroff-mode ".\\\"" ""))
89 "*Special comment delimiters to be used in generating vc headers only.
90Add an entry in this list if you need to override the normal comment-start
91and comment-end variables. This will only be necessary if the mode language
92is sensitive to blank lines.")
93
94;; Variables the user doesn't need to know about.
95(defvar vc-log-entry-mode nil)
96(defvar vc-log-operation nil)
97
98(defconst vc-name-assoc-file "VC-names")
99
100(defmacro vc-error-occurred (&rest body)
101 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
102
103;; File property caching
104
105(defun vc-file-clearprops (file)
106 ;; clear all properties of a given file
107 (setplist (intern file vc-file-prop-obarray) nil))
108
109;; Random helper functions
110
111(defun vc-name (file)
112 "Return the master name of a file, nil if it is not registered"
113 (or (vc-file-getprop file 'vc-name)
114 (vc-file-setprop file 'vc-name
115 (let ((name-and-type (vc-registered file)))
116 (and name-and-type (car name-and-type))))))
117
118(defvar vc-binary-assoc nil)
119
120(defun vc-find-binary (name)
121 "Look for a command anywhere on the subprocess-command search path."
122 (or (cdr (assoc name vc-binary-assoc))
123 (let ((full nil))
124 (catch 'found
125 (mapcar
126 (function (lambda (s)
127 (if (and s (file-exists-p (setq full (concat s "/" name))))
128 (throw 'found nil))))
129 exec-path))
130 (if full
131 (setq vc-binary-assoc (cons (cons name full) vc-binary-assoc)))
132 full)))
133
134(defun vc-do-command (okstatus command file &rest flags)
135 "Execute a version-control command, notifying user and checking for errors.
136The command is successful if its exit status does not exceed OKSTATUS.
137Output from COMMAND goes to buffer *vc*. The last argument of the command is
138the master name of FILE; this is appended to an optional list of FLAGS."
139 (setq file (expand-file-name file))
140 (if vc-command-messages
141 (message (format "Running %s on %s..." command file)))
142 (let ((obuf (current-buffer))
143 (squeezed nil)
144 (vc-file (and file (vc-name file)))
145 status)
146 (set-buffer (get-buffer-create "*vc*"))
147 (erase-buffer)
148 (mapcar
149 (function (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
150 flags)
151 (if vc-file
152 (setq squeezed (append squeezed (list vc-file))))
153 (let
154 ((default-directory (file-name-directory (or file "./"))))
155 (setq status (apply 'call-process command nil t nil squeezed))
156 )
157 (goto-char (point-max))
158 (previous-line 1)
159 (if (or (not (integerp status)) (< okstatus status))
160 (progn
161 (previous-line 1)
162 (print (cons command squeezed))
163 (next-line 1)
164 (pop-to-buffer "*vc*")
165 (vc-shrink-to-fit)
166 (goto-char (point-min))
167 (error (format "Running %s...FAILED (%s)" command
168 (if (integerp status)
169 (format "status %d" status)
170 status)))
171 )
172 (if vc-command-messages
173 (message (format "Running %s...OK" command)))
174 )
175 (set-buffer obuf)
176 status)
177 )
178
179(defun vc-revert-buffer1 (&optional arg no-confirm)
180 ;; This code was shamelessly lifted from Sebastian Kremer's rcs.el mode.
181 ;; Revert buffer, try to keep point where user expects it in spite
182 ;; of changes because of expanded version-control key words.
183 ;; This is quite important since otherwise typeahead won't work as expected.
184 (interactive "P")
185 (widen)
186 (let* ((opoint (point))
187 (osize (buffer-size))
188 diff
189 (context 100)
190 (ostring (buffer-substring (point)
191 (min (point-max)
192 (+ (point) context))))
193 (l (length ostring)))
194 (revert-buffer arg no-confirm)
195 (setq diff (- osize (buffer-size)))
196 (if (< diff 0) (setq diff (- diff)))
197 (goto-char opoint)
198 (cond ((equal "" ostring)
199 (goto-char (point-max)))
200 ((or (search-forward ostring nil t)
201 ;; Can't use search-backward since the match may continue
202 ;; after point.
203 (progn (goto-char (- (point) diff l))
204 ;; goto-char doesn't signal an error at
205 ;; beginning of buffer like backward-char would
206 (search-forward ostring nil t)))
207 ;; to beginning of OSTRING
208 (backward-char l)))))
209
210(defun vc-buffer-sync ()
211 ;; Make sure the current buffer and its working file are in sync
212 (if (and (buffer-modified-p)
213 (or
214 vc-suppress-confirm
215 (y-or-n-p (format "%s has been modified. Write it out? "
216 (buffer-name)))))
217 (save-buffer)))
218
219(defun vc-workfile-unchanged-p (file)
220 ;; Has the given workfile changed since last checkout?
221 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
222 (lastmod (nth 5 (file-attributes file))))
223 (if checkout-time
224 (equal lastmod checkout-time)
225 (if (zerop (vc-backend-diff file nil))
226 (progn
227 (vc-file-setprop file 'vc-checkout-time lastmod)
228 t)
229 (progn
230 (vc-file-setprop file 'vc-checkout-time '(0 . 0))
231 nil
232 ))
233 )))
234
637a8ae9 235;; Here's the major entry point.
594722a8 236
637a8ae9 237;;;###autoload
594722a8
ER
238(defun vc-next-action (verbose)
239 "Do the next logical checkin or checkout operation on the current file.
240 If the file is not already registered, this registers it for version
241control and then retrieves a writeable, locked copy for editing.
242 If the file is registered and not locked by anyone, this checks out
243a writeable and locked file ready for editing.
244 If the file is checked out and locked by the calling user, this
245first checks to see if the file has changed since checkout. If not,
246it performs a revert.
247 If the file has been changed, this pops up a buffer for creation of
248a log message; when the message has been entered, it checks in the
249resulting changes along with the log message as change commentary. If
250the variable vc-keep-workfiles is non-nil (which is its default), a
251read-only copy of the changed file is left in place afterwards.
252 If the file is registered and locked by someone else, you are given
253the option to steal the lock."
254 (interactive "P")
255 (if buffer-file-name
256 (let
257 (do-update owner version
258 (file buffer-file-name)
259 (vc-file (vc-name buffer-file-name))
260 (err-msg nil)
261 owner)
262
263 (cond
264
265 ;; if there is no master file corresponding, create one
266 ((not vc-file)
267 (vc-register verbose)
268 (vc-next-action verbose))
269
270 ;; if there is no lock on the file, assert one and get it
271 ((not (setq owner (vc-locking-user file)))
272 (vc-checkout file t))
273
274 ;; a checked-out version exists, but the user may not own the lock
275 ((not (string-equal owner (user-login-name)))
276 (vc-steal-lock
277 file
278 (and verbose (read-string "Version to steal: "))
279 owner))
280
281 ;; OK, user owns the lock on the file
282 (t (progn
283
284 ;; give luser a chance to save before checking in.
285 (vc-buffer-sync)
286
287 ;; revert if file is unchanged
288 (if (vc-workfile-unchanged-p file)
289 (progn
290 (vc-backend-revert file)
291 (vc-resynch-window file t))
292
293 ;; user may want to set nonstandard parameters
294 (if verbose
295 (setq version (read-string "New version level: ")))
296
297 ;; OK, let's do the checkin
298 (vc-checkin file version))))))
299 (error "There is no file associated with buffer %s" (buffer-name))))
300
301;;; These functions help the vc-next-action entry point
302
637a8ae9 303;;;###autoload
594722a8
ER
304(defun vc-register (&optional override)
305 "Register the current file into your version-control system."
306 (interactive "P")
307 (if (vc-name buffer-file-name)
308 (error "This file is already registered."))
309 (vc-buffer-sync)
310 (vc-admin
311 buffer-file-name
312 (and override (read-string "Initial version level: ")))
313 )
314
315(defun vc-resynch-window (file &optional keep)
316 ;; If the given file is in the current buffer,
317 ;; either revert on it so we see expanded keyworks,
318 ;; or unvisit it (depending on vc-keep-workfiles)
319 (and (string= buffer-file-name file)
320 (if keep
321 (progn
322 (vc-revert-buffer1 nil t)
323 (vc-mode-line buffer-file-name))
324 (progn
325 (delete-window)
326 (kill-buffer (current-buffer))))))
327
328
329(defun vc-admin (file rev)
330 "Checks a file into your version-control system.
331FILE is the unmodified name of the file. REV should be the base version
332level to check it in under."
333 (if vc-initial-comment
334 (progn
335 (pop-to-buffer (get-buffer-create "*VC-log*"))
336 (vc-log-mode)
337 (narrow-to-region (point-max) (point-max))
338 (vc-mode-line file (file-name-nondirectory file))
339 (setq vc-log-operation 'vc-backend-admin)
340 (setq vc-log-file file)
341 (setq vc-log-version rev)
342 (message "Enter initial comment. Type C-c C-c when done."))
343 (progn
344 (vc-backend-admin file rev)
345 (vc-resynch-window file vc-keep-workfiles))))
346
347(defun vc-steal-lock (file rev &optional owner)
348 "Steal the lock on the current workfile."
349 (interactive)
350 (if (not owner)
351 (setq owner (vc-locking-user file)))
352 (if (not (y-or-n-p (format "Take the lock on %s:%s from %s?" file rev owner)))
353 (error "Steal cancelled."))
354 (pop-to-buffer (get-buffer-create "*VC-log*"))
355 (vc-log-mode)
356 (narrow-to-region (point-max) (point-max))
357 (insert
358 (format "To: %s\n\nI stole the lock on %s:%s, " owner file rev)
359 (current-time-string)
360 "\n")
361 (vc-mode-line file (file-name-nondirectory file))
362 (setq vc-log-operation 'vc-finish-steal)
363 (setq vc-log-file file)
364 (setq vc-log-version rev)
365 (message "Please explain why you stole the lock. Type C-c C-c when done.")
366 )
367
368(defun vc-finish-steal (file version)
369 ;; Actually do the lock acquisition; send the former owner a notification
370 (vc-backend-steal file version)
371 (require 'sendmail) ;; (send-mail) isn't on the standard autoload list.
372 (mail-send)
373 (vc-resynch-window file t)
374 )
375
376(defun vc-checkout (file &optional writeable)
377 "Retrieve a copy of the latest version of the given file."
378 (vc-backend-checkout file writeable)
379 (if (string-equal file buffer-file-name)
380 (vc-resynch-window file t))
381 )
382
383(defun vc-checkin (file &optional rev comment)
384 "Check in the file specified by FILE.
385The optional argument REV may be a string specifying the new version level
386(if nil increment the current level). The file is either retained with write
387permissions zeroed, or deleted (according to the value of vc-keep-workfiles).
388COMMENT is a comment string; if omitted, a buffer is
389popped up to accept a comment."
390 (pop-to-buffer (get-buffer-create "*VC-log*"))
391 (vc-log-mode)
392 (narrow-to-region (point-max) (point-max))
393 (vc-mode-line file (file-name-nondirectory file))
394 (setq vc-log-operation 'vc-backend-checkin)
395 (setq vc-log-file file)
396 (setq vc-log-version rev)
397 (message "Enter log message. Type C-c C-c when done.")
398 (if comment
399 (progn
400 (insert comment)
401 (vc-finish-logentry))))
402
403(defun vc-finish-logentry ()
404 "Complete the operation implied by the current log entry."
405 (interactive)
406 (goto-char (point-max))
407 (if (not (bolp)) (newline))
408 ;; delimit current page
409 (save-excursion
410 (widen)
411 (goto-char (point-max))
412 (if (and (not (bobp)) (not (= (char-after (1- (point))) ?\f)))
413 (insert-char ?\f 1)))
414 (if (not (bobp))
415 (forward-char -1))
416 (mark-page)
417 ;; Check for errors
418 (vc-backend-logentry-check vc-log-file)
419 ;; OK, do it to it
420 (if vc-log-operation
421 (funcall vc-log-operation
422 vc-log-file
423 vc-log-version
424 (buffer-substring (region-beginning) (1- (region-end))))
425 (error "No log operation is pending."))
426 ;; Return to "parent" buffer of this checkin and remove checkin window
427 (pop-to-buffer (get-file-buffer vc-log-file))
428 (delete-window (get-buffer-window "*VC-log*"))
429 (bury-buffer "*VC-log*")
430 ;; Now make sure we see the expanded headers
431 (vc-resynch-window buffer-file-name vc-keep-workfiles)
432 )
433
434;; Code for access to the comment ring
435
436(defun vc-next-comment ()
437 "Fill the log buffer with the next message in the msg ring."
438 (interactive)
439 (widen)
440 (forward-page)
441 (if (= (point) (point-max))
442 (goto-char (point-min)))
443 (mark-page)
444 (narrow-to-page))
445
446(defun vc-previous-comment ()
447 "Fill the log buffer with the previous message in the msg ring."
448 (interactive)
449 (widen)
450 (if (= (point) (point-min))
451 (goto-char (point-max)))
452 (backward-page)
453 (mark-page)
454 (narrow-to-page))
455
456(defun vc-comment-search-backward (regexp)
457 "Fill the log buffer with the last message in the msg ring matching REGEXP."
458 (interactive "sSearch backward for: ")
459 (widen)
460 (if (= (point) (point-min))
461 (goto-char (point-max)))
462 (re-search-backward regexp nil t)
463 (mark-page)
464 (narrow-to-page))
465
466(defun vc-comment-search-forward (regexp)
467 "Fill the log buffer with the next message in the msg ring matching REGEXP."
468 (interactive "sSearch forward for: ")
469 (widen)
470 (if (= (point) (point-min))
471 (goto-char (point-max)))
472 (re-search-forward regexp nil t)
473 (mark-page)
474 (narrow-to-page))
475
476;; Additional entry points for examining version histories
477
637a8ae9 478;;;###autoload
594722a8
ER
479(defun vc-diff (historic)
480 "Display diffs between file versions."
481 (interactive "P")
482 (if historic
483 (call-interactively 'vc-version-diff)
484 (let ((old
485 (and
486 current-prefix-arg
487 (read-string "Version to compare against: ")))
488 (file buffer-file-name)
489 unchanged)
490 (vc-buffer-sync)
491 (setq unchanged (vc-workfile-unchanged-p buffer-file-name))
492 (if unchanged
493 (message (format "No changes to %s since latest version." file))
494 (pop-to-buffer "*vc*")
495 (vc-backend-diff file nil)
496 (goto-char (point-min))
497 )
498 (not unchanged)
499 )
500 )
501 )
502
503(defun vc-version-diff (file rel1 rel2)
504 "For FILE, report diffs between two stored versions REL1 and REL2 of it.
505If FILE is a directory, generate diffs between versions for all registered
506files in or below it."
507 (interactive "FFile or directory: \nsOlder version: \nsNewer version: ")
508 (if (string-equal rel1 "") (setq rel1 nil))
509 (if (string-equal rel2 "") (setq rel2 nil))
510 (if (file-directory-p file)
511 (progn
512 (set-buffer (get-buffer-create "*vc-status*"))
513 (erase-buffer)
514 (insert "Diffs between " rel1 " and " rel2 ":\n\n")
515 (set-buffer (get-buffer-create "*vc*"))
516 (vc-file-tree-walk
517 (function (lambda (f)
518 (and
519 (not (file-directory-p f))
520 (vc-name f)
521 (vc-backend-diff f rel1 rel2))
522 (append-to-buffer "*vc-status*" (point-min) (point-max))
523 ))
524 default-directory)
525 (pop-to-buffer "*vc-status*")
526 (insert "\nEnd of diffs.\n")
527 (goto-char (point-min))
528 (set-buffer-modified-p nil)
529 )
530 (progn
531 (vc-backend-diff file rel1 rel2)
532 (goto-char (point-min))
533 (if (equal (point-min) (point-max))
534 (message (format "No changes to %s between %s and %s." file rel1 rel2))
535 (pop-to-buffer "*vc*")
536 (goto-char (point-min))
537 )
538 )
539 )
540 )
541
542;; Header-insertion code
543
637a8ae9 544;;;###autoload
594722a8
ER
545(defun vc-insert-headers ()
546 "Insert headers in a file for use with your version-control system.
547Headers desired are inserted at the start of the buffer, and are pulled from
548the variable vc-header-strings"
549 (interactive)
550 (save-excursion
551 (save-restriction
552 (widen)
553 (if (or (not (vc-check-headers))
554 (y-or-n-p "Version headers already exist. Insert another set?"))
555 (progn
556 (let* ((delims (cdr (assq major-mode vc-comment-alist)))
557 (comment-start-vc (or (car delims) comment-start "#"))
558 (comment-end-vc (or (car (cdr delims)) comment-end ""))
559 (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist))))
560 (mapcar (function (lambda (s)
561 (insert comment-start-vc "\t" s "\t"
562 comment-end-vc "\n")))
563 hdstrings)
564 (if vc-static-header-alist
565 (mapcar (function (lambda (f)
566 (if (string-match (car f) buffer-file-name)
567 (insert (format (cdr f) (car hdstrings))))))
568 vc-static-header-alist))
569 )
570 )))))
571
572;; Status-checking functions
573
637a8ae9 574;;;###autoload
594722a8
ER
575(defun vc-directory (verbose)
576 "Show version-control status of all files under the current directory."
577 (interactive "P")
578 (let ((dir (substring default-directory 0 (1- (length default-directory))))
579 nonempty)
580 (save-excursion
581 (set-buffer (get-buffer-create "*vc-status*"))
582 (erase-buffer)
583 (vc-file-tree-walk
584 (function (lambda (f)
585 (if (vc-registered f)
586 (let ((user (vc-locking-user f)))
587 (if (or user verbose)
588 (insert (format
589 "%s %s\n"
590 (concat user) f)))))))
591 dir)
592 (setq nonempty (not (zerop (buffer-size)))))
593 (if nonempty
594 (progn
595 (pop-to-buffer "*vc-status*" t)
596 (vc-shrink-to-fit)
597 (goto-char (point-min)))
598 (message "No files are currently registered under %s" dir))
599 ))
600
601;; Named-configuration support for SCCS
602
603(defun vc-add-triple (name file rev)
604 (save-excursion
605 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
606 (goto-char (point-max))
607 (insert name "\t:\t" file "\t" rev "\n")
608 (basic-save-buffer)
609 (kill-buffer (current-buffer))
610 ))
611
612(defun vc-record-rename (file newname)
613 (save-excursion
614 (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
615 (goto-char (point-min))
616 (replace-regexp (concat ":" (regexp-quote file) "$") (concat ":" newname))
617 (basic-save-buffer)
618 (kill-buffer (current-buffer))
619 ))
620
621(defun vc-lookup-triple (file name)
622 (or
623 name
624 (let ((firstchar (aref name 0)))
625 (and (>= firstchar ?0) (<= firstchar ?9) name))
626 (car (vc-master-info
627 (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file)
628 (list (concat name "\t:\t" file "\t\\(.+\\)"))))
629 ))
630
631;; Named-configuration entry points
632
633(defun vc-quiescent-p ()
634 ;; Is the current directory ready to be snapshot?
635 (let ((dir (substring default-directory 0 (1- (length default-directory)))))
636 (catch 'quiet
637 (vc-file-tree-walk
638 (function (lambda (f)
639 (if (and (vc-registered f) (vc-locking-user f))
640 (throw 'quiet nil))))
641 dir)
642 t)))
643
637a8ae9 644;;;###autoload
594722a8
ER
645(defun vc-create-snapshot (name)
646 "Make a snapshot called NAME.
647The snapshot is made from all registered files at or below the current
648directory. For each file, the version level of its latest
649version becomes part of the named configuration."
650 (interactive "sNew snapshot name: ")
651 (if (not (vc-quiescent-p))
652 (error "Can't make a snapshot, locked files are in the way.")
653 (vc-file-tree-walk
654 (function (lambda (f) (and
655 (not (file-directory-p f))
656 (vc-name f)
657 (vc-backend-assign-name f name))))
658 default-directory)
659 ))
660
637a8ae9 661;;;###autoload
594722a8
ER
662(defun vc-retrieve-snapshot (name)
663 "Retrieve the snapshot called NAME.
664This function fails if any files are locked at or below the current directory
665Otherwise, all registered files are checked out (unlocked) at their version
666levels in the snapshot."
667 (interactive "sSnapshot name to retrieve: ")
668 (if (not (vc-quiescent-p))
669 (error "Can't retrieve a snapshot, locked files are in the way.")
670 (vc-file-tree-walk
671 (function (lambda (f) (and
672 (not (file-directory-p f))
673 (vc-name f)
674 (vc-error-occurred (vc-backend-checkout f nil name)))))
675 default-directory)
676 ))
677
678;; Miscellaneous other entry points
679
637a8ae9 680;;;###autoload
594722a8
ER
681(defun vc-print-log ()
682 "List the change log of the current buffer in a window."
683 (interactive)
684 (if (and buffer-file-name (vc-name buffer-file-name))
685 (progn
686 (vc-backend-print-log buffer-file-name)
687 (pop-to-buffer (get-buffer-create "*vc*"))
688 (goto-char (point-min))
689 )
690 (error "There is no version-control master associated with this buffer")
691 )
692 )
693
637a8ae9 694;;;###autoload
594722a8 695(defun vc-revert-buffer ()
9c95ac44
RS
696 "Revert the current buffer's file back to the latest checked-in version.
697This asks for confirmation if the buffer contents are not identical
698to that version."
594722a8
ER
699 (interactive)
700 (let ((file buffer-file-name)
701 (obuf (current-buffer)) (changed (vc-diff nil)))
9c95ac44
RS
702 (if (and changed (or vc-suppress-confirm
703 (not (yes-or-no-p "Discard changes? "))))
594722a8
ER
704 (progn
705 (delete-window)
706 (error "Revert cancelled."))
707 (set-buffer obuf))
708 (if changed
709 (delete-window))
710 (vc-backend-revert file)
711 (vc-resynch-window file t)
712 )
713 )
714
637a8ae9 715;;;###autoload
594722a8
ER
716(defun vc-cancel-version (norevert)
717 "Undo your latest checkin."
718 (interactive "P")
719 (let ((target (vc-your-latest-version (buffer-file-name))))
720 (if (null target)
721 (error "You didn't check in the last change."))
722 (and (y-or-n-p (format "Remove version %s from master? " target))
723 (vc-backend-uncheck (buffer-file-name) target)))
724 (if norevert
725 (vc-mode-line (buffer-file-name))
726 (vc-checkout (buffer-file-name) nil))
727 )
728
729(defun vc-rename-file (old new)
730 "Rename a file, taking its master files with it."
731 (interactive "fOld name: \nFNew name: ")
732 (let ((oldbuf (get-file-buffer old)))
733 (if (buffer-modified-p oldbuf)
734 (error "Please save files before moving them."))
735 (if (get-file-buffer new)
736 (error "Already editing new file name."))
737 (let ((oldmaster (vc-name old)))
738 (if oldmaster
739 (if (vc-locking-user old)
740 (error "Please check in files before moving them."))
741 (if (or (file-symlink-p oldmaster)
742 (file-symlink-p (vc-backend-subdirectory-name file)))
743 (error "This is not a safe thing to do in the presence of symbolic links."))
744 (rename-file oldmaster (vc-name new)))
745 (if (or (not oldmaster) (file-exists-p old))
746 (rename-file old new)))
747; ?? Renaming a file might change its contents due to keyword expansion.
748; We should really check out a new copy if the old copy was precisely equal
749; to some checked in version. However, testing for this is tricky....
750 (if oldbuf
751 (save-excursion
752 (set-buffer oldbuf)
753 (set-visited-file-name new)
754 (set-buffer-modified-p nil))))
755 (vc-backend-dispatch file
756 (vc-record-rename old new)
757 nil)
758 )
759
637a8ae9 760;;;###autoload
f35ecf88
RM
761(defun vc-update-change-log (&rest args)
762 "Find change log file and add entries from recent RCS logs.
763The mark is left at the end of the text prepended to the change log.
764With prefix arg of C-u, only find log entries for the current buffer's file.
765With any numeric prefix arg, find log entries for all files currently visited.
766From a program, any arguments are passed to the `rcs2log' script."
767 (interactive (cond ((consp current-prefix-arg) ;C-u
768 (list buffer-file-name))
769 (current-prefix-arg ;Numeric argument.
770 (let ((files nil)
771 (buffers (buffer-list))
772 file)
773 (while buffers
774 (setq file (buffer-file-name (car buffers)))
775 (and file
776 (setq file (vc-name file))
777 (setq files (cons file files)))
778 (setq buffers (cdr buffers)))
779 files))))
780 (find-file-other-window "ChangeLog")
781 (vc-buffer-sync)
782 (undo-boundary)
783 (goto-char (point-min))
784 (message "Computing change log entries...")
785 (shell-command (mapconcat 'identity (cons "rcs2log" args) " ") t)
786 (message "Computing change log entries... done"))
594722a8
ER
787
788;; Functions for querying the master and lock files.
789
790(defun match-substring (bn)
791 (buffer-substring (match-beginning bn) (match-end bn)))
792
793(defun vc-parse-buffer (patterns &optional file properties)
794 ;; Use PATTERNS to parse information out of the current buffer
795 ;; by matching each regular expression in the list and returning \\1.
796 ;; If a regexp has two tag brackets, assume the second is a date
797 ;; field and we want the most recent entry matching the template.
798 ;; If FILE and PROPERTIES are given, the latter must be a list of
799 ;; properties of the same length as PATTERNS; each property is assigned
800 ;; the corresponding value.
801 (mapcar (function (lambda (p)
802 (goto-char (point-min))
803 (if (string-match "\\\\(.*\\\\(" p)
804 (let ((latest-date "") (latest-val))
805 (while (re-search-forward p nil t)
806 (let ((date (match-substring 2)))
807 (if (string< latest-date date)
808 (progn
809 (setq latest-date date)
810 (setq latest-val
811 (match-substring 1))))))
812 latest-val))
813 (prog1
814 (and (re-search-forward p nil t)
815 (let ((value (match-substring 1)))
816 (if file
817 (vc-file-setprop file (car properties) value))
818 value))
819 (setq properties (cdr properties)))))
820 patterns)
821 )
822
823(defun vc-master-info (file fields &optional rfile properties)
824 ;; Search for information in a master file.
825 (if (and file (file-exists-p file))
826 (save-excursion
827 (let ((buf))
828 (setq buf (create-file-buffer file))
829 (set-buffer buf))
830 (erase-buffer)
831 (insert-file-contents file nil)
832 (set-buffer-modified-p nil)
833 (auto-save-mode nil)
834 (prog1
835 (vc-parse-buffer fields rfile properties)
836 (kill-buffer (current-buffer)))
837 )
838 (if rfile
839 (mapcar
840 (function (lambda (p) (vc-file-setprop rfile p nil)))
841 properties))
842 )
843 )
844
845(defun vc-log-info (command file patterns &optional properties)
846 ;; Search for information in log program output
847 (if (and file (file-exists-p file))
848 (save-excursion
849 (let ((buf))
850 (setq buf (get-buffer-create "*vc*"))
851 (set-buffer buf))
852 (apply 'vc-do-command 0 command file nil)
853 (set-buffer-modified-p nil)
854 (prog1
855 (vc-parse-buffer patterns file properties)
856 (kill-buffer (current-buffer))
857 )
858 )
859 (if file
860 (mapcar
861 (function (lambda (p) (vc-file-setprop file p nil)))
862 properties))
863 )
864 )
865
866(defun vc-locking-user (file)
867 "Return the name of the person currently holding a lock on FILE.
868Return nil if there is no such person."
869 (if (or (not vc-keep-workfiles)
870 (eq vc-mistrust-permissions 't)
871 (and vc-mistrust-permissions
872 (funcall vc-mistrust-permissions (vc-backend-subdirectory-name file))))
873 (vc-true-locking-user file)
874 ;; This implementation assumes that any file which is under version
875 ;; control and has -rw-r--r-- is locked by its owner. This is true
876 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
877 ;; We have to be careful not to exclude files with execute bits on;
878 ;; scripts can be under version control too. The advantage of this
879 ;; hack is that calls to the very expensive vc-fetch-properties
880 ;; function only have to be made if (a) the file is locked by someone
881 ;; other than the current user, or (b) some untoward manipulation
882 ;; behind vc's back has twiddled the `group' or `other' write bits.
883 (let ((attributes (file-attributes file)))
884 (cond ((string-match ".r-.r-.r-." (nth 8 attributes))
885 nil)
886 ((and (= (nth 2 attributes) (user-uid))
887 (string-match ".rw.r-.r-." (nth 8 attributes)))
888 (user-login-name))
889 (t
890 (vc-true-locking-user file))))))
891
892(defun vc-true-locking-user (file)
893 ;; The slow but reliable version
894 (vc-fetch-properties file)
895 (vc-file-getprop file 'vc-locking-user))
896
897(defun vc-latest-version (file)
898 ;; Return version level of the latest version of FILE
899 (vc-fetch-properties file)
900 (vc-file-getprop file 'vc-latest-version))
901
902(defun vc-your-latest-version (file)
903 ;; Return version level of the latest version of FILE checked in by you
904 (vc-fetch-properties file)
905 (vc-file-getprop file 'vc-your-latest-version))
906
907;; Collect back-end-dependent stuff here
908;;
909;; Everything eventually funnels through these functions. To implement
910;; support for a new version-control system, add another branch to the
911;; vc-backend-dispatch macro (in vc-hooks.el) and fill it in in each call.
912
913(defmacro vc-backend-dispatch (f s r)
914 "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
915 (list 'let (list (list 'type (list 'vc-backend-deduce f)))
916 (list 'cond
917 (list (list 'eq 'type (quote 'SCCS)) s) ;; SCCS
918 (list (list 'eq 'type (quote 'RCS)) r) ;; RCS
919 )))
920
921(defun vc-lock-file (file)
922 ;; Generate lock file name corresponding to FILE
923 (let ((master (vc-name file)))
924 (and
925 master
926 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
927 (concat
928 (substring master (match-beginning 1) (match-end 1))
929 "p."
930 (substring master (match-beginning 2) (match-end 2))))))
931
932
933(defun vc-fetch-properties (file)
934 ;; Re-fetch all properties associated with the given file.
935 ;; Currently these properties are:
936 ;; vc-locking-user
937 ;; vc-locked-version
938 ;; vc-latest-version
939 ;; vc-your-latest-version
940 (vc-backend-dispatch
941 file
942 ;; SCCS
943 (progn
944 (vc-master-info (vc-lock-file file)
945 (list
946 "^[^ ]+ [^ ]+ \\([^ ]+\\)"
947 "^\\([^ ]+\\)")
948 file
949 '(vc-locking-user vc-locked-version))
950 (vc-master-info (vc-name file)
951 (list
952 "^\001d D \\([^ ]+\\)"
953 (concat "^\001d D \\([^ ]+\\) .* "
954 (regexp-quote (user-login-name)) " ")
955 )
956 file
957 '(vc-latest-version vc-your-latest-version))
958 )
959 ;; RCS
960 (vc-log-info "rlog" file
961 (list
962 "^locks: strict\n\t\\([^:]+\\)"
963 "^locks: strict\n\t[^:]+: \\(.+\\)"
964 "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
965 (concat
966 "^revision[\t ]+\\([0-9.]+\\).*locked by: "
967 (regexp-quote (user-login-name))
968 ";\ndate: \\([ /0-9:]+\\);"))
969 '(vc-locking-user vc-locked-version
970 vc-latest-version vc-your-latest-version))
971 ))
972
973(defun vc-backend-subdirectory-name (&optional file)
974 ;; Where the master and lock files for the current directory are kept
975 (symbol-name
976 (or
977 (and file (vc-backend-deduce file))
978 vc-default-back-end
979 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
980
981(defun vc-backend-admin (file &optional rev comment)
982 ;; Register a file into the version-control system
983 ;; Automatically retrieves a read-only version of the file with
984 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
985 ;; it deletes the workfile.
986 (vc-file-clearprops file)
987 (or vc-default-back-end
988 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))
989 (message "Registering %s..." file)
990 (let ((backend
991 (cond
992 ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end)
993 ((file-exists-p "RCS") 'RCS)
994 ((file-exists-p "SCCS") 'SCCS)
995 (t vc-default-back-end))))
996 (cond ((eq backend 'SCCS)
997 (vc-do-command 0 "admin" file ;; SCCS
998 (and rev (concat "-r" rev))
999 "-fb"
1000 (concat "-i" file)
1001 (and comment (concat "-y" comment))
1002 (format
1003 (car (rassq 'SCCS vc-master-templates))
1004 (or (file-name-directory file) "")
1005 (file-name-nondirectory file)))
1006 (delete-file file)
1007 (if vc-keep-workfiles
1008 (vc-do-command 0 "get" file)))
1009 ((eq backend 'RCS)
1010 (vc-do-command 0 "ci" file ;; RCS
1011 (concat (if vc-keep-workfiles "-u" "-r") rev)
1012 (and comment (concat "-t-" comment))
1013 file)
1014 )))
1015 (message "Registering %s...done" file)
1016 )
1017
1018(defun vc-backend-checkout (file &optional writeable rev)
1019 ;; Retrieve a copy of a saved version into a workfile
1020 (message "Checking out %s..." file)
1021 (vc-backend-dispatch file
1022 (progn
1023 (vc-do-command 0 "get" file ;; SCCS
1024 (if writeable "-e")
1025 (and rev (concat "-r" (vc-lookup-triple file rev))))
1026 )
1027 (vc-do-command 0 "co" file ;; RCS
1028 (if writeable "-l")
1029 (and rev (concat "-r" rev)))
1030 )
1031 (vc-file-setprop file 'vc-checkout-time (nth 5 (file-attributes file)))
1032 (message "Checking out %s...done" file)
1033 )
1034
1035(defun vc-backend-logentry-check (file)
1036 (vc-backend-dispatch file
1037 (if (>= (- (region-end) (region-beginning)) 512) ;; SCCS
1038 (progn
1039 (goto-char 512)
1040 (error
1041 "Log must be less than 512 characters. Point is now at char 512.")))
1042 nil)
1043 )
1044
1045(defun vc-backend-checkin (file &optional rev comment)
1046 ;; Register changes to FILE as level REV with explanatory COMMENT.
1047 ;; Automatically retrieves a read-only version of the file with
1048 ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
1049 ;; it deletes the workfile.
1050 (message "Checking in %s..." file)
1051 (vc-backend-dispatch file
1052 (progn
1053 (vc-do-command 0 "delta" file
1054 (if rev (concat "-r" rev))
1055 (concat "-y" comment))
1056 (if vc-keep-workfiles
1057 (vc-do-command 0 "get" file))
1058 )
1059 (vc-do-command 0 "ci" file
1060 (concat (if vc-keep-workfiles "-u" "-r") rev)
1061 (concat "-m" comment))
1062 )
1063 (vc-file-setprop file 'vc-locking-user nil)
1064 (message "Checking in %s...done" file)
1065 )
1066
1067(defun vc-backend-revert (file)
1068 ;; Revert file to latest checked-in version.
1069 (message "Reverting %s..." file)
1070 (vc-backend-dispatch
1071 file
1072 (progn ;; SCCS
1073 (vc-do-command 0 "unget" file nil)
1074 (vc-do-command 0 "get" file nil))
1075 (progn
1076 (delete-file file) ;; RCS
1077 (vc-do-command 0 "co" file "-u")))
1078 (vc-file-setprop file 'vc-locking-user nil)
1079 (message "Reverting %s...done" file)
1080 )
1081
1082(defun vc-backend-steal (file &optional rev)
1083 ;; Steal the lock on the current workfile. Needs RCS 5.6.2 or later for -M.
1084 (message "Stealing lock on %s..." file)
1085 (progn
1086 (vc-do-command 0 "unget" file "-n" (if rev (concat "-r" rev)))
1087 (vc-do-command 0 "get" file "-g" (if rev (concat "-r" rev)))
1088 )
1089 (progn
1090 (vc-do-command 0 "rcs" file "-M" (concat "-u" rev))
1091 (vc-do-command 0 "rcs" file (concat "-l" rev))
1092 )
1093 (vc-file-setprop file 'vc-locking-user (user-login-name))
1094 (message "Stealing lock on %s...done" file)
1095 )
1096
1097(defun vc-backend-uncheck (file target)
1098 ;; Undo the latest checkin. Note: this code will have to get a lot
1099 ;; smarter when we support multiple branches.
1100 (message "Removing last change from %s..." file)
1101 (vc-backend-dispatch file
1102 (vc-do-command 0 "rmdel" file (concat "-r" target))
1103 (vc-do-command 0 "rcs" file (concat "-o" target))
1104 )
1105 (message "Removing last change from %s...done" file)
1106 )
1107
1108(defun vc-backend-print-log (file)
1109 ;; Print change log associated with FILE to buffer *vc*.
1110 (vc-do-command 0
1111 (vc-backend-dispatch file "prs" "rlog")
1112 file)
1113 )
1114
1115(defun vc-backend-assign-name (file name)
1116 ;; Assign to a FILE's latest version a given NAME.
1117 (vc-backend-dispatch file
1118 (vc-add-triple name file (vc-latest-version file)) ;; SCCS
1119 (vc-do-command 0 "rcs" file (concat "-n" name ":")) ;; RCS
1120 ))
1121
1122(defun vc-backend-diff (file oldvers &optional newvers)
1123 ;; Get a difference report between two versions
1124 (apply 'vc-do-command 1
1125 (or (vc-backend-dispatch file "vcdiff" "rcsdiff")
1126 (error (format "File %s is not under version control." file)))
1127 file
1128 (and oldvers (concat "-r" oldvers))
1129 (and newvers (concat "-r" newvers))
1130 vc-diff-options
1131 ))
1132
1133(defun vc-check-headers ()
1134 "Check if the current file has any headers in it."
1135 (interactive)
1136 (save-excursion
1137 (goto-char (point-min))
1138 (vc-backend-dispatch buffer-file-name
1139 (re-search-forward "%[MIRLBSDHTEGUYFPQCZWA]%" nil t) ;; SCCS
1140 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t) ;; RCS
1141 )
1142 ))
1143
1144;; Back-end-dependent stuff ends here.
1145
1146;; Set up key bindings for use while editing log messages
1147
1148(defun vc-log-mode ()
1149 "Minor mode for driving version-control tools.
1150These bindings are added to the global keymap when you enter this mode:
1151\\[vc-next-action] perform next logical version-control operation on current file
1152\\[vc-register] register current file
1153\\[vc-toggle-read-only] like next-action, but won't register files
1154\\[vc-insert-headers] insert version-control headers in current file
1155\\[vc-print-log] display change history of current file
1156\\[vc-revert-buffer] revert buffer to latest version
1157\\[vc-cancel-version] undo latest checkin
1158\\[vc-diff] show diffs between file versions
1159\\[vc-directory] show all files locked by any user in or below .
1160\\[vc-update-change-log] add change log entry from recent checkins
1161
1162While you are entering a change log message for a version, the following
1163additional bindings will be in effect.
1164
1165\\[vc-finish-logentry] proceed with check in, ending log message entry
1166
1167Whenever you do a checkin, your log comment is added to a ring of
1168saved comments. These can be recalled as follows:
1169
1170\\[vc-next-comment] replace region with next message in comment ring
1171\\[vc-previous-comment] replace region with previous message in comment ring
1172\\[vc-search-comment-reverse] search backward for regexp in the comment ring
1173\\[vc-search-comment-forward] search backward for regexp in the comment ring
1174
1175Entry to the change-log submode calls the value of text-mode-hook, then
1176the value of vc-log-mode-hook.
1177
1178Global user options:
1179 vc-initial-comment If non-nil, require user to enter a change
1180 comment upon first checkin of the file.
1181
1182 vc-keep-workfiles Non-nil value prevents workfiles from being
1183 deleted when changes are checked in
1184
1185 vc-suppress-confirm Suppresses some confirmation prompts,
1186 notably for reversions.
1187
1188 vc-diff-options A list consisting of the flags
1189 to be used for generating context diffs.
1190
1191 vc-header-strings Which keywords to insert when adding headers
1192 with \\[vc-insert-headers]. Defaults to
1193 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
1194
1195 vc-static-header-alist By default, version headers inserted in C files
1196 get stuffed in a static string area so that
1197 ident(RCS) or what(SCCS) can see them in the
1198 compiled object code. You can override this
1199 by setting this variable to nil, or change
1200 the header template by changing it.
1201
1202 vc-command-messages if non-nil, display run messages from the
1203 actual version-control utilities (this is
1204 intended primarily for people hacking vc
1205 itself).
1206"
1207 (interactive)
1208 (set-syntax-table text-mode-syntax-table)
1209 (use-local-map vc-log-entry-mode)
1210 (setq local-abbrev-table text-mode-abbrev-table)
1211 (setq major-mode 'vc-log-mode)
1212 (setq mode-name "VC-Log")
1213 (make-local-variable 'vc-log-file)
1214 (make-local-variable 'vc-log-version)
1215 (set-buffer-modified-p nil)
1216 (setq buffer-file-name nil)
1217 (run-hooks 'text-mode-hook 'vc-log-mode-hook)
1218)
1219
1220;; Initialization code, to be done just once at load-time
1221(if vc-log-entry-mode
1222 nil
1223 (setq vc-log-entry-mode (make-sparse-keymap))
1224 (define-key vc-log-entry-mode "\M-n" 'vc-next-comment)
1225 (define-key vc-log-entry-mode "\M-p" 'vc-previous-comment)
1226 (define-key vc-log-entry-mode "\M-r" 'vc-comment-search-backward)
1227 (define-key vc-log-entry-mode "\M-s" 'vc-comment-search-forward)
1228 (define-key vc-log-entry-mode "\C-c\C-c" 'vc-finish-logentry)
1229 )
1230
1231;;; These things should probably be generally available
1232
1233(defun vc-shrink-to-fit ()
1234 "Shrink a window vertically until it's just large enough to contain its text"
1235 (let ((minsize (1+ (count-lines (point-min) (point-max)))))
1236 (if (< minsize (window-height))
1237 (let ((window-min-height 2))
1238 (shrink-window (- (window-height) minsize))))))
1239
1240(defun vc-file-tree-walk (func dir &rest args)
1241 "Apply a given function to dir and all files underneath it, recursively."
1242 (apply 'funcall func dir args)
1243 (and (file-directory-p dir)
1244 (mapcar
1245 (function (lambda (f) (or
1246 (string-equal f ".")
1247 (string-equal f "..")
1248 (file-symlink-p f) ;; Avoid possible loops
1249 (apply 'vc-file-tree-walk
1250 func
1251 (if (= (aref dir (1- (length dir))) ?/)
1252 (concat dir f)
1253 (concat dir "/" f))
1254 args))))
1255 (directory-files dir))))
1256
1257(provide 'vc)
1258
1259;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
1260;;;
1261;;; These may be useful to anyone who has to debug or extend the package.
1262;;;
1263;;; A fundamental problem in VC is that there are time windows between
1264;;; vc-next-action's computations of the file's version-control state and
1265;;; the actions that change it. This is a window open to lossage in a
1266;;; multi-user environment; someone else could nip in and change the state
1267;;; of the master during it.
1268;;;
1269;;; The performance problem is that rlog/prs calls are very expensive; we want
1270;;; to avoid them as much as possible.
1271;;;
1272;;; ANALYSIS:
1273;;;
1274;;; The performance problem, it turns out, simplifies in practice to the
1275;;; problem of making vc-locking-user fast. The two other functions that call
1276;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
1277;;; makes snapshots, the other deletes the calling user's last change in the
1278;;; master.
1279;;;
1280;;; The race condition implies that we have to either (a) lock the master
1281;;; during the entire execution of vc-next-action, or (b) detect and
1282;;; recover from errors resulting from dispatch on an out-of-date state.
1283;;;
1284;;; Alternative (a) appears to be unfeasible. The problem is that we can't
1285;;; guarantee that the lock will ever be removed. Suppose a user starts a
1286;;; checkin, the change message buffer pops up, and the user, having wandered
1287;;; off to do something else, simply forgets about it?
1288;;;
1289;;; Alternative (b), on the other hand, works well with a cheap way to speed up
1290;;; vc-locking-user. Usually, if a file is registered, we can read its locked/
1291;;; unlocked state and its current owner from its permissions.
1292;;;
1293;;; This shortcut will fail if someone has manually changed the workfile's
1294;;; permissions; also if developers are munging the workfile in several
1295;;; directories, with symlinks to a master (in this latter case, the
1296;;; permissions shortcut will fail to detect a lock asserted from another
1297;;; directory).
1298;;;
1299;;; Note that these cases correspond exactly to the errors which could happen
1300;;; because of a competing checkin/checkout race in between two instances of
1301;;; vc-next-action.
1302;;;
1303;;; For VC's purposes, a workfile/master pair may have the following states:
1304;;;
1305;;; A. Unregistered. There is a workfile, there is no master.
1306;;;
1307;;; B. Registered and not locked by anyone.
1308;;;
1309;;; C. Locked by calling user and unchanged.
1310;;;
1311;;; D. Locked by the calling user and changed.
1312;;;
1313;;; E. Locked by someone other than the calling user.
1314;;;
1315;;; This makes for 25 states and 20 error conditions. Here's the matrix:
1316;;;
1317;;; VC's idea of state
1318;;; |
1319;;; V Actual state RCS action SCCS action Effect
1320;;; A B C D E
1321;;; A . 1 2 3 4 ci -u -t- admin -fb -i<file> initial admin
1322;;; B 5 . 6 7 8 co -l get -e checkout
1323;;; C 9 10 . 11 12 co -u unget; get revert
1324;;; D 13 14 15 . 16 ci -u -m<comment> delta -y<comment>; get checkin
1325;;; E 17 18 19 20 . rcs -u -M ; rcs -l unget -n ; get -g steal lock
1326;;;
1327;;; All commands take the master file name as a last argument (not shown).
1328;;;
1329;;; In the discussion below, a "self-race" is a pathological situation in
1330;;; which VC operations are being attempted simultaneously by two or more
1331;;; Emacsen running under the same username.
1332;;;
1333;;; The vc-next-action code has the following windows:
1334;;;
1335;;; Window P:
1336;;; Between the check for existence of a master file and the call to
1337;;; admin/checkin in vc-buffer-admin (apparent state A). This window may
1338;;; never close if the initial-comment feature is on.
1339;;;
1340;;; Window Q:
1341;;; Between the call to vc-workfile-unchanged-p in and the immediately
1342;;; following revert (apparent state C).
1343;;;
1344;;; Window R:
1345;;; Between the call to vc-workfile-unchanged-p in and the following
1346;;; checkin (apparent state D). This window may never close.
1347;;;
1348;;; Window S:
1349;;; Between the unlock and the immediately following checkout during a
1350;;; revert operation (apparent state C). Included in window Q.
1351;;;
1352;;; Window T:
1353;;; Between vc-locking-user and the following checkout (apparent state B).
1354;;;
1355;;; Window U:
1356;;; Between vc-locking-user and the following revert (apparent state C).
1357;;; Includes windows Q and S.
1358;;;
1359;;; Window V:
1360;;; Between vc-locking-user and the following checkin (apparent state
1361;;; D). This window may never be closed if the user fails to complete the
1362;;; checkin message. Includes window R.
1363;;;
1364;;; Window W:
1365;;; Between vc-locking-user and the following steal-lock (apparent
1366;;; state E). This window may never cloce if the user fails to complete
1367;;; the steal-lock message. Includes window X.
1368;;;
1369;;; Window X:
1370;;; Between the unlock and the immediately following re-lock during a
1371;;; steal-lock operation (apparent state E). This window may never cloce
1372;;; if the user fails to complete the steal-lock message.
1373;;;
1374;;; Errors:
1375;;;
1376;;; Apparent state A ---
1377;;;
1378;;; 1. File looked unregistered but is actually registered and not locked.
1379;;;
1380;;; Potential cause: someone else's admin during window P, with
1381;;; caller's admin happening before their checkout.
1382;;;
1383;;; RCS: ci will fail with a "no lock set by <user>" message.
1384;;; SCCS: admin will fail with error (ad19).
1385;;;
1386;;; We can let these errors be passed up to the user.
1387;;;
1388;;; 2. File looked unregistered but is actually locked by caller, unchanged.
1389;;;
1390;;; Potential cause: self-race during window P.
1391;;;
1392;;; RCS: will revert the file to the last saved version and unlock it.
1393;;; SCCS: will fail with error (ad19).
1394;;;
1395;;; Either of these consequences is acceptable.
1396;;;
1397;;; 3. File looked unregistered but is actually locked by caller, changed.
1398;;;
1399;;; Potential cause: self-race during window P.
1400;;;
1401;;; RCS: will register the caller's workfile as a delta with a
1402;;; null change comment (the -t- switch will be ignored).
1403;;; SCCS: will fail with error (ad19).
1404;;;
1405;;; 4. File looked unregistered but is locked by someone else.
1406;;;
1407;;; Potential cause: someone else's admin during window P, with
1408;;; caller's admin happening *after* their checkout.
1409;;;
1410;;; RCS: will fail with a "no lock set by <user>" message.
1411;;; SCCS: will fail with error (ad19).
1412;;;
1413;;; We can let these errors be passed up to the user.
1414;;;
1415;;; Apparent state B ---
1416;;;
1417;;; 5. File looked registered and not locked, but is actually unregistered.
1418;;;
1419;;; Potential cause: master file got nuked during window P.
1420;;;
1421;;; RCS: will fail with "RCS/<file>: No such file or directory"
1422;;; SCCS: will fail with error ut4.
1423;;;
1424;;; We can let these errors be passed up to the user.
1425;;;
1426;;; 6. File looked registered and not locked, but is actually locked by the
1427;;; calling user and unchanged.
1428;;;
1429;;; Potential cause: self-race during window T.
1430;;;
1431;;; RCS: in the same directory as the previous workfile, co -l will fail
1432;;; with "co error: writable foo exists; checkout aborted". In any other
1433;;; directory, checkout will succeed.
1434;;; SCCS: will fail with ge17.
1435;;;
1436;;; Either of these consequences is acceptable.
1437;;;
1438;;; 7. File looked registered and not locked, but is actually locked by the
1439;;; calling user and changed.
1440;;;
1441;;; As case 6.
1442;;;
1443;;; 8. File looked registered and not locked, but is actually locked by another
1444;;; user.
1445;;;
1446;;; Potential cause: someone else checks it out during window T.
1447;;;
1448;;; RCS: co error: revision 1.3 already locked by <user>
1449;;; SCCS: fails with ge4 (in directory) or ut7 (outside it).
1450;;;
1451;;; We can let these errors be passed up to the user.
1452;;;
1453;;; Apparent state C ---
1454;;;
1455;;; 9. File looks locked by calling user and unchanged, but is unregistered.
1456;;;
1457;;; As case 5.
1458;;;
1459;;; 10. File looks locked by calling user and unchanged, but is actually not
1460;;; locked.
1461;;;
1462;;; Potential cause: a self-race in window U, or by the revert's
1463;;; landing during window X of some other user's steal-lock or window S
1464;;; of another user's revert.
1465;;;
1466;;; RCS: succeeds, refreshing the file from the identical version in
1467;;; the master.
1468;;; SCCS: fails with error ut4 (p file nonexistent).
1469;;;
1470;;; Either of these consequences is acceptable.
1471;;;
1472;;; 11. File is locked by calling user. It looks unchanged, but is actually
1473;;; changed.
1474;;;
1475;;; Potential cause: the file would have to be touched by a self-race
1476;;; during window Q.
1477;;;
1478;;; The revert will succeed, removing whatever changes came with
1479;;; the touch. It is theoretically possible that work could be lost.
1480;;;
1481;;; 12. File looks like it's locked by the calling user and unchanged, but
1482;;; it's actually locked by someone else.
1483;;;
1484;;; Potential cause: a steal-lock in window V.
1485;;;
1486;;; RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
1487;;; SCCS: fails with error un2
1488;;;
1489;;; We can pass these errors up to the user.
1490;;;
1491;;; Apparent state D ---
1492;;;
1493;;; 13. File looks like it's locked by the calling user and changed, but it's
1494;;; actually unregistered.
1495;;;
1496;;; Potential cause: master file got nuked during window P.
1497;;;
1498;;; RCS: Checks in the user's version as an initial delta.
1499;;; SCCS: will fail with error ut4.
1500;;;
1501;;; This case is kind of nasty. It means VC may fail to detect the
1502;;; loss of previous version information.
1503;;;
1504;;; 14. File looks like it's locked by the calling user and changed, but it's
1505;;; actually unlocked.
1506;;;
1507;;; Potential cause: self-race in window V, or the checkin happening
1508;;; during the window X of someone else's steal-lock or window S of
1509;;; someone else's revert.
1510;;;
1511;;; RCS: ci will fail with "no lock set by <user>".
1512;;; SCCS: delta will fail with error ut4.
1513;;;
1514;;; 15. File looks like it's locked by the calling user and changed, but it's
1515;;; actually locked by the calling user and unchanged.
1516;;;
1517;;; Potential cause: another self-race --- a whole checkin/checkout
1518;;; sequence by the calling user would have to land in window R.
1519;;;
1520;;; SCCS: checks in a redundant delta and leaves the file unlocked as usual.
1521;;; RCS: reverts to the file state as of the second user's checkin, leaving
1522;;; the file unlocked.
1523;;;
1524;;; It is theoretically possible that work could be lost under RCS.
1525;;;
1526;;; 16. File looks like it's locked by the calling user and changed, but it's
1527;;; actually locked by a different user.
1528;;;
1529;;; RCS: ci error: no lock set by <user>
1530;;; SCCS: unget will fail with error un2
1531;;;
1532;;; We can pass these errors up to the user.
1533;;;
1534;;; Apparent state E ---
1535;;;
1536;;; 17. File looks like it's locked by some other user, but it's actually
1537;;; unregistered.
1538;;;
1539;;; As case 13.
1540;;;
1541;;; 18. File looks like it's locked by some other user, but it's actually
1542;;; unlocked.
1543;;;
1544;;; Potential cause: someone released a lock during window W.
1545;;;
1546;;; RCS: The calling user will get the lock on the file.
1547;;; SCCS: unget -n will fail with cm4.
1548;;;
1549;;; Either of these consequences will be OK.
1550;;;
1551;;; 19. File looks like it's locked by some other user, but it's actually
1552;;; locked by the calling user and unchanged.
1553;;;
1554;;; Potential cause: the other user relinquishing a lock followed by
1555;;; a self-race, both in window W.
1556;;;
1557;;; Under both RCS and SCCS, both unlock and lock will succeed, making
1558;;; the sequence a no-op.
1559;;;
1560;;; 20. File looks like it's locked by some other user, but it's actually
1561;;; locked by the calling user and changed.
1562;;;
1563;;; As case 19.
1564;;;
1565;;; PROBLEM CASES:
1566;;;
1567;;; In order of decreasing severity:
1568;;;
1569;;; Cases 11 and 15 under RCS are the only one that potentially lose work.
1570;;; They would require a self-race for this to happen.
1571;;;
1572;;; Case 13 in RCS loses information about previous deltas, retaining
1573;;; only the information in the current workfile. This can only happen
1574;;; if the master file gets nuked in window P.
1575;;;
1576;;; Case 3 in RCS and case 15 under SCCS insert a redundant delta with
1577;;; no change comment in the master. This would require a self-race in
1578;;; window P or R respectively.
1579;;;
1580;;; Cases 2, 10, 19 and 20 do extra work, but make no changes.
1581;;;
1582;;; Unfortunately, it appears to me that no recovery is possible in these
1583;;; cases. They don't yield error messages, so there's no way to tell that
1584;;; a race condition has occurred.
1585;;;
1586;;; All other cases don't change either the workfile or the master, and
1587;;; trigger command errors which the user will see.
1588;;;
1589;;; Thus, there is no explicit recovery code.
1590
1591;;; vc.el ends here