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

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed May 23, 2017
1 parent df5c648 commit 7854ebf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 61 deletions.
10 changes: 6 additions & 4 deletions internal/gps/maybe_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/url"
"path/filepath"
"strings"

"github.com/Masterminds/vcs"
)

// A maybeSource represents a set of information that, given some
Expand Down Expand Up @@ -82,7 +84,7 @@ func (m maybeGitSource) try(ctx context.Context, cachedir string, c singleSource
ustr := m.url.String()
path := filepath.Join(cachedir, "sources", sanitizer.Replace(ustr))

r, err := newGitRepo(ustr, path)
r, err := newCtxRepo(vcs.Git, ustr, path)

if err != nil {
return nil, 0, unwrapVcsErr(err)
Expand Down Expand Up @@ -138,7 +140,7 @@ func (m maybeGopkginSource) try(ctx context.Context, cachedir string, c singleSo
// So, it's OK to just dumb-join the scheme with the path.
path := filepath.Join(cachedir, "sources", sanitizer.Replace(m.url.Scheme+"/"+m.opath))
ustr := m.url.String()
r, err := newGitRepo(ustr, path)
r, err := newCtxRepo(vcs.Git, ustr, path)

if err != nil {
return nil, 0, unwrapVcsErr(err)
Expand Down Expand Up @@ -186,7 +188,7 @@ func (m maybeBzrSource) try(ctx context.Context, cachedir string, c singleSource
ustr := m.url.String()
path := filepath.Join(cachedir, "sources", sanitizer.Replace(ustr))

r, err := newBzrRepo(ustr, path)
r, err := newCtxRepo(vcs.Bzr, ustr, path)

if err != nil {
return nil, 0, unwrapVcsErr(err)
Expand Down Expand Up @@ -228,7 +230,7 @@ func (m maybeHgSource) try(ctx context.Context, cachedir string, c singleSourceC
ustr := m.url.String()
path := filepath.Join(cachedir, "sources", sanitizer.Replace(ustr))

r, err := newHgRepo(ustr, path)
r, err := newCtxRepo(vcs.Hg, ustr, path)

if err != nil {
return nil, 0, unwrapVcsErr(err)
Expand Down
91 changes: 34 additions & 57 deletions internal/gps/vcs_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,45 @@ type ctxRepo interface {
//ping(context.Context) (bool, error)
}

// original implementation of these methods come from
// https://github.com/Masterminds/vcs
func newCtxRepo(s vcs.Type, ustr, path string) (r ctxRepo, err error) {
r, err = getVCSRepo(s, ustr, path)
if err != nil {
// if vcs could not initialize the repo due to a local error
// then the local repo is in an incorrect state. Remove and
// treat it as a new not-yet-cloned repo.

type gitRepo struct {
*vcs.GitRepo
// TODO(marwan-at-work): pass a logger here to warn/give progress of the above comment.
os.RemoveAll(path)
r, err = getVCSRepo(s, ustr, path)
}

return
}

func newGitRepo(ustr, path string) (*gitRepo, error) {
r, err := vcs.NewGitRepo(ustr, path)
if err != nil {
if _, ok := err.(*vcs.LocalError); ok {
// if vcs could not initialize the repo due to a local error
// then the local repo is in an incorrect state. Remove and
// treat it as a new not-yet-cloned repo.
os.RemoveAll(path)
r, err = vcs.NewGitRepo(ustr, path)
}
}
func getVCSRepo(s vcs.Type, ustr, path string) (r ctxRepo, err error) {
switch s {
case vcs.Git:
var repo *vcs.GitRepo
repo, err = vcs.NewGitRepo(ustr, path)
r = &gitRepo{repo}
case vcs.Bzr:
var repo *vcs.BzrRepo
repo, err = vcs.NewBzrRepo(ustr, path)
r = &bzrRepo{repo}
case vcs.Hg:
var repo *vcs.HgRepo
repo, err = vcs.NewHgRepo(ustr, path)
r = &hgRepo{repo}
}

return
}

if err != nil {
return nil, err
}
// original implementation of these methods come from
// https://github.com/Masterminds/vcs

return &gitRepo{r}, nil
type gitRepo struct {
*vcs.GitRepo
}

func newVcsRemoteErrorOr(msg string, err error, out string) error {
Expand Down Expand Up @@ -120,25 +135,6 @@ type bzrRepo struct {
*vcs.BzrRepo
}

func newBzrRepo(ustr, path string) (*bzrRepo, error) {
r, err := vcs.NewBzrRepo(ustr, path)
if err != nil {
// if vcs could not initialize the repo due to a local error
// then the local repo is in an incorrect state. Remove and
// treat it as a new not-yet-cloned repo.
if _, ok := err.(*vcs.LocalError); ok {
os.RemoveAll(path)
r, err = vcs.NewBzrRepo(ustr, path)
}
}

if err != nil {
return nil, err
}

return &bzrRepo{r}, nil
}

func (r *bzrRepo) get(ctx context.Context) error {
basePath := filepath.Dir(filepath.FromSlash(r.LocalPath()))
if _, err := os.Stat(basePath); os.IsNotExist(err) {
Expand Down Expand Up @@ -176,25 +172,6 @@ type hgRepo struct {
*vcs.HgRepo
}

func newHgRepo(ustr, path string) (*hgRepo, error) {
r, err := vcs.NewHgRepo(ustr, path)
if err != nil {
// if vcs could not initialize the repo due to a local error
// then the local repo is in an incorrect state. Remove and
// treat it as a new not-yet-cloned repo.
if _, ok := err.(*vcs.LocalError); ok {
os.RemoveAll(path)
r, err = vcs.NewHgRepo(ustr, path)
}
}

if err != nil {
return nil, err
}

return &hgRepo{r}, nil
}

func (r *hgRepo) get(ctx context.Context) error {
out, err := runFromCwd(ctx, "hg", "clone", r.Remote(), r.LocalPath())
if err != nil {
Expand Down

0 comments on commit 7854ebf

Please sign in to comment.