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.
This commit is contained in:
Kirill Likhodedov
2011-12-07 14:10:16 +03:00
parent c103794e07
commit 9edf2d3c24
2 changed files with 40 additions and 8 deletions
@@ -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<GitRemote> getRemotesWithCommonNames(@NotNull Collection<GitRepository> 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);
@@ -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<String, String> 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<String, String> getRemoteAndTrackedBranchForCurrentBranch() {
if (myGitCommitsToPush != null) {
Collection<GitRepository> 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/")) {