Repository: dolthub/dolt
File: go/libraries/doltcore/sqle/dprocedures/dolt_checkout.go (Go)

Code before:
		roots, err = actions.MoveTablesFromHeadToWorking(ctx, roots, tableNames)
		if err != nil {
			if doltdb.IsRootValUnreachable(err) {
				rt := doltdb.GetUnreachableRootType(err)
				return fmt.Errorf("error: unable to read the %s", rt.String())
			} else if actions.IsTblNotExist(err) {
				return fmt.Errorf("error: given tables do not exist")
			} else {
				return fmt.Errorf("fatal: Unexpected error checking out tables")
			}
		}

		dSess := dsess.DSessFromSess(ctx.Session)
		if err := dSess.SetRoots(ctx, name, roots); err != nil {
			return err
		}
		if err := commitTransaction(ctx, dSess, nil); err != nil {
			return err
		}
	}

	if len(warningMsg) > 0 {
		return errors.New(strings.Join(warningMsg, "\n"))
	}
	return nil
}

// validateNoDetachedHead checks if the given branchName refers to a tag or commit hash
// which would result in a detached HEAD state, which Dolt doesn't support.
// Returns an error with appropriate message if it's a detached HEAD state, nil otherwise.
func validateNoDetachedHead(ctx *sql.Context, ddb *doltdb.DoltDB, branchName string, isMoveFlag bool) error {
	if isTag, err := actions.IsTag(ctx, ddb, branchName); err != nil {
		return err
	} else if isTag {
		// 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 tag instead.
		if isMoveFlag {
			return fmt.Errorf(`dolt does not support a detached head state. To create a branch at this tag, run: 
	dolt checkout %s -b {new_branch_name}`, branchName)
		} else {
			return fmt.Errorf(`dolt does not support a detached head state. To create a branch at this tag, run: 
	CALL DOLT_CHECKOUT('%s', '-b', <new_branch_name>)`, branchName)
		}
	}

	if doltdb.IsValidCommitHash(branchName) {
		// 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.
		if isMoveFlag {
			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}`, branchName)

Review comment (suggestion):
Returning nil, no error, is technically correct? Git's behavior can be a bit weird depending on whether or not you modified an already tracked item here. I think it should be fine but CI will have the final say.

Code after:
	if len(tableNames) > 0 {
		roots, err = actions.MoveTablesFromHeadToWorking(ctx, roots, tableNames)
		if err != nil {
			if doltdb.IsRootValUnreachable(err) {
				rt := doltdb.GetUnreachableRootType(err)
				return fmt.Errorf("error: unable to read the %s", rt.String())
			} else if actions.IsTblNotExist(err) {
				return fmt.Errorf("error: given tables do not exist")
			} else {
				return fmt.Errorf("fatal: Unexpected error checking out tables")
			}
		}

		dSess := dsess.DSessFromSess(ctx.Session)
		if err := dSess.SetRoots(ctx, name, roots); err != nil {
			return err
		}
		if err := commitTransaction(ctx, dSess, nil); err != nil {
			return err
		}
	}

	if warningMsg.Len() > 0 {
		return errors.New(strings.TrimSuffix(warningMsg.String(), "\n"))
	}
	return nil
}

// validateNoDetachedHead checks if the given branchName refers to a tag or commit hash
// which would result in a detached HEAD state, which Dolt doesn't support.
// Returns an error with appropriate message if it's a detached HEAD state, nil otherwise.
func validateNoDetachedHead(ctx *sql.Context, ddb *doltdb.DoltDB, branchName string, isMoveFlag bool) error {
	if isTag, err := actions.IsTag(ctx, ddb, branchName); err != nil {
		return err
	} else if isTag {
		// 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 tag instead.
		if isMoveFlag {
			return fmt.Errorf(`dolt does not support a detached head state. To create a branch at this tag, run: 
	dolt checkout %s -b {new_branch_name}`, branchName)
		} else {
			return fmt.Errorf(`dolt does not support a detached head state. To create a branch at this tag, run: 
	CALL DOLT_CHECKOUT('%s', '-b', <new_branch_name>)`, branchName)
		}
	}

	if doltdb.IsValidCommitHash(branchName) {
		// 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.
		if isMoveFlag {
			return fmt.Errorf(`dolt does not support a detached head state. To create a branch at this commit instead, run: