mirror of
https://github.com/git/git
synced 2024-08-24 18:26:02 +00:00
Merge branch 'rj/branch-copy-rename-error-codepath-cleanup'
Code simplification. * rj/branch-copy-rename-error-codepath-cleanup: branch: error copying or renaming a detached HEAD
This commit is contained in:
commit
0c025612d4
|
@ -520,13 +520,6 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
|
||||||
const char *interpreted_newname = NULL;
|
const char *interpreted_newname = NULL;
|
||||||
int recovery = 0;
|
int recovery = 0;
|
||||||
|
|
||||||
if (!oldname) {
|
|
||||||
if (copy)
|
|
||||||
die(_("cannot copy the current branch while not on any."));
|
|
||||||
else
|
|
||||||
die(_("cannot rename the current branch while not on any."));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strbuf_check_branch_ref(&oldref, oldname)) {
|
if (strbuf_check_branch_ref(&oldref, oldname)) {
|
||||||
/*
|
/*
|
||||||
* Bad name --- this could be an attempt to rename a
|
* Bad name --- this could be an attempt to rename a
|
||||||
|
@ -827,24 +820,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
|
||||||
strbuf_release(&buf);
|
strbuf_release(&buf);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
} else if (copy) {
|
} else if (copy || rename) {
|
||||||
if (!argc)
|
if (!argc)
|
||||||
die(_("branch name required"));
|
die(_("branch name required"));
|
||||||
|
else if ((argc == 1) && filter.detached)
|
||||||
|
die(copy? _("cannot copy the current branch while not on any.")
|
||||||
|
: _("cannot rename the current branch while not on any."));
|
||||||
else if (argc == 1)
|
else if (argc == 1)
|
||||||
copy_or_rename_branch(head, argv[0], 1, copy > 1);
|
copy_or_rename_branch(head, argv[0], copy, copy + rename > 1);
|
||||||
else if (argc == 2)
|
else if (argc == 2)
|
||||||
copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
|
copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1);
|
||||||
else
|
else
|
||||||
die(_("too many branches for a copy operation"));
|
die(copy? _("too many branches for a copy operation")
|
||||||
} else if (rename) {
|
: _("too many arguments for a rename operation"));
|
||||||
if (!argc)
|
|
||||||
die(_("branch name required"));
|
|
||||||
else if (argc == 1)
|
|
||||||
copy_or_rename_branch(head, argv[0], 0, rename > 1);
|
|
||||||
else if (argc == 2)
|
|
||||||
copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
|
|
||||||
else
|
|
||||||
die(_("too many arguments for a rename operation"));
|
|
||||||
} else if (new_upstream) {
|
} else if (new_upstream) {
|
||||||
struct branch *branch;
|
struct branch *branch;
|
||||||
struct strbuf buf = STRBUF_INIT;
|
struct strbuf buf = STRBUF_INIT;
|
||||||
|
|
|
@ -268,6 +268,17 @@ test_expect_success 'git branch -M topic topic should work when main is checked
|
||||||
git branch -M topic topic
|
git branch -M topic topic
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git branch -M and -C fail on detached HEAD' '
|
||||||
|
git checkout HEAD^{} &&
|
||||||
|
test_when_finished git checkout - &&
|
||||||
|
echo "fatal: cannot rename the current branch while not on any." >expect &&
|
||||||
|
test_must_fail git branch -M must-fail 2>err &&
|
||||||
|
test_cmp expect err &&
|
||||||
|
echo "fatal: cannot copy the current branch while not on any." >expect &&
|
||||||
|
test_must_fail git branch -C must-fail 2>err &&
|
||||||
|
test_cmp expect err
|
||||||
|
'
|
||||||
|
|
||||||
test_expect_success 'git branch -v -d t should work' '
|
test_expect_success 'git branch -v -d t should work' '
|
||||||
git branch t &&
|
git branch t &&
|
||||||
git rev-parse --verify refs/heads/t &&
|
git rev-parse --verify refs/heads/t &&
|
||||||
|
|
Loading…
Reference in a new issue