for-each-ref: rename some functions and make them public

Rename some of the functions and make them publicly available. This is a preparatory step for moving code from 'for-each-ref' to 'ref-filter' to make meaningful, targeted services available to other commands via public APIs. Functions renamed are: parse_atom() -> parse_ref_filter_atom() verify_format() -> verify_ref_format() get_value() -> get_ref_atom_value() grab_single_ref() -> ref_filter_handler() sort_refs() -> ref_array_sort() show_ref() -> show_ref_array_item() default_sort() -> ref_default_sorting() opt_parse_sort() -> parse_opt_ref_sorting() cmp_ref_sort() -> cmp_ref_sorting() Rename 'struct ref_sort' to 'struct ref_sorting' in this context. Based-on-patch-by: Jeff King <peff@peff.net> 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 277b915715b8f832c60be8469628d26328670707
1 file changed +32 -32
builtin/for-each-ref.c
+32 -32
@@ -113,7 +113,7 @@ static int need_color_reset_at_eol;
113 /*
114 * Used to parse format string and sort specifiers
115 */
116 -static int parse_atom(const char *atom, const char *ep)
116 +int parse_ref_filter_atom(const char *atom, const char *ep)
117 {
118 const char *sp;
119 int i, at;
@@ -190,7 +190,7 @@ static const char *find_next(const char *cp)
190 * Make sure the format string is well formed, and parse out
191 * the used atoms.
192 */
193 -static int verify_format(const char *format)
193 +int verify_ref_format(const char *format)
194 {
195 const char *cp, *sp;
196
@@ -202,7 +202,7 @@ static int verify_format(const char *format)
202 if (!ep)
203 return error("malformed format string %s", sp);
204 /* sp points at "%(" and ep points at the closing ")" */
205 - at = parse_atom(sp + 2, ep);
205 + at = parse_ref_filter_atom(sp + 2, ep);
206 cp = ep + 1;
207
208 if (skip_prefix(used_atom[at], "color:", &color))
@@ -409,7 +409,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
409 /*
410 * We got here because atomname ends in "date" or "date<something>";
411 * it's not possible that <something> is not ":<format>" because
412 - * parse_atom() wouldn't have allowed it, so we can assume that no
412 + * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
413 * ":" means no format is specified, and use the default.
414 */
415 formatp = strchr(atomname, ':');
@@ -849,7 +849,7 @@ static void populate_value(struct ref_array_item *ref)
849 * Given a ref, return the value for the atom. This lazily gets value
850 * out of the object by calling populate value.
851 */
852 -static void get_value(struct ref_array_item *ref, int atom, struct atom_value **v)
852 +static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v)
853 {
854 if (!ref->value) {
855 populate_value(ref);
@@ -900,8 +900,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
900 * A call-back given to for_each_ref(). Filter refs and keep them for
901 * later object processing.
902 */
903 -static int grab_single_ref(const char *refname, const struct object_id *oid,
904 - int flag, void *cb_data)
903 +int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
904 {
905 struct ref_filter_cbdata *ref_cbdata = cb_data;
906 struct ref_filter *filter = &ref_cbdata->filter;
@@ -947,14 +946,14 @@ void ref_array_clear(struct ref_array *array)
946 array->nr = array->alloc = 0;
947 }
948
950 -static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b)
949 +static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
950 {
951 struct atom_value *va, *vb;
952 int cmp;
953 cmp_type cmp_type = used_atom_type[s->atom];
954
956 - get_value(a, s->atom, &va);
957 - get_value(b, s->atom, &vb);
955 + get_ref_atom_value(a, s->atom, &va);
956 + get_ref_atom_value(b, s->atom, &vb);
957 switch (cmp_type) {
958 case FIELD_STR:
959 cmp = strcmp(va->s, vb->s);
@@ -971,24 +970,24 @@ static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref
970 return (s->reverse) ? -cmp : cmp;
971 }
972
974 -static struct ref_sort *ref_sort;
973 +static struct ref_sorting *ref_sorting;
974 static int compare_refs(const void *a_, const void *b_)
975 {
976 struct ref_array_item *a = *((struct ref_array_item **)a_);
977 struct ref_array_item *b = *((struct ref_array_item **)b_);
979 - struct ref_sort *s;
978 + struct ref_sorting *s;
979
981 - for (s = ref_sort; s; s = s->next) {
982 - int cmp = cmp_ref_sort(s, a, b);
980 + for (s = ref_sorting; s; s = s->next) {
981 + int cmp = cmp_ref_sorting(s, a, b);
982 if (cmp)
983 return cmp;
984 }
985 return 0;
986 }
987
989 -static void sort_refs(struct ref_sort *sort, struct ref_array *array)
988 +void ref_array_sort(struct ref_sorting *sort, struct ref_array *array)
989 {
991 - ref_sort = sort;
990 + ref_sorting = sort;
991 qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
992 }
993
@@ -1056,7 +1055,7 @@ static void emit(const char *cp, const char *ep)
1055 }
1056 }
1057
1059 -static void show_ref(struct ref_array_item *info, const char *format, int quote_style)
1058 +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1059 {
1060 const char *cp, *sp, *ep;
1061
@@ -1066,7 +1065,7 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_
1065 ep = strchr(sp, ')');
1066 if (cp < sp)
1067 emit(cp, sp);
1069 - get_value(info, parse_atom(sp + 2, ep), &atomv);
1068 + get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
1069 print_value(atomv, quote_style);
1070 }
1071 if (*cp) {
@@ -1085,21 +1084,22 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_
1084 putchar('\n');
1085 }
1086
1088 -static struct ref_sort *default_sort(void)
1087 +/* If no sorting option is given, use refname to sort as default */
1088 +struct ref_sorting *ref_default_sorting(void)
1089 {
1090 static const char cstr_name[] = "refname";
1091
1092 - struct ref_sort *sort = xcalloc(1, sizeof(*sort));
1092 + struct ref_sorting *sort = xcalloc(1, sizeof(*sort));
1093
1094 sort->next = NULL;
1095 - sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name));
1095 + sort->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name));
1096 return sort;
1097 }
1098
1099 -static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
1099 +int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1100 {
1101 - struct ref_sort **sort_tail = opt->value;
1102 - struct ref_sort *s;
1101 + struct ref_sorting **sort_tail = opt->value;
1102 + struct ref_sorting *s;
1103 int len;
1104
1105 if (!arg) /* should --no-sort void the list ? */
@@ -1114,7 +1114,7 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
1114 arg++;
1115 }
1116 len = strlen(arg);
1117 - s->atom = parse_atom(arg, arg+len);
1117 + s->atom = parse_ref_filter_atom(arg, arg+len);
1118 return 0;
1119 }
1120
@@ -1127,7 +1127,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1127 {
1128 int i;
1129 const char *format = "%(objectname) %(objecttype)\t%(refname)";
1130 - struct ref_sort *sort = NULL, **sort_tail = &sort;
1130 + struct ref_sorting *sort = NULL, **sort_tail = &sort;
1131 int maxcount = 0, quote_style = 0;
1132 struct ref_filter_cbdata ref_cbdata;
1133
@@ -1145,7 +1145,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1145 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
1146 OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
1147 OPT_CALLBACK(0 , "sort", sort_tail, N_("key"),
1148 - N_("field name to sort on"), &opt_parse_sort),
1148 + N_("field name to sort on"), &parse_opt_ref_sorting),
1149 OPT_END(),
1150 };
1151
@@ -1158,25 +1158,25 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1158 error("more than one quoting style?");
1159 usage_with_options(for_each_ref_usage, opts);
1160 }
1161 - if (verify_format(format))
1161 + if (verify_ref_format(format))
1162 usage_with_options(for_each_ref_usage, opts);
1163
1164 if (!sort)
1165 - sort = default_sort();
1165 + sort = ref_default_sorting();
1166
1167 /* for warn_ambiguous_refs */
1168 git_config(git_default_config, NULL);
1169
1170 memset(&ref_cbdata, 0, sizeof(ref_cbdata));
1171 ref_cbdata.filter.name_patterns = argv;
1172 - for_each_rawref(grab_single_ref, &ref_cbdata);
1172 + for_each_rawref(ref_filter_handler, &ref_cbdata);
1173
1174 - sort_refs(sort, &ref_cbdata.array);
1174 + ref_array_sort(sort, &ref_cbdata.array);
1175
1176 if (!maxcount || ref_cbdata.array.nr < maxcount)
1177 maxcount = ref_cbdata.array.nr;
1178 for (i = 0; i < maxcount; i++)
1179 - show_ref(ref_cbdata.array.items[i], format, quote_style);
1179 + show_ref_array_item(ref_cbdata.array.items[i], format, quote_style);
1180 ref_array_clear(&ref_cbdata.array);
1181 return 0;
1182 }