read_loose_refs(): treat NULL_SHA1 loose references as broken
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>
Michael Haggerty committed
Jun 3, 2015 at 15:51 UTC
501cf47cddfbf8040b6f9b8ac06d13094a70f729
2 files changed
+11
-1
refs.c
+10
@@ -1297,6 +1297,16 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1297
if (!read_ok) {
1298
hashclr(sha1);
1299
flag |= REF_ISBROKEN;
1300
+ } else if (is_null_sha1(sha1)) {
1301
+ /*
1302
+ * It is so astronomically unlikely
1303
+ * that NULL_SHA1 is the SHA-1 of an
1304
+ * actual object that we consider its
1305
+ * appearance in a loose reference
1306
+ * file to be repo corruption
1307
+ * (probably due to a software bug).
1308
+ */
1309
+ flag |= REF_ISBROKEN;
1310
}
1311
1312
if (check_refname_format(refname.buf,
t/t6301-for-each-ref-errors.sh
+1
-1
@@ -24,7 +24,7 @@ test_expect_success 'Broken refs are reported correctly' '
24
test_cmp broken-err err
25
'
26
27
-test_expect_failure 'NULL_SHA1 refs are reported correctly' '
27
+test_expect_success 'NULL_SHA1 refs are reported correctly' '
28
r=refs/heads/zeros &&
29
echo $ZEROS >.git/$r &&
30
test_when_finished "rm -f .git/$r" &&