View Javadoc
1   /*
2    * Copyright (C) 2012, 2014 IBM Corporation and others. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import java.io.File;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
21  import org.eclipse.jgit.lib.Constants;
22  import org.eclipse.jgit.lib.RefUpdate;
23  import org.eclipse.jgit.pgm.internal.CLIText;
24  import org.eclipse.jgit.revwalk.RevCommit;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class BranchTest extends CLIRepositoryTestCase {
29  	@Override
30  	@Before
31  	public void setUp() throws Exception {
32  		super.setUp();
33  		try (Git git = new Git(db)) {
34  			git.commit().setMessage("initial commit").call();
35  		}
36  	}
37  
38  	@Test
39  	public void testHelpAfterDelete() throws Exception {
40  		String err = toString(executeUnchecked("git branch -d"));
41  		String help = toString(executeUnchecked("git branch -h"));
42  		String errAndHelp = toString(executeUnchecked("git branch -d -h"));
43  		assertEquals(CLIText.fatalError(CLIText.get().branchNameRequired), err);
44  		assertEquals(toString(err, help), errAndHelp);
45  	}
46  
47  	@Test
48  	public void testList() throws Exception {
49  		assertEquals("* master", toString(execute("git branch")));
50  		assertEquals("* master 6fd41be initial commit",
51  				toString(execute("git branch -v")));
52  	}
53  
54  	@Test
55  	public void testListDetached() throws Exception {
56  		RefUpdate updateRef = db.updateRef(Constants.HEAD, true);
57  		updateRef.setNewObjectId(db.resolve("6fd41be"));
58  		updateRef.update();
59  		assertEquals(
60  				toString("* (no branch) 6fd41be initial commit",
61  						"master      6fd41be initial commit"),
62  				toString(execute("git branch -v")));
63  	}
64  
65  	@Test
66  	public void testListContains() throws Exception {
67  		try (Git git = new Git(db)) {
68  			git.branchCreate().setName("initial").call();
69  			RevCommit second = git.commit().setMessage("second commit")
70  					.call();
71  			assertEquals(toString("  initial", "* master"),
72  					toString(execute("git branch --contains 6fd41be")));
73  			assertEquals("* master",
74  					toString(execute("git branch --contains " + second.name())));
75  		}
76  	}
77  
78  	@Test
79  	public void testExistingBranch() throws Exception {
80  		assertEquals("fatal: A branch named 'master' already exists.",
81  				toString(executeUnchecked("git branch master")));
82  	}
83  
84  	@Test
85  	public void testRenameSingleArg() throws Exception {
86  		try {
87  			toString(execute("git branch -m"));
88  			fail("Must die");
89  		} catch (Die e) {
90  			// expected, requires argument
91  		}
92  		String result = toString(execute("git branch -m slave"));
93  		assertEquals("", result);
94  		result = toString(execute("git branch -a"));
95  		assertEquals("* slave", result);
96  	}
97  
98  	@Test
99  	public void testRenameTwoArgs() throws Exception {
100 		String result = toString(execute("git branch -m master slave"));
101 		assertEquals("", result);
102 		result = toString(execute("git branch -a"));
103 		assertEquals("* slave", result);
104 	}
105 
106 	@Test
107 	public void testCreate() throws Exception {
108 		try {
109 			toString(execute("git branch a b"));
110 			fail("Must die");
111 		} catch (Die e) {
112 			// expected, too many arguments
113 		}
114 		String result = toString(execute("git branch second"));
115 		assertEquals("", result);
116 		result = toString(execute("git branch"));
117 		assertEquals(toString("* master", "second"), result);
118 		result = toString(execute("git branch -v"));
119 		assertEquals(toString("* master 6fd41be initial commit",
120 				"second 6fd41be initial commit"), result);
121 	}
122 
123 	@Test
124 	public void testDelete() throws Exception {
125 		try {
126 			toString(execute("git branch -d"));
127 			fail("Must die");
128 		} catch (Die e) {
129 			// expected, requires argument
130 		}
131 		String result = toString(execute("git branch second"));
132 		assertEquals("", result);
133 		result = toString(execute("git branch -d second"));
134 		assertEquals("", result);
135 		result = toString(execute("git branch"));
136 		assertEquals("* master", result);
137 	}
138 
139 	@Test
140 	public void testDeleteMultiple() throws Exception {
141 		String result = toString(execute("git branch second",
142 				"git branch third", "git branch fourth"));
143 		assertEquals("", result);
144 		result = toString(execute("git branch -d second third fourth"));
145 		assertEquals("", result);
146 		result = toString(execute("git branch"));
147 		assertEquals("* master", result);
148 	}
149 
150 	@Test
151 	public void testDeleteForce() throws Exception {
152 		try {
153 			toString(execute("git branch -D"));
154 			fail("Must die");
155 		} catch (Die e) {
156 			// expected, requires argument
157 		}
158 		String result = toString(execute("git branch second"));
159 		assertEquals("", result);
160 		result = toString(execute("git checkout second"));
161 		assertEquals("Switched to branch 'second'", result);
162 
163 		File a = writeTrashFile("a", "a");
164 		assertTrue(a.exists());
165 		execute("git add a", "git commit -m 'added a'");
166 
167 		result = toString(execute("git checkout master"));
168 		assertEquals("Switched to branch 'master'", result);
169 
170 		result = toString(execute("git branch"));
171 		assertEquals(toString("* master", "second"), result);
172 
173 		try {
174 			toString(execute("git branch -d second"));
175 			fail("Must die");
176 		} catch (Die e) {
177 			// expected, the current HEAD is on second and not merged to master
178 		}
179 		result = toString(execute("git branch -D second"));
180 		assertEquals("", result);
181 
182 		result = toString(execute("git branch"));
183 		assertEquals("* master", result);
184 	}
185 
186 	@Test
187 	public void testDeleteForceMultiple() throws Exception {
188 		String result = toString(execute("git branch second",
189 				"git branch third", "git branch fourth"));
190 
191 		assertEquals("", result);
192 		result = toString(execute("git checkout second"));
193 		assertEquals("Switched to branch 'second'", result);
194 
195 		File a = writeTrashFile("a", "a");
196 		assertTrue(a.exists());
197 		execute("git add a", "git commit -m 'added a'");
198 
199 		result = toString(execute("git checkout master"));
200 		assertEquals("Switched to branch 'master'", result);
201 
202 		result = toString(execute("git branch"));
203 		assertEquals(toString("fourth", "* master", "second", "third"), result);
204 
205 		try {
206 			toString(execute("git branch -d second third fourth"));
207 			fail("Must die");
208 		} catch (Die e) {
209 			// expected, the current HEAD is on second and not merged to master
210 		}
211 		result = toString(execute("git branch"));
212 		assertEquals(toString("fourth", "* master", "second", "third"), result);
213 
214 		result = toString(execute("git branch -D second third fourth"));
215 		assertEquals("", result);
216 
217 		result = toString(execute("git branch"));
218 		assertEquals("* master", result);
219 	}
220 
221 	@Test
222 	public void testCreateFromOldCommit() throws Exception {
223 		File a = writeTrashFile("a", "a");
224 		assertTrue(a.exists());
225 		execute("git add a", "git commit -m 'added a'");
226 		File b = writeTrashFile("b", "b");
227 		assertTrue(b.exists());
228 		execute("git add b", "git commit -m 'added b'");
229 		String result = toString(execute("git log -n 1 --reverse"));
230 		String firstCommitId = result.substring("commit ".length(),
231 				result.indexOf('\n'));
232 
233 		result = toString(execute("git branch -f second " + firstCommitId));
234 		assertEquals("", result);
235 
236 		result = toString(execute("git branch"));
237 		assertEquals(toString("* master", "second"), result);
238 
239 		result = toString(execute("git checkout second"));
240 		assertEquals("Switched to branch 'second'", result);
241 		assertFalse(b.exists());
242 	}
243 }