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