From 9edf2d3c243ead94021fd55551ed71e8192492fc Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Wed, 7 Dec 2011 14:10:16 +0300 Subject: [PATCH] Fix Git push to alternative remote IDEA-78244: GitPushDialog#getTrackedCurrentBranchName -> getRemoteAndTrackedBranchForCurrentBranch When pre-filling values in the "push to manual branch panel", fill not only the tracked branch name, but select the tracked remote as well. In case of error or no tracked branch, use "origin" as the default remote. GitPushDialog.getNameWithoutRemote: use the discovered tracked remote name, not the pre-selected. Don't update the remote and branch fields if checkbox is selected: user is making his choice. IDEA-77985: GitPushDialog#pushSpecsForCurrentOrEnteredBranches : use the selected remote. IDEA-78245: Increase text field size to 20 for that unlikely (after above fixes) case when the full branch name would be written, or when branch name is long. --- .../git4idea/push/GitManualPushToBranch.java | 16 +++++++++- .../src/git4idea/push/GitPushDialog.java | 32 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/plugins/git4idea/src/git4idea/push/GitManualPushToBranch.java b/plugins/git4idea/src/git4idea/push/GitManualPushToBranch.java index 66665daa7174..e2272b1aef74 100644 --- a/plugins/git4idea/src/git4idea/push/GitManualPushToBranch.java +++ b/plugins/git4idea/src/git4idea/push/GitManualPushToBranch.java @@ -57,7 +57,7 @@ class GitManualPushToBranch extends JPanel { myManualPush = new JCheckBox("Push current branch to alternative branch: ", false); myManualPush.setMnemonic('b'); - myDestBranchTextField = new JTextField(15); + myDestBranchTextField = new JTextField(20); myComment = new JBLabel("This will apply to all selected repositories", UIUtil.ComponentStyle.SMALL); @@ -139,6 +139,10 @@ class GitManualPushToBranch extends JPanel { return myRemoteSelector.getSelectedValue(); } + public void selectRemote(String remoteName) { + myRemoteSelector.selectRemote(remoteName); + } + @NotNull public static Collection getRemotesWithCommonNames(@NotNull Collection repositories) { if (repositories.isEmpty()) { @@ -206,6 +210,16 @@ class GitManualPushToBranch extends JPanel { } } + public void selectRemote(@NotNull String remoteName) { + for (GitRemote remote : myRemotes) { + if (remote.getName().equals(remoteName)) { + myRemoteCombobox.setSelectedItem(remote); + return; + } + } + myRemoteCombobox.setSelectedIndex(0); + } + private static class RemoteCellRenderer extends ListCellRendererWrapper { public RemoteCellRenderer(final ListCellRenderer listCellRenderer) { super(listCellRenderer); diff --git a/plugins/git4idea/src/git4idea/push/GitPushDialog.java b/plugins/git4idea/src/git4idea/push/GitPushDialog.java index 03f26a58b440..96159428b801 100644 --- a/plugins/git4idea/src/git4idea/push/GitPushDialog.java +++ b/plugins/git4idea/src/git4idea/push/GitPushDialog.java @@ -48,6 +48,7 @@ import java.util.concurrent.atomic.AtomicReference; public class GitPushDialog extends DialogWrapper { private static final Logger LOG = Logger.getInstance(GitPushDialog.class); + private static final String DEFAULT_REMOTE = "origin"; private JComponent myRootPanel; private Project myProject; @@ -139,6 +140,7 @@ public class GitPushDialog extends DialogWrapper { error.set(collectInfoToPush()); } + final Pair remoteAndBranch = getRemoteAndTrackedBranchForCurrentBranch(); UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { @@ -147,7 +149,10 @@ public class GitPushDialog extends DialogWrapper { } else { myListPanel.setCommits(myGitCommitsToPush); } - myRefspecPanel.setBranchToPushIfNotSet(getTrackedCurrentBranchName()); + if (!myRefspecPanel.turnedOn()) { + myRefspecPanel.selectRemote(remoteAndBranch.getFirst()); + myRefspecPanel.setBranchToPushIfNotSet(remoteAndBranch.getSecond()); + } myLoadingPanel.stopLoading(); } }); @@ -156,7 +161,7 @@ public class GitPushDialog extends DialogWrapper { } @NotNull - private String getTrackedCurrentBranchName() { + private Pair getRemoteAndTrackedBranchForCurrentBranch() { if (myGitCommitsToPush != null) { Collection repositories = myGitCommitsToPush.getRepositories(); if (!repositories.isEmpty()) { @@ -164,17 +169,29 @@ public class GitPushDialog extends DialogWrapper { GitBranch currentBranch = repository.getCurrentBranch(); assert currentBranch != null; if (myGitCommitsToPush.get(repository).get(currentBranch).getDestBranch() == GitPusher.NO_TARGET_BRANCH) { // push to branch with the same name - return currentBranch.getName(); + return Pair.create(DEFAULT_REMOTE, currentBranch.getName()); } - return getNameWithoutRemote(myGitCommitsToPush.get(repository).get(currentBranch).getDestBranch()); + String remoteName; + try { + remoteName = currentBranch.getTrackedRemoteName(myProject, repository.getRoot()); + if (remoteName == null) { + remoteName = DEFAULT_REMOTE; + } + } + catch (VcsException e) { + LOG.info("Couldn't retrieve tracked branch for current branch " + currentBranch, e); + remoteName = DEFAULT_REMOTE; + } + String targetBranch = getNameWithoutRemote(myGitCommitsToPush.get(repository).get(currentBranch).getDestBranch(), remoteName); + return Pair.create(remoteName, targetBranch); } } - return ""; + return Pair.create(DEFAULT_REMOTE, ""); } @NotNull - private String getNameWithoutRemote(@NotNull GitBranch remoteBranch) { - String remoteName = myRefspecPanel.getSelectedRemote().getName() + "/"; + private static String getNameWithoutRemote(@NotNull GitBranch remoteBranch, @NotNull String remoteName) { + remoteName += "/"; String branchName = remoteBranch.getName(); if (branchName.startsWith(remoteName)) { return branchName.substring(remoteName.length()); @@ -228,6 +245,7 @@ public class GitPushDialog extends DialogWrapper { if (myRefspecPanel.turnedOn()) { String manualBranchName = myRefspecPanel.getBranchToPush(); + remote = myRefspecPanel.getSelectedRemote(); GitBranch manualBranch = findRemoteBranchByName(repository, remote, manualBranchName); if (manualBranch == null) { if (!manualBranchName.startsWith("refs/remotes/")) {