list-objects-filter-options.con commit list-objects-filter-options: always supply *errbuf (842b005)
   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/*
  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>
  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 (although it's better to pass it through
  22 * expand_list_objects_filter_spec() first).  We also "intern" the arg for the
  23 * convenience of the current command.
  24 */
  25static int gently_parse_list_objects_filter(
  26        struct list_objects_filter_options *filter_options,
  27        const char *arg,
  28        struct strbuf *errbuf)
  29{
  30        const char *v0;
  31
  32        if (filter_options->choice) {
  33                strbuf_addstr(
  34                        errbuf, _("multiple filter-specs cannot be combined"));
  35                return 1;
  36        }
  37
  38        filter_options->filter_spec = strdup(arg);
  39
  40        if (!strcmp(arg, "blob:none")) {
  41                filter_options->choice = LOFC_BLOB_NONE;
  42                return 0;
  43
  44        } else if (skip_prefix(arg, "blob:limit=", &v0)) {
  45                if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
  46                        filter_options->choice = LOFC_BLOB_LIMIT;
  47                        return 0;
  48                }
  49
  50        } else if (skip_prefix(arg, "tree:", &v0)) {
  51                if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
  52                        strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
  53                        return 1;
  54                }
  55                filter_options->choice = LOFC_TREE_DEPTH;
  56                return 0;
  57
  58        } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
  59                struct object_context oc;
  60                struct object_id sparse_oid;
  61
  62                /*
  63                 * Try to parse <oid-expression> into an OID for the current
  64                 * command, but DO NOT complain if we don't have the blob or
  65                 * ref locally.
  66                 */
  67                if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
  68                                          &sparse_oid, &oc))
  69                        filter_options->sparse_oid_value = oiddup(&sparse_oid);
  70                filter_options->choice = LOFC_SPARSE_OID;
  71                return 0;
  72
  73        } else if (skip_prefix(arg, "sparse:path=", &v0)) {
  74                if (errbuf) {
  75                        strbuf_addstr(
  76                                errbuf,
  77                                _("sparse:path filters support has been dropped"));
  78                }
  79                return 1;
  80        }
  81        /*
  82         * Please update _git_fetch() in git-completion.bash when you
  83         * add new filters
  84         */
  85
  86        strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
  87
  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);
  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
 106        if (unset || !arg) {
 107                list_objects_filter_set_no_filter(filter_options);
 108                return 0;
 109        }
 110
 111        return parse_list_objects_filter(filter_options, arg);
 112}
 113
 114void expand_list_objects_filter_spec(
 115        const struct list_objects_filter_options *filter,
 116        struct strbuf *expanded_spec)
 117{
 118        strbuf_init(expanded_spec, strlen(filter->filter_spec));
 119        if (filter->choice == LOFC_BLOB_LIMIT)
 120                strbuf_addf(expanded_spec, "blob:limit=%lu",
 121                            filter->blob_limit_value);
 122        else if (filter->choice == LOFC_TREE_DEPTH)
 123                strbuf_addf(expanded_spec, "tree:%lu",
 124                            filter->tree_exclude_depth);
 125        else
 126                strbuf_addstr(expanded_spec, filter->filter_spec);
 127}
 128
 129void list_objects_filter_release(
 130        struct list_objects_filter_options *filter_options)
 131{
 132        free(filter_options->filter_spec);
 133        free(filter_options->sparse_oid_value);
 134        memset(filter_options, 0, sizeof(*filter_options));
 135}
 136
 137void partial_clone_register(
 138        const char *remote,
 139        const struct list_objects_filter_options *filter_options)
 140{
 141        /*
 142         * Record the name of the partial clone remote in the
 143         * config and in the global variable -- the latter is
 144         * used throughout to indicate that partial clone is
 145         * enabled and to expect missing objects.
 146         */
 147        if (repository_format_partial_clone &&
 148            *repository_format_partial_clone &&
 149            strcmp(remote, repository_format_partial_clone))
 150                die(_("cannot change partial clone promisor remote"));
 151
 152        git_config_set("core.repositoryformatversion", "1");
 153        git_config_set("extensions.partialclone", remote);
 154
 155        repository_format_partial_clone = xstrdup(remote);
 156
 157        /*
 158         * Record the initial filter-spec in the config as
 159         * the default for subsequent fetches from this remote.
 160         */
 161        core_partial_clone_filter_default =
 162                xstrdup(filter_options->filter_spec);
 163        git_config_set("core.partialclonefilter",
 164                       core_partial_clone_filter_default);
 165}
 166
 167void partial_clone_get_default_filter_spec(
 168        struct list_objects_filter_options *filter_options)
 169{
 170        struct strbuf errbuf = STRBUF_INIT;
 171
 172        /*
 173         * Parse default value, but silently ignore it if it is invalid.
 174         */
 175        if (!core_partial_clone_filter_default)
 176                return;
 177        gently_parse_list_objects_filter(filter_options,
 178                                         core_partial_clone_filter_default,
 179                                         &errbuf);
 180        strbuf_release(&errbuf);
 181}