Skip to content

Commit 277b915

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> 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 8e33678 commit 277b915

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

builtin/for-each-ref.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static int need_color_reset_at_eol;
113113
/*
114114
* Used to parse format string and sort specifiers
115115
*/
116-
static int parse_atom(const char *atom, const char *ep)
116+
int parse_ref_filter_atom(const char *atom, const char *ep)
117117
{
118118
const char *sp;
119119
int i, at;
@@ -190,7 +190,7 @@ static const char *find_next(const char *cp)
190190
* Make sure the format string is well formed, and parse out
191191
* the used atoms.
192192
*/
193-
static int verify_format(const char *format)
193+
int verify_ref_format(const char *format)
194194
{
195195
const char *cp, *sp;
196196

@@ -202,7 +202,7 @@ static int verify_format(const char *format)
202202
if (!ep)
203203
return error("malformed format string %s", sp);
204204
/* 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);
206206
cp = ep + 1;
207207

208208
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
409409
/*
410410
* We got here because atomname ends in "date" or "date<something>";
411411
* 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
413413
* ":" means no format is specified, and use the default.
414414
*/
415415
formatp = strchr(atomname, ':');
@@ -849,7 +849,7 @@ static void populate_value(struct ref_array_item *ref)
849849
* Given a ref, return the value for the atom. This lazily gets value
850850
* out of the object by calling populate value.
851851
*/
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)
853853
{
854854
if (!ref->value) {
855855
populate_value(ref);
@@ -900,8 +900,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
900900
* A call-back given to for_each_ref(). Filter refs and keep them for
901901
* later object processing.
902902
*/
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)
905904
{
906905
struct ref_filter_cbdata *ref_cbdata = cb_data;
907906
struct ref_filter *filter = &ref_cbdata->filter;
@@ -947,14 +946,14 @@ void ref_array_clear(struct ref_array *array)
947946
array->nr = array->alloc = 0;
948947
}
949948

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)
951950
{
952951
struct atom_value *va, *vb;
953952
int cmp;
954953
cmp_type cmp_type = used_atom_type[s->atom];
955954

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);
958957
switch (cmp_type) {
959958
case FIELD_STR:
960959
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
971970
return (s->reverse) ? -cmp : cmp;
972971
}
973972

974-
static struct ref_sort *ref_sort;
973+
static struct ref_sorting *ref_sorting;
975974
static int compare_refs(const void *a_, const void *b_)
976975
{
977976
struct ref_array_item *a = *((struct ref_array_item **)a_);
978977
struct ref_array_item *b = *((struct ref_array_item **)b_);
979-
struct ref_sort *s;
978+
struct ref_sorting *s;
980979

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);
983982
if (cmp)
984983
return cmp;
985984
}
986985
return 0;
987986
}
988987

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)
990989
{
991-
ref_sort = sort;
990+
ref_sorting = sort;
992991
qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
993992
}
994993

@@ -1056,7 +1055,7 @@ static void emit(const char *cp, const char *ep)
10561055
}
10571056
}
10581057

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)
10601059
{
10611060
const char *cp, *sp, *ep;
10621061

@@ -1066,7 +1065,7 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_
10661065
ep = strchr(sp, ')');
10671066
if (cp < sp)
10681067
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);
10701069
print_value(atomv, quote_style);
10711070
}
10721071
if (*cp) {
@@ -1085,21 +1084,22 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_
10851084
putchar('\n');
10861085
}
10871086

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)
10891089
{
10901090
static const char cstr_name[] = "refname";
10911091

1092-
struct ref_sort *sort = xcalloc(1, sizeof(*sort));
1092+
struct ref_sorting *sort = xcalloc(1, sizeof(*sort));
10931093

10941094
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));
10961096
return sort;
10971097
}
10981098

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)
11001100
{
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;
11031103
int len;
11041104

11051105
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)
11141114
arg++;
11151115
}
11161116
len = strlen(arg);
1117-
s->atom = parse_atom(arg, arg+len);
1117+
s->atom = parse_ref_filter_atom(arg, arg+len);
11181118
return 0;
11191119
}
11201120

@@ -1127,7 +1127,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
11271127
{
11281128
int i;
11291129
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;
11311131
int maxcount = 0, quote_style = 0;
11321132
struct ref_filter_cbdata ref_cbdata;
11331133

@@ -1145,7 +1145,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
11451145
OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
11461146
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
11471147
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),
11491149
OPT_END(),
11501150
};
11511151

@@ -1158,25 +1158,25 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
11581158
error("more than one quoting style?");
11591159
usage_with_options(for_each_ref_usage, opts);
11601160
}
1161-
if (verify_format(format))
1161+
if (verify_ref_format(format))
11621162
usage_with_options(for_each_ref_usage, opts);
11631163

11641164
if (!sort)
1165-
sort = default_sort();
1165+
sort = ref_default_sorting();
11661166

11671167
/* for warn_ambiguous_refs */
11681168
git_config(git_default_config, NULL);
11691169

11701170
memset(&ref_cbdata, 0, sizeof(ref_cbdata));
11711171
ref_cbdata.filter.name_patterns = argv;
1172-
for_each_rawref(grab_single_ref, &ref_cbdata);
1172+
for_each_rawref(ref_filter_handler, &ref_cbdata);
11731173

1174-
sort_refs(sort, &ref_cbdata.array);
1174+
ref_array_sort(sort, &ref_cbdata.array);
11751175

11761176
if (!maxcount || ref_cbdata.array.nr < maxcount)
11771177
maxcount = ref_cbdata.array.nr;
11781178
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);
11801180
ref_array_clear(&ref_cbdata.array);
11811181
return 0;
11821182
}

0 commit comments

Comments
 (0)