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