Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to full Git clone if shallow clone is not supported #775

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 19 additions & 3 deletions libs/git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ type cloneOptions struct {
TargetPath string
}

func (opts cloneOptions) args() []string {
args := []string{"clone", opts.RepositoryUrl, opts.TargetPath, "--depth=1", "--no-tags"}
func (opts cloneOptions) args(shallow bool) []string {
args := []string{"clone", opts.RepositoryUrl, opts.TargetPath, "--no-tags"}
if opts.Reference != "" {
args = append(args, "--branch", opts.Reference)
}
if shallow {
args = append(args, "--depth=1")
}
return args
}

Expand All @@ -50,7 +53,7 @@ func Clone(ctx context.Context, url, reference, targetPath string) error {
TargetPath: targetPath,
}

cmd := exec.CommandContext(ctx, "git", opts.args()...)
cmd := exec.CommandContext(ctx, "git", opts.args(true)...)
var cmdErr bytes.Buffer
cmd.Stderr = &cmdErr

Expand All @@ -65,6 +68,19 @@ func Clone(ctx context.Context, url, reference, targetPath string) error {

// wait for git clone to complete
err = cmd.Wait()
// Git repos hosted via HTTP do not support shallow cloning. We try with
// a deep clone this time
if err != nil && strings.Contains(cmdErr.String(), "dumb http transport does not support shallow capabilities") {
retryCmd := exec.CommandContext(ctx, "git", opts.args(false)...)
var retryCmdErr bytes.Buffer
retryCmd.Stderr = &retryCmdErr
retryErr := retryCmd.Run()
if retryErr != nil {
return fmt.Errorf("git clone failed: %w. %s", retryErr, retryCmdErr.String())
} else {
return nil
}
}
if err != nil {
return fmt.Errorf("git clone failed: %w. %s", err, cmdErr.String())
}
Expand Down
15 changes: 11 additions & 4 deletions libs/git/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import (

func TestGitCloneArgs(t *testing.T) {
// case: No branch / tag specified. In this case git clones the default branch
assert.Equal(t, []string{"clone", "abc", "/def", "--depth=1", "--no-tags"}, cloneOptions{
assert.Equal(t, []string{"clone", "abc", "/def", "--no-tags", "--depth=1"}, cloneOptions{
Reference: "",
RepositoryUrl: "abc",
TargetPath: "/def",
}.args())
}.args(true))

// case: A branch is specified.
assert.Equal(t, []string{"clone", "abc", "/def", "--depth=1", "--no-tags", "--branch", "my-branch"}, cloneOptions{
assert.Equal(t, []string{"clone", "abc", "/def", "--no-tags", "--branch", "my-branch", "--depth=1"}, cloneOptions{
Reference: "my-branch",
RepositoryUrl: "abc",
TargetPath: "/def",
}.args())
}.args(true))

// case: deep cloning
assert.Equal(t, []string{"clone", "abc", "/def", "--no-tags"}, cloneOptions{
Reference: "",
RepositoryUrl: "abc",
TargetPath: "/def",
}.args(false))
}

func TestGitCloneWithGitNotFound(t *testing.T) {
Expand Down