51
51
ErrIsBareRepository = errors .New ("worktree not available in a bare repository" )
52
52
ErrUnableToResolveCommit = errors .New ("unable to resolve commit" )
53
53
ErrPackedObjectsNotSupported = errors .New ("Packed objects not supported" )
54
- ErrDirNotEmpty = errors .New ("directory is not empty" )
55
54
)
56
55
57
56
// Repository represents a git repository
@@ -344,65 +343,19 @@ func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error)
344
343
//
345
344
// TODO(mcuadros): move isBare to CloneOptions in v5
346
345
func PlainCloneContext (ctx context.Context , path string , isBare bool , o * CloneOptions ) (* Repository , error ) {
347
- dirEmpty := false
348
- dirExist := false
349
-
350
- file , err := os .Stat (path )
346
+ dirExists , err := checkExistsAndIsEmptyDir (path )
351
347
if err != nil {
352
348
return nil , err
353
349
}
354
350
355
- if ! os .IsNotExist (err ) {
356
- dirExist = file .IsDir ()
357
- }
358
-
359
- if dirExist {
360
- fh , err := os .Open (path )
361
- if err != nil {
362
- return nil , err
363
- }
364
- defer fh .Close ()
365
-
366
- names , err := fh .Readdirnames (1 )
367
- if err != io .EOF && err != nil {
368
- return nil , err
369
- }
370
- if len (names ) == 0 {
371
- dirEmpty = true
372
- } else {
373
- return nil , ErrDirNotEmpty
374
- }
375
- }
376
-
377
351
r , err := PlainInit (path , isBare )
378
352
if err != nil {
379
353
return nil , err
380
354
}
381
355
382
356
err = r .clone (ctx , o )
383
357
if err != nil && err != ErrRepositoryAlreadyExists {
384
- if dirEmpty {
385
- fh , err := os .Open (path )
386
- if err != nil {
387
- return nil , err
388
- }
389
- defer fh .Close ()
390
-
391
- names , err := fh .Readdirnames (- 1 )
392
- if err != nil && err != io .EOF {
393
- return nil , err
394
- }
395
-
396
- for _ , name := range names {
397
- err = os .RemoveAll (filepath .Join (path , name ))
398
- if err != nil {
399
- return nil , err
400
- }
401
- }
402
- } else if ! dirExist {
403
- os .RemoveAll (path )
404
- return nil , err
405
- }
358
+ cleanUpDir (path , ! dirExists )
406
359
}
407
360
408
361
return r , err
@@ -416,6 +369,65 @@ func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository {
416
369
}
417
370
}
418
371
372
+ func checkExistsAndIsEmptyDir (path string ) (exists bool , err error ) {
373
+ fi , err := os .Stat (path )
374
+ if err != nil {
375
+ if os .IsNotExist (err ) {
376
+ return false , nil
377
+ }
378
+
379
+ return false , err
380
+ }
381
+
382
+ if ! fi .IsDir () {
383
+ return false , fmt .Errorf ("path is not a directory: %s" , path )
384
+ }
385
+
386
+ f , err := os .Open (path )
387
+ if err != nil {
388
+ return false , err
389
+ }
390
+
391
+ defer ioutil .CheckClose (f , & err )
392
+
393
+ _ , err = f .Readdirnames (1 )
394
+ if err == io .EOF {
395
+ return true , nil
396
+ }
397
+
398
+ if err != nil {
399
+ return true , err
400
+ }
401
+
402
+ return true , fmt .Errorf ("directory is not empty: %s" , path )
403
+ }
404
+
405
+ func cleanUpDir (path string , all bool ) error {
406
+ if all {
407
+ return os .RemoveAll (path )
408
+ }
409
+
410
+ f , err := os .Open (path )
411
+ if err != nil {
412
+ return err
413
+ }
414
+
415
+ defer ioutil .CheckClose (f , & err )
416
+
417
+ names , err := f .Readdirnames (- 1 )
418
+ if err != nil {
419
+ return err
420
+ }
421
+
422
+ for _ , name := range names {
423
+ if err := os .RemoveAll (filepath .Join (path , name )); err != nil {
424
+ return err
425
+ }
426
+ }
427
+
428
+ return nil
429
+ }
430
+
419
431
// Config return the repository config
420
432
func (r * Repository ) Config () (* config.Config , error ) {
421
433
return r .Storer .Config ()
0 commit comments