git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] ref-filter.c: pass empty-string as NULL to atom parsers

From
Taylor Blau <me@ttaylorr.com>
Date
Oct 2, 2017, 05:53 UTC
Message-ID
<20171002055311.29681-1-me@ttaylorr.com>
In-Reply-To
<20171002055052.GB10729@D-10-157-251-166.dhcp4.washington.edu>

Peff points out that different atom parsers handle the empty "sub-argument" list differently. An example of this is the format "%(refname:)".

Since callers often use `string_list_split` (which splits the empty string with any delimiter as a 1-ary string_list containing the empty string), this makes handling empty sub-argument strings non-ergonomic.

Let's fix this by assuming that atom parser implementations don't care about distinguishing between the empty string "%(refname:)" and no sub-arguments "%(refname)".

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 ref-filter.c            | 17 ++++++++++++++++-
 t/t6300-for-each-ref.sh |  1 +
 2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/ref-filter.c b/ref-filter.c
index bc591f4f3..22be4097a 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -415,8 +415,23 @@ static int parse_ref_filter_atom(const struct ref_format *format,
 	REALLOC_ARRAY(used_atom, used_atom_cnt);
 	used_atom[at].name = xmemdupz(atom, ep - atom);
 	used_atom[at].type = valid_atom[i].cmp_type;
-	if (arg)
+	if (arg) {
 		arg = used_atom[at].name + (arg - atom) + 1;
+		if (!*arg) {
+			/*
+			 * string_list_split is often use by atom parsers to
+			 * split multiple sub-arguments for inspection.
+			 *
+			 * Given a string that does not contain a delimiter
+			 * (example: ""), string_list_split returns a 1-ary
+			 * string_list that requires adding special cases to
+			 * atom parsers.
+			 *
+			 * Thus, treat the empty argument string as NULL.
+			 */
+			arg = NULL;
+		}
+	}
 	memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
 	if (valid_atom[i].parser)
 		valid_atom[i].parser(format, &used_atom[at], arg);
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 2274a4b73..edc1bd8ea 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -51,6 +51,7 @@ test_atom() {
 }
 
 test_atom head refname refs/heads/master
+test_atom head refname: refs/heads/master
 test_atom head refname:short master
 test_atom head refname:lstrip=1 heads/master
 test_atom head refname:lstrip=2 master
-- 
2.14.1.145.gb3622a4ee
Previous: Taylor BlauNext: Jeff King
Message 2 of 12 in “ref-filter.c: pass empty-string as NULL to atom parsers”
  1. 0/1 ref-filter.c: pass empty-string as NULL to atom parsersTaylor Blau, Oct 2, 2017
  2. ref-filter.c: pass empty-string as NULL to atom parsersTaylor Blau, Oct 2, 2017
  3. Jeff KingOct 2, 2017
  4. Taylor BlauOct 2, 2017
  5. Jeff KingOct 2, 2017
  6. ref-filter.c: pass empty-string as NULL to atom parsersTaylor Blau, Oct 2, 2017
  7. Jeff KingOct 2, 2017
  8. Jonathan NiederOct 2, 2017
  9. Junio C HamanoOct 2, 2017
  10. Taylor BlauOct 3, 2017
  11. Junio C HamanoOct 5, 2017
  12. Jonathan NiederOct 5, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.