[log] IDEA-120178 Check if branch exists before querying Git or Hg

Otherwise both Git and Hg fail with an error "such branch doesn't exist"
This commit is contained in:
Kirill Likhodedov
2014-02-06 15:01:04 +04:00
parent 691926504d
commit 802a696ad7
3 changed files with 46 additions and 5 deletions
@@ -17,9 +17,11 @@ package git4idea.branch;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ContainerUtil;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
import git4idea.GitRemoteBranch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
@@ -58,12 +60,25 @@ public final class GitBranchesCollection {
return Collections.unmodifiableCollection(myRemoteBranches);
}
public GitLocalBranch findLocalBranch(@NotNull final String name) {
return ContainerUtil.find(myLocalBranches, new Condition<GitLocalBranch>() {
@Nullable
public GitLocalBranch findLocalBranch(@NotNull String name) {
return findByName(myLocalBranches, name);
}
@Nullable
public GitBranch findBranchByName(@NotNull String name) {
GitLocalBranch branch = findByName(myLocalBranches, name);
return branch != null ? branch : findByName(myRemoteBranches, name);
}
@Nullable
private static <T extends GitBranch> T findByName(Collection<T> branches, @NotNull final String name) {
return ContainerUtil.find(branches, new Condition<T>() {
@Override
public boolean value(GitLocalBranch branch) {
public boolean value(T branch) {
return name.equals(branch.getName());
}
});
}
}
@@ -203,7 +203,13 @@ public class GitLogProvider implements VcsLogProvider {
LOG.warn("More than one branch filter was passed. Using only the first one.");
}
VcsLogBranchFilter branchFilter = branchFilters.iterator().next();
filterParameters.add(branchFilter.getBranchName());
String branch = branchFilter.getBranchName();
GitRepository repository = getRepository(root);
assert repository != null : "repository is null for root " + root + " but was previously reported as 'ready'";
if (repository.getBranches().findBranchByName(branch) == null) {
return Collections.emptyList();
}
filterParameters.add(branch);
}
else {
filterParameters.addAll(GitHistoryUtils.LOG_ALL);
@@ -166,8 +166,22 @@ public class HgLogProvider implements VcsLogProvider {
// branch filter and user filter may be used several times without delimiter
if (!branchFilters.isEmpty()) {
HgRepository repository = myRepositoryManager.getRepositoryForRoot(root);
if (repository == null) {
LOG.error("Repository not found for root " + root);
return Collections.emptyList();
}
boolean atLeastOneBranchExists = false;
for (VcsLogBranchFilter branchFilter : branchFilters) {
filterParameters.add(prepareParameter("branch", branchFilter.getBranchName()));
String branchName = branchFilter.getBranchName();
if (branchExists(repository, branchName)) {
filterParameters.add(prepareParameter("branch", branchName));
atLeastOneBranchExists = true;
}
}
if (!atLeastOneBranchExists) { // no such branches => filter matches nothing
return Collections.emptyList();
}
}
@@ -238,4 +252,10 @@ public class HgLogProvider implements VcsLogProvider {
private static String prepareParameter(String paramName, String value) {
return "--" + paramName + "=" + value; // no value escaping needed, because the parameter itself will be quoted by GeneralCommandLine
}
private static boolean branchExists(@NotNull HgRepository repository, @NotNull String branchName) {
return repository.getBranches().keySet().contains(branchName) ||
HgUtil.getNamesWithoutHashes(repository.getBookmarks()).contains(branchName);
}
}