Don't store current branch in GitBranchesCollection

Reason: GitBranchesCollection is updated more less frequently that the current branch.
Current branch is accessible from GitRepository#getCurrentBranch()
This commit is contained in:
Kirill Likhodedov
2012-03-16 20:15:03 +04:00
parent 4dbd554038
commit dde1f42a7a
5 changed files with 19 additions and 28 deletions
@@ -17,7 +17,6 @@ package git4idea.branch;
import git4idea.GitBranch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
@@ -36,14 +35,12 @@ import java.util.HashSet;
*/
public final class GitBranchesCollection {
public static final GitBranchesCollection EMPTY = new GitBranchesCollection(null, Collections.<GitBranch>emptyList(), Collections.<GitBranch>emptyList());
public static final GitBranchesCollection EMPTY = new GitBranchesCollection(Collections.<GitBranch>emptyList(), Collections.<GitBranch>emptyList());
private final Collection<GitBranch> myLocalBranches;
private final Collection<GitBranch> myRemoteBranches;
private final GitBranch myCurrentBranch;
public GitBranchesCollection(@Nullable GitBranch currentBranch, @NotNull Collection<GitBranch> localBranches, @NotNull Collection<GitBranch> remoteBranches) {
myCurrentBranch = currentBranch;
public GitBranchesCollection(@NotNull Collection<GitBranch> localBranches, @NotNull Collection<GitBranch> remoteBranches) {
myRemoteBranches = remoteBranches;
myLocalBranches = localBranches;
}
@@ -52,7 +49,7 @@ public final class GitBranchesCollection {
* Copy constructor. Sets inside are also copied.
*/
public GitBranchesCollection(@NotNull GitBranchesCollection branches) {
this(branches.getCurrentBranch(), branches.getLocalBranches(), branches.getRemoteBranches());
this(branches.getLocalBranches(), branches.getRemoteBranches());
}
/**
@@ -71,12 +68,4 @@ public final class GitBranchesCollection {
return new HashSet<GitBranch>(myRemoteBranches);
}
/**
* @return currently active branch.
* Current branch may be <code>null</code> in the case of detached head,
*/
@Nullable
public GitBranch getCurrentBranch() {
return myCurrentBranch;
}
}
@@ -31,6 +31,7 @@ import java.util.*;
*/
public class CachedRefs implements SymbolicRefsI {
private GitBranchesCollection myCollection;
private GitBranch myCurrentBranch;
private String myTrackedRemoteName;
private String myUsername;
private AbstractHash myHeadHash;
@@ -44,6 +45,10 @@ public class CachedRefs implements SymbolicRefsI {
myRefsMap = new MultiMap<String, Pair<SymbolicRefs.Kind, String>>();
}
public void setCurrentBranch(GitBranch currentBranch) {
myCurrentBranch = currentBranch;
}
public void setCollection(GitBranchesCollection collection) {
myCollection = collection;
/*GitBranch branch = myCollection.getCurrentBranch();
@@ -109,13 +114,12 @@ public class CachedRefs implements SymbolicRefsI {
@Override
public String getCurrentName() {
GitBranch branch = myCollection.getCurrentBranch();
return branch == null ? null : branch.getName();
return myCurrentBranch == null ? null : myCurrentBranch.getName();
}
@Override
public GitBranch getCurrent() {
return myCollection.getCurrentBranch();
return myCurrentBranch;
}
@Override
@@ -115,21 +115,19 @@ public class LowLevelAccessImpl implements LowLevelAccess {
// uses cached version
public CachedRefs getRefs() throws VcsException {
final CachedRefs refs = new CachedRefs();
final GitRepository repositoryForRoot = GitUtil.getRepositoryManager(myProject).getRepositoryForRoot(myRoot);
final GitBranchesCollection branches;
if (repositoryForRoot != null) {
branches = repositoryForRoot.getBranches();
} else {
GitRepository repository = GitUtil.getRepositoryManager(myProject).getRepositoryForRoot(myRoot);
if (repository == null) {
final File child = new File(myRoot.getPath(), ".git");
if (! child.exists()) {
throw new VcsException("No git repository in " + myRoot.getPath());
}
GitRepository repository = GitRepository.getLightInstance(myRoot, myProject, myProject);
repository = GitRepository.getLightInstance(myRoot, myProject, myProject);
repository.getBranches();
branches = repository.getBranches();
}
GitBranchesCollection branches = repository.getBranches();
refs.setCollection(branches);
final GitBranch current = branches.getCurrentBranch();
final GitBranch current = repository.getCurrentBranch();
refs.setCurrentBranch(current);
if (current != null) {
GitBranch tracked = current.tracked(myProject, myRoot);
String fullName = tracked == null ? null : tracked.getFullName();
@@ -260,7 +260,7 @@ class GitRepositoryReader {
GitBranch currentBranch = readCurrentBranch();
markActiveBranch(localBranches, currentBranch);
return new GitBranchesCollection(currentBranch, localBranches, remoteBranches);
return new GitBranchesCollection(localBranches, remoteBranches);
}
/**
@@ -356,7 +356,7 @@ class GitRepositoryReader {
}
});
}
return new GitBranchesCollection(null, localBranches, remoteBranches);
return new GitBranchesCollection(localBranches, remoteBranches);
}
@@ -72,7 +72,7 @@ public class GitRepositoryReaderTest extends LightIdeaTestCase {
public void testBranches(){
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches();
GitBranch currentBranch = branchesCollection.getCurrentBranch();
GitBranch currentBranch = myRepositoryReader.readCurrentBranch();
Collection<GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<GitBranch> remoteBranches = branchesCollection.getRemoteBranches();