vc-hooks.el 42 KB

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