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"
10
11/*
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>
17 *
18 * The filter keyword will be used by many commands.
19 * See Documentation/rev-list-options.txt for allowed values for <arg>.
20 *
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.
25 */
26static int gently_parse_list_objects_filter(
27 struct list_objects_filter_options *filter_options,
28 const char *arg,
29 struct strbuf *errbuf)
30{
31 const char *v0;
32
33 if (filter_options->choice) {
34 if (errbuf) {
35 strbuf_addstr(
36 errbuf,
37 _("multiple filter-specs cannot be combined"));
38 }
39 return 1;
40 }
41
42 filter_options->filter_spec = strdup(arg);
43
44 if (!strcmp(arg, "blob:none")) {
45 filter_options->choice = LOFC_BLOB_NONE;
46 return 0;
47
48 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
49 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
50 filter_options->choice = LOFC_BLOB_LIMIT;
51 return 0;
52 }
53
54 } else if (skip_prefix(arg, "tree:", &v0)) {
55 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
56 if (errbuf) {
57 strbuf_addstr(
58 errbuf,
59 _("expected 'tree:<depth>'"));
60 }
61 return 1;
62 }
63 filter_options->choice = LOFC_TREE_DEPTH;
64 return 0;
65
66 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
67 struct object_context oc;
68 struct object_id sparse_oid;
69
70 /*
71 * Try to parse <oid-expression> into an OID for the current
72 * command, but DO NOT complain if we don't have the blob or
73 * ref locally.
74 */
75 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
76 &sparse_oid, &oc))
77 filter_options->sparse_oid_value = oiddup(&sparse_oid);
78 filter_options->choice = LOFC_SPARSE_OID;
79 return 0;
80
81 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
82 if (errbuf) {
83 strbuf_addstr(
84 errbuf,
85 _("sparse:path filters support has been dropped"));
86 }
87 return 1;
88 }
89 /*
90 * Please update _git_fetch() in git-completion.bash when you
91 * add new filters
92 */
93
94 if (errbuf)
95 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
96
97 memset(filter_options, 0, sizeof(*filter_options));
98 return 1;
99}
100
101int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
102 const char *arg)
103{
104 struct strbuf buf = STRBUF_INIT;
105 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
106 die("%s", buf.buf);
107 return 0;
108}
109
110int opt_parse_list_objects_filter(const struct option *opt,
111 const char *arg, int unset)
112{
113 struct list_objects_filter_options *filter_options = opt->value;
114
115 if (unset || !arg) {
116 list_objects_filter_set_no_filter(filter_options);
117 return 0;
118 }
119
120 return parse_list_objects_filter(filter_options, arg);
121}
122
123void expand_list_objects_filter_spec(
124 const struct list_objects_filter_options *filter,
125 struct strbuf *expanded_spec)
126{
127 strbuf_init(expanded_spec, strlen(filter->filter_spec));
128 if (filter->choice == LOFC_BLOB_LIMIT)
129 strbuf_addf(expanded_spec, "blob:limit=%lu",
130 filter->blob_limit_value);
131 else if (filter->choice == LOFC_TREE_DEPTH)
132 strbuf_addf(expanded_spec, "tree:%lu",
133 filter->tree_exclude_depth);
134 else
135 strbuf_addstr(expanded_spec, filter->filter_spec);
136}
137
138void list_objects_filter_release(
139 struct list_objects_filter_options *filter_options)
140{
141 free(filter_options->filter_spec);
142 free(filter_options->sparse_oid_value);
143 memset(filter_options, 0, sizeof(*filter_options));
144}
145
146void partial_clone_register(
147 const char *remote,
148 const struct list_objects_filter_options *filter_options)
149{
150 char *cfg_name;
151
152 /* Check if it is already registered */
153 if (!promisor_remote_find(remote)) {
154 git_config_set("core.repositoryformatversion", "1");
155
156 /* Add promisor config for the remote */
157 cfg_name = xstrfmt("remote.%s.promisor", remote);
158 git_config_set(cfg_name, "true");
159 free(cfg_name);
160 }
161
162 /*
163 * Record the initial filter-spec in the config as
164 * the default for subsequent fetches from this remote.
165 *
166 * TODO: record it into remote.<name>.partialclonefilter
167 */
168 core_partial_clone_filter_default =
169 xstrdup(filter_options->filter_spec);
170 git_config_set("core.partialclonefilter",
171 core_partial_clone_filter_default);
172
173 /* Make sure the config info are reset */
174 promisor_remote_reinit();
175}
176
177void partial_clone_get_default_filter_spec(
178 struct list_objects_filter_options *filter_options)
179{
180 /*
181 * Parse default value, but silently ignore it if it is invalid.
182 */
183 if (!core_partial_clone_filter_default)
184 return;
185 gently_parse_list_objects_filter(filter_options,
186 core_partial_clone_filter_default,
187 NULL);
188}