aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2018-11-27 11:42:07 +0100
committerSantiago M. Mola <santi@mola.io>2018-11-27 13:18:52 +0100
commit7441885e61650066f1b3ffa948caa86f9410bc86 (patch)
treebccbba050baaac17657165b679ebf2197f59b629 /repository.go
parentf62cd8e3495579a8323455fa0c4e6c44bb0d5e09 (diff)
downloadgo-git-7441885e61650066f1b3ffa948caa86f9410bc86.tar.gz
repository: fix plain clone error handling regression
PR #1008 introduced a regression by changing the errors returned by PlainClone when a repository did not exist. This change goes back to returned errors as they were in v4.7.0. Fixes #1027 Signed-off-by: Santiago M. Mola <santi@mola.io>
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go25
1 files changed, 14 insertions, 11 deletions
diff --git a/repository.go b/repository.go
index f548e9a..97134ec 100644
--- a/repository.go
+++ b/repository.go
@@ -342,8 +342,9 @@ func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error)
// transport operations.
//
// TODO(mcuadros): move isBare to CloneOptions in v5
+// TODO(smola): refuse upfront to clone on a non-empty directory in v5, see #1027
func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOptions) (*Repository, error) {
- dirExists, err := checkExistsAndIsEmptyDir(path)
+ cleanup, cleanupParent, err := checkIfCleanupIsNeeded(path)
if err != nil {
return nil, err
}
@@ -355,7 +356,9 @@ func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOp
err = r.clone(ctx, o)
if err != nil && err != ErrRepositoryAlreadyExists {
- cleanUpDir(path, !dirExists)
+ if cleanup {
+ cleanUpDir(path, cleanupParent)
+ }
}
return r, err
@@ -369,37 +372,37 @@ func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository {
}
}
-func checkExistsAndIsEmptyDir(path string) (exists bool, err error) {
+func checkIfCleanupIsNeeded(path string) (cleanup bool, cleanParent bool, err error) {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
- return false, nil
+ return true, true, nil
}
- return false, err
+ return false, false, err
}
if !fi.IsDir() {
- return false, fmt.Errorf("path is not a directory: %s", path)
+ return false, false, fmt.Errorf("path is not a directory: %s", path)
}
f, err := os.Open(path)
if err != nil {
- return false, err
+ return false, false, err
}
defer ioutil.CheckClose(f, &err)
_, err = f.Readdirnames(1)
if err == io.EOF {
- return true, nil
+ return true, false, nil
}
if err != nil {
- return true, err
+ return false, false, err
}
- return true, fmt.Errorf("directory is not empty: %s", path)
+ return false, false, nil
}
func cleanUpDir(path string, all bool) error {
@@ -425,7 +428,7 @@ func cleanUpDir(path string, all bool) error {
}
}
- return nil
+ return err
}
// Config return the repository config