list-objects-filter-options: do not over-strbuf_init
[git.git] / list-objects-filter-options.c
blobd259bdb2c1490bf8fe3b36e61a9a982b63ab4f3c
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"
11 * Parse value of the argument to the "filter" keyword.
12 * On the command line this looks like:
13 * --filter=<arg>
14 * and in the pack protocol as:
15 * "filter" SP <arg>
17 * The filter keyword will be used by many commands.
18 * See Documentation/rev-list-options.txt for allowed values for <arg>.
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.
24 static int gently_parse_list_objects_filter(
25 struct list_objects_filter_options *filter_options,
26 const char *arg,
27 struct strbuf *errbuf)
29 const char *v0;
31 if (filter_options->choice) {
32 if (errbuf) {
33 strbuf_addstr(
34 errbuf,
35 _("multiple filter-specs cannot be combined"));
37 return 1;
40 filter_options->filter_spec = strdup(arg);
42 if (!strcmp(arg, "blob:none")) {
43 filter_options->choice = LOFC_BLOB_NONE;
44 return 0;
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;
52 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
53 struct object_context oc;
54 struct object_id sparse_oid;
57 * Try to parse <oid-expression> into an OID for the current
58 * command, but DO NOT complain if we don't have the blob or
59 * ref locally.
61 if (!get_oid_with_context(v0, GET_OID_BLOB,
62 &sparse_oid, &oc))
63 filter_options->sparse_oid_value = oiddup(&sparse_oid);
64 filter_options->choice = LOFC_SPARSE_OID;
65 return 0;
67 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
68 filter_options->choice = LOFC_SPARSE_PATH;
69 filter_options->sparse_path_value = strdup(v0);
70 return 0;
73 if (errbuf)
74 strbuf_addf(errbuf, "invalid filter-spec '%s'", arg);
76 memset(filter_options, 0, sizeof(*filter_options));
77 return 1;
80 int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
81 const char *arg)
83 struct strbuf buf = STRBUF_INIT;
84 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
85 die("%s", buf.buf);
86 return 0;
89 int opt_parse_list_objects_filter(const struct option *opt,
90 const char *arg, int unset)
92 struct list_objects_filter_options *filter_options = opt->value;
94 if (unset || !arg) {
95 list_objects_filter_set_no_filter(filter_options);
96 return 0;
99 return parse_list_objects_filter(filter_options, arg);
102 void list_objects_filter_release(
103 struct list_objects_filter_options *filter_options)
105 free(filter_options->filter_spec);
106 free(filter_options->sparse_oid_value);
107 free(filter_options->sparse_path_value);
108 memset(filter_options, 0, sizeof(*filter_options));
111 void partial_clone_register(
112 const char *remote,
113 const struct list_objects_filter_options *filter_options)
116 * Record the name of the partial clone remote in the
117 * config and in the global variable -- the latter is
118 * used throughout to indicate that partial clone is
119 * enabled and to expect missing objects.
121 if (repository_format_partial_clone &&
122 *repository_format_partial_clone &&
123 strcmp(remote, repository_format_partial_clone))
124 die(_("cannot change partial clone promisor remote"));
126 git_config_set("core.repositoryformatversion", "1");
127 git_config_set("extensions.partialclone", remote);
129 repository_format_partial_clone = xstrdup(remote);
132 * Record the initial filter-spec in the config as
133 * the default for subsequent fetches from this remote.
135 core_partial_clone_filter_default =
136 xstrdup(filter_options->filter_spec);
137 git_config_set("core.partialclonefilter",
138 core_partial_clone_filter_default);
141 void partial_clone_get_default_filter_spec(
142 struct list_objects_filter_options *filter_options)
145 * Parse default value, but silently ignore it if it is invalid.
147 if (!core_partial_clone_filter_default)
148 return;
149 gently_parse_list_objects_filter(filter_options,
150 core_partial_clone_filter_default,
151 NULL);