diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java similarity index 93% rename from org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryConfigTest.java rename to org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java index 860d0d67f..e12e869ec 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java @@ -4,6 +4,7 @@ * Copyright (C) 2008, Marek Zawirski * Copyright (C) 2008, Robin Rosenberg * Copyright (C) 2008, Shawn O. Pearce + * Copyright (C) 2010, Mathias Kinzler * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -47,6 +48,7 @@ package org.eclipse.jgit.lib; +import java.text.MessageFormat; import java.util.Arrays; import java.util.LinkedList; import java.util.Set; @@ -61,7 +63,7 @@ import org.eclipse.jgit.util.SystemReader; /** * Test reading of git config */ -public class RepositoryConfigTest extends TestCase { +public class ConfigTest extends TestCase { public void test001_ReadBareKey() throws ConfigInvalidException { final Config c = parse("[foo]\nbar\n"); assertEquals(true, c.getBoolean("foo", null, "bar", false)); @@ -351,6 +353,28 @@ public class RepositoryConfigTest extends TestCase { assertTrue("Subsection should contain \"b\"", names.contains("b")); } + public void testQuotingForSubSectionNames() { + String resultPattern = "[testsection \"{0}\"]\n\ttestname = testvalue\n"; + String result; + + Config config = new Config(); + config.setString("testsection", "testsubsection", "testname", + "testvalue"); + + result = MessageFormat.format(resultPattern, "testsubsection"); + assertEquals(result, config.toText()); + config.clear(); + + config.setString("testsection", "#quotable", "testname", "testvalue"); + result = MessageFormat.format(resultPattern, "#quotable"); + assertEquals(result, config.toText()); + config.clear(); + + config.setString("testsection", "with\"quote", "testname", "testvalue"); + result = MessageFormat.format(resultPattern, "with\\\"quote"); + assertEquals(result, config.toText()); + } + private void assertReadLong(long exp) throws ConfigInvalidException { assertReadLong(exp, String.valueOf(exp)); } 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 335cada7a..884f49845 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java @@ -834,9 +834,15 @@ public class Config { out.append(e.section); if (e.subsection != null) { out.append(' '); - out.append('"'); - out.append(escapeValue(e.subsection)); - out.append('"'); + String escaped = escapeValue(e.subsection); + // make sure to avoid double quotes here + boolean quoted = escaped.startsWith("\"") + && escaped.endsWith("\""); + if (!quoted) + out.append('"'); + out.append(escaped); + if (!quoted) + out.append('"'); } out.append(']'); } else if (e.section != null && e.name != null) {