Skip to content

Commit 501cf47

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f551707 commit 501cf47

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

refs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,16 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
12971297
if (!read_ok) {
12981298
hashclr(sha1);
12991299
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;
13001310
}
13011311

13021312
if (check_refname_format(refname.buf,

t/t6301-for-each-ref-errors.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test_expect_success 'Broken refs are reported correctly' '
2424
test_cmp broken-err err
2525
'
2626

27-
test_expect_failure 'NULL_SHA1 refs are reported correctly' '
27+
test_expect_success 'NULL_SHA1 refs are reported correctly' '
2828
r=refs/heads/zeros &&
2929
echo $ZEROS >.git/$r &&
3030
test_when_finished "rm -f .git/$r" &&

0 commit comments

Comments
 (0)