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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertNotNull;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.IOException;
  50. import java.util.Arrays;
  51. import java.util.HashMap;
  52. import java.util.List;
  53. import java.util.Map;
  54. import org.eclipse.jgit.api.Git;
  55. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  56. import org.eclipse.jgit.api.ResetCommand.ResetType;
  57. import org.eclipse.jgit.api.errors.GitAPIException;
  58. import org.eclipse.jgit.api.errors.NoFilepatternException;
  59. import org.eclipse.jgit.dircache.DirCache;
  60. import org.eclipse.jgit.dircache.DirCacheCheckout;
  61. import org.eclipse.jgit.dircache.DirCacheEditor;
  62. import org.eclipse.jgit.dircache.DirCacheEntry;
  63. import org.eclipse.jgit.errors.CheckoutConflictException;
  64. import org.eclipse.jgit.errors.CorruptObjectException;
  65. import org.eclipse.jgit.errors.NoWorkTreeException;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.treewalk.FileTreeIterator;
  68. import org.eclipse.jgit.treewalk.TreeWalk;
  69. import org.junit.Test;
  70. public class DirCacheCheckoutTest extends RepositoryTestCase {
  71. private DirCacheCheckout dco;
  72. protected Tree theHead;
  73. protected Tree theMerge;
  74. private DirCache dirCache;
  75. private void prescanTwoTrees(Tree head, Tree merge)
  76. throws IllegalStateException, IOException {
  77. DirCache dc = db.lockDirCache();
  78. try {
  79. dco = new DirCacheCheckout(db, head.getId(), dc, merge.getId());
  80. dco.preScanTwoTrees();
  81. } finally {
  82. dc.unlock();
  83. }
  84. }
  85. private void checkout() throws IOException {
  86. DirCache dc = db.lockDirCache();
  87. try {
  88. dco = new DirCacheCheckout(db, theHead.getId(), dc, theMerge.getId());
  89. dco.checkout();
  90. } finally {
  91. dc.unlock();
  92. }
  93. }
  94. private List<String> getRemoved() {
  95. return dco.getRemoved();
  96. }
  97. private Map<String, ObjectId> getUpdated() {
  98. return dco.getUpdated();
  99. }
  100. private List<String> getConflicts() {
  101. return dco.getConflicts();
  102. }
  103. private static HashMap<String, String> mk(String a) {
  104. return mkmap(a, a);
  105. }
  106. private static HashMap<String, String> mkmap(String... args) {
  107. if ((args.length % 2) > 0)
  108. throw new IllegalArgumentException("needs to be pairs");
  109. HashMap<String, String> map = new HashMap<String, String>();
  110. for (int i = 0; i < args.length; i += 2) {
  111. map.put(args[i], args[i + 1]);
  112. }
  113. return map;
  114. }
  115. @Test
  116. public void testResetHard() throws IOException, NoFilepatternException,
  117. GitAPIException {
  118. Git git = new Git(db);
  119. writeTrashFile("f", "f()");
  120. writeTrashFile("D/g", "g()");
  121. git.add().addFilepattern(".").call();
  122. git.commit().setMessage("inital").call();
  123. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  124. git.branchCreate().setName("topic").call();
  125. writeTrashFile("f", "f()\nmaster");
  126. writeTrashFile("D/g", "g()\ng2()");
  127. writeTrashFile("E/h", "h()");
  128. git.add().addFilepattern(".").call();
  129. RevCommit master = git.commit().setMessage("master-1").call();
  130. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  131. checkoutBranch("refs/heads/topic");
  132. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  133. writeTrashFile("f", "f()\nside");
  134. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  135. writeTrashFile("G/i", "i()");
  136. git.add().addFilepattern(".").call();
  137. git.add().addFilepattern(".").setUpdate(true).call();
  138. RevCommit topic = git.commit().setMessage("topic-1").call();
  139. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  140. writeTrashFile("untracked", "untracked");
  141. resetHard(master);
  142. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  143. resetHard(topic);
  144. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  145. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
  146. "untracked"));
  147. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  148. .call().getMergeStatus());
  149. assertEquals(
  150. "[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]",
  151. indexState(0));
  152. resetHard(master);
  153. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  154. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  155. "h()", "untracked", "untracked"));
  156. }
  157. /**
  158. * Reset hard from unclean condition.
  159. * <p>
  160. * WorkDir: Empty <br/>
  161. * Index: f/g <br/>
  162. * Merge: x
  163. *
  164. * @throws Exception
  165. */
  166. @Test
  167. public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
  168. throws Exception {
  169. Git git = new Git(db);
  170. writeTrashFile("x", "x");
  171. git.add().addFilepattern("x").call();
  172. RevCommit id1 = git.commit().setMessage("c1").call();
  173. writeTrashFile("f/g", "f/g");
  174. git.rm().addFilepattern("x").call();
  175. git.add().addFilepattern("f/g").call();
  176. git.commit().setMessage("c2").call();
  177. deleteTrashFile("f/g");
  178. deleteTrashFile("f");
  179. // The actual test
  180. git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
  181. assertIndex(mkmap("x", "x"));
  182. }
  183. private DirCacheCheckout resetHard(RevCommit commit)
  184. throws NoWorkTreeException,
  185. CorruptObjectException, IOException {
  186. DirCacheCheckout dc;
  187. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  188. commit.getTree());
  189. dc.setFailOnConflict(true);
  190. assertTrue(dc.checkout());
  191. return dc;
  192. }
  193. private void assertIndex(HashMap<String, String> i)
  194. throws CorruptObjectException, IOException {
  195. String expectedValue;
  196. String path;
  197. DirCache read = DirCache.read(db.getIndexFile(), db.getFS());
  198. assertEquals("Index has not the right size.", i.size(),
  199. read.getEntryCount());
  200. for (int j = 0; j < read.getEntryCount(); j++) {
  201. path = read.getEntry(j).getPathString();
  202. expectedValue = i.get(path);
  203. assertNotNull("found unexpected entry for path " + path
  204. + " in index", expectedValue);
  205. assertTrue("unexpected content for path " + path
  206. + " in index. Expected: <" + expectedValue + ">",
  207. Arrays.equals(db.open(read.getEntry(j).getObjectId())
  208. .getCachedBytes(), i.get(path).getBytes()));
  209. }
  210. }
  211. @Test
  212. public void testRules1thru3_NoIndexEntry() throws IOException {
  213. Tree head = new Tree(db);
  214. head = buildTree(mk("foo"));
  215. ObjectId objectId = head.findBlobMember("foo").getId();
  216. Tree merge = new Tree(db);
  217. prescanTwoTrees(head, merge);
  218. assertTrue(getRemoved().contains("foo"));
  219. prescanTwoTrees(merge, head);
  220. assertEquals(objectId, getUpdated().get("foo"));
  221. merge = buildTree(mkmap("foo", "a"));
  222. ObjectId anotherId = merge.findBlobMember("foo").getId();
  223. prescanTwoTrees(head, merge);
  224. assertEquals(anotherId, getUpdated().get("foo"));
  225. }
  226. void setupCase(HashMap<String, String> headEntries, HashMap<String, String> mergeEntries, HashMap<String, String> indexEntries) throws IOException {
  227. theHead = buildTree(headEntries);
  228. theMerge = buildTree(mergeEntries);
  229. buildIndex(indexEntries);
  230. }
  231. private void buildIndex(HashMap<String, String> indexEntries) throws IOException {
  232. dirCache = new DirCache(db.getIndexFile(), db.getFS());
  233. if (indexEntries != null) {
  234. assertTrue(dirCache.lock());
  235. DirCacheEditor editor = dirCache.editor();
  236. for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
  237. writeTrashFile(e.getKey(), e.getValue());
  238. ObjectInserter inserter = db.newObjectInserter();
  239. final ObjectId id = inserter.insert(Constants.OBJ_BLOB,
  240. Constants.encode(e.getValue()));
  241. editor.add(new DirCacheEditor.DeletePath(e.getKey()));
  242. editor.add(new DirCacheEditor.PathEdit(e.getKey()) {
  243. @Override
  244. public void apply(DirCacheEntry ent) {
  245. ent.setFileMode(FileMode.REGULAR_FILE);
  246. ent.setObjectId(id);
  247. ent.setUpdateNeeded(false);
  248. }
  249. });
  250. }
  251. assertTrue(editor.commit());
  252. }
  253. }
  254. private Tree buildTree(HashMap<String, String> headEntries) throws IOException {
  255. Tree tree = new Tree(db);
  256. if (headEntries == null)
  257. return tree;
  258. FileTreeEntry fileEntry;
  259. Tree parent;
  260. ObjectInserter oi = db.newObjectInserter();
  261. try {
  262. for (java.util.Map.Entry<String, String> e : headEntries.entrySet()) {
  263. fileEntry = tree.addFile(e.getKey());
  264. fileEntry.setId(genSha1(e.getValue()));
  265. parent = fileEntry.getParent();
  266. while (parent != null) {
  267. parent.setId(oi.insert(Constants.OBJ_TREE, parent.format()));
  268. parent = parent.getParent();
  269. }
  270. }
  271. oi.flush();
  272. } finally {
  273. oi.release();
  274. }
  275. return tree;
  276. }
  277. ObjectId genSha1(String data) {
  278. ObjectInserter w = db.newObjectInserter();
  279. try {
  280. ObjectId id = w.insert(Constants.OBJ_BLOB, data.getBytes());
  281. w.flush();
  282. return id;
  283. } catch (IOException e) {
  284. fail(e.toString());
  285. } finally {
  286. w.release();
  287. }
  288. return null;
  289. }
  290. protected void go() throws IllegalStateException, IOException {
  291. prescanTwoTrees(theHead, theMerge);
  292. }
  293. @Test
  294. public void testRules4thru13_IndexEntryNotInHead() throws IOException {
  295. // rules 4 and 5
  296. HashMap<String, String> idxMap;
  297. idxMap = new HashMap<String, String>();
  298. idxMap.put("foo", "foo");
  299. setupCase(null, null, idxMap);
  300. go();
  301. assertTrue(getUpdated().isEmpty());
  302. assertTrue(getRemoved().isEmpty());
  303. assertTrue(getConflicts().isEmpty());
  304. // rules 6 and 7
  305. idxMap = new HashMap<String, String>();
  306. idxMap.put("foo", "foo");
  307. setupCase(null, idxMap, idxMap);
  308. go();
  309. assertAllEmpty();
  310. // rules 8 and 9
  311. HashMap<String, String> mergeMap;
  312. mergeMap = new HashMap<String, String>();
  313. mergeMap.put("foo", "merge");
  314. setupCase(null, mergeMap, idxMap);
  315. go();
  316. assertTrue(getUpdated().isEmpty());
  317. assertTrue(getRemoved().isEmpty());
  318. assertTrue(getConflicts().contains("foo"));
  319. // rule 10
  320. HashMap<String, String> headMap = new HashMap<String, String>();
  321. headMap.put("foo", "foo");
  322. setupCase(headMap, null, idxMap);
  323. go();
  324. assertTrue(getRemoved().contains("foo"));
  325. assertTrue(getUpdated().isEmpty());
  326. assertTrue(getConflicts().isEmpty());
  327. // rule 11
  328. setupCase(headMap, null, idxMap);
  329. assertTrue(new File(trash, "foo").delete());
  330. writeTrashFile("foo", "bar");
  331. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  332. go();
  333. assertTrue(getRemoved().isEmpty());
  334. assertTrue(getUpdated().isEmpty());
  335. assertTrue(getConflicts().contains("foo"));
  336. // rule 12 & 13
  337. headMap.put("foo", "head");
  338. setupCase(headMap, null, idxMap);
  339. go();
  340. assertTrue(getRemoved().isEmpty());
  341. assertTrue(getUpdated().isEmpty());
  342. assertTrue(getConflicts().contains("foo"));
  343. // rules 14 & 15
  344. setupCase(headMap, headMap, idxMap);
  345. go();
  346. assertAllEmpty();
  347. // rules 16 & 17
  348. setupCase(headMap, mergeMap, idxMap); go();
  349. assertTrue(getConflicts().contains("foo"));
  350. // rules 18 & 19
  351. setupCase(headMap, idxMap, idxMap); go();
  352. assertAllEmpty();
  353. // rule 20
  354. setupCase(idxMap, mergeMap, idxMap); go();
  355. assertTrue(getUpdated().containsKey("foo"));
  356. // rules 21
  357. setupCase(idxMap, mergeMap, idxMap);
  358. assertTrue(new File(trash, "foo").delete());
  359. writeTrashFile("foo", "bar");
  360. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  361. go();
  362. assertTrue(getConflicts().contains("foo"));
  363. }
  364. private void assertAllEmpty() {
  365. assertTrue(getRemoved().isEmpty());
  366. assertTrue(getUpdated().isEmpty());
  367. assertTrue(getConflicts().isEmpty());
  368. }
  369. /*-
  370. * Directory/File Conflict cases:
  371. * It's entirely possible that in practice a number of these may be equivalent
  372. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  373. * that's all I care about. These are basically reverse-engineered from
  374. * what git currently does. If there are tests for these in git, it's kind of
  375. * hard to track them all down...
  376. *
  377. * H I M Clean H==M H==I I==M Result
  378. * ------------------------------------------------------------------
  379. *1 D D F Y N Y N Update
  380. *2 D D F N N Y N Conflict
  381. *3 D F D Y N N Update
  382. *4 D F D N N N Update
  383. *5 D F F Y N N Y Keep
  384. *6 D F F N N N Y Keep
  385. *7 F D F Y Y N N Update
  386. *8 F D F N Y N N Conflict
  387. *9 F D F Y N N N Update
  388. *10 F D D N N Y Keep
  389. *11 F D D N N N Conflict
  390. *12 F F D Y N Y N Update
  391. *13 F F D N N Y N Conflict
  392. *14 F F D N N N Conflict
  393. *15 0 F D N N N Conflict
  394. *16 0 D F Y N N N Update
  395. *17 0 D F N N N Conflict
  396. *18 F 0 D Update
  397. *19 D 0 F Update
  398. */
  399. @Test
  400. public void testDirectoryFileSimple() throws IOException {
  401. Tree treeDF = buildTree(mkmap("DF", "DF"));
  402. Tree treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  403. buildIndex(mkmap("DF", "DF"));
  404. prescanTwoTrees(treeDF, treeDFDF);
  405. assertTrue(getRemoved().contains("DF"));
  406. assertTrue(getUpdated().containsKey("DF/DF"));
  407. recursiveDelete(new File(trash, "DF"));
  408. buildIndex(mkmap("DF/DF", "DF/DF"));
  409. prescanTwoTrees(treeDFDF, treeDF);
  410. assertTrue(getRemoved().contains("DF/DF"));
  411. assertTrue(getUpdated().containsKey("DF"));
  412. }
  413. @Test
  414. public void testDirectoryFileConflicts_1() throws Exception {
  415. // 1
  416. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  417. assertNoConflicts();
  418. assertUpdated("DF");
  419. assertRemoved("DF/DF");
  420. }
  421. @Test
  422. public void testDirectoryFileConflicts_2() throws Exception {
  423. // 2
  424. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  425. writeTrashFile("DF/DF", "different");
  426. go();
  427. assertConflict("DF/DF");
  428. }
  429. @Test
  430. public void testDirectoryFileConflicts_3() throws Exception {
  431. // 3 - the first to break!
  432. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  433. assertUpdated("DF/DF");
  434. assertRemoved("DF");
  435. }
  436. @Test
  437. public void testDirectoryFileConflicts_4() throws Exception {
  438. // 4 (basically same as 3, just with H and M different)
  439. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  440. assertUpdated("DF/DF");
  441. assertRemoved("DF");
  442. }
  443. @Test
  444. public void testDirectoryFileConflicts_5() throws Exception {
  445. // 5
  446. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  447. assertRemoved("DF/DF");
  448. }
  449. @Test
  450. public void testDirectoryFileConflicts_6() throws Exception {
  451. // 6
  452. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  453. writeTrashFile("DF", "different");
  454. go();
  455. assertRemoved("DF/DF");
  456. }
  457. @Test
  458. public void testDirectoryFileConflicts_7() throws Exception {
  459. // 7
  460. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  461. assertUpdated("DF");
  462. assertRemoved("DF/DF");
  463. cleanUpDF();
  464. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  465. go();
  466. assertRemoved("DF/DF/DF/DF/DF");
  467. assertUpdated("DF/DF");
  468. cleanUpDF();
  469. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  470. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  471. go();
  472. assertConflict("DF/DF/DF/DF/DF");
  473. // assertUpdated("DF/DF");
  474. // Why do we expect an update on DF/DF. H==M,
  475. // H&M are files and index contains a dir, index
  476. // is dirty: that case is not in the table but
  477. // we cannot update DF/DF to a file, this would
  478. // require that we delete DF/DF/DF/DF/DF in workdir
  479. // throwing away unsaved contents.
  480. // This test would fail in DirCacheCheckoutTests.
  481. }
  482. @Test
  483. public void testDirectoryFileConflicts_8() throws Exception {
  484. // 8
  485. setupCase(mk("DF"), mk("DF"), mk("DF/DF"));
  486. recursiveDelete(new File(db.getWorkTree(), "DF"));
  487. writeTrashFile("DF", "xy");
  488. go();
  489. assertConflict("DF/DF");
  490. }
  491. @Test
  492. public void testDirectoryFileConflicts_9() throws Exception {
  493. // 9
  494. doit(mk("DF"), mkmap("DF", "QP"), mk("DF/DF"));
  495. assertRemoved("DF/DF");
  496. assertUpdated("DF");
  497. }
  498. @Test
  499. public void testDirectoryFileConflicts_10() throws Exception {
  500. // 10
  501. cleanUpDF();
  502. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  503. assertNoConflicts();
  504. }
  505. @Test
  506. public void testDirectoryFileConflicts_11() throws Exception {
  507. // 11
  508. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  509. assertConflict("DF/DF");
  510. }
  511. @Test
  512. public void testDirectoryFileConflicts_12() throws Exception {
  513. // 12
  514. cleanUpDF();
  515. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  516. assertRemoved("DF");
  517. assertUpdated("DF/DF");
  518. }
  519. @Test
  520. public void testDirectoryFileConflicts_13() throws Exception {
  521. // 13
  522. cleanUpDF();
  523. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  524. writeTrashFile("DF", "asdfsdf");
  525. go();
  526. assertConflict("DF");
  527. assertUpdated("DF/DF");
  528. }
  529. @Test
  530. public void testDirectoryFileConflicts_14() throws Exception {
  531. // 14
  532. cleanUpDF();
  533. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  534. assertConflict("DF");
  535. assertUpdated("DF/DF");
  536. }
  537. @Test
  538. public void testDirectoryFileConflicts_15() throws Exception {
  539. // 15
  540. doit(mkmap(), mk("DF/DF"), mk("DF"));
  541. // This test would fail in DirCacheCheckoutTests. I think this test is wrong,
  542. // it should check for conflicts according to rule 15
  543. // assertRemoved("DF");
  544. assertUpdated("DF/DF");
  545. }
  546. @Test
  547. public void testDirectoryFileConflicts_15b() throws Exception {
  548. // 15, take 2, just to check multi-leveled
  549. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  550. // I think this test is wrong, it should
  551. // check for conflicts according to rule 15
  552. // This test would fail in DirCacheCheckouts
  553. // assertRemoved("DF");
  554. assertUpdated("DF/DF/DF/DF");
  555. }
  556. @Test
  557. public void testDirectoryFileConflicts_16() throws Exception {
  558. // 16
  559. cleanUpDF();
  560. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  561. assertRemoved("DF/DF/DF");
  562. assertUpdated("DF");
  563. }
  564. @Test
  565. public void testDirectoryFileConflicts_17() throws Exception {
  566. // 17
  567. cleanUpDF();
  568. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  569. writeTrashFile("DF/DF/DF", "asdf");
  570. go();
  571. assertConflict("DF/DF/DF");
  572. // Why do we expect an update on DF. If we really update
  573. // DF and update also the working tree we would have to
  574. // overwrite a dirty file in the work-tree DF/DF/DF
  575. // This test would fail in DirCacheCheckout
  576. // assertUpdated("DF");
  577. }
  578. @Test
  579. public void testDirectoryFileConflicts_18() throws Exception {
  580. // 18
  581. cleanUpDF();
  582. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  583. assertRemoved("DF/DF");
  584. assertUpdated("DF/DF/DF/DF");
  585. }
  586. @Test
  587. public void testDirectoryFileConflicts_19() throws Exception {
  588. // 19
  589. cleanUpDF();
  590. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  591. assertRemoved("DF/DF/DF/DF");
  592. assertUpdated("DF/DF/DF");
  593. }
  594. protected void cleanUpDF() throws Exception {
  595. tearDown();
  596. setUp();
  597. recursiveDelete(new File(trash, "DF"));
  598. }
  599. protected void assertConflict(String s) {
  600. assertTrue(getConflicts().contains(s));
  601. }
  602. protected void assertUpdated(String s) {
  603. assertTrue(getUpdated().containsKey(s));
  604. }
  605. protected void assertRemoved(String s) {
  606. assertTrue(getRemoved().contains(s));
  607. }
  608. protected void assertNoConflicts() {
  609. assertTrue(getConflicts().isEmpty());
  610. }
  611. protected void doit(HashMap<String, String> h, HashMap<String, String> m, HashMap<String, String> i)
  612. throws IOException {
  613. setupCase(h, m, i);
  614. go();
  615. }
  616. @Test
  617. public void testUntrackedConflicts() throws IOException {
  618. setupCase(null, mk("foo"), null);
  619. writeTrashFile("foo", "foo");
  620. go();
  621. // test that we don't overwrite untracked files when there is a HEAD
  622. recursiveDelete(new File(trash, "foo"));
  623. setupCase(mk("other"), mkmap("other", "other", "foo", "foo"),
  624. mk("other"));
  625. writeTrashFile("foo", "bar");
  626. try {
  627. checkout();
  628. fail("didn't get the expected exception");
  629. } catch (CheckoutConflictException e) {
  630. assertConflict("foo");
  631. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  632. assertIndex(mk("other"));
  633. }
  634. // test that we don't overwrite untracked files when there is no HEAD
  635. recursiveDelete(new File(trash, "other"));
  636. recursiveDelete(new File(trash, "foo"));
  637. setupCase(null, mk("foo"), null);
  638. writeTrashFile("foo", "bar");
  639. try {
  640. checkout();
  641. fail("didn't get the expected exception");
  642. } catch (CheckoutConflictException e) {
  643. assertConflict("foo");
  644. assertWorkDir(mkmap("foo", "bar"));
  645. assertIndex(mkmap("other", "other"));
  646. }
  647. // TODO: Why should we expect conflicts here?
  648. // H and M are empty and according to rule #5 of
  649. // the carry-over rules a dirty index is no reason
  650. // for a conflict. (I also feel it should be a
  651. // conflict because we are going to overwrite
  652. // unsaved content in the working tree
  653. // This test would fail in DirCacheCheckoutTest
  654. // assertConflict("foo");
  655. recursiveDelete(new File(trash, "foo"));
  656. recursiveDelete(new File(trash, "other"));
  657. setupCase(null, mk("foo"), null);
  658. writeTrashFile("foo/bar/baz", "");
  659. writeTrashFile("foo/blahblah", "");
  660. go();
  661. assertConflict("foo");
  662. assertConflict("foo/bar/baz");
  663. assertConflict("foo/blahblah");
  664. recursiveDelete(new File(trash, "foo"));
  665. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  666. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  667. assertTrue(new File(trash, "foo/bar").exists());
  668. go();
  669. assertNoConflicts();
  670. }
  671. @Test
  672. public void testCloseNameConflictsX0() throws IOException {
  673. 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") );
  674. checkout();
  675. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  676. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  677. go();
  678. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  679. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  680. assertNoConflicts();
  681. }
  682. @Test
  683. public void testCloseNameConflicts1() throws IOException {
  684. 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") );
  685. checkout();
  686. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  687. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  688. go();
  689. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  690. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  691. assertNoConflicts();
  692. }
  693. @Test
  694. public void testCheckoutHierarchy() throws IOException {
  695. setupCase(
  696. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  697. "e/g"),
  698. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  699. "e/g2"),
  700. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  701. "e/g3"));
  702. try {
  703. checkout();
  704. } catch (CheckoutConflictException e) {
  705. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  706. "e/f", "e/g", "e/g3"));
  707. assertConflict("e/g");
  708. }
  709. }
  710. @Test
  711. public void testCheckoutOutChanges() throws IOException {
  712. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  713. checkout();
  714. assertIndex(mk("foo/bar"));
  715. assertWorkDir(mk("foo/bar"));
  716. assertFalse(new File(trash, "foo").isFile());
  717. assertTrue(new File(trash, "foo/bar").isFile());
  718. recursiveDelete(new File(trash, "foo"));
  719. assertWorkDir(mkmap());
  720. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  721. checkout();
  722. assertIndex(mk("foo"));
  723. assertWorkDir(mk("foo"));
  724. assertFalse(new File(trash, "foo/bar").isFile());
  725. assertTrue(new File(trash, "foo").isFile());
  726. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  727. assertIndex(mkmap("foo", "bar"));
  728. assertWorkDir(mkmap("foo", "bar"));
  729. try {
  730. checkout();
  731. fail("did not throw exception");
  732. } catch (CheckoutConflictException e) {
  733. assertIndex(mkmap("foo", "bar"));
  734. assertWorkDir(mkmap("foo", "bar"));
  735. }
  736. }
  737. @Test
  738. public void testCheckoutUncachedChanges() throws IOException {
  739. setupCase(mk("foo"), mk("foo"), mk("foo"));
  740. writeTrashFile("foo", "otherData");
  741. checkout();
  742. assertIndex(mk("foo"));
  743. assertWorkDir(mkmap("foo", "otherData"));
  744. assertTrue(new File(trash, "foo").isFile());
  745. }
  746. @Test
  747. public void testDontOverwriteDirtyFile() throws IOException {
  748. setupCase(mk("foo"), mk("other"), mk("foo"));
  749. writeTrashFile("foo", "different");
  750. try {
  751. checkout();
  752. fail("Didn't got the expected conflict");
  753. } catch (CheckoutConflictException e) {
  754. assertIndex(mk("foo"));
  755. assertWorkDir(mkmap("foo", "different"));
  756. assertTrue(getConflicts().equals(Arrays.asList("foo")));
  757. assertTrue(new File(trash, "foo").isFile());
  758. }
  759. }
  760. public void assertWorkDir(HashMap<String, String> i) throws CorruptObjectException,
  761. IOException {
  762. TreeWalk walk = new TreeWalk(db);
  763. walk.setRecursive(true);
  764. walk.addTree(new FileTreeIterator(db));
  765. String expectedValue;
  766. String path;
  767. int nrFiles = 0;
  768. FileTreeIterator ft;
  769. while (walk.next()) {
  770. ft = walk.getTree(0, FileTreeIterator.class);
  771. path = ft.getEntryPathString();
  772. expectedValue = i.get(path);
  773. assertNotNull("found unexpected file for path " + path
  774. + " in workdir", expectedValue);
  775. File file = new File(db.getWorkTree(), path);
  776. assertTrue(file.exists());
  777. if (file.isFile()) {
  778. FileInputStream is = new FileInputStream(file);
  779. byte[] buffer = new byte[(int) file.length()];
  780. int offset = 0;
  781. int numRead = 0;
  782. while (offset < buffer.length
  783. && (numRead = is.read(buffer, offset, buffer.length
  784. - offset)) >= 0) {
  785. offset += numRead;
  786. }
  787. is.close();
  788. assertTrue("unexpected content for path " + path
  789. + " in workDir. Expected: <" + expectedValue + ">",
  790. Arrays.equals(buffer, i.get(path).getBytes()));
  791. nrFiles++;
  792. }
  793. }
  794. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  795. }
  796. }