Open fenollp opened 6 years ago
This feature will be helpful for us so I started writing something.
My idea was to get the log of the HEAD
and check each commit against the tag pool until a match...
But I see some issue with the tag check:
Initially I went for the repo.TagObject(commit.Hash)
when walking down the log. But TagObject only returns annotated tags and for git describe --tags
that is not a problem.
The approach I take now is to iterate through all tags like:
// Opening the repo
cwd , err := filepath.Abs(".")
PanicIfError(err)
r, err := git.PlainOpen(cwd)
PanicIfError(err)
// Head
head, err := r.Head()
PanicIfError(err)
cIter, err := r.Log(&git.LogOptions{
From: head.Hash(),
Order: git.LogOrderCommitterTime,
})
var tag *plumbing.Reference
err = cIter.ForEach(func(c *object.Commit) error {
// Tags
tags, err := r.Tags()
PanicIfError(err)
err = tags.ForEach(func(t *plumbing.Reference) error {
t_hash := t.Hash()
fmt.Printf("%v - %v\n", c.Hash, t_hash)
if bytes.Equal(c.Hash[:] ,t_hash[:]) {
// Found!
tag = t;
return storer.ErrStop
}
// No luck continue searching.
return nil
})
if tag != nil {
// Found
return storer.ErrStop
}
// Not found!
return nil
})
PanicIfError(err)
But this is no good solution for repositories where thousands of tags exists.
Further development will be to make a map
during the first iteration but for optimizing that I will create a wrapper class around git to hold the map for queries in my app and reuse it as much as possible.
From here is not hard to create a proto describe
using the tag, the number of new commits from the tag and the hash of the head...
Here is my basic implementation of a git describe
with the idea of a map
and a wrapper struct
for go-git.
https://github.com/edupo/semver-cli/blob/master/gitWrapper/git.go
I'm not an expert on the matter but if this solution works for others I may do a PR on the go-git. (If I ever understand the million details required to contribute... xD)
PRs are more than welcome.
I see in COMPATIBILITY.md that
describe
is neither supported nor unsupported (empty box). I also see that there are no implementation containing that name.I am looking for the equivalent of
git describe --abbrev --dirty --always --tags
.Maybe related: https://github.com/src-d/go-git/issues/599 https://github.com/src-d/go-git/pull/139 Keep up the amazing work!