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>
This commit is contained in:
Jeff King 2017-07-13 11:02:44 -04:00 коммит произвёл Junio C Hamano
Родитель bf285ae6db
Коммит 18a2565016
3 изменённых файлов: 11 добавлений и 30 удалений

Просмотреть файл

@ -136,30 +136,6 @@ static const char tag_template_nocleanup[] =
"Lines starting with '%c' will be kept; you may remove them"
" yourself if you want to.\n");
/* Parse arg given and add it the ref_sorting array */
static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
{
struct ref_sorting *s;
int len;
s = xcalloc(1, sizeof(*s));
s->next = *sorting_tail;
*sorting_tail = s;
if (*arg == '-') {
s->reverse = 1;
arg++;
}
if (skip_prefix(arg, "version:", &arg) ||
skip_prefix(arg, "v:", &arg))
s->version = 1;
len = strlen(arg);
s->atom = parse_ref_filter_atom(arg, arg+len);
return 0;
}
static int git_tag_config(const char *var, const char *value, void *cb)
{
int status;
@ -168,7 +144,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)
if (!strcmp(var, "tag.sort")) {
if (!value)
return config_error_nonbool(var);
parse_sorting_string(value, sorting_tail);
parse_ref_sorting(sorting_tail, value);
return 0;
}

Просмотреть файл

@ -2126,15 +2126,11 @@ struct ref_sorting *ref_default_sorting(void)
return sorting;
}
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
{
struct ref_sorting **sorting_tail = opt->value;
struct ref_sorting *s;
int len;
if (!arg) /* should --no-sort void the list ? */
return -1;
s = xcalloc(1, sizeof(*s));
s->next = *sorting_tail;
*sorting_tail = s;
@ -2148,6 +2144,13 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
s->version = 1;
len = strlen(arg);
s->atom = parse_ref_filter_atom(arg, arg+len);
}
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
{
if (!arg) /* should --no-sort void the list ? */
return -1;
parse_ref_sorting(opt->value, arg);
return 0;
}

Просмотреть файл

@ -116,6 +116,8 @@ void format_ref_array_item(struct ref_array_item *info,
struct strbuf *final_buf);
/* Print the ref using the given format and quote_style */
void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
/* Parse a single sort specifier and add it to the list */
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
/* Callback function for parsing the sort option */
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
/* Default sort option based on refname */