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 (defcustom 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."
39 :type '(choice (const nil) (const RCS) (const SCCS))
42 (defcustom vc-handle-cvs t
43 "*If non-nil, use VC for files managed with CVS.
44 If it is nil, don't use VC for those files."
48 (defcustom vc-rcsdiff-knows-brief nil
49 "*Indicates whether rcsdiff understands the --brief option.
50 The value is either `yes', `no', or nil. If it is nil, VC tries
51 to use --brief and sets this variable to remember whether it worked."
52 :type '(choice (const nil) (const yes) (const no))
56 (if (file-directory-p "/usr/sccs")
59 "*List of extra directories to search for version control commands."
60 :type '(repeat directory)
64 (defcustom vc-master-templates
65 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
66 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
68 "*Where to look for version-control master files.
69 The first pair corresponding to a given back end is used as a template
70 when creating new masters.
71 Setting this variable to nil turns off use of VC entirely."
75 (defcustom vc-make-backup-files nil
76 "*If non-nil, backups of registered files are made as with other files.
77 If nil (the default), files covered by version control don't get backups."
81 (defcustom vc-follow-symlinks 'ask
82 "*Indicates what to do if you visit a symbolic link to a file
83 that is under version control. Editing such a file through the
84 link bypasses the version control system, which is dangerous and
85 probably not what you want.
86 If this variable is t, VC follows the link and visits the real file,
87 telling you about it in the echo area. If it is `ask', VC asks for
88 confirmation whether it should follow the link. If nil, the link is
89 visited and a warning displayed."
90 :type '(choice (const ask) (const nil) (const t))
93 (defcustom vc-display-status t
94 "*If non-nil, display revision number and lock status in modeline.
95 Otherwise, not displayed."
100 (defcustom vc-consult-headers t
101 "*If non-nil, identify work files by searching for version headers."
105 (defcustom vc-keep-workfiles t
106 "*If non-nil, don't delete working files after registering changes.
107 If the back-end is CVS, workfiles are always kept, regardless of the
112 (defcustom vc-mistrust-permissions nil
113 "*If non-nil, don't assume that permissions and ownership track
114 version-control status. If nil, do rely on the permissions.
115 See also variable `vc-consult-headers'."
119 (defun vc-mistrust-permissions (file)
120 ;; Access function to the above.
121 (or (eq vc-mistrust-permissions 't)
122 (and vc-mistrust-permissions
123 (funcall vc-mistrust-permissions
124 (vc-backend-subdirectory-name file)))))
126 ;; Tell Emacs about this new kind of minor mode
127 (if (not (assoc 'vc-mode minor-mode-alist))
128 (setq minor-mode-alist (cons '(vc-mode vc-mode)
131 (make-variable-buffer-local 'vc-mode)
132 (put 'vc-mode 'permanent-local t)
134 ;; We need a notion of per-file properties because the version
135 ;; control state of a file is expensive to derive --- we compute
136 ;; them when the file is initially found, keep them up to date
137 ;; during any subsequent VC operations, and forget them when
138 ;; the buffer is killed.
140 (defmacro vc-error-occurred (&rest body)
141 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
143 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
144 "Obarray for per-file properties.")
146 (defvar vc-buffer-backend t)
147 (make-variable-buffer-local 'vc-buffer-backend)
149 (defun vc-file-setprop (file property value)
150 ;; set per-file property
151 (put (intern file vc-file-prop-obarray) property value))
153 (defun vc-file-getprop (file property)
154 ;; get per-file property
155 (get (intern file vc-file-prop-obarray) property))
157 (defun vc-file-clearprops (file)
158 ;; clear all properties of a given file
159 (setplist (intern file vc-file-prop-obarray) nil))
161 ;;; Functions that determine property values, by examining the
162 ;;; working file, the master file, or log program output
164 (defun vc-match-substring (bn)
165 (buffer-substring (match-beginning bn) (match-end bn)))
167 (defun vc-lock-file (file)
168 ;; Generate lock file name corresponding to FILE
169 (let ((master (vc-name file)))
172 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
174 (substring master (match-beginning 1) (match-end 1))
176 (substring master (match-beginning 2) (match-end 2))))))
178 (defun vc-parse-buffer (patterns &optional file properties)
179 ;; Use PATTERNS to parse information out of the current buffer.
180 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
181 ;; is the pattern to be matched, and the second (an integer) is the
182 ;; number of the subexpression that should be returned. If there's
183 ;; a third element (also the number of a subexpression), that
184 ;; subexpression is assumed to be a date field and we want the most
185 ;; recent entry matching the template.
186 ;; If FILE and PROPERTIES are given, the latter must be a list of
187 ;; properties of the same length as PATTERNS; each property is assigned
188 ;; the corresponding value.
189 (mapcar (function (lambda (p)
190 (goto-char (point-min))
192 ((eq (length p) 2) ;; search for first entry
194 (if (re-search-forward (car p) nil t)
195 (setq value (vc-match-substring (elt p 1))))
197 (progn (vc-file-setprop file (car properties) value)
198 (setq properties (cdr properties))))
200 ((eq (length p) 3) ;; search for latest entry
201 (let ((latest-date "") (latest-val))
202 (while (re-search-forward (car p) nil t)
203 (let ((date (vc-match-substring (elt p 2))))
204 (if (string< latest-date date)
206 (setq latest-date date)
208 (vc-match-substring (elt p 1)))))))
210 (progn (vc-file-setprop file (car properties) latest-val)
211 (setq properties (cdr properties))))
216 (defun vc-insert-file (file &optional limit blocksize)
217 ;; Insert the contents of FILE into the current buffer.
218 ;; Optional argument LIMIT is a regexp. If present,
219 ;; the file is inserted in chunks of size BLOCKSIZE
220 ;; (default 8 kByte), until the first occurrence of
221 ;; LIMIT is found. The function returns nil if FILE
224 (cond ((file-exists-p file)
226 (if (not blocksize) (setq blocksize 8192))
229 (setq s (buffer-size))
233 (insert-file-contents file nil s
235 (progn (beginning-of-line)
236 (re-search-forward limit nil t)))))))
237 (t (insert-file-contents file)))
238 (set-buffer-modified-p nil)
243 (defun vc-parse-locks (file locks)
244 ;; Parse RCS or SCCS locks.
245 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
246 ;; which is returned and stored into the property `vc-master-locks'.
248 (vc-file-setprop file 'vc-master-locks 'none)
249 (let ((found t) (index 0) master-locks version user)
250 (cond ((eq (vc-backend file) 'SCCS)
251 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
253 (setq version (substring locks
254 (match-beginning 1) (match-end 1)))
255 (setq user (substring locks
256 (match-beginning 2) (match-end 2)))
257 (setq master-locks (append master-locks
258 (list (cons version user))))
259 (setq index (match-end 0))))
260 ((eq (vc-backend file) 'RCS)
261 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
263 (setq version (substring locks
264 (match-beginning 2) (match-end 2)))
265 (setq user (substring locks
266 (match-beginning 1) (match-end 1)))
267 (setq master-locks (append master-locks
268 (list (cons version user))))
269 (setq index (match-end 0)))
270 (if (string-match ";[ \t\n]+strict;" locks index)
271 (vc-file-setprop file 'vc-checkout-model 'manual)
272 (vc-file-setprop file 'vc-checkout-model 'implicit))))
273 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
275 (defun vc-simple-command (okstatus command file &rest args)
276 ;; Simple version of vc-do-command, for use in vc-hooks only.
277 ;; Don't switch to the *vc-info* buffer before running the
278 ;; command, because that would change its default directory
279 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
281 (let ((exec-path (append vc-path exec-path)) exec-status
282 ;; Add vc-path to PATH for the execution of this command.
284 (cons (concat "PATH=" (getenv "PATH")
286 (mapconcat 'identity vc-path path-separator))
287 process-environment)))
289 (apply 'call-process command nil "*vc-info*" nil
290 (append args (list file))))
291 (cond ((> exec-status okstatus)
292 (switch-to-buffer (get-file-buffer file))
293 (shrink-window-if-larger-than-buffer
294 (display-buffer "*vc-info*"))
295 (error "Couldn't find version control information")))
298 (defun vc-fetch-master-properties (file)
299 ;; Fetch those properties of FILE that are stored in the master file.
300 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
301 ;; here because that is slow.
302 ;; That gets done if/when the functions vc-latest-version
303 ;; and vc-your-latest-version get called.
306 ((eq (vc-backend file) 'SCCS)
307 (set-buffer (get-buffer-create "*vc-info*"))
308 (if (vc-insert-file (vc-lock-file file))
309 (vc-parse-locks file (buffer-string))
310 (vc-file-setprop file 'vc-master-locks 'none))
311 (vc-insert-file (vc-name file) "^\001e")
313 (list '("^\001d D \\([^ ]+\\)" 1)
314 (list (concat "^\001d D \\([^ ]+\\) .* "
315 (regexp-quote (vc-user-login-name)) " ") 1))
317 '(vc-latest-version vc-your-latest-version)))
319 ((eq (vc-backend file) 'RCS)
320 (set-buffer (get-buffer-create "*vc-info*"))
321 (vc-insert-file (vc-name file) "^[0-9]")
323 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
324 '("^branch[ \t\n]+\\([^;]+\\);" 1)
325 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
330 ;; determine vc-master-workfile-version: it is either the head
331 ;; of the trunk, the head of the default branch, or the
332 ;; "default branch" itself, if that is a full revision number.
333 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
336 ((or (not default-branch) (string= "" default-branch))
337 (vc-file-setprop file 'vc-master-workfile-version
338 (vc-file-getprop file 'vc-head-version)))
339 ;; default branch is actually a revision
340 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
342 (vc-file-setprop file 'vc-master-workfile-version default-branch))
343 ;; else, search for the head of the default branch
344 (t (vc-insert-file (vc-name file) "^desc")
345 (vc-parse-buffer (list (list
347 (regexp-quote default-branch)
348 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
349 file '(vc-master-workfile-version)))))
350 ;; translate the locks
351 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
353 ((eq (vc-backend file) 'CVS)
355 ;; Call "cvs status" in the right directory, passing only the
356 ;; nondirectory part of the file name -- otherwise CVS might
357 ;; silently give a wrong result.
358 (let ((default-directory (file-name-directory file)))
359 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
360 (set-buffer (get-buffer "*vc-info*"))
362 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
363 ;; and CVS 1.4a1 says "Repository revision:".
364 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
365 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
367 '(vc-latest-version vc-cvs-status))
368 ;; Translate those status values that we understand into symbols.
369 ;; Any other value is converted to nil.
370 (let ((status (vc-file-getprop file 'vc-cvs-status)))
372 ((string-match "Up-to-date" status)
373 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
374 (vc-file-setprop file 'vc-checkout-time
375 (nth 5 (file-attributes file))))
376 ((vc-file-setprop file 'vc-cvs-status
378 ((string-match "Locally Modified" status) 'locally-modified)
379 ((string-match "Needs Merge" status) 'needs-merge)
380 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
382 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
383 ((string-match "Locally Added" status) 'locally-added)
384 ((string-match "New file!" status) 'locally-added)
387 (if (get-buffer "*vc-info*")
388 (kill-buffer (get-buffer "*vc-info*")))))
390 ;;; Functions that determine property values, by examining the
391 ;;; working file, the master file, or log program output
393 (defun vc-consult-rcs-headers (file)
394 ;; Search for RCS headers in FILE, and set properties
395 ;; accordingly. This function can be disabled by setting
396 ;; vc-consult-headers to nil.
397 ;; Returns: nil if no headers were found
398 ;; (or if the feature is disabled,
399 ;; or if there is currently no buffer
401 ;; 'rev if a workfile revision was found
402 ;; 'rev-and-lock if revision and lock info was found
404 ((or (not vc-consult-headers)
405 (not (get-file-buffer file))) nil)
406 ((let (status version locking-user)
408 (set-buffer (get-file-buffer file))
409 (goto-char (point-min))
411 ;; search for $Id or $Header
412 ;; -------------------------
413 ((or (and (search-forward "$Id: " nil t)
414 (looking-at "[^ ]+ \\([0-9.]+\\) "))
415 (and (progn (goto-char (point-min))
416 (search-forward "$Header: " nil t))
417 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
418 (goto-char (match-end 0))
419 ;; if found, store the revision number ...
420 (setq version (buffer-substring-no-properties (match-beginning 1)
422 ;; ... and check for the locking state
425 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
426 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
427 "[^ ]+ [^ ]+ ")) ; author & state
428 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
432 (setq locking-user 'none)
433 (setq status 'rev-and-lock))
434 ;; revision is locked by some user
435 ((looking-at "\\([^ ]+\\) \\$")
437 (buffer-substring-no-properties (match-beginning 1)
439 (setq status 'rev-and-lock))
440 ;; everything else: false
442 ;; unexpected information in
443 ;; keyword string --> quit
445 ;; search for $Revision
446 ;; --------------------
447 ((re-search-forward (concat "\\$"
448 "Revision: \\([0-9.]+\\) \\$")
450 ;; if found, store the revision number ...
451 (setq version (buffer-substring-no-properties (match-beginning 1)
453 ;; and see if there's any lock information
454 (goto-char (point-min))
455 (if (re-search-forward (concat "\\$" "Locker:") nil t)
456 (cond ((looking-at " \\([^ ]+\\) \\$")
457 (setq locking-user (buffer-substring-no-properties
460 (setq status 'rev-and-lock))
461 ((looking-at " *\\$")
462 (setq locking-user 'none)
463 (setq status 'rev-and-lock))
465 (setq locking-user 'none)
466 (setq status 'rev-and-lock)))
468 ;; else: nothing found
469 ;; -------------------
471 (if status (vc-file-setprop file 'vc-workfile-version version))
472 (and (eq status 'rev-and-lock)
473 (eq (vc-backend file) 'RCS)
474 (vc-file-setprop file 'vc-locking-user locking-user)
475 ;; If the file has headers, we don't want to query the master file,
476 ;; because that would eliminate all the performance gain the headers
477 ;; brought us. We therefore use a heuristic for the checkout model
478 ;; now: If we trust the file permissions, and the file is not
479 ;; locked, then if the file is read-only the checkout model is
480 ;; `manual', otherwise `implicit'.
481 (not (vc-mistrust-permissions file))
482 (not (vc-locking-user file))
483 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
484 (vc-file-setprop file 'vc-checkout-model 'manual)
485 (vc-file-setprop file 'vc-checkout-model 'implicit)))
488 ;;; Access functions to file properties
489 ;;; (Properties should be _set_ using vc-file-setprop, but
490 ;;; _retrieved_ only through these functions, which decide
491 ;;; if the property is already known or not. A property should
492 ;;; only be retrieved by vc-file-getprop if there is no
493 ;;; access function.)
495 ;;; properties indicating the backend
496 ;;; being used for FILE
498 (defun vc-backend-subdirectory-name (&optional file)
499 ;; Where the master and lock files for the current directory are kept
502 (and file (vc-backend file))
504 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
506 (defun vc-name (file)
507 "Return the master name of a file, nil if it is not registered.
508 For CVS, the full name of CVS/Entries is returned."
509 (or (vc-file-getprop file 'vc-name)
510 (let ((name-and-type (vc-registered file)))
513 (vc-file-setprop file 'vc-backend (cdr name-and-type))
514 (vc-file-setprop file 'vc-name (car name-and-type)))))))
516 (defun vc-backend (file)
517 "Return the version-control type of a file, nil if it is not registered."
519 (or (vc-file-getprop file 'vc-backend)
520 (let ((name-and-type (vc-registered file)))
523 (vc-file-setprop file 'vc-name (car name-and-type))
524 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
526 (defun vc-checkout-model (file)
527 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
528 ;; Return `implicit' if the file can be modified without locking it first.
530 (vc-file-getprop file 'vc-checkout-model)
532 ((eq (vc-backend file) 'SCCS)
533 (vc-file-setprop file 'vc-checkout-model 'manual))
534 ((eq (vc-backend file) 'RCS)
535 (vc-consult-rcs-headers file)
536 (or (vc-file-getprop file 'vc-checkout-model)
537 (progn (vc-fetch-master-properties file)
538 (vc-file-getprop file 'vc-checkout-model))))
539 ((eq (vc-backend file) 'CVS)
540 (vc-file-setprop file 'vc-checkout-model
542 ((getenv "CVSREAD") 'manual)
543 ;; If the file is not writeable, this is probably because the
544 ;; file is being "watched" by other developers. Use "manual"
545 ;; checkout in this case. (If vc-mistrust-permissions was t,
546 ;; we actually shouldn't trust this, but there is no other way
547 ;; to learn this from CVS at the moment (version 1.9).)
548 ((string-match "r-..-..-." (nth 8 (file-attributes file)))
552 ;;; properties indicating the locking state
554 (defun vc-cvs-status (file)
555 ;; Return the cvs status of FILE
556 ;; (Status field in output of "cvs status")
557 (cond ((vc-file-getprop file 'vc-cvs-status))
558 (t (vc-fetch-master-properties file)
559 (vc-file-getprop file 'vc-cvs-status))))
561 (defun vc-master-locks (file)
562 ;; Return the lock entries in the master of FILE.
563 ;; Return 'none if there are no such entries, and a list
564 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
565 (cond ((vc-file-getprop file 'vc-master-locks))
566 (t (vc-fetch-master-properties file)
567 (vc-file-getprop file 'vc-master-locks))))
569 (defun vc-master-locking-user (file)
570 ;; Return the master file's idea of who is locking
571 ;; the current workfile version of FILE.
572 ;; Return 'none if it is not locked.
573 (let ((master-locks (vc-master-locks file)) lock)
574 (if (eq master-locks 'none) 'none
575 ;; search for a lock on the current workfile version
576 (setq lock (assoc (vc-workfile-version file) master-locks))
577 (cond (lock (cdr lock))
580 (defun vc-lock-from-permissions (file)
581 ;; If the permissions can be trusted for this file, determine the
582 ;; locking state from them. Returns (user-login-name), `none', or nil.
583 ;; This implementation assumes that any file which is under version
584 ;; control and has -rw-r--r-- is locked by its owner. This is true
585 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
586 ;; We have to be careful not to exclude files with execute bits on;
587 ;; scripts can be under version control too. Also, we must ignore the
588 ;; group-read and other-read bits, since paranoid users turn them off.
589 ;; This hack wins because calls to the somewhat expensive
590 ;; `vc-fetch-master-properties' function only have to be made if
591 ;; (a) the file is locked by someone other than the current user,
592 ;; or (b) some untoward manipulation behind vc's back has changed
593 ;; the owner or the `group' or `other' write bits.
594 (let ((attributes (file-attributes file)))
595 (if (not (vc-mistrust-permissions file))
596 (cond ((string-match ".r-..-..-." (nth 8 attributes))
597 (vc-file-setprop file 'vc-locking-user 'none))
598 ((and (= (nth 2 attributes) (user-uid))
599 (string-match ".rw..-..-." (nth 8 attributes)))
600 (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
603 (defun vc-user-login-name (&optional uid)
604 ;; Return the name under which the user is logged in, as a string.
605 ;; (With optional argument UID, return the name of that user.)
606 ;; This function does the same as `user-login-name', but unlike
607 ;; that, it never returns nil. If a UID cannot be resolved, that
608 ;; UID is returned as a string.
609 (or (user-login-name uid)
610 (and uid (number-to-string uid))
611 (number-to-string (user-uid))))
613 (defun vc-file-owner (file)
614 ;; Return who owns FILE (user name, as a string).
615 (vc-user-login-name (nth 2 (file-attributes file))))
617 (defun vc-rcs-lock-from-diff (file)
618 ;; Diff the file against the master version. If differences are found,
619 ;; mark the file locked. This is only used for RCS with non-strict
620 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
621 ;; and remember the fact for the future.)
622 (let* ((version (concat "-r" (vc-workfile-version file)))
623 (status (if (eq vc-rcsdiff-knows-brief 'no)
624 (vc-simple-command 1 "rcsdiff" file version)
625 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
627 (if (not vc-rcsdiff-knows-brief)
628 (setq vc-rcsdiff-knows-brief 'no
629 status (vc-simple-command 1 "rcsdiff" file version))
630 (error "rcsdiff failed."))
631 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
633 (vc-file-setprop file 'vc-locking-user 'none)
634 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
636 (defun vc-locking-user (file)
637 ;; Return the name of the person currently holding a lock on FILE.
638 ;; Return nil if there is no such person.
639 ;; Under CVS, a file is considered locked if it has been modified since
640 ;; it was checked out.
641 ;; The property is cached. It is only looked up if it is currently nil.
642 ;; Note that, for a file that is not locked, the actual property value
643 ;; is `none', to distinguish it from an unknown locking state. That value
644 ;; is converted to nil by this function, and returned to the caller.
645 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
647 ;; if we already know the property, return it
648 (if (eq locking-user 'none) nil locking-user)
650 ;; otherwise, infer the property...
652 ((eq (vc-backend file) 'CVS)
653 (or (and (eq (vc-checkout-model file) 'manual)
654 (vc-lock-from-permissions file))
655 (and (equal (vc-file-getprop file 'vc-checkout-time)
656 (nth 5 (file-attributes file)))
657 (vc-file-setprop file 'vc-locking-user 'none))
658 (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
660 ((eq (vc-backend file) 'RCS)
663 ;; Check for RCS headers first
664 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
666 ;; If there are no headers, try to learn it
667 ;; from the permissions.
668 (and (setq p-lock (vc-lock-from-permissions file))
669 (if (eq p-lock 'none)
671 ;; If the permissions say "not locked", we know
672 ;; that the checkout model must be `manual'.
673 (vc-file-setprop file 'vc-checkout-model 'manual)
675 ;; If the permissions say "locked", we can only trust
676 ;; this *if* the checkout model is `manual'.
677 (eq (vc-checkout-model file) 'manual)))
679 ;; Otherwise, use lock information from the master file.
680 (vc-file-setprop file 'vc-locking-user
681 (vc-master-locking-user file)))
683 ;; Finally, if the file is not explicitly locked
684 ;; it might still be locked implicitly.
685 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
686 (eq (vc-checkout-model file) 'implicit)
687 (vc-rcs-lock-from-diff file))))
689 ((eq (vc-backend file) 'SCCS)
690 (or (vc-lock-from-permissions file)
691 (vc-file-setprop file 'vc-locking-user
692 (vc-master-locking-user file)))))
694 ;; convert a possible 'none value
695 (setq locking-user (vc-file-getprop file 'vc-locking-user))
696 (if (eq locking-user 'none) nil locking-user))))
698 ;;; properties to store current and recent version numbers
700 (defun vc-latest-version (file)
701 ;; Return version level of the latest version of FILE
702 (cond ((vc-file-getprop file 'vc-latest-version))
703 (t (vc-fetch-properties file)
704 (vc-file-getprop file 'vc-latest-version))))
706 (defun vc-your-latest-version (file)
707 ;; Return version level of the latest version of FILE checked in by you
708 (cond ((vc-file-getprop file 'vc-your-latest-version))
709 (t (vc-fetch-properties file)
710 (vc-file-getprop file 'vc-your-latest-version))))
712 (defun vc-master-workfile-version (file)
713 ;; Return the master file's idea of what is the current workfile version.
714 ;; This property is defined for RCS only.
715 (cond ((vc-file-getprop file 'vc-master-workfile-version))
716 (t (vc-fetch-master-properties file)
717 (vc-file-getprop file 'vc-master-workfile-version))))
719 (defun vc-fetch-properties (file)
720 ;; Fetch vc-latest-version and vc-your-latest-version
721 ;; if that wasn't already done.
723 ((eq (vc-backend file) 'RCS)
725 (set-buffer (get-buffer-create "*vc-info*"))
726 (vc-insert-file (vc-name file) "^desc")
728 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
729 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
730 "date[ \t]+\\([0-9.]+\\);[ \t]+"
732 (regexp-quote (vc-user-login-name)) ";") 1 2))
734 '(vc-latest-version vc-your-latest-version))
735 (if (get-buffer "*vc-info*")
736 (kill-buffer (get-buffer "*vc-info*")))))
737 (t (vc-fetch-master-properties file))
740 (defun vc-workfile-version (file)
741 ;; Return version level of the current workfile FILE
742 ;; This is attempted by first looking at the RCS keywords.
743 ;; If there are no keywords in the working file,
744 ;; vc-master-workfile-version is taken.
745 ;; Note that this property is cached, that is, it is only
746 ;; looked up if it is nil.
747 ;; For SCCS, this property is equivalent to vc-latest-version.
748 (cond ((vc-file-getprop file 'vc-workfile-version))
749 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
750 ((eq (vc-backend file) 'RCS)
751 (if (vc-consult-rcs-headers file)
752 (vc-file-getprop file 'vc-workfile-version)
753 (let ((rev (cond ((vc-master-workfile-version file))
754 ((vc-latest-version file)))))
755 (vc-file-setprop file 'vc-workfile-version rev)
757 ((eq (vc-backend file) 'CVS)
758 (if (vc-consult-rcs-headers file) ;; CVS
759 (vc-file-getprop file 'vc-workfile-version)
761 (vc-find-cvs-master (file-name-directory file)
762 (file-name-nondirectory file)))
763 (vc-file-getprop file 'vc-workfile-version)))))
765 ;;; actual version-control code starts here
767 (defun vc-registered (file)
768 (let (handler handlers)
769 (if (boundp 'file-name-handler-alist)
770 (setq handler (find-file-name-handler file 'vc-registered)))
772 (funcall handler 'vc-registered file)
773 ;; Search for a master corresponding to the given file
774 (let ((dirname (or (file-name-directory file) ""))
775 (basename (file-name-nondirectory file)))
778 (function (lambda (s)
780 (funcall s dirname basename)
781 (let ((trial (format (car s) dirname basename)))
782 (if (and (file-exists-p trial)
783 ;; Make sure the file we found with name
784 ;; TRIAL is not the source file itself.
785 ;; That can happen with RCS-style names
786 ;; if the file name is truncated
787 ;; (e.g. to 14 chars). See if either
788 ;; directory or attributes differ.
789 (or (not (string= dirname
790 (file-name-directory trial)))
792 (file-attributes file)
793 (file-attributes trial)))))
794 (throw 'found (cons trial (cdr s))))))))
798 (defun vc-find-cvs-master (dirname basename)
799 ;; Check if DIRNAME/BASENAME is handled by CVS.
800 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
801 ;; Note: This function throws the name of CVS/Entries
802 ;; NOT that of the RCS master file (because we wouldn't be able
803 ;; to access it under remote CVS).
804 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
805 (if (and vc-handle-cvs
806 (file-directory-p (concat dirname "CVS/"))
807 (file-readable-p (concat dirname "CVS/Entries")))
808 (let ((file (concat dirname basename))
809 ;; make sure that the file name is searched
811 (case-fold-search nil)
815 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
816 (vc-insert-file (concat dirname "CVS/Entries"))
817 (goto-char (point-min))
819 ;; entry for a "locally added" file (not yet committed)
821 (concat "^/" (regexp-quote basename) "/0/") nil t)
822 (vc-file-setprop file 'vc-checkout-time 0)
823 (vc-file-setprop file 'vc-workfile-version "0")
824 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
827 (concat "^/" (regexp-quote basename)
831 "/[A-Z][a-z][a-z]" ;; week day (irrelevant)
832 " \\([A-Z][a-z][a-z]\\)" ;; month name
833 " *\\([0-9]*\\)" ;; day of month
834 " \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\)" ;; hms
835 " \\([0-9]*\\)" ;; year
836 ;; optional conflict field
839 ;; We found it. Store away version number now that we
840 ;; are anyhow so close to finding it.
841 (vc-file-setprop file
844 ;; If the file hasn't been modified since checkout,
845 ;; store the checkout-time.
846 (let ((mtime (nth 5 (file-attributes file)))
847 (second (string-to-number (match-string 6)))
848 (minute (string-to-number (match-string 5)))
849 (hour (string-to-number (match-string 4)))
850 (day (string-to-number (match-string 3)))
851 (year (string-to-number (match-string 7))))
854 second minute hour day
857 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
860 (vc-file-setprop file 'vc-checkout-time mtime)
861 (vc-file-setprop file 'vc-checkout-time 0)))
862 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
863 ;; entry with arbitrary text as timestamp
864 ;; (this means we should consider it modified)
866 (concat "^/" (regexp-quote basename)
869 ;; timestamp (arbitrary text)
871 ;; optional conflict field
874 ;; We found it. Store away version number now that we
875 ;; are anyhow so close to finding it.
876 (vc-file-setprop file 'vc-workfile-version (match-string 1))
877 (vc-file-setprop file 'vc-checkout-time 0)
878 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
880 (kill-buffer buffer)))))
882 (defun vc-buffer-backend ()
883 "Return the version-control type of the visited file, or nil if none."
884 (if (eq vc-buffer-backend t)
885 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
888 (defun vc-toggle-read-only (&optional verbose)
889 "Change read-only status of current buffer, perhaps via version control.
890 If the buffer is visiting a file registered with version control,
891 then check the file in or out. Otherwise, just change the read-only flag
892 of the buffer. With prefix argument, ask for version number."
894 (if (or (and (boundp 'vc-dired-mode) vc-dired-mode)
895 ;; use boundp because vc.el might not be loaded
896 (vc-backend (buffer-file-name)))
897 (vc-next-action verbose)
899 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
901 (defun vc-after-save ()
902 ;; Function to be called by basic-save-buffer (in files.el).
903 ;; If the file in the current buffer is under version control,
904 ;; not locked, and the checkout model for it is `implicit',
905 ;; mark it "locked" and redisplay the mode line.
906 (let ((file (buffer-file-name)))
907 (and (vc-file-getprop file 'vc-backend)
908 ;; ...check the property directly, not through the function of the
909 ;; same name. Otherwise Emacs would check for a master file
910 ;; each time a non-version-controlled buffer is saved.
911 ;; The property is computed when the file is visited, so if it
912 ;; is `nil' now, it is certain that the file is NOT
913 ;; version-controlled.
914 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
915 (nth 5 (file-attributes file)))
916 ;; File has been saved in the same second in which
917 ;; it was checked out. Clear the checkout-time
918 ;; to avoid confusion.
919 (vc-file-setprop file 'vc-checkout-time nil))
921 (not (vc-locking-user file))
922 (eq (vc-checkout-model file) 'implicit)
923 (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
924 (or (and (eq (vc-backend file) 'CVS)
925 (vc-file-setprop file 'vc-cvs-status nil))
927 (vc-mode-line file))))
929 (defun vc-mode-line (file &optional label)
930 "Set `vc-mode' to display type of version control for FILE.
931 The value is set in the current buffer, which should be the buffer
932 visiting FILE. Second optional arg LABEL is put in place of version
933 control system name."
934 (interactive (list buffer-file-name nil))
935 (let ((vc-type (vc-backend file)))
938 (concat " " (or label (symbol-name vc-type))
939 (and vc-display-status (vc-status file)))))
940 ;; If the file is locked by some other user, make
941 ;; the buffer read-only. Like this, even root
942 ;; cannot modify a file that someone else has locked.
944 (equal file (buffer-file-name))
945 (vc-locking-user file)
946 (not (string= (vc-user-login-name) (vc-locking-user file)))
947 (setq buffer-read-only t))
948 ;; If the user is root, and the file is not owner-writable,
949 ;; then pretend that we can't write it
950 ;; even though we can (because root can write anything).
951 ;; This way, even root cannot modify a file that isn't locked.
953 (equal file (buffer-file-name))
954 (not buffer-read-only)
955 (zerop (user-real-uid))
956 (zerop (logand (file-modes (buffer-file-name)) 128))
957 (setq buffer-read-only t))
958 (force-mode-line-update)
959 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
962 (defun vc-status (file)
963 ;; Return string for placement in modeline by `vc-mode-line'.
966 ;; "-REV" if the revision is not locked
967 ;; ":REV" if the revision is locked by the user
968 ;; ":LOCKER:REV" if the revision is locked by somebody else
969 ;; " @@" for a CVS file that is added, but not yet committed
971 ;; In the CVS case, a "locked" working file is a
972 ;; working file that is modified with respect to the master.
973 ;; The file is "locked" from the moment when the user saves
974 ;; the modified buffer.
976 ;; This function assumes that the file is registered.
978 (let ((locker (vc-locking-user file))
979 (rev (vc-workfile-version file)))
980 (cond ((string= "0" rev)
984 ((string= locker (vc-user-login-name))
987 (concat ":" locker ":" rev)))))
989 (defun vc-follow-link ()
990 ;; If the current buffer visits a symbolic link, this function makes it
991 ;; visit the real file instead. If the real file is already visited in
992 ;; another buffer, make that buffer current, and kill the buffer
993 ;; that visits the link.
994 (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
995 (true-buffer (find-buffer-visiting truename))
996 (this-buffer (current-buffer)))
997 (if (eq true-buffer this-buffer)
999 (kill-buffer this-buffer)
1000 ;; In principle, we could do something like set-visited-file-name.
1001 ;; However, it can't be exactly the same as set-visited-file-name.
1002 ;; I'm not going to work out the details right now. -- rms.
1003 (set-buffer (find-file-noselect truename)))
1004 (set-buffer true-buffer)
1005 (kill-buffer this-buffer))))
1007 ;;; install a call to the above as a find-file hook
1008 (defun vc-find-file-hook ()
1009 ;; Recompute whether file is version controlled,
1010 ;; if user has killed the buffer and revisited.
1013 (vc-file-clearprops buffer-file-name)
1015 ((vc-backend buffer-file-name)
1016 (vc-mode-line buffer-file-name)
1017 (cond ((not vc-make-backup-files)
1018 ;; Use this variable, not make-backup-files,
1019 ;; because this is for things that depend on the file name.
1020 (make-local-variable 'backup-inhibited)
1021 (setq backup-inhibited t))))
1022 ((let* ((link (file-symlink-p buffer-file-name))
1023 (link-type (and link (vc-backend (file-chase-links link)))))
1025 (cond ((eq vc-follow-symlinks nil)
1027 "Warning: symbolic link to %s-controlled source file" link-type))
1028 ((or (not (eq vc-follow-symlinks 'ask))
1029 ;; If we already visited this file by following
1030 ;; the link, don't ask again if we try to visit
1031 ;; it again. GUD does that, and repeated questions
1034 (abbreviate-file-name (file-chase-links buffer-file-name))))
1037 (message "Followed link to %s" buffer-file-name)
1038 (vc-find-file-hook))
1040 (if (yes-or-no-p (format
1041 "Symbolic link to %s-controlled source file; follow link? " link-type))
1042 (progn (vc-follow-link)
1043 (message "Followed link to %s" buffer-file-name)
1044 (vc-find-file-hook))
1046 "Warning: editing through the link bypasses version control")
1049 (add-hook 'find-file-hooks 'vc-find-file-hook)
1051 ;;; more hooks, this time for file-not-found
1052 (defun vc-file-not-found-hook ()
1053 "When file is not found, try to check it out from RCS or SCCS.
1054 Returns t if checkout was successful, nil otherwise."
1055 (if (vc-backend buffer-file-name)
1058 (setq default-directory (file-name-directory (buffer-file-name)))
1059 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1061 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
1063 ;; Discard info about a file when we kill its buffer.
1064 (defun vc-kill-buffer-hook ()
1065 (if (stringp (buffer-file-name))
1067 (vc-file-clearprops (buffer-file-name))
1068 (kill-local-variable 'vc-buffer-backend))))
1070 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1072 ;;; Now arrange for bindings and autoloading of the main package.
1073 ;;; Bindings for this have to go in the global map, as we'll often
1074 ;;; want to call them from random buffers.
1076 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
1077 (if (not (keymapp vc-prefix-map))
1079 (setq vc-prefix-map (make-sparse-keymap))
1080 (define-key global-map "\C-xv" vc-prefix-map)
1081 (define-key vc-prefix-map "a" 'vc-update-change-log)
1082 (define-key vc-prefix-map "c" 'vc-cancel-version)
1083 (define-key vc-prefix-map "d" 'vc-directory)
1084 (define-key vc-prefix-map "g" 'vc-annotate)
1085 (define-key vc-prefix-map "h" 'vc-insert-headers)
1086 (define-key vc-prefix-map "i" 'vc-register)
1087 (define-key vc-prefix-map "l" 'vc-print-log)
1088 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
1089 (define-key vc-prefix-map "s" 'vc-create-snapshot)
1090 (define-key vc-prefix-map "u" 'vc-revert-buffer)
1091 (define-key vc-prefix-map "v" 'vc-next-action)
1092 (define-key vc-prefix-map "=" 'vc-diff)
1093 (define-key vc-prefix-map "~" 'vc-version-other-window)))
1095 (if (not (boundp 'vc-menu-map))
1096 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1099 ;;(define-key vc-menu-map [show-files]
1100 ;; '("Show Files under VC" . (vc-directory t)))
1101 (define-key vc-menu-map [vc-retrieve-snapshot]
1102 '("Retrieve Snapshot" . vc-retrieve-snapshot))
1103 (define-key vc-menu-map [vc-create-snapshot]
1104 '("Create Snapshot" . vc-create-snapshot))
1105 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
1106 (define-key vc-menu-map [separator1] '("----"))
1107 (define-key vc-menu-map [vc-annotate] '("Annotate" . vc-annotate))
1108 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
1109 (define-key vc-menu-map [vc-version-other-window]
1110 '("Show Other Version" . vc-version-other-window))
1111 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
1112 (define-key vc-menu-map [vc-update-change-log]
1113 '("Update ChangeLog" . vc-update-change-log))
1114 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
1115 (define-key vc-menu-map [separator2] '("----"))
1116 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
1117 (define-key vc-menu-map [vc-revert-buffer]
1118 '("Revert to Last Version" . vc-revert-buffer))
1119 (define-key vc-menu-map [vc-insert-header]
1120 '("Insert Header" . vc-insert-headers))
1121 (define-key vc-menu-map [vc-next-action] '("Check In/Out" . vc-next-action))
1122 (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
1124 (put 'vc-rename-file 'menu-enable 'vc-mode)
1125 (put 'vc-annotate 'menu-enable '(eq (vc-buffer-backend) 'CVS))
1126 (put 'vc-version-other-window 'menu-enable 'vc-mode)
1127 (put 'vc-diff 'menu-enable 'vc-mode)
1128 (put 'vc-update-change-log 'menu-enable
1129 '(eq (vc-buffer-backend) 'RCS))
1130 (put 'vc-print-log 'menu-enable 'vc-mode)
1131 (put 'vc-cancel-version 'menu-enable 'vc-mode)
1132 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
1133 (put 'vc-insert-headers 'menu-enable 'vc-mode)
1134 (put 'vc-next-action 'menu-enable 'vc-mode)
1135 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
1139 ;;; vc-hooks.el ends here