You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheCheckoutTest.java 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008-2011, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2008-2011, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2010-2011, Christian Halstrick <christian.halstrick@sap.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available under the
  9. * terms of the Eclipse Distribution License v1.0 which accompanies this
  10. * distribution, is reproduced below, and is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
  26. * contributors may be used to endorse or promote products derived from this
  27. * software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. package org.eclipse.jgit.lib;
  42. import static org.junit.Assert.assertArrayEquals;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.util.Arrays;
  52. import java.util.HashMap;
  53. import java.util.List;
  54. import java.util.Map;
  55. import org.eclipse.jgit.api.CheckoutCommand;
  56. import org.eclipse.jgit.api.CheckoutResult;
  57. import org.eclipse.jgit.api.Git;
  58. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  59. import org.eclipse.jgit.api.ResetCommand.ResetType;
  60. import org.eclipse.jgit.api.Status;
  61. import org.eclipse.jgit.api.errors.GitAPIException;
  62. import org.eclipse.jgit.api.errors.NoFilepatternException;
  63. import org.eclipse.jgit.dircache.DirCache;
  64. import org.eclipse.jgit.dircache.DirCacheCheckout;
  65. import org.eclipse.jgit.dircache.DirCacheCheckout.CheckoutMetadata;
  66. import org.eclipse.jgit.dircache.DirCacheEditor;
  67. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  68. import org.eclipse.jgit.dircache.DirCacheEntry;
  69. import org.eclipse.jgit.errors.CheckoutConflictException;
  70. import org.eclipse.jgit.errors.CorruptObjectException;
  71. import org.eclipse.jgit.errors.NoWorkTreeException;
  72. import org.eclipse.jgit.junit.RepositoryTestCase;
  73. import org.eclipse.jgit.junit.TestRepository;
  74. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  75. import org.eclipse.jgit.revwalk.RevCommit;
  76. import org.eclipse.jgit.treewalk.FileTreeIterator;
  77. import org.eclipse.jgit.treewalk.TreeWalk;
  78. import org.eclipse.jgit.util.FS;
  79. import org.eclipse.jgit.util.FileUtils;
  80. import org.junit.Assume;
  81. import org.junit.Test;
  82. public class DirCacheCheckoutTest extends RepositoryTestCase {
  83. private DirCacheCheckout dco;
  84. protected ObjectId theHead;
  85. protected ObjectId theMerge;
  86. private DirCache dirCache;
  87. private void prescanTwoTrees(ObjectId head, ObjectId merge)
  88. throws IllegalStateException, IOException {
  89. DirCache dc = db.lockDirCache();
  90. try {
  91. dco = new DirCacheCheckout(db, head, dc, merge);
  92. dco.preScanTwoTrees();
  93. } finally {
  94. dc.unlock();
  95. }
  96. }
  97. private void checkout() throws IOException {
  98. DirCache dc = db.lockDirCache();
  99. try {
  100. dco = new DirCacheCheckout(db, theHead, dc, theMerge);
  101. dco.checkout();
  102. } finally {
  103. dc.unlock();
  104. }
  105. }
  106. private List<String> getRemoved() {
  107. return dco.getRemoved();
  108. }
  109. private Map<String, CheckoutMetadata> getUpdated() {
  110. return dco.getUpdated();
  111. }
  112. private List<String> getConflicts() {
  113. return dco.getConflicts();
  114. }
  115. private static HashMap<String, String> mk(String a) {
  116. return mkmap(a, a);
  117. }
  118. private static HashMap<String, String> mkmap(String... args) {
  119. if ((args.length % 2) > 0)
  120. throw new IllegalArgumentException("needs to be pairs");
  121. HashMap<String, String> map = new HashMap<String, String>();
  122. for (int i = 0; i < args.length; i += 2) {
  123. map.put(args[i], args[i + 1]);
  124. }
  125. return map;
  126. }
  127. @Test
  128. public void testResetHard() throws IOException, NoFilepatternException,
  129. GitAPIException {
  130. try (Git git = new Git(db)) {
  131. writeTrashFile("f", "f()");
  132. writeTrashFile("D/g", "g()");
  133. git.add().addFilepattern(".").call();
  134. git.commit().setMessage("inital").call();
  135. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  136. git.branchCreate().setName("topic").call();
  137. writeTrashFile("f", "f()\nmaster");
  138. writeTrashFile("D/g", "g()\ng2()");
  139. writeTrashFile("E/h", "h()");
  140. git.add().addFilepattern(".").call();
  141. RevCommit master = git.commit().setMessage("master-1").call();
  142. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  143. checkoutBranch("refs/heads/topic");
  144. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  145. writeTrashFile("f", "f()\nside");
  146. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  147. writeTrashFile("G/i", "i()");
  148. git.add().addFilepattern(".").call();
  149. git.add().addFilepattern(".").setUpdate(true).call();
  150. RevCommit topic = git.commit().setMessage("topic-1").call();
  151. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  152. writeTrashFile("untracked", "untracked");
  153. resetHard(master);
  154. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  155. resetHard(topic);
  156. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  157. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
  158. "untracked"));
  159. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  160. .call().getMergeStatus());
  161. assertEquals(
  162. "[D/g, mode:100644, stage:1][D/g, mode:100644, stage:3][E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]",
  163. indexState(0));
  164. resetHard(master);
  165. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  166. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  167. "h()", "untracked", "untracked"));
  168. }
  169. }
  170. /**
  171. * Reset hard from unclean condition.
  172. * <p>
  173. * WorkDir: Empty <br/>
  174. * Index: f/g <br/>
  175. * Merge: x
  176. *
  177. * @throws Exception
  178. */
  179. @Test
  180. public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
  181. throws Exception {
  182. try (Git git = new Git(db)) {
  183. writeTrashFile("x", "x");
  184. git.add().addFilepattern("x").call();
  185. RevCommit id1 = git.commit().setMessage("c1").call();
  186. writeTrashFile("f/g", "f/g");
  187. git.rm().addFilepattern("x").call();
  188. git.add().addFilepattern("f/g").call();
  189. git.commit().setMessage("c2").call();
  190. deleteTrashFile("f/g");
  191. deleteTrashFile("f");
  192. // The actual test
  193. git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
  194. assertIndex(mkmap("x", "x"));
  195. }
  196. }
  197. /**
  198. * Test first checkout in a repo
  199. *
  200. * @throws Exception
  201. */
  202. @Test
  203. public void testInitialCheckout() throws Exception {
  204. try (Git git = new Git(db)) {
  205. TestRepository<Repository> db_t = new TestRepository<Repository>(db);
  206. BranchBuilder master = db_t.branch("master");
  207. master.commit().add("f", "1").message("m0").create();
  208. assertFalse(new File(db.getWorkTree(), "f").exists());
  209. git.checkout().setName("master").call();
  210. assertTrue(new File(db.getWorkTree(), "f").exists());
  211. }
  212. }
  213. private DirCacheCheckout resetHard(RevCommit commit)
  214. throws NoWorkTreeException,
  215. CorruptObjectException, IOException {
  216. DirCacheCheckout dc;
  217. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  218. commit.getTree());
  219. dc.setFailOnConflict(true);
  220. assertTrue(dc.checkout());
  221. return dc;
  222. }
  223. private void assertIndex(HashMap<String, String> i)
  224. throws CorruptObjectException, IOException {
  225. String expectedValue;
  226. String path;
  227. DirCache read = DirCache.read(db.getIndexFile(), db.getFS());
  228. assertEquals("Index has not the right size.", i.size(),
  229. read.getEntryCount());
  230. for (int j = 0; j < read.getEntryCount(); j++) {
  231. path = read.getEntry(j).getPathString();
  232. expectedValue = i.get(path);
  233. assertNotNull("found unexpected entry for path " + path
  234. + " in index", expectedValue);
  235. assertTrue("unexpected content for path " + path
  236. + " in index. Expected: <" + expectedValue + ">",
  237. Arrays.equals(db.open(read.getEntry(j).getObjectId())
  238. .getCachedBytes(), i.get(path).getBytes()));
  239. }
  240. }
  241. @Test
  242. public void testRules1thru3_NoIndexEntry() throws IOException {
  243. ObjectId head = buildTree(mk("foo"));
  244. ObjectId merge = db.newObjectInserter().insert(Constants.OBJ_TREE,
  245. new byte[0]);
  246. prescanTwoTrees(head, merge);
  247. assertTrue(getRemoved().contains("foo"));
  248. prescanTwoTrees(merge, head);
  249. assertTrue(getUpdated().containsKey("foo"));
  250. merge = buildTree(mkmap("foo", "a"));
  251. prescanTwoTrees(head, merge);
  252. assertConflict("foo");
  253. }
  254. void setupCase(HashMap<String, String> headEntries, HashMap<String, String> mergeEntries, HashMap<String, String> indexEntries) throws IOException {
  255. theHead = buildTree(headEntries);
  256. theMerge = buildTree(mergeEntries);
  257. buildIndex(indexEntries);
  258. }
  259. private void buildIndex(HashMap<String, String> indexEntries) throws IOException {
  260. dirCache = new DirCache(db.getIndexFile(), db.getFS());
  261. if (indexEntries != null) {
  262. assertTrue(dirCache.lock());
  263. DirCacheEditor editor = dirCache.editor();
  264. for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
  265. writeTrashFile(e.getKey(), e.getValue());
  266. ObjectInserter inserter = db.newObjectInserter();
  267. final ObjectId id = inserter.insert(Constants.OBJ_BLOB,
  268. Constants.encode(e.getValue()));
  269. editor.add(new DirCacheEditor.DeletePath(e.getKey()));
  270. editor.add(new DirCacheEditor.PathEdit(e.getKey()) {
  271. @Override
  272. public void apply(DirCacheEntry ent) {
  273. ent.setFileMode(FileMode.REGULAR_FILE);
  274. ent.setObjectId(id);
  275. ent.setUpdateNeeded(false);
  276. }
  277. });
  278. }
  279. assertTrue(editor.commit());
  280. }
  281. }
  282. static final class AddEdit extends PathEdit {
  283. private final ObjectId data;
  284. private final long length;
  285. public AddEdit(String entryPath, ObjectId data, long length) {
  286. super(entryPath);
  287. this.data = data;
  288. this.length = length;
  289. }
  290. @Override
  291. public void apply(DirCacheEntry ent) {
  292. ent.setFileMode(FileMode.REGULAR_FILE);
  293. ent.setLength(length);
  294. ent.setObjectId(data);
  295. }
  296. }
  297. private ObjectId buildTree(HashMap<String, String> headEntries)
  298. throws IOException {
  299. DirCache lockDirCache = DirCache.newInCore();
  300. // assertTrue(lockDirCache.lock());
  301. DirCacheEditor editor = lockDirCache.editor();
  302. if (headEntries != null) {
  303. for (java.util.Map.Entry<String, String> e : headEntries.entrySet()) {
  304. AddEdit addEdit = new AddEdit(e.getKey(),
  305. genSha1(e.getValue()), e.getValue().length());
  306. editor.add(addEdit);
  307. }
  308. }
  309. editor.finish();
  310. return lockDirCache.writeTree(db.newObjectInserter());
  311. }
  312. ObjectId genSha1(String data) {
  313. try (ObjectInserter w = db.newObjectInserter()) {
  314. ObjectId id = w.insert(Constants.OBJ_BLOB, data.getBytes());
  315. w.flush();
  316. return id;
  317. } catch (IOException e) {
  318. fail(e.toString());
  319. }
  320. return null;
  321. }
  322. protected void go() throws IllegalStateException, IOException {
  323. prescanTwoTrees(theHead, theMerge);
  324. }
  325. @Test
  326. public void testRules4thru13_IndexEntryNotInHead() throws IOException {
  327. // rules 4 and 5
  328. HashMap<String, String> idxMap;
  329. idxMap = new HashMap<String, String>();
  330. idxMap.put("foo", "foo");
  331. setupCase(null, null, idxMap);
  332. go();
  333. assertTrue(getUpdated().isEmpty());
  334. assertTrue(getRemoved().isEmpty());
  335. assertTrue(getConflicts().isEmpty());
  336. // rules 6 and 7
  337. idxMap = new HashMap<String, String>();
  338. idxMap.put("foo", "foo");
  339. setupCase(null, idxMap, idxMap);
  340. go();
  341. assertAllEmpty();
  342. // rules 8 and 9
  343. HashMap<String, String> mergeMap;
  344. mergeMap = new HashMap<String, String>();
  345. mergeMap.put("foo", "merge");
  346. setupCase(null, mergeMap, idxMap);
  347. go();
  348. assertTrue(getUpdated().isEmpty());
  349. assertTrue(getRemoved().isEmpty());
  350. assertTrue(getConflicts().contains("foo"));
  351. // rule 10
  352. HashMap<String, String> headMap = new HashMap<String, String>();
  353. headMap.put("foo", "foo");
  354. setupCase(headMap, null, idxMap);
  355. go();
  356. assertTrue(getRemoved().contains("foo"));
  357. assertTrue(getUpdated().isEmpty());
  358. assertTrue(getConflicts().isEmpty());
  359. // rule 11
  360. setupCase(headMap, null, idxMap);
  361. assertTrue(new File(trash, "foo").delete());
  362. writeTrashFile("foo", "bar");
  363. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  364. go();
  365. assertTrue(getRemoved().isEmpty());
  366. assertTrue(getUpdated().isEmpty());
  367. assertTrue(getConflicts().contains("foo"));
  368. // rule 12 & 13
  369. headMap.put("foo", "head");
  370. setupCase(headMap, null, idxMap);
  371. go();
  372. assertTrue(getRemoved().isEmpty());
  373. assertTrue(getUpdated().isEmpty());
  374. assertTrue(getConflicts().contains("foo"));
  375. // rules 14 & 15
  376. setupCase(headMap, headMap, idxMap);
  377. go();
  378. assertAllEmpty();
  379. // rules 16 & 17
  380. setupCase(headMap, mergeMap, idxMap); go();
  381. assertTrue(getConflicts().contains("foo"));
  382. // rules 18 & 19
  383. setupCase(headMap, idxMap, idxMap); go();
  384. assertAllEmpty();
  385. // rule 20
  386. setupCase(idxMap, mergeMap, idxMap); go();
  387. assertTrue(getUpdated().containsKey("foo"));
  388. // rules 21
  389. setupCase(idxMap, mergeMap, idxMap);
  390. assertTrue(new File(trash, "foo").delete());
  391. writeTrashFile("foo", "bar");
  392. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  393. go();
  394. assertTrue(getConflicts().contains("foo"));
  395. }
  396. private void assertAllEmpty() {
  397. assertTrue(getRemoved().isEmpty());
  398. assertTrue(getUpdated().isEmpty());
  399. assertTrue(getConflicts().isEmpty());
  400. }
  401. /*-
  402. * Directory/File Conflict cases:
  403. * It's entirely possible that in practice a number of these may be equivalent
  404. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  405. * that's all I care about. These are basically reverse-engineered from
  406. * what git currently does. If there are tests for these in git, it's kind of
  407. * hard to track them all down...
  408. *
  409. * H I M Clean H==M H==I I==M Result
  410. * ------------------------------------------------------------------
  411. *1 D D F Y N Y N Update
  412. *2 D D F N N Y N Conflict
  413. *3 D F D Y N N Keep
  414. *4 D F D N N N Conflict
  415. *5 D F F Y N N Y Keep
  416. *5b D F F Y N N N Conflict
  417. *6 D F F N N N Y Keep
  418. *6b D F F N N N N Conflict
  419. *7 F D F Y Y N N Update
  420. *8 F D F N Y N N Conflict
  421. *9 F D F Y N N N Update
  422. *10 F D D N N Y Keep
  423. *11 F D D N N N Conflict
  424. *12 F F D Y N Y N Update
  425. *13 F F D N N Y N Conflict
  426. *14 F F D N N N Conflict
  427. *15 0 F D N N N Conflict
  428. *16 0 D F Y N N N Update
  429. *17 0 D F N N N Conflict
  430. *18 F 0 D Update
  431. *19 D 0 F Update
  432. */
  433. @Test
  434. public void testDirectoryFileSimple() throws IOException {
  435. ObjectId treeDF = buildTree(mkmap("DF", "DF"));
  436. ObjectId treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  437. buildIndex(mkmap("DF", "DF"));
  438. prescanTwoTrees(treeDF, treeDFDF);
  439. assertTrue(getRemoved().contains("DF"));
  440. assertTrue(getUpdated().containsKey("DF/DF"));
  441. recursiveDelete(new File(trash, "DF"));
  442. buildIndex(mkmap("DF/DF", "DF/DF"));
  443. prescanTwoTrees(treeDFDF, treeDF);
  444. assertTrue(getRemoved().contains("DF/DF"));
  445. assertTrue(getUpdated().containsKey("DF"));
  446. }
  447. @Test
  448. public void testDirectoryFileConflicts_1() throws Exception {
  449. // 1
  450. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  451. assertNoConflicts();
  452. assertUpdated("DF");
  453. assertRemoved("DF/DF");
  454. }
  455. @Test
  456. public void testDirectoryFileConflicts_2() throws Exception {
  457. // 2
  458. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  459. writeTrashFile("DF/DF", "different");
  460. go();
  461. assertConflict("DF/DF");
  462. }
  463. @Test
  464. public void testDirectoryFileConflicts_3() throws Exception {
  465. // 3
  466. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  467. assertNoConflicts();
  468. }
  469. @Test
  470. public void testDirectoryFileConflicts_4() throws Exception {
  471. // 4 (basically same as 3, just with H and M different)
  472. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  473. assertConflict("DF/DF");
  474. }
  475. @Test
  476. public void testDirectoryFileConflicts_5() throws Exception {
  477. // 5
  478. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  479. assertRemoved("DF/DF");
  480. assertEquals(0, dco.getConflicts().size());
  481. assertEquals(0, dco.getUpdated().size());
  482. }
  483. @Test
  484. public void testDirectoryFileConflicts_5b() throws Exception {
  485. // 5
  486. doit(mk("DF/DF"), mkmap("DF", "different"), mk("DF"));
  487. assertRemoved("DF/DF");
  488. assertConflict("DF");
  489. assertEquals(0, dco.getUpdated().size());
  490. }
  491. @Test
  492. public void testDirectoryFileConflicts_6() throws Exception {
  493. // 6
  494. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  495. writeTrashFile("DF", "different");
  496. go();
  497. assertRemoved("DF/DF");
  498. assertEquals(0, dco.getConflicts().size());
  499. assertEquals(0, dco.getUpdated().size());
  500. }
  501. @Test
  502. public void testDirectoryFileConflicts_6b() throws Exception {
  503. // 6
  504. setupCase(mk("DF/DF"), mk("DF"), mkmap("DF", "different"));
  505. writeTrashFile("DF", "again different");
  506. go();
  507. assertRemoved("DF/DF");
  508. assertConflict("DF");
  509. assertEquals(0, dco.getUpdated().size());
  510. }
  511. @Test
  512. public void testDirectoryFileConflicts_7() throws Exception {
  513. // 7
  514. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  515. assertUpdated("DF");
  516. assertRemoved("DF/DF");
  517. cleanUpDF();
  518. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  519. go();
  520. assertRemoved("DF/DF/DF/DF/DF");
  521. assertUpdated("DF/DF");
  522. cleanUpDF();
  523. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  524. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  525. go();
  526. assertConflict("DF/DF/DF/DF/DF");
  527. // assertUpdated("DF/DF");
  528. // Why do we expect an update on DF/DF. H==M,
  529. // H&M are files and index contains a dir, index
  530. // is dirty: that case is not in the table but
  531. // we cannot update DF/DF to a file, this would
  532. // require that we delete DF/DF/DF/DF/DF in workdir
  533. // throwing away unsaved contents.
  534. // This test would fail in DirCacheCheckoutTests.
  535. }
  536. @Test
  537. public void testDirectoryFileConflicts_8() throws Exception {
  538. // 8
  539. setupCase(mk("DF"), mk("DF"), mk("DF/DF"));
  540. recursiveDelete(new File(db.getWorkTree(), "DF"));
  541. writeTrashFile("DF", "xy");
  542. go();
  543. assertConflict("DF/DF");
  544. }
  545. @Test
  546. public void testDirectoryFileConflicts_9() throws Exception {
  547. // 9
  548. doit(mkmap("DF", "QP"), mkmap("DF", "QP"), mkmap("DF/DF", "DF/DF"));
  549. assertRemoved("DF/DF");
  550. assertUpdated("DF");
  551. }
  552. @Test
  553. public void testDirectoryFileConflicts_10() throws Exception {
  554. // 10
  555. cleanUpDF();
  556. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  557. assertNoConflicts();
  558. }
  559. @Test
  560. public void testDirectoryFileConflicts_11() throws Exception {
  561. // 11
  562. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  563. assertConflict("DF/DF");
  564. }
  565. @Test
  566. public void testDirectoryFileConflicts_12() throws Exception {
  567. // 12
  568. cleanUpDF();
  569. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  570. assertRemoved("DF");
  571. assertUpdated("DF/DF");
  572. }
  573. @Test
  574. public void testDirectoryFileConflicts_13() throws Exception {
  575. // 13
  576. cleanUpDF();
  577. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  578. writeTrashFile("DF", "asdfsdf");
  579. go();
  580. assertConflict("DF");
  581. assertUpdated("DF/DF");
  582. }
  583. @Test
  584. public void testDirectoryFileConflicts_14() throws Exception {
  585. // 14
  586. cleanUpDF();
  587. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  588. assertConflict("DF");
  589. assertUpdated("DF/DF");
  590. }
  591. @Test
  592. public void testDirectoryFileConflicts_15() throws Exception {
  593. // 15
  594. doit(mkmap(), mk("DF/DF"), mk("DF"));
  595. // This test would fail in DirCacheCheckoutTests. I think this test is wrong,
  596. // it should check for conflicts according to rule 15
  597. // assertRemoved("DF");
  598. assertUpdated("DF/DF");
  599. }
  600. @Test
  601. public void testDirectoryFileConflicts_15b() throws Exception {
  602. // 15, take 2, just to check multi-leveled
  603. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  604. // I think this test is wrong, it should
  605. // check for conflicts according to rule 15
  606. // This test would fail in DirCacheCheckouts
  607. // assertRemoved("DF");
  608. assertUpdated("DF/DF/DF/DF");
  609. }
  610. @Test
  611. public void testDirectoryFileConflicts_16() throws Exception {
  612. // 16
  613. cleanUpDF();
  614. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  615. assertRemoved("DF/DF/DF");
  616. assertUpdated("DF");
  617. }
  618. @Test
  619. public void testDirectoryFileConflicts_17() throws Exception {
  620. // 17
  621. cleanUpDF();
  622. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  623. writeTrashFile("DF/DF/DF", "asdf");
  624. go();
  625. assertConflict("DF/DF/DF");
  626. // Why do we expect an update on DF. If we really update
  627. // DF and update also the working tree we would have to
  628. // overwrite a dirty file in the work-tree DF/DF/DF
  629. // This test would fail in DirCacheCheckout
  630. // assertUpdated("DF");
  631. }
  632. @Test
  633. public void testDirectoryFileConflicts_18() throws Exception {
  634. // 18
  635. cleanUpDF();
  636. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  637. assertRemoved("DF/DF");
  638. assertUpdated("DF/DF/DF/DF");
  639. }
  640. @Test
  641. public void testDirectoryFileConflicts_19() throws Exception {
  642. // 19
  643. cleanUpDF();
  644. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  645. assertRemoved("DF/DF/DF/DF");
  646. assertUpdated("DF/DF/DF");
  647. }
  648. protected void cleanUpDF() throws Exception {
  649. tearDown();
  650. setUp();
  651. recursiveDelete(new File(trash, "DF"));
  652. }
  653. protected void assertConflict(String s) {
  654. assertTrue(getConflicts().contains(s));
  655. }
  656. protected void assertUpdated(String s) {
  657. assertTrue(getUpdated().containsKey(s));
  658. }
  659. protected void assertRemoved(String s) {
  660. assertTrue(getRemoved().contains(s));
  661. }
  662. protected void assertNoConflicts() {
  663. assertTrue(getConflicts().isEmpty());
  664. }
  665. protected void doit(HashMap<String, String> h, HashMap<String, String> m, HashMap<String, String> i)
  666. throws IOException {
  667. setupCase(h, m, i);
  668. go();
  669. }
  670. @Test
  671. public void testUntrackedConflicts() throws IOException {
  672. setupCase(null, mk("foo"), null);
  673. writeTrashFile("foo", "foo");
  674. go();
  675. // test that we don't overwrite untracked files when there is a HEAD
  676. recursiveDelete(new File(trash, "foo"));
  677. setupCase(mk("other"), mkmap("other", "other", "foo", "foo"),
  678. mk("other"));
  679. writeTrashFile("foo", "bar");
  680. try {
  681. checkout();
  682. fail("didn't get the expected exception");
  683. } catch (CheckoutConflictException e) {
  684. assertConflict("foo");
  685. assertEquals("foo", e.getConflictingFiles()[0]);
  686. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  687. assertIndex(mk("other"));
  688. }
  689. // test that we don't overwrite untracked files when there is no HEAD
  690. recursiveDelete(new File(trash, "other"));
  691. recursiveDelete(new File(trash, "foo"));
  692. setupCase(null, mk("foo"), null);
  693. writeTrashFile("foo", "bar");
  694. try {
  695. checkout();
  696. fail("didn't get the expected exception");
  697. } catch (CheckoutConflictException e) {
  698. assertConflict("foo");
  699. assertWorkDir(mkmap("foo", "bar"));
  700. assertIndex(mkmap("other", "other"));
  701. }
  702. // TODO: Why should we expect conflicts here?
  703. // H and M are empty and according to rule #5 of
  704. // the carry-over rules a dirty index is no reason
  705. // for a conflict. (I also feel it should be a
  706. // conflict because we are going to overwrite
  707. // unsaved content in the working tree
  708. // This test would fail in DirCacheCheckoutTest
  709. // assertConflict("foo");
  710. recursiveDelete(new File(trash, "foo"));
  711. recursiveDelete(new File(trash, "other"));
  712. setupCase(null, mk("foo"), null);
  713. writeTrashFile("foo/bar/baz", "");
  714. writeTrashFile("foo/blahblah", "");
  715. go();
  716. assertConflict("foo");
  717. assertConflict("foo/bar/baz");
  718. assertConflict("foo/blahblah");
  719. recursiveDelete(new File(trash, "foo"));
  720. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  721. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  722. assertTrue(new File(trash, "foo/bar").exists());
  723. go();
  724. assertNoConflicts();
  725. }
  726. @Test
  727. public void testCloseNameConflictsX0() throws IOException {
  728. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "b.b/b.b","b.b/b.bs"), mkmap("a/a", "a/a-c") );
  729. checkout();
  730. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  731. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  732. go();
  733. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  734. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  735. assertNoConflicts();
  736. }
  737. @Test
  738. public void testCloseNameConflicts1() throws IOException {
  739. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "a.a/a.a","a.a/a.a"), mkmap("a/a", "a/a-c") );
  740. checkout();
  741. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  742. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  743. go();
  744. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  745. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  746. assertNoConflicts();
  747. }
  748. @Test
  749. public void testCheckoutHierarchy() throws IOException {
  750. setupCase(
  751. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  752. "e/g"),
  753. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  754. "e/g2"),
  755. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  756. "e/g3"));
  757. try {
  758. checkout();
  759. } catch (CheckoutConflictException e) {
  760. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  761. "e/f", "e/g", "e/g3"));
  762. assertConflict("e/g");
  763. assertEquals("e/g", e.getConflictingFiles()[0]);
  764. }
  765. }
  766. @Test
  767. public void testCheckoutOutChanges() throws IOException {
  768. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  769. checkout();
  770. assertIndex(mk("foo/bar"));
  771. assertWorkDir(mk("foo/bar"));
  772. assertFalse(new File(trash, "foo").isFile());
  773. assertTrue(new File(trash, "foo/bar").isFile());
  774. recursiveDelete(new File(trash, "foo"));
  775. assertWorkDir(mkmap());
  776. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  777. checkout();
  778. assertIndex(mk("foo"));
  779. assertWorkDir(mk("foo"));
  780. assertFalse(new File(trash, "foo/bar").isFile());
  781. assertTrue(new File(trash, "foo").isFile());
  782. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  783. assertIndex(mkmap("foo", "bar"));
  784. assertWorkDir(mkmap("foo", "bar"));
  785. try {
  786. checkout();
  787. fail("did not throw exception");
  788. } catch (CheckoutConflictException e) {
  789. assertIndex(mkmap("foo", "bar"));
  790. assertWorkDir(mkmap("foo", "bar"));
  791. }
  792. }
  793. @Test
  794. public void testCheckoutChangeLinkToEmptyDir() throws Exception {
  795. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  796. String fname = "was_file";
  797. Git git = Git.wrap(db);
  798. // Add a file
  799. writeTrashFile(fname, "a");
  800. git.add().addFilepattern(fname).call();
  801. // Add a link to file
  802. String linkName = "link";
  803. File link = writeLink(linkName, fname).toFile();
  804. git.add().addFilepattern(linkName).call();
  805. git.commit().setMessage("Added file and link").call();
  806. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  807. // replace link with empty directory
  808. FileUtils.delete(link);
  809. FileUtils.mkdir(link);
  810. assertTrue("Link must be a directory now", link.isDirectory());
  811. // modify file
  812. writeTrashFile(fname, "b");
  813. assertWorkDir(mkmap(fname, "b", linkName, "/"));
  814. // revert both paths to HEAD state
  815. git.checkout().setStartPoint(Constants.HEAD)
  816. .addPath(fname).addPath(linkName).call();
  817. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  818. Status st = git.status().call();
  819. assertTrue(st.isClean());
  820. }
  821. @Test
  822. public void testCheckoutChangeLinkToEmptyDirs() throws Exception {
  823. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  824. String fname = "was_file";
  825. Git git = Git.wrap(db);
  826. // Add a file
  827. writeTrashFile(fname, "a");
  828. git.add().addFilepattern(fname).call();
  829. // Add a link to file
  830. String linkName = "link";
  831. File link = writeLink(linkName, fname).toFile();
  832. git.add().addFilepattern(linkName).call();
  833. git.commit().setMessage("Added file and link").call();
  834. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  835. // replace link with directory containing only directories, no files
  836. FileUtils.delete(link);
  837. FileUtils.mkdirs(new File(link, "dummyDir"));
  838. assertTrue("Link must be a directory now", link.isDirectory());
  839. assertFalse("Must not delete non empty directory", link.delete());
  840. // modify file
  841. writeTrashFile(fname, "b");
  842. assertWorkDir(mkmap(fname, "b", linkName + "/dummyDir", "/"));
  843. // revert both paths to HEAD state
  844. git.checkout().setStartPoint(Constants.HEAD)
  845. .addPath(fname).addPath(linkName).call();
  846. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  847. Status st = git.status().call();
  848. assertTrue(st.isClean());
  849. }
  850. @Test
  851. public void testCheckoutChangeLinkToNonEmptyDirs() throws Exception {
  852. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  853. String fname = "file";
  854. Git git = Git.wrap(db);
  855. // Add a file
  856. writeTrashFile(fname, "a");
  857. git.add().addFilepattern(fname).call();
  858. // Add a link to file
  859. String linkName = "link";
  860. File link = writeLink(linkName, fname).toFile();
  861. git.add().addFilepattern(linkName).call();
  862. git.commit().setMessage("Added file and link").call();
  863. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  864. // replace link with directory containing only directories, no files
  865. FileUtils.delete(link);
  866. // create but do not add a file in the new directory to the index
  867. writeTrashFile(linkName + "/dir1", "file1", "c");
  868. // create but do not add a file in the new directory to the index
  869. writeTrashFile(linkName + "/dir2", "file2", "d");
  870. assertTrue("File must be a directory now", link.isDirectory());
  871. assertFalse("Must not delete non empty directory", link.delete());
  872. // 2 extra files are created
  873. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  874. linkName + "/dir2/file2", "d"));
  875. // revert path to HEAD state
  876. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  877. // expect only the one added to the index
  878. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  879. Status st = git.status().call();
  880. assertTrue(st.isClean());
  881. }
  882. @Test
  883. public void testCheckoutChangeLinkToNonEmptyDirsAndNewIndexEntry()
  884. throws Exception {
  885. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  886. String fname = "file";
  887. Git git = Git.wrap(db);
  888. // Add a file
  889. writeTrashFile(fname, "a");
  890. git.add().addFilepattern(fname).call();
  891. // Add a link to file
  892. String linkName = "link";
  893. File link = writeLink(linkName, fname).toFile();
  894. git.add().addFilepattern(linkName).call();
  895. git.commit().setMessage("Added file and link").call();
  896. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  897. // replace link with directory containing only directories, no files
  898. FileUtils.delete(link);
  899. // create and add a file in the new directory to the index
  900. writeTrashFile(linkName + "/dir1", "file1", "c");
  901. git.add().addFilepattern(linkName + "/dir1/file1").call();
  902. // create but do not add a file in the new directory to the index
  903. writeTrashFile(linkName + "/dir2", "file2", "d");
  904. assertTrue("File must be a directory now", link.isDirectory());
  905. assertFalse("Must not delete non empty directory", link.delete());
  906. // 2 extra files are created
  907. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  908. linkName + "/dir2/file2", "d"));
  909. // revert path to HEAD state
  910. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  911. // original file and link
  912. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  913. Status st = git.status().call();
  914. assertTrue(st.isClean());
  915. }
  916. @Test
  917. public void testCheckoutChangeFileToEmptyDir() throws Exception {
  918. String fname = "was_file";
  919. Git git = Git.wrap(db);
  920. // Add a file
  921. File file = writeTrashFile(fname, "a");
  922. git.add().addFilepattern(fname).call();
  923. git.commit().setMessage("Added file").call();
  924. // replace file with empty directory
  925. FileUtils.delete(file);
  926. FileUtils.mkdir(file);
  927. assertTrue("File must be a directory now", file.isDirectory());
  928. assertWorkDir(mkmap(fname, "/"));
  929. // revert path to HEAD state
  930. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  931. assertWorkDir(mkmap(fname, "a"));
  932. Status st = git.status().call();
  933. assertTrue(st.isClean());
  934. }
  935. @Test
  936. public void testCheckoutChangeFileToEmptyDirs() throws Exception {
  937. String fname = "was_file";
  938. Git git = Git.wrap(db);
  939. // Add a file
  940. File file = writeTrashFile(fname, "a");
  941. git.add().addFilepattern(fname).call();
  942. git.commit().setMessage("Added file").call();
  943. // replace file with directory containing only directories, no files
  944. FileUtils.delete(file);
  945. FileUtils.mkdirs(new File(file, "dummyDir"));
  946. assertTrue("File must be a directory now", file.isDirectory());
  947. assertFalse("Must not delete non empty directory", file.delete());
  948. assertWorkDir(mkmap(fname + "/dummyDir", "/"));
  949. // revert path to HEAD state
  950. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  951. assertWorkDir(mkmap(fname, "a"));
  952. Status st = git.status().call();
  953. assertTrue(st.isClean());
  954. }
  955. @Test
  956. public void testCheckoutChangeFileToNonEmptyDirs() throws Exception {
  957. String fname = "was_file";
  958. Git git = Git.wrap(db);
  959. // Add a file
  960. File file = writeTrashFile(fname, "a");
  961. git.add().addFilepattern(fname).call();
  962. git.commit().setMessage("Added file").call();
  963. assertWorkDir(mkmap(fname, "a"));
  964. // replace file with directory containing only directories, no files
  965. FileUtils.delete(file);
  966. // create but do not add a file in the new directory to the index
  967. writeTrashFile(fname + "/dir1", "file1", "c");
  968. // create but do not add a file in the new directory to the index
  969. writeTrashFile(fname + "/dir2", "file2", "d");
  970. assertTrue("File must be a directory now", file.isDirectory());
  971. assertFalse("Must not delete non empty directory", file.delete());
  972. // 2 extra files are created
  973. assertWorkDir(
  974. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  975. // revert path to HEAD state
  976. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  977. // expect only the one added to the index
  978. assertWorkDir(mkmap(fname, "a"));
  979. Status st = git.status().call();
  980. assertTrue(st.isClean());
  981. }
  982. @Test
  983. public void testCheckoutChangeFileToNonEmptyDirsAndNewIndexEntry()
  984. throws Exception {
  985. String fname = "was_file";
  986. Git git = Git.wrap(db);
  987. // Add a file
  988. File file = writeTrashFile(fname, "a");
  989. git.add().addFilepattern(fname).call();
  990. git.commit().setMessage("Added file").call();
  991. assertWorkDir(mkmap(fname, "a"));
  992. // replace file with directory containing only directories, no files
  993. FileUtils.delete(file);
  994. // create and add a file in the new directory to the index
  995. writeTrashFile(fname + "/dir", "file1", "c");
  996. git.add().addFilepattern(fname + "/dir/file1").call();
  997. // create but do not add a file in the new directory to the index
  998. writeTrashFile(fname + "/dir", "file2", "d");
  999. assertTrue("File must be a directory now", file.isDirectory());
  1000. assertFalse("Must not delete non empty directory", file.delete());
  1001. // 2 extra files are created
  1002. assertWorkDir(
  1003. mkmap(fname + "/dir/file1", "c", fname + "/dir/file2", "d"));
  1004. // revert path to HEAD state
  1005. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1006. assertWorkDir(mkmap(fname, "a"));
  1007. Status st = git.status().call();
  1008. assertTrue(st.isClean());
  1009. }
  1010. @Test
  1011. public void testCheckoutOutChangesAutoCRLFfalse() throws IOException {
  1012. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1013. checkout();
  1014. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1015. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1016. }
  1017. @Test
  1018. public void testCheckoutOutChangesAutoCRLFInput() throws IOException {
  1019. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1020. db.getConfig().setString("core", null, "autocrlf", "input");
  1021. checkout();
  1022. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1023. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1024. }
  1025. @Test
  1026. public void testCheckoutOutChangesAutoCRLFtrue() throws IOException {
  1027. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1028. db.getConfig().setString("core", null, "autocrlf", "true");
  1029. checkout();
  1030. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1031. assertWorkDir(mkmap("foo/bar", "foo\r\nbar"));
  1032. }
  1033. @Test
  1034. public void testCheckoutOutChangesAutoCRLFtrueBinary() throws IOException {
  1035. setupCase(mk("foo"), mkmap("foo/bar", "foo\nb\u0000ar"), mk("foo"));
  1036. db.getConfig().setString("core", null, "autocrlf", "true");
  1037. checkout();
  1038. assertIndex(mkmap("foo/bar", "foo\nb\u0000ar"));
  1039. assertWorkDir(mkmap("foo/bar", "foo\nb\u0000ar"));
  1040. }
  1041. @Test
  1042. public void testCheckoutUncachedChanges() throws IOException {
  1043. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1044. writeTrashFile("foo", "otherData");
  1045. checkout();
  1046. assertIndex(mk("foo"));
  1047. assertWorkDir(mkmap("foo", "otherData"));
  1048. assertTrue(new File(trash, "foo").isFile());
  1049. }
  1050. @Test
  1051. public void testDontOverwriteDirtyFile() throws IOException {
  1052. setupCase(mk("foo"), mk("other"), mk("foo"));
  1053. writeTrashFile("foo", "different");
  1054. try {
  1055. checkout();
  1056. fail("Didn't got the expected conflict");
  1057. } catch (CheckoutConflictException e) {
  1058. assertIndex(mk("foo"));
  1059. assertWorkDir(mkmap("foo", "different"));
  1060. assertEquals(Arrays.asList("foo"), getConflicts());
  1061. assertTrue(new File(trash, "foo").isFile());
  1062. }
  1063. }
  1064. @Test
  1065. public void testDontOverwriteEmptyFolder() throws IOException {
  1066. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1067. FileUtils.mkdir(new File(db.getWorkTree(), "d"));
  1068. checkout();
  1069. assertWorkDir(mkmap("foo", "foo", "d", "/"));
  1070. }
  1071. @Test
  1072. public void testOverwriteUntrackedIgnoredFile() throws IOException,
  1073. GitAPIException {
  1074. String fname="file.txt";
  1075. Git git = Git.wrap(db);
  1076. // Add a file
  1077. writeTrashFile(fname, "a");
  1078. git.add().addFilepattern(fname).call();
  1079. git.commit().setMessage("create file").call();
  1080. // Create branch
  1081. git.branchCreate().setName("side").call();
  1082. // Modify file
  1083. writeTrashFile(fname, "b");
  1084. git.add().addFilepattern(fname).call();
  1085. git.commit().setMessage("modify file").call();
  1086. // Switch branches
  1087. git.checkout().setName("side").call();
  1088. git.rm().addFilepattern(fname).call();
  1089. writeTrashFile(".gitignore", fname);
  1090. git.add().addFilepattern(".gitignore").call();
  1091. git.commit().setMessage("delete and ignore file").call();
  1092. writeTrashFile(fname, "Something different");
  1093. git.checkout().setName("master").call();
  1094. assertWorkDir(mkmap(fname, "b"));
  1095. assertTrue(git.status().call().isClean());
  1096. }
  1097. @Test
  1098. public void testOverwriteUntrackedFileModeChange()
  1099. throws IOException, GitAPIException {
  1100. String fname = "file.txt";
  1101. Git git = Git.wrap(db);
  1102. // Add a file
  1103. File file = writeTrashFile(fname, "a");
  1104. git.add().addFilepattern(fname).call();
  1105. git.commit().setMessage("create file").call();
  1106. assertWorkDir(mkmap(fname, "a"));
  1107. // Create branch
  1108. git.branchCreate().setName("side").call();
  1109. // Switch branches
  1110. git.checkout().setName("side").call();
  1111. // replace file with directory containing files
  1112. FileUtils.delete(file);
  1113. // create and add a file in the new directory to the index
  1114. writeTrashFile(fname + "/dir1", "file1", "c");
  1115. git.add().addFilepattern(fname + "/dir1/file1").call();
  1116. // create but do not add a file in the new directory to the index
  1117. writeTrashFile(fname + "/dir2", "file2", "d");
  1118. assertTrue("File must be a directory now", file.isDirectory());
  1119. assertFalse("Must not delete non empty directory", file.delete());
  1120. // 2 extra files are created
  1121. assertWorkDir(
  1122. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  1123. try {
  1124. git.checkout().setName("master").call();
  1125. fail("did not throw exception");
  1126. } catch (Exception e) {
  1127. // 2 extra files are still there
  1128. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1129. fname + "/dir2/file2", "d"));
  1130. }
  1131. }
  1132. @Test
  1133. public void testOverwriteUntrackedLinkModeChange()
  1134. throws Exception {
  1135. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  1136. String fname = "file.txt";
  1137. Git git = Git.wrap(db);
  1138. // Add a file
  1139. writeTrashFile(fname, "a");
  1140. git.add().addFilepattern(fname).call();
  1141. // Add a link to file
  1142. String linkName = "link";
  1143. File link = writeLink(linkName, fname).toFile();
  1144. git.add().addFilepattern(linkName).call();
  1145. git.commit().setMessage("Added file and link").call();
  1146. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  1147. // Create branch
  1148. git.branchCreate().setName("side").call();
  1149. // Switch branches
  1150. git.checkout().setName("side").call();
  1151. // replace link with directory containing files
  1152. FileUtils.delete(link);
  1153. // create and add a file in the new directory to the index
  1154. writeTrashFile(linkName + "/dir1", "file1", "c");
  1155. git.add().addFilepattern(linkName + "/dir1/file1").call();
  1156. // create but do not add a file in the new directory to the index
  1157. writeTrashFile(linkName + "/dir2", "file2", "d");
  1158. assertTrue("Link must be a directory now", link.isDirectory());
  1159. assertFalse("Must not delete non empty directory", link.delete());
  1160. // 2 extra files are created
  1161. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1162. linkName + "/dir2/file2", "d"));
  1163. try {
  1164. git.checkout().setName("master").call();
  1165. fail("did not throw exception");
  1166. } catch (Exception e) {
  1167. // 2 extra files are still there
  1168. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1169. linkName + "/dir2/file2", "d"));
  1170. }
  1171. }
  1172. @Test
  1173. public void testFileModeChangeWithNoContentChangeUpdate() throws Exception {
  1174. if (!FS.DETECTED.supportsExecute())
  1175. return;
  1176. Git git = Git.wrap(db);
  1177. // Add non-executable file
  1178. File file = writeTrashFile("file.txt", "a");
  1179. git.add().addFilepattern("file.txt").call();
  1180. git.commit().setMessage("commit1").call();
  1181. assertFalse(db.getFS().canExecute(file));
  1182. // Create branch
  1183. git.branchCreate().setName("b1").call();
  1184. // Make file executable
  1185. db.getFS().setExecute(file, true);
  1186. git.add().addFilepattern("file.txt").call();
  1187. git.commit().setMessage("commit2").call();
  1188. // Verify executable and working directory is clean
  1189. Status status = git.status().call();
  1190. assertTrue(status.getModified().isEmpty());
  1191. assertTrue(status.getChanged().isEmpty());
  1192. assertTrue(db.getFS().canExecute(file));
  1193. // Switch branches
  1194. git.checkout().setName("b1").call();
  1195. // Verify not executable and working directory is clean
  1196. status = git.status().call();
  1197. assertTrue(status.getModified().isEmpty());
  1198. assertTrue(status.getChanged().isEmpty());
  1199. assertFalse(db.getFS().canExecute(file));
  1200. }
  1201. @Test
  1202. public void testFileModeChangeAndContentChangeConflict() throws Exception {
  1203. if (!FS.DETECTED.supportsExecute())
  1204. return;
  1205. Git git = Git.wrap(db);
  1206. // Add non-executable file
  1207. File file = writeTrashFile("file.txt", "a");
  1208. git.add().addFilepattern("file.txt").call();
  1209. git.commit().setMessage("commit1").call();
  1210. assertFalse(db.getFS().canExecute(file));
  1211. // Create branch
  1212. git.branchCreate().setName("b1").call();
  1213. // Make file executable
  1214. db.getFS().setExecute(file, true);
  1215. git.add().addFilepattern("file.txt").call();
  1216. git.commit().setMessage("commit2").call();
  1217. // Verify executable and working directory is clean
  1218. Status status = git.status().call();
  1219. assertTrue(status.getModified().isEmpty());
  1220. assertTrue(status.getChanged().isEmpty());
  1221. assertTrue(db.getFS().canExecute(file));
  1222. writeTrashFile("file.txt", "b");
  1223. // Switch branches
  1224. CheckoutCommand checkout = git.checkout().setName("b1");
  1225. try {
  1226. checkout.call();
  1227. fail("Checkout exception not thrown");
  1228. } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) {
  1229. CheckoutResult result = checkout.getResult();
  1230. assertNotNull(result);
  1231. assertNotNull(result.getConflictList());
  1232. assertEquals(1, result.getConflictList().size());
  1233. assertTrue(result.getConflictList().contains("file.txt"));
  1234. }
  1235. }
  1236. @Test
  1237. public void testDirtyFileModeEqualHeadMerge()
  1238. throws Exception {
  1239. if (!FS.DETECTED.supportsExecute())
  1240. return;
  1241. Git git = Git.wrap(db);
  1242. // Add non-executable file
  1243. File file = writeTrashFile("file.txt", "a");
  1244. git.add().addFilepattern("file.txt").call();
  1245. git.commit().setMessage("commit1").call();
  1246. assertFalse(db.getFS().canExecute(file));
  1247. // Create branch
  1248. git.branchCreate().setName("b1").call();
  1249. // Create second commit and don't touch file
  1250. writeTrashFile("file2.txt", "");
  1251. git.add().addFilepattern("file2.txt").call();
  1252. git.commit().setMessage("commit2").call();
  1253. // stage a mode change
  1254. writeTrashFile("file.txt", "a");
  1255. db.getFS().setExecute(file, true);
  1256. git.add().addFilepattern("file.txt").call();
  1257. // dirty the file
  1258. writeTrashFile("file.txt", "b");
  1259. assertEquals(
  1260. "[file.txt, mode:100755, content:a][file2.txt, mode:100644, content:]",
  1261. indexState(CONTENT));
  1262. assertWorkDir(mkmap("file.txt", "b", "file2.txt", ""));
  1263. // Switch branches and check that the dirty file survived in worktree
  1264. // and index
  1265. git.checkout().setName("b1").call();
  1266. assertEquals("[file.txt, mode:100755, content:a]", indexState(CONTENT));
  1267. assertWorkDir(mkmap("file.txt", "b"));
  1268. }
  1269. @Test
  1270. public void testDirtyFileModeEqualIndexMerge()
  1271. throws Exception {
  1272. if (!FS.DETECTED.supportsExecute())
  1273. return;
  1274. Git git = Git.wrap(db);
  1275. // Add non-executable file
  1276. File file = writeTrashFile("file.txt", "a");
  1277. git.add().addFilepattern("file.txt").call();
  1278. git.commit().setMessage("commit1").call();
  1279. assertFalse(db.getFS().canExecute(file));
  1280. // Create branch
  1281. git.branchCreate().setName("b1").call();
  1282. // Create second commit with executable file
  1283. file = writeTrashFile("file.txt", "b");
  1284. db.getFS().setExecute(file, true);
  1285. git.add().addFilepattern("file.txt").call();
  1286. git.commit().setMessage("commit2").call();
  1287. // stage the same content as in the branch we want to switch to
  1288. writeTrashFile("file.txt", "a");
  1289. db.getFS().setExecute(file, false);
  1290. git.add().addFilepattern("file.txt").call();
  1291. // dirty the file
  1292. writeTrashFile("file.txt", "c");
  1293. db.getFS().setExecute(file, true);
  1294. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1295. assertWorkDir(mkmap("file.txt", "c"));
  1296. // Switch branches and check that the dirty file survived in worktree
  1297. // and index
  1298. git.checkout().setName("b1").call();
  1299. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1300. assertWorkDir(mkmap("file.txt", "c"));
  1301. }
  1302. @Test
  1303. public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
  1304. if (!FS.DETECTED.supportsExecute())
  1305. return;
  1306. Git git = Git.wrap(db);
  1307. // Add first file
  1308. File file1 = writeTrashFile("file1.txt", "a");
  1309. git.add().addFilepattern("file1.txt").call();
  1310. git.commit().setMessage("commit1").call();
  1311. assertFalse(db.getFS().canExecute(file1));
  1312. // Add second file
  1313. File file2 = writeTrashFile("file2.txt", "b");
  1314. git.add().addFilepattern("file2.txt").call();
  1315. git.commit().setMessage("commit2").call();
  1316. assertFalse(db.getFS().canExecute(file2));
  1317. // Create branch from first commit
  1318. assertNotNull(git.checkout().setCreateBranch(true).setName("b1")
  1319. .setStartPoint(Constants.HEAD + "~1").call());
  1320. // Change content and file mode in working directory and index
  1321. file1 = writeTrashFile("file1.txt", "c");
  1322. db.getFS().setExecute(file1, true);
  1323. git.add().addFilepattern("file1.txt").call();
  1324. // Switch back to 'master'
  1325. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  1326. }
  1327. @Test(expected = CheckoutConflictException.class)
  1328. public void testFolderFileConflict() throws Exception {
  1329. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1330. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1331. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1332. writeTrashFile("f", "file instead of folder");
  1333. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1334. checkoutCommit.getTree()).checkout();
  1335. }
  1336. @Test
  1337. public void testMultipleContentConflicts() throws Exception {
  1338. commitFile("a", "initial content", "master");
  1339. RevCommit headCommit = commitFile("b", "initial content", "master");
  1340. commitFile("a", "side content", "side");
  1341. RevCommit checkoutCommit = commitFile("b", "side content", "side");
  1342. writeTrashFile("a", "changed content");
  1343. writeTrashFile("b", "changed content");
  1344. try {
  1345. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1346. checkoutCommit.getTree()).checkout();
  1347. fail();
  1348. } catch (CheckoutConflictException expected) {
  1349. assertEquals(2, expected.getConflictingFiles().length);
  1350. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1351. .contains("a"));
  1352. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1353. .contains("b"));
  1354. assertEquals("changed content", read("a"));
  1355. assertEquals("changed content", read("b"));
  1356. }
  1357. }
  1358. @Test
  1359. public void testFolderFileAndContentConflicts() throws Exception {
  1360. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1361. commitFile("b", "side content", "side");
  1362. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1363. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1364. writeTrashFile("f", "file instead of a folder");
  1365. writeTrashFile("b", "changed content");
  1366. try {
  1367. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1368. checkoutCommit.getTree()).checkout();
  1369. fail();
  1370. } catch (CheckoutConflictException expected) {
  1371. assertEquals(2, expected.getConflictingFiles().length);
  1372. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1373. .contains("b"));
  1374. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1375. .contains("f"));
  1376. assertEquals("file instead of a folder", read("f"));
  1377. assertEquals("changed content", read("b"));
  1378. }
  1379. }
  1380. public void assertWorkDir(Map<String, String> i)
  1381. throws CorruptObjectException,
  1382. IOException {
  1383. try (TreeWalk walk = new TreeWalk(db)) {
  1384. walk.setRecursive(false);
  1385. walk.addTree(new FileTreeIterator(db));
  1386. String expectedValue;
  1387. String path;
  1388. int nrFiles = 0;
  1389. FileTreeIterator ft;
  1390. while (walk.next()) {
  1391. ft = walk.getTree(0, FileTreeIterator.class);
  1392. path = ft.getEntryPathString();
  1393. expectedValue = i.get(path);
  1394. File file = new File(db.getWorkTree(), path);
  1395. assertTrue(file.exists());
  1396. if (file.isFile()) {
  1397. assertNotNull("found unexpected file for path " + path
  1398. + " in workdir", expectedValue);
  1399. FileInputStream is = new FileInputStream(file);
  1400. byte[] buffer = new byte[(int) file.length()];
  1401. int offset = 0;
  1402. int numRead = 0;
  1403. while (offset < buffer.length
  1404. && (numRead = is.read(buffer, offset, buffer.length
  1405. - offset)) >= 0) {
  1406. offset += numRead;
  1407. }
  1408. is.close();
  1409. assertArrayEquals("unexpected content for path " + path
  1410. + " in workDir. ", buffer, i.get(path).getBytes());
  1411. nrFiles++;
  1412. } else if (file.isDirectory()) {
  1413. if (file.list().length == 0) {
  1414. assertEquals("found unexpected empty folder for path "
  1415. + path + " in workDir. ", "/", i.get(path));
  1416. nrFiles++;
  1417. }
  1418. }
  1419. if (walk.isSubtree()) {
  1420. walk.enterSubtree();
  1421. }
  1422. }
  1423. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  1424. }
  1425. }
  1426. }