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

git describe #816

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

git describe #816

wants to merge 1 commit into from

Conversation

edupo
Copy link

@edupo edupo commented Apr 20, 2018

Signed-off-by: Eduardo Lezcano eduardo.lezcano@be.atlascopco.com

@edupo
Copy link
Author

edupo commented Apr 20, 2018

RFC.

  • Functionality is still being developed.
  • I don't know how many options are worth implementing. (Any suggestion git-describe #736 ?)

I'll squash at the end. Nothing fancy yet xD

@edupo edupo changed the title Describe method initial implementation git describe Apr 20, 2018
@edupo edupo force-pushed the 736-git-describe branch 2 times, most recently from 030324a to 0b1ee9f Compare April 20, 2018 12:47
repository.go Outdated
}

// Tag map for rapid (?) query
tagIterator, err := r.Tags()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be implemented at Repository with a function Tag(referenceName), or a helper function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like TagObject but for lightweight tags will be ideal.
Then we just need to walk down the log checking for annotated tags or lw-tags deppending on the --tag option always preferring annotated ones as described in the git documentation. This search algorithm is nicely described doing a git describe --tags --debug:

$ git describe --tags --debug
searching to describe HEAD
 lightweight        2 v4.3.1
 lightweight       12 v4.3.0
 lightweight       18 v4.2.1
 lightweight       35 v4.2.0
 lightweight       56 v4.1.1
 lightweight       77 v4.1.0
 lightweight       90 v4.0.0
 lightweight      271 v4.0.0-rc15
 lightweight      282 v4.0.0-rc14
 lightweight      330 v4.0.0-rc13
traversed 393 commits
more than 10 tags found; listed 10 most recent
gave up search at d3c7400c39f86a4c59340c7a9cda8497186e00fc
v4.3.1-2-g87cc819

I did not dig into the details of TagObject. I'll check.

Copy link
Contributor

@mcuadros mcuadros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks ok but requires a lot of more work, git describe is a complex command.

Thanks for contributing.

@edupo
Copy link
Author

edupo commented Apr 22, 2018

Indeed! Is more RFC on the feature branch than a production-ready PR. Comments and PRs to the branch are also welcome. :)

@edupo edupo force-pushed the 736-git-describe branch 5 times, most recently from bcbda0d to 6104842 Compare April 27, 2018 12:50
@edupo
Copy link
Author

edupo commented Apr 27, 2018

  • I was able to replicate the regular output of git describe with minor deviations in the distance calculated by go-git and the distance calculated by git describe.
  • The algorithm now tries to closely follow the source of git describe.
  • The performance is an average of 3 times slower than git describe. A good part of the time is expend during the commitIterator, err := r.Log(&LogOptions ... I didn't dig too much on it but apparently r.CommitObject(h) is the slower inside. Of course the majority of the time though goes on the tag map query, but after trying with several different methods (even with go routines) this is, by far, the fastest algorithm.
$ time git describe --tags --debug; time go_git_describe_test
searching to describe HEAD
 lightweight        1 v4.3.1
 lightweight       11 v4.3.0
 lightweight       17 v4.2.1
 lightweight       34 v4.2.0
 lightweight       55 v4.1.1
 lightweight       76 v4.1.0
 lightweight       89 v4.0.0
 lightweight      270 v4.0.0-rc15
 lightweight      281 v4.0.0-rc14
 lightweight      329 v4.0.0-rc13
traversed 392 commits
more than 10 tags found; listed 10 most recent
gave up search at d3c7400c39f86a4c59340c7a9cda8497186e00fc
v4.3.1-1-ge99fdcd

real    0m0.027s
user    0m0.022s
sys     0m0.006s
/home/edupo/go/src/gopkg.in/src-d/go-git.v4
searching to describe refs/heads/736-git-describe
 lightweight        1 v4.3.1
 lightweight       11 v4.3.0
 lightweight       17 v4.2.1
 lightweight       34 v4.2.0
 lightweight       54 v4.1.1
 lightweight       76 v4.1.0
 lightweight       89 v4.0.0
 lightweight      269 v4.0.0-rc15
 lightweight      280 v4.0.0-rc14
 lightweight      329 v4.0.0-rc13
traversed 391 commits
more than 10 tags found; listed 10 most recent
gave up search at d3c7400c39f86a4c59340c7a9cda8497186e00fc
v4.3.1-1-ge99fdcd

real    0m0.062s
user    0m0.061s
sys     0m0.021s

Still some improvements to do and some more options to enable like All, Match and Always.

@edupo edupo force-pushed the 736-git-describe branch 4 times, most recently from 9425c85 to 8e14928 Compare April 27, 2018 13:33
- `Describe` method under repository allows to describe references based on tags.
- Options ported from `git describe` as close as possible.
- Basic test for `Describe`

Signed-off-by: Eduardo Lezcano <eduardo.lezcano@be.atlascopco.com>
@mcuadros
Copy link
Contributor

mcuadros commented May 7, 2018

Very cool @edupo keep the good work.

@Ilyes512
Copy link

I was looking at using go-git to fetch tag information using go generate. Are you guys still working on this or is this feature currently abandoned?

(I will just use bash or ldflags to solve my problem for now).

@Changdrew
Copy link

Have there been any updates for this feature? We'd love to use inbuilt library functions; we were thinking about using this workaround.

arcticicestudio added a commit to arcticicestudio/snowsaw that referenced this pull request Oct 17, 2019
Previously the version of the application was determined by calling
`git` commands in a new shell process. This works in most cases,
but might fail if Git is not installed on the running system.
To prevent further enlargement of the required development environment
setup dependencies by adding more checks for external dependencies,
the `go-git` library v4 [1] (`github.com/src-d/go-git/v4`) has been
added:
"A highly extensible Git implementation in pure Go"

It allows to interact with the project repository and extrac required
information like the latest tag and commit of the current branch to
assemble the application version. To simplify the processing and parsing
of the version, the `semver` library v3 [2]
(`github.com/Masterminds/semver/v3`) has also been added.

The new `getAppVersionFromGit()` function assembles the version of the
application from the metadata of the Git repository.
It searches for the latest "SemVer" [3] compatible version tag in the
current branch and falls back to the default version from the
application configuration if none is found.
If at least one tag is found but it is not the latest commit of the
current branch, the build metadata will be appended, consisting of the
amount of commits ahead and the shortened reference hash (8 digits) of
the latest commit from the current branch.
The function is a early implementation of the Git `describe` command
because support in `go-git` [1] has not been implemented yet. See the
full compatibility comparison documentation with Git [4] as well as the
proposed Git `describe` command implementation [5] for more details.

[1]: https://github.com/src-d/go-git/v4
[2]: https://github.com/Masterminds/semver/v3
[3]: https://semver.org
[4]: https://github.com/src-d/go-git/blob/master/COMPATIBILITY.md
[5]: src-d/go-git#816

Epic GH-33
GH-92
arcticicestudio added a commit to arcticicestudio/snowsaw that referenced this pull request Oct 18, 2019
App version with pure Go Git and SemVer libraries

Previously the version of the application was determined by calling
`git` commands in a new shell process. This works in most cases,
but might fail if Git is not installed on the running system.
To prevent further enlargement of the required development environment
setup dependencies by adding more checks for external dependencies,
the `go-git` library v4 [1] (`github.com/src-d/go-git/v4`) has been
added:
"A highly extensible Git implementation in pure Go"

It allows to interact with the project repository and extrac required
information like the latest tag and commit of the current branch to
assemble the application version. To simplify the processing and parsing
of the version, the `semver` library v3 [2]
(`github.com/Masterminds/semver/v3`) has also been added.

The new `getAppVersionFromGit()` function assembles the version of the
application from the metadata of the Git repository.
It searches for the latest "SemVer" [3] compatible version tag in the
current branch and falls back to the default version from the
application configuration if none is found.
If at least one tag is found but it is not the latest commit of the
current branch, the build metadata will be appended, consisting of the
amount of commits ahead and the shortened reference hash (8 digits) of
the latest commit from the current branch.
The function is a early implementation of the Git `describe` command
because support in `go-git` [1] has not been implemented yet. See the
full compatibility comparison documentation with Git [4] as well as the
proposed Git `describe` command implementation [5] for more details.

The function returned a composed struct to store application version
information and metadata that allows to use the version metadata for
other tasks. It consists of a embedded `semver.Version` struct and
additional fields that stores Git related information:

- `GitCommitHash` (type `plumbing.Hash`) - The latest commit hash
   of the current branch.
- `GitCommitsAhead` (type `int`) - The count of commits ahead to the
  latest Git tag from the current branch.
- `GitLatestVersionTag` (type `plumbing.Reference`) - The latest Git
  version tag in the current branch.

[1]: https://github.com/src-d/go-git/v4
[2]: https://github.com/Masterminds/semver/v3
[3]: https://semver.org
[4]: https://github.com/src-d/go-git/blob/master/COMPATIBILITY.md
[5]: src-d/go-git#816

Epic GH-33
Resolves GH-92
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants