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

DirCacheCheckoutTest.java 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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<>();
  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<>(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<>();
  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<>();
  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<>();
  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<>();
  353. headMap.put("foo", "foo");
  354. setupCase(headMap, null, idxMap);
  355. go();
  356. assertTrue(getRemoved().contains("foo"));
  357. assertTrue(getUpdated().isEmpty());
  358. assertTrue(getConflicts().isEmpty());
  359. // rule 11
  360. setupCase(headMap, null, idxMap);
  361. assertTrue(new File(trash, "foo").delete());
  362. writeTrashFile("foo", "bar");
  363. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  364. go();
  365. assertTrue(getRemoved().isEmpty());
  366. assertTrue(getUpdated().isEmpty());
  367. assertTrue(getConflicts().contains("foo"));
  368. // rule 12 & 13
  369. headMap.put("foo", "head");
  370. setupCase(headMap, null, idxMap);
  371. go();
  372. assertTrue(getRemoved().isEmpty());
  373. assertTrue(getUpdated().isEmpty());
  374. assertTrue(getConflicts().contains("foo"));
  375. // rules 14 & 15
  376. setupCase(headMap, headMap, idxMap);
  377. go();
  378. assertAllEmpty();
  379. // rules 16 & 17
  380. setupCase(headMap, mergeMap, idxMap); go();
  381. assertTrue(getConflicts().contains("foo"));
  382. // rules 18 & 19
  383. setupCase(headMap, idxMap, idxMap); go();
  384. assertAllEmpty();
  385. // rule 20
  386. setupCase(idxMap, mergeMap, idxMap); go();
  387. assertTrue(getUpdated().containsKey("foo"));
  388. // rules 21
  389. setupCase(idxMap, mergeMap, idxMap);
  390. assertTrue(new File(trash, "foo").delete());
  391. writeTrashFile("foo", "bar");
  392. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  393. go();
  394. assertTrue(getConflicts().contains("foo"));
  395. }
  396. private void assertAllEmpty() {
  397. assertTrue(getRemoved().isEmpty());
  398. assertTrue(getUpdated().isEmpty());
  399. assertTrue(getConflicts().isEmpty());
  400. }
  401. /*-
  402. * Directory/File Conflict cases:
  403. * It's entirely possible that in practice a number of these may be equivalent
  404. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  405. * that's all I care about. These are basically reverse-engineered from
  406. * what git currently does. If there are tests for these in git, it's kind of
  407. * hard to track them all down...
  408. *
  409. * H I M Clean H==M H==I I==M Result
  410. * ------------------------------------------------------------------
  411. *1 D D F Y N Y N Update
  412. *2 D D F N N Y N Conflict
  413. *3 D F D Y N N Keep
  414. *4 D F D N N N Conflict
  415. *5 D F F Y N N Y Keep
  416. *5b D F F Y N N N Conflict
  417. *6 D F F N N N Y Keep
  418. *6b D F F N N N N Conflict
  419. *7 F D F Y Y N N Update
  420. *8 F D F N Y N N Conflict
  421. *9 F D F Y N N N Update
  422. *10 F D D N N Y Keep
  423. *11 F D D N N N Conflict
  424. *12 F F D Y N Y N Update
  425. *13 F F D N N Y N Conflict
  426. *14 F F D N N N Conflict
  427. *15 0 F D N N N Conflict
  428. *16 0 D F Y N N N Update
  429. *17 0 D F N N N Conflict
  430. *18 F 0 D Update
  431. *19 D 0 F Update
  432. */
  433. @Test
  434. public void testDirectoryFileSimple() throws IOException {
  435. ObjectId treeDF = buildTree(mkmap("DF", "DF"));
  436. ObjectId treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  437. buildIndex(mkmap("DF", "DF"));
  438. prescanTwoTrees(treeDF, treeDFDF);
  439. assertTrue(getRemoved().contains("DF"));
  440. assertTrue(getUpdated().containsKey("DF/DF"));
  441. recursiveDelete(new File(trash, "DF"));
  442. buildIndex(mkmap("DF/DF", "DF/DF"));
  443. prescanTwoTrees(treeDFDF, treeDF);
  444. assertTrue(getRemoved().contains("DF/DF"));
  445. assertTrue(getUpdated().containsKey("DF"));
  446. }
  447. @Test
  448. public void testDirectoryFileConflicts_1() throws Exception {
  449. // 1
  450. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  451. assertNoConflicts();
  452. assertUpdated("DF");
  453. assertRemoved("DF/DF");
  454. }
  455. @Test
  456. public void testDirectoryFileConflicts_2() throws Exception {
  457. // 2
  458. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  459. writeTrashFile("DF/DF", "different");
  460. go();
  461. assertConflict("DF/DF");
  462. }
  463. @Test
  464. public void testDirectoryFileConflicts_3() throws Exception {
  465. // 3
  466. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  467. assertNoConflicts();
  468. }
  469. @Test
  470. public void testDirectoryFileConflicts_4() throws Exception {
  471. // 4 (basically same as 3, just with H and M different)
  472. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  473. assertConflict("DF/DF");
  474. }
  475. @Test
  476. public void testDirectoryFileConflicts_5() throws Exception {
  477. // 5
  478. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  479. assertRemoved("DF/DF");
  480. assertEquals(0, dco.getConflicts().size());
  481. assertEquals(0, dco.getUpdated().size());
  482. }
  483. @Test
  484. public void testDirectoryFileConflicts_5b() throws Exception {
  485. // 5
  486. doit(mk("DF/DF"), mkmap("DF", "different"), mk("DF"));
  487. assertRemoved("DF/DF");
  488. assertConflict("DF");
  489. assertEquals(0, dco.getUpdated().size());
  490. }
  491. @Test
  492. public void testDirectoryFileConflicts_6() throws Exception {
  493. // 6
  494. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  495. writeTrashFile("DF", "different");
  496. go();
  497. assertRemoved("DF/DF");
  498. assertEquals(0, dco.getConflicts().size());
  499. assertEquals(0, dco.getUpdated().size());
  500. }
  501. @Test
  502. public void testDirectoryFileConflicts_6b() throws Exception {
  503. // 6
  504. setupCase(mk("DF/DF"), mk("DF"), mkmap("DF", "different"));
  505. writeTrashFile("DF", "again different");
  506. go();
  507. assertRemoved("DF/DF");
  508. assertConflict("DF");
  509. assertEquals(0, dco.getUpdated().size());
  510. }
  511. @Test
  512. public void testDirectoryFileConflicts_7() throws Exception {
  513. // 7
  514. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  515. assertUpdated("DF");
  516. assertRemoved("DF/DF");
  517. cleanUpDF();
  518. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  519. go();
  520. assertRemoved("DF/DF/DF/DF/DF");
  521. assertUpdated("DF/DF");
  522. cleanUpDF();
  523. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  524. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  525. go();
  526. assertConflict("DF/DF/DF/DF/DF");
  527. // assertUpdated("DF/DF");
  528. // Why do we expect an update on DF/DF. H==M,
  529. // H&M are files and index contains a dir, index
  530. // is dirty: that case is not in the table but
  531. // we cannot update DF/DF to a file, this would
  532. // require that we delete DF/DF/DF/DF/DF in workdir
  533. // throwing away unsaved contents.
  534. // This test would fail in DirCacheCheckoutTests.
  535. }
  536. @Test
  537. public void testDirectoryFileConflicts_8() throws Exception {
  538. // 8
  539. setupCase(mk("DF"), mk("DF"), mk("DF/DF"));
  540. recursiveDelete(new File(db.getWorkTree(), "DF"));
  541. writeTrashFile("DF", "xy");
  542. go();
  543. assertConflict("DF/DF");
  544. }
  545. @Test
  546. public void testDirectoryFileConflicts_9() throws Exception {
  547. // 9
  548. doit(mkmap("DF", "QP"), mkmap("DF", "QP"), mkmap("DF/DF", "DF/DF"));
  549. assertRemoved("DF/DF");
  550. assertUpdated("DF");
  551. }
  552. @Test
  553. public void testDirectoryFileConflicts_10() throws Exception {
  554. // 10
  555. cleanUpDF();
  556. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  557. assertNoConflicts();
  558. }
  559. @Test
  560. public void testDirectoryFileConflicts_11() throws Exception {
  561. // 11
  562. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  563. assertConflict("DF/DF");
  564. }
  565. @Test
  566. public void testDirectoryFileConflicts_12() throws Exception {
  567. // 12
  568. cleanUpDF();
  569. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  570. assertRemoved("DF");
  571. assertUpdated("DF/DF");
  572. }
  573. @Test
  574. public void testDirectoryFileConflicts_13() throws Exception {
  575. // 13
  576. cleanUpDF();
  577. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  578. writeTrashFile("DF", "asdfsdf");
  579. go();
  580. assertConflict("DF");
  581. assertUpdated("DF/DF");
  582. }
  583. @Test
  584. public void testDirectoryFileConflicts_14() throws Exception {
  585. // 14
  586. cleanUpDF();
  587. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  588. assertConflict("DF");
  589. assertUpdated("DF/DF");
  590. }
  591. @Test
  592. public void testDirectoryFileConflicts_15() throws Exception {
  593. // 15
  594. doit(mkmap(), mk("DF/DF"), mk("DF"));
  595. // This test would fail in DirCacheCheckoutTests. I think this test is wrong,
  596. // it should check for conflicts according to rule 15
  597. // assertRemoved("DF");
  598. assertUpdated("DF/DF");
  599. }
  600. @Test
  601. public void testDirectoryFileConflicts_15b() throws Exception {
  602. // 15, take 2, just to check multi-leveled
  603. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  604. // I think this test is wrong, it should
  605. // check for conflicts according to rule 15
  606. // This test would fail in DirCacheCheckouts
  607. // assertRemoved("DF");
  608. assertUpdated("DF/DF/DF/DF");
  609. }
  610. @Test
  611. public void testDirectoryFileConflicts_16() throws Exception {
  612. // 16
  613. cleanUpDF();
  614. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  615. assertRemoved("DF/DF/DF");
  616. assertUpdated("DF");
  617. }
  618. @Test
  619. public void testDirectoryFileConflicts_17() throws Exception {
  620. // 17
  621. cleanUpDF();
  622. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  623. writeTrashFile("DF/DF/DF", "asdf");
  624. go();
  625. assertConflict("DF/DF/DF");
  626. // Why do we expect an update on DF. If we really update
  627. // DF and update also the working tree we would have to
  628. // overwrite a dirty file in the work-tree DF/DF/DF
  629. // This test would fail in DirCacheCheckout
  630. // assertUpdated("DF");
  631. }
  632. @Test
  633. public void testDirectoryFileConflicts_18() throws Exception {
  634. // 18
  635. cleanUpDF();
  636. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  637. assertRemoved("DF/DF");
  638. assertUpdated("DF/DF/DF/DF");
  639. }
  640. @Test
  641. public void testDirectoryFileConflicts_19() throws Exception {
  642. // 19
  643. cleanUpDF();
  644. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  645. assertRemoved("DF/DF/DF/DF");
  646. assertUpdated("DF/DF/DF");
  647. }
  648. protected void cleanUpDF() throws Exception {
  649. tearDown();
  650. setUp();
  651. recursiveDelete(new File(trash, "DF"));
  652. }
  653. protected void assertConflict(String s) {
  654. assertTrue(getConflicts().contains(s));
  655. }
  656. protected void assertUpdated(String s) {
  657. assertTrue(getUpdated().containsKey(s));
  658. }
  659. protected void assertRemoved(String s) {
  660. assertTrue(getRemoved().contains(s));
  661. }
  662. protected void assertNoConflicts() {
  663. assertTrue(getConflicts().isEmpty());
  664. }
  665. protected void doit(HashMap<String, String> h, HashMap<String, String> m, HashMap<String, String> i)
  666. throws IOException {
  667. setupCase(h, m, i);
  668. go();
  669. }
  670. @Test
  671. public void testUntrackedConflicts() throws IOException {
  672. setupCase(null, mk("foo"), null);
  673. writeTrashFile("foo", "foo");
  674. go();
  675. // test that we don't overwrite untracked files when there is a HEAD
  676. recursiveDelete(new File(trash, "foo"));
  677. setupCase(mk("other"), mkmap("other", "other", "foo", "foo"),
  678. mk("other"));
  679. writeTrashFile("foo", "bar");
  680. try {
  681. checkout();
  682. fail("didn't get the expected exception");
  683. } catch (CheckoutConflictException e) {
  684. assertConflict("foo");
  685. assertEquals("foo", e.getConflictingFiles()[0]);
  686. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  687. assertIndex(mk("other"));
  688. }
  689. // test that we don't overwrite untracked files when there is no HEAD
  690. recursiveDelete(new File(trash, "other"));
  691. recursiveDelete(new File(trash, "foo"));
  692. setupCase(null, mk("foo"), null);
  693. writeTrashFile("foo", "bar");
  694. try {
  695. checkout();
  696. fail("didn't get the expected exception");
  697. } catch (CheckoutConflictException e) {
  698. assertConflict("foo");
  699. assertWorkDir(mkmap("foo", "bar"));
  700. assertIndex(mkmap("other", "other"));
  701. }
  702. // TODO: Why should we expect conflicts here?
  703. // H and M are empty and according to rule #5 of
  704. // the carry-over rules a dirty index is no reason
  705. // for a conflict. (I also feel it should be a
  706. // conflict because we are going to overwrite
  707. // unsaved content in the working tree
  708. // This test would fail in DirCacheCheckoutTest
  709. // assertConflict("foo");
  710. recursiveDelete(new File(trash, "foo"));
  711. recursiveDelete(new File(trash, "other"));
  712. setupCase(null, mk("foo"), null);
  713. writeTrashFile("foo/bar/baz", "");
  714. writeTrashFile("foo/blahblah", "");
  715. go();
  716. assertConflict("foo");
  717. assertConflict("foo/bar/baz");
  718. assertConflict("foo/blahblah");
  719. recursiveDelete(new File(trash, "foo"));
  720. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  721. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  722. assertTrue(new File(trash, "foo/bar").exists());
  723. go();
  724. assertNoConflicts();
  725. }
  726. @Test
  727. public void testCloseNameConflictsX0() throws IOException {
  728. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "b.b/b.b","b.b/b.bs"), mkmap("a/a", "a/a-c") );
  729. checkout();
  730. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  731. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  732. go();
  733. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  734. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  735. assertNoConflicts();
  736. }
  737. @Test
  738. public void testCloseNameConflicts1() throws IOException {
  739. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "a.a/a.a","a.a/a.a"), mkmap("a/a", "a/a-c") );
  740. checkout();
  741. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  742. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  743. go();
  744. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  745. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  746. assertNoConflicts();
  747. }
  748. @Test
  749. public void testCheckoutHierarchy() throws IOException {
  750. setupCase(
  751. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  752. "e/g"),
  753. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  754. "e/g2"),
  755. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  756. "e/g3"));
  757. try {
  758. checkout();
  759. } catch (CheckoutConflictException e) {
  760. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  761. "e/f", "e/g", "e/g3"));
  762. assertConflict("e/g");
  763. assertEquals("e/g", e.getConflictingFiles()[0]);
  764. }
  765. }
  766. @Test
  767. public void testCheckoutOutChanges() throws IOException {
  768. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  769. checkout();
  770. assertIndex(mk("foo/bar"));
  771. assertWorkDir(mk("foo/bar"));
  772. assertFalse(new File(trash, "foo").isFile());
  773. assertTrue(new File(trash, "foo/bar").isFile());
  774. recursiveDelete(new File(trash, "foo"));
  775. assertWorkDir(mkmap());
  776. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  777. checkout();
  778. assertIndex(mk("foo"));
  779. assertWorkDir(mk("foo"));
  780. assertFalse(new File(trash, "foo/bar").isFile());
  781. assertTrue(new File(trash, "foo").isFile());
  782. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  783. assertIndex(mkmap("foo", "bar"));
  784. assertWorkDir(mkmap("foo", "bar"));
  785. try {
  786. checkout();
  787. fail("did not throw exception");
  788. } catch (CheckoutConflictException e) {
  789. assertIndex(mkmap("foo", "bar"));
  790. assertWorkDir(mkmap("foo", "bar"));
  791. }
  792. }
  793. @Test
  794. public void testCheckoutChangeLinkToEmptyDir() throws Exception {
  795. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  796. String fname = "was_file";
  797. Git git = Git.wrap(db);
  798. // Add a file
  799. writeTrashFile(fname, "a");
  800. git.add().addFilepattern(fname).call();
  801. // Add a link to file
  802. String linkName = "link";
  803. File link = writeLink(linkName, fname).toFile();
  804. git.add().addFilepattern(linkName).call();
  805. git.commit().setMessage("Added file and link").call();
  806. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  807. // replace link with empty directory
  808. FileUtils.delete(link);
  809. FileUtils.mkdir(link);
  810. assertTrue("Link must be a directory now", link.isDirectory());
  811. // modify file
  812. writeTrashFile(fname, "b");
  813. assertWorkDir(mkmap(fname, "b", linkName, "/"));
  814. // revert both paths to HEAD state
  815. git.checkout().setStartPoint(Constants.HEAD)
  816. .addPath(fname).addPath(linkName).call();
  817. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  818. Status st = git.status().call();
  819. assertTrue(st.isClean());
  820. }
  821. @Test
  822. public void testCheckoutChangeLinkToEmptyDirs() throws Exception {
  823. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  824. String fname = "was_file";
  825. Git git = Git.wrap(db);
  826. // Add a file
  827. writeTrashFile(fname, "a");
  828. git.add().addFilepattern(fname).call();
  829. // Add a link to file
  830. String linkName = "link";
  831. File link = writeLink(linkName, fname).toFile();
  832. git.add().addFilepattern(linkName).call();
  833. git.commit().setMessage("Added file and link").call();
  834. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  835. // replace link with directory containing only directories, no files
  836. FileUtils.delete(link);
  837. FileUtils.mkdirs(new File(link, "dummyDir"));
  838. assertTrue("Link must be a directory now", link.isDirectory());
  839. assertFalse("Must not delete non empty directory", link.delete());
  840. // modify file
  841. writeTrashFile(fname, "b");
  842. assertWorkDir(mkmap(fname, "b", linkName + "/dummyDir", "/"));
  843. // revert both paths to HEAD state
  844. git.checkout().setStartPoint(Constants.HEAD)
  845. .addPath(fname).addPath(linkName).call();
  846. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  847. Status st = git.status().call();
  848. assertTrue(st.isClean());
  849. }
  850. @Test
  851. public void testCheckoutChangeLinkToNonEmptyDirs() throws Exception {
  852. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  853. String fname = "file";
  854. Git git = Git.wrap(db);
  855. // Add a file
  856. writeTrashFile(fname, "a");
  857. git.add().addFilepattern(fname).call();
  858. // Add a link to file
  859. String linkName = "link";
  860. File link = writeLink(linkName, fname).toFile();
  861. git.add().addFilepattern(linkName).call();
  862. git.commit().setMessage("Added file and link").call();
  863. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  864. // replace link with directory containing only directories, no files
  865. FileUtils.delete(link);
  866. // create but do not add a file in the new directory to the index
  867. writeTrashFile(linkName + "/dir1", "file1", "c");
  868. // create but do not add a file in the new directory to the index
  869. writeTrashFile(linkName + "/dir2", "file2", "d");
  870. assertTrue("File must be a directory now", link.isDirectory());
  871. assertFalse("Must not delete non empty directory", link.delete());
  872. // 2 extra files are created
  873. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  874. linkName + "/dir2/file2", "d"));
  875. // revert path to HEAD state
  876. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  877. // expect only the one added to the index
  878. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  879. Status st = git.status().call();
  880. assertTrue(st.isClean());
  881. }
  882. @Test
  883. public void testCheckoutChangeLinkToNonEmptyDirsAndNewIndexEntry()
  884. throws Exception {
  885. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  886. String fname = "file";
  887. Git git = Git.wrap(db);
  888. // Add a file
  889. writeTrashFile(fname, "a");
  890. git.add().addFilepattern(fname).call();
  891. // Add a link to file
  892. String linkName = "link";
  893. File link = writeLink(linkName, fname).toFile();
  894. git.add().addFilepattern(linkName).call();
  895. git.commit().setMessage("Added file and link").call();
  896. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  897. // replace link with directory containing only directories, no files
  898. FileUtils.delete(link);
  899. // create and add a file in the new directory to the index
  900. writeTrashFile(linkName + "/dir1", "file1", "c");
  901. git.add().addFilepattern(linkName + "/dir1/file1").call();
  902. // create but do not add a file in the new directory to the index
  903. writeTrashFile(linkName + "/dir2", "file2", "d");
  904. assertTrue("File must be a directory now", link.isDirectory());
  905. assertFalse("Must not delete non empty directory", link.delete());
  906. // 2 extra files are created
  907. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  908. linkName + "/dir2/file2", "d"));
  909. // revert path to HEAD state
  910. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName).call();
  911. // original file and link
  912. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  913. Status st = git.status().call();
  914. assertTrue(st.isClean());
  915. }
  916. @Test
  917. public void testCheckoutChangeFileToEmptyDir() throws Exception {
  918. String fname = "was_file";
  919. Git git = Git.wrap(db);
  920. // Add a file
  921. File file = writeTrashFile(fname, "a");
  922. git.add().addFilepattern(fname).call();
  923. git.commit().setMessage("Added file").call();
  924. // replace file with empty directory
  925. FileUtils.delete(file);
  926. FileUtils.mkdir(file);
  927. assertTrue("File must be a directory now", file.isDirectory());
  928. assertWorkDir(mkmap(fname, "/"));
  929. // revert path to HEAD state
  930. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  931. assertWorkDir(mkmap(fname, "a"));
  932. Status st = git.status().call();
  933. assertTrue(st.isClean());
  934. }
  935. @Test
  936. public void testCheckoutChangeFileToEmptyDirs() throws Exception {
  937. String fname = "was_file";
  938. Git git = Git.wrap(db);
  939. // Add a file
  940. File file = writeTrashFile(fname, "a");
  941. git.add().addFilepattern(fname).call();
  942. git.commit().setMessage("Added file").call();
  943. // replace file with directory containing only directories, no files
  944. FileUtils.delete(file);
  945. FileUtils.mkdirs(new File(file, "dummyDir"));
  946. assertTrue("File must be a directory now", file.isDirectory());
  947. assertFalse("Must not delete non empty directory", file.delete());
  948. assertWorkDir(mkmap(fname + "/dummyDir", "/"));
  949. // revert path to HEAD state
  950. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  951. assertWorkDir(mkmap(fname, "a"));
  952. Status st = git.status().call();
  953. assertTrue(st.isClean());
  954. }
  955. @Test
  956. public void testCheckoutChangeFileToNonEmptyDirs() throws Exception {
  957. String fname = "was_file";
  958. Git git = Git.wrap(db);
  959. // Add a file
  960. File file = writeTrashFile(fname, "a");
  961. git.add().addFilepattern(fname).call();
  962. git.commit().setMessage("Added file").call();
  963. assertWorkDir(mkmap(fname, "a"));
  964. // replace file with directory containing only directories, no files
  965. FileUtils.delete(file);
  966. // create but do not add a file in the new directory to the index
  967. writeTrashFile(fname + "/dir1", "file1", "c");
  968. // create but do not add a file in the new directory to the index
  969. writeTrashFile(fname + "/dir2", "file2", "d");
  970. assertTrue("File must be a directory now", file.isDirectory());
  971. assertFalse("Must not delete non empty directory", file.delete());
  972. // 2 extra files are created
  973. assertWorkDir(
  974. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  975. // revert path to HEAD state
  976. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  977. // expect only the one added to the index
  978. assertWorkDir(mkmap(fname, "a"));
  979. Status st = git.status().call();
  980. assertTrue(st.isClean());
  981. }
  982. @Test
  983. public void testCheckoutChangeFileToNonEmptyDirsAndNewIndexEntry()
  984. throws Exception {
  985. String fname = "was_file";
  986. Git git = Git.wrap(db);
  987. // Add a file
  988. File file = writeTrashFile(fname, "a");
  989. git.add().addFilepattern(fname).call();
  990. git.commit().setMessage("Added file").call();
  991. assertWorkDir(mkmap(fname, "a"));
  992. // replace file with directory containing only directories, no files
  993. FileUtils.delete(file);
  994. // create and add a file in the new directory to the index
  995. writeTrashFile(fname + "/dir", "file1", "c");
  996. git.add().addFilepattern(fname + "/dir/file1").call();
  997. // create but do not add a file in the new directory to the index
  998. writeTrashFile(fname + "/dir", "file2", "d");
  999. assertTrue("File must be a directory now", file.isDirectory());
  1000. assertFalse("Must not delete non empty directory", file.delete());
  1001. // 2 extra files are created
  1002. assertWorkDir(
  1003. mkmap(fname + "/dir/file1", "c", fname + "/dir/file2", "d"));
  1004. // revert path to HEAD state
  1005. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1006. assertWorkDir(mkmap(fname, "a"));
  1007. Status st = git.status().call();
  1008. assertTrue(st.isClean());
  1009. }
  1010. @Test
  1011. public void testCheckoutOutChangesAutoCRLFfalse() throws IOException {
  1012. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1013. checkout();
  1014. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1015. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1016. }
  1017. @Test
  1018. public void testCheckoutOutChangesAutoCRLFInput() throws IOException {
  1019. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1020. db.getConfig().setString("core", null, "autocrlf", "input");
  1021. checkout();
  1022. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1023. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1024. }
  1025. @Test
  1026. public void testCheckoutOutChangesAutoCRLFtrue() throws IOException {
  1027. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1028. db.getConfig().setString("core", null, "autocrlf", "true");
  1029. checkout();
  1030. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1031. assertWorkDir(mkmap("foo/bar", "foo\r\nbar"));
  1032. }
  1033. @Test
  1034. public void testCheckoutOutChangesAutoCRLFtrueBinary() throws IOException {
  1035. setupCase(mk("foo"), mkmap("foo/bar", "foo\nb\u0000ar"), mk("foo"));
  1036. db.getConfig().setString("core", null, "autocrlf", "true");
  1037. checkout();
  1038. assertIndex(mkmap("foo/bar", "foo\nb\u0000ar"));
  1039. assertWorkDir(mkmap("foo/bar", "foo\nb\u0000ar"));
  1040. }
  1041. @Test
  1042. public void testCheckoutUncachedChanges() throws IOException {
  1043. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1044. writeTrashFile("foo", "otherData");
  1045. checkout();
  1046. assertIndex(mk("foo"));
  1047. assertWorkDir(mkmap("foo", "otherData"));
  1048. assertTrue(new File(trash, "foo").isFile());
  1049. }
  1050. @Test
  1051. public void testDontOverwriteDirtyFile() throws IOException {
  1052. setupCase(mk("foo"), mk("other"), mk("foo"));
  1053. writeTrashFile("foo", "different");
  1054. try {
  1055. checkout();
  1056. fail("Didn't got the expected conflict");
  1057. } catch (CheckoutConflictException e) {
  1058. assertIndex(mk("foo"));
  1059. assertWorkDir(mkmap("foo", "different"));
  1060. assertEquals(Arrays.asList("foo"), getConflicts());
  1061. assertTrue(new File(trash, "foo").isFile());
  1062. }
  1063. }
  1064. @Test
  1065. public void testDontOverwriteEmptyFolder() throws IOException {
  1066. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1067. FileUtils.mkdir(new File(db.getWorkTree(), "d"));
  1068. checkout();
  1069. assertWorkDir(mkmap("foo", "foo", "d", "/"));
  1070. }
  1071. @Test
  1072. public void testOverwriteUntrackedIgnoredFile() throws IOException,
  1073. GitAPIException {
  1074. String fname="file.txt";
  1075. Git git = Git.wrap(db);
  1076. // Add a file
  1077. writeTrashFile(fname, "a");
  1078. git.add().addFilepattern(fname).call();
  1079. git.commit().setMessage("create file").call();
  1080. // Create branch
  1081. git.branchCreate().setName("side").call();
  1082. // Modify file
  1083. writeTrashFile(fname, "b");
  1084. git.add().addFilepattern(fname).call();
  1085. git.commit().setMessage("modify file").call();
  1086. // Switch branches
  1087. git.checkout().setName("side").call();
  1088. git.rm().addFilepattern(fname).call();
  1089. writeTrashFile(".gitignore", fname);
  1090. git.add().addFilepattern(".gitignore").call();
  1091. git.commit().setMessage("delete and ignore file").call();
  1092. writeTrashFile(fname, "Something different");
  1093. git.checkout().setName("master").call();
  1094. assertWorkDir(mkmap(fname, "b"));
  1095. assertTrue(git.status().call().isClean());
  1096. }
  1097. @Test
  1098. public void testOverwriteUntrackedFileModeChange()
  1099. throws IOException, GitAPIException {
  1100. String fname = "file.txt";
  1101. Git git = Git.wrap(db);
  1102. // Add a file
  1103. File file = writeTrashFile(fname, "a");
  1104. git.add().addFilepattern(fname).call();
  1105. git.commit().setMessage("create file").call();
  1106. assertWorkDir(mkmap(fname, "a"));
  1107. // Create branch
  1108. git.branchCreate().setName("side").call();
  1109. // Switch branches
  1110. git.checkout().setName("side").call();
  1111. // replace file with directory containing files
  1112. FileUtils.delete(file);
  1113. // create and add a file in the new directory to the index
  1114. writeTrashFile(fname + "/dir1", "file1", "c");
  1115. git.add().addFilepattern(fname + "/dir1/file1").call();
  1116. // create but do not add a file in the new directory to the index
  1117. writeTrashFile(fname + "/dir2", "file2", "d");
  1118. assertTrue("File must be a directory now", file.isDirectory());
  1119. assertFalse("Must not delete non empty directory", file.delete());
  1120. // 2 extra files are created
  1121. assertWorkDir(
  1122. mkmap(fname + "/dir1/file1", "c", fname + "/dir2/file2", "d"));
  1123. try {
  1124. git.checkout().setName("master").call();
  1125. fail("did not throw exception");
  1126. } catch (Exception e) {
  1127. // 2 extra files are still there
  1128. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1129. fname + "/dir2/file2", "d"));
  1130. }
  1131. }
  1132. @Test
  1133. public void testOverwriteUntrackedLinkModeChange()
  1134. throws Exception {
  1135. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  1136. String fname = "file.txt";
  1137. Git git = Git.wrap(db);
  1138. // Add a file
  1139. writeTrashFile(fname, "a");
  1140. git.add().addFilepattern(fname).call();
  1141. // Add a link to file
  1142. String linkName = "link";
  1143. File link = writeLink(linkName, fname).toFile();
  1144. git.add().addFilepattern(linkName).call();
  1145. git.commit().setMessage("Added file and link").call();
  1146. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  1147. // Create branch
  1148. git.branchCreate().setName("side").call();
  1149. // Switch branches
  1150. git.checkout().setName("side").call();
  1151. // replace link with directory containing files
  1152. FileUtils.delete(link);
  1153. // create and add a file in the new directory to the index
  1154. writeTrashFile(linkName + "/dir1", "file1", "c");
  1155. git.add().addFilepattern(linkName + "/dir1/file1").call();
  1156. // create but do not add a file in the new directory to the index
  1157. writeTrashFile(linkName + "/dir2", "file2", "d");
  1158. assertTrue("Link must be a directory now", link.isDirectory());
  1159. assertFalse("Must not delete non empty directory", link.delete());
  1160. // 2 extra files are created
  1161. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1162. linkName + "/dir2/file2", "d"));
  1163. try {
  1164. git.checkout().setName("master").call();
  1165. fail("did not throw exception");
  1166. } catch (Exception e) {
  1167. // 2 extra files are still there
  1168. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1169. linkName + "/dir2/file2", "d"));
  1170. }
  1171. }
  1172. @Test
  1173. public void testFileModeChangeWithNoContentChangeUpdate() throws Exception {
  1174. if (!FS.DETECTED.supportsExecute())
  1175. return;
  1176. Git git = Git.wrap(db);
  1177. // Add non-executable file
  1178. File file = writeTrashFile("file.txt", "a");
  1179. git.add().addFilepattern("file.txt").call();
  1180. git.commit().setMessage("commit1").call();
  1181. assertFalse(db.getFS().canExecute(file));
  1182. // Create branch
  1183. git.branchCreate().setName("b1").call();
  1184. // Make file executable
  1185. db.getFS().setExecute(file, true);
  1186. git.add().addFilepattern("file.txt").call();
  1187. git.commit().setMessage("commit2").call();
  1188. // Verify executable and working directory is clean
  1189. Status status = git.status().call();
  1190. assertTrue(status.getModified().isEmpty());
  1191. assertTrue(status.getChanged().isEmpty());
  1192. assertTrue(db.getFS().canExecute(file));
  1193. // Switch branches
  1194. git.checkout().setName("b1").call();
  1195. // Verify not executable and working directory is clean
  1196. status = git.status().call();
  1197. assertTrue(status.getModified().isEmpty());
  1198. assertTrue(status.getChanged().isEmpty());
  1199. assertFalse(db.getFS().canExecute(file));
  1200. }
  1201. @Test
  1202. public void testFileModeChangeAndContentChangeConflict() throws Exception {
  1203. if (!FS.DETECTED.supportsExecute())
  1204. return;
  1205. Git git = Git.wrap(db);
  1206. // Add non-executable file
  1207. File file = writeTrashFile("file.txt", "a");
  1208. git.add().addFilepattern("file.txt").call();
  1209. git.commit().setMessage("commit1").call();
  1210. assertFalse(db.getFS().canExecute(file));
  1211. // Create branch
  1212. git.branchCreate().setName("b1").call();
  1213. // Make file executable
  1214. db.getFS().setExecute(file, true);
  1215. git.add().addFilepattern("file.txt").call();
  1216. git.commit().setMessage("commit2").call();
  1217. // Verify executable and working directory is clean
  1218. Status status = git.status().call();
  1219. assertTrue(status.getModified().isEmpty());
  1220. assertTrue(status.getChanged().isEmpty());
  1221. assertTrue(db.getFS().canExecute(file));
  1222. writeTrashFile("file.txt", "b");
  1223. // Switch branches
  1224. CheckoutCommand checkout = git.checkout().setName("b1");
  1225. try {
  1226. checkout.call();
  1227. fail("Checkout exception not thrown");
  1228. } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) {
  1229. CheckoutResult result = checkout.getResult();
  1230. assertNotNull(result);
  1231. assertNotNull(result.getConflictList());
  1232. assertEquals(1, result.getConflictList().size());
  1233. assertTrue(result.getConflictList().contains("file.txt"));
  1234. }
  1235. }
  1236. @Test
  1237. public void testDirtyFileModeEqualHeadMerge()
  1238. throws Exception {
  1239. if (!FS.DETECTED.supportsExecute())
  1240. return;
  1241. Git git = Git.wrap(db);
  1242. // Add non-executable file
  1243. File file = writeTrashFile("file.txt", "a");
  1244. git.add().addFilepattern("file.txt").call();
  1245. git.commit().setMessage("commit1").call();
  1246. assertFalse(db.getFS().canExecute(file));
  1247. // Create branch
  1248. git.branchCreate().setName("b1").call();
  1249. // Create second commit and don't touch file
  1250. writeTrashFile("file2.txt", "");
  1251. git.add().addFilepattern("file2.txt").call();
  1252. git.commit().setMessage("commit2").call();
  1253. // stage a mode change
  1254. writeTrashFile("file.txt", "a");
  1255. db.getFS().setExecute(file, true);
  1256. git.add().addFilepattern("file.txt").call();
  1257. // dirty the file
  1258. writeTrashFile("file.txt", "b");
  1259. assertEquals(
  1260. "[file.txt, mode:100755, content:a][file2.txt, mode:100644, content:]",
  1261. indexState(CONTENT));
  1262. assertWorkDir(mkmap("file.txt", "b", "file2.txt", ""));
  1263. // Switch branches and check that the dirty file survived in worktree
  1264. // and index
  1265. git.checkout().setName("b1").call();
  1266. assertEquals("[file.txt, mode:100755, content:a]", indexState(CONTENT));
  1267. assertWorkDir(mkmap("file.txt", "b"));
  1268. }
  1269. @Test
  1270. public void testDirtyFileModeEqualIndexMerge()
  1271. throws Exception {
  1272. if (!FS.DETECTED.supportsExecute())
  1273. return;
  1274. Git git = Git.wrap(db);
  1275. // Add non-executable file
  1276. File file = writeTrashFile("file.txt", "a");
  1277. git.add().addFilepattern("file.txt").call();
  1278. git.commit().setMessage("commit1").call();
  1279. assertFalse(db.getFS().canExecute(file));
  1280. // Create branch
  1281. git.branchCreate().setName("b1").call();
  1282. // Create second commit with executable file
  1283. file = writeTrashFile("file.txt", "b");
  1284. db.getFS().setExecute(file, true);
  1285. git.add().addFilepattern("file.txt").call();
  1286. git.commit().setMessage("commit2").call();
  1287. // stage the same content as in the branch we want to switch to
  1288. writeTrashFile("file.txt", "a");
  1289. db.getFS().setExecute(file, false);
  1290. git.add().addFilepattern("file.txt").call();
  1291. // dirty the file
  1292. writeTrashFile("file.txt", "c");
  1293. db.getFS().setExecute(file, true);
  1294. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1295. assertWorkDir(mkmap("file.txt", "c"));
  1296. // Switch branches and check that the dirty file survived in worktree
  1297. // and index
  1298. git.checkout().setName("b1").call();
  1299. assertEquals("[file.txt, mode:100644, content:a]", indexState(CONTENT));
  1300. assertWorkDir(mkmap("file.txt", "c"));
  1301. }
  1302. @Test
  1303. public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
  1304. if (!FS.DETECTED.supportsExecute())
  1305. return;
  1306. Git git = Git.wrap(db);
  1307. // Add first file
  1308. File file1 = writeTrashFile("file1.txt", "a");
  1309. git.add().addFilepattern("file1.txt").call();
  1310. git.commit().setMessage("commit1").call();
  1311. assertFalse(db.getFS().canExecute(file1));
  1312. // Add second file
  1313. File file2 = writeTrashFile("file2.txt", "b");
  1314. git.add().addFilepattern("file2.txt").call();
  1315. git.commit().setMessage("commit2").call();
  1316. assertFalse(db.getFS().canExecute(file2));
  1317. // Create branch from first commit
  1318. assertNotNull(git.checkout().setCreateBranch(true).setName("b1")
  1319. .setStartPoint(Constants.HEAD + "~1").call());
  1320. // Change content and file mode in working directory and index
  1321. file1 = writeTrashFile("file1.txt", "c");
  1322. db.getFS().setExecute(file1, true);
  1323. git.add().addFilepattern("file1.txt").call();
  1324. // Switch back to 'master'
  1325. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  1326. }
  1327. @Test(expected = CheckoutConflictException.class)
  1328. public void testFolderFileConflict() throws Exception {
  1329. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1330. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1331. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1332. writeTrashFile("f", "file instead of folder");
  1333. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1334. checkoutCommit.getTree()).checkout();
  1335. }
  1336. @Test
  1337. public void testMultipleContentConflicts() throws Exception {
  1338. commitFile("a", "initial content", "master");
  1339. RevCommit headCommit = commitFile("b", "initial content", "master");
  1340. commitFile("a", "side content", "side");
  1341. RevCommit checkoutCommit = commitFile("b", "side content", "side");
  1342. writeTrashFile("a", "changed content");
  1343. writeTrashFile("b", "changed content");
  1344. try {
  1345. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1346. checkoutCommit.getTree()).checkout();
  1347. fail();
  1348. } catch (CheckoutConflictException expected) {
  1349. assertEquals(2, expected.getConflictingFiles().length);
  1350. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1351. .contains("a"));
  1352. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1353. .contains("b"));
  1354. assertEquals("changed content", read("a"));
  1355. assertEquals("changed content", read("b"));
  1356. }
  1357. }
  1358. @Test
  1359. public void testFolderFileAndContentConflicts() throws Exception {
  1360. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1361. commitFile("b", "side content", "side");
  1362. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1363. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1364. writeTrashFile("f", "file instead of a folder");
  1365. writeTrashFile("b", "changed content");
  1366. try {
  1367. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1368. checkoutCommit.getTree()).checkout();
  1369. fail();
  1370. } catch (CheckoutConflictException expected) {
  1371. assertEquals(2, expected.getConflictingFiles().length);
  1372. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1373. .contains("b"));
  1374. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1375. .contains("f"));
  1376. assertEquals("file instead of a folder", read("f"));
  1377. assertEquals("changed content", read("b"));
  1378. }
  1379. }
  1380. public void assertWorkDir(Map<String, String> i)
  1381. throws CorruptObjectException,
  1382. IOException {
  1383. try (TreeWalk walk = new TreeWalk(db)) {
  1384. walk.setRecursive(false);
  1385. walk.addTree(new FileTreeIterator(db));
  1386. String expectedValue;
  1387. String path;
  1388. int nrFiles = 0;
  1389. FileTreeIterator ft;
  1390. while (walk.next()) {
  1391. ft = walk.getTree(0, FileTreeIterator.class);
  1392. path = ft.getEntryPathString();
  1393. expectedValue = i.get(path);
  1394. File file = new File(db.getWorkTree(), path);
  1395. assertTrue(file.exists());
  1396. if (file.isFile()) {
  1397. assertNotNull("found unexpected file for path " + path
  1398. + " in workdir", expectedValue);
  1399. FileInputStream is = new FileInputStream(file);
  1400. byte[] buffer = new byte[(int) file.length()];
  1401. int offset = 0;
  1402. int numRead = 0;
  1403. while (offset < buffer.length
  1404. && (numRead = is.read(buffer, offset, buffer.length
  1405. - offset)) >= 0) {
  1406. offset += numRead;
  1407. }
  1408. is.close();
  1409. assertArrayEquals("unexpected content for path " + path
  1410. + " in workDir. ", buffer, i.get(path).getBytes());
  1411. nrFiles++;
  1412. } else if (file.isDirectory()) {
  1413. String[] files = file.list();
  1414. if (files != null && files.length == 0) {
  1415. assertEquals("found unexpected empty folder for path "
  1416. + path + " in workDir. ", "/", i.get(path));
  1417. nrFiles++;
  1418. }
  1419. }
  1420. if (walk.isSubtree()) {
  1421. walk.enterSubtree();
  1422. }
  1423. }
  1424. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  1425. }
  1426. }
  1427. }