@@ -17,7 +17,6 @@ import (
17
17
"github.com/ProtonMail/go-crypto/openpgp"
18
18
"github.com/go-git/go-billy/v5"
19
19
"github.com/go-git/go-billy/v5/osfs"
20
- "github.com/go-git/go-billy/v5/util"
21
20
"github.com/go-git/go-git/v6/config"
22
21
"github.com/go-git/go-git/v6/internal/path_util"
23
22
"github.com/go-git/go-git/v6/internal/revision"
65
64
ErrAlternatePathNotSupported = errors .New ("alternate path must use the file scheme" )
66
65
ErrUnsupportedMergeStrategy = errors .New ("unsupported merge strategy" )
67
66
ErrFastForwardMergeNotPossible = errors .New ("not possible to fast-forward merge changes" )
67
+ ErrCloneDirNotEmpty = errors .New ("clone destination path already exists and is not empty" )
68
68
)
69
69
70
70
// Repository represents a git repository
@@ -491,21 +491,26 @@ func dotGitCommonDirectory(fs billy.Filesystem) (commonDir billy.Filesystem, err
491
491
492
492
// PlainClone a repository into the path with the given options, isBare defines
493
493
// if the new repository will be bare or normal. If the path is not empty
494
- // ErrRepositoryAlreadyExists is returned.
494
+ // ErrCloneDirNotEmpty is returned.
495
495
func PlainClone (path string , o * CloneOptions ) (* Repository , error ) {
496
496
return PlainCloneContext (context .Background (), path , o )
497
497
}
498
498
499
499
// PlainCloneContext a repository into the path with the given options, isBare
500
500
// defines if the new repository will be bare or normal. If the path is not empty
501
- // ErrRepositoryAlreadyExists is returned.
501
+ // ErrCloneDirNotEmpty is returned.
502
502
//
503
503
// The provided Context must be non-nil. If the context expires before the
504
504
// operation is complete, an error is returned. The context only affects the
505
505
// transport operations.
506
- //
507
- // TODO(smola): refuse upfront to clone on a non-empty directory in v5, see #1027
508
506
func PlainCloneContext (ctx context.Context , path string , o * CloneOptions ) (* Repository , error ) {
507
+ empty , err := checkCloneDirIsEmpty (path )
508
+ if err != nil {
509
+ return nil , err
510
+ }
511
+ if ! empty {
512
+ return nil , fmt .Errorf ("%w %s" , ErrCloneDirNotEmpty , path )
513
+ }
509
514
start := time .Now ()
510
515
defer func () {
511
516
url := ""
@@ -515,11 +520,6 @@ func PlainCloneContext(ctx context.Context, path string, o *CloneOptions) (*Repo
515
520
trace .Performance .Printf ("performance: %.9f s: git command: git clone %s" , time .Since (start ).Seconds (), url )
516
521
}()
517
522
518
- cleanup , cleanupParent , err := checkIfCleanupIsNeeded (path )
519
- if err != nil {
520
- return nil , err
521
- }
522
-
523
523
isBare := o .Bare
524
524
if o .Mirror {
525
525
isBare = true
@@ -530,12 +530,6 @@ func PlainCloneContext(ctx context.Context, path string, o *CloneOptions) (*Repo
530
530
}
531
531
532
532
err = r .clone (ctx , o )
533
- if err != nil && err != ErrRepositoryAlreadyExists {
534
- if cleanup {
535
- _ = cleanUpDir (path , cleanupParent )
536
- }
537
- }
538
-
539
533
return r , err
540
534
}
541
535
@@ -547,49 +541,30 @@ func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository {
547
541
}
548
542
}
549
543
550
- func checkIfCleanupIsNeeded (path string ) (cleanup bool , cleanParent bool , err error ) {
544
+ func checkCloneDirIsEmpty (path string ) (empty bool , err error ) {
551
545
fi , err := osfs .Default .Stat (path )
552
546
if err != nil {
553
547
if os .IsNotExist (err ) {
554
- return true , true , nil
548
+ return true , nil
555
549
}
556
550
557
- return false , false , err
551
+ return false , err
558
552
}
559
553
560
554
if ! fi .IsDir () {
561
- return false , false , fmt .Errorf ("path is not a directory: %s" , path )
555
+ return false , fmt .Errorf ("path is not a directory: %s" , path )
562
556
}
563
557
564
558
files , err := osfs .Default .ReadDir (path )
565
559
if err != nil {
566
- return false , false , err
560
+ return false , err
567
561
}
568
562
569
563
if len (files ) == 0 {
570
- return true , false , nil
571
- }
572
-
573
- return false , false , nil
574
- }
575
-
576
- func cleanUpDir (path string , all bool ) error {
577
- if all {
578
- return util .RemoveAll (osfs .Default , path )
579
- }
580
-
581
- files , err := osfs .Default .ReadDir (path )
582
- if err != nil {
583
- return err
584
- }
585
-
586
- for _ , fi := range files {
587
- if err := util .RemoveAll (osfs .Default , osfs .Default .Join (path , fi .Name ())); err != nil {
588
- return err
589
- }
564
+ return true , nil
590
565
}
591
566
592
- return err
567
+ return false , nil
593
568
}
594
569
595
570
// Config return the repository config. In a filesystem backed repository this
0 commit comments