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