This is an automated email from the ASF dual-hosted git repository.

mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a5b68635a Git: Replace invalid chars in branch name when creating a 
new branch
     new 83eca9bd05 Merge pull request #4306 from 
troizet/git_replace_invalid_characters_in_branch_name
8a5b68635a is described below

commit 8a5b68635ae2e01c99ff0f922131722ba54ac394
Author: Alexey Borohvostov <troi...@gmail.com>
AuthorDate: Thu Jun 30 23:29:13 2022 +0700

    Git: Replace invalid chars in branch name when creating a new branch
---
 .../org/netbeans/modules/git/GitModuleConfig.java  |  9 ++++++
 .../netbeans/modules/git/options/Bundle.properties |  2 ++
 .../modules/git/options/GitOptionsPanel.form       | 37 ++++++++++++++++------
 .../modules/git/options/GitOptionsPanel.java       | 13 ++++++--
 .../git/options/GitOptionsPanelController.java     |  4 +++
 .../modules/git/ui/branch/CreateBranch.java        |  4 +++
 .../git/ui/checkout/AbstractCheckoutRevision.java  |  8 +++--
 .../org/netbeans/modules/git/utils/GitUtils.java   |  5 +++
 .../org/netbeans/modules/git/utils/JGitUtils.java  |  4 +++
 9 files changed, 73 insertions(+), 13 deletions(-)

diff --git a/ide/git/src/org/netbeans/modules/git/GitModuleConfig.java 
b/ide/git/src/org/netbeans/modules/git/GitModuleConfig.java
index 613d5e04dc..0961b02e07 100644
--- a/ide/git/src/org/netbeans/modules/git/GitModuleConfig.java
+++ b/ide/git/src/org/netbeans/modules/git/GitModuleConfig.java
@@ -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
@@ -202,6 +203,14 @@ public final class GitModuleConfig {
         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();
diff --git a/ide/git/src/org/netbeans/modules/git/options/Bundle.properties 
b/ide/git/src/org/netbeans/modules/git/options/Bundle.properties
index dc19a55adb..2b5aa1e6ab 100644
--- a/ide/git/src/org/netbeans/modules/git/options/Bundle.properties
+++ b/ide/git/src/org/netbeans/modules/git/options/Bundle.properties
@@ -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
diff --git a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.form 
b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.form
index b286867251..d1e8bf7b03 100644
--- a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.form
+++ b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.form
@@ -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">
@@ -68,6 +68,10 @@
                       </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>
@@ -75,15 +79,17 @@
     <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"/>
@@ -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">
diff --git a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.java 
b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.java
index b907255e3b..aeca802fb2 100644
--- a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.java
+++ b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanel.java
@@ -91,6 +91,9 @@ final class GitOptionsPanel extends javax.swing.JPanel {
         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);
@@ -127,7 +130,10 @@ final class GitOptionsPanel extends javax.swing.JPanel {
                                     .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)
@@ -140,7 +146,9 @@ final class GitOptionsPanel extends javax.swing.JPanel {
                 .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))
@@ -161,6 +169,7 @@ final class GitOptionsPanel extends javax.swing.JPanel {
     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;
diff --git 
a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanelController.java 
b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanelController.java
index 38f1819efb..fb68a8ed0e 100644
--- 
a/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanelController.java
+++ 
b/ide/git/src/org/netbeans/modules/git/options/GitOptionsPanelController.java
@@ -143,6 +143,7 @@ final class GitOptionsPanelController extends 
OptionsPanelController implements
             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);
         }
@@ -160,6 +161,7 @@ final class GitOptionsPanelController extends 
OptionsPanelController implements
     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());
@@ -169,6 +171,7 @@ final class GitOptionsPanelController extends 
OptionsPanelController implements
     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());
@@ -178,6 +181,7 @@ final class GitOptionsPanelController extends 
OptionsPanelController implements
     
     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()
diff --git a/ide/git/src/org/netbeans/modules/git/ui/branch/CreateBranch.java 
b/ide/git/src/org/netbeans/modules/git/ui/branch/CreateBranch.java
index 9a1575f4fc..e7552e9ede 100644
--- a/ide/git/src/org/netbeans/modules/git/ui/branch/CreateBranch.java
+++ b/ide/git/src/org/netbeans/modules/git/ui/branch/CreateBranch.java
@@ -37,6 +37,7 @@ import org.openide.DialogDescriptor;
 import org.openide.DialogDisplayer;
 import org.openide.util.HelpCtx;
 import org.openide.util.NbBundle;
+import org.netbeans.modules.git.GitModuleConfig;
 
 /**
  *
@@ -68,6 +69,9 @@ public class CreateBranch implements DocumentListener {
     }
     
     String getBranchName () {
+        if 
(GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters()) {
+            return 
GitUtils.normalizeBranchName(panel.branchNameField.getText());
+        }
         return panel.branchNameField.getText().trim();
     }
 
diff --git 
a/ide/git/src/org/netbeans/modules/git/ui/checkout/AbstractCheckoutRevision.java
 
b/ide/git/src/org/netbeans/modules/git/ui/checkout/AbstractCheckoutRevision.java
index ef67824d76..82dcf72447 100644
--- 
a/ide/git/src/org/netbeans/modules/git/ui/checkout/AbstractCheckoutRevision.java
+++ 
b/ide/git/src/org/netbeans/modules/git/ui/checkout/AbstractCheckoutRevision.java
@@ -30,6 +30,7 @@ import javax.swing.JButton;
 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;
@@ -69,7 +70,10 @@ public abstract class AbstractCheckoutRevision implements 
DocumentListener, Acti
     }
     
     String getBranchName () {
-        return panel.branchNameField.getText();
+        if 
(GitModuleConfig.getDefault().getAutoReplaceInvalidBranchNameCharacters()) {
+            return 
GitUtils.normalizeBranchName(panel.branchNameField.getText());
+        }
+        return panel.branchNameField.getText().trim();
     }
     
     boolean isCreateBranchSelected () {
@@ -167,7 +171,7 @@ public abstract class AbstractCheckoutRevision implements 
DocumentListener, Acti
     })
     private void validateName () {
         msgInvalidName = null;
-        branchName = panel.branchNameField.getText();
+        branchName = getBranchName();
         if (branchName.isEmpty()) {
             msgInvalidName = 
Bundle.MSG_CheckoutRevision_errorBranchNameEmpty();
         } else if (!GitUtils.isValidBranchName(branchName)) {
diff --git a/ide/git/src/org/netbeans/modules/git/utils/GitUtils.java 
b/ide/git/src/org/netbeans/modules/git/utils/GitUtils.java
index e2a4b50645..874a9268bc 100644
--- a/ide/git/src/org/netbeans/modules/git/utils/GitUtils.java
+++ b/ide/git/src/org/netbeans/modules/git/utils/GitUtils.java
@@ -1084,6 +1084,11 @@ public final class GitUtils {
     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 });
diff --git a/ide/git/src/org/netbeans/modules/git/utils/JGitUtils.java 
b/ide/git/src/org/netbeans/modules/git/utils/JGitUtils.java
index e24c710a4c..787804f4ec 100644
--- a/ide/git/src/org/netbeans/modules/git/utils/JGitUtils.java
+++ b/ide/git/src/org/netbeans/modules/git/utils/JGitUtils.java
@@ -55,6 +55,10 @@ public class JGitUtils {
         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;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to