branch: error copying or renaming a detached HEAD
In c847f53712 (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 <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Rubén Justo committed
Oct 26, 2022 at 01:01 UTC
77e7267e47aac247244dc4fb3538baff7463261f
2 files changed
+19
-20
builtin/branch.c
+8
-20
@@ -520,13 +520,6 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
520
const char *interpreted_newname = NULL;
521
int recovery = 0;
522
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
-
523
if (strbuf_check_branch_ref(&oldref, oldname)) {
524
/*
525
* 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)
820
strbuf_release(&buf);
821
822
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) {
824
if (!argc)
825
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."));
829
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);
831
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);
833
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"));
836
} else if (new_upstream) {
837
struct branch *branch;
838
struct strbuf buf = STRBUF_INIT;
t/t3200-branch.sh
+11
@@ -268,6 +268,17 @@ test_expect_success 'git branch -M topic topic should work when main is checked
268
git branch -M topic topic
269
'
270
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
+
282
test_expect_success 'git branch -v -d t should work' '
283
git branch t &&
284
git rev-parse --verify refs/heads/t &&