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.

MergeTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2012, 2014 IBM Corporation and others. 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.pgm;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import java.util.Iterator;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  16. import org.eclipse.jgit.merge.MergeStrategy;
  17. import org.eclipse.jgit.pgm.internal.CLIText;
  18. import org.eclipse.jgit.revwalk.RevCommit;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class MergeTest extends CLIRepositoryTestCase {
  22. private Git git;
  23. @Override
  24. @Before
  25. public void setUp() throws Exception {
  26. super.setUp();
  27. git = new Git(db);
  28. git.commit().setMessage("initial commit").call();
  29. }
  30. @Test
  31. public void testMergeSelf() throws Exception {
  32. assertEquals("Already up-to-date.", execute("git merge master")[0]);
  33. }
  34. @Test
  35. public void testSquashSelf() throws Exception {
  36. assertEquals(" (nothing to squash)Already up-to-date.",
  37. execute("git merge master --squash")[0]);
  38. }
  39. @Test
  40. public void testFastForward() throws Exception {
  41. git.branchCreate().setName("side").call();
  42. writeTrashFile("file", "master");
  43. git.add().addFilepattern("file").call();
  44. git.commit().setMessage("commit").call();
  45. git.checkout().setName("side").call();
  46. assertArrayEquals(new String[] { "Updating 6fd41be..26a81a1",
  47. "Fast-forward", "" }, execute("git merge master"));
  48. }
  49. @Test
  50. public void testMerge() throws Exception {
  51. git.branchCreate().setName("side").call();
  52. writeTrashFile("master", "content");
  53. git.add().addFilepattern("master").call();
  54. git.commit().setMessage("master commit").call();
  55. git.checkout().setName("side").call();
  56. writeTrashFile("side", "content");
  57. git.add().addFilepattern("side").call();
  58. git.commit().setMessage("side commit").call();
  59. assertEquals("Merge made by the '" + MergeStrategy.RECURSIVE.getName()
  60. + "' strategy.", execute("git merge master")[0]);
  61. }
  62. @Test
  63. public void testMergeNoCommit() throws Exception {
  64. git.branchCreate().setName("side").call();
  65. writeTrashFile("master", "content");
  66. git.add().addFilepattern("master").call();
  67. git.commit().setMessage("master commit").call();
  68. git.checkout().setName("side").call();
  69. writeTrashFile("side", "content");
  70. git.add().addFilepattern("side").call();
  71. git.commit().setMessage("side commit").call();
  72. assertEquals(
  73. "Automatic merge went well; stopped before committing as requested",
  74. execute("git merge --no-commit master")[0]);
  75. }
  76. @Test
  77. public void testMergeNoCommitSquash() throws Exception {
  78. git.branchCreate().setName("side").call();
  79. writeTrashFile("master", "content");
  80. git.add().addFilepattern("master").call();
  81. git.commit().setMessage("master commit").call();
  82. git.checkout().setName("side").call();
  83. writeTrashFile("side", "content");
  84. git.add().addFilepattern("side").call();
  85. git.commit().setMessage("side commit").call();
  86. assertArrayEquals(
  87. new String[] {
  88. "Squash commit -- not updating HEAD",
  89. "Automatic merge went well; stopped before committing as requested",
  90. "" }, execute("git merge --no-commit --squash master"));
  91. }
  92. @Test
  93. public void testSquash() throws Exception {
  94. git.branchCreate().setName("side").call();
  95. writeTrashFile("file1", "content1");
  96. git.add().addFilepattern("file1").call();
  97. git.commit().setMessage("file1 commit").call();
  98. writeTrashFile("file2", "content2");
  99. git.add().addFilepattern("file2").call();
  100. git.commit().setMessage("file2 commit").call();
  101. git.checkout().setName("side").call();
  102. writeTrashFile("side", "content");
  103. git.add().addFilepattern("side").call();
  104. git.commit().setMessage("side commit").call();
  105. assertArrayEquals(
  106. new String[] { "Squash commit -- not updating HEAD",
  107. "Automatic merge went well; stopped before committing as requested",
  108. "" },
  109. execute("git merge master --squash"));
  110. }
  111. @Test
  112. public void testNoFastForward() throws Exception {
  113. git.branchCreate().setName("side").call();
  114. writeTrashFile("file", "master");
  115. git.add().addFilepattern("file").call();
  116. git.commit().setMessage("commit").call();
  117. git.checkout().setName("side").call();
  118. assertEquals("Merge made by the 'recursive' strategy.",
  119. execute("git merge master --no-ff")[0]);
  120. assertArrayEquals(new String[] {
  121. "commit 6db23724012376e8407fc24b5da4277a9601be81", //
  122. "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", //
  123. "Date: Sat Aug 15 20:12:58 2009 -0330", //
  124. "", //
  125. " Merge branch 'master' into side", //
  126. "", //
  127. "commit 6fd41be26b7ee41584dd997f665deb92b6c4c004", //
  128. "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", //
  129. "Date: Sat Aug 15 20:12:58 2009 -0330", //
  130. "", //
  131. " initial commit", //
  132. "", //
  133. "commit 26a81a1c6a105551ba703a8b6afc23994cacbae1", //
  134. "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>", //
  135. "Date: Sat Aug 15 20:12:58 2009 -0330", //
  136. "", //
  137. " commit", //
  138. "", //
  139. ""
  140. }, execute("git log"));
  141. }
  142. @Test
  143. public void testNoFastForwardAndSquash() throws Exception {
  144. assertEquals(
  145. CLIText.fatalError(CLIText.get().cannotCombineSquashWithNoff),
  146. executeUnchecked("git merge master --no-ff --squash")[0]);
  147. }
  148. @Test
  149. public void testFastForwardOnly() throws Exception {
  150. git.branchCreate().setName("side").call();
  151. writeTrashFile("file", "master");
  152. git.add().addFilepattern("file").call();
  153. git.commit().setMessage("commit#1").call();
  154. git.checkout().setName("side").call();
  155. writeTrashFile("file", "side");
  156. git.add().addFilepattern("file").call();
  157. git.commit().setMessage("commit#2").call();
  158. assertEquals(CLIText.fatalError(CLIText.get().ffNotPossibleAborting),
  159. executeUnchecked("git merge master --ff-only")[0]);
  160. }
  161. @Test
  162. public void testMergeWithUserMessage() throws Exception {
  163. git.branchCreate().setName("side").call();
  164. writeTrashFile("master", "content");
  165. git.add().addFilepattern("master").call();
  166. git.commit().setMessage("master commit").call();
  167. git.checkout().setName("side").call();
  168. writeTrashFile("side", "content");
  169. git.add().addFilepattern("side").call();
  170. git.commit().setMessage("side commit").call();
  171. assertEquals("Merge made by the '" + MergeStrategy.RECURSIVE.getName()
  172. + "' strategy.",
  173. execute("git merge master -m \"user message\"")[0]);
  174. Iterator<RevCommit> it = git.log().call().iterator();
  175. RevCommit newHead = it.next();
  176. assertEquals("user message", newHead.getFullMessage());
  177. }
  178. }