[git] IDEA-132259 Allow to checkout remote branch as local if some of repositories are already on this branch

This commit is contained in:
Kirill Likhodedov
2015-05-03 16:44:44 +03:00
parent 8e50f81fe1
commit 5b456c9506
@@ -24,6 +24,8 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ContainerUtil;
import git4idea.GitBranch; import git4idea.GitBranch;
import git4idea.branch.GitBranchUtil; import git4idea.branch.GitBranchUtil;
import git4idea.branch.GitBrancher; import git4idea.branch.GitBrancher;
@@ -282,25 +284,31 @@ class GitBranchPopupActions {
@NotNull String remoteBranchName) { @NotNull String remoteBranchName) {
super("Checkout as new local branch"); super("Checkout as new local branch");
myProject = project; myProject = project;
myRepositories = repositories;
myRemoteBranchName = remoteBranchName; myRemoteBranchName = remoteBranchName;
myRepositories = ContainerUtil.filter(repositories, new Condition<GitRepository>() {
@Override
public boolean value(GitRepository repository) {
return !guessBranchName(myRemoteBranchName).equals(repository.getCurrentBranchName());
}
});
} }
@Override @Override
public void actionPerformed(AnActionEvent e) { public void actionPerformed(AnActionEvent e) {
final String name = Messages.showInputDialog(myProject, "Enter name of new branch", "Checkout Remote Branch", Messages.getQuestionIcon(), final String name = Messages.showInputDialog(myProject, "Enter name of new branch", "Checkout Remote Branch", Messages.getQuestionIcon(),
guessBranchName(), GitNewBranchNameValidator.newInstance(myRepositories)); guessBranchName(myRemoteBranchName),
GitNewBranchNameValidator.newInstance(myRepositories));
if (name != null) { if (name != null) {
GitBrancher brancher = ServiceManager.getService(myProject, GitBrancher.class); GitBrancher brancher = ServiceManager.getService(myProject, GitBrancher.class);
brancher.checkoutNewBranchStartingFrom(name, myRemoteBranchName, myRepositories, null); brancher.checkoutNewBranchStartingFrom(name, myRemoteBranchName, myRepositories, null);
} }
} }
private String guessBranchName() { private static String guessBranchName(@NotNull String remoteBranchName) {
// TODO: check if we already have a branch with that name; check if that branch tracks this remote branch. Show different messages // TODO: check if we already have a branch with that name; check if that branch tracks this remote branch. Show different messages
int slashPosition = myRemoteBranchName.indexOf("/"); int slashPosition = remoteBranchName.indexOf("/");
// if no slash is found (for example, in the case of git-svn remote branches), propose the whole name. // if no slash is found (for example, in the case of git-svn remote branches), propose the whole name.
return myRemoteBranchName.substring(slashPosition+1); return remoteBranchName.substring(slashPosition + 1);
} }
} }