@@ -78,7 +78,7 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
78
78
RemoteName : git .DefaultRemote ,
79
79
ReferenceName : plumbing .NewBranchReferenceName (branch ),
80
80
SingleBranch : g .singleBranch ,
81
- NoCheckout : false ,
81
+ NoCheckout : len ( opts . SparseCheckoutDirectories ) != 0 ,
82
82
Depth : depth ,
83
83
RecurseSubmodules : recurseSubmodules (opts .RecurseSubmodules ),
84
84
Progress : nil ,
@@ -110,6 +110,20 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repos
110
110
}
111
111
}
112
112
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
+
113
127
head , err := repo .Head ()
114
128
if err != nil {
115
129
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.
161
175
RemoteName : git .DefaultRemote ,
162
176
ReferenceName : plumbing .NewTagReferenceName (tag ),
163
177
SingleBranch : g .singleBranch ,
164
- NoCheckout : false ,
178
+ NoCheckout : len ( opts . SparseCheckoutDirectories ) != 0 ,
165
179
Depth : depth ,
166
180
RecurseSubmodules : recurseSubmodules (opts .RecurseSubmodules ),
167
181
Progress : nil ,
@@ -182,6 +196,20 @@ func (g *Client) cloneTag(ctx context.Context, url, tag string, opts repository.
182
196
return nil , fmt .Errorf ("unable to clone '%s': %w" , url , err )
183
197
}
184
198
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
+
185
213
head , err := repo .Head ()
186
214
if err != nil {
187
215
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
222
250
Auth : authMethod ,
223
251
RemoteName : git .DefaultRemote ,
224
252
SingleBranch : false ,
225
- NoCheckout : true ,
253
+ NoCheckout : len ( opts . SparseCheckoutDirectories ) != 0 ,
226
254
RecurseSubmodules : recurseSubmodules (opts .RecurseSubmodules ),
227
255
Progress : nil ,
228
256
Tags : tagStrategy ,
@@ -255,8 +283,9 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repos
255
283
return nil , fmt .Errorf ("unable to resolve commit object for '%s': %w" , commit , err )
256
284
}
257
285
err = w .Checkout (& extgogit.CheckoutOptions {
258
- Hash : cc .Hash ,
259
- Force : true ,
286
+ Hash : cc .Hash ,
287
+ Force : true ,
288
+ SparseCheckoutDirectories : opts .SparseCheckoutDirectories ,
260
289
})
261
290
if err != nil {
262
291
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
288
317
URL : url ,
289
318
Auth : authMethod ,
290
319
RemoteName : git .DefaultRemote ,
291
- NoCheckout : false ,
320
+ NoCheckout : len ( opts . SparseCheckoutDirectories ) != 0 ,
292
321
Depth : depth ,
293
322
RecurseSubmodules : recurseSubmodules (opts .RecurseSubmodules ),
294
323
Progress : nil ,
@@ -376,7 +405,8 @@ func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts re
376
405
return nil , fmt .Errorf ("unable to find reference for tag '%s': %w" , t , err )
377
406
}
378
407
err = w .Checkout (& extgogit.CheckoutOptions {
379
- Branch : tagRef .Name (),
408
+ Branch : tagRef .Name (),
409
+ SparseCheckoutDirectories : opts .SparseCheckoutDirectories ,
380
410
})
381
411
if err != nil {
382
412
return nil , fmt .Errorf ("unable to checkout tag '%s': %w" , t , err )
@@ -458,7 +488,8 @@ func recurseSubmodules(recurse bool) extgogit.SubmoduleRescursivity {
458
488
}
459
489
460
490
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 ) {
462
493
// ref: https://git-scm.com/docs/git-check-ref-format#_description; point no. 6
463
494
if strings .HasPrefix (ref .String (), "/" ) || strings .HasSuffix (ref .String (), "/" ) {
464
495
return "" , fmt .Errorf ("ref %s is invalid; Git refs cannot begin or end with a slash '/'" , ref .String ())
0 commit comments