[git] IDEA-257690 Use dash instead of underscore in the branch name conversion

Make it configurable via Advanced settings.

Remove deduplication logic IDEA-258145
Drop all symbols except space

GitOrigin-RevId: 9f113d9567c2647bddfe71ab04f1f0f8c6d1bee2
This commit is contained in:
Dmitriy Smirnov
2023-06-20 04:38:48 +00:00
committed by intellij-monorepo-bot
parent d90ae2049a
commit 4d60a1ad40
3 changed files with 29 additions and 32 deletions
@@ -504,6 +504,7 @@
<advancedSetting id="git.use.push.force.with.lease" default="true" groupKey="advanced.settings.git"/>
<advancedSetting id="git.update.incoming.outgoing.info" default="true" groupKey="advanced.settings.git"/>
<advancedSetting id="git.use.schannel.on.windows" default="false" groupKey="advanced.settings.git" os="windows"/>
<advancedSetting id="git.branch.cleanup.symbol" default="-" groupKey="advanced.settings.git"/>
<registryKey key="git.execute.with.mediator" defaultValue="true"
description="Use wrapper executable on Windows to support soft Git process cancellation."/>
<registryKey key="git.blocking.read" defaultValue="true"
@@ -1462,6 +1462,8 @@ advanced.setting.git.read.content.with=Apply content transformation when reading
advanced.setting.git.read.content.with.description=Use '--filters' or '--textconv' flags for 'git cat-file' command when reading file content from Git
advanced.setting.git.use.schannel.on.windows=Use Windows certificate store
advanced.setting.git.use.schannel.on.windows.description=Force Git to use Windows certificate store to verify SSL connections. Works for Git 2.14 and above.
advanced.setting.git.branch.cleanup.symbol=Branch name cleanup symbol
advanced.setting.git.branch.cleanup.symbol.description=Symbol that will be used instead prohibited symbols in git branch names
git.content.transform.none=None
git.content.transform.filters=Filters
git.content.transform.textconv=TextConv
@@ -1,20 +1,7 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package git4idea.validators;
import com.intellij.openapi.options.advanced.AdvancedSettings;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
@@ -42,20 +29,31 @@ public final class GitRefNameValidator implements InputValidator {
sb.append("]");
CONTROL_CHARS = sb.toString();
}
private static final Pattern ILLEGALCHARS = Pattern.compile(
private static final Pattern REPLACE = Pattern.compile(
" +|" + // space
CONTROL_CHARS
);
private static final Pattern DROP = Pattern.compile(
"(^\\.)|" + // begins with a dot
"(^-)|" + // begins with '-'
"(^/)|" + // begins with '/'
"(\\.\\.)+|" + // two dots in a row
"[ ~:^?*\\[\\\\]+|(@\\{)+|" + // contains invalid character: space, one of ~:^?*[\ or @{ sequence
CONTROL_CHARS // contains a control character
"[~:^?*\"\\[\\\\]+|(@\\{)+|" + // invalid character: one of ~:^?*"[\ or @{ sequence
"/(?=/)|" + // has a double slash
"(\\.(?=\\.))+|" + // two dots in a row
"\\.(?=/)|" + // has a dot before slash in the middle
"(?<=/)\\.|" // has a dot after slash in the middle
);
private static final Pattern ILLEGAL = Pattern.compile(
"(([./]|\\.lock)$)|" + // ends with dot, slash or ".lock"
"\\.(?=/)|" + // has a dot before slash in the middle
"(?<=/)\\.|" + // has a dot after slash in the middle
ILLEGALCHARS.pattern()
private static final Pattern ENDPATTERNS = Pattern.compile(
"(([./]|\\.lock)$)|" // ends with dot, slash or ".lock"
);
private static final Pattern VALIDATEPATTERNS = Pattern.compile(
DROP.pattern() +
ENDPATTERNS.pattern() +
REPLACE.pattern()
);
public static GitRefNameValidator getInstance() {
@@ -66,7 +64,7 @@ public final class GitRefNameValidator implements InputValidator {
@Override
public boolean checkInput(String inputString) {
return !StringUtil.isEmptyOrSpaces(inputString) && !ILLEGAL.matcher(inputString).find();
return !StringUtil.isEmptyOrSpaces(inputString) && !VALIDATEPATTERNS.matcher(inputString).find();
}
@Override
@@ -76,16 +74,12 @@ public final class GitRefNameValidator implements InputValidator {
@NotNull
public String cleanUpBranchName(@NotNull String branchName) {
return deduplicateChars(branchName.replaceAll(ILLEGAL.pattern(), "_").replaceAll("\"", ""));
return cleanUpBranchNameOnTyping(branchName).replaceAll(ENDPATTERNS.pattern(), "");
}
// On typing replace only space, drop other invalid chars.
@NotNull
public String cleanUpBranchNameOnTyping(@NotNull String branchName) {
return branchName.replaceAll(ILLEGALCHARS.pattern(), "_");
}
@NotNull
public String deduplicateChars(@NotNull String branchName) {
return branchName.replaceAll("(/){2,}", "/" ).replaceAll("(_){2,}", "_");
return branchName.replaceAll(DROP.pattern(), "").replaceAll(REPLACE.pattern(), AdvancedSettings.getString("git.branch.cleanup.symbol"));
}
}