Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DirCacheCheckoutTest.java 50KB

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