Skip to content

Commit 18a2565

Browse files
peffgitster
authored andcommitted
ref-filter: provide a function for parsing sort options
The ref-filter module currently provides a callback suitable for parsing command-line --sort options. But since git-tag also supports the tag.sort config option, it needs a function whose implementation is quite similar, but with a slightly different interface. The end result is that builtin/tag.c has a copy-paste of parse_opt_ref_sorting(). Instead, let's provide a function to parse an arbitrary sort string, which we can then trivially wrap to make the parse_opt variant. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf285ae commit 18a2565

File tree

3 files changed

+11
-30
lines changed

3 files changed

+11
-30
lines changed

builtin/tag.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,6 @@ static const char tag_template_nocleanup[] =
136136
"Lines starting with '%c' will be kept; you may remove them"
137137
" yourself if you want to.\n");
138138

139-
/* Parse arg given and add it the ref_sorting array */
140-
static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
141-
{
142-
struct ref_sorting *s;
143-
int len;
144-
145-
s = xcalloc(1, sizeof(*s));
146-
s->next = *sorting_tail;
147-
*sorting_tail = s;
148-
149-
if (*arg == '-') {
150-
s->reverse = 1;
151-
arg++;
152-
}
153-
if (skip_prefix(arg, "version:", &arg) ||
154-
skip_prefix(arg, "v:", &arg))
155-
s->version = 1;
156-
157-
len = strlen(arg);
158-
s->atom = parse_ref_filter_atom(arg, arg+len);
159-
160-
return 0;
161-
}
162-
163139
static int git_tag_config(const char *var, const char *value, void *cb)
164140
{
165141
int status;
@@ -168,7 +144,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
168144
if (!strcmp(var, "tag.sort")) {
169145
if (!value)
170146
return config_error_nonbool(var);
171-
parse_sorting_string(value, sorting_tail);
147+
parse_ref_sorting(sorting_tail, value);
172148
return 0;
173149
}
174150

ref-filter.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,15 +2126,11 @@ struct ref_sorting *ref_default_sorting(void)
21262126
return sorting;
21272127
}
21282128

2129-
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2129+
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
21302130
{
2131-
struct ref_sorting **sorting_tail = opt->value;
21322131
struct ref_sorting *s;
21332132
int len;
21342133

2135-
if (!arg) /* should --no-sort void the list ? */
2136-
return -1;
2137-
21382134
s = xcalloc(1, sizeof(*s));
21392135
s->next = *sorting_tail;
21402136
*sorting_tail = s;
@@ -2148,6 +2144,13 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
21482144
s->version = 1;
21492145
len = strlen(arg);
21502146
s->atom = parse_ref_filter_atom(arg, arg+len);
2147+
}
2148+
2149+
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2150+
{
2151+
if (!arg) /* should --no-sort void the list ? */
2152+
return -1;
2153+
parse_ref_sorting(opt->value, arg);
21512154
return 0;
21522155
}
21532156

ref-filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ void format_ref_array_item(struct ref_array_item *info,
116116
struct strbuf *final_buf);
117117
/* Print the ref using the given format and quote_style */
118118
void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
119+
/* Parse a single sort specifier and add it to the list */
120+
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
119121
/* Callback function for parsing the sort option */
120122
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
121123
/* Default sort option based on refname */

0 commit comments

Comments
 (0)