do_for_each_ref_in_arrays(): new function

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@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 10, 2012 at 07:30 UTC b3fd060f9e60f842b46183067f1a76fa6a5358b1
1 file changed +53 -29
refs.c
+53 -29
@@ -287,6 +287,52 @@ static int do_for_each_ref_in_array(struct ref_array *array, int offset,
287 return 0;
288 }
289
290 +/*
291 + * Call fn for each reference in the union of array1 and array2, in
292 + * order by refname. If an entry appears in both array1 and array2,
293 + * then only process the version that is in array2. The input arrays
294 + * must already be sorted.
295 + */
296 +static int do_for_each_ref_in_arrays(struct ref_array *array1,
297 + struct ref_array *array2,
298 + const char *base, each_ref_fn fn, int trim,
299 + int flags, void *cb_data)
300 +{
301 + int retval;
302 + int i1 = 0, i2 = 0;
303 +
304 + assert(array1->sorted == array1->nr);
305 + assert(array2->sorted == array2->nr);
306 + while (i1 < array1->nr && i2 < array2->nr) {
307 + struct ref_entry *e1 = array1->refs[i1];
308 + struct ref_entry *e2 = array2->refs[i2];
309 + int cmp = strcmp(e1->name, e2->name);
310 + if (cmp < 0) {
311 + retval = do_one_ref(base, fn, trim, flags, cb_data, e1);
312 + i1++;
313 + } else {
314 + retval = do_one_ref(base, fn, trim, flags, cb_data, e2);
315 + i2++;
316 + if (cmp == 0) {
317 + /*
318 + * There was a ref in array1 with the
319 + * same name; ignore it.
320 + */
321 + i1++;
322 + }
323 + }
324 + if (retval)
325 + return retval;
326 + }
327 + if (i1 < array1->nr)
328 + return do_for_each_ref_in_array(array1, i1,
329 + base, fn, trim, flags, cb_data);
330 + if (i2 < array2->nr)
331 + return do_for_each_ref_in_array(array2, i2,
332 + base, fn, trim, flags, cb_data);
333 + return 0;
334 +}
335 +
336 /*
337 * Return true iff a reference named refname could be created without
338 * conflicting with the name of an existing reference. If oldrefname
@@ -873,36 +919,14 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
919 static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn,
920 int trim, int flags, void *cb_data)
921 {
876 - int retval = 0, p = 0, l = 0;
922 struct ref_cache *refs = get_ref_cache(submodule);
878 - struct ref_array *packed = get_packed_refs(refs);
879 - struct ref_array *loose = get_loose_refs(refs);
880 -
881 - sort_ref_array(packed);
882 - sort_ref_array(loose);
883 - while (p < packed->nr && l < loose->nr) {
884 - struct ref_entry *entry;
885 - int cmp = strcmp(packed->refs[p]->name, loose->refs[l]->name);
886 - if (!cmp) {
887 - p++;
888 - continue;
889 - }
890 - if (cmp > 0) {
891 - entry = loose->refs[l++];
892 - } else {
893 - entry = packed->refs[p++];
894 - }
895 - retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
896 - if (retval)
897 - return retval;
898 - }
899 -
900 - if (l < loose->nr)
901 - return do_for_each_ref_in_array(loose, l, base, fn, trim, flags, cb_data);
902 - if (p < packed->nr)
903 - return do_for_each_ref_in_array(packed, p, base, fn, trim, flags, cb_data);
904 -
905 - return 0;
923 + struct ref_array *packed_refs = get_packed_refs(refs);
924 + struct ref_array *loose_refs = get_loose_refs(refs);
925 + sort_ref_array(packed_refs);
926 + sort_ref_array(loose_refs);
927 + return do_for_each_ref_in_arrays(packed_refs,
928 + loose_refs,
929 + base, fn, trim, flags, cb_data);
930 }
931
932 static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)