]> source.dussan.org Git - gitblit.git/commitdiff
fix: Fix StoredUserConfig handling null subsections
authorFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 13 Mar 2022 16:48:19 +0000 (17:48 +0100)
committerFlorian Zschocke <f.zschocke+git@gmail.com>
Sun, 13 Mar 2022 16:48:19 +0000 (17:48 +0100)
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.

src/main/java/com/gitblit/StoredUserConfig.java
src/test/java/com/gitblit/StoredUserConfigTest.java [new file with mode: 0644]

index eae1d3cfd0bcb6d5243978f3933e0ef50ba4869d..63e1015ca2c1a98baa54c9343233e1a81d26dc9e 100644 (file)
@@ -73,7 +73,12 @@ public class StoredUserConfig {
        }
 
        private static void writeSection(PrintWriter printWriter, String key, Section section) {
-               printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
+               if (section.getSubSection() == null) {
+                       printWriter.printf("[%s]\n", section.getName());
+               }
+               else {
+                       printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
+               }
                for (Entry entry : section.getEntries().values()) {
                        writeEntry(printWriter, entry.getKey(), entry.getValue());
                }
@@ -90,7 +95,7 @@ public class StoredUserConfig {
        }
 
        private static String generateKey(String key, String subKey) {
-               return "k:" + key + "s:" + subKey;
+               return "k:" + key + "s:" + (subKey == null ? "" : subKey);
        }
 
        private static class Section {
diff --git a/src/test/java/com/gitblit/StoredUserConfigTest.java b/src/test/java/com/gitblit/StoredUserConfigTest.java
new file mode 100644 (file)
index 0000000..0196461
--- /dev/null
@@ -0,0 +1,58 @@
+package com.gitblit;
+
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.util.FS;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.*;
+
+public class StoredUserConfigTest
+{
+    private static File file;
+
+    @Before
+    public void setup()
+    {
+        file = new File("./suc-test.conf");
+        file.delete();
+    }
+
+    @After
+    public void teardown()
+    {
+        file.delete();
+    }
+
+
+
+    @Test
+    public void testSection() throws Exception
+    {
+        StoredUserConfig config = new StoredUserConfig(file);
+        config.setString("USER", "norman", "key", "value");
+        config.setString("USER", "admin", "displayName", "marusha");
+        config.setString("USER", null, "role", "none");
+
+        config.setString("TEAM", "admin", "role", "admin");
+        config.setString("TEAM", "ci", "email", "ci@example.com");
+        config.setString("TEAM", null, "displayName", "noone");
+
+        config.save();
+
+        StoredConfig cfg = new FileBasedConfig(file, FS.detect());
+        cfg.load();
+        assertEquals("value", cfg.getString("USER", "norman", "key"));
+        assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
+        assertEquals("none", cfg.getString("USER", null, "role"));
+
+        assertEquals("admin", cfg.getString("TEAM", "admin", "role"));
+        assertEquals("ci@example.com", cfg.getString("TEAM", "ci", "email"));
+        assertEquals("noone", cfg.getString("TEAM", null, "displayName"));
+    }
+
+}