diff options
author | Dave Borowitz <dborowitz@google.com> | 2017-11-20 13:55:25 -0500 |
---|---|---|
committer | Dave Borowitz <dborowitz@google.com> | 2017-11-20 13:55:25 -0500 |
commit | 8b3ab4343c4d34993176b5fa799a039e8114054b (patch) | |
tree | beeae1ed88b0253c579e81c5586204cb4e350dbe | |
parent | 3efea067a3db7e3bdfadd47d5420deaf9a35d740 (diff) | |
download | jgit-8b3ab4343c4d34993176b5fa799a039e8114054b.tar.gz jgit-8b3ab4343c4d34993176b5fa799a039e8114054b.zip |
Config: Handle leading/trailing single whitespaces
Change-Id: I468106acd2006d0a174c76dfd4bce231f1c7a6f8
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java | 52 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java | 22 |
2 files changed, 71 insertions, 3 deletions
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 e9505f67d0..748848e97f 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 @@ -965,4 +965,56 @@ public class ConfigTest { expectedEx.expect(IllegalArgumentException.class); parseTime("-1", MILLISECONDS); } + + @Test + public void testEscapeSpacesOnly() throws ConfigInvalidException { + assertEquals("", Config.escapeValue("")); + assertEquals("\" \"", Config.escapeValue(" ")); + assertEquals("\" \"", Config.escapeValue(" ")); + + assertParseRoundTrip(" "); + assertParseRoundTrip(" "); + } + + @Test + public void testEscapeLeadingSpace() throws ConfigInvalidException { + assertEquals("x", Config.escapeValue("x")); + assertEquals("\" x\"", Config.escapeValue(" x")); + assertEquals("\" x\"", Config.escapeValue(" x")); + + assertParseRoundTrip("x"); + assertParseRoundTrip(" x"); + assertParseRoundTrip(" x"); + } + + @Test + public void testEscapeTrailingSpace() throws ConfigInvalidException { + assertEquals("x", Config.escapeValue("x")); + assertEquals("\"x \"", Config.escapeValue("x ")); + assertEquals("x\" \"", Config.escapeValue("x ")); + + assertParseRoundTrip("x"); + assertParseRoundTrip("x "); + assertParseRoundTrip("x "); + } + + @Test + public void testEscapeLeadingAndTrailingSpace() + throws ConfigInvalidException { + assertEquals("\" x \"", Config.escapeValue(" x ")); + assertEquals("\" x \"", Config.escapeValue(" x ")); + assertEquals("\" x \"", Config.escapeValue(" x ")); + assertEquals("\" x \"", Config.escapeValue(" x ")); + + assertParseRoundTrip(" x "); + assertParseRoundTrip(" x "); + assertParseRoundTrip(" x "); + assertParseRoundTrip(" x "); + } + + private static void assertParseRoundTrip(String value) + throws ConfigInvalidException { + Config c = parse("[foo]\nbar = " + Config.escapeValue(value)); + assertEquals(value, c.getString("foo", null, "bar")); + } } 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 6281bcfb3d..77dfce1082 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java @@ -147,7 +147,10 @@ public class Config { * the value to escape * @return the escaped value */ - private static String escapeValue(final String x) { + static String escapeValue(final String x) { + if (x.isEmpty()) { + return ""; //$NON-NLS-1$ + } boolean inquote = false; int lineStart = 0; final StringBuilder r = new StringBuilder(x.length()); @@ -189,8 +192,7 @@ public class Config { break; case ' ': - if (!inquote && r.length() > 0 - && r.charAt(r.length() - 1) == ' ') { + if (!inquote && (r.length() == 0 || r.charAt(r.length() - 1) == ' ')) { r.insert(lineStart, '"'); inquote = true; } @@ -202,6 +204,20 @@ public class Config { break; } } + + if (!inquote) { + // Ensure any trailing whitespace is quoted. + int s = x.length(); + while (s > 0 && x.charAt(s - 1) == ' ') { + s--; + } + if (s != x.length()) { + // Can't insert at lineStart since there may be intervening quotes. + r.insert(s, '"'); + inquote = true; + } + } + if (inquote) { r.append('"'); } |