@@ -8,9 +8,11 @@ import (
88)
99
1010type GitSemVer struct {
11- Git Gitter // Git
12- Env Environment // environment
13- DebugOut io.Writer // if nit nil, write debug output here
11+ Git Gitter // Git
12+ Env Environment // environment
13+ DebugOut io.Writer // if nit nil, write debug output here
14+ cleanstatus bool // true if there are no uncommitted changes in current tree
15+ tags []GitTag // tags
1416}
1517
1618// New returns a GitSemVer ready to examine
@@ -72,42 +74,65 @@ func (vs *GitSemVer) Debug(f string, args ...any) {
7274 }
7375}
7476
77+ func (vs * GitSemVer ) getTreeHash (repo , tag string ) (gt GitTag ) {
78+ for i := range vs .tags {
79+ if vs .tags [i ].Tag == tag {
80+ return vs .tags [i ]
81+ }
82+ }
83+ if commit , tree := vs .Git .GetHashes (repo , tag ); commit != "" && tree != "" {
84+ gt .Tag = tag
85+ gt .Commit = commit
86+ gt .Tree = tree
87+ vs .tags = append (vs .tags , gt )
88+ }
89+ return
90+ }
91+
92+ func (vs * GitSemVer ) examineTags (repo string ) {
93+ vs .cleanstatus = vs .Git .CleanStatus (repo )
94+ headHashes := vs .getTreeHash (repo , "HEAD" )
95+ vs .Debug ("treehash %s: HEAD (clean: %v)\n " , headHashes .Tree , vs .cleanstatus )
96+ for _ , testtag := range vs .Git .GetTags (repo ) {
97+ tagtreehashes := vs .getTreeHash (repo , testtag )
98+ if tagtreehashes .Tree != "" {
99+ vs .Debug ("treehash %s: %q\n " , tagtreehashes .Tree , testtag )
100+ if vs .cleanstatus && tagtreehashes .Tree == headHashes .Tree {
101+ return
102+ }
103+ }
104+ }
105+ }
106+
75107// GetTag returns the semver git version tag matching the current tree, or
76108// the closest semver tag if none match exactly. It also returns a bool
77109// that is true if the tree hashes match and there are no uncommitted changes.
78110func (vs * GitSemVer ) GetTag (repo string ) (string , bool ) {
111+ vs .examineTags (repo )
79112 if tag := strings .TrimSpace (vs .Env .Getenv ("CI_COMMIT_TAG" )); tag != "" {
80113 return tag , true
81114 }
82- treehashes := map [string ]string {}
83- cleanstatus := vs .Git .CleanStatus (repo )
84- currtreehash := vs .Git .GetCurrentTreeHash (repo )
85- if currtreehash != "" {
86- vs .Debug ("treehash %s: HEAD (clean: %v)\n " , currtreehash , cleanstatus )
87- for _ , testtag := range vs .Git .GetTags (repo ) {
88- tagtreehash := vs .Git .GetTreeHash (repo , testtag )
89- if _ , ok := treehashes [tagtreehash ]; ! ok {
90- treehashes [tagtreehash ] = testtag
91- }
92- vs .Debug ("treehash %s: %q\n " , tagtreehash , testtag )
93- if tagtreehash == currtreehash {
94- return testtag , cleanstatus
95- }
115+ head := vs .getTreeHash (repo , "HEAD" )
116+ for _ , gt := range vs .tags {
117+ if gt .Tag != "HEAD" && gt .Tree == head .Tree {
118+ return gt .Tag , vs .cleanstatus
96119 }
97120 }
98121 if tag := vs .Git .GetClosestTag (repo , "HEAD" ); tag != "" {
99- tagtreehash := vs .Git .GetTreeHash (repo , tag )
100- if lasttag , ok := treehashes [tagtreehash ]; ok {
101- tag = lasttag
122+ found := vs .getTreeHash (repo , tag )
123+ for _ , gt := range vs .tags {
124+ if gt .Tag != "HEAD" && gt .Tree == found .Tree {
125+ found = gt
126+ break
127+ }
102128 }
103- vs .Debug ("treehash %s: %q is closest to HEAD\n " , tagtreehash , tag )
104- return tag , cleanstatus && (tagtreehash == currtreehash )
129+ vs .Debug ("treehash %s: %q is closest to HEAD\n " , found . Tree , found . Tag )
130+ return found . Tag , vs . cleanstatus && (found . Tree == head . Tree )
105131 }
106132 return "v0.0.0" , false
107133}
108134
109135func (vs * GitSemVer ) getBranchGitHub (repo string ) (branchName string ) {
110- //
111136 if branchName = strings .TrimSpace (vs .Env .Getenv ("GITHUB_REF_NAME" )); branchName != "" {
112137 if strings .TrimSpace (vs .Env .Getenv ("GITHUB_REF_TYPE" )) == "tag" {
113138 for _ , branchName = range vs .Git .GetBranchesFromTag (repo , branchName ) {
@@ -166,6 +191,7 @@ func (vs *GitSemVer) GetVersion(repo string) (vi VersionInfo, err error) {
166191 vi .Build = vs .GetBuild (repo )
167192 vi .Branch = vs .GetBranch (repo )
168193 vi .IsRelease = vs .IsReleaseBranch (vi .Branch )
194+ vi .Tags = vs .tags
169195 }
170196 }
171197 return
0 commit comments