promisor-remote: skip move_to_tail when no-op
[git/git.git] / list-objects-filter-options.c
CommitLineData
25ec7bca
JH
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"
b14ed5ad 9#include "promisor-remote.h"
25ec7bca
JH
10
11/*
1dde5fa2 12 * Parse value of the argument to the "filter" keyword.
25ec7bca
JH
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
87c2d9d3
JS
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.
25ec7bca 25 */
1e1e39b3
JH
26static int gently_parse_list_objects_filter(
27 struct list_objects_filter_options *filter_options,
28 const char *arg,
29 struct strbuf *errbuf)
25ec7bca
JH
30{
31 const char *v0;
32
fa3d1b63
CC
33 if (!arg)
34 return 0;
35
1e1e39b3
JH
36 if (filter_options->choice) {
37 if (errbuf) {
1e1e39b3
JH
38 strbuf_addstr(
39 errbuf,
40 _("multiple filter-specs cannot be combined"));
41 }
42 return 1;
43 }
25ec7bca
JH
44
45 filter_options->filter_spec = strdup(arg);
46
47 if (!strcmp(arg, "blob:none")) {
48 filter_options->choice = LOFC_BLOB_NONE;
49 return 0;
25ec7bca 50
1e1e39b3
JH
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;
55 }
25ec7bca 56
bc5975d2 57 } else if (skip_prefix(arg, "tree:", &v0)) {
c813a7c3 58 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
bc5975d2
MD
59 if (errbuf) {
60 strbuf_addstr(
61 errbuf,
c813a7c3 62 _("expected 'tree:<depth>'"));
bc5975d2
MD
63 }
64 return 1;
65 }
c813a7c3 66 filter_options->choice = LOFC_TREE_DEPTH;
bc5975d2
MD
67 return 0;
68
1e1e39b3 69 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
25ec7bca
JH
70 struct object_context oc;
71 struct object_id sparse_oid;
72
73 /*
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.
77 */
3a7a698e 78 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
25ec7bca
JH
79 &sparse_oid, &oc))
80 filter_options->sparse_oid_value = oiddup(&sparse_oid);
81 filter_options->choice = LOFC_SPARSE_OID;
82 return 0;
25ec7bca 83
1e1e39b3 84 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
e693237e
CC
85 if (errbuf) {
86 strbuf_addstr(
87 errbuf,
88 _("sparse:path filters support has been dropped"));
89 }
90 return 1;
25ec7bca 91 }
5a59a230
NTND
92 /*
93 * Please update _git_fetch() in git-completion.bash when you
94 * add new filters
95 */
25ec7bca 96
cc0b05a4 97 if (errbuf)
5c03bc8b 98 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
cc0b05a4 99
1e1e39b3
JH
100 memset(filter_options, 0, sizeof(*filter_options));
101 return 1;
102}
103
104int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
105 const char *arg)
106{
107 struct strbuf buf = STRBUF_INIT;
108 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
109 die("%s", buf.buf);
25ec7bca
JH
110 return 0;
111}
112
113int opt_parse_list_objects_filter(const struct option *opt,
114 const char *arg, int unset)
115{
116 struct list_objects_filter_options *filter_options = opt->value;
117
4875c979 118 if (unset || !arg) {
aa57b871 119 list_objects_filter_set_no_filter(filter_options);
4875c979
JH
120 return 0;
121 }
25ec7bca
JH
122
123 return parse_list_objects_filter(filter_options, arg);
124}
4875c979 125
87c2d9d3
JS
126void expand_list_objects_filter_spec(
127 const struct list_objects_filter_options *filter,
128 struct strbuf *expanded_spec)
129{
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);
139}
140
4875c979
JH
141void list_objects_filter_release(
142 struct list_objects_filter_options *filter_options)
143{
144 free(filter_options->filter_spec);
145 free(filter_options->sparse_oid_value);
4875c979
JH
146 memset(filter_options, 0, sizeof(*filter_options));
147}
1e1e39b3
JH
148
149void partial_clone_register(
150 const char *remote,
151 const struct list_objects_filter_options *filter_options)
152{
b14ed5ad 153 char *cfg_name;
fa3d1b63 154 char *filter_name;
1e1e39b3 155
b14ed5ad
CC
156 /* Check if it is already registered */
157 if (!promisor_remote_find(remote)) {
158 git_config_set("core.repositoryformatversion", "1");
1e1e39b3 159
b14ed5ad
CC
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);
164 }
1e1e39b3
JH
165
166 /*
167 * Record the initial filter-spec in the config as
168 * the default for subsequent fetches from this remote.
169 */
fa3d1b63
CC
170 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
171 git_config_set(filter_name, filter_options->filter_spec);
172 free(filter_name);
b14ed5ad
CC
173
174 /* Make sure the config info are reset */
175 promisor_remote_reinit();
1e1e39b3
JH
176}
177
178void partial_clone_get_default_filter_spec(
fa3d1b63
CC
179 struct list_objects_filter_options *filter_options,
180 const char *remote)
1e1e39b3 181{
fa3d1b63
CC
182 struct promisor_remote *promisor = promisor_remote_find(remote);
183
1e1e39b3
JH
184 /*
185 * Parse default value, but silently ignore it if it is invalid.
186 */
fa3d1b63
CC
187 if (promisor)
188 gently_parse_list_objects_filter(filter_options,
189 promisor->partial_clone_filter,
190 NULL);
1e1e39b3 191}