Skip to content

Commit 77e7267

Browse files
rjustogitster
authored andcommitted
branch: error copying or renaming a detached HEAD
In c847f53 (Detached HEAD (experimental), 2007-01-01) an error condition was introduced in rename_branch() to prevent renaming, later also copying, a detached HEAD. The condition used was checking for NULL in oldname, the source branch to rename/copy. That condition cannot be satisfied because if no source branch is specified, HEAD is going to be used in the call. The error issued instead is: fatal: Invalid branch name: 'HEAD' Let's remove the condition in copy_or_rename_branch() (the current function name) and check for HEAD before calling it, dying with the original intended error if we're in a detached HEAD. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent db29e6b commit 77e7267

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

builtin/branch.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,6 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
520520
const char *interpreted_newname = NULL;
521521
int recovery = 0;
522522

523-
if (!oldname) {
524-
if (copy)
525-
die(_("cannot copy the current branch while not on any."));
526-
else
527-
die(_("cannot rename the current branch while not on any."));
528-
}
529-
530523
if (strbuf_check_branch_ref(&oldref, oldname)) {
531524
/*
532525
* 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)
827820
strbuf_release(&buf);
828821

829822
return ret;
830-
} else if (copy) {
831-
if (!argc)
832-
die(_("branch name required"));
833-
else if (argc == 1)
834-
copy_or_rename_branch(head, argv[0], 1, copy > 1);
835-
else if (argc == 2)
836-
copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
837-
else
838-
die(_("too many branches for a copy operation"));
839-
} else if (rename) {
823+
} else if (copy || rename) {
840824
if (!argc)
841825
die(_("branch name required"));
826+
else if ((argc == 1) && filter.detached)
827+
die(copy? _("cannot copy the current branch while not on any.")
828+
: _("cannot rename the current branch while not on any."));
842829
else if (argc == 1)
843-
copy_or_rename_branch(head, argv[0], 0, rename > 1);
830+
copy_or_rename_branch(head, argv[0], copy, copy + rename > 1);
844831
else if (argc == 2)
845-
copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
832+
copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1);
846833
else
847-
die(_("too many arguments for a rename operation"));
834+
die(copy? _("too many branches for a copy operation")
835+
: _("too many arguments for a rename operation"));
848836
} else if (new_upstream) {
849837
struct branch *branch;
850838
struct strbuf buf = STRBUF_INIT;

t/t3200-branch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ test_expect_success 'git branch -M topic topic should work when main is checked
268268
git branch -M topic topic
269269
'
270270

271+
test_expect_success 'git branch -M and -C fail on detached HEAD' '
272+
git checkout HEAD^{} &&
273+
test_when_finished git checkout - &&
274+
echo "fatal: cannot rename the current branch while not on any." >expect &&
275+
test_must_fail git branch -M must-fail 2>err &&
276+
test_cmp expect err &&
277+
echo "fatal: cannot copy the current branch while not on any." >expect &&
278+
test_must_fail git branch -C must-fail 2>err &&
279+
test_cmp expect err
280+
'
281+
271282
test_expect_success 'git branch -v -d t should work' '
272283
git branch t &&
273284
git rev-parse --verify refs/heads/t &&

0 commit comments

Comments
 (0)