src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.9k stars 540 forks source link

repo.Reference(HEAD) return "reference not found" error in brand new repo #1209

Closed easonyq closed 5 years ago

easonyq commented 5 years ago

I've just init a normal repo by PlainInit and then check its current branch. I called:

// same as "git symbolic-ref HEAD"
ref, err := repo.Reference(plumbing.HEAD, true)

and get an error said "reference not found".

But when I use Git CLI tools and "refs/heads/master" is the result.

image

saguywalker commented 5 years ago

I also have the same problem. But it worked when I used: ref, err := repo.Reference(plumbing.HEAD, false)

jfontan commented 5 years ago

The second parameter means that you want a hash reference. In a newly created repo master does not point to any commit because no commits exist. When false it returns a symbolic ref, the same as your git example.