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