From f8d232213cb502214dc7d490b1bc612f88f0635f Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Tue, 7 Feb 2017 00:22:57 +0100 Subject: [PATCH] Branch normalizer should not normalize already valid branch names Change-Id: Ib746655e32a37c4ad323f1d12ac0817de8fa56cf Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/lib/ValidRefNameTest.java | 14 ++++++++++++++ .../src/org/eclipse/jgit/lib/Repository.java | 16 +++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ValidRefNameTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ValidRefNameTest.java index 9c85fbe1e..f069b65f6 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ValidRefNameTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ValidRefNameTest.java @@ -329,4 +329,18 @@ public class ValidRefNameTest { Repository.normalizeBranchName("Bug 1#$ 2345 - Hello World") .equals("Bug_12345-Hello_World")); } + + @Test + public void testNormalizeAlreadyValidRefName() { + assertEquals(true, + Repository.normalizeBranchName("refs/heads/m.a.s.t.e.r") + .equals("refs/heads/m.a.s.t.e.r")); + } + + @Test + public void testNormalizeTrimmedUnicodeAlreadyValidRefName() { + assertEquals(true, + Repository.normalizeBranchName(" \u00e5ngstr\u00f6m\t") + .equals("\u00e5ngstr\u00f6m")); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index 6be97ffda..c59224e3a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -1898,11 +1898,12 @@ public abstract class Repository implements AutoCloseable { * Future implementations of this method could be more restrictive or more * lenient about the validity of specific characters in the returned name. *

- * The current implementation returns a trimmed string only containing word - * characters ([a-zA-Z_0-9]) and hyphens ('-'). Colons are replaced by - * hyphens. Repeating underscores and hyphens are replaced by a single - * occurrence. Underscores and hyphens at the beginning of the string are - * removed. + * The current implementation returns the trimmed input string if this is + * already a valid branch name. Otherwise it returns a trimmed string only + * containing word characters ([a-zA-Z_0-9]) and hyphens ('-'). Colons are + * replaced by hyphens. Repeating underscores and hyphens are replaced by a + * single occurrence. Underscores and hyphens at the beginning of the string + * are removed. * * @param name * The name to normalize. @@ -1916,6 +1917,11 @@ public abstract class Repository implements AutoCloseable { return name; } String result = name.trim(); + String fullName = result.startsWith(Constants.R_HEADS) ? result + : Constants.R_HEADS + result; + if (isValidRefName(fullName)) { + return result; + } return result.replaceAll("\\s+([_:-])*?\\s+", "$1") //$NON-NLS-1$//$NON-NLS-2$ .replaceAll(":", "-") //$NON-NLS-1$//$NON-NLS-2$ .replaceAll("\\s+", "_") //$NON-NLS-1$//$NON-NLS-2$