submodule-config.con commit submodule: support reading .gitmodules when it's not in the working tree (76e9bdc)
   1#include "cache.h"
   2#include "dir.h"
   3#include "repository.h"
   4#include "config.h"
   5#include "submodule-config.h"
   6#include "submodule.h"
   7#include "strbuf.h"
   8#include "object-store.h"
   9#include "parse-options.h"
  10
  11/*
  12 * submodule cache lookup structure
  13 * There is one shared set of 'struct submodule' entries which can be
  14 * looked up by their sha1 blob id of the .gitmodules file and either
  15 * using path or name as key.
  16 * for_path stores submodule entries with path as key
  17 * for_name stores submodule entries with name as key
  18 */
  19struct submodule_cache {
  20        struct hashmap for_path;
  21        struct hashmap for_name;
  22        unsigned initialized:1;
  23        unsigned gitmodules_read:1;
  24};
  25
  26/*
  27 * thin wrapper struct needed to insert 'struct submodule' entries to
  28 * the hashmap
  29 */
  30struct submodule_entry {
  31        struct hashmap_entry ent;
  32        struct submodule *config;
  33};
  34
  35enum lookup_type {
  36        lookup_name,
  37        lookup_path
  38};
  39
  40static int config_path_cmp(const void *unused_cmp_data,
  41                           const void *entry,
  42                           const void *entry_or_key,
  43                           const void *unused_keydata)
  44{
  45        const struct submodule_entry *a = entry;
  46        const struct submodule_entry *b = entry_or_key;
  47
  48        return strcmp(a->config->path, b->config->path) ||
  49               oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
  50}
  51
  52static int config_name_cmp(const void *unused_cmp_data,
  53                           const void *entry,
  54                           const void *entry_or_key,
  55                           const void *unused_keydata)
  56{
  57        const struct submodule_entry *a = entry;
  58        const struct submodule_entry *b = entry_or_key;
  59
  60        return strcmp(a->config->name, b->config->name) ||
  61               oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
  62}
  63
  64static struct submodule_cache *submodule_cache_alloc(void)
  65{
  66        return xcalloc(1, sizeof(struct submodule_cache));
  67}
  68
  69static void submodule_cache_init(struct submodule_cache *cache)
  70{
  71        hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
  72        hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
  73        cache->initialized = 1;
  74}
  75
  76static void free_one_config(struct submodule_entry *entry)
  77{
  78        free((void *) entry->config->path);
  79        free((void *) entry->config->name);
  80        free((void *) entry->config->branch);
  81        free((void *) entry->config->update_strategy.command);
  82        free(entry->config);
  83}
  84
  85static void submodule_cache_clear(struct submodule_cache *cache)
  86{
  87        struct hashmap_iter iter;
  88        struct submodule_entry *entry;
  89
  90        if (!cache->initialized)
  91                return;
  92
  93        /*
  94         * We iterate over the name hash here to be symmetric with the
  95         * allocation of struct submodule entries. Each is allocated by
  96         * their .gitmodules blob sha1 and submodule name.
  97         */
  98        hashmap_iter_init(&cache->for_name, &iter);
  99        while ((entry = hashmap_iter_next(&iter)))
 100                free_one_config(entry);
 101
 102        hashmap_free(&cache->for_path, 1);
 103        hashmap_free(&cache->for_name, 1);
 104        cache->initialized = 0;
 105        cache->gitmodules_read = 0;
 106}
 107
 108void submodule_cache_free(struct submodule_cache *cache)
 109{
 110        submodule_cache_clear(cache);
 111        free(cache);
 112}
 113
 114static unsigned int hash_oid_string(const struct object_id *oid,
 115                                    const char *string)
 116{
 117        return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
 118}
 119
 120static void cache_put_path(struct submodule_cache *cache,
 121                           struct submodule *submodule)
 122{
 123        unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
 124                                            submodule->path);
 125        struct submodule_entry *e = xmalloc(sizeof(*e));
 126        hashmap_entry_init(e, hash);
 127        e->config = submodule;
 128        hashmap_put(&cache->for_path, e);
 129}
 130
 131static void cache_remove_path(struct submodule_cache *cache,
 132                              struct submodule *submodule)
 133{
 134        unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
 135                                            submodule->path);
 136        struct submodule_entry e;
 137        struct submodule_entry *removed;
 138        hashmap_entry_init(&e, hash);
 139        e.config = submodule;
 140        removed = hashmap_remove(&cache->for_path, &e, NULL);
 141        free(removed);
 142}
 143
 144static void cache_add(struct submodule_cache *cache,
 145                      struct submodule *submodule)
 146{
 147        unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
 148                                            submodule->name);
 149        struct submodule_entry *e = xmalloc(sizeof(*e));
 150        hashmap_entry_init(e, hash);
 151        e->config = submodule;
 152        hashmap_add(&cache->for_name, e);
 153}
 154
 155static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
 156                const struct object_id *gitmodules_oid, const char *path)
 157{
 158        struct submodule_entry *entry;
 159        unsigned int hash = hash_oid_string(gitmodules_oid, path);
 160        struct submodule_entry key;
 161        struct submodule key_config;
 162
 163        oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
 164        key_config.path = path;
 165
 166        hashmap_entry_init(&key, hash);
 167        key.config = &key_config;
 168
 169        entry = hashmap_get(&cache->for_path, &key, NULL);
 170        if (entry)
 171                return entry->config;
 172        return NULL;
 173}
 174
 175static struct submodule *cache_lookup_name(struct submodule_cache *cache,
 176                const struct object_id *gitmodules_oid, const char *name)
 177{
 178        struct submodule_entry *entry;
 179        unsigned int hash = hash_oid_string(gitmodules_oid, name);
 180        struct submodule_entry key;
 181        struct submodule key_config;
 182
 183        oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
 184        key_config.name = name;
 185
 186        hashmap_entry_init(&key, hash);
 187        key.config = &key_config;
 188
 189        entry = hashmap_get(&cache->for_name, &key, NULL);
 190        if (entry)
 191                return entry->config;
 192        return NULL;
 193}
 194
 195int check_submodule_name(const char *name)
 196{
 197        /* Disallow empty names */
 198        if (!*name)
 199                return -1;
 200
 201        /*
 202         * Look for '..' as a path component. Check both '/' and '\\' as
 203         * separators rather than is_dir_sep(), because we want the name rules
 204         * to be consistent across platforms.
 205         */
 206        goto in_component; /* always start inside component */
 207        while (*name) {
 208                char c = *name++;
 209                if (c == '/' || c == '\\') {
 210in_component:
 211                        if (name[0] == '.' && name[1] == '.' &&
 212                            (!name[2] || name[2] == '/' || name[2] == '\\'))
 213                                return -1;
 214                }
 215        }
 216
 217        return 0;
 218}
 219
 220static int name_and_item_from_var(const char *var, struct strbuf *name,
 221                                  struct strbuf *item)
 222{
 223        const char *subsection, *key;
 224        int subsection_len, parse;
 225        parse = parse_config_key(var, "submodule", &subsection,
 226                        &subsection_len, &key);
 227        if (parse < 0 || !subsection)
 228                return 0;
 229
 230        strbuf_add(name, subsection, subsection_len);
 231        if (check_submodule_name(name->buf) < 0) {
 232                warning(_("ignoring suspicious submodule name: %s"), name->buf);
 233                strbuf_release(name);
 234                return 0;
 235        }
 236
 237        strbuf_addstr(item, key);
 238
 239        return 1;
 240}
 241
 242static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
 243                const struct object_id *gitmodules_oid, const char *name)
 244{
 245        struct submodule *submodule;
 246        struct strbuf name_buf = STRBUF_INIT;
 247
 248        submodule = cache_lookup_name(cache, gitmodules_oid, name);
 249        if (submodule)
 250                return submodule;
 251
 252        submodule = xmalloc(sizeof(*submodule));
 253
 254        strbuf_addstr(&name_buf, name);
 255        submodule->name = strbuf_detach(&name_buf, NULL);
 256
 257        submodule->path = NULL;
 258        submodule->url = NULL;
 259        submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
 260        submodule->update_strategy.command = NULL;
 261        submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
 262        submodule->ignore = NULL;
 263        submodule->branch = NULL;
 264        submodule->recommend_shallow = -1;
 265
 266        oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
 267
 268        cache_add(cache, submodule);
 269
 270        return submodule;
 271}
 272
 273static int parse_fetch_recurse(const char *opt, const char *arg,
 274                               int die_on_error)
 275{
 276        switch (git_parse_maybe_bool(arg)) {
 277        case 1:
 278                return RECURSE_SUBMODULES_ON;
 279        case 0:
 280                return RECURSE_SUBMODULES_OFF;
 281        default:
 282                if (!strcmp(arg, "on-demand"))
 283                        return RECURSE_SUBMODULES_ON_DEMAND;
 284
 285                if (die_on_error)
 286                        die("bad %s argument: %s", opt, arg);
 287                else
 288                        return RECURSE_SUBMODULES_ERROR;
 289        }
 290}
 291
 292int parse_submodule_fetchjobs(const char *var, const char *value)
 293{
 294        int fetchjobs = git_config_int(var, value);
 295        if (fetchjobs < 0)
 296                die(_("negative values not allowed for submodule.fetchjobs"));
 297        return fetchjobs;
 298}
 299
 300int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 301{
 302        return parse_fetch_recurse(opt, arg, 1);
 303}
 304
 305int option_fetch_parse_recurse_submodules(const struct option *opt,
 306                                          const char *arg, int unset)
 307{
 308        int *v;
 309
 310        if (!opt->value)
 311                return -1;
 312
 313        v = opt->value;
 314
 315        if (unset) {
 316                *v = RECURSE_SUBMODULES_OFF;
 317        } else {
 318                if (arg)
 319                        *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
 320                else
 321                        *v = RECURSE_SUBMODULES_ON;
 322        }
 323        return 0;
 324}
 325
 326static int parse_update_recurse(const char *opt, const char *arg,
 327                                int die_on_error)
 328{
 329        switch (git_parse_maybe_bool(arg)) {
 330        case 1:
 331                return RECURSE_SUBMODULES_ON;
 332        case 0:
 333                return RECURSE_SUBMODULES_OFF;
 334        default:
 335                if (die_on_error)
 336                        die("bad %s argument: %s", opt, arg);
 337                return RECURSE_SUBMODULES_ERROR;
 338        }
 339}
 340
 341int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
 342{
 343        return parse_update_recurse(opt, arg, 1);
 344}
 345
 346static int parse_push_recurse(const char *opt, const char *arg,
 347                               int die_on_error)
 348{
 349        switch (git_parse_maybe_bool(arg)) {
 350        case 1:
 351                /* There's no simple "on" value when pushing */
 352                if (die_on_error)
 353                        die("bad %s argument: %s", opt, arg);
 354                else
 355                        return RECURSE_SUBMODULES_ERROR;
 356        case 0:
 357                return RECURSE_SUBMODULES_OFF;
 358        default:
 359                if (!strcmp(arg, "on-demand"))
 360                        return RECURSE_SUBMODULES_ON_DEMAND;
 361                else if (!strcmp(arg, "check"))
 362                        return RECURSE_SUBMODULES_CHECK;
 363                else if (!strcmp(arg, "only"))
 364                        return RECURSE_SUBMODULES_ONLY;
 365                else if (die_on_error)
 366                        die("bad %s argument: %s", opt, arg);
 367                else
 368                        return RECURSE_SUBMODULES_ERROR;
 369        }
 370}
 371
 372int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
 373{
 374        return parse_push_recurse(opt, arg, 1);
 375}
 376
 377static void warn_multiple_config(const struct object_id *treeish_name,
 378                                 const char *name, const char *option)
 379{
 380        const char *commit_string = "WORKTREE";
 381        if (treeish_name)
 382                commit_string = oid_to_hex(treeish_name);
 383        warning("%s:.gitmodules, multiple configurations found for "
 384                        "'submodule.%s.%s'. Skipping second one!",
 385                        commit_string, name, option);
 386}
 387
 388struct parse_config_parameter {
 389        struct submodule_cache *cache;
 390        const struct object_id *treeish_name;
 391        const struct object_id *gitmodules_oid;
 392        int overwrite;
 393};
 394
 395static int parse_config(const char *var, const char *value, void *data)
 396{
 397        struct parse_config_parameter *me = data;
 398        struct submodule *submodule;
 399        struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
 400        int ret = 0;
 401
 402        /* this also ensures that we only parse submodule entries */
 403        if (!name_and_item_from_var(var, &name, &item))
 404                return 0;
 405
 406        submodule = lookup_or_create_by_name(me->cache,
 407                                             me->gitmodules_oid,
 408                                             name.buf);
 409
 410        if (!strcmp(item.buf, "path")) {
 411                if (!value)
 412                        ret = config_error_nonbool(var);
 413                else if (!me->overwrite && submodule->path)
 414                        warn_multiple_config(me->treeish_name, submodule->name,
 415                                        "path");
 416                else {
 417                        if (submodule->path)
 418                                cache_remove_path(me->cache, submodule);
 419                        free((void *) submodule->path);
 420                        submodule->path = xstrdup(value);
 421                        cache_put_path(me->cache, submodule);
 422                }
 423        } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
 424                /* when parsing worktree configurations we can die early */
 425                int die_on_error = is_null_oid(me->gitmodules_oid);
 426                if (!me->overwrite &&
 427                    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
 428                        warn_multiple_config(me->treeish_name, submodule->name,
 429                                        "fetchrecursesubmodules");
 430                else
 431                        submodule->fetch_recurse = parse_fetch_recurse(
 432                                                                var, value,
 433                                                                die_on_error);
 434        } else if (!strcmp(item.buf, "ignore")) {
 435                if (!value)
 436                        ret = config_error_nonbool(var);
 437                else if (!me->overwrite && submodule->ignore)
 438                        warn_multiple_config(me->treeish_name, submodule->name,
 439                                        "ignore");
 440                else if (strcmp(value, "untracked") &&
 441                         strcmp(value, "dirty") &&
 442                         strcmp(value, "all") &&
 443                         strcmp(value, "none"))
 444                        warning("Invalid parameter '%s' for config option "
 445                                        "'submodule.%s.ignore'", value, name.buf);
 446                else {
 447                        free((void *) submodule->ignore);
 448                        submodule->ignore = xstrdup(value);
 449                }
 450        } else if (!strcmp(item.buf, "url")) {
 451                if (!value) {
 452                        ret = config_error_nonbool(var);
 453                } else if (!me->overwrite && submodule->url) {
 454                        warn_multiple_config(me->treeish_name, submodule->name,
 455                                        "url");
 456                } else {
 457                        free((void *) submodule->url);
 458                        submodule->url = xstrdup(value);
 459                }
 460        } else if (!strcmp(item.buf, "update")) {
 461                if (!value)
 462                        ret = config_error_nonbool(var);
 463                else if (!me->overwrite &&
 464                         submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
 465                        warn_multiple_config(me->treeish_name, submodule->name,
 466                                             "update");
 467                else if (parse_submodule_update_strategy(value,
 468                         &submodule->update_strategy) < 0)
 469                                die(_("invalid value for %s"), var);
 470        } else if (!strcmp(item.buf, "shallow")) {
 471                if (!me->overwrite && submodule->recommend_shallow != -1)
 472                        warn_multiple_config(me->treeish_name, submodule->name,
 473                                             "shallow");
 474                else
 475                        submodule->recommend_shallow =
 476                                git_config_bool(var, value);
 477        } else if (!strcmp(item.buf, "branch")) {
 478                if (!me->overwrite && submodule->branch)
 479                        warn_multiple_config(me->treeish_name, submodule->name,
 480                                             "branch");
 481                else {
 482                        free((void *)submodule->branch);
 483                        submodule->branch = xstrdup(value);
 484                }
 485        }
 486
 487        strbuf_release(&name);
 488        strbuf_release(&item);
 489
 490        return ret;
 491}
 492
 493static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
 494                                     struct object_id *gitmodules_oid,
 495                                     struct strbuf *rev)
 496{
 497        int ret = 0;
 498
 499        if (is_null_oid(treeish_name)) {
 500                oidclr(gitmodules_oid);
 501                return 1;
 502        }
 503
 504        strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
 505        if (get_oid(rev->buf, gitmodules_oid) >= 0)
 506                ret = 1;
 507
 508        return ret;
 509}
 510
 511/* This does a lookup of a submodule configuration by name or by path
 512 * (key) with on-demand reading of the appropriate .gitmodules from
 513 * revisions.
 514 */
 515static const struct submodule *config_from(struct submodule_cache *cache,
 516                const struct object_id *treeish_name, const char *key,
 517                enum lookup_type lookup_type)
 518{
 519        struct strbuf rev = STRBUF_INIT;
 520        unsigned long config_size;
 521        char *config = NULL;
 522        struct object_id oid;
 523        enum object_type type;
 524        const struct submodule *submodule = NULL;
 525        struct parse_config_parameter parameter;
 526
 527        /*
 528         * If any parameter except the cache is a NULL pointer just
 529         * return the first submodule. Can be used to check whether
 530         * there are any submodules parsed.
 531         */
 532        if (!treeish_name || !key) {
 533                struct hashmap_iter iter;
 534                struct submodule_entry *entry;
 535
 536                entry = hashmap_iter_first(&cache->for_name, &iter);
 537                if (!entry)
 538                        return NULL;
 539                return entry->config;
 540        }
 541
 542        if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
 543                goto out;
 544
 545        switch (lookup_type) {
 546        case lookup_name:
 547                submodule = cache_lookup_name(cache, &oid, key);
 548                break;
 549        case lookup_path:
 550                submodule = cache_lookup_path(cache, &oid, key);
 551                break;
 552        }
 553        if (submodule)
 554                goto out;
 555
 556        config = read_object_file(&oid, &type, &config_size);
 557        if (!config || type != OBJ_BLOB)
 558                goto out;
 559
 560        /* fill the submodule config into the cache */
 561        parameter.cache = cache;
 562        parameter.treeish_name = treeish_name;
 563        parameter.gitmodules_oid = &oid;
 564        parameter.overwrite = 0;
 565        git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
 566                        config, config_size, &parameter, NULL);
 567        strbuf_release(&rev);
 568        free(config);
 569
 570        switch (lookup_type) {
 571        case lookup_name:
 572                return cache_lookup_name(cache, &oid, key);
 573        case lookup_path:
 574                return cache_lookup_path(cache, &oid, key);
 575        default:
 576                return NULL;
 577        }
 578
 579out:
 580        strbuf_release(&rev);
 581        free(config);
 582        return submodule;
 583}
 584
 585static void submodule_cache_check_init(struct repository *repo)
 586{
 587        if (repo->submodule_cache && repo->submodule_cache->initialized)
 588                return;
 589
 590        if (!repo->submodule_cache)
 591                repo->submodule_cache = submodule_cache_alloc();
 592
 593        submodule_cache_init(repo->submodule_cache);
 594}
 595
 596/*
 597 * Note: This function is private for a reason, the '.gitmodules' file should
 598 * not be used as as a mechanism to retrieve arbitrary configuration stored in
 599 * the repository.
 600 *
 601 * Runs the provided config function on the '.gitmodules' file found in the
 602 * working directory.
 603 */
 604static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
 605{
 606        if (repo->worktree) {
 607                struct git_config_source config_source = { 0 };
 608                const struct config_options opts = { 0 };
 609                struct object_id oid;
 610                char *file;
 611
 612                file = repo_worktree_path(repo, GITMODULES_FILE);
 613                if (file_exists(file)) {
 614                        config_source.file = file;
 615                } else if (repo->submodule_prefix) {
 616                        /*
 617                         * When get_oid and config_with_options, used below,
 618                         * become able to work on a specific repository, this
 619                         * warning branch can be removed.
 620                         */
 621                        warning("nested submodules without %s in the working tree are not supported yet",
 622                                GITMODULES_FILE);
 623                        goto out;
 624                } else if (get_oid(GITMODULES_INDEX, &oid) >= 0) {
 625                        config_source.blob = GITMODULES_INDEX;
 626                } else if (get_oid(GITMODULES_HEAD, &oid) >= 0) {
 627                        config_source.blob = GITMODULES_HEAD;
 628                } else {
 629                        goto out;
 630                }
 631
 632                config_with_options(fn, data, &config_source, &opts);
 633
 634out:
 635                free(file);
 636        }
 637}
 638
 639static int gitmodules_cb(const char *var, const char *value, void *data)
 640{
 641        struct repository *repo = data;
 642        struct parse_config_parameter parameter;
 643
 644        parameter.cache = repo->submodule_cache;
 645        parameter.treeish_name = NULL;
 646        parameter.gitmodules_oid = &null_oid;
 647        parameter.overwrite = 1;
 648
 649        return parse_config(var, value, &parameter);
 650}
 651
 652void repo_read_gitmodules(struct repository *repo)
 653{
 654        submodule_cache_check_init(repo);
 655
 656        if (repo_read_index(repo) < 0)
 657                return;
 658
 659        if (!is_gitmodules_unmerged(repo->index))
 660                config_from_gitmodules(gitmodules_cb, repo, repo);
 661
 662        repo->submodule_cache->gitmodules_read = 1;
 663}
 664
 665void gitmodules_config_oid(const struct object_id *commit_oid)
 666{
 667        struct strbuf rev = STRBUF_INIT;
 668        struct object_id oid;
 669
 670        submodule_cache_check_init(the_repository);
 671
 672        if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
 673                git_config_from_blob_oid(gitmodules_cb, rev.buf,
 674                                         &oid, the_repository);
 675        }
 676        strbuf_release(&rev);
 677
 678        the_repository->submodule_cache->gitmodules_read = 1;
 679}
 680
 681static void gitmodules_read_check(struct repository *repo)
 682{
 683        submodule_cache_check_init(repo);
 684
 685        /* read the repo's .gitmodules file if it hasn't been already */
 686        if (!repo->submodule_cache->gitmodules_read)
 687                repo_read_gitmodules(repo);
 688}
 689
 690const struct submodule *submodule_from_name(struct repository *r,
 691                                            const struct object_id *treeish_name,
 692                const char *name)
 693{
 694        gitmodules_read_check(r);
 695        return config_from(r->submodule_cache, treeish_name, name, lookup_name);
 696}
 697
 698const struct submodule *submodule_from_path(struct repository *r,
 699                                            const struct object_id *treeish_name,
 700                const char *path)
 701{
 702        gitmodules_read_check(r);
 703        return config_from(r->submodule_cache, treeish_name, path, lookup_path);
 704}
 705
 706void submodule_free(struct repository *r)
 707{
 708        if (r->submodule_cache)
 709                submodule_cache_clear(r->submodule_cache);
 710}
 711
 712static int config_print_callback(const char *var, const char *value, void *cb_data)
 713{
 714        char *wanted_key = cb_data;
 715
 716        if (!strcmp(wanted_key, var))
 717                printf("%s\n", value);
 718
 719        return 0;
 720}
 721
 722int print_config_from_gitmodules(struct repository *repo, const char *key)
 723{
 724        int ret;
 725        char *store_key;
 726
 727        ret = git_config_parse_key(key, &store_key, NULL);
 728        if (ret < 0)
 729                return CONFIG_INVALID_KEY;
 730
 731        config_from_gitmodules(config_print_callback, repo, store_key);
 732
 733        free(store_key);
 734        return 0;
 735}
 736
 737int config_set_in_gitmodules_file_gently(const char *key, const char *value)
 738{
 739        int ret;
 740
 741        ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
 742        if (ret < 0)
 743                /* Maybe the user already did that, don't error out here */
 744                warning(_("Could not update .gitmodules entry %s"), key);
 745
 746        return ret;
 747}
 748
 749struct fetch_config {
 750        int *max_children;
 751        int *recurse_submodules;
 752};
 753
 754static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
 755{
 756        struct fetch_config *config = cb;
 757        if (!strcmp(var, "submodule.fetchjobs")) {
 758                *(config->max_children) = parse_submodule_fetchjobs(var, value);
 759                return 0;
 760        } else if (!strcmp(var, "fetch.recursesubmodules")) {
 761                *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
 762                return 0;
 763        }
 764
 765        return 0;
 766}
 767
 768void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
 769{
 770        struct fetch_config config = {
 771                .max_children = max_children,
 772                .recurse_submodules = recurse_submodules
 773        };
 774        config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
 775}
 776
 777static int gitmodules_update_clone_config(const char *var, const char *value,
 778                                          void *cb)
 779{
 780        int *max_jobs = cb;
 781        if (!strcmp(var, "submodule.fetchjobs"))
 782                *max_jobs = parse_submodule_fetchjobs(var, value);
 783        return 0;
 784}
 785
 786void update_clone_config_from_gitmodules(int *max_jobs)
 787{
 788        config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
 789}