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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 13, 2017 at 11:02 UTC 18a2565016d45538345a06cd9b912040b5581fc1
3 files changed +11 -30
builtin/tag.c
+1 -25
@@ -136,30 +136,6 @@ static const char tag_template_nocleanup[] =
136 "Lines starting with '%c' will be kept; you may remove them"
137 " yourself if you want to.\n");
138
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 -
139 static int git_tag_config(const char *var, const char *value, void *cb)
140 {
141 int status;
@@ -168,7 +144,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
144 if (!strcmp(var, "tag.sort")) {
145 if (!value)
146 return config_error_nonbool(var);
171 - parse_sorting_string(value, sorting_tail);
147 + parse_ref_sorting(sorting_tail, value);
148 return 0;
149 }
150
ref-filter.c
+8 -5
@@ -2126,15 +2126,11 @@ struct ref_sorting *ref_default_sorting(void)
2126 return sorting;
2127 }
2128
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)
2130 {
2131 - struct ref_sorting **sorting_tail = opt->value;
2131 struct ref_sorting *s;
2132 int len;
2133
2135 - if (!arg) /* should --no-sort void the list ? */
2136 - return -1;
2137 -
2134 s = xcalloc(1, sizeof(*s));
2135 s->next = *sorting_tail;
2136 *sorting_tail = s;
@@ -2148,6 +2144,13 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2144 s->version = 1;
2145 len = strlen(arg);
2146 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);
2154 return 0;
2155 }
2156
ref-filter.h
+2
@@ -116,6 +116,8 @@ void format_ref_array_item(struct ref_array_item *info,
116 struct strbuf *final_buf);
117 /* Print the ref using the given format and quote_style */
118 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);
121 /* Callback function for parsing the sort option */
122 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
123 /* Default sort option based on refname */