From: Junio C Hamano Date: Wed, 23 Nov 2016 19:23:16 +0000 (-0800) Subject: Merge branch 'jc/for-each-ref-head-segfault-fix' X-Git-Tag: v2.11.0-rc3~4 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/48e9ad5ef3afd39168e2fb72d0ce7ac5f7124d31?hp=1e37181391e305a7ab0c382ca3c3b2de998d4138 Merge branch 'jc/for-each-ref-head-segfault-fix' 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 --- diff --git a/ref-filter.c b/ref-filter.c index d4c2931f3a..f5f7a70c6d 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