@@ -38,10 +38,25 @@ type Gitter interface {
38
38
DeleteTag (repo , tag string ) (err error )
39
39
// PushTag pushes the given tag to the origin. Does nothing if tag is empty.
40
40
PushTag (repo , tag string ) (err error )
41
+ // CleanStatus returns true if there are no uncommitted changes in the repo
42
+ CleanStatus (repo string ) bool
41
43
}
42
44
43
45
type DefaultGitter string
44
46
47
+ func (dg DefaultGitter ) execKeepError (args ... string ) (err error ) {
48
+ var b []byte
49
+ b , err = exec .Command (string (dg ), args ... ).CombinedOutput () /* #nosec G204 */
50
+ if err != nil {
51
+ errText := err .Error ()
52
+ if s := strings .TrimSpace (string (b )); s != "" {
53
+ errText = s
54
+ }
55
+ err = fmt .Errorf ("%s %s: %q" , string (dg ), strings .Join (args , " " ), errText )
56
+ }
57
+ return
58
+ }
59
+
45
60
func NewDefaultGitter (gitBin string ) (gitter Gitter , err error ) {
46
61
if gitBin , err = exec .LookPath (gitBin ); err == nil {
47
62
gitter = DefaultGitter (gitBin )
@@ -122,7 +137,7 @@ func (dg DefaultGitter) GetTreeHash(repo, tag string) string {
122
137
123
138
// GetClosestTag returns the closest semver tag for the given commit hash.
124
139
func (dg DefaultGitter ) GetClosestTag (repo , commit string ) (tag string ) {
125
- _ = exec .Command (string (dg ), "-C" , repo , "fetch" , "--unshallow" ).Run () //#nosec G204
140
+ _ = exec .Command (string (dg ), "-C" , repo , "fetch" , "--unshallow" , "--tags" ).Run () //#nosec G204
126
141
if b , _ := exec .Command (string (dg ), "-C" , repo , "describe" , "--tags" , "--match=v[0-9]*" , "--match=[0-9]*" , "--abbrev=0" , commit ).Output (); len (b ) > 0 /* #nosec G204 */ {
127
142
return strings .TrimSpace (string (b ))
128
143
}
@@ -183,25 +198,26 @@ func (dg DefaultGitter) FetchTags(repo string) (err error) {
183
198
184
199
func (dg DefaultGitter ) CreateTag (repo , tag string ) (err error ) {
185
200
if tag != "" {
186
- err = exec . Command ( string ( dg ), "-C" , repo , "tag" , tag ). Run () /* #nosec G204 */
201
+ err = dg . execKeepError ( "-C" , repo , "tag" , tag )
187
202
}
188
203
return
189
204
}
190
205
191
206
func (dg DefaultGitter ) DeleteTag (repo , tag string ) (err error ) {
192
207
if tag != "" {
193
- err = exec . Command ( string ( dg ), "-C" , repo , "tag" , "-d" , tag ). Run () /* #nosec G204 */
208
+ err = dg . execKeepError ( "-C" , repo , "tag" , "-d" , tag )
194
209
}
195
210
return
196
211
}
197
212
198
213
func (dg DefaultGitter ) PushTag (repo , tag string ) (err error ) {
199
214
if tag != "" {
200
- var b []byte
201
- b , err = exec .Command (string (dg ), "-C" , repo , "push" , "origin" , tag ).CombinedOutput () /* #nosec G204 */
202
- if err != nil {
203
- err = fmt .Errorf ("%w: %s" , err , strings .TrimSpace (string (b )))
204
- }
215
+ err = dg .execKeepError ("-C" , repo , "push" , "origin" , tag )
205
216
}
206
217
return
207
218
}
219
+
220
+ func (dg DefaultGitter ) CleanStatus (repo string ) bool {
221
+ b , _ := exec .Command (string (dg ), "-C" , repo , "status" , "--untracked-files=no" , "--porcelain" ).Output () /* #nosec G204 */
222
+ return len (strings .TrimSpace (string (b ))) == 0
223
+ }
0 commit comments