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

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