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 <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jun 14, 2015 at 01:07 UTC 215b5651261f866dc7c885eb02b6f7df7246a7cf
1 file changed +30 -24
builtin/for-each-ref.c
+30 -24
@@ -39,6 +39,20 @@ struct ref_array_item {
39 char *refname;
40 };
41
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 +
56 static struct {
57 const char *name;
58 cmp_type cmp_type;
@@ -844,12 +858,6 @@ static void get_value(struct ref_array_item *ref, int atom, struct atom_value **
858 *v = &ref->value[atom];
859 }
860
847 -struct grab_ref_cbdata {
848 - struct ref_array_item **grab_array;
849 - const char **grab_pattern;
850 - int grab_cnt;
851 -};
852 -
861 /*
862 * Return 1 if the refname matches one of the patterns, otherwise 0.
863 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
@@ -895,7 +903,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
903 static int grab_single_ref(const char *refname, const struct object_id *oid,
904 int flag, void *cb_data)
905 {
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;
908 struct ref_array_item *ref;
909
910 if (flag & REF_BAD_NAME) {
@@ -903,7 +912,7 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
912 return 0;
913 }
914
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))
916 return 0;
917
918 /*
@@ -913,8 +922,8 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
922 */
923 ref = new_ref_array_item(refname, oid->hash, flag);
924
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;
927 return 0;
928 }
929
@@ -957,10 +966,10 @@ static int compare_refs(const void *a_, const void *b_)
966 return 0;
967 }
968
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)
970 {
971 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);
973 }
974
975 static void print_value(struct atom_value *v, int quote_style)
@@ -1096,12 +1105,11 @@ static char const * const for_each_ref_usage[] = {
1105
1106 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1107 {
1099 - int i, num_refs;
1108 + int i;
1109 const char *format = "%(objectname) %(objecttype)\t%(refname)";
1110 struct ref_sort *sort = NULL, **sort_tail = &sort;
1111 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;
1113
1114 struct option opts[] = {
1115 OPT_BIT('s', "shell", &quote_style,
@@ -1139,17 +1147,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1147 /* for warn_ambiguous_refs */
1148 git_config(git_default_config, NULL);
1149
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);
1153
1148 - sort_refs(sort, refs, num_refs);
1154 + sort_refs(sort, &ref_cbdata.array);
1155
1150 - if (!maxcount || num_refs < maxcount)
1151 - maxcount = num_refs;
1156 + if (!maxcount || ref_cbdata.array.nr < maxcount)
1157 + maxcount = ref_cbdata.array.nr;
1158 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);
1160 return 0;
1161 }