Skip to content

Commit c54980a

Browse files
peffgitster
authored andcommitted
list-objects-filter: convert filter_spec to a strbuf
Originally, the filter_spec field was just a string pointer. In cf9ceb5 (list-objects-filter-options: make filter_spec a string_list, 2019-06-27) it became a string_list, but that commit notes: A strbuf would seem to be a more natural choice for this object, but it unfortunately requires initialization besides just zero'ing out the memory. This results in all container structs, and all containers of those structs, etc., to also require initialization. Initializing them all would be more cumbersome that simply using a string_list, which behaves properly when its contents are zero'd. Now that we've changed the struct to require non-zero initialization anyway (ironically, because string_list also needed non-zero initialization to avoid leaks), we can now convert to that more natural type. This makes the list_objects_filter_spec() function much less awkward, as it had to collapse the string_list to a single-entry list on the fly. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2a01bde commit c54980a

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

list-objects-filter-options.c

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ static int allow_unencoded(char ch)
202202
static void filter_spec_append_urlencode(
203203
struct list_objects_filter_options *filter, const char *raw)
204204
{
205-
struct strbuf buf = STRBUF_INIT;
206-
strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
207-
trace_printf("Add to combine filter-spec: %s\n", buf.buf);
208-
string_list_append_nodup(&filter->filter_spec, strbuf_detach(&buf, NULL));
205+
size_t orig_len = filter->filter_spec.len;
206+
strbuf_addstr_urlencode(&filter->filter_spec, raw, allow_unencoded);
207+
trace_printf("Add to combine filter-spec: %s\n",
208+
filter->filter_spec.buf + orig_len);
209209
}
210210

211211
/*
@@ -229,15 +229,15 @@ static void transform_to_combine_type(
229229
}
230230
filter_options->sub_nr = 1;
231231
filter_options->choice = LOFC_COMBINE;
232-
string_list_append(&filter_options->filter_spec, "combine:");
232+
strbuf_addstr(&filter_options->filter_spec, "combine:");
233233
filter_spec_append_urlencode(
234234
filter_options,
235235
list_objects_filter_spec(&filter_options->sub[0]));
236236
/*
237237
* We don't need the filter_spec strings for subfilter specs, only the
238238
* top level.
239239
*/
240-
string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
240+
strbuf_release(&filter_options->sub[0].filter_spec);
241241
}
242242

243243
void list_objects_filter_die_if_populated(
@@ -254,11 +254,11 @@ void parse_list_objects_filter(
254254
struct strbuf errbuf = STRBUF_INIT;
255255
int parse_error;
256256

257-
if (!filter_options->filter_spec.strdup_strings)
257+
if (!filter_options->filter_spec.buf)
258258
BUG("filter_options not properly initialized");
259259

260260
if (!filter_options->choice) {
261-
string_list_append(&filter_options->filter_spec, arg);
261+
strbuf_addstr(&filter_options->filter_spec, arg);
262262

263263
parse_error = gently_parse_list_objects_filter(
264264
filter_options, arg, &errbuf);
@@ -269,7 +269,7 @@ void parse_list_objects_filter(
269269
*/
270270
transform_to_combine_type(filter_options);
271271

272-
string_list_append(&filter_options->filter_spec, "+");
272+
strbuf_addch(&filter_options->filter_spec, '+');
273273
filter_spec_append_urlencode(filter_options, arg);
274274
ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
275275
filter_options->sub_alloc);
@@ -300,31 +300,18 @@ int opt_parse_list_objects_filter(const struct option *opt,
300300

301301
const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
302302
{
303-
if (!filter->filter_spec.nr)
303+
if (!filter->filter_spec.len)
304304
BUG("no filter_spec available for this filter");
305-
if (filter->filter_spec.nr != 1) {
306-
struct strbuf concatted = STRBUF_INIT;
307-
strbuf_add_separated_string_list(
308-
&concatted, "", &filter->filter_spec);
309-
string_list_clear(&filter->filter_spec, /*free_util=*/0);
310-
string_list_append_nodup(
311-
&filter->filter_spec, strbuf_detach(&concatted, NULL));
312-
}
313-
314-
return filter->filter_spec.items[0].string;
305+
return filter->filter_spec.buf;
315306
}
316307

317308
const char *expand_list_objects_filter_spec(
318309
struct list_objects_filter_options *filter)
319310
{
320311
if (filter->choice == LOFC_BLOB_LIMIT) {
321-
struct strbuf expanded_spec = STRBUF_INIT;
322-
strbuf_addf(&expanded_spec, "blob:limit=%lu",
312+
strbuf_release(&filter->filter_spec);
313+
strbuf_addf(&filter->filter_spec, "blob:limit=%lu",
323314
filter->blob_limit_value);
324-
string_list_clear(&filter->filter_spec, /*free_util=*/0);
325-
string_list_append_nodup(
326-
&filter->filter_spec,
327-
strbuf_detach(&expanded_spec, NULL));
328315
}
329316

330317
return list_objects_filter_spec(filter);
@@ -337,7 +324,7 @@ void list_objects_filter_release(
337324

338325
if (!filter_options)
339326
return;
340-
string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
327+
strbuf_release(&filter_options->filter_spec);
341328
free(filter_options->sparse_oid_name);
342329
for (sub = 0; sub < filter_options->sub_nr; sub++)
343330
list_objects_filter_release(&filter_options->sub[sub]);
@@ -398,8 +385,8 @@ void partial_clone_get_default_filter_spec(
398385
if (!promisor || !promisor->partial_clone_filter)
399386
return;
400387

401-
string_list_append(&filter_options->filter_spec,
402-
promisor->partial_clone_filter);
388+
strbuf_addstr(&filter_options->filter_spec,
389+
promisor->partial_clone_filter);
403390
gently_parse_list_objects_filter(filter_options,
404391
promisor->partial_clone_filter,
405392
&errbuf);
@@ -411,14 +398,12 @@ void list_objects_filter_copy(
411398
const struct list_objects_filter_options *src)
412399
{
413400
int i;
414-
struct string_list_item *item;
415401

416402
/* Copy everything. We will overwrite the pointers shortly. */
417403
memcpy(dest, src, sizeof(struct list_objects_filter_options));
418404

419-
string_list_init_dup(&dest->filter_spec);
420-
for_each_string_list_item(item, &src->filter_spec)
421-
string_list_append(&dest->filter_spec, item->string);
405+
strbuf_init(&dest->filter_spec, 0);
406+
strbuf_addbuf(&dest->filter_spec, &src->filter_spec);
422407
dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);
423408

424409
ALLOC_ARRAY(dest->sub, dest->sub_alloc);

list-objects-filter-options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct list_objects_filter_options {
3535
* To get the raw filter spec given by the user, use the result of
3636
* list_objects_filter_spec().
3737
*/
38-
struct string_list filter_spec;
38+
struct strbuf filter_spec;
3939

4040
/*
4141
* 'choice' is determined by parsing the filter-spec. This indicates
@@ -69,7 +69,7 @@ struct list_objects_filter_options {
6969
*/
7070
};
7171

72-
#define LIST_OBJECTS_FILTER_INIT { .filter_spec = STRING_LIST_INIT_DUP }
72+
#define LIST_OBJECTS_FILTER_INIT { .filter_spec = STRBUF_INIT }
7373
void list_objects_filter_init(struct list_objects_filter_options *filter_options);
7474

7575
/*

0 commit comments

Comments
 (0)