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.

CherryPickCommandTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Iterator;
  19. import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
  20. import org.eclipse.jgit.api.ResetCommand.ResetType;
  21. import org.eclipse.jgit.api.errors.GitAPIException;
  22. import org.eclipse.jgit.api.errors.JGitInternalException;
  23. import org.eclipse.jgit.api.errors.MultipleParentsNotAllowedException;
  24. import org.eclipse.jgit.dircache.DirCache;
  25. import org.eclipse.jgit.events.ChangeRecorder;
  26. import org.eclipse.jgit.events.ListenerHandle;
  27. import org.eclipse.jgit.junit.RepositoryTestCase;
  28. import org.eclipse.jgit.lib.ConfigConstants;
  29. import org.eclipse.jgit.lib.Constants;
  30. import org.eclipse.jgit.lib.FileMode;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.lib.ReflogReader;
  33. import org.eclipse.jgit.lib.RepositoryState;
  34. import org.eclipse.jgit.merge.ContentMergeStrategy;
  35. import org.eclipse.jgit.merge.MergeStrategy;
  36. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  37. import org.eclipse.jgit.revwalk.RevCommit;
  38. import org.junit.Test;
  39. /**
  40. * Test cherry-pick command
  41. */
  42. public class CherryPickCommandTest extends RepositoryTestCase {
  43. @Test
  44. public void testCherryPick() throws IOException, JGitInternalException,
  45. GitAPIException {
  46. doTestCherryPick(false);
  47. }
  48. @Test
  49. public void testCherryPickNoCommit() throws IOException,
  50. JGitInternalException, GitAPIException {
  51. doTestCherryPick(true);
  52. }
  53. private void doTestCherryPick(boolean noCommit) throws IOException,
  54. JGitInternalException,
  55. GitAPIException {
  56. try (Git git = new Git(db)) {
  57. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  58. git.add().addFilepattern("a").call();
  59. RevCommit firstCommit = git.commit().setMessage("create a").call();
  60. writeTrashFile("b", "content\n");
  61. git.add().addFilepattern("b").call();
  62. git.commit().setMessage("create b").call();
  63. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  64. git.add().addFilepattern("a").call();
  65. git.commit().setMessage("enlarged a").call();
  66. writeTrashFile("a",
  67. "first line\nsecond line\nthird line\nfourth line\n");
  68. git.add().addFilepattern("a").call();
  69. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  70. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  71. checkoutBranch("refs/heads/side");
  72. writeTrashFile("a", "first line\nsec. line\nthird line\nfeature++\n");
  73. git.add().addFilepattern("a").call();
  74. git.commit().setMessage("enhanced a").call();
  75. CherryPickResult pickResult = git.cherryPick().include(fixingA)
  76. .setNoCommit(noCommit).call();
  77. assertEquals(CherryPickStatus.OK, pickResult.getStatus());
  78. assertFalse(new File(db.getWorkTree(), "b").exists());
  79. checkFile(new File(db.getWorkTree(), "a"),
  80. "first line\nsecond line\nthird line\nfeature++\n");
  81. Iterator<RevCommit> history = git.log().call().iterator();
  82. if (!noCommit)
  83. assertEquals("fixed a", history.next().getFullMessage());
  84. assertEquals("enhanced a", history.next().getFullMessage());
  85. assertEquals("create a", history.next().getFullMessage());
  86. assertFalse(history.hasNext());
  87. }
  88. }
  89. @Test
  90. public void testSequentialCherryPick() throws IOException, JGitInternalException,
  91. GitAPIException {
  92. try (Git git = new Git(db)) {
  93. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  94. git.add().addFilepattern("a").call();
  95. RevCommit firstCommit = git.commit().setMessage("create a").call();
  96. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  97. git.add().addFilepattern("a").call();
  98. RevCommit enlargingA = git.commit().setMessage("enlarged a").call();
  99. writeTrashFile("a",
  100. "first line\nsecond line\nthird line\nfourth line\n");
  101. git.add().addFilepattern("a").call();
  102. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  103. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  104. checkoutBranch("refs/heads/side");
  105. writeTrashFile("b", "nothing to do with a");
  106. git.add().addFilepattern("b").call();
  107. git.commit().setMessage("create b").call();
  108. CherryPickResult result = git.cherryPick().include(enlargingA).include(fixingA).call();
  109. assertEquals(CherryPickResult.CherryPickStatus.OK, result.getStatus());
  110. Iterator<RevCommit> history = git.log().call().iterator();
  111. assertEquals("fixed a", history.next().getFullMessage());
  112. assertEquals("enlarged a", history.next().getFullMessage());
  113. assertEquals("create b", history.next().getFullMessage());
  114. assertEquals("create a", history.next().getFullMessage());
  115. assertFalse(history.hasNext());
  116. }
  117. }
  118. @Test
  119. public void testCherryPickDirtyIndex() throws Exception {
  120. try (Git git = new Git(db)) {
  121. RevCommit sideCommit = prepareCherryPick(git);
  122. // modify and add file a
  123. writeTrashFile("a", "a(modified)");
  124. git.add().addFilepattern("a").call();
  125. // do not commit
  126. doCherryPickAndCheckResult(git, sideCommit,
  127. MergeFailureReason.DIRTY_INDEX);
  128. }
  129. }
  130. @Test
  131. public void testCherryPickDirtyWorktree() throws Exception {
  132. try (Git git = new Git(db)) {
  133. RevCommit sideCommit = prepareCherryPick(git);
  134. // modify file a
  135. writeTrashFile("a", "a(modified)");
  136. // do not add and commit
  137. doCherryPickAndCheckResult(git, sideCommit,
  138. MergeFailureReason.DIRTY_WORKTREE);
  139. }
  140. }
  141. @Test
  142. public void testCherryPickConflictResolution() throws Exception {
  143. try (Git git = new Git(db)) {
  144. RevCommit sideCommit = prepareCherryPick(git);
  145. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  146. .call();
  147. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  148. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  149. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  150. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  151. .exists());
  152. assertEquals(sideCommit.getId(), db.readCherryPickHead());
  153. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  154. // Resolve
  155. writeTrashFile("a", "a");
  156. git.add().addFilepattern("a").call();
  157. assertEquals(RepositoryState.CHERRY_PICKING_RESOLVED,
  158. db.getRepositoryState());
  159. git.commit().setOnly("a").setMessage("resolve").call();
  160. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  161. }
  162. }
  163. @Test
  164. public void testCherryPickConflictResolutionNoCommit() throws Exception {
  165. Git git = new Git(db);
  166. RevCommit sideCommit = prepareCherryPick(git);
  167. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  168. .setNoCommit(true).call();
  169. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  170. assertTrue(db.readDirCache().hasUnmergedPaths());
  171. String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  172. assertEquals(expected, read("a"));
  173. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  174. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  175. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  176. .exists());
  177. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  178. // Resolve
  179. writeTrashFile("a", "a");
  180. git.add().addFilepattern("a").call();
  181. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  182. git.commit().setOnly("a").setMessage("resolve").call();
  183. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  184. }
  185. @Test
  186. public void testCherryPickConflictReset() throws Exception {
  187. try (Git git = new Git(db)) {
  188. RevCommit sideCommit = prepareCherryPick(git);
  189. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  190. .call();
  191. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  192. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  193. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  194. .exists());
  195. git.reset().setMode(ResetType.MIXED).setRef("HEAD").call();
  196. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  197. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  198. .exists());
  199. }
  200. }
  201. @Test
  202. public void testCherryPickOverExecutableChangeOnNonExectuableFileSystem()
  203. throws Exception {
  204. try (Git git = new Git(db)) {
  205. File file = writeTrashFile("test.txt", "a");
  206. assertNotNull(git.add().addFilepattern("test.txt").call());
  207. assertNotNull(git.commit().setMessage("commit1").call());
  208. assertNotNull(git.checkout().setCreateBranch(true).setName("a").call());
  209. writeTrashFile("test.txt", "b");
  210. assertNotNull(git.add().addFilepattern("test.txt").call());
  211. RevCommit commit2 = git.commit().setMessage("commit2").call();
  212. assertNotNull(commit2);
  213. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  214. DirCache cache = db.lockDirCache();
  215. cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE);
  216. cache.write();
  217. assertTrue(cache.commit());
  218. cache.unlock();
  219. assertNotNull(git.commit().setMessage("commit3").call());
  220. db.getFS().setExecute(file, false);
  221. git.getRepository()
  222. .getConfig()
  223. .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  224. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  225. CherryPickResult result = git.cherryPick().include(commit2).call();
  226. assertNotNull(result);
  227. assertEquals(CherryPickStatus.OK, result.getStatus());
  228. }
  229. }
  230. @Test
  231. public void testCherryPickOurs() throws Exception {
  232. try (Git git = new Git(db)) {
  233. RevCommit sideCommit = prepareCherryPick(git);
  234. CherryPickResult result = git.cherryPick()
  235. .include(sideCommit.getId())
  236. .setStrategy(MergeStrategy.OURS)
  237. .call();
  238. assertEquals(CherryPickStatus.OK, result.getStatus());
  239. String expected = "a(master)";
  240. checkFile(new File(db.getWorkTree(), "a"), expected);
  241. }
  242. }
  243. @Test
  244. public void testCherryPickTheirs() throws Exception {
  245. try (Git git = new Git(db)) {
  246. RevCommit sideCommit = prepareCherryPick(git);
  247. CherryPickResult result = git.cherryPick()
  248. .include(sideCommit.getId())
  249. .setStrategy(MergeStrategy.THEIRS)
  250. .call();
  251. assertEquals(CherryPickStatus.OK, result.getStatus());
  252. String expected = "a(side)";
  253. checkFile(new File(db.getWorkTree(), "a"), expected);
  254. }
  255. }
  256. @Test
  257. public void testCherryPickXours() throws Exception {
  258. try (Git git = new Git(db)) {
  259. RevCommit sideCommit = prepareCherryPickStrategyOption(git);
  260. CherryPickResult result = git.cherryPick()
  261. .include(sideCommit.getId())
  262. .setContentMergeStrategy(ContentMergeStrategy.OURS)
  263. .call();
  264. assertEquals(CherryPickStatus.OK, result.getStatus());
  265. String expected = "a\nmaster\nc\nd\n";
  266. checkFile(new File(db.getWorkTree(), "a"), expected);
  267. }
  268. }
  269. @Test
  270. public void testCherryPickXtheirs() throws Exception {
  271. try (Git git = new Git(db)) {
  272. RevCommit sideCommit = prepareCherryPickStrategyOption(git);
  273. CherryPickResult result = git.cherryPick()
  274. .include(sideCommit.getId())
  275. .setContentMergeStrategy(ContentMergeStrategy.THEIRS)
  276. .call();
  277. assertEquals(CherryPickStatus.OK, result.getStatus());
  278. String expected = "a\nside\nc\nd\n";
  279. checkFile(new File(db.getWorkTree(), "a"), expected);
  280. }
  281. }
  282. @Test
  283. public void testCherryPickConflictMarkers() throws Exception {
  284. try (Git git = new Git(db)) {
  285. RevCommit sideCommit = prepareCherryPick(git);
  286. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  287. .call();
  288. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  289. String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  290. checkFile(new File(db.getWorkTree(), "a"), expected);
  291. }
  292. }
  293. @Test
  294. public void testCherryPickConflictFiresModifiedEvent() throws Exception {
  295. ListenerHandle listener = null;
  296. try (Git git = new Git(db)) {
  297. RevCommit sideCommit = prepareCherryPick(git);
  298. ChangeRecorder recorder = new ChangeRecorder();
  299. listener = db.getListenerList()
  300. .addWorkingTreeModifiedListener(recorder);
  301. CherryPickResult result = git.cherryPick()
  302. .include(sideCommit.getId()).call();
  303. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  304. recorder.assertEvent(new String[] { "a" }, ChangeRecorder.EMPTY);
  305. } finally {
  306. if (listener != null) {
  307. listener.remove();
  308. }
  309. }
  310. }
  311. @Test
  312. public void testCherryPickNewFileFiresModifiedEvent() throws Exception {
  313. ListenerHandle listener = null;
  314. try (Git git = new Git(db)) {
  315. writeTrashFile("test.txt", "a");
  316. git.add().addFilepattern("test.txt").call();
  317. git.commit().setMessage("commit1").call();
  318. git.checkout().setCreateBranch(true).setName("a").call();
  319. writeTrashFile("side.txt", "side");
  320. git.add().addFilepattern("side.txt").call();
  321. RevCommit side = git.commit().setMessage("side").call();
  322. assertNotNull(side);
  323. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  324. writeTrashFile("test.txt", "b");
  325. assertNotNull(git.add().addFilepattern("test.txt").call());
  326. assertNotNull(git.commit().setMessage("commit2").call());
  327. ChangeRecorder recorder = new ChangeRecorder();
  328. listener = db.getListenerList()
  329. .addWorkingTreeModifiedListener(recorder);
  330. CherryPickResult result = git.cherryPick()
  331. .include(side.getId()).call();
  332. assertEquals(CherryPickStatus.OK, result.getStatus());
  333. recorder.assertEvent(new String[] { "side.txt" },
  334. ChangeRecorder.EMPTY);
  335. } finally {
  336. if (listener != null) {
  337. listener.remove();
  338. }
  339. }
  340. }
  341. @Test
  342. public void testCherryPickOurCommitName() throws Exception {
  343. try (Git git = new Git(db)) {
  344. RevCommit sideCommit = prepareCherryPick(git);
  345. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  346. .setOurCommitName("custom name").call();
  347. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  348. String expected = "<<<<<<< custom name\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  349. checkFile(new File(db.getWorkTree(), "a"), expected);
  350. }
  351. }
  352. private RevCommit prepareCherryPick(Git git) throws Exception {
  353. // create, add and commit file a
  354. writeTrashFile("a", "a");
  355. git.add().addFilepattern("a").call();
  356. RevCommit firstMasterCommit = git.commit().setMessage("first master")
  357. .call();
  358. // create and checkout side branch
  359. createBranch(firstMasterCommit, "refs/heads/side");
  360. checkoutBranch("refs/heads/side");
  361. // modify, add and commit file a
  362. writeTrashFile("a", "a(side)");
  363. git.add().addFilepattern("a").call();
  364. RevCommit sideCommit = git.commit().setMessage("side").call();
  365. // checkout master branch
  366. checkoutBranch("refs/heads/master");
  367. // modify, add and commit file a
  368. writeTrashFile("a", "a(master)");
  369. git.add().addFilepattern("a").call();
  370. git.commit().setMessage("second master").call();
  371. return sideCommit;
  372. }
  373. private RevCommit prepareCherryPickStrategyOption(Git git)
  374. throws Exception {
  375. // create, add and commit file a
  376. writeTrashFile("a", "a\nb\nc\n");
  377. git.add().addFilepattern("a").call();
  378. RevCommit firstMasterCommit = git.commit().setMessage("first master")
  379. .call();
  380. // create and checkout side branch
  381. createBranch(firstMasterCommit, "refs/heads/side");
  382. checkoutBranch("refs/heads/side");
  383. // modify, add and commit file a
  384. writeTrashFile("a", "a\nside\nc\nd\n");
  385. git.add().addFilepattern("a").call();
  386. RevCommit sideCommit = git.commit().setMessage("side").call();
  387. // checkout master branch
  388. checkoutBranch("refs/heads/master");
  389. // modify, add and commit file a
  390. writeTrashFile("a", "a\nmaster\nc\n");
  391. git.add().addFilepattern("a").call();
  392. git.commit().setMessage("second master").call();
  393. return sideCommit;
  394. }
  395. private void doCherryPickAndCheckResult(final Git git,
  396. final RevCommit sideCommit, final MergeFailureReason reason)
  397. throws Exception {
  398. // get current index state
  399. String indexState = indexState(CONTENT);
  400. // cherry-pick
  401. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  402. .call();
  403. assertEquals(CherryPickStatus.FAILED, result.getStatus());
  404. // staged file a causes DIRTY_INDEX
  405. assertEquals(1, result.getFailingPaths().size());
  406. assertEquals(reason, result.getFailingPaths().get("a"));
  407. assertEquals("a(modified)", read(new File(db.getWorkTree(), "a")));
  408. // index shall be unchanged
  409. assertEquals(indexState, indexState(CONTENT));
  410. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  411. if (reason == null) {
  412. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  413. assertTrue(reader.getLastEntry().getComment()
  414. .startsWith("cherry-pick: "));
  415. reader = db.getReflogReader(db.getBranch());
  416. assertTrue(reader.getLastEntry().getComment()
  417. .startsWith("cherry-pick: "));
  418. }
  419. }
  420. /**
  421. * Cherry-picking merge commit M onto T
  422. * <pre>
  423. * M
  424. * |\
  425. * C D
  426. * |/
  427. * T B
  428. * | /
  429. * A
  430. * </pre>
  431. * @throws Exception
  432. */
  433. @Test
  434. public void testCherryPickMerge() throws Exception {
  435. try (Git git = new Git(db)) {
  436. commitFile("file", "1\n2\n3\n", "master");
  437. commitFile("file", "1\n2\n3\n", "side");
  438. checkoutBranch("refs/heads/side");
  439. RevCommit commitD = commitFile("file", "1\n2\n3\n4\n5\n", "side2");
  440. commitFile("file", "a\n2\n3\n", "side");
  441. MergeResult mergeResult = git.merge().include(commitD).call();
  442. ObjectId commitM = mergeResult.getNewHead();
  443. checkoutBranch("refs/heads/master");
  444. RevCommit commitT = commitFile("another", "t", "master");
  445. try {
  446. git.cherryPick().include(commitM).call();
  447. fail("merges should not be cherry-picked by default");
  448. } catch (MultipleParentsNotAllowedException e) {
  449. // expected
  450. }
  451. try {
  452. git.cherryPick().include(commitM).setMainlineParentNumber(3).call();
  453. fail("specifying a non-existent parent should fail");
  454. } catch (JGitInternalException e) {
  455. // expected
  456. assertTrue(e.getMessage().endsWith(
  457. "does not have a parent number 3."));
  458. }
  459. CherryPickResult result = git.cherryPick().include(commitM)
  460. .setMainlineParentNumber(1).call();
  461. assertEquals(CherryPickStatus.OK, result.getStatus());
  462. checkFile(new File(db.getWorkTree(), "file"), "1\n2\n3\n4\n5\n");
  463. git.reset().setMode(ResetType.HARD).setRef(commitT.getName()).call();
  464. CherryPickResult result2 = git.cherryPick().include(commitM)
  465. .setMainlineParentNumber(2).call();
  466. assertEquals(CherryPickStatus.OK, result2.getStatus());
  467. checkFile(new File(db.getWorkTree(), "file"), "a\n2\n3\n");
  468. }
  469. }
  470. }