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

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