-
Notifications
You must be signed in to change notification settings - Fork 534
repository: added cleanup for PlainClone. Fixes #741 #880
Changes from 4 commits
77f738c
203a372
5230acf
520ddb5
4599a77
6d3a9c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
stdioutil "io/ioutil" | ||
"os" | ||
"path" | ||
|
@@ -347,12 +348,64 @@ func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error) | |
// operation is complete, an error is returned. The context only affects to the | ||
// transport operations. | ||
func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOptions) (*Repository, error) { | ||
dirEmpty := false | ||
dirExist := false | ||
|
||
file, err := os.Stat(path) | ||
pathNotExist := os.IsNotExist(err) | ||
|
||
if !pathNotExist { | ||
dirExist = file.IsDir() | ||
} | ||
|
||
if dirExist { | ||
fh, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer fh.Close() | ||
|
||
names, err := fh.Readdirnames(1) | ||
|
||
if err == io.EOF && len(names) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here if |
||
dirEmpty = true | ||
} else { | ||
return nil, ErrDirNotEmpty | ||
} | ||
} | ||
|
||
r, err := PlainInit(path, isBare) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return r, r.clone(ctx, o) | ||
err = r.clone(ctx, o) | ||
if err != nil && err != ErrRepositoryAlreadyExists { | ||
if dirEmpty { | ||
fh, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer fh.Close() | ||
|
||
names, err := fh.Readdirnames(-1) | ||
if err != nil && err != io.EOF { | ||
return nil, err | ||
} | ||
|
||
for _, name := range names { | ||
err = os.RemoveAll(filepath.Join(path, name)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} else if !dirExist { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid additional indentation by reversing condition order:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meh, I confused |
||
os.RemoveAll(path) | ||
return nil, err | ||
} | ||
} | ||
|
||
return r, err | ||
} | ||
|
||
func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -568,7 +568,7 @@ func (s *RepositorySuite) TestPlainClone(c *C) { | |
c.Assert(cfg.Branches["master"].Name, Equals, "master") | ||
} | ||
|
||
func (s *RepositorySuite) TestPlainCloneContext(c *C) { | ||
func (s *RepositorySuite) TestPlainCloneContextWithProperParameters(c *C) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
|
@@ -579,6 +579,38 @@ func (s *RepositorySuite) TestPlainCloneContext(c *C) { | |
c.Assert(err, NotNil) | ||
} | ||
|
||
func (s *RepositorySuite) TestPlainCloneContextWithIncorrectRepo(c *C) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
tmpDir := c.MkDir() | ||
repoDir := filepath.Join(tmpDir, "repoDir") //path.Join(path.Dir(tmpDir), "repoDir") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented out code. |
||
r, err := PlainCloneContext(ctx, repoDir, false, &CloneOptions{ | ||
URL: "incorrectOnPurpose", | ||
}) | ||
c.Assert(r, IsNil) | ||
c.Assert(err, NotNil) | ||
|
||
_, err = os.Stat(repoDir) | ||
dirNotExist := os.IsNotExist(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be another test to check that if the directory exists, it is not removed by PlainClone. |
||
c.Assert(dirNotExist, Equals, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behaviour does not seem correct: it removes a directory that already existed before. go-git shouldn't remove anything it didn't create during clone. |
||
} | ||
|
||
func (s *RepositorySuite) TestPlainCloneContextWithNotEmptyDir(c *C) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
tmpDir := c.MkDir() | ||
err := ioutil.WriteFile(filepath.Join(tmpDir, "dummyFile"), []byte(fmt.Sprint("dummyContent")), 0644) | ||
c.Assert(err, IsNil) | ||
|
||
r, err := PlainCloneContext(ctx, tmpDir, false, &CloneOptions{ | ||
URL: "incorrectOnPurpose", | ||
}) | ||
c.Assert(r, IsNil) | ||
c.Assert(err, Equals, ErrDirNotEmpty) | ||
} | ||
|
||
func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) { | ||
if testing.Short() { | ||
c.Skip("skipping test in short mode.") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write here:
If
os.Stat
returns an error and it is not a not exist error, something is wrong (e.g. permissions) and we should not continue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my snippet was wrong.
Leave your logic as it is if behavior with permission errors is correct.
Do not use
pathNotExist
variable here only to use it for the if condition. Just writeif !os.IsNotExist(err)
.