builtin / submodule--helper.con commit submodule: port submodule subcommand 'foreach' from shell to C (fc1b924)
   1#include "builtin.h"
   2#include "repository.h"
   3#include "cache.h"
   4#include "config.h"
   5#include "parse-options.h"
   6#include "quote.h"
   7#include "pathspec.h"
   8#include "dir.h"
   9#include "submodule.h"
  10#include "submodule-config.h"
  11#include "string-list.h"
  12#include "run-command.h"
  13#include "remote.h"
  14#include "refs.h"
  15#include "connect.h"
  16#include "revision.h"
  17#include "diffcore.h"
  18#include "diff.h"
  19#include "object-store.h"
  20
  21#define OPT_QUIET (1 << 0)
  22#define OPT_CACHED (1 << 1)
  23#define OPT_RECURSIVE (1 << 2)
  24#define OPT_FORCE (1 << 3)
  25
  26typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
  27                                  void *cb_data);
  28
  29static char *get_default_remote(void)
  30{
  31        char *dest = NULL, *ret;
  32        struct strbuf sb = STRBUF_INIT;
  33        const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  34
  35        if (!refname)
  36                die(_("No such ref: %s"), "HEAD");
  37
  38        /* detached HEAD */
  39        if (!strcmp(refname, "HEAD"))
  40                return xstrdup("origin");
  41
  42        if (!skip_prefix(refname, "refs/heads/", &refname))
  43                die(_("Expecting a full ref name, got %s"), refname);
  44
  45        strbuf_addf(&sb, "branch.%s.remote", refname);
  46        if (git_config_get_string(sb.buf, &dest))
  47                ret = xstrdup("origin");
  48        else
  49                ret = dest;
  50
  51        strbuf_release(&sb);
  52        return ret;
  53}
  54
  55static int print_default_remote(int argc, const char **argv, const char *prefix)
  56{
  57        const char *remote;
  58
  59        if (argc != 1)
  60                die(_("submodule--helper print-default-remote takes no arguments"));
  61
  62        remote = get_default_remote();
  63        if (remote)
  64                printf("%s\n", remote);
  65
  66        return 0;
  67}
  68
  69static int starts_with_dot_slash(const char *str)
  70{
  71        return str[0] == '.' && is_dir_sep(str[1]);
  72}
  73
  74static int starts_with_dot_dot_slash(const char *str)
  75{
  76        return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
  77}
  78
  79/*
  80 * Returns 1 if it was the last chop before ':'.
  81 */
  82static int chop_last_dir(char **remoteurl, int is_relative)
  83{
  84        char *rfind = find_last_dir_sep(*remoteurl);
  85        if (rfind) {
  86                *rfind = '\0';
  87                return 0;
  88        }
  89
  90        rfind = strrchr(*remoteurl, ':');
  91        if (rfind) {
  92                *rfind = '\0';
  93                return 1;
  94        }
  95
  96        if (is_relative || !strcmp(".", *remoteurl))
  97                die(_("cannot strip one component off url '%s'"),
  98                        *remoteurl);
  99
 100        free(*remoteurl);
 101        *remoteurl = xstrdup(".");
 102        return 0;
 103}
 104
 105/*
 106 * The `url` argument is the URL that navigates to the submodule origin
 107 * repo. When relative, this URL is relative to the superproject origin
 108 * URL repo. The `up_path` argument, if specified, is the relative
 109 * path that navigates from the submodule working tree to the superproject
 110 * working tree. Returns the origin URL of the submodule.
 111 *
 112 * Return either an absolute URL or filesystem path (if the superproject
 113 * origin URL is an absolute URL or filesystem path, respectively) or a
 114 * relative file system path (if the superproject origin URL is a relative
 115 * file system path).
 116 *
 117 * When the output is a relative file system path, the path is either
 118 * relative to the submodule working tree, if up_path is specified, or to
 119 * the superproject working tree otherwise.
 120 *
 121 * NEEDSWORK: This works incorrectly on the domain and protocol part.
 122 * remote_url      url              outcome          expectation
 123 * http://a.com/b  ../c             http://a.com/c   as is
 124 * http://a.com/b/ ../c             http://a.com/c   same as previous line, but
 125 *                                                   ignore trailing slash in url
 126 * http://a.com/b  ../../c          http://c         error out
 127 * http://a.com/b  ../../../c       http:/c          error out
 128 * http://a.com/b  ../../../../c    http:c           error out
 129 * http://a.com/b  ../../../../../c    .:c           error out
 130 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
 131 * when a local part has a colon in its path component, too.
 132 */
 133static char *relative_url(const char *remote_url,
 134                                const char *url,
 135                                const char *up_path)
 136{
 137        int is_relative = 0;
 138        int colonsep = 0;
 139        char *out;
 140        char *remoteurl = xstrdup(remote_url);
 141        struct strbuf sb = STRBUF_INIT;
 142        size_t len = strlen(remoteurl);
 143
 144        if (is_dir_sep(remoteurl[len-1]))
 145                remoteurl[len-1] = '\0';
 146
 147        if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
 148                is_relative = 0;
 149        else {
 150                is_relative = 1;
 151                /*
 152                 * Prepend a './' to ensure all relative
 153                 * remoteurls start with './' or '../'
 154                 */
 155                if (!starts_with_dot_slash(remoteurl) &&
 156                    !starts_with_dot_dot_slash(remoteurl)) {
 157                        strbuf_reset(&sb);
 158                        strbuf_addf(&sb, "./%s", remoteurl);
 159                        free(remoteurl);
 160                        remoteurl = strbuf_detach(&sb, NULL);
 161                }
 162        }
 163        /*
 164         * When the url starts with '../', remove that and the
 165         * last directory in remoteurl.
 166         */
 167        while (url) {
 168                if (starts_with_dot_dot_slash(url)) {
 169                        url += 3;
 170                        colonsep |= chop_last_dir(&remoteurl, is_relative);
 171                } else if (starts_with_dot_slash(url))
 172                        url += 2;
 173                else
 174                        break;
 175        }
 176        strbuf_reset(&sb);
 177        strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
 178        if (ends_with(url, "/"))
 179                strbuf_setlen(&sb, sb.len - 1);
 180        free(remoteurl);
 181
 182        if (starts_with_dot_slash(sb.buf))
 183                out = xstrdup(sb.buf + 2);
 184        else
 185                out = xstrdup(sb.buf);
 186        strbuf_reset(&sb);
 187
 188        if (!up_path || !is_relative)
 189                return out;
 190
 191        strbuf_addf(&sb, "%s%s", up_path, out);
 192        free(out);
 193        return strbuf_detach(&sb, NULL);
 194}
 195
 196static int resolve_relative_url(int argc, const char **argv, const char *prefix)
 197{
 198        char *remoteurl = NULL;
 199        char *remote = get_default_remote();
 200        const char *up_path = NULL;
 201        char *res;
 202        const char *url;
 203        struct strbuf sb = STRBUF_INIT;
 204
 205        if (argc != 2 && argc != 3)
 206                die("resolve-relative-url only accepts one or two arguments");
 207
 208        url = argv[1];
 209        strbuf_addf(&sb, "remote.%s.url", remote);
 210        free(remote);
 211
 212        if (git_config_get_string(sb.buf, &remoteurl))
 213                /* the repository is its own authoritative upstream */
 214                remoteurl = xgetcwd();
 215
 216        if (argc == 3)
 217                up_path = argv[2];
 218
 219        res = relative_url(remoteurl, url, up_path);
 220        puts(res);
 221        free(res);
 222        free(remoteurl);
 223        return 0;
 224}
 225
 226static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
 227{
 228        char *remoteurl, *res;
 229        const char *up_path, *url;
 230
 231        if (argc != 4)
 232                die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
 233
 234        up_path = argv[1];
 235        remoteurl = xstrdup(argv[2]);
 236        url = argv[3];
 237
 238        if (!strcmp(up_path, "(null)"))
 239                up_path = NULL;
 240
 241        res = relative_url(remoteurl, url, up_path);
 242        puts(res);
 243        free(res);
 244        free(remoteurl);
 245        return 0;
 246}
 247
 248/* the result should be freed by the caller. */
 249static char *get_submodule_displaypath(const char *path, const char *prefix)
 250{
 251        const char *super_prefix = get_super_prefix();
 252
 253        if (prefix && super_prefix) {
 254                BUG("cannot have prefix '%s' and superprefix '%s'",
 255                    prefix, super_prefix);
 256        } else if (prefix) {
 257                struct strbuf sb = STRBUF_INIT;
 258                char *displaypath = xstrdup(relative_path(path, prefix, &sb));
 259                strbuf_release(&sb);
 260                return displaypath;
 261        } else if (super_prefix) {
 262                return xstrfmt("%s%s", super_prefix, path);
 263        } else {
 264                return xstrdup(path);
 265        }
 266}
 267
 268static char *compute_rev_name(const char *sub_path, const char* object_id)
 269{
 270        struct strbuf sb = STRBUF_INIT;
 271        const char ***d;
 272
 273        static const char *describe_bare[] = { NULL };
 274
 275        static const char *describe_tags[] = { "--tags", NULL };
 276
 277        static const char *describe_contains[] = { "--contains", NULL };
 278
 279        static const char *describe_all_always[] = { "--all", "--always", NULL };
 280
 281        static const char **describe_argv[] = { describe_bare, describe_tags,
 282                                                describe_contains,
 283                                                describe_all_always, NULL };
 284
 285        for (d = describe_argv; *d; d++) {
 286                struct child_process cp = CHILD_PROCESS_INIT;
 287                prepare_submodule_repo_env(&cp.env_array);
 288                cp.dir = sub_path;
 289                cp.git_cmd = 1;
 290                cp.no_stderr = 1;
 291
 292                argv_array_push(&cp.args, "describe");
 293                argv_array_pushv(&cp.args, *d);
 294                argv_array_push(&cp.args, object_id);
 295
 296                if (!capture_command(&cp, &sb, 0)) {
 297                        strbuf_strip_suffix(&sb, "\n");
 298                        return strbuf_detach(&sb, NULL);
 299                }
 300        }
 301
 302        strbuf_release(&sb);
 303        return NULL;
 304}
 305
 306struct module_list {
 307        const struct cache_entry **entries;
 308        int alloc, nr;
 309};
 310#define MODULE_LIST_INIT { NULL, 0, 0 }
 311
 312static int module_list_compute(int argc, const char **argv,
 313                               const char *prefix,
 314                               struct pathspec *pathspec,
 315                               struct module_list *list)
 316{
 317        int i, result = 0;
 318        char *ps_matched = NULL;
 319        parse_pathspec(pathspec, 0,
 320                       PATHSPEC_PREFER_FULL,
 321                       prefix, argv);
 322
 323        if (pathspec->nr)
 324                ps_matched = xcalloc(pathspec->nr, 1);
 325
 326        if (read_cache() < 0)
 327                die(_("index file corrupt"));
 328
 329        for (i = 0; i < active_nr; i++) {
 330                const struct cache_entry *ce = active_cache[i];
 331
 332                if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
 333                                    0, ps_matched, 1) ||
 334                    !S_ISGITLINK(ce->ce_mode))
 335                        continue;
 336
 337                ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
 338                list->entries[list->nr++] = ce;
 339                while (i + 1 < active_nr &&
 340                       !strcmp(ce->name, active_cache[i + 1]->name))
 341                        /*
 342                         * Skip entries with the same name in different stages
 343                         * to make sure an entry is returned only once.
 344                         */
 345                        i++;
 346        }
 347
 348        if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
 349                result = -1;
 350
 351        free(ps_matched);
 352
 353        return result;
 354}
 355
 356static void module_list_active(struct module_list *list)
 357{
 358        int i;
 359        struct module_list active_modules = MODULE_LIST_INIT;
 360
 361        for (i = 0; i < list->nr; i++) {
 362                const struct cache_entry *ce = list->entries[i];
 363
 364                if (!is_submodule_active(the_repository, ce->name))
 365                        continue;
 366
 367                ALLOC_GROW(active_modules.entries,
 368                           active_modules.nr + 1,
 369                           active_modules.alloc);
 370                active_modules.entries[active_modules.nr++] = ce;
 371        }
 372
 373        free(list->entries);
 374        *list = active_modules;
 375}
 376
 377static char *get_up_path(const char *path)
 378{
 379        int i;
 380        struct strbuf sb = STRBUF_INIT;
 381
 382        for (i = count_slashes(path); i; i--)
 383                strbuf_addstr(&sb, "../");
 384
 385        /*
 386         * Check if 'path' ends with slash or not
 387         * for having the same output for dir/sub_dir
 388         * and dir/sub_dir/
 389         */
 390        if (!is_dir_sep(path[strlen(path) - 1]))
 391                strbuf_addstr(&sb, "../");
 392
 393        return strbuf_detach(&sb, NULL);
 394}
 395
 396static int module_list(int argc, const char **argv, const char *prefix)
 397{
 398        int i;
 399        struct pathspec pathspec;
 400        struct module_list list = MODULE_LIST_INIT;
 401
 402        struct option module_list_options[] = {
 403                OPT_STRING(0, "prefix", &prefix,
 404                           N_("path"),
 405                           N_("alternative anchor for relative paths")),
 406                OPT_END()
 407        };
 408
 409        const char *const git_submodule_helper_usage[] = {
 410                N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
 411                NULL
 412        };
 413
 414        argc = parse_options(argc, argv, prefix, module_list_options,
 415                             git_submodule_helper_usage, 0);
 416
 417        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 418                return 1;
 419
 420        for (i = 0; i < list.nr; i++) {
 421                const struct cache_entry *ce = list.entries[i];
 422
 423                if (ce_stage(ce))
 424                        printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
 425                else
 426                        printf("%06o %s %d\t", ce->ce_mode,
 427                               oid_to_hex(&ce->oid), ce_stage(ce));
 428
 429                fprintf(stdout, "%s\n", ce->name);
 430        }
 431        return 0;
 432}
 433
 434static void for_each_listed_submodule(const struct module_list *list,
 435                                      each_submodule_fn fn, void *cb_data)
 436{
 437        int i;
 438        for (i = 0; i < list->nr; i++)
 439                fn(list->entries[i], cb_data);
 440}
 441
 442struct cb_foreach {
 443        int argc;
 444        const char **argv;
 445        const char *prefix;
 446        int quiet;
 447        int recursive;
 448};
 449#define CB_FOREACH_INIT { 0 }
 450
 451static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
 452                                       void *cb_data)
 453{
 454        struct cb_foreach *info = cb_data;
 455        const char *path = list_item->name;
 456        const struct object_id *ce_oid = &list_item->oid;
 457
 458        const struct submodule *sub;
 459        struct child_process cp = CHILD_PROCESS_INIT;
 460        char *displaypath;
 461
 462        displaypath = get_submodule_displaypath(path, info->prefix);
 463
 464        sub = submodule_from_path(the_repository, &null_oid, path);
 465
 466        if (!sub)
 467                die(_("No url found for submodule path '%s' in .gitmodules"),
 468                        displaypath);
 469
 470        if (!is_submodule_populated_gently(path, NULL))
 471                goto cleanup;
 472
 473        prepare_submodule_repo_env(&cp.env_array);
 474
 475        /*
 476         * For the purpose of executing <command> in the submodule,
 477         * separate shell is used for the purpose of running the
 478         * child process.
 479         */
 480        cp.use_shell = 1;
 481        cp.dir = path;
 482
 483        /*
 484         * NEEDSWORK: the command currently has access to the variables $name,
 485         * $sm_path, $displaypath, $sha1 and $toplevel only when the command
 486         * contains a single argument. This is done for maintaining a faithful
 487         * translation from shell script.
 488         */
 489        if (info->argc == 1) {
 490                char *toplevel = xgetcwd();
 491                struct strbuf sb = STRBUF_INIT;
 492
 493                argv_array_pushf(&cp.env_array, "name=%s", sub->name);
 494                argv_array_pushf(&cp.env_array, "sm_path=%s", path);
 495                argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
 496                argv_array_pushf(&cp.env_array, "sha1=%s",
 497                                oid_to_hex(ce_oid));
 498                argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
 499
 500                /*
 501                 * Since the path variable was accessible from the script
 502                 * before porting, it is also made available after porting.
 503                 * The environment variable "PATH" has a very special purpose
 504                 * on windows. And since environment variables are
 505                 * case-insensitive in windows, it interferes with the
 506                 * existing PATH variable. Hence, to avoid that, we expose
 507                 * path via the args argv_array and not via env_array.
 508                 */
 509                sq_quote_buf(&sb, path);
 510                argv_array_pushf(&cp.args, "path=%s; %s",
 511                                 sb.buf, info->argv[0]);
 512                strbuf_release(&sb);
 513                free(toplevel);
 514        } else {
 515                argv_array_pushv(&cp.args, info->argv);
 516        }
 517
 518        if (!info->quiet)
 519                printf(_("Entering '%s'\n"), displaypath);
 520
 521        if (info->argv[0] && run_command(&cp))
 522                die(_("run_command returned non-zero status for %s\n."),
 523                        displaypath);
 524
 525        if (info->recursive) {
 526                struct child_process cpr = CHILD_PROCESS_INIT;
 527
 528                cpr.git_cmd = 1;
 529                cpr.dir = path;
 530                prepare_submodule_repo_env(&cpr.env_array);
 531
 532                argv_array_pushl(&cpr.args, "--super-prefix", NULL);
 533                argv_array_pushf(&cpr.args, "%s/", displaypath);
 534                argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
 535                                NULL);
 536
 537                if (info->quiet)
 538                        argv_array_push(&cpr.args, "--quiet");
 539
 540                argv_array_pushv(&cpr.args, info->argv);
 541
 542                if (run_command(&cpr))
 543                        die(_("run_command returned non-zero status while"
 544                                "recursing in the nested submodules of %s\n."),
 545                                displaypath);
 546        }
 547
 548cleanup:
 549        free(displaypath);
 550}
 551
 552static int module_foreach(int argc, const char **argv, const char *prefix)
 553{
 554        struct cb_foreach info = CB_FOREACH_INIT;
 555        struct pathspec pathspec;
 556        struct module_list list = MODULE_LIST_INIT;
 557
 558        struct option module_foreach_options[] = {
 559                OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
 560                OPT_BOOL(0, "recursive", &info.recursive,
 561                         N_("Recurse into nested submodules")),
 562                OPT_END()
 563        };
 564
 565        const char *const git_submodule_helper_usage[] = {
 566                N_("git submodule--helper foreach [--quiet] [--recursive] <command>"),
 567                NULL
 568        };
 569
 570        argc = parse_options(argc, argv, prefix, module_foreach_options,
 571                             git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN);
 572
 573        if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
 574                return 1;
 575
 576        info.argc = argc;
 577        info.argv = argv;
 578        info.prefix = prefix;
 579
 580        for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
 581
 582        return 0;
 583}
 584
 585struct init_cb {
 586        const char *prefix;
 587        unsigned int flags;
 588};
 589
 590#define INIT_CB_INIT { NULL, 0 }
 591
 592static void init_submodule(const char *path, const char *prefix,
 593                           unsigned int flags)
 594{
 595        const struct submodule *sub;
 596        struct strbuf sb = STRBUF_INIT;
 597        char *upd = NULL, *url = NULL, *displaypath;
 598
 599        displaypath = get_submodule_displaypath(path, prefix);
 600
 601        sub = submodule_from_path(the_repository, &null_oid, path);
 602
 603        if (!sub)
 604                die(_("No url found for submodule path '%s' in .gitmodules"),
 605                        displaypath);
 606
 607        /*
 608         * NEEDSWORK: In a multi-working-tree world, this needs to be
 609         * set in the per-worktree config.
 610         *
 611         * Set active flag for the submodule being initialized
 612         */
 613        if (!is_submodule_active(the_repository, path)) {
 614                strbuf_addf(&sb, "submodule.%s.active", sub->name);
 615                git_config_set_gently(sb.buf, "true");
 616                strbuf_reset(&sb);
 617        }
 618
 619        /*
 620         * Copy url setting when it is not set yet.
 621         * To look up the url in .git/config, we must not fall back to
 622         * .gitmodules, so look it up directly.
 623         */
 624        strbuf_addf(&sb, "submodule.%s.url", sub->name);
 625        if (git_config_get_string(sb.buf, &url)) {
 626                if (!sub->url)
 627                        die(_("No url found for submodule path '%s' in .gitmodules"),
 628                                displaypath);
 629
 630                url = xstrdup(sub->url);
 631
 632                /* Possibly a url relative to parent */
 633                if (starts_with_dot_dot_slash(url) ||
 634                    starts_with_dot_slash(url)) {
 635                        char *remoteurl, *relurl;
 636                        char *remote = get_default_remote();
 637                        struct strbuf remotesb = STRBUF_INIT;
 638                        strbuf_addf(&remotesb, "remote.%s.url", remote);
 639                        free(remote);
 640
 641                        if (git_config_get_string(remotesb.buf, &remoteurl)) {
 642                                warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
 643                                remoteurl = xgetcwd();
 644                        }
 645                        relurl = relative_url(remoteurl, url, NULL);
 646                        strbuf_release(&remotesb);
 647                        free(remoteurl);
 648                        free(url);
 649                        url = relurl;
 650                }
 651
 652                if (git_config_set_gently(sb.buf, url))
 653                        die(_("Failed to register url for submodule path '%s'"),
 654                            displaypath);
 655                if (!(flags & OPT_QUIET))
 656                        fprintf(stderr,
 657                                _("Submodule '%s' (%s) registered for path '%s'\n"),
 658                                sub->name, url, displaypath);
 659        }
 660        strbuf_reset(&sb);
 661
 662        /* Copy "update" setting when it is not set yet */
 663        strbuf_addf(&sb, "submodule.%s.update", sub->name);
 664        if (git_config_get_string(sb.buf, &upd) &&
 665            sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
 666                if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
 667                        fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
 668                                sub->name);
 669                        upd = xstrdup("none");
 670                } else
 671                        upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
 672
 673                if (git_config_set_gently(sb.buf, upd))
 674                        die(_("Failed to register update mode for submodule path '%s'"), displaypath);
 675        }
 676        strbuf_release(&sb);
 677        free(displaypath);
 678        free(url);
 679        free(upd);
 680}
 681
 682static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
 683{
 684        struct init_cb *info = cb_data;
 685        init_submodule(list_item->name, info->prefix, info->flags);
 686}
 687
 688static int module_init(int argc, const char **argv, const char *prefix)
 689{
 690        struct init_cb info = INIT_CB_INIT;
 691        struct pathspec pathspec;
 692        struct module_list list = MODULE_LIST_INIT;
 693        int quiet = 0;
 694
 695        struct option module_init_options[] = {
 696                OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
 697                OPT_END()
 698        };
 699
 700        const char *const git_submodule_helper_usage[] = {
 701                N_("git submodule--helper init [<path>]"),
 702                NULL
 703        };
 704
 705        argc = parse_options(argc, argv, prefix, module_init_options,
 706                             git_submodule_helper_usage, 0);
 707
 708        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 709                return 1;
 710
 711        /*
 712         * If there are no path args and submodule.active is set then,
 713         * by default, only initialize 'active' modules.
 714         */
 715        if (!argc && git_config_get_value_multi("submodule.active"))
 716                module_list_active(&list);
 717
 718        info.prefix = prefix;
 719        if (quiet)
 720                info.flags |= OPT_QUIET;
 721
 722        for_each_listed_submodule(&list, init_submodule_cb, &info);
 723
 724        return 0;
 725}
 726
 727struct status_cb {
 728        const char *prefix;
 729        unsigned int flags;
 730};
 731
 732#define STATUS_CB_INIT { NULL, 0 }
 733
 734static void print_status(unsigned int flags, char state, const char *path,
 735                         const struct object_id *oid, const char *displaypath)
 736{
 737        if (flags & OPT_QUIET)
 738                return;
 739
 740        printf("%c%s %s", state, oid_to_hex(oid), displaypath);
 741
 742        if (state == ' ' || state == '+') {
 743                const char *name = compute_rev_name(path, oid_to_hex(oid));
 744
 745                if (name)
 746                        printf(" (%s)", name);
 747        }
 748
 749        printf("\n");
 750}
 751
 752static int handle_submodule_head_ref(const char *refname,
 753                                     const struct object_id *oid, int flags,
 754                                     void *cb_data)
 755{
 756        struct object_id *output = cb_data;
 757        if (oid)
 758                oidcpy(output, oid);
 759
 760        return 0;
 761}
 762
 763static void status_submodule(const char *path, const struct object_id *ce_oid,
 764                             unsigned int ce_flags, const char *prefix,
 765                             unsigned int flags)
 766{
 767        char *displaypath;
 768        struct argv_array diff_files_args = ARGV_ARRAY_INIT;
 769        struct rev_info rev;
 770        int diff_files_result;
 771
 772        if (!submodule_from_path(the_repository, &null_oid, path))
 773                die(_("no submodule mapping found in .gitmodules for path '%s'"),
 774                      path);
 775
 776        displaypath = get_submodule_displaypath(path, prefix);
 777
 778        if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
 779                print_status(flags, 'U', path, &null_oid, displaypath);
 780                goto cleanup;
 781        }
 782
 783        if (!is_submodule_active(the_repository, path)) {
 784                print_status(flags, '-', path, ce_oid, displaypath);
 785                goto cleanup;
 786        }
 787
 788        argv_array_pushl(&diff_files_args, "diff-files",
 789                         "--ignore-submodules=dirty", "--quiet", "--",
 790                         path, NULL);
 791
 792        git_config(git_diff_basic_config, NULL);
 793        init_revisions(&rev, prefix);
 794        rev.abbrev = 0;
 795        diff_files_args.argc = setup_revisions(diff_files_args.argc,
 796                                               diff_files_args.argv,
 797                                               &rev, NULL);
 798        diff_files_result = run_diff_files(&rev, 0);
 799
 800        if (!diff_result_code(&rev.diffopt, diff_files_result)) {
 801                print_status(flags, ' ', path, ce_oid,
 802                             displaypath);
 803        } else if (!(flags & OPT_CACHED)) {
 804                struct object_id oid;
 805                struct ref_store *refs = get_submodule_ref_store(path);
 806
 807                if (!refs) {
 808                        print_status(flags, '-', path, ce_oid, displaypath);
 809                        goto cleanup;
 810                }
 811                if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
 812                        die(_("could not resolve HEAD ref inside the "
 813                              "submodule '%s'"), path);
 814
 815                print_status(flags, '+', path, &oid, displaypath);
 816        } else {
 817                print_status(flags, '+', path, ce_oid, displaypath);
 818        }
 819
 820        if (flags & OPT_RECURSIVE) {
 821                struct child_process cpr = CHILD_PROCESS_INIT;
 822
 823                cpr.git_cmd = 1;
 824                cpr.dir = path;
 825                prepare_submodule_repo_env(&cpr.env_array);
 826
 827                argv_array_push(&cpr.args, "--super-prefix");
 828                argv_array_pushf(&cpr.args, "%s/", displaypath);
 829                argv_array_pushl(&cpr.args, "submodule--helper", "status",
 830                                 "--recursive", NULL);
 831
 832                if (flags & OPT_CACHED)
 833                        argv_array_push(&cpr.args, "--cached");
 834
 835                if (flags & OPT_QUIET)
 836                        argv_array_push(&cpr.args, "--quiet");
 837
 838                if (run_command(&cpr))
 839                        die(_("failed to recurse into submodule '%s'"), path);
 840        }
 841
 842cleanup:
 843        argv_array_clear(&diff_files_args);
 844        free(displaypath);
 845}
 846
 847static void status_submodule_cb(const struct cache_entry *list_item,
 848                                void *cb_data)
 849{
 850        struct status_cb *info = cb_data;
 851        status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
 852                         info->prefix, info->flags);
 853}
 854
 855static int module_status(int argc, const char **argv, const char *prefix)
 856{
 857        struct status_cb info = STATUS_CB_INIT;
 858        struct pathspec pathspec;
 859        struct module_list list = MODULE_LIST_INIT;
 860        int quiet = 0;
 861
 862        struct option module_status_options[] = {
 863                OPT__QUIET(&quiet, N_("Suppress submodule status output")),
 864                OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
 865                OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
 866                OPT_END()
 867        };
 868
 869        const char *const git_submodule_helper_usage[] = {
 870                N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
 871                NULL
 872        };
 873
 874        argc = parse_options(argc, argv, prefix, module_status_options,
 875                             git_submodule_helper_usage, 0);
 876
 877        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 878                return 1;
 879
 880        info.prefix = prefix;
 881        if (quiet)
 882                info.flags |= OPT_QUIET;
 883
 884        for_each_listed_submodule(&list, status_submodule_cb, &info);
 885
 886        return 0;
 887}
 888
 889static int module_name(int argc, const char **argv, const char *prefix)
 890{
 891        const struct submodule *sub;
 892
 893        if (argc != 2)
 894                usage(_("git submodule--helper name <path>"));
 895
 896        sub = submodule_from_path(the_repository, &null_oid, argv[1]);
 897
 898        if (!sub)
 899                die(_("no submodule mapping found in .gitmodules for path '%s'"),
 900                    argv[1]);
 901
 902        printf("%s\n", sub->name);
 903
 904        return 0;
 905}
 906
 907struct sync_cb {
 908        const char *prefix;
 909        unsigned int flags;
 910};
 911
 912#define SYNC_CB_INIT { NULL, 0 }
 913
 914static void sync_submodule(const char *path, const char *prefix,
 915                           unsigned int flags)
 916{
 917        const struct submodule *sub;
 918        char *remote_key = NULL;
 919        char *sub_origin_url, *super_config_url, *displaypath;
 920        struct strbuf sb = STRBUF_INIT;
 921        struct child_process cp = CHILD_PROCESS_INIT;
 922        char *sub_config_path = NULL;
 923
 924        if (!is_submodule_active(the_repository, path))
 925                return;
 926
 927        sub = submodule_from_path(the_repository, &null_oid, path);
 928
 929        if (sub && sub->url) {
 930                if (starts_with_dot_dot_slash(sub->url) ||
 931                    starts_with_dot_slash(sub->url)) {
 932                        char *remote_url, *up_path;
 933                        char *remote = get_default_remote();
 934                        strbuf_addf(&sb, "remote.%s.url", remote);
 935
 936                        if (git_config_get_string(sb.buf, &remote_url))
 937                                remote_url = xgetcwd();
 938
 939                        up_path = get_up_path(path);
 940                        sub_origin_url = relative_url(remote_url, sub->url, up_path);
 941                        super_config_url = relative_url(remote_url, sub->url, NULL);
 942
 943                        free(remote);
 944                        free(up_path);
 945                        free(remote_url);
 946                } else {
 947                        sub_origin_url = xstrdup(sub->url);
 948                        super_config_url = xstrdup(sub->url);
 949                }
 950        } else {
 951                sub_origin_url = xstrdup("");
 952                super_config_url = xstrdup("");
 953        }
 954
 955        displaypath = get_submodule_displaypath(path, prefix);
 956
 957        if (!(flags & OPT_QUIET))
 958                printf(_("Synchronizing submodule url for '%s'\n"),
 959                         displaypath);
 960
 961        strbuf_reset(&sb);
 962        strbuf_addf(&sb, "submodule.%s.url", sub->name);
 963        if (git_config_set_gently(sb.buf, super_config_url))
 964                die(_("failed to register url for submodule path '%s'"),
 965                      displaypath);
 966
 967        if (!is_submodule_populated_gently(path, NULL))
 968                goto cleanup;
 969
 970        prepare_submodule_repo_env(&cp.env_array);
 971        cp.git_cmd = 1;
 972        cp.dir = path;
 973        argv_array_pushl(&cp.args, "submodule--helper",
 974                         "print-default-remote", NULL);
 975
 976        strbuf_reset(&sb);
 977        if (capture_command(&cp, &sb, 0))
 978                die(_("failed to get the default remote for submodule '%s'"),
 979                      path);
 980
 981        strbuf_strip_suffix(&sb, "\n");
 982        remote_key = xstrfmt("remote.%s.url", sb.buf);
 983
 984        strbuf_reset(&sb);
 985        submodule_to_gitdir(&sb, path);
 986        strbuf_addstr(&sb, "/config");
 987
 988        if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
 989                die(_("failed to update remote for submodule '%s'"),
 990                      path);
 991
 992        if (flags & OPT_RECURSIVE) {
 993                struct child_process cpr = CHILD_PROCESS_INIT;
 994
 995                cpr.git_cmd = 1;
 996                cpr.dir = path;
 997                prepare_submodule_repo_env(&cpr.env_array);
 998
 999                argv_array_push(&cpr.args, "--super-prefix");
1000                argv_array_pushf(&cpr.args, "%s/", displaypath);
1001                argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1002                                 "--recursive", NULL);
1003
1004                if (flags & OPT_QUIET)
1005                        argv_array_push(&cpr.args, "--quiet");
1006
1007                if (run_command(&cpr))
1008                        die(_("failed to recurse into submodule '%s'"),
1009                              path);
1010        }
1011
1012cleanup:
1013        free(super_config_url);
1014        free(sub_origin_url);
1015        strbuf_release(&sb);
1016        free(remote_key);
1017        free(displaypath);
1018        free(sub_config_path);
1019}
1020
1021static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1022{
1023        struct sync_cb *info = cb_data;
1024        sync_submodule(list_item->name, info->prefix, info->flags);
1025
1026}
1027
1028static int module_sync(int argc, const char **argv, const char *prefix)
1029{
1030        struct sync_cb info = SYNC_CB_INIT;
1031        struct pathspec pathspec;
1032        struct module_list list = MODULE_LIST_INIT;
1033        int quiet = 0;
1034        int recursive = 0;
1035
1036        struct option module_sync_options[] = {
1037                OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1038                OPT_BOOL(0, "recursive", &recursive,
1039                        N_("Recurse into nested submodules")),
1040                OPT_END()
1041        };
1042
1043        const char *const git_submodule_helper_usage[] = {
1044                N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1045                NULL
1046        };
1047
1048        argc = parse_options(argc, argv, prefix, module_sync_options,
1049                             git_submodule_helper_usage, 0);
1050
1051        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1052                return 1;
1053
1054        info.prefix = prefix;
1055        if (quiet)
1056                info.flags |= OPT_QUIET;
1057        if (recursive)
1058                info.flags |= OPT_RECURSIVE;
1059
1060        for_each_listed_submodule(&list, sync_submodule_cb, &info);
1061
1062        return 0;
1063}
1064
1065struct deinit_cb {
1066        const char *prefix;
1067        unsigned int flags;
1068};
1069#define DEINIT_CB_INIT { NULL, 0 }
1070
1071static void deinit_submodule(const char *path, const char *prefix,
1072                             unsigned int flags)
1073{
1074        const struct submodule *sub;
1075        char *displaypath = NULL;
1076        struct child_process cp_config = CHILD_PROCESS_INIT;
1077        struct strbuf sb_config = STRBUF_INIT;
1078        char *sub_git_dir = xstrfmt("%s/.git", path);
1079
1080        sub = submodule_from_path(the_repository, &null_oid, path);
1081
1082        if (!sub || !sub->name)
1083                goto cleanup;
1084
1085        displaypath = get_submodule_displaypath(path, prefix);
1086
1087        /* remove the submodule work tree (unless the user already did it) */
1088        if (is_directory(path)) {
1089                struct strbuf sb_rm = STRBUF_INIT;
1090                const char *format;
1091
1092                /*
1093                 * protect submodules containing a .git directory
1094                 * NEEDSWORK: instead of dying, automatically call
1095                 * absorbgitdirs and (possibly) warn.
1096                 */
1097                if (is_directory(sub_git_dir))
1098                        die(_("Submodule work tree '%s' contains a .git "
1099                              "directory (use 'rm -rf' if you really want "
1100                              "to remove it including all of its history)"),
1101                            displaypath);
1102
1103                if (!(flags & OPT_FORCE)) {
1104                        struct child_process cp_rm = CHILD_PROCESS_INIT;
1105                        cp_rm.git_cmd = 1;
1106                        argv_array_pushl(&cp_rm.args, "rm", "-qn",
1107                                         path, NULL);
1108
1109                        if (run_command(&cp_rm))
1110                                die(_("Submodule work tree '%s' contains local "
1111                                      "modifications; use '-f' to discard them"),
1112                                      displaypath);
1113                }
1114
1115                strbuf_addstr(&sb_rm, path);
1116
1117                if (!remove_dir_recursively(&sb_rm, 0))
1118                        format = _("Cleared directory '%s'\n");
1119                else
1120                        format = _("Could not remove submodule work tree '%s'\n");
1121
1122                if (!(flags & OPT_QUIET))
1123                        printf(format, displaypath);
1124
1125                strbuf_release(&sb_rm);
1126        }
1127
1128        if (mkdir(path, 0777))
1129                printf(_("could not create empty submodule directory %s"),
1130                      displaypath);
1131
1132        cp_config.git_cmd = 1;
1133        argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1134        argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1135
1136        /* remove the .git/config entries (unless the user already did it) */
1137        if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1138                char *sub_key = xstrfmt("submodule.%s", sub->name);
1139                /*
1140                 * remove the whole section so we have a clean state when
1141                 * the user later decides to init this submodule again
1142                 */
1143                git_config_rename_section_in_file(NULL, sub_key, NULL);
1144                if (!(flags & OPT_QUIET))
1145                        printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1146                                 sub->name, sub->url, displaypath);
1147                free(sub_key);
1148        }
1149
1150cleanup:
1151        free(displaypath);
1152        free(sub_git_dir);
1153        strbuf_release(&sb_config);
1154}
1155
1156static void deinit_submodule_cb(const struct cache_entry *list_item,
1157                                void *cb_data)
1158{
1159        struct deinit_cb *info = cb_data;
1160        deinit_submodule(list_item->name, info->prefix, info->flags);
1161}
1162
1163static int module_deinit(int argc, const char **argv, const char *prefix)
1164{
1165        struct deinit_cb info = DEINIT_CB_INIT;
1166        struct pathspec pathspec;
1167        struct module_list list = MODULE_LIST_INIT;
1168        int quiet = 0;
1169        int force = 0;
1170        int all = 0;
1171
1172        struct option module_deinit_options[] = {
1173                OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1174                OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1175                OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1176                OPT_END()
1177        };
1178
1179        const char *const git_submodule_helper_usage[] = {
1180                N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1181                NULL
1182        };
1183
1184        argc = parse_options(argc, argv, prefix, module_deinit_options,
1185                             git_submodule_helper_usage, 0);
1186
1187        if (all && argc) {
1188                error("pathspec and --all are incompatible");
1189                usage_with_options(git_submodule_helper_usage,
1190                                   module_deinit_options);
1191        }
1192
1193        if (!argc && !all)
1194                die(_("Use '--all' if you really want to deinitialize all submodules"));
1195
1196        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1197                return 1;
1198
1199        info.prefix = prefix;
1200        if (quiet)
1201                info.flags |= OPT_QUIET;
1202        if (force)
1203                info.flags |= OPT_FORCE;
1204
1205        for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1206
1207        return 0;
1208}
1209
1210static int clone_submodule(const char *path, const char *gitdir, const char *url,
1211                           const char *depth, struct string_list *reference,
1212                           int quiet, int progress)
1213{
1214        struct child_process cp = CHILD_PROCESS_INIT;
1215
1216        argv_array_push(&cp.args, "clone");
1217        argv_array_push(&cp.args, "--no-checkout");
1218        if (quiet)
1219                argv_array_push(&cp.args, "--quiet");
1220        if (progress)
1221                argv_array_push(&cp.args, "--progress");
1222        if (depth && *depth)
1223                argv_array_pushl(&cp.args, "--depth", depth, NULL);
1224        if (reference->nr) {
1225                struct string_list_item *item;
1226                for_each_string_list_item(item, reference)
1227                        argv_array_pushl(&cp.args, "--reference",
1228                                         item->string, NULL);
1229        }
1230        if (gitdir && *gitdir)
1231                argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1232
1233        argv_array_push(&cp.args, url);
1234        argv_array_push(&cp.args, path);
1235
1236        cp.git_cmd = 1;
1237        prepare_submodule_repo_env(&cp.env_array);
1238        cp.no_stdin = 1;
1239
1240        return run_command(&cp);
1241}
1242
1243struct submodule_alternate_setup {
1244        const char *submodule_name;
1245        enum SUBMODULE_ALTERNATE_ERROR_MODE {
1246                SUBMODULE_ALTERNATE_ERROR_DIE,
1247                SUBMODULE_ALTERNATE_ERROR_INFO,
1248                SUBMODULE_ALTERNATE_ERROR_IGNORE
1249        } error_mode;
1250        struct string_list *reference;
1251};
1252#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1253        SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1254
1255static int add_possible_reference_from_superproject(
1256                struct alternate_object_database *alt, void *sas_cb)
1257{
1258        struct submodule_alternate_setup *sas = sas_cb;
1259
1260        /*
1261         * If the alternate object store is another repository, try the
1262         * standard layout with .git/(modules/<name>)+/objects
1263         */
1264        if (ends_with(alt->path, "/objects")) {
1265                char *sm_alternate;
1266                struct strbuf sb = STRBUF_INIT;
1267                struct strbuf err = STRBUF_INIT;
1268                strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
1269
1270                /*
1271                 * We need to end the new path with '/' to mark it as a dir,
1272                 * otherwise a submodule name containing '/' will be broken
1273                 * as the last part of a missing submodule reference would
1274                 * be taken as a file name.
1275                 */
1276                strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
1277
1278                sm_alternate = compute_alternate_path(sb.buf, &err);
1279                if (sm_alternate) {
1280                        string_list_append(sas->reference, xstrdup(sb.buf));
1281                        free(sm_alternate);
1282                } else {
1283                        switch (sas->error_mode) {
1284                        case SUBMODULE_ALTERNATE_ERROR_DIE:
1285                                die(_("submodule '%s' cannot add alternate: %s"),
1286                                    sas->submodule_name, err.buf);
1287                        case SUBMODULE_ALTERNATE_ERROR_INFO:
1288                                fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
1289                                        sas->submodule_name, err.buf);
1290                        case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1291                                ; /* nothing */
1292                        }
1293                }
1294                strbuf_release(&sb);
1295        }
1296
1297        return 0;
1298}
1299
1300static void prepare_possible_alternates(const char *sm_name,
1301                struct string_list *reference)
1302{
1303        char *sm_alternate = NULL, *error_strategy = NULL;
1304        struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1305
1306        git_config_get_string("submodule.alternateLocation", &sm_alternate);
1307        if (!sm_alternate)
1308                return;
1309
1310        git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1311
1312        if (!error_strategy)
1313                error_strategy = xstrdup("die");
1314
1315        sas.submodule_name = sm_name;
1316        sas.reference = reference;
1317        if (!strcmp(error_strategy, "die"))
1318                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1319        else if (!strcmp(error_strategy, "info"))
1320                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1321        else if (!strcmp(error_strategy, "ignore"))
1322                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1323        else
1324                die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1325
1326        if (!strcmp(sm_alternate, "superproject"))
1327                foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1328        else if (!strcmp(sm_alternate, "no"))
1329                ; /* do nothing */
1330        else
1331                die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1332
1333        free(sm_alternate);
1334        free(error_strategy);
1335}
1336
1337static int module_clone(int argc, const char **argv, const char *prefix)
1338{
1339        const char *name = NULL, *url = NULL, *depth = NULL;
1340        int quiet = 0;
1341        int progress = 0;
1342        char *p, *path = NULL, *sm_gitdir;
1343        struct strbuf sb = STRBUF_INIT;
1344        struct string_list reference = STRING_LIST_INIT_NODUP;
1345        char *sm_alternate = NULL, *error_strategy = NULL;
1346
1347        struct option module_clone_options[] = {
1348                OPT_STRING(0, "prefix", &prefix,
1349                           N_("path"),
1350                           N_("alternative anchor for relative paths")),
1351                OPT_STRING(0, "path", &path,
1352                           N_("path"),
1353                           N_("where the new submodule will be cloned to")),
1354                OPT_STRING(0, "name", &name,
1355                           N_("string"),
1356                           N_("name of the new submodule")),
1357                OPT_STRING(0, "url", &url,
1358                           N_("string"),
1359                           N_("url where to clone the submodule from")),
1360                OPT_STRING_LIST(0, "reference", &reference,
1361                           N_("repo"),
1362                           N_("reference repository")),
1363                OPT_STRING(0, "depth", &depth,
1364                           N_("string"),
1365                           N_("depth for shallow clones")),
1366                OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1367                OPT_BOOL(0, "progress", &progress,
1368                           N_("force cloning progress")),
1369                OPT_END()
1370        };
1371
1372        const char *const git_submodule_helper_usage[] = {
1373                N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1374                   "[--reference <repository>] [--name <name>] [--depth <depth>] "
1375                   "--url <url> --path <path>"),
1376                NULL
1377        };
1378
1379        argc = parse_options(argc, argv, prefix, module_clone_options,
1380                             git_submodule_helper_usage, 0);
1381
1382        if (argc || !url || !path || !*path)
1383                usage_with_options(git_submodule_helper_usage,
1384                                   module_clone_options);
1385
1386        strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1387        sm_gitdir = absolute_pathdup(sb.buf);
1388        strbuf_reset(&sb);
1389
1390        if (!is_absolute_path(path)) {
1391                strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1392                path = strbuf_detach(&sb, NULL);
1393        } else
1394                path = xstrdup(path);
1395
1396        if (!file_exists(sm_gitdir)) {
1397                if (safe_create_leading_directories_const(sm_gitdir) < 0)
1398                        die(_("could not create directory '%s'"), sm_gitdir);
1399
1400                prepare_possible_alternates(name, &reference);
1401
1402                if (clone_submodule(path, sm_gitdir, url, depth, &reference,
1403                                    quiet, progress))
1404                        die(_("clone of '%s' into submodule path '%s' failed"),
1405                            url, path);
1406        } else {
1407                if (safe_create_leading_directories_const(path) < 0)
1408                        die(_("could not create directory '%s'"), path);
1409                strbuf_addf(&sb, "%s/index", sm_gitdir);
1410                unlink_or_warn(sb.buf);
1411                strbuf_reset(&sb);
1412        }
1413
1414        connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1415
1416        p = git_pathdup_submodule(path, "config");
1417        if (!p)
1418                die(_("could not get submodule directory for '%s'"), path);
1419
1420        /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1421        git_config_get_string("submodule.alternateLocation", &sm_alternate);
1422        if (sm_alternate)
1423                git_config_set_in_file(p, "submodule.alternateLocation",
1424                                           sm_alternate);
1425        git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1426        if (error_strategy)
1427                git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1428                                           error_strategy);
1429
1430        free(sm_alternate);
1431        free(error_strategy);
1432
1433        strbuf_release(&sb);
1434        free(sm_gitdir);
1435        free(path);
1436        free(p);
1437        return 0;
1438}
1439
1440struct submodule_update_clone {
1441        /* index into 'list', the list of submodules to look into for cloning */
1442        int current;
1443        struct module_list list;
1444        unsigned warn_if_uninitialized : 1;
1445
1446        /* update parameter passed via commandline */
1447        struct submodule_update_strategy update;
1448
1449        /* configuration parameters which are passed on to the children */
1450        int progress;
1451        int quiet;
1452        int recommend_shallow;
1453        struct string_list references;
1454        const char *depth;
1455        const char *recursive_prefix;
1456        const char *prefix;
1457
1458        /* Machine-readable status lines to be consumed by git-submodule.sh */
1459        struct string_list projectlines;
1460
1461        /* If we want to stop as fast as possible and return an error */
1462        unsigned quickstop : 1;
1463
1464        /* failed clones to be retried again */
1465        const struct cache_entry **failed_clones;
1466        int failed_clones_nr, failed_clones_alloc;
1467};
1468#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1469        SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
1470        NULL, NULL, NULL, \
1471        STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
1472
1473
1474static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1475                struct strbuf *out, const char *displaypath)
1476{
1477        /*
1478         * Only mention uninitialized submodules when their
1479         * paths have been specified.
1480         */
1481        if (suc->warn_if_uninitialized) {
1482                strbuf_addf(out,
1483                        _("Submodule path '%s' not initialized"),
1484                        displaypath);
1485                strbuf_addch(out, '\n');
1486                strbuf_addstr(out,
1487                        _("Maybe you want to use 'update --init'?"));
1488                strbuf_addch(out, '\n');
1489        }
1490}
1491
1492/**
1493 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1494 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1495 */
1496static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1497                                           struct child_process *child,
1498                                           struct submodule_update_clone *suc,
1499                                           struct strbuf *out)
1500{
1501        const struct submodule *sub = NULL;
1502        const char *url = NULL;
1503        const char *update_string;
1504        enum submodule_update_type update_type;
1505        char *key;
1506        struct strbuf displaypath_sb = STRBUF_INIT;
1507        struct strbuf sb = STRBUF_INIT;
1508        const char *displaypath = NULL;
1509        int needs_cloning = 0;
1510
1511        if (ce_stage(ce)) {
1512                if (suc->recursive_prefix)
1513                        strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1514                else
1515                        strbuf_addstr(&sb, ce->name);
1516                strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1517                strbuf_addch(out, '\n');
1518                goto cleanup;
1519        }
1520
1521        sub = submodule_from_path(the_repository, &null_oid, ce->name);
1522
1523        if (suc->recursive_prefix)
1524                displaypath = relative_path(suc->recursive_prefix,
1525                                            ce->name, &displaypath_sb);
1526        else
1527                displaypath = ce->name;
1528
1529        if (!sub) {
1530                next_submodule_warn_missing(suc, out, displaypath);
1531                goto cleanup;
1532        }
1533
1534        key = xstrfmt("submodule.%s.update", sub->name);
1535        if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1536                update_type = parse_submodule_update_type(update_string);
1537        } else {
1538                update_type = sub->update_strategy.type;
1539        }
1540        free(key);
1541
1542        if (suc->update.type == SM_UPDATE_NONE
1543            || (suc->update.type == SM_UPDATE_UNSPECIFIED
1544                && update_type == SM_UPDATE_NONE)) {
1545                strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1546                strbuf_addch(out, '\n');
1547                goto cleanup;
1548        }
1549
1550        /* Check if the submodule has been initialized. */
1551        if (!is_submodule_active(the_repository, ce->name)) {
1552                next_submodule_warn_missing(suc, out, displaypath);
1553                goto cleanup;
1554        }
1555
1556        strbuf_reset(&sb);
1557        strbuf_addf(&sb, "submodule.%s.url", sub->name);
1558        if (repo_config_get_string_const(the_repository, sb.buf, &url))
1559                url = sub->url;
1560
1561        strbuf_reset(&sb);
1562        strbuf_addf(&sb, "%s/.git", ce->name);
1563        needs_cloning = !file_exists(sb.buf);
1564
1565        strbuf_reset(&sb);
1566        strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
1567                        oid_to_hex(&ce->oid), ce_stage(ce),
1568                        needs_cloning, ce->name);
1569        string_list_append(&suc->projectlines, sb.buf);
1570
1571        if (!needs_cloning)
1572                goto cleanup;
1573
1574        child->git_cmd = 1;
1575        child->no_stdin = 1;
1576        child->stdout_to_stderr = 1;
1577        child->err = -1;
1578        argv_array_push(&child->args, "submodule--helper");
1579        argv_array_push(&child->args, "clone");
1580        if (suc->progress)
1581                argv_array_push(&child->args, "--progress");
1582        if (suc->quiet)
1583                argv_array_push(&child->args, "--quiet");
1584        if (suc->prefix)
1585                argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1586        if (suc->recommend_shallow && sub->recommend_shallow == 1)
1587                argv_array_push(&child->args, "--depth=1");
1588        argv_array_pushl(&child->args, "--path", sub->path, NULL);
1589        argv_array_pushl(&child->args, "--name", sub->name, NULL);
1590        argv_array_pushl(&child->args, "--url", url, NULL);
1591        if (suc->references.nr) {
1592                struct string_list_item *item;
1593                for_each_string_list_item(item, &suc->references)
1594                        argv_array_pushl(&child->args, "--reference", item->string, NULL);
1595        }
1596        if (suc->depth)
1597                argv_array_push(&child->args, suc->depth);
1598
1599cleanup:
1600        strbuf_reset(&displaypath_sb);
1601        strbuf_reset(&sb);
1602
1603        return needs_cloning;
1604}
1605
1606static int update_clone_get_next_task(struct child_process *child,
1607                                      struct strbuf *err,
1608                                      void *suc_cb,
1609                                      void **idx_task_cb)
1610{
1611        struct submodule_update_clone *suc = suc_cb;
1612        const struct cache_entry *ce;
1613        int index;
1614
1615        for (; suc->current < suc->list.nr; suc->current++) {
1616                ce = suc->list.entries[suc->current];
1617                if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1618                        int *p = xmalloc(sizeof(*p));
1619                        *p = suc->current;
1620                        *idx_task_cb = p;
1621                        suc->current++;
1622                        return 1;
1623                }
1624        }
1625
1626        /*
1627         * The loop above tried cloning each submodule once, now try the
1628         * stragglers again, which we can imagine as an extension of the
1629         * entry list.
1630         */
1631        index = suc->current - suc->list.nr;
1632        if (index < suc->failed_clones_nr) {
1633                int *p;
1634                ce = suc->failed_clones[index];
1635                if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1636                        suc->current ++;
1637                        strbuf_addstr(err, "BUG: submodule considered for "
1638                                           "cloning, doesn't need cloning "
1639                                           "any more?\n");
1640                        return 0;
1641                }
1642                p = xmalloc(sizeof(*p));
1643                *p = suc->current;
1644                *idx_task_cb = p;
1645                suc->current ++;
1646                return 1;
1647        }
1648
1649        return 0;
1650}
1651
1652static int update_clone_start_failure(struct strbuf *err,
1653                                      void *suc_cb,
1654                                      void *idx_task_cb)
1655{
1656        struct submodule_update_clone *suc = suc_cb;
1657        suc->quickstop = 1;
1658        return 1;
1659}
1660
1661static int update_clone_task_finished(int result,
1662                                      struct strbuf *err,
1663                                      void *suc_cb,
1664                                      void *idx_task_cb)
1665{
1666        const struct cache_entry *ce;
1667        struct submodule_update_clone *suc = suc_cb;
1668
1669        int *idxP = idx_task_cb;
1670        int idx = *idxP;
1671        free(idxP);
1672
1673        if (!result)
1674                return 0;
1675
1676        if (idx < suc->list.nr) {
1677                ce  = suc->list.entries[idx];
1678                strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1679                            ce->name);
1680                strbuf_addch(err, '\n');
1681                ALLOC_GROW(suc->failed_clones,
1682                           suc->failed_clones_nr + 1,
1683                           suc->failed_clones_alloc);
1684                suc->failed_clones[suc->failed_clones_nr++] = ce;
1685                return 0;
1686        } else {
1687                idx -= suc->list.nr;
1688                ce  = suc->failed_clones[idx];
1689                strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1690                            ce->name);
1691                strbuf_addch(err, '\n');
1692                suc->quickstop = 1;
1693                return 1;
1694        }
1695
1696        return 0;
1697}
1698
1699static int gitmodules_update_clone_config(const char *var, const char *value,
1700                                          void *cb)
1701{
1702        int *max_jobs = cb;
1703        if (!strcmp(var, "submodule.fetchjobs"))
1704                *max_jobs = parse_submodule_fetchjobs(var, value);
1705        return 0;
1706}
1707
1708static int update_clone(int argc, const char **argv, const char *prefix)
1709{
1710        const char *update = NULL;
1711        int max_jobs = 1;
1712        struct string_list_item *item;
1713        struct pathspec pathspec;
1714        struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1715
1716        struct option module_update_clone_options[] = {
1717                OPT_STRING(0, "prefix", &prefix,
1718                           N_("path"),
1719                           N_("path into the working tree")),
1720                OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1721                           N_("path"),
1722                           N_("path into the working tree, across nested "
1723                              "submodule boundaries")),
1724                OPT_STRING(0, "update", &update,
1725                           N_("string"),
1726                           N_("rebase, merge, checkout or none")),
1727                OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1728                           N_("reference repository")),
1729                OPT_STRING(0, "depth", &suc.depth, "<depth>",
1730                           N_("Create a shallow clone truncated to the "
1731                              "specified number of revisions")),
1732                OPT_INTEGER('j', "jobs", &max_jobs,
1733                            N_("parallel jobs")),
1734                OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1735                            N_("whether the initial clone should follow the shallow recommendation")),
1736                OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1737                OPT_BOOL(0, "progress", &suc.progress,
1738                            N_("force cloning progress")),
1739                OPT_END()
1740        };
1741
1742        const char *const git_submodule_helper_usage[] = {
1743                N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1744                NULL
1745        };
1746        suc.prefix = prefix;
1747
1748        config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1749        git_config(gitmodules_update_clone_config, &max_jobs);
1750
1751        argc = parse_options(argc, argv, prefix, module_update_clone_options,
1752                             git_submodule_helper_usage, 0);
1753
1754        if (update)
1755                if (parse_submodule_update_strategy(update, &suc.update) < 0)
1756                        die(_("bad value for update parameter"));
1757
1758        if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1759                return 1;
1760
1761        if (pathspec.nr)
1762                suc.warn_if_uninitialized = 1;
1763
1764        run_processes_parallel(max_jobs,
1765                               update_clone_get_next_task,
1766                               update_clone_start_failure,
1767                               update_clone_task_finished,
1768                               &suc);
1769
1770        /*
1771         * We saved the output and put it out all at once now.
1772         * That means:
1773         * - the listener does not have to interleave their (checkout)
1774         *   work with our fetching.  The writes involved in a
1775         *   checkout involve more straightforward sequential I/O.
1776         * - the listener can avoid doing any work if fetching failed.
1777         */
1778        if (suc.quickstop)
1779                return 1;
1780
1781        for_each_string_list_item(item, &suc.projectlines)
1782                fprintf(stdout, "%s", item->string);
1783
1784        return 0;
1785}
1786
1787static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1788{
1789        struct strbuf sb = STRBUF_INIT;
1790        if (argc != 3)
1791                die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1792
1793        printf("%s", relative_path(argv[1], argv[2], &sb));
1794        strbuf_release(&sb);
1795        return 0;
1796}
1797
1798static const char *remote_submodule_branch(const char *path)
1799{
1800        const struct submodule *sub;
1801        const char *branch = NULL;
1802        char *key;
1803
1804        sub = submodule_from_path(the_repository, &null_oid, path);
1805        if (!sub)
1806                return NULL;
1807
1808        key = xstrfmt("submodule.%s.branch", sub->name);
1809        if (repo_config_get_string_const(the_repository, key, &branch))
1810                branch = sub->branch;
1811        free(key);
1812
1813        if (!branch)
1814                return "master";
1815
1816        if (!strcmp(branch, ".")) {
1817                const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1818
1819                if (!refname)
1820                        die(_("No such ref: %s"), "HEAD");
1821
1822                /* detached HEAD */
1823                if (!strcmp(refname, "HEAD"))
1824                        die(_("Submodule (%s) branch configured to inherit "
1825                              "branch from superproject, but the superproject "
1826                              "is not on any branch"), sub->name);
1827
1828                if (!skip_prefix(refname, "refs/heads/", &refname))
1829                        die(_("Expecting a full ref name, got %s"), refname);
1830                return refname;
1831        }
1832
1833        return branch;
1834}
1835
1836static int resolve_remote_submodule_branch(int argc, const char **argv,
1837                const char *prefix)
1838{
1839        const char *ret;
1840        struct strbuf sb = STRBUF_INIT;
1841        if (argc != 2)
1842                die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1843
1844        ret = remote_submodule_branch(argv[1]);
1845        if (!ret)
1846                die("submodule %s doesn't exist", argv[1]);
1847
1848        printf("%s", ret);
1849        strbuf_release(&sb);
1850        return 0;
1851}
1852
1853static int push_check(int argc, const char **argv, const char *prefix)
1854{
1855        struct remote *remote;
1856        const char *superproject_head;
1857        char *head;
1858        int detached_head = 0;
1859        struct object_id head_oid;
1860
1861        if (argc < 3)
1862                die("submodule--helper push-check requires at least 2 arguments");
1863
1864        /*
1865         * superproject's resolved head ref.
1866         * if HEAD then the superproject is in a detached head state, otherwise
1867         * it will be the resolved head ref.
1868         */
1869        superproject_head = argv[1];
1870        argv++;
1871        argc--;
1872        /* Get the submodule's head ref and determine if it is detached */
1873        head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1874        if (!head)
1875                die(_("Failed to resolve HEAD as a valid ref."));
1876        if (!strcmp(head, "HEAD"))
1877                detached_head = 1;
1878
1879        /*
1880         * The remote must be configured.
1881         * This is to avoid pushing to the exact same URL as the parent.
1882         */
1883        remote = pushremote_get(argv[1]);
1884        if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1885                die("remote '%s' not configured", argv[1]);
1886
1887        /* Check the refspec */
1888        if (argc > 2) {
1889                int i, refspec_nr = argc - 2;
1890                struct ref *local_refs = get_local_heads();
1891                struct refspec *refspec = parse_push_refspec(refspec_nr,
1892                                                             argv + 2);
1893
1894                for (i = 0; i < refspec_nr; i++) {
1895                        struct refspec *rs = refspec + i;
1896
1897                        if (rs->pattern || rs->matching)
1898                                continue;
1899
1900                        /* LHS must match a single ref */
1901                        switch (count_refspec_match(rs->src, local_refs, NULL)) {
1902                        case 1:
1903                                break;
1904                        case 0:
1905                                /*
1906                                 * If LHS matches 'HEAD' then we need to ensure
1907                                 * that it matches the same named branch
1908                                 * checked out in the superproject.
1909                                 */
1910                                if (!strcmp(rs->src, "HEAD")) {
1911                                        if (!detached_head &&
1912                                            !strcmp(head, superproject_head))
1913                                                break;
1914                                        die("HEAD does not match the named branch in the superproject");
1915                                }
1916                                /* fallthrough */
1917                        default:
1918                                die("src refspec '%s' must name a ref",
1919                                    rs->src);
1920                        }
1921                }
1922                free_refspec(refspec_nr, refspec);
1923        }
1924        free(head);
1925
1926        return 0;
1927}
1928
1929static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1930{
1931        int i;
1932        struct pathspec pathspec;
1933        struct module_list list = MODULE_LIST_INIT;
1934        unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1935
1936        struct option embed_gitdir_options[] = {
1937                OPT_STRING(0, "prefix", &prefix,
1938                           N_("path"),
1939                           N_("path into the working tree")),
1940                OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1941                        ABSORB_GITDIR_RECURSE_SUBMODULES),
1942                OPT_END()
1943        };
1944
1945        const char *const git_submodule_helper_usage[] = {
1946                N_("git submodule--helper embed-git-dir [<path>...]"),
1947                NULL
1948        };
1949
1950        argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1951                             git_submodule_helper_usage, 0);
1952
1953        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1954                return 1;
1955
1956        for (i = 0; i < list.nr; i++)
1957                absorb_git_dir_into_superproject(prefix,
1958                                list.entries[i]->name, flags);
1959
1960        return 0;
1961}
1962
1963static int is_active(int argc, const char **argv, const char *prefix)
1964{
1965        if (argc != 2)
1966                die("submodule--helper is-active takes exactly 1 argument");
1967
1968        return !is_submodule_active(the_repository, argv[1]);
1969}
1970
1971#define SUPPORT_SUPER_PREFIX (1<<0)
1972
1973struct cmd_struct {
1974        const char *cmd;
1975        int (*fn)(int, const char **, const char *);
1976        unsigned option;
1977};
1978
1979static struct cmd_struct commands[] = {
1980        {"list", module_list, 0},
1981        {"name", module_name, 0},
1982        {"clone", module_clone, 0},
1983        {"update-clone", update_clone, 0},
1984        {"relative-path", resolve_relative_path, 0},
1985        {"resolve-relative-url", resolve_relative_url, 0},
1986        {"resolve-relative-url-test", resolve_relative_url_test, 0},
1987        {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
1988        {"init", module_init, SUPPORT_SUPER_PREFIX},
1989        {"status", module_status, SUPPORT_SUPER_PREFIX},
1990        {"print-default-remote", print_default_remote, 0},
1991        {"sync", module_sync, SUPPORT_SUPER_PREFIX},
1992        {"deinit", module_deinit, 0},
1993        {"remote-branch", resolve_remote_submodule_branch, 0},
1994        {"push-check", push_check, 0},
1995        {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1996        {"is-active", is_active, 0},
1997};
1998
1999int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2000{
2001        int i;
2002        if (argc < 2 || !strcmp(argv[1], "-h"))
2003                usage("git submodule--helper <command>");
2004
2005        for (i = 0; i < ARRAY_SIZE(commands); i++) {
2006                if (!strcmp(argv[1], commands[i].cmd)) {
2007                        if (get_super_prefix() &&
2008                            !(commands[i].option & SUPPORT_SUPER_PREFIX))
2009                                die(_("%s doesn't support --super-prefix"),
2010                                    commands[i].cmd);
2011                        return commands[i].fn(argc - 1, argv + 1, prefix);
2012                }
2013        }
2014
2015        die(_("'%s' is not a valid submodule--helper "
2016              "subcommand"), argv[1]);
2017}