]> git.ipfire.org Git - thirdparty/git.git/blame - list-objects-filter-options.c
tree:<depth>: skip some trees even when collecting omits
[thirdparty/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"
9
10/*
1dde5fa2 11 * Parse value of the argument to the "filter" keyword.
25ec7bca
JH
12 * On the command line this looks like:
13 * --filter=<arg>
14 * and in the pack protocol as:
15 * "filter" SP <arg>
16 *
17 * The filter keyword will be used by many commands.
18 * See Documentation/rev-list-options.txt for allowed values for <arg>.
19 *
20 * Capture the given arg as the "filter_spec". This can be forwarded to
21 * subordinate commands when necessary. We also "intern" the arg for
22 * the convenience of the current command.
23 */
1e1e39b3
JH
24static int gently_parse_list_objects_filter(
25 struct list_objects_filter_options *filter_options,
26 const char *arg,
27 struct strbuf *errbuf)
25ec7bca
JH
28{
29 const char *v0;
30
1e1e39b3
JH
31 if (filter_options->choice) {
32 if (errbuf) {
1e1e39b3
JH
33 strbuf_addstr(
34 errbuf,
35 _("multiple filter-specs cannot be combined"));
36 }
37 return 1;
38 }
25ec7bca
JH
39
40 filter_options->filter_spec = strdup(arg);
41
42 if (!strcmp(arg, "blob:none")) {
43 filter_options->choice = LOFC_BLOB_NONE;
44 return 0;
25ec7bca 45
1e1e39b3
JH
46 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
47 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
48 filter_options->choice = LOFC_BLOB_LIMIT;
49 return 0;
50 }
25ec7bca 51
bc5975d2 52 } else if (skip_prefix(arg, "tree:", &v0)) {
c813a7c3 53 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
bc5975d2
MD
54 if (errbuf) {
55 strbuf_addstr(
56 errbuf,
c813a7c3 57 _("expected 'tree:<depth>'"));
bc5975d2
MD
58 }
59 return 1;
60 }
c813a7c3 61 filter_options->choice = LOFC_TREE_DEPTH;
bc5975d2
MD
62 return 0;
63
1e1e39b3 64 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
25ec7bca
JH
65 struct object_context oc;
66 struct object_id sparse_oid;
67
68 /*
69 * Try to parse <oid-expression> into an OID for the current
70 * command, but DO NOT complain if we don't have the blob or
71 * ref locally.
72 */
73 if (!get_oid_with_context(v0, GET_OID_BLOB,
74 &sparse_oid, &oc))
75 filter_options->sparse_oid_value = oiddup(&sparse_oid);
76 filter_options->choice = LOFC_SPARSE_OID;
77 return 0;
25ec7bca 78
1e1e39b3 79 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
25ec7bca
JH
80 filter_options->choice = LOFC_SPARSE_PATH;
81 filter_options->sparse_path_value = strdup(v0);
82 return 0;
83 }
84
cc0b05a4 85 if (errbuf)
1e1e39b3 86 strbuf_addf(errbuf, "invalid filter-spec '%s'", arg);
cc0b05a4 87
1e1e39b3
JH
88 memset(filter_options, 0, sizeof(*filter_options));
89 return 1;
90}
91
92int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
93 const char *arg)
94{
95 struct strbuf buf = STRBUF_INIT;
96 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
97 die("%s", buf.buf);
25ec7bca
JH
98 return 0;
99}
100
101int opt_parse_list_objects_filter(const struct option *opt,
102 const char *arg, int unset)
103{
104 struct list_objects_filter_options *filter_options = opt->value;
105
4875c979 106 if (unset || !arg) {
aa57b871 107 list_objects_filter_set_no_filter(filter_options);
4875c979
JH
108 return 0;
109 }
25ec7bca
JH
110
111 return parse_list_objects_filter(filter_options, arg);
112}
4875c979
JH
113
114void list_objects_filter_release(
115 struct list_objects_filter_options *filter_options)
116{
117 free(filter_options->filter_spec);
118 free(filter_options->sparse_oid_value);
119 free(filter_options->sparse_path_value);
120 memset(filter_options, 0, sizeof(*filter_options));
121}
1e1e39b3
JH
122
123void partial_clone_register(
124 const char *remote,
125 const struct list_objects_filter_options *filter_options)
126{
127 /*
128 * Record the name of the partial clone remote in the
129 * config and in the global variable -- the latter is
130 * used throughout to indicate that partial clone is
131 * enabled and to expect missing objects.
132 */
133 if (repository_format_partial_clone &&
134 *repository_format_partial_clone &&
135 strcmp(remote, repository_format_partial_clone))
136 die(_("cannot change partial clone promisor remote"));
137
138 git_config_set("core.repositoryformatversion", "1");
139 git_config_set("extensions.partialclone", remote);
140
141 repository_format_partial_clone = xstrdup(remote);
142
143 /*
144 * Record the initial filter-spec in the config as
145 * the default for subsequent fetches from this remote.
146 */
147 core_partial_clone_filter_default =
148 xstrdup(filter_options->filter_spec);
149 git_config_set("core.partialclonefilter",
150 core_partial_clone_filter_default);
151}
152
153void partial_clone_get_default_filter_spec(
154 struct list_objects_filter_options *filter_options)
155{
156 /*
157 * Parse default value, but silently ignore it if it is invalid.
158 */
cac1137d
JT
159 if (!core_partial_clone_filter_default)
160 return;
1e1e39b3
JH
161 gently_parse_list_objects_filter(filter_options,
162 core_partial_clone_filter_default,
163 NULL);
164}