Skip to content

Commit de82fb4

Browse files
committed
Merge branch 'rs/checkout-b-track-error'
The error message from "git checkout -b foo -t bar baz" was confusing. * rs/checkout-b-track-error: checkout: improve error messages for -b with extra argument checkout: add tests for -b and --track
2 parents 202a2b8 + bb2198f commit de82fb4

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16891689
* Try to give more helpful suggestion.
16901690
* new_branch && argc > 1 will be caught later.
16911691
*/
1692-
if (opts->new_branch && argc == 1)
1692+
if (opts->new_branch && argc == 1 && !new_branch_info.commit)
16931693
die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
16941694
argv[0], opts->new_branch);
16951695

t/t2018-checkout-branch.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,14 @@ test_expect_success 'checkout -b to a new branch preserves mergeable changes des
260260
test_cmp expect actual
261261
'
262262

263+
test_expect_success 'checkout -b rejects an invalid start point' '
264+
test_must_fail git checkout -b branch4 file1 2>err &&
265+
test_i18ngrep "is not a commit" err
266+
'
267+
268+
test_expect_success 'checkout -b rejects an extra path argument' '
269+
test_must_fail git checkout -b branch5 branch1 file1 2>err &&
270+
test_i18ngrep "Cannot update paths and switch to branch" err
271+
'
272+
263273
test_done

t/t2027-checkout-track.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
test_description='tests for git branch --track'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
test_commit one &&
9+
test_commit two
10+
'
11+
12+
test_expect_success 'checkout --track -b creates a new tracking branch' '
13+
git checkout --track -b branch1 master &&
14+
test $(git rev-parse --abbrev-ref HEAD) = branch1 &&
15+
test $(git config --get branch.branch1.remote) = . &&
16+
test $(git config --get branch.branch1.merge) = refs/heads/master
17+
'
18+
19+
test_expect_success 'checkout --track -b rejects an extra path argument' '
20+
test_must_fail git checkout --track -b branch2 master one.t 2>err &&
21+
test_i18ngrep "cannot be used with updating paths" err
22+
'
23+
24+
test_done

0 commit comments

Comments
 (0)