diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java new file mode 100644 index 000000000000..6992ebe851eb --- /dev/null +++ b/plugins/git4idea/src/git4idea/branch/GitBranchesCollection.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package git4idea.branch; + +import git4idea.GitBranch; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.Set; + +/** + *
+ * Storage for local, remote and current branches. + * The reason of creating this special collection is that + * in the terms of performance, they are detected by {@link git4idea.repo.GitRepositoryReader} at once; + * and also usually both sets of branches are needed by components, but are treated differently, + * so it is more convenient to have them separated, but in a single container. + *
+ * + * @author Kirill Likhodedov + */ +public final class GitBranchesCollection { + + public static final GitBranchesCollection EMPTY = new GitBranchesCollection(null, new HashSetnull in the case of detached head,
+ */
+ @Nullable
+ public GitBranch getCurrentBranch() {
+ return myCurrentBranch;
+ }
+}
diff --git a/plugins/git4idea/src/git4idea/repo/GitRepository.java b/plugins/git4idea/src/git4idea/repo/GitRepository.java
index 0e7fcf04e89b..4dbbd98a0c48 100644
--- a/plugins/git4idea/src/git4idea/repo/GitRepository.java
+++ b/plugins/git4idea/src/git4idea/repo/GitRepository.java
@@ -23,6 +23,7 @@ import com.intellij.util.messages.MessageBus;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.messages.Topic;
import git4idea.GitBranch;
+import git4idea.branch.GitBranchesCollection;
import git4idea.status.GitUntrackedFilesHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -64,11 +65,13 @@ public final class GitRepository implements Disposable {
private volatile State myState;
private volatile String myCurrentRevision;
-
private volatile GitBranch myCurrentBranch;
+ private volatile GitBranchesCollection myBranches = GitBranchesCollection.EMPTY;
+
private final ReadWriteLock STATE_LOCK = new ReentrantReadWriteLock();
private final ReadWriteLock CUR_REV_LOCK = new ReentrantReadWriteLock();
private final ReadWriteLock CUR_BRANCH_LOCK = new ReentrantReadWriteLock();
+ private final ReadWriteLock BRANCHES_LOCK = new ReentrantReadWriteLock();
/**
* Current state of the repository.
@@ -112,6 +115,11 @@ public final class GitRepository implements Disposable {
repository.updateCurrentBranch();
}
},
+ BRANCHES {
+ @Override void update(GitRepository repository) {
+ repository.updateBranchList();
+ }
+ },
ALL_CURRENT {
@Override void update(GitRepository repository) {
STATE.update(repository);
@@ -122,6 +130,7 @@ public final class GitRepository implements Disposable {
ALL {
@Override void update(GitRepository repository) {
ALL_CURRENT.update(repository);
+ BRANCHES.update(repository);
}
};
@@ -220,6 +229,17 @@ public final class GitRepository implements Disposable {
public boolean isOnBranch() {
return getState() != State.DETACHED && getState() != State.REBASING;
}
+
+ @NotNull
+ public GitBranchesCollection getBranches() {
+ try {
+ BRANCHES_LOCK.readLock().lock();
+ return myBranches;
+ }
+ finally {
+ BRANCHES_LOCK.readLock().unlock();
+ }
+ }
public void addListener(GitRepositoryChangeListener listener) {
MessageBusConnection connection = myMessageBus.connect();
@@ -280,6 +300,18 @@ public final class GitRepository implements Disposable {
notifyListeners();
}
+
+ private void updateBranchList() {
+ GitBranchesCollection branches = myReader.readBranches();
+ try {
+ BRANCHES_LOCK.writeLock().lock();
+ myBranches = branches;
+ }
+ finally {
+ BRANCHES_LOCK.writeLock().unlock();
+ }
+ notifyListeners();
+ }
private void notifyListeners() {
myMessageBus.syncPublisher(GIT_REPO_CHANGE).repositoryChanged();
diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java
index 927b73443309..6e3f30656766 100644
--- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java
+++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java
@@ -20,6 +20,7 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.util.Processor;
import git4idea.GitBranch;
+import git4idea.branch.GitBranchesCollection;
import git4idea.merge.GitMergeUtil;
import git4idea.rebase.GitRebaseUtils;
import org.jetbrains.annotations.NotNull;
@@ -30,8 +31,11 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -53,12 +57,15 @@ class GitRepositoryReader {
private static Pattern PACKED_REFS_TAGREF_LINE = Pattern.compile("\\^[0-9a-fA-F]+"); // tag reference in .git/packed-refs
private static final String REFS_HEADS_PREFIX = "refs/heads/";
+ private static final String REFS_REMOTES_PREFIX = "refs/remotes/";
private static final int IO_RETRIES = 3; // number of retries before fail if an IOException happens during file read.
private final GitRepository myRepository;
- private final File myGitDir; // .git/
- private final File myHeadFile; // .git/HEAD
- private final File myRefsHeadsDir; // .git/refs/heads/
+ private final File myGitDir; // .git/
+ private final File myHeadFile; // .git/HEAD
+ private final File myRefsHeadsDir; // .git/refs/heads/
+ private final File myRefsRemotesDir; // .git/refs/remotes/
+ private final File myPackedRefsFile; // .git/packed-refs
GitRepositoryReader(@NotNull GitRepository repository) {
myRepository = repository;
@@ -67,6 +74,8 @@ class GitRepositoryReader {
myHeadFile = new File(myGitDir, "HEAD");
assertFileExists(myHeadFile, ".git/HEAD file not found in " + myRepository.getRoot());
myRefsHeadsDir = new File(new File(myGitDir, "refs"), "heads");
+ myRefsRemotesDir = new File(new File(myGitDir, "refs"), "remotes");
+ myPackedRefsFile = new File(myGitDir, "packed-refs");
}
@NotNull
@@ -97,7 +106,7 @@ class GitRepositoryReader {
// look in /refs/heads/