Skip to content

Commit a033d2f

Browse files
committed
support sparse checkout
Signed-off-by: kane8n <[email protected]>
1 parent 528bc56 commit a033d2f

File tree

9 files changed

+467
-64
lines changed

9 files changed

+467
-64
lines changed

git/gogit/clone.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
7878
RemoteName: git.DefaultRemote,
7979
ReferenceName: plumbing.NewBranchReferenceName(branch),
8080
SingleBranch: g.singleBranch,
81-
NoCheckout: false,
81+
NoCheckout: len(opts.SparseCheckoutDirectories) != 0,
8282
Depth: depth,
8383
RecurseSubmodules: recurseSubmodules(opts.RecurseSubmodules),
8484
Progress: nil,
@@ -110,6 +110,20 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
110110
}
111111
}
112112

113+
if len(opts.SparseCheckoutDirectories) != 0 {
114+
w, err := repo.Worktree()
115+
if err != nil {
116+
return nil, fmt.Errorf("unable to open repo worktree: %w", err)
117+
}
118+
err = w.Checkout(&extgogit.CheckoutOptions{
119+
Branch: ref,
120+
SparseCheckoutDirectories: opts.SparseCheckoutDirectories,
121+
})
122+
if err != nil {
123+
return nil, fmt.Errorf("unable to sparse checkout branch '%s': %w", branch, err)
124+
}
125+
}
126+
113127
head, err := repo.Head()
114128
if err != nil {
115129
return nil, fmt.Errorf("unable to resolve HEAD of branch '%s': %w", branch, err)
@@ -161,7 +175,7 @@ func (g *Client) cloneTag(ctx context.Context, url, tag string, opts repository.
161175
RemoteName: git.DefaultRemote,
162176
ReferenceName: plumbing.NewTagReferenceName(tag),
163177
SingleBranch: g.singleBranch,
164-
NoCheckout: false,
178+
NoCheckout: len(opts.SparseCheckoutDirectories) != 0,
165179
Depth: depth,
166180
RecurseSubmodules: recurseSubmodules(opts.RecurseSubmodules),
167181
Progress: nil,
@@ -182,6 +196,20 @@ func (g *Client) cloneTag(ctx context.Context, url, tag string, opts repository.
182196
return nil, fmt.Errorf("unable to clone '%s': %w", url, err)
183197
}
184198

199+
if len(opts.SparseCheckoutDirectories) != 0 {
200+
w, err := repo.Worktree()
201+
if err != nil {
202+
return nil, fmt.Errorf("unable to open repo worktree: %w", err)
203+
}
204+
err = w.Checkout(&extgogit.CheckoutOptions{
205+
Branch: ref,
206+
SparseCheckoutDirectories: opts.SparseCheckoutDirectories,
207+
})
208+
if err != nil {
209+
return nil, fmt.Errorf("unable to sparse checkout tag '%s': %w", tag, err)
210+
}
211+
}
212+
185213
head, err := repo.Head()
186214
if err != nil {
187215
return nil, fmt.Errorf("unable to resolve HEAD of tag '%s': %w", tag, err)
@@ -222,7 +250,7 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
222250
Auth: authMethod,
223251
RemoteName: git.DefaultRemote,
224252
SingleBranch: false,
225-
NoCheckout: true,
253+
NoCheckout: len(opts.SparseCheckoutDirectories) != 0,
226254
RecurseSubmodules: recurseSubmodules(opts.RecurseSubmodules),
227255
Progress: nil,
228256
Tags: tagStrategy,
@@ -255,8 +283,9 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
255283
return nil, fmt.Errorf("unable to resolve commit object for '%s': %w", commit, err)
256284
}
257285
err = w.Checkout(&extgogit.CheckoutOptions{
258-
Hash: cc.Hash,
259-
Force: true,
286+
Hash: cc.Hash,
287+
Force: true,
288+
SparseCheckoutDirectories: opts.SparseCheckoutDirectories,
260289
})
261290
if err != nil {
262291
return nil, fmt.Errorf("unable to checkout commit '%s': %w", commit, err)
@@ -288,7 +317,7 @@ func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts re
288317
URL: url,
289318
Auth: authMethod,
290319
RemoteName: git.DefaultRemote,
291-
NoCheckout: false,
320+
NoCheckout: len(opts.SparseCheckoutDirectories) != 0,
292321
Depth: depth,
293322
RecurseSubmodules: recurseSubmodules(opts.RecurseSubmodules),
294323
Progress: nil,
@@ -376,7 +405,8 @@ func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts re
376405
return nil, fmt.Errorf("unable to find reference for tag '%s': %w", t, err)
377406
}
378407
err = w.Checkout(&extgogit.CheckoutOptions{
379-
Branch: tagRef.Name(),
408+
Branch: tagRef.Name(),
409+
SparseCheckoutDirectories: opts.SparseCheckoutDirectories,
380410
})
381411
if err != nil {
382412
return nil, fmt.Errorf("unable to checkout tag '%s': %w", t, err)
@@ -458,7 +488,8 @@ func recurseSubmodules(recurse bool) extgogit.SubmoduleRescursivity {
458488
}
459489

460490
func (g *Client) getRemoteHEAD(ctx context.Context, url string, ref plumbing.ReferenceName,
461-
authMethod transport.AuthMethod) (string, error) {
491+
authMethod transport.AuthMethod,
492+
) (string, error) {
462493
// ref: https://git-scm.com/docs/git-check-ref-format#_description; point no. 6
463494
if strings.HasPrefix(ref.String(), "/") || strings.HasSuffix(ref.String(), "/") {
464495
return "", fmt.Errorf("ref %s is invalid; Git refs cannot begin or end with a slash '/'", ref.String())

0 commit comments

Comments
 (0)