Merge branch 'mm/checkout-auto-track-fix'
"git checkout topic", when there is not yet a local "topic" branch but there is a unique remote-tracking branch for a remote "topic" branch, pretended as if "git checkout -t -b topic remote/$r/topic" (for that unique remote $r) was run. This hack however was not implemented for "git checkout topic --". * mm/checkout-auto-track-fix: checkout: proper error message on 'git checkout foo bar --' checkout: allow dwim for branch creation for "git checkout $branch --"
This commit is contained in:
commit
f101b888f2
@ -873,7 +873,9 @@ static int parse_branchname_arg(int argc, const char **argv,
|
|||||||
int argcount = 0;
|
int argcount = 0;
|
||||||
unsigned char branch_rev[20];
|
unsigned char branch_rev[20];
|
||||||
const char *arg;
|
const char *arg;
|
||||||
int has_dash_dash;
|
int dash_dash_pos;
|
||||||
|
int has_dash_dash = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* case 1: git checkout <ref> -- [<paths>]
|
* case 1: git checkout <ref> -- [<paths>]
|
||||||
@ -885,20 +887,30 @@ static int parse_branchname_arg(int argc, const char **argv,
|
|||||||
*
|
*
|
||||||
* everything after the '--' must be paths.
|
* everything after the '--' must be paths.
|
||||||
*
|
*
|
||||||
* case 3: git checkout <something> [<paths>]
|
* case 3: git checkout <something> [--]
|
||||||
*
|
*
|
||||||
* With no paths, if <something> is a commit, that is to
|
* (a) If <something> is a commit, that is to
|
||||||
* switch to the branch or detach HEAD at it. As a special case,
|
* switch to the branch or detach HEAD at it. As a special case,
|
||||||
* if <something> is A...B (missing A or B means HEAD but you can
|
* if <something> is A...B (missing A or B means HEAD but you can
|
||||||
* omit at most one side), and if there is a unique merge base
|
* omit at most one side), and if there is a unique merge base
|
||||||
* between A and B, A...B names that merge base.
|
* between A and B, A...B names that merge base.
|
||||||
*
|
*
|
||||||
* With no paths, if <something> is _not_ a commit, no -t nor -b
|
* (b) If <something> is _not_ a commit, either "--" is present
|
||||||
* was given, and there is a tracking branch whose name is
|
* or <something> is not a path, no -t nor -b was given, and
|
||||||
* <something> in one and only one remote, then this is a short-hand
|
* and there is a tracking branch whose name is <something>
|
||||||
* to fork local <something> from that remote-tracking branch.
|
* in one and only one remote, then this is a short-hand to
|
||||||
|
* fork local <something> from that remote-tracking branch.
|
||||||
*
|
*
|
||||||
* Otherwise <something> shall not be ambiguous.
|
* (c) Otherwise, if "--" is present, treat it like case (1).
|
||||||
|
*
|
||||||
|
* (d) Otherwise :
|
||||||
|
* - if it's a reference, treat it like case (1)
|
||||||
|
* - else if it's a path, treat it like case (2)
|
||||||
|
* - else: fail.
|
||||||
|
*
|
||||||
|
* case 4: git checkout <something> <paths>
|
||||||
|
*
|
||||||
|
* The first argument must not be ambiguous.
|
||||||
* - If it's *only* a reference, treat it like case (1).
|
* - If it's *only* a reference, treat it like case (1).
|
||||||
* - If it's only a path, treat it like case (2).
|
* - If it's only a path, treat it like case (2).
|
||||||
* - else: fail.
|
* - else: fail.
|
||||||
@ -907,28 +919,59 @@ static int parse_branchname_arg(int argc, const char **argv,
|
|||||||
if (!argc)
|
if (!argc)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!strcmp(argv[0], "--")) /* case (2) */
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
arg = argv[0];
|
arg = argv[0];
|
||||||
has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
|
dash_dash_pos = -1;
|
||||||
|
for (i = 0; i < argc; i++) {
|
||||||
|
if (!strcmp(argv[i], "--")) {
|
||||||
|
dash_dash_pos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dash_dash_pos == 0)
|
||||||
|
return 1; /* case (2) */
|
||||||
|
else if (dash_dash_pos == 1)
|
||||||
|
has_dash_dash = 1; /* case (3) or (1) */
|
||||||
|
else if (dash_dash_pos >= 2)
|
||||||
|
die(_("only one reference expected, %d given."), dash_dash_pos);
|
||||||
|
|
||||||
if (!strcmp(arg, "-"))
|
if (!strcmp(arg, "-"))
|
||||||
arg = "@{-1}";
|
arg = "@{-1}";
|
||||||
|
|
||||||
if (get_sha1_mb(arg, rev)) {
|
if (get_sha1_mb(arg, rev)) {
|
||||||
if (has_dash_dash) /* case (1) */
|
/*
|
||||||
die(_("invalid reference: %s"), arg);
|
* Either case (3) or (4), with <something> not being
|
||||||
if (dwim_new_local_branch_ok &&
|
* a commit, or an attempt to use case (1) with an
|
||||||
!check_filename(NULL, arg) &&
|
* invalid ref.
|
||||||
argc == 1) {
|
*
|
||||||
|
* It's likely an error, but we need to find out if
|
||||||
|
* we should auto-create the branch, case (3).(b).
|
||||||
|
*/
|
||||||
|
int recover_with_dwim = dwim_new_local_branch_ok;
|
||||||
|
|
||||||
|
if (check_filename(NULL, arg) && !has_dash_dash)
|
||||||
|
recover_with_dwim = 0;
|
||||||
|
/*
|
||||||
|
* Accept "git checkout foo" and "git checkout foo --"
|
||||||
|
* as candidates for dwim.
|
||||||
|
*/
|
||||||
|
if (!(argc == 1 && !has_dash_dash) &&
|
||||||
|
!(argc == 2 && has_dash_dash))
|
||||||
|
recover_with_dwim = 0;
|
||||||
|
|
||||||
|
if (recover_with_dwim) {
|
||||||
const char *remote = unique_tracking_name(arg, rev);
|
const char *remote = unique_tracking_name(arg, rev);
|
||||||
if (!remote)
|
if (remote) {
|
||||||
return argcount;
|
*new_branch = arg;
|
||||||
*new_branch = arg;
|
arg = remote;
|
||||||
arg = remote;
|
/* DWIMmed to create local branch, case (3).(b) */
|
||||||
/* DWIMmed to create local branch */
|
} else {
|
||||||
} else {
|
recover_with_dwim = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!recover_with_dwim) {
|
||||||
|
if (has_dash_dash)
|
||||||
|
die(_("invalid reference: %s"), arg);
|
||||||
return argcount;
|
return argcount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -958,7 +1001,7 @@ static int parse_branchname_arg(int argc, const char **argv,
|
|||||||
|
|
||||||
if (!*source_tree) /* case (1): want a tree */
|
if (!*source_tree) /* case (1): want a tree */
|
||||||
die(_("reference is not a tree: %s"), arg);
|
die(_("reference is not a tree: %s"), arg);
|
||||||
if (!has_dash_dash) {/* case (3 -> 1) */
|
if (!has_dash_dash) {/* case (3).(d) -> (1) */
|
||||||
/*
|
/*
|
||||||
* Do not complain the most common case
|
* Do not complain the most common case
|
||||||
* git checkout branch
|
* git checkout branch
|
||||||
|
@ -47,4 +47,10 @@ test_expect_success 'disambiguate checking out from a tree-ish' '
|
|||||||
git diff --exit-code --quiet
|
git diff --exit-code --quiet
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'accurate error message with more than one ref' '
|
||||||
|
test_must_fail git checkout HEAD master -- 2>actual &&
|
||||||
|
grep 2 actual &&
|
||||||
|
test_i18ngrep "one reference expected, 2 given" actual
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
@ -164,4 +164,25 @@ test_expect_success 'checkout of branch from a single remote succeeds #4' '
|
|||||||
test_branch_upstream eggs repo_d eggs
|
test_branch_upstream eggs repo_d eggs
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'checkout of branch with a file having the same name fails' '
|
||||||
|
git checkout -B master &&
|
||||||
|
test_might_fail git branch -D spam &&
|
||||||
|
|
||||||
|
>spam &&
|
||||||
|
test_must_fail git checkout spam &&
|
||||||
|
test_must_fail git rev-parse --verify refs/heads/spam &&
|
||||||
|
test_branch master
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
|
||||||
|
git checkout -B master &&
|
||||||
|
test_might_fail git branch -D spam &&
|
||||||
|
|
||||||
|
>spam &&
|
||||||
|
git checkout spam -- &&
|
||||||
|
test_branch spam &&
|
||||||
|
test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
|
||||||
|
test_branch_upstream spam repo_c spam
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
Loading…
Reference in New Issue
Block a user