Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package git
// Package gitclone provides reusable git clone management with lifecycle control,
// concurrency management, and large repository optimizations.
package gitclone

import (
"bufio"
Expand All @@ -9,8 +11,6 @@ import (
"github.com/alecthomas/errors"
)

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

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

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

return args, nil
}

func ParseGitRefs(output []byte) map[string]string {
refs := make(map[string]string)
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) >= 2 {
sha := parts[0]
ref := parts[1]
refs[ref] = sha
}
}
return refs
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git //nolint:testpackage // Internal functions need to be tested
package gitclone //nolint:testpackage // Internal functions need to be tested

import (
"context"
Expand All @@ -13,9 +13,7 @@ func TestGetInsteadOfDisableArgsForURL(t *testing.T) {
tests := []struct {
name string
targetURL string
// We can't easily test the actual git config reading in a unit test,
// but we can test the logic would work correctly
skipTest bool
skipTest bool
}{
{
name: "EmptyURL",
Expand Down Expand Up @@ -47,12 +45,10 @@ func TestGetInsteadOfDisableArgsForURL(t *testing.T) {
func TestGitCommand(t *testing.T) {
ctx := context.Background()

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

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

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

Expand Down
Loading