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