6eb13a5536d31b7b29bd906fa5d9ae3f1ee1c2af
   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
 185int check_submodule_name(const char *name)
 186{
 187        /* Disallow empty names */
 188        if (!*name)
 189                return -1;
 190
 191        /*
 192         * Look for '..' as a path component. Check both '/' and '\\' as
 193         * separators rather than is_dir_sep(), because we want the name rules
 194         * to be consistent across platforms.
 195         */
 196        goto in_component; /* always start inside component */
 197        while (*name) {
 198                char c = *name++;
 199                if (c == '/' || c == '\\') {
 200in_component:
 201                        if (name[0] == '.' && name[1] == '.' &&
 202                            (!name[2] || name[2] == '/' || name[2] == '\\'))
 203                                return -1;
 204                }
 205        }
 206
 207        return 0;
 208}
 209
 210static int name_and_item_from_var(const char *var, struct strbuf *name,
 211                                  struct strbuf *item)
 212{
 213        const char *subsection, *key;
 214        int subsection_len, parse;
 215        parse = parse_config_key(var, "submodule", &subsection,
 216                        &subsection_len, &key);
 217        if (parse < 0 || !subsection)
 218                return 0;
 219
 220        strbuf_add(name, subsection, subsection_len);
 221        if (check_submodule_name(name->buf) < 0) {
 222                warning(_("ignoring suspicious submodule name: %s"), name->buf);
 223                strbuf_release(name);
 224                return 0;
 225        }
 226
 227        strbuf_addstr(item, key);
 228
 229        return 1;
 230}
 231
 232static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
 233                const unsigned char *gitmodules_sha1, const char *name)
 234{
 235        struct submodule *submodule;
 236        struct strbuf name_buf = STRBUF_INIT;
 237
 238        submodule = cache_lookup_name(cache, gitmodules_sha1, name);
 239        if (submodule)
 240                return submodule;
 241
 242        submodule = xmalloc(sizeof(*submodule));
 243
 244        strbuf_addstr(&name_buf, name);
 245        submodule->name = strbuf_detach(&name_buf, NULL);
 246
 247        submodule->path = NULL;
 248        submodule->url = NULL;
 249        submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
 250        submodule->update_strategy.command = NULL;
 251        submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
 252        submodule->ignore = NULL;
 253        submodule->branch = NULL;
 254        submodule->recommend_shallow = -1;
 255
 256        hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
 257
 258        cache_add(cache, submodule);
 259
 260        return submodule;
 261}
 262
 263static int parse_fetch_recurse(const char *opt, const char *arg,
 264                               int die_on_error)
 265{
 266        switch (git_config_maybe_bool(opt, arg)) {
 267        case 1:
 268                return RECURSE_SUBMODULES_ON;
 269        case 0:
 270                return RECURSE_SUBMODULES_OFF;
 271        default:
 272                if (!strcmp(arg, "on-demand"))
 273                        return RECURSE_SUBMODULES_ON_DEMAND;
 274
 275                if (die_on_error)
 276                        die("bad %s argument: %s", opt, arg);
 277                else
 278                        return RECURSE_SUBMODULES_ERROR;
 279        }
 280}
 281
 282int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 283{
 284        return parse_fetch_recurse(opt, arg, 1);
 285}
 286
 287int option_fetch_parse_recurse_submodules(const struct option *opt,
 288                                          const char *arg, int unset)
 289{
 290        int *v;
 291
 292        if (!opt->value)
 293                return -1;
 294
 295        v = opt->value;
 296
 297        if (unset) {
 298                *v = RECURSE_SUBMODULES_OFF;
 299        } else {
 300                if (arg)
 301                        *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
 302                else
 303                        *v = RECURSE_SUBMODULES_ON;
 304        }
 305        return 0;
 306}
 307
 308static int parse_update_recurse(const char *opt, const char *arg,
 309                                int die_on_error)
 310{
 311        switch (git_config_maybe_bool(opt, arg)) {
 312        case 1:
 313                return RECURSE_SUBMODULES_ON;
 314        case 0:
 315                return RECURSE_SUBMODULES_OFF;
 316        default:
 317                if (die_on_error)
 318                        die("bad %s argument: %s", opt, arg);
 319                return RECURSE_SUBMODULES_ERROR;
 320        }
 321}
 322
 323int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
 324{
 325        return parse_update_recurse(opt, arg, 1);
 326}
 327
 328static int parse_push_recurse(const char *opt, const char *arg,
 329                               int die_on_error)
 330{
 331        switch (git_config_maybe_bool(opt, arg)) {
 332        case 1:
 333                /* There's no simple "on" value when pushing */
 334                if (die_on_error)
 335                        die("bad %s argument: %s", opt, arg);
 336                else
 337                        return RECURSE_SUBMODULES_ERROR;
 338        case 0:
 339                return RECURSE_SUBMODULES_OFF;
 340        default:
 341                if (!strcmp(arg, "on-demand"))
 342                        return RECURSE_SUBMODULES_ON_DEMAND;
 343                else if (!strcmp(arg, "check"))
 344                        return RECURSE_SUBMODULES_CHECK;
 345                else if (!strcmp(arg, "only"))
 346                        return RECURSE_SUBMODULES_ONLY;
 347                else if (die_on_error)
 348                        die("bad %s argument: %s", opt, arg);
 349                else
 350                        return RECURSE_SUBMODULES_ERROR;
 351        }
 352}
 353
 354int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
 355{
 356        return parse_push_recurse(opt, arg, 1);
 357}
 358
 359static void warn_multiple_config(const unsigned char *treeish_name,
 360                                 const char *name, const char *option)
 361{
 362        const char *commit_string = "WORKTREE";
 363        if (treeish_name)
 364                commit_string = sha1_to_hex(treeish_name);
 365        warning("%s:.gitmodules, multiple configurations found for "
 366                        "'submodule.%s.%s'. Skipping second one!",
 367                        commit_string, name, option);
 368}
 369
 370static void warn_command_line_option(const char *var, const char *value)
 371{
 372        warning(_("ignoring '%s' which may be interpreted as"
 373                  " a command-line option: %s"), var, value);
 374}
 375
 376struct parse_config_parameter {
 377        struct submodule_cache *cache;
 378        const unsigned char *treeish_name;
 379        const unsigned char *gitmodules_sha1;
 380        int overwrite;
 381};
 382
 383static int parse_config(const char *var, const char *value, void *data)
 384{
 385        struct parse_config_parameter *me = data;
 386        struct submodule *submodule;
 387        struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
 388        int ret = 0;
 389
 390        /* this also ensures that we only parse submodule entries */
 391        if (!name_and_item_from_var(var, &name, &item))
 392                return 0;
 393
 394        submodule = lookup_or_create_by_name(me->cache,
 395                                             me->gitmodules_sha1,
 396                                             name.buf);
 397
 398        if (!strcmp(item.buf, "path")) {
 399                if (!value)
 400                        ret = config_error_nonbool(var);
 401                else if (!me->overwrite && submodule->path)
 402                        warn_multiple_config(me->treeish_name, submodule->name,
 403                                        "path");
 404                else {
 405                        if (submodule->path)
 406                                cache_remove_path(me->cache, submodule);
 407                        free((void *) submodule->path);
 408                        submodule->path = xstrdup(value);
 409                        cache_put_path(me->cache, submodule);
 410                }
 411        } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
 412                /* when parsing worktree configurations we can die early */
 413                int die_on_error = is_null_sha1(me->gitmodules_sha1);
 414                if (!me->overwrite &&
 415                    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
 416                        warn_multiple_config(me->treeish_name, submodule->name,
 417                                        "fetchrecursesubmodules");
 418                else
 419                        submodule->fetch_recurse = parse_fetch_recurse(
 420                                                                var, value,
 421                                                                die_on_error);
 422        } else if (!strcmp(item.buf, "ignore")) {
 423                if (!value)
 424                        ret = config_error_nonbool(var);
 425                else if (!me->overwrite && submodule->ignore)
 426                        warn_multiple_config(me->treeish_name, submodule->name,
 427                                        "ignore");
 428                else if (strcmp(value, "untracked") &&
 429                         strcmp(value, "dirty") &&
 430                         strcmp(value, "all") &&
 431                         strcmp(value, "none"))
 432                        warning("Invalid parameter '%s' for config option "
 433                                        "'submodule.%s.ignore'", value, name.buf);
 434                else {
 435                        free((void *) submodule->ignore);
 436                        submodule->ignore = xstrdup(value);
 437                }
 438        } else if (!strcmp(item.buf, "url")) {
 439                if (!value) {
 440                        ret = config_error_nonbool(var);
 441                } else if (looks_like_command_line_option(value)) {
 442                        warn_command_line_option(var, value);
 443                } else if (!me->overwrite && submodule->url) {
 444                        warn_multiple_config(me->treeish_name, submodule->name,
 445                                        "url");
 446                } else {
 447                        free((void *) submodule->url);
 448                        submodule->url = xstrdup(value);
 449                }
 450        } else if (!strcmp(item.buf, "update")) {
 451                if (!value)
 452                        ret = config_error_nonbool(var);
 453                else if (!me->overwrite &&
 454                         submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
 455                        warn_multiple_config(me->treeish_name, submodule->name,
 456                                             "update");
 457                else if (parse_submodule_update_strategy(value,
 458                         &submodule->update_strategy) < 0)
 459                                die(_("invalid value for %s"), var);
 460        } else if (!strcmp(item.buf, "shallow")) {
 461                if (!me->overwrite && submodule->recommend_shallow != -1)
 462                        warn_multiple_config(me->treeish_name, submodule->name,
 463                                             "shallow");
 464                else
 465                        submodule->recommend_shallow =
 466                                git_config_bool(var, value);
 467        } else if (!strcmp(item.buf, "branch")) {
 468                if (!me->overwrite && submodule->branch)
 469                        warn_multiple_config(me->treeish_name, submodule->name,
 470                                             "branch");
 471                else {
 472                        free((void *)submodule->branch);
 473                        submodule->branch = xstrdup(value);
 474                }
 475        }
 476
 477        strbuf_release(&name);
 478        strbuf_release(&item);
 479
 480        return ret;
 481}
 482
 483int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
 484                                      unsigned char *gitmodules_sha1,
 485                                      struct strbuf *rev)
 486{
 487        int ret = 0;
 488
 489        if (is_null_sha1(treeish_name)) {
 490                hashclr(gitmodules_sha1);
 491                return 1;
 492        }
 493
 494        strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
 495        if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
 496                ret = 1;
 497
 498        return ret;
 499}
 500
 501/* This does a lookup of a submodule configuration by name or by path
 502 * (key) with on-demand reading of the appropriate .gitmodules from
 503 * revisions.
 504 */
 505static const struct submodule *config_from(struct submodule_cache *cache,
 506                const unsigned char *treeish_name, const char *key,
 507                enum lookup_type lookup_type)
 508{
 509        struct strbuf rev = STRBUF_INIT;
 510        unsigned long config_size;
 511        char *config = NULL;
 512        unsigned char sha1[20];
 513        enum object_type type;
 514        const struct submodule *submodule = NULL;
 515        struct parse_config_parameter parameter;
 516
 517        /*
 518         * If any parameter except the cache is a NULL pointer just
 519         * return the first submodule. Can be used to check whether
 520         * there are any submodules parsed.
 521         */
 522        if (!treeish_name || !key) {
 523                struct hashmap_iter iter;
 524                struct submodule_entry *entry;
 525
 526                entry = hashmap_iter_first(&cache->for_name, &iter);
 527                if (!entry)
 528                        return NULL;
 529                return entry->config;
 530        }
 531
 532        if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
 533                goto out;
 534
 535        switch (lookup_type) {
 536        case lookup_name:
 537                submodule = cache_lookup_name(cache, sha1, key);
 538                break;
 539        case lookup_path:
 540                submodule = cache_lookup_path(cache, sha1, key);
 541                break;
 542        }
 543        if (submodule)
 544                goto out;
 545
 546        config = read_sha1_file(sha1, &type, &config_size);
 547        if (!config || type != OBJ_BLOB)
 548                goto out;
 549
 550        /* fill the submodule config into the cache */
 551        parameter.cache = cache;
 552        parameter.treeish_name = treeish_name;
 553        parameter.gitmodules_sha1 = sha1;
 554        parameter.overwrite = 0;
 555        git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
 556                        config, config_size, &parameter);
 557        strbuf_release(&rev);
 558        free(config);
 559
 560        switch (lookup_type) {
 561        case lookup_name:
 562                return cache_lookup_name(cache, sha1, key);
 563        case lookup_path:
 564                return cache_lookup_path(cache, sha1, key);
 565        default:
 566                return NULL;
 567        }
 568
 569out:
 570        strbuf_release(&rev);
 571        free(config);
 572        return submodule;
 573}
 574
 575static void submodule_cache_check_init(struct repository *repo)
 576{
 577        if (repo->submodule_cache && repo->submodule_cache->initialized)
 578                return;
 579
 580        if (!repo->submodule_cache)
 581                repo->submodule_cache = submodule_cache_alloc();
 582
 583        submodule_cache_init(repo->submodule_cache);
 584}
 585
 586int submodule_config_option(struct repository *repo,
 587                            const char *var, const char *value)
 588{
 589        struct parse_config_parameter parameter;
 590
 591        submodule_cache_check_init(repo);
 592
 593        parameter.cache = repo->submodule_cache;
 594        parameter.treeish_name = NULL;
 595        parameter.gitmodules_sha1 = null_sha1;
 596        parameter.overwrite = 1;
 597
 598        return parse_config(var, value, &parameter);
 599}
 600
 601int parse_submodule_config_option(const char *var, const char *value)
 602{
 603        return submodule_config_option(the_repository, var, value);
 604}
 605
 606const struct submodule *submodule_from_name(const unsigned char *treeish_name,
 607                const char *name)
 608{
 609        submodule_cache_check_init(the_repository);
 610        return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
 611}
 612
 613const struct submodule *submodule_from_path(const unsigned char *treeish_name,
 614                const char *path)
 615{
 616        submodule_cache_check_init(the_repository);
 617        return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
 618}
 619
 620const struct submodule *submodule_from_cache(struct repository *repo,
 621                                             const unsigned char *treeish_name,
 622                                             const char *key)
 623{
 624        submodule_cache_check_init(repo);
 625        return config_from(repo->submodule_cache, treeish_name,
 626                           key, lookup_path);
 627}
 628
 629void submodule_free(void)
 630{
 631        if (the_repository->submodule_cache)
 632                submodule_cache_clear(the_repository->submodule_cache);
 633}