From c2e13fb763ec4764f8ebbeb25b190b48ac7b99ad Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 23 Jan 2022 21:19:32 +0000 Subject: [PATCH] Fix partial cloning a repo (#18373) - Pass the Global command args into serviceRPC. - Fixes error with partial cloning. - Add partial clone test - Include diff Co-authored-by: Lunny Xiao --- integrations/git_helper_for_declarative_test.go | 11 +++++++++++ integrations/git_test.go | 6 ++++++ modules/git/diff.go | 2 +- modules/git/repo.go | 5 ++++- routers/web/repo/http.go | 2 +- services/gitdiff/gitdiff.go | 2 +- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/integrations/git_helper_for_declarative_test.go b/integrations/git_helper_for_declarative_test.go index b13c912fd7..de96f633c0 100644 --- a/integrations/git_helper_for_declarative_test.go +++ b/integrations/git_helper_for_declarative_test.go @@ -122,6 +122,17 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { } } +func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { + return func(t *testing.T) { + assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{ + Filter: "blob:none", + })) + exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md")) + assert.NoError(t, err) + assert.True(t, exist) + } +} + func doGitCloneFail(u *url.URL) func(*testing.T) { return func(t *testing.T) { tmpDir, err := os.MkdirTemp("", "doGitCloneFail") diff --git a/integrations/git_test.go b/integrations/git_test.go index 243cca2e55..93ff9d2543 100644 --- a/integrations/git_test.go +++ b/integrations/git_test.go @@ -69,6 +69,12 @@ func testGit(t *testing.T, u *url.URL) { t.Run("Clone", doGitClone(dstPath, u)) + dstPath2, err := os.MkdirTemp("", httpContext.Reponame) + assert.NoError(t, err) + defer util.RemoveAll(dstPath2) + + t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) + little, big := standardCommitAndPushTest(t, dstPath) littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath) rawTest(t, &httpContext, little, big, littleLFS, bigLFS) diff --git a/modules/git/diff.go b/modules/git/diff.go index 38aefabf1a..4723898e37 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -81,7 +81,7 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff } stderr := new(bytes.Buffer) - cmd := NewCommandContextNoGlobals(repo.Ctx, args...) + cmd := NewCommandContext(repo.Ctx, args...) if err = cmd.RunWithContext(&RunContext{ Timeout: -1, Dir: repo.Path, diff --git a/modules/git/repo.go b/modules/git/repo.go index 6368c6459b..5636405118 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -101,6 +101,7 @@ type CloneRepoOptions struct { Shared bool NoCheckout bool Depth int + Filter string } // Clone clones original repository to target path. @@ -136,7 +137,9 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo if opts.Depth > 0 { cmd.AddArguments("--depth", strconv.Itoa(opts.Depth)) } - + if opts.Filter != "" { + cmd.AddArguments("--filter", opts.Filter) + } if len(opts.Branch) > 0 { cmd.AddArguments("-b", opts.Branch) } diff --git a/routers/web/repo/http.go b/routers/web/repo/http.go index 1b5004017f..32811734d3 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -485,7 +485,7 @@ func serviceRPC(ctx gocontext.Context, h serviceHandler, service string) { } var stderr bytes.Buffer - cmd := git.NewCommandContextNoGlobals(h.r.Context(), service, "--stateless-rpc", h.dir) + cmd := git.NewCommandContext(h.r.Context(), service, "--stateless-rpc", h.dir) cmd.SetDescription(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir)) if err := cmd.RunWithContext(&git.RunContext{ Timeout: -1, diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 80e706b5ed..ae2800d180 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1376,7 +1376,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff }() go func(ctx context.Context, diffArgs []string, repoPath string, writer *io.PipeWriter) { - cmd := git.NewCommandContextNoGlobals(ctx, diffArgs...) + cmd := git.NewCommandContext(ctx, diffArgs...) cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath)) if err := cmd.RunWithContext(&git.RunContext{ Timeout: time.Duration(setting.Git.Timeout.Default) * time.Second,