Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

git-describe #736

Open
fenollp opened this issue Jan 29, 2018 · 3 comments
Open

git-describe #736

fenollp opened this issue Jan 29, 2018 · 3 comments

Comments

@fenollp
Copy link

fenollp commented Jan 29, 2018

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.

  1. If it is implemented, could you point me to the right function?
  2. If not, any plans on supporting it?

I am looking for the equivalent of git describe --abbrev --dirty --always --tags.

Maybe related: #599 #139
Keep up the amazing work!

@edupo
Copy link

edupo commented Apr 19, 2018

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...

@edupo
Copy link

edupo commented Apr 19, 2018

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)

@mcuadros
Copy link
Contributor

PRs are more than welcome.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants