Skip to content

Commit 613d991

Browse files
vdyegitster
authored andcommitted
ref-filter.c: refactor to create common helper functions
Factor out parts of 'ref_array_push()', 'ref_filter_handler()', and 'filter_refs()' into new helper functions: * Extract the code to grow a 'struct ref_array' and append a given 'struct ref_array_item *' to it from 'ref_array_push()' into 'ref_array_append()'. * Extract the code to filter a given ref by refname & object ID then create a new 'struct ref_array_item *' from 'filter_one()' into 'apply_ref_filter()'. * Extract the code for filter pre-processing, contains cache creation, and ref iteration from 'filter_refs()' into 'do_filter_refs()'. In later patches, these helpers will be used by new ref-filter API functions. This patch does not result in any user-facing behavior changes or changes to callers outside of 'ref-filter.c'. Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9c575b0 commit 613d991

File tree

1 file changed

+69
-46
lines changed

1 file changed

+69
-46
lines changed

ref-filter.c

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,15 +2632,18 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
26322632
return ref;
26332633
}
26342634

2635+
static void ref_array_append(struct ref_array *array, struct ref_array_item *ref)
2636+
{
2637+
ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2638+
array->items[array->nr++] = ref;
2639+
}
2640+
26352641
struct ref_array_item *ref_array_push(struct ref_array *array,
26362642
const char *refname,
26372643
const struct object_id *oid)
26382644
{
26392645
struct ref_array_item *ref = new_ref_array_item(refname, oid);
2640-
2641-
ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2642-
array->items[array->nr++] = ref;
2643-
2646+
ref_array_append(array, ref);
26442647
return ref;
26452648
}
26462649

@@ -2677,46 +2680,36 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
26772680
return ref_kind_from_refname(refname);
26782681
}
26792682

2680-
struct ref_filter_cbdata {
2681-
struct ref_array *array;
2682-
struct ref_filter *filter;
2683-
};
2684-
2685-
/*
2686-
* A call-back given to for_each_ref(). Filter refs and keep them for
2687-
* later object processing.
2688-
*/
2689-
static int filter_one(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2683+
static struct ref_array_item *apply_ref_filter(const char *refname, const struct object_id *oid,
2684+
int flag, struct ref_filter *filter)
26902685
{
2691-
struct ref_filter_cbdata *ref_cbdata = cb_data;
2692-
struct ref_filter *filter = ref_cbdata->filter;
26932686
struct ref_array_item *ref;
26942687
struct commit *commit = NULL;
26952688
unsigned int kind;
26962689

26972690
if (flag & REF_BAD_NAME) {
26982691
warning(_("ignoring ref with broken name %s"), refname);
2699-
return 0;
2692+
return NULL;
27002693
}
27012694

27022695
if (flag & REF_ISBROKEN) {
27032696
warning(_("ignoring broken ref %s"), refname);
2704-
return 0;
2697+
return NULL;
27052698
}
27062699

27072700
/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
27082701
kind = filter_ref_kind(filter, refname);
27092702
if (!(kind & filter->kind))
2710-
return 0;
2703+
return NULL;
27112704

27122705
if (!filter_pattern_match(filter, refname))
2713-
return 0;
2706+
return NULL;
27142707

27152708
if (filter_exclude_match(filter, refname))
2716-
return 0;
2709+
return NULL;
27172710

27182711
if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
2719-
return 0;
2712+
return NULL;
27202713

27212714
/*
27222715
* A merge filter is applied on refs pointing to commits. Hence
@@ -2727,27 +2720,48 @@ static int filter_one(const char *refname, const struct object_id *oid, int flag
27272720
filter->with_commit || filter->no_commit || filter->verbose) {
27282721
commit = lookup_commit_reference_gently(the_repository, oid, 1);
27292722
if (!commit)
2730-
return 0;
2723+
return NULL;
27312724
/* We perform the filtering for the '--contains' option... */
27322725
if (filter->with_commit &&
27332726
!commit_contains(filter, commit, filter->with_commit, &filter->internal.contains_cache))
2734-
return 0;
2727+
return NULL;
27352728
/* ...or for the `--no-contains' option */
27362729
if (filter->no_commit &&
27372730
commit_contains(filter, commit, filter->no_commit, &filter->internal.no_contains_cache))
2738-
return 0;
2731+
return NULL;
27392732
}
27402733

27412734
/*
27422735
* We do not open the object yet; sort may only need refname
27432736
* to do its job and the resulting list may yet to be pruned
27442737
* by maxcount logic.
27452738
*/
2746-
ref = ref_array_push(ref_cbdata->array, refname, oid);
2739+
ref = new_ref_array_item(refname, oid);
27472740
ref->commit = commit;
27482741
ref->flag = flag;
27492742
ref->kind = kind;
27502743

2744+
return ref;
2745+
}
2746+
2747+
struct ref_filter_cbdata {
2748+
struct ref_array *array;
2749+
struct ref_filter *filter;
2750+
};
2751+
2752+
/*
2753+
* A call-back given to for_each_ref(). Filter refs and keep them for
2754+
* later object processing.
2755+
*/
2756+
static int filter_one(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2757+
{
2758+
struct ref_filter_cbdata *ref_cbdata = cb_data;
2759+
struct ref_array_item *ref;
2760+
2761+
ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter);
2762+
if (ref)
2763+
ref_array_append(ref_cbdata->array, ref);
2764+
27512765
return 0;
27522766
}
27532767

@@ -2883,26 +2897,12 @@ void filter_ahead_behind(struct repository *r,
28832897
free(commits);
28842898
}
28852899

2886-
/*
2887-
* API for filtering a set of refs. Based on the type of refs the user
2888-
* has requested, we iterate through those refs and apply filters
2889-
* as per the given ref_filter structure and finally store the
2890-
* filtered refs in the ref_array structure.
2891-
*/
2892-
int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2900+
static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref_fn fn, void *cb_data)
28932901
{
2894-
struct ref_filter_cbdata ref_cbdata;
2895-
int save_commit_buffer_orig;
28962902
int ret = 0;
28972903

2898-
ref_cbdata.array = array;
2899-
ref_cbdata.filter = filter;
2900-
29012904
filter->kind = type & FILTER_REFS_KIND_MASK;
29022905

2903-
save_commit_buffer_orig = save_commit_buffer;
2904-
save_commit_buffer = 0;
2905-
29062906
init_contains_cache(&filter->internal.contains_cache);
29072907
init_contains_cache(&filter->internal.no_contains_cache);
29082908

@@ -2917,20 +2917,43 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
29172917
* of filter_ref_kind().
29182918
*/
29192919
if (filter->kind == FILTER_REFS_BRANCHES)
2920-
ret = for_each_fullref_in("refs/heads/", filter_one, &ref_cbdata);
2920+
ret = for_each_fullref_in("refs/heads/", fn, cb_data);
29212921
else if (filter->kind == FILTER_REFS_REMOTES)
2922-
ret = for_each_fullref_in("refs/remotes/", filter_one, &ref_cbdata);
2922+
ret = for_each_fullref_in("refs/remotes/", fn, cb_data);
29232923
else if (filter->kind == FILTER_REFS_TAGS)
2924-
ret = for_each_fullref_in("refs/tags/", filter_one, &ref_cbdata);
2924+
ret = for_each_fullref_in("refs/tags/", fn, cb_data);
29252925
else if (filter->kind & FILTER_REFS_ALL)
2926-
ret = for_each_fullref_in_pattern(filter, filter_one, &ref_cbdata);
2926+
ret = for_each_fullref_in_pattern(filter, fn, cb_data);
29272927
if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
2928-
head_ref(filter_one, &ref_cbdata);
2928+
head_ref(fn, cb_data);
29292929
}
29302930

29312931
clear_contains_cache(&filter->internal.contains_cache);
29322932
clear_contains_cache(&filter->internal.no_contains_cache);
29332933

2934+
return ret;
2935+
}
2936+
2937+
/*
2938+
* API for filtering a set of refs. Based on the type of refs the user
2939+
* has requested, we iterate through those refs and apply filters
2940+
* as per the given ref_filter structure and finally store the
2941+
* filtered refs in the ref_array structure.
2942+
*/
2943+
int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2944+
{
2945+
struct ref_filter_cbdata ref_cbdata;
2946+
int save_commit_buffer_orig;
2947+
int ret = 0;
2948+
2949+
ref_cbdata.array = array;
2950+
ref_cbdata.filter = filter;
2951+
2952+
save_commit_buffer_orig = save_commit_buffer;
2953+
save_commit_buffer = 0;
2954+
2955+
ret = do_filter_refs(filter, type, filter_one, &ref_cbdata);
2956+
29342957
/* Filters that need revision walking */
29352958
reach_filter(array, &filter->reachable_from, INCLUDE_REACHED);
29362959
reach_filter(array, &filter->unreachable_from, EXCLUDE_REACHED);

0 commit comments

Comments
 (0)