From f0ea5015b4a05880a1463066c4860c69062da4aa Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Wed, 14 Jan 2015 17:23:35 +0300 Subject: [PATCH] [git] IDEA-134412 fresh repository should be on branch --- .../git4idea/repo/GitRepositoryReader.java | 17 ++++++++++++++--- .../repo/GitRepositoryReaderNewTest.java | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java index 892791fcff5c..ddd741f7d125 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java @@ -80,11 +80,22 @@ class GitRepositoryReader { @NotNull GitBranchState readState(@NotNull Collection remotes) { Pair, Set> branches = readBranches(remotes); + Set localBranches = branches.first; + HeadInfo headInfo = readHead(); Repository.State state = readRepositoryState(headInfo); - GitLocalBranch currentBranch = findCurrentBranch(headInfo, state, branches.first); - String currentRevision = getCurrentRevision(headInfo, currentBranch); - return new GitBranchState(currentRevision, currentBranch, state, branches.first, branches.second); + + GitLocalBranch currentBranch; + String currentRevision; + if (localBranches.isEmpty() && headInfo.content != null) { + currentBranch = new GitLocalBranch(headInfo.content, GitBranch.DUMMY_HASH); + currentRevision = null; + } + else { + currentBranch = findCurrentBranch(headInfo, state, localBranches); + currentRevision = getCurrentRevision(headInfo, currentBranch); + } + return new GitBranchState(currentRevision, currentBranch, state, localBranches, branches.second); } @Nullable diff --git a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java index 0ae3fff2356f..1f8846f06133 100644 --- a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java +++ b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java @@ -18,6 +18,7 @@ package git4idea.repo; import com.intellij.dvcs.repo.Repository; import com.intellij.openapi.util.Condition; import com.intellij.util.containers.ContainerUtil; +import git4idea.GitLocalBranch; import git4idea.GitRemoteBranch; import git4idea.test.GitSingleRepoTest; import org.jetbrains.annotations.NotNull; @@ -39,7 +40,8 @@ import static git4idea.test.GitTestUtil.makeCommit; public class GitRepositoryReaderNewTest extends GitSingleRepoTest { // inspired by IDEA-93806 - public void test_rebase_with_conflicts_while_being_on_detached_HEAD() { + public void test_rebase_with_conflicts_while_being_on_detached_HEAD() throws IOException { + makeCommit("file.txt"); conflict(myRepo, "feature"); commit(myRepo); commit(myRepo); @@ -52,7 +54,8 @@ public class GitRepositoryReaderNewTest extends GitSingleRepoTest { } // inspired by IDEA-124052 - public void test_remote_reference_without_remote() { + public void test_remote_reference_without_remote() throws IOException { + makeCommit("file.txt"); final String INVALID_REMOTE = "invalid-remote"; final String INVALID_REMOTE_BRANCH = "master"; git("update-ref refs/remotes/" + INVALID_REMOTE + "/" + INVALID_REMOTE_BRANCH + " HEAD"); @@ -68,6 +71,7 @@ public class GitRepositoryReaderNewTest extends GitSingleRepoTest { // inspired by IDEA-134286 public void test_detached_HEAD() throws IOException { + makeCommit("file.txt"); makeCommit("file.txt"); git("checkout HEAD^"); GitBranchState state = readState(); @@ -84,4 +88,15 @@ public class GitRepositoryReaderNewTest extends GitSingleRepoTest { return reader.readState(remotes); } + // inspired by IDEA-134412 + public void test_fresh_repository_is_on_branch() { + GitLocalBranch currentBranch = readState().getCurrentBranch(); + assertNotNull("Current branch shouldn't be null in a fresh repository", currentBranch); + assertEquals("Fresh repository should be on master", "master", currentBranch.getName()); + } + + @Override + protected boolean makeInitialCommit() { + return false; + } }