選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CherryPickCommandTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.util.Iterator;
  52. import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
  53. import org.eclipse.jgit.api.ResetCommand.ResetType;
  54. import org.eclipse.jgit.api.errors.GitAPIException;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.api.errors.MultipleParentsNotAllowedException;
  57. import org.eclipse.jgit.dircache.DirCache;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.ConfigConstants;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.FileMode;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ReflogReader;
  64. import org.eclipse.jgit.lib.RepositoryState;
  65. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.junit.Test;
  68. /**
  69. * Test cherry-pick command
  70. */
  71. public class CherryPickCommandTest extends RepositoryTestCase {
  72. @Test
  73. public void testCherryPick() throws IOException, JGitInternalException,
  74. GitAPIException {
  75. doTestCherryPick(false);
  76. }
  77. @Test
  78. public void testCherryPickNoCommit() throws IOException,
  79. JGitInternalException, GitAPIException {
  80. doTestCherryPick(true);
  81. }
  82. private void doTestCherryPick(boolean noCommit) throws IOException,
  83. JGitInternalException,
  84. GitAPIException {
  85. try (Git git = new Git(db)) {
  86. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  87. git.add().addFilepattern("a").call();
  88. RevCommit firstCommit = git.commit().setMessage("create a").call();
  89. writeTrashFile("b", "content\n");
  90. git.add().addFilepattern("b").call();
  91. git.commit().setMessage("create b").call();
  92. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  93. git.add().addFilepattern("a").call();
  94. git.commit().setMessage("enlarged a").call();
  95. writeTrashFile("a",
  96. "first line\nsecond line\nthird line\nfourth line\n");
  97. git.add().addFilepattern("a").call();
  98. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  99. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  100. checkoutBranch("refs/heads/side");
  101. writeTrashFile("a", "first line\nsec. line\nthird line\nfeature++\n");
  102. git.add().addFilepattern("a").call();
  103. git.commit().setMessage("enhanced a").call();
  104. CherryPickResult pickResult = git.cherryPick().include(fixingA)
  105. .setNoCommit(noCommit).call();
  106. assertEquals(CherryPickStatus.OK, pickResult.getStatus());
  107. assertFalse(new File(db.getWorkTree(), "b").exists());
  108. checkFile(new File(db.getWorkTree(), "a"),
  109. "first line\nsecond line\nthird line\nfeature++\n");
  110. Iterator<RevCommit> history = git.log().call().iterator();
  111. if (!noCommit)
  112. assertEquals("fixed a", history.next().getFullMessage());
  113. assertEquals("enhanced a", history.next().getFullMessage());
  114. assertEquals("create a", history.next().getFullMessage());
  115. assertFalse(history.hasNext());
  116. }
  117. }
  118. @Test
  119. public void testSequentialCherryPick() throws IOException, JGitInternalException,
  120. GitAPIException {
  121. try (Git git = new Git(db)) {
  122. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  123. git.add().addFilepattern("a").call();
  124. RevCommit firstCommit = git.commit().setMessage("create a").call();
  125. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  126. git.add().addFilepattern("a").call();
  127. RevCommit enlargingA = git.commit().setMessage("enlarged a").call();
  128. writeTrashFile("a",
  129. "first line\nsecond line\nthird line\nfourth line\n");
  130. git.add().addFilepattern("a").call();
  131. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  132. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  133. checkoutBranch("refs/heads/side");
  134. writeTrashFile("b", "nothing to do with a");
  135. git.add().addFilepattern("b").call();
  136. git.commit().setMessage("create b").call();
  137. CherryPickResult result = git.cherryPick().include(enlargingA).include(fixingA).call();
  138. assertEquals(CherryPickResult.CherryPickStatus.OK, result.getStatus());
  139. Iterator<RevCommit> history = git.log().call().iterator();
  140. assertEquals("fixed a", history.next().getFullMessage());
  141. assertEquals("enlarged a", history.next().getFullMessage());
  142. assertEquals("create b", history.next().getFullMessage());
  143. assertEquals("create a", history.next().getFullMessage());
  144. assertFalse(history.hasNext());
  145. }
  146. }
  147. @Test
  148. public void testCherryPickDirtyIndex() throws Exception {
  149. try (Git git = new Git(db)) {
  150. RevCommit sideCommit = prepareCherryPick(git);
  151. // modify and add file a
  152. writeTrashFile("a", "a(modified)");
  153. git.add().addFilepattern("a").call();
  154. // do not commit
  155. doCherryPickAndCheckResult(git, sideCommit,
  156. MergeFailureReason.DIRTY_INDEX);
  157. }
  158. }
  159. @Test
  160. public void testCherryPickDirtyWorktree() throws Exception {
  161. try (Git git = new Git(db)) {
  162. RevCommit sideCommit = prepareCherryPick(git);
  163. // modify file a
  164. writeTrashFile("a", "a(modified)");
  165. // do not add and commit
  166. doCherryPickAndCheckResult(git, sideCommit,
  167. MergeFailureReason.DIRTY_WORKTREE);
  168. }
  169. }
  170. @Test
  171. public void testCherryPickConflictResolution() throws Exception {
  172. try (Git git = new Git(db)) {
  173. RevCommit sideCommit = prepareCherryPick(git);
  174. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  175. .call();
  176. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  177. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  178. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  179. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  180. .exists());
  181. assertEquals(sideCommit.getId(), db.readCherryPickHead());
  182. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  183. // Resolve
  184. writeTrashFile("a", "a");
  185. git.add().addFilepattern("a").call();
  186. assertEquals(RepositoryState.CHERRY_PICKING_RESOLVED,
  187. db.getRepositoryState());
  188. git.commit().setOnly("a").setMessage("resolve").call();
  189. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  190. }
  191. }
  192. @Test
  193. public void testCherryPickConflictResolutionNoCOmmit() throws Exception {
  194. Git git = new Git(db);
  195. RevCommit sideCommit = prepareCherryPick(git);
  196. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  197. .setNoCommit(true).call();
  198. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  199. assertTrue(db.readDirCache().hasUnmergedPaths());
  200. String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  201. assertEquals(expected, read("a"));
  202. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  203. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  204. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  205. .exists());
  206. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  207. // Resolve
  208. writeTrashFile("a", "a");
  209. git.add().addFilepattern("a").call();
  210. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  211. git.commit().setOnly("a").setMessage("resolve").call();
  212. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  213. }
  214. @Test
  215. public void testCherryPickConflictReset() throws Exception {
  216. try (Git git = new Git(db)) {
  217. RevCommit sideCommit = prepareCherryPick(git);
  218. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  219. .call();
  220. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  221. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  222. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  223. .exists());
  224. git.reset().setMode(ResetType.MIXED).setRef("HEAD").call();
  225. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  226. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  227. .exists());
  228. }
  229. }
  230. @Test
  231. public void testCherryPickOverExecutableChangeOnNonExectuableFileSystem()
  232. throws Exception {
  233. try (Git git = new Git(db)) {
  234. File file = writeTrashFile("test.txt", "a");
  235. assertNotNull(git.add().addFilepattern("test.txt").call());
  236. assertNotNull(git.commit().setMessage("commit1").call());
  237. assertNotNull(git.checkout().setCreateBranch(true).setName("a").call());
  238. writeTrashFile("test.txt", "b");
  239. assertNotNull(git.add().addFilepattern("test.txt").call());
  240. RevCommit commit2 = git.commit().setMessage("commit2").call();
  241. assertNotNull(commit2);
  242. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  243. DirCache cache = db.lockDirCache();
  244. cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE);
  245. cache.write();
  246. assertTrue(cache.commit());
  247. cache.unlock();
  248. assertNotNull(git.commit().setMessage("commit3").call());
  249. db.getFS().setExecute(file, false);
  250. git.getRepository()
  251. .getConfig()
  252. .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  253. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  254. CherryPickResult result = git.cherryPick().include(commit2).call();
  255. assertNotNull(result);
  256. assertEquals(CherryPickStatus.OK, result.getStatus());
  257. }
  258. }
  259. @Test
  260. public void testCherryPickConflictMarkers() throws Exception {
  261. try (Git git = new Git(db)) {
  262. RevCommit sideCommit = prepareCherryPick(git);
  263. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  264. .call();
  265. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  266. String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  267. checkFile(new File(db.getWorkTree(), "a"), expected);
  268. }
  269. }
  270. @Test
  271. public void testCherryPickOurCommitName() throws Exception {
  272. try (Git git = new Git(db)) {
  273. RevCommit sideCommit = prepareCherryPick(git);
  274. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  275. .setOurCommitName("custom name").call();
  276. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  277. String expected = "<<<<<<< custom name\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  278. checkFile(new File(db.getWorkTree(), "a"), expected);
  279. }
  280. }
  281. private RevCommit prepareCherryPick(Git git) throws Exception {
  282. // create, add and commit file a
  283. writeTrashFile("a", "a");
  284. git.add().addFilepattern("a").call();
  285. RevCommit firstMasterCommit = git.commit().setMessage("first master")
  286. .call();
  287. // create and checkout side branch
  288. createBranch(firstMasterCommit, "refs/heads/side");
  289. checkoutBranch("refs/heads/side");
  290. // modify, add and commit file a
  291. writeTrashFile("a", "a(side)");
  292. git.add().addFilepattern("a").call();
  293. RevCommit sideCommit = git.commit().setMessage("side").call();
  294. // checkout master branch
  295. checkoutBranch("refs/heads/master");
  296. // modify, add and commit file a
  297. writeTrashFile("a", "a(master)");
  298. git.add().addFilepattern("a").call();
  299. git.commit().setMessage("second master").call();
  300. return sideCommit;
  301. }
  302. private void doCherryPickAndCheckResult(final Git git,
  303. final RevCommit sideCommit, final MergeFailureReason reason)
  304. throws Exception {
  305. // get current index state
  306. String indexState = indexState(CONTENT);
  307. // cherry-pick
  308. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  309. .call();
  310. assertEquals(CherryPickStatus.FAILED, result.getStatus());
  311. // staged file a causes DIRTY_INDEX
  312. assertEquals(1, result.getFailingPaths().size());
  313. assertEquals(reason, result.getFailingPaths().get("a"));
  314. assertEquals("a(modified)", read(new File(db.getWorkTree(), "a")));
  315. // index shall be unchanged
  316. assertEquals(indexState, indexState(CONTENT));
  317. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  318. if (reason == null) {
  319. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  320. assertTrue(reader.getLastEntry().getComment()
  321. .startsWith("cherry-pick: "));
  322. reader = db.getReflogReader(db.getBranch());
  323. assertTrue(reader.getLastEntry().getComment()
  324. .startsWith("cherry-pick: "));
  325. }
  326. }
  327. /**
  328. * Cherry-picking merge commit M onto T
  329. * <pre>
  330. * M
  331. * |\
  332. * C D
  333. * |/
  334. * T B
  335. * | /
  336. * A
  337. * </pre>
  338. * @throws Exception
  339. */
  340. @Test
  341. public void testCherryPickMerge() throws Exception {
  342. try (Git git = new Git(db)) {
  343. commitFile("file", "1\n2\n3\n", "master");
  344. commitFile("file", "1\n2\n3\n", "side");
  345. checkoutBranch("refs/heads/side");
  346. RevCommit commitD = commitFile("file", "1\n2\n3\n4\n5\n", "side2");
  347. commitFile("file", "a\n2\n3\n", "side");
  348. MergeResult mergeResult = git.merge().include(commitD).call();
  349. ObjectId commitM = mergeResult.getNewHead();
  350. checkoutBranch("refs/heads/master");
  351. RevCommit commitT = commitFile("another", "t", "master");
  352. try {
  353. git.cherryPick().include(commitM).call();
  354. fail("merges should not be cherry-picked by default");
  355. } catch (MultipleParentsNotAllowedException e) {
  356. // expected
  357. }
  358. try {
  359. git.cherryPick().include(commitM).setMainlineParentNumber(3).call();
  360. fail("specifying a non-existent parent should fail");
  361. } catch (JGitInternalException e) {
  362. // expected
  363. assertTrue(e.getMessage().endsWith(
  364. "does not have a parent number 3."));
  365. }
  366. CherryPickResult result = git.cherryPick().include(commitM)
  367. .setMainlineParentNumber(1).call();
  368. assertEquals(CherryPickStatus.OK, result.getStatus());
  369. checkFile(new File(db.getWorkTree(), "file"), "1\n2\n3\n4\n5\n");
  370. git.reset().setMode(ResetType.HARD).setRef(commitT.getName()).call();
  371. CherryPickResult result2 = git.cherryPick().include(commitM)
  372. .setMainlineParentNumber(2).call();
  373. assertEquals(CherryPickStatus.OK, result2.getStatus());
  374. checkFile(new File(db.getWorkTree(), "file"), "a\n2\n3\n");
  375. }
  376. }
  377. }