From ce312d8afbb11e41bb33575f96e073eee7dcc6e8 Mon Sep 17 00:00:00 2001 From: Robin Stocker Date: Thu, 21 Aug 2014 23:10:55 +1000 Subject: [PATCH] Strip "<", ">", and "\n" from name/email in UserConfig This matches what C Git does, see "stripped" in `man git-commit-tree`. It also fixes the bug of the user where an user.email like "<>" would show up as "<<>>" in EGit. Bug: 439844 Change-Id: I567a3c620e191ce9d37d318417e63cb5d4483419 Signed-off-by: Robin Stocker --- .../tst/org/eclipse/jgit/lib/ConfigTest.java | 14 ++++++++++++++ .../src/org/eclipse/jgit/lib/UserConfig.java | 8 ++++++-- 2 files changed, 20 insertions(+), 2 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 d30c1911d..69926f915 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 @@ -213,6 +213,20 @@ public class ConfigTest { assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit()); } + @Test + public void testReadUserConfigWithInvalidCharactersStripped() { + final MockSystemReader mockSystemReader = new MockSystemReader(); + final Config localConfig = new Config(mockSystemReader.openUserConfig( + null, FS.DETECTED)); + + localConfig.setString("user", null, "name", "foo\nqux@example.com"); + + UserConfig userConfig = localConfig.get(UserConfig.KEY); + assertEquals("foobar", userConfig.getAuthorName()); + assertEquals("bazqux@example.com", userConfig.getAuthorEmail()); + } + @Test public void testReadBoolean_TrueFalse1() throws ConfigInvalidException { final Config c = parse("[s]\na = true\nb = false\n"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java index 60b4819ed..60ac6f176 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/UserConfig.java @@ -180,7 +180,7 @@ public class UserConfig { username = system().getenv(envKey); } - return username; + return stripInvalidCharacters(username); } /** @@ -204,7 +204,11 @@ public class UserConfig { email = system().getenv(envKey); } - return email; + return stripInvalidCharacters(email); + } + + private static String stripInvalidCharacters(String s) { + return s == null ? null : s.replaceAll("<|>|\n", ""); //$NON-NLS-1$//$NON-NLS-2$ } /**