1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; This is the always-loaded portion of VC. It takes care of
28 ;; VC-related activities that are done when you visit a file, so that
29 ;; vc.el itself is loaded only when you use a VC command. See the
30 ;; commentary of vc.el.
37 ;; Customization Variables (the rest is in vc.el)
39 (defvar vc-ignore-vc-files nil
)
40 (make-obsolete-variable 'vc-ignore-vc-files
41 "set `vc-handled-backends' to nil to disable VC."
44 (defvar vc-master-templates
())
45 (make-obsolete-variable 'vc-master-templates
46 "to define master templates for a given BACKEND, use
47 vc-BACKEND-master-templates. To enable or disable VC for a given
48 BACKEND, use `vc-handled-backends'."
51 (defvar vc-header-alist
())
52 (make-obsolete-variable 'vc-header-alist
'vc-BACKEND-header
"21.1")
54 (defcustom vc-ignore-dir-regexp
55 ;; Stop SMB, automounter, AFS, and DFS host lookups.
56 locate-dominating-stop-dir-regexp
57 "Regexp matching directory names that are not under VC's control.
58 The default regexp prevents fruitless and time-consuming attempts
59 to determine the VC status in directories in which filenames are
60 interpreted as hostnames."
64 (defcustom vc-handled-backends
'(RCS CVS SVN SCCS Bzr Git Hg Mtn Arch
)
65 ;; RCS, CVS, SVN and SCCS come first because they are per-dir
66 ;; rather than per-tree. RCS comes first because of the multibackend
67 ;; support intended to use RCS for local commits (with a remote CVS server).
68 "List of version control backends for which VC will be used.
69 Entries in this list will be tried in order to determine whether a
70 file is under that sort of version control.
71 Removing an entry from the list prevents VC from being activated
72 when visiting a file managed by that backend.
73 An empty list disables VC altogether."
74 :type
'(repeat symbol
)
78 ;; Note: we don't actually have a darcs back end yet.
79 ;; Also, Meta-CVS (corresponsding to MCVS) is unsupported.
80 (defcustom vc-directory-exclusion-list
(purecopy '("SCCS" "RCS" "CVS" "MCVS"
81 ".svn" ".git" ".hg" ".bzr"
82 "_MTN" "_darcs" "{arch}"))
83 "List of directory names to be ignored when walking directory trees."
84 :type
'(repeat string
)
87 (defcustom vc-path nil
88 "List of extra directories to search for version control commands."
89 :type
'(repeat directory
)
92 (defcustom vc-make-backup-files nil
93 "If non-nil, backups of registered files are made as with other files.
94 If nil (the default), files covered by version control don't get backups."
99 (defcustom vc-follow-symlinks
'ask
100 "What to do if visiting a symbolic link to a file under version control.
101 Editing such a file through the link bypasses the version control system,
102 which is dangerous and probably not what you want.
104 If this variable is t, VC follows the link and visits the real file,
105 telling you about it in the echo area. If it is `ask', VC asks for
106 confirmation whether it should follow the link. If nil, the link is
107 visited and a warning displayed."
108 :type
'(choice (const :tag
"Ask for confirmation" ask
)
109 (const :tag
"Visit link and warn" nil
)
110 (const :tag
"Follow link" t
))
113 (defcustom vc-display-status t
114 "If non-nil, display revision number and lock status in modeline.
115 Otherwise, not displayed."
120 (defcustom vc-consult-headers t
121 "If non-nil, identify work files by searching for version headers."
125 (defcustom vc-keep-workfiles t
126 "If non-nil, don't delete working files after registering changes.
127 If the back-end is CVS, workfiles are always kept, regardless of the
132 (defcustom vc-mistrust-permissions nil
133 "If non-nil, don't assume permissions/ownership track version-control status.
134 If nil, do rely on the permissions.
135 See also variable `vc-consult-headers'."
139 (defun vc-mistrust-permissions (file)
140 "Internal access function to variable `vc-mistrust-permissions' for FILE."
141 (or (eq vc-mistrust-permissions
't
)
142 (and vc-mistrust-permissions
143 (funcall vc-mistrust-permissions
144 (vc-backend-subdirectory-name file
)))))
146 (defcustom vc-stay-local
'only-file
147 "Non-nil means use local operations when possible for remote repositories.
148 This avoids slow queries over the network and instead uses heuristics
149 and past information to determine the current status of a file.
151 If value is the symbol `only-file' `vc-dir' will connect to the
152 server, but heuristics will be used to determine the status for
153 all other VC operations.
155 The value can also be a regular expression or list of regular
156 expressions to match against the host name of a repository; then VC
157 only stays local for hosts that match it. Alternatively, the value
158 can be a list of regular expressions where the first element is the
159 symbol `except'; then VC always stays local except for hosts matched
160 by these regular expressions."
162 (const :tag
"Always stay local" t
)
163 (const :tag
"Only for file operations" only-file
)
164 (const :tag
"Don't stay local" nil
)
165 (list :format
"\nExamine hostname and %v" :tag
"Examine hostname ..."
166 (set :format
"%v" :inline t
(const :format
"%t" :tag
"don't" except
))
167 (regexp :format
" stay local,\n%t: %v" :tag
"if it matches")
168 (repeat :format
"%v%i\n" :inline t
(regexp :tag
"or"))))
172 (defun vc-stay-local-p (file &optional backend
)
173 "Return non-nil if VC should stay local when handling FILE.
174 This uses the `repository-hostname' backend operation.
175 If FILE is a list of files, return non-nil if any of them
176 individually should stay local."
178 (delq nil
(mapcar (lambda (arg) (vc-stay-local-p arg backend
)) file
))
179 (setq backend
(or backend
(vc-backend file
)))
180 (let* ((sym (vc-make-backend-sym backend
'stay-local
))
181 (stay-local (if (boundp sym
) (symbol-value sym
) vc-stay-local
)))
182 (if (symbolp stay-local
) stay-local
183 (let ((dirname (if (file-directory-p file
)
184 (directory-file-name file
)
185 (file-name-directory file
))))
187 (or (vc-file-getprop dirname
'vc-stay-local-p
)
189 dirname
'vc-stay-local-p
190 (let ((hostname (vc-call-backend
191 backend
'repository-hostname dirname
)))
195 (if (eq (car-safe stay-local
) 'except
)
196 (setq default nil stay-local
(cdr stay-local
)))
197 (when (consp stay-local
)
199 (mapconcat 'identity stay-local
"\\|")))
200 (if (if (string-match stay-local hostname
)
201 default
(not default
))
204 ;;; This is handled specially now.
205 ;; Tell Emacs about this new kind of minor mode
206 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
209 (put 'vc-mode
'risky-local-variable t
)
210 (make-variable-buffer-local 'vc-mode
)
211 (put 'vc-mode
'permanent-local t
)
213 (defun vc-mode (&optional arg
)
214 ;; Dummy function for C-h m
215 "Version Control minor mode.
216 This minor mode is automatically activated whenever you visit a file under
217 control of one of the revision control systems in `vc-handled-backends'.
218 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
221 (defmacro vc-error-occurred
(&rest body
)
222 `(condition-case nil
(progn ,@body nil
) (error t
)))
224 ;; We need a notion of per-file properties because the version
225 ;; control state of a file is expensive to derive --- we compute
226 ;; them when the file is initially found, keep them up to date
227 ;; during any subsequent VC operations, and forget them when
228 ;; the buffer is killed.
230 (defvar vc-file-prop-obarray
(make-vector 17 0)
231 "Obarray for per-file properties.")
233 (defvar vc-touched-properties nil
)
235 (defun vc-file-setprop (file property value
)
236 "Set per-file VC PROPERTY for FILE to VALUE."
237 (if (and vc-touched-properties
238 (not (memq property vc-touched-properties
)))
239 (setq vc-touched-properties
(append (list property
)
240 vc-touched-properties
)))
241 (put (intern file vc-file-prop-obarray
) property value
))
243 (defun vc-file-getprop (file property
)
244 "Get per-file VC PROPERTY for FILE."
245 (get (intern file vc-file-prop-obarray
) property
))
247 (defun vc-file-clearprops (file)
248 "Clear all VC properties of FILE."
249 (setplist (intern file vc-file-prop-obarray
) nil
))
252 ;; We keep properties on each symbol naming a backend as follows:
253 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
255 (defun vc-make-backend-sym (backend sym
)
256 "Return BACKEND-specific version of VC symbol SYM."
257 (intern (concat "vc-" (downcase (symbol-name backend
))
258 "-" (symbol-name sym
))))
260 (defun vc-find-backend-function (backend fun
)
261 "Return BACKEND-specific implementation of FUN.
262 If there is no such implementation, return the default implementation;
263 if that doesn't exist either, return nil."
264 (let ((f (vc-make-backend-sym backend fun
)))
266 ;; Load vc-BACKEND.el if needed.
267 (require (intern (concat "vc-" (downcase (symbol-name backend
)))))
269 (let ((def (vc-make-backend-sym 'default fun
)))
270 (if (fboundp def
) (cons def backend
) nil
))))))
272 (defun vc-call-backend (backend function-name
&rest args
)
273 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
276 (apply 'vc-BACKEND-FUN ARGS)
278 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
281 (apply 'vc-default-FUN BACKEND ARGS)
283 It is usually called via the `vc-call' macro."
284 (let ((f (assoc function-name
(get backend
'vc-functions
))))
285 (if f
(setq f
(cdr f
))
286 (setq f
(vc-find-backend-function backend function-name
))
287 (push (cons function-name f
) (get backend
'vc-functions
)))
290 (error "Sorry, %s is not implemented for %s" function-name backend
))
291 ((consp f
) (apply (car f
) (cdr f
) args
))
292 (t (apply f args
)))))
294 (defmacro vc-call
(fun file
&rest args
)
295 "A convenience macro for calling VC backend functions.
296 Functions called by this macro must accept FILE as the first argument.
297 ARGS specifies any additional arguments. FUN should be unquoted.
298 BEWARE!! FILE is evaluated twice!!"
299 `(vc-call-backend (vc-backend ,file
) ',fun
,file
,@args
))
301 (defsubst vc-parse-buffer
(pattern i
)
302 "Find PATTERN in the current buffer and return its Ith submatch."
303 (goto-char (point-min))
304 (if (re-search-forward pattern nil t
)
307 (defun vc-insert-file (file &optional limit blocksize
)
308 "Insert the contents of FILE into the current buffer.
310 Optional argument LIMIT is a regexp. If present, the file is inserted
311 in chunks of size BLOCKSIZE (default 8 kByte), until the first
312 occurrence of LIMIT is found. Anything from the start of that occurrence
313 to the end of the buffer is then deleted. The function returns
314 non-nil if FILE exists and its contents were successfully inserted."
316 (when (file-exists-p file
)
318 (insert-file-contents file
)
319 (unless blocksize
(setq blocksize
8192))
322 (and (< 0 (cadr (insert-file-contents
323 file nil filepos
(incf filepos blocksize
))))
324 (progn (beginning-of-line)
325 (let ((pos (re-search-forward limit nil
'move
)))
326 (when pos
(delete-region (match-beginning 0)
329 (set-buffer-modified-p nil
)
332 (defun vc-find-root (file witness
)
333 "Find the root of a checked out project.
334 The function walks up the directory tree from FILE looking for WITNESS.
335 If WITNESS if not found, return nil, otherwise return the root."
336 (let ((locate-dominating-stop-dir-regexp
337 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp
)))
338 (locate-dominating-file file witness
)))
340 ;; Access functions to file properties
341 ;; (Properties should be _set_ using vc-file-setprop, but
342 ;; _retrieved_ only through these functions, which decide
343 ;; if the property is already known or not. A property should
344 ;; only be retrieved by vc-file-getprop if there is no
347 ;; properties indicating the backend being used for FILE
349 (defun vc-registered (file)
350 "Return non-nil if FILE is registered in a version control system.
352 This function performs the check each time it is called. To rely
353 on the result of a previous call, use `vc-backend' instead. If the
354 file was previously registered under a certain backend, then that
355 backend is tried first."
358 ((and (file-name-directory file
)
359 (string-match vc-ignore-dir-regexp
(file-name-directory file
)))
361 ((and (boundp 'file-name-handler-alist
)
362 (setq handler
(find-file-name-handler file
'vc-registered
)))
363 ;; handler should set vc-backend and return t if registered
364 (funcall handler
'vc-registered file
))
366 ;; There is no file name handler.
367 ;; Try vc-BACKEND-registered for each handled BACKEND.
369 (let ((backend (vc-file-getprop file
'vc-backend
)))
372 (and (vc-call-backend b
'registered file
)
373 (vc-file-setprop file
'vc-backend b
)
375 (if (or (not backend
) (eq backend
'none
))
377 (cons backend vc-handled-backends
))))
378 ;; File is not registered.
379 (vc-file-setprop file
'vc-backend
'none
)
382 (defun vc-backend (file-or-list)
383 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
384 If the argument is a list, the files must all have the same back end."
385 ;; `file' can be nil in several places (typically due to the use of
386 ;; code like (vc-backend buffer-file-name)).
387 (cond ((stringp file-or-list
)
388 (let ((property (vc-file-getprop file-or-list
'vc-backend
)))
389 ;; Note that internally, Emacs remembers unregistered
390 ;; files by setting the property to `none'.
391 (cond ((eq property
'none
) nil
)
393 ;; vc-registered sets the vc-backend property
394 (t (if (vc-registered file-or-list
)
395 (vc-file-getprop file-or-list
'vc-backend
)
397 ((and file-or-list
(listp file-or-list
))
398 (vc-backend (car file-or-list
)))
403 (defun vc-backend-subdirectory-name (file)
404 "Return where the master and lock FILEs for the current directory are kept."
405 (symbol-name (vc-backend file
)))
407 (defun vc-name (file)
408 "Return the master name of FILE.
409 If the file is not registered, or the master name is not known, return nil."
410 ;; TODO: This should ultimately become obsolete, at least up here
412 (or (vc-file-getprop file
'vc-name
)
413 ;; force computation of the property by calling
414 ;; vc-BACKEND-registered explicitly
415 (let ((backend (vc-backend file
)))
417 (vc-call-backend backend
'registered file
))
418 (vc-file-getprop file
'vc-name
)))))
420 (defun vc-checkout-model (backend files
)
421 "Indicate how FILES are checked out.
423 If FILES are not registered, this function always returns nil.
424 For registered files, the possible values are:
426 'implicit FILES are always writable, and checked out `implicitly'
427 when the user saves the first changes to the file.
429 'locking FILES are read-only if up-to-date; user must type
430 \\[vc-next-action] before editing. Strict locking
433 'announce FILES are read-only if up-to-date; user must type
434 \\[vc-next-action] before editing. But other users
435 may be editing at the same time."
436 (vc-call-backend backend
'checkout-model files
))
438 (defun vc-user-login-name (file)
439 "Return the name under which the user accesses the given FILE."
440 (or (and (eq (string-match tramp-file-name-regexp file
) 0)
441 ;; tramp case: execute "whoami" via tramp
442 (let ((default-directory (file-name-directory file
))
443 process-file-side-effects
)
445 (if (not (zerop (process-file "whoami" nil t
)))
446 ;; fall through if "whoami" didn't work
448 ;; remove trailing newline
449 (delete-region (1- (point-max)) (point-max))
453 ;; if user-login-name is nil, return the UID as a string
454 (number-to-string (user-uid))))
456 (defun vc-state (file &optional backend
)
457 "Return the version control state of FILE.
459 If FILE is not registered, this function always returns nil.
460 For registered files, the value returned is one of:
462 'up-to-date The working file is unmodified with respect to the
463 latest version on the current branch, and not locked.
465 'edited The working file has been edited by the user. If
466 locking is used for the file, this state means that
467 the current version is locked by the calling user.
469 USER The current version of the working file is locked by
470 some other USER (a string).
472 'needs-update The file has not been edited by the user, but there is
473 a more recent version on the current branch stored
476 'needs-merge The file has been edited by the user, and there is also
477 a more recent version on the current branch stored in
478 the master file. This state can only occur if locking
479 is not used for the file.
481 'unlocked-changes The working version of the file is not locked,
482 but the working file has been changed with respect
483 to that version. This state can only occur for files
484 with locking; it represents an erroneous condition that
485 should be resolved by the user (vc-next-action will
486 prompt the user to do it).
488 'added Scheduled to go into the repository on the next commit.
489 Often represented by vc-working-revision = \"0\" in VCSes
490 with monotonic IDs like Subversion and Mercurial.
492 'removed Scheduled to be deleted from the repository on next commit.
494 'conflict The file contains conflicts as the result of a merge.
495 For now the conflicts are text conflicts. In the
496 future this might be extended to deal with metadata
499 'missing The file is not present in the file system, but the VC
500 system still tracks it.
502 'ignored The file showed up in a dir-status listing with a flag
503 indicating the version-control system is ignoring it,
504 Note: This property is not set reliably (some VCSes
505 don't have useful directory-status commands) so assume
506 that any file with vc-state nil might be ignorable
507 without VC knowing it.
509 'unregistered The file is not under version control.
511 A return of nil from this function means we have no information on the
512 status of this file."
513 ;; Note: in Emacs 22 and older, return of nil meant the file was
514 ;; unregistered. This is potentially a source of
515 ;; backward-compatibility bugs.
517 ;; FIXME: New (sub)states needed (?):
518 ;; - `copied' and `moved' (might be handled by `removed' and `added')
519 (or (vc-file-getprop file
'vc-state
)
520 (when (> (length file
) 0) ;Why?? --Stef
521 (setq backend
(or backend
(vc-backend file
)))
523 (vc-state-refresh file backend
)))))
525 (defun vc-state-refresh (file backend
)
526 "Quickly recompute the `state' of FILE."
529 (vc-call-backend backend
'state-heuristic file
)))
531 (defsubst vc-up-to-date-p
(file)
532 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
533 (eq (vc-state file
) 'up-to-date
))
535 (defun vc-default-state-heuristic (backend file
)
536 "Default implementation of vc-BACKEND-state-heuristic.
537 It simply calls the real state computation function `vc-BACKEND-state'
538 and does not employ any heuristic at all."
539 (vc-call-backend backend
'state file
))
541 (defun vc-workfile-unchanged-p (file)
542 "Return non-nil if FILE has not changed since the last checkout."
543 (let ((checkout-time (vc-file-getprop file
'vc-checkout-time
))
544 (lastmod (nth 5 (file-attributes file
))))
545 ;; This is a shortcut for determining when the workfile is
546 ;; unchanged. It can fail under some circumstances; see the
547 ;; discussion in bug#694.
548 (if (and checkout-time
549 ;; Tramp and Ange-FTP return this when they don't know the time.
550 (not (equal lastmod
'(0 0))))
551 (equal checkout-time lastmod
)
552 (let ((unchanged (vc-call workfile-unchanged-p file
)))
553 (vc-file-setprop file
'vc-checkout-time
(if unchanged lastmod
0))
556 (defun vc-default-workfile-unchanged-p (backend file
)
557 "Check if FILE is unchanged by diffing against the master version.
558 Return non-nil if FILE is unchanged."
559 (zerop (condition-case err
560 ;; If the implementation supports it, let the output
561 ;; go to *vc*, not *vc-diff*, since this is an internal call.
562 (vc-call-backend backend
'diff
(list file
) nil nil
"*vc*")
563 (wrong-number-of-arguments
564 ;; If this error came from the above call to vc-BACKEND-diff,
565 ;; try again without the optional buffer argument (for
566 ;; backward compatibility). Otherwise, resignal.
567 (if (or (not (eq (cadr err
)
569 (vc-find-backend-function backend
'diff
))))
570 (not (eq (caddr err
) 4)))
571 (signal (car err
) (cdr err
))
572 (vc-call-backend backend
'diff
(list file
)))))))
574 (defun vc-working-revision (file &optional backend
)
575 "Return the repository version from which FILE was checked out.
576 If FILE is not registered, this function always returns nil."
577 (or (vc-file-getprop file
'vc-working-revision
)
579 (setq backend
(or backend
(vc-backend file
)))
581 (vc-file-setprop file
'vc-working-revision
582 (vc-call-backend backend
'working-revision file
))))))
584 ;; Backward compatibility.
585 (define-obsolete-function-alias
586 'vc-workfile-version
'vc-working-revision
"23.1")
587 (defun vc-default-working-revision (backend file
)
589 "`working-revision' not found: using the old `workfile-version' instead")
590 (vc-call-backend backend
'workfile-version file
))
592 (defun vc-default-registered (backend file
)
593 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
594 (let ((sym (vc-make-backend-sym backend
'master-templates
)))
595 (unless (get backend
'vc-templates-grabbed
)
596 (put backend
'vc-templates-grabbed t
)
597 (set sym
(append (delq nil
600 (and (consp template
)
601 (eq (cdr template
) backend
)
604 vc-master-templates
)))
605 (symbol-value sym
))))
606 (let ((result (vc-check-master-templates file
(symbol-value sym
))))
608 (vc-file-setprop file
'vc-name result
)
609 nil
)))) ; Not registered
611 (defun vc-possible-master (s dirname basename
)
613 ((stringp s
) (format s dirname basename
))
615 ;; The template is a function to invoke. If the
616 ;; function returns non-nil, that means it has found a
617 ;; master. For backward compatibility, we also handle
618 ;; the case that the function throws a 'found atom
619 ;; and a pair (cons MASTER-FILE BACKEND).
620 (let ((result (catch 'found
(funcall s dirname basename
))))
621 (if (consp result
) (car result
) result
)))))
623 (defun vc-check-master-templates (file templates
)
624 "Return non-nil if there is a master corresponding to FILE.
626 TEMPLATES is a list of strings or functions. If an element is a
627 string, it must be a control string as required by `format', with two
628 string placeholders, such as \"%sRCS/%s,v\". The directory part of
629 FILE is substituted for the first placeholder, the basename of FILE
630 for the second. If a file with the resulting name exists, it is taken
631 as the master of FILE, and returned.
633 If an element of TEMPLATES is a function, it is called with the
634 directory part and the basename of FILE as arguments. It should
635 return non-nil if it finds a master; that value is then returned by
637 (let ((dirname (or (file-name-directory file
) ""))
638 (basename (file-name-nondirectory file
)))
642 (let ((trial (vc-possible-master s dirname basename
)))
643 (when (and trial
(file-exists-p trial
)
644 ;; Make sure the file we found with name
645 ;; TRIAL is not the source file itself.
646 ;; That can happen with RCS-style names if
647 ;; the file name is truncated (e.g. to 14
648 ;; chars). See if either directory or
649 ;; attributes differ.
650 (or (not (string= dirname
651 (file-name-directory trial
)))
652 (not (equal (file-attributes file
)
653 (file-attributes trial
)))))
654 (throw 'found trial
))))
657 (defun vc-toggle-read-only (&optional verbose
)
658 "Change read-only status of current buffer, perhaps via version control.
660 If the buffer is visiting a file registered with version control,
661 throw an error, because this is not a safe or really meaningful operation
662 on any version-control system newer than RCS.
664 Otherwise, just change the read-only flag of the buffer.
666 If you bind this function to \\[toggle-read-only], then Emacs
667 will properly intercept all attempts to toggle the read-only flag
668 on version-controlled buffer."
670 (if (vc-backend buffer-file-name
)
671 (error "Toggling the readability of a version controlled file is likely to wreak havoc")
674 (defun vc-default-make-version-backups-p (backend file
)
675 "Return non-nil if unmodified versions should be backed up locally.
676 The default is to switch off this feature."
679 (defun vc-version-backup-file-name (file &optional rev manual regexp
)
680 "Return a backup file name for REV or the current version of FILE.
681 If MANUAL is non-nil it means that a name for backups created by
682 the user should be returned; if REGEXP is non-nil that means to return
683 a regexp for matching all such backup files, regardless of the version."
685 (concat (regexp-quote (file-name-nondirectory file
))
686 "\\.~.+" (unless manual
"\\.") "~")
687 (expand-file-name (concat (file-name-nondirectory file
)
688 ".~" (subst-char-in-string
689 ?
/ ?_
(or rev
(vc-working-revision file
)))
690 (unless manual
".") "~")
691 (file-name-directory file
))))
693 (defun vc-delete-automatic-version-backups (file)
694 "Delete all existing automatic version backups for FILE."
698 (directory-files (or (file-name-directory file
) default-directory
) t
699 (vc-version-backup-file-name file nil nil t
)))
700 ;; Don't fail when the directory doesn't exist.
703 (defun vc-make-version-backup (file)
704 "Make a backup copy of FILE, which is assumed in sync with the repository.
705 Before doing that, check if there are any old backups and get rid of them."
706 (unless (and (fboundp 'msdos-long-file-names
)
707 (not (with-no-warnings (msdos-long-file-names))))
708 (vc-delete-automatic-version-backups file
)
710 (copy-file file
(vc-version-backup-file-name file
)
712 ;; It's ok if it doesn't work (e.g. directory not writable),
713 ;; since this is just for efficiency.
716 (concat "Warning: Cannot make version backup; "
717 "diff/revert therefore not local"))))))
719 (defun vc-before-save ()
720 "Function to be called by `basic-save-buffer' (in files.el)."
721 ;; If the file on disk is still in sync with the repository,
722 ;; and version backups should be made, copy the file to
723 ;; another name. This enables local diffs and local reverting.
724 (let ((file buffer-file-name
)
726 (ignore-errors ;Be careful not to prevent saving the file.
727 (and (setq backend
(vc-backend file
))
728 (vc-up-to-date-p file
)
729 (eq (vc-checkout-model backend
(list file
)) 'implicit
)
730 (vc-call-backend backend
'make-version-backups-p file
)
731 (vc-make-version-backup file
)))))
733 (declare-function vc-dir-resynch-file
"vc-dir" (&optional fname
))
735 (defvar vc-dir-buffers nil
"List of vc-dir buffers.")
737 (defun vc-after-save ()
738 "Function to be called by `basic-save-buffer' (in files.el)."
739 ;; If the file in the current buffer is under version control,
740 ;; up-to-date, and locking is not used for the file, set
741 ;; the state to 'edited and redisplay the mode line.
742 (let* ((file buffer-file-name
)
743 (backend (vc-backend file
)))
745 (or (and (equal (vc-file-getprop file
'vc-checkout-time
)
746 (nth 5 (file-attributes file
)))
747 ;; File has been saved in the same second in which
748 ;; it was checked out. Clear the checkout-time
749 ;; to avoid confusion.
750 (vc-file-setprop file
'vc-checkout-time nil
))
752 (eq (vc-checkout-model backend
(list file
)) 'implicit
)
753 (vc-state-refresh file backend
)
754 (vc-mode-line file backend
))
755 ;; Try to avoid unnecessary work, a *vc-dir* buffer is
756 ;; present if this is true.
758 (vc-dir-resynch-file file
))))
760 (defvar vc-menu-entry
761 `(menu-item ,(purecopy "Version Control") vc-menu-map
762 :filter vc-menu-map-filter
))
764 (when (boundp 'menu-bar-tools-menu
)
765 ;; We do not need to worry here about the placement of this entry
766 ;; because menu-bar.el has already created the proper spot for us
767 ;; and this will simply use it.
768 (define-key menu-bar-tools-menu
[vc] vc-menu-entry))
770 (defconst vc-mode-line-map
771 (let ((map (make-sparse-keymap)))
772 (define-key map [mode-line down-mouse-1] vc-menu-entry)
775 (defun vc-mode-line (file &optional backend)
776 "Set `vc-mode' to display type of version control for FILE.
777 The value is set in the current buffer, which should be the buffer
779 If BACKEND is passed use it as the VC backend when computing the result."
780 (interactive (list buffer-file-name))
781 (setq backend (or backend (vc-backend file)))
784 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
785 (ml-echo (get-text-property 0 'help-echo ml-string)))
789 (if (null vc-display-status)
790 (symbol-name backend)
793 'mouse-face 'mode-line-highlight
796 (format "File under the %s version control system"
798 "\nmouse-1: Version Control menu")
799 'local-map vc-mode-line-map)))))
800 ;; If the user is root, and the file is not owner-writable,
801 ;; then pretend that we can't write it
802 ;; even though we can (because root can write anything).
803 ;; This way, even root cannot modify a file that isn't locked.
804 (and (equal file buffer-file-name)
805 (not buffer-read-only)
806 (zerop (user-real-uid))
807 (zerop (logand (file-modes buffer-file-name) 128))
808 (setq buffer-read-only t)))
809 (force-mode-line-update)
812 (defun vc-default-mode-line-string (backend file)
813 "Return string for placement in modeline by `vc-mode-line' for FILE.
816 \"BACKEND-REV\" if the file is up-to-date
817 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
818 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
820 This function assumes that the file is registered."
821 (let* ((backend-name (symbol-name backend))
822 (state (vc-state file backend))
824 (rev (vc-working-revision file backend)))
826 (cond ((or (eq state 'up-to-date)
827 (eq state 'needs-update))
828 (setq state-echo "Up to date file")
829 (concat backend-name "-" rev))
831 (setq state-echo (concat "File locked by" state))
832 (concat backend-name ":" state ":" rev))
834 (setq state-echo "Locally added file")
835 (concat backend-name "@" rev))
836 ((eq state 'conflict)
837 (setq state-echo "File contains conflicts after the last merge")
838 (concat backend-name "!" rev))
840 (setq state-echo "File removed from the VC system")
841 (concat backend-name "!" rev))
843 (setq state-echo "File tracked by the VC system, but missing from the file system")
844 (concat backend-name "?" rev))
846 ;; Not just for the 'edited state, but also a fallback
847 ;; for all other states. Think about different symbols
848 ;; for 'needs-update and 'needs-merge.
849 (setq state-echo "Locally modified file")
850 (concat backend-name ":" rev)))
851 'help-echo (concat state-echo " under the " backend-name
852 " version control system"))))
854 (defun vc-follow-link ()
855 "If current buffer visits a symbolic link, visit the real file.
856 If the real file is already visited in another buffer, make that buffer
857 current, and kill the buffer that visits the link."
858 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
859 (this-buffer (current-buffer)))
860 (if (eq true-buffer this-buffer)
861 (let ((truename buffer-file-truename))
862 (kill-buffer this-buffer)
863 ;; In principle, we could do something like set-visited-file-name.
864 ;; However, it can't be exactly the same as set-visited-file-name.
865 ;; I'm not going to work out the details right now. -- rms.
866 (set-buffer (find-file-noselect truename)))
867 (set-buffer true-buffer)
868 (kill-buffer this-buffer))))
870 (defun vc-default-find-file-hook (backend)
873 (defun vc-find-file-hook ()
874 "Function for `find-file-hook' activating VC mode if appropriate."
875 ;; Recompute whether file is version controlled,
876 ;; if user has killed the buffer and revisited.
879 (when buffer-file-name
880 (vc-file-clearprops buffer-file-name)
881 (add-hook 'mode-line-hook 'vc-mode-line nil t)
884 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
885 ;; Compute the state and put it in the modeline.
886 (vc-mode-line buffer-file-name backend)
887 (unless vc-make-backup-files
888 ;; Use this variable, not make-backup-files,
889 ;; because this is for things that depend on the file name.
890 (set (make-local-variable 'backup-inhibited) t))
891 ;; Let the backend setup any buffer-local things he needs.
892 (vc-call-backend backend 'find-file-hook))
893 ((let ((link-type (and (not (equal buffer-file-name buffer-file-truename))
894 (vc-backend buffer-file-truename))))
895 (cond ((not link-type) nil) ;Nothing to do.
896 ((eq vc-follow-symlinks nil)
898 "Warning: symbolic link to %s-controlled source file" link-type))
899 ((or (not (eq vc-follow-symlinks 'ask))
900 ;; If we already visited this file by following
901 ;; the link, don't ask again if we try to visit
902 ;; it again. GUD does that, and repeated questions
905 (abbreviate-file-name
906 (file-chase-links buffer-file-name))))
909 (message "Followed link to %s" buffer-file-name)
912 (if (yes-or-no-p (format
913 "Symbolic link to %s-controlled source file; follow link? " link-type))
914 (progn (vc-follow-link)
915 (message "Followed link to %s" buffer-file-name)
918 "Warning: editing through the link bypasses version control")
921 (add-hook 'find-file-hook 'vc-find-file-hook)
923 (defun vc-kill-buffer-hook ()
924 "Discard VC info about a file when we kill its buffer."
925 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
927 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
929 ;; Now arrange for (autoloaded) bindings of the main package.
930 ;; Bindings for this have to go in the global map, as we'll often
931 ;; want to call them from random buffers.
933 ;; Autoloading works fine, but it prevents shortcuts from appearing
934 ;; in the menu because they don't exist yet when the menu is built.
935 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
936 (defvar vc-prefix-map
937 (let ((map (make-sparse-keymap)))
938 (define-key map "a" 'vc-update-change-log)
939 (define-key map "b" 'vc-switch-backend)
940 (define-key map "c" 'vc-rollback)
941 (define-key map "d" 'vc-dir)
942 (define-key map "g" 'vc-annotate)
943 (define-key map "h" 'vc-insert-headers)
944 (define-key map "i" 'vc-register)
945 (define-key map "l" 'vc-print-log)
946 (define-key map "L" 'vc-print-root-log)
947 (define-key map "m" 'vc-merge)
948 (define-key map "r" 'vc-retrieve-tag)
949 (define-key map "s" 'vc-create-tag)
950 (define-key map "u" 'vc-revert)
951 (define-key map "v" 'vc-next-action)
952 (define-key map "+" 'vc-update)
953 (define-key map "=" 'vc-diff)
954 (define-key map "D" 'vc-root-diff)
955 (define-key map "~" 'vc-revision-other-window)
957 (fset 'vc-prefix-map vc-prefix-map)
958 (define-key global-map "\C-xv" 'vc-prefix-map)
961 (let ((map (make-sparse-keymap "Version Control")))
962 ;;(define-key map [show-files]
963 ;; '("Show Files under VC" . (vc-directory t)))
964 (define-key map [vc-retrieve-tag]
965 `(menu-item ,(purecopy "Retrieve Tag") vc-retrieve-tag
966 :help ,(purecopy "Retrieve tagged version or branch")))
967 (define-key map [vc-create-tag]
968 `(menu-item ,(purecopy "Create Tag") vc-create-tag
969 :help ,(purecopy "Create version tag")))
970 (define-key map [separator1] menu-bar-separator)
971 (define-key map [vc-annotate]
972 `(menu-item ,(purecopy "Annotate") vc-annotate
973 :help ,(purecopy "Display the edit history of the current file using colors")))
974 (define-key map [vc-rename-file]
975 `(menu-item ,(purecopy "Rename File") vc-rename-file
976 :help ,(purecopy "Rename file")))
977 (define-key map [vc-revision-other-window]
978 `(menu-item ,(purecopy "Show Other Version") vc-revision-other-window
979 :help ,(purecopy "Visit another version of the current file in another window")))
980 (define-key map [vc-diff]
981 `(menu-item ,(purecopy "Compare with Base Version") vc-diff
982 :help ,(purecopy "Compare file set with the base version")))
983 (define-key map [vc-root-diff]
984 `(menu-item ,(purecopy "Compare Tree with Base Version") vc-root-diff
985 :help ,(purecopy "Compare current tree with the base version")))
986 (define-key map [vc-update-change-log]
987 `(menu-item ,(purecopy "Update ChangeLog") vc-update-change-log
988 :help ,(purecopy "Find change log file and add entries from recent version control logs")))
989 (define-key map [vc-print-log]
990 `(menu-item ,(purecopy "Show History") vc-print-log
991 :help ,(purecopy "List the change log of the current file set in a window")))
992 (define-key map [vc-print-root-log]
993 `(menu-item ,(purecopy "Show Top of the Tree History ") vc-print-root-log
994 :help ,(purecopy "List the change log for the current tree in a window")))
995 (define-key map [separator2] menu-bar-separator)
996 (define-key map [vc-insert-header]
997 `(menu-item ,(purecopy "Insert Header") vc-insert-headers
998 :help ,(purecopy "Insert headers into a file for use with a version control system.
1000 (define-key map [undo]
1001 `(menu-item ,(purecopy "Undo Last Check-In") vc-rollback
1002 :help ,(purecopy "Remove the most recent changeset committed to the repository")))
1003 (define-key map [vc-revert]
1004 `(menu-item ,(purecopy "Revert to Base Version") vc-revert
1005 :help ,(purecopy "Revert working copies of the selected file set to their repository contents")))
1006 (define-key map [vc-update]
1007 `(menu-item ,(purecopy "Update to Latest Version") vc-update
1008 :help ,(purecopy "Update the current fileset's files to their tip revisions")))
1009 (define-key map [vc-next-action]
1010 `(menu-item ,(purecopy "Check In/Out") vc-next-action
1011 :help ,(purecopy "Do the next logical version control operation on the current fileset")))
1012 (define-key map [vc-register]
1013 `(menu-item ,(purecopy "Register") vc-register
1014 :help ,(purecopy "Register file set into a version control system")))
1015 (define-key map [vc-dir]
1016 `(menu-item ,(purecopy "VC Dir") vc-dir
1017 :help ,(purecopy "Show the VC status of files in a directory")))
1020 (defalias 'vc-menu-map vc-menu-map)
1022 (declare-function vc-responsible-backend "vc" (file))
1024 (defun vc-menu-map-filter (orig-binding)
1025 (if (and (symbolp orig-binding) (fboundp orig-binding))
1026 (setq orig-binding (indirect-function orig-binding)))
1030 (if buffer-file-name
1031 (vc-backend buffer-file-name)
1032 (vc-responsible-backend default-directory))
1034 ;; Give the VC backend a chance to add menu entries
1035 ;; specific for that backend.
1036 (if (null ext-binding)
1038 (append orig-binding
1039 '((ext-menu-separator "--"))
1042 (defun vc-default-extra-menu (backend)
1047 ;; arch-tag: 2e5a6fa7-1d30-48e2-8bd0-e3d335f04f32
1048 ;;; vc-hooks.el ends here