Skip to content

Commit 215b565

Browse files
KarthikNayakgitster
authored andcommitted
for-each-ref: introduce new structures for better organisation
Introduce 'ref_filter_cbdata' which will hold 'ref_filter' (conditions to filter the refs on) and 'ref_array' (the array of ref_array_items). Modify the code to use these new structures. This is a preparatory patch to eventually move code from 'for-each-ref' to 'ref-filter' and make it publicly available. Mentored-by: Christian Couder <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5879232 commit 215b565

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

builtin/for-each-ref.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ struct ref_array_item {
3939
char *refname;
4040
};
4141

42+
struct ref_array {
43+
int nr, alloc;
44+
struct ref_array_item **items;
45+
};
46+
47+
struct ref_filter {
48+
const char **name_patterns;
49+
};
50+
51+
struct ref_filter_cbdata {
52+
struct ref_array array;
53+
struct ref_filter filter;
54+
};
55+
4256
static struct {
4357
const char *name;
4458
cmp_type cmp_type;
@@ -844,12 +858,6 @@ static void get_value(struct ref_array_item *ref, int atom, struct atom_value **
844858
*v = &ref->value[atom];
845859
}
846860

847-
struct grab_ref_cbdata {
848-
struct ref_array_item **grab_array;
849-
const char **grab_pattern;
850-
int grab_cnt;
851-
};
852-
853861
/*
854862
* Return 1 if the refname matches one of the patterns, otherwise 0.
855863
* A pattern can be path prefix (e.g. a refname "refs/heads/master"
@@ -895,15 +903,16 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
895903
static int grab_single_ref(const char *refname, const struct object_id *oid,
896904
int flag, void *cb_data)
897905
{
898-
struct grab_ref_cbdata *cb = cb_data;
906+
struct ref_filter_cbdata *ref_cbdata = cb_data;
907+
struct ref_filter *filter = &ref_cbdata->filter;
899908
struct ref_array_item *ref;
900909

901910
if (flag & REF_BAD_NAME) {
902911
warning("ignoring ref with broken name %s", refname);
903912
return 0;
904913
}
905914

906-
if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname))
915+
if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
907916
return 0;
908917

909918
/*
@@ -913,8 +922,8 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
913922
*/
914923
ref = new_ref_array_item(refname, oid->hash, flag);
915924

916-
REALLOC_ARRAY(cb->grab_array, cb->grab_cnt + 1);
917-
cb->grab_array[cb->grab_cnt++] = ref;
925+
REALLOC_ARRAY(ref_cbdata->array.items, ref_cbdata->array.nr + 1);
926+
ref_cbdata->array.items[ref_cbdata->array.nr++] = ref;
918927
return 0;
919928
}
920929

@@ -957,10 +966,10 @@ static int compare_refs(const void *a_, const void *b_)
957966
return 0;
958967
}
959968

960-
static void sort_refs(struct ref_sort *sort, struct ref_array_item **refs, int num_refs)
969+
static void sort_refs(struct ref_sort *sort, struct ref_array *array)
961970
{
962971
ref_sort = sort;
963-
qsort(refs, num_refs, sizeof(struct ref_array_item *), compare_refs);
972+
qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
964973
}
965974

966975
static void print_value(struct atom_value *v, int quote_style)
@@ -1096,12 +1105,11 @@ static char const * const for_each_ref_usage[] = {
10961105

10971106
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
10981107
{
1099-
int i, num_refs;
1108+
int i;
11001109
const char *format = "%(objectname) %(objecttype)\t%(refname)";
11011110
struct ref_sort *sort = NULL, **sort_tail = &sort;
11021111
int maxcount = 0, quote_style = 0;
1103-
struct ref_array_item **refs;
1104-
struct grab_ref_cbdata cbdata;
1112+
struct ref_filter_cbdata ref_cbdata;
11051113

11061114
struct option opts[] = {
11071115
OPT_BIT('s', "shell", &quote_style,
@@ -1139,17 +1147,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
11391147
/* for warn_ambiguous_refs */
11401148
git_config(git_default_config, NULL);
11411149

1142-
memset(&cbdata, 0, sizeof(cbdata));
1143-
cbdata.grab_pattern = argv;
1144-
for_each_rawref(grab_single_ref, &cbdata);
1145-
refs = cbdata.grab_array;
1146-
num_refs = cbdata.grab_cnt;
1150+
memset(&ref_cbdata, 0, sizeof(ref_cbdata));
1151+
ref_cbdata.filter.name_patterns = argv;
1152+
for_each_rawref(grab_single_ref, &ref_cbdata);
11471153

1148-
sort_refs(sort, refs, num_refs);
1154+
sort_refs(sort, &ref_cbdata.array);
11491155

1150-
if (!maxcount || num_refs < maxcount)
1151-
maxcount = num_refs;
1156+
if (!maxcount || ref_cbdata.array.nr < maxcount)
1157+
maxcount = ref_cbdata.array.nr;
11521158
for (i = 0; i < maxcount; i++)
1153-
show_ref(refs[i], format, quote_style);
1159+
show_ref(ref_cbdata.array.items[i], format, quote_style);
11541160
return 0;
11551161
}

0 commit comments

Comments
 (0)