diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java index 6ebef6cbf..630bd7dc0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java @@ -519,6 +519,31 @@ public void test009_readNamesInSection() throws ConfigInvalidException { assertFalse(itr.hasNext()); } + @Test + public void test_ReadNamesInSectionRecursive() + throws ConfigInvalidException { + String baseConfigString = "[core]\n" + "logAllRefUpdates = true\n"; + String configString = "[core]\n" + "repositoryFormatVersion = 0\n" + + "filemode = false\n"; + final Config c = parse(configString, parse(baseConfigString)); + Set names = c.getNames("core", true); + assertEquals("Core section size", 3, names.size()); + assertTrue("Core section should contain \"filemode\"", + names.contains("filemode")); + assertTrue("Core section should contain \"repositoryFormatVersion\"", + names.contains("repositoryFormatVersion")); + assertTrue("Core section should contain \"logAllRefUpdates\"", + names.contains("logAllRefUpdates")); + assertTrue("Core section should contain \"logallrefupdates\"", + names.contains("logallrefupdates")); + + Iterator itr = names.iterator(); + assertEquals("filemode", itr.next()); + assertEquals("repositoryFormatVersion", itr.next()); + assertEquals("logAllRefUpdates", itr.next()); + assertFalse(itr.hasNext()); + } + @Test public void test010_readNamesInSubSection() throws ConfigInvalidException { String configString = "[a \"sub1\"]\n"// @@ -540,6 +565,30 @@ public void test010_readNamesInSubSection() throws ConfigInvalidException { assertTrue("Subsection should contain \"b\"", names.contains("b")); } + @Test + public void readNamesInSubSectionRecursive() throws ConfigInvalidException { + String baseConfigString = "[a \"sub1\"]\n"// + + "x = 0\n" // + + "y = false\n"// + + "[a \"sub2\"]\n"// + + "A=0\n";// + String configString = "[a \"sub1\"]\n"// + + "z = true\n"// + + "[a \"sub2\"]\n"// + + "B=1\n"; + final Config c = parse(configString, parse(baseConfigString)); + Set names = c.getNames("a", "sub1", true); + assertEquals("Subsection size", 3, names.size()); + assertTrue("Subsection should contain \"x\"", names.contains("x")); + assertTrue("Subsection should contain \"y\"", names.contains("y")); + assertTrue("Subsection should contain \"z\"", names.contains("z")); + names = c.getNames("a", "sub2", true); + assertEquals("Subsection size", 2, names.size()); + assertTrue("Subsection should contain \"A\"", names.contains("A")); + assertTrue("Subsection should contain \"a\"", names.contains("a")); + assertTrue("Subsection should contain \"B\"", names.contains("B")); + } + @Test public void testQuotingForSubSectionNames() { String resultPattern = "[testsection \"{0}\"]\n\ttestname = testvalue\n"; @@ -584,7 +633,12 @@ private static void assertReadLong(long exp, String act) private static Config parse(final String content) throws ConfigInvalidException { - final Config c = new Config(null); + return parse(content, null); + } + + private static Config parse(final String content, Config baseConfig) + throws ConfigInvalidException { + final Config c = new Config(baseConfig); c.fromText(content); return c; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java index 81977d74a..452ecddf4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java @@ -527,6 +527,33 @@ public Set getNames(String section, String subsection) { return getState().getNames(section, subsection); } + /** + * @param section + * the section + * @param recursive + * if {@code true} recursively adds the names defined in all base + * configurations + * @return the list of names defined for this section + */ + public Set getNames(String section, boolean recursive) { + return getState().getNames(section, null, recursive); + } + + /** + * @param section + * the section + * @param subsection + * the subsection + * @param recursive + * if {@code true} recursively adds the names defined in all base + * configurations + * @return the list of names defined for this subsection + */ + public Set getNames(String section, String subsection, + boolean recursive) { + return getState().getNames(section, subsection, recursive); + } + /** * Obtain a handle to a parsed set of configuration values. * diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigSnapshot.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigSnapshot.java index b7c4c6432..5ed129ed0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigSnapshot.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigSnapshot.java @@ -97,6 +97,16 @@ Set getSubsections(String section) { } Set getNames(String section, String subsection) { + return getNames(section, subsection, false); + } + + Set getNames(String section, String subsection, boolean recursive) { + Map m = getNamesInternal(section, subsection, recursive); + return new CaseFoldingSet(m); + } + + private Map getNamesInternal(String section, + String subsection, boolean recursive) { List s = sorted(); int idx = find(s, section, subsection, ""); //$NON-NLS-1$ if (idx < 0) @@ -113,7 +123,9 @@ Set getNames(String section, String subsection) { if (!m.containsKey(l)) m.put(l, e.name); } - return new CaseFoldingSet(m); + if (recursive && baseState != null) + m.putAll(baseState.getNamesInternal(section, subsection, recursive)); + return m; } String[] get(String section, String subsection, String name) {