From dde1f42a7ab24b2ead2f949b3bea3856ec9ce345 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Fri, 16 Mar 2012 18:02:15 +0400 Subject: [PATCH] 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() --- .../git4idea/branch/GitBranchesCollection.java | 17 +++-------------- .../git4idea/history/browser/CachedRefs.java | 10 +++++++--- .../history/browser/LowLevelAccessImpl.java | 14 ++++++-------- .../src/git4idea/repo/GitRepositoryReader.java | 4 ++-- .../git4idea/repo/GitRepositoryReaderTest.java | 2 +- 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java index d4802f2310d4..de1d8dcaac80 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java @@ -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.emptyList(), Collections.emptyList()); + public static final GitBranchesCollection EMPTY = new GitBranchesCollection(Collections.emptyList(), Collections.emptyList()); private final Collection myLocalBranches; private final Collection myRemoteBranches; - private final GitBranch myCurrentBranch; - public GitBranchesCollection(@Nullable GitBranch currentBranch, @NotNull Collection localBranches, @NotNull Collection remoteBranches) { - myCurrentBranch = currentBranch; + public GitBranchesCollection(@NotNull Collection localBranches, @NotNull Collection 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(myRemoteBranches); } - /** - * @return currently active branch. - * Current branch may be null in the case of detached head, - */ - @Nullable - public GitBranch getCurrentBranch() { - return myCurrentBranch; - } } diff --git a/plugins/git4idea/src/git4idea/history/browser/CachedRefs.java b/plugins/git4idea/src/git4idea/history/browser/CachedRefs.java index 54e1cc1df9bd..8c97455f1ff5 100644 --- a/plugins/git4idea/src/git4idea/history/browser/CachedRefs.java +++ b/plugins/git4idea/src/git4idea/history/browser/CachedRefs.java @@ -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>(); } + 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 diff --git a/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java b/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java index 0863d4d10dfb..64f8707fb354 100644 --- a/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java +++ b/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java @@ -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(); diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java index a043edaf279c..ca92d7cefef3 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java @@ -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); } diff --git a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java index 02df94ede3ad..a0cc84ab6fe2 100644 --- a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java +++ b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java @@ -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 localBranches = branchesCollection.getLocalBranches(); Collection remoteBranches = branchesCollection.getRemoteBranches();