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