Skip to content

Commit 16ec6d0

Browse files
committed
fix: Fix StoredUserConfig handling null subsections
Te `StoredUserConfig` did not handle sections without a subsection. When the subsection did not exist, i.e. was `null`, then the subsection name would be set to the string "null". This is not how the config file format works. It should create a `[SECTIONNAME]` entry instead. This fix handles a `null` subsection correctly, by handling it as a section without a subsection.
1 parent 1c4fbc0 commit 16ec6d0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/main/java/com/gitblit/StoredUserConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ public void save() throws IOException {
7373
}
7474

7575
private static void writeSection(PrintWriter printWriter, String key, Section section) {
76-
printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
76+
if (section.getSubSection() == null) {
77+
printWriter.printf("[%s]\n", section.getName());
78+
}
79+
else {
80+
printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
81+
}
7782
for (Entry entry : section.getEntries().values()) {
7883
writeEntry(printWriter, entry.getKey(), entry.getValue());
7984
}
@@ -90,7 +95,7 @@ private static String escape(String value) {
9095
}
9196

9297
private static String generateKey(String key, String subKey) {
93-
return "k:" + key + "s:" + subKey;
98+
return "k:" + key + "s:" + (subKey == null ? "" : subKey);
9499
}
95100

96101
private static class Section {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.gitblit;
2+
3+
import org.eclipse.jgit.lib.StoredConfig;
4+
import org.eclipse.jgit.storage.file.FileBasedConfig;
5+
import org.eclipse.jgit.util.FS;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.io.File;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class StoredUserConfigTest
15+
{
16+
private static File file;
17+
18+
@Before
19+
public void setup()
20+
{
21+
file = new File("./suc-test.conf");
22+
file.delete();
23+
}
24+
25+
@After
26+
public void teardown()
27+
{
28+
file.delete();
29+
}
30+
31+
32+
33+
@Test
34+
public void testSection() throws Exception
35+
{
36+
StoredUserConfig config = new StoredUserConfig(file);
37+
config.setString("USER", "norman", "key", "value");
38+
config.setString("USER", "admin", "displayName", "marusha");
39+
config.setString("USER", null, "role", "none");
40+
41+
config.setString("TEAM", "admin", "role", "admin");
42+
config.setString("TEAM", "ci", "email", "[email protected]");
43+
config.setString("TEAM", null, "displayName", "noone");
44+
45+
config.save();
46+
47+
StoredConfig cfg = new FileBasedConfig(file, FS.detect());
48+
cfg.load();
49+
assertEquals("value", cfg.getString("USER", "norman", "key"));
50+
assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
51+
assertEquals("none", cfg.getString("USER", null, "role"));
52+
53+
assertEquals("admin", cfg.getString("TEAM", "admin", "role"));
54+
assertEquals("[email protected]", cfg.getString("TEAM", "ci", "email"));
55+
assertEquals("noone", cfg.getString("TEAM", null, "displayName"));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)