Skip to content

Commit 0533a9b

Browse files
committed
Merge branch 'mh/reporting-broken-refs-from-for-each-ref' into maint
"git for-each-ref" reported "missing object" for 0{40} when it encounters a broken ref. The lack of object whose name is 0{40} is not the problem; the ref being broken is. * mh/reporting-broken-refs-from-for-each-ref: read_loose_refs(): treat NULL_SHA1 loose references as broken read_loose_refs(): simplify function logic for-each-ref: report broken references correctly t6301: new tests of for-each-ref error handling
2 parents a94594d + 501cf47 commit 0533a9b

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

builtin/for-each-ref.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,11 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
851851
return 0;
852852
}
853853

854+
if (flag & REF_ISBROKEN) {
855+
warning("ignoring broken ref %s", refname);
856+
return 0;
857+
}
858+
854859
if (*cb->grab_pattern) {
855860
const char **pattern;
856861
int namelen = strlen(refname);

refs.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,19 +1374,34 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
13741374
create_dir_entry(refs, refname.buf,
13751375
refname.len, 1));
13761376
} else {
1377+
int read_ok;
1378+
13771379
if (*refs->name) {
13781380
hashclr(sha1);
13791381
flag = 0;
1380-
if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
1381-
hashclr(sha1);
1382-
flag |= REF_ISBROKEN;
1383-
}
1384-
} else if (read_ref_full(refname.buf,
1385-
RESOLVE_REF_READING,
1386-
sha1, &flag)) {
1382+
read_ok = !resolve_gitlink_ref(refs->name,
1383+
refname.buf, sha1);
1384+
} else {
1385+
read_ok = !read_ref_full(refname.buf,
1386+
RESOLVE_REF_READING,
1387+
sha1, &flag);
1388+
}
1389+
1390+
if (!read_ok) {
13871391
hashclr(sha1);
13881392
flag |= REF_ISBROKEN;
1393+
} else if (is_null_sha1(sha1)) {
1394+
/*
1395+
* It is so astronomically unlikely
1396+
* that NULL_SHA1 is the SHA-1 of an
1397+
* actual object that we consider its
1398+
* appearance in a loose reference
1399+
* file to be repo corruption
1400+
* (probably due to a software bug).
1401+
*/
1402+
flag |= REF_ISBROKEN;
13891403
}
1404+
13901405
if (check_refname_format(refname.buf,
13911406
REFNAME_ALLOW_ONELEVEL)) {
13921407
hashclr(sha1);

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
3+
test_description='for-each-ref errors for broken refs'
4+
5+
. ./test-lib.sh
6+
7+
ZEROS=$_z40
8+
MISSING=abababababababababababababababababababab
9+
10+
test_expect_success setup '
11+
git commit --allow-empty -m "Initial" &&
12+
git tag testtag &&
13+
git for-each-ref >full-list &&
14+
git for-each-ref --format="%(objectname) %(refname)" >brief-list
15+
'
16+
17+
test_expect_success 'Broken refs are reported correctly' '
18+
r=refs/heads/bogus &&
19+
: >.git/$r &&
20+
test_when_finished "rm -f .git/$r" &&
21+
echo "warning: ignoring broken ref $r" >broken-err &&
22+
git for-each-ref >out 2>err &&
23+
test_cmp full-list out &&
24+
test_cmp broken-err err
25+
'
26+
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" &&
31+
echo "warning: ignoring broken ref $r" >zeros-err &&
32+
git for-each-ref >out 2>err &&
33+
test_cmp full-list out &&
34+
test_cmp zeros-err err &&
35+
git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
36+
test_cmp brief-list brief-out &&
37+
test_cmp zeros-err brief-err
38+
'
39+
40+
test_expect_success 'Missing objects are reported correctly' '
41+
r=refs/heads/missing &&
42+
echo $MISSING >.git/$r &&
43+
test_when_finished "rm -f .git/$r" &&
44+
echo "fatal: missing object $MISSING for $r" >missing-err &&
45+
test_must_fail git for-each-ref 2>err &&
46+
test_cmp missing-err err &&
47+
(
48+
cat brief-list &&
49+
echo "$MISSING $r"
50+
) | sort -k 2 >missing-brief-expected &&
51+
git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
52+
test_cmp missing-brief-expected brief-out &&
53+
test_must_be_empty brief-err
54+
'
55+
56+
test_done

0 commit comments

Comments
 (0)