Skip to content

Commit 7259440

Browse files
authored
Add Options struct that allows setting the Git command
1 parent 024a706 commit 7259440

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

‎gitmap.go‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,30 @@ type GitInfo struct {
4949
Body string `json:"body"` // The commit message body
5050
}
5151

52-
// Map creates a GitRepo with a file map from the given repository path and revision.
53-
// Use blank or HEAD as revision for the currently active revision.
54-
func Map(repository, revision string) (*GitRepo, error) {
52+
// Options for the Map function
53+
type Options struct {
54+
Repository string // Path to the repository to map
55+
Revision string // Use blank or HEAD for the currently active revision
56+
GetGitCommandFunc func(args ...string) *exec.Cmd
57+
}
58+
59+
// Map creates a GitRepo with a file map from the given options.
60+
func Map(opts Options) (*GitRepo, error) {
61+
if opts.GetGitCommandFunc == nil {
62+
opts.GetGitCommandFunc = func(args ...string) *exec.Cmd {
63+
return exec.Command(gitExec, args...)
64+
}
65+
}
66+
5567
m := make(GitMap)
5668

5769
// First get the top level repo path
58-
absRepoPath, err := filepath.Abs(repository)
70+
absRepoPath, err := filepath.Abs(opts.Repository)
5971
if err != nil {
6072
return nil, err
6173
}
6274

63-
out, err := git("-C", repository, "rev-parse", "--show-cdup")
75+
out, err := git(opts, "-C", opts.Repository, "rev-parse", "--show-cdup")
6476
if err != nil {
6577
return nil, err
6678
}
@@ -70,11 +82,11 @@ func Map(repository, revision string) (*GitRepo, error) {
7082

7183
gitLogArgs := strings.Fields(fmt.Sprintf(
7284
`--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci%%x1f%%b%%x1d %s`,
73-
revision,
85+
opts.Revision,
7486
))
7587

76-
gitLogArgs = append([]string{"-c", "diff.renames=0", "-c", "log.showSignature=0", "-C", repository, "log"}, gitLogArgs...)
77-
out, err = git(gitLogArgs...)
88+
gitLogArgs = append([]string{"-c", "diff.renames=0", "-c", "log.showSignature=0", "-C", opts.Repository, "log"}, gitLogArgs...)
89+
out, err = git(opts, gitLogArgs...)
7890
if err != nil {
7991
return nil, err
8092
}
@@ -104,8 +116,8 @@ func Map(repository, revision string) (*GitRepo, error) {
104116
return &GitRepo{Files: m, TopLevelAbsPath: topLevelPath}, nil
105117
}
106118

107-
func git(args ...string) ([]byte, error) {
108-
out, err := exec.Command(gitExec, args...).CombinedOutput()
119+
func git(opts Options, args ...string) ([]byte, error) {
120+
out, err := opts.GetGitCommandFunc(args...).CombinedOutput()
109121
if err != nil {
110122
if ee, ok := err.(*exec.Error); ok {
111123
if ee.Err == exec.ErrNotFound {

‎gitmap_test.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestMap(t *testing.T) {
3131
err error
3232
)
3333

34-
if gr, err = Map(repository, revision); err != nil {
34+
if gr, err = Map(Options{Repository: repository, Revision: revision}); err != nil {
3535
t.Fatal(err)
3636
}
3737

@@ -120,7 +120,7 @@ func TestCommitMessage(t *testing.T) {
120120
err error
121121
)
122122

123-
if gr, err = Map(repository, "HEAD"); err != nil {
123+
if gr, err = Map(Options{Repository: repository, Revision: "HEAD"}); err != nil {
124124
t.Fatal(err)
125125
}
126126

@@ -182,7 +182,7 @@ func TestActiveRevision(t *testing.T) {
182182
err error
183183
)
184184

185-
if gr, err = Map(repository, "HEAD"); err != nil {
185+
if gr, err = Map(Options{Repository: repository, Revision: "HEAD"}); err != nil {
186186
t.Fatal(err)
187187
}
188188

@@ -200,7 +200,7 @@ func TestActiveRevision(t *testing.T) {
200200
func TestGitExecutableNotFound(t *testing.T) {
201201
defer initDefaults()
202202
gitExec = "thisShouldHopefullyNotExistOnPath"
203-
gi, err := Map(repository, revision)
203+
gi, err := Map(Options{Repository: repository, Revision: revision})
204204

205205
if err != ErrGitNotFound || gi != nil {
206206
t.Fatal("Invalid error handling")
@@ -217,7 +217,7 @@ func TestEncodeJSON(t *testing.T) {
217217
filename = "README.md"
218218
)
219219

220-
if gr, err = Map(repository, revision); err != nil {
220+
if gr, err = Map(Options{Repository: repository, Revision: revision}); err != nil {
221221
t.Fatal(err)
222222
}
223223

@@ -240,7 +240,7 @@ func TestEncodeJSON(t *testing.T) {
240240
}
241241

242242
func TestGitRevisionNotFound(t *testing.T) {
243-
gi, err := Map(repository, "adfasdfasdf")
243+
gi, err := Map(Options{Repository: repository, Revision: "adfasdfasdf"})
244244

245245
// TODO(bep) improve error handling.
246246
if err == nil || gi != nil {
@@ -249,7 +249,7 @@ func TestGitRevisionNotFound(t *testing.T) {
249249
}
250250

251251
func TestGitRepoNotFound(t *testing.T) {
252-
gi, err := Map("adfasdfasdf", revision)
252+
gi, err := Map(Options{Repository: "adfasdfasdf", Revision: revision})
253253

254254
// TODO(bep) improve error handling.
255255
if err == nil || gi != nil {
@@ -263,7 +263,7 @@ func TestTopLevelAbsPath(t *testing.T) {
263263
err error
264264
)
265265

266-
if gr, err = Map(repository, revision); err != nil {
266+
if gr, err = Map(Options{Repository: repository, Revision: revision}); err != nil {
267267
t.Fatal(err)
268268
}
269269

@@ -276,7 +276,7 @@ func TestTopLevelAbsPath(t *testing.T) {
276276

277277
func BenchmarkMap(b *testing.B) {
278278
for i := 0; i < b.N; i++ {
279-
_, err := Map(repository, revision)
279+
_, err := Map(Options{Repository: repository, Revision: revision})
280280
if err != nil {
281281
b.Fatalf("Got error: %s", err)
282282
}

0 commit comments

Comments
 (0)