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

DirCacheCheckoutTest.java 35KB

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