@@ -17,7 +17,6 @@ import (
17
17
"github.com/ProtonMail/go-crypto/openpgp"
18
18
"github.com/go-git/go-billy/v6"
19
19
"github.com/go-git/go-billy/v6/osfs"
20
- "github.com/go-git/go-billy/v6/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
@@ -306,7 +306,6 @@ func PlainInit(path string, isBare bool, options ...InitOption) (*Repository, er
306
306
}
307
307
s := filesystem .NewStorage (dot , cache .NewObjectLRUDefault ())
308
308
r , err := initFn (s )
309
-
310
309
if err != nil {
311
310
return nil , err
312
311
}
@@ -491,21 +490,26 @@ func dotGitCommonDirectory(fs billy.Filesystem) (commonDir billy.Filesystem, err
491
490
492
491
// PlainClone a repository into the path with the given options, isBare defines
493
492
// if the new repository will be bare or normal. If the path is not empty
494
- // ErrRepositoryAlreadyExists is returned.
493
+ // ErrCloneDirNotEmpty is returned.
495
494
func PlainClone (path string , o * CloneOptions ) (* Repository , error ) {
496
495
return PlainCloneContext (context .Background (), path , o )
497
496
}
498
497
499
498
// PlainCloneContext a repository into the path with the given options, isBare
500
499
// defines if the new repository will be bare or normal. If the path is not empty
501
- // ErrRepositoryAlreadyExists is returned.
500
+ // ErrCloneDirNotEmpty is returned.
502
501
//
503
502
// The provided Context must be non-nil. If the context expires before the
504
503
// operation is complete, an error is returned. The context only affects the
505
504
// transport operations.
506
- //
507
- // TODO(smola): refuse upfront to clone on a non-empty directory in v5, see #1027
508
505
func PlainCloneContext (ctx context.Context , path string , o * CloneOptions ) (* Repository , error ) {
506
+ empty , err := checkCloneDirIsEmpty (path )
507
+ if err != nil {
508
+ return nil , err
509
+ }
510
+ if ! empty {
511
+ return nil , fmt .Errorf ("%w %s" , ErrCloneDirNotEmpty , path )
512
+ }
509
513
start := time .Now ()
510
514
defer func () {
511
515
url := ""
@@ -515,11 +519,6 @@ func PlainCloneContext(ctx context.Context, path string, o *CloneOptions) (*Repo
515
519
trace .Performance .Printf ("performance: %.9f s: git command: git clone %s" , time .Since (start ).Seconds (), url )
516
520
}()
517
521
518
- cleanup , cleanupParent , err := checkIfCleanupIsNeeded (path )
519
- if err != nil {
520
- return nil , err
521
- }
522
-
523
522
isBare := o .Bare
524
523
if o .Mirror {
525
524
isBare = true
@@ -530,12 +529,6 @@ func PlainCloneContext(ctx context.Context, path string, o *CloneOptions) (*Repo
530
529
}
531
530
532
531
err = r .clone (ctx , o )
533
- if err != nil && err != ErrRepositoryAlreadyExists {
534
- if cleanup {
535
- _ = cleanUpDir (path , cleanupParent )
536
- }
537
- }
538
-
539
532
return r , err
540
533
}
541
534
@@ -547,49 +540,30 @@ func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository {
547
540
}
548
541
}
549
542
550
- func checkIfCleanupIsNeeded (path string ) (cleanup bool , cleanParent bool , err error ) {
543
+ func checkCloneDirIsEmpty (path string ) (empty bool , err error ) {
551
544
fi , err := osfs .Default .Stat (path )
552
545
if err != nil {
553
546
if os .IsNotExist (err ) {
554
- return true , true , nil
547
+ return true , nil
555
548
}
556
549
557
- return false , false , err
550
+ return false , err
558
551
}
559
552
560
553
if ! fi .IsDir () {
561
- return false , false , fmt .Errorf ("path is not a directory: %s" , path )
554
+ return false , fmt .Errorf ("path is not a directory: %s" , path )
562
555
}
563
556
564
557
files , err := osfs .Default .ReadDir (path )
565
558
if err != nil {
566
- return false , false , err
559
+ return false , err
567
560
}
568
561
569
562
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
- }
563
+ return true , nil
590
564
}
591
565
592
- return err
566
+ return false , nil
593
567
}
594
568
595
569
// Config return the repository config. In a filesystem backed repository this
0 commit comments