[git] Don't spam "Couldn't find branch with name" & simplify

Although it might indicate an inconsistency in our data structures,
it can also be a valid situation, e.g. when branch was removes,
but the record from .git/config wasn't removed.
This commit is contained in:
Kirill Likhodedov
2014-11-06 18:20:14 +03:00
parent af18624b89
commit 30f65070cb
3 changed files with 28 additions and 65 deletions
@@ -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<GitLocalBranch> optional = Iterables.tryFind(repository.getBranches().getLocalBranches(), new Predicate<GitLocalBranch>() {
@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<GitRemoteBranch> remoteBranches) {
final String branchName = stripRefsPrefix(remoteBranchName);
Optional<GitRemoteBranch> optional = Iterables.tryFind(remoteBranches, new Predicate<GitRemoteBranch>() {
@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)) {
@@ -158,7 +158,7 @@ public class GitConfig {
Pair<Collection<Remote>, Collection<Url>> remotesAndUrls = parseRemotes(ini, classLoader);
Collection<BranchConfig> 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<GitLocalBranch> localBranches) {
final String name = GitBranchUtil.stripRefsPrefix(branchName);
try {
return ContainerUtil.find(localBranches, new Condition<GitLocalBranch>() {
@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<GitLocalBranch>() {
@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<GitRemoteBranch> remoteBranches) {
final String branchName = GitBranchUtil.stripRefsPrefix(remoteBranchName);
return ContainerUtil.find(remoteBranches, new Condition<GitRemoteBranch>() {
@Override
public boolean value(GitRemoteBranch branch) {
return branch.getNameForRemoteOperations().equals(branchName) && branch.getRemote().getName().equals(remoteName);
}
});
}
@Nullable
@@ -103,7 +103,7 @@ public class GitMultiRootBranchConfig extends DvcsMultiRootBranchConfig<GitRepos
@Nullable
private static GitRemoteBranch getTrackedBranch(@NotNull GitRepository repository, @NotNull String branchName) {
GitLocalBranch branch = GitBranchUtil.findLocalBranchByName(repository, branchName);
GitLocalBranch branch = repository.getBranches().findLocalBranch(branchName);
return branch == null ? null : branch.findTrackedBranch(repository);
}