From: Junio C Hamano <gitster@pobox.com>
To: "Rubén Justo" <rjusto@gmail.com>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH] branch: error copying or renaming a detached HEAD
Date: Tue, 25 Oct 2022 16:50:41 -0700 [thread overview]
Message-ID: <xmqqmt9jmpim.fsf@gitster.g> (raw)
In-Reply-To: <0ac8cd48-08d7-9bdd-b074-c8d5ded522f6@gmail.com> ("Rubén Justo"'s message of "Wed, 26 Oct 2022 01:01:29 +0200")
Rubén Justo <rjusto@gmail.com> writes:
> 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.
Makes sense.
> @@ -827,24 +820,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
> strbuf_release(&buf);
>
> return ret;
> - } else if (copy) {
> - if (!argc)
> - die(_("branch name required"));
> - else if (argc == 1)
> - copy_or_rename_branch(head, argv[0], 1, copy > 1);
> - else if (argc == 2)
> - copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
> - else
> - die(_("too many branches for a copy operation"));
> - } else if (rename) {
> + } else if (copy || rename) {
> if (!argc)
> 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."));
Missing " " before "?".
> else if (argc == 1)
> - copy_or_rename_branch(head, argv[0], 0, rename > 1);
> + copy_or_rename_branch(head, argv[0], copy, copy + rename > 1);
The third argument being 'copy' makes sense (as the original has 1
for copy and 0 for rename). As we reject if rename and copy are
both set, here we have either copy > 0 or rename > 0 but not both,
so the fourth argument makes sense, too.
> else if (argc == 2)
> - copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
> + copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1);
> else
> - die(_("too many arguments for a rename operation"));
> + die(copy? _("too many branches for a copy operation")
> + : _("too many arguments for a rename operation"));
Ditto.
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index 7d8edff9c3..38c57de71b 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -268,6 +268,17 @@ test_expect_success 'git branch -M topic topic should work when main is checked
> 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
> +'
Excellent.
I'll do the whitespace tweak while queueing, so there is no need to
resend only to fix them.
Thanks.
prev parent reply other threads:[~2022-10-25 23:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-25 23:01 [PATCH] branch: error copying or renaming a detached HEAD Rubén Justo
2022-10-25 23:50 ` Junio C Hamano [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqmt9jmpim.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=rjusto@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).