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
This commit is contained in:
Junio C Hamano 2015-08-03 10:41:31 -07:00
commit 0533a9b70c
3 changed files with 83 additions and 7 deletions

View file

@ -851,6 +851,11 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
return 0;
}
if (flag & REF_ISBROKEN) {
warning("ignoring broken ref %s", refname);
return 0;
}
if (*cb->grab_pattern) {
const char **pattern;
int namelen = strlen(refname);

29
refs.c
View file

@ -1374,19 +1374,34 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
create_dir_entry(refs, refname.buf,
refname.len, 1));
} else {
int read_ok;
if (*refs->name) {
hashclr(sha1);
flag = 0;
if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
hashclr(sha1);
flag |= REF_ISBROKEN;
}
} else if (read_ref_full(refname.buf,
RESOLVE_REF_READING,
sha1, &flag)) {
read_ok = !resolve_gitlink_ref(refs->name,
refname.buf, sha1);
} else {
read_ok = !read_ref_full(refname.buf,
RESOLVE_REF_READING,
sha1, &flag);
}
if (!read_ok) {
hashclr(sha1);
flag |= REF_ISBROKEN;
} else if (is_null_sha1(sha1)) {
/*
* It is so astronomically unlikely
* that NULL_SHA1 is the SHA-1 of an
* actual object that we consider its
* appearance in a loose reference
* file to be repo corruption
* (probably due to a software bug).
*/
flag |= REF_ISBROKEN;
}
if (check_refname_format(refname.buf,
REFNAME_ALLOW_ONELEVEL)) {
hashclr(sha1);

56
t/t6301-for-each-ref-errors.sh Executable file
View file

@ -0,0 +1,56 @@
#!/bin/sh
test_description='for-each-ref errors for broken refs'
. ./test-lib.sh
ZEROS=$_z40
MISSING=abababababababababababababababababababab
test_expect_success setup '
git commit --allow-empty -m "Initial" &&
git tag testtag &&
git for-each-ref >full-list &&
git for-each-ref --format="%(objectname) %(refname)" >brief-list
'
test_expect_success 'Broken refs are reported correctly' '
r=refs/heads/bogus &&
: >.git/$r &&
test_when_finished "rm -f .git/$r" &&
echo "warning: ignoring broken ref $r" >broken-err &&
git for-each-ref >out 2>err &&
test_cmp full-list out &&
test_cmp broken-err err
'
test_expect_success 'NULL_SHA1 refs are reported correctly' '
r=refs/heads/zeros &&
echo $ZEROS >.git/$r &&
test_when_finished "rm -f .git/$r" &&
echo "warning: ignoring broken ref $r" >zeros-err &&
git for-each-ref >out 2>err &&
test_cmp full-list out &&
test_cmp zeros-err err &&
git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
test_cmp brief-list brief-out &&
test_cmp zeros-err brief-err
'
test_expect_success 'Missing objects are reported correctly' '
r=refs/heads/missing &&
echo $MISSING >.git/$r &&
test_when_finished "rm -f .git/$r" &&
echo "fatal: missing object $MISSING for $r" >missing-err &&
test_must_fail git for-each-ref 2>err &&
test_cmp missing-err err &&
(
cat brief-list &&
echo "$MISSING $r"
) | sort -k 2 >missing-brief-expected &&
git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
test_cmp missing-brief-expected brief-out &&
test_must_be_empty brief-err
'
test_done