Karthik Nayak <karthik....@gmail.com> writes:

> Thanks, will add it in.

OK, here is a reroll of what I sent earlier in

    
http://public-inbox.org/git/<xmqq7f84tqa7.fsf...@gitster.mtv.corp.google.com>

but rebased so that it can happen as a preparatory bugfix before
your series.  

The bug dates back to the very original implementation of %(HEAD) in
7a48b83219 ("for-each-ref: introduce %(HEAD) asterisk marker",
2013-11-18) and was moved to the current location in the v2.6 days
at c95b758587 ("ref-filter: move code from 'for-each-ref'",
2015-06-14).

-- >8 --
Subject: [PATCH] for-each-ref: do not segv with %(HEAD) on an unborn branch

The code to flip between "*" and " " prefixes depending on what
branch is checked out used in --format='%(HEAD)' did not consider
that HEAD may resolve to an unborn branch and dereferenced a NULL.

This will become a lot easier to trigger as the codepath will be
used to reimplement "git branch [--list]" in the future.

Signed-off-by: Junio C Hamano <gits...@pobox.com>
---
 ref-filter.c            |  2 +-
 t/t6300-for-each-ref.sh | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/ref-filter.c b/ref-filter.c
index bc551a752c..d7e91a78da 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1017,7 +1017,7 @@ static void populate_value(struct ref_array_item *ref)
 
                        head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
                                                  sha1, NULL);
-                       if (!strcmp(ref->refname, head))
+                       if (head && !strcmp(ref->refname, head))
                                v->s = "*";
                        else
                                v->s = " ";
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 19a2823025..039509a9cb 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -553,4 +553,14 @@ test_expect_success 'Verify sort with multiple keys' '
                refs/tags/bogo refs/tags/master > actual &&
        test_cmp expected actual
 '
+
+test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
+       test_when_finished "git checkout master" &&
+       git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ 
>actual &&
+       sed -e "s/^\* /  /" actual >expect &&
+       git checkout --orphan HEAD &&
+       git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ 
>actual &&
+       test_cmp expect actual
+'
+
 test_done
-- 
2.11.0-rc2-152-gc9ad1dc38a

Reply via email to