diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java index 4cc356e27df8..6dfa3f23dd36 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java @@ -16,10 +16,8 @@ package git4idea.branch; import com.google.common.base.Function; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; -import com.google.common.collect.Iterables; import com.intellij.dvcs.DvcsUtil; import com.intellij.dvcs.repo.RepositoryUtil; import com.intellij.openapi.diagnostic.Logger; @@ -47,7 +45,10 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; /** * @author Kirill Likhodedov @@ -262,50 +263,6 @@ public class GitBranchUtil { }); } - /** - * @deprecated Don't use names, use {@link GitLocalBranch} objects. - */ - @Deprecated - @Nullable - public static GitLocalBranch findLocalBranchByName(@NotNull GitRepository repository, @NotNull final String branchName) { - Optional optional = Iterables.tryFind(repository.getBranches().getLocalBranches(), new Predicate() { - @Override - public boolean apply(@Nullable GitLocalBranch input) { - assert input != null; - return input.getName().equals(branchName); - } - }); - if (optional.isPresent()) { - return optional.get(); - } - LOG.info(String.format("Couldn't find branch with name %s in %s", branchName, repository)); - return null; - - } - - /** - * Looks through the remote branches in the given repository and tries to find the one from the given remote, - * which the given name. - * @return remote branch or null if such branch couldn't be found. - */ - @Nullable - public static GitRemoteBranch findRemoteBranchByName(@NotNull String remoteBranchName, @NotNull final String remoteName, - @NotNull final Collection remoteBranches) { - final String branchName = stripRefsPrefix(remoteBranchName); - Optional optional = Iterables.tryFind(remoteBranches, new Predicate() { - @Override - public boolean apply(@Nullable GitRemoteBranch input) { - assert input != null; - return input.getNameForRemoteOperations().equals(branchName) && input.getRemote().getName().equals(remoteName); - } - }); - if (optional.isPresent()) { - return optional.get(); - } - LOG.info(String.format("Couldn't find branch with name %s", branchName)); - return null; - } - @NotNull public static String stripRefsPrefix(@NotNull String branchName) { if (branchName.startsWith(GitBranch.REFS_HEADS_PREFIX)) { diff --git a/plugins/git4idea/src/git4idea/repo/GitConfig.java b/plugins/git4idea/src/git4idea/repo/GitConfig.java index 4b0bdeadc8f9..aeb0bf8eb55c 100644 --- a/plugins/git4idea/src/git4idea/repo/GitConfig.java +++ b/plugins/git4idea/src/git4idea/repo/GitConfig.java @@ -158,7 +158,7 @@ public class GitConfig { Pair, Collection> remotesAndUrls = parseRemotes(ini, classLoader); Collection trackedInfos = parseTrackedInfos(ini, classLoader); - + return new GitConfig(remotesAndUrls.getFirst(), remotesAndUrls.getSecond(), trackedInfos); } @@ -201,12 +201,12 @@ public class GitConfig { boolean merge = mergeName != null; final String remoteBranchName = (merge ? mergeName : rebaseName); - assert remoteName != null; - assert remoteBranchName != null; GitLocalBranch localBranch = findLocalBranch(branchName, localBranches); - GitRemoteBranch remoteBranch = GitBranchUtil.findRemoteBranchByName(remoteBranchName, remoteName, remoteBranches); + GitRemoteBranch remoteBranch = findRemoteBranch(remoteBranchName, remoteName, remoteBranches); if (localBranch == null || remoteBranch == null) { + // obsolete record in .git/config: local or remote branch doesn't exist, but the tracking information wasn't removed + LOG.debug("localBranch: " + localBranch + ", remoteBranch: " + remoteBranch); return null; } return new GitBranchTrackInfo(localBranch, remoteBranch, merge); @@ -215,19 +215,25 @@ public class GitConfig { @Nullable private static GitLocalBranch findLocalBranch(@NotNull String branchName, @NotNull Collection localBranches) { final String name = GitBranchUtil.stripRefsPrefix(branchName); - try { - return ContainerUtil.find(localBranches, new Condition() { - @Override - public boolean value(@Nullable GitLocalBranch input) { - assert input != null; - return input.getName().equals(name); - } - }); - } - catch (NoSuchElementException e) { - LOG.info("Couldn't find branch with name " + name); - return null; - } + return ContainerUtil.find(localBranches, new Condition() { + @Override + public boolean value(@Nullable GitLocalBranch input) { + assert input != null; + return input.getName().equals(name); + } + }); + } + + @Nullable + public static GitRemoteBranch findRemoteBranch(@NotNull String remoteBranchName, @NotNull final String remoteName, + @NotNull final Collection remoteBranches) { + final String branchName = GitBranchUtil.stripRefsPrefix(remoteBranchName); + return ContainerUtil.find(remoteBranches, new Condition() { + @Override + public boolean value(GitRemoteBranch branch) { + return branch.getNameForRemoteOperations().equals(branchName) && branch.getRemote().getName().equals(remoteName); + } + }); } @Nullable diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitMultiRootBranchConfig.java b/plugins/git4idea/src/git4idea/ui/branch/GitMultiRootBranchConfig.java index 18076a1b9a61..360cf56fe505 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitMultiRootBranchConfig.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitMultiRootBranchConfig.java @@ -103,7 +103,7 @@ public class GitMultiRootBranchConfig extends DvcsMultiRootBranchConfig