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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. * Merging two equal subtrees when the index does not contain any file in
  167. * that subtree should lead to a merged state.
  168. *
  169. * @param strategy
  170. * @throws Exception
  171. */
  172. @Theory
  173. public void checkMergeEqualTreesWithoutIndex(MergeStrategy strategy)
  174. throws Exception {
  175. Git git = Git.wrap(db);
  176. writeTrashFile("d/1", "orig");
  177. git.add().addFilepattern("d/1").call();
  178. RevCommit first = git.commit().setMessage("added d/1").call();
  179. writeTrashFile("d/1", "modified");
  180. RevCommit masterCommit = git.commit().setAll(true)
  181. .setMessage("modified d/1 on master").call();
  182. git.checkout().setCreateBranch(true).setStartPoint(first)
  183. .setName("side").call();
  184. writeTrashFile("d/1", "modified");
  185. git.commit().setAll(true).setMessage("modified d/1 on side").call();
  186. git.rm().addFilepattern("d/1").call();
  187. git.rm().addFilepattern("d").call();
  188. MergeResult mergeRes = git.merge().setStrategy(strategy)
  189. .include(masterCommit).call();
  190. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  191. assertEquals("[d/1, mode:100644, content:modified]",
  192. indexState(CONTENT));
  193. }
  194. /**
  195. * Merging two equal subtrees with an incore merger should lead to a merged
  196. * state (The 'Gerrit' use case).
  197. *
  198. * @param strategy
  199. * @throws Exception
  200. */
  201. @Theory
  202. public void checkMergeEqualTreesInCore(MergeStrategy strategy)
  203. throws Exception {
  204. Git git = Git.wrap(db);
  205. writeTrashFile("d/1", "orig");
  206. git.add().addFilepattern("d/1").call();
  207. RevCommit first = git.commit().setMessage("added d/1").call();
  208. writeTrashFile("d/1", "modified");
  209. RevCommit masterCommit = git.commit().setAll(true)
  210. .setMessage("modified d/1 on master").call();
  211. git.checkout().setCreateBranch(true).setStartPoint(first)
  212. .setName("side").call();
  213. writeTrashFile("d/1", "modified");
  214. RevCommit sideCommit = git.commit().setAll(true)
  215. .setMessage("modified d/1 on side").call();
  216. git.rm().addFilepattern("d/1").call();
  217. git.rm().addFilepattern("d").call();
  218. ThreeWayMerger resolveMerger = (ThreeWayMerger) strategy.newMerger(db,
  219. true);
  220. boolean noProblems = resolveMerger.merge(masterCommit, sideCommit);
  221. assertTrue(noProblems);
  222. }
  223. /**
  224. * Merging two equal subtrees when the index and HEAD does not contain any
  225. * file in that subtree should lead to a merged state.
  226. *
  227. * @param strategy
  228. * @throws Exception
  229. */
  230. @Theory
  231. public void checkMergeEqualNewTrees(MergeStrategy strategy)
  232. throws Exception {
  233. Git git = Git.wrap(db);
  234. writeTrashFile("2", "orig");
  235. git.add().addFilepattern("2").call();
  236. RevCommit first = git.commit().setMessage("added 2").call();
  237. writeTrashFile("d/1", "orig");
  238. git.add().addFilepattern("d/1").call();
  239. RevCommit masterCommit = git.commit().setAll(true)
  240. .setMessage("added d/1 on master").call();
  241. git.checkout().setCreateBranch(true).setStartPoint(first)
  242. .setName("side").call();
  243. writeTrashFile("d/1", "orig");
  244. git.add().addFilepattern("d/1").call();
  245. git.commit().setAll(true).setMessage("added d/1 on side").call();
  246. git.rm().addFilepattern("d/1").call();
  247. git.rm().addFilepattern("d").call();
  248. MergeResult mergeRes = git.merge().setStrategy(strategy)
  249. .include(masterCommit).call();
  250. assertEquals(MergeStatus.MERGED, mergeRes.getMergeStatus());
  251. assertEquals(
  252. "[2, mode:100644, content:orig][d/1, mode:100644, content:orig]",
  253. indexState(CONTENT));
  254. }
  255. /**
  256. * Merging two conflicting subtrees when the index and HEAD does not contain
  257. * any file in that subtree should lead to a conflicting state.
  258. *
  259. * @param strategy
  260. * @throws Exception
  261. */
  262. @Theory
  263. public void checkMergeConflictingNewTrees(MergeStrategy strategy)
  264. throws Exception {
  265. Git git = Git.wrap(db);
  266. writeTrashFile("2", "orig");
  267. git.add().addFilepattern("2").call();
  268. RevCommit first = git.commit().setMessage("added 2").call();
  269. writeTrashFile("d/1", "master");
  270. git.add().addFilepattern("d/1").call();
  271. RevCommit masterCommit = git.commit().setAll(true)
  272. .setMessage("added d/1 on master").call();
  273. git.checkout().setCreateBranch(true).setStartPoint(first)
  274. .setName("side").call();
  275. writeTrashFile("d/1", "side");
  276. git.add().addFilepattern("d/1").call();
  277. git.commit().setAll(true).setMessage("added d/1 on side").call();
  278. git.rm().addFilepattern("d/1").call();
  279. git.rm().addFilepattern("d").call();
  280. MergeResult mergeRes = git.merge().setStrategy(strategy)
  281. .include(masterCommit).call();
  282. assertEquals(MergeStatus.CONFLICTING, mergeRes.getMergeStatus());
  283. assertEquals(
  284. "[2, mode:100644, content:orig][d/1, mode:100644, stage:2, content:side][d/1, mode:100644, stage:3, content:master]",
  285. indexState(CONTENT));
  286. }
  287. /**
  288. * Merging two conflicting files when the index contains a tree for that
  289. * path should lead to a failed state.
  290. *
  291. * @param strategy
  292. * @throws Exception
  293. */
  294. @Theory
  295. public void checkMergeConflictingFilesWithTreeInIndex(MergeStrategy strategy)
  296. throws Exception {
  297. Git git = Git.wrap(db);
  298. writeTrashFile("0", "orig");
  299. git.add().addFilepattern("0").call();
  300. RevCommit first = git.commit().setMessage("added 0").call();
  301. writeTrashFile("0", "master");
  302. RevCommit masterCommit = git.commit().setAll(true)
  303. .setMessage("modified 0 on master").call();
  304. git.checkout().setCreateBranch(true).setStartPoint(first)
  305. .setName("side").call();
  306. writeTrashFile("0", "side");
  307. git.commit().setAll(true).setMessage("modified 0 on side").call();
  308. git.rm().addFilepattern("0").call();
  309. writeTrashFile("0/0", "side");
  310. git.add().addFilepattern("0/0").call();
  311. MergeResult mergeRes = git.merge().setStrategy(strategy)
  312. .include(masterCommit).call();
  313. assertEquals(MergeStatus.FAILED, mergeRes.getMergeStatus());
  314. }
  315. /**
  316. * Merging two equal files when the index contains a tree for that path
  317. * should lead to a failed state.
  318. *
  319. * @param strategy
  320. * @throws Exception
  321. */
  322. @Theory
  323. public void checkMergeMergeableFilesWithTreeInIndex(MergeStrategy strategy)
  324. throws Exception {
  325. Git git = Git.wrap(db);
  326. writeTrashFile("0", "orig");
  327. writeTrashFile("1", "1\n2\n3");
  328. git.add().addFilepattern("0").addFilepattern("1").call();
  329. RevCommit first = git.commit().setMessage("added 0, 1").call();
  330. writeTrashFile("1", "1master\n2\n3");
  331. RevCommit masterCommit = git.commit().setAll(true)
  332. .setMessage("modified 1 on master").call();
  333. git.checkout().setCreateBranch(true).setStartPoint(first)
  334. .setName("side").call();
  335. writeTrashFile("1", "1\n2\n3side");
  336. git.commit().setAll(true).setMessage("modified 1 on side").call();
  337. git.rm().addFilepattern("0").call();
  338. writeTrashFile("0/0", "modified");
  339. git.add().addFilepattern("0/0").call();
  340. try {
  341. git.merge().setStrategy(strategy).include(masterCommit).call();
  342. Assert.fail("Didn't get the expected exception");
  343. } catch (CheckoutConflictException e) {
  344. assertEquals(1, e.getConflictingPaths().size());
  345. assertEquals("0/0", e.getConflictingPaths().get(0));
  346. }
  347. }
  348. /**
  349. * Merging after criss-cross merges. In this case we merge together two
  350. * commits which have two equally good common ancestors
  351. *
  352. * @param strategy
  353. * @throws Exception
  354. */
  355. @Theory
  356. public void checkMergeCrissCross(MergeStrategy strategy) throws Exception {
  357. Git git = Git.wrap(db);
  358. writeTrashFile("1", "1\n2\n3");
  359. git.add().addFilepattern("1").call();
  360. RevCommit first = git.commit().setMessage("added 1").call();
  361. writeTrashFile("1", "1master\n2\n3");
  362. RevCommit masterCommit = git.commit().setAll(true)
  363. .setMessage("modified 1 on master").call();
  364. writeTrashFile("1", "1master2\n2\n3");
  365. git.commit().setAll(true)
  366. .setMessage("modified 1 on master again").call();
  367. git.checkout().setCreateBranch(true).setStartPoint(first)
  368. .setName("side").call();
  369. writeTrashFile("1", "1\n2\na\nb\nc\n3side");
  370. RevCommit sideCommit = git.commit().setAll(true)
  371. .setMessage("modified 1 on side").call();
  372. writeTrashFile("1", "1\n2\n3side2");
  373. git.commit().setAll(true)
  374. .setMessage("modified 1 on side again").call();
  375. MergeResult result = git.merge().setStrategy(strategy)
  376. .include(masterCommit).call();
  377. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  378. result.getNewHead();
  379. git.checkout().setName("master").call();
  380. result = git.merge().setStrategy(strategy).include(sideCommit).call();
  381. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  382. // we have two branches which are criss-cross merged. Try to merge the
  383. // tips. This should succeed with RecursiveMerge and fail with
  384. // ResolveMerge
  385. try {
  386. MergeResult mergeResult = git.merge().setStrategy(strategy)
  387. .include(git.getRepository().getRef("refs/heads/side"))
  388. .call();
  389. assertEquals(MergeStrategy.RECURSIVE, strategy);
  390. assertEquals(MergeResult.MergeStatus.MERGED,
  391. mergeResult.getMergeStatus());
  392. assertEquals("1master2\n2\n3side2\n", read("1"));
  393. } catch (JGitInternalException e) {
  394. assertEquals(MergeStrategy.RESOLVE, strategy);
  395. assertTrue(e.getCause() instanceof NoMergeBaseException);
  396. assertEquals(((NoMergeBaseException) e.getCause()).getReason(),
  397. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED);
  398. }
  399. }
  400. @Theory
  401. public void checkLockedFilesToBeDeleted(MergeStrategy strategy)
  402. throws Exception {
  403. Git git = Git.wrap(db);
  404. writeTrashFile("a.txt", "orig");
  405. writeTrashFile("b.txt", "orig");
  406. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  407. RevCommit first = git.commit().setMessage("added a.txt, b.txt").call();
  408. // modify and delete files on the master branch
  409. writeTrashFile("a.txt", "master");
  410. git.rm().addFilepattern("b.txt").call();
  411. RevCommit masterCommit = git.commit()
  412. .setMessage("modified a.txt, deleted b.txt").setAll(true)
  413. .call();
  414. // switch back to a side branch
  415. git.checkout().setCreateBranch(true).setStartPoint(first)
  416. .setName("side").call();
  417. writeTrashFile("c.txt", "side");
  418. git.add().addFilepattern("c.txt").call();
  419. git.commit().setMessage("added c.txt").call();
  420. // Get a handle to the the file so on windows it can't be deleted.
  421. FileInputStream fis = new FileInputStream(new File(db.getWorkTree(),
  422. "b.txt"));
  423. MergeResult mergeRes = git.merge().setStrategy(strategy)
  424. .include(masterCommit).call();
  425. if (mergeRes.getMergeStatus().equals(MergeStatus.FAILED)) {
  426. // probably windows
  427. assertEquals(1, mergeRes.getFailingPaths().size());
  428. assertEquals(MergeFailureReason.COULD_NOT_DELETE, mergeRes
  429. .getFailingPaths().get("b.txt"));
  430. }
  431. assertEquals("[a.txt, mode:100644, content:master]"
  432. + "[c.txt, mode:100644, content:side]", indexState(CONTENT));
  433. fis.close();
  434. }
  435. @Theory
  436. public void checkForCorrectIndex(MergeStrategy strategy) throws Exception {
  437. File f;
  438. long lastTs4, lastTsIndex;
  439. Git git = Git.wrap(db);
  440. File indexFile = db.getIndexFile();
  441. // Create initial content and remember when the last file was written.
  442. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  443. lastTs4 = f.lastModified();
  444. // add all files, commit and check this doesn't update any working tree
  445. // files and that the index is in a new file system timer tick. Make
  446. // sure to wait long enough before adding so the index doesn't contain
  447. // racily clean entries
  448. fsTick(f);
  449. git.add().addFilepattern(".").call();
  450. RevCommit firstCommit = git.commit().setMessage("initial commit")
  451. .call();
  452. checkConsistentLastModified("0", "1", "2", "3", "4");
  453. checkModificationTimeStampOrder("1", "2", "3", "4", "<.git/index");
  454. assertEquals("Commit should not touch working tree file 4", lastTs4,
  455. new File(db.getWorkTree(), "4").lastModified());
  456. lastTsIndex = indexFile.lastModified();
  457. // Do modifications on the master branch. Then add and commit. This
  458. // should touch only "0", "2 and "3"
  459. fsTick(indexFile);
  460. f = writeTrashFiles(false, "master", null, "1master\n2\n3", "master",
  461. null);
  462. fsTick(f);
  463. git.add().addFilepattern(".").call();
  464. RevCommit masterCommit = git.commit().setMessage("master commit")
  465. .call();
  466. checkConsistentLastModified("0", "1", "2", "3", "4");
  467. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  468. + lastTsIndex, "<0", "2", "3", "<.git/index");
  469. lastTsIndex = indexFile.lastModified();
  470. // Checkout a side branch. This should touch only "0", "2 and "3"
  471. fsTick(indexFile);
  472. git.checkout().setCreateBranch(true).setStartPoint(firstCommit)
  473. .setName("side").call();
  474. checkConsistentLastModified("0", "1", "2", "3", "4");
  475. checkModificationTimeStampOrder("1", "4", "*" + lastTs4, "<*"
  476. + lastTsIndex, "<0", "2", "3", ".git/index");
  477. lastTsIndex = indexFile.lastModified();
  478. // This checkout may have populated worktree and index so fast that we
  479. // may have smudged entries now. Check that we have the right content
  480. // and then rewrite the index to get rid of smudged state
  481. assertEquals("[0, mode:100644, content:orig]" //
  482. + "[1, mode:100644, content:orig]" //
  483. + "[2, mode:100644, content:1\n2\n3]" //
  484. + "[3, mode:100644, content:orig]" //
  485. + "[4, mode:100644, content:orig]", //
  486. indexState(CONTENT));
  487. fsTick(indexFile);
  488. f = writeTrashFiles(false, "orig", "orig", "1\n2\n3", "orig", "orig");
  489. lastTs4 = f.lastModified();
  490. fsTick(f);
  491. git.add().addFilepattern(".").call();
  492. checkConsistentLastModified("0", "1", "2", "3", "4");
  493. checkModificationTimeStampOrder("*" + lastTsIndex, "<0", "1", "2", "3",
  494. "4", "<.git/index");
  495. lastTsIndex = indexFile.lastModified();
  496. // Do modifications on the side branch. Touch only "1", "2 and "3"
  497. fsTick(indexFile);
  498. f = writeTrashFiles(false, null, "side", "1\n2\n3side", "side", null);
  499. fsTick(f);
  500. git.add().addFilepattern(".").call();
  501. git.commit().setMessage("side commit").call();
  502. checkConsistentLastModified("0", "1", "2", "3", "4");
  503. checkModificationTimeStampOrder("0", "4", "*" + lastTs4, "<*"
  504. + lastTsIndex, "<1", "2", "3", "<.git/index");
  505. lastTsIndex = indexFile.lastModified();
  506. // merge master and side. Should only touch "0," "2" and "3"
  507. fsTick(indexFile);
  508. git.merge().setStrategy(strategy).include(masterCommit).call();
  509. checkConsistentLastModified("0", "1", "2", "4");
  510. checkModificationTimeStampOrder("4", "*" + lastTs4, "<1", "<*"
  511. + lastTsIndex, "<0", "2", "3", ".git/index");
  512. assertEquals(
  513. "[0, mode:100644, content:master]" //
  514. + "[1, mode:100644, content:side]" //
  515. + "[2, mode:100644, content:1master\n2\n3side\n]" //
  516. + "[3, mode:100644, stage:1, content:orig][3, mode:100644, stage:2, content:side][3, mode:100644, stage:3, content:master]" //
  517. + "[4, mode:100644, content:orig]", //
  518. indexState(CONTENT));
  519. }
  520. // Assert that every specified index entry has the same last modification
  521. // timestamp as the associated file
  522. private void checkConsistentLastModified(String... pathes)
  523. throws IOException {
  524. DirCache dc = db.readDirCache();
  525. File workTree = db.getWorkTree();
  526. for (String path : pathes)
  527. assertEquals(
  528. "IndexEntry with path "
  529. + path
  530. + " has lastmodified with is different from the worktree file",
  531. new File(workTree, path).lastModified(), dc.getEntry(path)
  532. .getLastModified());
  533. }
  534. // Assert that modification timestamps of working tree files are as
  535. // expected. You may specify n files. It is asserted that every file
  536. // i+1 is not older than file i. If a path of file i+1 is prefixed with "<"
  537. // then this file must be younger then file i. A path "*<modtime>"
  538. // represents a file with a modification time of <modtime>
  539. // E.g. ("a", "b", "<c", "f/a.txt") means: a<=b<c<=f/a.txt
  540. private void checkModificationTimeStampOrder(String... pathes) {
  541. long lastMod = Long.MIN_VALUE;
  542. for (String p : pathes) {
  543. boolean strong = p.startsWith("<");
  544. boolean fixed = p.charAt(strong ? 1 : 0) == '*';
  545. p = p.substring((strong ? 1 : 0) + (fixed ? 1 : 0));
  546. long curMod = fixed ? Long.valueOf(p).longValue() : new File(
  547. db.getWorkTree(), p).lastModified();
  548. if (strong)
  549. assertTrue("path " + p + " is not younger than predecesssor",
  550. curMod > lastMod);
  551. else
  552. assertTrue("path " + p + " is older than predecesssor",
  553. curMod >= lastMod);
  554. }
  555. }
  556. }