From c8215b4446e616d765c49e66a72e8f2fe426e2d2 Mon Sep 17 00:00:00 2001 From: Brian Hendriks Date: Thu, 7 Nov 2019 12:25:26 -0800 Subject: [PATCH] checkout a remote only branch --- go/cmd/dolt/commands/checkout.go | 21 ++++++++++++++++++++- go/libraries/doltcore/ref/remote_ref.go | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/go/cmd/dolt/commands/checkout.go b/go/cmd/dolt/commands/checkout.go index fe6854ed5a..a190fb8ef5 100644 --- a/go/cmd/dolt/commands/checkout.go +++ b/go/cmd/dolt/commands/checkout.go @@ -17,6 +17,8 @@ package commands import ( "context" + "github.com/liquidata-inc/dolt/go/libraries/doltcore/ref" + "github.com/liquidata-inc/dolt/go/cmd/dolt/cli" "github.com/liquidata-inc/dolt/go/cmd/dolt/errhand" "github.com/liquidata-inc/dolt/go/libraries/doltcore/doltdb" @@ -76,7 +78,24 @@ func Checkout(ctx context.Context, commandStr string, args []string, dEnv *env.D } else if isBranch { verr = checkoutBranch(ctx, dEnv, name) } else { - verr = errhand.BuildDError("error: could not find %s", name).Build() + refs, err := dEnv.DoltDB.GetRefs(ctx) + + if err != nil { + verr = errhand.BuildDError("fatal: unable to read from data repository.").AddCause(err).Build() + } + + found := false + for _, rf := range refs { + if remRef, ok := rf.(ref.RemoteRef); ok && remRef.GetBranch() == name { + verr = checkoutNewBranch(ctx, dEnv, name, rf.String()) + found = true + break + } + } + + if !found { + verr = errhand.BuildDError("error: could not find %s", name).Build() + } } } diff --git a/go/libraries/doltcore/ref/remote_ref.go b/go/libraries/doltcore/ref/remote_ref.go index 9d41cacc98..9deec0ac22 100644 --- a/go/libraries/doltcore/ref/remote_ref.go +++ b/go/libraries/doltcore/ref/remote_ref.go @@ -47,6 +47,11 @@ func (rr RemoteRef) GetRemote() string { return rr.remote } +// GetBranch returns the name of a remote branch +func (rr RemoteRef) GetBranch() string { + return rr.branch +} + // NewRemoteRef creates a remote ref from an origin name and a path func NewRemoteRef(remote, branch string) RemoteRef { return RemoteRef{remote, branch}