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.api;
44
45 import static org.junit.Assert.assertArrayEquals;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51
52 import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
53 import org.eclipse.jgit.junit.RepositoryTestCase;
54 import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
55 import org.eclipse.jgit.lib.ConfigConstants;
56 import org.eclipse.jgit.lib.Constants;
57 import org.eclipse.jgit.lib.Ref;
58 import org.eclipse.jgit.lib.StoredConfig;
59 import org.eclipse.jgit.revwalk.RevCommit;
60 import org.junit.Before;
61 import org.junit.Rule;
62 import org.junit.Test;
63 import org.junit.rules.ExpectedException;
64
65
66
67
68 public class RenameBranchCommandTest extends RepositoryTestCase {
69
70 private static final String PATH = "file.txt";
71
72 private RevCommit head;
73
74 private Git git;
75
76 @Rule
77 public ExpectedException thrown = ExpectedException.none();
78
79 @Override
80 @Before
81 public void setUp() throws Exception {
82 super.setUp();
83 git = Git.wrap(db);
84 writeTrashFile(PATH, "content");
85 git.add().addFilepattern(PATH).call();
86 head = git.commit().setMessage("add file").call();
87 assertNotNull(head);
88 }
89
90 @Test
91 public void renameToExisting() throws Exception {
92 assertNotNull(git.branchCreate().setName("foo").call());
93 thrown.expect(RefAlreadyExistsException.class);
94 git.branchRename().setOldName("master").setNewName("foo").call();
95 }
96
97 @Test
98 public void renameToTag() throws Exception {
99 Ref ref = git.tag().setName("foo").call();
100 assertNotNull(ref);
101 assertEquals("Unexpected tag name", Constants.R_TAGS + "foo",
102 ref.getName());
103 ref = git.branchRename().setNewName("foo").call();
104 assertNotNull(ref);
105 assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
106 ref.getName());
107
108 ref = git.branchRename().setOldName("foo").setNewName(Constants.MASTER)
109 .call();
110 assertNotNull(ref);
111 assertEquals("Unexpected ref name",
112 Constants.R_HEADS + Constants.MASTER, ref.getName());
113 }
114
115 @Test
116 public void renameToStupidName() throws Exception {
117 Ref ref = git.branchRename().setNewName(Constants.R_HEADS + "foo")
118 .call();
119 assertEquals("Unexpected ref name",
120 Constants.R_HEADS + Constants.R_HEADS + "foo",
121 ref.getName());
122
123 ref = git.branchRename().setNewName("foo").call();
124 assertNotNull(ref);
125 assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
126 ref.getName());
127 }
128
129 @Test
130 public void renameBranchNoConfigValues() throws Exception {
131 StoredConfig config = git.getRepository().getConfig();
132 config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
133 Constants.MASTER);
134 config.save();
135
136 String branch = "b1";
137 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
138 Constants.MASTER).isEmpty());
139 assertNotNull(git.branchRename().setNewName(branch).call());
140
141 config = git.getRepository().getConfig();
142 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
143 Constants.MASTER).isEmpty());
144 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
145 branch).isEmpty());
146 }
147
148 @Test
149 public void renameBranchSingleConfigValue() throws Exception {
150 StoredConfig config = git.getRepository().getConfig();
151 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
152 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
153 config.save();
154
155 String branch = "b1";
156
157 assertEquals(BranchRebaseMode.REBASE,
158 config.getEnum(BranchRebaseMode.values(),
159 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
160 ConfigConstants.CONFIG_KEY_REBASE,
161 BranchRebaseMode.NONE));
162 assertNull(config.getEnum(BranchRebaseMode.values(),
163 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
164 ConfigConstants.CONFIG_KEY_REBASE, null));
165
166 assertNotNull(git.branchRename().setNewName(branch).call());
167
168 config = git.getRepository().getConfig();
169 assertNull(config.getEnum(BranchRebaseMode.values(),
170 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
171 ConfigConstants.CONFIG_KEY_REBASE, null));
172 assertEquals(BranchRebaseMode.REBASE,
173 config.getEnum(BranchRebaseMode.values(),
174 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
175 ConfigConstants.CONFIG_KEY_REBASE,
176 BranchRebaseMode.NONE));
177 }
178
179 @Test
180 public void renameBranchExistingSection() throws Exception {
181 String branch = "b1";
182 StoredConfig config = git.getRepository().getConfig();
183 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
184 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
185 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
186 Constants.MASTER, "a", "a");
187 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
188 "b");
189 config.save();
190
191 assertNotNull(git.branchRename().setNewName(branch).call());
192
193 config = git.getRepository().getConfig();
194 assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
195 ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
196 }
197
198 @Test
199 public void renameBranchMultipleConfigValues() throws Exception {
200 StoredConfig config = git.getRepository().getConfig();
201 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
202 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
203 config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
204 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
205 config.save();
206
207 String branch = "b1";
208
209 assertEquals(BranchRebaseMode.REBASE,
210 config.getEnum(BranchRebaseMode.values(),
211 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
212 ConfigConstants.CONFIG_KEY_REBASE,
213 BranchRebaseMode.NONE));
214 assertNull(config.getEnum(BranchRebaseMode.values(),
215 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
216 ConfigConstants.CONFIG_KEY_REBASE, null));
217 assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
218 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
219 assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
220 branch, ConfigConstants.CONFIG_KEY_MERGE, false));
221
222 assertNotNull(git.branchRename().setNewName(branch).call());
223
224 config = git.getRepository().getConfig();
225 assertNull(config.getEnum(BranchRebaseMode.values(),
226 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
227 ConfigConstants.CONFIG_KEY_REBASE, null));
228 assertEquals(BranchRebaseMode.REBASE,
229 config.getEnum(BranchRebaseMode.values(),
230 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
231 ConfigConstants.CONFIG_KEY_REBASE,
232 BranchRebaseMode.NONE));
233 assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
234 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
235 assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
236 branch, ConfigConstants.CONFIG_KEY_MERGE, false));
237 }
238 }