in org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java [1451:1518]
public static String normalizeBranchName(String name) {
if (name == null || name.isEmpty()) {
return ""; //$NON-NLS-1$
}
String result = name.trim();
String fullName = result.startsWith(Constants.R_HEADS) ? result
: Constants.R_HEADS + result;
if (isValidRefName(fullName)) {
return result;
}
// All Unicode blanks to underscore
result = result.replaceAll("(?:\\h|\\v)+", "_"); //$NON-NLS-1$ //$NON-NLS-2$
StringBuilder b = new StringBuilder();
char p = '/';
for (int i = 0, len = result.length(); i < len; i++) {
char c = result.charAt(i);
if (c < ' ' || c == 127) {
continue;
}
// Substitute a dash for problematic characters
switch (c) {
case '\\':
case '^':
case '~':
case ':':
case '?':
case '*':
case '[':
case '@':
case '<':
case '>':
case '|':
case '"':
c = '-';
break;
default:
break;
}
// Collapse multiple slashes, dashes, dots, underscores, and omit
// dashes, dots, and underscores following a slash.
switch (c) {
case '/':
if (p == '/') {
continue;
}
p = '/';
break;
case '.':
case '_':
case '-':
if (p == '/' || p == '-') {
continue;
}
p = '-';
break;
default:
p = c;
break;
}
b.append(c);
}
// Strip trailing special characters, and avoid the .lock extension
result = b.toString().replaceFirst("[/_.-]+$", "") //$NON-NLS-1$ //$NON-NLS-2$
.replaceAll("\\.lock($|/)", "_lock$1"); //$NON-NLS-1$ //$NON-NLS-2$
return FORBIDDEN_BRANCH_NAME_COMPONENTS.matcher(result)
.replaceAll("$1+$2$3"); //$NON-NLS-1$
}