diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java index ddd741f7d125..57857dff03d8 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java @@ -87,13 +87,21 @@ class GitRepositoryReader { GitLocalBranch currentBranch; String currentRevision; - if (localBranches.isEmpty() && headInfo.content != null) { + if (!headInfo.isBranch) { + currentBranch = null; + currentRevision = headInfo.content; + } + else if (!localBranches.isEmpty()) { + currentBranch = findCurrentBranch(headInfo, state, localBranches); + currentRevision = getCurrentRevision(headInfo, currentBranch); + } + else if (headInfo.content != null) { currentBranch = new GitLocalBranch(headInfo.content, GitBranch.DUMMY_HASH); currentRevision = null; } else { - currentBranch = findCurrentBranch(headInfo, state, localBranches); - currentRevision = getCurrentRevision(headInfo, currentBranch); + currentBranch = null; + currentRevision = null; } return new GitBranchState(currentRevision, currentBranch, state, localBranches, branches.second); } diff --git a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java index 1f8846f06133..6f882e8705df 100644 --- a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java +++ b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderNewTest.java @@ -71,12 +71,28 @@ public class GitRepositoryReaderNewTest extends GitSingleRepoTest { // inspired by IDEA-134286 public void test_detached_HEAD() throws IOException { + String head = getToDetachedHead(); + GitBranchState state = readState(); + assertEquals("Detached HEAD is not detected", GitRepository.State.DETACHED, state.getState()); + assertEquals("Detached HEAD hash is incorrect", head, state.getCurrentRevision()); + } + + // inspired by IDEA-135966 + public void test_no_local_branches() throws IOException { + String head = getToDetachedHead(); + git("branch -D master"); + GitBranchState state = readState(); + assertEquals("Detached HEAD is not detected", GitRepository.State.DETACHED, state.getState()); + assertEquals("Detached HEAD hash is incorrect", head, state.getCurrentRevision()); + assertTrue("There should be no local branches", state.getLocalBranches().isEmpty()); + } + + @NotNull + private static String getToDetachedHead() throws IOException { makeCommit("file.txt"); makeCommit("file.txt"); git("checkout HEAD^"); - GitBranchState state = readState(); - assertEquals("Detached HEAD is not detected", GitRepository.State.DETACHED, state.getState()); - assertEquals("Detached HEAD hash is incorrect", last(), state.getCurrentRevision()); + return last(); } @NotNull