-
Notifications
You must be signed in to change notification settings - Fork 534
repository: added cleanup for PlainClone. Fixes #741 #880
Conversation
repository.go
Outdated
@@ -334,7 +335,23 @@ func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (bfs billy.Files | |||
// if the new repository will be bare or normal. If the path is not empty | |||
// ErrRepositoryAlreadyExists is returned. | |||
func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error) { | |||
return PlainCloneContext(context.Background(), path, isBare, o) | |||
r, err := PlainCloneContext(context.Background(), path, isBare, o) | |||
if err != nil { |
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.
Cleanup logic should be the same for PlainCloneContext
repository.go
Outdated
os.Remove(path) | ||
return nil, err | ||
} | ||
} |
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.
nitpick: add a space here (we usually add a space after every if/for block.
repository.go
Outdated
case ErrRepositoryAlreadyExists: | ||
return nil, err | ||
default: | ||
os.Remove(path) |
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.
Why the default case is Remove
and auth errors are RemoveAll
?
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.
Please, add some test.
repository.go
Outdated
@@ -334,7 +334,13 @@ func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (bfs billy.Files | |||
// if the new repository will be bare or normal. If the path is not empty | |||
// ErrRepositoryAlreadyExists is returned. | |||
func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error) { | |||
return PlainCloneContext(context.Background(), path, isBare, o) | |||
r, err := PlainCloneContext(context.Background(), path, isBare, o) |
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.
Keep it in a single place. If you implement it in PlainCloneContext
, then that's it, you don't need it here.
Signed-off-by: Bartek Jaroszewski <[email protected]>
|
||
_, err = os.Stat(dir) | ||
dirNotExist := os.IsNotExist(err) | ||
c.Assert(dirNotExist, Equals, true) |
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.
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.
Signed-off-by: Bartek Jaroszewski <[email protected]>
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 comment
The 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.
@bjaroszewski friendly ping |
Signed-off-by: Bartek Jaroszewski <[email protected]>
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.
Looks good. Some style comments:
repository.go
Outdated
dirExist := false | ||
|
||
file, err := os.Stat(path) | ||
pathNotExist := os.IsNotExist(err) |
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:
file, err := os.Stat(path)
if os.IsNotExist(err) {
dirExist = true
} else if err != nil {
return nil, err
}
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 write if !os.IsNotExist(err)
.
repository.go
Outdated
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Here if err
is not nil or io.EOF
, an error should be returned.
return nil, err | ||
} | ||
} | ||
} else if !dirExist { |
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.
Avoid additional indentation by reversing condition order:
if !dirExist {
os.RemoveAll(path)
return nil, err
}
// continue here
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.
Meh, I confused dirExist
and dirEmpty
, sorry.
repository_test.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Remove commented out code.
CI fails:
|
Signed-off-by: Bartek Jaroszewski <[email protected]>
199a321
to
4599a77
Compare
Signed-off-by: Bartek Jaroszewski <[email protected]>
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.
Your typo fix still had a typo: NotNill should be NotNil
@@ -576,7 +576,7 @@ func (s *RepositorySuite) TestPlainCloneContextWithProperParameters(c *C) { | |||
URL: s.GetBasicLocalRepositoryURL(), | |||
}) | |||
|
|||
c.Assert(r, NoNill) | |||
c.Assert(r, NotNill) |
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.
still a typo here... you want NotNil, not NotNill
This PR introduces the cleanup for the PlainClone().
While using PlainClone, if the clone will raise an error (f.e. incorrect credentials used for authentication) we have work dir left.