864dd187866b407863877e307bd36837c23bd0bf
   1#include "builtin.h"
   2#include "cache.h"
   3#include "parse-options.h"
   4#include "quote.h"
   5#include "pathspec.h"
   6#include "dir.h"
   7#include "utf8.h"
   8#include "submodule.h"
   9#include "submodule-config.h"
  10#include "string-list.h"
  11#include "run-command.h"
  12
  13struct module_list {
  14        const struct cache_entry **entries;
  15        int alloc, nr;
  16};
  17#define MODULE_LIST_INIT { NULL, 0, 0 }
  18
  19static int module_list_compute(int argc, const char **argv,
  20                               const char *prefix,
  21                               struct pathspec *pathspec,
  22                               struct module_list *list)
  23{
  24        int i, result = 0;
  25        char *ps_matched = NULL;
  26        parse_pathspec(pathspec, 0,
  27                       PATHSPEC_PREFER_FULL |
  28                       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
  29                       prefix, argv);
  30
  31        if (pathspec->nr)
  32                ps_matched = xcalloc(pathspec->nr, 1);
  33
  34        if (read_cache() < 0)
  35                die(_("index file corrupt"));
  36
  37        for (i = 0; i < active_nr; i++) {
  38                const struct cache_entry *ce = active_cache[i];
  39
  40                if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
  41                                    0, ps_matched, 1) ||
  42                    !S_ISGITLINK(ce->ce_mode))
  43                        continue;
  44
  45                ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
  46                list->entries[list->nr++] = ce;
  47                while (i + 1 < active_nr &&
  48                       !strcmp(ce->name, active_cache[i + 1]->name))
  49                        /*
  50                         * Skip entries with the same name in different stages
  51                         * to make sure an entry is returned only once.
  52                         */
  53                        i++;
  54        }
  55
  56        if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
  57                result = -1;
  58
  59        free(ps_matched);
  60
  61        return result;
  62}
  63
  64static int module_list(int argc, const char **argv, const char *prefix)
  65{
  66        int i;
  67        struct pathspec pathspec;
  68        struct module_list list = MODULE_LIST_INIT;
  69
  70        struct option module_list_options[] = {
  71                OPT_STRING(0, "prefix", &prefix,
  72                           N_("path"),
  73                           N_("alternative anchor for relative paths")),
  74                OPT_END()
  75        };
  76
  77        const char *const git_submodule_helper_usage[] = {
  78                N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
  79                NULL
  80        };
  81
  82        argc = parse_options(argc, argv, prefix, module_list_options,
  83                             git_submodule_helper_usage, 0);
  84
  85        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
  86                printf("#unmatched\n");
  87                return 1;
  88        }
  89
  90        for (i = 0; i < list.nr; i++) {
  91                const struct cache_entry *ce = list.entries[i];
  92
  93                if (ce_stage(ce))
  94                        printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
  95                else
  96                        printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
  97
  98                utf8_fprintf(stdout, "%s\n", ce->name);
  99        }
 100        return 0;
 101}
 102
 103static int module_name(int argc, const char **argv, const char *prefix)
 104{
 105        const struct submodule *sub;
 106
 107        if (argc != 2)
 108                usage(_("git submodule--helper name <path>"));
 109
 110        gitmodules_config();
 111        sub = submodule_from_path(null_sha1, argv[1]);
 112
 113        if (!sub)
 114                die(_("no submodule mapping found in .gitmodules for path '%s'"),
 115                    argv[1]);
 116
 117        printf("%s\n", sub->name);
 118
 119        return 0;
 120}
 121static int clone_submodule(const char *path, const char *gitdir, const char *url,
 122                           const char *depth, const char *reference, int quiet)
 123{
 124        struct child_process cp;
 125        child_process_init(&cp);
 126
 127        argv_array_push(&cp.args, "clone");
 128        argv_array_push(&cp.args, "--no-checkout");
 129        if (quiet)
 130                argv_array_push(&cp.args, "--quiet");
 131        if (depth && *depth)
 132                argv_array_pushl(&cp.args, "--depth", depth, NULL);
 133        if (reference && *reference)
 134                argv_array_pushl(&cp.args, "--reference", reference, NULL);
 135        if (gitdir && *gitdir)
 136                argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
 137
 138        argv_array_push(&cp.args, url);
 139        argv_array_push(&cp.args, path);
 140
 141        cp.git_cmd = 1;
 142        cp.env = local_repo_env;
 143        cp.no_stdin = 1;
 144
 145        return run_command(&cp);
 146}
 147
 148static int module_clone(int argc, const char **argv, const char *prefix)
 149{
 150        const char *name = NULL, *url = NULL;
 151        const char *reference = NULL, *depth = NULL;
 152        int quiet = 0;
 153        FILE *submodule_dot_git;
 154        char *p, *path = NULL, *sm_gitdir;
 155        struct strbuf rel_path = STRBUF_INIT;
 156        struct strbuf sb = STRBUF_INIT;
 157
 158        struct option module_clone_options[] = {
 159                OPT_STRING(0, "prefix", &prefix,
 160                           N_("path"),
 161                           N_("alternative anchor for relative paths")),
 162                OPT_STRING(0, "path", &path,
 163                           N_("path"),
 164                           N_("where the new submodule will be cloned to")),
 165                OPT_STRING(0, "name", &name,
 166                           N_("string"),
 167                           N_("name of the new submodule")),
 168                OPT_STRING(0, "url", &url,
 169                           N_("string"),
 170                           N_("url where to clone the submodule from")),
 171                OPT_STRING(0, "reference", &reference,
 172                           N_("string"),
 173                           N_("reference repository")),
 174                OPT_STRING(0, "depth", &depth,
 175                           N_("string"),
 176                           N_("depth for shallow clones")),
 177                OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
 178                OPT_END()
 179        };
 180
 181        const char *const git_submodule_helper_usage[] = {
 182                N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
 183                   "[--reference <repository>] [--name <name>] [--url <url>]"
 184                   "[--depth <depth>] [--] [<path>...]"),
 185                NULL
 186        };
 187
 188        argc = parse_options(argc, argv, prefix, module_clone_options,
 189                             git_submodule_helper_usage, 0);
 190
 191        if (!path || !*path)
 192                die(_("submodule--helper: unspecified or empty --path"));
 193
 194        strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
 195        sm_gitdir = xstrdup(absolute_path(sb.buf));
 196        strbuf_reset(&sb);
 197
 198        if (!is_absolute_path(path)) {
 199                strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
 200                path = strbuf_detach(&sb, NULL);
 201        } else
 202                path = xstrdup(path);
 203
 204        if (!file_exists(sm_gitdir)) {
 205                if (safe_create_leading_directories_const(sm_gitdir) < 0)
 206                        die(_("could not create directory '%s'"), sm_gitdir);
 207                if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
 208                        die(_("clone of '%s' into submodule path '%s' failed"),
 209                            url, path);
 210        } else {
 211                if (safe_create_leading_directories_const(path) < 0)
 212                        die(_("could not create directory '%s'"), path);
 213                strbuf_addf(&sb, "%s/index", sm_gitdir);
 214                unlink_or_warn(sb.buf);
 215                strbuf_reset(&sb);
 216        }
 217
 218        /* Write a .git file in the submodule to redirect to the superproject. */
 219        strbuf_addf(&sb, "%s/.git", path);
 220        if (safe_create_leading_directories_const(sb.buf) < 0)
 221                die(_("could not create leading directories of '%s'"), sb.buf);
 222        submodule_dot_git = fopen(sb.buf, "w");
 223        if (!submodule_dot_git)
 224                die_errno(_("cannot open file '%s'"), sb.buf);
 225
 226        fprintf_or_die(submodule_dot_git, "gitdir: %s\n",
 227                       relative_path(sm_gitdir, path, &rel_path));
 228        if (fclose(submodule_dot_git))
 229                die(_("could not close file %s"), sb.buf);
 230        strbuf_reset(&sb);
 231        strbuf_reset(&rel_path);
 232
 233        /* Redirect the worktree of the submodule in the superproject's config */
 234        p = git_pathdup_submodule(path, "config");
 235        if (!p)
 236                die(_("could not get submodule directory for '%s'"), path);
 237        git_config_set_in_file(p, "core.worktree",
 238                               relative_path(path, sm_gitdir, &rel_path));
 239        strbuf_release(&sb);
 240        strbuf_release(&rel_path);
 241        free(sm_gitdir);
 242        free(path);
 243        free(p);
 244        return 0;
 245}
 246
 247struct submodule_update_clone {
 248        /* index into 'list', the list of submodules to look into for cloning */
 249        int current;
 250        struct module_list list;
 251        unsigned warn_if_uninitialized : 1;
 252
 253        /* update parameter passed via commandline */
 254        struct submodule_update_strategy update;
 255
 256        /* configuration parameters which are passed on to the children */
 257        int quiet;
 258        const char *reference;
 259        const char *depth;
 260        const char *recursive_prefix;
 261        const char *prefix;
 262
 263        /* Machine-readable status lines to be consumed by git-submodule.sh */
 264        struct string_list projectlines;
 265
 266        /* If we want to stop as fast as possible and return an error */
 267        unsigned quickstop : 1;
 268};
 269#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
 270        SUBMODULE_UPDATE_STRATEGY_INIT, 0, NULL, NULL, NULL, NULL, \
 271        STRING_LIST_INIT_DUP, 0}
 272
 273/**
 274 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
 275 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
 276 */
 277static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
 278                                           struct child_process *child,
 279                                           struct submodule_update_clone *suc,
 280                                           struct strbuf *out)
 281{
 282        const struct submodule *sub = NULL;
 283        struct strbuf displaypath_sb = STRBUF_INIT;
 284        struct strbuf sb = STRBUF_INIT;
 285        const char *displaypath = NULL;
 286        char *url = NULL;
 287        int needs_cloning = 0;
 288
 289        if (ce_stage(ce)) {
 290                if (suc->recursive_prefix)
 291                        strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
 292                else
 293                        strbuf_addf(&sb, "%s", ce->name);
 294                strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
 295                strbuf_addch(out, '\n');
 296                goto cleanup;
 297        }
 298
 299        sub = submodule_from_path(null_sha1, ce->name);
 300
 301        if (suc->recursive_prefix)
 302                displaypath = relative_path(suc->recursive_prefix,
 303                                            ce->name, &displaypath_sb);
 304        else
 305                displaypath = ce->name;
 306
 307        if (suc->update.type == SM_UPDATE_NONE
 308            || (suc->update.type == SM_UPDATE_UNSPECIFIED
 309                && sub->update_strategy.type == SM_UPDATE_NONE)) {
 310                strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
 311                strbuf_addch(out, '\n');
 312                goto cleanup;
 313        }
 314
 315        /*
 316         * Looking up the url in .git/config.
 317         * We must not fall back to .gitmodules as we only want
 318         * to process configured submodules.
 319         */
 320        strbuf_reset(&sb);
 321        strbuf_addf(&sb, "submodule.%s.url", sub->name);
 322        git_config_get_string(sb.buf, &url);
 323        if (!url) {
 324                /*
 325                 * Only mention uninitialized submodules when their
 326                 * path have been specified
 327                 */
 328                if (suc->warn_if_uninitialized) {
 329                        strbuf_addf(out,
 330                                _("Submodule path '%s' not initialized"),
 331                                displaypath);
 332                        strbuf_addch(out, '\n');
 333                        strbuf_addstr(out,
 334                                _("Maybe you want to use 'update --init'?"));
 335                        strbuf_addch(out, '\n');
 336                }
 337                goto cleanup;
 338        }
 339
 340        strbuf_reset(&sb);
 341        strbuf_addf(&sb, "%s/.git", ce->name);
 342        needs_cloning = !file_exists(sb.buf);
 343
 344        strbuf_reset(&sb);
 345        strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
 346                        sha1_to_hex(ce->sha1), ce_stage(ce),
 347                        needs_cloning, ce->name);
 348        string_list_append(&suc->projectlines, sb.buf);
 349
 350        if (!needs_cloning)
 351                goto cleanup;
 352
 353        child->git_cmd = 1;
 354        child->no_stdin = 1;
 355        child->stdout_to_stderr = 1;
 356        child->err = -1;
 357        argv_array_push(&child->args, "submodule--helper");
 358        argv_array_push(&child->args, "clone");
 359        if (suc->quiet)
 360                argv_array_push(&child->args, "--quiet");
 361        if (suc->prefix)
 362                argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
 363        argv_array_pushl(&child->args, "--path", sub->path, NULL);
 364        argv_array_pushl(&child->args, "--name", sub->name, NULL);
 365        argv_array_pushl(&child->args, "--url", url, NULL);
 366        if (suc->reference)
 367                argv_array_push(&child->args, suc->reference);
 368        if (suc->depth)
 369                argv_array_push(&child->args, suc->depth);
 370
 371cleanup:
 372        free(url);
 373        strbuf_reset(&displaypath_sb);
 374        strbuf_reset(&sb);
 375
 376        return needs_cloning;
 377}
 378
 379static int update_clone_get_next_task(struct child_process *child,
 380                                      struct strbuf *err,
 381                                      void *suc_cb,
 382                                      void **void_task_cb)
 383{
 384        struct submodule_update_clone *suc = suc_cb;
 385
 386        for (; suc->current < suc->list.nr; suc->current++) {
 387                const struct cache_entry *ce = suc->list.entries[suc->current];
 388                if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
 389                        suc->current++;
 390                        return 1;
 391                }
 392        }
 393        return 0;
 394}
 395
 396static int update_clone_start_failure(struct strbuf *err,
 397                                      void *suc_cb,
 398                                      void *void_task_cb)
 399{
 400        struct submodule_update_clone *suc = suc_cb;
 401        suc->quickstop = 1;
 402        return 1;
 403}
 404
 405static int update_clone_task_finished(int result,
 406                                      struct strbuf *err,
 407                                      void *suc_cb,
 408                                      void *void_task_cb)
 409{
 410        struct submodule_update_clone *suc = suc_cb;
 411
 412        if (!result)
 413                return 0;
 414
 415        suc->quickstop = 1;
 416        return 1;
 417}
 418
 419static int update_clone(int argc, const char **argv, const char *prefix)
 420{
 421        const char *update = NULL;
 422        int max_jobs = -1;
 423        struct string_list_item *item;
 424        struct pathspec pathspec;
 425        struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
 426
 427        struct option module_update_clone_options[] = {
 428                OPT_STRING(0, "prefix", &prefix,
 429                           N_("path"),
 430                           N_("path into the working tree")),
 431                OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
 432                           N_("path"),
 433                           N_("path into the working tree, across nested "
 434                              "submodule boundaries")),
 435                OPT_STRING(0, "update", &update,
 436                           N_("string"),
 437                           N_("rebase, merge, checkout or none")),
 438                OPT_STRING(0, "reference", &suc.reference, N_("repo"),
 439                           N_("reference repository")),
 440                OPT_STRING(0, "depth", &suc.depth, "<depth>",
 441                           N_("Create a shallow clone truncated to the "
 442                              "specified number of revisions")),
 443                OPT_INTEGER('j', "jobs", &max_jobs,
 444                            N_("parallel jobs")),
 445                OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
 446                OPT_END()
 447        };
 448
 449        const char *const git_submodule_helper_usage[] = {
 450                N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
 451                NULL
 452        };
 453        suc.prefix = prefix;
 454
 455        argc = parse_options(argc, argv, prefix, module_update_clone_options,
 456                             git_submodule_helper_usage, 0);
 457
 458        if (update)
 459                if (parse_submodule_update_strategy(update, &suc.update) < 0)
 460                        die(_("bad value for update parameter"));
 461
 462        if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
 463                return 1;
 464
 465        if (pathspec.nr)
 466                suc.warn_if_uninitialized = 1;
 467
 468        /* Overlay the parsed .gitmodules file with .git/config */
 469        gitmodules_config();
 470        git_config(submodule_config, NULL);
 471
 472        if (max_jobs < 0)
 473                max_jobs = parallel_submodules();
 474
 475        run_processes_parallel(max_jobs,
 476                               update_clone_get_next_task,
 477                               update_clone_start_failure,
 478                               update_clone_task_finished,
 479                               &suc);
 480
 481        /*
 482         * We saved the output and put it out all at once now.
 483         * That means:
 484         * - the listener does not have to interleave their (checkout)
 485         *   work with our fetching.  The writes involved in a
 486         *   checkout involve more straightforward sequential I/O.
 487         * - the listener can avoid doing any work if fetching failed.
 488         */
 489        if (suc.quickstop)
 490                return 1;
 491
 492        for_each_string_list_item(item, &suc.projectlines)
 493                utf8_fprintf(stdout, "%s", item->string);
 494
 495        return 0;
 496}
 497
 498struct cmd_struct {
 499        const char *cmd;
 500        int (*fn)(int, const char **, const char *);
 501};
 502
 503static struct cmd_struct commands[] = {
 504        {"list", module_list},
 505        {"name", module_name},
 506        {"clone", module_clone},
 507        {"update-clone", update_clone}
 508};
 509
 510int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
 511{
 512        int i;
 513        if (argc < 2)
 514                die(_("submodule--helper subcommand must be "
 515                      "called with a subcommand"));
 516
 517        for (i = 0; i < ARRAY_SIZE(commands); i++)
 518                if (!strcmp(argv[1], commands[i].cmd))
 519                        return commands[i].fn(argc - 1, argv + 1, prefix);
 520
 521        die(_("'%s' is not a valid submodule--helper "
 522              "subcommand"), argv[1]);
 523}