Skip to content

Commit

Permalink
Git: Replace invalid chars in branch name when creating a new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
troizet committed Jul 1, 2022
1 parent 3e8a0a5 commit 8a5b686
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 13 deletions.
9 changes: 9 additions & 0 deletions ide/git/src/org/netbeans/modules/git/GitModuleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class GitModuleConfig {

private static GitModuleConfig instance;
private static final String AUTO_OPEN_OUTPUT_WINDOW = "autoOpenOutput"; // NOI18N
private static final String AUTO_REPLACE_INVALID_BRANCH_NAME_CHARACTERS = "autoReplaceInvalidBranchNameCharacters";
public static final String PROP_COMMIT_EXCLUSIONS = "commitExclusions"; // NOI18N
private static final String PROP_LAST_USED_MODE = "lastUsedMode"; // NOI18N
private static final String EXCLUDE_NEW_FILES = "excludeNewFiles"; // NOI18N
Expand Down Expand Up @@ -202,6 +203,14 @@ public void setAutoOpenOutput(boolean value) {
getPreferences().putBoolean(AUTO_OPEN_OUTPUT_WINDOW, value);
}

public boolean getAutoReplaceInvalidBranchNameCharacters() {
return getPreferences().getBoolean(AUTO_REPLACE_INVALID_BRANCH_NAME_CHARACTERS, false);
}

public void setAutoReplaceInvalidBranchNameCharacters(boolean value) {
getPreferences().putBoolean(AUTO_REPLACE_INVALID_BRANCH_NAME_CHARACTERS, value);
}

public void putRecentCommitAuthors(String author) {
if(author == null) return;
author = author.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ GitOptionsPanel.excludeNewFiles.AccessibleContext.accessibleName_1=
GitOptionsPanel.excludeNewFiles.AccessibleContext.accessibleName_2=
GitOptionsPanel.excludeNewFiles.AccessibleContext.accessibleDescription_2=
GitOptionsPanel.cbOpenOutputWindow.AccessibleContext.accessibleDescription_2=
GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.toolTipText=Special characters not allowed in branch names will be replaced by hyphens ('-') and spaces by underscores ('_'). leading and trailing slashes, dots, hyphens and underscores are removed.
GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.text=Automatically replace invalid characters in new branch name
37 changes: 28 additions & 9 deletions ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="12" max="-2" attributes="0"/>
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="txtProjectAnnotation" max="32767" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnAddVariable" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
Expand All @@ -68,22 +68,28 @@
</Group>
<EmptySpace min="-2" pref="12" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="cbReplaceInvalidBranchNameCharacters" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cbOpenOutputWindow" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="excludeNewFiles" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="signOffCheckBox" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="cbIgnoreNotSharableFiles" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="cbReplaceInvalidBranchNameCharacters" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<Component id="jSeparator1" min="-2" max="-2" attributes="0"/>
Expand Down Expand Up @@ -166,6 +172,19 @@
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="16"/>
</AuxValues>
</Component>
<Component class="javax.swing.JCheckBox" name="cbReplaceInvalidBranchNameCharacters">
<Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/git/options/Bundle.properties" key="GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
</Property>
<Property name="toolTipText" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/git/options/Bundle.properties" key="GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.toolTipText" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
</Property>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="16"/>
</AuxValues>
</Component>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
Expand Down
13 changes: 11 additions & 2 deletions ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ private void initComponents() {
org.openide.awt.Mnemonics.setLocalizedText(cbIgnoreNotSharableFiles, org.openide.util.NbBundle.getMessage(GitOptionsPanel.class, "GitOptionsPanel.cbIgnoreNotSharableFiles.text")); // NOI18N
cbIgnoreNotSharableFiles.setToolTipText(org.openide.util.NbBundle.getMessage(GitOptionsPanel.class, "GitOptionsPanel.cbIgnoreNotSharableFiles.toolTipText")); // NOI18N

org.openide.awt.Mnemonics.setLocalizedText(cbReplaceInvalidBranchNameCharacters, org.openide.util.NbBundle.getMessage(GitOptionsPanel.class, "GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.text")); // NOI18N
cbReplaceInvalidBranchNameCharacters.setToolTipText(org.openide.util.NbBundle.getMessage(GitOptionsPanel.class, "GitOptionsPanel.cbReplaceInvalidBranchNameCharacters.toolTipText")); // NOI18N

org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(GitOptionsPanel.class, "GitOptionsPanel.jLabel1.text")); // NOI18N

jLabel2.setLabelFor(txtProjectAnnotation);
Expand Down Expand Up @@ -127,7 +130,10 @@ private void initComponents() {
.addComponent(cbOpenOutputWindow)
.addComponent(cbIgnoreNotSharableFiles))
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(12, 12, 12))))
.addGap(12, 12, 12))
.addGroup(layout.createSequentialGroup()
.addComponent(cbReplaceInvalidBranchNameCharacters)
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
Expand All @@ -140,7 +146,9 @@ private void initComponents() {
.addComponent(signOffCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cbIgnoreNotSharableFiles)
.addGap(18, 18, 18)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cbReplaceInvalidBranchNameCharacters)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
Expand All @@ -161,6 +169,7 @@ private void initComponents() {
final javax.swing.JButton btnAddVariable = new javax.swing.JButton();
final javax.swing.JCheckBox cbIgnoreNotSharableFiles = new javax.swing.JCheckBox();
final javax.swing.JCheckBox cbOpenOutputWindow = new javax.swing.JCheckBox();
final javax.swing.JCheckBox cbReplaceInvalidBranchNameCharacters = new javax.swing.JCheckBox();
final javax.swing.JCheckBox excludeNewFiles = new javax.swing.JCheckBox();
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ private GitOptionsPanel getPanel() {
panel.cbOpenOutputWindow.addActionListener(this);
panel.excludeNewFiles.addActionListener(this);
panel.signOffCheckBox.addActionListener(this);
panel.cbReplaceInvalidBranchNameCharacters.addActionListener(this);
panel.txtProjectAnnotation.setText(GitModuleConfig.getDefault().getProjectAnnotationFormat());
panel.txtProjectAnnotation.getDocument().addDocumentListener(this);
}
Expand All @@ -160,6 +161,7 @@ private void changed () {
private void load () {
getPanel();
panel.cbOpenOutputWindow.setSelected(GitModuleConfig.getDefault().getAutoOpenOutput());
panel.cbReplaceInvalidBranchNameCharacters.setSelected(GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters());
panel.excludeNewFiles.setSelected(GitModuleConfig.getDefault().getExludeNewFiles());
panel.signOffCheckBox.setSelected(GitModuleConfig.getDefault().getSignOff());
panel.cbIgnoreNotSharableFiles.setSelected(GitModuleConfig.getDefault().getAutoIgnoreFiles());
Expand All @@ -169,6 +171,7 @@ private void load () {
private void store () {
getPanel();
GitModuleConfig.getDefault().setAutoOpenOutput(panel.cbOpenOutputWindow.isSelected());
GitModuleConfig.getDefault().setAutoReplaceInvalidBranchNameCharacters(panel.cbReplaceInvalidBranchNameCharacters.isSelected());
GitModuleConfig.getDefault().setExcludeNewFiles(panel.excludeNewFiles.isSelected());
GitModuleConfig.getDefault().setSignOff(panel.signOffCheckBox.isSelected());
GitModuleConfig.getDefault().setAutoIgnoreFiles(panel.cbIgnoreNotSharableFiles.isSelected());
Expand All @@ -178,6 +181,7 @@ private void store () {

private void fireChanged() {
changed = GitModuleConfig.getDefault().getAutoOpenOutput() != panel.cbOpenOutputWindow.isSelected()
|| GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters() != panel.cbReplaceInvalidBranchNameCharacters.isSelected()
|| GitModuleConfig.getDefault().getExludeNewFiles() != panel.excludeNewFiles.isSelected()
|| GitModuleConfig.getDefault().getSignOff() != panel.signOffCheckBox.isSelected()
|| GitModuleConfig.getDefault().getAutoIgnoreFiles() != panel.cbIgnoreNotSharableFiles.isSelected()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.openide.DialogDisplayer;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.netbeans.modules.git.GitModuleConfig;

/**
*
Expand Down Expand Up @@ -68,6 +69,9 @@ String getRevision () {
}

String getBranchName () {
if (GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters()) {
return GitUtils.normalizeBranchName(panel.branchNameField.getText());
}
return panel.branchNameField.getText().trim();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.libs.git.GitBranch;
import org.netbeans.modules.git.GitModuleConfig;
import org.netbeans.modules.git.ui.repository.RepositoryInfo;
import org.netbeans.modules.git.ui.repository.RevisionDialogController;
import org.netbeans.modules.git.utils.GitUtils;
Expand Down Expand Up @@ -69,7 +70,10 @@ String getRevision () {
}

String getBranchName () {
return panel.branchNameField.getText();
if (GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters()) {
return GitUtils.normalizeBranchName(panel.branchNameField.getText());
}
return panel.branchNameField.getText().trim();
}

boolean isCreateBranchSelected () {
Expand Down Expand Up @@ -167,7 +171,7 @@ public void actionPerformed (ActionEvent e) {
})
private void validateName () {
msgInvalidName = null;
branchName = panel.branchNameField.getText();
branchName = getBranchName();
if (branchName.isEmpty()) {
msgInvalidName = Bundle.MSG_CheckoutRevision_errorBranchNameEmpty();
} else if (!GitUtils.isValidBranchName(branchName)) {
Expand Down
5 changes: 5 additions & 0 deletions ide/git/src/org/netbeans/modules/git/utils/GitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,11 @@ public static boolean isValidTagName (String tagName) {
public static boolean isValidBranchName (String branchName) {
return isValidRefName(PREFIX_R_HEADS + branchName);
}

public static String normalizeBranchName (String refName) {
return JGitUtils.normalizeBranchName(refName);
}


public static VCSContext getContextForFile (final File root) {
return getContextForFiles(new File[] { root });
Expand Down
4 changes: 4 additions & 0 deletions ide/git/src/org/netbeans/modules/git/utils/JGitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static boolean isValidRefName (String refName) {
return Repository.isValidRefName(refName);
}

public static String normalizeBranchName (String refName) {
return Repository.normalizeBranchName(refName);
}

public static boolean isUserSetup (File root) {
Repository repository = getRepository(root);
boolean userExists = true;
Expand Down

0 comments on commit 8a5b686

Please sign in to comment.