From 802a696ad7ecb185a2e7fb35491f08481ab6b506 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Thu, 6 Feb 2014 13:57:48 +0400 Subject: [PATCH] [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" --- .../branch/GitBranchesCollection.java | 21 +++++++++++++++--- .../src/git4idea/log/GitLogProvider.java | 8 ++++++- .../org/zmlx/hg4idea/log/HgLogProvider.java | 22 ++++++++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java index e3081f99b088..4f40fc68446d 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java @@ -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() { + @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 findByName(Collection branches, @NotNull final String name) { + return ContainerUtil.find(branches, new Condition() { @Override - public boolean value(GitLocalBranch branch) { + public boolean value(T branch) { return name.equals(branch.getName()); } }); } + } diff --git a/plugins/git4idea/src/git4idea/log/GitLogProvider.java b/plugins/git4idea/src/git4idea/log/GitLogProvider.java index ed33df0718b0..32d96fc5f73f 100644 --- a/plugins/git4idea/src/git4idea/log/GitLogProvider.java +++ b/plugins/git4idea/src/git4idea/log/GitLogProvider.java @@ -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); diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java index d10f8c8c2742..7dd9cebd3634 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java @@ -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); + } + }