Merge branch 'jc/for-each-ref-head-segfault-fix'
authorJunio C Hamano <gitster@pobox.com>
Wed, 23 Nov 2016 19:23:16 +0000 (11:23 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 23 Nov 2016 19:23:16 +0000 (11:23 -0800)
Using a %(HEAD) placeholder in "for-each-ref --format=" option
caused the command to segfault when on an unborn branch.

* jc/for-each-ref-head-segfault-fix:
for-each-ref: do not segv with %(HEAD) on an unborn branch

ref-filter.c
t/t6300-for-each-ref.sh
index d4c2931f3aab14accaa5720c87b2fc58aed83faa..f5f7a70c6d9b8ef36ecfe282167714640cace6b1 100644 (file)
@@ -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 = " ";
index 19a2823025e794a6adc3f75d65dd175a2021f282..039509a9cb94ef5c653df09e0453ac83157bf184 100755 (executable)
@@ -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