Skip to content

Commit 8afc493

Browse files
mhaggergitster
authored andcommitted
for-each-ref: report broken references correctly
If there is a loose reference file with invalid contents, "git for-each-ref" incorrectly reports the problem as being a missing object with name NULL_SHA1: $ echo '12345678' >.git/refs/heads/nonsense $ git for-each-ref fatal: missing object 0000000000000000000000000000000000000000 for refs/heads/nonsense With an explicit "--format" string, it can even report that the reference validly points at NULL_SHA1: $ git for-each-ref --format='%(objectname) %(refname)' 0000000000000000000000000000000000000000 refs/heads/nonsense $ echo $? 0 This has been broken since b7dd2d2 for-each-ref: Do not lookup objects when they will not be used (2009-05-27) , which changed for-each-ref from using for_each_ref() to using git_for_each_rawref() in order to avoid looking up the referred-to objects unnecessarily. (When "git for-each-ref" is given a "--format" string that doesn't include information about the pointed-to object, it does not look up the object at all, which makes it considerably faster. Iterating with DO_FOR_EACH_INCLUDE_BROKEN is essential to this optimization because otherwise for_each_ref() would itself need to check whether the object exists as part of its brokenness test.) But for_each_rawref() includes broken references in the iteration, and "git for-each-ref" doesn't itself reject references with REF_ISBROKEN. The result is that broken references are processed *as if* they had the value NULL_SHA1, which is the value stored in entries for broken references. Change "git for-each-ref" to emit warnings for references that are REF_ISBROKEN but to otherwise skip them. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3e23dc commit 8afc493

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
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);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test_expect_success setup '
1414
git for-each-ref --format="%(objectname) %(refname)" >brief-list
1515
'
1616

17-
test_expect_failure 'Broken refs are reported correctly' '
17+
test_expect_success 'Broken refs are reported correctly' '
1818
r=refs/heads/bogus &&
1919
: >.git/$r &&
2020
test_when_finished "rm -f .git/$r" &&

0 commit comments

Comments
 (0)