Skip to content

Commit 427cbc9

Browse files
peffgitster
authored andcommitted
ref-filter: factor ref_array pushing into its own function
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ffaa00 commit 427cbc9

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

ref-filter.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,18 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
18401840
return ref;
18411841
}
18421842

1843+
struct ref_array_item *ref_array_push(struct ref_array *array,
1844+
const char *refname,
1845+
const struct object_id *oid)
1846+
{
1847+
struct ref_array_item *ref = new_ref_array_item(refname, oid);
1848+
1849+
ALLOC_GROW(array->items, array->nr + 1, array->alloc);
1850+
array->items[array->nr++] = ref;
1851+
1852+
return ref;
1853+
}
1854+
18431855
static int ref_kind_from_refname(const char *refname)
18441856
{
18451857
unsigned int i;
@@ -1930,13 +1942,11 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
19301942
* to do its job and the resulting list may yet to be pruned
19311943
* by maxcount logic.
19321944
*/
1933-
ref = new_ref_array_item(refname, oid);
1945+
ref = ref_array_push(ref_cbdata->array, refname, oid);
19341946
ref->commit = commit;
19351947
ref->flag = flag;
19361948
ref->kind = kind;
19371949

1938-
REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1939-
ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
19401950
return 0;
19411951
}
19421952

ref-filter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ void setup_ref_filter_porcelain_msg(void);
135135
void pretty_print_ref(const char *name, const struct object_id *oid,
136136
const struct ref_format *format);
137137

138+
/*
139+
* Push a single ref onto the array; this can be used to construct your own
140+
* ref_array without using filter_refs().
141+
*/
142+
struct ref_array_item *ref_array_push(struct ref_array *array,
143+
const char *refname,
144+
const struct object_id *oid);
145+
138146
#endif /* REF_FILTER_H */

0 commit comments

Comments
 (0)