]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: factor ref_array pushing into its own function
authorJeff King <peff@peff.net>
Fri, 6 Apr 2018 18:59:45 +0000 (14:59 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 8 Apr 2018 21:14:46 +0000 (06:14 +0900)
In preparation for callers constructing their own ref_array
structs, let's move our own internal push operation into its
own function.

While we're at it, we can replace REALLOC_ARRAY() with
ALLOC_GROW(), which should give the growth operation
amortized linear complexity (as opposed to growing by one,
which is potentially quadratic, though in-place realloc
growth often makes this faster in practice).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c
ref-filter.h

index c1c3cc9480037b7f78a57e76248df7c2ac7137e0..6e9328b274de7e4c8f7935ff289fba5b64dbd43f 100644 (file)
@@ -1840,6 +1840,18 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
        return ref;
 }
 
+struct ref_array_item *ref_array_push(struct ref_array *array,
+                                     const char *refname,
+                                     const struct object_id *oid)
+{
+       struct ref_array_item *ref = new_ref_array_item(refname, oid);
+
+       ALLOC_GROW(array->items, array->nr + 1, array->alloc);
+       array->items[array->nr++] = ref;
+
+       return ref;
+}
+
 static int ref_kind_from_refname(const char *refname)
 {
        unsigned int i;
@@ -1930,13 +1942,11 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
         * to do its job and the resulting list may yet to be pruned
         * by maxcount logic.
         */
-       ref = new_ref_array_item(refname, oid);
+       ref = ref_array_push(ref_cbdata->array, refname, oid);
        ref->commit = commit;
        ref->flag = flag;
        ref->kind = kind;
 
-       REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
-       ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
        return 0;
 }
 
index 68268f9ebcbc683ac2df348a9096a5ebd2227e85..76cf87cb6c64928437c2cc8c31784eba58d09bc7 100644 (file)
@@ -135,4 +135,12 @@ void setup_ref_filter_porcelain_msg(void);
 void pretty_print_ref(const char *name, const struct object_id *oid,
                      const struct ref_format *format);
 
+/*
+ * Push a single ref onto the array; this can be used to construct your own
+ * ref_array without using filter_refs().
+ */
+struct ref_array_item *ref_array_push(struct ref_array *array,
+                                     const char *refname,
+                                     const struct object_id *oid);
+
 #endif /*  REF_FILTER_H  */