1
1
package gitsemver
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"io"
6
7
"strconv"
@@ -74,13 +75,14 @@ func (vs *GitSemVer) Debug(f string, args ...any) {
74
75
}
75
76
}
76
77
77
- func (vs * GitSemVer ) getTreeHash (repo , tag string ) (gt GitTag ) {
78
+ func (vs * GitSemVer ) getTreeHash (repo , tag string ) (gt GitTag , err error ) {
78
79
for i := range vs .tags {
79
80
if vs .tags [i ].Tag == tag {
80
- return vs .tags [i ]
81
+ return vs .tags [i ], nil
81
82
}
82
83
}
83
- if commit , tree := vs .Git .GetHashes (repo , tag ); commit != "" && tree != "" {
84
+ var commit , tree string
85
+ if commit , tree , err = vs .Git .GetHashes (repo , tag ); commit != "" && tree != "" && err == nil {
84
86
gt .Tag = tag
85
87
gt .Commit = commit
86
88
gt .Tree = tree
@@ -89,56 +91,75 @@ func (vs *GitSemVer) getTreeHash(repo, tag string) (gt GitTag) {
89
91
return
90
92
}
91
93
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
94
+ func (vs * GitSemVer ) examineTags (repo string ) (err error ) {
95
+ if vs .cleanstatus , err = vs .Git .CleanStatus (repo ); err == nil {
96
+ var headHashes GitTag
97
+ if headHashes , err = vs .getTreeHash (repo , "HEAD" ); err == nil {
98
+ vs .Debug ("treehash %s: HEAD (clean: %v)\n " , headHashes .Tree , vs .cleanstatus )
99
+ var tags []string
100
+ if tags , err = vs .Git .GetTags (repo ); err == nil {
101
+ for _ , testtag := range tags {
102
+ var tagtreehashes GitTag
103
+ if tagtreehashes , err = vs .getTreeHash (repo , testtag ); err == nil {
104
+ if tagtreehashes .Tree != "" {
105
+ vs .Debug ("treehash %s: %q\n " , tagtreehashes .Tree , testtag )
106
+ if vs .cleanstatus && tagtreehashes .Tree == headHashes .Tree {
107
+ return
108
+ }
109
+ }
110
+ }
111
+ }
102
112
}
103
113
}
104
114
}
115
+ return
105
116
}
106
117
107
118
// GetTag returns the semver git version tag matching the current tree, or
108
119
// the closest semver tag if none match exactly. It also returns a bool
109
120
// that is true if the tree hashes match and there are no uncommitted changes.
110
- func (vs * GitSemVer ) GetTag (repo string ) (string , bool ) {
111
- vs .examineTags (repo )
112
- if tag := strings .TrimSpace (vs .Env .Getenv ("CI_COMMIT_TAG" )); tag != "" {
113
- return tag , true
114
- }
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
121
+ func (vs * GitSemVer ) GetTag (repo string ) (tag string , match bool , err error ) {
122
+ if tag = strings .TrimSpace (vs .Env .Getenv ("CI_COMMIT_TAG" )); tag != "" {
123
+ return tag , true , nil
124
+ }
125
+ tag = "v0.0.0"
126
+ if err = vs .examineTags (repo ); err == nil {
127
+ var head GitTag
128
+ if head , err = vs .getTreeHash (repo , "HEAD" ); err == nil {
129
+ for _ , gt := range vs .tags {
130
+ if gt .Tag != "HEAD" && gt .Tree == head .Tree {
131
+ return gt .Tag , vs .cleanstatus , nil
132
+ }
133
+ }
119
134
}
120
- }
121
- if tag := vs .Git .GetClosestTag (repo , "HEAD" ); tag != "" {
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
135
+ var closeToHEAD string
136
+ if closeToHEAD , err = vs .Git .GetClosestTag (repo , "HEAD" ); closeToHEAD != "" {
137
+ var found GitTag
138
+ if found , err = vs .getTreeHash (repo , closeToHEAD ); err == nil {
139
+ for _ , gt := range vs .tags {
140
+ if gt .Tag != "HEAD" && gt .Tree == found .Tree {
141
+ found = gt
142
+ break
143
+ }
144
+ }
145
+ vs .Debug ("treehash %s: %q is closest to HEAD\n " , found .Tree , found .Tag )
146
+ return found .Tag , vs .cleanstatus && (found .Tree == head .Tree ), nil
127
147
}
128
148
}
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 )
131
149
}
132
- return "v0.0.0" , false
150
+ return
133
151
}
134
152
135
- func (vs * GitSemVer ) getBranchGitHub (repo string ) (branchName string ) {
153
+ func (vs * GitSemVer ) getBranchGitHub (repo string ) (branchName string , err error ) {
136
154
if branchName = strings .TrimSpace (vs .Env .Getenv ("GITHUB_BASE_REF" )); branchName == "" {
137
155
if branchName = strings .TrimSpace (vs .Env .Getenv ("GITHUB_REF_NAME" )); branchName != "" {
138
156
if strings .TrimSpace (vs .Env .Getenv ("GITHUB_REF_TYPE" )) == "tag" {
139
- for _ , branchName = range vs .Git .GetBranchesFromTag (repo , branchName ) {
140
- if vs .IsReleaseBranch (branchName ) {
141
- break
157
+ var branches []string
158
+ if branches , err = vs .Git .GetBranchesFromTag (repo , branchName ); err == nil {
159
+ for _ , branchName = range branches {
160
+ if vs .IsReleaseBranch (branchName ) {
161
+ return
162
+ }
142
163
}
143
164
}
144
165
}
@@ -147,12 +168,15 @@ func (vs *GitSemVer) getBranchGitHub(repo string) (branchName string) {
147
168
return
148
169
}
149
170
150
- func (vs * GitSemVer ) getBranchGitLab (repo string ) (branchName string ) {
171
+ func (vs * GitSemVer ) getBranchGitLab (repo string ) (branchName string , err error ) {
151
172
if branchName = strings .TrimSpace (vs .Env .Getenv ("CI_COMMIT_REF_NAME" )); branchName != "" {
152
173
if strings .TrimSpace (vs .Env .Getenv ("CI_COMMIT_TAG" )) == branchName {
153
- for _ , branchName = range vs .Git .GetBranchesFromTag (repo , branchName ) {
154
- if vs .IsReleaseBranch (branchName ) {
155
- break
174
+ var branches []string
175
+ if branches , err = vs .Git .GetBranchesFromTag (repo , branchName ); err == nil {
176
+ for _ , branchName = range branches {
177
+ if vs .IsReleaseBranch (branchName ) {
178
+ return
179
+ }
156
180
}
157
181
}
158
182
}
@@ -165,10 +189,10 @@ func (vs *GitSemVer) getBranchGitLab(repo string) (branchName string) {
165
189
// branch name in the build system or Git. If no branch name
166
190
// can be found (for example, in detached HEAD state),
167
191
// then an empty string is returned.
168
- func (vs * GitSemVer ) GetBranch (repo string ) (branchName string ) {
169
- if branchName = vs .Git .GetBranch (repo ); branchName == "" {
170
- if branchName = vs .getBranchGitHub (repo ); branchName == "" {
171
- branchName = vs .getBranchGitLab (repo )
192
+ func (vs * GitSemVer ) GetBranch (repo string ) (branchName string , err error ) {
193
+ if branchName , err = vs .Git .GetBranch (repo ); branchName == "" {
194
+ if branchName , err = vs .getBranchGitHub (repo ); branchName == "" {
195
+ branchName , err = vs .getBranchGitLab (repo )
172
196
}
173
197
}
174
198
return
@@ -177,10 +201,10 @@ func (vs *GitSemVer) GetBranch(repo string) (branchName string) {
177
201
// GetBuild returns the build counter. This is taken from the CI system if available,
178
202
// otherwise the Git commit count is used. Returns an empty string if no reasonable build
179
203
// counter can be found.
180
- func (vs * GitSemVer ) GetBuild (repo string ) (build string ) {
204
+ func (vs * GitSemVer ) GetBuild (repo string ) (build string , err error ) {
181
205
if build = strings .TrimSpace (vs .Env .Getenv ("CI_PIPELINE_IID" )); build == "" {
182
206
if build = strings .TrimSpace (vs .Env .Getenv ("GITHUB_RUN_NUMBER" )); build == "" {
183
- build = vs .Git .GetBuild (repo )
207
+ build , err = vs .Git .GetBuild (repo )
184
208
}
185
209
}
186
210
return
@@ -189,9 +213,12 @@ func (vs *GitSemVer) GetBuild(repo string) (build string) {
189
213
// GetVersion returns a VersionInfo for the source code in the Git repository.
190
214
func (vs * GitSemVer ) GetVersion (repo string ) (vi VersionInfo , err error ) {
191
215
if repo , err = vs .Git .CheckGitRepo (repo ); err == nil {
192
- if vi .Tag , vi .SameTree = vs .GetTag (repo ); vi .Tag != "" {
193
- vi .Build = vs .GetBuild (repo )
194
- vi .Branch = vs .GetBranch (repo )
216
+ if vi .Tag , vi .SameTree , err = vs .GetTag (repo ); vi .Tag != "" && err == nil {
217
+ var e error
218
+ vi .Build , e = vs .GetBuild (repo )
219
+ err = errors .Join (err , e )
220
+ vi .Branch , e = vs .GetBranch (repo )
221
+ err = errors .Join (err , e )
195
222
vi .IsRelease = vs .IsReleaseBranch (vi .Branch )
196
223
vi .Tags = vs .tags
197
224
}
0 commit comments