Skip to content

Commit f21967e

Browse files
deveshidwivedigitster
authored andcommitted
list-objects-filter-options: avoid strbuf_split_str()
parse_combine_filter() splits a combine: filter spec at '+' using strbuf_split_str(), which yields an array of strbufs with the delimiter left at the end of each non-final piece. The code then mutates each non-final piece to strip the trailing '+' before parsing. Allocating an array of strbufs is unnecessary. The function processes one sub-spec at a time and does not use strbuf editing on the pieces. The two helpers it calls, has_reserved_character() and parse_combine_subfilter(), only read the string content of the strbuf they receive. Walk the input string directly with strchrnul() to find each '+', copying each sub-spec into a reusable temporary buffer. The '+' delimiter is naturally excluded. Empty sub-specs (e.g. from a trailing '+') are silently skipped for consistency. Change the helpers to take const char * instead of struct strbuf *. The test that expected an error on a trailing '+' is removed, since that behavior was incorrect. Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4107c0b commit f21967e

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

list-objects-filter-options.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ int gently_parse_list_objects_filter(
125125
static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
126126

127127
static int has_reserved_character(
128-
struct strbuf *sub_spec, struct strbuf *errbuf)
128+
const char *sub_spec, struct strbuf *errbuf)
129129
{
130-
const char *c = sub_spec->buf;
130+
const char *c = sub_spec;
131131
while (*c) {
132132
if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
133133
strbuf_addf(
@@ -144,7 +144,7 @@ static int has_reserved_character(
144144

145145
static int parse_combine_subfilter(
146146
struct list_objects_filter_options *filter_options,
147-
struct strbuf *subspec,
147+
const char *subspec,
148148
struct strbuf *errbuf)
149149
{
150150
size_t new_index = filter_options->sub_nr;
@@ -155,7 +155,7 @@ static int parse_combine_subfilter(
155155
filter_options->sub_alloc);
156156
list_objects_filter_init(&filter_options->sub[new_index]);
157157

158-
decoded = url_percent_decode(subspec->buf);
158+
decoded = url_percent_decode(subspec);
159159

160160
result = has_reserved_character(subspec, errbuf);
161161
if (result)
@@ -182,34 +182,34 @@ static int parse_combine_filter(
182182
const char *arg,
183183
struct strbuf *errbuf)
184184
{
185-
struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
186-
size_t sub;
185+
const char *p = arg;
186+
struct strbuf sub = STRBUF_INIT;
187187
int result = 0;
188188

189-
if (!subspecs[0]) {
189+
if (!*p) {
190190
strbuf_addstr(errbuf, _("expected something after combine:"));
191191
result = 1;
192192
goto cleanup;
193193
}
194194

195-
for (sub = 0; subspecs[sub] && !result; sub++) {
196-
if (subspecs[sub + 1]) {
197-
/*
198-
* This is not the last subspec. Remove trailing "+" so
199-
* we can parse it.
200-
*/
201-
size_t last = subspecs[sub]->len - 1;
202-
assert(subspecs[sub]->buf[last] == '+');
203-
strbuf_remove(subspecs[sub], last, 1);
204-
}
205-
result = parse_combine_subfilter(
206-
filter_options, subspecs[sub], errbuf);
195+
while (*p && !result) {
196+
const char *end = strchrnul(p, '+');
197+
198+
strbuf_reset(&sub);
199+
strbuf_add(&sub, p, end - p);
200+
201+
if (sub.len)
202+
result = parse_combine_subfilter(filter_options, sub.buf, errbuf);
203+
204+
if (!*end)
205+
break;
206+
p = end + 1;
207207
}
208+
strbuf_release(&sub);
208209

209210
filter_options->choice = LOFC_COMBINE;
210211

211212
cleanup:
212-
strbuf_list_free(subspecs);
213213
if (result)
214214
list_objects_filter_release(filter_options);
215215
return result;

t/t6112-rev-list-filters-objects.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,6 @@ test_expect_success 'combine:... with non-encoded reserved chars' '
483483
"must escape char in sub-filter-spec: .\~."
484484
'
485485

486-
test_expect_success 'validate err msg for "combine:<valid-filter>+"' '
487-
expect_invalid_filter_spec combine:tree:2+ "expected .tree:<depth>."
488-
'
489-
490486
test_expect_success 'combine:... with edge-case hex digits: Ff Aa 0 9' '
491487
git -C r3 rev-list --objects --filter="combine:tree:2+bl%6Fb:n%6fne" \
492488
HEAD >actual &&

0 commit comments

Comments
 (0)