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.

ResolveMergerTest.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (C) 2012, Robin Stocker <robin@nibor.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.IOException;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.api.MergeResult;
  51. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  52. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.JGitInternalException;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.errors.NoMergeBaseException;
  57. import org.eclipse.jgit.errors.NoMergeBaseException.MergeBaseFailureReason;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.treewalk.FileTreeIterator;
  62. import org.eclipse.jgit.util.FileUtils;
  63. import org.junit.Assert;
  64. import org.junit.experimental.theories.DataPoint;
  65. import org.junit.experimental.theories.Theories;
  66. import org.junit.experimental.theories.Theory;
  67. import org.junit.runner.RunWith;
  68. @RunWith(Theories.class)
  69. public class ResolveMergerTest extends RepositoryTestCase {
  70. @DataPoint
  71. public static MergeStrategy resolve = MergeStrategy.RESOLVE;
  72. @DataPoint
  73. public static MergeStrategy recursive = MergeStrategy.RECURSIVE;
  74. @Theory
  75. public void failingDeleteOfDirectoryWithUntrackedContent(
  76. MergeStrategy strategy) throws Exception {
  77. File folder1 = new File(db.getWorkTree(), "folder1");
  78. FileUtils.mkdir(folder1);
  79. File file = new File(folder1, "file1.txt");
  80. write(file, "folder1--file1.txt");
  81. file = new File(folder1, "file2.txt");
  82. write(file, "folder1--file2.txt");
  83. Git git = new Git(db);
  84. git.add().addFilepattern(folder1.getName()).call();
  85. RevCommit base = git.commit().setMessage("adding folder").call();
  86. recursiveDelete(folder1);
  87. git.rm().addFilepattern("folder1/file1.txt")
  88. .addFilepattern("folder1/file2.txt").call();
  89. RevCommit other = git.commit()
  90. .setMessage("removing folders on 'other'").call();
  91. git.checkout().setName(base.name()).call();
  92. file = new File(db.getWorkTree(), "unrelated.txt");
  93. write(file, "unrelated");
  94. git.add().addFilepattern("unrelated.txt").call();
  95. RevCommit head = git.commit().setMessage("Adding another file").call();
  96. // Untracked file to cause failing path for delete() of folder1
  97. // but that's ok.
  98. file = new File(folder1, "file3.txt");
  99. write(file, "folder1--file3.txt");
  100. ResolveMerger merger = (ResolveMerger) strategy.newMerger(db, false);
  101. merger.setCommitNames(new String[] { "BASE", "HEAD", "other" });
  102. merger.setWorkingTreeIterator(new FileTreeIterator(db));
  103. boolean ok = merger.merge(head.getId(), other.getId());
  104. assertTrue(ok);
  105. assertTrue(file.exists());
  106. }
  107. /**
  108. * Merging two conflicting subtrees when the index does not contain any file
  109. * in that subtree should lead to a conflicting state.
  110. *
  111. * @param strategy
  112. * @throws Exception
  113. */
  114. @Theory
  115. public void checkMergeConflictingTreesWithoutIndex(MergeStrategy strategy)
  116. throws Exception {
  117. Git git = Git.wrap(db);
  118. writeTrashFile("d/1", "orig");
  119. git.add().addFilepattern("d/1").call();
  120. RevCommit first = git.commit().setMessage("added d/1").call();
  121. writeTrashFile("d/1", "master");
  122. RevCommit masterCommit = git.commit().setAll(true)
  123. .setMessage("modified d/1 on master").call();
  124. git.checkout().setCreateBranch(true).setStartPoint(first)
  125. .setName("side").call();
  126. writeTrashFile("d/1", "side");
  127. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  128. git.rm().addFilepattern("d/1").call();
  129. git.rm().addFilepattern("d").call();
  130. MergeResult mergeRes = git.merge().setStrategy(strategy)
  131. .include(masterCommit).call();
  132. assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
  133. assertEquals(
  134. "[d/1, mode:100644, stage:1, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
  135. indexState(CONTENT));
  136. }
  137. /**
  138. * Merging two different but mergeable subtrees when the index does not
  139. * contain any file in that subtree should lead to a merged state.
  140. *
  141. * @param strategy
  142. * @throws Exception
  143. */
  144. @Theory
  145. public void checkMergeMergeableTreesWithoutIndex(MergeStrategy strategy)
  146. throws Exception {
  147. Git git = Git.wrap(db);
  148. writeTrashFile("d/1", "1\n2\n3");
  149. git.add().addFilepattern("d/1").call();
  150. RevCommit first = git.commit().setMessage("added d/1").call();
  151. writeTrashFile("d/1", "1master\n2\n3");
  152. RevCommit masterCommit = git.commit().setAll(true)
  153. .setMessage("modified d/1 on master").call();
  154. git.checkout().setCreateBranch(true).setStartPoint(first)
  155. .setName("side").call();
  156. writeTrashFile("d/1", "1\n2\n3side");
  157. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  158. git.rm().addFilepattern("d/1").call();
  159. git.rm().addFilepattern("d").call();
  160. MergeResult mergeRes = git.merge().setStrategy(strategy)
  161. .include(masterCommit).call();
  162. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  163. assertEquals("[d/1, mode:100644, content:1master\n2\n3side\n]",
  164. indexState(CONTENT));
  165. }
  166. /**
  167. * An existing directory without tracked content should not prevent merging
  168. * a tree where that directory exists.
  169. *
  170. * @param strategy
  171. * @throws Exception
  172. */
  173. @Theory
  174. public void checkUntrackedFolderIsNotAConflict(
  175. MergeStrategy strategy) throws Exception {
  176. Git git = Git.wrap(db);
  177. writeTrashFile("d/1", "1");
  178. git.add().addFilepattern("d/1").call();
  179. RevCommit first = git.commit().setMessage("added d/1").call();
  180. writeTrashFile("e/1", "4");
  181. git.add().addFilepattern("e/1").call();
  182. RevCommit masterCommit = git.commit().setMessage("added e/1").call();
  183. git.checkout().setCreateBranch(true).setStartPoint(first)
  184. .setName("side").call();
  185. writeTrashFile("f/1", "5");
  186. git.add().addFilepattern("f/1").call();
  187. git.commit().setAll(true).setMessage("added f/1")
  188. .call();
  189. // Untracked directory e shall not conflict with merged e/1
  190. writeTrashFile("e/2", "d two");
  191. MergeResult mergeRes = git.merge().setStrategy(strategy)
  192. .include(masterCommit).call();
  193. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  194. assertEquals(
  195. "[d/1, mode:100644, content:1][e/1, mode:100644, content:4][f/1, mode:100644, content:5]",
  196. indexState(CONTENT));
  197. }
  198. /**
  199. * An existing directory without tracked content should not prevent merging
  200. * a file with that name.
  201. *
  202. * @param strategy
  203. * @throws Exception
  204. */
  205. @Theory
  206. public void checkUntrackedEmpytFolderIsNotAConflictWithFile(
  207. MergeStrategy strategy)
  208. throws Exception {
  209. Git git = Git.wrap(db);
  210. writeTrashFile("d/1", "1");
  211. git.add().addFilepattern("d/1").call();
  212. RevCommit first = git.commit().setMessage("added d/1").call();
  213. writeTrashFile("e", "4");
  214. git.add().addFilepattern("e").call();
  215. RevCommit masterCommit = git.commit().setMessage("added e").call();
  216. git.checkout().setCreateBranch(true).setStartPoint(first)
  217. .setName("side").call();
  218. writeTrashFile("f/1", "5");
  219. git.add().addFilepattern("f/1").call();
  220. git.commit().setAll(true).setMessage("added f/1").call();
  221. // Untracked empty directory hierarcy e/1 shall not conflict with merged
  222. // e/1
  223. FileUtils.mkdirs(new File(trash, "e/1"), true);
  224. MergeResult mergeRes = git.merge().setStrategy(strategy)
  225. .include(masterCommit).call();
  226. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  227. assertEquals(
  228. "[d/1, mode:100644, content:1][e, mode:100644, content:4][f/1, mode:100644, content:5]",
  229. indexState(CONTENT));
  230. }
  231. @Theory
  232. public void mergeWithCrlfInWT(MergeStrategy strategy) throws IOException,
  233. GitAPIException {
  234. Git git = Git.wrap(db);
  235. db.getConfig().setString("core", null, "autocrlf", "false");
  236. db.getConfig().save();
  237. writeTrashFile("crlf.txt", "some\r\ndata\r\n");
  238. git.add().addFilepattern("crlf.txt").call();
  239. git.commit().setMessage("base").call();
  240. git.branchCreate().setName("brancha").call();
  241. writeTrashFile("crlf.txt", "some\r\nmore\r\ndata\r\n");
  242. git.add().addFilepattern("crlf.txt").call();
  243. git.commit().setMessage("on master").call();
  244. git.checkout().setName("brancha").call();
  245. writeTrashFile("crlf.txt", "some\r\ndata\r\ntoo\r\n");
  246. git.add().addFilepattern("crlf.txt").call();
  247. git.commit().setMessage("on brancha").call();
  248. db.getConfig().setString("core", null, "autocrlf", "input");
  249. db.getConfig().save();
  250. MergeResult mergeResult = git.merge().setStrategy(strategy)
  251. .include(db.resolve("master"))
  252. .call();
  253. assertEquals(MergeResult.MergeStatus.MERGED,
  254. mergeResult.getMergeStatus());
  255. }
  256. /**
  257. * Merging two equal subtrees when the index does not contain any file in
  258. * that subtree should lead to a merged state.
  259. *
  260. * @param strategy
  261. * @throws Exception
  262. */
  263. @Theory
  264. public void checkMergeEqualTreesWithoutIndex(MergeStrategy strategy)
  265. throws Exception {
  266. Git git = Git.wrap(db);
  267. writeTrashFile("d/1", "orig");
  268. git.add().addFilepattern("d/1").call();
  269. RevCommit first = git.commit().setMessage("added d/1").call();
  270. writeTrashFile("d/1", "modified");
  271. RevCommit masterCommit = git.commit().setAll(true)
  272. .setMessage("modified d/1 on master").call();
  273. git.checkout().setCreateBranch(true).setStartPoint(first)
  274. .setName("side").call();
  275. writeTrashFile("d/1", "modified");
  276. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  277. git.rm().addFilepattern("d/1").call();
  278. git.rm().addFilepattern("d").call();
  279. MergeResult mergeRes = git.merge().setStrategy(strategy)
  280. .include(masterCommit).call();
  281. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  282. assertEquals("[d/1, mode:100644, content:modified]",
  283. indexState(CONTENT));
  284. }
  285. /**
  286. * Merging two equal subtrees with an incore merger should lead to a merged
  287. * state (The 'Gerrit' use case).
  288. *
  289. * @param strategy
  290. * @throws Exception
  291. */
  292. @Theory
  293. public void checkMergeEqualTreesInCore(MergeStrategy strategy)
  294. throws Exception {
  295. Git git = Git.wrap(db);
  296. writeTrashFile("d/1", "orig");
  297. git.add().addFilepattern("d/1").call();
  298. RevCommit first = git.commit().setMessage("added d/1").call();
  299. writeTrashFile("d/1", "modified");
  300. RevCommit masterCommit = git.commit().setAll(true)
  301. .setMessage("modified d/1 on master").call();
  302. git.checkout().setCreateBranch(true).setStartPoint(first)
  303. .setName("side").call();
  304. writeTrashFile("d/1", "modified");
  305. RevCommit sideCommit = git.commit().setAll(true)
  306. .setMessage("modified d/1 on side").call();
  307. git.rm().addFilepattern("d/1").call();
  308. git.rm().addFilepattern("d").call();
  309. ThreeWayMerger resolveMerger = (ThreeWayMerger) strategy.newMerger(db,
  310. true);
  311. boolean noProblems = resolveMerger.merge(masterCommit, sideCommit);
  312. assertTrue(noProblems);
  313. }
  314. /**
  315. * Merging two equal subtrees when the index and HEAD does not contain any
  316. * file in that subtree should lead to a merged state.
  317. *
  318. * @param strategy
  319. * @throws Exception
  320. */
  321. @Theory
  322. public void checkMergeEqualNewTrees(MergeStrategy strategy)
  323. throws Exception {
  324. Git git = Git.wrap(db);
  325. writeTrashFile("2", "orig");
  326. git.add().addFilepattern("2").call();
  327. RevCommit first = git.commit().setMessage("added 2").call();
  328. writeTrashFile("d/1", "orig");
  329. git.add().addFilepattern("d/1").call();
  330. RevCommit masterCommit = git.commit().setAll(true)
  331. .setMessage("added d/1 on master").call();
  332. git.checkout().setCreateBranch(true).setStartPoint(first)
  333. .setName("side").call();
  334. writeTrashFile("d/1", "orig");
  335. git.add().addFilepattern("d/1").call();
  336. git.commit().setAll(true).setMessage("added d/1 on side").call();
  337. git.rm().addFilepattern("d/1").call();
  338. git.rm().addFilepattern("d").call();
  339. MergeResult mergeRes = git.merge().setStrategy(strategy)
  340. .include(masterCommit).call();
  341. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  342. assertEquals(
  343. "[2, mode:100644, content:orig][d/1, mode:100644, content:orig]",
  344. indexState(CONTENT));
  345. }
  346. /**
  347. * Merging two conflicting subtrees when the index and HEAD does not contain
  348. * any file in that subtree should lead to a conflicting state.
  349. *
  350. * @param strategy
  351. * @throws Exception
  352. */
  353. @Theory
  354. public void checkMergeConflictingNewTrees(MergeStrategy strategy)
  355. throws Exception {
  356. Git git = Git.wrap(db);
  357. writeTrashFile("2", "orig");
  358. git.add().addFilepattern("2").call();
  359. RevCommit first = git.commit().setMessage("added 2").call();
  360. writeTrashFile("d/1", "master");
  361. git.add().addFilepattern("d/1").call();
  362. RevCommit masterCommit = git.commit().setAll(true)
  363. .setMessage("added d/1 on master").call();
  364. git.checkout().setCreateBranch(true).setStartPoint(first)
  365. .setName("side").call();
  366. writeTrashFile("d/1", "side");
  367. git.add().addFilepattern("d/1").call();
  368. git.commit().setAll(true).setMessage("added d/1 on side").call();
  369. git.rm().addFilepattern("d/1").call();
  370. git.rm().addFilepattern("d").call();
  371. MergeResult mergeRes = git.merge().setStrategy(strategy)
  372. .include(masterCommit).call();
  373. assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
  374. assertEquals(
  375. "[2, mode:100644, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
  376. indexState(CONTENT));
  377. }
  378. /**
  379. * Merging two conflicting files when the index contains a tree for that
  380. * path should lead to a failed state.
  381. *
  382. * @param strategy
  383. * @throws Exception
  384. */
  385. @Theory
  386. public void checkMergeConflictingFilesWithTreeInIndex(MergeStrategy strategy)
  387. throws Exception {
  388. Git git = Git.wrap(db);
  389. writeTrashFile("0", "orig");
  390. git.add().addFilepattern("0").call();
  391. RevCommit first = git.commit().setMessage("added 0").call();
  392. writeTrashFile("0", "master");
  393. RevCommit masterCommit = git.commit().setAll(true)
  394. .setMessage("modified 0 on master").call();
  395. git.checkout().setCreateBranch(true).setStartPoint(first)
  396. .setName("side").call();
  397. writeTrashFile("0", "side");
  398. git.commit().setAll(true).setMessage("modified 0 on side").call();
  399. git.rm().addFilepattern("0").call();
  400. writeTrashFile("0/0", "side");
  401. git.add().addFilepattern("0/0").call();
  402. MergeResult mergeRes = git.merge().setStrategy(strategy)
  403. .include(masterCommit).call();
  404. assertEquals(MergeStatus.FAILED, mergeRes.getMergeStatus());
  405. }
  406. /**
  407. * Merging two equal files when the index contains a tree for that path
  408. * should lead to a failed state.
  409. *
  410. * @param strategy
  411. * @throws Exception
  412. */
  413. @Theory
  414. public void checkMergeMergeableFilesWithTreeInIndex(MergeStrategy strategy)
  415. throws Exception {
  416. Git git = Git.wrap(db);
  417. writeTrashFile("0", "orig");
  418. writeTrashFile("1", "1\n2\n3");
  419. git.add().addFilepattern("0").addFilepattern("1").call();
  420. RevCommit first = git.commit().setMessage("added 0, 1").call();
  421. writeTrashFile("1", "1master\n2\n3");
  422. RevCommit masterCommit = git.commit().setAll(true)
  423. .setMessage("modified 1 on master").call();
  424. git.checkout().setCreateBranch(true).setStartPoint(first)
  425. .setName("side").call();
  426. writeTrashFile("1", "1\n2\n3side");
  427. git.commit().setAll(true).setMessage("modified 1 on side").call();
  428. git.rm().addFilepattern("0").call();
  429. writeTrashFile("0/0", "modified");
  430. git.add().addFilepattern("0/0").call();
  431. try {
  432. git.merge().setStrategy(strategy).include(masterCommit).call();
  433. Assert.fail("Didn't get the expected exception");
  434. } catch (CheckoutConflictException e) {
  435. assertEquals(1, e.getConflictingPaths().size());
  436. assertEquals("0/0", e.getConflictingPaths().get(0));
  437. }
  438. }
  439. /**
  440. * Merging after criss-cross merges. In this case we merge together two
  441. * commits which have two equally good common ancestors
  442. *
  443. * @param strategy
  444. * @throws Exception
  445. */
  446. @Theory
  447. public void checkMergeCrissCross(MergeStrategy strategy) throws Exception {
  448. Git git = Git.wrap(db);
  449. writeTrashFile("1", "1\n2\n3");
  450. git.add().addFilepattern("1").call();
  451. RevCommit first = git.commit().setMessage("added 1").call();
  452. writeTrashFile("1", "1master\n2\n3");
  453. RevCommit masterCommit = git.commit().setAll(true)
  454. .setMessage("modified 1 on master").call();
  455. writeTrashFile("1", "1master2\n2\n3");
  456. git.commit().setAll(true)
  457. .setMessage("modified 1 on master again").call();
  458. git.checkout().setCreateBranch(true).setStartPoint(first)
  459. .setName("side").call();
  460. writeTrashFile("1", "1\n2\na\nb\nc\n3side");
  461. RevCommit sideCommit = git.commit().setAll(true)
  462. .setMessage("modified 1 on side").call();
  463. writeTrashFile("1", "1\n2\n3side2");
  464. git.commit().setAll(true)
  465. .setMessage("modified 1 on side again").call();
  466. MergeResult result = git.merge().setStrategy(strategy)
  467. .include(masterCommit).call();
  468. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  469. result.getNewHead();
  470. git.checkout().setName("master").call();
  471. result = git.merge().setStrategy(strategy).include(sideCommit).call();
  472. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  473. // we have two branches which are criss-cross merged. Try to merge the
  474. // tips. This should succeed with RecursiveMerge and fail with
  475. // ResolveMerge
  476. try {
  477. MergeResult mergeResult = git.merge().setStrategy(strategy)
  478. .include(git.getRepository().getRef("refs/heads/side"))
  479. .call();
  480. assertEquals(MergeStrategy.RECURSIVE, strategy);
  481. assertEquals(MergeResult.MergeStatus.MERGED,
  482. mergeResult.getMergeStatus());
  483. assertEquals("1master2\n2\n3side2\n", read("1"));
  484. } catch (JGitInternalException e) {
  485. assertEquals(MergeStrategy.RESOLVE, strategy);
  486. assertTrue(e.getCause() instanceof NoMergeBaseException);
  487. assertEquals(((NoMergeBaseException) e.getCause()).getReason(),
  488. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED);
  489. }
  490. }
  491. @Theory
  492. public void checkLockedFilesToBeDeleted(MergeStrategy strategy)
  493. throws Exception {
  494. Git git = Git.wrap(db);
  495. writeTrashFile("a.txt", "orig");
  496. writeTrashFile("b.txt", "orig");
  497. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  498. RevCommit first = git.commit().setMessage("added a.txt, b.txt").call();
  499. // modify and delete files on the master branch
  500. writeTrashFile("a.txt", "master");
  501. git.rm().addFilepattern("b.txt").call();
  502. RevCommit masterCommit = git.commit()
  503. .setMessage("modified a.txt, deleted b.txt").setAll(true)
  504. .call();
  505. // switch back to a side branch
  506. git.checkout().setCreateBranch(true).setStartPoint(first)
  507. .setName("side").call();
  508. writeTrashFile("c.txt", "side");
  509. git.add().addFilepattern("c.txt").call();
  510. git.commit().setMessage("added c.txt").call();
  511. // Get a handle to the the file so on windows it can't be deleted.
  512. FileInputStream fis = new FileInputStream(new File(db.getWorkTree(),
  513. "b.txt"));
  514. MergeResult mergeRes = git.merge().setStrategy(strategy)
  515. .include(masterCommit).call();
  516. if (mergeRes.getMergeStatus().equals(MergeStatus.FAILED)) {
  517. // probably windows
  518. assertEquals(1, mergeRes.getFailingPaths().size());
  519. assertEquals(MergeFailureReason.COULD_NOT_DELETE, mergeRes
  520. .getFailingPaths().get("b.txt"));
  521. }
  522. assertEquals("[a.txt, mode:100644, content:master]"
  523. + "[c.txt, mode:100644, content:side]", indexState(CONTENT));
  524. fis.close();
  525. }
  526. @Theory
  527. public void checkForCorrectIndex(MergeStrategy strategy) throws Exception {
  528. File f;
  529. long lastTs4, lastTsIndex;
  530. Git git = Git.wrap(db);
  531. File indexFile = db.getIndexFile();
  532. // Create initial content and remember when the last file was written.
  533. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  534. lastTs4 = f.lastModified();
  535. // add all files, commit and check this doesn't update any working tree
  536. // files and that the index is in a new file system timer tick. Make
  537. // sure to wait long enough before adding so the index doesn't contain
  538. // racily clean entries
  539. fsTick(f);
  540. git.add().addFilepattern(".").call();
  541. RevCommit firstCommit = git.commit().setMessage("initial commit")
  542. .call();
  543. checkConsistentLastModified("0", "1", "2", "3", "4");
  544. checkModificationTimeStampOrder("1", "2", "3", "4", "<.git/index");
  545. assertEquals("Commit should not touch working tree file 4", lastTs4,
  546. new File(db.getWorkTree(), "4").lastModified());
  547. lastTsIndex = indexFile.lastModified();
  548. // Do modifications on the master branch. Then add and commit. This
  549. // should touch only "0", "2 and "3"
  550. fsTick(indexFile);
  551. f = writeTrashFiles(false, "master", null, "1master\n2\n3", "master",
  552. null);
  553. fsTick(f);
  554. git.add().addFilepattern(".").call();
  555. RevCommit masterCommit = git.commit().setMessage("master commit")
  556. .call();
  557. checkConsistentLastModified("0", "1", "2", "3", "4");
  558. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  559. + lastTsIndex, "<0", "2", "3", "<.git/index");
  560. lastTsIndex = indexFile.lastModified();
  561. // Checkout a side branch. This should touch only "0", "2 and "3"
  562. fsTick(indexFile);
  563. git.checkout().setCreateBranch(true).setStartPoint(firstCommit)
  564. .setName("side").call();
  565. checkConsistentLastModified("0", "1", "2", "3", "4");
  566. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  567. + lastTsIndex, "<0", "2", "3", ".git/index");
  568. lastTsIndex = indexFile.lastModified();
  569. // This checkout may have populated worktree and index so fast that we
  570. // may have smudged entries now. Check that we have the right content
  571. // and then rewrite the index to get rid of smudged state
  572. assertEquals("[0, mode:100644, content:orig]" //
  573. + "[1, mode:100644, content:orig]" //
  574. + "[2, mode:100644, content:1\n2\n3]" //
  575. + "[3, mode:100644, content:orig]" //
  576. + "[4, mode:100644, content:orig]", //
  577. indexState(CONTENT));
  578. fsTick(indexFile);
  579. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  580. lastTs4 = f.lastModified();
  581. fsTick(f);
  582. git.add().addFilepattern(".").call();
  583. checkConsistentLastModified("0", "1", "2", "3", "4");
  584. checkModificationTimeStampOrder("*" + lastTsIndex, "<0", "1", "2", "3",
  585. "4", "<.git/index");
  586. lastTsIndex = indexFile.lastModified();
  587. // Do modifications on the side branch. Touch only "1", "2 and "3"
  588. fsTick(indexFile);
  589. f = writeTrashFiles(false, null, "side", "1\n2\n3side", "side", null);
  590. fsTick(f);
  591. git.add().addFilepattern(".").call();
  592. git.commit().setMessage("side commit").call();
  593. checkConsistentLastModified("0", "1", "2", "3", "4");
  594. checkModificationTimeStampOrder("0", "4", "*" + lastTs4, "<*"
  595. + lastTsIndex, "<1", "2", "3", "<.git/index");
  596. lastTsIndex = indexFile.lastModified();
  597. // merge master and side. Should only touch "0," "2" and "3"
  598. fsTick(indexFile);
  599. git.merge().setStrategy(strategy).include(masterCommit).call();
  600. checkConsistentLastModified("0", "1", "2", "4");
  601. checkModificationTimeStampOrder("4", "*" + lastTs4, "<1", "<*"
  602. + lastTsIndex, "<0", "2", "3", ".git/index");
  603. assertEquals(
  604. "[0, mode:100644, content:master]" //
  605. + "[1, mode:100644, content:side]" //
  606. + "[2, mode:100644, content:1master\n2\n3side\n]" //
  607. + "[3, mode:100644, stage:1, content:orig][3, mode:100644, stage:2, content:side][3, mode:100644, stage:3, content:master]" //
  608. + "[4, mode:100644, content:orig]", //
  609. indexState(CONTENT));
  610. }
  611. // Assert that every specified index entry has the same last modification
  612. // timestamp as the associated file
  613. private void checkConsistentLastModified(String... pathes)
  614. throws IOException {
  615. DirCache dc = db.readDirCache();
  616. File workTree = db.getWorkTree();
  617. for (String path : pathes)
  618. assertEquals(
  619. "IndexEntry with path "
  620. + path
  621. + " has lastmodified with is different from the worktree file",
  622. new File(workTree, path).lastModified(), dc.getEntry(path)
  623. .getLastModified());
  624. }
  625. // Assert that modification timestamps of working tree files are as
  626. // expected. You may specify n files. It is asserted that every file
  627. // i+1 is not older than file i. If a path of file i+1 is prefixed with "<"
  628. // then this file must be younger then file i. A path "*<modtime>"
  629. // represents a file with a modification time of <modtime>
  630. // E.g. ("a", "b", "<c", "f/a.txt") means: a<=b<c<=f/a.txt
  631. private void checkModificationTimeStampOrder(String... pathes) {
  632. long lastMod = Long.MIN_VALUE;
  633. for (String p : pathes) {
  634. boolean strong = p.startsWith("<");
  635. boolean fixed = p.charAt(strong ? 1 : 0) == '*';
  636. p = p.substring((strong ? 1 : 0) + (fixed ? 1 : 0));
  637. long curMod = fixed ? Long.valueOf(p).longValue() : new File(
  638. db.getWorkTree(), p).lastModified();
  639. if (strong)
  640. assertTrue("path " + p + " is not younger than predecesssor",
  641. curMod > lastMod);
  642. else
  643. assertTrue("path " + p + " is older than predecesssor",
  644. curMod >= lastMod);
  645. }
  646. }
  647. }