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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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 java.nio.charset.StandardCharsets.UTF_8;
  43. import static org.junit.Assert.assertArrayEquals;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.FileInputStream;
  51. import java.io.IOException;
  52. import java.util.Arrays;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. import org.eclipse.jgit.api.CheckoutCommand;
  57. import org.eclipse.jgit.api.CheckoutResult;
  58. import org.eclipse.jgit.api.Git;
  59. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  60. import org.eclipse.jgit.api.ResetCommand.ResetType;
  61. import org.eclipse.jgit.api.Status;
  62. import org.eclipse.jgit.api.errors.GitAPIException;
  63. import org.eclipse.jgit.api.errors.NoFilepatternException;
  64. import org.eclipse.jgit.dircache.DirCache;
  65. import org.eclipse.jgit.dircache.DirCacheCheckout;
  66. import org.eclipse.jgit.dircache.DirCacheCheckout.CheckoutMetadata;
  67. import org.eclipse.jgit.dircache.DirCacheEditor;
  68. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  69. import org.eclipse.jgit.dircache.DirCacheEntry;
  70. import org.eclipse.jgit.errors.CheckoutConflictException;
  71. import org.eclipse.jgit.errors.CorruptObjectException;
  72. import org.eclipse.jgit.errors.NoWorkTreeException;
  73. import org.eclipse.jgit.events.ChangeRecorder;
  74. import org.eclipse.jgit.events.ListenerHandle;
  75. import org.eclipse.jgit.junit.RepositoryTestCase;
  76. import org.eclipse.jgit.junit.TestRepository;
  77. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  78. import org.eclipse.jgit.revwalk.RevCommit;
  79. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  80. import org.eclipse.jgit.treewalk.FileTreeIterator;
  81. import org.eclipse.jgit.treewalk.TreeWalk;
  82. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  83. import org.eclipse.jgit.util.FS;
  84. import org.eclipse.jgit.util.FileUtils;
  85. import org.junit.Assume;
  86. import org.junit.Test;
  87. public class DirCacheCheckoutTest extends RepositoryTestCase {
  88. private DirCacheCheckout dco;
  89. protected ObjectId theHead;
  90. protected ObjectId theMerge;
  91. private DirCache dirCache;
  92. private void prescanTwoTrees(ObjectId head, ObjectId merge)
  93. throws IllegalStateException, IOException {
  94. DirCache dc = db.lockDirCache();
  95. try {
  96. dco = new DirCacheCheckout(db, head, dc, merge);
  97. dco.preScanTwoTrees();
  98. } finally {
  99. dc.unlock();
  100. }
  101. }
  102. private void checkout() throws IOException {
  103. DirCache dc = db.lockDirCache();
  104. try {
  105. dco = new DirCacheCheckout(db, theHead, dc, theMerge);
  106. dco.checkout();
  107. } finally {
  108. dc.unlock();
  109. }
  110. }
  111. private List<String> getRemoved() {
  112. return dco.getRemoved();
  113. }
  114. private Map<String, CheckoutMetadata> getUpdated() {
  115. return dco.getUpdated();
  116. }
  117. private List<String> getConflicts() {
  118. return dco.getConflicts();
  119. }
  120. private static HashMap<String, String> mk(String a) {
  121. return mkmap(a, a);
  122. }
  123. private static HashMap<String, String> mkmap(String... args) {
  124. if ((args.length % 2) > 0)
  125. throw new IllegalArgumentException("needs to be pairs");
  126. HashMap<String, String> map = new HashMap<>();
  127. for (int i = 0; i < args.length; i += 2) {
  128. map.put(args[i], args[i + 1]);
  129. }
  130. return map;
  131. }
  132. @Test
  133. public void testResetHard() throws IOException, NoFilepatternException,
  134. GitAPIException {
  135. ChangeRecorder recorder = new ChangeRecorder();
  136. ListenerHandle handle = null;
  137. try (Git git = new Git(db)) {
  138. handle = db.getListenerList()
  139. .addWorkingTreeModifiedListener(recorder);
  140. writeTrashFile("f", "f()");
  141. writeTrashFile("D/g", "g()");
  142. git.add().addFilepattern(".").call();
  143. git.commit().setMessage("inital").call();
  144. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  145. recorder.assertNoEvent();
  146. git.branchCreate().setName("topic").call();
  147. recorder.assertNoEvent();
  148. writeTrashFile("f", "f()\nmaster");
  149. writeTrashFile("D/g", "g()\ng2()");
  150. writeTrashFile("E/h", "h()");
  151. git.add().addFilepattern(".").call();
  152. RevCommit master = git.commit().setMessage("master-1").call();
  153. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  154. recorder.assertNoEvent();
  155. checkoutBranch("refs/heads/topic");
  156. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  157. recorder.assertEvent(new String[] { "f", "D/g" },
  158. new String[] { "E/h" });
  159. writeTrashFile("f", "f()\nside");
  160. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  161. writeTrashFile("G/i", "i()");
  162. git.add().addFilepattern(".").call();
  163. git.add().addFilepattern(".").setUpdate(true).call();
  164. RevCommit topic = git.commit().setMessage("topic-1").call();
  165. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  166. recorder.assertNoEvent();
  167. writeTrashFile("untracked", "untracked");
  168. resetHard(master);
  169. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  170. recorder.assertEvent(new String[] { "f", "D/g", "E/h" },
  171. new String[] { "G", "G/i" });
  172. resetHard(topic);
  173. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  174. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()", "untracked",
  175. "untracked"));
  176. recorder.assertEvent(new String[] { "f", "G/i" },
  177. new String[] { "D", "D/g", "E", "E/h" });
  178. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  179. .call().getMergeStatus());
  180. assertEquals(
  181. "[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]",
  182. indexState(0));
  183. recorder.assertEvent(new String[] { "f", "D/g", "E/h" },
  184. ChangeRecorder.EMPTY);
  185. resetHard(master);
  186. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  187. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  188. "h()", "untracked", "untracked"));
  189. recorder.assertEvent(new String[] { "f", "D/g" },
  190. new String[] { "G", "G/i" });
  191. } finally {
  192. if (handle != null) {
  193. handle.remove();
  194. }
  195. }
  196. }
  197. /**
  198. * Reset hard from unclean condition.
  199. * <p>
  200. * WorkDir: Empty <br>
  201. * Index: f/g <br>
  202. * Merge: x
  203. *
  204. * @throws Exception
  205. */
  206. @Test
  207. public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
  208. throws Exception {
  209. ChangeRecorder recorder = new ChangeRecorder();
  210. ListenerHandle handle = null;
  211. try (Git git = new Git(db)) {
  212. handle = db.getListenerList()
  213. .addWorkingTreeModifiedListener(recorder);
  214. writeTrashFile("x", "x");
  215. git.add().addFilepattern("x").call();
  216. RevCommit id1 = git.commit().setMessage("c1").call();
  217. writeTrashFile("f/g", "f/g");
  218. git.rm().addFilepattern("x").call();
  219. recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { "x" });
  220. git.add().addFilepattern("f/g").call();
  221. git.commit().setMessage("c2").call();
  222. deleteTrashFile("f/g");
  223. deleteTrashFile("f");
  224. // The actual test
  225. git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call();
  226. assertIndex(mkmap("x", "x"));
  227. recorder.assertEvent(new String[] { "x" }, ChangeRecorder.EMPTY);
  228. } finally {
  229. if (handle != null) {
  230. handle.remove();
  231. }
  232. }
  233. }
  234. /**
  235. * Test first checkout in a repo
  236. *
  237. * @throws Exception
  238. */
  239. @Test
  240. public void testInitialCheckout() throws Exception {
  241. ChangeRecorder recorder = new ChangeRecorder();
  242. ListenerHandle handle = null;
  243. try (Git git = new Git(db);
  244. TestRepository<Repository> db_t = new TestRepository<>(db)) {
  245. handle = db.getListenerList()
  246. .addWorkingTreeModifiedListener(recorder);
  247. BranchBuilder master = db_t.branch("master");
  248. master.commit().add("f", "1").message("m0").create();
  249. assertFalse(new File(db.getWorkTree(), "f").exists());
  250. git.checkout().setName("master").call();
  251. assertTrue(new File(db.getWorkTree(), "f").exists());
  252. recorder.assertEvent(new String[] { "f" }, ChangeRecorder.EMPTY);
  253. } finally {
  254. if (handle != null) {
  255. handle.remove();
  256. }
  257. }
  258. }
  259. private DirCacheCheckout resetHard(RevCommit commit)
  260. throws NoWorkTreeException,
  261. CorruptObjectException, IOException {
  262. DirCacheCheckout dc;
  263. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  264. commit.getTree());
  265. dc.setFailOnConflict(true);
  266. assertTrue(dc.checkout());
  267. return dc;
  268. }
  269. private void assertIndex(HashMap<String, String> i)
  270. throws CorruptObjectException, IOException {
  271. String expectedValue;
  272. String path;
  273. DirCache read = DirCache.read(db.getIndexFile(), db.getFS());
  274. assertEquals("Index has not the right size.", i.size(),
  275. read.getEntryCount());
  276. for (int j = 0; j < read.getEntryCount(); j++) {
  277. path = read.getEntry(j).getPathString();
  278. expectedValue = i.get(path);
  279. assertNotNull("found unexpected entry for path " + path
  280. + " in index", expectedValue);
  281. assertTrue("unexpected content for path " + path
  282. + " in index. Expected: <" + expectedValue + ">",
  283. Arrays.equals(db.open(read.getEntry(j).getObjectId())
  284. .getCachedBytes(), i.get(path).getBytes(UTF_8)));
  285. }
  286. }
  287. @Test
  288. public void testRules1thru3_NoIndexEntry() throws IOException {
  289. ObjectId head = buildTree(mk("foo"));
  290. ObjectId merge = db.newObjectInserter().insert(Constants.OBJ_TREE,
  291. new byte[0]);
  292. prescanTwoTrees(head, merge);
  293. assertTrue(getRemoved().contains("foo"));
  294. prescanTwoTrees(merge, head);
  295. assertTrue(getUpdated().containsKey("foo"));
  296. merge = buildTree(mkmap("foo", "a"));
  297. prescanTwoTrees(head, merge);
  298. assertConflict("foo");
  299. }
  300. void setupCase(HashMap<String, String> headEntries, HashMap<String, String> mergeEntries, HashMap<String, String> indexEntries) throws IOException {
  301. theHead = buildTree(headEntries);
  302. theMerge = buildTree(mergeEntries);
  303. buildIndex(indexEntries);
  304. }
  305. private void buildIndex(HashMap<String, String> indexEntries) throws IOException {
  306. dirCache = new DirCache(db.getIndexFile(), db.getFS());
  307. if (indexEntries != null) {
  308. assertTrue(dirCache.lock());
  309. DirCacheEditor editor = dirCache.editor();
  310. for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
  311. writeTrashFile(e.getKey(), e.getValue());
  312. ObjectInserter inserter = db.newObjectInserter();
  313. final ObjectId id = inserter.insert(Constants.OBJ_BLOB,
  314. Constants.encode(e.getValue()));
  315. editor.add(new DirCacheEditor.DeletePath(e.getKey()));
  316. editor.add(new DirCacheEditor.PathEdit(e.getKey()) {
  317. @Override
  318. public void apply(DirCacheEntry ent) {
  319. ent.setFileMode(FileMode.REGULAR_FILE);
  320. ent.setObjectId(id);
  321. ent.setUpdateNeeded(false);
  322. }
  323. });
  324. }
  325. assertTrue(editor.commit());
  326. }
  327. }
  328. static final class AddEdit extends PathEdit {
  329. private final ObjectId data;
  330. private final long length;
  331. public AddEdit(String entryPath, ObjectId data, long length) {
  332. super(entryPath);
  333. this.data = data;
  334. this.length = length;
  335. }
  336. @Override
  337. public void apply(DirCacheEntry ent) {
  338. ent.setFileMode(FileMode.REGULAR_FILE);
  339. ent.setLength(length);
  340. ent.setObjectId(data);
  341. }
  342. }
  343. private ObjectId buildTree(HashMap<String, String> headEntries)
  344. throws IOException {
  345. DirCache lockDirCache = DirCache.newInCore();
  346. // assertTrue(lockDirCache.lock());
  347. DirCacheEditor editor = lockDirCache.editor();
  348. if (headEntries != null) {
  349. for (java.util.Map.Entry<String, String> e : headEntries.entrySet()) {
  350. AddEdit addEdit = new AddEdit(e.getKey(),
  351. genSha1(e.getValue()), e.getValue().length());
  352. editor.add(addEdit);
  353. }
  354. }
  355. editor.finish();
  356. return lockDirCache.writeTree(db.newObjectInserter());
  357. }
  358. ObjectId genSha1(String data) {
  359. try (ObjectInserter w = db.newObjectInserter()) {
  360. ObjectId id = w.insert(Constants.OBJ_BLOB, data.getBytes(UTF_8));
  361. w.flush();
  362. return id;
  363. } catch (IOException e) {
  364. fail(e.toString());
  365. }
  366. return null;
  367. }
  368. protected void go() throws IllegalStateException, IOException {
  369. prescanTwoTrees(theHead, theMerge);
  370. }
  371. @Test
  372. public void testRules4thru13_IndexEntryNotInHead() throws IOException {
  373. // rules 4 and 5
  374. HashMap<String, String> idxMap;
  375. idxMap = new HashMap<>();
  376. idxMap.put("foo", "foo");
  377. setupCase(null, null, idxMap);
  378. go();
  379. assertTrue(getUpdated().isEmpty());
  380. assertTrue(getRemoved().isEmpty());
  381. assertTrue(getConflicts().isEmpty());
  382. // rules 6 and 7
  383. idxMap = new HashMap<>();
  384. idxMap.put("foo", "foo");
  385. setupCase(null, idxMap, idxMap);
  386. go();
  387. assertAllEmpty();
  388. // rules 8 and 9
  389. HashMap<String, String> mergeMap;
  390. mergeMap = new HashMap<>();
  391. mergeMap.put("foo", "merge");
  392. setupCase(null, mergeMap, idxMap);
  393. go();
  394. assertTrue(getUpdated().isEmpty());
  395. assertTrue(getRemoved().isEmpty());
  396. assertTrue(getConflicts().contains("foo"));
  397. // rule 10
  398. HashMap<String, String> headMap = new HashMap<>();
  399. headMap.put("foo", "foo");
  400. setupCase(headMap, null, idxMap);
  401. go();
  402. assertTrue(getRemoved().contains("foo"));
  403. assertTrue(getUpdated().isEmpty());
  404. assertTrue(getConflicts().isEmpty());
  405. // rule 11
  406. setupCase(headMap, null, idxMap);
  407. assertTrue(new File(trash, "foo").delete());
  408. writeTrashFile("foo", "bar");
  409. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  410. go();
  411. assertTrue(getRemoved().isEmpty());
  412. assertTrue(getUpdated().isEmpty());
  413. assertTrue(getConflicts().contains("foo"));
  414. // rule 12 & 13
  415. headMap.put("foo", "head");
  416. setupCase(headMap, null, idxMap);
  417. go();
  418. assertTrue(getRemoved().isEmpty());
  419. assertTrue(getUpdated().isEmpty());
  420. assertTrue(getConflicts().contains("foo"));
  421. // rules 14 & 15
  422. setupCase(headMap, headMap, idxMap);
  423. go();
  424. assertAllEmpty();
  425. // rules 16 & 17
  426. setupCase(headMap, mergeMap, idxMap); go();
  427. assertTrue(getConflicts().contains("foo"));
  428. // rules 18 & 19
  429. setupCase(headMap, idxMap, idxMap); go();
  430. assertAllEmpty();
  431. // rule 20
  432. setupCase(idxMap, mergeMap, idxMap); go();
  433. assertTrue(getUpdated().containsKey("foo"));
  434. // rules 21
  435. setupCase(idxMap, mergeMap, idxMap);
  436. assertTrue(new File(trash, "foo").delete());
  437. writeTrashFile("foo", "bar");
  438. db.readDirCache().getEntry(0).setUpdateNeeded(true);
  439. go();
  440. assertTrue(getConflicts().contains("foo"));
  441. }
  442. private void assertAllEmpty() {
  443. assertTrue(getRemoved().isEmpty());
  444. assertTrue(getUpdated().isEmpty());
  445. assertTrue(getConflicts().isEmpty());
  446. }
  447. /*-
  448. * Directory/File Conflict cases:
  449. * It's entirely possible that in practice a number of these may be equivalent
  450. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  451. * that's all I care about. These are basically reverse-engineered from
  452. * what git currently does. If there are tests for these in git, it's kind of
  453. * hard to track them all down...
  454. *
  455. * H I M Clean H==M H==I I==M Result
  456. * ------------------------------------------------------------------
  457. *1 D D F Y N Y N Update
  458. *2 D D F N N Y N Conflict
  459. *3 D F D Y N N Keep
  460. *4 D F D N N N Conflict
  461. *5 D F F Y N N Y Keep
  462. *5b D F F Y N N N Conflict
  463. *6 D F F N N N Y Keep
  464. *6b D F F N N N N Conflict
  465. *7 F D F Y Y N N Update
  466. *8 F D F N Y N N Conflict
  467. *9 F D F Y N N N Update
  468. *10 F D D N N Y Keep
  469. *11 F D D N N N Conflict
  470. *12 F F D Y N Y N Update
  471. *13 F F D N N Y N Conflict
  472. *14 F F D N N N Conflict
  473. *15 0 F D N N N Conflict
  474. *16 0 D F Y N N N Update
  475. *17 0 D F N N N Conflict
  476. *18 F 0 D Update
  477. *19 D 0 F Update
  478. */
  479. @Test
  480. public void testDirectoryFileSimple() throws IOException {
  481. ObjectId treeDF = buildTree(mkmap("DF", "DF"));
  482. ObjectId treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  483. buildIndex(mkmap("DF", "DF"));
  484. prescanTwoTrees(treeDF, treeDFDF);
  485. assertTrue(getRemoved().contains("DF"));
  486. assertTrue(getUpdated().containsKey("DF/DF"));
  487. recursiveDelete(new File(trash, "DF"));
  488. buildIndex(mkmap("DF/DF", "DF/DF"));
  489. prescanTwoTrees(treeDFDF, treeDF);
  490. assertTrue(getRemoved().contains("DF/DF"));
  491. assertTrue(getUpdated().containsKey("DF"));
  492. }
  493. @Test
  494. public void testDirectoryFileConflicts_1() throws Exception {
  495. // 1
  496. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  497. assertNoConflicts();
  498. assertUpdated("DF");
  499. assertRemoved("DF/DF");
  500. }
  501. @Test
  502. public void testDirectoryFileConflicts_2() throws Exception {
  503. // 2
  504. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  505. writeTrashFile("DF/DF", "different");
  506. go();
  507. assertConflict("DF/DF");
  508. }
  509. @Test
  510. public void testDirectoryFileConflicts_3() throws Exception {
  511. // 3
  512. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  513. assertNoConflicts();
  514. }
  515. @Test
  516. public void testDirectoryFileConflicts_4() throws Exception {
  517. // 4 (basically same as 3, just with H and M different)
  518. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  519. assertConflict("DF/DF");
  520. }
  521. @Test
  522. public void testDirectoryFileConflicts_5() throws Exception {
  523. // 5
  524. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  525. assertRemoved("DF/DF");
  526. assertEquals(0, dco.getConflicts().size());
  527. assertEquals(0, dco.getUpdated().size());
  528. }
  529. @Test
  530. public void testDirectoryFileConflicts_5b() throws Exception {
  531. // 5
  532. doit(mk("DF/DF"), mkmap("DF", "different"), mk("DF"));
  533. assertRemoved("DF/DF");
  534. assertConflict("DF");
  535. assertEquals(0, dco.getUpdated().size());
  536. }
  537. @Test
  538. public void testDirectoryFileConflicts_6() throws Exception {
  539. // 6
  540. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  541. writeTrashFile("DF", "different");
  542. go();
  543. assertRemoved("DF/DF");
  544. assertEquals(0, dco.getConflicts().size());
  545. assertEquals(0, dco.getUpdated().size());
  546. }
  547. @Test
  548. public void testDirectoryFileConflicts_6b() throws Exception {
  549. // 6
  550. setupCase(mk("DF/DF"), mk("DF"), mkmap("DF", "different"));
  551. writeTrashFile("DF", "again different");
  552. go();
  553. assertRemoved("DF/DF");
  554. assertConflict("DF");
  555. assertEquals(0, dco.getUpdated().size());
  556. }
  557. @Test
  558. public void testDirectoryFileConflicts_7() throws Exception {
  559. // 7
  560. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  561. assertUpdated("DF");
  562. assertRemoved("DF/DF");
  563. cleanUpDF();
  564. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  565. go();
  566. assertRemoved("DF/DF/DF/DF/DF");
  567. assertUpdated("DF/DF");
  568. cleanUpDF();
  569. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  570. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  571. go();
  572. assertConflict("DF/DF/DF/DF/DF");
  573. // assertUpdated("DF/DF");
  574. // Why do we expect an update on DF/DF. H==M,
  575. // H&M are files and index contains a dir, index
  576. // is dirty: that case is not in the table but
  577. // we cannot update DF/DF to a file, this would
  578. // require that we delete DF/DF/DF/DF/DF in workdir
  579. // throwing away unsaved contents.
  580. // This test would fail in DirCacheCheckoutTests.
  581. }
  582. @Test
  583. public void testDirectoryFileConflicts_8() throws Exception {
  584. // 8
  585. setupCase(mk("DF"), mk("DF"), mk("DF/DF"));
  586. recursiveDelete(new File(db.getWorkTree(), "DF"));
  587. writeTrashFile("DF", "xy");
  588. go();
  589. assertConflict("DF/DF");
  590. }
  591. @Test
  592. public void testDirectoryFileConflicts_9() throws Exception {
  593. // 9
  594. doit(mkmap("DF", "QP"), mkmap("DF", "QP"), mkmap("DF/DF", "DF/DF"));
  595. assertRemoved("DF/DF");
  596. assertUpdated("DF");
  597. }
  598. @Test
  599. public void testDirectoryFileConflicts_10() throws Exception {
  600. // 10
  601. cleanUpDF();
  602. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  603. assertNoConflicts();
  604. }
  605. @Test
  606. public void testDirectoryFileConflicts_11() throws Exception {
  607. // 11
  608. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  609. assertConflict("DF/DF");
  610. }
  611. @Test
  612. public void testDirectoryFileConflicts_12() throws Exception {
  613. // 12
  614. cleanUpDF();
  615. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  616. assertRemoved("DF");
  617. assertUpdated("DF/DF");
  618. }
  619. @Test
  620. public void testDirectoryFileConflicts_13() throws Exception {
  621. // 13
  622. cleanUpDF();
  623. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  624. writeTrashFile("DF", "asdfsdf");
  625. go();
  626. assertConflict("DF");
  627. assertUpdated("DF/DF");
  628. }
  629. @Test
  630. public void testDirectoryFileConflicts_14() throws Exception {
  631. // 14
  632. cleanUpDF();
  633. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  634. assertConflict("DF");
  635. assertUpdated("DF/DF");
  636. }
  637. @Test
  638. public void testDirectoryFileConflicts_15() throws Exception {
  639. // 15
  640. doit(mkmap(), mk("DF/DF"), mk("DF"));
  641. // This test would fail in DirCacheCheckoutTests. I think this test is wrong,
  642. // it should check for conflicts according to rule 15
  643. // assertRemoved("DF");
  644. assertUpdated("DF/DF");
  645. }
  646. @Test
  647. public void testDirectoryFileConflicts_15b() throws Exception {
  648. // 15, take 2, just to check multi-leveled
  649. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  650. // I think this test is wrong, it should
  651. // check for conflicts according to rule 15
  652. // This test would fail in DirCacheCheckouts
  653. // assertRemoved("DF");
  654. assertUpdated("DF/DF/DF/DF");
  655. }
  656. @Test
  657. public void testDirectoryFileConflicts_16() throws Exception {
  658. // 16
  659. cleanUpDF();
  660. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  661. assertRemoved("DF/DF/DF");
  662. assertUpdated("DF");
  663. }
  664. @Test
  665. public void testDirectoryFileConflicts_17() throws Exception {
  666. // 17
  667. cleanUpDF();
  668. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  669. writeTrashFile("DF/DF/DF", "asdf");
  670. go();
  671. assertConflict("DF/DF/DF");
  672. // Why do we expect an update on DF. If we really update
  673. // DF and update also the working tree we would have to
  674. // overwrite a dirty file in the work-tree DF/DF/DF
  675. // This test would fail in DirCacheCheckout
  676. // assertUpdated("DF");
  677. }
  678. @Test
  679. public void testDirectoryFileConflicts_18() throws Exception {
  680. // 18
  681. cleanUpDF();
  682. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  683. assertRemoved("DF/DF");
  684. assertUpdated("DF/DF/DF/DF");
  685. }
  686. @Test
  687. public void testDirectoryFileConflicts_19() throws Exception {
  688. // 19
  689. cleanUpDF();
  690. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  691. assertRemoved("DF/DF/DF/DF");
  692. assertUpdated("DF/DF/DF");
  693. }
  694. protected void cleanUpDF() throws Exception {
  695. tearDown();
  696. setUp();
  697. recursiveDelete(new File(trash, "DF"));
  698. }
  699. protected void assertConflict(String s) {
  700. assertTrue(getConflicts().contains(s));
  701. }
  702. protected void assertUpdated(String s) {
  703. assertTrue(getUpdated().containsKey(s));
  704. }
  705. protected void assertRemoved(String s) {
  706. assertTrue(getRemoved().contains(s));
  707. }
  708. protected void assertNoConflicts() {
  709. assertTrue(getConflicts().isEmpty());
  710. }
  711. protected void doit(HashMap<String, String> h, HashMap<String, String> m, HashMap<String, String> i)
  712. throws IOException {
  713. setupCase(h, m, i);
  714. go();
  715. }
  716. @Test
  717. public void testUntrackedConflicts() throws IOException {
  718. setupCase(null, mk("foo"), null);
  719. writeTrashFile("foo", "foo");
  720. go();
  721. // test that we don't overwrite untracked files when there is a HEAD
  722. recursiveDelete(new File(trash, "foo"));
  723. setupCase(mk("other"), mkmap("other", "other", "foo", "foo"),
  724. mk("other"));
  725. writeTrashFile("foo", "bar");
  726. try {
  727. checkout();
  728. fail("didn't get the expected exception");
  729. } catch (CheckoutConflictException e) {
  730. assertConflict("foo");
  731. assertEquals("foo", e.getConflictingFiles()[0]);
  732. assertWorkDir(mkmap("foo", "bar", "other", "other"));
  733. assertIndex(mk("other"));
  734. }
  735. // test that we don't overwrite untracked files when there is no HEAD
  736. recursiveDelete(new File(trash, "other"));
  737. recursiveDelete(new File(trash, "foo"));
  738. setupCase(null, mk("foo"), null);
  739. writeTrashFile("foo", "bar");
  740. try {
  741. checkout();
  742. fail("didn't get the expected exception");
  743. } catch (CheckoutConflictException e) {
  744. assertConflict("foo");
  745. assertWorkDir(mkmap("foo", "bar"));
  746. assertIndex(mkmap("other", "other"));
  747. }
  748. // TODO: Why should we expect conflicts here?
  749. // H and M are empty and according to rule #5 of
  750. // the carry-over rules a dirty index is no reason
  751. // for a conflict. (I also feel it should be a
  752. // conflict because we are going to overwrite
  753. // unsaved content in the working tree
  754. // This test would fail in DirCacheCheckoutTest
  755. // assertConflict("foo");
  756. recursiveDelete(new File(trash, "foo"));
  757. recursiveDelete(new File(trash, "other"));
  758. setupCase(null, mk("foo"), null);
  759. writeTrashFile("foo/bar/baz", "");
  760. writeTrashFile("foo/blahblah", "");
  761. go();
  762. assertConflict("foo");
  763. assertConflict("foo/bar/baz");
  764. assertConflict("foo/blahblah");
  765. recursiveDelete(new File(trash, "foo"));
  766. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  767. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  768. assertTrue(new File(trash, "foo/bar").exists());
  769. go();
  770. assertNoConflicts();
  771. }
  772. @Test
  773. public void testCloseNameConflictsX0() throws IOException {
  774. 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") );
  775. checkout();
  776. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  777. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  778. go();
  779. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  780. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  781. assertNoConflicts();
  782. }
  783. @Test
  784. public void testCloseNameConflicts1() throws IOException {
  785. 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") );
  786. checkout();
  787. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  788. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  789. go();
  790. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  791. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  792. assertNoConflicts();
  793. }
  794. @Test
  795. public void testCheckoutHierarchy() throws IOException {
  796. setupCase(
  797. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  798. "e/g"),
  799. mkmap("a", "a2", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  800. "e/g2"),
  801. mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f", "e/f", "e/g",
  802. "e/g3"));
  803. try {
  804. checkout();
  805. fail("did not throw CheckoutConflictException");
  806. } catch (CheckoutConflictException e) {
  807. assertWorkDir(mkmap("a", "a", "b/c", "b/c", "d", "d", "e/f",
  808. "e/f", "e/g", "e/g3"));
  809. assertConflict("e/g");
  810. assertEquals("e/g", e.getConflictingFiles()[0]);
  811. }
  812. }
  813. @Test
  814. public void testCheckoutOutChanges() throws IOException {
  815. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  816. checkout();
  817. assertIndex(mk("foo/bar"));
  818. assertWorkDir(mk("foo/bar"));
  819. assertFalse(new File(trash, "foo").isFile());
  820. assertTrue(new File(trash, "foo/bar").isFile());
  821. recursiveDelete(new File(trash, "foo"));
  822. assertWorkDir(mkmap());
  823. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  824. checkout();
  825. assertIndex(mk("foo"));
  826. assertWorkDir(mk("foo"));
  827. assertFalse(new File(trash, "foo/bar").isFile());
  828. assertTrue(new File(trash, "foo").isFile());
  829. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  830. assertIndex(mkmap("foo", "bar"));
  831. assertWorkDir(mkmap("foo", "bar"));
  832. try {
  833. checkout();
  834. fail("did not throw exception");
  835. } catch (CheckoutConflictException e) {
  836. assertIndex(mkmap("foo", "bar"));
  837. assertWorkDir(mkmap("foo", "bar"));
  838. }
  839. }
  840. @Test
  841. public void testCheckoutChangeLinkToEmptyDir() throws Exception {
  842. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  843. String fname = "was_file";
  844. ChangeRecorder recorder = new ChangeRecorder();
  845. ListenerHandle handle = null;
  846. try (Git git = new Git(db)) {
  847. handle = db.getListenerList()
  848. .addWorkingTreeModifiedListener(recorder);
  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 empty directory
  859. FileUtils.delete(link);
  860. FileUtils.mkdir(link);
  861. assertTrue("Link must be a directory now", link.isDirectory());
  862. // modify file
  863. writeTrashFile(fname, "b");
  864. assertWorkDir(mkmap(fname, "b", linkName, "/"));
  865. recorder.assertNoEvent();
  866. // revert both paths to HEAD state
  867. git.checkout().setStartPoint(Constants.HEAD).addPath(fname)
  868. .addPath(linkName).call();
  869. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  870. recorder.assertEvent(new String[] { fname, linkName },
  871. ChangeRecorder.EMPTY);
  872. Status st = git.status().call();
  873. assertTrue(st.isClean());
  874. } finally {
  875. if (handle != null) {
  876. handle.remove();
  877. }
  878. }
  879. }
  880. @Test
  881. public void testCheckoutChangeLinkToEmptyDirs() throws Exception {
  882. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  883. String fname = "was_file";
  884. ChangeRecorder recorder = new ChangeRecorder();
  885. ListenerHandle handle = null;
  886. try (Git git = new Git(db)) {
  887. handle = db.getListenerList()
  888. .addWorkingTreeModifiedListener(recorder);
  889. // Add a file
  890. writeTrashFile(fname, "a");
  891. git.add().addFilepattern(fname).call();
  892. // Add a link to file
  893. String linkName = "link";
  894. File link = writeLink(linkName, fname).toFile();
  895. git.add().addFilepattern(linkName).call();
  896. git.commit().setMessage("Added file and link").call();
  897. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  898. // replace link with directory containing only directories, no files
  899. FileUtils.delete(link);
  900. FileUtils.mkdirs(new File(link, "dummyDir"));
  901. assertTrue("Link must be a directory now", link.isDirectory());
  902. assertFalse("Must not delete non empty directory", link.delete());
  903. // modify file
  904. writeTrashFile(fname, "b");
  905. assertWorkDir(mkmap(fname, "b", linkName + "/dummyDir", "/"));
  906. recorder.assertNoEvent();
  907. // revert both paths to HEAD state
  908. git.checkout().setStartPoint(Constants.HEAD).addPath(fname)
  909. .addPath(linkName).call();
  910. assertWorkDir(mkmap(fname, "a", linkName, "a"));
  911. recorder.assertEvent(new String[] { fname, linkName },
  912. ChangeRecorder.EMPTY);
  913. Status st = git.status().call();
  914. assertTrue(st.isClean());
  915. } finally {
  916. if (handle != null) {
  917. handle.remove();
  918. }
  919. }
  920. }
  921. @Test
  922. public void testCheckoutChangeLinkToNonEmptyDirs() throws Exception {
  923. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  924. String fname = "file";
  925. ChangeRecorder recorder = new ChangeRecorder();
  926. ListenerHandle handle = null;
  927. try (Git git = new Git(db)) {
  928. handle = db.getListenerList()
  929. .addWorkingTreeModifiedListener(recorder);
  930. // Add a file
  931. writeTrashFile(fname, "a");
  932. git.add().addFilepattern(fname).call();
  933. // Add a link to file
  934. String linkName = "link";
  935. File link = writeLink(linkName, fname).toFile();
  936. git.add().addFilepattern(linkName).call();
  937. git.commit().setMessage("Added file and link").call();
  938. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  939. // replace link with directory containing only directories, no files
  940. FileUtils.delete(link);
  941. // create but do not add a file in the new directory to the index
  942. writeTrashFile(linkName + "/dir1", "file1", "c");
  943. // create but do not add a file in the new directory to the index
  944. writeTrashFile(linkName + "/dir2", "file2", "d");
  945. assertTrue("File must be a directory now", link.isDirectory());
  946. assertFalse("Must not delete non empty directory", link.delete());
  947. // 2 extra files are created
  948. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  949. linkName + "/dir2/file2", "d"));
  950. recorder.assertNoEvent();
  951. // revert path to HEAD state
  952. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName)
  953. .call();
  954. // expect only the one added to the index
  955. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  956. recorder.assertEvent(new String[] { linkName },
  957. ChangeRecorder.EMPTY);
  958. Status st = git.status().call();
  959. assertTrue(st.isClean());
  960. } finally {
  961. if (handle != null) {
  962. handle.remove();
  963. }
  964. }
  965. }
  966. @Test
  967. public void testCheckoutChangeLinkToNonEmptyDirsAndNewIndexEntry()
  968. throws Exception {
  969. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  970. String fname = "file";
  971. ChangeRecorder recorder = new ChangeRecorder();
  972. ListenerHandle handle = null;
  973. try (Git git = new Git(db)) {
  974. handle = db.getListenerList()
  975. .addWorkingTreeModifiedListener(recorder);
  976. // Add a file
  977. writeTrashFile(fname, "a");
  978. git.add().addFilepattern(fname).call();
  979. // Add a link to file
  980. String linkName = "link";
  981. File link = writeLink(linkName, fname).toFile();
  982. git.add().addFilepattern(linkName).call();
  983. git.commit().setMessage("Added file and link").call();
  984. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  985. // replace link with directory containing only directories, no files
  986. FileUtils.delete(link);
  987. // create and add a file in the new directory to the index
  988. writeTrashFile(linkName + "/dir1", "file1", "c");
  989. git.add().addFilepattern(linkName + "/dir1/file1").call();
  990. // create but do not add a file in the new directory to the index
  991. writeTrashFile(linkName + "/dir2", "file2", "d");
  992. assertTrue("File must be a directory now", link.isDirectory());
  993. assertFalse("Must not delete non empty directory", link.delete());
  994. // 2 extra files are created
  995. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  996. linkName + "/dir2/file2", "d"));
  997. recorder.assertNoEvent();
  998. // revert path to HEAD state
  999. git.checkout().setStartPoint(Constants.HEAD).addPath(linkName)
  1000. .call();
  1001. // original file and link
  1002. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  1003. recorder.assertEvent(new String[] { linkName },
  1004. ChangeRecorder.EMPTY);
  1005. Status st = git.status().call();
  1006. assertTrue(st.isClean());
  1007. } finally {
  1008. if (handle != null) {
  1009. handle.remove();
  1010. }
  1011. }
  1012. }
  1013. @Test
  1014. public void testCheckoutChangeFileToEmptyDir() throws Exception {
  1015. String fname = "was_file";
  1016. ChangeRecorder recorder = new ChangeRecorder();
  1017. ListenerHandle handle = null;
  1018. try (Git git = new Git(db)) {
  1019. handle = db.getListenerList()
  1020. .addWorkingTreeModifiedListener(recorder);
  1021. // Add a file
  1022. File file = writeTrashFile(fname, "a");
  1023. git.add().addFilepattern(fname).call();
  1024. git.commit().setMessage("Added file").call();
  1025. // replace file with empty directory
  1026. FileUtils.delete(file);
  1027. FileUtils.mkdir(file);
  1028. assertTrue("File must be a directory now", file.isDirectory());
  1029. assertWorkDir(mkmap(fname, "/"));
  1030. recorder.assertNoEvent();
  1031. // revert path to HEAD state
  1032. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1033. assertWorkDir(mkmap(fname, "a"));
  1034. recorder.assertEvent(new String[] { fname }, ChangeRecorder.EMPTY);
  1035. Status st = git.status().call();
  1036. assertTrue(st.isClean());
  1037. } finally {
  1038. if (handle != null) {
  1039. handle.remove();
  1040. }
  1041. }
  1042. }
  1043. @Test
  1044. public void testCheckoutChangeFileToEmptyDirs() throws Exception {
  1045. String fname = "was_file";
  1046. ChangeRecorder recorder = new ChangeRecorder();
  1047. ListenerHandle handle = null;
  1048. try (Git git = new Git(db)) {
  1049. handle = db.getListenerList()
  1050. .addWorkingTreeModifiedListener(recorder);
  1051. // Add a file
  1052. File file = writeTrashFile(fname, "a");
  1053. git.add().addFilepattern(fname).call();
  1054. git.commit().setMessage("Added file").call();
  1055. // replace file with directory containing only directories, no files
  1056. FileUtils.delete(file);
  1057. FileUtils.mkdirs(new File(file, "dummyDir"));
  1058. assertTrue("File must be a directory now", file.isDirectory());
  1059. assertFalse("Must not delete non empty directory", file.delete());
  1060. assertWorkDir(mkmap(fname + "/dummyDir", "/"));
  1061. recorder.assertNoEvent();
  1062. // revert path to HEAD state
  1063. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1064. assertWorkDir(mkmap(fname, "a"));
  1065. recorder.assertEvent(new String[] { fname }, ChangeRecorder.EMPTY);
  1066. Status st = git.status().call();
  1067. assertTrue(st.isClean());
  1068. } finally {
  1069. if (handle != null) {
  1070. handle.remove();
  1071. }
  1072. }
  1073. }
  1074. @Test
  1075. public void testCheckoutChangeFileToNonEmptyDirs() throws Exception {
  1076. String fname = "was_file";
  1077. ChangeRecorder recorder = new ChangeRecorder();
  1078. ListenerHandle handle = null;
  1079. try (Git git = new Git(db)) {
  1080. handle = db.getListenerList()
  1081. .addWorkingTreeModifiedListener(recorder);
  1082. // Add a file
  1083. File file = writeTrashFile(fname, "a");
  1084. git.add().addFilepattern(fname).call();
  1085. git.commit().setMessage("Added file").call();
  1086. assertWorkDir(mkmap(fname, "a"));
  1087. // replace file with directory containing only directories, no files
  1088. FileUtils.delete(file);
  1089. // create but do not add a file in the new directory to the index
  1090. writeTrashFile(fname + "/dir1", "file1", "c");
  1091. // create but do not add a file in the new directory to the index
  1092. writeTrashFile(fname + "/dir2", "file2", "d");
  1093. assertTrue("File must be a directory now", file.isDirectory());
  1094. assertFalse("Must not delete non empty directory", file.delete());
  1095. // 2 extra files are created
  1096. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1097. fname + "/dir2/file2", "d"));
  1098. recorder.assertNoEvent();
  1099. // revert path to HEAD state
  1100. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1101. // expect only the one added to the index
  1102. assertWorkDir(mkmap(fname, "a"));
  1103. recorder.assertEvent(new String[] { fname }, ChangeRecorder.EMPTY);
  1104. Status st = git.status().call();
  1105. assertTrue(st.isClean());
  1106. } finally {
  1107. if (handle != null) {
  1108. handle.remove();
  1109. }
  1110. }
  1111. }
  1112. @Test
  1113. public void testCheckoutChangeFileToNonEmptyDirsAndNewIndexEntry()
  1114. throws Exception {
  1115. String fname = "was_file";
  1116. ChangeRecorder recorder = new ChangeRecorder();
  1117. ListenerHandle handle = null;
  1118. try (Git git = new Git(db)) {
  1119. handle = db.getListenerList()
  1120. .addWorkingTreeModifiedListener(recorder);
  1121. // Add a file
  1122. File file = writeTrashFile(fname, "a");
  1123. git.add().addFilepattern(fname).call();
  1124. git.commit().setMessage("Added file").call();
  1125. assertWorkDir(mkmap(fname, "a"));
  1126. // replace file with directory containing only directories, no files
  1127. FileUtils.delete(file);
  1128. // create and add a file in the new directory to the index
  1129. writeTrashFile(fname + "/dir", "file1", "c");
  1130. git.add().addFilepattern(fname + "/dir/file1").call();
  1131. // create but do not add a file in the new directory to the index
  1132. writeTrashFile(fname + "/dir", "file2", "d");
  1133. assertTrue("File must be a directory now", file.isDirectory());
  1134. assertFalse("Must not delete non empty directory", file.delete());
  1135. // 2 extra files are created
  1136. assertWorkDir(mkmap(fname + "/dir/file1", "c", fname + "/dir/file2",
  1137. "d"));
  1138. recorder.assertNoEvent();
  1139. // revert path to HEAD state
  1140. git.checkout().setStartPoint(Constants.HEAD).addPath(fname).call();
  1141. assertWorkDir(mkmap(fname, "a"));
  1142. recorder.assertEvent(new String[] { fname }, ChangeRecorder.EMPTY);
  1143. Status st = git.status().call();
  1144. assertTrue(st.isClean());
  1145. } finally {
  1146. if (handle != null) {
  1147. handle.remove();
  1148. }
  1149. }
  1150. }
  1151. @Test
  1152. public void testCheckoutOutChangesAutoCRLFfalse() throws IOException {
  1153. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1154. checkout();
  1155. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1156. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1157. }
  1158. @Test
  1159. public void testCheckoutOutChangesAutoCRLFInput() throws IOException {
  1160. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1161. db.getConfig().setString("core", null, "autocrlf", "input");
  1162. checkout();
  1163. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1164. assertWorkDir(mkmap("foo/bar", "foo\nbar"));
  1165. }
  1166. @Test
  1167. public void testCheckoutOutChangesAutoCRLFtrue() throws IOException {
  1168. setupCase(mk("foo"), mkmap("foo/bar", "foo\nbar"), mk("foo"));
  1169. db.getConfig().setString("core", null, "autocrlf", "true");
  1170. checkout();
  1171. assertIndex(mkmap("foo/bar", "foo\nbar"));
  1172. assertWorkDir(mkmap("foo/bar", "foo\r\nbar"));
  1173. }
  1174. @Test
  1175. public void testCheckoutOutChangesAutoCRLFtrueBinary() throws IOException {
  1176. setupCase(mk("foo"), mkmap("foo/bar", "foo\nb\u0000ar"), mk("foo"));
  1177. db.getConfig().setString("core", null, "autocrlf", "true");
  1178. checkout();
  1179. assertIndex(mkmap("foo/bar", "foo\nb\u0000ar"));
  1180. assertWorkDir(mkmap("foo/bar", "foo\nb\u0000ar"));
  1181. }
  1182. @Test
  1183. public void testCheckoutUncachedChanges() throws IOException {
  1184. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1185. writeTrashFile("foo", "otherData");
  1186. checkout();
  1187. assertIndex(mk("foo"));
  1188. assertWorkDir(mkmap("foo", "otherData"));
  1189. assertTrue(new File(trash, "foo").isFile());
  1190. }
  1191. @Test
  1192. public void testDontOverwriteDirtyFile() throws IOException {
  1193. setupCase(mk("foo"), mk("other"), mk("foo"));
  1194. writeTrashFile("foo", "different");
  1195. try {
  1196. checkout();
  1197. fail("Didn't got the expected conflict");
  1198. } catch (CheckoutConflictException e) {
  1199. assertIndex(mk("foo"));
  1200. assertWorkDir(mkmap("foo", "different"));
  1201. assertEquals(Arrays.asList("foo"), getConflicts());
  1202. assertTrue(new File(trash, "foo").isFile());
  1203. }
  1204. }
  1205. @Test
  1206. public void testDontOverwriteEmptyFolder() throws IOException {
  1207. setupCase(mk("foo"), mk("foo"), mk("foo"));
  1208. FileUtils.mkdir(new File(db.getWorkTree(), "d"));
  1209. checkout();
  1210. assertWorkDir(mkmap("foo", "foo", "d", "/"));
  1211. }
  1212. @Test
  1213. public void testOverwriteUntrackedIgnoredFile() throws IOException,
  1214. GitAPIException {
  1215. String fname="file.txt";
  1216. ChangeRecorder recorder = new ChangeRecorder();
  1217. ListenerHandle handle = null;
  1218. try (Git git = new Git(db)) {
  1219. handle = db.getListenerList()
  1220. .addWorkingTreeModifiedListener(recorder);
  1221. // Add a file
  1222. writeTrashFile(fname, "a");
  1223. git.add().addFilepattern(fname).call();
  1224. git.commit().setMessage("create file").call();
  1225. // Create branch
  1226. git.branchCreate().setName("side").call();
  1227. // Modify file
  1228. writeTrashFile(fname, "b");
  1229. git.add().addFilepattern(fname).call();
  1230. git.commit().setMessage("modify file").call();
  1231. recorder.assertNoEvent();
  1232. // Switch branches
  1233. git.checkout().setName("side").call();
  1234. recorder.assertEvent(new String[] { fname }, ChangeRecorder.EMPTY);
  1235. git.rm().addFilepattern(fname).call();
  1236. recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { fname });
  1237. writeTrashFile(".gitignore", fname);
  1238. git.add().addFilepattern(".gitignore").call();
  1239. git.commit().setMessage("delete and ignore file").call();
  1240. writeTrashFile(fname, "Something different");
  1241. recorder.assertNoEvent();
  1242. git.checkout().setName("master").call();
  1243. assertWorkDir(mkmap(fname, "b"));
  1244. recorder.assertEvent(new String[] { fname },
  1245. new String[] { ".gitignore" });
  1246. assertTrue(git.status().call().isClean());
  1247. } finally {
  1248. if (handle != null) {
  1249. handle.remove();
  1250. }
  1251. }
  1252. }
  1253. @Test
  1254. public void testOverwriteUntrackedFileModeChange()
  1255. throws IOException, GitAPIException {
  1256. String fname = "file.txt";
  1257. ChangeRecorder recorder = new ChangeRecorder();
  1258. ListenerHandle handle = null;
  1259. try (Git git = new Git(db)) {
  1260. handle = db.getListenerList()
  1261. .addWorkingTreeModifiedListener(recorder);
  1262. // Add a file
  1263. File file = writeTrashFile(fname, "a");
  1264. git.add().addFilepattern(fname).call();
  1265. git.commit().setMessage("create file").call();
  1266. assertWorkDir(mkmap(fname, "a"));
  1267. // Create branch
  1268. git.branchCreate().setName("side").call();
  1269. // Switch branches
  1270. git.checkout().setName("side").call();
  1271. recorder.assertNoEvent();
  1272. // replace file with directory containing files
  1273. FileUtils.delete(file);
  1274. // create and add a file in the new directory to the index
  1275. writeTrashFile(fname + "/dir1", "file1", "c");
  1276. git.add().addFilepattern(fname + "/dir1/file1").call();
  1277. // create but do not add a file in the new directory to the index
  1278. writeTrashFile(fname + "/dir2", "file2", "d");
  1279. assertTrue("File must be a directory now", file.isDirectory());
  1280. assertFalse("Must not delete non empty directory", file.delete());
  1281. // 2 extra files are created
  1282. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1283. fname + "/dir2/file2", "d"));
  1284. try {
  1285. git.checkout().setName("master").call();
  1286. fail("did not throw exception");
  1287. } catch (Exception e) {
  1288. // 2 extra files are still there
  1289. assertWorkDir(mkmap(fname + "/dir1/file1", "c",
  1290. fname + "/dir2/file2", "d"));
  1291. }
  1292. recorder.assertNoEvent();
  1293. } finally {
  1294. if (handle != null) {
  1295. handle.remove();
  1296. }
  1297. }
  1298. }
  1299. @Test
  1300. public void testOverwriteUntrackedLinkModeChange()
  1301. throws Exception {
  1302. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  1303. String fname = "file.txt";
  1304. ChangeRecorder recorder = new ChangeRecorder();
  1305. ListenerHandle handle = null;
  1306. try (Git git = new Git(db)) {
  1307. handle = db.getListenerList()
  1308. .addWorkingTreeModifiedListener(recorder);
  1309. // Add a file
  1310. writeTrashFile(fname, "a");
  1311. git.add().addFilepattern(fname).call();
  1312. // Add a link to file
  1313. String linkName = "link";
  1314. File link = writeLink(linkName, fname).toFile();
  1315. git.add().addFilepattern(linkName).call();
  1316. git.commit().setMessage("Added file and link").call();
  1317. assertWorkDir(mkmap(linkName, "a", fname, "a"));
  1318. // Create branch
  1319. git.branchCreate().setName("side").call();
  1320. // Switch branches
  1321. git.checkout().setName("side").call();
  1322. recorder.assertNoEvent();
  1323. // replace link with directory containing files
  1324. FileUtils.delete(link);
  1325. // create and add a file in the new directory to the index
  1326. writeTrashFile(linkName + "/dir1", "file1", "c");
  1327. git.add().addFilepattern(linkName + "/dir1/file1").call();
  1328. // create but do not add a file in the new directory to the index
  1329. writeTrashFile(linkName + "/dir2", "file2", "d");
  1330. assertTrue("Link must be a directory now", link.isDirectory());
  1331. assertFalse("Must not delete non empty directory", link.delete());
  1332. // 2 extra files are created
  1333. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1334. linkName + "/dir2/file2", "d"));
  1335. try {
  1336. git.checkout().setName("master").call();
  1337. fail("did not throw exception");
  1338. } catch (Exception e) {
  1339. // 2 extra files are still there
  1340. assertWorkDir(mkmap(fname, "a", linkName + "/dir1/file1", "c",
  1341. linkName + "/dir2/file2", "d"));
  1342. }
  1343. recorder.assertNoEvent();
  1344. } finally {
  1345. if (handle != null) {
  1346. handle.remove();
  1347. }
  1348. }
  1349. }
  1350. @Test
  1351. public void testFileModeChangeWithNoContentChangeUpdate() throws Exception {
  1352. if (!FS.DETECTED.supportsExecute())
  1353. return;
  1354. ChangeRecorder recorder = new ChangeRecorder();
  1355. ListenerHandle handle = null;
  1356. try (Git git = new Git(db)) {
  1357. handle = db.getListenerList()
  1358. .addWorkingTreeModifiedListener(recorder);
  1359. // Add non-executable file
  1360. File file = writeTrashFile("file.txt", "a");
  1361. git.add().addFilepattern("file.txt").call();
  1362. git.commit().setMessage("commit1").call();
  1363. assertFalse(db.getFS().canExecute(file));
  1364. // Create branch
  1365. git.branchCreate().setName("b1").call();
  1366. // Make file executable
  1367. db.getFS().setExecute(file, true);
  1368. git.add().addFilepattern("file.txt").call();
  1369. git.commit().setMessage("commit2").call();
  1370. recorder.assertNoEvent();
  1371. // Verify executable and working directory is clean
  1372. Status status = git.status().call();
  1373. assertTrue(status.getModified().isEmpty());
  1374. assertTrue(status.getChanged().isEmpty());
  1375. assertTrue(db.getFS().canExecute(file));
  1376. // Switch branches
  1377. git.checkout().setName("b1").call();
  1378. // Verify not executable and working directory is clean
  1379. status = git.status().call();
  1380. assertTrue(status.getModified().isEmpty());
  1381. assertTrue(status.getChanged().isEmpty());
  1382. assertFalse(db.getFS().canExecute(file));
  1383. recorder.assertEvent(new String[] { "file.txt" },
  1384. ChangeRecorder.EMPTY);
  1385. } finally {
  1386. if (handle != null) {
  1387. handle.remove();
  1388. }
  1389. }
  1390. }
  1391. @Test
  1392. public void testFileModeChangeAndContentChangeConflict() throws Exception {
  1393. if (!FS.DETECTED.supportsExecute())
  1394. return;
  1395. ChangeRecorder recorder = new ChangeRecorder();
  1396. ListenerHandle handle = null;
  1397. try (Git git = new Git(db)) {
  1398. handle = db.getListenerList()
  1399. .addWorkingTreeModifiedListener(recorder);
  1400. // Add non-executable file
  1401. File file = writeTrashFile("file.txt", "a");
  1402. git.add().addFilepattern("file.txt").call();
  1403. git.commit().setMessage("commit1").call();
  1404. assertFalse(db.getFS().canExecute(file));
  1405. // Create branch
  1406. git.branchCreate().setName("b1").call();
  1407. // Make file executable
  1408. db.getFS().setExecute(file, true);
  1409. git.add().addFilepattern("file.txt").call();
  1410. git.commit().setMessage("commit2").call();
  1411. // Verify executable and working directory is clean
  1412. Status status = git.status().call();
  1413. assertTrue(status.getModified().isEmpty());
  1414. assertTrue(status.getChanged().isEmpty());
  1415. assertTrue(db.getFS().canExecute(file));
  1416. writeTrashFile("file.txt", "b");
  1417. // Switch branches
  1418. CheckoutCommand checkout = git.checkout().setName("b1");
  1419. try {
  1420. checkout.call();
  1421. fail("Checkout exception not thrown");
  1422. } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) {
  1423. CheckoutResult result = checkout.getResult();
  1424. assertNotNull(result);
  1425. assertNotNull(result.getConflictList());
  1426. assertEquals(1, result.getConflictList().size());
  1427. assertTrue(result.getConflictList().contains("file.txt"));
  1428. }
  1429. recorder.assertNoEvent();
  1430. } finally {
  1431. if (handle != null) {
  1432. handle.remove();
  1433. }
  1434. }
  1435. }
  1436. @Test
  1437. public void testDirtyFileModeEqualHeadMerge()
  1438. throws Exception {
  1439. if (!FS.DETECTED.supportsExecute())
  1440. return;
  1441. ChangeRecorder recorder = new ChangeRecorder();
  1442. ListenerHandle handle = null;
  1443. try (Git git = new Git(db)) {
  1444. handle = db.getListenerList()
  1445. .addWorkingTreeModifiedListener(recorder);
  1446. // Add non-executable file
  1447. File file = writeTrashFile("file.txt", "a");
  1448. git.add().addFilepattern("file.txt").call();
  1449. git.commit().setMessage("commit1").call();
  1450. assertFalse(db.getFS().canExecute(file));
  1451. // Create branch
  1452. git.branchCreate().setName("b1").call();
  1453. // Create second commit and don't touch file
  1454. writeTrashFile("file2.txt", "");
  1455. git.add().addFilepattern("file2.txt").call();
  1456. git.commit().setMessage("commit2").call();
  1457. // stage a mode change
  1458. writeTrashFile("file.txt", "a");
  1459. db.getFS().setExecute(file, true);
  1460. git.add().addFilepattern("file.txt").call();
  1461. // dirty the file
  1462. writeTrashFile("file.txt", "b");
  1463. assertEquals(
  1464. "[file.txt, mode:100755, content:a][file2.txt, mode:100644, content:]",
  1465. indexState(CONTENT));
  1466. assertWorkDir(mkmap("file.txt", "b", "file2.txt", ""));
  1467. recorder.assertNoEvent();
  1468. // Switch branches and check that the dirty file survived in
  1469. // worktree and index
  1470. git.checkout().setName("b1").call();
  1471. assertEquals("[file.txt, mode:100755, content:a]",
  1472. indexState(CONTENT));
  1473. assertWorkDir(mkmap("file.txt", "b"));
  1474. recorder.assertEvent(ChangeRecorder.EMPTY,
  1475. new String[] { "file2.txt" });
  1476. } finally {
  1477. if (handle != null) {
  1478. handle.remove();
  1479. }
  1480. }
  1481. }
  1482. @Test
  1483. public void testDirtyFileModeEqualIndexMerge()
  1484. throws Exception {
  1485. if (!FS.DETECTED.supportsExecute())
  1486. return;
  1487. ChangeRecorder recorder = new ChangeRecorder();
  1488. ListenerHandle handle = null;
  1489. try (Git git = new Git(db)) {
  1490. handle = db.getListenerList()
  1491. .addWorkingTreeModifiedListener(recorder);
  1492. // Add non-executable file
  1493. File file = writeTrashFile("file.txt", "a");
  1494. git.add().addFilepattern("file.txt").call();
  1495. git.commit().setMessage("commit1").call();
  1496. assertFalse(db.getFS().canExecute(file));
  1497. // Create branch
  1498. git.branchCreate().setName("b1").call();
  1499. // Create second commit with executable file
  1500. file = writeTrashFile("file.txt", "b");
  1501. db.getFS().setExecute(file, true);
  1502. git.add().addFilepattern("file.txt").call();
  1503. git.commit().setMessage("commit2").call();
  1504. // stage the same content as in the branch we want to switch to
  1505. writeTrashFile("file.txt", "a");
  1506. db.getFS().setExecute(file, false);
  1507. git.add().addFilepattern("file.txt").call();
  1508. // dirty the file
  1509. writeTrashFile("file.txt", "c");
  1510. db.getFS().setExecute(file, true);
  1511. assertEquals("[file.txt, mode:100644, content:a]",
  1512. indexState(CONTENT));
  1513. assertWorkDir(mkmap("file.txt", "c"));
  1514. recorder.assertNoEvent();
  1515. // Switch branches and check that the dirty file survived in
  1516. // worktree
  1517. // and index
  1518. git.checkout().setName("b1").call();
  1519. assertEquals("[file.txt, mode:100644, content:a]",
  1520. indexState(CONTENT));
  1521. assertWorkDir(mkmap("file.txt", "c"));
  1522. recorder.assertNoEvent();
  1523. } finally {
  1524. if (handle != null) {
  1525. handle.remove();
  1526. }
  1527. }
  1528. }
  1529. @Test
  1530. public void testFileModeChangeAndContentChangeNoConflict() throws Exception {
  1531. if (!FS.DETECTED.supportsExecute())
  1532. return;
  1533. ChangeRecorder recorder = new ChangeRecorder();
  1534. ListenerHandle handle = null;
  1535. try (Git git = new Git(db)) {
  1536. handle = db.getListenerList()
  1537. .addWorkingTreeModifiedListener(recorder);
  1538. // Add first file
  1539. File file1 = writeTrashFile("file1.txt", "a");
  1540. git.add().addFilepattern("file1.txt").call();
  1541. git.commit().setMessage("commit1").call();
  1542. assertFalse(db.getFS().canExecute(file1));
  1543. // Add second file
  1544. File file2 = writeTrashFile("file2.txt", "b");
  1545. git.add().addFilepattern("file2.txt").call();
  1546. git.commit().setMessage("commit2").call();
  1547. assertFalse(db.getFS().canExecute(file2));
  1548. recorder.assertNoEvent();
  1549. // Create branch from first commit
  1550. assertNotNull(git.checkout().setCreateBranch(true).setName("b1")
  1551. .setStartPoint(Constants.HEAD + "~1").call());
  1552. recorder.assertEvent(ChangeRecorder.EMPTY,
  1553. new String[] { "file2.txt" });
  1554. // Change content and file mode in working directory and index
  1555. file1 = writeTrashFile("file1.txt", "c");
  1556. db.getFS().setExecute(file1, true);
  1557. git.add().addFilepattern("file1.txt").call();
  1558. // Switch back to 'master'
  1559. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  1560. recorder.assertEvent(new String[] { "file2.txt" },
  1561. ChangeRecorder.EMPTY);
  1562. } finally {
  1563. if (handle != null) {
  1564. handle.remove();
  1565. }
  1566. }
  1567. }
  1568. @Test(expected = CheckoutConflictException.class)
  1569. public void testFolderFileConflict() throws Exception {
  1570. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1571. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1572. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1573. writeTrashFile("f", "file instead of folder");
  1574. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1575. checkoutCommit.getTree()).checkout();
  1576. }
  1577. @Test
  1578. public void testMultipleContentConflicts() throws Exception {
  1579. commitFile("a", "initial content", "master");
  1580. RevCommit headCommit = commitFile("b", "initial content", "master");
  1581. commitFile("a", "side content", "side");
  1582. RevCommit checkoutCommit = commitFile("b", "side content", "side");
  1583. writeTrashFile("a", "changed content");
  1584. writeTrashFile("b", "changed content");
  1585. try {
  1586. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1587. checkoutCommit.getTree()).checkout();
  1588. fail();
  1589. } catch (CheckoutConflictException expected) {
  1590. assertEquals(2, expected.getConflictingFiles().length);
  1591. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1592. .contains("a"));
  1593. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1594. .contains("b"));
  1595. assertEquals("changed content", read("a"));
  1596. assertEquals("changed content", read("b"));
  1597. }
  1598. }
  1599. @Test
  1600. public void testFolderFileAndContentConflicts() throws Exception {
  1601. RevCommit headCommit = commitFile("f/a", "initial content", "master");
  1602. commitFile("b", "side content", "side");
  1603. RevCommit checkoutCommit = commitFile("f/a", "side content", "side");
  1604. FileUtils.delete(new File(db.getWorkTree(), "f"), FileUtils.RECURSIVE);
  1605. writeTrashFile("f", "file instead of a folder");
  1606. writeTrashFile("b", "changed content");
  1607. try {
  1608. new DirCacheCheckout(db, headCommit.getTree(), db.lockDirCache(),
  1609. checkoutCommit.getTree()).checkout();
  1610. fail();
  1611. } catch (CheckoutConflictException expected) {
  1612. assertEquals(2, expected.getConflictingFiles().length);
  1613. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1614. .contains("b"));
  1615. assertTrue(Arrays.asList(expected.getConflictingFiles())
  1616. .contains("f"));
  1617. assertEquals("file instead of a folder", read("f"));
  1618. assertEquals("changed content", read("b"));
  1619. }
  1620. }
  1621. @Test
  1622. public void testLongFilename() throws Exception {
  1623. char[] bytes = new char[253];
  1624. Arrays.fill(bytes, 'f');
  1625. String longFileName = new String(bytes);
  1626. // 1
  1627. doit(mkmap(longFileName, "a"), mkmap(longFileName, "b"),
  1628. mkmap(longFileName, "a"));
  1629. writeTrashFile(longFileName, "a");
  1630. checkout();
  1631. assertNoConflicts();
  1632. assertUpdated(longFileName);
  1633. }
  1634. @Test
  1635. public void testIgnoredDirectory() throws Exception {
  1636. writeTrashFile(".gitignore", "src/ignored");
  1637. writeTrashFile("src/ignored/sub/foo.txt", "1");
  1638. try (Git git = new Git(db)) {
  1639. git.add().addFilepattern(".").call();
  1640. RevCommit commit = git.commit().setMessage("adding .gitignore")
  1641. .call();
  1642. writeTrashFile("foo.txt", "2");
  1643. writeTrashFile("zzz.txt", "3");
  1644. git.add().addFilepattern("foo.txt").call();
  1645. git.commit().setMessage("add file").call();
  1646. assertEquals("Should not have entered ignored directory", 1,
  1647. resetHardAndCount(commit));
  1648. }
  1649. }
  1650. @Test
  1651. public void testIgnoredDirectoryWithTrackedContent() throws Exception {
  1652. writeTrashFile("src/ignored/sub/foo.txt", "1");
  1653. try (Git git = new Git(db)) {
  1654. git.add().addFilepattern(".").call();
  1655. git.commit().setMessage("adding foo.txt").call();
  1656. writeTrashFile(".gitignore", "src/ignored");
  1657. writeTrashFile("src/ignored/sub/foo.txt", "2");
  1658. writeTrashFile("src/ignored/other/bar.txt", "3");
  1659. git.add().addFilepattern(".").call();
  1660. RevCommit commit = git.commit().setMessage("adding .gitignore")
  1661. .call();
  1662. writeTrashFile("foo.txt", "2");
  1663. writeTrashFile("zzz.txt", "3");
  1664. git.add().addFilepattern("foo.txt").call();
  1665. git.commit().setMessage("add file").call();
  1666. File file = writeTrashFile("src/ignored/sub/foo.txt", "3");
  1667. assertEquals("Should have entered ignored directory", 3,
  1668. resetHardAndCount(commit));
  1669. checkFile(file, "2");
  1670. }
  1671. }
  1672. @Test
  1673. public void testResetWithChangeInGitignore() throws Exception {
  1674. writeTrashFile(".gitignore", "src/ignored");
  1675. writeTrashFile("src/ignored/sub/foo.txt", "1");
  1676. try (Git git = new Git(db)) {
  1677. git.add().addFilepattern(".").call();
  1678. RevCommit initial = git.commit().setMessage("initial").call();
  1679. writeTrashFile("src/newignored/foo.txt", "2");
  1680. writeTrashFile("src/.gitignore", "newignored");
  1681. git.add().addFilepattern(".").call();
  1682. RevCommit commit = git.commit().setMessage("newignored").call();
  1683. assertEquals("Should not have entered src/newignored directory", 1,
  1684. resetHardAndCount(initial));
  1685. assertEquals("Should have entered src/newignored directory", 2,
  1686. resetHardAndCount(commit));
  1687. deleteTrashFile("src/.gitignore");
  1688. git.rm().addFilepattern("src/.gitignore").call();
  1689. RevCommit top = git.commit().setMessage("Unignore newignore")
  1690. .call();
  1691. assertEquals("Should have entered src/newignored directory", 2,
  1692. resetHardAndCount(initial));
  1693. assertEquals("Should have entered src/newignored directory", 2,
  1694. resetHardAndCount(commit));
  1695. assertEquals("Should not have entered src/newignored directory", 1,
  1696. resetHardAndCount(top));
  1697. }
  1698. }
  1699. private static class TestFileTreeIterator extends FileTreeIterator {
  1700. // For assertions only
  1701. private final int[] count;
  1702. public TestFileTreeIterator(Repository repo, int[] count) {
  1703. super(repo);
  1704. this.count = count;
  1705. }
  1706. protected TestFileTreeIterator(final WorkingTreeIterator p,
  1707. final File root, FS fs, FileModeStrategy fileModeStrategy,
  1708. int[] count) {
  1709. super(p, root, fs, fileModeStrategy);
  1710. this.count = count;
  1711. }
  1712. @Override
  1713. protected AbstractTreeIterator enterSubtree() {
  1714. count[0] += 1;
  1715. return new TestFileTreeIterator(this,
  1716. ((FileEntry) current()).getFile(), fs, fileModeStrategy,
  1717. count);
  1718. }
  1719. }
  1720. private int resetHardAndCount(RevCommit commit) throws Exception {
  1721. int[] callCount = { 0 };
  1722. DirCache cache = db.lockDirCache();
  1723. FileTreeIterator workingTreeIterator = new TestFileTreeIterator(db,
  1724. callCount);
  1725. try {
  1726. DirCacheCheckout checkout = new DirCacheCheckout(db, null, cache,
  1727. commit.getTree().getId(), workingTreeIterator);
  1728. checkout.setFailOnConflict(false);
  1729. checkout.checkout();
  1730. } finally {
  1731. cache.unlock();
  1732. }
  1733. return callCount[0];
  1734. }
  1735. public void assertWorkDir(Map<String, String> i)
  1736. throws CorruptObjectException,
  1737. IOException {
  1738. try (TreeWalk walk = new TreeWalk(db)) {
  1739. walk.setRecursive(false);
  1740. walk.addTree(new FileTreeIterator(db));
  1741. String expectedValue;
  1742. String path;
  1743. int nrFiles = 0;
  1744. FileTreeIterator ft;
  1745. while (walk.next()) {
  1746. ft = walk.getTree(0, FileTreeIterator.class);
  1747. path = ft.getEntryPathString();
  1748. expectedValue = i.get(path);
  1749. File file = new File(db.getWorkTree(), path);
  1750. assertTrue(file.exists());
  1751. if (file.isFile()) {
  1752. assertNotNull("found unexpected file for path " + path
  1753. + " in workdir", expectedValue);
  1754. try (FileInputStream is = new FileInputStream(file)) {
  1755. byte[] buffer = new byte[(int) file.length()];
  1756. int offset = 0;
  1757. int numRead = 0;
  1758. while (offset < buffer.length
  1759. && (numRead = is.read(buffer, offset,
  1760. buffer.length - offset)) >= 0) {
  1761. offset += numRead;
  1762. }
  1763. assertArrayEquals(
  1764. "unexpected content for path " + path
  1765. + " in workDir. ",
  1766. buffer, i.get(path).getBytes(UTF_8));
  1767. }
  1768. nrFiles++;
  1769. } else if (file.isDirectory()) {
  1770. String[] files = file.list();
  1771. if (files != null && files.length == 0) {
  1772. assertEquals("found unexpected empty folder for path "
  1773. + path + " in workDir. ", "/", i.get(path));
  1774. nrFiles++;
  1775. }
  1776. }
  1777. if (walk.isSubtree()) {
  1778. walk.enterSubtree();
  1779. }
  1780. }
  1781. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  1782. }
  1783. }
  1784. }