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.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  
50  import java.io.File;
51  
52  import org.eclipse.jgit.api.Git;
53  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
54  import org.eclipse.jgit.lib.Constants;
55  import org.eclipse.jgit.lib.RefUpdate;
56  import org.eclipse.jgit.pgm.internal.CLIText;
57  import org.eclipse.jgit.revwalk.RevCommit;
58  import org.junit.Before;
59  import org.junit.Test;
60  
61  public class BranchTest extends CLIRepositoryTestCase {
62  	@Override
63  	@Before
64  	public void setUp() throws Exception {
65  		super.setUp();
66  		try (Git git = new Git(db)) {
67  			git.commit().setMessage("initial commit").call();
68  		}
69  	}
70  
71  	@Test
72  	public void testHelpAfterDelete() throws Exception {
73  		String err = toString(executeUnchecked("git branch -d"));
74  		String help = toString(executeUnchecked("git branch -h"));
75  		String errAndHelp = toString(executeUnchecked("git branch -d -h"));
76  		assertEquals(CLIText.fatalError(CLIText.get().branchNameRequired), err);
77  		assertEquals(toString(err, help), errAndHelp);
78  	}
79  
80  	@Test
81  	public void testList() throws Exception {
82  		assertEquals("* master", toString(execute("git branch")));
83  		assertEquals("* master 6fd41be initial commit",
84  				toString(execute("git branch -v")));
85  	}
86  
87  	@Test
88  	public void testListDetached() throws Exception {
89  		RefUpdate updateRef = db.updateRef(Constants.HEAD, true);
90  		updateRef.setNewObjectId(db.resolve("6fd41be"));
91  		updateRef.update();
92  		assertEquals(
93  				toString("* (no branch) 6fd41be initial commit",
94  						"master      6fd41be initial commit"),
95  				toString(execute("git branch -v")));
96  	}
97  
98  	@Test
99  	public void testListContains() throws Exception {
100 		try (Git git = new Git(db)) {
101 			git.branchCreate().setName("initial").call();
102 			RevCommit second = git.commit().setMessage("second commit")
103 					.call();
104 			assertEquals(toString("  initial", "* master"),
105 					toString(execute("git branch --contains 6fd41be")));
106 			assertEquals("* master",
107 					toString(execute("git branch --contains " + second.name())));
108 		}
109 	}
110 
111 	@Test
112 	public void testExistingBranch() throws Exception {
113 		assertEquals("fatal: A branch named 'master' already exists.",
114 				toString(executeUnchecked("git branch master")));
115 	}
116 
117 	@Test
118 	public void testRenameSingleArg() throws Exception {
119 		try {
120 			toString(execute("git branch -m"));
121 			fail("Must die");
122 		} catch (Die e) {
123 			// expected, requires argument
124 		}
125 		String result = toString(execute("git branch -m slave"));
126 		assertEquals("", result);
127 		result = toString(execute("git branch -a"));
128 		assertEquals("* slave", result);
129 	}
130 
131 	@Test
132 	public void testRenameTwoArgs() throws Exception {
133 		String result = toString(execute("git branch -m master slave"));
134 		assertEquals("", result);
135 		result = toString(execute("git branch -a"));
136 		assertEquals("* slave", result);
137 	}
138 
139 	@Test
140 	public void testCreate() throws Exception {
141 		try {
142 			toString(execute("git branch a b"));
143 			fail("Must die");
144 		} catch (Die e) {
145 			// expected, too many arguments
146 		}
147 		String result = toString(execute("git branch second"));
148 		assertEquals("", result);
149 		result = toString(execute("git branch"));
150 		assertEquals(toString("* master", "second"), result);
151 		result = toString(execute("git branch -v"));
152 		assertEquals(toString("* master 6fd41be initial commit",
153 				"second 6fd41be initial commit"), result);
154 	}
155 
156 	@Test
157 	public void testDelete() throws Exception {
158 		try {
159 			toString(execute("git branch -d"));
160 			fail("Must die");
161 		} catch (Die e) {
162 			// expected, requires argument
163 		}
164 		String result = toString(execute("git branch second"));
165 		assertEquals("", result);
166 		result = toString(execute("git branch -d second"));
167 		assertEquals("", result);
168 		result = toString(execute("git branch"));
169 		assertEquals("* master", result);
170 	}
171 
172 	@Test
173 	public void testDeleteMultiple() throws Exception {
174 		String result = toString(execute("git branch second",
175 				"git branch third", "git branch fourth"));
176 		assertEquals("", result);
177 		result = toString(execute("git branch -d second third fourth"));
178 		assertEquals("", result);
179 		result = toString(execute("git branch"));
180 		assertEquals("* master", result);
181 	}
182 
183 	@Test
184 	public void testDeleteForce() throws Exception {
185 		try {
186 			toString(execute("git branch -D"));
187 			fail("Must die");
188 		} catch (Die e) {
189 			// expected, requires argument
190 		}
191 		String result = toString(execute("git branch second"));
192 		assertEquals("", result);
193 		result = toString(execute("git checkout second"));
194 		assertEquals("Switched to branch 'second'", result);
195 
196 		File a = writeTrashFile("a", "a");
197 		assertTrue(a.exists());
198 		execute("git add a", "git commit -m 'added a'");
199 
200 		result = toString(execute("git checkout master"));
201 		assertEquals("Switched to branch 'master'", result);
202 
203 		result = toString(execute("git branch"));
204 		assertEquals(toString("* master", "second"), result);
205 
206 		try {
207 			toString(execute("git branch -d second"));
208 			fail("Must die");
209 		} catch (Die e) {
210 			// expected, the current HEAD is on second and not merged to master
211 		}
212 		result = toString(execute("git branch -D second"));
213 		assertEquals("", result);
214 
215 		result = toString(execute("git branch"));
216 		assertEquals("* master", result);
217 	}
218 
219 	@Test
220 	public void testDeleteForceMultiple() throws Exception {
221 		String result = toString(execute("git branch second",
222 				"git branch third", "git branch fourth"));
223 
224 		assertEquals("", result);
225 		result = toString(execute("git checkout second"));
226 		assertEquals("Switched to branch 'second'", result);
227 
228 		File a = writeTrashFile("a", "a");
229 		assertTrue(a.exists());
230 		execute("git add a", "git commit -m 'added a'");
231 
232 		result = toString(execute("git checkout master"));
233 		assertEquals("Switched to branch 'master'", result);
234 
235 		result = toString(execute("git branch"));
236 		assertEquals(toString("fourth", "* master", "second", "third"), result);
237 
238 		try {
239 			toString(execute("git branch -d second third fourth"));
240 			fail("Must die");
241 		} catch (Die e) {
242 			// expected, the current HEAD is on second and not merged to master
243 		}
244 		result = toString(execute("git branch"));
245 		assertEquals(toString("fourth", "* master", "second", "third"), result);
246 
247 		result = toString(execute("git branch -D second third fourth"));
248 		assertEquals("", result);
249 
250 		result = toString(execute("git branch"));
251 		assertEquals("* master", result);
252 	}
253 
254 	@Test
255 	public void testCreateFromOldCommit() throws Exception {
256 		File a = writeTrashFile("a", "a");
257 		assertTrue(a.exists());
258 		execute("git add a", "git commit -m 'added a'");
259 		File b = writeTrashFile("b", "b");
260 		assertTrue(b.exists());
261 		execute("git add b", "git commit -m 'added b'");
262 		String result = toString(execute("git log -n 1 --reverse"));
263 		String firstCommitId = result.substring("commit ".length(),
264 				result.indexOf('\n'));
265 
266 		result = toString(execute("git branch -f second " + firstCommitId));
267 		assertEquals("", result);
268 
269 		result = toString(execute("git branch"));
270 		assertEquals(toString("* master", "second"), result);
271 
272 		result = toString(execute("git checkout second"));
273 		assertEquals("Switched to branch 'second'", result);
274 		assertFalse(b.exists());
275 	}
276 }