refs: extract a function ref_resolves_to_object()

It is a nice unit of work and soon will be needed from multiple locations. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 22, 2013 at 21:52 UTC 662428f4e98a740800659081910ef8b1ef3940fa
1 file changed +20 -8
refs.c
+20 -8
@@ -529,6 +529,22 @@ static void sort_ref_dir(struct ref_dir *dir)
529 /* Include broken references in a do_for_each_ref*() iteration: */
530 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
531
532 +/*
533 + * Return true iff the reference described by entry can be resolved to
534 + * an object in the database. Emit a warning if the referred-to
535 + * object does not exist.
536 + */
537 +static int ref_resolves_to_object(struct ref_entry *entry)
538 +{
539 + if (entry->flag & REF_ISBROKEN)
540 + return 0;
541 + if (!has_sha1_file(entry->u.value.sha1)) {
542 + error("%s does not point to a valid object!", entry->name);
543 + return 0;
544 + }
545 + return 1;
546 +}
547 +
548 /*
549 * current_ref is a performance hack: when iterating over references
550 * using the for_each_ref*() functions, current_ref is set to the
@@ -550,14 +566,10 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
566 if (prefixcmp(entry->name, base))
567 return 0;
568
553 - if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
554 - if (entry->flag & REF_ISBROKEN)
555 - return 0; /* ignore broken refs e.g. dangling symref */
556 - if (!has_sha1_file(entry->u.value.sha1)) {
557 - error("%s does not point to a valid object!", entry->name);
558 - return 0;
559 - }
560 - }
569 + if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
570 + !ref_resolves_to_object(entry))
571 + return 0;
572 +
573 current_ref = entry;
574 retval = fn(entry->name + trim, entry->u.value.sha1, entry->flag, cb_data);
575 current_ref = NULL;