Skip to content

Commit c2e13fb

Browse files
Gustedlunny
Gusted
andauthored
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 <[email protected]>
1 parent 5e5740a commit c2e13fb

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

integrations/git_helper_for_declarative_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
122122
}
123123
}
124124

125+
func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
126+
return func(t *testing.T) {
127+
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{
128+
Filter: "blob:none",
129+
}))
130+
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
131+
assert.NoError(t, err)
132+
assert.True(t, exist)
133+
}
134+
}
135+
125136
func doGitCloneFail(u *url.URL) func(*testing.T) {
126137
return func(t *testing.T) {
127138
tmpDir, err := os.MkdirTemp("", "doGitCloneFail")

integrations/git_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func testGit(t *testing.T, u *url.URL) {
6969

7070
t.Run("Clone", doGitClone(dstPath, u))
7171

72+
dstPath2, err := os.MkdirTemp("", httpContext.Reponame)
73+
assert.NoError(t, err)
74+
defer util.RemoveAll(dstPath2)
75+
76+
t.Run("Partial Clone", doPartialGitClone(dstPath2, u))
77+
7278
little, big := standardCommitAndPushTest(t, dstPath)
7379
littleLFS, bigLFS := lfsCommitAndPushTest(t, dstPath)
7480
rawTest(t, &httpContext, little, big, littleLFS, bigLFS)

modules/git/diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
8181
}
8282

8383
stderr := new(bytes.Buffer)
84-
cmd := NewCommandContextNoGlobals(repo.Ctx, args...)
84+
cmd := NewCommandContext(repo.Ctx, args...)
8585
if err = cmd.RunWithContext(&RunContext{
8686
Timeout: -1,
8787
Dir: repo.Path,

modules/git/repo.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type CloneRepoOptions struct {
101101
Shared bool
102102
NoCheckout bool
103103
Depth int
104+
Filter string
104105
}
105106

106107
// Clone clones original repository to target path.
@@ -136,7 +137,9 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo
136137
if opts.Depth > 0 {
137138
cmd.AddArguments("--depth", strconv.Itoa(opts.Depth))
138139
}
139-
140+
if opts.Filter != "" {
141+
cmd.AddArguments("--filter", opts.Filter)
142+
}
140143
if len(opts.Branch) > 0 {
141144
cmd.AddArguments("-b", opts.Branch)
142145
}

routers/web/repo/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func serviceRPC(ctx gocontext.Context, h serviceHandler, service string) {
485485
}
486486

487487
var stderr bytes.Buffer
488-
cmd := git.NewCommandContextNoGlobals(h.r.Context(), service, "--stateless-rpc", h.dir)
488+
cmd := git.NewCommandContext(h.r.Context(), service, "--stateless-rpc", h.dir)
489489
cmd.SetDescription(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir))
490490
if err := cmd.RunWithContext(&git.RunContext{
491491
Timeout: -1,

services/gitdiff/gitdiff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
13761376
}()
13771377

13781378
go func(ctx context.Context, diffArgs []string, repoPath string, writer *io.PipeWriter) {
1379-
cmd := git.NewCommandContextNoGlobals(ctx, diffArgs...)
1379+
cmd := git.NewCommandContext(ctx, diffArgs...)
13801380
cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
13811381
if err := cmd.RunWithContext(&git.RunContext{
13821382
Timeout: time.Duration(setting.Git.Timeout.Default) * time.Second,

0 commit comments

Comments
 (0)