Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 311117

Summary: Egit branch corrupts repository "fatal: HEAD not found below refs/heads!"
Product: [Technology] EGit Reporter: Darren Hart <darren>
Component: CoreAssignee: Mathias Kinzler <mathias.kinzler>
Status: RESOLVED FIXED QA Contact:
Severity: critical    
Priority: P3 CC: caniszczyk, christian.halstrick, darren, eclipse, jens.baumgart, joakim.erdfelt, matthias.sohn, remy.suen, sop
Version: unspecified   
Target Milestone: 0.10.0   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Darren Hart CLA 2010-04-29 17:13:06 EDT
Build Identifier: M20090917-0800

After cloning the linux kernel source, adding a local remote repo and updating that, and then changing to one of those branches via Team->Branch...->Checkout the command line "git branch" command reports the following:

fatal: HEAD not found below refs/heads!

Instead of listing the branch with the one I'm on starred. I haven't yet found a way to restore the repository to a working state.

Reproducible: Always

Steps to Reproduce:
1. Checkout a remote branch
2. Run "git branch" from the console
Comment 1 Chris Aniszczyk CLA 2010-04-29 17:13:51 EDT
Shawn?
Comment 2 Darren Hart CLA 2010-04-29 17:40:47 EDT
Checkout out local master again seems to bring things back to good, then using the command line git or "git gui" tool can be used to successfully change branches.
Comment 3 Chris Aniszczyk CLA 2010-04-29 17:42:15 EDT
I almost recommend using our nightly update site...

http://download.eclipse.org/egit/updates-nightly

0.7.0 is a bit old now and we've made a lot of progress already in the 0.8.0 train
Comment 4 Darren Hart CLA 2010-04-29 18:04:38 EDT
Still occurs with current build from updates-nightly. Note I am doing a checkout of a remote branch, I presume the end result would be the same as:

$ git checkout origin/remote-branch

Which should be:

$ git branch
* (no branch)
  master

Instead, I still get:

$ git branch
fatal: HEAD not found below refs/heads!
Comment 5 Darren Hart CLA 2010-04-29 18:13:10 EDT
If I first create a new local branch from a remote branch (within the eclipse UI even) and then checkout the local branch, it works fine. So this seems to an issue with trying to work with a view of a remote branch and no local branch (if I'm getting the terminology correct). Perhaps Eclipse should just force a local branch be created (I'm not sure) but the current behavior is certainly incorrect.
Comment 6 Matthias Sohn CLA 2010-05-03 17:42:59 EDT
I tried to reproduce on Ubuntu 10.04 using open JDK 6b18-1.8-0ubuntu1
and Helios M6 and latest jgit (d011a377...) and egit (1050aaa6...).

cloned linux-next using native git: 
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git

using EGit (started with max heap size set to -Xmx1024m, 512m seems to be insufficient):
- File > New > Project to create a generic eclipse project in the root of the linux-next tree
- in context menu of project Team > Share Project > Git Repository to connect the egit team provider
- in context menu of project Team > Branch
- select remote branch origin/stable
- click checkout
-> this works for me

- then I switched to origin/history and origin/master in the same way
- checking out local master and again back to origin/history also works
Comment 7 Darren Hart CLA 2010-05-03 17:46:16 EDT
(In reply to comment #6)
> I tried to reproduce on Ubuntu 10.04 using open JDK 6b18-1.8-0ubuntu1
> and Helios M6 and latest jgit (d011a377...) and egit (1050aaa6...).
> 
> cloned linux-next using native git: 
> $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
> 
> using EGit (started with max heap size set to -Xmx1024m, 512m seems to be
> insufficient):
> - File > New > Project to create a generic eclipse project in the root of the
> linux-next tree
> - in context menu of project Team > Share Project > Git Repository to connect
> the egit team provider
> - in context menu of project Team > Branch
> - select remote branch origin/stable
> - click checkout
> -> this works for me

Did you also check if the git command line could display it's branch after you changed it via Egit?

$ git branch
* branch-you-checked-out
master
Comment 8 Jens Baumgart CLA 2010-07-22 11:26:38 EDT
(In reply to comment #7)

Hi Darren,

we still can't reproduce the issue.
Can you describe the exact steps you did to reproduce the error (including repository url etc.)?

Thanks & regards

Jens
Comment 9 Joakim Erdfelt CLA 2010-08-09 01:08:52 EDT
I had this happen to me as well.

Once I started to see this, I exited eclipse and started poking around on the command line, with native git tools.

Some digging showed this ...

[joakim@monolyth phone.git]$ git branch
fatal: HEAD not found below refs/heads!

[joakim@monolyth phone.git]$ git status
# On branch refs/tags/android-sdk-1.6_r1
nothing to commit (working directory clean)

[joakim@monolyth phone.git]$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	logallrefupdates = true
	autocrlf = false
	bare = false
[remote "origin"]
	url = git://android.git.kernel.org/platform/packages/apps/Phone.git
	fetch = +refs/heads/*:refs/remotes/origin/*

[joakim@monolyth phone.git]$ cat .git/HEAD 
ref: refs/tags/android-sdk-1.6_r1

/* NOTE: the .git/HEAD seems to be the problem.
  * Now I reset my repo to master.
 */

[joakim@monolyth phone.git]$ git checkout master
Switched to branch 'master'

[joakim@monolyth phone.git]$ git branch
* master

[joakim@monolyth phone.git]$ cat .git/HEAD 
ref: refs/heads/master

/* Next I did a command line checkout of a tag, and poked around some more */

[joakim@monolyth phone.git]$ git checkout android-sdk-1.6_r1
Note: checking out 'android-sdk-1.6_r1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 6d34074... donut snapshot

[joakim@monolyth phone.git]$ git status
# Not currently on any branch.
nothing to commit (working directory clean)

[joakim@monolyth phone.git]$ git branch
* (no branch)
  master

[joakim@monolyth phone.git]$ cat .git/HEAD 
6d34074d24f430e46d1a66dd1515715515fd0da3

/* Now the .git/HEAD looks like it should */

I suspect there's a updateRef(HEAD).link(refId) when it should be updateRef(HEAD).link(objectId).

I am unable to reproduce this at will, but I have noticed that it seems to occur on projects that use tag references mostly.
Comment 10 Joakim Erdfelt CLA 2010-08-09 01:27:28 EDT
Looking at the jgit code for RefUpdate.link(String target)
It appears that it's not possible to link HEAD to a ObjectId that is of type Commit, only to references to within refs/*

Is this behavioral difference between native git and jgit intentional?
Comment 11 Shawn Pearce CLA 2010-12-03 19:52:41 EST
(In reply to comment #10)
> Looking at the jgit code for RefUpdate.link(String target)
> It appears that it's not possible to link HEAD to a ObjectId that is of type
> Commit, only to references to within refs/*
> 
> Is this behavioral difference between native git and jgit intentional?

This is intentional, and EGit is using JGit incorrectly.

RefUpdate.link() is *only* to create a symbolic reference.  HEAD must only be a symbolic reference to a refs/heads/* name, otherwise command line Git gets very unhappy (as this bug reports).

When EGit checks out a non refs/heads/* name it needs to use a detached checkout for HEAD:

  RefUpdate u = repo.updateRef(Constants.HEAD, true);
  u.setNewObjectId(....);
  u.update();

The final boolean argument of detach = true will cause HEAD to become a detached HEAD, rather than updating the current reference that HEAD points to.
Comment 12 Mathias Kinzler CLA 2011-01-26 08:45:13 EST
I believe the changes proposed in

http://egit.eclipse.org/r/#change,2139

and merged as 

89a4dcf71ff3326aa6313b3a988b7721c3470858

solved this issue.