Skip to content

Commit 9b93d26

Browse files
matvoregitster
authored andcommitted
list-objects-filter-options: make filter_spec a string_list
Make the filter_spec string a string_list rather than a raw C string. The list of strings must be concatted together to make a complete filter_spec. A future patch will use this capability to build "combine:" filter specs gradually. 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. For the purposes of code simplification, change behavior in how filter specs are conveyed over the protocol: do not normalize the tree:<depth> filter specs since there should be no server in existence that supports tree:# but not tree:#k etc. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Matthew DeVore <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3d10e5 commit 9b93d26

9 files changed

+78
-70
lines changed

builtin/clone.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,13 +1141,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11411141
transport->server_options = &server_options;
11421142

11431143
if (filter_options.choice) {
1144-
struct strbuf expanded_filter_spec = STRBUF_INIT;
1145-
expand_list_objects_filter_spec(&filter_options,
1146-
&expanded_filter_spec);
1144+
const char *spec =
1145+
expand_list_objects_filter_spec(&filter_options);
11471146
transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1148-
expanded_filter_spec.buf);
1147+
spec);
11491148
transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1150-
strbuf_release(&expanded_filter_spec);
11511149
}
11521150

11531151
if (transport->smart_options && !deepen && !filter_options.choice)

builtin/fetch.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,13 +1188,10 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
11881188
if (update_shallow)
11891189
set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
11901190
if (filter_options.choice) {
1191-
struct strbuf expanded_filter_spec = STRBUF_INIT;
1192-
expand_list_objects_filter_spec(&filter_options,
1193-
&expanded_filter_spec);
1194-
set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1195-
expanded_filter_spec.buf);
1191+
const char *spec =
1192+
expand_list_objects_filter_spec(&filter_options);
1193+
set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
11961194
set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1197-
strbuf_release(&expanded_filter_spec);
11981195
}
11991196
if (negotiation_tip.nr) {
12001197
if (transport->smart_options)

builtin/rev-list.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,10 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
466466
die(_("object filtering requires --objects"));
467467
if (filter_options.choice == LOFC_SPARSE_OID &&
468468
!filter_options.sparse_oid_value)
469-
die(_("invalid sparse value '%s'"),
470-
filter_options.filter_spec);
469+
die(
470+
_("invalid sparse value '%s'"),
471+
list_objects_filter_spec(
472+
&filter_options));
471473
continue;
472474
}
473475
if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {

fetch-pack.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,9 @@ static int find_common(struct fetch_negotiator *negotiator,
339339
}
340340
}
341341
if (server_supports_filtering && args->filter_options.choice) {
342-
struct strbuf expanded_filter_spec = STRBUF_INIT;
343-
expand_list_objects_filter_spec(&args->filter_options,
344-
&expanded_filter_spec);
345-
packet_buf_write(&req_buf, "filter %s",
346-
expanded_filter_spec.buf);
347-
strbuf_release(&expanded_filter_spec);
342+
const char *spec =
343+
expand_list_objects_filter_spec(&args->filter_options);
344+
packet_buf_write(&req_buf, "filter %s", spec);
348345
}
349346
packet_buf_flush(&req_buf);
350347
state_len = req_buf.len;
@@ -1099,7 +1096,7 @@ static int add_haves(struct fetch_negotiator *negotiator,
10991096
}
11001097

11011098
static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1102-
const struct fetch_pack_args *args,
1099+
struct fetch_pack_args *args,
11031100
const struct ref *wants, struct oidset *common,
11041101
int *haves_to_send, int *in_vain,
11051102
int sideband_all)
@@ -1140,13 +1137,10 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
11401137
/* Add filter */
11411138
if (server_supports_feature("fetch", "filter", 0) &&
11421139
args->filter_options.choice) {
1143-
struct strbuf expanded_filter_spec = STRBUF_INIT;
1140+
const char *spec =
1141+
expand_list_objects_filter_spec(&args->filter_options);
11441142
print_verbose(args, _("Server supports filter"));
1145-
expand_list_objects_filter_spec(&args->filter_options,
1146-
&expanded_filter_spec);
1147-
packet_buf_write(&req_buf, "filter %s",
1148-
expanded_filter_spec.buf);
1149-
strbuf_release(&expanded_filter_spec);
1143+
packet_buf_write(&req_buf, "filter %s", spec);
11501144
} else if (args->filter_options.choice) {
11511145
warning("filtering not recognized by server, ignoring");
11521146
}

list-objects-filter-options.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int parse_list_objects_filter(struct list_objects_filter_options *filter_options
184184
struct strbuf buf = STRBUF_INIT;
185185
if (filter_options->choice)
186186
die(_("multiple filter-specs cannot be combined"));
187-
filter_options->filter_spec = strdup(arg);
187+
string_list_append(&filter_options->filter_spec, xstrdup(arg));
188188
if (gently_parse_list_objects_filter(filter_options, arg, &buf))
189189
die("%s", buf.buf);
190190
return 0;
@@ -203,19 +203,36 @@ int opt_parse_list_objects_filter(const struct option *opt,
203203
return parse_list_objects_filter(filter_options, arg);
204204
}
205205

206-
void expand_list_objects_filter_spec(
207-
const struct list_objects_filter_options *filter,
208-
struct strbuf *expanded_spec)
206+
const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
209207
{
210-
strbuf_init(expanded_spec, strlen(filter->filter_spec));
211-
if (filter->choice == LOFC_BLOB_LIMIT)
212-
strbuf_addf(expanded_spec, "blob:limit=%lu",
208+
if (!filter->filter_spec.nr)
209+
BUG("no filter_spec available for this filter");
210+
if (filter->filter_spec.nr != 1) {
211+
struct strbuf concatted = STRBUF_INIT;
212+
strbuf_add_separated_string_list(
213+
&concatted, "", &filter->filter_spec);
214+
string_list_clear(&filter->filter_spec, /*free_util=*/0);
215+
string_list_append(
216+
&filter->filter_spec, strbuf_detach(&concatted, NULL));
217+
}
218+
219+
return filter->filter_spec.items[0].string;
220+
}
221+
222+
const char *expand_list_objects_filter_spec(
223+
struct list_objects_filter_options *filter)
224+
{
225+
if (filter->choice == LOFC_BLOB_LIMIT) {
226+
struct strbuf expanded_spec = STRBUF_INIT;
227+
strbuf_addf(&expanded_spec, "blob:limit=%lu",
213228
filter->blob_limit_value);
214-
else if (filter->choice == LOFC_TREE_DEPTH)
215-
strbuf_addf(expanded_spec, "tree:%lu",
216-
filter->tree_exclude_depth);
217-
else
218-
strbuf_addstr(expanded_spec, filter->filter_spec);
229+
string_list_clear(&filter->filter_spec, /*free_util=*/0);
230+
string_list_append(
231+
&filter->filter_spec,
232+
strbuf_detach(&expanded_spec, NULL));
233+
}
234+
235+
return list_objects_filter_spec(filter);
219236
}
220237

221238
void list_objects_filter_release(
@@ -225,7 +242,7 @@ void list_objects_filter_release(
225242

226243
if (!filter_options)
227244
return;
228-
free(filter_options->filter_spec);
245+
string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
229246
free(filter_options->sparse_oid_value);
230247
for (sub = 0; sub < filter_options->sub_nr; sub++)
231248
list_objects_filter_release(&filter_options->sub[sub]);
@@ -235,7 +252,7 @@ void list_objects_filter_release(
235252

236253
void partial_clone_register(
237254
const char *remote,
238-
const struct list_objects_filter_options *filter_options)
255+
struct list_objects_filter_options *filter_options)
239256
{
240257
/*
241258
* Record the name of the partial clone remote in the
@@ -258,7 +275,7 @@ void partial_clone_register(
258275
* the default for subsequent fetches from this remote.
259276
*/
260277
core_partial_clone_filter_default =
261-
xstrdup(filter_options->filter_spec);
278+
xstrdup(expand_list_objects_filter_spec(filter_options));
262279
git_config_set("core.partialclonefilter",
263280
core_partial_clone_filter_default);
264281
}
@@ -274,7 +291,8 @@ void partial_clone_get_default_filter_spec(
274291
if (!core_partial_clone_filter_default)
275292
return;
276293

277-
filter_options->filter_spec = strdup(core_partial_clone_filter_default);
294+
string_list_append(&filter_options->filter_spec,
295+
core_partial_clone_filter_default);
278296
gently_parse_list_objects_filter(filter_options,
279297
core_partial_clone_filter_default,
280298
&errbuf);

list-objects-filter-options.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIST_OBJECTS_FILTER_OPTIONS_H
33

44
#include "parse-options.h"
5-
#include "strbuf.h"
5+
#include "string-list.h"
66

77
/*
88
* The list of defined filters for list-objects.
@@ -24,8 +24,10 @@ struct list_objects_filter_options {
2424
* commands that launch filtering sub-processes, or for communication
2525
* over the network, don't use this value; use the result of
2626
* expand_list_objects_filter_spec() instead.
27+
* To get the raw filter spec given by the user, use the result of
28+
* list_objects_filter_spec().
2729
*/
28-
char *filter_spec;
30+
struct string_list filter_spec;
2931

3032
/*
3133
* 'choice' is determined by parsing the filter-spec. This indicates
@@ -76,13 +78,22 @@ int opt_parse_list_objects_filter(const struct option *opt,
7678
/*
7779
* Translates abbreviated numbers in the filter's filter_spec into their
7880
* fully-expanded forms (e.g., "limit:blob=1k" becomes "limit:blob=1024").
81+
* Returns a string owned by the list_objects_filter_options object.
7982
*
80-
* This form should be used instead of the raw filter_spec field when
81-
* communicating with a remote process or subprocess.
83+
* This form should be used instead of the raw list_objects_filter_spec()
84+
* value when communicating with a remote process or subprocess.
8285
*/
83-
void expand_list_objects_filter_spec(
84-
const struct list_objects_filter_options *filter,
85-
struct strbuf *expanded_spec);
86+
const char *expand_list_objects_filter_spec(
87+
struct list_objects_filter_options *filter);
88+
89+
/*
90+
* Returns the filter spec string more or less in the form as the user
91+
* entered it. This form of the filter_spec can be used in user-facing
92+
* messages. Returns a string owned by the list_objects_filter_options
93+
* object.
94+
*/
95+
const char *list_objects_filter_spec(
96+
struct list_objects_filter_options *filter);
8697

8798
void list_objects_filter_release(
8899
struct list_objects_filter_options *filter_options);
@@ -96,7 +107,7 @@ static inline void list_objects_filter_set_no_filter(
96107

97108
void partial_clone_register(
98109
const char *remote,
99-
const struct list_objects_filter_options *filter_options);
110+
struct list_objects_filter_options *filter_options);
100111
void partial_clone_get_default_filter_spec(
101112
struct list_objects_filter_options *filter_options);
102113

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,4 @@ test_expect_success 'expand blob limit in protocol' '
590590
grep "blob:limit=1024" trace
591591
'
592592

593-
test_expect_success 'expand tree depth limit in protocol' '
594-
GIT_TRACE_PACKET="$(pwd)/tree_trace" git -c protocol.version=2 clone \
595-
--filter=tree:0k "file://$(pwd)/r2" tree &&
596-
! grep "tree:0k" tree_trace &&
597-
grep "tree:0" tree_trace
598-
'
599-
600593
test_done

transport-helper.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,9 @@ static int fetch(struct transport *transport,
682682
set_helper_option(transport, "update-shallow", "true");
683683

684684
if (data->transport_options.filter_options.choice) {
685-
struct strbuf expanded_filter_spec = STRBUF_INIT;
686-
expand_list_objects_filter_spec(
687-
&data->transport_options.filter_options,
688-
&expanded_filter_spec);
689-
set_helper_option(transport, "filter",
690-
expanded_filter_spec.buf);
691-
strbuf_release(&expanded_filter_spec);
685+
const char *spec = expand_list_objects_filter_spec(
686+
&data->transport_options.filter_options);
687+
set_helper_option(transport, "filter", spec);
692688
}
693689

694690
if (data->transport_options.negotiation_tips)

upload-pack.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,17 @@ static void create_pack_file(const struct object_array *have_obj,
140140
argv_array_push(&pack_objects.args, "--delta-base-offset");
141141
if (use_include_tag)
142142
argv_array_push(&pack_objects.args, "--include-tag");
143-
if (filter_options.filter_spec) {
144-
struct strbuf expanded_filter_spec = STRBUF_INIT;
145-
expand_list_objects_filter_spec(&filter_options,
146-
&expanded_filter_spec);
143+
if (filter_options.choice) {
144+
const char *spec =
145+
expand_list_objects_filter_spec(&filter_options);
147146
if (pack_objects.use_shell) {
148147
struct strbuf buf = STRBUF_INIT;
149-
sq_quote_buf(&buf, expanded_filter_spec.buf);
148+
sq_quote_buf(&buf, spec);
150149
argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
151150
strbuf_release(&buf);
152151
} else {
153152
argv_array_pushf(&pack_objects.args, "--filter=%s",
154-
expanded_filter_spec.buf);
153+
spec);
155154
}
156155
}
157156

0 commit comments

Comments
 (0)