From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Todd Zullinger" <tmz@pobox.com>,
"Petr Šplíchal" <psplicha@redhat.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH] checkout: fix BUG() case in 9081a421a6
Date: Thu, 20 Jan 2022 22:26:57 +0100 [thread overview]
Message-ID: <patch-1.1-21ddf7c628d-20220120T212233Z-avarab@gmail.com> (raw)
In-Reply-To: <YemTGQZ97vAPUPY0@pobox.com>
Fix a regression in my 9081a421a6d (checkout: fix "branch info" memory
leaks, 2021-11-16) where I'd assumed that the old_branch_info.path
would have to start with refs/heads/*, but as has been reported[1]
that's not the case.
As a test case[2] to reproduce this shows the second "git checkout"
here runs into the BUG() in the pre-image. The test being added is
amended from[2] and will pass both with this change, and before
9081a421a6. I.e. our behavior now is again the same as before that
commit.
1. https://bugzilla.redhat.com/show_bug.cgi?id=2042920
2. https://lore.kernel.org/git/YemTGQZ97vAPUPY0@pobox.com/
Reported-by: Petr Šplíchal <psplicha@redhat.com>
Reported-by: Todd Zullinger <tmz@pobox.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
On Thu, Jan 20 2022, Todd Zullinger wrote:
> Hi,
>
> A bug was filed in the Fedora/Red Hat bugzilla today for the
> git 2.35.0 rc (rc1, but it's the same in rc2). Petr (Cc'd),
> ran the following
>
> git clone https://github.com/psss/fmf /tmp/fmf
> cd /tmp/fmf
> cp .git/refs/remotes/origin/HEAD .git/refs/heads/__DEFAULT__
> git checkout -f __DEFAULT__
> git checkout -f __DEFAULT__
>
> The second git checkout call runs into the BUG() call added
> in 9081a421a6 (checkout: fix "branch info" memory leaks,
> 2021-11-16):
>
> BUG: builtin/checkout.c:1098: should be able to skip
> past 'refs/heads/' in 'refs/remotes/origin/master'!
> Aborted (core dumped)
>
> This worked in 2.34.1, so it's new to 2.35.0. Should this
> work or does the manual copy to setup a branch fall into a
> new category of "don't do that"?
>
> (It's novel to get a bug report from rc testing of a distro
> build -- that doesn't happen often.)
Thanks to both you and Petr for the report and easy to reproduce case,
and sorry about causing it.
In retrospec it's a rather obvious thinko. Here's a minimal fix for
it, along with a derived test case that I made more exhaustive to
check the state of the repo before, after, and in-between the two "git
checkout" commands. As noted it'll also pass with 9081a421a6d
reverted, showing that our behavior is the same as before that commit.
builtin/checkout.c | 11 ++++-------
t/t2018-checkout-branch.sh | 33 +++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 6a5dd2a2a22..52a47ef40e1 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1090,13 +1090,10 @@ static int switch_branches(const struct checkout_opts *opts,
FREE_AND_NULL(old_branch_info.path);
if (old_branch_info.path) {
- const char *const prefix = "refs/heads/";
- const char *p;
- if (skip_prefix(old_branch_info.path, prefix, &p))
- old_branch_info.name = xstrdup(p);
- else
- BUG("should be able to skip past '%s' in '%s'!",
- prefix, old_branch_info.path);
+ const char *p = old_branch_info.path;
+
+ skip_prefix(old_branch_info.path, "refs/heads/", &p);
+ old_branch_info.name = xstrdup(p);
}
if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index 3e93506c045..82df9b8bf64 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -85,6 +85,39 @@ test_expect_success 'setup' '
git branch -m branch1
'
+test_expect_success REFFILES 'checkout a branch without refs/heads/* prefix' '
+ git clone --no-tags . repo-odd-prefix &&
+ (
+ cd repo-odd-prefix &&
+
+ cp .git/refs/remotes/origin/HEAD .git/refs/heads/a-branch &&
+
+ echo branch1 >expect.ref &&
+ git rev-parse --abbrev-ref HEAD >actual.ref &&
+ test_cmp expect.ref actual.ref &&
+
+ git checkout -f a-branch &&
+
+ echo origin/branch1 >expect.ref &&
+ git rev-parse --abbrev-ref HEAD >actual.ref &&
+ test_cmp expect.ref actual.ref &&
+
+ git checkout -f a-branch &&
+
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) commit refs/heads/a-branch
+ $(git rev-parse HEAD) commit refs/heads/branch1
+ $(git rev-parse HEAD) commit refs/remotes/origin/HEAD
+ $(git rev-parse HEAD) commit refs/remotes/origin/branch1
+ EOF
+ git for-each-ref >actual &&
+ test_cmp expect actual &&
+
+ git rev-parse --abbrev-ref HEAD >actual &&
+ test_cmp expect.ref actual.ref
+ )
+'
+
test_expect_success 'checkout -b to a new branch, set to HEAD' '
test_when_finished "
git checkout branch1 &&
--
2.35.0.rc1.864.g57621b115b6
next prev parent reply other threads:[~2022-01-20 21:27 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-20 16:51 [BUG] builtin/checkout.c:1098: should be able to skip past 'refs/heads/' Todd Zullinger
2022-01-20 17:04 ` Todd Zullinger
2022-01-20 21:26 ` Ævar Arnfjörð Bjarmason [this message]
2022-01-20 22:29 ` [PATCH] checkout: fix BUG() case in 9081a421a6 Junio C Hamano
2022-01-20 22:33 ` Junio C Hamano
2022-01-21 11:14 ` Ævar Arnfjörð Bjarmason
2022-01-21 14:29 ` Petr Šplíchal
2022-01-21 21:58 ` Todd Zullinger
2022-01-21 21:19 ` Junio C Hamano
2022-01-20 22:33 ` Todd Zullinger
2022-01-22 0:33 ` Junio C Hamano
2022-01-22 0:45 ` Junio C Hamano
2022-01-22 0:58 ` [PATCH] checkout: avoid BUG() when hitting a broken repository Junio C Hamano
2022-01-22 8:10 ` Johannes Sixt
2022-01-22 11:55 ` Ævar Arnfjörð Bjarmason
2022-01-23 16:38 ` Johannes Schindelin
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=patch-1.1-21ddf7c628d-20220120T212233Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=psplicha@redhat.com \
--cc=tmz@pobox.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.
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 NNTP newsgroup(s).