@@ -7,28 +7,10 @@ import (
77 "path/filepath"
88)
99
10- type gitRunner struct {
11- repoPath string
12- gitExecutable string
13- }
14-
15- func (r * gitRunner ) getRefSha (ref string ) (string , error ) {
16- b , err := r .run ("rev-parse" , ref )
17- if err != nil {
18- return "" , err
19- }
20- b = bytes .TrimSpace (b )
21- return string (b ), nil
22- }
23-
24- func (r * gitRunner ) run (args ... string ) ([]byte , error ) {
25- executable := "git"
26- if r .gitExecutable != "" {
27- executable = r .gitExecutable
28- }
29- cmd := exec .Command (executable , args ... ) //nolint:gosec // this is fine
10+ func runGitCmd (gitCmd , repoPath string , args ... string ) ([]byte , error ) {
11+ cmd := exec .Command (gitCmd , args ... ) //nolint:gosec // this is fine
3012 var err error
31- cmd .Dir , err = filepath .Abs (r . repoPath )
13+ cmd .Dir , err = filepath .Abs (repoPath )
3214 if err != nil {
3315 return nil , err
3416 }
@@ -37,43 +19,39 @@ func (r *gitRunner) run(args ...string) ([]byte, error) {
3719 if exitErr , ok := err .(* exec.ExitError ); ok {
3820 err = fmt .Errorf ("error running git command: %s" , string (exitErr .Stderr ))
3921 }
22+ b = bytes .TrimSpace (b )
4023 return b , err
4124}
4225
43- type refRunner struct {
44- gitRunner gitRunner
45- ref string
46- }
47-
48- func (r * refRunner ) stashAndReset () (revert func () error , err error ) {
26+ func stashAndReset (gitCmd , repoPath string ) (revert func () error , err error ) {
4927 revert = func () error {
5028 return nil
5129 }
52- stash , err := r . gitRunner . run ( "stash" , "create" , "--quiet" )
30+ stash , err := runGitCmd ( gitCmd , repoPath , "stash" , "create" , "--quiet" )
5331 if err != nil {
5432 return nil , err
5533 }
5634 stash = bytes .TrimSpace (stash )
5735 if len (stash ) > 0 {
5836 revert = func () error {
59- _ , revertErr := r . gitRunner . run ( "stash" , "apply" , "--quiet" , string (stash ))
37+ _ , revertErr := runGitCmd ( gitCmd , repoPath , "stash" , "apply" , "--quiet" , string (stash ))
6038 return revertErr
6139 }
6240 }
63- _ , err = r . gitRunner . run ( "reset" , "--hard" , "--quiet" )
41+ _ , err = runGitCmd ( gitCmd , repoPath , "reset" , "--hard" , "--quiet" )
6442 if err != nil {
6543 return nil , err
6644 }
6745 return revert , nil
6846}
6947
70- func ( r * refRunner ) run ( fn func ()) error {
71- origRef , err := r . gitRunner . run ( "rev-parse" , "--abbrev-ref" , "HEAD" )
48+ func runAtGitRef ( gitCmd , repoPath , ref string , fn func ()) error {
49+ origRef , err := runGitCmd ( gitCmd , repoPath , "rev-parse" , "--abbrev-ref" , "HEAD" )
7250 if err != nil {
7351 return err
7452 }
7553 origRef = bytes .TrimSpace (origRef )
76- unstash , err := r . stashAndReset ()
54+ unstash , err := stashAndReset (gitCmd , repoPath )
7755 if err != nil {
7856 return err
7957 }
@@ -83,12 +61,12 @@ func (r *refRunner) run(fn func()) error {
8361 panic (unstashErr )
8462 }
8563 }()
86- _ , err = r . gitRunner . run ( "checkout" , "--quiet" , r . ref )
64+ _ , err = runGitCmd ( gitCmd , repoPath , "checkout" , "--quiet" , ref )
8765 if err != nil {
8866 return err
8967 }
9068 defer func () {
91- _ , cerr := r . gitRunner . run ( "checkout" , "--quiet" , string (origRef ))
69+ _ , cerr := runGitCmd ( gitCmd , repoPath , "checkout" , "--quiet" , string (origRef ))
9270 if cerr != nil {
9371 if exitErr , ok := cerr .(* exec.ExitError ); ok {
9472 fmt .Println (string (exitErr .Stderr ))
0 commit comments