Skip to content

Commit

Permalink
improve errors for checking out tags
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cor committed Sep 24, 2024
1 parent 6a2c500 commit 8ed8710
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
9 changes: 5 additions & 4 deletions go/libraries/doltcore/env/actions/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,15 @@ func createBranch(ctx context.Context, dbData env.DbData, newBranch, startingPoi
var emptyHash = hash.Hash{}

func IsBranch(ctx context.Context, ddb *doltdb.DoltDB, str string) (bool, error) {
return IsBranchOnDB(ctx, ddb, str)
}

func IsBranchOnDB(ctx context.Context, ddb *doltdb.DoltDB, str string) (bool, error) {
dref := ref.NewBranchRef(str)
return ddb.HasRef(ctx, dref)
}

func IsTag(ctx context.Context, ddb *doltdb.DoltDB, str string) (bool, error) {
tRef := ref.NewTagRef(str)
return ddb.HasRef(ctx, tRef)
}

func MaybeGetCommit(ctx context.Context, dEnv *env.DoltEnv, str string) (*doltdb.Commit, error) {
cs, err := doltdb.NewCommitSpec(str)

Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/env/actions/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func CreateWorkspace(ctx context.Context, dEnv *env.DoltEnv, name, startPoint st
}

func CreateWorkspaceOnDB(ctx context.Context, ddb *doltdb.DoltDB, name, startPoint string, headRef ref.DoltRef) error {
isBranch, err := IsBranchOnDB(ctx, ddb, name)
isBranch, err := IsBranch(ctx, ddb, name)
if err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions go/libraries/doltcore/sqle/dprocedures/dolt_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ func doDoltCheckout(ctx *sql.Context, args []string) (statusCode int, successMes
return 0, generateSuccessMessage(branchName, ""), nil
}

if isTag, err := actions.IsTag(ctx, dbData.Ddb, branchName); err != nil {
return 1, "", err
} else if isTag {
return 1, "", fmt.Errorf("error: tag '%s' is not a branch", branchName)
}

roots, ok := dSess.GetRoots(ctx, currentDbName)
if !ok {
return 1, "", fmt.Errorf("Could not load database %s", currentDbName)
Expand Down Expand Up @@ -290,11 +296,15 @@ func checkoutRemoteBranch(ctx *sql.Context, dSess *dsess.DoltSession, dbName str
}

if len(remoteRefs) == 0 {
if isTag, err := actions.IsTag(ctx, dbData.Ddb, branchName); err != nil {
return "", err
} else if isTag && apr.Contains(cli.MoveFlag) {
return "", fmt.Errorf(`dolt does not support a detached head state. To checkout a tag, run:
dolt checkout %s -b {new_branch_namee}`, branchName)
}
if doltdb.IsValidCommitHash(branchName) && apr.Contains(cli.MoveFlag) {

// User tried to enter a detached head state, which we don't support.
// Inform and suggest that they check-out a new branch at this commit instead.

return "", fmt.Errorf(`dolt does not support a detached head state. To create a branch at this commit instead, run:
dolt checkout %s -b {new_branch_name}
Expand Down

0 comments on commit 8ed8710

Please sign in to comment.