Skip to content

Commit ecf5911

Browse files
authored
feat: Move git clone management to a seperate API (#58)
# What? Migrates the git clone component of the git strategy into a separate API # Why? Experimentation on implementing private repo support for the gomod strategy in #56 revealed we are likely better off separating the git clone behaviour so that it can be shared between multiple strategies.
1 parent f477e8a commit ecf5911

File tree

8 files changed

+823
-517
lines changed

8 files changed

+823
-517
lines changed
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package git
1+
// Package gitclone provides reusable git clone management with lifecycle control,
2+
// concurrency management, and large repository optimizations.
3+
package gitclone
24

35
import (
46
"bufio"
@@ -9,8 +11,6 @@ import (
911
"github.com/alecthomas/errors"
1012
)
1113

12-
// gitCommand creates a git command with insteadOf URL rewriting disabled for the given URL
13-
// to prevent infinite loops where git config rules rewrite URLs to point back through the proxy.
1414
func gitCommand(ctx context.Context, url string, args ...string) (*exec.Cmd, error) {
1515
configArgs, err := getInsteadOfDisableArgsForURL(ctx, url)
1616
if err != nil {
@@ -27,7 +27,6 @@ func gitCommand(ctx context.Context, url string, args ...string) (*exec.Cmd, err
2727
return cmd, nil
2828
}
2929

30-
// getInsteadOfDisableArgsForURL returns arguments to disable insteadOf rules that would affect the given URL.
3130
func getInsteadOfDisableArgsForURL(ctx context.Context, targetURL string) ([]string, error) {
3231
if targetURL == "" {
3332
return nil, nil
@@ -36,7 +35,6 @@ func getInsteadOfDisableArgsForURL(ctx context.Context, targetURL string) ([]str
3635
cmd := exec.CommandContext(ctx, "git", "config", "--get-regexp", "^url\\..*\\.(insteadof|pushinsteadof)$")
3736
output, err := cmd.CombinedOutput()
3837
if err != nil {
39-
// Exit code 1 when no insteadOf rules exist is expected, not an error
4038
return []string{}, nil //nolint:nilerr
4139
}
4240

@@ -60,3 +58,18 @@ func getInsteadOfDisableArgsForURL(ctx context.Context, targetURL string) ([]str
6058

6159
return args, nil
6260
}
61+
62+
func ParseGitRefs(output []byte) map[string]string {
63+
refs := make(map[string]string)
64+
scanner := bufio.NewScanner(strings.NewReader(string(output)))
65+
for scanner.Scan() {
66+
line := scanner.Text()
67+
parts := strings.Fields(line)
68+
if len(parts) >= 2 {
69+
sha := parts[0]
70+
ref := parts[1]
71+
refs[ref] = sha
72+
}
73+
}
74+
return refs
75+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package git //nolint:testpackage // Internal functions need to be tested
1+
package gitclone //nolint:testpackage // Internal functions need to be tested
22

33
import (
44
"context"
@@ -13,9 +13,7 @@ func TestGetInsteadOfDisableArgsForURL(t *testing.T) {
1313
tests := []struct {
1414
name string
1515
targetURL string
16-
// We can't easily test the actual git config reading in a unit test,
17-
// but we can test the logic would work correctly
18-
skipTest bool
16+
skipTest bool
1917
}{
2018
{
2119
name: "EmptyURL",
@@ -47,12 +45,10 @@ func TestGetInsteadOfDisableArgsForURL(t *testing.T) {
4745
func TestGitCommand(t *testing.T) {
4846
ctx := context.Background()
4947

50-
// Test that gitCommand creates a valid command
5148
cmd, err := gitCommand(ctx, "https://github.com/user/repo", "version")
5249
assert.NoError(t, err)
5350

5451
assert.NotZero(t, cmd)
55-
// Should have at least "git" and "version" in args
5652
assert.True(t, len(cmd.Args) >= 2)
5753
// First arg should be git binary path
5854
assert.Equal(t, "git", cmd.Args[0])
@@ -63,7 +59,6 @@ func TestGitCommand(t *testing.T) {
6359
func TestGitCommandWithEmptyURL(t *testing.T) {
6460
ctx := context.Background()
6561

66-
// Test with empty URL (for commands that don't need URL filtering)
6762
cmd, err := gitCommand(ctx, "", "version")
6863
assert.NoError(t, err)
6964

0 commit comments

Comments
 (0)