git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] for-each-ref: delay parsing of --sort=<atom> options
@ 2021-10-18 18:32 Junio C Hamano
  2021-10-19 22:18 ` Jeff King
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2021-10-18 18:32 UTC (permalink / raw)
  To: git

The for-each-ref family of commands invoke parsers immediately when
it sees each --sort=<atom> option, and die before even seeing the
other options on the command line when the <atom> is unrecognised.

Instead, accumulate them in a string list, and have them parsed into
a ref_sorting structure after the command line parsing is done.  As
a consequence, "git branch --sort=bogus -h" used to fail to give the
brief help, which arguably may have been a feature, now does so,
which is more consistent with how other options work.

The patch is smaller than the actual extent of the "damage" to the
codebase, thanks to the fact that the original code consistently
used OPT_REF_SORT() macro to handle command line options.  We only
needed to replace the variable used for the list, and implementation
of the callback function used in the macro.

The old rule was for the users of the API to:

 - Declare ref_sorting and ref_sorting_tail variables;

 - OPT_REF_SORT() macro will instantiate ref_sorting instance (which
   may barf and die) and append it to the tail;

 - Append to the tail each ref_sorting read from the configuration
   by parsing in the config callback (which may barf and die);

 - See if ref_sorting is null and use ref_sorting_default() instead.

Now the rule is not all that different but is simpler:

 - Declare ref_sorting_options string list.

 - OPT_REF_SORT() macro will append it to the string list;

 - Append to the string list the sort key read from the
   configuration;

 - call ref_sorting_options() to turn the string list to ref_sorting
   structure (which also deals with the default value).

As side effects, this change also cleans up a few issues:

 - 95be717c (parse_opt_ref_sorting: always use with NONEG flag,
   2019-03-20) muses that "git for-each-ref --no-sort" should simply
   clear the sort keys accumulated so far; it now does.

 - The implementation detail of "struct ref_sorting" and the helper
   function parse_ref_sorting() can now be private to the ref-filter
   API implementation.

 - If you set branch.sort to a bogus value, the any "git branch"
   invocation, not only the listing mode, would abort with the
   original code; now it doesn't

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/branch.c        | 13 ++++++-------
 builtin/for-each-ref.c  |  8 ++++----
 builtin/ls-remote.c     | 13 ++++++++-----
 builtin/tag.c           | 13 ++++++-------
 ref-filter.c            | 34 ++++++++++++++++++++++++----------
 ref-filter.h            | 28 ++++++++++------------------
 t/t3200-branch.sh       | 12 +++++++++++-
 t/t6300-for-each-ref.sh |  5 +++++
 8 files changed, 74 insertions(+), 52 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 0b7ed82654..69211f8dd1 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -77,12 +77,11 @@ define_list_config_array(color_branch_slots);
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
 	const char *slot_name;
-	struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
 
 	if (!strcmp(var, "branch.sort")) {
 		if (!value)
 			return config_error_nonbool(var);
-		parse_ref_sorting(sorting_tail, value);
+		string_list_append(cb, value);
 		return 0;
 	}
 
@@ -624,7 +623,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	int icase = 0;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	static struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 
 	struct option options[] = {
@@ -665,7 +665,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_MERGED(&filter, N_("print only branches that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
 		OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
 			N_("print only branches of the object"), parse_opt_object_name),
 		OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
@@ -682,7 +682,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage_with_options(builtin_branch_usage, options);
 
-	git_config(git_branch_config, sorting_tail);
+	git_config(git_branch_config, &sorting_options);
 
 	track = git_branch_track;
 
@@ -748,8 +748,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		 * local branches 'refs/heads/...' and finally remote-tracking
 		 * branches 'refs/remotes/...'.
 		 */
-		if (!sorting)
-			sorting = ref_default_sorting();
+		sorting = ref_sorting_options(&sorting_options);
 		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 		ref_sorting_set_sort_flags_all(
 			sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 642b4b888f..75d277ed60 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -17,7 +17,8 @@ static char const * const for_each_ref_usage[] = {
 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 {
 	int i;
-	struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	int maxcount = 0, icase = 0;
 	struct ref_array array;
 	struct ref_filter filter;
@@ -39,7 +40,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
 		OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
 		OPT__COLOR(&format.use_color, N_("respect format colors")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_CALLBACK(0, "points-at", &filter.points_at,
 			     N_("object"), N_("print only refs which points at the given object"),
 			     parse_opt_object_name),
@@ -70,8 +71,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	if (verify_ref_format(&format))
 		usage_with_options(for_each_ref_usage, opts);
 
-	if (!sorting)
-		sorting = ref_default_sorting();
+	sorting = ref_sorting_options(&sorting_options);
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 318949c3d7..1e6017cdaa 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -54,7 +54,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	struct transport *transport;
 	const struct ref *ref;
 	struct ref_array ref_array;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("do not print remote URL")),
@@ -68,7 +68,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
 		OPT_BOOL(0, "get-url", &get_url,
 			 N_("take url.<base>.insteadOf into account")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_SET_INT_F(0, "exit-code", &status,
 			      N_("exit with exit code 2 if no matching refs are found"),
 			      2, PARSE_OPT_NOCOMPLETE),
@@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	packet_trace_identity("ls-remote");
 
-	UNLEAK(sorting);
-
 	if (argc > 1) {
 		int i;
 		CALLOC_ARRAY(pattern, argc);
@@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		item->symref = xstrdup_or_null(ref->symref);
 	}
 
-	if (sorting)
+	if (sorting_options.nr) {
+		struct ref_sorting *sorting;
+		UNLEAK(sorting);
+
+		sorting = ref_sorting_options(&sorting_options);
 		ref_array_sort(sorting, &ref_array);
+	}
 
 	for (i = 0; i < ref_array.nr; i++) {
 		const struct ref_array_item *ref = ref_array.items[i];
diff --git a/builtin/tag.c b/builtin/tag.c
index 6535ed27ee..7b03cddd29 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -178,7 +178,6 @@ static const char tag_template_nocleanup[] =
 static int git_tag_config(const char *var, const char *value, void *cb)
 {
 	int status;
-	struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
 
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -188,7 +187,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_ref_sorting(sorting_tail, value);
+		string_list_append(cb, value);
 		return 0;
 	}
 
@@ -436,7 +435,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 	int icase = 0;
 	int edit_flag = 0;
@@ -470,7 +470,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
 		OPT_MERGED(&filter, N_("print only tags that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		{
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
@@ -485,7 +485,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	setup_ref_filter_porcelain_msg();
 
-	git_config(git_tag_config, sorting_tail);
+	git_config(git_tag_config, &sorting_options);
 
 	memset(&opt, 0, sizeof(opt));
 	memset(&filter, 0, sizeof(filter));
@@ -524,8 +524,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			die(_("--column and -n are incompatible"));
 		colopts = 0;
 	}
-	if (!sorting)
-		sorting = ref_default_sorting();
+	sorting = ref_sorting_options(&sorting_options);
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
diff --git a/ref-filter.c b/ref-filter.c
index add429be79..fc80abd432 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2470,6 +2470,12 @@ static int memcasecmp(const void *vs1, const void *vs2, size_t n)
 	return 0;
 }
 
+struct ref_sorting {
+	struct ref_sorting *next;
+	int atom; /* index into used_atom array (internal) */
+	enum ref_sorting_order sort_flags;
+};
+
 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
 {
 	struct atom_value *va, *vb;
@@ -2663,7 +2669,7 @@ static int parse_sorting_atom(const char *atom)
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-struct ref_sorting *ref_default_sorting(void)
+static struct ref_sorting *ref_default_sorting(void)
 {
 	static const char cstr_name[] = "refname";
 
@@ -2674,7 +2680,7 @@ struct ref_sorting *ref_default_sorting(void)
 	return sorting;
 }
 
-void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
+static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
 {
 	struct ref_sorting *s;
 
@@ -2692,17 +2698,25 @@ void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
 	s->atom = parse_sorting_atom(arg);
 }
 
-int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
+struct ref_sorting *ref_sorting_options(struct string_list *options)
 {
+	struct string_list_item *item;
+	struct ref_sorting *sorting = NULL, **tail = &sorting;
+
+	if (!options->nr) {
+		sorting = ref_default_sorting();
+	} else {
+		for_each_string_list_item(item, options)
+			parse_ref_sorting(tail, item->string);
+	}
+
 	/*
-	 * NEEDSWORK: We should probably clear the list in this case, but we've
-	 * already munged the global used_atoms list, which would need to be
-	 * undone.
+	 * From here on, the ref_sorting list should be used to talk
+	 * about the sort order used for the output.  The caller
+	 * should not touch the string form anymore.
 	 */
-	BUG_ON_OPT_NEG(unset);
-
-	parse_ref_sorting(opt->value, arg);
-	return 0;
+	string_list_clear(options, 0);
+	return sorting;
 }
 
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
diff --git a/ref-filter.h b/ref-filter.h
index b636f4389d..706c90a18f 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -23,16 +23,13 @@
 #define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
 struct atom_value;
+struct ref_sorting;
 
-struct ref_sorting {
-	struct ref_sorting *next;
-	int atom; /* index into used_atom array (internal) */
-	enum {
-		REF_SORTING_REVERSE = 1<<0,
-		REF_SORTING_ICASE = 1<<1,
-		REF_SORTING_VERSION = 1<<2,
-		REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
-	} sort_flags;
+enum ref_sorting_order {
+	REF_SORTING_REVERSE = 1<<0,
+	REF_SORTING_ICASE = 1<<1,
+	REF_SORTING_VERSION = 1<<2,
+	REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
 };
 
 struct ref_array_item {
@@ -97,9 +94,8 @@ struct ref_format {
 #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
 
 #define OPT_REF_SORT(var) \
-	OPT_CALLBACK_F(0, "sort", (var), \
-		       N_("key"), N_("field name to sort on"), \
-		       PARSE_OPT_NONEG, parse_opt_ref_sorting)
+	OPT_STRING_LIST(0, "sort", (var), \
+			N_("key"), N_("field name to sort on"))
 
 /*
  * API for filtering a set of refs. Based on the type of refs the user
@@ -121,12 +117,8 @@ int format_ref_array_item(struct ref_array_item *info,
 			  struct ref_format *format,
 			  struct strbuf *final_buf,
 			  struct strbuf *error_buf);
-/*  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 */
-struct ref_sorting *ref_default_sorting(void);
+/*  Convert list of sort options into ref_sorting */
+struct ref_sorting *ref_sorting_options(struct string_list *);
 /*  Function to parse --merged and --no-merged options */
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
 /*  Get the current HEAD's description */
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index e575ffb4ff..0e23d18807 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1418,7 +1418,17 @@ test_expect_success 'invalid sort parameter in configuration' '
 	(
 		cd sort &&
 		git config branch.sort "v:notvalid" &&
-		test_must_fail git branch
+
+		# this works in the "listing" mode, so bad sort key
+		# is a dying offence.
+		test_must_fail git branch &&
+
+		# these do not need to use sorting, and should all
+		# succeed
+		git branch newone main &&
+		git branch -c newone newerone &&
+		git branch -m newone newestone &&
+		git branch -d newerone newestone
 	)
 '
 
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 80679d5e12..83921502a4 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -419,6 +419,11 @@ test_expect_success 'Verify descending sort' '
 	test_cmp expected actual
 '
 
+test_expect_success 'Give help even with invalid sort atoms' '
+	test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
+	grep "^usage: git for-each-ref" actual
+'
+
 cat >expected <<\EOF
 refs/tags/testtag
 refs/tags/testtag-2
-- 
2.33.1-878-gcde26e7e77


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-18 18:32 [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
@ 2021-10-19 22:18 ` Jeff King
  2021-10-20  2:20   ` Jeff King
  2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
  0 siblings, 2 replies; 20+ messages in thread
From: Jeff King @ 2021-10-19 22:18 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Oct 18, 2021 at 11:32:38AM -0700, Junio C Hamano wrote:

> The for-each-ref family of commands invoke parsers immediately when
> it sees each --sort=<atom> option, and die before even seeing the
> other options on the command line when the <atom> is unrecognised.
> 
> Instead, accumulate them in a string list, and have them parsed into
> a ref_sorting structure after the command line parsing is done.  As
> a consequence, "git branch --sort=bogus -h" used to fail to give the
> brief help, which arguably may have been a feature, now does so,
> which is more consistent with how other options work.
> [...]

Very nicely explained. As you often say, I wish all patches came with a
commit message like this. :)

> As side effects, this change also cleans up a few issues:
> 
>  - 95be717c (parse_opt_ref_sorting: always use with NONEG flag,
>    2019-03-20) muses that "git for-each-ref --no-sort" should simply
>    clear the sort keys accumulated so far; it now does.

Neat. Is it worth adding a test here?

>  - The implementation detail of "struct ref_sorting" and the helper
>    function parse_ref_sorting() can now be private to the ref-filter
>    API implementation.

Yes, I think this makes the API overall cleaner. Including the fact that
ref_sorting_default() is now handled internally.

>  - If you set branch.sort to a bogus value, the any "git branch"
>    invocation, not only the listing mode, would abort with the
>    original code; now it doesn't

And the new tests nicely cover these cases.

>  builtin/branch.c        | 13 ++++++-------
>  builtin/for-each-ref.c  |  8 ++++----
>  builtin/ls-remote.c     | 13 ++++++++-----
>  builtin/tag.c           | 13 ++++++-------
>  ref-filter.c            | 34 ++++++++++++++++++++++++----------
>  ref-filter.h            | 28 ++++++++++------------------
>  t/t3200-branch.sh       | 12 +++++++++++-
>  t/t6300-for-each-ref.sh |  5 +++++
>  8 files changed, 74 insertions(+), 52 deletions(-)

The patch overall looks good to me. Just a few observations:

> @@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  
>  	packet_trace_identity("ls-remote");
>  
> -	UNLEAK(sorting);
> -
>  	if (argc > 1) {
>  		int i;
>  		CALLOC_ARRAY(pattern, argc);
> @@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  		item->symref = xstrdup_or_null(ref->symref);
>  	}
>  
> -	if (sorting)
> +	if (sorting_options.nr) {
> +		struct ref_sorting *sorting;
> +		UNLEAK(sorting);
> +
> +		sorting = ref_sorting_options(&sorting_options);
>  		ref_array_sort(sorting, &ref_array);
> +	}

I wondered at first about pulling this UNLEAK() down, but it's because
you move the "sorting" variable itself into the smaller scope. So this
makes sense (and calling UNLEAK() before the pointer is set is perfectly
fine, since it takes the address of the auto variable). It is a shame
you can't just ref_sorting_free() afterwards, but we don't have that
function yet. And adding it is way out of scope here. :)

I do find it interesting that this case checks sorting_options.nr
itself, rather than relying on ref_sorting_options() to give us the
default. But that's because the existing code avoids sorting at all in
that case, so this is staying faithful to the original.

> -int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
> +struct ref_sorting *ref_sorting_options(struct string_list *options)
>  {
> +	struct string_list_item *item;
> +	struct ref_sorting *sorting = NULL, **tail = &sorting;
> +
> +	if (!options->nr) {
> +		sorting = ref_default_sorting();
> +	} else {
> +		for_each_string_list_item(item, options)
> +			parse_ref_sorting(tail, item->string);
> +	}
> +
>  	/*
> -	 * NEEDSWORK: We should probably clear the list in this case, but we've
> -	 * already munged the global used_atoms list, which would need to be
> -	 * undone.
> +	 * From here on, the ref_sorting list should be used to talk
> +	 * about the sort order used for the output.  The caller
> +	 * should not touch the string form anymore.
>  	 */
> -	BUG_ON_OPT_NEG(unset);
> -
> -	parse_ref_sorting(opt->value, arg);
> -	return 0;
> +	string_list_clear(options, 0);
> +	return sorting;
>  }

This interface seems pretty sensible overall. A few idle thoughts:

  - I'd probably have kept the word "parse" somewhere in the name, since
    it really is turning the user-provided text into our internal form

  - using a string_list is probably reasonable. We don't care about the
    util field here. Taking a "const char **" with NULL-termination or a
    number would be more general (and the callers could use a strvec to
    collect it). But I doubt any of the callers would ever be care about
    the distinction.

  - clearing the list at the end feels a little funny to me, just
    because this is conceptually a read-only operation (parse the user's
    text into our internal format). Your comment tells me what you're
    trying to protect against, but I find it unlikely anybody would
    mis-use the string_list afterwards (it doesn't do anything itself
    unless you parse it into the ref_sorting struct).

    All of the current callers are happy with this (and it even saves
    them clearing it themselves), but it just feels like an unusual
    interface.

> -struct ref_sorting {
> -	struct ref_sorting *next;
> -	int atom; /* index into used_atom array (internal) */
> -	enum {
> -		REF_SORTING_REVERSE = 1<<0,
> -		REF_SORTING_ICASE = 1<<1,
> -		REF_SORTING_VERSION = 1<<2,
> -		REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
> -	} sort_flags;
> +enum ref_sorting_order {
> +	REF_SORTING_REVERSE = 1<<0,
> +	REF_SORTING_ICASE = 1<<1,
> +	REF_SORTING_VERSION = 1<<2,
> +	REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
>  };

OK, so we have to leave this order enum public because some of the
callers use it with ref_sorting_set_sort_flags_all(). I was confused
that it never had a name before, but it looks like we just passed it as
an "unsigned int".

We could change that now that it has a name, but it's not a big deal
either way (and certainly a separate patch if we do).

> @@ -97,9 +94,8 @@ struct ref_format {
>  #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
>  
>  #define OPT_REF_SORT(var) \
> -	OPT_CALLBACK_F(0, "sort", (var), \
> -		       N_("key"), N_("field name to sort on"), \
> -		       PARSE_OPT_NONEG, parse_opt_ref_sorting)
> +	OPT_STRING_LIST(0, "sort", (var), \
> +			N_("key"), N_("field name to sort on"))

Oh, this part makes using a string_list more appealing. ;)

> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index e575ffb4ff..0e23d18807 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -1418,7 +1418,17 @@ test_expect_success 'invalid sort parameter in configuration' '
>  	(
>  		cd sort &&
>  		git config branch.sort "v:notvalid" &&
> -		test_must_fail git branch
> +
> +		# this works in the "listing" mode, so bad sort key
> +		# is a dying offence.
> +		test_must_fail git branch &&
> +
> +		# these do not need to use sorting, and should all
> +		# succeed
> +		git branch newone main &&
> +		git branch -c newone newerone &&
> +		git branch -m newone newestone &&
> +		git branch -d newerone newestone
>  	)
>  '

And this test looks very nice.

-Peff

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-19 22:18 ` Jeff King
@ 2021-10-20  2:20   ` Jeff King
  2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason
  2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
  1 sibling, 1 reply; 20+ messages in thread
From: Jeff King @ 2021-10-20  2:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Tue, Oct 19, 2021 at 06:18:40PM -0400, Jeff King wrote:

> > @@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
> >  
> >  	packet_trace_identity("ls-remote");
> >  
> > -	UNLEAK(sorting);
> > -
> >  	if (argc > 1) {
> >  		int i;
> >  		CALLOC_ARRAY(pattern, argc);
> > @@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
> >  		item->symref = xstrdup_or_null(ref->symref);
> >  	}
> >  
> > -	if (sorting)
> > +	if (sorting_options.nr) {
> > +		struct ref_sorting *sorting;
> > +		UNLEAK(sorting);
> > +
> > +		sorting = ref_sorting_options(&sorting_options);
> >  		ref_array_sort(sorting, &ref_array);
> > +	}
> 
> I wondered at first about pulling this UNLEAK() down, but it's because
> you move the "sorting" variable itself into the smaller scope. So this
> makes sense (and calling UNLEAK() before the pointer is set is perfectly
> fine, since it takes the address of the auto variable). It is a shame
> you can't just ref_sorting_free() afterwards, but we don't have that
> function yet. And adding it is way out of scope here. :)

Actually, I think I was wrong here. UNLEAK() will look at &sorting, but
it will snapshot its data at the time of the call. So it won't do
anything when the variable doesn't yet have a value.

You can demonstrate with:

  $ make SANITIZE=leak
  $ ./git ls-remote --sort=refname .

which will complain. Bumping it down like this:

diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 1e6017cdaa..a94a220256 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -139,10 +139,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	if (sorting_options.nr) {
 		struct ref_sorting *sorting;
-		UNLEAK(sorting);
 
 		sorting = ref_sorting_options(&sorting_options);
 		ref_array_sort(sorting, &ref_array);
+		UNLEAK(sorting);
 	}
 
 	for (i = 0; i < ref_array.nr; i++) {

clears it up. Note that there are other similar "leaks" (e.g., if you
give a pattern in argv[1]) which should be punted to another topic, but
I think you'd want to deal with this one since you're moving the
UNLEAK() around.

-Peff

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-20  2:20   ` Jeff King
@ 2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 12:30 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git


On Tue, Oct 19 2021, Jeff King wrote:

> On Tue, Oct 19, 2021 at 06:18:40PM -0400, Jeff King wrote:
>
>> > @@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>> >  
>> >  	packet_trace_identity("ls-remote");
>> >  
>> > -	UNLEAK(sorting);
>> > -
>> >  	if (argc > 1) {
>> >  		int i;
>> >  		CALLOC_ARRAY(pattern, argc);
>> > @@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>> >  		item->symref = xstrdup_or_null(ref->symref);
>> >  	}
>> >  
>> > -	if (sorting)
>> > +	if (sorting_options.nr) {
>> > +		struct ref_sorting *sorting;
>> > +		UNLEAK(sorting);
>> > +
>> > +		sorting = ref_sorting_options(&sorting_options);
>> >  		ref_array_sort(sorting, &ref_array);
>> > +	}
>> 
>> I wondered at first about pulling this UNLEAK() down, but it's because
>> you move the "sorting" variable itself into the smaller scope. So this
>> makes sense (and calling UNLEAK() before the pointer is set is perfectly
>> fine, since it takes the address of the auto variable). It is a shame
>> you can't just ref_sorting_free() afterwards, but we don't have that
>> function yet. And adding it is way out of scope here. :)
>
> Actually, I think I was wrong here. UNLEAK() will look at &sorting, but
> it will snapshot its data at the time of the call. So it won't do
> anything when the variable doesn't yet have a value.
>
> You can demonstrate with:
>
>   $ make SANITIZE=leak
>   $ ./git ls-remote --sort=refname .
>
> which will complain. Bumping it down like this:
>
> diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
> index 1e6017cdaa..a94a220256 100644
> --- a/builtin/ls-remote.c
> +++ b/builtin/ls-remote.c
> @@ -139,10 +139,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  
>  	if (sorting_options.nr) {
>  		struct ref_sorting *sorting;
> -		UNLEAK(sorting);
>  
>  		sorting = ref_sorting_options(&sorting_options);
>  		ref_array_sort(sorting, &ref_array);
> +		UNLEAK(sorting);
>  	}
>  
>  	for (i = 0; i < ref_array.nr; i++) {
>
> clears it up. Note that there are other similar "leaks" (e.g., if you
> give a pattern in argv[1]) which should be punted to another topic, but
> I think you'd want to deal with this one since you're moving the
> UNLEAK() around.
>
> -Peff

With or without your change Junio's patch still makes t0016-oidmap.sh
fail when applied on top of master under SANITIZE=leak, it passed
before:
    
    =================================================================
    ==3448774==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 16 byte(s) in 1 object(s) allocated from:
        #0 0x7ff5c1c8ab45 in __interceptor_calloc ../../../../src/libsanitizer/lsan/lsan_interceptors.cpp:76
        #1 0x5614d26ac4e5 in xcalloc /home/avar/g/git/wrapper.c:140
        #2 0x5614d26186b0 in ref_default_sorting /home/avar/g/git/ref-filter.c:2676
        #3 0x5614d26187c9 in ref_sorting_options /home/avar/g/git/ref-filter.c:2707
        #4 0x5614d24e21c3 in cmd_tag builtin/tag.c:527
        #5 0x5614d2407a89 in run_builtin /home/avar/g/git/git.c:461
        #6 0x5614d2407e98 in handle_builtin /home/avar/g/git/git.c:713
        #7 0x5614d2408105 in run_argv /home/avar/g/git/git.c:780
        #8 0x5614d24085ae in cmd_main /home/avar/g/git/git.c:911
        #9 0x5614d24ef3d8 in main /home/avar/g/git/common-main.c:52
        #10 0x7ff5c1a06d09 in __libc_start_main ../csu/libc-start.c:308

I.e. the leak from ref_sorting_options() via ref_default_sorting() is
new in this commit for those codepaths.
    

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes
  2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason
@ 2021-10-20 13:24       ` Ævar Arnfjörð Bjarmason
  2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
                           ` (5 more replies)
  0 siblings, 6 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 13:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

As noted in <211020.864k9boo0f.gmgdl@evledraar.gmail.com> the upthread
patch breaks the linux-leaks job, since simple invocations of "git
tag" start leaking memory: https://github.com/git/git/runs/3934925278

I tried to structure this as somethnig that could be merged or queued
up separately, but the semantic conflict in "branch.c" was difficult,
as Junio's patch changes how those variables are managed.

Junio C Hamano (1):
  for-each-ref: delay parsing of --sort=<atom> options

Ævar Arnfjörð Bjarmason (3):
  tag: use a "goto cleanup" pattern, leak less memory
  ref-filter API user: add and use a ref_sorting_release()
  branch: use ref_sorting_release()

 builtin/branch.c        | 24 ++++++++++++-----------
 builtin/for-each-ref.c  | 10 +++++-----
 builtin/ls-remote.c     | 13 ++++++++-----
 builtin/tag.c           | 42 ++++++++++++++++++++++-------------------
 ref-filter.c            | 40 ++++++++++++++++++++++++++++++---------
 ref-filter.h            | 30 ++++++++++++-----------------
 t/t3200-branch.sh       | 12 +++++++++++-
 t/t6300-for-each-ref.sh |  5 +++++
 8 files changed, 108 insertions(+), 68 deletions(-)

Range-diff against v1:
-:  ----------- > 1:  fc776c3f1cd tag: use a "goto cleanup" pattern, leak less memory
-:  ----------- > 2:  0ae71c19ab7 ref-filter API user: add and use a ref_sorting_release()
1:  21a1f4d3b08 ! 3:  7abbbe4468c for-each-ref: delay parsing of --sort=<atom> options
    @@ builtin/ls-remote.c: int cmd_ls_remote(int argc, const char **argv, const char *
     -	if (sorting)
     +	if (sorting_options.nr) {
     +		struct ref_sorting *sorting;
    -+		UNLEAK(sorting);
     +
     +		sorting = ref_sorting_options(&sorting_options);
      		ref_array_sort(sorting, &ref_array);
    ++		ref_sorting_release(sorting);
     +	}
      
      	for (i = 0; i < ref_array.nr; i++) {
    @@ ref-filter.c: void parse_ref_sorting(struct ref_sorting **sorting_tail, const ch
     +	return sorting;
      }
      
    - int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
    + void ref_sorting_release(struct ref_sorting *sorting)
     
      ## ref-filter.h ##
     @@
    @@ ref-filter.h: int format_ref_array_item(struct ref_array_item *info,
     -struct ref_sorting *ref_default_sorting(void);
     +/*  Convert list of sort options into ref_sorting */
     +struct ref_sorting *ref_sorting_options(struct string_list *);
    + /* Release a "struct ref_sorting" */
    + void ref_sorting_release(struct ref_sorting *);
      /*  Function to parse --merged and --no-merged options */
    - int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
    - /*  Get the current HEAD's description */
     
      ## t/t3200-branch.sh ##
     @@ t/t3200-branch.sh: test_expect_success 'invalid sort parameter in configuration' '
-:  ----------- > 4:  f7d87aea384 branch: use ref_sorting_release()
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
@ 2021-10-20 13:24         ` Ævar Arnfjörð Bjarmason
  2021-10-20 14:37           ` Junio C Hamano
  2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
                           ` (4 subsequent siblings)
  5 siblings, 1 reply; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 13:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Change cmd_tag() to free its "struct string_list"'s instead of using
an UNLEAK() pattern. This changes code added in 886e1084d78 (builtin/:
add UNLEAKs, 2017-10-01).

As shown in the context of the deceleration of the "struct
msg_arg" (which I'm changing to use a designated initializer while at
it, and to show the context in this change), that struct is just a
thin wrapper around an int and "struct strbuf".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/tag.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/builtin/tag.c b/builtin/tag.c
index 6535ed27ee9..ad6c9855914 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	int annotate = 0, force = 0;
 	int cmdmode = 0, create_tag_object = 0;
 	const char *msgfile = NULL, *keyid = NULL;
-	struct msg_arg msg = { 0, STRBUF_INIT };
+	struct msg_arg msg = { .buf = STRBUF_INIT };
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
@@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
 		OPT_END()
 	};
+	int ret = 0;
 
 	setup_ref_filter_porcelain_msg();
 
@@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
-		int ret;
 		if (column_active(colopts)) {
 			struct column_options copts;
 			memset(&copts, 0, sizeof(copts));
@@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		ret = list_tags(&filter, sorting, &format);
 		if (column_active(colopts))
 			stop_column_filter();
-		return ret;
+		goto cleanup;
 	}
 	if (filter.lines != -1)
 		die(_("-n option is only allowed in list mode"));
@@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		die(_("--points-at option is only allowed in list mode"));
 	if (filter.reachable_from || filter.unreachable_from)
 		die(_("--merged and --no-merged options are only allowed in list mode"));
-	if (cmdmode == 'd')
-		return delete_tags(argv);
+	if (cmdmode == 'd') {
+		ret = delete_tags(argv);
+		goto cleanup;
+	}
 	if (cmdmode == 'v') {
 		if (format.format && verify_ref_format(&format))
 			usage_with_options(git_tag_usage, options);
-		return for_each_tag_name(argv, verify_tag, &format);
+		ret = for_each_tag_name(argv, verify_tag, &format);
+		goto cleanup;
 	}
 
 	if (msg.given || msgfile) {
@@ -626,10 +629,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		printf(_("Updated tag '%s' (was %s)\n"), tag,
 		       find_unique_abbrev(&prev, DEFAULT_ABBREV));
 
-	UNLEAK(buf);
-	UNLEAK(ref);
-	UNLEAK(reflog_msg);
-	UNLEAK(msg);
-	UNLEAK(err);
-	return 0;
+cleanup:
+	strbuf_release(&buf);
+	strbuf_release(&ref);
+	strbuf_release(&reflog_msg);
+	strbuf_release(&msg.buf);
+	strbuf_release(&err);
+	return ret;
 }
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release()
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
  2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
@ 2021-10-20 13:24         ` Ævar Arnfjörð Bjarmason
  2021-10-20 14:39           ` Junio C Hamano
  2021-10-20 13:24         ` [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options Ævar Arnfjörð Bjarmason
                           ` (3 subsequent siblings)
  5 siblings, 1 reply; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 13:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Add a ref_sorting_release() and use it for some of the current API
users, the ref_sorting_default() function and its siblings will do a
malloc() which wasn't being free'd previously.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/for-each-ref.c | 2 +-
 builtin/tag.c          | 1 +
 ref-filter.c           | 8 ++++++++
 ref-filter.h           | 2 ++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 642b4b888fb..16a2c7d57ca 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -96,6 +96,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	ref_array_clear(&array);
 	free_commit_list(filter.with_commit);
 	free_commit_list(filter.no_commit);
-	UNLEAK(sorting);
+	ref_sorting_release(sorting);
 	return 0;
 }
diff --git a/builtin/tag.c b/builtin/tag.c
index ad6c9855914..6fe646710d6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -630,6 +630,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		       find_unique_abbrev(&prev, DEFAULT_ABBREV));
 
 cleanup:
+	ref_sorting_release(sorting);
 	strbuf_release(&buf);
 	strbuf_release(&ref);
 	strbuf_release(&reflog_msg);
diff --git a/ref-filter.c b/ref-filter.c
index add429be797..bdf8b0725df 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2705,6 +2705,14 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
+void ref_sorting_release(struct ref_sorting *sorting)
+{
+	struct ref_sorting *next = sorting->next;
+	if (next)
+	       ref_sorting_release(next);
+	free(sorting);
+}
+
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 {
 	struct ref_filter *rf = opt->value;
diff --git a/ref-filter.h b/ref-filter.h
index b636f4389d0..6228458d306 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -127,6 +127,8 @@ void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
 /*  Default sort option based on refname */
 struct ref_sorting *ref_default_sorting(void);
+/* Release a "struct ref_sorting" */
+void ref_sorting_release(struct ref_sorting *);
 /*  Function to parse --merged and --no-merged options */
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
 /*  Get the current HEAD's description */
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
  2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
  2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
@ 2021-10-20 13:24         ` Ævar Arnfjörð Bjarmason
  2021-10-20 13:24         ` [RFC PATCH v2 4/4] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
                           ` (2 subsequent siblings)
  5 siblings, 0 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 13:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King

From: Junio C Hamano <gitster@pobox.com>

The for-each-ref family of commands invoke parsers immediately when
it sees each --sort=<atom> option, and die before even seeing the
other options on the command line when the <atom> is unrecognised.

Instead, accumulate them in a string list, and have them parsed into
a ref_sorting structure after the command line parsing is done.  As
a consequence, "git branch --sort=bogus -h" used to fail to give the
brief help, which arguably may have been a feature, now does so,
which is more consistent with how other options work.

The patch is smaller than the actual extent of the "damage" to the
codebase, thanks to the fact that the original code consistently
used OPT_REF_SORT() macro to handle command line options.  We only
needed to replace the variable used for the list, and implementation
of the callback function used in the macro.

The old rule was for the users of the API to:

 - Declare ref_sorting and ref_sorting_tail variables;

 - OPT_REF_SORT() macro will instantiate ref_sorting instance (which
   may barf and die) and append it to the tail;

 - Append to the tail each ref_sorting read from the configuration
   by parsing in the config callback (which may barf and die);

 - See if ref_sorting is null and use ref_sorting_default() instead.

Now the rule is not all that different but is simpler:

 - Declare ref_sorting_options string list.

 - OPT_REF_SORT() macro will append it to the string list;

 - Append to the string list the sort key read from the
   configuration;

 - call ref_sorting_options() to turn the string list to ref_sorting
   structure (which also deals with the default value).

As side effects, this change also cleans up a few issues:

 - 95be717c (parse_opt_ref_sorting: always use with NONEG flag,
   2019-03-20) muses that "git for-each-ref --no-sort" should simply
   clear the sort keys accumulated so far; it now does.

 - The implementation detail of "struct ref_sorting" and the helper
   function parse_ref_sorting() can now be private to the ref-filter
   API implementation.

 - If you set branch.sort to a bogus value, the any "git branch"
   invocation, not only the listing mode, would abort with the
   original code; now it doesn't

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/branch.c        | 13 ++++++-------
 builtin/for-each-ref.c  |  8 ++++----
 builtin/ls-remote.c     | 13 ++++++++-----
 builtin/tag.c           | 13 ++++++-------
 ref-filter.c            | 34 ++++++++++++++++++++++++----------
 ref-filter.h            | 28 ++++++++++------------------
 t/t3200-branch.sh       | 12 +++++++++++-
 t/t6300-for-each-ref.sh |  5 +++++
 8 files changed, 74 insertions(+), 52 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 0b7ed82654a..69211f8dd1c 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -77,12 +77,11 @@ define_list_config_array(color_branch_slots);
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
 	const char *slot_name;
-	struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
 
 	if (!strcmp(var, "branch.sort")) {
 		if (!value)
 			return config_error_nonbool(var);
-		parse_ref_sorting(sorting_tail, value);
+		string_list_append(cb, value);
 		return 0;
 	}
 
@@ -624,7 +623,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	int icase = 0;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	static struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 
 	struct option options[] = {
@@ -665,7 +665,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_MERGED(&filter, N_("print only branches that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
 		OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
 			N_("print only branches of the object"), parse_opt_object_name),
 		OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
@@ -682,7 +682,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage_with_options(builtin_branch_usage, options);
 
-	git_config(git_branch_config, sorting_tail);
+	git_config(git_branch_config, &sorting_options);
 
 	track = git_branch_track;
 
@@ -748,8 +748,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		 * local branches 'refs/heads/...' and finally remote-tracking
 		 * branches 'refs/remotes/...'.
 		 */
-		if (!sorting)
-			sorting = ref_default_sorting();
+		sorting = ref_sorting_options(&sorting_options);
 		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 		ref_sorting_set_sort_flags_all(
 			sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 16a2c7d57ca..6f62f40d126 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -17,7 +17,8 @@ static char const * const for_each_ref_usage[] = {
 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 {
 	int i;
-	struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	int maxcount = 0, icase = 0;
 	struct ref_array array;
 	struct ref_filter filter;
@@ -39,7 +40,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
 		OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
 		OPT__COLOR(&format.use_color, N_("respect format colors")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_CALLBACK(0, "points-at", &filter.points_at,
 			     N_("object"), N_("print only refs which points at the given object"),
 			     parse_opt_object_name),
@@ -70,8 +71,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	if (verify_ref_format(&format))
 		usage_with_options(for_each_ref_usage, opts);
 
-	if (!sorting)
-		sorting = ref_default_sorting();
+	sorting = ref_sorting_options(&sorting_options);
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 318949c3d75..44448fa61d1 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -54,7 +54,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	struct transport *transport;
 	const struct ref *ref;
 	struct ref_array ref_array;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("do not print remote URL")),
@@ -68,7 +68,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
 		OPT_BOOL(0, "get-url", &get_url,
 			 N_("take url.<base>.insteadOf into account")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		OPT_SET_INT_F(0, "exit-code", &status,
 			      N_("exit with exit code 2 if no matching refs are found"),
 			      2, PARSE_OPT_NOCOMPLETE),
@@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	packet_trace_identity("ls-remote");
 
-	UNLEAK(sorting);
-
 	if (argc > 1) {
 		int i;
 		CALLOC_ARRAY(pattern, argc);
@@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		item->symref = xstrdup_or_null(ref->symref);
 	}
 
-	if (sorting)
+	if (sorting_options.nr) {
+		struct ref_sorting *sorting;
+
+		sorting = ref_sorting_options(&sorting_options);
 		ref_array_sort(sorting, &ref_array);
+		ref_sorting_release(sorting);
+	}
 
 	for (i = 0; i < ref_array.nr; i++) {
 		const struct ref_array_item *ref = ref_array.items[i];
diff --git a/builtin/tag.c b/builtin/tag.c
index 6fe646710d6..41863c5ab77 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -178,7 +178,6 @@ static const char tag_template_nocleanup[] =
 static int git_tag_config(const char *var, const char *value, void *cb)
 {
 	int status;
-	struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
 
 	if (!strcmp(var, "tag.gpgsign")) {
 		config_sign_tag = git_config_bool(var, value);
@@ -188,7 +187,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_ref_sorting(sorting_tail, value);
+		string_list_append(cb, value);
 		return 0;
 	}
 
@@ -436,7 +435,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
-	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 	int icase = 0;
 	int edit_flag = 0;
@@ -470,7 +470,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
 		OPT_MERGED(&filter, N_("print only tags that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
-		OPT_REF_SORT(sorting_tail),
+		OPT_REF_SORT(&sorting_options),
 		{
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
@@ -486,7 +486,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	setup_ref_filter_porcelain_msg();
 
-	git_config(git_tag_config, sorting_tail);
+	git_config(git_tag_config, &sorting_options);
 
 	memset(&opt, 0, sizeof(opt));
 	memset(&filter, 0, sizeof(filter));
@@ -525,8 +525,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			die(_("--column and -n are incompatible"));
 		colopts = 0;
 	}
-	if (!sorting)
-		sorting = ref_default_sorting();
+	sorting = ref_sorting_options(&sorting_options);
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
diff --git a/ref-filter.c b/ref-filter.c
index bdf8b0725df..ec7f30c5d31 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2470,6 +2470,12 @@ static int memcasecmp(const void *vs1, const void *vs2, size_t n)
 	return 0;
 }
 
+struct ref_sorting {
+	struct ref_sorting *next;
+	int atom; /* index into used_atom array (internal) */
+	enum ref_sorting_order sort_flags;
+};
+
 static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
 {
 	struct atom_value *va, *vb;
@@ -2663,7 +2669,7 @@ static int parse_sorting_atom(const char *atom)
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-struct ref_sorting *ref_default_sorting(void)
+static struct ref_sorting *ref_default_sorting(void)
 {
 	static const char cstr_name[] = "refname";
 
@@ -2674,7 +2680,7 @@ struct ref_sorting *ref_default_sorting(void)
 	return sorting;
 }
 
-void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
+static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
 {
 	struct ref_sorting *s;
 
@@ -2692,17 +2698,25 @@ void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
 	s->atom = parse_sorting_atom(arg);
 }
 
-int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
+struct ref_sorting *ref_sorting_options(struct string_list *options)
 {
+	struct string_list_item *item;
+	struct ref_sorting *sorting = NULL, **tail = &sorting;
+
+	if (!options->nr) {
+		sorting = ref_default_sorting();
+	} else {
+		for_each_string_list_item(item, options)
+			parse_ref_sorting(tail, item->string);
+	}
+
 	/*
-	 * NEEDSWORK: We should probably clear the list in this case, but we've
-	 * already munged the global used_atoms list, which would need to be
-	 * undone.
+	 * From here on, the ref_sorting list should be used to talk
+	 * about the sort order used for the output.  The caller
+	 * should not touch the string form anymore.
 	 */
-	BUG_ON_OPT_NEG(unset);
-
-	parse_ref_sorting(opt->value, arg);
-	return 0;
+	string_list_clear(options, 0);
+	return sorting;
 }
 
 void ref_sorting_release(struct ref_sorting *sorting)
diff --git a/ref-filter.h b/ref-filter.h
index 6228458d306..b6a317e92c5 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -23,16 +23,13 @@
 #define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
 struct atom_value;
+struct ref_sorting;
 
-struct ref_sorting {
-	struct ref_sorting *next;
-	int atom; /* index into used_atom array (internal) */
-	enum {
-		REF_SORTING_REVERSE = 1<<0,
-		REF_SORTING_ICASE = 1<<1,
-		REF_SORTING_VERSION = 1<<2,
-		REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
-	} sort_flags;
+enum ref_sorting_order {
+	REF_SORTING_REVERSE = 1<<0,
+	REF_SORTING_ICASE = 1<<1,
+	REF_SORTING_VERSION = 1<<2,
+	REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
 };
 
 struct ref_array_item {
@@ -97,9 +94,8 @@ struct ref_format {
 #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
 
 #define OPT_REF_SORT(var) \
-	OPT_CALLBACK_F(0, "sort", (var), \
-		       N_("key"), N_("field name to sort on"), \
-		       PARSE_OPT_NONEG, parse_opt_ref_sorting)
+	OPT_STRING_LIST(0, "sort", (var), \
+			N_("key"), N_("field name to sort on"))
 
 /*
  * API for filtering a set of refs. Based on the type of refs the user
@@ -121,12 +117,8 @@ int format_ref_array_item(struct ref_array_item *info,
 			  struct ref_format *format,
 			  struct strbuf *final_buf,
 			  struct strbuf *error_buf);
-/*  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 */
-struct ref_sorting *ref_default_sorting(void);
+/*  Convert list of sort options into ref_sorting */
+struct ref_sorting *ref_sorting_options(struct string_list *);
 /* Release a "struct ref_sorting" */
 void ref_sorting_release(struct ref_sorting *);
 /*  Function to parse --merged and --no-merged options */
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index e575ffb4ffb..0e23d18807b 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1418,7 +1418,17 @@ test_expect_success 'invalid sort parameter in configuration' '
 	(
 		cd sort &&
 		git config branch.sort "v:notvalid" &&
-		test_must_fail git branch
+
+		# this works in the "listing" mode, so bad sort key
+		# is a dying offence.
+		test_must_fail git branch &&
+
+		# these do not need to use sorting, and should all
+		# succeed
+		git branch newone main &&
+		git branch -c newone newerone &&
+		git branch -m newone newestone &&
+		git branch -d newerone newestone
 	)
 '
 
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 80679d5e12d..83921502a40 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -419,6 +419,11 @@ test_expect_success 'Verify descending sort' '
 	test_cmp expected actual
 '
 
+test_expect_success 'Give help even with invalid sort atoms' '
+	test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
+	grep "^usage: git for-each-ref" actual
+'
+
 cat >expected <<\EOF
 refs/tags/testtag
 refs/tags/testtag-2
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [RFC PATCH v2 4/4] branch: use ref_sorting_release()
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
                           ` (2 preceding siblings ...)
  2021-10-20 13:24         ` [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options Ævar Arnfjörð Bjarmason
@ 2021-10-20 13:24         ` Ævar Arnfjörð Bjarmason
  2021-10-20 14:43         ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Junio C Hamano
  2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
  5 siblings, 0 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 13:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Use a ref_sorting_release() in branch.c to free the memory from the
ref_sorting_options(). This plugs the final in-tree memory leak of
that API.

In the preceding commit the "sorting" variable was left in the
cmd_branch() scope, even though that wasn't needed anymore. Move it to
the "else if (list)" scope instead. We can also move the "struct
string_list" only used for that branch to be declared in that block

That "struct ref_sorting" does not need to be "static" (and isn't
re-used). The "ref_sorting_options()" will return a valid one, we
don't need to make it "static" to have it zero'd out. That it was
static was another artifact of the pre-image of the preceding commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/branch.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 69211f8dd1c..3963c592f56 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -69,7 +69,6 @@ static const char *color_branch_slots[] = {
 	[BRANCH_COLOR_WORKTREE] = "worktree",
 };
 
-static struct string_list output = STRING_LIST_INIT_DUP;
 static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
@@ -406,7 +405,8 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
 	return strbuf_detach(&fmt, NULL);
 }
 
-static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
+static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
+			   struct ref_format *format, struct string_list *output)
 {
 	int i;
 	struct ref_array array;
@@ -448,7 +448,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
 		if (column_active(colopts)) {
 			assert(!filter->verbose && "--column and --verbose are incompatible");
 			 /* format to a string_list to let print_columns() do its job */
-			string_list_append(&output, out.buf);
+			string_list_append(output, out.buf);
 		} else {
 			fwrite(out.buf, 1, out.len, stdout);
 			putchar('\n');
@@ -623,7 +623,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	int icase = 0;
-	static struct ref_sorting *sorting;
 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 
@@ -737,6 +736,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		print_current_branch_name();
 		return 0;
 	} else if (list) {
+		struct ref_sorting *sorting;
+		static struct string_list output = STRING_LIST_INIT_DUP;
+
 		/*  git branch --list also shows HEAD when it is detached */
 		if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
 			filter.kind |= FILTER_REFS_DETACHED_HEAD;
@@ -752,9 +754,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 		ref_sorting_set_sort_flags_all(
 			sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
-		print_ref_list(&filter, sorting, &format);
+		print_ref_list(&filter, sorting, &format, &output);
 		print_columns(&output, colopts, NULL);
 		string_list_clear(&output, 0);
+		ref_sorting_release(sorting);
 		return 0;
 	} else if (edit_description) {
 		const char *branch_name;
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-19 22:18 ` Jeff King
  2021-10-20  2:20   ` Jeff King
@ 2021-10-20 13:58   ` Junio C Hamano
  2021-10-20 20:48     ` Jeff King
  1 sibling, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2021-10-20 13:58 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

>> As side effects, this change also cleans up a few issues:
>> 
>>  - 95be717c (parse_opt_ref_sorting: always use with NONEG flag,
>>    2019-03-20) muses that "git for-each-ref --no-sort" should simply
>>    clear the sort keys accumulated so far; it now does.
>
> Neat. Is it worth adding a test here?

It probably is.  The feature lets you defeat the configured personal
default, if I understand the code right, which is probably a good
thing.

I think that the command line option is cumulative on top of
configured values, with or without this change, and I think that
qualifies as a bug to be fixed.  E.g. with a command line option

    $ git -c branch.sort=-committerdate branch --sort=subject

any configured sort keys should be cleared and the branches ought to
be sorted solely on their subject string, but I think the code with
or without the patch still uses the "-committerdate" as a secondary
key to tiebreak sorting by "subject".

Until that bug is fixed, using --no-sort as the first command line
option before the true --sort=<key> option(s) you want to use would
be a workaround.

However, it is tricky to arrange, as the command already takes
multiple --sort keys, and the laster ones are taken as more
significant sort order, so it is tricky to come up with two keys A
and B such that --sort=A --no-sort --sort=B will produce one order,
while --sort=A --sort=B will produce another different order.

>> +	if (sorting_options.nr) {
>> +		struct ref_sorting *sorting;
>> +		UNLEAK(sorting);
>> +
>> +		sorting = ref_sorting_options(&sorting_options);
>>  		ref_array_sort(sorting, &ref_array);
>> +	}
>
> I wondered at first about pulling this UNLEAK() down, but it's because
> you move the "sorting" variable itself into the smaller scope. So this
> makes sense (and calling UNLEAK() before the pointer is set is perfectly
> fine, since it takes the address of the auto variable). It is a shame
> you can't just ref_sorting_free() afterwards, but we don't have that
> function yet. And adding it is way out of scope here. :)
>
> I do find it interesting that this case checks sorting_options.nr
> itself, rather than relying on ref_sorting_options() to give us the
> default. But that's because the existing code avoids sorting at all in
> that case, so this is staying faithful to the original.

One thing that is somewhat scary was that with all the other
changes, but without the changes to builtin/ls-remote.c file, the
resulting tree still _compiles_ without any warning and only
segfaults at runtime.  Since this does not use the "if nothing is
specified, use the default", I didn't even find it as a candidate
for conversion before seeing the tests to fail.  This is an oddball
case.

>   - I'd probably have kept the word "parse" somewhere in the name, since
>     it really is turning the user-provided text into our internal form

Perhaps.

>   - clearing the list at the end feels a little funny to me, just
>     because this is conceptually a read-only operation (parse the user's
>     text into our internal format). Your comment tells me what you're
>     trying to protect against, but I find it unlikely anybody would
>     mis-use the string_list afterwards (it doesn't do anything itself
>     unless you parse it into the ref_sorting struct).
>
>     All of the current callers are happy with this (and it even saves
>     them clearing it themselves), but it just feels like an unusual
>     interface.

Yes.  The story the comment gives is an officially sounding lame
excuse; the true motivation was that I was too lazy to repeat
writing resource deallocation for each caller and made the callee to
do the freeing ;-)

>> @@ -97,9 +94,8 @@ struct ref_format {
>>  #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
>>  
>>  #define OPT_REF_SORT(var) \
>> -	OPT_CALLBACK_F(0, "sort", (var), \
>> -		       N_("key"), N_("field name to sort on"), \
>> -		       PARSE_OPT_NONEG, parse_opt_ref_sorting)
>> +	OPT_STRING_LIST(0, "sort", (var), \
>> +			N_("key"), N_("field name to sort on"))
>
> Oh, this part makes using a string_list more appealing. ;)

Yes.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory
  2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
@ 2021-10-20 14:37           ` Junio C Hamano
  0 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2021-10-20 14:37 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Jeff King

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Change cmd_tag() to free its "struct string_list"'s instead of using

string_list -> strbuf

> an UNLEAK() pattern. This changes code added in 886e1084d78 (builtin/:
> add UNLEAKs, 2017-10-01).
>
> As shown in the context of the deceleration of the "struct

deceleration?

> msg_arg" (which I'm changing to use a designated initializer while at
> it, and to show the context in this change), that struct is just a
> thin wrapper around an int and "struct strbuf".
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/tag.c | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 6535ed27ee9..ad6c9855914 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  	int annotate = 0, force = 0;
>  	int cmdmode = 0, create_tag_object = 0;
>  	const char *msgfile = NULL, *keyid = NULL;
> -	struct msg_arg msg = { 0, STRBUF_INIT };
> +	struct msg_arg msg = { .buf = STRBUF_INIT };
>  	struct ref_transaction *transaction;
>  	struct strbuf err = STRBUF_INIT;
>  	struct ref_filter filter;
> @@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  		OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
>  		OPT_END()
>  	};
> +	int ret = 0;
>  
>  	setup_ref_filter_porcelain_msg();
>  
> @@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
>  	filter.ignore_case = icase;
>  	if (cmdmode == 'l') {
> -		int ret;
>  		if (column_active(colopts)) {
>  			struct column_options copts;
>  			memset(&copts, 0, sizeof(copts));
> @@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  		ret = list_tags(&filter, sorting, &format);
>  		if (column_active(colopts))
>  			stop_column_filter();
> -		return ret;
> +		goto cleanup;
>  	}
>  	if (filter.lines != -1)
>  		die(_("-n option is only allowed in list mode"));
> @@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  		die(_("--points-at option is only allowed in list mode"));
>  	if (filter.reachable_from || filter.unreachable_from)
>  		die(_("--merged and --no-merged options are only allowed in list mode"));
> -	if (cmdmode == 'd')
> -		return delete_tags(argv);
> +	if (cmdmode == 'd') {
> +		ret = delete_tags(argv);
> +		goto cleanup;
> +	}
>  	if (cmdmode == 'v') {
>  		if (format.format && verify_ref_format(&format))
>  			usage_with_options(git_tag_usage, options);
> -		return for_each_tag_name(argv, verify_tag, &format);
> +		ret = for_each_tag_name(argv, verify_tag, &format);
> +		goto cleanup;
>  	}
>  
>  	if (msg.given || msgfile) {
> @@ -626,10 +629,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  		printf(_("Updated tag '%s' (was %s)\n"), tag,
>  		       find_unique_abbrev(&prev, DEFAULT_ABBREV));
>  
> -	UNLEAK(buf);
> -	UNLEAK(ref);
> -	UNLEAK(reflog_msg);
> -	UNLEAK(msg);
> -	UNLEAK(err);
> -	return 0;
> +cleanup:
> +	strbuf_release(&buf);
> +	strbuf_release(&ref);
> +	strbuf_release(&reflog_msg);
> +	strbuf_release(&msg.buf);
> +	strbuf_release(&err);
> +	return ret;
>  }

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release()
  2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
@ 2021-10-20 14:39           ` Junio C Hamano
  0 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2021-10-20 14:39 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Jeff King

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> +void ref_sorting_release(struct ref_sorting *sorting)
> +{
> +	struct ref_sorting *next = sorting->next;
> +	if (next)
> +	       ref_sorting_release(next);
> +	free(sorting);
> +}

Looks like a deep recursion that can be turned into an iteration
fairly easily?  I guess it does not matter as long as we won't deal
with thousands of sort keys...


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
                           ` (3 preceding siblings ...)
  2021-10-20 13:24         ` [RFC PATCH v2 4/4] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
@ 2021-10-20 14:43         ` Junio C Hamano
  2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
  5 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2021-10-20 14:43 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Jeff King

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> I tried to structure this as somethnig that could be merged or queued
> up separately, but the semantic conflict in "branch.c" was difficult,
> as Junio's patch changes how those variables are managed.

I tend to agree that it would have been simpler if you did 1, 2 and
4 first and then rebased 3 on top, possibly as an "after the dust
settles" separate topic.

The releasing of these allocated pieces of memory does make sense.

Thanks.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 0/3] ref-filter: add a ref_sorting_release()
  2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
                           ` (4 preceding siblings ...)
  2021-10-20 14:43         ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Junio C Hamano
@ 2021-10-20 18:27         ` Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
                             ` (2 more replies)
  5 siblings, 3 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 18:27 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

A follow-up to
https://lore.kernel.org/git/RFC-cover-v2-0.4-00000000000-20211020T131627Z-avarab@gmail.com/;

Junio: Hopefully this addresses the things you noted, thank[s| you]!
:)

I ejected your patch from this iteration, if you'd like to build on
top of it it should address the build failures in "seen" when combined
with your patch.

Ævar Arnfjörð Bjarmason (3):
  tag: use a "goto cleanup" pattern, leak less memory
  ref-filter API user: add and use a ref_sorting_release()
  branch: use ref_sorting_release()

 builtin/branch.c       |  8 +++++---
 builtin/for-each-ref.c |  2 +-
 builtin/tag.c          | 29 +++++++++++++++++------------
 ref-filter.c           |  9 +++++++++
 ref-filter.h           |  2 ++
 5 files changed, 34 insertions(+), 16 deletions(-)

Range-diff:
1:  fc776c3f1cd ! 1:  8ccd07abf86 tag: use a "goto cleanup" pattern, leak less memory
    @@ Metadata
      ## Commit message ##
         tag: use a "goto cleanup" pattern, leak less memory
     
    -    Change cmd_tag() to free its "struct string_list"'s instead of using
    -    an UNLEAK() pattern. This changes code added in 886e1084d78 (builtin/:
    +    Change cmd_tag() to free its "struct strbuf"'s instead of using an
    +    UNLEAK() pattern. This changes code added in 886e1084d78 (builtin/:
         add UNLEAKs, 2017-10-01).
     
    -    As shown in the context of the deceleration of the "struct
    +    As shown in the context of the declaration of the "struct
         msg_arg" (which I'm changing to use a designated initializer while at
         it, and to show the context in this change), that struct is just a
         thin wrapper around an int and "struct strbuf".
2:  0ae71c19ab7 ! 2:  07062ca276a ref-filter API user: add and use a ref_sorting_release()
    @@ ref-filter.c: int parse_opt_ref_sorting(const struct option *opt, const char *ar
      
     +void ref_sorting_release(struct ref_sorting *sorting)
     +{
    -+	struct ref_sorting *next = sorting->next;
    -+	if (next)
    -+	       ref_sorting_release(next);
    -+	free(sorting);
    ++	while (sorting) {
    ++		struct ref_sorting *next = sorting->next;
    ++		free(sorting);
    ++		sorting = next;
    ++	}
     +}
     +
      int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
3:  7abbbe4468c < -:  ----------- for-each-ref: delay parsing of --sort=<atom> options
4:  f7d87aea384 ! 3:  c8e8bc46ac4 branch: use ref_sorting_release()
    @@ Commit message
         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
     
      ## builtin/branch.c ##
    -@@ builtin/branch.c: static const char *color_branch_slots[] = {
    - 	[BRANCH_COLOR_WORKTREE] = "worktree",
    - };
    - 
    --static struct string_list output = STRING_LIST_INIT_DUP;
    - static unsigned int colopts;
    - 
    - define_list_config_array(color_branch_slots);
     @@ builtin/branch.c: static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
      	return strbuf_detach(&fmt, NULL);
      }
    @@ builtin/branch.c: static void print_ref_list(struct ref_filter *filter, struct r
      		} else {
      			fwrite(out.buf, 1, out.len, stdout);
      			putchar('\n');
    -@@ builtin/branch.c: int cmd_branch(int argc, const char **argv, const char *prefix)
    - 	enum branch_track track;
    - 	struct ref_filter filter;
    - 	int icase = 0;
    --	static struct ref_sorting *sorting;
    - 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
    - 	struct ref_format format = REF_FORMAT_INIT;
    - 
    -@@ builtin/branch.c: int cmd_branch(int argc, const char **argv, const char *prefix)
    - 		print_current_branch_name();
    - 		return 0;
    - 	} else if (list) {
    -+		struct ref_sorting *sorting;
    -+		static struct string_list output = STRING_LIST_INIT_DUP;
    -+
    - 		/*  git branch --list also shows HEAD when it is detached */
    - 		if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
    - 			filter.kind |= FILTER_REFS_DETACHED_HEAD;
     @@ builtin/branch.c: int cmd_branch(int argc, const char **argv, const char *prefix)
      		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
      		ref_sorting_set_sort_flags_all(
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory
  2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
@ 2021-10-20 18:27           ` Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 3/3] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
  2 siblings, 0 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 18:27 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Change cmd_tag() to free its "struct strbuf"'s instead of using an
UNLEAK() pattern. This changes code added in 886e1084d78 (builtin/:
add UNLEAKs, 2017-10-01).

As shown in the context of the declaration of the "struct
msg_arg" (which I'm changing to use a designated initializer while at
it, and to show the context in this change), that struct is just a
thin wrapper around an int and "struct strbuf".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/tag.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/builtin/tag.c b/builtin/tag.c
index 6535ed27ee9..ad6c9855914 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -432,7 +432,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	int annotate = 0, force = 0;
 	int cmdmode = 0, create_tag_object = 0;
 	const char *msgfile = NULL, *keyid = NULL;
-	struct msg_arg msg = { 0, STRBUF_INIT };
+	struct msg_arg msg = { .buf = STRBUF_INIT };
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
@@ -482,6 +482,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
 		OPT_END()
 	};
+	int ret = 0;
 
 	setup_ref_filter_porcelain_msg();
 
@@ -529,7 +530,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 	filter.ignore_case = icase;
 	if (cmdmode == 'l') {
-		int ret;
 		if (column_active(colopts)) {
 			struct column_options copts;
 			memset(&copts, 0, sizeof(copts));
@@ -540,7 +540,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		ret = list_tags(&filter, sorting, &format);
 		if (column_active(colopts))
 			stop_column_filter();
-		return ret;
+		goto cleanup;
 	}
 	if (filter.lines != -1)
 		die(_("-n option is only allowed in list mode"));
@@ -552,12 +552,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		die(_("--points-at option is only allowed in list mode"));
 	if (filter.reachable_from || filter.unreachable_from)
 		die(_("--merged and --no-merged options are only allowed in list mode"));
-	if (cmdmode == 'd')
-		return delete_tags(argv);
+	if (cmdmode == 'd') {
+		ret = delete_tags(argv);
+		goto cleanup;
+	}
 	if (cmdmode == 'v') {
 		if (format.format && verify_ref_format(&format))
 			usage_with_options(git_tag_usage, options);
-		return for_each_tag_name(argv, verify_tag, &format);
+		ret = for_each_tag_name(argv, verify_tag, &format);
+		goto cleanup;
 	}
 
 	if (msg.given || msgfile) {
@@ -626,10 +629,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		printf(_("Updated tag '%s' (was %s)\n"), tag,
 		       find_unique_abbrev(&prev, DEFAULT_ABBREV));
 
-	UNLEAK(buf);
-	UNLEAK(ref);
-	UNLEAK(reflog_msg);
-	UNLEAK(msg);
-	UNLEAK(err);
-	return 0;
+cleanup:
+	strbuf_release(&buf);
+	strbuf_release(&ref);
+	strbuf_release(&reflog_msg);
+	strbuf_release(&msg.buf);
+	strbuf_release(&err);
+	return ret;
 }
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release()
  2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
@ 2021-10-20 18:27           ` Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 3/3] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
  2 siblings, 0 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 18:27 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Add a ref_sorting_release() and use it for some of the current API
users, the ref_sorting_default() function and its siblings will do a
malloc() which wasn't being free'd previously.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/for-each-ref.c | 2 +-
 builtin/tag.c          | 1 +
 ref-filter.c           | 9 +++++++++
 ref-filter.h           | 2 ++
 4 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 642b4b888fb..16a2c7d57ca 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -96,6 +96,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	ref_array_clear(&array);
 	free_commit_list(filter.with_commit);
 	free_commit_list(filter.no_commit);
-	UNLEAK(sorting);
+	ref_sorting_release(sorting);
 	return 0;
 }
diff --git a/builtin/tag.c b/builtin/tag.c
index ad6c9855914..6fe646710d6 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -630,6 +630,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		       find_unique_abbrev(&prev, DEFAULT_ABBREV));
 
 cleanup:
+	ref_sorting_release(sorting);
 	strbuf_release(&buf);
 	strbuf_release(&ref);
 	strbuf_release(&reflog_msg);
diff --git a/ref-filter.c b/ref-filter.c
index add429be797..282cdad1036 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2705,6 +2705,15 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
+void ref_sorting_release(struct ref_sorting *sorting)
+{
+	while (sorting) {
+		struct ref_sorting *next = sorting->next;
+		free(sorting);
+		sorting = next;
+	}
+}
+
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 {
 	struct ref_filter *rf = opt->value;
diff --git a/ref-filter.h b/ref-filter.h
index b636f4389d0..6228458d306 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -127,6 +127,8 @@ void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
 /*  Default sort option based on refname */
 struct ref_sorting *ref_default_sorting(void);
+/* Release a "struct ref_sorting" */
+void ref_sorting_release(struct ref_sorting *);
 /*  Function to parse --merged and --no-merged options */
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
 /*  Get the current HEAD's description */
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/3] branch: use ref_sorting_release()
  2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
  2021-10-20 18:27           ` [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
@ 2021-10-20 18:27           ` Ævar Arnfjörð Bjarmason
  2 siblings, 0 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-20 18:27 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason

Use a ref_sorting_release() in branch.c to free the memory from the
ref_sorting_options(). This plugs the final in-tree memory leak of
that API.

In the preceding commit the "sorting" variable was left in the
cmd_branch() scope, even though that wasn't needed anymore. Move it to
the "else if (list)" scope instead. We can also move the "struct
string_list" only used for that branch to be declared in that block

That "struct ref_sorting" does not need to be "static" (and isn't
re-used). The "ref_sorting_options()" will return a valid one, we
don't need to make it "static" to have it zero'd out. That it was
static was another artifact of the pre-image of the preceding commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/branch.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 0b7ed82654a..7a1d1eeb070 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -407,7 +407,8 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
 	return strbuf_detach(&fmt, NULL);
 }
 
-static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
+static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
+			   struct ref_format *format, struct string_list *output)
 {
 	int i;
 	struct ref_array array;
@@ -449,7 +450,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
 		if (column_active(colopts)) {
 			assert(!filter->verbose && "--column and --verbose are incompatible");
 			 /* format to a string_list to let print_columns() do its job */
-			string_list_append(&output, out.buf);
+			string_list_append(output, out.buf);
 		} else {
 			fwrite(out.buf, 1, out.len, stdout);
 			putchar('\n');
@@ -753,9 +754,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 		ref_sorting_set_sort_flags_all(
 			sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
-		print_ref_list(&filter, sorting, &format);
+		print_ref_list(&filter, sorting, &format, &output);
 		print_columns(&output, colopts, NULL);
 		string_list_clear(&output, 0);
+		ref_sorting_release(sorting);
 		return 0;
 	} else if (edit_description) {
 		const char *branch_name;
-- 
2.33.1.1338.g20da966911a


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
@ 2021-10-20 20:48     ` Jeff King
  2021-10-20 21:32       ` Junio C Hamano
  0 siblings, 1 reply; 20+ messages in thread
From: Jeff King @ 2021-10-20 20:48 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Wed, Oct 20, 2021 at 06:58:04AM -0700, Junio C Hamano wrote:

> However, it is tricky to arrange, as the command already takes
> multiple --sort keys, and the laster ones are taken as more
> significant sort order, so it is tricky to come up with two keys A
> and B such that --sort=A --no-sort --sort=B will produce one order,
> while --sort=A --sort=B will produce another different order.

Yeah, I faced something similar with 7c5045fc18 (ref-filter: apply
fallback refname sort only after all user sorts, 2020-05-03). I suspect
you could use the same keys as those tests, though I am OK if we simply
leave it as a quietly-fixed bug.

-Peff

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-20 20:48     ` Jeff King
@ 2021-10-20 21:32       ` Junio C Hamano
  2021-10-21 14:54         ` Jeff King
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2021-10-20 21:32 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> Yeah, I faced something similar with 7c5045fc18 (ref-filter: apply
> fallback refname sort only after all user sorts, 2020-05-03). I suspect
> you could use the same keys as those tests, though I am OK if we simply
> leave it as a quietly-fixed bug.

Ah, I guess I can cheat and add a new test after these.

If --no-sort weren't taking effect, the expected outcome would be
the asme as the previous step this copied from, but with --no-sort
clearing the sort keys, we sort by taggerdate and then tiebreak with
the refname, and taggeremail does not get into the picture (other
than being repeated at the end of the refname).

 t/t6300-for-each-ref.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git c/t/t6300-for-each-ref.sh w/t/t6300-for-each-ref.sh
index 83921502a4..9f2c706c12 100755
--- c/t/t6300-for-each-ref.sh
+++ w/t/t6300-for-each-ref.sh
@@ -1024,6 +1024,27 @@ test_expect_success 'equivalent sorts fall back on refname' '
 	test_cmp expected actual
 '
 
+test_expect_success '--no-sort cancels the previous sort keys' '
+	cat >expected <<-\EOF &&
+	100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
+	100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
+	100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
+	100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
+	200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
+	200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
+	200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
+	200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
+	EOF
+	git for-each-ref \
+		--format="%(taggerdate:unix) %(taggeremail) %(refname)" \
+		--sort=-refname \
+		--sort=taggeremail \
+		--no-sort \
+		--sort=taggerdate \
+		"refs/tags/multi-*" >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 	test_when_finished "git checkout main" &&
 	git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
  2021-10-20 21:32       ` Junio C Hamano
@ 2021-10-21 14:54         ` Jeff King
  0 siblings, 0 replies; 20+ messages in thread
From: Jeff King @ 2021-10-21 14:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Wed, Oct 20, 2021 at 02:32:21PM -0700, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > Yeah, I faced something similar with 7c5045fc18 (ref-filter: apply
> > fallback refname sort only after all user sorts, 2020-05-03). I suspect
> > you could use the same keys as those tests, though I am OK if we simply
> > leave it as a quietly-fixed bug.
> 
> Ah, I guess I can cheat and add a new test after these.
> 
> If --no-sort weren't taking effect, the expected outcome would be
> the asme as the previous step this copied from, but with --no-sort
> clearing the sort keys, we sort by taggerdate and then tiebreak with
> the refname, and taggeremail does not get into the picture (other
> than being repeated at the end of the refname).

Yeah, I think what you have here makes sense. It's too bad we can't run
it on the "before" state to double-check that we are triggering the old
breakage, but there is simply no "--no-sort" at all before your patch
(which is good, because it would have been broken ;) ).

-Peff

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2021-10-21 14:54 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-18 18:32 [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-19 22:18 ` Jeff King
2021-10-20  2:20   ` Jeff King
2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason
2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 14:37           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 14:39           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` [RFC PATCH v2 4/4] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 14:43         ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Junio C Hamano
2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 3/3] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-20 20:48     ` Jeff King
2021-10-20 21:32       ` Junio C Hamano
2021-10-21 14:54         ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).