@@ -38,10 +38,25 @@ type Gitter interface {
3838 DeleteTag (repo , tag string ) (err error )
3939 // PushTag pushes the given tag to the origin. Does nothing if tag is empty.
4040 PushTag (repo , tag string ) (err error )
41+ // CleanStatus returns true if there are no uncommitted changes in the repo
42+ CleanStatus (repo string ) bool
4143}
4244
4345type DefaultGitter string
4446
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+
4560func NewDefaultGitter (gitBin string ) (gitter Gitter , err error ) {
4661 if gitBin , err = exec .LookPath (gitBin ); err == nil {
4762 gitter = DefaultGitter (gitBin )
@@ -122,7 +137,7 @@ func (dg DefaultGitter) GetTreeHash(repo, tag string) string {
122137
123138// GetClosestTag returns the closest semver tag for the given commit hash.
124139func (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
126141 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 */ {
127142 return strings .TrimSpace (string (b ))
128143 }
@@ -183,25 +198,26 @@ func (dg DefaultGitter) FetchTags(repo string) (err error) {
183198
184199func (dg DefaultGitter ) CreateTag (repo , tag string ) (err error ) {
185200 if tag != "" {
186- err = exec . Command ( string ( dg ), "-C" , repo , "tag" , tag ). Run () /* #nosec G204 */
201+ err = dg . execKeepError ( "-C" , repo , "tag" , tag )
187202 }
188203 return
189204}
190205
191206func (dg DefaultGitter ) DeleteTag (repo , tag string ) (err error ) {
192207 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 )
194209 }
195210 return
196211}
197212
198213func (dg DefaultGitter ) PushTag (repo , tag string ) (err error ) {
199214 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 )
205216 }
206217 return
207218}
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