A bit of ref-filter atom parsing cleanups
cover A bit of ref-filter atom parsing cleanups
The diffstat of the second patch doesn't show any benefits, but only because the first patch removed a callsite that would have benefited from it.
It merges cleanly with Karthik's "port branch.c to use ref-filter's printing options" series.
SZEDER Gábor (2):
ref-filter, tag: eliminate duplicated sorting option parsing
ref-filter: add function to parse atoms from a nul-terminated string
builtin/tag.c | 24 ------------------------ ref-filter.c | 24 +++++++++++++----------- ref-filter.h | 6 ++++++ 3 files changed, 19 insertions(+), 35 deletions(-)
Signature · 1 line
2.11.0.78.g5a2d011
PATCH 1/2 ref-filter, tag: eliminate duplicated sorting option parsing
Sorting options are either specified on the command line, which is handled by parse_opt_ref_sorting() in ref-filter.c, or via the 'tag.sort' config variable, which is handled by parse_sorting_string() in builtin/tag.c. These two functions are nearly identical, the difference being only their signature and the former having a couple of extra lines at the beginning.
Eliminate the code duplication by making parse_sorting_string() part of the public ref-filter API, and turning parse_opt_ref_sorting() into a thin wrapper around that function. This way builtin/tag.c can continue using it as before (and it might be useful if there ever will be a 'branch.sort' config variable). Change its return type from int to void, because it always returned zero and none of its callers cared about it (the actual error handling is done inside parse_ref_filter_atom() by die()ing on error).
--- builtin/tag.c | 24 ------------------------ ref-filter.c | 16 +++++++++++----- ref-filter.h | 2 ++ 3 files changed, 13 insertions(+), 29 deletions(-)
builtin/tag.c +0 -24
@@ -122,30 +122,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;
ref-filter.c +11 -5
@@ -1667,15 +1667,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_sorting_string (const char *arg,struct ref_sorting **sorting_tail) { -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;
@@ -1689,6 +1685,16 @@ 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) +{ +struct ref_sorting **sorting_tail = opt->value; + +if (!arg)/* should --no-sort void the list ? */ +return -1 ; + +parse_sorting_string (arg, sorting_tail);return 0 ; }
ref-filter.h +2 -0
@@ -100,6 +100,8 @@ int verify_ref_format(const char *format);void ref_array_sort (struct ref_sorting *sort,struct ref_array *array);/* Print the ref using the given format and quote_style */ void show_ref_array_item (struct ref_array_item *info,const char *format,int quote_style); +/* Parse given arg and append it to the ref_sorting list */ +void parse_sorting_string (const char *arg,struct ref_sorting **sorting_tail);/* 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 */
Signature · 1 line
2.11.0.78.g5a2d011
PATCH 2/2 ref-filter: add function to parse atoms from a nul-terminated string
In reply to SZEDER Gáborref-filter's parse_ref_filter_atom() function parses an atom between the start and end pointers it gets as arguments. This is fine for two of its callers, which process '%(atom)' format specifiers and the end pointer comes directly from strchr() looking for the closing ')'. However, it's not quite so straightforward for its other two callers, which process sort specifiers given as plain nul-terminated strings. Especially not for ref_default_sorting(), which has the default hard-coded as a string literal, but can't use it directly, because a pointer to the end of that string literal is needed as well. The next patch will add yet another caller using a string literal.
Add a helper function around parse_ref_filter_atom() to parse an atom up to the terminating nul, and call this helper in those two callers.
--- ref-filter.c | 8 ++------ ref-filter.h | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-)
ref-filter.c +2 -6
@@ -1658,19 +1658,16 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu/* If no sorting option is given, use refname to sort as default */ struct ref_sorting *ref_default_sorting (void ) { -static const char cstr_name[] ="refname" ; -struct ref_sorting *sorting =xcalloc (1 ,sizeof (*sorting)); sorting->next =NULL ; - sorting->atom =parse_ref_filter_atom (cstr_name, cstr_name +strlen (cstr_name)); + sorting->atom =parse_ref_filter_atom_from_string ("refname" );return sorting; }void 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;
@@ -1683,8 +1680,7 @@ void parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)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); + s->atom =parse_ref_filter_atom_from_string (arg); }int parse_opt_ref_sorting (const struct option *opt,const char *arg,int unset)
ref-filter.h +4 -0
@@ -94,6 +94,10 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned intvoid ref_array_clear (struct ref_array *array);/* Parse format string and sort specifiers */ int parse_ref_filter_atom (const char *atom,const char *ep); +static inline int parse_ref_filter_atom_from_string (const char *atom) +{ +return parse_ref_filter_atom (atom, atom+strlen (atom)); +}/* Used to verify if the given format is correct and to parse out the used atoms */ int verify_ref_format (const char *format);/* Sort the given ref_array as per the ref_sorting provided */
Signature · 1 line
2.11.0.78.g5a2d011
Re: [PATCH 2/2] ref-filter: add function to parse atoms from a nul-terminated string
Quoted 10 lines · SZEDER Gábor
ref-filter's
parse_ref_filter_atom()function parses an atom between the start and end pointers it gets as arguments. This is fine for two of its callers, which process '%(atom)' format specifiers and the end pointer comes directly fromstrchr()looking for the closing ')'. However, it's not quite so straightforward for its other two callers, which process sort specifiers given as plain nul-terminated strings. Especially not forref_default_sorting(), which has the default hard-coded as a string literal, but can't use it directly, because a pointer to the end of that string literal is needed as well. The next patch will add yet another caller using a string literal.
Oops, that last sentence should be deleted, there is no third patch, sorry.
Gábor
Re: [PATCH 2/2] ref-filter: add function to parse atoms from a nul-terminated string
In reply to SZEDER GáborQuoted 11 lines · SZEDER Gábor
ref-filter's
parse_ref_filter_atom()function parses an atom between the start and end pointers it gets as arguments. This is fine for two of its callers, which process '%(atom)' format specifiers and the end pointer comes directly fromstrchr()looking for the closing ')'. However, it's not quite so straightforward for its other two callers, which process sort specifiers given as plain nul-terminated strings. Especially not forref_default_sorting(), which has the default hard-coded as a string literal, but can't use it directly, because a pointer to the end of that string literal is needed as well. The next patch will add yet another caller using a string literal.
Quoted 8 lines
Add a helper function around
parse_ref_filter_atom()to parse an atom up to the terminating nul, and call this helper in those two callers.Signed-off-by SZEDER Gábor <[email protected]>--- ref-filter.c | 8 ++------ ref-filter.h | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-)
Ping?
It looks like that this little two piece cleanup series fell on the floor.
Quoted 53 lines
ref-filter.c +2
-6@@ -1658,19 +1658,16 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu/* If no sorting option is given, use refname to sort as default */ struct ref_sorting *ref_default_sorting (void ) { -static const char cstr_name[] ="refname" ; -struct ref_sorting *sorting =xcalloc (1 ,sizeof (*sorting)); sorting->next =NULL ; - sorting->atom =parse_ref_filter_atom (cstr_name, cstr_name +strlen (cstr_name)); + sorting->atom =parse_ref_filter_atom_from_string ("refname" );return sorting; }void 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;@@ -1683,8 +1680,7 @@ void parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)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); + s->atom =parse_ref_filter_atom_from_string (arg); }int parse_opt_ref_sorting (const struct option *opt,const char *arg,int unset)ref-filter.h +4
-1@@ -94,6 +94,10 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned intvoid ref_array_clear (struct ref_array *array);/* Parse format string and sort specifiers */ int parse_ref_filter_atom (const char *atom,const char *ep); +static inline int parse_ref_filter_atom_from_string (const char *atom) +{ +return parse_ref_filter_atom (atom, atom+strlen (atom)); +}/* Used to verify if the given format is correct and to parse out the used atoms */ int verify_ref_format (const char *format);/* Sort the given ref_array as per the ref_sorting provided */ --2.11.0.78.g5a2d011
Re: [PATCH 2/2] ref-filter: add function to parse atoms from a nul-terminated string
Quoted 21 lines · SZEDER Gábor
Quoted 11 lines · SZEDER Gábor
ref-filter's
parse_ref_filter_atom()function parses an atom between the start and end pointers it gets as arguments. This is fine for two of its callers, which process '%(atom)' format specifiers and the end pointer comes directly fromstrchr()looking for the closing ')'. However, it's not quite so straightforward for its other two callers, which process sort specifiers given as plain nul-terminated strings. Especially not forref_default_sorting(), which has the default hard-coded as a string literal, but can't use it directly, because a pointer to the end of that string literal is needed as well. The next patch will add yet another caller using a string literal.Quoted 8 lines
Add a helper function around
parse_ref_filter_atom()to parse an atom up to the terminating nul, and call this helper in those two callers.Signed-off-by SZEDER Gábor <[email protected]>--- ref-filter.c | 8 ++------ ref-filter.h | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-)
Quoted 3 lines
Ping?
It looks like that this little two piece cleanup series fell on the floor.
I am still waiting for somebody else to comment on the changes, and the final reroll after such a review discussion to address your own comment in <CAM0VKjk1mnNzQX6LThq1t7keesBz_fjE9x2e0ywsBKSNKP9SCw@mail.gmail.com>