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

DirCacheCheckoutTest.java 49KB

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