@@ -331,17 +331,9 @@ func NthTag(g Interface, dir string, n int) (string, string, error) {
331
331
332
332
// CloneToDir clones the git repository to either the given directory or create a temporary
333
333
func CloneToDir (g Interface , gitURL , dir string ) (string , error ) {
334
- var err error
335
- if dir != "" {
336
- err = os .MkdirAll (dir , util .DefaultWritePermissions )
337
- if err != nil {
338
- return "" , fmt .Errorf ("failed to create directory %s: %w" , dir , err )
339
- }
340
- } else {
341
- dir , err = os .MkdirTemp ("" , "jx-git-" )
342
- if err != nil {
343
- return "" , fmt .Errorf ("failed to create temporary directory: %w" , err )
344
- }
334
+ dir , err := createDir (dir )
335
+ if err != nil {
336
+ return "" , err
345
337
}
346
338
347
339
log .Logger ().Debugf ("cloning %s to directory %s" , termcolor .ColorInfo (gitURL ), termcolor .ColorInfo (dir ))
@@ -354,23 +346,40 @@ func CloneToDir(g Interface, gitURL, dir string) (string, error) {
354
346
return dir , nil
355
347
}
356
348
349
+ // PartialCloneToDir Partially clones the git repository to either the given directory or create a temporary one
350
+ // Alternative to SparseCloneToDir when git provider does not support sparse-checkout
351
+ // sparseCheckoutPatterns not supported
352
+ // If shallow is true the clone is made with --depth=1
353
+ func PartialCloneToDir (g Interface , gitURL , dir string , shallow bool ) (string , error ) {
354
+ dir , err := createDir (dir )
355
+ if err != nil {
356
+ return "" , err
357
+ }
358
+
359
+ log .Logger ().Debugf ("initiating partial clone %s to directory %s" , termcolor .ColorInfo (gitURL ), termcolor .ColorInfo (dir ))
360
+
361
+ parentDir := filepath .Dir (dir )
362
+ partialCloneArgs := []string {"clone" , "--filter=blob:none" }
363
+ if shallow {
364
+ log .Logger ().Debugf ("setting clone depth to 1" )
365
+ partialCloneArgs = append (partialCloneArgs , "--depth=1" )
366
+ }
367
+ _ , err = g .Command (parentDir , append (partialCloneArgs , gitURL , dir )... )
368
+ if err != nil {
369
+ return "" , fmt .Errorf ("failed to partially clone repository %s to directory: %s: %w" , gitURL , dir , err )
370
+ }
371
+ return dir , nil
372
+ }
373
+
357
374
// SparseCloneToDir clones the git repository sparsely to either the given directory or create a temporary on.
358
375
// SparseCheckoutPatterns are checked out interpreted as in .gitignore. If no sparseCheckoutPatterns are given the files
359
376
// directly under the root of the repository are checked out.
360
377
// NOTE: This functionality is experimental and also the behaviour may vary between different git servers.
361
378
// If shallow is true the clone is made with --depth=1
362
379
func SparseCloneToDir (g Interface , gitURL , dir string , shallow bool , sparseCheckoutPatterns ... string ) (string , error ) {
363
- var err error
364
- if dir != "" {
365
- err = os .MkdirAll (dir , util .DefaultWritePermissions )
366
- if err != nil {
367
- return "" , fmt .Errorf ("failed to create directory %s: %w" , dir , err )
368
- }
369
- } else {
370
- dir , err = os .MkdirTemp ("" , "jx-git-" )
371
- if err != nil {
372
- return "" , fmt .Errorf ("failed to create temporary directory: %w" , err )
373
- }
380
+ dir , err := createDir (dir )
381
+ if err != nil {
382
+ return "" , err
374
383
}
375
384
376
385
log .Logger ().Debugf ("cloning %s to directory %s sparsely" , termcolor .ColorInfo (gitURL ), termcolor .ColorInfo (dir ))
@@ -566,6 +575,24 @@ func CheckoutRemoteBranch(g Interface, dir string, branch string) error {
566
575
return Checkout (g , dir , branch )
567
576
}
568
577
578
+ // createDir creates input directory if it does not exist, or creates a temporary directory
579
+ // createDir creates the input directory if it does not exist, or creates a temporary directory
580
+ func createDir (dir string ) (string , error ) {
581
+ var err error
582
+ if dir != "" {
583
+ err = os .MkdirAll (dir , util .DefaultWritePermissions )
584
+ if err != nil {
585
+ return "" , fmt .Errorf ("failed to create directory %s: %w" , dir , err )
586
+ }
587
+ } else {
588
+ dir , err = os .MkdirTemp ("" , "jx-git-" )
589
+ if err != nil {
590
+ return "" , fmt .Errorf ("failed to create temporary directory: %w" , err )
591
+ }
592
+ }
593
+ return dir , nil
594
+ }
595
+
569
596
// GetLatestCommitMessage returns the latest git commit message
570
597
func GetLatestCommitMessage (g Interface , dir string ) (string , error ) {
571
598
return g .Command (dir , "log" , "-1" , "--pretty=%B" )
0 commit comments