vc-hooks.el 39 KB

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