]> code.delx.au - gnu-emacs/blob - lisp/vc-hooks.el
(vc-find-cvs-master): Handle a "New file!" entry.
[gnu-emacs] / lisp / vc-hooks.el
1 ;;; vc-hooks.el --- resident support for version-control
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: Andre Spiegel <spiegel@inf.fu-berlin.de>
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
31
32 ;;; Code:
33
34 ;; Customization Variables (the rest is in vc.el)
35
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.")
39
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.")
43
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.")
48
49 (defvar vc-path
50 (if (file-directory-p "/usr/sccs")
51 '("/usr/sccs")
52 nil)
53 "*List of extra directories to search for version control commands.")
54
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)
58 vc-find-cvs-master)
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.")
62
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.")
66
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.")
76
77 (defvar vc-display-status t
78 "*If non-nil, display revision number and lock status in modeline.
79 Otherwise, not displayed.")
80
81 (defvar vc-consult-headers t
82 "*If non-nil, identify work files by searching for version headers.")
83
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
87 value of this flag.")
88
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'.")
93
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)))))
100
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)
104 minor-mode-alist)))
105
106 (make-variable-buffer-local 'vc-mode)
107 (put 'vc-mode 'permanent-local t)
108
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.
114
115 (defmacro vc-error-occurred (&rest body)
116 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
117
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.")
120
121 (defvar vc-buffer-backend t)
122 (make-variable-buffer-local 'vc-buffer-backend)
123
124 (defun vc-file-setprop (file property value)
125 ;; set per-file property
126 (put (intern file vc-file-prop-obarray) property value))
127
128 (defun vc-file-getprop (file property)
129 ;; get per-file property
130 (get (intern file vc-file-prop-obarray) property))
131
132 (defun vc-file-clearprops (file)
133 ;; clear all properties of a given file
134 (setplist (intern file vc-file-prop-obarray) nil))
135
136 ;;; Functions that determine property values, by examining the
137 ;;; working file, the master file, or log program output
138
139 (defun vc-match-substring (bn)
140 (buffer-substring (match-beginning bn) (match-end bn)))
141
142 (defun vc-lock-file (file)
143 ;; Generate lock file name corresponding to FILE
144 (let ((master (vc-name file)))
145 (and
146 master
147 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
148 (concat
149 (substring master (match-beginning 1) (match-end 1))
150 "p."
151 (substring master (match-beginning 2) (match-end 2))))))
152
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))
166 (cond
167 ((eq (length p) 2) ;; search for first entry
168 (let ((value nil))
169 (if (re-search-forward (car p) nil t)
170 (setq value (vc-match-substring (elt p 1))))
171 (if file
172 (progn (vc-file-setprop file (car properties) value)
173 (setq properties (cdr properties))))
174 value))
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)
180 (progn
181 (setq latest-date date)
182 (setq latest-val
183 (vc-match-substring (elt p 1)))))))
184 (if file
185 (progn (vc-file-setprop file (car properties) latest-val)
186 (setq properties (cdr properties))))
187 latest-val)))))
188 patterns)
189 )
190
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
197 ;; doesn't exist.
198 (erase-buffer)
199 (cond ((file-exists-p file)
200 (cond (limit
201 (if (not blocksize) (setq blocksize 8192))
202 (let (found s)
203 (while (not found)
204 (setq s (buffer-size))
205 (goto-char (1+ s))
206 (setq found
207 (or (zerop (car (cdr
208 (insert-file-contents file nil s
209 (+ s blocksize)))))
210 (progn (beginning-of-line)
211 (re-search-forward limit nil t)))))))
212 (t (insert-file-contents file)))
213 (set-buffer-modified-p nil)
214 (auto-save-mode nil)
215 t)
216 (t nil)))
217
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'.
222 (if (not 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?"
227 locks index)
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.]+\\)"
237 locks index)
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)))))
249
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*"))
255 (erase-buffer))
256 (let ((exec-path (append vc-path exec-path)) exec-status
257 ;; Add vc-path to PATH for the execution of this command.
258 (process-environment
259 (cons (concat "PATH=" (getenv "PATH")
260 path-separator
261 (mapconcat 'identity vc-path path-separator))
262 process-environment)))
263 (setq exec-status
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")))
271 exec-status))
272
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.
279 (save-excursion
280 (cond
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")
287 (vc-parse-buffer
288 (list '("^\001d D \\([^ ]+\\)" 1)
289 (list (concat "^\001d D \\([^ ]+\\) .* "
290 (regexp-quote (vc-user-login-name)) " ") 1))
291 file
292 '(vc-latest-version vc-your-latest-version)))
293
294 ((eq (vc-backend file) 'RCS)
295 (set-buffer (get-buffer-create "*vc-info*"))
296 (vc-insert-file (vc-name file) "^[0-9]")
297 (vc-parse-buffer
298 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
299 '("^branch[ \t\n]+\\([^;]+\\);" 1)
300 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
301 file
302 '(vc-head-version
303 vc-default-branch
304 vc-master-locks))
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)))
309 (cond
310 ;; no 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]+\\)*$"
316 default-branch)
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
321 (concat "^\\("
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)))
327
328 ((eq (vc-backend file) 'CVS)
329 (save-excursion
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*"))
336 (vc-parse-buffer
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))
341 file
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)))
346 (cond
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
352 (cond
353 ((string-match "Locally Modified" status) 'locally-modified)
354 ((string-match "Needs Merge" status) 'needs-merge)
355 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
356 'needs-checkout)
357 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
358 ((string-match "Locally Added" status) 'locally-added)
359 ((string-match "New file!" status) 'locally-added)
360 (t 'unknown)
361 ))))))))
362 (if (get-buffer "*vc-info*")
363 (kill-buffer (get-buffer "*vc-info*")))))
364
365 ;;; Functions that determine property values, by examining the
366 ;;; working file, the master file, or log program output
367
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
375 ;; visiting FILE)
376 ;; 'rev if a workfile revision was found
377 ;; 'rev-and-lock if revision and lock info was found
378 (cond
379 ((or (not vc-consult-headers)
380 (not (get-file-buffer file))) nil)
381 ((let (status version locking-user)
382 (save-excursion
383 (set-buffer (get-file-buffer file))
384 (goto-char (point-min))
385 (cond
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)
396 (match-end 1)))
397 ;; ... and check for the locking state
398 (cond
399 ((looking-at
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
404 (cond
405 ;; unlocked revision
406 ((looking-at "\\$")
407 (setq locking-user 'none)
408 (setq status 'rev-and-lock))
409 ;; revision is locked by some user
410 ((looking-at "\\([^ ]+\\) \\$")
411 (setq locking-user
412 (buffer-substring-no-properties (match-beginning 1)
413 (match-end 1)))
414 (setq status 'rev-and-lock))
415 ;; everything else: false
416 (nil)))
417 ;; unexpected information in
418 ;; keyword string --> quit
419 (nil)))
420 ;; search for $Revision
421 ;; --------------------
422 ((re-search-forward (concat "\\$"
423 "Revision: \\([0-9.]+\\) \\$")
424 nil t)
425 ;; if found, store the revision number ...
426 (setq version (buffer-substring-no-properties (match-beginning 1)
427 (match-end 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
433 (match-beginning 1)
434 (match-end 1)))
435 (setq status 'rev-and-lock))
436 ((looking-at " *\\$")
437 (setq locking-user 'none)
438 (setq status 'rev-and-lock))
439 (t
440 (setq locking-user 'none)
441 (setq status 'rev-and-lock)))
442 (setq status 'rev)))
443 ;; else: nothing found
444 ;; -------------------
445 (t nil)))
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)))
461 status))))
462
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.)
469
470 ;;; properties indicating the backend
471 ;;; being used for FILE
472
473 (defun vc-backend-subdirectory-name (&optional file)
474 ;; Where the master and lock files for the current directory are kept
475 (symbol-name
476 (or
477 (and file (vc-backend file))
478 vc-default-back-end
479 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
480
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)))
486 (if name-and-type
487 (progn
488 (vc-file-setprop file 'vc-backend (cdr name-and-type))
489 (vc-file-setprop file 'vc-name (car name-and-type)))))))
490
491 (defun vc-backend (file)
492 "Return the version-control type of a file, nil if it is not registered."
493 (and file
494 (or (vc-file-getprop file 'vc-backend)
495 (let ((name-and-type (vc-registered file)))
496 (if name-and-type
497 (progn
498 (vc-file-setprop file 'vc-name (car name-and-type))
499 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
500
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.
504 (or
505 (vc-file-getprop file 'vc-checkout-model)
506 (cond
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))))))
517
518 ;;; properties indicating the locking state
519
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))))
526
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))))
534
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))
544 ('none)))))
545
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)))
567 (nil)))))
568
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))))
578
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))))
582
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))))
592 (if (eq status 2)
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)))
598 (if (zerop status)
599 (vc-file-setprop file 'vc-locking-user 'none)
600 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
601
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)))
612 (if locking-user
613 ;; if we already know the property, return it
614 (if (eq locking-user 'none) nil locking-user)
615
616 ;; otherwise, infer the property...
617 (cond
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))))
625
626 ((eq (vc-backend file) 'RCS)
627 (let (p-lock)
628
629 ;; Check for RCS headers first
630 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
631
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)
636
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)
640
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)))
644
645 ;; Otherwise, use lock information from the master file.
646 (vc-file-setprop file 'vc-locking-user
647 (vc-master-locking-user file)))
648
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))))
654
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)))))
659
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))))
663
664 ;;; properties to store current and recent version numbers
665
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))))
671
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))))
677
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))))
684
685 (defun vc-fetch-properties (file)
686 ;; Fetch vc-latest-version and vc-your-latest-version
687 ;; if that wasn't already done.
688 (cond
689 ((eq (vc-backend file) 'RCS)
690 (save-excursion
691 (set-buffer (get-buffer-create "*vc-info*"))
692 (vc-insert-file (vc-name file) "^desc")
693 (vc-parse-buffer
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]+"
697 "author[ \t]+"
698 (regexp-quote (vc-user-login-name)) ";") 1 2))
699 file
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))
704 ))
705
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)
722 rev)))
723 ((eq (vc-backend file) 'CVS)
724 (if (vc-consult-rcs-headers file) ;; CVS
725 (vc-file-getprop file 'vc-workfile-version)
726 (catch 'found
727 (vc-find-cvs-master (file-name-directory file)
728 (file-name-nondirectory file)))
729 (vc-file-getprop file 'vc-workfile-version)))))
730
731 ;;; actual version-control code starts here
732
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)))
737 (if handler
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)))
742 (catch 'found
743 (mapcar
744 (function (lambda (s)
745 (if (atom 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)))
757 (not (equal
758 (file-attributes file)
759 (file-attributes trial)))))
760 (throw 'found (cons trial (cdr s))))))))
761 vc-master-templates)
762 nil)))))
763
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)))
776 (unwind-protect
777 (save-excursion
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
782 ;; case-sensitively
783 (setq case-fold-search nil)
784 (cond
785 ;; normal entry
786 ((re-search-forward
787 (concat "^/" (regexp-quote basename)
788 "/\\([^/]*\\)/[^ /]* \\([A-Z][a-z][a-z]\\) *\\([0-9]*\\) \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\) \\([0-9]*\\)")
789 nil t)
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
794 'vc-workfile-version
795 (match-string 1))
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))))
804 (if (equal mtime
805 (encode-time
806 second minute hour day
807 (/ (string-match
808 (match-string 2)
809 "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
810 3)
811 year 0))
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)
816 ((re-search-forward
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)))
822 ((re-search-forward
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
830 nil)))
831 (kill-buffer buffer)))))
832
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)))
837 vc-buffer-backend))
838
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."
844 (interactive "P")
845 (if (vc-backend (buffer-file-name))
846 (vc-next-action verbose)
847 (toggle-read-only)))
848 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
849
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))
869 t)
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))
875 t)
876 (vc-mode-line file))))
877
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)))
885 (setq vc-mode
886 (and vc-type
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.
892 (and vc-type
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.
901 (and vc-type
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
909 vc-type))
910
911 (defun vc-status (file)
912 ;; Return string for placement in modeline by `vc-mode-line'.
913 ;; Format:
914 ;;
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
919 ;;
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.
924 ;;
925 ;; This function assumes that the file is registered.
926
927 (let ((locker (vc-locking-user file))
928 (rev (vc-workfile-version file)))
929 (cond ((string= "0" rev)
930 " @@")
931 ((not locker)
932 (concat "-" rev))
933 ((string= locker (vc-user-login-name))
934 (concat ":" rev))
935 (t
936 (concat ":" locker ":" rev)))))
937
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)
947 (progn
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))))
955
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.
960 (cond
961 (buffer-file-name
962 (vc-file-clearprops buffer-file-name)
963 (cond
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)))))
973 (if link-type
974 (cond ((eq vc-follow-symlinks nil)
975 (message
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
981 ;; are painful.
982 (get-file-buffer
983 (abbreviate-file-name (file-chase-links buffer-file-name))))
984
985 (vc-follow-link)
986 (message "Followed link to %s" buffer-file-name)
987 (vc-find-file-hook))
988 (t
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)
993 (vc-find-file-hook))
994 (message
995 "Warning: editing through the link bypasses version control")
996 ))))))))))
997
998 (add-hook 'find-file-hooks 'vc-find-file-hook)
999
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)
1005 (save-excursion
1006 (require 'vc)
1007 (setq default-directory (file-name-directory (buffer-file-name)))
1008 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
1009
1010 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
1011
1012 ;; Discard info about a file when we kill its buffer.
1013 (defun vc-kill-buffer-hook ()
1014 (if (stringp (buffer-file-name))
1015 (progn
1016 (vc-file-clearprops (buffer-file-name))
1017 (kill-local-variable 'vc-buffer-backend))))
1018
1019 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
1020
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.
1024
1025 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
1026 (if (not (keymapp vc-prefix-map))
1027 (progn
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)))
1042
1043 (if (not (boundp 'vc-menu-map))
1044 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
1045 ;; vc-menu-map.
1046 ()
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)))
1067
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)))
1080
1081 (provide 'vc-hooks)
1082
1083 ;;; vc-hooks.el ends here