read_loose_refs(): treat NULL_SHA1 loose references as broken
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 3 Jun 2015 13:51:59 +0000 (15:51 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Jun 2015 17:35:41 +0000 (10:35 -0700)
NULL_SHA1 is used to indicate an "invalid object name" throughout our
code (and the code of other git implementations), so it is vastly more
likely that an on-disk reference was set to this value due to a
software bug than that NULL_SHA1 is the legitimate SHA-1 of an actual
object. Therefore, if a loose reference has the value NULL_SHA1,
consider it to be broken.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
t/t6301-for-each-ref-errors.sh
diff --git a/refs.c b/refs.c
index 3c311d44c152000898459561222ef863a56bcc48..07f8847e6d78423a4b70b9e66d6ddfe97845e421 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1297,6 +1297,16 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
                        if (!read_ok) {
                                hashclr(sha1);
                                flag |= REF_ISBROKEN;
+                       } else if (is_null_sha1(sha1)) {
+                               /*
+                                * It is so astronomically unlikely
+                                * that NULL_SHA1 is the SHA-1 of an
+                                * actual object that we consider its
+                                * appearance in a loose reference
+                                * file to be repo corruption
+                                * (probably due to a software bug).
+                                */
+                               flag |= REF_ISBROKEN;
                        }
 
                        if (check_refname_format(refname.buf,
index 72d2397e1219a704a599cd860cd60f09c75db568..cdb67a03b712cbd2cf187e91f7d69a3f717847bf 100755 (executable)
@@ -24,7 +24,7 @@ test_expect_success 'Broken refs are reported correctly' '
        test_cmp broken-err err
 '
 
-test_expect_failure 'NULL_SHA1 refs are reported correctly' '
+test_expect_success 'NULL_SHA1 refs are reported correctly' '
        r=refs/heads/zeros &&
        echo $ZEROS >.git/$r &&
        test_when_finished "rm -f .git/$r" &&