From 8900297179f62353866e99877d26ddc2376771a1 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sun, 18 Nov 2007 23:12:53 +0100 Subject: [PATCH] Introduce subsection as a separate argument to the config API Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/lib/CoreConfig.java | 6 +-- .../src/org/spearce/jgit/lib/GitIndex.java | 2 +- .../src/org/spearce/jgit/lib/PersonIdent.java | 4 +- .../src/org/spearce/jgit/lib/Repository.java | 2 +- .../src/org/spearce/jgit/lib/RepositoryConfig.java | 44 +++++++++++++--------- .../org/spearce/jgit/lib/RepositoryConfigTest.java | 18 ++++----- .../tst/org/spearce/jgit/lib/T0003_Basic.java | 30 +++++++-------- 7 files changed, 57 insertions(+), 49 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/CoreConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/CoreConfig.java index d88f5a34..04b55019 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/CoreConfig.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/CoreConfig.java @@ -24,9 +24,9 @@ public class CoreConfig { private final boolean legacyHeaders; protected CoreConfig(final RepositoryConfig rc) { - compression = rc.getInt("core", "compression", - Deflater.DEFAULT_COMPRESSION); - legacyHeaders = rc.getBoolean("core", "legacyHeaders", false); + compression = rc.getInt("core", null, + "compression", Deflater.DEFAULT_COMPRESSION); + legacyHeaders = rc.getBoolean("core", null, "legacyHeaders", false); } public int getCompression() { diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java b/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java index 47fd0cff..91bde305 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/GitIndex.java @@ -258,7 +258,7 @@ public class GitIndex { if (filemode != null) return filemode.booleanValue(); RepositoryConfig config = db.getConfig(); - return config.getBoolean("core", "filemode", true); + return config.getBoolean("core", null, "filemode", true); } public class Entry { diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java index c1343787..02eb6ff5 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java @@ -45,11 +45,11 @@ public class PersonIdent { */ public PersonIdent(final Repository repo) { RepositoryConfig config = repo.getConfig(); - String username = config.getString("user", "name"); + String username = config.getString("user", null, "name"); if (username == null) username = System.getProperty("user.name"); - String email = config.getString("user", "email"); + String email = config.getString("user", null, "email"); if (email == null) email = System.getProperty("user.name") + "@" + getHostName(); diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java index dfbabdc4..a7f1f330 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java @@ -70,7 +70,7 @@ public class Repository { if (objectsDirs[0].exists()) { getConfig().load(); final String repositoryFormatVersion = getConfig().getString( - "core", "repositoryFormatVersion"); + "core", null, "repositoryFormatVersion"); if (!"0".equals(repositoryFormatVersion)) { throw new IOException("Unknown repository format \"" + repositoryFormatVersion + "\"; expected \"0\"."); diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java index fd3eed95..43c1bfa3 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java @@ -93,44 +93,46 @@ public class RepositoryConfig { } /** - * @param group + * @param section + * @param subsection * @param name * @param defaultValue * @return an integer value from the git config */ - public int getInt(final String group, final String name, - final int defaultValue) { - final String n = getString(group, name); + public int getInt(final String section, String subsection, + final String name, final int defaultValue) { + final String n = getString(section, subsection, name); if (n == null) { if (repo == null) return defaultValue; - return getGlobalConfig().getInt(group, name, defaultValue); + return getGlobalConfig().getInt(section, subsection, name, defaultValue); } try { return Integer.parseInt(n); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("Invalid integer value: " - + group + "." + name + "=" + n); + + section + "." + name + "=" + n); } } /** * Get a boolean value from the git config * - * @param group + * @param section + * @param subsection * @param name * @param defaultValue * @return true if any value or defaultValue is true, false for missing or * explicit fallse */ - public boolean getBoolean(final String group, final String name, - final boolean defaultValue) { - String n = getRawString(group, name); + protected boolean getBoolean(final String section, String subsection, + final String name, final boolean defaultValue) { + String n = getRawString(section, subsection, name); if (n == null) { if (repo == null) return defaultValue; - return getGlobalConfig().getBoolean(group, name, defaultValue); + return getGlobalConfig().getBoolean(section, subsection, name, defaultValue); } n = n.toLowerCase(); @@ -140,26 +142,32 @@ public class RepositoryConfig { return false; } else { throw new IllegalArgumentException("Invalid boolean value: " - + group + "." + name + "=" + n); + + section + "." + name + "=" + n); } } /** - * @param group + * @param section + * @param subsection * @param name * @return a String value from git config. */ - public String getString(final String group, final String name) { - String val = getRawString(group, name); + public String getString(final String section, String subsection, final String name) { + String val = getRawString(section, subsection, name); if (MAGIC_EMPTY_VALUE.equals(val)) { return ""; } return val; } - private String getRawString(final String group, final String name) { + private String getRawString(final String section, String subsection, final String name) { + String ss; + if (subsection != null) + ss = "."+subsection.toLowerCase(); + else + ss = ""; final Object o; - o = byName.get(group.toLowerCase() + "." + name.toLowerCase()); + o = byName.get(section.toLowerCase() + ss + "." + name.toLowerCase()); if (o instanceof List) { return ((Entry) ((List) o).get(0)).value; } else if (o instanceof Entry) { @@ -167,7 +175,7 @@ public class RepositoryConfig { } else { if (repo == null) return null; - return getGlobalConfig().getString(group, name); + return getGlobalConfig().getString(section, subsection, name); } } diff --git a/org.spearce.jgit/tst/org/spearce/jgit/lib/RepositoryConfigTest.java b/org.spearce.jgit/tst/org/spearce/jgit/lib/RepositoryConfigTest.java index 2e3c51cb..2fc4e896 100644 --- a/org.spearce.jgit/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +++ b/org.spearce.jgit/tst/org/spearce/jgit/lib/RepositoryConfigTest.java @@ -14,9 +14,9 @@ public class RepositoryConfigTest extends RepositoryTestCase { public void test001_ReadBareKey() throws IOException { String path = writeTrashFile("config_001", "[foo]\nbar\n").getAbsolutePath(); RepositoryConfig repositoryConfig = new RepositoryConfig(path); - System.out.println(repositoryConfig.getString("foo", "bar")); - assertEquals(true, repositoryConfig.getBoolean("foo", "bar", false)); - assertEquals("", repositoryConfig.getString("foo", "bar")); + System.out.println(repositoryConfig.getString("foo", null, "bar")); + assertEquals(true, repositoryConfig.getBoolean("foo", null, "bar", false)); + assertEquals("", repositoryConfig.getString("foo", null, "bar")); } /** @@ -27,11 +27,11 @@ public class RepositoryConfigTest extends RepositoryTestCase { public void test002_ReadWithSubsection() throws IOException { String path = writeTrashFile("config_002", "[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n").getAbsolutePath(); RepositoryConfig repositoryConfig = new RepositoryConfig(path); - assertEquals(true, repositoryConfig.getBoolean("foo.zip", "bar", false)); - assertEquals("", repositoryConfig.getString("foo.zip", "bar")); - assertEquals(false, repositoryConfig.getBoolean("foo.zap", "bar", true)); - assertEquals("false", repositoryConfig.getString("foo.zap", "bar")); - assertEquals(3, repositoryConfig.getInt("foo.zap", "n", 4)); - assertEquals(4, repositoryConfig.getInt("foo.zap", "m",4)); + assertEquals(true, repositoryConfig.getBoolean("foo", "zip", "bar", false)); + assertEquals("", repositoryConfig.getString("foo","zip", "bar")); + assertEquals(false, repositoryConfig.getBoolean("foo", "zap", "bar", true)); + assertEquals("false", repositoryConfig.getString("foo", "zap", "bar")); + assertEquals(3, repositoryConfig.getInt("foo", "zap", "n", 4)); + assertEquals(4, repositoryConfig.getInt("foo", "zap","m", 4)); } } diff --git a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java index bd861197..1e7efba4 100644 --- a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java +++ b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java @@ -100,11 +100,11 @@ public class T0003_Basic extends RepositoryTestCase { public void test004_CheckNewConfig() throws IOException { final RepositoryConfig c = db.getConfig(); assertNotNull(c); - assertEquals("0", c.getString("core", "repositoryformatversion")); - assertEquals("0", c.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN")); - assertEquals("true", c.getString("core", "filemode")); - assertEquals("true", c.getString("cOrE", "fIlEModE")); - assertNull(c.getString("notavalue", "reallyNotAValue")); + assertEquals("0", c.getString("core", null, "repositoryformatversion")); + assertEquals("0", c.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN")); + assertEquals("true", c.getString("core", null, "filemode")); + assertEquals("true", c.getString("cOrE", null, "fIlEModE")); + assertNull(c.getString("notavalue", null, "reallyNotAValue")); c.load(); } @@ -112,11 +112,11 @@ public class T0003_Basic extends RepositoryTestCase { final RepositoryConfig c = db.getConfig(); assertNotNull(c); c.load(); - assertEquals("0", c.getString("core", "repositoryformatversion")); - assertEquals("0", c.getString("CoRe", "REPOSITORYFoRmAtVeRsIoN")); - assertEquals("true", c.getString("core", "filemode")); - assertEquals("true", c.getString("cOrE", "fIlEModE")); - assertNull(c.getString("notavalue", "reallyNotAValue")); + assertEquals("0", c.getString("core", null, "repositoryformatversion")); + assertEquals("0", c.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN")); + assertEquals("true", c.getString("core", null, "filemode")); + assertEquals("true", c.getString("cOrE", null, "fIlEModE")); + assertNull(c.getString("notavalue", null, "reallyNotAValue")); } public void test006_ReadUglyConfig() throws IOException { @@ -132,12 +132,12 @@ public class T0003_Basic extends RepositoryTestCase { pw.write(configStr); pw.close(); c.load(); - assertEquals("yes", c.getString("core", "filemode")); + assertEquals("yes", c.getString("core", null, "filemode")); assertEquals("A U Thor ", c - .getString("user", "email")); - assertEquals("A Thor \\ \"\t ", c.getString("user", "name")); + .getString("user", null, "email")); + assertEquals("A Thor \\ \"\t ", c.getString("user", null, "name")); assertEquals("a many line\ncomment\n to test", c.getString("user", - "defaultCheckInComment")); + null, "defaultCheckInComment")); c.save(); final FileReader fr = new FileReader(cfg); final char[] cbuf = new char[configStr.length()]; @@ -401,7 +401,7 @@ public class T0003_Basic extends RepositoryTestCase { assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().toString()); } - public void test023_createCommitNonAscii() throws IOException { + public void test023_createCommitNonAnullii() throws IOException { final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]); final Tree almostEmptyTree = new Tree(db); almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false)); -- 2.11.4.GIT