Skip to content

Commit 9d71c5f

Browse files
committed
Merge branch 'mh/reporting-broken-refs-from-for-each-ref'
"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 54a17cd + 501cf47 commit 9d71c5f

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
@@ -866,6 +866,11 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
866866
return 0;
867867
}
868868

869+
if (flag & REF_ISBROKEN) {
870+
warning("ignoring broken ref %s", refname);
871+
return 0;
872+
}
873+
869874
if (*cb->grab_pattern) {
870875
const char **pattern;
871876
int namelen = strlen(refname);

refs.c

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

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)