]> source.dussan.org Git - gitea.git/commitdiff
Fix partial cloning a repo (#18373)
authorGusted <williamzijl7@hotmail.com>
Sun, 23 Jan 2022 21:19:32 +0000 (21:19 +0000)
committerGitHub <noreply@github.com>
Sun, 23 Jan 2022 21:19:32 +0000 (22:19 +0100)
- Pass the Global command args into serviceRPC.
- Fixes error with partial cloning.
- Add partial clone test
- Include diff

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
integrations/git_helper_for_declarative_test.go
integrations/git_test.go
modules/git/diff.go
modules/git/repo.go
routers/web/repo/http.go
services/gitdiff/gitdiff.go

index b13c912fd79d949d985eafcd8f3644682379d992..de96f633c0425e07fd52b925d85d9dd9ede09cc6 100644 (file)
@@ -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")
index 243cca2e55bd7d0f95f508b0241f508aa43bf84c..93ff9d25433809c531bd61405ef8dc1962f7fab5 100644 (file)
@@ -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)
index 38aefabf1a32bebfdb1c62c44e41c10a2e88dbd7..4723898e37411500eb49ba66a9c29fc5fd8f9d84 100644 (file)
@@ -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,
index 6368c6459b5055d80667894559e1293c92997d3f..5636405118fdac5182c3f9582dd5ad6131b8dc8d 100644 (file)
@@ -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)
        }
index 1b5004017fa35f81c8fa8fc7c7ca12e611afa05f..32811734d3d42acd729f96b0da47db61e3a2ebd0 100644 (file)
@@ -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,
index 80e706b5ed25df2398985a14d91b5e77b4a3e7ea..ae2800d180421fca39557250a4a96030b42fede2 100644 (file)
@@ -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,