Skip to content

Commit 42991dc

Browse files
Gustedlunny6543
authored
Fix partial cloning a repo (#18373) (#18377)
* Fix partial cloning a repo (#18373) - Backport from: #18373 - Backport isn't 1-1, because the frontport had a refactor in that area, which v1.16 doesn't have. * Include diff & use copy * Add partial clone test * patch * Apply suggestions from code review * globalArgs first * avoid copy but make GlobalCMDArgs append first * please linter Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 160de9f commit 42991dc

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

integrations/git_helper_for_declarative_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
123123
}
124124
}
125125

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

integrations/git_test.go

Lines changed: 6 additions & 0 deletions
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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,28 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
5959
ctx, _, finished := process.GetManager().AddContext(repo.Ctx, fmt.Sprintf("GetRawDiffForFile: [repo_path: %s]", repo.Path))
6060
defer finished()
6161

62-
var cmd *exec.Cmd
62+
cmd := exec.CommandContext(ctx, GitExecutable, GlobalCommandArgs...)
63+
6364
switch diffType {
6465
case RawDiffNormal:
6566
if len(startCommit) != 0 {
66-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"diff", "-M", startCommit, endCommit}, fileArgs...)...)
67+
cmd.Args = append(cmd.Args, append([]string{"diff", "-M", startCommit, endCommit}, fileArgs...)...)
6768
} else if commit.ParentCount() == 0 {
68-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"show", endCommit}, fileArgs...)...)
69+
cmd.Args = append(cmd.Args, append([]string{"show", endCommit}, fileArgs...)...)
6970
} else {
7071
c, _ := commit.Parent(0)
71-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"diff", "-M", c.ID.String(), endCommit}, fileArgs...)...)
72+
cmd.Args = append(cmd.Args, append([]string{"diff", "-M", c.ID.String(), endCommit}, fileArgs...)...)
7273
}
7374
case RawDiffPatch:
7475
if len(startCommit) != 0 {
7576
query := fmt.Sprintf("%s...%s", endCommit, startCommit)
76-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", query}, fileArgs...)...)
77+
cmd.Args = append(cmd.Args, append([]string{"format-patch", "--no-signature", "--stdout", "--root", query}, fileArgs...)...)
7778
} else if commit.ParentCount() == 0 {
78-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", endCommit}, fileArgs...)...)
79+
cmd.Args = append(cmd.Args, append([]string{"format-patch", "--no-signature", "--stdout", "--root", endCommit}, fileArgs...)...)
7980
} else {
8081
c, _ := commit.Parent(0)
8182
query := fmt.Sprintf("%s...%s", endCommit, c.ID.String())
82-
cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", query}, fileArgs...)...)
83+
cmd.Args = append(cmd.Args, append([]string{"format-patch", "--no-signature", "--stdout", query}, fileArgs...)...)
8384
}
8485
default:
8586
return fmt.Errorf("invalid diffType: %s", diffType)

modules/git/repo.go

Lines changed: 4 additions & 1 deletion
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.
@@ -141,7 +142,9 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo
141142
if opts.Depth > 0 {
142143
cmd.AddArguments("--depth", strconv.Itoa(opts.Depth))
143144
}
144-
145+
if opts.Filter != "" {
146+
cmd.AddArguments("--filter", opts.Filter)
147+
}
145148
if len(opts.Branch) > 0 {
146149
cmd.AddArguments("-b", opts.Branch)
147150
}

routers/web/repo/http.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,10 @@ func serviceRPC(h serviceHandler, service string) {
491491
defer finished()
492492

493493
var stderr bytes.Buffer
494-
cmd := exec.CommandContext(ctx, git.GitExecutable, service, "--stateless-rpc", h.dir)
494+
args := make([]string, len(git.GlobalCommandArgs))
495+
copy(args, git.GlobalCommandArgs)
496+
args = append(args, []string{service, "--stateless-rpc", h.dir}...)
497+
cmd := exec.CommandContext(ctx, git.GitExecutable, args...)
495498
cmd.Dir = h.dir
496499
cmd.Env = append(os.Environ(), h.environ...)
497500
cmd.Stdout = h.w

0 commit comments

Comments
 (0)