matthiasblaesing commented on code in PR #8653:
URL: https://github.com/apache/netbeans/pull/8653#discussion_r2213854386


##########
ide/libs.git/src/org/netbeans/libs/git/GitClient.java:
##########
@@ -1141,6 +1142,20 @@ public void rename (File source, File target, boolean 
after, ProgressMonitor mon
         cmd.execute();
     }
     
+    /**
+     * Renames a branch in the repository
+     * @param oldName current branch name
+     * @param newName desired branch name
+     * @param monitor progress monitor
+     * @return renamed branch information
+     * @throws GitException an unexpected error occurs
+     */
+    public GitBranch renameBranch (String oldName, String newName, 
ProgressMonitor monitor) throws GitException {

Review Comment:
   What is the motivation for the return value?



##########
ide/libs.git/src/org/netbeans/libs/git/jgit/commands/RenameBranchCommand.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.netbeans.libs.git.jgit.commands;
+
+import java.util.Map;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.errors.JGitInternalException;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.netbeans.libs.git.GitBranch;
+import org.netbeans.libs.git.GitException;
+import org.netbeans.libs.git.jgit.DelegatingGitProgressMonitor;
+import org.netbeans.libs.git.jgit.GitClassFactory;
+import org.netbeans.libs.git.progress.ProgressMonitor;
+
+/**
+ * @author Christian Lenz
+ * Renames an existing branch.
+ */
+public class RenameBranchCommand extends GitCommand {
+    private final String oldName;
+    private final String newName;
+    private final ProgressMonitor monitor;
+    private GitBranch branch;
+
+    public RenameBranchCommand(Repository repository, GitClassFactory 
gitFactory,
+            String oldName, String newName, ProgressMonitor monitor) {
+        super(repository, gitFactory, monitor);
+        this.oldName = oldName;
+        this.newName = newName;
+        this.monitor = monitor;
+    }
+
+    @Override
+    protected void run() throws GitException {
+        Repository repository = getRepository();
+        try {
+            Ref ref = new Git(repository).branchRename()
+                    .setOldName(oldName)
+                    .setNewName(newName)
+                    .call();
+            String renamed = 
ref.getName().substring(Constants.R_HEADS.length());
+            ListBranchCommand branchCmd = new ListBranchCommand(repository, 
getClassFactory(), false, new DelegatingGitProgressMonitor(monitor));
+            branchCmd.run();
+            Map<String, GitBranch> branches = branchCmd.getBranches();
+            branch = branches.get(renamed);
+        } catch (JGitInternalException | GitAPIException ex) {
+            throw new GitException(ex);
+        }
+    }
+
+    @Override
+    protected String getCommandDescription() {
+        return "git branch -m " + oldName + " " + newName; //NOI18N
+    }
+
+    public GitBranch getBranch() {
+        return branch;
+    }

Review Comment:
   What is the intention for this? The extraction of the target branch seems 
unnessary.



##########
ide/git/src/org/netbeans/modules/git/ui/branch/RenameBranchAction.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.netbeans.modules.git.ui.branch;
+
+import java.io.File;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.netbeans.libs.git.GitBranch;
+import org.netbeans.libs.git.GitException;
+import org.netbeans.modules.git.Git;
+import org.netbeans.modules.git.client.GitClient;
+import org.netbeans.modules.git.client.GitClientExceptionHandler;
+import org.netbeans.modules.git.client.GitProgressSupport;
+import org.netbeans.modules.git.ui.actions.SingleRepositoryAction;
+import org.netbeans.modules.git.ui.repository.RepositoryInfo;
+import org.netbeans.modules.versioning.spi.VCSContext;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionRegistration;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author christian lenz
+ */
+/**
+ * Action to rename an existing branch.
+ */
+@ActionID(id = "org.netbeans.modules.git.ui.branch.RenameBranchAction", 
category = "Git")
+@ActionRegistration(displayName = "#LBL_RenameBranchAction_Name")
+public class RenameBranchAction extends SingleRepositoryAction {
+
+    private static final Logger LOG = 
Logger.getLogger(RenameBranchAction.class.getName());
+
+    @Override
+    protected void performAction(File repository, File[] roots, VCSContext 
context) {
+        RepositoryInfo info = RepositoryInfo.getInstance(repository);
+        GitBranch activeBranch = info.getActiveBranch();
+        if (activeBranch != null && 
!GitBranch.NO_BRANCH.equals(activeBranch.getName())) {
+            renameBranch(repository, activeBranch.getName());
+        }
+    }
+
+    public void renameBranch(final File repository, final String oldName) {
+        NotifyDescriptor.InputLine nd = new 
NotifyDescriptor.InputLine(NbBundle.getMessage(RenameBranchAction.class, 
"LBL_RenameBranchAction.prompt"), NbBundle.getMessage(RenameBranchAction.class, 
"LBL_RenameBranchAction.title"));

Review Comment:
   The guard from `performAction` needs to here:
   
   <img width="515" height="286" alt="Image" 
src="https://github.com/user-attachments/assets/6e9834cb-3b7c-4f10-8955-cb43525049ea";
 />
   
   ```
   INFO [org.netbeans.modules.git]: 
org.eclipse.jgit.api.errors.RefNotFoundException: Ref (no branch) cannot be 
resolved
   org.eclipse.jgit.api.errors.RefNotFoundException: Ref (no branch) cannot be 
resolved
        at 
org.eclipse.jgit.api.RenameBranchCommand.call(RenameBranchCommand.java:90)
        at 
org.netbeans.libs.git.jgit.commands.RenameBranchCommand.run(RenameBranchCommand.java:59)
   Caused: org.netbeans.libs.git.GitException
        at 
org.netbeans.libs.git.jgit.commands.RenameBranchCommand.run(RenameBranchCommand.java:66)
        at 
org.netbeans.libs.git.jgit.commands.GitCommand$1.run(GitCommand.java:56)
        at 
org.netbeans.libs.git.jgit.commands.GitCommand$1.run(GitCommand.java:53)
        at 
java.base/java.security.AccessController.doPrivileged(AccessController.java:571)
        at 
org.netbeans.libs.git.jgit.commands.GitCommand.execute(GitCommand.java:53)
        at org.netbeans.libs.git.GitClient.renameBranch(GitClient.java:1155)
        at org.netbeans.modules.git.client.GitClient$47.call(GitClient.java:716)
        at org.netbeans.modules.git.client.GitClient$47.call(GitClient.java:712)
        at 
org.netbeans.modules.git.client.GitClient$CommandInvoker$1$1.call(GitClient.java:944)
        at 
org.netbeans.modules.git.client.GitClient$CommandInvoker$1.call(GitClient.java:967)
        at 
org.netbeans.modules.git.FilesystemInterceptor.runWithoutExternalEvents(FilesystemInterceptor.java:477)
        at org.netbeans.modules.git.Git.runWithoutExternalEvents(Git.java:259)
        at 
org.netbeans.modules.git.client.GitClient$CommandInvoker.runMethodIntern(GitClient.java:977)
        at 
org.netbeans.modules.git.client.GitClient$CommandInvoker.runMethod(GitClient.java:904)
        at 
org.netbeans.modules.git.client.GitClient$CommandInvoker.runMethod(GitClient.java:886)
        at 
org.netbeans.modules.git.client.GitClient.renameBranch(GitClient.java:712)
   [catch] at 
org.netbeans.modules.git.ui.branch.RenameBranchAction$1.perform(RenameBranchAction.java:79)
        at 
org.netbeans.modules.git.client.GitProgressSupport.performIntern(GitProgressSupport.java:92)
        at 
org.netbeans.modules.git.client.GitProgressSupport.run(GitProgressSupport.java:85)
        at 
org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1403)
        at 
org.netbeans.modules.openide.util.GlobalLookup.execute(GlobalLookup.java:45)
        at org.openide.util.lookup.Lookups.executeWith(Lookups.java:287)
        at 
org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2018)
   ```



##########
ide/git/src/org/netbeans/modules/git/ui/branch/RenameBranchAction.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.netbeans.modules.git.ui.branch;
+
+import java.io.File;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.netbeans.libs.git.GitBranch;
+import org.netbeans.libs.git.GitException;
+import org.netbeans.modules.git.Git;
+import org.netbeans.modules.git.client.GitClient;
+import org.netbeans.modules.git.client.GitClientExceptionHandler;
+import org.netbeans.modules.git.client.GitProgressSupport;
+import org.netbeans.modules.git.ui.actions.SingleRepositoryAction;
+import org.netbeans.modules.git.ui.repository.RepositoryInfo;
+import org.netbeans.modules.versioning.spi.VCSContext;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionRegistration;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author christian lenz
+ */
+/**
+ * Action to rename an existing branch.
+ */
+@ActionID(id = "org.netbeans.modules.git.ui.branch.RenameBranchAction", 
category = "Git")
+@ActionRegistration(displayName = "#LBL_RenameBranchAction_Name")
+public class RenameBranchAction extends SingleRepositoryAction {
+
+    private static final Logger LOG = 
Logger.getLogger(RenameBranchAction.class.getName());
+
+    @Override
+    protected void performAction(File repository, File[] roots, VCSContext 
context) {
+        RepositoryInfo info = RepositoryInfo.getInstance(repository);
+        GitBranch activeBranch = info.getActiveBranch();
+        if (activeBranch != null && 
!GitBranch.NO_BRANCH.equals(activeBranch.getName())) {

Review Comment:
   Ideally this would be handled in `isEnabled`, but it seems many of the git 
actions just let the user choose the option and then turn into a no-op. Ugly, 
but in line with the existing actions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to