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.

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