Subject: [PATCH 1/2] ref-filter, tag: eliminate duplicated sorting option parsing

From: SZEDER Gábor <[email-masked]>
Date: Wed, 7 Dec 2016 17:09:22 +0100

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).

Signed-off-by: SZEDER Gábor <[email-masked]>
---
 builtin/tag.c | 24 ------------------------
 ref-filter.c  | 16 +++++++++++-----
 ref-filter.h  |  2 ++
 3 files changed, 13 insertions(+), 29 deletions(-)

diff --git a/builtin/tag.c b/builtin/tag.c
index 50e4ae567..6fe723bee 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -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;
diff --git a/ref-filter.c b/ref-filter.c
index bc551a752..dfadf577c 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -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;
 }
 
diff --git a/ref-filter.h b/ref-filter.h
index 14d435e2c..49466a17d 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -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 */
-- 
2.11.0.78.g5a2d011