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