submodule-config.con commit grep: recurse in-process using 'struct repository' (f9ee2fc)
   1#include "cache.h"
   2#include "repository.h"
   3#include "config.h"
   4#include "submodule-config.h"
   5#include "submodule.h"
   6#include "strbuf.h"
   7#include "parse-options.h"
   8
   9/*
  10 * submodule cache lookup structure
  11 * There is one shared set of 'struct submodule' entries which can be
  12 * looked up by their sha1 blob id of the .gitmodule file and either
  13 * using path or name as key.
  14 * for_path stores submodule entries with path as key
  15 * for_name stores submodule entries with name as key
  16 */
  17struct submodule_cache {
  18        struct hashmap for_path;
  19        struct hashmap for_name;
  20        unsigned initialized:1;
  21};
  22
  23/*
  24 * thin wrapper struct needed to insert 'struct submodule' entries to
  25 * the hashmap
  26 */
  27struct submodule_entry {
  28        struct hashmap_entry ent;
  29        struct submodule *config;
  30};
  31
  32enum lookup_type {
  33        lookup_name,
  34        lookup_path
  35};
  36
  37static int config_path_cmp(const void *unused_cmp_data,
  38                           const struct submodule_entry *a,
  39                           const struct submodule_entry *b,
  40                           const void *unused_keydata)
  41{
  42        return strcmp(a->config->path, b->config->path) ||
  43               hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
  44}
  45
  46static int config_name_cmp(const void *unused_cmp_data,
  47                           const struct submodule_entry *a,
  48                           const struct submodule_entry *b,
  49                           const void *unused_keydata)
  50{
  51        return strcmp(a->config->name, b->config->name) ||
  52               hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
  53}
  54
  55static struct submodule_cache *submodule_cache_alloc(void)
  56{
  57        return xcalloc(1, sizeof(struct submodule_cache));
  58}
  59
  60static void submodule_cache_init(struct submodule_cache *cache)
  61{
  62        hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
  63        hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
  64        cache->initialized = 1;
  65}
  66
  67static void free_one_config(struct submodule_entry *entry)
  68{
  69        free((void *) entry->config->path);
  70        free((void *) entry->config->name);
  71        free((void *) entry->config->branch);
  72        free((void *) entry->config->update_strategy.command);
  73        free(entry->config);
  74}
  75
  76static void submodule_cache_clear(struct submodule_cache *cache)
  77{
  78        struct hashmap_iter iter;
  79        struct submodule_entry *entry;
  80
  81        if (!cache->initialized)
  82                return;
  83
  84        /*
  85         * We iterate over the name hash here to be symmetric with the
  86         * allocation of struct submodule entries. Each is allocated by
  87         * their .gitmodule blob sha1 and submodule name.
  88         */
  89        hashmap_iter_init(&cache->for_name, &iter);
  90        while ((entry = hashmap_iter_next(&iter)))
  91                free_one_config(entry);
  92
  93        hashmap_free(&cache->for_path, 1);
  94        hashmap_free(&cache->for_name, 1);
  95        cache->initialized = 0;
  96}
  97
  98void submodule_cache_free(struct submodule_cache *cache)
  99{
 100        submodule_cache_clear(cache);
 101        free(cache);
 102}
 103
 104static unsigned int hash_sha1_string(const unsigned char *sha1,
 105                                     const char *string)
 106{
 107        return memhash(sha1, 20) + strhash(string);
 108}
 109
 110static void cache_put_path(struct submodule_cache *cache,
 111                           struct submodule *submodule)
 112{
 113        unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
 114                                             submodule->path);
 115        struct submodule_entry *e = xmalloc(sizeof(*e));
 116        hashmap_entry_init(e, hash);
 117        e->config = submodule;
 118        hashmap_put(&cache->for_path, e);
 119}
 120
 121static void cache_remove_path(struct submodule_cache *cache,
 122                              struct submodule *submodule)
 123{
 124        unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
 125                                             submodule->path);
 126        struct submodule_entry e;
 127        struct submodule_entry *removed;
 128        hashmap_entry_init(&e, hash);
 129        e.config = submodule;
 130        removed = hashmap_remove(&cache->for_path, &e, NULL);
 131        free(removed);
 132}
 133
 134static void cache_add(struct submodule_cache *cache,
 135                      struct submodule *submodule)
 136{
 137        unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
 138                                             submodule->name);
 139        struct submodule_entry *e = xmalloc(sizeof(*e));
 140        hashmap_entry_init(e, hash);
 141        e->config = submodule;
 142        hashmap_add(&cache->for_name, e);
 143}
 144
 145static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
 146                const unsigned char *gitmodules_sha1, const char *path)
 147{
 148        struct submodule_entry *entry;
 149        unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
 150        struct submodule_entry key;
 151        struct submodule key_config;
 152
 153        hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
 154        key_config.path = path;
 155
 156        hashmap_entry_init(&key, hash);
 157        key.config = &key_config;
 158
 159        entry = hashmap_get(&cache->for_path, &key, NULL);
 160        if (entry)
 161                return entry->config;
 162        return NULL;
 163}
 164
 165static struct submodule *cache_lookup_name(struct submodule_cache *cache,
 166                const unsigned char *gitmodules_sha1, const char *name)
 167{
 168        struct submodule_entry *entry;
 169        unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
 170        struct submodule_entry key;
 171        struct submodule key_config;
 172
 173        hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
 174        key_config.name = name;
 175
 176        hashmap_entry_init(&key, hash);
 177        key.config = &key_config;
 178
 179        entry = hashmap_get(&cache->for_name, &key, NULL);
 180        if (entry)
 181                return entry->config;
 182        return NULL;
 183}
 184
 185static int name_and_item_from_var(const char *var, struct strbuf *name,
 186                                  struct strbuf *item)
 187{
 188        const char *subsection, *key;
 189        int subsection_len, parse;
 190        parse = parse_config_key(var, "submodule", &subsection,
 191                        &subsection_len, &key);
 192        if (parse < 0 || !subsection)
 193                return 0;
 194
 195        strbuf_add(name, subsection, subsection_len);
 196        strbuf_addstr(item, key);
 197
 198        return 1;
 199}
 200
 201static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
 202                const unsigned char *gitmodules_sha1, const char *name)
 203{
 204        struct submodule *submodule;
 205        struct strbuf name_buf = STRBUF_INIT;
 206
 207        submodule = cache_lookup_name(cache, gitmodules_sha1, name);
 208        if (submodule)
 209                return submodule;
 210
 211        submodule = xmalloc(sizeof(*submodule));
 212
 213        strbuf_addstr(&name_buf, name);
 214        submodule->name = strbuf_detach(&name_buf, NULL);
 215
 216        submodule->path = NULL;
 217        submodule->url = NULL;
 218        submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
 219        submodule->update_strategy.command = NULL;
 220        submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
 221        submodule->ignore = NULL;
 222        submodule->branch = NULL;
 223        submodule->recommend_shallow = -1;
 224
 225        hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
 226
 227        cache_add(cache, submodule);
 228
 229        return submodule;
 230}
 231
 232static int parse_fetch_recurse(const char *opt, const char *arg,
 233                               int die_on_error)
 234{
 235        switch (git_config_maybe_bool(opt, arg)) {
 236        case 1:
 237                return RECURSE_SUBMODULES_ON;
 238        case 0:
 239                return RECURSE_SUBMODULES_OFF;
 240        default:
 241                if (!strcmp(arg, "on-demand"))
 242                        return RECURSE_SUBMODULES_ON_DEMAND;
 243
 244                if (die_on_error)
 245                        die("bad %s argument: %s", opt, arg);
 246                else
 247                        return RECURSE_SUBMODULES_ERROR;
 248        }
 249}
 250
 251int parse_submodule_fetchjobs(const char *var, const char *value)
 252{
 253        int fetchjobs = git_config_int(var, value);
 254        if (fetchjobs < 0)
 255                die(_("negative values not allowed for submodule.fetchjobs"));
 256        return fetchjobs;
 257}
 258
 259int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 260{
 261        return parse_fetch_recurse(opt, arg, 1);
 262}
 263
 264int option_fetch_parse_recurse_submodules(const struct option *opt,
 265                                          const char *arg, int unset)
 266{
 267        int *v;
 268
 269        if (!opt->value)
 270                return -1;
 271
 272        v = opt->value;
 273
 274        if (unset) {
 275                *v = RECURSE_SUBMODULES_OFF;
 276        } else {
 277                if (arg)
 278                        *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
 279                else
 280                        *v = RECURSE_SUBMODULES_ON;
 281        }
 282        return 0;
 283}
 284
 285static int parse_update_recurse(const char *opt, const char *arg,
 286                                int die_on_error)
 287{
 288        switch (git_config_maybe_bool(opt, arg)) {
 289        case 1:
 290                return RECURSE_SUBMODULES_ON;
 291        case 0:
 292                return RECURSE_SUBMODULES_OFF;
 293        default:
 294                if (die_on_error)
 295                        die("bad %s argument: %s", opt, arg);
 296                return RECURSE_SUBMODULES_ERROR;
 297        }
 298}
 299
 300int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
 301{
 302        return parse_update_recurse(opt, arg, 1);
 303}
 304
 305static int parse_push_recurse(const char *opt, const char *arg,
 306                               int die_on_error)
 307{
 308        switch (git_config_maybe_bool(opt, arg)) {
 309        case 1:
 310                /* There's no simple "on" value when pushing */
 311                if (die_on_error)
 312                        die("bad %s argument: %s", opt, arg);
 313                else
 314                        return RECURSE_SUBMODULES_ERROR;
 315        case 0:
 316                return RECURSE_SUBMODULES_OFF;
 317        default:
 318                if (!strcmp(arg, "on-demand"))
 319                        return RECURSE_SUBMODULES_ON_DEMAND;
 320                else if (!strcmp(arg, "check"))
 321                        return RECURSE_SUBMODULES_CHECK;
 322                else if (!strcmp(arg, "only"))
 323                        return RECURSE_SUBMODULES_ONLY;
 324                else if (die_on_error)
 325                        die("bad %s argument: %s", opt, arg);
 326                else
 327                        return RECURSE_SUBMODULES_ERROR;
 328        }
 329}
 330
 331int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
 332{
 333        return parse_push_recurse(opt, arg, 1);
 334}
 335
 336static void warn_multiple_config(const unsigned char *treeish_name,
 337                                 const char *name, const char *option)
 338{
 339        const char *commit_string = "WORKTREE";
 340        if (treeish_name)
 341                commit_string = sha1_to_hex(treeish_name);
 342        warning("%s:.gitmodules, multiple configurations found for "
 343                        "'submodule.%s.%s'. Skipping second one!",
 344                        commit_string, name, option);
 345}
 346
 347struct parse_config_parameter {
 348        struct submodule_cache *cache;
 349        const unsigned char *treeish_name;
 350        const unsigned char *gitmodules_sha1;
 351        int overwrite;
 352};
 353
 354static int parse_config(const char *var, const char *value, void *data)
 355{
 356        struct parse_config_parameter *me = data;
 357        struct submodule *submodule;
 358        struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
 359        int ret = 0;
 360
 361        /* this also ensures that we only parse submodule entries */
 362        if (!name_and_item_from_var(var, &name, &item))
 363                return 0;
 364
 365        submodule = lookup_or_create_by_name(me->cache,
 366                                             me->gitmodules_sha1,
 367                                             name.buf);
 368
 369        if (!strcmp(item.buf, "path")) {
 370                if (!value)
 371                        ret = config_error_nonbool(var);
 372                else if (!me->overwrite && submodule->path)
 373                        warn_multiple_config(me->treeish_name, submodule->name,
 374                                        "path");
 375                else {
 376                        if (submodule->path)
 377                                cache_remove_path(me->cache, submodule);
 378                        free((void *) submodule->path);
 379                        submodule->path = xstrdup(value);
 380                        cache_put_path(me->cache, submodule);
 381                }
 382        } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
 383                /* when parsing worktree configurations we can die early */
 384                int die_on_error = is_null_sha1(me->gitmodules_sha1);
 385                if (!me->overwrite &&
 386                    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
 387                        warn_multiple_config(me->treeish_name, submodule->name,
 388                                        "fetchrecursesubmodules");
 389                else
 390                        submodule->fetch_recurse = parse_fetch_recurse(
 391                                                                var, value,
 392                                                                die_on_error);
 393        } else if (!strcmp(item.buf, "ignore")) {
 394                if (!value)
 395                        ret = config_error_nonbool(var);
 396                else if (!me->overwrite && submodule->ignore)
 397                        warn_multiple_config(me->treeish_name, submodule->name,
 398                                        "ignore");
 399                else if (strcmp(value, "untracked") &&
 400                         strcmp(value, "dirty") &&
 401                         strcmp(value, "all") &&
 402                         strcmp(value, "none"))
 403                        warning("Invalid parameter '%s' for config option "
 404                                        "'submodule.%s.ignore'", value, name.buf);
 405                else {
 406                        free((void *) submodule->ignore);
 407                        submodule->ignore = xstrdup(value);
 408                }
 409        } else if (!strcmp(item.buf, "url")) {
 410                if (!value) {
 411                        ret = config_error_nonbool(var);
 412                } else if (!me->overwrite && submodule->url) {
 413                        warn_multiple_config(me->treeish_name, submodule->name,
 414                                        "url");
 415                } else {
 416                        free((void *) submodule->url);
 417                        submodule->url = xstrdup(value);
 418                }
 419        } else if (!strcmp(item.buf, "update")) {
 420                if (!value)
 421                        ret = config_error_nonbool(var);
 422                else if (!me->overwrite &&
 423                         submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
 424                        warn_multiple_config(me->treeish_name, submodule->name,
 425                                             "update");
 426                else if (parse_submodule_update_strategy(value,
 427                         &submodule->update_strategy) < 0)
 428                                die(_("invalid value for %s"), var);
 429        } else if (!strcmp(item.buf, "shallow")) {
 430                if (!me->overwrite && submodule->recommend_shallow != -1)
 431                        warn_multiple_config(me->treeish_name, submodule->name,
 432                                             "shallow");
 433                else
 434                        submodule->recommend_shallow =
 435                                git_config_bool(var, value);
 436        } else if (!strcmp(item.buf, "branch")) {
 437                if (!me->overwrite && submodule->branch)
 438                        warn_multiple_config(me->treeish_name, submodule->name,
 439                                             "branch");
 440                else {
 441                        free((void *)submodule->branch);
 442                        submodule->branch = xstrdup(value);
 443                }
 444        }
 445
 446        strbuf_release(&name);
 447        strbuf_release(&item);
 448
 449        return ret;
 450}
 451
 452int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
 453                                      unsigned char *gitmodules_sha1,
 454                                      struct strbuf *rev)
 455{
 456        int ret = 0;
 457
 458        if (is_null_sha1(treeish_name)) {
 459                hashclr(gitmodules_sha1);
 460                return 1;
 461        }
 462
 463        strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
 464        if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
 465                ret = 1;
 466
 467        return ret;
 468}
 469
 470/* This does a lookup of a submodule configuration by name or by path
 471 * (key) with on-demand reading of the appropriate .gitmodules from
 472 * revisions.
 473 */
 474static const struct submodule *config_from(struct submodule_cache *cache,
 475                const unsigned char *treeish_name, const char *key,
 476                enum lookup_type lookup_type)
 477{
 478        struct strbuf rev = STRBUF_INIT;
 479        unsigned long config_size;
 480        char *config = NULL;
 481        unsigned char sha1[20];
 482        enum object_type type;
 483        const struct submodule *submodule = NULL;
 484        struct parse_config_parameter parameter;
 485
 486        /*
 487         * If any parameter except the cache is a NULL pointer just
 488         * return the first submodule. Can be used to check whether
 489         * there are any submodules parsed.
 490         */
 491        if (!treeish_name || !key) {
 492                struct hashmap_iter iter;
 493                struct submodule_entry *entry;
 494
 495                entry = hashmap_iter_first(&cache->for_name, &iter);
 496                if (!entry)
 497                        return NULL;
 498                return entry->config;
 499        }
 500
 501        if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
 502                goto out;
 503
 504        switch (lookup_type) {
 505        case lookup_name:
 506                submodule = cache_lookup_name(cache, sha1, key);
 507                break;
 508        case lookup_path:
 509                submodule = cache_lookup_path(cache, sha1, key);
 510                break;
 511        }
 512        if (submodule)
 513                goto out;
 514
 515        config = read_sha1_file(sha1, &type, &config_size);
 516        if (!config || type != OBJ_BLOB)
 517                goto out;
 518
 519        /* fill the submodule config into the cache */
 520        parameter.cache = cache;
 521        parameter.treeish_name = treeish_name;
 522        parameter.gitmodules_sha1 = sha1;
 523        parameter.overwrite = 0;
 524        git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
 525                        config, config_size, &parameter);
 526        strbuf_release(&rev);
 527        free(config);
 528
 529        switch (lookup_type) {
 530        case lookup_name:
 531                return cache_lookup_name(cache, sha1, key);
 532        case lookup_path:
 533                return cache_lookup_path(cache, sha1, key);
 534        default:
 535                return NULL;
 536        }
 537
 538out:
 539        strbuf_release(&rev);
 540        free(config);
 541        return submodule;
 542}
 543
 544static void submodule_cache_check_init(struct repository *repo)
 545{
 546        if (repo->submodule_cache && repo->submodule_cache->initialized)
 547                return;
 548
 549        if (!repo->submodule_cache)
 550                repo->submodule_cache = submodule_cache_alloc();
 551
 552        submodule_cache_init(repo->submodule_cache);
 553}
 554
 555int submodule_config_option(struct repository *repo,
 556                            const char *var, const char *value)
 557{
 558        struct parse_config_parameter parameter;
 559
 560        submodule_cache_check_init(repo);
 561
 562        parameter.cache = repo->submodule_cache;
 563        parameter.treeish_name = NULL;
 564        parameter.gitmodules_sha1 = null_sha1;
 565        parameter.overwrite = 1;
 566
 567        return parse_config(var, value, &parameter);
 568}
 569
 570int parse_submodule_config_option(const char *var, const char *value)
 571{
 572        return submodule_config_option(the_repository, var, value);
 573}
 574
 575const struct submodule *submodule_from_name(const unsigned char *treeish_name,
 576                const char *name)
 577{
 578        submodule_cache_check_init(the_repository);
 579        return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
 580}
 581
 582const struct submodule *submodule_from_path(const unsigned char *treeish_name,
 583                const char *path)
 584{
 585        submodule_cache_check_init(the_repository);
 586        return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
 587}
 588
 589const struct submodule *submodule_from_cache(struct repository *repo,
 590                                             const unsigned char *treeish_name,
 591                                             const char *key)
 592{
 593        submodule_cache_check_init(repo);
 594        return config_from(repo->submodule_cache, treeish_name,
 595                           key, lookup_path);
 596}
 597
 598void submodule_free(void)
 599{
 600        if (the_repository->submodule_cache)
 601                submodule_cache_clear(the_repository->submodule_cache);
 602}