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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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.DirCacheEditor;
  66. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  67. import org.eclipse.jgit.dircache.DirCacheEntry;
  68. import org.eclipse.jgit.errors.CheckoutConflictException;
  69. import org.eclipse.jgit.errors.CorruptObjectException;
  70. import org.eclipse.jgit.errors.NoWorkTreeException;
  71. import org.eclipse.jgit.revwalk.RevCommit;
  72. import org.eclipse.jgit.treewalk.FileTreeIterator;
  73. import org.eclipse.jgit.treewalk.TreeWalk;
  74. import org.eclipse.jgit.util.FS;
  75. import org.junit.Test;
  76. public class DirCacheCheckoutTest extends RepositoryTestCase {
  77. private DirCacheCheckout dco;
  78. protected ObjectId theHead;
  79. protected ObjectId theMerge;
  80. private DirCache dirCache;
  81. private void prescanTwoTrees(ObjectId head, ObjectId merge)
  82. throws IllegalStateException, IOException {
  83. DirCache dc = db.lockDirCache();
  84. try {
  85. dco = new DirCacheCheckout(db, head, dc, merge);
  86. dco.preScanTwoTrees();
  87. } finally {
  88. dc.unlock();
  89. }
  90. }
  91. private void checkout() throws IOException {
  92. DirCache dc = db.lockDirCache();
  93. try {
  94. dco = new DirCacheCheckout(db, theHead, dc, theMerge);
  95. dco.checkout();
  96. } finally {
  97. dc.unlock();
  98. }
  99. }
  100. private List<String> getRemoved() {
  101. return dco.getRemoved();
  102. }
  103. private Map<String, ObjectId> getUpdated() {
  104. return dco.getUpdated();
  105. }
  106. private List<String> getConflicts() {
  107. return dco.getConflicts();
  108. }
  109. private static HashMap<String, String> mk(String a) {
  110. return mkmap(a, a);
  111. }
  112. private static HashMap<String, String> mkmap(String... args) {
  113. if ((args.length % 2) > 0)
  114. throw new IllegalArgumentException("needs to be pairs");
  115. HashMap<String, String> map = new HashMap<String, String>();
  116. for (int i = 0; i < args.length; i += 2) {
  117. map.put(args[i], args[i + 1]);
  118. }
  119. return map;
  120. }
  121. @Test
  122. public void testResetHard() throws IOException, NoFilepatternException,
  123. GitAPIException {
  124. Git git = new Git(db);
  125. writeTrashFile("f", "f()");
  126. writeTrashFile("D/g", "g()");
  127. git.add().addFilepattern(".").call();
  128. git.commit().setMessage("inital").call();
  129. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  130. git.branchCreate().setName("topic").call();
  131. writeTrashFile("f", "f()\nmaster");
  132. writeTrashFile("D/g", "g()\ng2()");
  133. writeTrashFile("E/h", "h()");
  134. git.add().addFilepattern(".").call();
  135. RevCommit master = git.commit().setMessage("master-1").call();
  136. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  137. checkoutBranch("refs/heads/topic");
  138. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  139. writeTrashFile("f", "f()\nside");
  140. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  141. writeTrashFile("G/i", "i()");
  142. git.add().addFilepattern(".").call();
  143. git.add().addFilepattern(".").setUpdate(true).call();
  144. RevCommit topic = git.commit().setMessage("topic-1").call();
  145. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  146. writeTrashFile("untracked", "untracked");
  147. resetHard(master);
  148. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  149. resetHard(topic);
  150. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  151. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
  152. "untracked"));
  153. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  154. .call().getMergeStatus());
  155. assertEquals(
  156. "[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]",
  157. indexState(0));
  158. resetHard(master);
  159. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  160. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  161. "h()", "untracked", "untracked"));
  162. }
  163. /**
  164. * Reset hard from unclean condition.
  165. * <p>
  166. * WorkDir: Empty <br/>
  167. * Index: f/g <br/>
  168. * Merge: x
  169. *
  170. * @throws Exception
  171. */
  172. @Test
  173. public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
  174. throws Exception {
  175. Git git = new Git(db);
  176. writeTrashFile("x", "x");
  177. git.add().addFilepattern("x").call();
  178. RevCommit id1 = git.commit().setMessage("c1").call();
  179. writeTrashFile("f/g", "f/g");
  180. git.rm().addFilepattern("x").call();
  181. git.add().addFilepattern("f/g").call();
  182. git.commit().setMessage("c2").call();
  183. deleteTrashFile("f/g");
  184. deleteTrashFile("f");
  185. // The actual test
  186. git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
  187. assertIndex(mkmap("x", "x"));
  188. }
  189. private DirCacheCheckout resetHard(RevCommit commit)
  190. throws NoWorkTreeException,
  191. CorruptObjectException, IOException {
  192. DirCacheCheckout dc;
  193. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  194. commit.getTree());
  195. dc.setFailOnConflict(true);
  196. assertTrue(dc.checkout());
  197. return dc;
  198. }
  199. private void assertIndex(HashMap<String, String> i)
  200. throws CorruptObjectException, IOException {
  201. String expectedValue;
  202. String path;
  203. DirCache read = DirCache.read(db.getIndexFile(), db.getFS());
  204. assertEquals("Index has not the right size.", i.size(),
  205. read.getEntryCount());
  206. for (int j = 0; j < read.getEntryCount(); j++) {
  207. path = read.getEntry(j).getPathString();
  208. expectedValue = i.get(path);
  209. assertNotNull("found unexpected entry for path " + path
  210. + " in index", expectedValue);
  211. assertTrue("unexpected content for path " + path
  212. + " in index. Expected: <" + expectedValue + ">",
  213. Arrays.equals(db.open(read.getEntry(j).getObjectId())
  214. .getCachedBytes(), i.get(path).getBytes()));
  215. }
  216. }
  217. @Test
  218. public void testRules1thru3_NoIndexEntry() throws IOException {
  219. ObjectId head = buildTree(mk("foo"));
  220. TreeWalk tw = TreeWalk.forPath(db, "foo", head);
  221. ObjectId objectId = tw.getObjectId(0);
  222. ObjectId merge = db.newObjectInserter().insert(Constants.OBJ_TREE,
  223. new byte[0]);
  224. prescanTwoTrees(head, merge);
  225. assertTrue(getRemoved().contains("foo"));
  226. prescanTwoTrees(merge, head);
  227. assertEquals(objectId, getUpdated().get("foo"));
  228. merge = buildTree(mkmap("foo", "a"));
  229. tw = TreeWalk.forPath(db, "foo", merge);
  230. prescanTwoTrees(head, merge);
  231. assertConflict("foo");
  232. }
  233. void setupCase(HashMap<String, String> headEntries, HashMap<String, String> mergeEntries, HashMap<String, String> indexEntries) throws IOException {
  234. theHead = buildTree(headEntries);
  235. theMerge = buildTree(mergeEntries);
  236. buildIndex(indexEntries);
  237. }
  238. private void buildIndex(HashMap<String, String> indexEntries) throws IOException {
  239. dirCache = new DirCache(db.getIndexFile(), db.getFS());
  240. if (indexEntries != null) {
  241. assertTrue(dirCache.lock());
  242. DirCacheEditor editor = dirCache.editor();
  243. for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
  244. writeTrashFile(e.getKey(), e.getValue());
  245. ObjectInserter inserter = db.newObjectInserter();
  246. final ObjectId id = inserter.insert(Constants.OBJ_BLOB,
  247. Constants.encode(e.getValue()));
  248. editor.add(new DirCacheEditor.DeletePath(e.getKey()));
  249. editor.add(new DirCacheEditor.PathEdit(e.getKey()) {
  250. @Override
  251. public void apply(DirCacheEntry ent) {
  252. ent.setFileMode(FileMode.REGULAR_FILE);
  253. ent.setObjectId(id);
  254. ent.setUpdateNeeded(false);
  255. }
  256. });
  257. }
  258. assertTrue(editor.commit());
  259. }
  260. }
  261. static final class AddEdit extends PathEdit {
  262. private final ObjectId data;
  263. private final long length;
  264. public AddEdit(String entryPath, ObjectId data, long length) {
  265. super(entryPath);
  266. this.data = data;
  267. this.length = length;
  268. }
  269. @Override
  270. public void apply(DirCacheEntry ent) {
  271. ent.setFileMode(FileMode.REGULAR_FILE);
  272. ent.setLength(length);
  273. ent.setObjectId(data);
  274. }
  275. }
  276. private ObjectId buildTree(HashMap<String, String> headEntries)
  277. throws IOException {
  278. DirCache lockDirCache = DirCache.newInCore();
  279. // assertTrue(lockDirCache.lock());
  280. DirCacheEditor editor = lockDirCache.editor();
  281. if (headEntries != null) {
  282. for (java.util.Map.Entry<String, String> e : headEntries.entrySet()) {
  283. AddEdit addEdit = new AddEdit(e.getKey(),
  284. genSha1(e.getValue()), e.getValue().length());
  285. editor.add(addEdit);
  286. }
  287. }
  288. editor.finish();
  289. return lockDirCache.writeTree(db.newObjectInserter());
  290. }
  291. ObjectId genSha1(String data) {
  292. ObjectInserter w = db.newObjectInserter();
  293. try {
  294. ObjectId id = w.insert(Constants.OBJ_BLOB, data.getBytes());
  295. w.flush();
  296. return id;
  297. } catch (IOException e) {
  298. fail(e.toString());
  299. } finally {
  300. w.release();
  301. }
  302. return null;
  303. }
  304. protected void go() throws IllegalStateException, IOException {
  305. prescanTwoTrees(theHead, theMerge);
  306. }
  307. @Test
  308. public void testRules4thru13_IndexEntryNotInHead() throws IOException {
  309. // rules 4 and 5
  310. HashMap<String, String> idxMap;
  311. idxMap = new HashMap<String, String>();
  312. idxMap.put("foo", "foo");
  313. setupCase(null, null, idxMap);
  314. go();
  315. assertTrue(getUpdated().isEmpty());
  316. assertTrue(getRemoved().isEmpty());
  317. assertTrue(getConflicts().isEmpty());
  318. // rules 6 and 7
  319. idxMap = new HashMap<String, String>();
  320. idxMap.put("foo", "foo");
  321. setupCase(null, idxMap, idxMap);
  322. go();
  323. assertAllEmpty();
  324. // rules 8 and 9
  325. HashMap<String, String> mergeMap;
  326. mergeMap = new HashMap<String, String>();
  327. mergeMap.put("foo", "merge");
  328. setupCase(null, mergeMap, idxMap);
  329. go();
  330. assertTrue(getUpdated().isEmpty());
  331. assertTrue(getRemoved().isEmpty());
  332. assertTrue(getConflicts().contains("foo"));
  333. // rule 10
  334. HashMap<String, String> headMap = new HashMap<String, String>();
  335. headMap.put("foo", "foo");
  336. setupCase(headMap, null, idxMap);
  337. go();
  338. assertTrue(getRemoved().contains("foo"));
  339. assertTrue(getUpdated().isEmpty());
  340. assertTrue(getConflicts().isEmpty());
  341. // rule 11
  342. setupCase(headMap, null, idxMap);
  343. assertTrue(new File(trash, "foo").delete());
  344. writeTrashFile("foo", "bar");
  345. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  346. go();
  347. assertTrue(getRemoved().isEmpty());
  348. assertTrue(getUpdated().isEmpty());
  349. assertTrue(getConflicts().contains("foo"));
  350. // rule 12 & 13
  351. headMap.put("foo", "head");
  352. setupCase(headMap, null, idxMap);
  353. go();
  354. assertTrue(getRemoved().isEmpty());
  355. assertTrue(getUpdated().isEmpty());
  356. assertTrue(getConflicts().contains("foo"));
  357. // rules 14 & 15
  358. setupCase(headMap, headMap, idxMap);
  359. go();
  360. assertAllEmpty();
  361. // rules 16 & 17
  362. setupCase(headMap, mergeMap, idxMap); go();
  363. assertTrue(getConflicts().contains("foo"));
  364. // rules 18 & 19
  365. setupCase(headMap, idxMap, idxMap); go();
  366. assertAllEmpty();
  367. // rule 20
  368. setupCase(idxMap, mergeMap, idxMap); go();
  369. assertTrue(getUpdated().containsKey("foo"));
  370. // rules 21
  371. setupCase(idxMap, mergeMap, idxMap);
  372. assertTrue(new File(trash, "foo").delete());
  373. writeTrashFile("foo", "bar");
  374. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  375. go();
  376. assertTrue(getConflicts().contains("foo"));
  377. }
  378. private void assertAllEmpty() {
  379. assertTrue(getRemoved().isEmpty());
  380. assertTrue(getUpdated().isEmpty());
  381. assertTrue(getConflicts().isEmpty());
  382. }
  383. /*-
  384. * Directory/File Conflict cases:
  385. * It's entirely possible that in practice a number of these may be equivalent
  386. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  387. * that's all I care about. These are basically reverse-engineered from
  388. * what git currently does. If there are tests for these in git, it's kind of
  389. * hard to track them all down...
  390. *
  391. * H I M Clean H==M H==I I==M Result
  392. * ------------------------------------------------------------------
  393. *1 D D F Y N Y N Update
  394. *2 D D F N N Y N Conflict
  395. *3 D F D Y N N Keep
  396. *4 D F D N N N Conflict
  397. *5 D F F Y N N Y Keep
  398. *6 D F F N N N Y Keep
  399. *7 F D F Y Y N N Update
  400. *8 F D F N Y N N Conflict
  401. *9 F D F Y N N N Update
  402. *10 F D D N N Y Keep
  403. *11 F D D N N N Conflict
  404. *12 F F D Y N Y N Update
  405. *13 F F D N N Y N Conflict
  406. *14 F F D N N N Conflict
  407. *15 0 F D N N N Conflict
  408. *16 0 D F Y N N N Update
  409. *17 0 D F N N N Conflict
  410. *18 F 0 D Update
  411. *19 D 0 F Update
  412. */
  413. @Test
  414. public void testDirectoryFileSimple() throws IOException {
  415. ObjectId treeDF = buildTree(mkmap("DF", "DF"));
  416. ObjectId treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  417. buildIndex(mkmap("DF", "DF"));
  418. prescanTwoTrees(treeDF, treeDFDF);
  419. assertTrue(getRemoved().contains("DF"));
  420. assertTrue(getUpdated().containsKey("DF/DF"));
  421. recursiveDelete(new File(trash, "DF"));
  422. buildIndex(mkmap("DF/DF", "DF/DF"));
  423. prescanTwoTrees(treeDFDF, treeDF);
  424. assertTrue(getRemoved().contains("DF/DF"));
  425. assertTrue(getUpdated().containsKey("DF"));
  426. }
  427. @Test
  428. public void testDirectoryFileConflicts_1() throws Exception {
  429. // 1
  430. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  431. assertNoConflicts();
  432. assertUpdated("DF");
  433. assertRemoved("DF/DF");
  434. }
  435. @Test
  436. public void testDirectoryFileConflicts_2() throws Exception {
  437. // 2
  438. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  439. writeTrashFile("DF/DF", "different");
  440. go();
  441. assertConflict("DF/DF");
  442. }
  443. @Test
  444. public void testDirectoryFileConflicts_3() throws Exception {
  445. // 3
  446. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  447. assertNoConflicts();
  448. }
  449. @Test
  450. public void testDirectoryFileConflicts_4() throws Exception {
  451. // 4 (basically same as 3, just with H and M different)
  452. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  453. assertConflict("DF/DF");
  454. }
  455. @Test
  456. public void testDirectoryFileConflicts_5() throws Exception {
  457. // 5
  458. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  459. assertRemoved("DF/DF");
  460. }
  461. @Test
  462. public void testDirectoryFileConflicts_6() throws Exception {
  463. // 6
  464. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  465. writeTrashFile("DF", "different");
  466. go();
  467. assertRemoved("DF/DF");
  468. }
  469. @Test
  470. public void testDirectoryFileConflicts_7() throws Exception {
  471. // 7
  472. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  473. assertUpdated("DF");
  474. assertRemoved("DF/DF");
  475. cleanUpDF();
  476. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  477. go();
  478. assertRemoved("DF/DF/DF/DF/DF");
  479. assertUpdated("DF/DF");
  480. cleanUpDF();
  481. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  482. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  483. go();
  484. assertConflict("DF/DF/DF/DF/DF");
  485. // assertUpdated("DF/DF");
  486. // Why do we expect an update on DF/DF. H==M,
  487. // H&M are files and index contains a dir, index
  488. // is dirty: that case is not in the table but
  489. // we cannot update DF/DF to a file, this would
  490. // require that we delete DF/DF/DF/DF/DF in workdir
  491. // throwing away unsaved contents.
  492. // This test would fail in DirCacheCheckoutTests.
  493. }
  494. @Test
  495. public void testDirectoryFileConflicts_8() throws Exception {
  496. // 8
  497. setupCase(mk("DF"), mk("DF"), mk("DF/DF"));
  498. recursiveDelete(new File(db.getWorkTree(), "DF"));
  499. writeTrashFile("DF", "xy");
  500. go();
  501. assertConflict("DF/DF");
  502. }
  503. @Test
  504. public void testDirectoryFileConflicts_9() throws Exception {
  505. // 9
  506. doit(mk("DF"), mkmap("DF", "QP"), mk("DF/DF"));
  507. assertRemoved("DF/DF");
  508. assertUpdated("DF");
  509. }
  510. @Test
  511. public void testDirectoryFileConflicts_10() throws Exception {
  512. // 10
  513. cleanUpDF();
  514. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  515. assertNoConflicts();
  516. }
  517. @Test
  518. public void testDirectoryFileConflicts_11() throws Exception {
  519. // 11
  520. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  521. assertConflict("DF/DF");
  522. }
  523. @Test
  524. public void testDirectoryFileConflicts_12() throws Exception {
  525. // 12
  526. cleanUpDF();
  527. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  528. assertRemoved("DF");
  529. assertUpdated("DF/DF");
  530. }
  531. @Test
  532. public void testDirectoryFileConflicts_13() throws Exception {
  533. // 13
  534. cleanUpDF();
  535. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  536. writeTrashFile("DF", "asdfsdf");
  537. go();
  538. assertConflict("DF");
  539. assertUpdated("DF/DF");
  540. }
  541. @Test
  542. public void testDirectoryFileConflicts_14() throws Exception {
  543. // 14
  544. cleanUpDF();
  545. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  546. assertConflict("DF");
  547. assertUpdated("DF/DF");
  548. }
  549. @Test
  550. public void testDirectoryFileConflicts_15() throws Exception {
  551. // 15
  552. doit(mkmap(), mk("DF/DF"), mk("DF"));
  553. // This test would fail in DirCacheCheckoutTests. I think this test is wrong,
  554. // it should check for conflicts according to rule 15
  555. // assertRemoved("DF");
  556. assertUpdated("DF/DF");
  557. }
  558. @Test
  559. public void testDirectoryFileConflicts_15b() throws Exception {
  560. // 15, take 2, just to check multi-leveled
  561. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  562. // I think this test is wrong, it should
  563. // check for conflicts according to rule 15
  564. // This test would fail in DirCacheCheckouts
  565. // assertRemoved("DF");
  566. assertUpdated("DF/DF/DF/DF");
  567. }
  568. @Test
  569. public void testDirectoryFileConflicts_16() throws Exception {
  570. // 16
  571. cleanUpDF();
  572. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  573. assertRemoved("DF/DF/DF");
  574. assertUpdated("DF");
  575. }
  576. @Test
  577. public void testDirectoryFileConflicts_17() throws Exception {
  578. // 17
  579. cleanUpDF();
  580. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  581. writeTrashFile("DF/DF/DF", "asdf");
  582. go();
  583. assertConflict("DF/DF/DF");
  584. // Why do we expect an update on DF. If we really update
  585. // DF and update also the working tree we would have to
  586. // overwrite a dirty file in the work-tree DF/DF/DF
  587. // This test would fail in DirCacheCheckout
  588. // assertUpdated("DF");
  589. }
  590. @Test
  591. public void testDirectoryFileConflicts_18() throws Exception {
  592. // 18
  593. cleanUpDF();
  594. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  595. assertRemoved("DF/DF");
  596. assertUpdated("DF/DF/DF/DF");
  597. }
  598. @Test
  599. public void testDirectoryFileConflicts_19() throws Exception {
  600. // 19
  601. cleanUpDF();
  602. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  603. assertRemoved("DF/DF/DF/DF");
  604. assertUpdated("DF/DF/DF");
  605. }
  606. protected void cleanUpDF() throws Exception {
  607. tearDown();
  608. setUp();
  609. recursiveDelete(new File(trash, "DF"));
  610. }
  611. protected void assertConflict(String s) {
  612. assertTrue(getConflicts().contains(s));
  613. }
  614. protected void assertUpdated(String s) {
  615. assertTrue(getUpdated().containsKey(s));
  616. }
  617. protected void assertRemoved(String s) {
  618. assertTrue(getRemoved().contains(s));
  619. }
  620. protected void assertNoConflicts() {
  621. assertTrue(getConflicts().isEmpty());
  622. }
  623. protected void doit(HashMap<String, String> h, HashMap<String, String> m, HashMap<String, String> i)
  624. throws IOException {
  625. setupCase(h, m, i);
  626. go();
  627. }
  628. @Test
  629. public void testUntrackedConflicts() throws IOException {
  630. setupCase(null, mk("foo"), null);
  631. writeTrashFile("foo", "foo");
  632. go();
  633. // test that we don't overwrite untracked files when there is a HEAD
  634. recursiveDelete(new File(trash, "foo"));
  635. setupCase(mk("other"), mkmap("other", "other", "foo", "foo"),
  636. mk("other"));
  637. writeTrashFile("foo", "bar");
  638. try {
  639. checkout();
  640. fail("didn't get the expected exception");
  641. } catch (CheckoutConflictException e) {
  642. assertConflict("foo");
  643. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  644. assertIndex(mk("other"));
  645. }
  646. // test that we don't overwrite untracked files when there is no HEAD
  647. recursiveDelete(new File(trash, "other"));
  648. recursiveDelete(new File(trash, "foo"));
  649. setupCase(null, mk("foo"), null);
  650. writeTrashFile("foo", "bar");
  651. try {
  652. checkout();
  653. fail("didn't get the expected exception");
  654. } catch (CheckoutConflictException e) {
  655. assertConflict("foo");
  656. assertWorkDir(mkmap("foo", "bar"));
  657. assertIndex(mkmap("other", "other"));
  658. }
  659. // TODO: Why should we expect conflicts here?
  660. // H and M are empty and according to rule #5 of
  661. // the carry-over rules a dirty index is no reason
  662. // for a conflict. (I also feel it should be a
  663. // conflict because we are going to overwrite
  664. // unsaved content in the working tree
  665. // This test would fail in DirCacheCheckoutTest
  666. // assertConflict("foo");
  667. recursiveDelete(new File(trash, "foo"));
  668. recursiveDelete(new File(trash, "other"));
  669. setupCase(null, mk("foo"), null);
  670. writeTrashFile("foo/bar/baz", "");
  671. writeTrashFile("foo/blahblah", "");
  672. go();
  673. assertConflict("foo");
  674. assertConflict("foo/bar/baz");
  675. assertConflict("foo/blahblah");
  676. recursiveDelete(new File(trash, "foo"));
  677. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  678. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  679. assertTrue(new File(trash, "foo/bar").exists());
  680. go();
  681. assertNoConflicts();
  682. }
  683. @Test
  684. public void testCloseNameConflictsX0() throws IOException {
  685. 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") );
  686. checkout();
  687. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  688. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  689. go();
  690. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  691. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  692. assertNoConflicts();
  693. }
  694. @Test
  695. public void testCloseNameConflicts1() throws IOException {
  696. 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") );
  697. checkout();
  698. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  699. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  700. go();
  701. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  702. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  703. assertNoConflicts();
  704. }
  705. @Test
  706. public void testCheckoutHierarchy() throws IOException {
  707. setupCase(
  708. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  709. "e/g"),
  710. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  711. "e/g2"),
  712. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  713. "e/g3"));
  714. try {
  715. checkout();
  716. } catch (CheckoutConflictException e) {
  717. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  718. "e/f", "e/g", "e/g3"));
  719. assertConflict("e/g");
  720. }
  721. }
  722. @Test
  723. public void testCheckoutOutChanges() throws IOException {
  724. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  725. checkout();
  726. assertIndex(mk("foo/bar"));
  727. assertWorkDir(mk("foo/bar"));
  728. assertFalse(new File(trash, "foo").isFile());
  729. assertTrue(new File(trash, "foo/bar").isFile());
  730. recursiveDelete(new File(trash, "foo"));
  731. assertWorkDir(mkmap());
  732. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  733. checkout();
  734. assertIndex(mk("foo"));
  735. assertWorkDir(mk("foo"));
  736. assertFalse(new File(trash, "foo/bar").isFile());
  737. assertTrue(new File(trash, "foo").isFile());
  738. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  739. assertIndex(mkmap("foo", "bar"));
  740. assertWorkDir(mkmap("foo", "bar"));
  741. try {
  742. checkout();
  743. fail("did not throw exception");
  744. } catch (CheckoutConflictException e) {
  745. assertIndex(mkmap("foo", "bar"));
  746. assertWorkDir(mkmap("foo", "bar"));
  747. }
  748. }
  749. @Test
  750. public void testCheckoutOutChangesAutoCRLFfalse() throws IOException {
  751. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  752. checkout();
  753. assertIndex(mkmap("foo/bar", "foo\nbar"));
  754. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  755. }
  756. @Test
  757. public void testCheckoutOutChangesAutoCRLFInput() throws IOException {
  758. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  759. db.getConfig().setString("core", null, "autocrlf", "input");
  760. checkout();
  761. assertIndex(mkmap("foo/bar", "foo\nbar"));
  762. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  763. }
  764. @Test
  765. public void testCheckoutOutChangesAutoCRLFtrue() throws IOException {
  766. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  767. db.getConfig().setString("core", null, "autocrlf", "true");
  768. checkout();
  769. assertIndex(mkmap("foo/bar", "foo\nbar"));
  770. assertWorkDir(mkmap("foo/bar", "foo\r\nbar"));
  771. }
  772. @Test
  773. public void testCheckoutOutChangesAutoCRLFtrueBinary() throws IOException {
  774. setupCase(mk("foo"), mkmap("foo/bar", "foo\nb\u0000ar"), mk("foo"));
  775. db.getConfig().setString("core", null, "autocrlf", "true");
  776. checkout();
  777. assertIndex(mkmap("foo/bar", "foo\nb\u0000ar"));
  778. assertWorkDir(mkmap("foo/bar", "foo\nb\u0000ar"));
  779. }
  780. @Test
  781. public void testCheckoutUncachedChanges() throws IOException {
  782. setupCase(mk("foo"), mk("foo"), mk("foo"));
  783. writeTrashFile("foo", "otherData");
  784. checkout();
  785. assertIndex(mk("foo"));
  786. assertWorkDir(mkmap("foo", "otherData"));
  787. assertTrue(new File(trash, "foo").isFile());
  788. }
  789. @Test
  790. public void testDontOverwriteDirtyFile() throws IOException {
  791. setupCase(mk("foo"), mk("other"), mk("foo"));
  792. writeTrashFile("foo", "different");
  793. try {
  794. checkout();
  795. fail("Didn't got the expected conflict");
  796. } catch (CheckoutConflictException e) {
  797. assertIndex(mk("foo"));
  798. assertWorkDir(mkmap("foo", "different"));
  799. assertEquals(Arrays.asList("foo"), getConflicts());
  800. assertTrue(new File(trash, "foo").isFile());
  801. }
  802. }
  803. @Test
  804. public void testFileModeChangeWithNoContentChangeUpdate() throws Exception {
  805. if (!FS.DETECTED.supportsExecute())
  806. return;
  807. Git git = Git.wrap(db);
  808. // Add non-executable file
  809. File file = writeTrashFile("file.txt", "a");
  810. git.add().addFilepattern("file.txt").call();
  811. git.commit().setMessage("commit1").call();
  812. assertFalse(db.getFS().canExecute(file));
  813. // Create branch
  814. git.branchCreate().setName("b1").call();
  815. // Make file executable
  816. db.getFS().setExecute(file, true);
  817. git.add().addFilepattern("file.txt").call();
  818. git.commit().setMessage("commit2").call();
  819. // Verify executable and working directory is clean
  820. Status status = git.status().call();
  821. assertTrue(status.getModified().isEmpty());
  822. assertTrue(status.getChanged().isEmpty());
  823. assertTrue(db.getFS().canExecute(file));
  824. // Switch branches
  825. git.checkout().setName("b1").call();
  826. // Verify not executable and working directory is clean
  827. status = git.status().call();
  828. assertTrue(status.getModified().isEmpty());
  829. assertTrue(status.getChanged().isEmpty());
  830. assertFalse(db.getFS().canExecute(file));
  831. }
  832. @Test
  833. public void testFileModeChangeAndContentChangeConflict() throws Exception {
  834. if (!FS.DETECTED.supportsExecute())
  835. return;
  836. Git git = Git.wrap(db);
  837. // Add non-executable file
  838. File file = writeTrashFile("file.txt", "a");
  839. git.add().addFilepattern("file.txt").call();
  840. git.commit().setMessage("commit1").call();
  841. assertFalse(db.getFS().canExecute(file));
  842. // Create branch
  843. git.branchCreate().setName("b1").call();
  844. // Make file executable
  845. db.getFS().setExecute(file, true);
  846. git.add().addFilepattern("file.txt").call();
  847. git.commit().setMessage("commit2").call();
  848. // Verify executable and working directory is clean
  849. Status status = git.status().call();
  850. assertTrue(status.getModified().isEmpty());
  851. assertTrue(status.getChanged().isEmpty());
  852. assertTrue(db.getFS().canExecute(file));
  853. writeTrashFile("file.txt", "b");
  854. // Switch branches
  855. CheckoutCommand checkout = git.checkout().setName("b1");
  856. try {
  857. checkout.call();
  858. fail("Checkout exception not thrown");
  859. } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) {
  860. CheckoutResult result = checkout.getResult();
  861. assertNotNull(result);
  862. assertNotNull(result.getConflictList());
  863. assertEquals(1, result.getConflictList().size());
  864. assertTrue(result.getConflictList().contains("file.txt"));
  865. }
  866. }
  867. @Test
  868. public void testDirtyFileModeEqualHeadMerge()
  869. throws Exception {
  870. if (!FS.DETECTED.supportsExecute())
  871. return;
  872. Git git = Git.wrap(db);
  873. // Add non-executable file
  874. File file = writeTrashFile("file.txt", "a");
  875. git.add().addFilepattern("file.txt").call();
  876. git.commit().setMessage("commit1").call();
  877. assertFalse(db.getFS().canExecute(file));
  878. // Create branch
  879. git.branchCreate().setName("b1").call();
  880. // Create second commit and don't touch file
  881. writeTrashFile("file2.txt", "");
  882. git.add().addFilepattern("file2.txt").call();
  883. git.commit().setMessage("commit2").call();
  884. // stage a mode change
  885. writeTrashFile("file.txt", "a");
  886. db.getFS().setExecute(file, true);
  887. git.add().addFilepattern("file.txt").call();
  888. // dirty the file
  889. writeTrashFile("file.txt", "b");
  890. assertEquals(
  891. "[file.txt, mode:100755, content:a][file2.txt, mode:100644, content:]",
  892. indexState(CONTENT));
  893. assertWorkDir(mkmap("file.txt", "b", "file2.txt", ""));
  894. // Switch branches and check that the dirty file survived in worktree
  895. // and index
  896. git.checkout().setName("b1").call();
  897. assertEquals("[file.txt, mode:100755, content:a]", indexState(CONTENT));
  898. assertWorkDir(mkmap("file.txt", "b"));
  899. }
  900. @Test
  901. public void testDirtyFileModeEqualIndexMerge()
  902. throws Exception {
  903. if (!FS.DETECTED.supportsExecute())
  904. return;
  905. Git git = Git.wrap(db);
  906. // Add non-executable file
  907. File file = writeTrashFile("file.txt", "a");
  908. git.add().addFilepattern("file.txt").call();
  909. git.commit().setMessage("commit1").call();
  910. assertFalse(db.getFS().canExecute(file));
  911. // Create branch
  912. git.branchCreate().setName("b1").call();
  913. // Create second commit with executable file
  914. file = writeTrashFile("file.txt", "b");
  915. db.getFS().setExecute(file, true);
  916. git.add().addFilepattern("file.txt").call();
  917. git.commit().setMessage("commit2").call();
  918. // stage the same content as in the branch we want to switch to
  919. writeTrashFile("file.txt", "a");
  920. db.getFS().setExecute(file, false);
  921. git.add().addFilepattern("file.txt").call();
  922. // dirty the file
  923. writeTrashFile("file.txt", "c");
  924. db.getFS().setExecute(file, true);
  925. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  926. assertWorkDir(mkmap("file.txt", "c"));
  927. // Switch branches and check that the dirty file survived in worktree
  928. // and index
  929. git.checkout().setName("b1").call();
  930. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  931. assertWorkDir(mkmap("file.txt", "c"));
  932. }
  933. @Test
  934. public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
  935. if (!FS.DETECTED.supportsExecute())
  936. return;
  937. Git git = Git.wrap(db);
  938. // Add first file
  939. File file1 = writeTrashFile("file1.txt", "a");
  940. git.add().addFilepattern("file1.txt").call();
  941. git.commit().setMessage("commit1").call();
  942. assertFalse(db.getFS().canExecute(file1));
  943. // Add second file
  944. File file2 = writeTrashFile("file2.txt", "b");
  945. git.add().addFilepattern("file2.txt").call();
  946. git.commit().setMessage("commit2").call();
  947. assertFalse(db.getFS().canExecute(file2));
  948. // Create branch from first commit
  949. assertNotNull(git.checkout().setCreateBranch(true).setName("b1")
  950. .setStartPoint(Constants.HEAD + "~1").call());
  951. // Change content and file mode in working directory and index
  952. file1 = writeTrashFile("file1.txt", "c");
  953. db.getFS().setExecute(file1, true);
  954. git.add().addFilepattern("file1.txt").call();
  955. // Switch back to 'master'
  956. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  957. }
  958. public void assertWorkDir(HashMap<String, String> i) throws CorruptObjectException,
  959. IOException {
  960. TreeWalk walk = new TreeWalk(db);
  961. walk.setRecursive(true);
  962. walk.addTree(new FileTreeIterator(db));
  963. String expectedValue;
  964. String path;
  965. int nrFiles = 0;
  966. FileTreeIterator ft;
  967. while (walk.next()) {
  968. ft = walk.getTree(0, FileTreeIterator.class);
  969. path = ft.getEntryPathString();
  970. expectedValue = i.get(path);
  971. assertNotNull("found unexpected file for path " + path
  972. + " in workdir", expectedValue);
  973. File file = new File(db.getWorkTree(), path);
  974. assertTrue(file.exists());
  975. if (file.isFile()) {
  976. FileInputStream is = new FileInputStream(file);
  977. byte[] buffer = new byte[(int) file.length()];
  978. int offset = 0;
  979. int numRead = 0;
  980. while (offset < buffer.length
  981. && (numRead = is.read(buffer, offset, buffer.length
  982. - offset)) >= 0) {
  983. offset += numRead;
  984. }
  985. is.close();
  986. assertArrayEquals("unexpected content for path " + path
  987. + " in workDir. ", buffer, i.get(path).getBytes());
  988. nrFiles++;
  989. }
  990. }
  991. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  992. }
  993. }