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 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  686. assertIndex(mk("other"));
  687. }
  688. // test that we don't overwrite untracked files when there is no HEAD
  689. recursiveDelete(new File(trash, "other"));
  690. recursiveDelete(new File(trash, "foo"));
  691. setupCase(null, mk("foo"), null);
  692. writeTrashFile("foo", "bar");
  693. try {
  694. checkout();
  695. fail("didn't get the expected exception");
  696. } catch (CheckoutConflictException e) {
  697. assertConflict("foo");
  698. assertWorkDir(mkmap("foo", "bar"));
  699. assertIndex(mkmap("other", "other"));
  700. }
  701. // TODO: Why should we expect conflicts here?
  702. // H and M are empty and according to rule #5 of
  703. // the carry-over rules a dirty index is no reason
  704. // for a conflict. (I also feel it should be a
  705. // conflict because we are going to overwrite
  706. // unsaved content in the working tree
  707. // This test would fail in DirCacheCheckoutTest
  708. // assertConflict("foo");
  709. recursiveDelete(new File(trash, "foo"));
  710. recursiveDelete(new File(trash, "other"));
  711. setupCase(null, mk("foo"), null);
  712. writeTrashFile("foo/bar/baz", "");
  713. writeTrashFile("foo/blahblah", "");
  714. go();
  715. assertConflict("foo");
  716. assertConflict("foo/bar/baz");
  717. assertConflict("foo/blahblah");
  718. recursiveDelete(new File(trash, "foo"));
  719. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  720. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  721. assertTrue(new File(trash, "foo/bar").exists());
  722. go();
  723. assertNoConflicts();
  724. }
  725. @Test
  726. public void testCloseNameConflictsX0() throws IOException {
  727. 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") );
  728. checkout();
  729. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  730. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  731. go();
  732. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  733. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  734. assertNoConflicts();
  735. }
  736. @Test
  737. public void testCloseNameConflicts1() throws IOException {
  738. 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") );
  739. checkout();
  740. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  741. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  742. go();
  743. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  744. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  745. assertNoConflicts();
  746. }
  747. @Test
  748. public void testCheckoutHierarchy() throws IOException {
  749. setupCase(
  750. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  751. "e/g"),
  752. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  753. "e/g2"),
  754. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  755. "e/g3"));
  756. try {
  757. checkout();
  758. } catch (CheckoutConflictException e) {
  759. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  760. "e/f", "e/g", "e/g3"));
  761. assertConflict("e/g");
  762. }
  763. }
  764. @Test
  765. public void testCheckoutOutChanges() throws IOException {
  766. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  767. checkout();
  768. assertIndex(mk("foo/bar"));
  769. assertWorkDir(mk("foo/bar"));
  770. assertFalse(new File(trash, "foo").isFile());
  771. assertTrue(new File(trash, "foo/bar").isFile());
  772. recursiveDelete(new File(trash, "foo"));
  773. assertWorkDir(mkmap());
  774. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  775. checkout();
  776. assertIndex(mk("foo"));
  777. assertWorkDir(mk("foo"));
  778. assertFalse(new File(trash, "foo/bar").isFile());
  779. assertTrue(new File(trash, "foo").isFile());
  780. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  781. assertIndex(mkmap("foo", "bar"));
  782. assertWorkDir(mkmap("foo", "bar"));
  783. try {
  784. checkout();
  785. fail("did not throw exception");
  786. } catch (CheckoutConflictException e) {
  787. assertIndex(mkmap("foo", "bar"));
  788. assertWorkDir(mkmap("foo", "bar"));
  789. }
  790. }
  791. @Test
  792. public void testCheckoutChangeLinkToEmptyDir() throws Exception {
  793. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  794. String fname = "was_file";
  795. Git git = Git.wrap(db);
  796. // Add a file
  797. writeTrashFile(fname, "a");
  798. git.add().addFilepattern(fname).call();
  799. // Add a link to file
  800. String linkName = "link";
  801. File link = writeLink(linkName, fname).toFile();
  802. git.add().addFilepattern(linkName).call();
  803. git.commit().setMessage("Added file and link").call();
  804. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  805. // replace link with empty directory
  806. FileUtils.delete(link);
  807. FileUtils.mkdir(link);
  808. assertTrue("Link must be a directory now", link.isDirectory());
  809. // modify file
  810. writeTrashFile(fname, "b");
  811. assertWorkDir(mkmap(fname, "b", linkName, "/"));
  812. // revert both paths to HEAD state
  813. git.checkout().setStartPoint(Constants.HEAD)
  814. .addPath(fname).addPath(linkName).call();
  815. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  816. Status st = git.status().call();
  817. assertTrue(st.isClean());
  818. }
  819. @Test
  820. public void testCheckoutChangeLinkToEmptyDirs() throws Exception {
  821. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  822. String fname = "was_file";
  823. Git git = Git.wrap(db);
  824. // Add a file
  825. writeTrashFile(fname, "a");
  826. git.add().addFilepattern(fname).call();
  827. // Add a link to file
  828. String linkName = "link";
  829. File link = writeLink(linkName, fname).toFile();
  830. git.add().addFilepattern(linkName).call();
  831. git.commit().setMessage("Added file and link").call();
  832. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  833. // replace link with directory containing only directories, no files
  834. FileUtils.delete(link);
  835. FileUtils.mkdirs(new File(link, "dummyDir"));
  836. assertTrue("Link must be a directory now", link.isDirectory());
  837. assertFalse("Must not delete non empty directory", link.delete());
  838. // modify file
  839. writeTrashFile(fname, "b");
  840. assertWorkDir(mkmap(fname, "b", linkName + "/dummyDir", "/"));
  841. // revert both paths to HEAD state
  842. git.checkout().setStartPoint(Constants.HEAD)
  843. .addPath(fname).addPath(linkName).call();
  844. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  845. Status st = git.status().call();
  846. assertTrue(st.isClean());
  847. }
  848. @Test
  849. public void testCheckoutChangeLinkToNonEmptyDirs() throws Exception {
  850. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  851. String fname = "file";
  852. Git git = Git.wrap(db);
  853. // Add a file
  854. writeTrashFile(fname, "a");
  855. git.add().addFilepattern(fname).call();
  856. // Add a link to file
  857. String linkName = "link";
  858. File link = writeLink(linkName, fname).toFile();
  859. git.add().addFilepattern(linkName).call();
  860. git.commit().setMessage("Added file and link").call();
  861. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  862. // replace link with directory containing only directories, no files
  863. FileUtils.delete(link);
  864. // create but do not add a file in the new directory to the index
  865. writeTrashFile(linkName + "/dir1", "file1", "c");
  866. // create but do not add a file in the new directory to the index
  867. writeTrashFile(linkName + "/dir2", "file2", "d");
  868. assertTrue("File must be a directory now", link.isDirectory());
  869. assertFalse("Must not delete non empty directory", link.delete());
  870. // 2 extra files are created
  871. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  872. linkName + "/dir2/file2", "d"));
  873. // revert path to HEAD state
  874. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  875. // expect only the one added to the index
  876. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  877. Status st = git.status().call();
  878. assertTrue(st.isClean());
  879. }
  880. @Test
  881. public void testCheckoutChangeLinkToNonEmptyDirsAndNewIndexEntry()
  882. throws Exception {
  883. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  884. String fname = "file";
  885. Git git = Git.wrap(db);
  886. // Add a file
  887. writeTrashFile(fname, "a");
  888. git.add().addFilepattern(fname).call();
  889. // Add a link to file
  890. String linkName = "link";
  891. File link = writeLink(linkName, fname).toFile();
  892. git.add().addFilepattern(linkName).call();
  893. git.commit().setMessage("Added file and link").call();
  894. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  895. // replace link with directory containing only directories, no files
  896. FileUtils.delete(link);
  897. // create and add a file in the new directory to the index
  898. writeTrashFile(linkName + "/dir1", "file1", "c");
  899. git.add().addFilepattern(linkName + "/dir1/file1").call();
  900. // create but do not add a file in the new directory to the index
  901. writeTrashFile(linkName + "/dir2", "file2", "d");
  902. assertTrue("File must be a directory now", link.isDirectory());
  903. assertFalse("Must not delete non empty directory", link.delete());
  904. // 2 extra files are created
  905. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  906. linkName + "/dir2/file2", "d"));
  907. // revert path to HEAD state
  908. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  909. // original file and link
  910. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  911. Status st = git.status().call();
  912. assertTrue(st.isClean());
  913. }
  914. @Test
  915. public void testCheckoutChangeFileToEmptyDir() throws Exception {
  916. String fname = "was_file";
  917. Git git = Git.wrap(db);
  918. // Add a file
  919. File file = writeTrashFile(fname, "a");
  920. git.add().addFilepattern(fname).call();
  921. git.commit().setMessage("Added file").call();
  922. // replace file with empty directory
  923. FileUtils.delete(file);
  924. FileUtils.mkdir(file);
  925. assertTrue("File must be a directory now", file.isDirectory());
  926. assertWorkDir(mkmap(fname, "/"));
  927. // revert path to HEAD state
  928. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  929. assertWorkDir(mkmap(fname, "a"));
  930. Status st = git.status().call();
  931. assertTrue(st.isClean());
  932. }
  933. @Test
  934. public void testCheckoutChangeFileToEmptyDirs() throws Exception {
  935. String fname = "was_file";
  936. Git git = Git.wrap(db);
  937. // Add a file
  938. File file = writeTrashFile(fname, "a");
  939. git.add().addFilepattern(fname).call();
  940. git.commit().setMessage("Added file").call();
  941. // replace file with directory containing only directories, no files
  942. FileUtils.delete(file);
  943. FileUtils.mkdirs(new File(file, "dummyDir"));
  944. assertTrue("File must be a directory now", file.isDirectory());
  945. assertFalse("Must not delete non empty directory", file.delete());
  946. assertWorkDir(mkmap(fname + "/dummyDir", "/"));
  947. // revert path to HEAD state
  948. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  949. assertWorkDir(mkmap(fname, "a"));
  950. Status st = git.status().call();
  951. assertTrue(st.isClean());
  952. }
  953. @Test
  954. public void testCheckoutChangeFileToNonEmptyDirs() throws Exception {
  955. String fname = "was_file";
  956. Git git = Git.wrap(db);
  957. // Add a file
  958. File file = writeTrashFile(fname, "a");
  959. git.add().addFilepattern(fname).call();
  960. git.commit().setMessage("Added file").call();
  961. assertWorkDir(mkmap(fname, "a"));
  962. // replace file with directory containing only directories, no files
  963. FileUtils.delete(file);
  964. // create but do not add a file in the new directory to the index
  965. writeTrashFile(fname + "/dir1", "file1", "c");
  966. // create but do not add a file in the new directory to the index
  967. writeTrashFile(fname + "/dir2", "file2", "d");
  968. assertTrue("File must be a directory now", file.isDirectory());
  969. assertFalse("Must not delete non empty directory", file.delete());
  970. // 2 extra files are created
  971. assertWorkDir(
  972. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  973. // revert path to HEAD state
  974. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  975. // expect only the one added to the index
  976. assertWorkDir(mkmap(fname, "a"));
  977. Status st = git.status().call();
  978. assertTrue(st.isClean());
  979. }
  980. @Test
  981. public void testCheckoutChangeFileToNonEmptyDirsAndNewIndexEntry()
  982. throws Exception {
  983. String fname = "was_file";
  984. Git git = Git.wrap(db);
  985. // Add a file
  986. File file = writeTrashFile(fname, "a");
  987. git.add().addFilepattern(fname).call();
  988. git.commit().setMessage("Added file").call();
  989. assertWorkDir(mkmap(fname, "a"));
  990. // replace file with directory containing only directories, no files
  991. FileUtils.delete(file);
  992. // create and add a file in the new directory to the index
  993. writeTrashFile(fname + "/dir", "file1", "c");
  994. git.add().addFilepattern(fname + "/dir/file1").call();
  995. // create but do not add a file in the new directory to the index
  996. writeTrashFile(fname + "/dir", "file2", "d");
  997. assertTrue("File must be a directory now", file.isDirectory());
  998. assertFalse("Must not delete non empty directory", file.delete());
  999. // 2 extra files are created
  1000. assertWorkDir(
  1001. mkmap(fname + "/dir/file1", "c", fname + "/dir/file2", "d"));
  1002. // revert path to HEAD state
  1003. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1004. assertWorkDir(mkmap(fname, "a"));
  1005. Status st = git.status().call();
  1006. assertTrue(st.isClean());
  1007. }
  1008. @Test
  1009. public void testCheckoutOutChangesAutoCRLFfalse() throws IOException {
  1010. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1011. checkout();
  1012. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1013. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1014. }
  1015. @Test
  1016. public void testCheckoutOutChangesAutoCRLFInput() throws IOException {
  1017. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1018. db.getConfig().setString("core", null, "autocrlf", "input");
  1019. checkout();
  1020. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1021. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1022. }
  1023. @Test
  1024. public void testCheckoutOutChangesAutoCRLFtrue() throws IOException {
  1025. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1026. db.getConfig().setString("core", null, "autocrlf", "true");
  1027. checkout();
  1028. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1029. assertWorkDir(mkmap("foo/bar", "foo\r\nbar"));
  1030. }
  1031. @Test
  1032. public void testCheckoutOutChangesAutoCRLFtrueBinary() throws IOException {
  1033. setupCase(mk("foo"), mkmap("foo/bar", "foo\nb\u0000ar"), mk("foo"));
  1034. db.getConfig().setString("core", null, "autocrlf", "true");
  1035. checkout();
  1036. assertIndex(mkmap("foo/bar", "foo\nb\u0000ar"));
  1037. assertWorkDir(mkmap("foo/bar", "foo\nb\u0000ar"));
  1038. }
  1039. @Test
  1040. public void testCheckoutUncachedChanges() throws IOException {
  1041. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1042. writeTrashFile("foo", "otherData");
  1043. checkout();
  1044. assertIndex(mk("foo"));
  1045. assertWorkDir(mkmap("foo", "otherData"));
  1046. assertTrue(new File(trash, "foo").isFile());
  1047. }
  1048. @Test
  1049. public void testDontOverwriteDirtyFile() throws IOException {
  1050. setupCase(mk("foo"), mk("other"), mk("foo"));
  1051. writeTrashFile("foo", "different");
  1052. try {
  1053. checkout();
  1054. fail("Didn't got the expected conflict");
  1055. } catch (CheckoutConflictException e) {
  1056. assertIndex(mk("foo"));
  1057. assertWorkDir(mkmap("foo", "different"));
  1058. assertEquals(Arrays.asList("foo"), getConflicts());
  1059. assertTrue(new File(trash, "foo").isFile());
  1060. }
  1061. }
  1062. @Test
  1063. public void testDontOverwriteEmptyFolder() throws IOException {
  1064. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1065. FileUtils.mkdir(new File(db.getWorkTree(), "d"));
  1066. checkout();
  1067. assertWorkDir(mkmap("foo", "foo", "d", "/"));
  1068. }
  1069. @Test
  1070. public void testOverwriteUntrackedIgnoredFile() throws IOException,
  1071. GitAPIException {
  1072. String fname="file.txt";
  1073. Git git = Git.wrap(db);
  1074. // Add a file
  1075. writeTrashFile(fname, "a");
  1076. git.add().addFilepattern(fname).call();
  1077. git.commit().setMessage("create file").call();
  1078. // Create branch
  1079. git.branchCreate().setName("side").call();
  1080. // Modify file
  1081. writeTrashFile(fname, "b");
  1082. git.add().addFilepattern(fname).call();
  1083. git.commit().setMessage("modify file").call();
  1084. // Switch branches
  1085. git.checkout().setName("side").call();
  1086. git.rm().addFilepattern(fname).call();
  1087. writeTrashFile(".gitignore", fname);
  1088. git.add().addFilepattern(".gitignore").call();
  1089. git.commit().setMessage("delete and ignore file").call();
  1090. writeTrashFile(fname, "Something different");
  1091. git.checkout().setName("master").call();
  1092. assertWorkDir(mkmap(fname, "b"));
  1093. assertTrue(git.status().call().isClean());
  1094. }
  1095. @Test
  1096. public void testOverwriteUntrackedFileModeChange()
  1097. throws IOException, GitAPIException {
  1098. String fname = "file.txt";
  1099. Git git = Git.wrap(db);
  1100. // Add a file
  1101. File file = writeTrashFile(fname, "a");
  1102. git.add().addFilepattern(fname).call();
  1103. git.commit().setMessage("create file").call();
  1104. assertWorkDir(mkmap(fname, "a"));
  1105. // Create branch
  1106. git.branchCreate().setName("side").call();
  1107. // Switch branches
  1108. git.checkout().setName("side").call();
  1109. // replace file with directory containing files
  1110. FileUtils.delete(file);
  1111. // create and add a file in the new directory to the index
  1112. writeTrashFile(fname + "/dir1", "file1", "c");
  1113. git.add().addFilepattern(fname + "/dir1/file1").call();
  1114. // create but do not add a file in the new directory to the index
  1115. writeTrashFile(fname + "/dir2", "file2", "d");
  1116. assertTrue("File must be a directory now", file.isDirectory());
  1117. assertFalse("Must not delete non empty directory", file.delete());
  1118. // 2 extra files are created
  1119. assertWorkDir(
  1120. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  1121. try {
  1122. git.checkout().setName("master").call();
  1123. fail("did not throw exception");
  1124. } catch (Exception e) {
  1125. // 2 extra files are still there
  1126. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1127. fname + "/dir2/file2", "d"));
  1128. }
  1129. }
  1130. @Test
  1131. public void testOverwriteUntrackedLinkModeChange()
  1132. throws Exception {
  1133. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  1134. String fname = "file.txt";
  1135. Git git = Git.wrap(db);
  1136. // Add a file
  1137. writeTrashFile(fname, "a");
  1138. git.add().addFilepattern(fname).call();
  1139. // Add a link to file
  1140. String linkName = "link";
  1141. File link = writeLink(linkName, fname).toFile();
  1142. git.add().addFilepattern(linkName).call();
  1143. git.commit().setMessage("Added file and link").call();
  1144. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  1145. // Create branch
  1146. git.branchCreate().setName("side").call();
  1147. // Switch branches
  1148. git.checkout().setName("side").call();
  1149. // replace link with directory containing files
  1150. FileUtils.delete(link);
  1151. // create and add a file in the new directory to the index
  1152. writeTrashFile(linkName + "/dir1", "file1", "c");
  1153. git.add().addFilepattern(linkName + "/dir1/file1").call();
  1154. // create but do not add a file in the new directory to the index
  1155. writeTrashFile(linkName + "/dir2", "file2", "d");
  1156. assertTrue("Link must be a directory now", link.isDirectory());
  1157. assertFalse("Must not delete non empty directory", link.delete());
  1158. // 2 extra files are created
  1159. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1160. linkName + "/dir2/file2", "d"));
  1161. try {
  1162. git.checkout().setName("master").call();
  1163. fail("did not throw exception");
  1164. } catch (Exception e) {
  1165. // 2 extra files are still there
  1166. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1167. linkName + "/dir2/file2", "d"));
  1168. }
  1169. }
  1170. @Test
  1171. public void testFileModeChangeWithNoContentChangeUpdate() throws Exception {
  1172. if (!FS.DETECTED.supportsExecute())
  1173. return;
  1174. Git git = Git.wrap(db);
  1175. // Add non-executable file
  1176. File file = writeTrashFile("file.txt", "a");
  1177. git.add().addFilepattern("file.txt").call();
  1178. git.commit().setMessage("commit1").call();
  1179. assertFalse(db.getFS().canExecute(file));
  1180. // Create branch
  1181. git.branchCreate().setName("b1").call();
  1182. // Make file executable
  1183. db.getFS().setExecute(file, true);
  1184. git.add().addFilepattern("file.txt").call();
  1185. git.commit().setMessage("commit2").call();
  1186. // Verify executable and working directory is clean
  1187. Status status = git.status().call();
  1188. assertTrue(status.getModified().isEmpty());
  1189. assertTrue(status.getChanged().isEmpty());
  1190. assertTrue(db.getFS().canExecute(file));
  1191. // Switch branches
  1192. git.checkout().setName("b1").call();
  1193. // Verify not executable and working directory is clean
  1194. status = git.status().call();
  1195. assertTrue(status.getModified().isEmpty());
  1196. assertTrue(status.getChanged().isEmpty());
  1197. assertFalse(db.getFS().canExecute(file));
  1198. }
  1199. @Test
  1200. public void testFileModeChangeAndContentChangeConflict() throws Exception {
  1201. if (!FS.DETECTED.supportsExecute())
  1202. return;
  1203. Git git = Git.wrap(db);
  1204. // Add non-executable file
  1205. File file = writeTrashFile("file.txt", "a");
  1206. git.add().addFilepattern("file.txt").call();
  1207. git.commit().setMessage("commit1").call();
  1208. assertFalse(db.getFS().canExecute(file));
  1209. // Create branch
  1210. git.branchCreate().setName("b1").call();
  1211. // Make file executable
  1212. db.getFS().setExecute(file, true);
  1213. git.add().addFilepattern("file.txt").call();
  1214. git.commit().setMessage("commit2").call();
  1215. // Verify executable and working directory is clean
  1216. Status status = git.status().call();
  1217. assertTrue(status.getModified().isEmpty());
  1218. assertTrue(status.getChanged().isEmpty());
  1219. assertTrue(db.getFS().canExecute(file));
  1220. writeTrashFile("file.txt", "b");
  1221. // Switch branches
  1222. CheckoutCommand checkout = git.checkout().setName("b1");
  1223. try {
  1224. checkout.call();
  1225. fail("Checkout exception not thrown");
  1226. } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) {
  1227. CheckoutResult result = checkout.getResult();
  1228. assertNotNull(result);
  1229. assertNotNull(result.getConflictList());
  1230. assertEquals(1, result.getConflictList().size());
  1231. assertTrue(result.getConflictList().contains("file.txt"));
  1232. }
  1233. }
  1234. @Test
  1235. public void testDirtyFileModeEqualHeadMerge()
  1236. throws Exception {
  1237. if (!FS.DETECTED.supportsExecute())
  1238. return;
  1239. Git git = Git.wrap(db);
  1240. // Add non-executable file
  1241. File file = writeTrashFile("file.txt", "a");
  1242. git.add().addFilepattern("file.txt").call();
  1243. git.commit().setMessage("commit1").call();
  1244. assertFalse(db.getFS().canExecute(file));
  1245. // Create branch
  1246. git.branchCreate().setName("b1").call();
  1247. // Create second commit and don't touch file
  1248. writeTrashFile("file2.txt", "");
  1249. git.add().addFilepattern("file2.txt").call();
  1250. git.commit().setMessage("commit2").call();
  1251. // stage a mode change
  1252. writeTrashFile("file.txt", "a");
  1253. db.getFS().setExecute(file, true);
  1254. git.add().addFilepattern("file.txt").call();
  1255. // dirty the file
  1256. writeTrashFile("file.txt", "b");
  1257. assertEquals(
  1258. "[file.txt, mode:100755, content:a][file2.txt, mode:100644, content:]",
  1259. indexState(CONTENT));
  1260. assertWorkDir(mkmap("file.txt", "b", "file2.txt", ""));
  1261. // Switch branches and check that the dirty file survived in worktree
  1262. // and index
  1263. git.checkout().setName("b1").call();
  1264. assertEquals("[file.txt, mode:100755, content:a]", indexState(CONTENT));
  1265. assertWorkDir(mkmap("file.txt", "b"));
  1266. }
  1267. @Test
  1268. public void testDirtyFileModeEqualIndexMerge()
  1269. throws Exception {
  1270. if (!FS.DETECTED.supportsExecute())
  1271. return;
  1272. Git git = Git.wrap(db);
  1273. // Add non-executable file
  1274. File file = writeTrashFile("file.txt", "a");
  1275. git.add().addFilepattern("file.txt").call();
  1276. git.commit().setMessage("commit1").call();
  1277. assertFalse(db.getFS().canExecute(file));
  1278. // Create branch
  1279. git.branchCreate().setName("b1").call();
  1280. // Create second commit with executable file
  1281. file = writeTrashFile("file.txt", "b");
  1282. db.getFS().setExecute(file, true);
  1283. git.add().addFilepattern("file.txt").call();
  1284. git.commit().setMessage("commit2").call();
  1285. // stage the same content as in the branch we want to switch to
  1286. writeTrashFile("file.txt", "a");
  1287. db.getFS().setExecute(file, false);
  1288. git.add().addFilepattern("file.txt").call();
  1289. // dirty the file
  1290. writeTrashFile("file.txt", "c");
  1291. db.getFS().setExecute(file, true);
  1292. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1293. assertWorkDir(mkmap("file.txt", "c"));
  1294. // Switch branches and check that the dirty file survived in worktree
  1295. // and index
  1296. git.checkout().setName("b1").call();
  1297. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1298. assertWorkDir(mkmap("file.txt", "c"));
  1299. }
  1300. @Test
  1301. public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
  1302. if (!FS.DETECTED.supportsExecute())
  1303. return;
  1304. Git git = Git.wrap(db);
  1305. // Add first file
  1306. File file1 = writeTrashFile("file1.txt", "a");
  1307. git.add().addFilepattern("file1.txt").call();
  1308. git.commit().setMessage("commit1").call();
  1309. assertFalse(db.getFS().canExecute(file1));
  1310. // Add second file
  1311. File file2 = writeTrashFile("file2.txt", "b");
  1312. git.add().addFilepattern("file2.txt").call();
  1313. git.commit().setMessage("commit2").call();
  1314. assertFalse(db.getFS().canExecute(file2));
  1315. // Create branch from first commit
  1316. assertNotNull(git.checkout().setCreateBranch(true).setName("b1")
  1317. .setStartPoint(Constants.HEAD + "~1").call());
  1318. // Change content and file mode in working directory and index
  1319. file1 = writeTrashFile("file1.txt", "c");
  1320. db.getFS().setExecute(file1, true);
  1321. git.add().addFilepattern("file1.txt").call();
  1322. // Switch back to 'master'
  1323. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  1324. }
  1325. public void assertWorkDir(Map<String, String> i)
  1326. throws CorruptObjectException,
  1327. IOException {
  1328. try (TreeWalk walk = new TreeWalk(db)) {
  1329. walk.setRecursive(false);
  1330. walk.addTree(new FileTreeIterator(db));
  1331. String expectedValue;
  1332. String path;
  1333. int nrFiles = 0;
  1334. FileTreeIterator ft;
  1335. while (walk.next()) {
  1336. ft = walk.getTree(0, FileTreeIterator.class);
  1337. path = ft.getEntryPathString();
  1338. expectedValue = i.get(path);
  1339. File file = new File(db.getWorkTree(), path);
  1340. assertTrue(file.exists());
  1341. if (file.isFile()) {
  1342. assertNotNull("found unexpected file for path " + path
  1343. + " in workdir", expectedValue);
  1344. FileInputStream is = new FileInputStream(file);
  1345. byte[] buffer = new byte[(int) file.length()];
  1346. int offset = 0;
  1347. int numRead = 0;
  1348. while (offset < buffer.length
  1349. && (numRead = is.read(buffer, offset, buffer.length
  1350. - offset)) >= 0) {
  1351. offset += numRead;
  1352. }
  1353. is.close();
  1354. assertArrayEquals("unexpected content for path " + path
  1355. + " in workDir. ", buffer, i.get(path).getBytes());
  1356. nrFiles++;
  1357. } else if (file.isDirectory()) {
  1358. if (file.list().length == 0) {
  1359. assertEquals("found unexpected empty folder for path "
  1360. + path + " in workDir. ", "/", i.get(path));
  1361. nrFiles++;
  1362. }
  1363. }
  1364. if (walk.isSubtree()) {
  1365. walk.enterSubtree();
  1366. }
  1367. }
  1368. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  1369. }
  1370. }
  1371. }