Skip to content

Commit

Permalink
ref-filter: provide a function for parsing sort options
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
peff authored and gitster committed Jul 13, 2017
1 parent bf285ae commit 18a2565
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
26 changes: 1 addition & 25 deletions builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
13 changes: 8 additions & 5 deletions ref-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions ref-filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 18a2565

Please sign in to comment.