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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.lib.ConfigConstants;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ReflogReader;
  61. import org.eclipse.jgit.lib.RepositoryState;
  62. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  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 testSequentialCherryPick() throws IOException, JGitInternalException,
  103. GitAPIException {
  104. Git git = new Git(db);
  105. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  106. git.add().addFilepattern("a").call();
  107. RevCommit firstCommit = git.commit().setMessage("create a").call();
  108. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  109. git.add().addFilepattern("a").call();
  110. RevCommit enlargingA = git.commit().setMessage("enlarged a").call();
  111. writeTrashFile("a",
  112. "first line\nsecond line\nthird line\nfourth line\n");
  113. git.add().addFilepattern("a").call();
  114. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  115. git.branchCreate().setName("side").setStartPoint(firstCommit).call();
  116. checkoutBranch("refs/heads/side");
  117. writeTrashFile("b", "nothing to do with a");
  118. git.add().addFilepattern("b").call();
  119. git.commit().setMessage("create b").call();
  120. CherryPickResult result = git.cherryPick().include(enlargingA).include(fixingA).call();
  121. assertEquals(CherryPickResult.CherryPickStatus.OK, result.getStatus());
  122. Iterator<RevCommit> history = git.log().call().iterator();
  123. assertEquals("fixed a", history.next().getFullMessage());
  124. assertEquals("enlarged a", history.next().getFullMessage());
  125. assertEquals("create b", history.next().getFullMessage());
  126. assertEquals("create a", history.next().getFullMessage());
  127. assertFalse(history.hasNext());
  128. }
  129. @Test
  130. public void testCherryPickDirtyIndex() throws Exception {
  131. Git git = new Git(db);
  132. RevCommit sideCommit = prepareCherryPick(git);
  133. // modify and add file a
  134. writeTrashFile("a", "a(modified)");
  135. git.add().addFilepattern("a").call();
  136. // do not commit
  137. doCherryPickAndCheckResult(git, sideCommit,
  138. MergeFailureReason.DIRTY_INDEX);
  139. }
  140. @Test
  141. public void testCherryPickDirtyWorktree() throws Exception {
  142. Git git = new Git(db);
  143. RevCommit sideCommit = prepareCherryPick(git);
  144. // modify file a
  145. writeTrashFile("a", "a(modified)");
  146. // do not add and commit
  147. doCherryPickAndCheckResult(git, sideCommit,
  148. MergeFailureReason.DIRTY_WORKTREE);
  149. }
  150. @Test
  151. public void testCherryPickConflictResolution() throws Exception {
  152. Git git = new Git(db);
  153. RevCommit sideCommit = prepareCherryPick(git);
  154. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  155. .call();
  156. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  157. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  158. assertEquals("side\n\nConflicts:\n\ta\n", db.readMergeCommitMsg());
  159. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  160. .exists());
  161. assertEquals(sideCommit.getId(), db.readCherryPickHead());
  162. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  163. // Resolve
  164. writeTrashFile("a", "a");
  165. git.add().addFilepattern("a").call();
  166. assertEquals(RepositoryState.CHERRY_PICKING_RESOLVED,
  167. db.getRepositoryState());
  168. git.commit().setOnly("a").setMessage("resolve").call();
  169. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  170. }
  171. @Test
  172. public void testCherryPickConflictReset() throws Exception {
  173. Git git = new Git(db);
  174. RevCommit sideCommit = prepareCherryPick(git);
  175. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  176. .call();
  177. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  178. assertEquals(RepositoryState.CHERRY_PICKING, db.getRepositoryState());
  179. assertTrue(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  180. .exists());
  181. git.reset().setMode(ResetType.MIXED).setRef("HEAD").call();
  182. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  183. assertFalse(new File(db.getDirectory(), Constants.CHERRY_PICK_HEAD)
  184. .exists());
  185. }
  186. @Test
  187. public void testCherryPickOverExecutableChangeOnNonExectuableFileSystem()
  188. throws Exception {
  189. Git git = new Git(db);
  190. File file = writeTrashFile("test.txt", "a");
  191. assertNotNull(git.add().addFilepattern("test.txt").call());
  192. assertNotNull(git.commit().setMessage("commit1").call());
  193. assertNotNull(git.checkout().setCreateBranch(true).setName("a").call());
  194. writeTrashFile("test.txt", "b");
  195. assertNotNull(git.add().addFilepattern("test.txt").call());
  196. RevCommit commit2 = git.commit().setMessage("commit2").call();
  197. assertNotNull(commit2);
  198. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  199. DirCache cache = db.lockDirCache();
  200. cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE);
  201. cache.write();
  202. assertTrue(cache.commit());
  203. cache.unlock();
  204. assertNotNull(git.commit().setMessage("commit3").call());
  205. db.getFS().setExecute(file, false);
  206. git.getRepository()
  207. .getConfig()
  208. .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  209. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  210. CherryPickResult result = git.cherryPick().include(commit2).call();
  211. assertNotNull(result);
  212. assertEquals(CherryPickStatus.OK, result.getStatus());
  213. }
  214. @Test
  215. public void testCherryPickConflictMarkers() throws Exception {
  216. 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. String expected = "<<<<<<< master\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  222. checkFile(new File(db.getWorkTree(), "a"), expected);
  223. }
  224. @Test
  225. public void testCherryPickOurCommitName() throws Exception {
  226. Git git = new Git(db);
  227. RevCommit sideCommit = prepareCherryPick(git);
  228. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  229. .setOurCommitName("custom name").call();
  230. assertEquals(CherryPickStatus.CONFLICTING, result.getStatus());
  231. String expected = "<<<<<<< custom name\na(master)\n=======\na(side)\n>>>>>>> 527460a side\n";
  232. checkFile(new File(db.getWorkTree(), "a"), expected);
  233. }
  234. private RevCommit prepareCherryPick(final Git git) throws Exception {
  235. // create, add and commit file a
  236. writeTrashFile("a", "a");
  237. git.add().addFilepattern("a").call();
  238. RevCommit firstMasterCommit = git.commit().setMessage("first master")
  239. .call();
  240. // create and checkout side branch
  241. createBranch(firstMasterCommit, "refs/heads/side");
  242. checkoutBranch("refs/heads/side");
  243. // modify, add and commit file a
  244. writeTrashFile("a", "a(side)");
  245. git.add().addFilepattern("a").call();
  246. RevCommit sideCommit = git.commit().setMessage("side").call();
  247. // checkout master branch
  248. checkoutBranch("refs/heads/master");
  249. // modify, add and commit file a
  250. writeTrashFile("a", "a(master)");
  251. git.add().addFilepattern("a").call();
  252. git.commit().setMessage("second master").call();
  253. return sideCommit;
  254. }
  255. private void doCherryPickAndCheckResult(final Git git,
  256. final RevCommit sideCommit, final MergeFailureReason reason)
  257. throws Exception {
  258. // get current index state
  259. String indexState = indexState(CONTENT);
  260. // cherry-pick
  261. CherryPickResult result = git.cherryPick().include(sideCommit.getId())
  262. .call();
  263. assertEquals(CherryPickStatus.FAILED, result.getStatus());
  264. // staged file a causes DIRTY_INDEX
  265. assertEquals(1, result.getFailingPaths().size());
  266. assertEquals(reason, result.getFailingPaths().get("a"));
  267. assertEquals("a(modified)", read(new File(db.getWorkTree(), "a")));
  268. // index shall be unchanged
  269. assertEquals(indexState, indexState(CONTENT));
  270. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  271. if (reason == null) {
  272. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  273. assertTrue(reader.getLastEntry().getComment()
  274. .startsWith("cherry-pick: "));
  275. reader = db.getReflogReader(db.getBranch());
  276. assertTrue(reader.getLastEntry().getComment()
  277. .startsWith("cherry-pick: "));
  278. }
  279. }
  280. }