mirror of
https://github.com/dolthub/dolt.git
synced 2026-03-13 11:09:10 -05:00
Add CLI support for checkout -B
This commit is contained in:
@@ -106,15 +106,20 @@ func (cmd CheckoutCmd) Exec(ctx context.Context, commandStr string, args []strin
|
||||
return 1
|
||||
}
|
||||
|
||||
branchOrTrack := apr.Contains(cli.CheckoutCreateBranch) || apr.Contains(cli.TrackFlag)
|
||||
// Argument validation in the CLI is strictly nice to have. The stored procedure will do the same, but the errors
|
||||
// won't be as nice.
|
||||
branchOrTrack := apr.Contains(cli.CheckoutCreateBranch) || apr.Contains(cli.CreateResetBranch) || apr.Contains(cli.TrackFlag)
|
||||
if (branchOrTrack && apr.NArg() > 1) || (!branchOrTrack && apr.NArg() == 0) {
|
||||
usagePrt()
|
||||
return 1
|
||||
}
|
||||
|
||||
// Branch name retrieval here is strictly for messages. dolt_checkout procedure is the authority on logic around validation.
|
||||
var branchName string
|
||||
if apr.Contains(cli.CheckoutCreateBranch) {
|
||||
branchName, _ = apr.GetValue(cli.CheckoutCreateBranch)
|
||||
} else if apr.Contains(cli.CreateResetBranch) {
|
||||
branchName, _ = apr.GetValue(cli.CreateResetBranch)
|
||||
} else if apr.Contains(cli.TrackFlag) {
|
||||
if apr.NArg() > 0 {
|
||||
usagePrt()
|
||||
|
||||
@@ -314,6 +314,46 @@ SQL
|
||||
[[ ! "$output" =~ "4" ]] || false
|
||||
}
|
||||
|
||||
@test "checkout: -B flag will forcefully reset an existing branch" {
|
||||
dolt sql -q 'create table test (id int primary key);'
|
||||
dolt sql -q 'insert into test (id) values (89012);'
|
||||
dolt commit -Am 'first change.'
|
||||
dolt sql -q 'insert into test (id) values (76543);'
|
||||
dolt commit -Am 'second change.'
|
||||
|
||||
dolt checkout -b testbr main~1
|
||||
run dolt sql -q "select * from test;"
|
||||
[[ "$output" =~ "89012" ]] || false
|
||||
[[ ! "$output" =~ "76543" ]] || false
|
||||
|
||||
# make a change to the branch which we'll lose
|
||||
dolt sql -q 'insert into test (id) values (19283);'
|
||||
dolt commit -Am 'change to testbr.'
|
||||
|
||||
dolt checkout main
|
||||
dolt checkout -B testbr main
|
||||
run dolt sql -q "select * from test;"
|
||||
[[ "$output" =~ "89012" ]] || false
|
||||
[[ "$output" =~ "76543" ]] || false
|
||||
[[ ! "$output" =~ "19283" ]] || false
|
||||
}
|
||||
|
||||
@test "checkout: -B will create a branch that does not exist" {
|
||||
dolt sql -q 'create table test (id int primary key);'
|
||||
dolt sql -q 'insert into test (id) values (89012);'
|
||||
dolt commit -Am 'first change.'
|
||||
dolt sql -q 'insert into test (id) values (76543);'
|
||||
dolt commit -Am 'second change.'
|
||||
|
||||
dolt checkout -B testbr main~1
|
||||
run dolt sql -q "select * from test;"
|
||||
[[ "$output" =~ "89012" ]] || false
|
||||
[[ ! "$output" =~ "76543" ]] || false
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@test "checkout: attempting to checkout a detached head shows a suggestion instead" {
|
||||
dolt sql -q "create table test (id int primary key);"
|
||||
dolt add .
|
||||
|
||||
Reference in New Issue
Block a user