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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. try (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. /**
  109. * Merging two conflicting subtrees when the index does not contain any file
  110. * in that subtree should lead to a conflicting state.
  111. *
  112. * @param strategy
  113. * @throws Exception
  114. */
  115. @Theory
  116. public void checkMergeConflictingTreesWithoutIndex(MergeStrategy strategy)
  117. throws Exception {
  118. Git git = Git.wrap(db);
  119. writeTrashFile("d/1", "orig");
  120. git.add().addFilepattern("d/1").call();
  121. RevCommit first = git.commit().setMessage("added d/1").call();
  122. writeTrashFile("d/1", "master");
  123. RevCommit masterCommit = git.commit().setAll(true)
  124. .setMessage("modified d/1 on master").call();
  125. git.checkout().setCreateBranch(true).setStartPoint(first)
  126. .setName("side").call();
  127. writeTrashFile("d/1", "side");
  128. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  129. git.rm().addFilepattern("d/1").call();
  130. git.rm().addFilepattern("d").call();
  131. MergeResult mergeRes = git.merge().setStrategy(strategy)
  132. .include(masterCommit).call();
  133. assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
  134. assertEquals(
  135. "[d/1, mode:100644, stage:1, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
  136. indexState(CONTENT));
  137. }
  138. /**
  139. * Merging two different but mergeable subtrees when the index does not
  140. * contain any file in that subtree should lead to a merged state.
  141. *
  142. * @param strategy
  143. * @throws Exception
  144. */
  145. @Theory
  146. public void checkMergeMergeableTreesWithoutIndex(MergeStrategy strategy)
  147. throws Exception {
  148. Git git = Git.wrap(db);
  149. writeTrashFile("d/1", "1\n2\n3");
  150. git.add().addFilepattern("d/1").call();
  151. RevCommit first = git.commit().setMessage("added d/1").call();
  152. writeTrashFile("d/1", "1master\n2\n3");
  153. RevCommit masterCommit = git.commit().setAll(true)
  154. .setMessage("modified d/1 on master").call();
  155. git.checkout().setCreateBranch(true).setStartPoint(first)
  156. .setName("side").call();
  157. writeTrashFile("d/1", "1\n2\n3side");
  158. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  159. git.rm().addFilepattern("d/1").call();
  160. git.rm().addFilepattern("d").call();
  161. MergeResult mergeRes = git.merge().setStrategy(strategy)
  162. .include(masterCommit).call();
  163. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  164. assertEquals("[d/1, mode:100644, content:1master\n2\n3side]",
  165. indexState(CONTENT));
  166. }
  167. /**
  168. * An existing directory without tracked content should not prevent merging
  169. * a tree where that directory exists.
  170. *
  171. * @param strategy
  172. * @throws Exception
  173. */
  174. @Theory
  175. public void checkUntrackedFolderIsNotAConflict(
  176. MergeStrategy strategy) throws Exception {
  177. Git git = Git.wrap(db);
  178. writeTrashFile("d/1", "1");
  179. git.add().addFilepattern("d/1").call();
  180. RevCommit first = git.commit().setMessage("added d/1").call();
  181. writeTrashFile("e/1", "4");
  182. git.add().addFilepattern("e/1").call();
  183. RevCommit masterCommit = git.commit().setMessage("added e/1").call();
  184. git.checkout().setCreateBranch(true).setStartPoint(first)
  185. .setName("side").call();
  186. writeTrashFile("f/1", "5");
  187. git.add().addFilepattern("f/1").call();
  188. git.commit().setAll(true).setMessage("added f/1")
  189. .call();
  190. // Untracked directory e shall not conflict with merged e/1
  191. writeTrashFile("e/2", "d two");
  192. MergeResult mergeRes = git.merge().setStrategy(strategy)
  193. .include(masterCommit).call();
  194. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  195. assertEquals(
  196. "[d/1, mode:100644, content:1][e/1, mode:100644, content:4][f/1, mode:100644, content:5]",
  197. indexState(CONTENT));
  198. }
  199. /**
  200. * A tracked file is replaced by a folder in THEIRS.
  201. *
  202. * @param strategy
  203. * @throws Exception
  204. */
  205. @Theory
  206. public void checkFileReplacedByFolderInTheirs(MergeStrategy strategy)
  207. throws Exception {
  208. Git git = Git.wrap(db);
  209. writeTrashFile("sub", "file");
  210. git.add().addFilepattern("sub").call();
  211. RevCommit first = git.commit().setMessage("initial").call();
  212. git.checkout().setCreateBranch(true).setStartPoint(first)
  213. .setName("side").call();
  214. git.rm().addFilepattern("sub").call();
  215. writeTrashFile("sub/file", "subfile");
  216. git.add().addFilepattern("sub/file").call();
  217. RevCommit masterCommit = git.commit().setMessage("file -> folder")
  218. .call();
  219. git.checkout().setName("master").call();
  220. writeTrashFile("noop", "other");
  221. git.add().addFilepattern("noop").call();
  222. git.commit().setAll(true).setMessage("noop").call();
  223. MergeResult mergeRes = git.merge().setStrategy(strategy)
  224. .include(masterCommit).call();
  225. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  226. assertEquals(
  227. "[noop, mode:100644, content:other][sub/file, mode:100644, content:subfile]",
  228. indexState(CONTENT));
  229. }
  230. /**
  231. * A tracked file is replaced by a folder in OURS.
  232. *
  233. * @param strategy
  234. * @throws Exception
  235. */
  236. @Theory
  237. public void checkFileReplacedByFolderInOurs(MergeStrategy strategy)
  238. throws Exception {
  239. Git git = Git.wrap(db);
  240. writeTrashFile("sub", "file");
  241. git.add().addFilepattern("sub").call();
  242. RevCommit first = git.commit().setMessage("initial").call();
  243. git.checkout().setCreateBranch(true).setStartPoint(first)
  244. .setName("side").call();
  245. writeTrashFile("noop", "other");
  246. git.add().addFilepattern("noop").call();
  247. RevCommit sideCommit = git.commit().setAll(true).setMessage("noop")
  248. .call();
  249. git.checkout().setName("master").call();
  250. git.rm().addFilepattern("sub").call();
  251. writeTrashFile("sub/file", "subfile");
  252. git.add().addFilepattern("sub/file").call();
  253. git.commit().setMessage("file -> folder")
  254. .call();
  255. MergeResult mergeRes = git.merge().setStrategy(strategy)
  256. .include(sideCommit).call();
  257. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  258. assertEquals(
  259. "[noop, mode:100644, content:other][sub/file, mode:100644, content:subfile]",
  260. indexState(CONTENT));
  261. }
  262. /**
  263. * An existing directory without tracked content should not prevent merging
  264. * a file with that name.
  265. *
  266. * @param strategy
  267. * @throws Exception
  268. */
  269. @Theory
  270. public void checkUntrackedEmpytFolderIsNotAConflictWithFile(
  271. MergeStrategy strategy)
  272. throws Exception {
  273. Git git = Git.wrap(db);
  274. writeTrashFile("d/1", "1");
  275. git.add().addFilepattern("d/1").call();
  276. RevCommit first = git.commit().setMessage("added d/1").call();
  277. writeTrashFile("e", "4");
  278. git.add().addFilepattern("e").call();
  279. RevCommit masterCommit = git.commit().setMessage("added e").call();
  280. git.checkout().setCreateBranch(true).setStartPoint(first)
  281. .setName("side").call();
  282. writeTrashFile("f/1", "5");
  283. git.add().addFilepattern("f/1").call();
  284. git.commit().setAll(true).setMessage("added f/1").call();
  285. // Untracked empty directory hierarcy e/1 shall not conflict with merged
  286. // e/1
  287. FileUtils.mkdirs(new File(trash, "e/1"), true);
  288. MergeResult mergeRes = git.merge().setStrategy(strategy)
  289. .include(masterCommit).call();
  290. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  291. assertEquals(
  292. "[d/1, mode:100644, content:1][e, mode:100644, content:4][f/1, mode:100644, content:5]",
  293. indexState(CONTENT));
  294. }
  295. @Theory
  296. public void mergeWithCrlfInWT(MergeStrategy strategy) throws IOException,
  297. GitAPIException {
  298. Git git = Git.wrap(db);
  299. db.getConfig().setString("core", null, "autocrlf", "false");
  300. db.getConfig().save();
  301. writeTrashFile("crlf.txt", "some\r\ndata\r\n");
  302. git.add().addFilepattern("crlf.txt").call();
  303. git.commit().setMessage("base").call();
  304. git.branchCreate().setName("brancha").call();
  305. writeTrashFile("crlf.txt", "some\r\nmore\r\ndata\r\n");
  306. git.add().addFilepattern("crlf.txt").call();
  307. git.commit().setMessage("on master").call();
  308. git.checkout().setName("brancha").call();
  309. writeTrashFile("crlf.txt", "some\r\ndata\r\ntoo\r\n");
  310. git.add().addFilepattern("crlf.txt").call();
  311. git.commit().setMessage("on brancha").call();
  312. db.getConfig().setString("core", null, "autocrlf", "input");
  313. db.getConfig().save();
  314. MergeResult mergeResult = git.merge().setStrategy(strategy)
  315. .include(db.resolve("master"))
  316. .call();
  317. assertEquals(MergeResult.MergeStatus.MERGED,
  318. mergeResult.getMergeStatus());
  319. }
  320. /**
  321. * Merging two equal subtrees when the index does not contain any file in
  322. * that subtree should lead to a merged state.
  323. *
  324. * @param strategy
  325. * @throws Exception
  326. */
  327. @Theory
  328. public void checkMergeEqualTreesWithoutIndex(MergeStrategy strategy)
  329. throws Exception {
  330. Git git = Git.wrap(db);
  331. writeTrashFile("d/1", "orig");
  332. git.add().addFilepattern("d/1").call();
  333. RevCommit first = git.commit().setMessage("added d/1").call();
  334. writeTrashFile("d/1", "modified");
  335. RevCommit masterCommit = git.commit().setAll(true)
  336. .setMessage("modified d/1 on master").call();
  337. git.checkout().setCreateBranch(true).setStartPoint(first)
  338. .setName("side").call();
  339. writeTrashFile("d/1", "modified");
  340. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  341. git.rm().addFilepattern("d/1").call();
  342. git.rm().addFilepattern("d").call();
  343. MergeResult mergeRes = git.merge().setStrategy(strategy)
  344. .include(masterCommit).call();
  345. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  346. assertEquals("[d/1, mode:100644, content:modified]",
  347. indexState(CONTENT));
  348. }
  349. /**
  350. * Merging two equal subtrees with an incore merger should lead to a merged
  351. * state (The 'Gerrit' use case).
  352. *
  353. * @param strategy
  354. * @throws Exception
  355. */
  356. @Theory
  357. public void checkMergeEqualTreesInCore(MergeStrategy strategy)
  358. throws Exception {
  359. Git git = Git.wrap(db);
  360. writeTrashFile("d/1", "orig");
  361. git.add().addFilepattern("d/1").call();
  362. RevCommit first = git.commit().setMessage("added d/1").call();
  363. writeTrashFile("d/1", "modified");
  364. RevCommit masterCommit = git.commit().setAll(true)
  365. .setMessage("modified d/1 on master").call();
  366. git.checkout().setCreateBranch(true).setStartPoint(first)
  367. .setName("side").call();
  368. writeTrashFile("d/1", "modified");
  369. RevCommit sideCommit = git.commit().setAll(true)
  370. .setMessage("modified d/1 on side").call();
  371. git.rm().addFilepattern("d/1").call();
  372. git.rm().addFilepattern("d").call();
  373. ThreeWayMerger resolveMerger = (ThreeWayMerger) strategy.newMerger(db,
  374. true);
  375. boolean noProblems = resolveMerger.merge(masterCommit, sideCommit);
  376. assertTrue(noProblems);
  377. }
  378. /**
  379. * Merging two equal subtrees when the index and HEAD does not contain any
  380. * file in that subtree should lead to a merged state.
  381. *
  382. * @param strategy
  383. * @throws Exception
  384. */
  385. @Theory
  386. public void checkMergeEqualNewTrees(MergeStrategy strategy)
  387. throws Exception {
  388. Git git = Git.wrap(db);
  389. writeTrashFile("2", "orig");
  390. git.add().addFilepattern("2").call();
  391. RevCommit first = git.commit().setMessage("added 2").call();
  392. writeTrashFile("d/1", "orig");
  393. git.add().addFilepattern("d/1").call();
  394. RevCommit masterCommit = git.commit().setAll(true)
  395. .setMessage("added d/1 on master").call();
  396. git.checkout().setCreateBranch(true).setStartPoint(first)
  397. .setName("side").call();
  398. writeTrashFile("d/1", "orig");
  399. git.add().addFilepattern("d/1").call();
  400. git.commit().setAll(true).setMessage("added d/1 on side").call();
  401. git.rm().addFilepattern("d/1").call();
  402. git.rm().addFilepattern("d").call();
  403. MergeResult mergeRes = git.merge().setStrategy(strategy)
  404. .include(masterCommit).call();
  405. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  406. assertEquals(
  407. "[2, mode:100644, content:orig][d/1, mode:100644, content:orig]",
  408. indexState(CONTENT));
  409. }
  410. /**
  411. * Merging two conflicting subtrees when the index and HEAD does not contain
  412. * any file in that subtree should lead to a conflicting state.
  413. *
  414. * @param strategy
  415. * @throws Exception
  416. */
  417. @Theory
  418. public void checkMergeConflictingNewTrees(MergeStrategy strategy)
  419. throws Exception {
  420. Git git = Git.wrap(db);
  421. writeTrashFile("2", "orig");
  422. git.add().addFilepattern("2").call();
  423. RevCommit first = git.commit().setMessage("added 2").call();
  424. writeTrashFile("d/1", "master");
  425. git.add().addFilepattern("d/1").call();
  426. RevCommit masterCommit = git.commit().setAll(true)
  427. .setMessage("added d/1 on master").call();
  428. git.checkout().setCreateBranch(true).setStartPoint(first)
  429. .setName("side").call();
  430. writeTrashFile("d/1", "side");
  431. git.add().addFilepattern("d/1").call();
  432. git.commit().setAll(true).setMessage("added d/1 on side").call();
  433. git.rm().addFilepattern("d/1").call();
  434. git.rm().addFilepattern("d").call();
  435. MergeResult mergeRes = git.merge().setStrategy(strategy)
  436. .include(masterCommit).call();
  437. assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
  438. assertEquals(
  439. "[2, mode:100644, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
  440. indexState(CONTENT));
  441. }
  442. /**
  443. * Merging two conflicting files when the index contains a tree for that
  444. * path should lead to a failed state.
  445. *
  446. * @param strategy
  447. * @throws Exception
  448. */
  449. @Theory
  450. public void checkMergeConflictingFilesWithTreeInIndex(MergeStrategy strategy)
  451. throws Exception {
  452. Git git = Git.wrap(db);
  453. writeTrashFile("0", "orig");
  454. git.add().addFilepattern("0").call();
  455. RevCommit first = git.commit().setMessage("added 0").call();
  456. writeTrashFile("0", "master");
  457. RevCommit masterCommit = git.commit().setAll(true)
  458. .setMessage("modified 0 on master").call();
  459. git.checkout().setCreateBranch(true).setStartPoint(first)
  460. .setName("side").call();
  461. writeTrashFile("0", "side");
  462. git.commit().setAll(true).setMessage("modified 0 on side").call();
  463. git.rm().addFilepattern("0").call();
  464. writeTrashFile("0/0", "side");
  465. git.add().addFilepattern("0/0").call();
  466. MergeResult mergeRes = git.merge().setStrategy(strategy)
  467. .include(masterCommit).call();
  468. assertEquals(MergeStatus.FAILED, mergeRes.getMergeStatus());
  469. }
  470. /**
  471. * Merging two equal files when the index contains a tree for that path
  472. * should lead to a failed state.
  473. *
  474. * @param strategy
  475. * @throws Exception
  476. */
  477. @Theory
  478. public void checkMergeMergeableFilesWithTreeInIndex(MergeStrategy strategy)
  479. throws Exception {
  480. Git git = Git.wrap(db);
  481. writeTrashFile("0", "orig");
  482. writeTrashFile("1", "1\n2\n3");
  483. git.add().addFilepattern("0").addFilepattern("1").call();
  484. RevCommit first = git.commit().setMessage("added 0, 1").call();
  485. writeTrashFile("1", "1master\n2\n3");
  486. RevCommit masterCommit = git.commit().setAll(true)
  487. .setMessage("modified 1 on master").call();
  488. git.checkout().setCreateBranch(true).setStartPoint(first)
  489. .setName("side").call();
  490. writeTrashFile("1", "1\n2\n3side");
  491. git.commit().setAll(true).setMessage("modified 1 on side").call();
  492. git.rm().addFilepattern("0").call();
  493. writeTrashFile("0/0", "modified");
  494. git.add().addFilepattern("0/0").call();
  495. try {
  496. git.merge().setStrategy(strategy).include(masterCommit).call();
  497. Assert.fail("Didn't get the expected exception");
  498. } catch (CheckoutConflictException e) {
  499. assertEquals(1, e.getConflictingPaths().size());
  500. assertEquals("0/0", e.getConflictingPaths().get(0));
  501. }
  502. }
  503. /**
  504. * Merging after criss-cross merges. In this case we merge together two
  505. * commits which have two equally good common ancestors
  506. *
  507. * @param strategy
  508. * @throws Exception
  509. */
  510. @Theory
  511. public void checkMergeCrissCross(MergeStrategy strategy) throws Exception {
  512. Git git = Git.wrap(db);
  513. writeTrashFile("1", "1\n2\n3");
  514. git.add().addFilepattern("1").call();
  515. RevCommit first = git.commit().setMessage("added 1").call();
  516. writeTrashFile("1", "1master\n2\n3");
  517. RevCommit masterCommit = git.commit().setAll(true)
  518. .setMessage("modified 1 on master").call();
  519. writeTrashFile("1", "1master2\n2\n3");
  520. git.commit().setAll(true)
  521. .setMessage("modified 1 on master again").call();
  522. git.checkout().setCreateBranch(true).setStartPoint(first)
  523. .setName("side").call();
  524. writeTrashFile("1", "1\n2\na\nb\nc\n3side");
  525. RevCommit sideCommit = git.commit().setAll(true)
  526. .setMessage("modified 1 on side").call();
  527. writeTrashFile("1", "1\n2\n3side2");
  528. git.commit().setAll(true)
  529. .setMessage("modified 1 on side again").call();
  530. MergeResult result = git.merge().setStrategy(strategy)
  531. .include(masterCommit).call();
  532. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  533. result.getNewHead();
  534. git.checkout().setName("master").call();
  535. result = git.merge().setStrategy(strategy).include(sideCommit).call();
  536. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  537. // we have two branches which are criss-cross merged. Try to merge the
  538. // tips. This should succeed with RecursiveMerge and fail with
  539. // ResolveMerge
  540. try {
  541. MergeResult mergeResult = git.merge().setStrategy(strategy)
  542. .include(git.getRepository().exactRef("refs/heads/side"))
  543. .call();
  544. assertEquals(MergeStrategy.RECURSIVE, strategy);
  545. assertEquals(MergeResult.MergeStatus.MERGED,
  546. mergeResult.getMergeStatus());
  547. assertEquals("1master2\n2\n3side2", read("1"));
  548. } catch (JGitInternalException e) {
  549. assertEquals(MergeStrategy.RESOLVE, strategy);
  550. assertTrue(e.getCause() instanceof NoMergeBaseException);
  551. assertEquals(((NoMergeBaseException) e.getCause()).getReason(),
  552. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED);
  553. }
  554. }
  555. @Theory
  556. public void checkLockedFilesToBeDeleted(MergeStrategy strategy)
  557. throws Exception {
  558. Git git = Git.wrap(db);
  559. writeTrashFile("a.txt", "orig");
  560. writeTrashFile("b.txt", "orig");
  561. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  562. RevCommit first = git.commit().setMessage("added a.txt, b.txt").call();
  563. // modify and delete files on the master branch
  564. writeTrashFile("a.txt", "master");
  565. git.rm().addFilepattern("b.txt").call();
  566. RevCommit masterCommit = git.commit()
  567. .setMessage("modified a.txt, deleted b.txt").setAll(true)
  568. .call();
  569. // switch back to a side branch
  570. git.checkout().setCreateBranch(true).setStartPoint(first)
  571. .setName("side").call();
  572. writeTrashFile("c.txt", "side");
  573. git.add().addFilepattern("c.txt").call();
  574. git.commit().setMessage("added c.txt").call();
  575. // Get a handle to the the file so on windows it can't be deleted.
  576. FileInputStream fis = new FileInputStream(new File(db.getWorkTree(),
  577. "b.txt"));
  578. MergeResult mergeRes = git.merge().setStrategy(strategy)
  579. .include(masterCommit).call();
  580. if (mergeRes.getMergeStatus().equals(MergeStatus.FAILED)) {
  581. // probably windows
  582. assertEquals(1, mergeRes.getFailingPaths().size());
  583. assertEquals(MergeFailureReason.COULD_NOT_DELETE, mergeRes
  584. .getFailingPaths().get("b.txt"));
  585. }
  586. assertEquals("[a.txt, mode:100644, content:master]"
  587. + "[c.txt, mode:100644, content:side]", indexState(CONTENT));
  588. fis.close();
  589. }
  590. @Theory
  591. public void checkForCorrectIndex(MergeStrategy strategy) throws Exception {
  592. File f;
  593. long lastTs4, lastTsIndex;
  594. Git git = Git.wrap(db);
  595. File indexFile = db.getIndexFile();
  596. // Create initial content and remember when the last file was written.
  597. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  598. lastTs4 = f.lastModified();
  599. // add all files, commit and check this doesn't update any working tree
  600. // files and that the index is in a new file system timer tick. Make
  601. // sure to wait long enough before adding so the index doesn't contain
  602. // racily clean entries
  603. fsTick(f);
  604. git.add().addFilepattern(".").call();
  605. RevCommit firstCommit = git.commit().setMessage("initial commit")
  606. .call();
  607. checkConsistentLastModified("0", "1", "2", "3", "4");
  608. checkModificationTimeStampOrder("1", "2", "3", "4", "<.git/index");
  609. assertEquals("Commit should not touch working tree file 4", lastTs4,
  610. new File(db.getWorkTree(), "4").lastModified());
  611. lastTsIndex = indexFile.lastModified();
  612. // Do modifications on the master branch. Then add and commit. This
  613. // should touch only "0", "2 and "3"
  614. fsTick(indexFile);
  615. f = writeTrashFiles(false, "master", null, "1master\n2\n3", "master",
  616. null);
  617. fsTick(f);
  618. git.add().addFilepattern(".").call();
  619. RevCommit masterCommit = git.commit().setMessage("master commit")
  620. .call();
  621. checkConsistentLastModified("0", "1", "2", "3", "4");
  622. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  623. + lastTsIndex, "<0", "2", "3", "<.git/index");
  624. lastTsIndex = indexFile.lastModified();
  625. // Checkout a side branch. This should touch only "0", "2 and "3"
  626. fsTick(indexFile);
  627. git.checkout().setCreateBranch(true).setStartPoint(firstCommit)
  628. .setName("side").call();
  629. checkConsistentLastModified("0", "1", "2", "3", "4");
  630. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  631. + lastTsIndex, "<0", "2", "3", ".git/index");
  632. lastTsIndex = indexFile.lastModified();
  633. // This checkout may have populated worktree and index so fast that we
  634. // may have smudged entries now. Check that we have the right content
  635. // and then rewrite the index to get rid of smudged state
  636. assertEquals("[0, mode:100644, content:orig]" //
  637. + "[1, mode:100644, content:orig]" //
  638. + "[2, mode:100644, content:1\n2\n3]" //
  639. + "[3, mode:100644, content:orig]" //
  640. + "[4, mode:100644, content:orig]", //
  641. indexState(CONTENT));
  642. fsTick(indexFile);
  643. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  644. lastTs4 = f.lastModified();
  645. fsTick(f);
  646. git.add().addFilepattern(".").call();
  647. checkConsistentLastModified("0", "1", "2", "3", "4");
  648. checkModificationTimeStampOrder("*" + lastTsIndex, "<0", "1", "2", "3",
  649. "4", "<.git/index");
  650. lastTsIndex = indexFile.lastModified();
  651. // Do modifications on the side branch. Touch only "1", "2 and "3"
  652. fsTick(indexFile);
  653. f = writeTrashFiles(false, null, "side", "1\n2\n3side", "side", null);
  654. fsTick(f);
  655. git.add().addFilepattern(".").call();
  656. git.commit().setMessage("side commit").call();
  657. checkConsistentLastModified("0", "1", "2", "3", "4");
  658. checkModificationTimeStampOrder("0", "4", "*" + lastTs4, "<*"
  659. + lastTsIndex, "<1", "2", "3", "<.git/index");
  660. lastTsIndex = indexFile.lastModified();
  661. // merge master and side. Should only touch "0," "2" and "3"
  662. fsTick(indexFile);
  663. git.merge().setStrategy(strategy).include(masterCommit).call();
  664. checkConsistentLastModified("0", "1", "2", "4");
  665. checkModificationTimeStampOrder("4", "*" + lastTs4, "<1", "<*"
  666. + lastTsIndex, "<0", "2", "3", ".git/index");
  667. assertEquals(
  668. "[0, mode:100644, content:master]" //
  669. + "[1, mode:100644, content:side]" //
  670. + "[2, mode:100644, content:1master\n2\n3side]" //
  671. + "[3, mode:100644, stage:1, content:orig][3, mode:100644, stage:2, content:side][3, mode:100644, stage:3, content:master]" //
  672. + "[4, mode:100644, content:orig]", //
  673. indexState(CONTENT));
  674. }
  675. // Assert that every specified index entry has the same last modification
  676. // timestamp as the associated file
  677. private void checkConsistentLastModified(String... pathes)
  678. throws IOException {
  679. DirCache dc = db.readDirCache();
  680. File workTree = db.getWorkTree();
  681. for (String path : pathes)
  682. assertEquals(
  683. "IndexEntry with path "
  684. + path
  685. + " has lastmodified with is different from the worktree file",
  686. new File(workTree, path).lastModified(), dc.getEntry(path)
  687. .getLastModified());
  688. }
  689. // Assert that modification timestamps of working tree files are as
  690. // expected. You may specify n files. It is asserted that every file
  691. // i+1 is not older than file i. If a path of file i+1 is prefixed with "<"
  692. // then this file must be younger then file i. A path "*<modtime>"
  693. // represents a file with a modification time of <modtime>
  694. // E.g. ("a", "b", "<c", "f/a.txt") means: a<=b<c<=f/a.txt
  695. private void checkModificationTimeStampOrder(String... pathes) {
  696. long lastMod = Long.MIN_VALUE;
  697. for (String p : pathes) {
  698. boolean strong = p.startsWith("<");
  699. boolean fixed = p.charAt(strong ? 1 : 0) == '*';
  700. p = p.substring((strong ? 1 : 0) + (fixed ? 1 : 0));
  701. long curMod = fixed ? Long.valueOf(p).longValue() : new File(
  702. db.getWorkTree(), p).lastModified();
  703. if (strong)
  704. assertTrue("path " + p + " is not younger than predecesssor",
  705. curMod > lastMod);
  706. else
  707. assertTrue("path " + p + " is older than predecesssor",
  708. curMod >= lastMod);
  709. }
  710. }
  711. }