Skip to content

Commit

Permalink
Enable setting the Git command externally
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyTeddy committed Jul 12, 2024
1 parent fc0086b commit 3328c12
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions gitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 51,9 @@ type GitInfo struct {

// Options for the Map function
type Options struct {
Repository string // Path to the repository to map
Revision string // Use blank or HEAD for the currently active revision
Repository string // Path to the repository to map
Revision string // Use blank or HEAD for the currently active revision
GetGitCommandFunc func(args ...string) *exec.Cmd
}

// Map creates a GitRepo with a file map from the given options.
Expand All @@ -65,7 66,7 @@ func Map(opts Options) (*GitRepo, error) {
return nil, err
}

out, err := git("-C", opts.Repository, "rev-parse", "--show-cdup")
out, err := git(opts, "-C", opts.Repository, "rev-parse", "--show-cdup")
if err != nil {
return nil, err
}
Expand All @@ -79,7 80,7 @@ func Map(opts Options) (*GitRepo, error) {
))

gitLogArgs = append([]string{"-c", "diff.renames=0", "-c", "log.showSignature=0", "-C", opts.Repository, "log"}, gitLogArgs...)
out, err = git(gitLogArgs...)
out, err = git(opts, gitLogArgs...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,8 110,14 @@ func Map(opts Options) (*GitRepo, error) {
return &GitRepo{Files: m, TopLevelAbsPath: topLevelPath}, nil
}

func git(args ...string) ([]byte, error) {
out, err := exec.Command(gitExec, args...).CombinedOutput()
func git(opts Options, args ...string) ([]byte, error) {
var cmd *exec.Cmd
if opts.GetGitCommandFunc != nil {
cmd = opts.GetGitCommandFunc(args...)
} else {
cmd = exec.Command(gitExec, args...)
}
out, err := cmd.CombinedOutput()
if err != nil {
if ee, ok := err.(*exec.Error); ok {
if ee.Err == exec.ErrNotFound {
Expand Down

0 comments on commit 3328c12

Please sign in to comment.