Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 45 additions & 89 deletions internal/gps/vcs_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ type gitRepo struct {
*vcs.GitRepo
}

func newVcsRemoteErrorOr(msg string, err error, out string) error {
func newVcsRemoteErrorOr(err error, args []string, out, msg string) error {
if err == context.Canceled || err == context.DeadlineExceeded {
return err
}
return vcs.NewRemoteError(msg, err, out)
return vcs.NewRemoteError(msg, errors.Wrapf(err, "command failed: %v", args), out)
}

func newVcsLocalErrorOr(msg string, err error, out string) error {
func newVcsLocalErrorOr(err error, args []string, out, msg string) error {
if err == context.Canceled || err == context.DeadlineExceeded {
return err
}
return vcs.NewLocalError(msg, err, out)
return vcs.NewLocalError(msg, errors.Wrapf(err, "command failed: %v", args), out)
}

func (r *gitRepo) get(ctx context.Context) error {
Expand All @@ -96,11 +96,8 @@ func (r *gitRepo) get(ctx context.Context) error {
r.LocalPath(),
)
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to get repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}

return nil
Expand All @@ -117,10 +114,8 @@ func (r *gitRepo) fetch(ctx context.Context) error {
)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to update repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out))
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}
return nil
}
Expand All @@ -129,10 +124,8 @@ func (r *gitRepo) updateVersion(ctx context.Context, v string) error {
cmd := commandContext(ctx, "git", "checkout", v)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(
"unable to update checked out version",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out))
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}

return r.defendAgainstSubmodules(ctx)
Expand All @@ -153,11 +146,8 @@ func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {
)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(
"unexpected error while defensively updating submodules",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively updating submodules")
}
}

Expand All @@ -167,11 +157,8 @@ func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {
cmd := commandContext(ctx, "git", "clean", "-x", "-d", "-f", "-f")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(
"unexpected error while defensively cleaning up after possible derelict submodule directories",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively cleaning up after possible derelict submodule directories")
}
}

Expand All @@ -188,11 +175,8 @@ func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {
)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(
"unexpected error while defensively cleaning up after possible derelict nested submodule directories",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively cleaning up after possible derelict nested submodule directories")
}
}

Expand All @@ -208,17 +192,14 @@ func (r *bzrRepo) get(ctx context.Context) error {
if _, err := os.Stat(basePath); os.IsNotExist(err) {
err = os.MkdirAll(basePath, 0755)
if err != nil {
return newVcsLocalErrorOr("unable to create directory", err, "")
return newVcsLocalErrorOr(err, nil, "", "unable to create directory")
}
}

cmd := commandContext(ctx, "bzr", "branch", r.Remote(), r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to get repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}

return nil
Expand All @@ -228,11 +209,8 @@ func (r *bzrRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "bzr", "pull")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to update repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}
return nil
}
Expand All @@ -241,11 +219,8 @@ func (r *bzrRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "bzr", "update", "-r", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(
"unable to update checked out version",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}
return nil
}
Expand All @@ -257,11 +232,8 @@ type hgRepo struct {
func (r *hgRepo) get(ctx context.Context) error {
cmd := commandContext(ctx, "hg", "clone", r.Remote(), r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to get repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}

return nil
Expand All @@ -271,11 +243,8 @@ func (r *hgRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "hg", "pull")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to fetch latest changes",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to fetch latest changes")
}
return nil
}
Expand All @@ -284,11 +253,8 @@ func (r *hgRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "hg", "update", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to update checked out version",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}

return nil
Expand All @@ -308,11 +274,8 @@ func (r *svnRepo) get(ctx context.Context) error {

cmd := commandContext(ctx, "svn", "checkout", remote, r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to get repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}

return nil
Expand All @@ -322,11 +285,8 @@ func (r *svnRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "svn", "update")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to update repository",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}

return nil
Expand All @@ -336,11 +296,8 @@ func (r *svnRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "svn", "update", "-r", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(
"unable to update checked out version",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}

return nil
Expand All @@ -364,15 +321,14 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
cmd.SetDir(r.LocalPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, newVcsLocalErrorOr("unable to retrieve commit information",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}

infos := new(info)
if err := xml.Unmarshal(out, &infos); err != nil {
return nil, newVcsLocalErrorOr("unable to retrieve commit information", err, string(out))
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}

id = infos.Commit.Revision
Expand All @@ -385,10 +341,8 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
cmd.SetDir(r.LocalPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, newVcsRemoteErrorOr("unable to retrieve commit information",
errors.Wrapf(err, "command failed: %v", cmd.Args()),
string(out),
)
return nil, newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}

type logentry struct {
Expand All @@ -404,7 +358,8 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {

logs := new(log)
if err := xml.Unmarshal(out, &logs); err != nil {
return nil, newVcsLocalErrorOr("unable to retrieve commit information", err, string(out))
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}

if len(logs.Logs) == 0 {
Expand All @@ -420,7 +375,8 @@ func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
if len(logs.Logs[0].Date) > 0 {
ci.Date, err = time.Parse(time.RFC3339Nano, logs.Logs[0].Date)
if err != nil {
return nil, newVcsLocalErrorOr("unable to retrieve commit information", err, string(out))
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
}

Expand Down
14 changes: 7 additions & 7 deletions internal/gps/vcs_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ import (
const gitRemoteTestRepo = "https://github.com/Masterminds/VCSTestRepo"

func TestErrs(t *testing.T) {
err := newVcsLocalErrorOr("", context.Canceled, "")
err := newVcsLocalErrorOr(context.Canceled, nil, "", "")
if err != context.Canceled {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsRemoteErrorOr("", context.Canceled, "")
err = newVcsRemoteErrorOr(context.Canceled, nil, "", "")
if err != context.Canceled {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsLocalErrorOr("", context.DeadlineExceeded, "")
err = newVcsLocalErrorOr(context.DeadlineExceeded, nil, "", "")
if err != context.DeadlineExceeded {
t.Errorf("context errors should always pass through, got %s", err)
}
err = newVcsRemoteErrorOr("", context.DeadlineExceeded, "")
err = newVcsRemoteErrorOr(context.DeadlineExceeded, nil, "", "")
if err != context.DeadlineExceeded {
t.Errorf("context errors should always pass through, got %s", err)
}

err = newVcsLocalErrorOr("foo", errors.New("bar"), "baz")
err = newVcsLocalErrorOr(errors.New("bar"), nil, "foo", "baz")
if _, is := err.(*vcs.LocalError); !is {
t.Errorf("should have gotten local error, got %T %v", err, err)
}
err = newVcsRemoteErrorOr("foo", errors.New("bar"), "baz")
err = newVcsRemoteErrorOr(errors.New("bar"), nil, "foo", "baz")
if _, is := err.(*vcs.RemoteError); !is {
t.Errorf("should have gotten remote error, got %T %v", err, err)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func testSvnRepo(t *testing.T) {
// Do an initial checkout.
err = repo.get(ctx)
if err != nil {
t.Fatalf("Unable to checkout SVN repo. Err was %#v", err)
t.Fatalf("Unable to checkout SVN repo. Err was %s", err)
}

// Verify SVN repo is a SVN repo
Expand Down