]> source.dussan.org Git - jgit.git/commitdiff
Add recursive variant of Config.getNames() methods 29/19429/1
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 1 Dec 2013 00:19:35 +0000 (01:19 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Fri, 6 Dec 2013 13:27:42 +0000 (14:27 +0100)
These methods allow to find all configuration entry names for a given
section or section/subsection searching recursively through all base
configurations of the given configuration.

These methods are needed to calculate the names for the effective
configuration of a git repository which combines the configuration entry
names found in the repository, global and system configuration files

Bug: 396659
Change-Id: Ie3731b5e877f8686aadad3f1a46b2e583ad3b7c6
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigSnapshot.java

index 6ebef6cbf9ffebf4ba4f87b93609de937e030ab7..630bd7dc0d811a7f218e444b9e273cf2298a5f6c 100644 (file)
@@ -519,6 +519,31 @@ public class ConfigTest {
                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<String> 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<String> 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 class ConfigTest {
                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<String> 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 @@ public class ConfigTest {
 
        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;
        }
index 81977d74ac1799df1a8e3f2e970d927965379f3d..452ecddf42c5f14e61e530e1faa1f90ea24d9d31 100644 (file)
@@ -527,6 +527,33 @@ public class Config {
                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<String> 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<String> getNames(String section, String subsection,
+                       boolean recursive) {
+               return getState().getNames(section, subsection, recursive);
+       }
+
        /**
         * Obtain a handle to a parsed set of configuration values.
         *
index b7c4c6432180d8756d805f036486848e5acae151..5ed129ed02aaaf1cf88565e1c9cb60c3a19b5c60 100644 (file)
@@ -97,6 +97,16 @@ class ConfigSnapshot {
        }
 
        Set<String> getNames(String section, String subsection) {
+               return getNames(section, subsection, false);
+       }
+
+       Set<String> getNames(String section, String subsection, boolean recursive) {
+               Map<String, String> m = getNamesInternal(section, subsection, recursive);
+               return new CaseFoldingSet(m);
+       }
+
+       private Map<String, String> getNamesInternal(String section,
+                       String subsection, boolean recursive) {
                List<ConfigLine> s = sorted();
                int idx = find(s, section, subsection, ""); //$NON-NLS-1$
                if (idx < 0)
@@ -113,7 +123,9 @@ class ConfigSnapshot {
                        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) {