Merge branch 'jk/prune-with-corrupt-refs'

"git prune" used to largely ignore broken refs when deciding which
objects are still being used, which could spread an existing small
damage and make it a larger one.

* jk/prune-with-corrupt-refs:
  refs.c: drop curate_packed_refs
  repack: turn on "ref paranoia" when doing a destructive repack
  prune: turn on ref_paranoia flag
  refs: introduce a "ref paranoia" flag
  t5312: test object deletion code paths in a corrupted repository
This commit is contained in:
Junio C Hamano
2015-03-25 12:54:26 -07:00
7 changed files with 147 additions and 68 deletions

72
refs.c
View File

@ -1934,6 +1934,11 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
data.fn = fn;
data.cb_data = cb_data;
if (ref_paranoia < 0)
ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
if (ref_paranoia)
data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
return do_for_each_entry(refs, base, do_one_ref, &data);
}
@ -2616,68 +2621,10 @@ int pack_refs(unsigned int flags)
return 0;
}
/*
* If entry is no longer needed in packed-refs, add it to the string
* list pointed to by cb_data. Reasons for deleting entries:
*
* - Entry is broken.
* - Entry is overridden by a loose ref.
* - Entry does not point at a valid object.
*
* In the first and third cases, also emit an error message because these
* are indications of repository corruption.
*/
static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
{
struct string_list *refs_to_delete = cb_data;
if (entry->flag & REF_ISBROKEN) {
/* This shouldn't happen to packed refs. */
error("%s is broken!", entry->name);
string_list_append(refs_to_delete, entry->name);
return 0;
}
if (!has_sha1_file(entry->u.value.sha1)) {
unsigned char sha1[20];
int flags;
if (read_ref_full(entry->name, 0, sha1, &flags))
/* We should at least have found the packed ref. */
die("Internal error");
if ((flags & REF_ISSYMREF) || !(flags & REF_ISPACKED)) {
/*
* This packed reference is overridden by a
* loose reference, so it is OK that its value
* is no longer valid; for example, it might
* refer to an object that has been garbage
* collected. For this purpose we don't even
* care whether the loose reference itself is
* invalid, broken, symbolic, etc. Silently
* remove the packed reference.
*/
string_list_append(refs_to_delete, entry->name);
return 0;
}
/*
* There is no overriding loose reference, so the fact
* that this reference doesn't refer to a valid object
* indicates some kind of repository corruption.
* Report the problem, then omit the reference from
* the output.
*/
error("%s does not point to a valid object!", entry->name);
string_list_append(refs_to_delete, entry->name);
return 0;
}
return 0;
}
int repack_without_refs(struct string_list *refnames, struct strbuf *err)
{
struct ref_dir *packed;
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
struct string_list_item *refname, *ref_to_delete;
struct string_list_item *refname;
int ret, needs_repacking = 0, removed = 0;
assert(err);
@ -2713,13 +2660,6 @@ int repack_without_refs(struct string_list *refnames, struct strbuf *err)
return 0;
}
/* Remove any other accumulated cruft */
do_for_each_entry_in_dir(packed, 0, curate_packed_ref_fn, &refs_to_delete);
for_each_string_list_item(ref_to_delete, &refs_to_delete) {
if (remove_entry(packed, ref_to_delete->string) == -1)
die("internal error");
}
/* Write what remains */
ret = commit_packed_refs();
if (ret)