Skip to content

Commit

Permalink
repository: added cleanup for the PlainCloneContext()
Browse files Browse the repository at this point in the history
Signed-off-by: Bartek Jaroszewski <[email protected]>
  • Loading branch information
bjaroszewski authored and smola committed Oct 30, 2018
1 parent dfd6c82 commit 507681b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
60 changes: 59 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
stdioutil "io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -50,6 +51,7 @@ var (
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
ErrPackedObjectsNotSupported = errors.New("Packed objects not supported")
ErrDirNotEmpty = errors.New("directory is not empty")
)

// Repository represents a git repository
Expand Down Expand Up @@ -342,12 +344,68 @@ func PlainClone(path string, isBare bool, o *CloneOptions) (*Repository, error)
//
// TODO(mcuadros): move isBare to CloneOptions in v5
func PlainCloneContext(ctx context.Context, path string, isBare bool, o *CloneOptions) (*Repository, error) {
dirEmpty := false
dirExist := false

file, err := os.Stat(path)
if err != nil {
return nil, err
}

if !os.IsNotExist(err) {
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 && err != nil {
return nil, err
}
if len(names) == 0 {
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 {
os.RemoveAll(path)
return nil, err
}
}

return r, err
}

func newRepository(s storage.Storer, worktree billy.Filesystem) *Repository {
Expand Down
48 changes: 43 additions & 5 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,55 @@ func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) {
c.Assert(remote, NotNil)
}

func (s *RepositorySuite) TestPlainCloneContext(c *C) {
func (s *RepositorySuite) TestPlainCloneContextWithProperParameters(c *C) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := PlainCloneContext(ctx, c.MkDir(), false, &CloneOptions{
r, err := PlainCloneContext(ctx, c.MkDir(), false, &CloneOptions{
URL: s.GetBasicLocalRepositoryURL(),
})

c.Assert(r, NotNil)
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")
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)
c.Assert(dirNotExist, Equals, true)
}

func (s *RepositorySuite) TestPlainCloneContextWithNotEmptyDir(c *C) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

tmpDir := c.MkDir()
repoDirPath := filepath.Join(tmpDir, "repoDir")
err := os.Mkdir(repoDirPath, 0777)
c.Assert(err, IsNil)

dummyFile := filepath.Join(repoDirPath, "dummyFile")
err = ioutil.WriteFile(dummyFile, []byte(fmt.Sprint("dummyContent")), 0644)
c.Assert(err, IsNil)

r, err := PlainCloneContext(ctx, repoDirPath, 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.")
Expand Down Expand Up @@ -2104,9 +2142,9 @@ func (s *RepositorySuite) TestResolveRevisionWithErrors(c *C) {
c.Assert(err, IsNil)

datas := map[string]string{
"efs/heads/master~": "reference not found",
"HEAD^3": `Revision invalid : "3" found must be 0, 1 or 2 after "^"`,
"HEAD^{/whatever}": `No commit message match regexp : "whatever"`,
"efs/heads/master~": "reference not found",
"HEAD^3": `Revision invalid : "3" found must be 0, 1 or 2 after "^"`,
"HEAD^{/whatever}": `No commit message match regexp : "whatever"`,
"4e1243bd22c66e76c2ba9eddc1f91394e57f9f83": "reference not found",
"918c48b83bd081e863dbe1b80f8998f058cd8294": `refname "918c48b83bd081e863dbe1b80f8998f058cd8294" is ambiguous`,
}
Expand Down

0 comments on commit 507681b

Please sign in to comment.