authors.el: Add missing ignored and renamed files
[emacs.git] / lisp / vc / vc-hooks.el
blobf3891fc6a292e46f82ca6a916c90c651b5df243a
1 ;;; vc-hooks.el --- resident support for version-control
3 ;; Copyright (C) 1992-1996, 1998-2015 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This is the always-loaded portion of VC. It takes care of
27 ;; VC-related activities that are done when you visit a file, so that
28 ;; vc.el itself is loaded only when you use a VC command. See the
29 ;; commentary of vc.el.
31 ;;; Code:
33 (eval-when-compile (require 'cl-lib))
35 ;; Customization Variables (the rest is in vc.el)
37 (defcustom vc-ignore-dir-regexp
38 ;; Stop SMB, automounter, AFS, and DFS host lookups.
39 locate-dominating-stop-dir-regexp
40 "Regexp matching directory names that are not under VC's control.
41 The default regexp prevents fruitless and time-consuming attempts
42 to determine the VC status in directories in which filenames are
43 interpreted as hostnames."
44 :type 'regexp
45 :group 'vc)
47 (defcustom vc-handled-backends '(RCS CVS SVN SCCS Bzr Git Hg Mtn Arch)
48 ;; RCS, CVS, SVN and SCCS come first because they are per-dir
49 ;; rather than per-tree. RCS comes first because of the multibackend
50 ;; support intended to use RCS for local commits (with a remote CVS server).
51 "List of version control backends for which VC will be used.
52 Entries in this list will be tried in order to determine whether a
53 file is under that sort of version control.
54 Removing an entry from the list prevents VC from being activated
55 when visiting a file managed by that backend.
56 An empty list disables VC altogether."
57 :type '(repeat symbol)
58 :version "23.1"
59 :group 'vc)
61 ;; Note: we don't actually have a darcs back end yet.
62 ;; Also, Meta-CVS (corresponding to MCVS) is unsupported.
63 (defcustom vc-directory-exclusion-list (purecopy '("SCCS" "RCS" "CVS" "MCVS"
64 ".svn" ".git" ".hg" ".bzr"
65 "_MTN" "_darcs" "{arch}"))
66 "List of directory names to be ignored when walking directory trees."
67 :type '(repeat string)
68 :group 'vc)
70 (defcustom vc-make-backup-files nil
71 "If non-nil, backups of registered files are made as with other files.
72 If nil (the default), files covered by version control don't get backups."
73 :type 'boolean
74 :group 'vc
75 :group 'backup)
77 (defcustom vc-follow-symlinks 'ask
78 "What to do if visiting a symbolic link to a file under version control.
79 Editing such a file through the link bypasses the version control system,
80 which is dangerous and probably not what you want.
82 If this variable is t, VC follows the link and visits the real file,
83 telling you about it in the echo area. If it is `ask', VC asks for
84 confirmation whether it should follow the link. If nil, the link is
85 visited and a warning displayed."
86 :type '(choice (const :tag "Ask for confirmation" ask)
87 (const :tag "Visit link and warn" nil)
88 (const :tag "Follow link" t))
89 :group 'vc)
91 (defcustom vc-display-status t
92 "If non-nil, display revision number and lock status in mode line.
93 Otherwise, not displayed."
94 :type 'boolean
95 :group 'vc)
98 (defcustom vc-consult-headers t
99 "If non-nil, identify work files by searching for version headers."
100 :type 'boolean
101 :group 'vc)
103 (defcustom vc-keep-workfiles t
104 "Whether to keep work files on disk after commits, on a locking VCS.
105 This variable has no effect on modern merging-based version
106 control systems."
107 :type 'boolean
108 :group 'vc)
110 ;; If you fix bug#11490, probably you can set this back to nil.
111 (defcustom vc-mistrust-permissions t
112 "If non-nil, don't assume permissions/ownership track version-control status.
113 If nil, do rely on the permissions.
114 See also variable `vc-consult-headers'."
115 :version "24.3" ; nil->t, bug#11490
116 :type 'boolean
117 :group 'vc)
119 (defun vc-mistrust-permissions (file)
120 "Internal access function to variable `vc-mistrust-permissions' for FILE."
121 (or (eq vc-mistrust-permissions 't)
122 (and vc-mistrust-permissions
123 (funcall vc-mistrust-permissions
124 (vc-backend-subdirectory-name file)))))
126 (defcustom vc-stay-local 'only-file
127 "Non-nil means use local operations when possible for remote repositories.
128 This avoids slow queries over the network and instead uses heuristics
129 and past information to determine the current status of a file.
131 If value is the symbol `only-file', `vc-dir' will connect to the
132 server, but heuristics will be used to determine the status for
133 all other VC operations.
135 The value can also be a regular expression or list of regular
136 expressions to match against the host name of a repository; then VC
137 only stays local for hosts that match it. Alternatively, the value
138 can be a list of regular expressions where the first element is the
139 symbol `except'; then VC always stays local except for hosts matched
140 by these regular expressions."
141 :type '(choice
142 (const :tag "Always stay local" t)
143 (const :tag "Only for file operations" only-file)
144 (const :tag "Don't stay local" nil)
145 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
146 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
147 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
148 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
149 :version "23.1"
150 :group 'vc)
152 (defun vc-stay-local-p (file &optional backend)
153 "Return non-nil if VC should stay local when handling FILE.
154 This uses the `repository-hostname' backend operation.
155 If FILE is a list of files, return non-nil if any of them
156 individually should stay local."
157 (if (listp file)
158 (delq nil (mapcar (lambda (arg) (vc-stay-local-p arg backend)) file))
159 (setq backend (or backend (vc-backend file)))
160 (let* ((sym (vc-make-backend-sym backend 'stay-local))
161 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
162 (if (symbolp stay-local) stay-local
163 (let ((dirname (if (file-directory-p file)
164 (directory-file-name file)
165 (file-name-directory file))))
166 (eq 'yes
167 (or (vc-file-getprop dirname 'vc-stay-local-p)
168 (vc-file-setprop
169 dirname 'vc-stay-local-p
170 (let ((hostname (vc-call-backend
171 backend 'repository-hostname dirname)))
172 (if (not hostname)
174 (let ((default t))
175 (if (eq (car-safe stay-local) 'except)
176 (setq default nil stay-local (cdr stay-local)))
177 (when (consp stay-local)
178 (setq stay-local
179 (mapconcat 'identity stay-local "\\|")))
180 (if (if (string-match stay-local hostname)
181 default (not default))
182 'yes 'no))))))))))))
184 ;;; This is handled specially now.
185 ;; Tell Emacs about this new kind of minor mode
186 ;; (add-to-list 'minor-mode-alist '(vc-mode vc-mode))
188 ;;;###autoload
189 (put 'vc-mode 'risky-local-variable t)
190 (make-variable-buffer-local 'vc-mode)
191 (put 'vc-mode 'permanent-local t)
193 (defun vc-mode (&optional _arg)
194 ;; Dummy function for C-h m
195 "Version Control minor mode.
196 This minor mode is automatically activated whenever you visit a file under
197 control of one of the revision control systems in `vc-handled-backends'.
198 VC commands are globally reachable under the prefix `\\[vc-prefix-map]':
199 \\{vc-prefix-map}")
201 (defmacro vc-error-occurred (&rest body)
202 `(condition-case nil (progn ,@body nil) (error t)))
204 ;; We need a notion of per-file properties because the version
205 ;; control state of a file is expensive to derive --- we compute
206 ;; them when the file is initially found, keep them up to date
207 ;; during any subsequent VC operations, and forget them when
208 ;; the buffer is killed.
210 (defvar vc-file-prop-obarray (make-vector 17 0)
211 "Obarray for per-file properties.")
213 (defvar vc-touched-properties nil)
215 (defun vc-file-setprop (file property value)
216 "Set per-file VC PROPERTY for FILE to VALUE."
217 (if (and vc-touched-properties
218 (not (memq property vc-touched-properties)))
219 (setq vc-touched-properties (append (list property)
220 vc-touched-properties)))
221 (put (intern file vc-file-prop-obarray) property value))
223 (defun vc-file-getprop (file property)
224 "Get per-file VC PROPERTY for FILE."
225 (get (intern file vc-file-prop-obarray) property))
227 (defun vc-file-clearprops (file)
228 "Clear all VC properties of FILE."
229 (if (boundp 'vc-parent-buffer)
230 (kill-local-variable 'vc-parent-buffer))
231 (setplist (intern file vc-file-prop-obarray) nil))
234 ;; We keep properties on each symbol naming a backend as follows:
235 ;; * `vc-functions': an alist mapping vc-FUNCTION to vc-BACKEND-FUNCTION.
237 (defun vc-make-backend-sym (backend sym)
238 "Return BACKEND-specific version of VC symbol SYM."
239 (intern (concat "vc-" (downcase (symbol-name backend))
240 "-" (symbol-name sym))))
242 (defun vc-find-backend-function (backend fun)
243 "Return BACKEND-specific implementation of FUN.
244 If there is no such implementation, return the default implementation;
245 if that doesn't exist either, return nil."
246 (let ((f (vc-make-backend-sym backend fun)))
247 (if (fboundp f) f
248 ;; Load vc-BACKEND.el if needed.
249 (require (intern (concat "vc-" (downcase (symbol-name backend)))))
250 (if (fboundp f) f
251 (let ((def (vc-make-backend-sym 'default fun)))
252 (if (fboundp def) (cons def backend) nil))))))
254 (defun vc-call-backend (backend function-name &rest args)
255 "Call for BACKEND the implementation of FUNCTION-NAME with the given ARGS.
256 Calls
258 (apply 'vc-BACKEND-FUN ARGS)
260 if vc-BACKEND-FUN exists (after trying to find it in vc-BACKEND.el)
261 and else calls
263 (apply 'vc-default-FUN BACKEND ARGS)
265 It is usually called via the `vc-call' macro."
266 (let ((f (assoc function-name (get backend 'vc-functions))))
267 (if f (setq f (cdr f))
268 (setq f (vc-find-backend-function backend function-name))
269 (push (cons function-name f) (get backend 'vc-functions)))
270 (cond
271 ((null f)
272 (error "Sorry, %s is not implemented for %s" function-name backend))
273 ((consp f) (apply (car f) (cdr f) args))
274 (t (apply f args)))))
276 (defmacro vc-call (fun file &rest args)
277 "A convenience macro for calling VC backend functions.
278 Functions called by this macro must accept FILE as the first argument.
279 ARGS specifies any additional arguments. FUN should be unquoted.
280 BEWARE!! FILE is evaluated twice!!"
281 `(vc-call-backend (vc-backend ,file) ',fun ,file ,@args))
283 (defsubst vc-parse-buffer (pattern i)
284 "Find PATTERN in the current buffer and return its Ith submatch."
285 (goto-char (point-min))
286 (if (re-search-forward pattern nil t)
287 (match-string i)))
289 (defun vc-insert-file (file &optional limit blocksize)
290 "Insert the contents of FILE into the current buffer.
292 Optional argument LIMIT is a regexp. If present, the file is inserted
293 in chunks of size BLOCKSIZE (default 8 kByte), until the first
294 occurrence of LIMIT is found. Anything from the start of that occurrence
295 to the end of the buffer is then deleted. The function returns
296 non-nil if FILE exists and its contents were successfully inserted."
297 (erase-buffer)
298 (when (file-exists-p file)
299 (if (not limit)
300 (insert-file-contents file)
301 (unless blocksize (setq blocksize 8192))
302 (let ((filepos 0))
303 (while
304 (and (< 0 (cadr (insert-file-contents
305 file nil filepos (cl-incf filepos blocksize))))
306 (progn (beginning-of-line)
307 (let ((pos (re-search-forward limit nil 'move)))
308 (when pos (delete-region (match-beginning 0)
309 (point-max)))
310 (not pos)))))))
311 (set-buffer-modified-p nil)
314 (defun vc-find-root (file witness)
315 "Find the root of a checked out project.
316 The function walks up the directory tree from FILE looking for WITNESS.
317 If WITNESS if not found, return nil, otherwise return the root."
318 (let ((locate-dominating-stop-dir-regexp
319 (or vc-ignore-dir-regexp locate-dominating-stop-dir-regexp)))
320 (locate-dominating-file file witness)))
322 ;; Access functions to file properties
323 ;; (Properties should be _set_ using vc-file-setprop, but
324 ;; _retrieved_ only through these functions, which decide
325 ;; if the property is already known or not. A property should
326 ;; only be retrieved by vc-file-getprop if there is no
327 ;; access function.)
329 ;; properties indicating the backend being used for FILE
331 (defun vc-registered (file)
332 "Return non-nil if FILE is registered in a version control system.
334 This function performs the check each time it is called. To rely
335 on the result of a previous call, use `vc-backend' instead. If the
336 file was previously registered under a certain backend, then that
337 backend is tried first."
338 (let (handler)
339 (cond
340 ((and (file-name-directory file)
341 (string-match vc-ignore-dir-regexp (file-name-directory file)))
342 nil)
343 ((and (boundp 'file-name-handler-alist)
344 (setq handler (find-file-name-handler file 'vc-registered)))
345 ;; handler should set vc-backend and return t if registered
346 (funcall handler 'vc-registered file))
348 ;; There is no file name handler.
349 ;; Try vc-BACKEND-registered for each handled BACKEND.
350 (catch 'found
351 (let ((backend (vc-file-getprop file 'vc-backend)))
352 (mapc
353 (lambda (b)
354 (and (vc-call-backend b 'registered file)
355 (vc-file-setprop file 'vc-backend b)
356 (throw 'found t)))
357 (if (or (not backend) (eq backend 'none))
358 vc-handled-backends
359 (cons backend vc-handled-backends))))
360 ;; File is not registered.
361 (vc-file-setprop file 'vc-backend 'none)
362 nil)))))
364 (defun vc-backend (file-or-list)
365 "Return the version control type of FILE-OR-LIST, nil if it's not registered.
366 If the argument is a list, the files must all have the same back end."
367 ;; `file' can be nil in several places (typically due to the use of
368 ;; code like (vc-backend buffer-file-name)).
369 (cond ((stringp file-or-list)
370 (let ((property (vc-file-getprop file-or-list 'vc-backend)))
371 ;; Note that internally, Emacs remembers unregistered
372 ;; files by setting the property to `none'.
373 (cond ((eq property 'none) nil)
374 (property)
375 ;; vc-registered sets the vc-backend property
376 (t (if (vc-registered file-or-list)
377 (vc-file-getprop file-or-list 'vc-backend)
378 nil)))))
379 ((and file-or-list (listp file-or-list))
380 (vc-backend (car file-or-list)))
382 nil)))
385 (defun vc-backend-subdirectory-name (file)
386 "Return where the repository for the current directory is kept."
387 (symbol-name (vc-backend file)))
389 (defun vc-name (file)
390 "Return the master name of FILE.
391 If the file is not registered, or the master name is not known, return nil."
392 ;; TODO: This should ultimately become obsolete, at least up here
393 ;; in vc-hooks.
394 (or (vc-file-getprop file 'vc-name)
395 ;; force computation of the property by calling
396 ;; vc-BACKEND-registered explicitly
397 (let ((backend (vc-backend file)))
398 (if (and backend
399 (vc-call-backend backend 'registered file))
400 (vc-file-getprop file 'vc-name)))))
402 (defun vc-checkout-model (backend files)
403 "Indicate how FILES are checked out.
405 If FILES are not registered, this function always returns nil.
406 For registered files, the possible values are:
408 'implicit FILES are always writable, and checked out `implicitly'
409 when the user saves the first changes to the file.
411 'locking FILES are read-only if up-to-date; user must type
412 \\[vc-next-action] before editing. Strict locking
413 is assumed.
415 'announce FILES are read-only if up-to-date; user must type
416 \\[vc-next-action] before editing. But other users
417 may be editing at the same time."
418 (vc-call-backend backend 'checkout-model files))
420 (defun vc-user-login-name (file)
421 "Return the name under which the user accesses the given FILE."
422 (or (and (eq (string-match tramp-file-name-regexp file) 0)
423 ;; tramp case: execute "whoami" via tramp
424 (let ((default-directory (file-name-directory file))
425 process-file-side-effects)
426 (with-temp-buffer
427 (if (not (zerop (process-file "whoami" nil t)))
428 ;; fall through if "whoami" didn't work
430 ;; remove trailing newline
431 (delete-region (1- (point-max)) (point-max))
432 (buffer-string)))))
433 ;; normal case
434 (user-login-name)
435 ;; if user-login-name is nil, return the UID as a string
436 (number-to-string (user-uid))))
438 (defun vc-state (file &optional backend)
439 "Return the version control state of FILE.
441 A return of nil from this function means we have no information on the
442 status of this file. Otherwise, the value returned is one of:
444 'up-to-date The working file is unmodified with respect to the
445 latest version on the current branch, and not locked.
447 'edited The working file has been edited by the user. If
448 locking is used for the file, this state means that
449 the current version is locked by the calling user.
450 This status should *not* be reported for files
451 which have a changed mtime but the same content
452 as the repo copy.
454 USER The current version of the working file is locked by
455 some other USER (a string).
457 'needs-update The file has not been edited by the user, but there is
458 a more recent version on the current branch stored
459 in the repository.
461 'needs-merge The file has been edited by the user, and there is also
462 a more recent version on the current branch stored in
463 the repository. This state can only occur if locking
464 is not used for the file.
466 'unlocked-changes The working version of the file is not locked,
467 but the working file has been changed with respect
468 to that version. This state can only occur for files
469 with locking; it represents an erroneous condition that
470 should be resolved by the user (vc-next-action will
471 prompt the user to do it).
473 'added Scheduled to go into the repository on the next commit.
474 Often represented by vc-working-revision = \"0\" in VCSes
475 with monotonic IDs like Subversion and Mercurial.
477 'removed Scheduled to be deleted from the repository on next commit.
479 'conflict The file contains conflicts as the result of a merge.
480 For now the conflicts are text conflicts. In the
481 future this might be extended to deal with metadata
482 conflicts too.
484 'missing The file is not present in the file system, but the VC
485 system still tracks it.
487 'ignored The file showed up in a dir-status listing with a flag
488 indicating the version-control system is ignoring it,
489 Note: This property is not set reliably (some VCSes
490 don't have useful directory-status commands) so assume
491 that any file with vc-state nil might be ignorable
492 without VC knowing it.
494 'unregistered The file is not under version control."
496 ;; Note: in Emacs 22 and older, return of nil meant the file was
497 ;; unregistered. This is potentially a source of
498 ;; backward-compatibility bugs.
500 ;; FIXME: New (sub)states needed (?):
501 ;; - `copied' and `moved' (might be handled by `removed' and `added')
502 (or (vc-file-getprop file 'vc-state)
503 (when (> (length file) 0) ;Why?? --Stef
504 (setq backend (or backend (vc-backend file)))
505 (when backend
506 (vc-state-refresh file backend)))))
508 (defun vc-state-refresh (file backend)
509 "Quickly recompute the `state' of FILE."
510 (vc-file-setprop
511 file 'vc-state
512 (vc-call-backend backend 'state-heuristic file)))
514 (defsubst vc-up-to-date-p (file)
515 "Convenience function that checks whether `vc-state' of FILE is `up-to-date'."
516 (eq (vc-state file) 'up-to-date))
518 (defun vc-default-state-heuristic (backend file)
519 "Default implementation of vc-BACKEND-state-heuristic.
520 It simply calls the real state computation function `vc-BACKEND-state'
521 and does not employ any heuristic at all."
522 (vc-call-backend backend 'state file))
524 (defun vc-workfile-unchanged-p (file)
525 "Return non-nil if FILE has not changed since the last checkout."
526 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
527 (lastmod (nth 5 (file-attributes file))))
528 ;; This is a shortcut for determining when the workfile is
529 ;; unchanged. It can fail under some circumstances; see the
530 ;; discussion in bug#694.
531 (if (and checkout-time
532 ;; Tramp and Ange-FTP return this when they don't know the time.
533 (not (equal lastmod '(0 0))))
534 (equal checkout-time lastmod)
535 (let ((unchanged (vc-call workfile-unchanged-p file)))
536 (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
537 unchanged))))
539 (defun vc-default-workfile-unchanged-p (backend file)
540 "Check if FILE is unchanged by diffing against the repository version.
541 Return non-nil if FILE is unchanged."
542 (zerop (condition-case err
543 ;; If the implementation supports it, let the output
544 ;; go to *vc*, not *vc-diff*, since this is an internal call.
545 (vc-call-backend backend 'diff (list file) nil nil "*vc*")
546 (wrong-number-of-arguments
547 ;; If this error came from the above call to vc-BACKEND-diff,
548 ;; try again without the optional buffer argument (for
549 ;; backward compatibility). Otherwise, resignal.
550 (if (or (not (eq (cadr err)
551 (indirect-function
552 (vc-find-backend-function backend 'diff))))
553 (not (eq (cl-caddr err) 4)))
554 (signal (car err) (cdr err))
555 (vc-call-backend backend 'diff (list file)))))))
557 (defun vc-working-revision (file &optional backend)
558 "Return the repository version from which FILE was checked out.
559 If FILE is not registered, this function always returns nil."
560 (or (vc-file-getprop file 'vc-working-revision)
561 (progn
562 (setq backend (or backend (vc-backend file)))
563 (when backend
564 (vc-file-setprop file 'vc-working-revision
565 (vc-call-backend backend 'working-revision file))))))
567 ;; Backward compatibility.
568 (define-obsolete-function-alias
569 'vc-workfile-version 'vc-working-revision "23.1")
570 (defun vc-default-working-revision (backend file)
571 (message
572 "`working-revision' not found: using the old `workfile-version' instead")
573 (vc-call-backend backend 'workfile-version file))
575 (defun vc-default-registered (backend file)
576 "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates."
577 (let ((sym (vc-make-backend-sym backend 'master-templates)))
578 (unless (get backend 'vc-templates-grabbed)
579 (put backend 'vc-templates-grabbed t))
580 (let ((result (vc-check-master-templates file (symbol-value sym))))
581 (if (stringp result)
582 (vc-file-setprop file 'vc-name result)
583 nil)))) ; Not registered
585 (defun vc-possible-master (s dirname basename)
586 (cond
587 ((stringp s) (format s dirname basename))
588 ((functionp s)
589 ;; The template is a function to invoke. If the
590 ;; function returns non-nil, that means it has found a
591 ;; master. For backward compatibility, we also handle
592 ;; the case that the function throws a 'found atom
593 ;; and a pair (cons MASTER-FILE BACKEND).
594 (let ((result (catch 'found (funcall s dirname basename))))
595 (if (consp result) (car result) result)))))
597 (defun vc-check-master-templates (file templates)
598 "Return non-nil if there is a master corresponding to FILE.
600 TEMPLATES is a list of strings or functions. If an element is a
601 string, it must be a control string as required by `format', with two
602 string placeholders, such as \"%sRCS/%s,v\". The directory part of
603 FILE is substituted for the first placeholder, the basename of FILE
604 for the second. If a file with the resulting name exists, it is taken
605 as the master of FILE, and returned.
607 If an element of TEMPLATES is a function, it is called with the
608 directory part and the basename of FILE as arguments. It should
609 return non-nil if it finds a master; that value is then returned by
610 this function."
611 (let ((dirname (or (file-name-directory file) ""))
612 (basename (file-name-nondirectory file)))
613 (catch 'found
614 (mapcar
615 (lambda (s)
616 (let ((trial (vc-possible-master s dirname basename)))
617 (when (and trial (file-exists-p trial)
618 ;; Make sure the file we found with name
619 ;; TRIAL is not the source file itself.
620 ;; That can happen with RCS-style names if
621 ;; the file name is truncated (e.g. to 14
622 ;; chars). See if either directory or
623 ;; attributes differ.
624 (or (not (string= dirname
625 (file-name-directory trial)))
626 (not (equal (file-attributes file)
627 (file-attributes trial)))))
628 (throw 'found trial))))
629 templates))))
632 ;; toggle-read-only is obsolete since 24.3, but since vc-t-r-o was made
633 ;; obsolete earlier, it is ok for the latter to be an alias to the former,
634 ;; since the latter will be removed first. We can't just make it
635 ;; an alias for read-only-mode, since that is not 100% the same.
636 (defalias 'vc-toggle-read-only 'toggle-read-only)
637 (make-obsolete 'vc-toggle-read-only
638 "use `read-only-mode' instead (or `toggle-read-only' in older versions of Emacs)."
639 "24.1")
641 (defun vc-default-make-version-backups-p (_backend _file)
642 "Return non-nil if unmodified versions should be backed up locally.
643 The default is to switch off this feature."
644 nil)
646 (defun vc-version-backup-file-name (file &optional rev manual regexp)
647 "Return a backup file name for REV or the current version of FILE.
648 If MANUAL is non-nil it means that a name for backups created by
649 the user should be returned; if REGEXP is non-nil that means to return
650 a regexp for matching all such backup files, regardless of the version."
651 (if regexp
652 (concat (regexp-quote (file-name-nondirectory file))
653 "\\.~.+" (unless manual "\\.") "~")
654 (expand-file-name (concat (file-name-nondirectory file)
655 ".~" (subst-char-in-string
656 ?/ ?_ (or rev (vc-working-revision file)))
657 (unless manual ".") "~")
658 (file-name-directory file))))
660 (defun vc-delete-automatic-version-backups (file)
661 "Delete all existing automatic version backups for FILE."
662 (condition-case nil
663 (mapc
664 'delete-file
665 (directory-files (or (file-name-directory file) default-directory) t
666 (vc-version-backup-file-name file nil nil t)))
667 ;; Don't fail when the directory doesn't exist.
668 (file-error nil)))
670 (defun vc-make-version-backup (file)
671 "Make a backup copy of FILE, which is assumed in sync with the repository.
672 Before doing that, check if there are any old backups and get rid of them."
673 (unless (and (fboundp 'msdos-long-file-names)
674 (not (with-no-warnings (msdos-long-file-names))))
675 (vc-delete-automatic-version-backups file)
676 (condition-case nil
677 (copy-file file (vc-version-backup-file-name file)
678 nil 'keep-date)
679 ;; It's ok if it doesn't work (e.g. directory not writable),
680 ;; since this is just for efficiency.
681 (file-error
682 (message
683 (concat "Warning: Cannot make version backup; "
684 "diff/revert therefore not local"))))))
686 (defun vc-before-save ()
687 "Function to be called by `basic-save-buffer' (in files.el)."
688 ;; If the file on disk is still in sync with the repository,
689 ;; and version backups should be made, copy the file to
690 ;; another name. This enables local diffs and local reverting.
691 (let ((file buffer-file-name)
692 backend)
693 (ignore-errors ;Be careful not to prevent saving the file.
694 (unless (file-exists-p file)
695 (vc-file-clearprops file))
696 (and (setq backend (vc-backend file))
697 (vc-up-to-date-p file)
698 (eq (vc-checkout-model backend (list file)) 'implicit)
699 (vc-call-backend backend 'make-version-backups-p file)
700 (vc-make-version-backup file)))))
702 (declare-function vc-dir-resynch-file "vc-dir" (&optional fname))
704 (defvar vc-dir-buffers nil "List of vc-dir buffers.")
706 (defun vc-after-save ()
707 "Function to be called by `basic-save-buffer' (in files.el)."
708 ;; If the file in the current buffer is under version control,
709 ;; up-to-date, and locking is not used for the file, set
710 ;; the state to 'edited and redisplay the mode line.
711 (let* ((file buffer-file-name)
712 (backend (vc-backend file)))
713 (cond
714 ((null backend))
715 ((eq (vc-checkout-model backend (list file)) 'implicit)
716 ;; If the file was saved in the same second in which it was
717 ;; checked out, clear the checkout-time to avoid confusion.
718 (if (equal (vc-file-getprop file 'vc-checkout-time)
719 (nth 5 (file-attributes file)))
720 (vc-file-setprop file 'vc-checkout-time nil))
721 (if (vc-state-refresh file backend)
722 (vc-mode-line file backend)))
723 ;; If we saved an unlocked file on a locking based VCS, that
724 ;; file is not longer up-to-date.
725 ((eq (vc-file-getprop file 'vc-state) 'up-to-date)
726 (vc-file-setprop file 'vc-state nil)))
727 ;; Resynch *vc-dir* buffers, if any are present.
728 (when vc-dir-buffers
729 (vc-dir-resynch-file file))))
731 (defvar vc-menu-entry
732 `(menu-item ,(purecopy "Version Control") vc-menu-map
733 :filter vc-menu-map-filter))
735 (when (boundp 'menu-bar-tools-menu)
736 ;; We do not need to worry here about the placement of this entry
737 ;; because menu-bar.el has already created the proper spot for us
738 ;; and this will simply use it.
739 (define-key menu-bar-tools-menu [vc] vc-menu-entry))
741 (defconst vc-mode-line-map
742 (let ((map (make-sparse-keymap)))
743 (define-key map [mode-line down-mouse-1] vc-menu-entry)
744 map))
746 (defun vc-mode-line (file &optional backend)
747 "Set `vc-mode' to display type of version control for FILE.
748 The value is set in the current buffer, which should be the buffer
749 visiting FILE.
750 If BACKEND is passed use it as the VC backend when computing the result."
751 (interactive (list buffer-file-name))
752 (setq backend (or backend (vc-backend file)))
753 (if (not backend)
754 (setq vc-mode nil)
755 (let* ((ml-string (vc-call-backend backend 'mode-line-string file))
756 (ml-echo (get-text-property 0 'help-echo ml-string)))
757 (setq vc-mode
758 (concat
760 (if (null vc-display-status)
761 (symbol-name backend)
762 (propertize
763 ml-string
764 'mouse-face 'mode-line-highlight
765 'help-echo
766 (concat (or ml-echo
767 (format "File under the %s version control system"
768 backend))
769 "\nmouse-1: Version Control menu")
770 'local-map vc-mode-line-map)))))
771 ;; If the user is root, and the file is not owner-writable,
772 ;; then pretend that we can't write it
773 ;; even though we can (because root can write anything).
774 ;; This way, even root cannot modify a file that isn't locked.
775 (and (equal file buffer-file-name)
776 (not buffer-read-only)
777 (zerop (user-real-uid))
778 (zerop (logand (file-modes buffer-file-name) 128))
779 (setq buffer-read-only t)))
780 (force-mode-line-update)
781 backend)
783 (defun vc-default-mode-line-string (backend file)
784 "Return a string for `vc-mode-line' to put in the mode line for FILE.
785 Format:
787 \"BACKEND-REV\" if the file is up-to-date
788 \"BACKEND:REV\" if the file is edited (or locked by the calling user)
789 \"BACKEND:LOCKER:REV\" if the file is locked by somebody else
790 \"BACKEND@REV\" if the file was locally added
791 \"BACKEND!REV\" if the file contains conflicts or was removed
792 \"BACKEND?REV\" if the file is under VC, but is missing
794 This function assumes that the file is registered."
795 (let* ((backend-name (symbol-name backend))
796 (state (vc-state file backend))
797 (state-echo nil)
798 (rev (vc-working-revision file backend)))
799 (propertize
800 (cond ((or (eq state 'up-to-date)
801 (eq state 'needs-update))
802 (setq state-echo "Up to date file")
803 (concat backend-name "-" rev))
804 ((stringp state)
805 (setq state-echo (concat "File locked by" state))
806 (concat backend-name ":" state ":" rev))
807 ((eq state 'added)
808 (setq state-echo "Locally added file")
809 (concat backend-name "@" rev))
810 ((eq state 'conflict)
811 (setq state-echo "File contains conflicts after the last merge")
812 (concat backend-name "!" rev))
813 ((eq state 'removed)
814 (setq state-echo "File removed from the VC system")
815 (concat backend-name "!" rev))
816 ((eq state 'missing)
817 (setq state-echo "File tracked by the VC system, but missing from the file system")
818 (concat backend-name "?" rev))
820 ;; Not just for the 'edited state, but also a fallback
821 ;; for all other states. Think about different symbols
822 ;; for 'needs-update and 'needs-merge.
823 (setq state-echo "Locally modified file")
824 (concat backend-name ":" rev)))
825 'help-echo (concat state-echo " under the " backend-name
826 " version control system"))))
828 (defun vc-follow-link ()
829 "If current buffer visits a symbolic link, visit the real file.
830 If the real file is already visited in another buffer, make that buffer
831 current, and kill the buffer that visits the link."
832 (let* ((true-buffer (find-buffer-visiting buffer-file-truename))
833 (this-buffer (current-buffer)))
834 (if (eq true-buffer this-buffer)
835 (let ((truename buffer-file-truename))
836 (kill-buffer this-buffer)
837 ;; In principle, we could do something like set-visited-file-name.
838 ;; However, it can't be exactly the same as set-visited-file-name.
839 ;; I'm not going to work out the details right now. -- rms.
840 (set-buffer (find-file-noselect truename)))
841 (set-buffer true-buffer)
842 (kill-buffer this-buffer))))
844 (defun vc-default-find-file-hook (_backend)
845 nil)
847 (defun vc-find-file-hook ()
848 "Function for `find-file-hook' activating VC mode if appropriate."
849 ;; Recompute whether file is version controlled,
850 ;; if user has killed the buffer and revisited.
851 (when vc-mode
852 (setq vc-mode nil))
853 (when buffer-file-name
854 (vc-file-clearprops buffer-file-name)
855 ;; FIXME: Why use a hook? Why pass it buffer-file-name?
856 (add-hook 'vc-mode-line-hook 'vc-mode-line nil t)
857 (let (backend)
858 (cond
859 ((setq backend (with-demoted-errors (vc-backend buffer-file-name)))
860 ;; Compute the state and put it in the mode line.
861 (vc-mode-line buffer-file-name backend)
862 (unless vc-make-backup-files
863 ;; Use this variable, not make-backup-files,
864 ;; because this is for things that depend on the file name.
865 (set (make-local-variable 'backup-inhibited) t))
866 ;; Let the backend setup any buffer-local things he needs.
867 (vc-call-backend backend 'find-file-hook))
868 ((let* ((truename (and buffer-file-truename
869 (expand-file-name buffer-file-truename)))
870 (link-type (and truename
871 (not (equal buffer-file-name truename))
872 (vc-backend truename))))
873 (cond ((not link-type) nil) ;Nothing to do.
874 ((eq vc-follow-symlinks nil)
875 (message
876 "Warning: symbolic link to %s-controlled source file" link-type))
877 ((or (not (eq vc-follow-symlinks 'ask))
878 ;; Assume we cannot ask, default to yes.
879 noninteractive
880 ;; Copied from server-start. Seems like there should
881 ;; be a better way to ask "can we get user input?"...
882 (and (daemonp)
883 (null (cdr (frame-list)))
884 (eq (selected-frame) terminal-frame))
885 ;; If we already visited this file by following
886 ;; the link, don't ask again if we try to visit
887 ;; it again. GUD does that, and repeated questions
888 ;; are painful.
889 (get-file-buffer
890 (abbreviate-file-name
891 (file-chase-links buffer-file-name))))
893 (vc-follow-link)
894 (message "Followed link to %s" buffer-file-name)
895 (vc-find-file-hook))
897 (if (yes-or-no-p (format
898 "Symbolic link to %s-controlled source file; follow link? " link-type))
899 (progn (vc-follow-link)
900 (message "Followed link to %s" buffer-file-name)
901 (vc-find-file-hook))
902 (message
903 "Warning: editing through the link bypasses version control")
904 )))))))))
906 (add-hook 'find-file-hook 'vc-find-file-hook)
908 (defun vc-kill-buffer-hook ()
909 "Discard VC info about a file when we kill its buffer."
910 (when buffer-file-name (vc-file-clearprops buffer-file-name)))
912 (add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
914 ;; Now arrange for (autoloaded) bindings of the main package.
915 ;; Bindings for this have to go in the global map, as we'll often
916 ;; want to call them from random buffers.
918 ;; Autoloading works fine, but it prevents shortcuts from appearing
919 ;; in the menu because they don't exist yet when the menu is built.
920 ;; (autoload 'vc-prefix-map "vc" nil nil 'keymap)
921 (defvar vc-prefix-map
922 (let ((map (make-sparse-keymap)))
923 (define-key map "a" 'vc-update-change-log)
924 (define-key map "b" 'vc-switch-backend)
925 (define-key map "c" 'vc-rollback)
926 (define-key map "d" 'vc-dir)
927 (define-key map "g" 'vc-annotate)
928 (define-key map "G" 'vc-ignore)
929 (define-key map "h" 'vc-insert-headers)
930 (define-key map "i" 'vc-register)
931 (define-key map "l" 'vc-print-log)
932 (define-key map "L" 'vc-print-root-log)
933 (define-key map "I" 'vc-log-incoming)
934 (define-key map "O" 'vc-log-outgoing)
935 (define-key map "m" 'vc-merge)
936 (define-key map "r" 'vc-retrieve-tag)
937 (define-key map "s" 'vc-create-tag)
938 (define-key map "u" 'vc-revert)
939 (define-key map "v" 'vc-next-action)
940 (define-key map "+" 'vc-update)
941 (define-key map "=" 'vc-diff)
942 (define-key map "D" 'vc-root-diff)
943 (define-key map "~" 'vc-revision-other-window)
944 map))
945 (fset 'vc-prefix-map vc-prefix-map)
946 (define-key ctl-x-map "v" 'vc-prefix-map)
948 (defvar vc-menu-map
949 (let ((map (make-sparse-keymap "Version Control")))
950 ;;(define-key map [show-files]
951 ;; '("Show Files under VC" . (vc-directory t)))
952 (bindings--define-key map [vc-retrieve-tag]
953 '(menu-item "Retrieve Tag" vc-retrieve-tag
954 :help "Retrieve tagged version or branch"))
955 (bindings--define-key map [vc-create-tag]
956 '(menu-item "Create Tag" vc-create-tag
957 :help "Create version tag"))
958 (bindings--define-key map [separator1] menu-bar-separator)
959 (bindings--define-key map [vc-annotate]
960 '(menu-item "Annotate" vc-annotate
961 :help "Display the edit history of the current file using colors"))
962 (bindings--define-key map [vc-rename-file]
963 '(menu-item "Rename File" vc-rename-file
964 :help "Rename file"))
965 (bindings--define-key map [vc-revision-other-window]
966 '(menu-item "Show Other Version" vc-revision-other-window
967 :help "Visit another version of the current file in another window"))
968 (bindings--define-key map [vc-diff]
969 '(menu-item "Compare with Base Version" vc-diff
970 :help "Compare file set with the base version"))
971 (bindings--define-key map [vc-root-diff]
972 '(menu-item "Compare Tree with Base Version" vc-root-diff
973 :help "Compare current tree with the base version"))
974 (bindings--define-key map [vc-update-change-log]
975 '(menu-item "Update ChangeLog" vc-update-change-log
976 :help "Find change log file and add entries from recent version control logs"))
977 (bindings--define-key map [vc-log-out]
978 '(menu-item "Show Outgoing Log" vc-log-outgoing
979 :help "Show a log of changes that will be sent with a push operation"))
980 (bindings--define-key map [vc-log-in]
981 '(menu-item "Show Incoming Log" vc-log-incoming
982 :help "Show a log of changes that will be received with a pull operation"))
983 (bindings--define-key map [vc-print-log]
984 '(menu-item "Show History" vc-print-log
985 :help "List the change log of the current file set in a window"))
986 (bindings--define-key map [vc-print-root-log]
987 '(menu-item "Show Top of the Tree History " vc-print-root-log
988 :help "List the change log for the current tree in a window"))
989 (bindings--define-key map [separator2] menu-bar-separator)
990 (bindings--define-key map [vc-insert-header]
991 '(menu-item "Insert Header" vc-insert-headers
992 :help "Insert headers into a file for use with a version control system.
994 (bindings--define-key map [undo]
995 '(menu-item "Undo Last Check-In" vc-rollback
996 :enable (let ((backend (if buffer-file-name
997 (vc-backend buffer-file-name))))
998 (or (not backend)
999 (vc-find-backend-function backend 'rollback)))
1000 :help "Remove the most recent changeset committed to the repository"))
1001 (bindings--define-key map [vc-revert]
1002 '(menu-item "Revert to Base Version" vc-revert
1003 :help "Revert working copies of the selected file set to their repository contents"))
1004 (bindings--define-key map [vc-update]
1005 '(menu-item "Update to Latest Version" vc-update
1006 :help "Update the current fileset's files to their tip revisions"))
1007 (bindings--define-key map [vc-next-action]
1008 '(menu-item "Check In/Out" vc-next-action
1009 :help "Do the next logical version control operation on the current fileset"))
1010 (bindings--define-key map [vc-register]
1011 '(menu-item "Register" vc-register
1012 :help "Register file set into a version control system"))
1013 (bindings--define-key map [vc-ignore]
1014 '(menu-item "Ignore File..." vc-ignore
1015 :help "Ignore a file under current version control system"))
1016 (bindings--define-key map [vc-dir]
1017 '(menu-item "VC Dir" vc-dir
1018 :help "Show the VC status of files in a directory"))
1019 map))
1021 (defalias 'vc-menu-map vc-menu-map)
1023 (declare-function vc-responsible-backend "vc" (file))
1025 (defun vc-menu-map-filter (orig-binding)
1026 (if (and (symbolp orig-binding) (fboundp orig-binding))
1027 (setq orig-binding (indirect-function orig-binding)))
1028 (let ((ext-binding
1029 (when vc-mode
1030 (vc-call-backend
1031 (if buffer-file-name
1032 (vc-backend buffer-file-name)
1033 (vc-responsible-backend default-directory))
1034 'extra-menu))))
1035 ;; Give the VC backend a chance to add menu entries
1036 ;; specific for that backend.
1037 (if (null ext-binding)
1038 orig-binding
1039 (append orig-binding
1040 '((ext-menu-separator "--"))
1041 ext-binding))))
1043 (defun vc-default-extra-menu (_backend)
1044 nil)
1046 (provide 'vc-hooks)
1048 ;;; vc-hooks.el ends here