From: Michael Haggerty <mhagger@xxxxxxxxxxxx> Extract function do_for_each_ref_in_arrays() from do_for_each_ref(). The new function will be a useful building block for storing refs hierarchically. Signed-off-by: Michael Haggerty <mhagger@xxxxxxxxxxxx> --- refs.c | 71 +++++++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 42 insertions(+), 29 deletions(-) diff --git a/refs.c b/refs.c index bc14437..601665b 100644 --- a/refs.c +++ b/refs.c @@ -714,45 +714,58 @@ static int do_for_each_ref_in_array(struct ref_array *array, int offset, return 0; } -static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn, - int trim, int flags, void *cb_data) +static int do_for_each_ref_in_arrays(struct ref_array *array1, + struct ref_array *array2, + const char *base, each_ref_fn fn, int trim, + int flags, void *cb_data) { - int retval = 0, p = 0, l = 0; - struct ref_cache *refs = get_ref_cache(submodule); - struct ref_array *packed = get_packed_refs(refs); - struct ref_array *loose = get_loose_refs(refs); + int retval; + int i1 = 0, i2 = 0; - retval = do_for_each_ref_in_array(&extra_refs, 0, - base, fn, trim, flags, cb_data); - if (retval) - goto end_each; - - while (p < packed->nr && l < loose->nr) { - struct ref_entry *entry; - int cmp = strcmp(packed->refs[p]->name, loose->refs[l]->name); - if (!cmp) { - p++; + while (1) { + struct ref_entry *e1, *e2; + int cmp; + if (i1 == array1->nr) { + return do_for_each_ref_in_array(array2, i2, + base, fn, trim, flags, cb_data); + } + if (i2 == array2->nr) { + return do_for_each_ref_in_array(array1, i1, + base, fn, trim, flags, cb_data); + } + e1 = array1->refs[i1]; + e2 = array2->refs[i2]; + cmp = strcmp(e1->name, e2->name); + if (cmp == 0) { + /* Two refs with the same name; ignore the one from array1. */ + i1++; continue; } - if (cmp > 0) { - entry = loose->refs[l++]; + if (cmp < 0) { + retval = do_one_ref(base, fn, trim, flags, cb_data, e1); + i1++; } else { - entry = packed->refs[p++]; + retval = do_one_ref(base, fn, trim, flags, cb_data, e2); + i2++; } - retval = do_one_ref(base, fn, trim, flags, cb_data, entry); if (retval) - goto end_each; + return retval; } +} - if (l < loose->nr) { - retval = do_for_each_ref_in_array(loose, l, - base, fn, trim, flags, cb_data); - } else { - retval = do_for_each_ref_in_array(packed, p, - base, fn, trim, flags, cb_data); - } +static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn, + int trim, int flags, void *cb_data) +{ + int retval = 0; + struct ref_cache *refs = get_ref_cache(submodule); + + retval = do_for_each_ref_in_array(&extra_refs, 0, + base, fn, trim, flags, cb_data); + if (!retval) + retval = do_for_each_ref_in_arrays(get_packed_refs(refs), + get_loose_refs(refs), + base, fn, trim, flags, cb_data); -end_each: current_ref = NULL; return retval; } -- 1.7.8 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html