Merge branch 'cc/multi-promisor'
[git/debian.git] / list-objects-filter-options.c
blob28c571f922e46cd6ff7aff659677d9003cf3c948
1 #include "cache.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "revision.h"
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:
14 * --filter=<arg>
15 * and in the pack protocol as:
16 * "filter" SP <arg>
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,
28 const char *arg,
29 struct strbuf *errbuf)
31 const char *v0;
33 if (!arg)
34 return 0;
36 if (filter_options->choice) {
37 if (errbuf) {
38 strbuf_addstr(
39 errbuf,
40 _("multiple filter-specs cannot be combined"));
42 return 1;
45 filter_options->filter_spec = strdup(arg);
47 if (!strcmp(arg, "blob:none")) {
48 filter_options->choice = LOFC_BLOB_NONE;
49 return 0;
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;
54 return 0;
57 } else if (skip_prefix(arg, "tree:", &v0)) {
58 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
59 if (errbuf) {
60 strbuf_addstr(
61 errbuf,
62 _("expected 'tree:<depth>'"));
64 return 1;
66 filter_options->choice = LOFC_TREE_DEPTH;
67 return 0;
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
76 * ref locally.
78 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
79 &sparse_oid, &oc))
80 filter_options->sparse_oid_value = oiddup(&sparse_oid);
81 filter_options->choice = LOFC_SPARSE_OID;
82 return 0;
84 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
85 if (errbuf) {
86 strbuf_addstr(
87 errbuf,
88 _("sparse:path filters support has been dropped"));
90 return 1;
93 * Please update _git_fetch() in git-completion.bash when you
94 * add new filters
97 if (errbuf)
98 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
100 memset(filter_options, 0, sizeof(*filter_options));
101 return 1;
104 int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
105 const char *arg)
107 struct strbuf buf = STRBUF_INIT;
108 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
109 die("%s", buf.buf);
110 return 0;
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;
118 if (unset || !arg) {
119 list_objects_filter_set_no_filter(filter_options);
120 return 0;
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);
137 else
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(
150 const char *remote,
151 const struct list_objects_filter_options *filter_options)
153 char *cfg_name;
154 char *filter_name;
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");
163 free(cfg_name);
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);
172 free(filter_name);
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,
180 const char *remote)
182 struct promisor_remote *promisor = promisor_remote_find(remote);
185 * Parse default value, but silently ignore it if it is invalid.
187 if (promisor)
188 gently_parse_list_objects_filter(filter_options,
189 promisor->partial_clone_filter,
190 NULL);