vc.el 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. ;;; vc.el --- drive a version-control system from within Emacs
  2. ;; Copyright (C) 1992-1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: FSF (see below for full credits)
  4. ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
  5. ;; Keywords: vc tools
  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. ;;; Credits:
  18. ;; VC was initially designed and implemented by Eric S. Raymond
  19. ;; <esr@thyrsus.com> in 1992. Over the years, many other people have
  20. ;; contributed substantial amounts of work to VC. These include:
  21. ;;
  22. ;; Per Cederqvist <ceder@lysator.liu.se>
  23. ;; Paul Eggert <eggert@twinsun.com>
  24. ;; Sebastian Kremer <sk@thp.uni-koeln.de>
  25. ;; Martin Lorentzson <martinl@gnu.org>
  26. ;; Dave Love <fx@gnu.org>
  27. ;; Stefan Monnier <monnier@cs.yale.edu>
  28. ;; Thien-Thi Nguyen <ttn@gnu.org>
  29. ;; Dan Nicolaescu <dann@ics.uci.edu>
  30. ;; J.D. Smith <jdsmith@alum.mit.edu>
  31. ;; Andre Spiegel <spiegel@gnu.org>
  32. ;; Richard Stallman <rms@gnu.org>
  33. ;;
  34. ;; In July 2007 ESR returned and redesigned the mode to cope better
  35. ;; with modern version-control systems that do commits by fileset
  36. ;; rather than per individual file.
  37. ;;
  38. ;; If you maintain a client of the mode or customize it in your .emacs,
  39. ;; note that some backend functions which formerly took single file arguments
  40. ;; now take a list of files. These include: register, checkin, print-log,
  41. ;; rollback, and diff.
  42. ;;; Commentary:
  43. ;; This mode is fully documented in the Emacs user's manual.
  44. ;;
  45. ;; Supported version-control systems presently include CVS, RCS, GNU
  46. ;; Arch, Subversion, Bzr, Git, Mercurial, Monotone and SCCS
  47. ;; (or its free replacement, CSSC).
  48. ;;
  49. ;; If your site uses the ChangeLog convention supported by Emacs, the
  50. ;; function `log-edit-comment-to-change-log' could prove a useful checkin hook,
  51. ;; although you might prefer to use C-c C-a (i.e. `log-edit-insert-changelog')
  52. ;; from the commit buffer instead or to set `log-edit-setup-invert'.
  53. ;;
  54. ;; When using SCCS, RCS, CVS: be careful not to do repo surgery, or
  55. ;; operations like registrations and deletions and renames, outside VC
  56. ;; while VC is running. The support for these systems was designed
  57. ;; when disks were much slower, and the code maintains a lot of
  58. ;; internal state in order to reduce expensive operations to a
  59. ;; minimum. Thus, if you mess with the repo while VC's back is turned,
  60. ;; VC may get seriously confused.
  61. ;;
  62. ;; When using Subversion or a later system, anything you do outside VC
  63. ;; *through the VCS tools* should safely interlock with VC
  64. ;; operations. Under these VC does little state caching, because local
  65. ;; operations are assumed to be fast. The dividing line is
  66. ;;
  67. ;; ADDING SUPPORT FOR OTHER BACKENDS
  68. ;;
  69. ;; VC can use arbitrary version control systems as a backend. To add
  70. ;; support for a new backend named SYS, write a library vc-sys.el that
  71. ;; contains functions of the form `vc-sys-...' (note that SYS is in lower
  72. ;; case for the function and library names). VC will use that library if
  73. ;; you put the symbol SYS somewhere into the list of
  74. ;; `vc-handled-backends'. Then, for example, if `vc-sys-registered'
  75. ;; returns non-nil for a file, all SYS-specific versions of VC commands
  76. ;; will be available for that file.
  77. ;;
  78. ;; VC keeps some per-file information in the form of properties (see
  79. ;; vc-file-set/getprop in vc-hooks.el). The backend-specific functions
  80. ;; do not generally need to be aware of these properties. For example,
  81. ;; `vc-sys-working-revision' should compute the working revision and
  82. ;; return it; it should not look it up in the property, and it needn't
  83. ;; store it there either. However, if a backend-specific function does
  84. ;; store a value in a property, that value takes precedence over any
  85. ;; value that the generic code might want to set (check for uses of
  86. ;; the macro `with-vc-properties' in vc.el).
  87. ;;
  88. ;; In the list of functions below, each identifier needs to be prepended
  89. ;; with `vc-sys-'. Some of the functions are mandatory (marked with a
  90. ;; `*'), others are optional (`-').
  91. ;; BACKEND PROPERTIES
  92. ;;
  93. ;; * revision-granularity
  94. ;;
  95. ;; Takes no arguments. Returns either 'file or 'repository. Backends
  96. ;; that return 'file have per-file revision numbering; backends
  97. ;; that return 'repository have per-repository revision numbering,
  98. ;; so a revision level implicitly identifies a changeset
  99. ;; STATE-QUERYING FUNCTIONS
  100. ;;
  101. ;; * registered (file)
  102. ;;
  103. ;; Return non-nil if FILE is registered in this backend. Both this
  104. ;; function as well as `state' should be careful to fail gracefully
  105. ;; in the event that the backend executable is absent. It is
  106. ;; preferable that this function's body is autoloaded, that way only
  107. ;; calling vc-registered does not cause the backend to be loaded
  108. ;; (all the vc-FOO-registered functions are called to try to find
  109. ;; the controlling backend for FILE.
  110. ;;
  111. ;; * state (file)
  112. ;;
  113. ;; Return the current version control state of FILE. For a list of
  114. ;; possible values, see `vc-state'. This function should do a full and
  115. ;; reliable state computation; it is usually called immediately after
  116. ;; C-x v v. If you want to use a faster heuristic when visiting a
  117. ;; file, put that into `state-heuristic' below. Note that under most
  118. ;; VCSes this won't be called at all, dir-status is used instead.
  119. ;;
  120. ;; - state-heuristic (file)
  121. ;;
  122. ;; If provided, this function is used to estimate the version control
  123. ;; state of FILE at visiting time. It should be considerably faster
  124. ;; than the implementation of `state'. For a list of possible values,
  125. ;; see the doc string of `vc-state'.
  126. ;;
  127. ;; - dir-status (dir update-function)
  128. ;;
  129. ;; Produce RESULT: a list of lists of the form (FILE VC-STATE EXTRA)
  130. ;; for the files in DIR.
  131. ;; EXTRA can be used for backend specific information about FILE.
  132. ;; If a command needs to be run to compute this list, it should be
  133. ;; run asynchronously using (current-buffer) as the buffer for the
  134. ;; command. When RESULT is computed, it should be passed back by
  135. ;; doing: (funcall UPDATE-FUNCTION RESULT nil).
  136. ;; If the backend uses a process filter, hence it produces partial results,
  137. ;; they can be passed back by doing:
  138. ;; (funcall UPDATE-FUNCTION RESULT t)
  139. ;; and then do a (funcall UPDATE-FUNCTION RESULT nil)
  140. ;; when all the results have been computed.
  141. ;; To provide more backend specific functionality for `vc-dir'
  142. ;; the following functions might be needed: `dir-extra-headers',
  143. ;; `dir-printer', `extra-dir-menu' and `dir-status-files'.
  144. ;;
  145. ;; - dir-status-files (dir files default-state update-function)
  146. ;;
  147. ;; This function is identical to dir-status except that it should
  148. ;; only report status for the specified FILES. Also it needs to
  149. ;; report on all requested files, including up-to-date or ignored
  150. ;; files. If not provided, the default is to consider that the files
  151. ;; are in DEFAULT-STATE.
  152. ;;
  153. ;; - dir-extra-headers (dir)
  154. ;;
  155. ;; Return a string that will be added to the *vc-dir* buffer header.
  156. ;;
  157. ;; - dir-printer (fileinfo)
  158. ;;
  159. ;; Pretty print the `vc-dir-fileinfo' FILEINFO.
  160. ;; If a backend needs to show more information than the default FILE
  161. ;; and STATE in the vc-dir listing, it can store that extra
  162. ;; information in `vc-dir-fileinfo->extra'. This function can be
  163. ;; used to display that extra information in the *vc-dir* buffer.
  164. ;;
  165. ;; - status-fileinfo-extra (file)
  166. ;;
  167. ;; Compute `vc-dir-fileinfo->extra' for FILE.
  168. ;;
  169. ;; * working-revision (file)
  170. ;;
  171. ;; Return the working revision of FILE. This is the revision fetched
  172. ;; by the last checkout or update, not necessarily the same thing as the
  173. ;; head or tip revision. Should return "0" for a file added but not yet
  174. ;; committed.
  175. ;;
  176. ;; - latest-on-branch-p (file)
  177. ;;
  178. ;; Return non-nil if the working revision of FILE is the latest revision
  179. ;; on its branch (many VCSes call this the 'tip' or 'head' revision).
  180. ;; The default implementation always returns t, which means that
  181. ;; working with non-current revisions is not supported by default.
  182. ;;
  183. ;; * checkout-model (files)
  184. ;;
  185. ;; Indicate whether FILES need to be "checked out" before they can be
  186. ;; edited. See `vc-checkout-model' for a list of possible values.
  187. ;;
  188. ;; - workfile-unchanged-p (file)
  189. ;;
  190. ;; Return non-nil if FILE is unchanged from the working revision.
  191. ;; This function should do a brief comparison of FILE's contents
  192. ;; with those of the repository copy of the working revision. If
  193. ;; the backend does not have such a brief-comparison feature, the
  194. ;; default implementation of this function can be used, which
  195. ;; delegates to a full vc-BACKEND-diff. (Note that vc-BACKEND-diff
  196. ;; must not run asynchronously in this case, see variable
  197. ;; `vc-disable-async-diff'.)
  198. ;;
  199. ;; - mode-line-string (file)
  200. ;;
  201. ;; If provided, this function should return the VC-specific mode
  202. ;; line string for FILE. The returned string should have a
  203. ;; `help-echo' property which is the text to be displayed as a
  204. ;; tooltip when the mouse hovers over the VC entry on the mode-line.
  205. ;; The default implementation deals well with all states that
  206. ;; `vc-state' can return.
  207. ;;
  208. ;; STATE-CHANGING FUNCTIONS
  209. ;;
  210. ;; * create-repo (backend)
  211. ;;
  212. ;; Create an empty repository in the current directory and initialize
  213. ;; it so VC mode can add files to it. For file-oriented systems, this
  214. ;; need do no more than create a subdirectory with the right name.
  215. ;;
  216. ;; * register (files &optional rev comment)
  217. ;;
  218. ;; Register FILES in this backend. Optionally, an initial revision REV
  219. ;; and an initial description of the file, COMMENT, may be specified,
  220. ;; but it is not guaranteed that the backend will do anything with this.
  221. ;; The implementation should pass the value of vc-register-switches
  222. ;; to the backend command. (Note: in older versions of VC, this
  223. ;; command took a single file argument and not a list.)
  224. ;;
  225. ;; - init-revision (file)
  226. ;;
  227. ;; The initial revision to use when registering FILE if one is not
  228. ;; specified by the user. If not provided, the variable
  229. ;; vc-default-init-revision is used instead.
  230. ;;
  231. ;; - responsible-p (file)
  232. ;;
  233. ;; Return non-nil if this backend considers itself "responsible" for
  234. ;; FILE, which can also be a directory. This function is used to find
  235. ;; out what backend to use for registration of new files and for things
  236. ;; like change log generation. The default implementation always
  237. ;; returns nil.
  238. ;;
  239. ;; - could-register (file)
  240. ;;
  241. ;; Return non-nil if FILE could be registered under this backend. The
  242. ;; default implementation always returns t.
  243. ;;
  244. ;; - receive-file (file rev)
  245. ;;
  246. ;; Let this backend "receive" a file that is already registered under
  247. ;; another backend. The default implementation simply calls `register'
  248. ;; for FILE, but it can be overridden to do something more specific,
  249. ;; e.g. keep revision numbers consistent or choose editing modes for
  250. ;; FILE that resemble those of the other backend.
  251. ;;
  252. ;; - unregister (file)
  253. ;;
  254. ;; Unregister FILE from this backend. This is only needed if this
  255. ;; backend may be used as a "more local" backend for temporary editing.
  256. ;;
  257. ;; * checkin (files rev comment)
  258. ;;
  259. ;; Commit changes in FILES to this backend. REV is a historical artifact
  260. ;; and should be ignored. COMMENT is used as a check-in comment.
  261. ;; The implementation should pass the value of vc-checkin-switches to
  262. ;; the backend command.
  263. ;;
  264. ;; * find-revision (file rev buffer)
  265. ;;
  266. ;; Fetch revision REV of file FILE and put it into BUFFER.
  267. ;; If REV is the empty string, fetch the head of the trunk.
  268. ;; The implementation should pass the value of vc-checkout-switches
  269. ;; to the backend command.
  270. ;;
  271. ;; * checkout (file &optional editable rev)
  272. ;;
  273. ;; Check out revision REV of FILE into the working area. If EDITABLE
  274. ;; is non-nil, FILE should be writable by the user and if locking is
  275. ;; used for FILE, a lock should also be set. If REV is non-nil, that
  276. ;; is the revision to check out (default is the working revision).
  277. ;; If REV is t, that means to check out the head of the current branch;
  278. ;; if it is the empty string, check out the head of the trunk.
  279. ;; The implementation should pass the value of vc-checkout-switches
  280. ;; to the backend command.
  281. ;;
  282. ;; * revert (file &optional contents-done)
  283. ;;
  284. ;; Revert FILE back to the working revision. If optional
  285. ;; arg CONTENTS-DONE is non-nil, then the contents of FILE have
  286. ;; already been reverted from a version backup, and this function
  287. ;; only needs to update the status of FILE within the backend.
  288. ;; If FILE is in the `added' state it should be returned to the
  289. ;; `unregistered' state.
  290. ;;
  291. ;; - rollback (files)
  292. ;;
  293. ;; Remove the tip revision of each of FILES from the repository. If
  294. ;; this function is not provided, trying to cancel a revision is
  295. ;; caught as an error. (Most backends don't provide it.) (Also
  296. ;; note that older versions of this backend command were called
  297. ;; 'cancel-version' and took a single file arg, not a list of
  298. ;; files.)
  299. ;;
  300. ;; - merge (file rev1 rev2)
  301. ;;
  302. ;; Merge the changes between REV1 and REV2 into the current working file
  303. ;; (for non-distributed VCS).
  304. ;;
  305. ;; - merge-branch ()
  306. ;;
  307. ;; Merge another branch into the current one, prompting for a
  308. ;; location to merge from.
  309. ;;
  310. ;; - merge-news (file)
  311. ;;
  312. ;; Merge recent changes from the current branch into FILE.
  313. ;; (for non-distributed VCS).
  314. ;;
  315. ;; - pull (prompt)
  316. ;;
  317. ;; Pull "upstream" changes into the current branch (for distributed
  318. ;; VCS). If PROMPT is non-nil, or if necessary, prompt for a
  319. ;; location to pull from.
  320. ;;
  321. ;; - steal-lock (file &optional revision)
  322. ;;
  323. ;; Steal any lock on the working revision of FILE, or on REVISION if
  324. ;; that is provided. This function is only needed if locking is
  325. ;; used for files under this backend, and if files can indeed be
  326. ;; locked by other users.
  327. ;;
  328. ;; - modify-change-comment (files rev comment)
  329. ;;
  330. ;; Modify the change comments associated with the files at the
  331. ;; given revision. This is optional, many backends do not support it.
  332. ;;
  333. ;; - mark-resolved (files)
  334. ;;
  335. ;; Mark conflicts as resolved. Some VC systems need to run a
  336. ;; command to mark conflicts as resolved.
  337. ;; HISTORY FUNCTIONS
  338. ;;
  339. ;; * print-log (files buffer &optional shortlog start-revision limit)
  340. ;;
  341. ;; Insert the revision log for FILES into BUFFER.
  342. ;; If SHORTLOG is true insert a short version of the log.
  343. ;; If LIMIT is true insert only insert LIMIT log entries. If the
  344. ;; backend does not support limiting the number of entries to show
  345. ;; it should return `limit-unsupported'.
  346. ;; If START-REVISION is given, then show the log starting from the
  347. ;; revision. At this point START-REVISION is only required to work
  348. ;; in conjunction with LIMIT = 1.
  349. ;;
  350. ;; * log-outgoing (backend remote-location)
  351. ;;
  352. ;; Insert in BUFFER the revision log for the changes that will be
  353. ;; sent when performing a push operation to REMOTE-LOCATION.
  354. ;;
  355. ;; * log-incoming (backend remote-location)
  356. ;;
  357. ;; Insert in BUFFER the revision log for the changes that will be
  358. ;; received when performing a pull operation from REMOTE-LOCATION.
  359. ;;
  360. ;; - log-view-mode ()
  361. ;;
  362. ;; Mode to use for the output of print-log. This defaults to
  363. ;; `log-view-mode' and is expected to be changed (if at all) to a derived
  364. ;; mode of `log-view-mode'.
  365. ;;
  366. ;; - show-log-entry (revision)
  367. ;;
  368. ;; If provided, search the log entry for REVISION in the current buffer,
  369. ;; and make sure it is displayed in the buffer's window. The default
  370. ;; implementation of this function works for RCS-style logs.
  371. ;;
  372. ;; - comment-history (file)
  373. ;;
  374. ;; Return a string containing all log entries that were made for FILE.
  375. ;; This is used for transferring a file from one backend to another,
  376. ;; retaining comment information.
  377. ;;
  378. ;; - update-changelog (files)
  379. ;;
  380. ;; Using recent log entries, create ChangeLog entries for FILES, or for
  381. ;; all files at or below the default-directory if FILES is nil. The
  382. ;; default implementation runs rcs2log, which handles RCS- and
  383. ;; CVS-style logs.
  384. ;;
  385. ;; * diff (files &optional rev1 rev2 buffer)
  386. ;;
  387. ;; Insert the diff for FILE into BUFFER, or the *vc-diff* buffer if
  388. ;; BUFFER is nil. If REV1 and REV2 are non-nil, report differences
  389. ;; from REV1 to REV2. If REV1 is nil, use the working revision (as
  390. ;; found in the repository) as the older revision; if REV2 is nil,
  391. ;; use the current working-copy contents as the newer revision. This
  392. ;; function should pass the value of (vc-switches BACKEND 'diff) to
  393. ;; the backend command. It should return a status of either 0 (no
  394. ;; differences found), or 1 (either non-empty diff or the diff is
  395. ;; run asynchronously).
  396. ;;
  397. ;; - revision-completion-table (files)
  398. ;;
  399. ;; Return a completion table for existing revisions of FILES.
  400. ;; The default is to not use any completion table.
  401. ;;
  402. ;; - annotate-command (file buf &optional rev)
  403. ;;
  404. ;; If this function is provided, it should produce an annotated display
  405. ;; of FILE in BUF, relative to revision REV. Annotation means each line
  406. ;; of FILE displayed is prefixed with version information associated with
  407. ;; its addition (deleted lines leave no history) and that the text of the
  408. ;; file is fontified according to age.
  409. ;;
  410. ;; - annotate-time ()
  411. ;;
  412. ;; Only required if `annotate-command' is defined for the backend.
  413. ;; Return the time of the next line of annotation at or after point,
  414. ;; as a floating point fractional number of days. The helper
  415. ;; function `vc-annotate-convert-time' may be useful for converting
  416. ;; multi-part times as returned by `current-time' and `encode-time'
  417. ;; to this format. Return nil if no more lines of annotation appear
  418. ;; in the buffer. You can safely assume that point is placed at the
  419. ;; beginning of each line, starting at `point-min'. The buffer that
  420. ;; point is placed in is the Annotate output, as defined by the
  421. ;; relevant backend. This function also affects how much of the line
  422. ;; is fontified; where it leaves point is where fontification begins.
  423. ;;
  424. ;; - annotate-current-time ()
  425. ;;
  426. ;; Only required if `annotate-command' is defined for the backend,
  427. ;; AND you'd like the current time considered to be anything besides
  428. ;; (vc-annotate-convert-time (current-time)) -- i.e. the current
  429. ;; time with hours, minutes, and seconds included. Probably safe to
  430. ;; ignore. Return the current-time, in units of fractional days.
  431. ;;
  432. ;; - annotate-extract-revision-at-line ()
  433. ;;
  434. ;; Only required if `annotate-command' is defined for the backend.
  435. ;; Invoked from a buffer in vc-annotate-mode, return the revision
  436. ;; corresponding to the current line, or nil if there is no revision
  437. ;; corresponding to the current line.
  438. ;; If the backend supports annotating through copies and renames,
  439. ;; and displays a file name and a revision, then return a cons
  440. ;; (REVISION . FILENAME).
  441. ;; TAG SYSTEM
  442. ;;
  443. ;; - create-tag (dir name branchp)
  444. ;;
  445. ;; Attach the tag NAME to the state of the working copy. This
  446. ;; should make sure that files are up-to-date before proceeding with
  447. ;; the action. DIR can also be a file and if BRANCHP is specified,
  448. ;; NAME should be created as a branch and DIR should be checked out
  449. ;; under this new branch. The default implementation does not
  450. ;; support branches but does a sanity check, a tree traversal and
  451. ;; assigns the tag to each file.
  452. ;;
  453. ;; - retrieve-tag (dir name update)
  454. ;;
  455. ;; Retrieve the version tagged by NAME of all registered files at or below DIR.
  456. ;; If UPDATE is non-nil, then update buffers of any files in the
  457. ;; tag that are currently visited. The default implementation
  458. ;; does a sanity check whether there aren't any uncommitted changes at
  459. ;; or below DIR, and then performs a tree walk, using the `checkout'
  460. ;; function to retrieve the corresponding revisions.
  461. ;; MISCELLANEOUS
  462. ;;
  463. ;; - make-version-backups-p (file)
  464. ;;
  465. ;; Return non-nil if unmodified repository revisions of FILE should be
  466. ;; backed up locally. If this is done, VC can perform `diff' and
  467. ;; `revert' operations itself, without calling the backend system. The
  468. ;; default implementation always returns nil.
  469. ;;
  470. ;; - root (file)
  471. ;; Return the root of the VC controlled hierarchy for file.
  472. ;;
  473. ;; - repository-hostname (dirname)
  474. ;;
  475. ;; Return the hostname that the backend will have to contact
  476. ;; in order to operate on a file in DIRNAME. If the return value
  477. ;; is nil, it means that the repository is local.
  478. ;; This function is used in `vc-stay-local-p' which backends can use
  479. ;; for their convenience.
  480. ;;
  481. ;; - previous-revision (file rev)
  482. ;;
  483. ;; Return the revision number that precedes REV for FILE, or nil if no such
  484. ;; revision exists.
  485. ;;
  486. ;; - next-revision (file rev)
  487. ;;
  488. ;; Return the revision number that follows REV for FILE, or nil if no such
  489. ;; revision exists.
  490. ;;
  491. ;; - log-edit-mode ()
  492. ;;
  493. ;; Turn on the mode used for editing the check in log. This
  494. ;; defaults to `log-edit-mode'. If changed, it should use a mode
  495. ;; derived from`log-edit-mode'.
  496. ;;
  497. ;; - check-headers ()
  498. ;;
  499. ;; Return non-nil if the current buffer contains any version headers.
  500. ;;
  501. ;; - clear-headers ()
  502. ;;
  503. ;; In the current buffer, reset all version headers to their unexpanded
  504. ;; form. This function should be provided if the state-querying code
  505. ;; for this backend uses the version headers to determine the state of
  506. ;; a file. This function will then be called whenever VC changes the
  507. ;; version control state in such a way that the headers would give
  508. ;; wrong information.
  509. ;;
  510. ;; - delete-file (file)
  511. ;;
  512. ;; Delete FILE and mark it as deleted in the repository. If this
  513. ;; function is not provided, the command `vc-delete-file' will
  514. ;; signal an error.
  515. ;;
  516. ;; - rename-file (old new)
  517. ;;
  518. ;; Rename file OLD to NEW, both in the working area and in the
  519. ;; repository. If this function is not provided, the renaming
  520. ;; will be done by (vc-delete-file old) and (vc-register new).
  521. ;;
  522. ;; - find-file-hook ()
  523. ;;
  524. ;; Operation called in current buffer when opening a file. This can
  525. ;; be used by the backend to setup some local variables it might need.
  526. ;;
  527. ;; - extra-menu ()
  528. ;;
  529. ;; Return a menu keymap, the items in the keymap will appear at the
  530. ;; end of the Version Control menu. The goal is to allow backends
  531. ;; to specify extra menu items that appear in the VC menu. This way
  532. ;; you can provide menu entries for functionality that is specific
  533. ;; to your backend and which does not map to any of the VC generic
  534. ;; concepts.
  535. ;;
  536. ;; - extra-dir-menu ()
  537. ;;
  538. ;; Return a menu keymap, the items in the keymap will appear at the
  539. ;; end of the VC Status menu. The goal is to allow backends to
  540. ;; specify extra menu items that appear in the VC Status menu. This
  541. ;; makes it possible to provide menu entries for functionality that
  542. ;; is specific to a backend and which does not map to any of the VC
  543. ;; generic concepts.
  544. ;;
  545. ;; - conflicted-files (dir)
  546. ;;
  547. ;; Return the list of files where conflict resolution is needed in
  548. ;; the project that contains DIR.
  549. ;; FIXME: what should it do with non-text conflicts?
  550. ;;; Todo:
  551. ;; - Get rid of the "master file" terminology.
  552. ;; - Add key-binding for vc-delete-file.
  553. ;;;; New Primitives:
  554. ;;
  555. ;; - deal with push/pull operations.
  556. ;;
  557. ;; - add a mechanism for editing the underlying VCS's list of files
  558. ;; to be ignored, when that's possible.
  559. ;;
  560. ;;;; Primitives that need changing:
  561. ;;
  562. ;; - vc-update/vc-merge should deal with VC systems that don't
  563. ;; update/merge on a file basis, but on a whole repository basis.
  564. ;; vc-update and vc-merge assume the arguments are always files,
  565. ;; they don't deal with directories. Make sure the *vc-dir* buffer
  566. ;; is updated after these operations.
  567. ;; At least bzr, git and hg should benefit from this.
  568. ;;
  569. ;;;; Improved branch and tag handling:
  570. ;;
  571. ;; - add a generic mechanism for remembering the current branch names,
  572. ;; display the branch name in the mode-line. Replace
  573. ;; vc-cvs-sticky-tag with that.
  574. ;;
  575. ;;;; Internal cleanups:
  576. ;;
  577. ;; - backends that care about vc-stay-local should try to take it into
  578. ;; account for vc-dir. Is this likely to be useful??? YES!
  579. ;;
  580. ;; - vc-expand-dirs should take a backend parameter and only look for
  581. ;; files managed by that backend.
  582. ;;
  583. ;; - Another important thing: merge all the status-like backend operations.
  584. ;; We should remove dir-status, state, and dir-status-files, and
  585. ;; replace them with just `status' which takes a fileset and a continuation
  586. ;; (like dir-status) and returns a buffer in which the process(es) are run
  587. ;; (or nil if it worked synchronously). Hopefully we can define the old
  588. ;; 4 operations in term of this one.
  589. ;;
  590. ;;;; Other
  591. ;;
  592. ;; - when a file is in `conflict' state, turn on smerge-mode.
  593. ;;
  594. ;; - figure out what to do with conflicts that are not caused by the
  595. ;; file contents, but by metadata or other causes. Example: File A
  596. ;; gets renamed to B in one branch and to C in another and you merge
  597. ;; the two branches. Or you locally add file FOO and then pull a
  598. ;; change that also adds a new file FOO, ...
  599. ;;
  600. ;; - make it easier to write logs. Maybe C-x 4 a should add to the log
  601. ;; buffer, if one is present, instead of adding to the ChangeLog.
  602. ;;
  603. ;; - When vc-next-action calls vc-checkin it could pre-fill the
  604. ;; *vc-log* buffer with some obvious items: the list of files that
  605. ;; were added, the list of files that were removed. If the diff is
  606. ;; available, maybe it could even call something like
  607. ;; `diff-add-change-log-entries-other-window' to create a detailed
  608. ;; skeleton for the log...
  609. ;;
  610. ;; - most vc-dir backends need more work. They might need to
  611. ;; provide custom headers, use the `extra' field and deal with all
  612. ;; possible VC states.
  613. ;;
  614. ;; - add a function that calls vc-dir to `find-directory-functions'.
  615. ;;
  616. ;; - vc-diff, vc-annotate, etc. need to deal better with unregistered
  617. ;; files. Now that unregistered and ignored files are shown in
  618. ;; vc-dir, it is possible that these commands are called
  619. ;; for unregistered/ignored files.
  620. ;;
  621. ;; - vc-next-action needs work in order to work with multiple
  622. ;; backends: `vc-state' returns the state for the default backend,
  623. ;; not for the backend in the current *vc-dir* buffer.
  624. ;;
  625. ;; - vc-dir-kill-dir-status-process should not be specific to dir-status,
  626. ;; it should work for other async commands done through vc-do-command
  627. ;; as well,
  628. ;;
  629. ;; - vc-dir toolbar needs more icons.
  630. ;;
  631. ;; - The backends should avoid using `vc-file-setprop' and `vc-file-getprop'.
  632. ;;
  633. ;;; Code:
  634. (require 'vc-hooks)
  635. (require 'vc-dispatcher)
  636. (require 'ediff)
  637. (eval-when-compile
  638. (require 'cl)
  639. (require 'dired))
  640. (unless (assoc 'vc-parent-buffer minor-mode-alist)
  641. (setq minor-mode-alist
  642. (cons '(vc-parent-buffer vc-parent-buffer-name)
  643. minor-mode-alist)))
  644. ;; General customization
  645. (defgroup vc nil
  646. "Version-control system in Emacs."
  647. :group 'tools)
  648. (defcustom vc-initial-comment nil
  649. "If non-nil, prompt for initial comment when a file is registered."
  650. :type 'boolean
  651. :group 'vc)
  652. (make-obsolete-variable 'vc-initial-comment "it has no effect." "23.2")
  653. (defcustom vc-default-init-revision "1.1"
  654. "A string used as the default revision number when a new file is registered.
  655. This can be overridden by giving a prefix argument to \\[vc-register]. This
  656. can also be overridden by a particular VC backend."
  657. :type 'string
  658. :group 'vc
  659. :version "20.3")
  660. (defcustom vc-checkin-switches nil
  661. "A string or list of strings specifying extra switches for checkin.
  662. These are passed to the checkin program by \\[vc-checkin]."
  663. :type '(choice (const :tag "None" nil)
  664. (string :tag "Argument String")
  665. (repeat :tag "Argument List"
  666. :value ("")
  667. string))
  668. :group 'vc)
  669. (defcustom vc-checkout-switches nil
  670. "A string or list of strings specifying extra switches for checkout.
  671. These are passed to the checkout program by \\[vc-checkout]."
  672. :type '(choice (const :tag "None" nil)
  673. (string :tag "Argument String")
  674. (repeat :tag "Argument List"
  675. :value ("")
  676. string))
  677. :group 'vc)
  678. (defcustom vc-register-switches nil
  679. "A string or list of strings; extra switches for registering a file.
  680. These are passed to the checkin program by \\[vc-register]."
  681. :type '(choice (const :tag "None" nil)
  682. (string :tag "Argument String")
  683. (repeat :tag "Argument List"
  684. :value ("")
  685. string))
  686. :group 'vc)
  687. (defcustom vc-diff-switches nil
  688. "A string or list of strings specifying switches for diff under VC.
  689. When running diff under a given BACKEND, VC uses the first
  690. non-nil value of `vc-BACKEND-diff-switches', `vc-diff-switches',
  691. and `diff-switches', in that order. Since nil means to check the
  692. next variable in the sequence, either of the first two may use
  693. the value t to mean no switches at all. `vc-diff-switches'
  694. should contain switches that are specific to version control, but
  695. not specific to any particular backend."
  696. :type '(choice (const :tag "Unspecified" nil)
  697. (const :tag "None" t)
  698. (string :tag "Argument String")
  699. (repeat :tag "Argument List" :value ("") string))
  700. :group 'vc
  701. :version "21.1")
  702. (defcustom vc-diff-knows-L nil
  703. "Indicates whether diff understands the -L option.
  704. The value is either `yes', `no', or nil. If it is nil, VC tries
  705. to use -L and sets this variable to remember whether it worked."
  706. :type '(choice (const :tag "Work out" nil) (const yes) (const no))
  707. :group 'vc)
  708. (defcustom vc-log-show-limit 2000
  709. "Limit the number of items shown by the VC log commands.
  710. Zero means unlimited.
  711. Not all VC backends are able to support this feature."
  712. :type 'integer
  713. :group 'vc)
  714. (defcustom vc-allow-async-revert nil
  715. "Specifies whether the diff during \\[vc-revert] may be asynchronous.
  716. Enabling this option means that you can confirm a revert operation even
  717. if the local changes in the file have not been found and displayed yet."
  718. :type '(choice (const :tag "No" nil)
  719. (const :tag "Yes" t))
  720. :group 'vc
  721. :version "22.1")
  722. ;;;###autoload
  723. (defcustom vc-checkout-hook nil
  724. "Normal hook (list of functions) run after checking out a file.
  725. See `run-hooks'."
  726. :type 'hook
  727. :group 'vc
  728. :version "21.1")
  729. ;;;###autoload
  730. (defcustom vc-checkin-hook nil
  731. "Normal hook (list of functions) run after commit or file checkin.
  732. See also `log-edit-done-hook'."
  733. :type 'hook
  734. :options '(log-edit-comment-to-change-log)
  735. :group 'vc)
  736. ;;;###autoload
  737. (defcustom vc-before-checkin-hook nil
  738. "Normal hook (list of functions) run before a commit or a file checkin.
  739. See `run-hooks'."
  740. :type 'hook
  741. :group 'vc)
  742. (defcustom vc-revert-show-diff t
  743. "If non-nil, `vc-revert' shows a `vc-diff' buffer before querying."
  744. :type 'boolean
  745. :group 'vc
  746. :version "24.1")
  747. ;; Header-insertion hair
  748. (defcustom vc-static-header-alist
  749. '(("\\.c\\'" .
  750. "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
  751. "Associate static header string templates with file types.
  752. A \%s in the template is replaced with the first string associated with
  753. the file's version control type in `vc-BACKEND-header'."
  754. :type '(repeat (cons :format "%v"
  755. (regexp :tag "File Type")
  756. (string :tag "Header String")))
  757. :group 'vc)
  758. (defcustom vc-comment-alist
  759. '((nroff-mode ".\\\"" ""))
  760. "Special comment delimiters for generating VC headers.
  761. Add an entry in this list if you need to override the normal `comment-start'
  762. and `comment-end' variables. This will only be necessary if the mode language
  763. is sensitive to blank lines."
  764. :type '(repeat (list :format "%v"
  765. (symbol :tag "Mode")
  766. (string :tag "Comment Start")
  767. (string :tag "Comment End")))
  768. :group 'vc)
  769. (defcustom vc-checkout-carefully (= (user-uid) 0)
  770. "Non-nil means be extra-careful in checkout.
  771. Verify that the file really is not locked
  772. and that its contents match what the repository version says."
  773. :type 'boolean
  774. :group 'vc)
  775. (make-obsolete-variable 'vc-checkout-carefully
  776. "the corresponding checks are always done now."
  777. "21.1")
  778. ;; Variables users don't need to see
  779. (defvar vc-disable-async-diff nil
  780. "VC sets this to t locally to disable some async diff operations.
  781. Backends that offer asynchronous diffs should respect this variable
  782. in their implementation of vc-BACKEND-diff.")
  783. ;; File property caching
  784. (defun vc-clear-context ()
  785. "Clear all cached file properties."
  786. (interactive)
  787. (fillarray vc-file-prop-obarray 0))
  788. (defmacro with-vc-properties (files form settings)
  789. "Execute FORM, then maybe set per-file properties for FILES.
  790. If any of FILES is actually a directory, then do the same for all
  791. buffers for files in that directory.
  792. SETTINGS is an association list of property/value pairs. After
  793. executing FORM, set those properties from SETTINGS that have not yet
  794. been updated to their corresponding values."
  795. (declare (debug t))
  796. `(let ((vc-touched-properties (list t))
  797. (flist nil))
  798. (dolist (file ,files)
  799. (if (file-directory-p file)
  800. (dolist (buffer (buffer-list))
  801. (let ((fname (buffer-file-name buffer)))
  802. (when (and fname (vc-string-prefix-p file fname))
  803. (push fname flist))))
  804. (push file flist)))
  805. ,form
  806. (dolist (file flist)
  807. (dolist (setting ,settings)
  808. (let ((property (car setting)))
  809. (unless (memq property vc-touched-properties)
  810. (put (intern file vc-file-prop-obarray)
  811. property (cdr setting))))))))
  812. ;;; Code for deducing what fileset and backend to assume
  813. (defun vc-backend-for-registration (file)
  814. "Return a backend that can be used for registering FILE.
  815. If no backend declares itself responsible for FILE, then FILE
  816. must not be in a version controlled directory, so try to create a
  817. repository, prompting for the directory and the VC backend to
  818. use."
  819. (catch 'found
  820. ;; First try: find a responsible backend, it must be a backend
  821. ;; under which FILE is not yet registered.
  822. (dolist (backend vc-handled-backends)
  823. (and (not (vc-call-backend backend 'registered file))
  824. (vc-call-backend backend 'responsible-p file)
  825. (throw 'found backend)))
  826. ;; no responsible backend
  827. (let* ((possible-backends
  828. (let (pos)
  829. (dolist (crt vc-handled-backends)
  830. (when (vc-find-backend-function crt 'create-repo)
  831. (push crt pos)))
  832. pos))
  833. (bk
  834. (intern
  835. ;; Read the VC backend from the user, only
  836. ;; complete with the backends that have the
  837. ;; 'create-repo method.
  838. (completing-read
  839. (format "%s is not in a version controlled directory.\nUse VC backend: " file)
  840. (mapcar 'symbol-name possible-backends) nil t)))
  841. (repo-dir
  842. (let ((def-dir (file-name-directory file)))
  843. ;; read the directory where to create the
  844. ;; repository, make sure it's a parent of
  845. ;; file.
  846. (read-file-name
  847. (format "create %s repository in: " bk)
  848. default-directory def-dir t nil
  849. (lambda (arg)
  850. (message "arg %s" arg)
  851. (and (file-directory-p arg)
  852. (vc-string-prefix-p (expand-file-name arg) def-dir)))))))
  853. (let ((default-directory repo-dir))
  854. (vc-call-backend bk 'create-repo))
  855. (throw 'found bk))))
  856. (defun vc-responsible-backend (file)
  857. "Return the name of a backend system that is responsible for FILE.
  858. If FILE is already registered, return the
  859. backend of FILE. If FILE is not registered, then the
  860. first backend in `vc-handled-backends' that declares itself
  861. responsible for FILE is returned."
  862. (or (and (not (file-directory-p file)) (vc-backend file))
  863. (catch 'found
  864. ;; First try: find a responsible backend. If this is for registration,
  865. ;; it must be a backend under which FILE is not yet registered.
  866. (dolist (backend vc-handled-backends)
  867. (and (vc-call-backend backend 'responsible-p file)
  868. (throw 'found backend))))
  869. (error "No VC backend is responsible for %s" file)))
  870. (defun vc-expand-dirs (file-or-dir-list)
  871. "Expands directories in a file list specification.
  872. Within directories, only files already under version control are noticed."
  873. (let ((flattened '()))
  874. (dolist (node file-or-dir-list)
  875. (when (file-directory-p node)
  876. (vc-file-tree-walk
  877. node (lambda (f) (when (vc-backend f) (push f flattened)))))
  878. (unless (file-directory-p node) (push node flattened)))
  879. (nreverse flattened)))
  880. (defvar vc-dir-backend)
  881. (defvar log-view-vc-backend)
  882. (defvar diff-vc-backend)
  883. (defun vc-deduce-backend ()
  884. (cond ((derived-mode-p 'vc-dir-mode) vc-dir-backend)
  885. ((derived-mode-p 'log-view-mode) log-view-vc-backend)
  886. ((derived-mode-p 'diff-mode) diff-vc-backend)
  887. ;; Maybe we could even use comint-mode rather than shell-mode?
  888. ((derived-mode-p 'dired-mode 'shell-mode 'compilation-mode)
  889. (vc-responsible-backend default-directory))
  890. (vc-mode (vc-backend buffer-file-name))))
  891. (declare-function vc-dir-current-file "vc-dir" ())
  892. (declare-function vc-dir-deduce-fileset "vc-dir" (&optional state-model-only-files))
  893. (defun vc-deduce-fileset (&optional observer allow-unregistered
  894. state-model-only-files)
  895. "Deduce a set of files and a backend to which to apply an operation.
  896. Return (BACKEND FILESET FILESET-ONLY-FILES STATE CHECKOUT-MODEL).
  897. If we're in VC-dir mode, FILESET is the list of marked files.
  898. Otherwise, if in a buffer visiting a version-controlled file,
  899. FILESET is a single-file fileset containing that file.
  900. Otherwise, if ALLOW-UNREGISTERED is non-nil and the visited file
  901. is unregistered, FILESET is a single-file fileset containing it.
  902. Otherwise, throw an error.
  903. STATE-MODEL-ONLY-FILES if non-nil, means that the caller needs
  904. the FILESET-ONLY-FILES STATE and MODEL info. Otherwise, that
  905. part may be skipped.
  906. BEWARE: this function may change the
  907. current buffer."
  908. ;; FIXME: OBSERVER is unused. The name is not intuitive and is not
  909. ;; documented. It's set to t when called from diff and print-log.
  910. (let (backend)
  911. (cond
  912. ((derived-mode-p 'vc-dir-mode)
  913. (vc-dir-deduce-fileset state-model-only-files))
  914. ((derived-mode-p 'dired-mode)
  915. (if observer
  916. (vc-dired-deduce-fileset)
  917. (error "State changing VC operations not supported in `dired-mode'")))
  918. ((setq backend (vc-backend buffer-file-name))
  919. (if state-model-only-files
  920. (list backend (list buffer-file-name)
  921. (list buffer-file-name)
  922. (vc-state buffer-file-name)
  923. (vc-checkout-model backend buffer-file-name))
  924. (list backend (list buffer-file-name))))
  925. ((and (buffer-live-p vc-parent-buffer)
  926. ;; FIXME: Why this test? --Stef
  927. (or (buffer-file-name vc-parent-buffer)
  928. (with-current-buffer vc-parent-buffer
  929. (derived-mode-p 'vc-dir-mode))))
  930. (progn ;FIXME: Why not `with-current-buffer'? --Stef.
  931. (set-buffer vc-parent-buffer)
  932. (vc-deduce-fileset observer allow-unregistered state-model-only-files)))
  933. ((not buffer-file-name)
  934. (error "Buffer %s is not associated with a file" (buffer-name)))
  935. ((and allow-unregistered (not (vc-registered buffer-file-name)))
  936. (if state-model-only-files
  937. (list (vc-backend-for-registration (buffer-file-name))
  938. (list buffer-file-name)
  939. (list buffer-file-name)
  940. (when state-model-only-files 'unregistered)
  941. nil)
  942. (list (vc-backend-for-registration (buffer-file-name))
  943. (list buffer-file-name))))
  944. (t (error "No fileset is available here")))))
  945. (defun vc-dired-deduce-fileset ()
  946. (let ((backend (vc-responsible-backend default-directory)))
  947. (unless backend (error "Directory not under VC"))
  948. (list backend
  949. (dired-map-over-marks (dired-get-filename nil t) nil))))
  950. (defun vc-ensure-vc-buffer ()
  951. "Make sure that the current buffer visits a version-controlled file."
  952. (cond
  953. ((derived-mode-p 'vc-dir-mode)
  954. (set-buffer (find-file-noselect (vc-dir-current-file))))
  955. (t
  956. (while (and vc-parent-buffer
  957. (buffer-live-p vc-parent-buffer)
  958. ;; Avoid infinite looping when vc-parent-buffer and
  959. ;; current buffer are the same buffer.
  960. (not (eq vc-parent-buffer (current-buffer))))
  961. (set-buffer vc-parent-buffer))
  962. (if (not buffer-file-name)
  963. (error "Buffer %s is not associated with a file" (buffer-name))
  964. (unless (vc-backend buffer-file-name)
  965. (error "File %s is not under version control" buffer-file-name))))))
  966. ;;; Support for the C-x v v command.
  967. ;; This is where all the single-file-oriented code from before the fileset
  968. ;; rewrite lives.
  969. (defsubst vc-editable-p (file)
  970. "Return non-nil if FILE can be edited."
  971. (let ((backend (vc-backend file)))
  972. (and backend
  973. (or (eq (vc-checkout-model backend (list file)) 'implicit)
  974. (memq (vc-state file) '(edited needs-merge conflict))))))
  975. (defun vc-compatible-state (p q)
  976. "Controls which states can be in the same commit."
  977. (or
  978. (eq p q)
  979. (and (member p '(edited added removed)) (member q '(edited added removed)))))
  980. ;; Here's the major entry point.
  981. ;;;###autoload
  982. (defun vc-next-action (verbose)
  983. "Do the next logical version control operation on the current fileset.
  984. This requires that all files in the current VC fileset be in the
  985. same state. If not, signal an error.
  986. For merging-based version control systems:
  987. If every file in the VC fileset is not registered for version
  988. control, register the fileset (but don't commit).
  989. If every work file in the VC fileset is added or changed, pop
  990. up a *vc-log* buffer to commit the fileset.
  991. For a centralized version control system, if any work file in
  992. the VC fileset is out of date, offer to update the fileset.
  993. For old-style locking-based version control systems, like RCS:
  994. If every file is not registered, register the file(s).
  995. If every file is registered and unlocked, check out (lock)
  996. the file(s) for editing.
  997. If every file is locked by you and has changes, pop up a
  998. *vc-log* buffer to check in the changes. If the variable
  999. `vc-keep-workfiles' is non-nil (the default), leave a
  1000. read-only copy of each changed file after checking in.
  1001. If every file is locked by you and unchanged, unlock them.
  1002. If every file is locked by someone else, offer to steal the lock."
  1003. (interactive "P")
  1004. (let* ((vc-fileset (vc-deduce-fileset nil t 'state-model-only-files))
  1005. (backend (car vc-fileset))
  1006. (files (nth 1 vc-fileset))
  1007. (fileset-only-files (nth 2 vc-fileset))
  1008. ;; FIXME: We used to call `vc-recompute-state' here.
  1009. (state (nth 3 vc-fileset))
  1010. ;; The backend should check that the checkout-model is consistent
  1011. ;; among all the `files'.
  1012. (model (nth 4 vc-fileset)))
  1013. ;; Do the right thing
  1014. (cond
  1015. ((eq state 'missing)
  1016. (error "Fileset files are missing, so cannot be operated on"))
  1017. ((eq state 'ignored)
  1018. (error "Fileset files are ignored by the version-control system"))
  1019. ((or (null state) (eq state 'unregistered))
  1020. (vc-register nil vc-fileset))
  1021. ;; Files are up-to-date, or need a merge and user specified a revision
  1022. ((or (eq state 'up-to-date) (and verbose (eq state 'needs-update)))
  1023. (cond
  1024. (verbose
  1025. ;; go to a different revision
  1026. (let* ((revision
  1027. (read-string "Branch, revision, or backend to move to: "))
  1028. (revision-downcase (downcase revision)))
  1029. (if (member
  1030. revision-downcase
  1031. (mapcar (lambda (arg) (downcase (symbol-name arg)))
  1032. vc-handled-backends))
  1033. (let ((vsym (intern-soft revision-downcase)))
  1034. (dolist (file files) (vc-transfer-file file vsym)))
  1035. (dolist (file files)
  1036. (vc-checkout file (eq model 'implicit) revision)))))
  1037. ((not (eq model 'implicit))
  1038. ;; check the files out
  1039. (dolist (file files) (vc-checkout file t)))
  1040. (t
  1041. ;; do nothing
  1042. (message "Fileset is up-to-date"))))
  1043. ;; Files have local changes
  1044. ((vc-compatible-state state 'edited)
  1045. (let ((ready-for-commit files))
  1046. ;; If files are edited but read-only, give user a chance to correct.
  1047. (dolist (file files)
  1048. ;; If committing a mix of removed and edited files, the
  1049. ;; fileset has state = 'edited. Rather than checking the
  1050. ;; state of each individual file in the fileset, it seems
  1051. ;; simplest to just check if the file exists. Bug#9781.
  1052. (when (and (file-exists-p file) (not (file-writable-p file)))
  1053. ;; Make the file+buffer read-write.
  1054. (unless (y-or-n-p (format "%s is edited but read-only; make it writable and continue? " file))
  1055. (error "Aborted"))
  1056. ;; Maybe we somehow lost permissions on the directory.
  1057. (condition-case nil
  1058. (set-file-modes file (logior (file-modes file) 128))
  1059. (error (error "Unable to make file writable")))
  1060. (let ((visited (get-file-buffer file)))
  1061. (when visited
  1062. (with-current-buffer visited
  1063. (toggle-read-only -1))))))
  1064. ;; Allow user to revert files with no changes
  1065. (save-excursion
  1066. (dolist (file files)
  1067. (let ((visited (get-file-buffer file)))
  1068. ;; For files with locking, if the file does not contain
  1069. ;; any changes, just let go of the lock, i.e. revert.
  1070. (when (and (not (eq model 'implicit))
  1071. (vc-workfile-unchanged-p file)
  1072. ;; If buffer is modified, that means the user just
  1073. ;; said no to saving it; in that case, don't revert,
  1074. ;; because the user might intend to save after
  1075. ;; finishing the log entry and committing.
  1076. (not (and visited (buffer-modified-p))))
  1077. (vc-revert-file file)
  1078. (setq ready-for-commit (delete file ready-for-commit))))))
  1079. ;; Remaining files need to be committed
  1080. (if (not ready-for-commit)
  1081. (message "No files remain to be committed")
  1082. (if (not verbose)
  1083. (vc-checkin ready-for-commit backend)
  1084. (let* ((revision (read-string "New revision or backend: "))
  1085. (revision-downcase (downcase revision)))
  1086. (if (member
  1087. revision-downcase
  1088. (mapcar (lambda (arg) (downcase (symbol-name arg)))
  1089. vc-handled-backends))
  1090. (let ((vsym (intern revision-downcase)))
  1091. (dolist (file files) (vc-transfer-file file vsym)))
  1092. (vc-checkin ready-for-commit backend revision)))))))
  1093. ;; locked by somebody else (locking VCSes only)
  1094. ((stringp state)
  1095. ;; In the old days, we computed the revision once and used it on
  1096. ;; the single file. Then, for the 2007-2008 fileset rewrite, we
  1097. ;; computed the revision once (incorrectly, using a free var) and
  1098. ;; used it on all files. To fix the free var bug, we can either
  1099. ;; use `(car files)' or do what we do here: distribute the
  1100. ;; revision computation among `files'. Although this may be
  1101. ;; tedious for those backends where a "revision" is a trans-file
  1102. ;; concept, it is nonetheless correct for both those and (more
  1103. ;; importantly) for those where "revision" is a per-file concept.
  1104. ;; If the intersection of the former group and "locking VCSes" is
  1105. ;; non-empty [I vaguely doubt it --ttn], we can reinstate the
  1106. ;; pre-computation approach of yore.
  1107. (dolist (file files)
  1108. (vc-steal-lock
  1109. file (if verbose
  1110. (read-string (format "%s revision to steal: " file))
  1111. (vc-working-revision file))
  1112. state)))
  1113. ;; conflict
  1114. ((eq state 'conflict)
  1115. ;; FIXME: Is it really the UI we want to provide?
  1116. ;; In my experience, the conflicted files should be marked as resolved
  1117. ;; one-by-one when saving the file after resolving the conflicts.
  1118. ;; I.e. stating explicitly that the conflicts are resolved is done
  1119. ;; very rarely.
  1120. (vc-mark-resolved backend files))
  1121. ;; needs-update
  1122. ((eq state 'needs-update)
  1123. (dolist (file files)
  1124. (if (yes-or-no-p (format
  1125. "%s is not up-to-date. Get latest revision? "
  1126. (file-name-nondirectory file)))
  1127. (vc-checkout file (eq model 'implicit) t)
  1128. (when (and (not (eq model 'implicit))
  1129. (yes-or-no-p "Lock this revision? "))
  1130. (vc-checkout file t)))))
  1131. ;; needs-merge
  1132. ((eq state 'needs-merge)
  1133. (dolist (file files)
  1134. (when (yes-or-no-p (format
  1135. "%s is not up-to-date. Merge in changes now? "
  1136. (file-name-nondirectory file)))
  1137. (vc-maybe-resolve-conflicts
  1138. file (vc-call-backend backend 'merge-news file)))))
  1139. ;; unlocked-changes
  1140. ((eq state 'unlocked-changes)
  1141. (dolist (file files)
  1142. (when (not (equal buffer-file-name file))
  1143. (find-file-other-window file))
  1144. (if (save-window-excursion
  1145. (vc-diff-internal nil
  1146. (cons (car vc-fileset) (cons (cadr vc-fileset) (list file)))
  1147. (vc-working-revision file) nil)
  1148. (goto-char (point-min))
  1149. (let ((inhibit-read-only t))
  1150. (insert
  1151. (format "Changes to %s since last lock:\n\n" file)))
  1152. (not (beep))
  1153. (yes-or-no-p (concat "File has unlocked changes. "
  1154. "Claim lock retaining changes? ")))
  1155. (progn (vc-call-backend backend 'steal-lock file)
  1156. (clear-visited-file-modtime)
  1157. ;; Must clear any headers here because they wouldn't
  1158. ;; show that the file is locked now.
  1159. (vc-clear-headers file)
  1160. (write-file buffer-file-name)
  1161. (vc-mode-line file backend))
  1162. (if (not (yes-or-no-p
  1163. "Revert to checked-in revision, instead? "))
  1164. (error "Checkout aborted")
  1165. (vc-revert-buffer-internal t t)
  1166. (vc-checkout file t)))))
  1167. ;; Unknown fileset state
  1168. (t
  1169. (error "Fileset is in an unknown state %s" state)))))
  1170. (defun vc-create-repo (backend)
  1171. "Create an empty repository in the current directory."
  1172. (interactive
  1173. (list
  1174. (intern
  1175. (upcase
  1176. (completing-read
  1177. "Create repository for: "
  1178. (mapcar (lambda (b) (list (downcase (symbol-name b)))) vc-handled-backends)
  1179. nil t)))))
  1180. (vc-call-backend backend 'create-repo))
  1181. (declare-function vc-dir-move-to-goal-column "vc-dir" ())
  1182. ;;;###autoload
  1183. (defun vc-register (&optional set-revision vc-fileset comment)
  1184. "Register into a version control system.
  1185. If VC-FILESET is given, register the files in that fileset.
  1186. Otherwise register the current file.
  1187. With prefix argument SET-REVISION, allow user to specify initial revision
  1188. level. If COMMENT is present, use that as an initial comment.
  1189. The version control system to use is found by cycling through the list
  1190. `vc-handled-backends'. The first backend in that list which declares
  1191. itself responsible for the file (usually because other files in that
  1192. directory are already registered under that backend) will be used to
  1193. register the file. If no backend declares itself responsible, the
  1194. first backend that could register the file is used."
  1195. (interactive "P")
  1196. (let* ((fileset-arg (or vc-fileset (vc-deduce-fileset nil t)))
  1197. (backend (car fileset-arg))
  1198. (files (nth 1 fileset-arg)))
  1199. ;; We used to operate on `only-files', but VC wants to provide the
  1200. ;; possibility to register directories rather than files only, since
  1201. ;; many VCS allow that as well.
  1202. (dolist (fname files)
  1203. (let ((bname (get-file-buffer fname)))
  1204. (unless fname (setq fname buffer-file-name))
  1205. (when (vc-backend fname)
  1206. (if (vc-registered fname)
  1207. (error "This file is already registered")
  1208. (unless (y-or-n-p "Previous master file has vanished. Make a new one? ")
  1209. (error "Aborted"))))
  1210. ;; Watch out for new buffers of size 0: the corresponding file
  1211. ;; does not exist yet, even though buffer-modified-p is nil.
  1212. (when bname
  1213. (with-current-buffer bname
  1214. (when (and (not (buffer-modified-p))
  1215. (zerop (buffer-size))
  1216. (not (file-exists-p buffer-file-name)))
  1217. (set-buffer-modified-p t))
  1218. (vc-buffer-sync)))))
  1219. (message "Registering %s... " files)
  1220. (mapc 'vc-file-clearprops files)
  1221. (vc-call-backend backend 'register files
  1222. (if set-revision
  1223. (read-string (format "Initial revision level for %s: " files))
  1224. (vc-call-backend backend 'init-revision))
  1225. comment)
  1226. (mapc
  1227. (lambda (file)
  1228. (vc-file-setprop file 'vc-backend backend)
  1229. ;; FIXME: This is wrong: it should set `backup-inhibited' in all
  1230. ;; the buffers visiting files affected by this `vc-register', not
  1231. ;; in the current-buffer.
  1232. ;; (unless vc-make-backup-files
  1233. ;; (make-local-variable 'backup-inhibited)
  1234. ;; (setq backup-inhibited t))
  1235. (vc-resynch-buffer file vc-keep-workfiles t))
  1236. files)
  1237. (when (derived-mode-p 'vc-dir-mode)
  1238. (vc-dir-move-to-goal-column))
  1239. (message "Registering %s... done" files)))
  1240. (defun vc-register-with (backend)
  1241. "Register the current file with a specified back end."
  1242. (interactive "SBackend: ")
  1243. (when (not (member backend vc-handled-backends))
  1244. (error "Unknown back end"))
  1245. (let ((vc-handled-backends (list backend)))
  1246. (call-interactively 'vc-register)))
  1247. (defun vc-checkout (file &optional writable rev)
  1248. "Retrieve a copy of the revision REV of FILE.
  1249. If WRITABLE is non-nil, make sure the retrieved file is writable.
  1250. REV defaults to the latest revision.
  1251. After check-out, runs the normal hook `vc-checkout-hook'."
  1252. (and writable
  1253. (not rev)
  1254. (vc-call make-version-backups-p file)
  1255. (vc-up-to-date-p file)
  1256. (vc-make-version-backup file))
  1257. (let ((backend (vc-backend file)))
  1258. (with-vc-properties (list file)
  1259. (condition-case err
  1260. (vc-call-backend backend 'checkout file writable rev)
  1261. (file-error
  1262. ;; Maybe the backend is not installed ;-(
  1263. (when writable
  1264. (let ((buf (get-file-buffer file)))
  1265. (when buf (with-current-buffer buf (toggle-read-only -1)))))
  1266. (signal (car err) (cdr err))))
  1267. `((vc-state . ,(if (or (eq (vc-checkout-model backend (list file)) 'implicit)
  1268. (not writable))
  1269. (if (vc-call-backend backend 'latest-on-branch-p file)
  1270. 'up-to-date
  1271. 'needs-update)
  1272. 'edited))
  1273. (vc-checkout-time . ,(nth 5 (file-attributes file))))))
  1274. (vc-resynch-buffer file t t)
  1275. (run-hooks 'vc-checkout-hook))
  1276. (defun vc-mark-resolved (backend files)
  1277. (prog1 (with-vc-properties
  1278. files
  1279. (vc-call-backend backend 'mark-resolved files)
  1280. ;; FIXME: Is this TRTD? Might not be.
  1281. `((vc-state . edited)))
  1282. (message
  1283. (substitute-command-keys
  1284. "Conflicts have been resolved in %s. \
  1285. Type \\[vc-next-action] to check in changes.")
  1286. (if (> (length files) 1)
  1287. (format "%d files" (length files))
  1288. "this file"))))
  1289. (defun vc-steal-lock (file rev owner)
  1290. "Steal the lock on FILE."
  1291. (let (file-description)
  1292. (if rev
  1293. (setq file-description (format "%s:%s" file rev))
  1294. (setq file-description file))
  1295. (when (not (yes-or-no-p (format "Steal the lock on %s from %s? "
  1296. file-description owner)))
  1297. (error "Steal canceled"))
  1298. (message "Stealing lock on %s..." file)
  1299. (with-vc-properties
  1300. (list file)
  1301. (vc-call steal-lock file rev)
  1302. `((vc-state . edited)))
  1303. (vc-resynch-buffer file t t)
  1304. (message "Stealing lock on %s...done" file)
  1305. ;; Write mail after actually stealing, because if the stealing
  1306. ;; goes wrong, we don't want to send any mail.
  1307. (compose-mail owner (format "Stolen lock on %s" file-description))
  1308. (setq default-directory (expand-file-name "~/"))
  1309. (goto-char (point-max))
  1310. (insert
  1311. (format "I stole the lock on %s, " file-description)
  1312. (current-time-string)
  1313. ".\n")
  1314. (message "Please explain why you stole the lock. Type C-c C-c when done.")))
  1315. (defun vc-checkin (files backend &optional rev comment initial-contents)
  1316. "Check in FILES.
  1317. The optional argument REV may be a string specifying the new revision
  1318. level (strongly deprecated). COMMENT is a comment
  1319. string; if omitted, a buffer is popped up to accept a comment. If
  1320. INITIAL-CONTENTS is non-nil, then COMMENT is used as the initial contents
  1321. of the log entry buffer.
  1322. If `vc-keep-workfiles' is nil, FILE is deleted afterwards, provided
  1323. that the version control system supports this mode of operation.
  1324. Runs the normal hooks `vc-before-checkin-hook' and `vc-checkin-hook'."
  1325. (when vc-before-checkin-hook
  1326. (run-hooks 'vc-before-checkin-hook))
  1327. (lexical-let
  1328. ((backend backend))
  1329. (vc-start-logentry
  1330. files comment initial-contents
  1331. "Enter a change comment."
  1332. "*vc-log*"
  1333. (lambda ()
  1334. (vc-call-backend backend 'log-edit-mode))
  1335. (lexical-let ((rev rev))
  1336. (lambda (files comment)
  1337. (message "Checking in %s..." (vc-delistify files))
  1338. ;; "This log message intentionally left almost blank".
  1339. ;; RCS 5.7 gripes about white-space-only comments too.
  1340. (or (and comment (string-match "[^\t\n ]" comment))
  1341. (setq comment "*** empty log message ***"))
  1342. (with-vc-properties
  1343. files
  1344. ;; We used to change buffers to get local value of
  1345. ;; vc-checkin-switches, but 'the' local buffer is
  1346. ;; not a well-defined concept for filesets.
  1347. (progn
  1348. (vc-call-backend backend 'checkin files rev comment)
  1349. (mapc 'vc-delete-automatic-version-backups files))
  1350. `((vc-state . up-to-date)
  1351. (vc-checkout-time . ,(nth 5 (file-attributes file)))
  1352. (vc-working-revision . nil)))
  1353. (message "Checking in %s...done" (vc-delistify files))))
  1354. 'vc-checkin-hook)))
  1355. ;;; Additional entry points for examining version histories
  1356. ;; (defun vc-default-diff-tree (backend dir rev1 rev2)
  1357. ;; "List differences for all registered files at and below DIR.
  1358. ;; The meaning of REV1 and REV2 is the same as for `vc-revision-diff'."
  1359. ;; ;; This implementation does an explicit tree walk, and calls
  1360. ;; ;; vc-BACKEND-diff directly for each file. An optimization
  1361. ;; ;; would be to use `vc-diff-internal', so that diffs can be local,
  1362. ;; ;; and to call it only for files that are actually changed.
  1363. ;; ;; However, this is expensive for some backends, and so it is left
  1364. ;; ;; to backend-specific implementations.
  1365. ;; (setq default-directory dir)
  1366. ;; (vc-file-tree-walk
  1367. ;; default-directory
  1368. ;; (lambda (f)
  1369. ;; (vc-exec-after
  1370. ;; `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
  1371. ;; (message "Looking at %s" ',f)
  1372. ;; (vc-call-backend ',(vc-backend f)
  1373. ;; 'diff (list ',f) ',rev1 ',rev2))))))
  1374. (defvar vc-coding-system-inherit-eol t
  1375. "When non-nil, inherit the EOL format for reading Diff output from the file.
  1376. Used in `vc-coding-system-for-diff' to determine the EOL format to use
  1377. for reading Diff output for a file. If non-nil, the EOL format is
  1378. inherited from the file itself.
  1379. Set this variable to nil if your Diff tool might use a different
  1380. EOL. Then Emacs will auto-detect the EOL format in Diff output, which
  1381. gives better results.") ;; Cf. bug#4451.
  1382. (defun vc-coding-system-for-diff (file)
  1383. "Return the coding system for reading diff output for FILE."
  1384. (or coding-system-for-read
  1385. ;; if we already have this file open,
  1386. ;; use the buffer's coding system
  1387. (let ((buf (find-buffer-visiting file)))
  1388. (when buf (with-current-buffer buf
  1389. (if vc-coding-system-inherit-eol
  1390. buffer-file-coding-system
  1391. ;; Don't inherit the EOL part of the coding-system,
  1392. ;; because some Diff tools may choose to use
  1393. ;; a different one. bug#4451.
  1394. (coding-system-base buffer-file-coding-system)))))
  1395. ;; otherwise, try to find one based on the file name
  1396. (car (find-operation-coding-system 'insert-file-contents file))
  1397. ;; and a final fallback
  1398. 'undecided))
  1399. (defun vc-switches (backend op)
  1400. "Return a list of vc-BACKEND switches for operation OP.
  1401. BACKEND is a symbol such as `CVS', which will be downcased.
  1402. OP is a symbol such as `diff'.
  1403. In decreasing order of preference, return the value of:
  1404. vc-BACKEND-OP-switches (e.g. `vc-cvs-diff-switches');
  1405. vc-OP-switches (e.g. `vc-diff-switches'); or, in the case of
  1406. diff only, `diff-switches'.
  1407. If the chosen value is not a string or a list, return nil.
  1408. This is so that you may set, e.g. `vc-svn-diff-switches' to t in order
  1409. to override the value of `vc-diff-switches' and `diff-switches'."
  1410. (let ((switches
  1411. (or (when backend
  1412. (let ((sym (vc-make-backend-sym
  1413. backend (intern (concat (symbol-name op)
  1414. "-switches")))))
  1415. (when (boundp sym) (symbol-value sym))))
  1416. (let ((sym (intern (format "vc-%s-switches" (symbol-name op)))))
  1417. (when (boundp sym) (symbol-value sym)))
  1418. (cond
  1419. ((eq op 'diff) diff-switches)))))
  1420. (if (stringp switches) (list switches)
  1421. ;; If not a list, return nil.
  1422. ;; This is so we can set vc-diff-switches to t to override
  1423. ;; any switches in diff-switches.
  1424. (when (listp switches) switches))))
  1425. ;; Old def for compatibility with Emacs-21.[123].
  1426. (defmacro vc-diff-switches-list (backend) `(vc-switches ',backend 'diff))
  1427. (make-obsolete 'vc-diff-switches-list 'vc-switches "22.1")
  1428. (defun vc-diff-finish (buffer messages)
  1429. ;; The empty sync output case has already been handled, so the only
  1430. ;; possibility of an empty output is for an async process.
  1431. (when (buffer-live-p buffer)
  1432. (let ((window (get-buffer-window buffer t))
  1433. (emptyp (zerop (buffer-size buffer))))
  1434. (with-current-buffer buffer
  1435. (and messages emptyp
  1436. (let ((inhibit-read-only t))
  1437. (insert (cdr messages) ".\n")
  1438. (message "%s" (cdr messages))))
  1439. (goto-char (point-min))
  1440. (when window
  1441. (shrink-window-if-larger-than-buffer window)))
  1442. (when (and messages (not emptyp))
  1443. (message "%sdone" (car messages))))))
  1444. (defvar vc-diff-added-files nil
  1445. "If non-nil, diff added files by comparing them to /dev/null.")
  1446. (defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose buffer)
  1447. "Report diffs between two revisions of a fileset.
  1448. Output goes to the buffer BUFFER, which defaults to *vc-diff*.
  1449. BUFFER, if non-nil, should be a buffer or a buffer name.
  1450. Return t if the buffer had changes, nil otherwise."
  1451. (unless buffer
  1452. (setq buffer "*vc-diff*"))
  1453. (let* ((files (cadr vc-fileset))
  1454. (messages (cons (format "Finding changes in %s..."
  1455. (vc-delistify files))
  1456. (format "No changes between %s and %s"
  1457. (or rev1 "working revision")
  1458. (or rev2 "workfile"))))
  1459. ;; Set coding system based on the first file. It's a kluge,
  1460. ;; but the only way to set it for each file included would
  1461. ;; be to call the back end separately for each file.
  1462. (coding-system-for-read
  1463. (if files (vc-coding-system-for-diff (car files)) 'undecided)))
  1464. (vc-setup-buffer buffer)
  1465. (message "%s" (car messages))
  1466. ;; Many backends don't handle well the case of a file that has been
  1467. ;; added but not yet committed to the repo (notably CVS and Subversion).
  1468. ;; Do that work here so the backends don't have to futz with it. --ESR
  1469. ;;
  1470. ;; Actually most backends (including CVS) have options to control the
  1471. ;; behavior since which one is better depends on the user and on the
  1472. ;; situation). Worse yet: this code does not handle the case where
  1473. ;; `file' is a directory which contains added files.
  1474. ;; I made it conditional on vc-diff-added-files but it should probably
  1475. ;; just be removed (or copied/moved to specific backends). --Stef.
  1476. (when vc-diff-added-files
  1477. (let ((filtered '())
  1478. process-file-side-effects)
  1479. (dolist (file files)
  1480. (if (or (file-directory-p file)
  1481. (not (string= (vc-working-revision file) "0")))
  1482. (push file filtered)
  1483. ;; This file is added but not yet committed;
  1484. ;; there is no repository version to diff against.
  1485. (if (or rev1 rev2)
  1486. (error "No revisions of %s exist" file)
  1487. ;; We regard this as "changed".
  1488. ;; Diff it against /dev/null.
  1489. (apply 'vc-do-command buffer
  1490. 1 "diff" file
  1491. (append (vc-switches nil 'diff) '("/dev/null"))))))
  1492. (setq files (nreverse filtered))))
  1493. (let ((vc-disable-async-diff (not async)))
  1494. (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 buffer))
  1495. (set-buffer buffer)
  1496. (if (and (zerop (buffer-size))
  1497. (not (get-buffer-process (current-buffer))))
  1498. ;; Treat this case specially so as not to pop the buffer.
  1499. (progn
  1500. (message "%s" (cdr messages))
  1501. nil)
  1502. (diff-mode)
  1503. (set (make-local-variable 'diff-vc-backend) (car vc-fileset))
  1504. (set (make-local-variable 'revert-buffer-function)
  1505. `(lambda (ignore-auto noconfirm)
  1506. (vc-diff-internal ,async ',vc-fileset ,rev1 ,rev2 ,verbose)))
  1507. ;; Make the *vc-diff* buffer read only, the diff-mode key
  1508. ;; bindings are nicer for read only buffers. pcl-cvs does the
  1509. ;; same thing.
  1510. (setq buffer-read-only t)
  1511. ;; Display the buffer, but at the end because it can change point.
  1512. (pop-to-buffer (current-buffer))
  1513. ;; The diff process may finish early, so call `vc-diff-finish'
  1514. ;; after `pop-to-buffer'; the former assumes the diff buffer is
  1515. ;; shown in some window.
  1516. (vc-exec-after `(vc-diff-finish ,(current-buffer)
  1517. ',(when verbose messages)))
  1518. ;; In the async case, we return t even if there are no differences
  1519. ;; because we don't know that yet.
  1520. t)))
  1521. (defun vc-read-revision (prompt &optional files backend default initial-input)
  1522. (cond
  1523. ((null files)
  1524. (let ((vc-fileset (vc-deduce-fileset t))) ;FIXME: why t? --Stef
  1525. (setq files (cadr vc-fileset))
  1526. (setq backend (car vc-fileset))))
  1527. ((null backend) (setq backend (vc-backend (car files)))))
  1528. (let ((completion-table
  1529. (vc-call-backend backend 'revision-completion-table files)))
  1530. (if completion-table
  1531. (completing-read prompt completion-table
  1532. nil nil initial-input nil default)
  1533. (read-string prompt initial-input nil default))))
  1534. (defun vc-diff-build-argument-list-internal ()
  1535. "Build argument list for calling internal diff functions."
  1536. (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: why t? --Stef
  1537. (files (cadr vc-fileset))
  1538. (backend (car vc-fileset))
  1539. (first (car files))
  1540. (rev1-default nil)
  1541. (rev2-default nil))
  1542. (cond
  1543. ;; someday we may be able to do revision completion on non-singleton
  1544. ;; filesets, but not yet.
  1545. ((/= (length files) 1)
  1546. nil)
  1547. ;; if it's a directory, don't supply any revision default
  1548. ((file-directory-p first)
  1549. nil)
  1550. ;; if the file is not up-to-date, use working revision as older revision
  1551. ((not (vc-up-to-date-p first))
  1552. (setq rev1-default (vc-working-revision first)))
  1553. ;; if the file is not locked, use last and previous revisions as defaults
  1554. (t
  1555. (setq rev1-default (vc-call-backend backend 'previous-revision first
  1556. (vc-working-revision first)))
  1557. (when (string= rev1-default "") (setq rev1-default nil))
  1558. (setq rev2-default (vc-working-revision first))))
  1559. ;; construct argument list
  1560. (let* ((rev1-prompt (if rev1-default
  1561. (concat "Older revision (default "
  1562. rev1-default "): ")
  1563. "Older revision: "))
  1564. (rev2-prompt (concat "Newer revision (default "
  1565. (or rev2-default "current source") "): "))
  1566. (rev1 (vc-read-revision rev1-prompt files backend rev1-default))
  1567. (rev2 (vc-read-revision rev2-prompt files backend rev2-default)))
  1568. (when (string= rev1 "") (setq rev1 nil))
  1569. (when (string= rev2 "") (setq rev2 nil))
  1570. (list files rev1 rev2))))
  1571. ;;;###autoload
  1572. (defun vc-version-diff (files rev1 rev2)
  1573. "Report diffs between revisions of the fileset in the repository history."
  1574. (interactive (vc-diff-build-argument-list-internal))
  1575. ;; All that was just so we could do argument completion!
  1576. (when (and (not rev1) rev2)
  1577. (error "Not a valid revision range"))
  1578. ;; Yes, it's painful to call (vc-deduce-fileset) again. Alas, the
  1579. ;; placement rules for (interactive) don't actually leave us a choice.
  1580. (vc-diff-internal t (vc-deduce-fileset t) rev1 rev2
  1581. (called-interactively-p 'interactive)))
  1582. ;;;###autoload
  1583. (defun vc-diff (historic &optional not-urgent)
  1584. "Display diffs between file revisions.
  1585. Normally this compares the currently selected fileset with their
  1586. working revisions. With a prefix argument HISTORIC, it reads two revision
  1587. designators specifying which revisions to compare.
  1588. The optional argument NOT-URGENT non-nil means it is ok to say no to
  1589. saving the buffer."
  1590. (interactive (list current-prefix-arg t))
  1591. (if historic
  1592. (call-interactively 'vc-version-diff)
  1593. (when buffer-file-name (vc-buffer-sync not-urgent))
  1594. (vc-diff-internal t (vc-deduce-fileset t) nil nil
  1595. (called-interactively-p 'interactive))))
  1596. (declare-function ediff-vc-internal (rev1 rev2 &optional startup-hooks))
  1597. ;;;###autoload
  1598. (defun vc-version-ediff (files rev1 rev2)
  1599. "Show differences between revisions of the fileset in the
  1600. repository history using ediff."
  1601. (interactive (vc-diff-build-argument-list-internal))
  1602. ;; All that was just so we could do argument completion!
  1603. (when (and (not rev1) rev2)
  1604. (error "Not a valid revision range"))
  1605. (message "%s" (format "Finding changes in %s..." (vc-delistify files)))
  1606. ;; Functions ediff-(vc|rcs)-internal use "" instead of nil.
  1607. (when (null rev1) (setq rev1 ""))
  1608. (when (null rev2) (setq rev2 ""))
  1609. (cond
  1610. ;; FIXME We only support running ediff on one file for now.
  1611. ;; We could spin off an ediff session per file in the file set.
  1612. ((= (length files) 1)
  1613. (ediff-load-version-control)
  1614. (find-file (car files)) ;FIXME: find-file from Elisp is bad.
  1615. (ediff-vc-internal rev1 rev2 nil))
  1616. (t
  1617. (error "More than one file is not supported"))))
  1618. ;;;###autoload
  1619. (defun vc-ediff (historic &optional not-urgent)
  1620. "Display diffs between file revisions using ediff.
  1621. Normally this compares the currently selected fileset with their
  1622. working revisions. With a prefix argument HISTORIC, it reads two revision
  1623. designators specifying which revisions to compare.
  1624. The optional argument NOT-URGENT non-nil means it is ok to say no to
  1625. saving the buffer."
  1626. (interactive (list current-prefix-arg t))
  1627. (if historic
  1628. (call-interactively 'vc-version-ediff)
  1629. (when buffer-file-name (vc-buffer-sync not-urgent))
  1630. (vc-version-ediff (cadr (vc-deduce-fileset t)) nil nil)))
  1631. ;;;###autoload
  1632. (defun vc-root-diff (historic &optional not-urgent)
  1633. "Display diffs between VC-controlled whole tree revisions.
  1634. Normally, this compares the tree corresponding to the current
  1635. fileset with the working revision.
  1636. With a prefix argument HISTORIC, prompt for two revision
  1637. designators specifying which revisions to compare.
  1638. The optional argument NOT-URGENT non-nil means it is ok to say no to
  1639. saving the buffer."
  1640. (interactive (list current-prefix-arg t))
  1641. (if historic
  1642. ;; FIXME: this does not work right, `vc-version-diff' ends up
  1643. ;; calling `vc-deduce-fileset' to find the files to diff, and
  1644. ;; that's not what we want here, we want the diff for the VC root dir.
  1645. (call-interactively 'vc-version-diff)
  1646. (when buffer-file-name (vc-buffer-sync not-urgent))
  1647. (let ((backend (vc-deduce-backend))
  1648. rootdir working-revision)
  1649. (unless backend
  1650. (error "Buffer is not version controlled"))
  1651. (setq rootdir (vc-call-backend backend 'root default-directory))
  1652. (setq working-revision (vc-working-revision rootdir))
  1653. ;; VC diff for the root directory produces output that is
  1654. ;; relative to it. Bind default-directory to the root directory
  1655. ;; here, this way the *vc-diff* buffer is setup correctly, so
  1656. ;; relative file names work.
  1657. (let ((default-directory rootdir))
  1658. (vc-diff-internal
  1659. t (list backend (list rootdir) working-revision) nil nil
  1660. (called-interactively-p 'interactive))))))
  1661. ;;;###autoload
  1662. (defun vc-revision-other-window (rev)
  1663. "Visit revision REV of the current file in another window.
  1664. If the current file is named `F', the revision is named `F.~REV~'.
  1665. If `F.~REV~' already exists, use it instead of checking it out again."
  1666. (interactive
  1667. (save-current-buffer
  1668. (vc-ensure-vc-buffer)
  1669. (list
  1670. (vc-read-revision "Revision to visit (default is working revision): "
  1671. (list buffer-file-name)))))
  1672. (vc-ensure-vc-buffer)
  1673. (let* ((file buffer-file-name)
  1674. (revision (if (string-equal rev "")
  1675. (vc-working-revision file)
  1676. rev)))
  1677. (switch-to-buffer-other-window (vc-find-revision file revision))))
  1678. (defun vc-find-revision (file revision &optional backend)
  1679. "Read REVISION of FILE into a buffer and return the buffer.
  1680. Use BACKEND as the VC backend if specified."
  1681. (let ((automatic-backup (vc-version-backup-file-name file revision))
  1682. (filebuf (or (get-file-buffer file) (current-buffer)))
  1683. (filename (vc-version-backup-file-name file revision 'manual)))
  1684. (unless (file-exists-p filename)
  1685. (if (file-exists-p automatic-backup)
  1686. (rename-file automatic-backup filename nil)
  1687. (message "Checking out %s..." filename)
  1688. (with-current-buffer filebuf
  1689. (let ((failed t))
  1690. (unwind-protect
  1691. (let ((coding-system-for-read 'no-conversion)
  1692. (coding-system-for-write 'no-conversion))
  1693. (with-temp-file filename
  1694. (let ((outbuf (current-buffer)))
  1695. ;; Change buffer to get local value of
  1696. ;; vc-checkout-switches.
  1697. (with-current-buffer filebuf
  1698. (if backend
  1699. (vc-call-backend backend 'find-revision file revision outbuf)
  1700. (vc-call find-revision file revision outbuf)))))
  1701. (setq failed nil))
  1702. (when (and failed (file-exists-p filename))
  1703. (delete-file filename))))
  1704. (vc-mode-line file))
  1705. (message "Checking out %s...done" filename)))
  1706. (let ((result-buf (find-file-noselect filename)))
  1707. (with-current-buffer result-buf
  1708. ;; Set the parent buffer so that things like
  1709. ;; C-x v g, C-x v l, ... etc work.
  1710. (set (make-local-variable 'vc-parent-buffer) filebuf))
  1711. result-buf)))
  1712. ;; Header-insertion code
  1713. ;;;###autoload
  1714. (defun vc-insert-headers ()
  1715. "Insert headers into a file for use with a version control system.
  1716. Headers desired are inserted at point, and are pulled from
  1717. the variable `vc-BACKEND-header'."
  1718. (interactive)
  1719. (vc-ensure-vc-buffer)
  1720. (save-excursion
  1721. (save-restriction
  1722. (widen)
  1723. (when (or (not (vc-check-headers))
  1724. (y-or-n-p "Version headers already exist. Insert another set? "))
  1725. (let* ((delims (cdr (assq major-mode vc-comment-alist)))
  1726. (comment-start-vc (or (car delims) comment-start "#"))
  1727. (comment-end-vc (or (car (cdr delims)) comment-end ""))
  1728. (hdsym (vc-make-backend-sym (vc-backend buffer-file-name)
  1729. 'header))
  1730. (hdstrings (and (boundp hdsym) (symbol-value hdsym))))
  1731. (dolist (s hdstrings)
  1732. (insert comment-start-vc "\t" s "\t"
  1733. comment-end-vc "\n"))
  1734. (when vc-static-header-alist
  1735. (dolist (f vc-static-header-alist)
  1736. (when (string-match (car f) buffer-file-name)
  1737. (insert (format (cdr f) (car hdstrings)))))))))))
  1738. (defun vc-clear-headers (&optional file)
  1739. "Clear all version headers in the current buffer (or FILE).
  1740. The headers are reset to their non-expanded form."
  1741. (let* ((filename (or file buffer-file-name))
  1742. (visited (find-buffer-visiting filename))
  1743. (backend (vc-backend filename)))
  1744. (when (vc-find-backend-function backend 'clear-headers)
  1745. (if visited
  1746. (let ((context (vc-buffer-context)))
  1747. ;; save-excursion may be able to relocate point and mark
  1748. ;; properly. If it fails, vc-restore-buffer-context
  1749. ;; will give it a second try.
  1750. (save-excursion
  1751. (vc-call-backend backend 'clear-headers))
  1752. (vc-restore-buffer-context context))
  1753. (set-buffer (find-file-noselect filename))
  1754. (vc-call-backend backend 'clear-headers)
  1755. (kill-buffer filename)))))
  1756. (defun vc-modify-change-comment (files rev oldcomment)
  1757. "Edit the comment associated with the given files and revision."
  1758. ;; Less of a kluge than it looks like; log-view mode only passes
  1759. ;; this function a singleton list. Arguments left in this form in
  1760. ;; case the more general operation ever becomes meaningful.
  1761. (let ((backend (vc-responsible-backend (car files))))
  1762. (vc-start-logentry
  1763. files oldcomment t
  1764. "Enter a replacement change comment."
  1765. "*vc-log*"
  1766. (lambda () (vc-call-backend backend 'log-edit-mode))
  1767. (lexical-let ((rev rev)
  1768. (backend backend))
  1769. (lambda (files comment)
  1770. (vc-call-backend backend
  1771. 'modify-change-comment files rev comment))))))
  1772. ;;;###autoload
  1773. (defun vc-merge ()
  1774. "Perform a version control merge operation.
  1775. You must be visiting a version controlled file, or in a `vc-dir' buffer.
  1776. On a distributed version control system, this runs a \"merge\"
  1777. operation to incorporate changes from another branch onto the
  1778. current branch, prompting for an argument list.
  1779. On a non-distributed version control system, this merges changes
  1780. between two revisions into the current fileset. This asks for
  1781. two revisions to merge from in the minibuffer. If the first
  1782. revision is a branch number, then merge all changes from that
  1783. branch. If the first revision is empty, merge the most recent
  1784. changes from the current branch."
  1785. (interactive)
  1786. (let* ((vc-fileset (vc-deduce-fileset t))
  1787. (backend (car vc-fileset))
  1788. (files (cadr vc-fileset)))
  1789. (cond
  1790. ;; If a branch-merge operation is defined, use it.
  1791. ((vc-find-backend-function backend 'merge-branch)
  1792. (vc-call-backend backend 'merge-branch))
  1793. ;; Otherwise, do a per-file merge.
  1794. ((vc-find-backend-function backend 'merge)
  1795. (vc-buffer-sync)
  1796. (dolist (file files)
  1797. (let* ((state (vc-state file))
  1798. first-revision second-revision status)
  1799. (cond
  1800. ((stringp state) ;; Locking VCses only
  1801. (error "File %s is locked by %s" file state))
  1802. ((not (vc-editable-p file))
  1803. (vc-checkout file t)))
  1804. (setq first-revision
  1805. (vc-read-revision
  1806. (concat "Merge " file
  1807. " from branch or revision "
  1808. "(default news on current branch): ")
  1809. (list file)
  1810. backend))
  1811. (cond
  1812. ((string= first-revision "")
  1813. (setq status (vc-call-backend backend 'merge-news file)))
  1814. (t
  1815. (if (not (vc-branch-p first-revision))
  1816. (setq second-revision
  1817. (vc-read-revision
  1818. "Second revision: "
  1819. (list file) backend nil
  1820. ;; FIXME: This is CVS/RCS/SCCS specific.
  1821. (concat (vc-branch-part first-revision) ".")))
  1822. ;; We want to merge an entire branch. Set revisions
  1823. ;; accordingly, so that vc-BACKEND-merge understands us.
  1824. (setq second-revision first-revision)
  1825. ;; first-revision must be the starting point of the branch
  1826. (setq first-revision (vc-branch-part first-revision)))
  1827. (setq status (vc-call-backend backend 'merge file
  1828. first-revision second-revision))))
  1829. (vc-maybe-resolve-conflicts file status "WORKFILE" "MERGE SOURCE"))))
  1830. (t
  1831. (error "Sorry, merging is not implemented for %s" backend)))))
  1832. (defun vc-maybe-resolve-conflicts (file status &optional name-A name-B)
  1833. (vc-resynch-buffer file t (not (buffer-modified-p)))
  1834. (if (zerop status) (message "Merge successful")
  1835. (smerge-mode 1)
  1836. (message "File contains conflicts.")))
  1837. ;;;###autoload
  1838. (defalias 'vc-resolve-conflicts 'smerge-ediff)
  1839. ;; TODO: This is OK but maybe we could integrate it better.
  1840. ;; E.g. it could be run semi-automatically (via a prompt?) when saving a file
  1841. ;; that was conflicted (i.e. upon mark-resolved).
  1842. ;; FIXME: should we add an "other-window" version? Or maybe we should
  1843. ;; hook it inside find-file so it automatically works for
  1844. ;; find-file-other-window as well. E.g. find-file could use a new
  1845. ;; `default-next-file' variable for its default file (M-n), and
  1846. ;; we could then set it upon mark-resolve, so C-x C-s C-x C-f M-n would
  1847. ;; automatically offer the next conflicted file.
  1848. (defun vc-find-conflicted-file ()
  1849. "Visit the next conflicted file in the current project."
  1850. (interactive)
  1851. (let* ((backend (or (if buffer-file-name (vc-backend buffer-file-name))
  1852. (vc-responsible-backend default-directory)
  1853. (error "No VC backend")))
  1854. (files (vc-call-backend backend
  1855. 'conflicted-files default-directory)))
  1856. ;; Don't try and visit the current file.
  1857. (if (equal (car files) buffer-file-name) (pop files))
  1858. (if (null files)
  1859. (message "No more conflicted files")
  1860. (find-file (pop files))
  1861. (message "%s more conflicted files after this one"
  1862. (if files (length files) "No")))))
  1863. ;; Named-configuration entry points
  1864. (defun vc-tag-precondition (dir)
  1865. "Scan the tree below DIR, looking for files not up-to-date.
  1866. If any file is not up-to-date, return the name of the first such file.
  1867. \(This means, neither tag creation nor retrieval is allowed.\)
  1868. If one or more of the files are currently visited, return `visited'.
  1869. Otherwise, return nil."
  1870. (let ((status nil))
  1871. (catch 'vc-locked-example
  1872. (vc-file-tree-walk
  1873. dir
  1874. (lambda (f)
  1875. (if (not (vc-up-to-date-p f)) (throw 'vc-locked-example f)
  1876. (when (get-file-buffer f) (setq status 'visited)))))
  1877. status)))
  1878. ;;;###autoload
  1879. (defun vc-create-tag (dir name branchp)
  1880. "Descending recursively from DIR, make a tag called NAME.
  1881. For each registered file, the working revision becomes part of
  1882. the named configuration. If the prefix argument BRANCHP is
  1883. given, the tag is made as a new branch and the files are
  1884. checked out in that new branch."
  1885. (interactive
  1886. (let ((granularity
  1887. (vc-call-backend (vc-responsible-backend default-directory)
  1888. 'revision-granularity)))
  1889. (list
  1890. (if (eq granularity 'repository)
  1891. ;; For VC's that do not work at file level, it's pointless
  1892. ;; to ask for a directory, branches are created at repository level.
  1893. default-directory
  1894. (read-directory-name "Directory: " default-directory default-directory t))
  1895. (read-string (if current-prefix-arg "New branch name: " "New tag name: "))
  1896. current-prefix-arg)))
  1897. (message "Making %s... " (if branchp "branch" "tag"))
  1898. (when (file-directory-p dir) (setq dir (file-name-as-directory dir)))
  1899. (vc-call-backend (vc-responsible-backend dir)
  1900. 'create-tag dir name branchp)
  1901. (vc-resynch-buffer dir t t t)
  1902. (message "Making %s... done" (if branchp "branch" "tag")))
  1903. ;;;###autoload
  1904. (defun vc-retrieve-tag (dir name)
  1905. "Descending recursively from DIR, retrieve the tag called NAME.
  1906. If NAME is empty, it refers to the latest revisions.
  1907. If locking is used for the files in DIR, then there must not be any
  1908. locked files at or below DIR (but if NAME is empty, locked files are
  1909. allowed and simply skipped)."
  1910. (interactive
  1911. (let ((granularity
  1912. (vc-call-backend (vc-responsible-backend default-directory)
  1913. 'revision-granularity)))
  1914. (list
  1915. (if (eq granularity 'repository)
  1916. ;; For VC's that do not work at file level, it's pointless
  1917. ;; to ask for a directory, branches are created at repository level.
  1918. default-directory
  1919. (read-directory-name "Directory: " default-directory default-directory t))
  1920. (read-string "Tag name to retrieve (default latest revisions): "))))
  1921. (let ((update (yes-or-no-p "Update any affected buffers? "))
  1922. (msg (if (or (not name) (string= name ""))
  1923. (format "Updating %s... " (abbreviate-file-name dir))
  1924. (format "Retrieving tag into %s... "
  1925. (abbreviate-file-name dir)))))
  1926. (message "%s" msg)
  1927. (vc-call-backend (vc-responsible-backend dir)
  1928. 'retrieve-tag dir name update)
  1929. (vc-resynch-buffer dir t t t)
  1930. (message "%s" (concat msg "done"))))
  1931. ;; Miscellaneous other entry points
  1932. ;; FIXME: this should be a defcustom
  1933. ;; FIXME: maybe add another choice:
  1934. ;; `root-directory' (or somesuch), which would mean show a short log
  1935. ;; for the root directory.
  1936. (defvar vc-log-short-style '(directory)
  1937. "Whether or not to show a short log.
  1938. If it contains `directory' then if the fileset contains a directory show a short log.
  1939. If it contains `file' then show short logs for files.
  1940. Not all VC backends support short logs!")
  1941. (defvar log-view-vc-fileset)
  1942. (defun vc-print-log-setup-buttons (working-revision is-start-revision limit pl-return)
  1943. (when (and limit (not (eq 'limit-unsupported pl-return))
  1944. (not is-start-revision))
  1945. (goto-char (point-max))
  1946. (lexical-let ((working-revision working-revision)
  1947. (limit limit))
  1948. (insert "\n")
  1949. (insert-text-button "Show 2X entries"
  1950. 'action (lambda (&rest ignore)
  1951. (vc-print-log-internal
  1952. log-view-vc-backend log-view-vc-fileset
  1953. working-revision nil (* 2 limit)))
  1954. 'help-echo "Show the log again, and double the number of log entries shown")
  1955. (insert " ")
  1956. (insert-text-button "Show unlimited entries"
  1957. 'action (lambda (&rest ignore)
  1958. (vc-print-log-internal
  1959. log-view-vc-backend log-view-vc-fileset
  1960. working-revision nil nil))
  1961. 'help-echo "Show the log again, including all entries"))))
  1962. (defun vc-print-log-internal (backend files working-revision
  1963. &optional is-start-revision limit)
  1964. ;; Don't switch to the output buffer before running the command,
  1965. ;; so that any buffer-local settings in the vc-controlled
  1966. ;; buffer can be accessed by the command.
  1967. (let ((dir-present nil)
  1968. (vc-short-log nil)
  1969. (buffer-name "*vc-change-log*")
  1970. type
  1971. pl-return)
  1972. (dolist (file files)
  1973. (when (file-directory-p file)
  1974. (setq dir-present t)))
  1975. (setq vc-short-log
  1976. (not (null (if dir-present
  1977. (memq 'directory vc-log-short-style)
  1978. (memq 'file vc-log-short-style)))))
  1979. (setq type (if vc-short-log 'short 'long))
  1980. (lexical-let
  1981. ((working-revision working-revision)
  1982. (backend backend)
  1983. (limit limit)
  1984. (shortlog vc-short-log)
  1985. (files files)
  1986. (is-start-revision is-start-revision))
  1987. (vc-log-internal-common
  1988. backend buffer-name files type
  1989. (lambda (bk buf type-arg files-arg)
  1990. (vc-call-backend bk 'print-log files-arg buf
  1991. shortlog (when is-start-revision working-revision) limit))
  1992. (lambda (bk files-arg ret)
  1993. (vc-print-log-setup-buttons working-revision
  1994. is-start-revision limit ret))
  1995. (lambda (bk)
  1996. (vc-call-backend bk 'show-log-entry working-revision))
  1997. (lambda (ignore-auto noconfirm)
  1998. (vc-print-log-internal backend files working-revision is-start-revision limit))))))
  1999. (defvar vc-log-view-type nil
  2000. "Set this to differentiate the different types of logs.")
  2001. (put 'vc-log-view-type 'permanent-local t)
  2002. (defun vc-log-internal-common (backend
  2003. buffer-name
  2004. files
  2005. type
  2006. backend-func
  2007. setup-buttons-func
  2008. goto-location-func
  2009. rev-buff-func)
  2010. (let (retval)
  2011. (with-current-buffer (get-buffer-create buffer-name)
  2012. (set (make-local-variable 'vc-log-view-type) type))
  2013. (setq retval (funcall backend-func backend buffer-name type files))
  2014. (pop-to-buffer buffer-name)
  2015. (let ((inhibit-read-only t))
  2016. ;; log-view-mode used to be called with inhibit-read-only bound
  2017. ;; to t, so let's keep doing it, just in case.
  2018. (vc-call-backend backend 'log-view-mode)
  2019. (set (make-local-variable 'log-view-vc-backend) backend)
  2020. (set (make-local-variable 'log-view-vc-fileset) files)
  2021. (set (make-local-variable 'revert-buffer-function)
  2022. rev-buff-func))
  2023. (vc-exec-after
  2024. `(let ((inhibit-read-only t))
  2025. (funcall ',setup-buttons-func ',backend ',files ',retval)
  2026. (shrink-window-if-larger-than-buffer)
  2027. (funcall ',goto-location-func ',backend)
  2028. (setq vc-sentinel-movepoint (point))
  2029. (set-buffer-modified-p nil)))))
  2030. (defun vc-incoming-outgoing-internal (backend remote-location buffer-name type)
  2031. (vc-log-internal-common
  2032. backend buffer-name nil type
  2033. (lexical-let
  2034. ((remote-location remote-location))
  2035. (lambda (bk buf type-arg files)
  2036. (vc-call-backend bk type-arg buf remote-location)))
  2037. (lambda (bk files-arg ret))
  2038. (lambda (bk)
  2039. (goto-char (point-min)))
  2040. (lexical-let
  2041. ((backend backend)
  2042. (remote-location remote-location)
  2043. (buffer-name buffer-name)
  2044. (type type))
  2045. (lambda (ignore-auto noconfirm)
  2046. (vc-incoming-outgoing-internal backend remote-location buffer-name type)))))
  2047. ;;;###autoload
  2048. (defun vc-print-log (&optional working-revision limit)
  2049. "List the change log of the current fileset in a window.
  2050. If WORKING-REVISION is non-nil, leave point at that revision.
  2051. If LIMIT is non-nil, it should be a number specifying the maximum
  2052. number of revisions to show; the default is `vc-log-show-limit'.
  2053. When called interactively with a prefix argument, prompt for
  2054. WORKING-REVISION and LIMIT."
  2055. (interactive
  2056. (cond
  2057. (current-prefix-arg
  2058. (let ((rev (read-from-minibuffer "Log from revision (default: last revision): " nil
  2059. nil nil nil))
  2060. (lim (string-to-number
  2061. (read-from-minibuffer
  2062. "Limit display (unlimited: 0): "
  2063. (format "%s" vc-log-show-limit)
  2064. nil nil nil))))
  2065. (when (string= rev "") (setq rev nil))
  2066. (when (<= lim 0) (setq lim nil))
  2067. (list rev lim)))
  2068. (t
  2069. (list nil (when (> vc-log-show-limit 0) vc-log-show-limit)))))
  2070. (let* ((vc-fileset (vc-deduce-fileset t)) ;FIXME: Why t? --Stef
  2071. (backend (car vc-fileset))
  2072. (files (cadr vc-fileset))
  2073. (working-revision (or working-revision (vc-working-revision (car files)))))
  2074. (vc-print-log-internal backend files working-revision nil limit)))
  2075. ;;;###autoload
  2076. (defun vc-print-root-log (&optional limit)
  2077. "List the change log for the current VC controlled tree in a window.
  2078. If LIMIT is non-nil, it should be a number specifying the maximum
  2079. number of revisions to show; the default is `vc-log-show-limit'.
  2080. When called interactively with a prefix argument, prompt for LIMIT."
  2081. (interactive
  2082. (cond
  2083. (current-prefix-arg
  2084. (let ((lim (string-to-number
  2085. (read-from-minibuffer
  2086. "Limit display (unlimited: 0): "
  2087. (format "%s" vc-log-show-limit)
  2088. nil nil nil))))
  2089. (when (<= lim 0) (setq lim nil))
  2090. (list lim)))
  2091. (t
  2092. (list (when (> vc-log-show-limit 0) vc-log-show-limit)))))
  2093. (let ((backend (vc-deduce-backend))
  2094. rootdir working-revision)
  2095. (unless backend
  2096. (error "Buffer is not version controlled"))
  2097. (setq rootdir (vc-call-backend backend 'root default-directory))
  2098. (setq working-revision (vc-working-revision rootdir))
  2099. (vc-print-log-internal backend (list rootdir) working-revision nil limit)))
  2100. ;;;###autoload
  2101. (defun vc-log-incoming (&optional remote-location)
  2102. "Show a log of changes that will be received with a pull operation from REMOTE-LOCATION.
  2103. When called interactively with a prefix argument, prompt for REMOTE-LOCATION.."
  2104. (interactive
  2105. (when current-prefix-arg
  2106. (list (read-string "Remote location (empty for default): "))))
  2107. (let ((backend (vc-deduce-backend))
  2108. rootdir working-revision)
  2109. (unless backend
  2110. (error "Buffer is not version controlled"))
  2111. (vc-incoming-outgoing-internal backend remote-location "*vc-incoming*" 'log-incoming)))
  2112. ;;;###autoload
  2113. (defun vc-log-outgoing (&optional remote-location)
  2114. "Show a log of changes that will be sent with a push operation to REMOTE-LOCATION.
  2115. When called interactively with a prefix argument, prompt for REMOTE-LOCATION."
  2116. (interactive
  2117. (when current-prefix-arg
  2118. (list (read-string "Remote location (empty for default): "))))
  2119. (let ((backend (vc-deduce-backend))
  2120. rootdir working-revision)
  2121. (unless backend
  2122. (error "Buffer is not version controlled"))
  2123. (vc-incoming-outgoing-internal backend remote-location "*vc-outgoing*" 'log-outgoing)))
  2124. ;;;###autoload
  2125. (defun vc-revert ()
  2126. "Revert working copies of the selected fileset to their repository contents.
  2127. This asks for confirmation if the buffer contents are not identical
  2128. to the working revision (except for keyword expansion)."
  2129. (interactive)
  2130. (let* ((vc-fileset (vc-deduce-fileset))
  2131. (files (cadr vc-fileset))
  2132. (queried nil)
  2133. diff-buffer)
  2134. ;; If any of the files is visited by the current buffer, make sure
  2135. ;; buffer is saved. If the user says `no', abort since we cannot
  2136. ;; show the changes and ask for confirmation to discard them.
  2137. (when (or (not files) (memq (buffer-file-name) files))
  2138. (vc-buffer-sync nil))
  2139. (dolist (file files)
  2140. (let ((buf (get-file-buffer file)))
  2141. (when (and buf (buffer-modified-p buf))
  2142. (error "Please kill or save all modified buffers before reverting")))
  2143. (when (vc-up-to-date-p file)
  2144. (if (yes-or-no-p (format "%s seems up-to-date. Revert anyway? " file))
  2145. (setq queried t)
  2146. (error "Revert canceled"))))
  2147. (unwind-protect
  2148. (when (if vc-revert-show-diff
  2149. (progn
  2150. (setq diff-buffer (generate-new-buffer-name "*vc-diff*"))
  2151. (vc-diff-internal vc-allow-async-revert vc-fileset
  2152. nil nil nil diff-buffer))
  2153. ;; Avoid querying the user again.
  2154. (null queried))
  2155. (unless (yes-or-no-p
  2156. (format "Discard changes in %s? "
  2157. (let ((str (vc-delistify files))
  2158. (nfiles (length files)))
  2159. (if (< (length str) 50)
  2160. str
  2161. (format "%d file%s" nfiles
  2162. (if (= nfiles 1) "" "s"))))))
  2163. (error "Revert canceled")))
  2164. (when diff-buffer
  2165. (quit-windows-on diff-buffer t)))
  2166. (dolist (file files)
  2167. (message "Reverting %s..." (vc-delistify files))
  2168. (vc-revert-file file)
  2169. (message "Reverting %s...done" (vc-delistify files)))))
  2170. ;;;###autoload
  2171. (defun vc-rollback ()
  2172. "Roll back (remove) the most recent changeset committed to the repository.
  2173. This may be either a file-level or a repository-level operation,
  2174. depending on the underlying version-control system."
  2175. (interactive)
  2176. (let* ((vc-fileset (vc-deduce-fileset))
  2177. (backend (car vc-fileset))
  2178. (files (cadr vc-fileset))
  2179. (granularity (vc-call-backend backend 'revision-granularity)))
  2180. (unless (vc-find-backend-function backend 'rollback)
  2181. (error "Rollback is not supported in %s" backend))
  2182. (when (and (not (eq granularity 'repository)) (/= (length files) 1))
  2183. (error "Rollback requires a singleton fileset or repository versioning"))
  2184. ;; FIXME: latest-on-branch-p should take the fileset.
  2185. (when (not (vc-call-backend backend 'latest-on-branch-p (car files)))
  2186. (error "Rollback is only possible at the tip revision"))
  2187. ;; If any of the files is visited by the current buffer, make
  2188. ;; sure buffer is saved. If the user says `no', abort since
  2189. ;; we cannot show the changes and ask for confirmation to
  2190. ;; discard them.
  2191. (when (or (not files) (memq (buffer-file-name) files))
  2192. (vc-buffer-sync nil))
  2193. (dolist (file files)
  2194. (when (buffer-modified-p (get-file-buffer file))
  2195. (error "Please kill or save all modified buffers before rollback"))
  2196. (when (not (vc-up-to-date-p file))
  2197. (error "Please revert all modified workfiles before rollback")))
  2198. ;; Accumulate changes associated with the fileset
  2199. (vc-setup-buffer "*vc-diff*")
  2200. (not-modified)
  2201. (message "Finding changes...")
  2202. (let* ((tip (vc-working-revision (car files)))
  2203. ;; FIXME: `previous-revision' should take the fileset.
  2204. (previous (vc-call-backend backend 'previous-revision
  2205. (car files) tip)))
  2206. (vc-diff-internal nil vc-fileset previous tip))
  2207. ;; Display changes
  2208. (unless (yes-or-no-p "Discard these revisions? ")
  2209. (error "Rollback canceled"))
  2210. (quit-windows-on "*vc-diff*" t)
  2211. ;; Do the actual reversions
  2212. (message "Rolling back %s..." (vc-delistify files))
  2213. (with-vc-properties
  2214. files
  2215. (vc-call-backend backend 'rollback files)
  2216. `((vc-state . ,'up-to-date)
  2217. (vc-checkout-time . , (nth 5 (file-attributes file)))
  2218. (vc-working-revision . nil)))
  2219. (dolist (f files) (vc-resynch-buffer f t t))
  2220. (message "Rolling back %s...done" (vc-delistify files))))
  2221. ;;;###autoload
  2222. (define-obsolete-function-alias 'vc-revert-buffer 'vc-revert "23.1")
  2223. ;;;###autoload
  2224. (defun vc-pull (&optional arg)
  2225. "Update the current fileset or branch.
  2226. You must be visiting a version controlled file, or in a `vc-dir' buffer.
  2227. On a distributed version control system, this runs a \"pull\"
  2228. operation to update the current branch, prompting for an argument
  2229. list if required. Optional prefix ARG forces a prompt.
  2230. On a non-distributed version control system, update the current
  2231. fileset to the tip revisions. For each unchanged and unlocked
  2232. file, this simply replaces the work file with the latest revision
  2233. on its branch. If the file contains changes, any changes in the
  2234. tip revision are merged into the working file."
  2235. (interactive "P")
  2236. (let* ((vc-fileset (vc-deduce-fileset t))
  2237. (backend (car vc-fileset))
  2238. (files (cadr vc-fileset)))
  2239. (cond
  2240. ;; If a pull operation is defined, use it.
  2241. ((vc-find-backend-function backend 'pull)
  2242. (vc-call-backend backend 'pull arg))
  2243. ;; If VCS has `merge-news' functionality (CVS and SVN), use it.
  2244. ((vc-find-backend-function backend 'merge-news)
  2245. (save-some-buffers ; save buffers visiting files
  2246. nil (lambda ()
  2247. (and (buffer-modified-p)
  2248. (let ((file (buffer-file-name)))
  2249. (and file (member file files))))))
  2250. (dolist (file files)
  2251. (if (vc-up-to-date-p file)
  2252. (vc-checkout file nil t)
  2253. (vc-maybe-resolve-conflicts
  2254. file (vc-call-backend backend 'merge-news file)))))
  2255. ;; For a locking VCS, check out each file.
  2256. ((eq (vc-checkout-model backend files) 'locking)
  2257. (dolist (file files)
  2258. (if (vc-up-to-date-p file)
  2259. (vc-checkout file nil t))))
  2260. (t
  2261. (error "VC update is unsupported for `%s'" backend)))))
  2262. ;;;###autoload
  2263. (defalias 'vc-update 'vc-pull)
  2264. (defun vc-version-backup-file (file &optional rev)
  2265. "Return name of backup file for revision REV of FILE.
  2266. If version backups should be used for FILE, and there exists
  2267. such a backup for REV or the working revision of file, return
  2268. its name; otherwise return nil."
  2269. (when (vc-call make-version-backups-p file)
  2270. (let ((backup-file (vc-version-backup-file-name file rev)))
  2271. (if (file-exists-p backup-file)
  2272. backup-file
  2273. ;; there is no automatic backup, but maybe the user made one manually
  2274. (setq backup-file (vc-version-backup-file-name file rev 'manual))
  2275. (when (file-exists-p backup-file)
  2276. backup-file)))))
  2277. (defun vc-revert-file (file)
  2278. "Revert FILE back to the repository working revision it was based on."
  2279. (with-vc-properties
  2280. (list file)
  2281. (let ((backup-file (vc-version-backup-file file)))
  2282. (when backup-file
  2283. (copy-file backup-file file 'ok-if-already-exists)
  2284. (vc-delete-automatic-version-backups file))
  2285. (vc-call revert file backup-file))
  2286. `((vc-state . up-to-date)
  2287. (vc-checkout-time . ,(nth 5 (file-attributes file)))))
  2288. (vc-resynch-buffer file t t))
  2289. ;;;###autoload
  2290. (defun vc-switch-backend (file backend)
  2291. "Make BACKEND the current version control system for FILE.
  2292. FILE must already be registered in BACKEND. The change is not
  2293. permanent, only for the current session. This function only changes
  2294. VC's perspective on FILE, it does not register or unregister it.
  2295. By default, this command cycles through the registered backends.
  2296. To get a prompt, use a prefix argument."
  2297. (interactive
  2298. (list
  2299. (or buffer-file-name
  2300. (error "There is no version-controlled file in this buffer"))
  2301. (let ((crt-bk (vc-backend buffer-file-name))
  2302. (backends nil))
  2303. (unless crt-bk
  2304. (error "File %s is not under version control" buffer-file-name))
  2305. ;; Find the registered backends.
  2306. (dolist (crt vc-handled-backends)
  2307. (when (and (vc-call-backend crt 'registered buffer-file-name)
  2308. (not (eq crt-bk crt)))
  2309. (push crt backends)))
  2310. ;; Find the next backend.
  2311. (let ((def (car backends))
  2312. (others backends))
  2313. (cond
  2314. ((null others) (error "No other backend to switch to"))
  2315. (current-prefix-arg
  2316. (intern
  2317. (upcase
  2318. (completing-read
  2319. (format "Switch to backend [%s]: " def)
  2320. (mapcar (lambda (b) (list (downcase (symbol-name b)))) backends)
  2321. nil t nil nil (downcase (symbol-name def))))))
  2322. (t def))))))
  2323. (unless (eq backend (vc-backend file))
  2324. (vc-file-clearprops file)
  2325. (vc-file-setprop file 'vc-backend backend)
  2326. ;; Force recomputation of the state
  2327. (unless (vc-call-backend backend 'registered file)
  2328. (vc-file-clearprops file)
  2329. (error "%s is not registered in %s" file backend))
  2330. (vc-mode-line file)))
  2331. ;;;###autoload
  2332. (defun vc-transfer-file (file new-backend)
  2333. "Transfer FILE to another version control system NEW-BACKEND.
  2334. If NEW-BACKEND has a higher precedence than FILE's current backend
  2335. \(i.e. it comes earlier in `vc-handled-backends'), then register FILE in
  2336. NEW-BACKEND, using the revision number from the current backend as the
  2337. base level. If NEW-BACKEND has a lower precedence than the current
  2338. backend, then commit all changes that were made under the current
  2339. backend to NEW-BACKEND, and unregister FILE from the current backend.
  2340. \(If FILE is not yet registered under NEW-BACKEND, register it.)"
  2341. (let* ((old-backend (vc-backend file))
  2342. (edited (memq (vc-state file) '(edited needs-merge)))
  2343. (registered (vc-call-backend new-backend 'registered file))
  2344. (move
  2345. (and registered ; Never move if not registered in new-backend yet.
  2346. ;; move if new-backend comes later in vc-handled-backends
  2347. (or (memq new-backend (memq old-backend vc-handled-backends))
  2348. (y-or-n-p "Final transfer? "))))
  2349. (comment nil))
  2350. (when (eq old-backend new-backend)
  2351. (error "%s is the current backend of %s" new-backend file))
  2352. (if registered
  2353. (set-file-modes file (logior (file-modes file) 128))
  2354. ;; `registered' might have switched under us.
  2355. (vc-switch-backend file old-backend)
  2356. (let* ((rev (vc-working-revision file))
  2357. (modified-file (and edited (make-temp-file file)))
  2358. (unmodified-file (and modified-file (vc-version-backup-file file))))
  2359. ;; Go back to the base unmodified file.
  2360. (unwind-protect
  2361. (progn
  2362. (when modified-file
  2363. (copy-file file modified-file 'ok-if-already-exists)
  2364. ;; If we have a local copy of the unmodified file, handle that
  2365. ;; here and not in vc-revert-file because we don't want to
  2366. ;; delete that copy -- it is still useful for OLD-BACKEND.
  2367. (if unmodified-file
  2368. (copy-file unmodified-file file
  2369. 'ok-if-already-exists 'keep-date)
  2370. (when (y-or-n-p "Get base revision from repository? ")
  2371. (vc-revert-file file))))
  2372. (vc-call-backend new-backend 'receive-file file rev))
  2373. (when modified-file
  2374. (vc-switch-backend file new-backend)
  2375. (unless (eq (vc-checkout-model new-backend (list file)) 'implicit)
  2376. (vc-checkout file t nil))
  2377. (rename-file modified-file file 'ok-if-already-exists)
  2378. (vc-file-setprop file 'vc-checkout-time nil)))))
  2379. (when move
  2380. (vc-switch-backend file old-backend)
  2381. (setq comment (vc-call-backend old-backend 'comment-history file))
  2382. (vc-call-backend old-backend 'unregister file))
  2383. (vc-switch-backend file new-backend)
  2384. (when (or move edited)
  2385. (vc-file-setprop file 'vc-state 'edited)
  2386. (vc-mode-line file new-backend)
  2387. (vc-checkin file new-backend nil comment (stringp comment)))))
  2388. (defun vc-rename-master (oldmaster newfile templates)
  2389. "Rename OLDMASTER to be the master file for NEWFILE based on TEMPLATES."
  2390. (let* ((dir (file-name-directory (expand-file-name oldmaster)))
  2391. (newdir (or (file-name-directory newfile) ""))
  2392. (newbase (file-name-nondirectory newfile))
  2393. (masters
  2394. ;; List of potential master files for `newfile'
  2395. (mapcar
  2396. (lambda (s) (vc-possible-master s newdir newbase))
  2397. templates)))
  2398. (when (or (file-symlink-p oldmaster)
  2399. (file-symlink-p (file-name-directory oldmaster)))
  2400. (error "This is unsafe in the presence of symbolic links"))
  2401. (rename-file
  2402. oldmaster
  2403. (catch 'found
  2404. ;; If possible, keep the master file in the same directory.
  2405. (dolist (f masters)
  2406. (when (and f (string= (file-name-directory (expand-file-name f)) dir))
  2407. (throw 'found f)))
  2408. ;; If not, just use the first possible place.
  2409. (dolist (f masters)
  2410. (and f (or (not (setq dir (file-name-directory f)))
  2411. (file-directory-p dir))
  2412. (throw 'found f)))
  2413. (error "New file lacks a version control directory")))))
  2414. ;;;###autoload
  2415. (defun vc-delete-file (file)
  2416. "Delete file and mark it as such in the version control system."
  2417. (interactive "fVC delete file: ")
  2418. (setq file (expand-file-name file))
  2419. (let ((buf (get-file-buffer file))
  2420. (backend (vc-backend file)))
  2421. (unless backend
  2422. (error "File %s is not under version control"
  2423. (file-name-nondirectory file)))
  2424. (unless (vc-find-backend-function backend 'delete-file)
  2425. (error "Deleting files under %s is not supported in VC" backend))
  2426. (when (and buf (buffer-modified-p buf))
  2427. (error "Please save or undo your changes before deleting %s" file))
  2428. (let ((state (vc-state file)))
  2429. (when (eq state 'edited)
  2430. (error "Please commit or undo your changes before deleting %s" file))
  2431. (when (eq state 'conflict)
  2432. (error "Please resolve the conflicts before deleting %s" file)))
  2433. (unless (y-or-n-p (format "Really want to delete %s? "
  2434. (file-name-nondirectory file)))
  2435. (error "Abort!"))
  2436. (unless (or (file-directory-p file) (null make-backup-files)
  2437. (not (file-exists-p file)))
  2438. (with-current-buffer (or buf (find-file-noselect file))
  2439. (let ((backup-inhibited nil))
  2440. (backup-buffer))))
  2441. ;; Bind `default-directory' so that the command that the backend
  2442. ;; runs to remove the file is invoked in the correct context.
  2443. (let ((default-directory (file-name-directory file)))
  2444. (vc-call-backend backend 'delete-file file))
  2445. ;; If the backend hasn't deleted the file itself, let's do it for him.
  2446. (when (file-exists-p file) (delete-file file))
  2447. ;; Forget what VC knew about the file.
  2448. (vc-file-clearprops file)
  2449. ;; Make sure the buffer is deleted and the *vc-dir* buffers are
  2450. ;; updated after this.
  2451. (vc-resynch-buffer file nil t)))
  2452. ;;;###autoload
  2453. (defun vc-rename-file (old new)
  2454. "Rename file OLD to NEW in both work area and repository."
  2455. (interactive "fVC rename file: \nFRename to: ")
  2456. ;; in CL I would have said (setq new (merge-pathnames new old))
  2457. (let ((old-base (file-name-nondirectory old)))
  2458. (when (and (not (string= "" old-base))
  2459. (string= "" (file-name-nondirectory new)))
  2460. (setq new (concat new old-base))))
  2461. (let ((oldbuf (get-file-buffer old)))
  2462. (when (and oldbuf (buffer-modified-p oldbuf))
  2463. (error "Please save files before moving them"))
  2464. (when (get-file-buffer new)
  2465. (error "Already editing new file name"))
  2466. (when (file-exists-p new)
  2467. (error "New file already exists"))
  2468. (let ((state (vc-state old)))
  2469. (unless (memq state '(up-to-date edited))
  2470. (error "Please %s files before moving them"
  2471. (if (stringp state) "check in" "update"))))
  2472. (vc-call rename-file old new)
  2473. (vc-file-clearprops old)
  2474. ;; Move the actual file (unless the backend did it already)
  2475. (when (file-exists-p old) (rename-file old new))
  2476. ;; ?? Renaming a file might change its contents due to keyword expansion.
  2477. ;; We should really check out a new copy if the old copy was precisely equal
  2478. ;; to some checked-in revision. However, testing for this is tricky....
  2479. (when oldbuf
  2480. (with-current-buffer oldbuf
  2481. (let ((buffer-read-only buffer-read-only))
  2482. (set-visited-file-name new))
  2483. (vc-mode-line new (vc-backend new))
  2484. (set-buffer-modified-p nil)))))
  2485. ;;;###autoload
  2486. (defun vc-update-change-log (&rest args)
  2487. "Find change log file and add entries from recent version control logs.
  2488. Normally, find log entries for all registered files in the default
  2489. directory.
  2490. With prefix arg of \\[universal-argument], only find log entries for the current buffer's file.
  2491. With any numeric prefix arg, find log entries for all currently visited
  2492. files that are under version control. This puts all the entries in the
  2493. log for the default directory, which may not be appropriate.
  2494. From a program, any ARGS are assumed to be filenames for which
  2495. log entries should be gathered."
  2496. (interactive
  2497. (cond ((consp current-prefix-arg) ;C-u
  2498. (list buffer-file-name))
  2499. (current-prefix-arg ;Numeric argument.
  2500. (let ((files nil)
  2501. (buffers (buffer-list))
  2502. file)
  2503. (while buffers
  2504. (setq file (buffer-file-name (car buffers)))
  2505. (and file (vc-backend file)
  2506. (setq files (cons file files)))
  2507. (setq buffers (cdr buffers)))
  2508. files))
  2509. (t
  2510. ;; Don't supply any filenames to backend; this means
  2511. ;; it should find all relevant files relative to
  2512. ;; the default-directory.
  2513. nil)))
  2514. (vc-call-backend (vc-responsible-backend default-directory)
  2515. 'update-changelog args))
  2516. ;; functions that operate on RCS revision numbers. This code should
  2517. ;; also be moved into the backends. It stays for now, however, since
  2518. ;; it is used in code below.
  2519. (defun vc-branch-p (rev)
  2520. "Return t if REV is a branch revision."
  2521. (not (eq nil (string-match "\\`[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*\\'" rev))))
  2522. ;;;###autoload
  2523. (defun vc-branch-part (rev)
  2524. "Return the branch part of a revision number REV."
  2525. (let ((index (string-match "\\.[0-9]+\\'" rev)))
  2526. (when index
  2527. (substring rev 0 index))))
  2528. (defun vc-default-responsible-p (backend file)
  2529. "Indicate whether BACKEND is responsible for FILE.
  2530. The default is to return nil always."
  2531. nil)
  2532. (defun vc-default-could-register (backend file)
  2533. "Return non-nil if BACKEND could be used to register FILE.
  2534. The default implementation returns t for all files."
  2535. t)
  2536. (defun vc-default-latest-on-branch-p (backend file)
  2537. "Return non-nil if FILE is the latest on its branch.
  2538. This default implementation always returns non-nil, which means that
  2539. editing non-current revisions is not supported by default."
  2540. t)
  2541. (defun vc-default-init-revision (backend) vc-default-init-revision)
  2542. (defun vc-default-find-revision (backend file rev buffer)
  2543. "Provide the new `find-revision' op based on the old `checkout' op.
  2544. This is only for compatibility with old backends. They should be updated
  2545. to provide the `find-revision' operation instead."
  2546. (let ((tmpfile (make-temp-file (expand-file-name file))))
  2547. (unwind-protect
  2548. (progn
  2549. (vc-call-backend backend 'checkout file nil rev tmpfile)
  2550. (with-current-buffer buffer
  2551. (insert-file-contents-literally tmpfile)))
  2552. (delete-file tmpfile))))
  2553. (defun vc-default-rename-file (backend old new)
  2554. (condition-case nil
  2555. (add-name-to-file old new)
  2556. (error (rename-file old new)))
  2557. (vc-delete-file old)
  2558. (with-current-buffer (find-file-noselect new)
  2559. (vc-register)))
  2560. (defalias 'vc-default-check-headers 'ignore)
  2561. (declare-function log-edit-mode "log-edit" ())
  2562. (defun vc-default-log-edit-mode (backend) (log-edit-mode))
  2563. (defun vc-default-log-view-mode (backend) (log-view-mode))
  2564. (defun vc-default-show-log-entry (backend rev)
  2565. (with-no-warnings
  2566. (log-view-goto-rev rev)))
  2567. (defun vc-default-comment-history (backend file)
  2568. "Return a string with all log entries stored in BACKEND for FILE."
  2569. (when (vc-find-backend-function backend 'print-log)
  2570. (with-current-buffer "*vc*"
  2571. (vc-call-backend backend 'print-log (list file))
  2572. (buffer-string))))
  2573. (defun vc-default-receive-file (backend file rev)
  2574. "Let BACKEND receive FILE from another version control system."
  2575. (vc-call-backend backend 'register (list file) rev ""))
  2576. (defun vc-default-retrieve-tag (backend dir name update)
  2577. (if (string= name "")
  2578. (progn
  2579. (vc-file-tree-walk
  2580. dir
  2581. (lambda (f) (and
  2582. (vc-up-to-date-p f)
  2583. (vc-error-occurred
  2584. (vc-call-backend backend 'checkout f nil "")
  2585. (when update (vc-resynch-buffer f t t)))))))
  2586. (let ((result (vc-tag-precondition dir)))
  2587. (if (stringp result)
  2588. (error "File %s is locked" result)
  2589. (setq update (and (eq result 'visited) update))
  2590. (vc-file-tree-walk
  2591. dir
  2592. (lambda (f) (vc-error-occurred
  2593. (vc-call-backend backend 'checkout f nil name)
  2594. (when update (vc-resynch-buffer f t t)))))))))
  2595. (defun vc-default-revert (backend file contents-done)
  2596. (unless contents-done
  2597. (let ((rev (vc-working-revision file))
  2598. (file-buffer (or (get-file-buffer file) (current-buffer))))
  2599. (message "Checking out %s..." file)
  2600. (let ((failed t)
  2601. (backup-name (car (find-backup-file-name file))))
  2602. (when backup-name
  2603. (copy-file file backup-name 'ok-if-already-exists 'keep-date)
  2604. (unless (file-writable-p file)
  2605. (set-file-modes file (logior (file-modes file) 128))))
  2606. (unwind-protect
  2607. (let ((coding-system-for-read 'no-conversion)
  2608. (coding-system-for-write 'no-conversion))
  2609. (with-temp-file file
  2610. (let ((outbuf (current-buffer)))
  2611. ;; Change buffer to get local value of vc-checkout-switches.
  2612. (with-current-buffer file-buffer
  2613. (let ((default-directory (file-name-directory file)))
  2614. (vc-call-backend backend 'find-revision
  2615. file rev outbuf)))))
  2616. (setq failed nil))
  2617. (when backup-name
  2618. (if failed
  2619. (rename-file backup-name file 'ok-if-already-exists)
  2620. (and (not vc-make-backup-files) (delete-file backup-name))))))
  2621. (message "Checking out %s...done" file))))
  2622. (defalias 'vc-default-revision-completion-table 'ignore)
  2623. (defalias 'vc-default-mark-resolved 'ignore)
  2624. (defun vc-default-dir-status-files (backend dir files default-state update-function)
  2625. (funcall update-function
  2626. (mapcar (lambda (file) (list file default-state)) files)))
  2627. (defun vc-check-headers ()
  2628. "Check if the current file has any headers in it."
  2629. (interactive)
  2630. (vc-call-backend (vc-backend buffer-file-name) 'check-headers))
  2631. ;; These things should probably be generally available
  2632. (defun vc-string-prefix-p (prefix string)
  2633. (let ((lpref (length prefix)))
  2634. (and (>= (length string) lpref)
  2635. (eq t (compare-strings prefix nil nil string nil lpref)))))
  2636. (defun vc-file-tree-walk (dirname func &rest args)
  2637. "Walk recursively through DIRNAME.
  2638. Invoke FUNC f ARGS on each VC-managed file f underneath it."
  2639. (vc-file-tree-walk-internal (expand-file-name dirname) func args)
  2640. (message "Traversing directory %s...done" dirname))
  2641. (defun vc-file-tree-walk-internal (file func args)
  2642. (if (not (file-directory-p file))
  2643. (when (vc-backend file) (apply func file args))
  2644. (message "Traversing directory %s..." (abbreviate-file-name file))
  2645. (let ((dir (file-name-as-directory file)))
  2646. (mapcar
  2647. (lambda (f) (or
  2648. (string-equal f ".")
  2649. (string-equal f "..")
  2650. (member f vc-directory-exclusion-list)
  2651. (let ((dirf (expand-file-name f dir)))
  2652. (or
  2653. (file-symlink-p dirf) ;; Avoid possible loops.
  2654. (vc-file-tree-walk-internal dirf func args)))))
  2655. (directory-files dir)))))
  2656. (provide 'vc)
  2657. ;;; vc.el ends here