Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ResolveMergerTest.java 25KB

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