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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.Iterator;
  50. import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
  51. import org.eclipse.jgit.api.ResetCommand.ResetType;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.RepositoryState;
  56. import org.eclipse.jgit.lib.RepositoryTestCase;
  57. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.storage.file.ReflogReader;
  60. import org.junit.Test;
  61. /**
  62. * Test cherry-pick command
  63. */
  64. public class CherryPickCommandTest extends RepositoryTestCase {
  65. @Test
  66. public void testCherryPick() throws IOException, JGitInternalException,
  67. GitAPIException {
  68. Git git = new Git(db);
  69. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  70. git.add().addFilepattern("a").call();
  71. RevCommit firstCommit = git.commit().setMessage("create a").call();
  72. writeTrashFile("b", "content\n");
  73. git.add().addFilepattern("b").call();
  74. git.commit().setMessage("create b").call();
  75. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  76. git.add().addFilepattern("a").call();
  77. git.commit().setMessage("enlarged a").call();
  78. writeTrashFile("a",
  79. "first line\nsecond line\nthird line\nfourth line\n");
  80. git.add().addFilepattern("a").call();
  81. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  82. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  83. checkoutBranch("refs/heads/side");
  84. writeTrashFile("a", "first line\nsec. line\nthird line\nfeature++\n");
  85. git.add().addFilepattern("a").call();
  86. git.commit().setMessage("enhanced a").call();
  87. git.cherryPick().include(fixingA).call();
  88. assertFalse(new File(db.getWorkTree(), "b").exists());
  89. checkFile(new File(db.getWorkTree(), "a"),
  90. "first line\nsecond line\nthird line\nfeature++\n");
  91. Iterator<RevCommit> history = git.log().call().iterator();
  92. assertEquals("fixed a", history.next().getFullMessage());
  93. assertEquals("enhanced a", history.next().getFullMessage());
  94. assertEquals("create a", history.next().getFullMessage());
  95. assertFalse(history.hasNext());
  96. }
  97. @Test
  98. public void testCherryPickDirtyIndex() throws Exception {
  99. Git git = new Git(db);
  100. RevCommit sideCommit = prepareCherryPick(git);
  101. // modify and add file a
  102. writeTrashFile("a", "a(modified)");
  103. git.add().addFilepattern("a").call();
  104. // do not commit
  105. doCherryPickAndCheckResult(git, sideCommit,
  106. MergeFailureReason.DIRTY_INDEX);
  107. }
  108. @Test
  109. public void testCherryPickDirtyWorktree() throws Exception {
  110. Git git = new Git(db);
  111. RevCommit sideCommit = prepareCherryPick(git);
  112. // modify file a
  113. writeTrashFile("a", "a(modified)");
  114. // do not add and commit
  115. doCherryPickAndCheckResult(git, sideCommit,
  116. MergeFailureReason.DIRTY_WORKTREE);
  117. }
  118. @Test
  119. public void testCherryPickConflictResolution() throws Exception {
  120. Git git = new Git(db);
  121. RevCommit sideCommit = prepareCherryPick(git);
  122. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  123. .call();
  124. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  125. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  126. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  127. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  128. .exists());
  129. assertEquals(sideCommit.getId(), db.readCherryPickHead());
  130. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  131. // Resolve
  132. writeTrashFile("a", "a");
  133. git.add().addFilepattern("a").call();
  134. assertEquals(RepositoryState.CHERRY_PICKING_RESOLVED,
  135. db.getRepositoryState());
  136. git.commit().setOnly("a").setMessage("resolve").call();
  137. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  138. }
  139. @Test
  140. public void testCherryPickConflictReset() throws Exception {
  141. Git git = new Git(db);
  142. RevCommit sideCommit = prepareCherryPick(git);
  143. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  144. .call();
  145. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  146. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  147. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  148. .exists());
  149. git.reset().setMode(ResetType.MIXED).setRef("HEAD").call();
  150. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  151. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  152. .exists());
  153. }
  154. private RevCommit prepareCherryPick(final Git git) throws Exception {
  155. // create, add and commit file a
  156. writeTrashFile("a", "a");
  157. git.add().addFilepattern("a").call();
  158. RevCommit firstMasterCommit = git.commit().setMessage("first master")
  159. .call();
  160. // create and checkout side branch
  161. createBranch(firstMasterCommit, "refs/heads/side");
  162. checkoutBranch("refs/heads/side");
  163. // modify, add and commit file a
  164. writeTrashFile("a", "a(side)");
  165. git.add().addFilepattern("a").call();
  166. RevCommit sideCommit = git.commit().setMessage("side").call();
  167. // checkout master branch
  168. checkoutBranch("refs/heads/master");
  169. // modify, add and commit file a
  170. writeTrashFile("a", "a(master)");
  171. git.add().addFilepattern("a").call();
  172. git.commit().setMessage("second master").call();
  173. return sideCommit;
  174. }
  175. private void doCherryPickAndCheckResult(final Git git,
  176. final RevCommit sideCommit, final MergeFailureReason reason)
  177. throws Exception {
  178. // get current index state
  179. String indexState = indexState(CONTENT);
  180. // cherry-pick
  181. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  182. .call();
  183. assertEquals(CherryPickStatus.FAILED, result.getStatus());
  184. // staged file a causes DIRTY_INDEX
  185. assertEquals(1, result.getFailingPaths().size());
  186. assertEquals(reason, result.getFailingPaths().get("a"));
  187. assertEquals("a(modified)", read(new File(db.getWorkTree(), "a")));
  188. // index shall be unchanged
  189. assertEquals(indexState, indexState(CONTENT));
  190. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  191. if (reason == null) {
  192. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  193. assertTrue(reader.getLastEntry().getComment()
  194. .startsWith("cherry-pick: "));
  195. reader = db.getReflogReader(db.getBranch());
  196. assertTrue(reader.getLastEntry().getComment()
  197. .startsWith("cherry-pick: "));
  198. }
  199. }
  200. }