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.junit.RepositoryTestCase;
53 import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
54 import org.eclipse.jgit.lib.ConfigConstants;
55 import org.eclipse.jgit.lib.Constants;
56 import org.eclipse.jgit.lib.StoredConfig;
57 import org.eclipse.jgit.revwalk.RevCommit;
58 import org.junit.Before;
59 import org.junit.Test;
60
61
62
63
64 public class RenameBranchCommandTest extends RepositoryTestCase {
65
66 private static final String PATH = "file.txt";
67
68 private RevCommit head;
69
70 private Git git;
71
72 @Override
73 @Before
74 public void setUp() throws Exception {
75 super.setUp();
76 git = Git.wrap(db);
77 writeTrashFile(PATH, "content");
78 git.add().addFilepattern(PATH).call();
79 head = git.commit().setMessage("add file").call();
80 assertNotNull(head);
81 }
82
83 @Test
84 public void renameBranchNoConfigValues() throws Exception {
85 StoredConfig config = git.getRepository().getConfig();
86 config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
87 Constants.MASTER);
88 config.save();
89
90 String branch = "b1";
91 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
92 Constants.MASTER).isEmpty());
93 assertNotNull(git.branchRename().setNewName(branch).call());
94
95 config = git.getRepository().getConfig();
96 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
97 Constants.MASTER).isEmpty());
98 assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
99 branch).isEmpty());
100 }
101
102 @Test
103 public void renameBranchSingleConfigValue() throws Exception {
104 StoredConfig config = git.getRepository().getConfig();
105 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
106 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
107 config.save();
108
109 String branch = "b1";
110
111 assertEquals(BranchRebaseMode.REBASE,
112 config.getEnum(BranchRebaseMode.values(),
113 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
114 ConfigConstants.CONFIG_KEY_REBASE,
115 BranchRebaseMode.NONE));
116 assertNull(config.getEnum(BranchRebaseMode.values(),
117 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
118 ConfigConstants.CONFIG_KEY_REBASE, null));
119
120 assertNotNull(git.branchRename().setNewName(branch).call());
121
122 config = git.getRepository().getConfig();
123 assertNull(config.getEnum(BranchRebaseMode.values(),
124 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
125 ConfigConstants.CONFIG_KEY_REBASE, null));
126 assertEquals(BranchRebaseMode.REBASE,
127 config.getEnum(BranchRebaseMode.values(),
128 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
129 ConfigConstants.CONFIG_KEY_REBASE,
130 BranchRebaseMode.NONE));
131 }
132
133 @Test
134 public void renameBranchExistingSection() throws Exception {
135 String branch = "b1";
136 StoredConfig config = git.getRepository().getConfig();
137 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
138 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
139 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
140 Constants.MASTER, "a", "a");
141 config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
142 "b");
143 config.save();
144
145 assertNotNull(git.branchRename().setNewName(branch).call());
146
147 config = git.getRepository().getConfig();
148 assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
149 ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
150 }
151
152 @Test
153 public void renameBranchMultipleConfigValues() throws Exception {
154 StoredConfig config = git.getRepository().getConfig();
155 config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
156 ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
157 config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
158 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
159 config.save();
160
161 String branch = "b1";
162
163 assertEquals(BranchRebaseMode.REBASE,
164 config.getEnum(BranchRebaseMode.values(),
165 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
166 ConfigConstants.CONFIG_KEY_REBASE,
167 BranchRebaseMode.NONE));
168 assertNull(config.getEnum(BranchRebaseMode.values(),
169 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
170 ConfigConstants.CONFIG_KEY_REBASE, null));
171 assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
172 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
173 assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
174 branch, ConfigConstants.CONFIG_KEY_MERGE, false));
175
176 assertNotNull(git.branchRename().setNewName(branch).call());
177
178 config = git.getRepository().getConfig();
179 assertNull(config.getEnum(BranchRebaseMode.values(),
180 ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
181 ConfigConstants.CONFIG_KEY_REBASE, null));
182 assertEquals(BranchRebaseMode.REBASE,
183 config.getEnum(BranchRebaseMode.values(),
184 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
185 ConfigConstants.CONFIG_KEY_REBASE,
186 BranchRebaseMode.NONE));
187 assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
188 Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
189 assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
190 branch, ConfigConstants.CONFIG_KEY_MERGE, false));
191 }
192 }