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