Skip to content

Commit bea4dbe

Browse files
ttaylorrgitster
authored andcommitted
ref-filter.c: pass empty-string as NULL to atom parsers
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 declaring that atom parser implementations must not care about distinguishing between the empty string "%(refname:)" and no sub-arguments "%(refname)". Current code aborts, either with "unrecognised arg" (e.g. "refname:") or "does not take args" (e.g. "body:") as an error message. Signed-off-by: Taylor Blau <[email protected]> Reviewed-by: Jeff King <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4010f1d commit bea4dbe

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

ref-filter.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,16 @@ static int parse_ref_filter_atom(const struct ref_format *format,
415415
REALLOC_ARRAY(used_atom, used_atom_cnt);
416416
used_atom[at].name = xmemdupz(atom, ep - atom);
417417
used_atom[at].type = valid_atom[i].cmp_type;
418-
if (arg)
418+
if (arg) {
419419
arg = used_atom[at].name + (arg - atom) + 1;
420+
if (!*arg) {
421+
/*
422+
* Treat empty sub-arguments list as NULL (i.e.,
423+
* "%(atom:)" is equivalent to "%(atom)").
424+
*/
425+
arg = NULL;
426+
}
427+
}
420428
memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
421429
if (valid_atom[i].parser)
422430
valid_atom[i].parser(format, &used_atom[at], arg);

t/t6300-for-each-ref.sh

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ test_atom() {
5151
}
5252

5353
test_atom head refname refs/heads/master
54+
test_atom head refname: refs/heads/master
5455
test_atom head refname:short master
5556
test_atom head refname:lstrip=1 heads/master
5657
test_atom head refname:lstrip=2 master

0 commit comments

Comments
 (0)