Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

repository: added cleanup for PlainClone. Fixes #741 #880

Closed
wants to merge 6 commits into from

Conversation

bjaroszewski
Copy link
Contributor

@bjaroszewski bjaroszewski commented Jul 3, 2018

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.

@mcuadros mcuadros requested a review from smola July 6, 2018 09:26
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 {
Copy link
Collaborator

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
}
}
Copy link
Collaborator

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)
Copy link
Collaborator

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?

Copy link
Collaborator

@smola smola left a 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)
Copy link
Collaborator

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.


_, err = os.Stat(dir)
dirNotExist := os.IsNotExist(err)
c.Assert(dirNotExist, Equals, true)
Copy link
Collaborator

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.

c.Assert(err, NotNil)

_, err = os.Stat(repoDir)
dirNotExist := os.IsNotExist(err)
Copy link
Collaborator

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.

@mcuadros
Copy link
Contributor

mcuadros commented Aug 7, 2018

@bjaroszewski friendly ping

@bjaroszewski
Copy link
Contributor Author

@mcuadros as per discussion on the slack, I will introduce the changes as @smola suggested for this cleanup on PlainCloneContext(). I should have some free evening this week, so I'll update the PR. Sorry for the delay with it.

Copy link
Collaborator

@smola smola left a 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)
Copy link
Collaborator

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.Statreturns an error and it is not a not exist error, something is wrong (e.g. permissions) and we should not continue.

Copy link
Collaborator

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

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

Copy link
Collaborator

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.

cancel()

tmpDir := c.MkDir()
repoDir := filepath.Join(tmpDir, "repoDir") //path.Join(path.Dir(tmpDir), "repoDir")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out code.

@smola
Copy link
Collaborator

smola commented Sep 17, 2018

CI fails:

# gopkg.in/src-d/go-git.v4
./repository.go:373:16: undefined: ErrDirNotEmpty

Copy link

@PaluMacil PaluMacil left a 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)

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

@mcuadros mcuadros closed this Oct 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants