Prefer go-git to find the reference name (#633)
Walking the directory tree underneath `.git/refs` is not reliable, as it usually does not return tags, especially for freshly cloned repos and/or tags fetched from a remote. The go-git library provides an iterator over all git references. This approach prefers a reference (tag, branch) from go-git, if found. If none is found, it falls back to the previous implementation.
This commit is contained in:
parent
f32babb51d
commit
ea7503bc25
1 changed files with 34 additions and 0 deletions
|
@ -71,6 +71,40 @@ func FindGitRef(file string) (string, error) {
|
||||||
|
|
||||||
log.Debugf("HEAD points to '%s'", ref)
|
log.Debugf("HEAD points to '%s'", ref)
|
||||||
|
|
||||||
|
// Prefer the git library to iterate over the references and find a matching tag or branch.
|
||||||
|
var refTag = ""
|
||||||
|
var refBranch = ""
|
||||||
|
r, err := git.PlainOpen(filepath.Join(gitDir, ".."))
|
||||||
|
if err == nil {
|
||||||
|
iter, err := r.References()
|
||||||
|
if err == nil {
|
||||||
|
for {
|
||||||
|
r, err := iter.Next()
|
||||||
|
if r == nil || err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Debugf("Reference: name=%s sha=%s", r.Name().String(), r.Hash().String())
|
||||||
|
if r.Hash().String() == ref {
|
||||||
|
if r.Name().IsTag() {
|
||||||
|
refTag = r.Name().String()
|
||||||
|
}
|
||||||
|
if r.Name().IsBranch() {
|
||||||
|
refBranch = r.Name().String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iter.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if refTag != "" {
|
||||||
|
return refTag, nil
|
||||||
|
}
|
||||||
|
if refBranch != "" {
|
||||||
|
return refBranch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the above doesn't work, fall back to the old way
|
||||||
|
|
||||||
// try tags first
|
// try tags first
|
||||||
tag, err := findGitPrettyRef(ref, gitDir, "refs/tags")
|
tag, err := findGitPrettyRef(ref, gitDir, "refs/tags")
|
||||||
if err != nil || tag != "" {
|
if err != nil || tag != "" {
|
||||||
|
|
Loading…
Reference in a new issue