(using-unix-filesystems):
[emacs.git] / lisp / vc-hooks.el
blobf20b1e15820ff85eb3bd71b286e5fdfc3e27d3ad
1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Modified by:
7 ;; Per Cederqvist <ceder@lysator.liu.se>
8 ;; Andre Spiegel <spiegel@berlin.informatik.uni-stuttgart.de>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;; This is the always-loaded portion of VC.
29 ;; It takes care VC-related activities that are done when you visit a file,
30 ;; so that vc.el itself is loaded only when you use a VC command.
31 ;; See the commentary of vc.el.
33 ;;; Code:
35 ;; Customization Variables (the rest is in vc.el)
37 (defvar vc-default-back-end nil
38 "*Back-end actually used by this interface; may be SCCS or RCS.
39 The value is only computed when needed to avoid an expensive search.")
41 (defvar vc-handle-cvs t
42 "*If non-nil, use VC for files managed with CVS.
43 If it is nil, don't use VC for those files.")
45 (defvar vc-rcsdiff-knows-brief nil
46 "*Indicates whether rcsdiff understands the --brief option.
47 The value is either `yes', `no', or nil. If it is nil, VC tries
48 to use --brief and sets this variable to remember whether it worked.")
50 (defvar vc-path
51 (if (file-directory-p "/usr/sccs")
52 '("/usr/sccs")
53 nil)
54 "*List of extra directories to search for version control commands.")
56 (defvar vc-master-templates
57 '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
58 ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
59 vc-find-cvs-master)
60 "*Where to look for version-control master files.
61 The first pair corresponding to a given back end is used as a template
62 when creating new masters.")
64 (defvar vc-make-backup-files nil
65 "*If non-nil, backups of registered files are made as with other files.
66 If nil (the default), files covered by version control don't get backups.")
68 (defvar vc-display-status t
69 "*If non-nil, display revision number and lock status in modeline.
70 Otherwise, not displayed.")
72 (defvar vc-consult-headers t
73 "*If non-nil, identify work files by searching for version headers.")
75 (defvar vc-keep-workfiles t
76 "*If non-nil, don't delete working files after registering changes.
77 If the back-end is CVS, workfiles are always kept, regardless of the
78 value of this flag.")
80 (defvar vc-mistrust-permissions nil
81 "*If non-nil, don't assume that permissions and ownership track
82 version-control status. If nil, do rely on the permissions.
83 See also variable `vc-consult-headers'.")
85 (defun vc-mistrust-permissions (file)
86 ;; Access function to the above.
87 (or (eq vc-mistrust-permissions 't)
88 (and vc-mistrust-permissions
89 (funcall vc-mistrust-permissions
90 (vc-backend-subdirectory-name file)))))
92 ;; Tell Emacs about this new kind of minor mode
93 (if (not (assoc 'vc-mode minor-mode-alist))
94 (setq minor-mode-alist (cons '(vc-mode vc-mode)
95 minor-mode-alist)))
97 (make-variable-buffer-local 'vc-mode)
98 (put 'vc-mode 'permanent-local t)
100 ;; We need a notion of per-file properties because the version
101 ;; control state of a file is expensive to derive --- we compute
102 ;; them when the file is initially found, keep them up to date
103 ;; during any subsequent VC operations, and forget them when
104 ;; the buffer is killed.
106 (defmacro vc-error-occurred (&rest body)
107 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
109 (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
110 "Obarray for per-file properties.")
112 (defvar vc-buffer-backend t)
113 (make-variable-buffer-local 'vc-buffer-backend)
115 (defun vc-file-setprop (file property value)
116 ;; set per-file property
117 (put (intern file vc-file-prop-obarray) property value))
119 (defun vc-file-getprop (file property)
120 ;; get per-file property
121 (get (intern file vc-file-prop-obarray) property))
123 (defun vc-file-clearprops (file)
124 ;; clear all properties of a given file
125 (setplist (intern file vc-file-prop-obarray) nil))
127 ;;; Functions that determine property values, by examining the
128 ;;; working file, the master file, or log program output
130 (defun vc-match-substring (bn)
131 (buffer-substring (match-beginning bn) (match-end bn)))
133 (defun vc-lock-file (file)
134 ;; Generate lock file name corresponding to FILE
135 (let ((master (vc-name file)))
136 (and
137 master
138 (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
139 (concat
140 (substring master (match-beginning 1) (match-end 1))
141 "p."
142 (substring master (match-beginning 2) (match-end 2))))))
144 (defun vc-parse-buffer (patterns &optional file properties)
145 ;; Use PATTERNS to parse information out of the current buffer.
146 ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
147 ;; is the pattern to be matched, and the second (an integer) is the
148 ;; number of the subexpression that should be returned. If there's
149 ;; a third element (also the number of a subexpression), that
150 ;; subexpression is assumed to be a date field and we want the most
151 ;; recent entry matching the template.
152 ;; If FILE and PROPERTIES are given, the latter must be a list of
153 ;; properties of the same length as PATTERNS; each property is assigned
154 ;; the corresponding value.
155 (mapcar (function (lambda (p)
156 (goto-char (point-min))
157 (cond
158 ((eq (length p) 2) ;; search for first entry
159 (let ((value nil))
160 (if (re-search-forward (car p) nil t)
161 (setq value (vc-match-substring (elt p 1))))
162 (if file
163 (progn (vc-file-setprop file (car properties) value)
164 (setq properties (cdr properties))))
165 value))
166 ((eq (length p) 3) ;; search for latest entry
167 (let ((latest-date "") (latest-val))
168 (while (re-search-forward (car p) nil t)
169 (let ((date (vc-match-substring (elt p 2))))
170 (if (string< latest-date date)
171 (progn
172 (setq latest-date date)
173 (setq latest-val
174 (vc-match-substring (elt p 1)))))))
175 (if file
176 (progn (vc-file-setprop file (car properties) latest-val)
177 (setq properties (cdr properties))))
178 latest-val)))))
179 patterns)
182 (defun vc-insert-file (file &optional limit blocksize)
183 ;; Insert the contents of FILE into the current buffer.
184 ;; Optional argument LIMIT is a regexp. If present,
185 ;; the file is inserted in chunks of size BLOCKSIZE
186 ;; (default 8 kByte), until the first occurrence of
187 ;; LIMIT is found. The function returns nil if FILE
188 ;; doesn't exist.
189 (erase-buffer)
190 (cond ((file-exists-p file)
191 (cond (limit
192 (if (not blocksize) (setq blocksize 8192))
193 (let (found s)
194 (while (not found)
195 (setq s (buffer-size))
196 (goto-char (1+ s))
197 (setq found
198 (or (zerop (car (cdr
199 (insert-file-contents file nil s
200 (+ s blocksize)))))
201 (progn (beginning-of-line)
202 (re-search-forward limit nil t)))))))
203 (t (insert-file-contents file)))
204 (set-buffer-modified-p nil)
205 (auto-save-mode nil)
207 (t nil)))
209 (defun vc-parse-locks (file locks)
210 ;; Parse RCS or SCCS locks.
211 ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
212 ;; which is returned and stored into the property `vc-master-locks'.
213 (if (not locks)
214 (vc-file-setprop file 'vc-master-locks 'none)
215 (let ((found t) (index 0) master-locks version user)
216 (cond ((eq (vc-backend file) 'SCCS)
217 (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
218 locks index)
219 (setq version (substring locks
220 (match-beginning 1) (match-end 1)))
221 (setq user (substring locks
222 (match-beginning 2) (match-end 2)))
223 (setq master-locks (append master-locks
224 (list (cons version user))))
225 (setq index (match-end 0))))
226 ((eq (vc-backend file) 'RCS)
227 (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
228 locks index)
229 (setq version (substring locks
230 (match-beginning 2) (match-end 2)))
231 (setq user (substring locks
232 (match-beginning 1) (match-end 1)))
233 (setq master-locks (append master-locks
234 (list (cons version user))))
235 (setq index (match-end 0)))
236 (if (string-match ";[ \t\n]+strict;" locks index)
237 (vc-file-setprop file 'vc-checkout-model 'manual)
238 (vc-file-setprop file 'vc-checkout-model 'implicit))))
239 (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
241 (defun vc-simple-command (okstatus command file &rest args)
242 ;; Simple version of vc-do-command, for use in vc-hooks only.
243 ;; Don't switch to the *vc-info* buffer before running the
244 ;; command, because that would change its default directory
245 (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
246 (erase-buffer))
247 (let ((exec-path (append vc-path exec-path)) exec-status
248 ;; Add vc-path to PATH for the execution of this command.
249 (process-environment
250 (cons (concat "PATH=" (getenv "PATH")
251 path-separator
252 (mapconcat 'identity vc-path path-separator))
253 process-environment)))
254 (setq exec-status
255 (apply 'call-process command nil "*vc-info*" nil
256 (append args (list file))))
257 (cond ((> exec-status okstatus)
258 (switch-to-buffer (get-file-buffer file))
259 (shrink-window-if-larger-than-buffer
260 (display-buffer "*vc-info*"))
261 (error "Couldn't find version control information")))
262 exec-status))
264 (defun vc-fetch-master-properties (file)
265 ;; Fetch those properties of FILE that are stored in the master file.
266 ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
267 ;; here because that is slow.
268 ;; That gets done if/when the functions vc-latest-version
269 ;; and vc-your-latest-version get called.
270 (save-excursion
271 (cond
272 ((eq (vc-backend file) 'SCCS)
273 (set-buffer (get-buffer-create "*vc-info*"))
274 (if (vc-insert-file (vc-lock-file file))
275 (vc-parse-locks file (buffer-string))
276 (vc-file-setprop file 'vc-master-locks 'none))
277 (vc-insert-file (vc-name file) "^\001e")
278 (vc-parse-buffer
279 (list '("^\001d D \\([^ ]+\\)" 1)
280 (list (concat "^\001d D \\([^ ]+\\) .* "
281 (regexp-quote (user-login-name)) " ") 1))
282 file
283 '(vc-latest-version vc-your-latest-version)))
285 ((eq (vc-backend file) 'RCS)
286 (set-buffer (get-buffer-create "*vc-info*"))
287 (vc-insert-file (vc-name file) "^[0-9]")
288 (vc-parse-buffer
289 (list '("^head[ \t\n]+\\([^;]+\\);" 1)
290 '("^branch[ \t\n]+\\([^;]+\\);" 1)
291 '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
292 file
293 '(vc-head-version
294 vc-default-branch
295 vc-master-locks))
296 ;; determine vc-master-workfile-version: it is either the head
297 ;; of the trunk, the head of the default branch, or the
298 ;; "default branch" itself, if that is a full revision number.
299 (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
300 (cond
301 ;; no default branch
302 ((or (not default-branch) (string= "" default-branch))
303 (vc-file-setprop file 'vc-master-workfile-version
304 (vc-file-getprop file 'vc-head-version)))
305 ;; default branch is actually a revision
306 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
307 default-branch)
308 (vc-file-setprop file 'vc-master-workfile-version default-branch))
309 ;; else, search for the head of the default branch
310 (t (vc-insert-file (vc-name file) "^desc")
311 (vc-parse-buffer (list (list
312 (concat "^\\("
313 (regexp-quote default-branch)
314 "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
315 file '(vc-master-workfile-version)))))
316 ;; translate the locks
317 (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
319 ((eq (vc-backend file) 'CVS)
320 (save-excursion
321 ;; Call "cvs status" in the right directory, passing only the
322 ;; nondirectory part of the file name -- otherwise CVS might
323 ;; silently give a wrong result.
324 (let ((default-directory (file-name-directory file)))
325 (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
326 (set-buffer (get-buffer "*vc-info*"))
327 (vc-parse-buffer
328 ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
329 ;; and CVS 1.4a1 says "Repository revision:".
330 '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
331 ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
332 file
333 '(vc-latest-version vc-cvs-status))
334 ;; Translate those status values that we understand into symbols.
335 ;; Any other value is converted to nil.
336 (let ((status (vc-file-getprop file 'vc-cvs-status)))
337 (cond
338 ((string-match "Up-to-date" status)
339 (vc-file-setprop file 'vc-cvs-status 'up-to-date)
340 (vc-file-setprop file 'vc-checkout-time
341 (nth 5 (file-attributes file))))
342 ((vc-file-setprop file 'vc-cvs-status
343 (cond
344 ((string-match "Locally Modified" status) 'locally-modified)
345 ((string-match "Needs Merge" status) 'needs-merge)
346 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
347 'needs-checkout)
348 ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
349 ((string-match "Locally Added" status) 'locally-added)
350 (t 'unknown)
351 ))))))))
352 (if (get-buffer "*vc-info*")
353 (kill-buffer (get-buffer "*vc-info*")))))
355 ;;; Functions that determine property values, by examining the
356 ;;; working file, the master file, or log program output
358 (defun vc-consult-rcs-headers (file)
359 ;; Search for RCS headers in FILE, and set properties
360 ;; accordingly. This function can be disabled by setting
361 ;; vc-consult-headers to nil.
362 ;; Returns: nil if no headers were found
363 ;; (or if the feature is disabled,
364 ;; or if there is currently no buffer
365 ;; visiting FILE)
366 ;; 'rev if a workfile revision was found
367 ;; 'rev-and-lock if revision and lock info was found
368 (cond
369 ((or (not vc-consult-headers)
370 (not (get-file-buffer file))) nil)
371 ((let (status version locking-user)
372 (save-excursion
373 (set-buffer (get-file-buffer file))
374 (goto-char (point-min))
375 (cond
376 ;; search for $Id or $Header
377 ;; -------------------------
378 ((or (and (search-forward "$Id: " nil t)
379 (looking-at "[^ ]+ \\([0-9.]+\\) "))
380 (and (progn (goto-char (point-min))
381 (search-forward "$Header: " nil t))
382 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
383 (goto-char (match-end 0))
384 ;; if found, store the revision number ...
385 (setq version (buffer-substring (match-beginning 1) (match-end 1)))
386 ;; ... and check for the locking state
387 (cond
388 ((looking-at
389 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
390 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
391 "[^ ]+ [^ ]+ ")) ; author & state
392 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
393 (cond
394 ;; unlocked revision
395 ((looking-at "\\$")
396 (setq locking-user 'none)
397 (setq status 'rev-and-lock))
398 ;; revision is locked by some user
399 ((looking-at "\\([^ ]+\\) \\$")
400 (setq locking-user
401 (buffer-substring (match-beginning 1) (match-end 1)))
402 (setq status 'rev-and-lock))
403 ;; everything else: false
404 (nil)))
405 ;; unexpected information in
406 ;; keyword string --> quit
407 (nil)))
408 ;; search for $Revision
409 ;; --------------------
410 ((re-search-forward (concat "\\$"
411 "Revision: \\([0-9.]+\\) \\$")
412 nil t)
413 ;; if found, store the revision number ...
414 (setq version (buffer-substring (match-beginning 1) (match-end 1)))
415 ;; and see if there's any lock information
416 (goto-char (point-min))
417 (if (re-search-forward (concat "\\$" "Locker:") nil t)
418 (cond ((looking-at " \\([^ ]+\\) \\$")
419 (setq locking-user (buffer-substring (match-beginning 1)
420 (match-end 1)))
421 (setq status 'rev-and-lock))
422 ((looking-at " *\\$")
423 (setq locking-user 'none)
424 (setq status 'rev-and-lock))
426 (setq locking-user 'none)
427 (setq status 'rev-and-lock)))
428 (setq status 'rev)))
429 ;; else: nothing found
430 ;; -------------------
431 (t nil)))
432 (if status (vc-file-setprop file 'vc-workfile-version version))
433 (and (eq status 'rev-and-lock)
434 (eq (vc-backend file) 'RCS)
435 (vc-file-setprop file 'vc-locking-user locking-user)
436 ;; If the file has headers, we don't want to query the master file,
437 ;; because that would eliminate all the performance gain the headers
438 ;; brought us. We therefore use a heuristic for the checkout model
439 ;; now: If we trust the file permissions, and the file is not
440 ;; locked, then if the file is read-only the checkout model is
441 ;; `manual', otherwise `implicit'.
442 (not (vc-mistrust-permissions file))
443 (not (vc-locking-user file))
444 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
445 (vc-file-setprop file 'vc-checkout-model 'manual)
446 (vc-file-setprop file 'vc-checkout-model 'implicit)))
447 status))))
449 ;;; Access functions to file properties
450 ;;; (Properties should be _set_ using vc-file-setprop, but
451 ;;; _retrieved_ only through these functions, which decide
452 ;;; if the property is already known or not. A property should
453 ;;; only be retrieved by vc-file-getprop if there is no
454 ;;; access function.)
456 ;;; properties indicating the backend
457 ;;; being used for FILE
459 (defun vc-backend-subdirectory-name (&optional file)
460 ;; Where the master and lock files for the current directory are kept
461 (symbol-name
463 (and file (vc-backend file))
464 vc-default-back-end
465 (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
467 (defun vc-name (file)
468 "Return the master name of a file, nil if it is not registered.
469 For CVS, the full name of CVS/Entries is returned."
470 (or (vc-file-getprop file 'vc-name)
471 (let ((name-and-type (vc-registered file)))
472 (if name-and-type
473 (progn
474 (vc-file-setprop file 'vc-backend (cdr name-and-type))
475 (vc-file-setprop file 'vc-name (car name-and-type)))))))
477 (defun vc-backend (file)
478 "Return the version-control type of a file, nil if it is not registered."
479 (and file
480 (or (vc-file-getprop file 'vc-backend)
481 (let ((name-and-type (vc-registered file)))
482 (if name-and-type
483 (progn
484 (vc-file-setprop file 'vc-name (car name-and-type))
485 (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
487 (defun vc-checkout-model (file)
488 ;; Return `manual' if the user has to type C-x C-q to check out FILE.
489 ;; Return `implicit' if the file can be modified without locking it first.
491 (vc-file-getprop file 'vc-checkout-model)
492 (cond
493 ((eq (vc-backend file) 'SCCS)
494 (vc-file-setprop file 'vc-checkout-model 'manual))
495 ((eq (vc-backend file) 'RCS)
496 (vc-consult-rcs-headers file)
497 (or (vc-file-getprop file 'vc-checkout-model)
498 (progn (vc-fetch-master-properties file)
499 (vc-file-getprop file 'vc-checkout-model))))
500 ((eq (vc-backend file) 'CVS)
501 (vc-file-setprop file 'vc-checkout-model
502 (if (getenv "CVSREAD") 'manual 'implicit))))))
504 ;;; properties indicating the locking state
506 (defun vc-cvs-status (file)
507 ;; Return the cvs status of FILE
508 ;; (Status field in output of "cvs status")
509 (cond ((vc-file-getprop file 'vc-cvs-status))
510 (t (vc-fetch-master-properties file)
511 (vc-file-getprop file 'vc-cvs-status))))
513 (defun vc-master-locks (file)
514 ;; Return the lock entries in the master of FILE.
515 ;; Return 'none if there are no such entries, and a list
516 ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
517 (cond ((vc-file-getprop file 'vc-master-locks))
518 (t (vc-fetch-master-properties file)
519 (vc-file-getprop file 'vc-master-locks))))
521 (defun vc-master-locking-user (file)
522 ;; Return the master file's idea of who is locking
523 ;; the current workfile version of FILE.
524 ;; Return 'none if it is not locked.
525 (let ((master-locks (vc-master-locks file)) lock)
526 (if (eq master-locks 'none) 'none
527 ;; search for a lock on the current workfile version
528 (setq lock (assoc (vc-workfile-version file) master-locks))
529 (cond (lock (cdr lock))
530 ('none)))))
532 (defun vc-lock-from-permissions (file)
533 ;; If the permissions can be trusted for this file, determine the
534 ;; locking state from them. Returns (user-login-name), `none', or nil.
535 ;; This implementation assumes that any file which is under version
536 ;; control and has -rw-r--r-- is locked by its owner. This is true
537 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
538 ;; We have to be careful not to exclude files with execute bits on;
539 ;; scripts can be under version control too. Also, we must ignore the
540 ;; group-read and other-read bits, since paranoid users turn them off.
541 ;; This hack wins because calls to the somewhat expensive
542 ;; `vc-fetch-master-properties' function only have to be made if
543 ;; (a) the file is locked by someone other than the current user,
544 ;; or (b) some untoward manipulation behind vc's back has changed
545 ;; the owner or the `group' or `other' write bits.
546 (let ((attributes (file-attributes file)))
547 (if (not (vc-mistrust-permissions file))
548 (cond ((string-match ".r-..-..-." (nth 8 attributes))
549 (vc-file-setprop file 'vc-locking-user 'none))
550 ((and (= (nth 2 attributes) (user-uid))
551 (string-match ".rw..-..-." (nth 8 attributes)))
552 (vc-file-setprop file 'vc-locking-user (user-login-name)))
553 (nil)))))
555 (defun vc-file-owner (file)
556 ;; The expression below should return the username of the owner
557 ;; of the file. It doesn't. It returns the username if it is
558 ;; you, or otherwise the UID of the owner of the file. The
559 ;; return value from this function is only used by
560 ;; vc-dired-reformat-line, and it does the proper thing if a UID
561 ;; is returned.
562 ;; The *proper* way to fix this would be to implement a built-in
563 ;; function in Emacs, say, (username UID), that returns the
564 ;; username of a given UID.
565 ;; The result of this hack is that vc-directory will print the
566 ;; name of the owner of the file for any files that are
567 ;; modified.
568 (let ((uid (nth 2 (file-attributes file))))
569 (if (= uid (user-uid)) (user-login-name) uid)))
571 (defun vc-rcs-lock-from-diff (file)
572 ;; Diff the file against the master version. If differences are found,
573 ;; mark the file locked. This is only used for RCS with non-strict
574 ;; locking. (If "rcsdiff" doesn't understand --brief, we do a double-take
575 ;; and remember the fact for the future.)
576 (let* ((version (concat "-r" (vc-workfile-version file)))
577 (status (if (eq vc-rcsdiff-knows-brief 'no)
578 (vc-simple-command 1 "rcsdiff" file version)
579 (vc-simple-command 2 "rcsdiff" file "--brief" version))))
580 (if (eq status 2)
581 (if (not vc-rcsdiff-knows-brief)
582 (setq vc-rcsdiff-knows-brief 'no
583 status (vc-simple-command 1 "rcsdiff" file version))
584 (error "rcsdiff failed."))
585 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
586 (if (zerop status)
587 (vc-file-setprop file 'vc-locking-user 'none)
588 (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
590 (defun vc-locking-user (file)
591 ;; Return the name of the person currently holding a lock on FILE.
592 ;; Return nil if there is no such person. (Sometimes, not the name
593 ;; of the locking user but his uid will be returned.)
594 ;; Under CVS, a file is considered locked if it has been modified since
595 ;; it was checked out.
596 ;; The property is cached. It is only looked up if it is currently nil.
597 ;; Note that, for a file that is not locked, the actual property value
598 ;; is `none', to distinguish it from an unknown locking state. That value
599 ;; is converted to nil by this function, and returned to the caller.
600 (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
601 (if locking-user
602 ;; if we already know the property, return it
603 (if (eq locking-user 'none) nil locking-user)
605 ;; otherwise, infer the property...
606 (cond
607 ((eq (vc-backend file) 'CVS)
608 (or (and (eq (vc-checkout-model file) 'manual)
609 (vc-lock-from-permissions file))
610 (and (equal (vc-file-getprop file 'vc-checkout-time)
611 (nth 5 (file-attributes file)))
612 (vc-file-setprop file 'vc-locking-user 'none))
613 (let ((locker (vc-file-owner file)))
614 (vc-file-setprop file 'vc-locking-user
615 (if (stringp locker) locker
616 (format "%d" locker))))))
618 ((eq (vc-backend file) 'RCS)
619 (let (p-lock)
621 ;; Check for RCS headers first
622 (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
624 ;; If there are no headers, try to learn it
625 ;; from the permissions.
626 (and (setq p-lock (vc-lock-from-permissions file))
627 (if (eq p-lock 'none)
629 ;; If the permissions say "not locked", we know
630 ;; that the checkout model must be `manual'.
631 (vc-file-setprop file 'vc-checkout-model 'manual)
633 ;; If the permissions say "locked", we can only trust
634 ;; this *if* the checkout model is `manual'.
635 (eq (vc-checkout-model file) 'manual)))
637 ;; Otherwise, use lock information from the master file.
638 (vc-file-setprop file 'vc-locking-user
639 (vc-master-locking-user file)))
641 ;; Finally, if the file is not explicitly locked
642 ;; it might still be locked implicitly.
643 (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
644 (eq (vc-checkout-model file) 'implicit)
645 (vc-rcs-lock-from-diff file))))
647 ((eq (vc-backend file) 'SCCS)
648 (or (vc-lock-from-permissions file)
649 (vc-file-setprop file 'vc-locking-user
650 (vc-master-locking-user file)))))
652 ;; convert a possible 'none value
653 (setq locking-user (vc-file-getprop file 'vc-locking-user))
654 (if (eq locking-user 'none) nil locking-user))))
656 ;;; properties to store current and recent version numbers
658 (defun vc-latest-version (file)
659 ;; Return version level of the latest version of FILE
660 (cond ((vc-file-getprop file 'vc-latest-version))
661 (t (vc-fetch-properties file)
662 (vc-file-getprop file 'vc-latest-version))))
664 (defun vc-your-latest-version (file)
665 ;; Return version level of the latest version of FILE checked in by you
666 (cond ((vc-file-getprop file 'vc-your-latest-version))
667 (t (vc-fetch-properties file)
668 (vc-file-getprop file 'vc-your-latest-version))))
670 (defun vc-master-workfile-version (file)
671 ;; Return the master file's idea of what is the current workfile version.
672 ;; This property is defined for RCS only.
673 (cond ((vc-file-getprop file 'vc-master-workfile-version))
674 (t (vc-fetch-master-properties file)
675 (vc-file-getprop file 'vc-master-workfile-version))))
677 (defun vc-fetch-properties (file)
678 ;; Fetch vc-latest-version and vc-your-latest-version
679 ;; if that wasn't already done.
680 (cond
681 ((eq (vc-backend file) 'RCS)
682 (save-excursion
683 (set-buffer (get-buffer-create "*vc-info*"))
684 (vc-insert-file (vc-name file) "^desc")
685 (vc-parse-buffer
686 (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
687 (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
688 "date[ \t]+\\([0-9.]+\\);[ \t]+"
689 "author[ \t]+"
690 (regexp-quote (user-login-name)) ";") 1 2))
691 file
692 '(vc-latest-version vc-your-latest-version))
693 (if (get-buffer "*vc-info*")
694 (kill-buffer (get-buffer "*vc-info*")))))
695 (t (vc-fetch-master-properties file))
698 (defun vc-workfile-version (file)
699 ;; Return version level of the current workfile FILE
700 ;; This is attempted by first looking at the RCS keywords.
701 ;; If there are no keywords in the working file,
702 ;; vc-master-workfile-version is taken.
703 ;; Note that this property is cached, that is, it is only
704 ;; looked up if it is nil.
705 ;; For SCCS, this property is equivalent to vc-latest-version.
706 (cond ((vc-file-getprop file 'vc-workfile-version))
707 ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
708 ((eq (vc-backend file) 'RCS)
709 (if (vc-consult-rcs-headers file)
710 (vc-file-getprop file 'vc-workfile-version)
711 (let ((rev (cond ((vc-master-workfile-version file))
712 ((vc-latest-version file)))))
713 (vc-file-setprop file 'vc-workfile-version rev)
714 rev)))
715 ((eq (vc-backend file) 'CVS)
716 (if (vc-consult-rcs-headers file) ;; CVS
717 (vc-file-getprop file 'vc-workfile-version)
718 (catch 'found
719 (vc-find-cvs-master (file-name-directory file)
720 (file-name-nondirectory file)))
721 (vc-file-getprop file 'vc-workfile-version)))))
723 ;;; actual version-control code starts here
725 (defun vc-registered (file)
726 (let (handler handlers)
727 (if (boundp 'file-name-handler-alist)
728 (setq handler (find-file-name-handler file 'vc-registered)))
729 (if handler
730 (funcall handler 'vc-registered file)
731 ;; Search for a master corresponding to the given file
732 (let ((dirname (or (file-name-directory file) ""))
733 (basename (file-name-nondirectory file)))
734 (catch 'found
735 (mapcar
736 (function (lambda (s)
737 (if (atom s)
738 (funcall s dirname basename)
739 (let ((trial (format (car s) dirname basename)))
740 (if (and (file-exists-p trial)
741 ;; Make sure the file we found with name
742 ;; TRIAL is not the source file itself.
743 ;; That can happen with RCS-style names
744 ;; if the file name is truncated
745 ;; (e.g. to 14 chars). See if either
746 ;; directory or attributes differ.
747 (or (not (string= dirname
748 (file-name-directory trial)))
749 (not (equal
750 (file-attributes file)
751 (file-attributes trial)))))
752 (throw 'found (cons trial (cdr s))))))))
753 vc-master-templates)
754 nil)))))
756 (defun vc-utc-string (timeval)
757 ;; Convert a time value into universal time, and return it as a
758 ;; human-readable string. This is for comparing CVS checkout times
759 ;; with file modification times.
760 (let (utc (high (car timeval)) (low (nth 1 timeval))
761 (offset (car (current-time-zone timeval))))
762 (setq low (- low offset))
763 (setq utc (if (> low 65535)
764 (list (1+ high) (- low 65536))
765 (if (< low 0)
766 (list (1- high) (+ 65536 low))
767 (list high low))))
768 (current-time-string utc)))
770 (defun vc-find-cvs-master (dirname basename)
771 ;; Check if DIRNAME/BASENAME is handled by CVS.
772 ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
773 ;; Note: This function throws the name of CVS/Entries
774 ;; NOT that of the RCS master file (because we wouldn't be able
775 ;; to access it under remote CVS).
776 ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
777 (if (and vc-handle-cvs
778 (file-directory-p (concat dirname "CVS/"))
779 (file-readable-p (concat dirname "CVS/Entries")))
780 (let (buffer time (fold case-fold-search)
781 (file (concat dirname basename)))
782 (unwind-protect
783 (save-excursion
784 (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
785 (vc-insert-file (concat dirname "CVS/Entries"))
786 (goto-char (point-min))
787 ;; make sure the file name is searched
788 ;; case-sensitively
789 (setq case-fold-search nil)
790 (cond
791 ((re-search-forward
792 (concat "^/" (regexp-quote basename)
793 "/\\([^/]*\\)/\\([^/]*\\)/")
794 nil t)
795 (setq case-fold-search fold) ;; restore the old value
796 ;; We found it. Store away version number now that we
797 ;; are anyhow so close to finding it.
798 (vc-file-setprop file
799 'vc-workfile-version
800 (match-string 1))
801 ;; If the file hasn't been modified since checkout,
802 ;; store the checkout-time.
803 (let ((mtime (nth 5 (file-attributes file))))
804 (if (string= (match-string 2) (vc-utc-string mtime))
805 (vc-file-setprop file 'vc-checkout-time mtime)
806 (vc-file-setprop file 'vc-checkout-time 0)))
807 (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
808 (t (setq case-fold-search fold) ;; restore the old value
809 nil)))
810 (kill-buffer buffer)))))
812 (defun vc-buffer-backend ()
813 "Return the version-control type of the visited file, or nil if none."
814 (if (eq vc-buffer-backend t)
815 (setq vc-buffer-backend (vc-backend (buffer-file-name)))
816 vc-buffer-backend))
818 (defun vc-toggle-read-only (&optional verbose)
819 "Change read-only status of current buffer, perhaps via version control.
820 If the buffer is visiting a file registered with version control,
821 then check the file in or out. Otherwise, just change the read-only flag
822 of the buffer. With prefix argument, ask for version number."
823 (interactive "P")
824 (if (vc-backend (buffer-file-name))
825 (vc-next-action verbose)
826 (toggle-read-only)))
827 (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
829 (defun vc-after-save ()
830 ;; Function to be called by basic-save-buffer (in files.el).
831 ;; If the file in the current buffer is under version control,
832 ;; not locked, and the checkout model for it is `implicit',
833 ;; mark it "locked" and redisplay the mode line.
834 (let ((file (buffer-file-name)))
835 (and (vc-file-getprop file 'vc-backend)
836 ;; ...check the property directly, not through the function of the
837 ;; same name. Otherwise Emacs would check for a master file
838 ;; each time a non-version-controlled buffer is saved.
839 ;; The property is computed when the file is visited, so if it
840 ;; is `nil' now, it is certain that the file is NOT
841 ;; version-controlled.
842 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
843 (nth 5 (file-attributes file)))
844 ;; File has been saved in the same second in which
845 ;; it was checked out. Clear the checkout-time
846 ;; to avoid confusion.
847 (vc-file-setprop file 'vc-checkout-time nil))
849 (not (vc-locking-user file))
850 (eq (vc-checkout-model file) 'implicit)
851 (vc-file-setprop file 'vc-locking-user (user-login-name))
852 (or (and (eq (vc-backend file) 'CVS)
853 (vc-file-setprop file 'vc-cvs-status nil))
855 (vc-mode-line file))))
857 (defun vc-mode-line (file &optional label)
858 "Set `vc-mode' to display type of version control for FILE.
859 The value is set in the current buffer, which should be the buffer
860 visiting FILE. Second optional arg LABEL is put in place of version
861 control system name."
862 (interactive (list buffer-file-name nil))
863 (let ((vc-type (vc-backend file)))
864 (setq vc-mode
865 (and vc-type
866 (concat " " (or label (symbol-name vc-type))
867 (and vc-display-status (vc-status file)))))
868 (and vc-type
869 (equal file (buffer-file-name))
870 (vc-locking-user file)
871 ;; If the file is locked by some other user, make
872 ;; the buffer read-only. Like this, even root
873 ;; cannot modify a file without locking it first.
874 (not (string= (user-login-name) (vc-locking-user file)))
875 (setq buffer-read-only t))
876 (force-mode-line-update)
877 ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
878 vc-type))
880 (defun vc-status (file)
881 ;; Return string for placement in modeline by `vc-mode-line'.
882 ;; Format:
884 ;; "-REV" if the revision is not locked
885 ;; ":REV" if the revision is locked by the user
886 ;; ":LOCKER:REV" if the revision is locked by somebody else
887 ;; " @@" for a CVS file that is added, but not yet committed
889 ;; In the CVS case, a "locked" working file is a
890 ;; working file that is modified with respect to the master.
891 ;; The file is "locked" from the moment when the user saves
892 ;; the modified buffer.
894 ;; This function assumes that the file is registered.
896 (let ((locker (vc-locking-user file))
897 (rev (vc-workfile-version file)))
898 (cond ((string= "0" rev)
899 " @@")
900 ((not locker)
901 (concat "-" rev))
902 ((if (stringp locker)
903 (string= locker (user-login-name))
904 (= locker (user-uid)))
905 (concat ":" rev))
907 (concat ":" locker ":" rev)))))
909 ;;; install a call to the above as a find-file hook
910 (defun vc-find-file-hook ()
911 ;; Recompute whether file is version controlled,
912 ;; if user has killed the buffer and revisited.
913 (cond
914 (buffer-file-name
915 (vc-file-clearprops buffer-file-name)
916 (cond
917 ((vc-backend buffer-file-name)
918 (vc-mode-line buffer-file-name)
919 (cond ((not vc-make-backup-files)
920 ;; Use this variable, not make-backup-files,
921 ;; because this is for things that depend on the file name.
922 (make-local-variable 'backup-inhibited)
923 (setq backup-inhibited t))))
924 ((let* ((link (file-symlink-p buffer-file-name))
925 (link-type (and link (vc-backend link))))
926 (if link-type
927 (message
928 "Warning: symbolic link to %s-controlled source file"
929 link-type))))))))
931 (add-hook 'find-file-hooks 'vc-find-file-hook)
933 ;;; more hooks, this time for file-not-found
934 (defun vc-file-not-found-hook ()
935 "When file is not found, try to check it out from RCS or SCCS.
936 Returns t if checkout was successful, nil otherwise."
937 (if (vc-backend buffer-file-name)
938 (save-excursion
939 (require 'vc)
940 (setq default-directory (file-name-directory (buffer-file-name)))
941 (not (vc-error-occurred (vc-checkout buffer-file-name))))))
943 (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
945 ;; Discard info about a file when we kill its buffer.
946 (defun vc-kill-buffer-hook ()
947 (if (stringp (buffer-file-name))
948 (progn
949 (vc-file-clearprops (buffer-file-name))
950 (kill-local-variable 'vc-buffer-backend))))
952 ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
954 ;;; Now arrange for bindings and autoloading of the main package.
955 ;;; Bindings for this have to go in the global map, as we'll often
956 ;;; want to call them from random buffers.
958 (setq vc-prefix-map (lookup-key global-map "\C-xv"))
959 (if (not (keymapp vc-prefix-map))
960 (progn
961 (setq vc-prefix-map (make-sparse-keymap))
962 (define-key global-map "\C-xv" vc-prefix-map)
963 (define-key vc-prefix-map "a" 'vc-update-change-log)
964 (define-key vc-prefix-map "c" 'vc-cancel-version)
965 (define-key vc-prefix-map "d" 'vc-directory)
966 (define-key vc-prefix-map "h" 'vc-insert-headers)
967 (define-key vc-prefix-map "i" 'vc-register)
968 (define-key vc-prefix-map "l" 'vc-print-log)
969 (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
970 (define-key vc-prefix-map "s" 'vc-create-snapshot)
971 (define-key vc-prefix-map "u" 'vc-revert-buffer)
972 (define-key vc-prefix-map "v" 'vc-next-action)
973 (define-key vc-prefix-map "=" 'vc-diff)
974 (define-key vc-prefix-map "~" 'vc-version-other-window)))
976 (if (not (boundp 'vc-menu-map))
977 ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
978 ;; vc-menu-map.
980 ;;(define-key vc-menu-map [show-files]
981 ;; '("Show Files under VC" . (vc-directory t)))
982 (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
983 (define-key vc-menu-map [separator1] '("----"))
984 (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
985 (define-key vc-menu-map [vc-version-other-window]
986 '("Show Other Version" . vc-version-other-window))
987 (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
988 (define-key vc-menu-map [vc-update-change-log]
989 '("Update ChangeLog" . vc-update-change-log))
990 (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
991 (define-key vc-menu-map [separator2] '("----"))
992 (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
993 (define-key vc-menu-map [vc-revert-buffer]
994 '("Revert to Last Version" . vc-revert-buffer))
995 (define-key vc-menu-map [vc-insert-header]
996 '("Insert Header" . vc-insert-headers))
997 (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
998 (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
999 (define-key vc-menu-map [vc-register] '("Register" . vc-register))
1000 (put 'vc-rename-file 'menu-enable 'vc-mode)
1001 (put 'vc-version-other-window 'menu-enable 'vc-mode)
1002 (put 'vc-diff 'menu-enable 'vc-mode)
1003 (put 'vc-update-change-log 'menu-enable
1004 '(eq (vc-buffer-backend) 'RCS))
1005 (put 'vc-print-log 'menu-enable 'vc-mode)
1006 (put 'vc-cancel-version 'menu-enable 'vc-mode)
1007 (put 'vc-revert-buffer 'menu-enable 'vc-mode)
1008 (put 'vc-insert-headers 'menu-enable 'vc-mode)
1009 (put 'vc-next-action 'menu-enable '(and vc-mode (not buffer-read-only)))
1010 (put 'vc-toggle-read-only 'menu-enable '(and vc-mode buffer-read-only))
1011 (put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
1014 (provide 'vc-hooks)
1016 ;;; vc-hooks.el ends here