summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWim Jongman <wim.jongman@remainsoftware.com>2017-01-06 02:27:43 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2017-01-18 22:05:28 +0100
commitb667c182cbaf56fd4382362f042062343bac0856 (patch)
treeab774e7eeb0f69529683c9ba0014b1d5d3f409b5
parent8a46b603711145a05e5cd59010bc07c6f36dde41 (diff)
downloadjgit-b667c182cbaf56fd4382362f042062343bac0856.tar.gz
jgit-b667c182cbaf56fd4382362f042062343bac0856.zip
Normalizer creating a valid branch name from a string
Generic normalization method for a possible invalid branch name. The method compresses dividers between spaces, then replaces spaces and non word characters with underscores. This method is needed in preparation for subsequent EGit changes. Bug: 509878 Change-Id: Ic0d12f098f90f912a45bcc5693d6accf751d4e58 Signed-off-by: Wim Jongman <wim.jongman@remainsoftware.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ValidRefNameTest.java82
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java37
2 files changed, 119 insertions, 0 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 e9d46bb582..9c85fbe1ea 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
@@ -247,4 +247,86 @@ public class ValidRefNameTest {
assertValid(true, "refs/heads/conx");
assertValid(true, "refs/heads/xcon");
}
+
+ @Test
+ public void testNormalizeBranchName() {
+
+ assertEquals(true, Repository.normalizeBranchName("").equals(""));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("__@#$@#$@$____ _")
+ .equals(""));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("~`!@#$%^&*()_+}]{[|\\\";?>.<,/")
+ .equals(""));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 :::: Hello World")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 :::: Hello::: World")
+ .equals("Bug_12345-Hello-_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName(":::Bug 12345 - Hello World")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("---Bug 12345 - Hello World")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 ---- Hello --- World")
+ .equals("Bug_12345-Hello-World"));
+
+ assertEquals(true, Repository.normalizeBranchName(null) == null);
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 - Hello World!")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 : Hello World!")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 12345 _ Hello World!")
+ .equals("Bug_12345_Hello_World"));
+
+ assertEquals(true,
+ Repository
+ .normalizeBranchName("Bug 12345 - Hello World!")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName(" Bug 12345 - Hello World! ")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository
+ .normalizeBranchName(" Bug 12345 - Hello World! ")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository
+ .normalizeBranchName(
+ "Bug 12345 - Hello______ World!")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("_Bug 12345 - Hello World!")
+ .equals("Bug_12345-Hello_World"));
+
+ assertEquals(true,
+ Repository
+ .normalizeBranchName(
+ "Bug 12345 - Hello Wo!@#$%^&*(rld {@")
+ .equals("Bug_12345-Hello_World_"));
+
+ assertEquals(true,
+ Repository.normalizeBranchName("Bug 1#$ 2345 - Hello World")
+ .equals("Bug_12345-Hello_World"));
+ }
}
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 c5b2ef8e5b..641262cd50 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
@@ -4,6 +4,7 @@
* Copyright (C) 2006-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2006-2012, Shawn O. Pearce <spearce@spearce.org>
* Copyright (C) 2012, Daniel Megert <daniel_megert@ch.ibm.com>
+ * Copyright (C) 2017, Wim Jongman <wim.jongman@remainsoftware.com>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
@@ -1887,4 +1888,40 @@ public abstract class Repository implements AutoCloseable {
public void autoGC(ProgressMonitor monitor) {
// default does nothing
}
+
+ /**
+ * Normalizes the passed branch name into a possible valid branch name. The
+ * validity of the returned name should be checked by a subsequent call to
+ * {@link #isValidRefName(String)}.
+ * <p/>
+ * Future implementations of this method could be more restrictive or more
+ * lenient about the validity of specific characters in the returned name.
+ * <p/>
+ * 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.
+ *
+ * @param name
+ * The name to normalize.
+ *
+ * @return The normalized String or null if null was passed.
+ * @since 4.7
+ * @see #isValidRefName(String)
+ */
+ public static String normalizeBranchName(String name) {
+ if (name == null || name.length() == 0) {
+ return name;
+ }
+ String result = name.trim();
+ 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$
+ .replaceAll("_{2,}", "_") //$NON-NLS-1$//$NON-NLS-2$
+ .replaceAll("-{2,}", "-") //$NON-NLS-1$//$NON-NLS-2$
+ .replaceAll("[^\\w-]", "") //$NON-NLS-1$ //$NON-NLS-2$
+ .replaceAll("^_+", "") //$NON-NLS-1$//$NON-NLS-2$
+ .replaceAll("^-+", ""); //$NON-NLS-1$//$NON-NLS-2$
+ }
}