1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: Andre Spiegel <spiegel@inf.fu-berlin.de>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This is the always-loaded portion of VC.
28 ;; It takes care VC-related activities that are done when you visit a file,
29 ;; so that vc.el itself is loaded only when you use a VC command.
30 ;; See the commentary of vc.el.
34 ;; Customization Variables (the rest is in vc.el)
36 (defvar vc-default-back-end nil
37 "*Back-end actually used by this interface; may be SCCS or RCS.
38 The value is only computed when needed to avoid an expensive search.")
40 (defvar vc-handle-cvs t
41 "*If non-nil, use VC for files managed with CVS.
42 If it is nil, don't use VC for those files.")
44 (defvar vc-rcsdiff-knows-brief nil
45 "*Indicates whether rcsdiff understands the --brief option.
46 The value is either `yes', `no', or nil. If it is nil, VC tries
47 to use --brief and sets this variable to remember whether it worked.")
50 (if (file-directory-p "/usr/sccs")
53 "*List of extra directories to search for version control commands.")
55 (defvar vc-master-templates
56 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
57 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
59 "*Where to look for version-control master files.
60 The first pair corresponding to a given back end is used as a template
61 when creating new masters.")
63 (defvar vc-make-backup-files nil
64 "*If non-nil, backups of registered files are made as with other files.
65 If nil (the default), files covered by version control don't get backups.")
67 (defvar vc-follow-symlinks 'ask
68 "*Indicates what to do if you visit a symbolic link to a file
69 that is under version control. Editing such a file through the
70 link bypasses the version control system, which is dangerous and
71 probably not what you want.
72 If this variable is t, VC follows the link and visits the real file,
73 telling you about it in the echo area. If it is `ask', VC asks for
74 confirmation whether it should follow the link. If nil, the link is
75 visited and a warning displayed.")
77 (defvar vc-display-status t
78 "*If non-nil, display revision number and lock status in modeline.
79 Otherwise, not displayed.")
81 (defvar vc-consult-headers t
82 "*If non-nil, identify work files by searching for version headers.")
84 (defvar vc-keep-workfiles t
85 "*If non-nil, don't delete working files after registering changes.
86 If the back-end is CVS, workfiles are always kept, regardless of the
89 (defvar vc-mistrust-permissions nil
90 "*If non-nil, don't assume that permissions and ownership track
91 version-control status. If nil, do rely on the permissions.
92 See also variable `vc-consult-headers'.")
94 (defun vc-mistrust-permissions (file)
95 ;; Access function to the above.
96 (or (eq vc-mistrust-permissions 't)
97 (and vc-mistrust-permissions
98 (funcall vc-mistrust-permissions
99 (vc-backend-subdirectory-name file)))))
101 ;; Tell Emacs about this new kind of minor mode
102 (if (not (assoc 'vc-mode minor-mode-alist))
103 (setq minor-mode-alist (cons '(vc-mode vc-mode)
106 (make-variable-buffer-local 'vc-mode)
107 (put 'vc-mode 'permanent-local t)
109 ;; We need a notion of per-file properties because the version
110 ;; control state of a file is expensive to derive --- we compute
111 ;; them when the file is initially found, keep them up to date
112 ;; during any subsequent VC operations, and forget them when
113 ;; the buffer is killed.
115 (defmacro vc-error-occurred (&rest body)
116 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
118 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
119 "Obarray for per-file properties.")
121 (defvar vc-buffer-backend t)
122 (make-variable-buffer-local 'vc-buffer-backend)
124 (defun vc-file-setprop (file property value)
125 ;; set per-file property
126 (put (intern file vc-file-prop-obarray) property value))
128 (defun vc-file-getprop (file property)
129 ;; get per-file property
130 (get (intern file vc-file-prop-obarray) property))
132 (defun vc-file-clearprops (file)
133 ;; clear all properties of a given file
134 (setplist (intern file vc-file-prop-obarray) nil))
136 ;;; Functions that determine property values, by examining the
137 ;;; working file, the master file, or log program output
139 (defun vc-match-substring (bn)
140 (buffer-substring (match-beginning bn) (match-end bn)))
142 (defun vc-lock-file (file)
143 ;; Generate lock file name corresponding to FILE
144 (let ((master (vc-name file)))
147 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
149 (substring master (match-beginning 1) (match-end 1))
151 (substring master (match-beginning 2) (match-end 2))))))
153 (defun vc-parse-buffer (patterns &optional file properties)
154 ;; Use PATTERNS to parse information out of the current buffer.
155 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
156 ;; is the pattern to be matched, and the second (an integer) is the
157 ;; number of the subexpression that should be returned. If there's
158 ;; a third element (also the number of a subexpression), that
159 ;; subexpression is assumed to be a date field and we want the most
160 ;; recent entry matching the template.
161 ;; If FILE and PROPERTIES are given, the latter must be a list of
162 ;; properties of the same length as PATTERNS; each property is assigned
163 ;; the corresponding value.
164 (mapcar (function (lambda (p)
165 (goto-char (point-min))
167 ((eq (length p) 2) ;; search for first entry
169 (if (re-search-forward (car p) nil t)
170 (setq value (vc-match-substring (elt p 1))))
172 (progn (vc-file-setprop file (car properties) value)
173 (setq properties (cdr properties))))
175 ((eq (length p) 3) ;; search for latest entry
176 (let ((latest-date "") (latest-val))
177 (while (re-search-forward (car p) nil t)
178 (let ((date (vc-match-substring (elt p 2))))
179 (if (string< latest-date date)
181 (setq latest-date date)
183 (vc-match-substring (elt p 1)))))))
185 (progn (vc-file-setprop file (car properties) latest-val)
186 (setq properties (cdr properties))))
191 (defun vc-insert-file (file &optional limit blocksize)
192 ;; Insert the contents of FILE into the current buffer.
193 ;; Optional argument LIMIT is a regexp. If present,
194 ;; the file is inserted in chunks of size BLOCKSIZE
195 ;; (default 8 kByte), until the first occurrence of
196 ;; LIMIT is found. The function returns nil if FILE
199 (cond ((file-exists-p file)
201 (if (not blocksize) (setq blocksize 8192))
204 (setq s (buffer-size))
208 (insert-file-contents file nil s
210 (progn (beginning-of-line)
211 (re-search-forward limit nil t)))))))
212 (t (insert-file-contents file)))
213 (set-buffer-modified-p nil)
218 (defun vc-parse-locks (file locks)
219 ;; Parse RCS or SCCS locks.
220 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
221 ;; which is returned and stored into the property `vc-master-locks'.
223 (vc-file-setprop file 'vc-master-locks 'none)
224 (let ((found t) (index 0) master-locks version user)
225 (cond ((eq (vc-backend file) 'SCCS)
226 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
228 (setq version (substring locks
229 (match-beginning 1) (match-end 1)))
230 (setq user (substring locks
231 (match-beginning 2) (match-end 2)))
232 (setq master-locks (append master-locks
233 (list (cons version user))))
234 (setq index (match-end 0))))
235 ((eq (vc-backend file) 'RCS)
236 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
238 (setq version (substring locks
239 (match-beginning 2) (match-end 2)))
240 (setq user (substring locks
241 (match-beginning 1) (match-end 1)))
242 (setq master-locks (append master-locks
243 (list (cons version user))))
244 (setq index (match-end 0)))
245 (if (string-match ";[ \t\n]+strict;" locks index)
246 (vc-file-setprop file 'vc-checkout-model 'manual)
247 (vc-file-setprop file 'vc-checkout-model 'implicit))))
248 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
250 (defun vc-simple-command (okstatus command file &rest args)
251 ;; Simple version of vc-do-command, for use in vc-hooks only.
252 ;; Don't switch to the *vc-info* buffer before running the
253 ;; command, because that would change its default directory
254 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
256 (let ((exec-path (append vc-path exec-path)) exec-status
257 ;; Add vc-path to PATH for the execution of this command.
259 (cons (concat "PATH=" (getenv "PATH")
261 (mapconcat 'identity vc-path path-separator))
262 process-environment)))
264 (apply 'call-process command nil "*vc-info*" nil
265 (append args (list file))))
266 (cond ((> exec-status okstatus)
267 (switch-to-buffer (get-file-buffer file))
268 (shrink-window-if-larger-than-buffer
269 (display-buffer "*vc-info*"))
270 (error "Couldn't find version control information")))
273 (defun vc-fetch-master-properties (file)
274 ;; Fetch those properties of FILE that are stored in the master file.
275 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
276 ;; here because that is slow.
277 ;; That gets done if/when the functions vc-latest-version
278 ;; and vc-your-latest-version get called.
281 ((eq (vc-backend file) 'SCCS)
282 (set-buffer (get-buffer-create "*vc-info*"))
283 (if (vc-insert-file (vc-lock-file file))
284 (vc-parse-locks file (buffer-string))
285 (vc-file-setprop file 'vc-master-locks 'none))
286 (vc-insert-file (vc-name file) "^\001e")
288 (list '("^\001d D \\([^ ]+\\)" 1)
289 (list (concat "^\001d D \\([^ ]+\\) .* "
290 (regexp-quote (vc-user-login-name)) " ") 1))
292 '(vc-latest-version vc-your-latest-version)))
294 ((eq (vc-backend file) 'RCS)
295 (set-buffer (get-buffer-create "*vc-info*"))
296 (vc-insert-file (vc-name file) "^[0-9]")
298 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
299 '("^branch[ \t\n]+\\([^;]+\\);" 1)
300 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
305 ;; determine vc-master-workfile-version: it is either the head
306 ;; of the trunk, the head of the default branch, or the
307 ;; "default branch" itself, if that is a full revision number.
308 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
311 ((or (not default-branch) (string= "" default-branch))
312 (vc-file-setprop file 'vc-master-workfile-version
313 (vc-file-getprop file 'vc-head-version)))
314 ;; default branch is actually a revision
315 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
317 (vc-file-setprop file 'vc-master-workfile-version default-branch))
318 ;; else, search for the head of the default branch
319 (t (vc-insert-file (vc-name file) "^desc")
320 (vc-parse-buffer (list (list
322 (regexp-quote default-branch)
323 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
324 file '(vc-master-workfile-version)))))
325 ;; translate the locks
326 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
328 ((eq (vc-backend file) 'CVS)
330 ;; Call "cvs status" in the right directory, passing only the
331 ;; nondirectory part of the file name -- otherwise CVS might
332 ;; silently give a wrong result.
333 (let ((default-directory (file-name-directory file)))
334 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
335 (set-buffer (get-buffer "*vc-info*"))
337 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
338 ;; and CVS 1.4a1 says "Repository revision:".
339 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
340 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
342 '(vc-latest-version vc-cvs-status))
343 ;; Translate those status values that we understand into symbols.
344 ;; Any other value is converted to nil.
345 (let ((status (vc-file-getprop file 'vc-cvs-status)))
347 ((string-match "Up-to-date" status)
348 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
349 (vc-file-setprop file 'vc-checkout-time
350 (nth 5 (file-attributes file))))
351 ((vc-file-setprop file 'vc-cvs-status
353 ((string-match "Locally Modified" status) 'locally-modified)
354 ((string-match "Needs Merge" status) 'needs-merge)
355 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
357 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
358 ((string-match "Locally Added" status) 'locally-added)
359 ((string-match "New file!" status) 'locally-added)
362 (if (get-buffer "*vc-info*")
363 (kill-buffer (get-buffer "*vc-info*")))))
365 ;;; Functions that determine property values, by examining the
366 ;;; working file, the master file, or log program output
368 (defun vc-consult-rcs-headers (file)
369 ;; Search for RCS headers in FILE, and set properties
370 ;; accordingly. This function can be disabled by setting
371 ;; vc-consult-headers to nil.
372 ;; Returns: nil if no headers were found
373 ;; (or if the feature is disabled,
374 ;; or if there is currently no buffer
376 ;; 'rev if a workfile revision was found
377 ;; 'rev-and-lock if revision and lock info was found
379 ((or (not vc-consult-headers)
380 (not (get-file-buffer file))) nil)
381 ((let (status version locking-user)
383 (set-buffer (get-file-buffer file))
384 (goto-char (point-min))
386 ;; search for $Id or $Header
387 ;; -------------------------
388 ((or (and (search-forward "$Id: " nil t)
389 (looking-at "[^ ]+ \\([0-9.]+\\) "))
390 (and (progn (goto-char (point-min))
391 (search-forward "$Header: " nil t))
392 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
393 (goto-char (match-end 0))
394 ;; if found, store the revision number ...
395 (setq version (buffer-substring-no-properties (match-beginning 1)
397 ;; ... and check for the locking state
400 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
401 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
402 "[^ ]+ [^ ]+ ")) ; author & state
403 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
407 (setq locking-user 'none)
408 (setq status 'rev-and-lock))
409 ;; revision is locked by some user
410 ((looking-at "\\([^ ]+\\) \\$")
412 (buffer-substring-no-properties (match-beginning 1)
414 (setq status 'rev-and-lock))
415 ;; everything else: false
417 ;; unexpected information in
418 ;; keyword string --> quit
420 ;; search for $Revision
421 ;; --------------------
422 ((re-search-forward (concat "\\$"
423 "Revision: \\([0-9.]+\\) \\$")
425 ;; if found, store the revision number ...
426 (setq version (buffer-substring-no-properties (match-beginning 1)
428 ;; and see if there's any lock information
429 (goto-char (point-min))
430 (if (re-search-forward (concat "\\$" "Locker:") nil t)
431 (cond ((looking-at " \\([^ ]+\\) \\$")
432 (setq locking-user (buffer-substring-no-properties
435 (setq status 'rev-and-lock))
436 ((looking-at " *\\$")
437 (setq locking-user 'none)
438 (setq status 'rev-and-lock))
440 (setq locking-user 'none)
441 (setq status 'rev-and-lock)))
443 ;; else: nothing found
444 ;; -------------------
446 (if status (vc-file-setprop file 'vc-workfile-version version))
447 (and (eq status 'rev-and-lock)
448 (eq (vc-backend file) 'RCS)
449 (vc-file-setprop file 'vc-locking-user locking-user)
450 ;; If the file has headers, we don't want to query the master file,
451 ;; because that would eliminate all the performance gain the headers
452 ;; brought us. We therefore use a heuristic for the checkout model
453 ;; now: If we trust the file permissions, and the file is not
454 ;; locked, then if the file is read-only the checkout model is
455 ;; `manual', otherwise `implicit'.
456 (not (vc-mistrust-permissions file))
457 (not (vc-locking-user file))
458 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
459 (vc-file-setprop file 'vc-checkout-model 'manual)
460 (vc-file-setprop file 'vc-checkout-model 'implicit)))
463 ;;; Access functions to file properties
464 ;;; (Properties should be _set_ using vc-file-setprop, but
465 ;;; _retrieved_ only through these functions, which decide
466 ;;; if the property is already known or not. A property should
467 ;;; only be retrieved by vc-file-getprop if there is no
468 ;;; access function.)
470 ;;; properties indicating the backend
471 ;;; being used for FILE
473 (defun vc-backend-subdirectory-name (&optional file)
474 ;; Where the master and lock files for the current directory are kept
477 (and file (vc-backend file))
479 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
481 (defun vc-name (file)
482 "Return the master name of a file, nil if it is not registered.
483 For CVS, the full name of CVS/Entries is returned."
484 (or (vc-file-getprop file 'vc-name)
485 (let ((name-and-type (vc-registered file)))
488 (vc-file-setprop file 'vc-backend (cdr name-and-type))
489 (vc-file-setprop file 'vc-name (car name-and-type)))))))
491 (defun vc-backend (file)
492 "Return the version-control type of a file, nil if it is not registered."
494 (or (vc-file-getprop file 'vc-backend)
495 (let ((name-and-type (vc-registered file)))
498 (vc-file-setprop file 'vc-name (car name-and-type))
499 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
501 (defun vc-checkout-model (file)
502 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
503 ;; Return `implicit' if the file can be modified without locking it first.
505 (vc-file-getprop file 'vc-checkout-model)
507 ((eq (vc-backend file) 'SCCS)
508 (vc-file-setprop file 'vc-checkout-model 'manual))
509 ((eq (vc-backend file) 'RCS)
510 (vc-consult-rcs-headers file)
511 (or (vc-file-getprop file 'vc-checkout-model)
512 (progn (vc-fetch-master-properties file)
513 (vc-file-getprop file 'vc-checkout-model))))
514 ((eq (vc-backend file) 'CVS)
515 (vc-file-setprop file 'vc-checkout-model
516 (if (getenv "CVSREAD") 'manual 'implicit))))))
518 ;;; properties indicating the locking state
520 (defun vc-cvs-status (file)
521 ;; Return the cvs status of FILE
522 ;; (Status field in output of "cvs status")
523 (cond ((vc-file-getprop file 'vc-cvs-status))
524 (t (vc-fetch-master-properties file)
525 (vc-file-getprop file 'vc-cvs-status))))
527 (defun vc-master-locks (file)
528 ;; Return the lock entries in the master of FILE.
529 ;; Return 'none if there are no such entries, and a list
530 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
531 (cond ((vc-file-getprop file 'vc-master-locks))
532 (t (vc-fetch-master-properties file)
533 (vc-file-getprop file 'vc-master-locks))))
535 (defun vc-master-locking-user (file)
536 ;; Return the master file's idea of who is locking
537 ;; the current workfile version of FILE.
538 ;; Return 'none if it is not locked.
539 (let ((master-locks (vc-master-locks file)) lock)
540 (if (eq master-locks 'none) 'none
541 ;; search for a lock on the current workfile version
542 (setq lock (assoc (vc-workfile-version file) master-locks))
543 (cond (lock (cdr lock))
546 (defun vc-lock-from-permissions (file)
547 ;; If the permissions can be trusted for this file, determine the
548 ;; locking state from them. Returns (user-login-name), `none', or nil.
549 ;; This implementation assumes that any file which is under version
550 ;; control and has -rw-r--r-- is locked by its owner. This is true
551 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
552 ;; We have to be careful not to exclude files with execute bits on;
553 ;; scripts can be under version control too. Also, we must ignore the
554 ;; group-read and other-read bits, since paranoid users turn them off.
555 ;; This hack wins because calls to the somewhat expensive
556 ;; `vc-fetch-master-properties' function only have to be made if
557 ;; (a) the file is locked by someone other than the current user,
558 ;; or (b) some untoward manipulation behind vc's back has changed
559 ;; the owner or the `group' or `other' write bits.
560 (let ((attributes (file-attributes file)))
561 (if (not (vc-mistrust-permissions file))
562 (cond ((string-match ".r-..-..-." (nth 8 attributes))
563 (vc-file-setprop file 'vc-locking-user 'none))
564 ((and (= (nth 2 attributes) (user-uid))
565 (string-match ".rw..-..-." (nth 8 attributes)))
566 (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
569 (defun vc-user-login-name (&optional uid)
570 ;; Return the name under which the user is logged in, as a string.
571 ;; (With optional argument UID, return the name of that user.)
572 ;; This function does the same as `user-login-name', but unlike
573 ;; that, it never returns nil. If a UID cannot be resolved, that
574 ;; UID is returned as a string.
575 (or (user-login-name uid)
576 (and uid (number-to-string uid))
577 (number-to-string (user-uid))))
579 (defun vc-file-owner (file)
580 ;; Return who owns FILE (user name, as a string).
581 (vc-user-login-name (nth 2 (file-attributes file))))
583 (defun vc-rcs-lock-from-diff (file)
584 ;; Diff the file against the master version. If differences are found,
585 ;; mark the file locked. This is only used for RCS with non-strict
586 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
587 ;; and remember the fact for the future.)
588 (let* ((version (concat "-r" (vc-workfile-version file)))
589 (status (if (eq vc-rcsdiff-knows-brief 'no)
590 (vc-simple-command 1 "rcsdiff" file version)
591 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
593 (if (not vc-rcsdiff-knows-brief)
594 (setq vc-rcsdiff-knows-brief 'no
595 status (vc-simple-command 1 "rcsdiff" file version))
596 (error "rcsdiff failed."))
597 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
599 (vc-file-setprop file 'vc-locking-user 'none)
600 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
602 (defun vc-locking-user (file)
603 ;; Return the name of the person currently holding a lock on FILE.
604 ;; Return nil if there is no such person.
605 ;; Under CVS, a file is considered locked if it has been modified since
606 ;; it was checked out.
607 ;; The property is cached. It is only looked up if it is currently nil.
608 ;; Note that, for a file that is not locked, the actual property value
609 ;; is `none', to distinguish it from an unknown locking state. That value
610 ;; is converted to nil by this function, and returned to the caller.
611 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
613 ;; if we already know the property, return it
614 (if (eq locking-user 'none) nil locking-user)
616 ;; otherwise, infer the property...
618 ((eq (vc-backend file) 'CVS)
619 (or (and (eq (vc-checkout-model file) 'manual)
620 (vc-lock-from-permissions file))
621 (and (equal (vc-file-getprop file 'vc-checkout-time)
622 (nth 5 (file-attributes file)))
623 (vc-file-setprop file 'vc-locking-user 'none))
624 (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
626 ((eq (vc-backend file) 'RCS)
629 ;; Check for RCS headers first
630 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
632 ;; If there are no headers, try to learn it
633 ;; from the permissions.
634 (and (setq p-lock (vc-lock-from-permissions file))
635 (if (eq p-lock 'none)
637 ;; If the permissions say "not locked", we know
638 ;; that the checkout model must be `manual'.
639 (vc-file-setprop file 'vc-checkout-model 'manual)
641 ;; If the permissions say "locked", we can only trust
642 ;; this *if* the checkout model is `manual'.
643 (eq (vc-checkout-model file) 'manual)))
645 ;; Otherwise, use lock information from the master file.
646 (vc-file-setprop file 'vc-locking-user
647 (vc-master-locking-user file)))
649 ;; Finally, if the file is not explicitly locked
650 ;; it might still be locked implicitly.
651 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
652 (eq (vc-checkout-model file) 'implicit)
653 (vc-rcs-lock-from-diff file))))
655 ((eq (vc-backend file) 'SCCS)
656 (or (vc-lock-from-permissions file)
657 (vc-file-setprop file 'vc-locking-user
658 (vc-master-locking-user file)))))
660 ;; convert a possible 'none value
661 (setq locking-user (vc-file-getprop file 'vc-locking-user))
662 (if (eq locking-user 'none) nil locking-user))))
664 ;;; properties to store current and recent version numbers
666 (defun vc-latest-version (file)
667 ;; Return version level of the latest version of FILE
668 (cond ((vc-file-getprop file 'vc-latest-version))
669 (t (vc-fetch-properties file)
670 (vc-file-getprop file 'vc-latest-version))))
672 (defun vc-your-latest-version (file)
673 ;; Return version level of the latest version of FILE checked in by you
674 (cond ((vc-file-getprop file 'vc-your-latest-version))
675 (t (vc-fetch-properties file)
676 (vc-file-getprop file 'vc-your-latest-version))))
678 (defun vc-master-workfile-version (file)
679 ;; Return the master file's idea of what is the current workfile version.
680 ;; This property is defined for RCS only.
681 (cond ((vc-file-getprop file 'vc-master-workfile-version))
682 (t (vc-fetch-master-properties file)
683 (vc-file-getprop file 'vc-master-workfile-version))))
685 (defun vc-fetch-properties (file)
686 ;; Fetch vc-latest-version and vc-your-latest-version
687 ;; if that wasn't already done.
689 ((eq (vc-backend file) 'RCS)
691 (set-buffer (get-buffer-create "*vc-info*"))
692 (vc-insert-file (vc-name file) "^desc")
694 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
695 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
696 "date[ \t]+\\([0-9.]+\\);[ \t]+"
698 (regexp-quote (vc-user-login-name)) ";") 1 2))
700 '(vc-latest-version vc-your-latest-version))
701 (if (get-buffer "*vc-info*")
702 (kill-buffer (get-buffer "*vc-info*")))))
703 (t (vc-fetch-master-properties file))
706 (defun vc-workfile-version (file)
707 ;; Return version level of the current workfile FILE
708 ;; This is attempted by first looking at the RCS keywords.
709 ;; If there are no keywords in the working file,
710 ;; vc-master-workfile-version is taken.
711 ;; Note that this property is cached, that is, it is only
712 ;; looked up if it is nil.
713 ;; For SCCS, this property is equivalent to vc-latest-version.
714 (cond ((vc-file-getprop file 'vc-workfile-version))
715 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
716 ((eq (vc-backend file) 'RCS)
717 (if (vc-consult-rcs-headers file)
718 (vc-file-getprop file 'vc-workfile-version)
719 (let ((rev (cond ((vc-master-workfile-version file))
720 ((vc-latest-version file)))))
721 (vc-file-setprop file 'vc-workfile-version rev)
723 ((eq (vc-backend file) 'CVS)
724 (if (vc-consult-rcs-headers file) ;; CVS
725 (vc-file-getprop file 'vc-workfile-version)
727 (vc-find-cvs-master (file-name-directory file)
728 (file-name-nondirectory file)))
729 (vc-file-getprop file 'vc-workfile-version)))))
731 ;;; actual version-control code starts here
733 (defun vc-registered (file)
734 (let (handler handlers)
735 (if (boundp 'file-name-handler-alist)
736 (setq handler (find-file-name-handler file 'vc-registered)))
738 (funcall handler 'vc-registered file)
739 ;; Search for a master corresponding to the given file
740 (let ((dirname (or (file-name-directory file) ""))
741 (basename (file-name-nondirectory file)))
744 (function (lambda (s)
746 (funcall s dirname basename)
747 (let ((trial (format (car s) dirname basename)))
748 (if (and (file-exists-p trial)
749 ;; Make sure the file we found with name
750 ;; TRIAL is not the source file itself.
751 ;; That can happen with RCS-style names
752 ;; if the file name is truncated
753 ;; (e.g. to 14 chars). See if either
754 ;; directory or attributes differ.
755 (or (not (string= dirname
756 (file-name-directory trial)))
758 (file-attributes file)
759 (file-attributes trial)))))
760 (throw 'found (cons trial (cdr s))))))))
764 (defun vc-find-cvs-master (dirname basename)
765 ;; Check if DIRNAME/BASENAME is handled by CVS.
766 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
767 ;; Note: This function throws the name of CVS/Entries
768 ;; NOT that of the RCS master file (because we wouldn't be able
769 ;; to access it under remote CVS).
770 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
771 (if (and vc-handle-cvs
772 (file-directory-p (concat dirname "CVS/"))
773 (file-readable-p (concat dirname "CVS/Entries")))
774 (let (buffer time (fold case-fold-search)
775 (file (concat dirname basename)))
778 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
779 (vc-insert-file (concat dirname "CVS/Entries"))
780 (goto-char (point-min))
781 ;; make sure the file name is searched
783 (setq case-fold-search nil)
787 (concat "^/" (regexp-quote basename)
788 "/\\([^/]*\\)/[^ /]* \\([A-Z][a-z][a-z]\\) *\\([0-9]*\\) \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\) \\([0-9]*\\)")
790 (setq case-fold-search fold) ;; restore the old value
791 ;; We found it. Store away version number now that we
792 ;; are anyhow so close to finding it.
793 (vc-file-setprop file
796 ;; If the file hasn't been modified since checkout,
797 ;; store the checkout-time.
798 (let ((mtime (nth 5 (file-attributes file)))
799 (second (string-to-number (match-string 6)))
800 (minute (string-to-number (match-string 5)))
801 (hour (string-to-number (match-string 4)))
802 (day (string-to-number (match-string 3)))
803 (year (string-to-number (match-string 7))))
806 second minute hour day
809 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
812 (vc-file-setprop file 'vc-checkout-time mtime)
813 (vc-file-setprop file 'vc-checkout-time 0)))
814 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
815 ;; entry for a "locally added" file (not yet committed)
817 (concat "^/" (regexp-quote basename) "/0/Initial ") nil t)
818 (setq case-fold-search fold) ;; restore the old value
819 (vc-file-setprop file 'vc-checkout-time 0)
820 (vc-file-setprop file 'vc-workfile-version "0")
821 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
823 (concat "^/" (regexp-quote basename)
824 "/\\([^/]*\\)/Initial") nil t)
825 (setq case-fold-search fold) ;; restore the old value
826 (vc-file-setprop file 'vc-workfile-version "0")
827 (vc-file-setprop file 'vc-checkout-time 0)
828 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
829 (t (setq case-fold-search fold) ;; restore the old value
831 (kill-buffer buffer)))))
833 (defun vc-buffer-backend ()
834 "Return the version-control type of the visited file, or nil if none."
835 (if (eq vc-buffer-backend t)
836 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
839 (defun vc-toggle-read-only (&optional verbose)
840 "Change read-only status of current buffer, perhaps via version control.
841 If the buffer is visiting a file registered with version control,
842 then check the file in or out. Otherwise, just change the read-only flag
843 of the buffer. With prefix argument, ask for version number."
845 (if (vc-backend (buffer-file-name))
846 (vc-next-action verbose)
848 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
850 (defun vc-after-save ()
851 ;; Function to be called by basic-save-buffer (in files.el).
852 ;; If the file in the current buffer is under version control,
853 ;; not locked, and the checkout model for it is `implicit',
854 ;; mark it "locked" and redisplay the mode line.
855 (let ((file (buffer-file-name)))
856 (and (vc-file-getprop file 'vc-backend)
857 ;; ...check the property directly, not through the function of the
858 ;; same name. Otherwise Emacs would check for a master file
859 ;; each time a non-version-controlled buffer is saved.
860 ;; The property is computed when the file is visited, so if it
861 ;; is `nil' now, it is certain that the file is NOT
862 ;; version-controlled.
863 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
864 (nth 5 (file-attributes file)))
865 ;; File has been saved in the same second in which
866 ;; it was checked out. Clear the checkout-time
867 ;; to avoid confusion.
868 (vc-file-setprop file 'vc-checkout-time nil))
870 (not (vc-locking-user file))
871 (eq (vc-checkout-model file) 'implicit)
872 (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
873 (or (and (eq (vc-backend file) 'CVS)
874 (vc-file-setprop file 'vc-cvs-status nil))
876 (vc-mode-line file))))
878 (defun vc-mode-line (file &optional label)
879 "Set `vc-mode' to display type of version control for FILE.
880 The value is set in the current buffer, which should be the buffer
881 visiting FILE. Second optional arg LABEL is put in place of version
882 control system name."
883 (interactive (list buffer-file-name nil))
884 (let ((vc-type (vc-backend file)))
887 (concat " " (or label (symbol-name vc-type))
888 (and vc-display-status (vc-status file)))))
889 ;; If the file is locked by some other user, make
890 ;; the buffer read-only. Like this, even root
891 ;; cannot modify a file that someone else has locked.
893 (equal file (buffer-file-name))
894 (vc-locking-user file)
895 (not (string= (vc-user-login-name) (vc-locking-user file)))
896 (setq buffer-read-only t))
897 ;; If the user is root, and the file is not owner-writable,
898 ;; then pretend that we can't write it
899 ;; even though we can (because root can write anything).
900 ;; This way, even root cannot modify a file that isn't locked.
902 (equal file (buffer-file-name))
903 (not buffer-read-only)
904 (zerop (user-real-uid))
905 (zerop (logand (file-modes (buffer-file-name)) 128))
906 (setq buffer-read-only t))
907 (force-mode-line-update)
908 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
911 (defun vc-status (file)
912 ;; Return string for placement in modeline by `vc-mode-line'.
915 ;; "-REV" if the revision is not locked
916 ;; ":REV" if the revision is locked by the user
917 ;; ":LOCKER:REV" if the revision is locked by somebody else
918 ;; " @@" for a CVS file that is added, but not yet committed
920 ;; In the CVS case, a "locked" working file is a
921 ;; working file that is modified with respect to the master.
922 ;; The file is "locked" from the moment when the user saves
923 ;; the modified buffer.
925 ;; This function assumes that the file is registered.
927 (let ((locker (vc-locking-user file))
928 (rev (vc-workfile-version file)))
929 (cond ((string= "0" rev)
933 ((string= locker (vc-user-login-name))
936 (concat ":" locker ":" rev)))))
938 (defun vc-follow-link ()
939 ;; If the current buffer visits a symbolic link, this function makes it
940 ;; visit the real file instead. If the real file is already visited in
941 ;; another buffer, make that buffer current, and kill the buffer
942 ;; that visits the link.
943 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
944 (true-buffer (find-buffer-visiting truename))
945 (this-buffer (current-buffer)))
946 (if (eq true-buffer this-buffer)
948 (kill-buffer this-buffer)
949 ;; In principle, we could do something like set-visited-file-name.
950 ;; However, it can't be exactly the same as set-visited-file-name.
951 ;; I'm not going to work out the details right now. -- rms.
952 (set-buffer (find-file-noselect truename)))
953 (set-buffer true-buffer)
954 (kill-buffer this-buffer))))
956 ;;; install a call to the above as a find-file hook
957 (defun vc-find-file-hook ()
958 ;; Recompute whether file is version controlled,
959 ;; if user has killed the buffer and revisited.
962 (vc-file-clearprops buffer-file-name)
964 ((vc-backend buffer-file-name)
965 (vc-mode-line buffer-file-name)
966 (cond ((not vc-make-backup-files)
967 ;; Use this variable, not make-backup-files,
968 ;; because this is for things that depend on the file name.
969 (make-local-variable 'backup-inhibited)
970 (setq backup-inhibited t))))
971 ((let* ((link (file-symlink-p buffer-file-name))
972 (link-type (and link (vc-backend (file-chase-links link)))))
974 (cond ((eq vc-follow-symlinks nil)
976 "Warning: symbolic link to %s-controlled source file" link-type))
977 ((or (not (eq vc-follow-symlinks 'ask))
978 ;; If we already visited this file by following
979 ;; the link, don't ask again if we try to visit
980 ;; it again. GUD does that, and repeated questions
983 (abbreviate-file-name (file-chase-links buffer-file-name))))
986 (message "Followed link to %s" buffer-file-name)
989 (if (yes-or-no-p (format
990 "Symbolic link to %s-controlled source file; follow link? " link-type))
991 (progn (vc-follow-link)
992 (message "Followed link to %s" buffer-file-name)
995 "Warning: editing through the link bypasses version control")
998 (add-hook 'find-file-hooks 'vc-find-file-hook)
1000 ;;; more hooks, this time for file-not-found
1001 (defun vc-file-not-found-hook ()
1002 "When file is not found, try to check it out from RCS or SCCS.
1003 Returns t if checkout was successful, nil otherwise."
1004 (if (vc-backend buffer-file-name)
1007 (setq default-directory (file-name-directory (buffer-file-name)))
1008 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1010 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
1012 ;; Discard info about a file when we kill its buffer.
1013 (defun vc-kill-buffer-hook ()
1014 (if (stringp (buffer-file-name))
1016 (vc-file-clearprops (buffer-file-name))
1017 (kill-local-variable 'vc-buffer-backend))))
1019 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1021 ;;; Now arrange for bindings and autoloading of the main package.
1022 ;;; Bindings for this have to go in the global map, as we'll often
1023 ;;; want to call them from random buffers.
1025 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
1026 (if (not (keymapp vc-prefix-map))
1028 (setq vc-prefix-map (make-sparse-keymap))
1029 (define-key global-map "\C-xv" vc-prefix-map)
1030 (define-key vc-prefix-map "a" 'vc-update-change-log)
1031 (define-key vc-prefix-map "c" 'vc-cancel-version)
1032 (define-key vc-prefix-map "d" 'vc-directory)
1033 (define-key vc-prefix-map "h" 'vc-insert-headers)
1034 (define-key vc-prefix-map "i" 'vc-register)
1035 (define-key vc-prefix-map "l" 'vc-print-log)
1036 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
1037 (define-key vc-prefix-map "s" 'vc-create-snapshot)
1038 (define-key vc-prefix-map "u" 'vc-revert-buffer)
1039 (define-key vc-prefix-map "v" 'vc-next-action)
1040 (define-key vc-prefix-map "=" 'vc-diff)
1041 (define-key vc-prefix-map "~" 'vc-version-other-window)))
1043 (if (not (boundp 'vc-menu-map))
1044 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1047 ;;(define-key vc-menu-map [show-files]
1048 ;; '("Show Files under VC" . (vc-directory t)))
1049 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
1050 (define-key vc-menu-map [separator1] '("----"))
1051 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
1052 (define-key vc-menu-map [vc-version-other-window]
1053 '("Show Other Version" . vc-version-other-window))
1054 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
1055 (define-key vc-menu-map [vc-update-change-log]
1056 '("Update ChangeLog" . vc-update-change-log))
1057 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
1058 (define-key vc-menu-map [separator2] '("----"))
1059 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
1060 (define-key vc-menu-map [vc-revert-buffer]
1061 '("Revert to Last Version" . vc-revert-buffer))
1062 (define-key vc-menu-map [vc-insert-header]
1063 '("Insert Header" . vc-insert-headers))
1064 (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
1065 (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
1066 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
1068 (put 'vc-rename-file 'menu-enable 'vc-mode)
1069 (put 'vc-version-other-window 'menu-enable 'vc-mode)
1070 (put 'vc-diff 'menu-enable 'vc-mode)
1071 (put 'vc-update-change-log 'menu-enable
1072 '(eq (vc-buffer-backend) 'RCS))
1073 (put 'vc-print-log 'menu-enable 'vc-mode)
1074 (put 'vc-cancel-version 'menu-enable 'vc-mode)
1075 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
1076 (put 'vc-insert-headers 'menu-enable 'vc-mode)
1077 (put 'vc-next-action 'menu-enable 'vc-mode)
1078 (put 'vc-toggle-read-only 'menu-enable 'vc-mode)
1079 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
1083 ;;; vc-hooks.el ends here