5 #include "argv-array.h"
6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
9 #include "promisor-remote.h"
12 * Parse value of the argument to the "filter" keyword.
13 * On the command line this looks like:
15 * and in the pack protocol as:
18 * The filter keyword will be used by many commands.
19 * See Documentation/rev-list-options.txt for allowed values for <arg>.
21 * Capture the given arg as the "filter_spec". This can be forwarded to
22 * subordinate commands when necessary (although it's better to pass it through
23 * expand_list_objects_filter_spec() first). We also "intern" the arg for the
24 * convenience of the current command.
26 static int gently_parse_list_objects_filter(
27 struct list_objects_filter_options *filter_options,
29 struct strbuf *errbuf)
36 if (filter_options->choice) {
40 _("multiple filter-specs cannot be combined"));
45 filter_options->filter_spec = strdup(arg);
47 if (!strcmp(arg, "blob:none")) {
48 filter_options->choice = LOFC_BLOB_NONE;
51 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
52 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
53 filter_options->choice = LOFC_BLOB_LIMIT;
57 } else if (skip_prefix(arg, "tree:", &v0)) {
58 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
62 _("expected 'tree:<depth>'"));
66 filter_options->choice = LOFC_TREE_DEPTH;
69 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
70 struct object_context oc;
71 struct object_id sparse_oid;
74 * Try to parse <oid-expression> into an OID for the current
75 * command, but DO NOT complain if we don't have the blob or
78 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
80 filter_options->sparse_oid_value = oiddup(&sparse_oid);
81 filter_options->choice = LOFC_SPARSE_OID;
84 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
88 _("sparse:path filters support has been dropped"));
93 * Please update _git_fetch() in git-completion.bash when you
98 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
100 memset(filter_options, 0, sizeof(*filter_options));
104 int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
107 struct strbuf buf = STRBUF_INIT;
108 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
113 int opt_parse_list_objects_filter(const struct option *opt,
114 const char *arg, int unset)
116 struct list_objects_filter_options *filter_options = opt->value;
119 list_objects_filter_set_no_filter(filter_options);
123 return parse_list_objects_filter(filter_options, arg);
126 void expand_list_objects_filter_spec(
127 const struct list_objects_filter_options *filter,
128 struct strbuf *expanded_spec)
130 strbuf_init(expanded_spec, strlen(filter->filter_spec));
131 if (filter->choice == LOFC_BLOB_LIMIT)
132 strbuf_addf(expanded_spec, "blob:limit=%lu",
133 filter->blob_limit_value);
134 else if (filter->choice == LOFC_TREE_DEPTH)
135 strbuf_addf(expanded_spec, "tree:%lu",
136 filter->tree_exclude_depth);
138 strbuf_addstr(expanded_spec, filter->filter_spec);
141 void list_objects_filter_release(
142 struct list_objects_filter_options *filter_options)
144 free(filter_options->filter_spec);
145 free(filter_options->sparse_oid_value);
146 memset(filter_options, 0, sizeof(*filter_options));
149 void partial_clone_register(
151 const struct list_objects_filter_options *filter_options)
156 /* Check if it is already registered */
157 if (!promisor_remote_find(remote)) {
158 git_config_set("core.repositoryformatversion", "1");
160 /* Add promisor config for the remote */
161 cfg_name = xstrfmt("remote.%s.promisor", remote);
162 git_config_set(cfg_name, "true");
167 * Record the initial filter-spec in the config as
168 * the default for subsequent fetches from this remote.
170 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
171 git_config_set(filter_name, filter_options->filter_spec);
174 /* Make sure the config info are reset */
175 promisor_remote_reinit();
178 void partial_clone_get_default_filter_spec(
179 struct list_objects_filter_options *filter_options,
182 struct promisor_remote *promisor = promisor_remote_find(remote);
185 * Parse default value, but silently ignore it if it is invalid.
188 gently_parse_list_objects_filter(filter_options,
189 promisor->partial_clone_filter,