list-objects-filter-options.con commit Remove fetch-object.{c,h} in favor of promisor-remote.{c,h} (db27dca)
   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 (!arg)
  34                return 0;
  35
  36        if (filter_options->choice) {
  37                if (errbuf) {
  38                        strbuf_addstr(
  39                                errbuf,
  40                                _("multiple filter-specs cannot be combined"));
  41                }
  42                return 1;
  43        }
  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;
  50
  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                }
  56
  57        } else if (skip_prefix(arg, "tree:", &v0)) {
  58                if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
  59                        if (errbuf) {
  60                                strbuf_addstr(
  61                                        errbuf,
  62                                        _("expected 'tree:<depth>'"));
  63                        }
  64                        return 1;
  65                }
  66                filter_options->choice = LOFC_TREE_DEPTH;
  67                return 0;
  68
  69        } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
  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                 */
  78                if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
  79                                          &sparse_oid, &oc))
  80                        filter_options->sparse_oid_value = oiddup(&sparse_oid);
  81                filter_options->choice = LOFC_SPARSE_OID;
  82                return 0;
  83
  84        } else if (skip_prefix(arg, "sparse:path=", &v0)) {
  85                if (errbuf) {
  86                        strbuf_addstr(
  87                                errbuf,
  88                                _("sparse:path filters support has been dropped"));
  89                }
  90                return 1;
  91        }
  92        /*
  93         * Please update _git_fetch() in git-completion.bash when you
  94         * add new filters
  95         */
  96
  97        if (errbuf)
  98                strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
  99
 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);
 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
 118        if (unset || !arg) {
 119                list_objects_filter_set_no_filter(filter_options);
 120                return 0;
 121        }
 122
 123        return parse_list_objects_filter(filter_options, arg);
 124}
 125
 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
 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);
 146        memset(filter_options, 0, sizeof(*filter_options));
 147}
 148
 149void partial_clone_register(
 150        const char *remote,
 151        const struct list_objects_filter_options *filter_options)
 152{
 153        char *cfg_name;
 154        char *filter_name;
 155
 156        /* Check if it is already registered */
 157        if (!promisor_remote_find(remote)) {
 158                git_config_set("core.repositoryformatversion", "1");
 159
 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        }
 165
 166        /*
 167         * Record the initial filter-spec in the config as
 168         * the default for subsequent fetches from this remote.
 169         */
 170        filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
 171        git_config_set(filter_name, filter_options->filter_spec);
 172        free(filter_name);
 173
 174        /* Make sure the config info are reset */
 175        promisor_remote_reinit();
 176}
 177
 178void partial_clone_get_default_filter_spec(
 179        struct list_objects_filter_options *filter_options,
 180        const char *remote)
 181{
 182        struct promisor_remote *promisor = promisor_remote_find(remote);
 183
 184        /*
 185         * Parse default value, but silently ignore it if it is invalid.
 186         */
 187        if (promisor)
 188                gently_parse_list_objects_filter(filter_options,
 189                                                 promisor->partial_clone_filter,
 190                                                 NULL);
 191}