1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
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
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
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
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
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 }