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

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