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 8737b697c..d5da16ad8 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 @@ -58,6 +58,7 @@ import java.text.MessageFormat; import java.util.Arrays; +import java.util.Iterator; import java.util.LinkedList; import java.util.Set; @@ -386,13 +387,25 @@ public void test008_readSectionNames() throws ConfigInvalidException { @Test public void test009_readNamesInSection() throws ConfigInvalidException { - String configString = "[core]\n" + "repositoryformatversion = 0\n" - + "filemode = false\n" + "logallrefupdates = true\n"; + String configString = "[core]\n" + "repositoryFormatVersion = 0\n" + + "filemode = false\n" + "logAllRefUpdates = true\n"; final Config c = parse(configString); Set names = c.getNames("core"); 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 \"repositoryformatversion\"", + names.contains("repositoryformatversion")); + + Iterator itr = names.iterator(); + assertEquals("repositoryFormatVersion", itr.next()); + assertEquals("filemode", itr.next()); + assertEquals("logAllRefUpdates", itr.next()); + assertFalse(itr.hasNext()); } @Test 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 daad67e29..ce86dc20f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java @@ -52,9 +52,12 @@ package org.eclipse.jgit.lib; import java.text.MessageFormat; +import java.util.AbstractSet; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -1327,39 +1330,71 @@ public boolean equals(Object obj) { } public Set parse(Config cfg) { - final Set result = new HashSet(); + final Map m = new LinkedHashMap(); while (cfg != null) { for (final Entry e : cfg.state.get().entryList) { - if (e.name != null - && StringUtils.equalsIgnoreCase(e.section, section)) { - if (subsection == null && e.subsection == null) - result.add(StringUtils.toLowerCase(e.name)); - else if (e.subsection != null - && e.subsection.equals(subsection)) - result.add(StringUtils.toLowerCase(e.name)); - + if (e.name == null) + continue; + if (!StringUtils.equalsIgnoreCase(section, e.section)) + continue; + if ((subsection == null && e.subsection == null) + || (subsection != null && subsection + .equals(e.subsection))) { + String lc = StringUtils.toLowerCase(e.name); + if (!m.containsKey(lc)) + m.put(lc, e.name); } } cfg = cfg.baseConfig; } - return Collections.unmodifiableSet(result); + return new CaseFoldingSet(m); } } private static class SectionNames implements SectionParser> { public Set parse(Config cfg) { - final Set result = new HashSet(); + final Map m = new LinkedHashMap(); while (cfg != null) { for (final Entry e : cfg.state.get().entryList) { - if (e.section != null) - result.add(StringUtils.toLowerCase(e.section)); + if (e.section != null) { + String lc = StringUtils.toLowerCase(e.section); + if (!m.containsKey(lc)) + m.put(lc, e.section); + } } cfg = cfg.baseConfig; } - return Collections.unmodifiableSet(result); + return new CaseFoldingSet(m); } } + private static class CaseFoldingSet extends AbstractSet { + private final Map names; + + CaseFoldingSet(Map names) { + this.names = Collections.unmodifiableMap(names); + } + + @Override + public boolean contains(Object needle) { + if (!(needle instanceof String)) + return false; + + String n = (String) needle; + return names.containsKey(n) + || names.containsKey(StringUtils.toLowerCase(n)); + } + + @Override + public Iterator iterator() { + return names.values().iterator(); + } + + @Override + public int size() { + return names.size(); + } + } private static class State { final List entryList;