builtin / submodule--helper.con commit submodule--helper: don't print null in 'submodule status' (0b5e2ea)
   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
  20#define OPT_QUIET (1 << 0)
  21#define OPT_CACHED (1 << 1)
  22#define OPT_RECURSIVE (1 << 2)
  23
  24typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
  25                                  void *cb_data);
  26
  27static char *get_default_remote(void)
  28{
  29        char *dest = NULL, *ret;
  30        unsigned char sha1[20];
  31        struct strbuf sb = STRBUF_INIT;
  32        const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
  33
  34        if (!refname)
  35                die(_("No such ref: %s"), "HEAD");
  36
  37        /* detached HEAD */
  38        if (!strcmp(refname, "HEAD"))
  39                return xstrdup("origin");
  40
  41        if (!skip_prefix(refname, "refs/heads/", &refname))
  42                die(_("Expecting a full ref name, got %s"), refname);
  43
  44        strbuf_addf(&sb, "branch.%s.remote", refname);
  45        if (git_config_get_string(sb.buf, &dest))
  46                ret = xstrdup("origin");
  47        else
  48                ret = dest;
  49
  50        strbuf_release(&sb);
  51        return ret;
  52}
  53
  54static int starts_with_dot_slash(const char *str)
  55{
  56        return str[0] == '.' && is_dir_sep(str[1]);
  57}
  58
  59static int starts_with_dot_dot_slash(const char *str)
  60{
  61        return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
  62}
  63
  64/*
  65 * Returns 1 if it was the last chop before ':'.
  66 */
  67static int chop_last_dir(char **remoteurl, int is_relative)
  68{
  69        char *rfind = find_last_dir_sep(*remoteurl);
  70        if (rfind) {
  71                *rfind = '\0';
  72                return 0;
  73        }
  74
  75        rfind = strrchr(*remoteurl, ':');
  76        if (rfind) {
  77                *rfind = '\0';
  78                return 1;
  79        }
  80
  81        if (is_relative || !strcmp(".", *remoteurl))
  82                die(_("cannot strip one component off url '%s'"),
  83                        *remoteurl);
  84
  85        free(*remoteurl);
  86        *remoteurl = xstrdup(".");
  87        return 0;
  88}
  89
  90/*
  91 * The `url` argument is the URL that navigates to the submodule origin
  92 * repo. When relative, this URL is relative to the superproject origin
  93 * URL repo. The `up_path` argument, if specified, is the relative
  94 * path that navigates from the submodule working tree to the superproject
  95 * working tree. Returns the origin URL of the submodule.
  96 *
  97 * Return either an absolute URL or filesystem path (if the superproject
  98 * origin URL is an absolute URL or filesystem path, respectively) or a
  99 * relative file system path (if the superproject origin URL is a relative
 100 * file system path).
 101 *
 102 * When the output is a relative file system path, the path is either
 103 * relative to the submodule working tree, if up_path is specified, or to
 104 * the superproject working tree otherwise.
 105 *
 106 * NEEDSWORK: This works incorrectly on the domain and protocol part.
 107 * remote_url      url              outcome          expectation
 108 * http://a.com/b  ../c             http://a.com/c   as is
 109 * http://a.com/b/ ../c             http://a.com/c   same as previous line, but
 110 *                                                   ignore trailing slash in url
 111 * http://a.com/b  ../../c          http://c         error out
 112 * http://a.com/b  ../../../c       http:/c          error out
 113 * http://a.com/b  ../../../../c    http:c           error out
 114 * http://a.com/b  ../../../../../c    .:c           error out
 115 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
 116 * when a local part has a colon in its path component, too.
 117 */
 118static char *relative_url(const char *remote_url,
 119                                const char *url,
 120                                const char *up_path)
 121{
 122        int is_relative = 0;
 123        int colonsep = 0;
 124        char *out;
 125        char *remoteurl = xstrdup(remote_url);
 126        struct strbuf sb = STRBUF_INIT;
 127        size_t len = strlen(remoteurl);
 128
 129        if (is_dir_sep(remoteurl[len-1]))
 130                remoteurl[len-1] = '\0';
 131
 132        if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
 133                is_relative = 0;
 134        else {
 135                is_relative = 1;
 136                /*
 137                 * Prepend a './' to ensure all relative
 138                 * remoteurls start with './' or '../'
 139                 */
 140                if (!starts_with_dot_slash(remoteurl) &&
 141                    !starts_with_dot_dot_slash(remoteurl)) {
 142                        strbuf_reset(&sb);
 143                        strbuf_addf(&sb, "./%s", remoteurl);
 144                        free(remoteurl);
 145                        remoteurl = strbuf_detach(&sb, NULL);
 146                }
 147        }
 148        /*
 149         * When the url starts with '../', remove that and the
 150         * last directory in remoteurl.
 151         */
 152        while (url) {
 153                if (starts_with_dot_dot_slash(url)) {
 154                        url += 3;
 155                        colonsep |= chop_last_dir(&remoteurl, is_relative);
 156                } else if (starts_with_dot_slash(url))
 157                        url += 2;
 158                else
 159                        break;
 160        }
 161        strbuf_reset(&sb);
 162        strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
 163        if (ends_with(url, "/"))
 164                strbuf_setlen(&sb, sb.len - 1);
 165        free(remoteurl);
 166
 167        if (starts_with_dot_slash(sb.buf))
 168                out = xstrdup(sb.buf + 2);
 169        else
 170                out = xstrdup(sb.buf);
 171        strbuf_reset(&sb);
 172
 173        if (!up_path || !is_relative)
 174                return out;
 175
 176        strbuf_addf(&sb, "%s%s", up_path, out);
 177        free(out);
 178        return strbuf_detach(&sb, NULL);
 179}
 180
 181static int resolve_relative_url(int argc, const char **argv, const char *prefix)
 182{
 183        char *remoteurl = NULL;
 184        char *remote = get_default_remote();
 185        const char *up_path = NULL;
 186        char *res;
 187        const char *url;
 188        struct strbuf sb = STRBUF_INIT;
 189
 190        if (argc != 2 && argc != 3)
 191                die("resolve-relative-url only accepts one or two arguments");
 192
 193        url = argv[1];
 194        strbuf_addf(&sb, "remote.%s.url", remote);
 195        free(remote);
 196
 197        if (git_config_get_string(sb.buf, &remoteurl))
 198                /* the repository is its own authoritative upstream */
 199                remoteurl = xgetcwd();
 200
 201        if (argc == 3)
 202                up_path = argv[2];
 203
 204        res = relative_url(remoteurl, url, up_path);
 205        puts(res);
 206        free(res);
 207        free(remoteurl);
 208        return 0;
 209}
 210
 211static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
 212{
 213        char *remoteurl, *res;
 214        const char *up_path, *url;
 215
 216        if (argc != 4)
 217                die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
 218
 219        up_path = argv[1];
 220        remoteurl = xstrdup(argv[2]);
 221        url = argv[3];
 222
 223        if (!strcmp(up_path, "(null)"))
 224                up_path = NULL;
 225
 226        res = relative_url(remoteurl, url, up_path);
 227        puts(res);
 228        free(res);
 229        free(remoteurl);
 230        return 0;
 231}
 232
 233/* the result should be freed by the caller. */
 234static char *get_submodule_displaypath(const char *path, const char *prefix)
 235{
 236        const char *super_prefix = get_super_prefix();
 237
 238        if (prefix && super_prefix) {
 239                BUG("cannot have prefix '%s' and superprefix '%s'",
 240                    prefix, super_prefix);
 241        } else if (prefix) {
 242                struct strbuf sb = STRBUF_INIT;
 243                char *displaypath = xstrdup(relative_path(path, prefix, &sb));
 244                strbuf_release(&sb);
 245                return displaypath;
 246        } else if (super_prefix) {
 247                return xstrfmt("%s%s", super_prefix, path);
 248        } else {
 249                return xstrdup(path);
 250        }
 251}
 252
 253static char *compute_rev_name(const char *sub_path, const char* object_id)
 254{
 255        struct strbuf sb = STRBUF_INIT;
 256        const char ***d;
 257
 258        static const char *describe_bare[] = { NULL };
 259
 260        static const char *describe_tags[] = { "--tags", NULL };
 261
 262        static const char *describe_contains[] = { "--contains", NULL };
 263
 264        static const char *describe_all_always[] = { "--all", "--always", NULL };
 265
 266        static const char **describe_argv[] = { describe_bare, describe_tags,
 267                                                describe_contains,
 268                                                describe_all_always, NULL };
 269
 270        for (d = describe_argv; *d; d++) {
 271                struct child_process cp = CHILD_PROCESS_INIT;
 272                prepare_submodule_repo_env(&cp.env_array);
 273                cp.dir = sub_path;
 274                cp.git_cmd = 1;
 275                cp.no_stderr = 1;
 276
 277                argv_array_push(&cp.args, "describe");
 278                argv_array_pushv(&cp.args, *d);
 279                argv_array_push(&cp.args, object_id);
 280
 281                if (!capture_command(&cp, &sb, 0)) {
 282                        strbuf_strip_suffix(&sb, "\n");
 283                        return strbuf_detach(&sb, NULL);
 284                }
 285        }
 286
 287        strbuf_release(&sb);
 288        return NULL;
 289}
 290
 291struct module_list {
 292        const struct cache_entry **entries;
 293        int alloc, nr;
 294};
 295#define MODULE_LIST_INIT { NULL, 0, 0 }
 296
 297static int module_list_compute(int argc, const char **argv,
 298                               const char *prefix,
 299                               struct pathspec *pathspec,
 300                               struct module_list *list)
 301{
 302        int i, result = 0;
 303        char *ps_matched = NULL;
 304        parse_pathspec(pathspec, 0,
 305                       PATHSPEC_PREFER_FULL,
 306                       prefix, argv);
 307
 308        if (pathspec->nr)
 309                ps_matched = xcalloc(pathspec->nr, 1);
 310
 311        if (read_cache() < 0)
 312                die(_("index file corrupt"));
 313
 314        for (i = 0; i < active_nr; i++) {
 315                const struct cache_entry *ce = active_cache[i];
 316
 317                if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
 318                                    0, ps_matched, 1) ||
 319                    !S_ISGITLINK(ce->ce_mode))
 320                        continue;
 321
 322                ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
 323                list->entries[list->nr++] = ce;
 324                while (i + 1 < active_nr &&
 325                       !strcmp(ce->name, active_cache[i + 1]->name))
 326                        /*
 327                         * Skip entries with the same name in different stages
 328                         * to make sure an entry is returned only once.
 329                         */
 330                        i++;
 331        }
 332
 333        if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
 334                result = -1;
 335
 336        free(ps_matched);
 337
 338        return result;
 339}
 340
 341static void module_list_active(struct module_list *list)
 342{
 343        int i;
 344        struct module_list active_modules = MODULE_LIST_INIT;
 345
 346        for (i = 0; i < list->nr; i++) {
 347                const struct cache_entry *ce = list->entries[i];
 348
 349                if (!is_submodule_active(the_repository, ce->name))
 350                        continue;
 351
 352                ALLOC_GROW(active_modules.entries,
 353                           active_modules.nr + 1,
 354                           active_modules.alloc);
 355                active_modules.entries[active_modules.nr++] = ce;
 356        }
 357
 358        free(list->entries);
 359        *list = active_modules;
 360}
 361
 362static int module_list(int argc, const char **argv, const char *prefix)
 363{
 364        int i;
 365        struct pathspec pathspec;
 366        struct module_list list = MODULE_LIST_INIT;
 367
 368        struct option module_list_options[] = {
 369                OPT_STRING(0, "prefix", &prefix,
 370                           N_("path"),
 371                           N_("alternative anchor for relative paths")),
 372                OPT_END()
 373        };
 374
 375        const char *const git_submodule_helper_usage[] = {
 376                N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
 377                NULL
 378        };
 379
 380        argc = parse_options(argc, argv, prefix, module_list_options,
 381                             git_submodule_helper_usage, 0);
 382
 383        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 384                return 1;
 385
 386        for (i = 0; i < list.nr; i++) {
 387                const struct cache_entry *ce = list.entries[i];
 388
 389                if (ce_stage(ce))
 390                        printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
 391                else
 392                        printf("%06o %s %d\t", ce->ce_mode,
 393                               oid_to_hex(&ce->oid), ce_stage(ce));
 394
 395                fprintf(stdout, "%s\n", ce->name);
 396        }
 397        return 0;
 398}
 399
 400static void for_each_listed_submodule(const struct module_list *list,
 401                                      each_submodule_fn fn, void *cb_data)
 402{
 403        int i;
 404        for (i = 0; i < list->nr; i++)
 405                fn(list->entries[i], cb_data);
 406}
 407
 408struct init_cb {
 409        const char *prefix;
 410        unsigned int flags;
 411};
 412
 413#define INIT_CB_INIT { NULL, 0 }
 414
 415static void init_submodule(const char *path, const char *prefix,
 416                           unsigned int flags)
 417{
 418        const struct submodule *sub;
 419        struct strbuf sb = STRBUF_INIT;
 420        char *upd = NULL, *url = NULL, *displaypath;
 421
 422        displaypath = get_submodule_displaypath(path, prefix);
 423
 424        sub = submodule_from_path(&null_oid, path);
 425
 426        if (!sub)
 427                die(_("No url found for submodule path '%s' in .gitmodules"),
 428                        displaypath);
 429
 430        /*
 431         * NEEDSWORK: In a multi-working-tree world, this needs to be
 432         * set in the per-worktree config.
 433         *
 434         * Set active flag for the submodule being initialized
 435         */
 436        if (!is_submodule_active(the_repository, path)) {
 437                strbuf_addf(&sb, "submodule.%s.active", sub->name);
 438                git_config_set_gently(sb.buf, "true");
 439                strbuf_reset(&sb);
 440        }
 441
 442        /*
 443         * Copy url setting when it is not set yet.
 444         * To look up the url in .git/config, we must not fall back to
 445         * .gitmodules, so look it up directly.
 446         */
 447        strbuf_addf(&sb, "submodule.%s.url", sub->name);
 448        if (git_config_get_string(sb.buf, &url)) {
 449                if (!sub->url)
 450                        die(_("No url found for submodule path '%s' in .gitmodules"),
 451                                displaypath);
 452
 453                url = xstrdup(sub->url);
 454
 455                /* Possibly a url relative to parent */
 456                if (starts_with_dot_dot_slash(url) ||
 457                    starts_with_dot_slash(url)) {
 458                        char *remoteurl, *relurl;
 459                        char *remote = get_default_remote();
 460                        struct strbuf remotesb = STRBUF_INIT;
 461                        strbuf_addf(&remotesb, "remote.%s.url", remote);
 462                        free(remote);
 463
 464                        if (git_config_get_string(remotesb.buf, &remoteurl)) {
 465                                warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
 466                                remoteurl = xgetcwd();
 467                        }
 468                        relurl = relative_url(remoteurl, url, NULL);
 469                        strbuf_release(&remotesb);
 470                        free(remoteurl);
 471                        free(url);
 472                        url = relurl;
 473                }
 474
 475                if (git_config_set_gently(sb.buf, url))
 476                        die(_("Failed to register url for submodule path '%s'"),
 477                            displaypath);
 478                if (!(flags & OPT_QUIET))
 479                        fprintf(stderr,
 480                                _("Submodule '%s' (%s) registered for path '%s'\n"),
 481                                sub->name, url, displaypath);
 482        }
 483        strbuf_reset(&sb);
 484
 485        /* Copy "update" setting when it is not set yet */
 486        strbuf_addf(&sb, "submodule.%s.update", sub->name);
 487        if (git_config_get_string(sb.buf, &upd) &&
 488            sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
 489                if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
 490                        fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
 491                                sub->name);
 492                        upd = xstrdup("none");
 493                } else
 494                        upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
 495
 496                if (git_config_set_gently(sb.buf, upd))
 497                        die(_("Failed to register update mode for submodule path '%s'"), displaypath);
 498        }
 499        strbuf_release(&sb);
 500        free(displaypath);
 501        free(url);
 502        free(upd);
 503}
 504
 505static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
 506{
 507        struct init_cb *info = cb_data;
 508        init_submodule(list_item->name, info->prefix, info->flags);
 509}
 510
 511static int module_init(int argc, const char **argv, const char *prefix)
 512{
 513        struct init_cb info = INIT_CB_INIT;
 514        struct pathspec pathspec;
 515        struct module_list list = MODULE_LIST_INIT;
 516        int quiet = 0;
 517
 518        struct option module_init_options[] = {
 519                OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
 520                OPT_END()
 521        };
 522
 523        const char *const git_submodule_helper_usage[] = {
 524                N_("git submodule--helper init [<path>]"),
 525                NULL
 526        };
 527
 528        argc = parse_options(argc, argv, prefix, module_init_options,
 529                             git_submodule_helper_usage, 0);
 530
 531        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 532                return 1;
 533
 534        /*
 535         * If there are no path args and submodule.active is set then,
 536         * by default, only initialize 'active' modules.
 537         */
 538        if (!argc && git_config_get_value_multi("submodule.active"))
 539                module_list_active(&list);
 540
 541        info.prefix = prefix;
 542        if (quiet)
 543                info.flags |= OPT_QUIET;
 544
 545        for_each_listed_submodule(&list, init_submodule_cb, &info);
 546
 547        return 0;
 548}
 549
 550struct status_cb {
 551        const char *prefix;
 552        unsigned int flags;
 553};
 554
 555#define STATUS_CB_INIT { NULL, 0 }
 556
 557static void print_status(unsigned int flags, char state, const char *path,
 558                         const struct object_id *oid, const char *displaypath)
 559{
 560        if (flags & OPT_QUIET)
 561                return;
 562
 563        printf("%c%s %s", state, oid_to_hex(oid), displaypath);
 564
 565        if (state == ' ' || state == '+') {
 566                const char *name = compute_rev_name(path, oid_to_hex(oid));
 567
 568                if (name)
 569                        printf(" (%s)", name);
 570        }
 571
 572        printf("\n");
 573}
 574
 575static int handle_submodule_head_ref(const char *refname,
 576                                     const struct object_id *oid, int flags,
 577                                     void *cb_data)
 578{
 579        struct object_id *output = cb_data;
 580        if (oid)
 581                oidcpy(output, oid);
 582
 583        return 0;
 584}
 585
 586static void status_submodule(const char *path, const struct object_id *ce_oid,
 587                             unsigned int ce_flags, const char *prefix,
 588                             unsigned int flags)
 589{
 590        char *displaypath;
 591        struct argv_array diff_files_args = ARGV_ARRAY_INIT;
 592        struct rev_info rev;
 593        int diff_files_result;
 594
 595        if (!submodule_from_path(&null_oid, path))
 596                die(_("no submodule mapping found in .gitmodules for path '%s'"),
 597                      path);
 598
 599        displaypath = get_submodule_displaypath(path, prefix);
 600
 601        if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
 602                print_status(flags, 'U', path, &null_oid, displaypath);
 603                goto cleanup;
 604        }
 605
 606        if (!is_submodule_active(the_repository, path)) {
 607                print_status(flags, '-', path, ce_oid, displaypath);
 608                goto cleanup;
 609        }
 610
 611        argv_array_pushl(&diff_files_args, "diff-files",
 612                         "--ignore-submodules=dirty", "--quiet", "--",
 613                         path, NULL);
 614
 615        git_config(git_diff_basic_config, NULL);
 616        init_revisions(&rev, prefix);
 617        rev.abbrev = 0;
 618        diff_files_args.argc = setup_revisions(diff_files_args.argc,
 619                                               diff_files_args.argv,
 620                                               &rev, NULL);
 621        diff_files_result = run_diff_files(&rev, 0);
 622
 623        if (!diff_result_code(&rev.diffopt, diff_files_result)) {
 624                print_status(flags, ' ', path, ce_oid,
 625                             displaypath);
 626        } else if (!(flags & OPT_CACHED)) {
 627                struct object_id oid;
 628
 629                if (refs_head_ref(get_submodule_ref_store(path),
 630                                  handle_submodule_head_ref, &oid))
 631                        die(_("could not resolve HEAD ref inside the"
 632                              "submodule '%s'"), path);
 633
 634                print_status(flags, '+', path, &oid, displaypath);
 635        } else {
 636                print_status(flags, '+', path, ce_oid, displaypath);
 637        }
 638
 639        if (flags & OPT_RECURSIVE) {
 640                struct child_process cpr = CHILD_PROCESS_INIT;
 641
 642                cpr.git_cmd = 1;
 643                cpr.dir = path;
 644                prepare_submodule_repo_env(&cpr.env_array);
 645
 646                argv_array_push(&cpr.args, "--super-prefix");
 647                argv_array_pushf(&cpr.args, "%s/", displaypath);
 648                argv_array_pushl(&cpr.args, "submodule--helper", "status",
 649                                 "--recursive", NULL);
 650
 651                if (flags & OPT_CACHED)
 652                        argv_array_push(&cpr.args, "--cached");
 653
 654                if (flags & OPT_QUIET)
 655                        argv_array_push(&cpr.args, "--quiet");
 656
 657                if (run_command(&cpr))
 658                        die(_("failed to recurse into submodule '%s'"), path);
 659        }
 660
 661cleanup:
 662        argv_array_clear(&diff_files_args);
 663        free(displaypath);
 664}
 665
 666static void status_submodule_cb(const struct cache_entry *list_item,
 667                                void *cb_data)
 668{
 669        struct status_cb *info = cb_data;
 670        status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
 671                         info->prefix, info->flags);
 672}
 673
 674static int module_status(int argc, const char **argv, const char *prefix)
 675{
 676        struct status_cb info = STATUS_CB_INIT;
 677        struct pathspec pathspec;
 678        struct module_list list = MODULE_LIST_INIT;
 679        int quiet = 0;
 680
 681        struct option module_status_options[] = {
 682                OPT__QUIET(&quiet, N_("Suppress submodule status output")),
 683                OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
 684                OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
 685                OPT_END()
 686        };
 687
 688        const char *const git_submodule_helper_usage[] = {
 689                N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
 690                NULL
 691        };
 692
 693        argc = parse_options(argc, argv, prefix, module_status_options,
 694                             git_submodule_helper_usage, 0);
 695
 696        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
 697                return 1;
 698
 699        info.prefix = prefix;
 700        if (quiet)
 701                info.flags |= OPT_QUIET;
 702
 703        for_each_listed_submodule(&list, status_submodule_cb, &info);
 704
 705        return 0;
 706}
 707
 708static int module_name(int argc, const char **argv, const char *prefix)
 709{
 710        const struct submodule *sub;
 711
 712        if (argc != 2)
 713                usage(_("git submodule--helper name <path>"));
 714
 715        sub = submodule_from_path(&null_oid, argv[1]);
 716
 717        if (!sub)
 718                die(_("no submodule mapping found in .gitmodules for path '%s'"),
 719                    argv[1]);
 720
 721        printf("%s\n", sub->name);
 722
 723        return 0;
 724}
 725
 726static int clone_submodule(const char *path, const char *gitdir, const char *url,
 727                           const char *depth, struct string_list *reference,
 728                           int quiet, int progress)
 729{
 730        struct child_process cp = CHILD_PROCESS_INIT;
 731
 732        argv_array_push(&cp.args, "clone");
 733        argv_array_push(&cp.args, "--no-checkout");
 734        if (quiet)
 735                argv_array_push(&cp.args, "--quiet");
 736        if (progress)
 737                argv_array_push(&cp.args, "--progress");
 738        if (depth && *depth)
 739                argv_array_pushl(&cp.args, "--depth", depth, NULL);
 740        if (reference->nr) {
 741                struct string_list_item *item;
 742                for_each_string_list_item(item, reference)
 743                        argv_array_pushl(&cp.args, "--reference",
 744                                         item->string, NULL);
 745        }
 746        if (gitdir && *gitdir)
 747                argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
 748
 749        argv_array_push(&cp.args, url);
 750        argv_array_push(&cp.args, path);
 751
 752        cp.git_cmd = 1;
 753        prepare_submodule_repo_env(&cp.env_array);
 754        cp.no_stdin = 1;
 755
 756        return run_command(&cp);
 757}
 758
 759struct submodule_alternate_setup {
 760        const char *submodule_name;
 761        enum SUBMODULE_ALTERNATE_ERROR_MODE {
 762                SUBMODULE_ALTERNATE_ERROR_DIE,
 763                SUBMODULE_ALTERNATE_ERROR_INFO,
 764                SUBMODULE_ALTERNATE_ERROR_IGNORE
 765        } error_mode;
 766        struct string_list *reference;
 767};
 768#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
 769        SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
 770
 771static int add_possible_reference_from_superproject(
 772                struct alternate_object_database *alt, void *sas_cb)
 773{
 774        struct submodule_alternate_setup *sas = sas_cb;
 775
 776        /*
 777         * If the alternate object store is another repository, try the
 778         * standard layout with .git/(modules/<name>)+/objects
 779         */
 780        if (ends_with(alt->path, "/objects")) {
 781                char *sm_alternate;
 782                struct strbuf sb = STRBUF_INIT;
 783                struct strbuf err = STRBUF_INIT;
 784                strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
 785
 786                /*
 787                 * We need to end the new path with '/' to mark it as a dir,
 788                 * otherwise a submodule name containing '/' will be broken
 789                 * as the last part of a missing submodule reference would
 790                 * be taken as a file name.
 791                 */
 792                strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
 793
 794                sm_alternate = compute_alternate_path(sb.buf, &err);
 795                if (sm_alternate) {
 796                        string_list_append(sas->reference, xstrdup(sb.buf));
 797                        free(sm_alternate);
 798                } else {
 799                        switch (sas->error_mode) {
 800                        case SUBMODULE_ALTERNATE_ERROR_DIE:
 801                                die(_("submodule '%s' cannot add alternate: %s"),
 802                                    sas->submodule_name, err.buf);
 803                        case SUBMODULE_ALTERNATE_ERROR_INFO:
 804                                fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
 805                                        sas->submodule_name, err.buf);
 806                        case SUBMODULE_ALTERNATE_ERROR_IGNORE:
 807                                ; /* nothing */
 808                        }
 809                }
 810                strbuf_release(&sb);
 811        }
 812
 813        return 0;
 814}
 815
 816static void prepare_possible_alternates(const char *sm_name,
 817                struct string_list *reference)
 818{
 819        char *sm_alternate = NULL, *error_strategy = NULL;
 820        struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
 821
 822        git_config_get_string("submodule.alternateLocation", &sm_alternate);
 823        if (!sm_alternate)
 824                return;
 825
 826        git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
 827
 828        if (!error_strategy)
 829                error_strategy = xstrdup("die");
 830
 831        sas.submodule_name = sm_name;
 832        sas.reference = reference;
 833        if (!strcmp(error_strategy, "die"))
 834                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
 835        else if (!strcmp(error_strategy, "info"))
 836                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
 837        else if (!strcmp(error_strategy, "ignore"))
 838                sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
 839        else
 840                die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
 841
 842        if (!strcmp(sm_alternate, "superproject"))
 843                foreach_alt_odb(add_possible_reference_from_superproject, &sas);
 844        else if (!strcmp(sm_alternate, "no"))
 845                ; /* do nothing */
 846        else
 847                die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
 848
 849        free(sm_alternate);
 850        free(error_strategy);
 851}
 852
 853static int module_clone(int argc, const char **argv, const char *prefix)
 854{
 855        const char *name = NULL, *url = NULL, *depth = NULL;
 856        int quiet = 0;
 857        int progress = 0;
 858        char *p, *path = NULL, *sm_gitdir;
 859        struct strbuf sb = STRBUF_INIT;
 860        struct string_list reference = STRING_LIST_INIT_NODUP;
 861        char *sm_alternate = NULL, *error_strategy = NULL;
 862
 863        struct option module_clone_options[] = {
 864                OPT_STRING(0, "prefix", &prefix,
 865                           N_("path"),
 866                           N_("alternative anchor for relative paths")),
 867                OPT_STRING(0, "path", &path,
 868                           N_("path"),
 869                           N_("where the new submodule will be cloned to")),
 870                OPT_STRING(0, "name", &name,
 871                           N_("string"),
 872                           N_("name of the new submodule")),
 873                OPT_STRING(0, "url", &url,
 874                           N_("string"),
 875                           N_("url where to clone the submodule from")),
 876                OPT_STRING_LIST(0, "reference", &reference,
 877                           N_("repo"),
 878                           N_("reference repository")),
 879                OPT_STRING(0, "depth", &depth,
 880                           N_("string"),
 881                           N_("depth for shallow clones")),
 882                OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
 883                OPT_BOOL(0, "progress", &progress,
 884                           N_("force cloning progress")),
 885                OPT_END()
 886        };
 887
 888        const char *const git_submodule_helper_usage[] = {
 889                N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
 890                   "[--reference <repository>] [--name <name>] [--depth <depth>] "
 891                   "--url <url> --path <path>"),
 892                NULL
 893        };
 894
 895        argc = parse_options(argc, argv, prefix, module_clone_options,
 896                             git_submodule_helper_usage, 0);
 897
 898        if (argc || !url || !path || !*path)
 899                usage_with_options(git_submodule_helper_usage,
 900                                   module_clone_options);
 901
 902        strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
 903        sm_gitdir = absolute_pathdup(sb.buf);
 904        strbuf_reset(&sb);
 905
 906        if (!is_absolute_path(path)) {
 907                strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
 908                path = strbuf_detach(&sb, NULL);
 909        } else
 910                path = xstrdup(path);
 911
 912        if (!file_exists(sm_gitdir)) {
 913                if (safe_create_leading_directories_const(sm_gitdir) < 0)
 914                        die(_("could not create directory '%s'"), sm_gitdir);
 915
 916                prepare_possible_alternates(name, &reference);
 917
 918                if (clone_submodule(path, sm_gitdir, url, depth, &reference,
 919                                    quiet, progress))
 920                        die(_("clone of '%s' into submodule path '%s' failed"),
 921                            url, path);
 922        } else {
 923                if (safe_create_leading_directories_const(path) < 0)
 924                        die(_("could not create directory '%s'"), path);
 925                strbuf_addf(&sb, "%s/index", sm_gitdir);
 926                unlink_or_warn(sb.buf);
 927                strbuf_reset(&sb);
 928        }
 929
 930        /* Connect module worktree and git dir */
 931        connect_work_tree_and_git_dir(path, sm_gitdir);
 932
 933        p = git_pathdup_submodule(path, "config");
 934        if (!p)
 935                die(_("could not get submodule directory for '%s'"), path);
 936
 937        /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
 938        git_config_get_string("submodule.alternateLocation", &sm_alternate);
 939        if (sm_alternate)
 940                git_config_set_in_file(p, "submodule.alternateLocation",
 941                                           sm_alternate);
 942        git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
 943        if (error_strategy)
 944                git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 945                                           error_strategy);
 946
 947        free(sm_alternate);
 948        free(error_strategy);
 949
 950        strbuf_release(&sb);
 951        free(sm_gitdir);
 952        free(path);
 953        free(p);
 954        return 0;
 955}
 956
 957struct submodule_update_clone {
 958        /* index into 'list', the list of submodules to look into for cloning */
 959        int current;
 960        struct module_list list;
 961        unsigned warn_if_uninitialized : 1;
 962
 963        /* update parameter passed via commandline */
 964        struct submodule_update_strategy update;
 965
 966        /* configuration parameters which are passed on to the children */
 967        int progress;
 968        int quiet;
 969        int recommend_shallow;
 970        struct string_list references;
 971        const char *depth;
 972        const char *recursive_prefix;
 973        const char *prefix;
 974
 975        /* Machine-readable status lines to be consumed by git-submodule.sh */
 976        struct string_list projectlines;
 977
 978        /* If we want to stop as fast as possible and return an error */
 979        unsigned quickstop : 1;
 980
 981        /* failed clones to be retried again */
 982        const struct cache_entry **failed_clones;
 983        int failed_clones_nr, failed_clones_alloc;
 984};
 985#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
 986        SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
 987        NULL, NULL, NULL, \
 988        STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
 989
 990
 991static void next_submodule_warn_missing(struct submodule_update_clone *suc,
 992                struct strbuf *out, const char *displaypath)
 993{
 994        /*
 995         * Only mention uninitialized submodules when their
 996         * paths have been specified.
 997         */
 998        if (suc->warn_if_uninitialized) {
 999                strbuf_addf(out,
1000                        _("Submodule path '%s' not initialized"),
1001                        displaypath);
1002                strbuf_addch(out, '\n');
1003                strbuf_addstr(out,
1004                        _("Maybe you want to use 'update --init'?"));
1005                strbuf_addch(out, '\n');
1006        }
1007}
1008
1009/**
1010 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1011 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1012 */
1013static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1014                                           struct child_process *child,
1015                                           struct submodule_update_clone *suc,
1016                                           struct strbuf *out)
1017{
1018        const struct submodule *sub = NULL;
1019        const char *url = NULL;
1020        const char *update_string;
1021        enum submodule_update_type update_type;
1022        char *key;
1023        struct strbuf displaypath_sb = STRBUF_INIT;
1024        struct strbuf sb = STRBUF_INIT;
1025        const char *displaypath = NULL;
1026        int needs_cloning = 0;
1027
1028        if (ce_stage(ce)) {
1029                if (suc->recursive_prefix)
1030                        strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1031                else
1032                        strbuf_addstr(&sb, ce->name);
1033                strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1034                strbuf_addch(out, '\n');
1035                goto cleanup;
1036        }
1037
1038        sub = submodule_from_path(&null_oid, ce->name);
1039
1040        if (suc->recursive_prefix)
1041                displaypath = relative_path(suc->recursive_prefix,
1042                                            ce->name, &displaypath_sb);
1043        else
1044                displaypath = ce->name;
1045
1046        if (!sub) {
1047                next_submodule_warn_missing(suc, out, displaypath);
1048                goto cleanup;
1049        }
1050
1051        key = xstrfmt("submodule.%s.update", sub->name);
1052        if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1053                update_type = parse_submodule_update_type(update_string);
1054        } else {
1055                update_type = sub->update_strategy.type;
1056        }
1057        free(key);
1058
1059        if (suc->update.type == SM_UPDATE_NONE
1060            || (suc->update.type == SM_UPDATE_UNSPECIFIED
1061                && update_type == SM_UPDATE_NONE)) {
1062                strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1063                strbuf_addch(out, '\n');
1064                goto cleanup;
1065        }
1066
1067        /* Check if the submodule has been initialized. */
1068        if (!is_submodule_active(the_repository, ce->name)) {
1069                next_submodule_warn_missing(suc, out, displaypath);
1070                goto cleanup;
1071        }
1072
1073        strbuf_reset(&sb);
1074        strbuf_addf(&sb, "submodule.%s.url", sub->name);
1075        if (repo_config_get_string_const(the_repository, sb.buf, &url))
1076                url = sub->url;
1077
1078        strbuf_reset(&sb);
1079        strbuf_addf(&sb, "%s/.git", ce->name);
1080        needs_cloning = !file_exists(sb.buf);
1081
1082        strbuf_reset(&sb);
1083        strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
1084                        oid_to_hex(&ce->oid), ce_stage(ce),
1085                        needs_cloning, ce->name);
1086        string_list_append(&suc->projectlines, sb.buf);
1087
1088        if (!needs_cloning)
1089                goto cleanup;
1090
1091        child->git_cmd = 1;
1092        child->no_stdin = 1;
1093        child->stdout_to_stderr = 1;
1094        child->err = -1;
1095        argv_array_push(&child->args, "submodule--helper");
1096        argv_array_push(&child->args, "clone");
1097        if (suc->progress)
1098                argv_array_push(&child->args, "--progress");
1099        if (suc->quiet)
1100                argv_array_push(&child->args, "--quiet");
1101        if (suc->prefix)
1102                argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1103        if (suc->recommend_shallow && sub->recommend_shallow == 1)
1104                argv_array_push(&child->args, "--depth=1");
1105        argv_array_pushl(&child->args, "--path", sub->path, NULL);
1106        argv_array_pushl(&child->args, "--name", sub->name, NULL);
1107        argv_array_pushl(&child->args, "--url", url, NULL);
1108        if (suc->references.nr) {
1109                struct string_list_item *item;
1110                for_each_string_list_item(item, &suc->references)
1111                        argv_array_pushl(&child->args, "--reference", item->string, NULL);
1112        }
1113        if (suc->depth)
1114                argv_array_push(&child->args, suc->depth);
1115
1116cleanup:
1117        strbuf_reset(&displaypath_sb);
1118        strbuf_reset(&sb);
1119
1120        return needs_cloning;
1121}
1122
1123static int update_clone_get_next_task(struct child_process *child,
1124                                      struct strbuf *err,
1125                                      void *suc_cb,
1126                                      void **idx_task_cb)
1127{
1128        struct submodule_update_clone *suc = suc_cb;
1129        const struct cache_entry *ce;
1130        int index;
1131
1132        for (; suc->current < suc->list.nr; suc->current++) {
1133                ce = suc->list.entries[suc->current];
1134                if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1135                        int *p = xmalloc(sizeof(*p));
1136                        *p = suc->current;
1137                        *idx_task_cb = p;
1138                        suc->current++;
1139                        return 1;
1140                }
1141        }
1142
1143        /*
1144         * The loop above tried cloning each submodule once, now try the
1145         * stragglers again, which we can imagine as an extension of the
1146         * entry list.
1147         */
1148        index = suc->current - suc->list.nr;
1149        if (index < suc->failed_clones_nr) {
1150                int *p;
1151                ce = suc->failed_clones[index];
1152                if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1153                        suc->current ++;
1154                        strbuf_addstr(err, "BUG: submodule considered for "
1155                                           "cloning, doesn't need cloning "
1156                                           "any more?\n");
1157                        return 0;
1158                }
1159                p = xmalloc(sizeof(*p));
1160                *p = suc->current;
1161                *idx_task_cb = p;
1162                suc->current ++;
1163                return 1;
1164        }
1165
1166        return 0;
1167}
1168
1169static int update_clone_start_failure(struct strbuf *err,
1170                                      void *suc_cb,
1171                                      void *idx_task_cb)
1172{
1173        struct submodule_update_clone *suc = suc_cb;
1174        suc->quickstop = 1;
1175        return 1;
1176}
1177
1178static int update_clone_task_finished(int result,
1179                                      struct strbuf *err,
1180                                      void *suc_cb,
1181                                      void *idx_task_cb)
1182{
1183        const struct cache_entry *ce;
1184        struct submodule_update_clone *suc = suc_cb;
1185
1186        int *idxP = idx_task_cb;
1187        int idx = *idxP;
1188        free(idxP);
1189
1190        if (!result)
1191                return 0;
1192
1193        if (idx < suc->list.nr) {
1194                ce  = suc->list.entries[idx];
1195                strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1196                            ce->name);
1197                strbuf_addch(err, '\n');
1198                ALLOC_GROW(suc->failed_clones,
1199                           suc->failed_clones_nr + 1,
1200                           suc->failed_clones_alloc);
1201                suc->failed_clones[suc->failed_clones_nr++] = ce;
1202                return 0;
1203        } else {
1204                idx -= suc->list.nr;
1205                ce  = suc->failed_clones[idx];
1206                strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1207                            ce->name);
1208                strbuf_addch(err, '\n');
1209                suc->quickstop = 1;
1210                return 1;
1211        }
1212
1213        return 0;
1214}
1215
1216static int gitmodules_update_clone_config(const char *var, const char *value,
1217                                          void *cb)
1218{
1219        int *max_jobs = cb;
1220        if (!strcmp(var, "submodule.fetchjobs"))
1221                *max_jobs = parse_submodule_fetchjobs(var, value);
1222        return 0;
1223}
1224
1225static int update_clone(int argc, const char **argv, const char *prefix)
1226{
1227        const char *update = NULL;
1228        int max_jobs = 1;
1229        struct string_list_item *item;
1230        struct pathspec pathspec;
1231        struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1232
1233        struct option module_update_clone_options[] = {
1234                OPT_STRING(0, "prefix", &prefix,
1235                           N_("path"),
1236                           N_("path into the working tree")),
1237                OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1238                           N_("path"),
1239                           N_("path into the working tree, across nested "
1240                              "submodule boundaries")),
1241                OPT_STRING(0, "update", &update,
1242                           N_("string"),
1243                           N_("rebase, merge, checkout or none")),
1244                OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1245                           N_("reference repository")),
1246                OPT_STRING(0, "depth", &suc.depth, "<depth>",
1247                           N_("Create a shallow clone truncated to the "
1248                              "specified number of revisions")),
1249                OPT_INTEGER('j', "jobs", &max_jobs,
1250                            N_("parallel jobs")),
1251                OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1252                            N_("whether the initial clone should follow the shallow recommendation")),
1253                OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1254                OPT_BOOL(0, "progress", &suc.progress,
1255                            N_("force cloning progress")),
1256                OPT_END()
1257        };
1258
1259        const char *const git_submodule_helper_usage[] = {
1260                N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1261                NULL
1262        };
1263        suc.prefix = prefix;
1264
1265        config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1266        git_config(gitmodules_update_clone_config, &max_jobs);
1267
1268        argc = parse_options(argc, argv, prefix, module_update_clone_options,
1269                             git_submodule_helper_usage, 0);
1270
1271        if (update)
1272                if (parse_submodule_update_strategy(update, &suc.update) < 0)
1273                        die(_("bad value for update parameter"));
1274
1275        if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1276                return 1;
1277
1278        if (pathspec.nr)
1279                suc.warn_if_uninitialized = 1;
1280
1281        run_processes_parallel(max_jobs,
1282                               update_clone_get_next_task,
1283                               update_clone_start_failure,
1284                               update_clone_task_finished,
1285                               &suc);
1286
1287        /*
1288         * We saved the output and put it out all at once now.
1289         * That means:
1290         * - the listener does not have to interleave their (checkout)
1291         *   work with our fetching.  The writes involved in a
1292         *   checkout involve more straightforward sequential I/O.
1293         * - the listener can avoid doing any work if fetching failed.
1294         */
1295        if (suc.quickstop)
1296                return 1;
1297
1298        for_each_string_list_item(item, &suc.projectlines)
1299                fprintf(stdout, "%s", item->string);
1300
1301        return 0;
1302}
1303
1304static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1305{
1306        struct strbuf sb = STRBUF_INIT;
1307        if (argc != 3)
1308                die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1309
1310        printf("%s", relative_path(argv[1], argv[2], &sb));
1311        strbuf_release(&sb);
1312        return 0;
1313}
1314
1315static const char *remote_submodule_branch(const char *path)
1316{
1317        const struct submodule *sub;
1318        const char *branch = NULL;
1319        char *key;
1320
1321        sub = submodule_from_path(&null_oid, path);
1322        if (!sub)
1323                return NULL;
1324
1325        key = xstrfmt("submodule.%s.branch", sub->name);
1326        if (repo_config_get_string_const(the_repository, key, &branch))
1327                branch = sub->branch;
1328        free(key);
1329
1330        if (!branch)
1331                return "master";
1332
1333        if (!strcmp(branch, ".")) {
1334                unsigned char sha1[20];
1335                const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
1336
1337                if (!refname)
1338                        die(_("No such ref: %s"), "HEAD");
1339
1340                /* detached HEAD */
1341                if (!strcmp(refname, "HEAD"))
1342                        die(_("Submodule (%s) branch configured to inherit "
1343                              "branch from superproject, but the superproject "
1344                              "is not on any branch"), sub->name);
1345
1346                if (!skip_prefix(refname, "refs/heads/", &refname))
1347                        die(_("Expecting a full ref name, got %s"), refname);
1348                return refname;
1349        }
1350
1351        return branch;
1352}
1353
1354static int resolve_remote_submodule_branch(int argc, const char **argv,
1355                const char *prefix)
1356{
1357        const char *ret;
1358        struct strbuf sb = STRBUF_INIT;
1359        if (argc != 2)
1360                die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1361
1362        ret = remote_submodule_branch(argv[1]);
1363        if (!ret)
1364                die("submodule %s doesn't exist", argv[1]);
1365
1366        printf("%s", ret);
1367        strbuf_release(&sb);
1368        return 0;
1369}
1370
1371static int push_check(int argc, const char **argv, const char *prefix)
1372{
1373        struct remote *remote;
1374        const char *superproject_head;
1375        char *head;
1376        int detached_head = 0;
1377        struct object_id head_oid;
1378
1379        if (argc < 3)
1380                die("submodule--helper push-check requires at least 2 arguments");
1381
1382        /*
1383         * superproject's resolved head ref.
1384         * if HEAD then the superproject is in a detached head state, otherwise
1385         * it will be the resolved head ref.
1386         */
1387        superproject_head = argv[1];
1388        argv++;
1389        argc--;
1390        /* Get the submodule's head ref and determine if it is detached */
1391        head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1392        if (!head)
1393                die(_("Failed to resolve HEAD as a valid ref."));
1394        if (!strcmp(head, "HEAD"))
1395                detached_head = 1;
1396
1397        /*
1398         * The remote must be configured.
1399         * This is to avoid pushing to the exact same URL as the parent.
1400         */
1401        remote = pushremote_get(argv[1]);
1402        if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1403                die("remote '%s' not configured", argv[1]);
1404
1405        /* Check the refspec */
1406        if (argc > 2) {
1407                int i, refspec_nr = argc - 2;
1408                struct ref *local_refs = get_local_heads();
1409                struct refspec *refspec = parse_push_refspec(refspec_nr,
1410                                                             argv + 2);
1411
1412                for (i = 0; i < refspec_nr; i++) {
1413                        struct refspec *rs = refspec + i;
1414
1415                        if (rs->pattern || rs->matching)
1416                                continue;
1417
1418                        /* LHS must match a single ref */
1419                        switch (count_refspec_match(rs->src, local_refs, NULL)) {
1420                        case 1:
1421                                break;
1422                        case 0:
1423                                /*
1424                                 * If LHS matches 'HEAD' then we need to ensure
1425                                 * that it matches the same named branch
1426                                 * checked out in the superproject.
1427                                 */
1428                                if (!strcmp(rs->src, "HEAD")) {
1429                                        if (!detached_head &&
1430                                            !strcmp(head, superproject_head))
1431                                                break;
1432                                        die("HEAD does not match the named branch in the superproject");
1433                                }
1434                        default:
1435                                die("src refspec '%s' must name a ref",
1436                                    rs->src);
1437                        }
1438                }
1439                free_refspec(refspec_nr, refspec);
1440        }
1441        free(head);
1442
1443        return 0;
1444}
1445
1446static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1447{
1448        int i;
1449        struct pathspec pathspec;
1450        struct module_list list = MODULE_LIST_INIT;
1451        unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1452
1453        struct option embed_gitdir_options[] = {
1454                OPT_STRING(0, "prefix", &prefix,
1455                           N_("path"),
1456                           N_("path into the working tree")),
1457                OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1458                        ABSORB_GITDIR_RECURSE_SUBMODULES),
1459                OPT_END()
1460        };
1461
1462        const char *const git_submodule_helper_usage[] = {
1463                N_("git submodule--helper embed-git-dir [<path>...]"),
1464                NULL
1465        };
1466
1467        argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1468                             git_submodule_helper_usage, 0);
1469
1470        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1471                return 1;
1472
1473        for (i = 0; i < list.nr; i++)
1474                absorb_git_dir_into_superproject(prefix,
1475                                list.entries[i]->name, flags);
1476
1477        return 0;
1478}
1479
1480static int is_active(int argc, const char **argv, const char *prefix)
1481{
1482        if (argc != 2)
1483                die("submodule--helper is-active takes exactly 1 argument");
1484
1485        return !is_submodule_active(the_repository, argv[1]);
1486}
1487
1488#define SUPPORT_SUPER_PREFIX (1<<0)
1489
1490struct cmd_struct {
1491        const char *cmd;
1492        int (*fn)(int, const char **, const char *);
1493        unsigned option;
1494};
1495
1496static struct cmd_struct commands[] = {
1497        {"list", module_list, 0},
1498        {"name", module_name, 0},
1499        {"clone", module_clone, 0},
1500        {"update-clone", update_clone, 0},
1501        {"relative-path", resolve_relative_path, 0},
1502        {"resolve-relative-url", resolve_relative_url, 0},
1503        {"resolve-relative-url-test", resolve_relative_url_test, 0},
1504        {"init", module_init, SUPPORT_SUPER_PREFIX},
1505        {"status", module_status, SUPPORT_SUPER_PREFIX},
1506        {"remote-branch", resolve_remote_submodule_branch, 0},
1507        {"push-check", push_check, 0},
1508        {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1509        {"is-active", is_active, 0},
1510};
1511
1512int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1513{
1514        int i;
1515        if (argc < 2 || !strcmp(argv[1], "-h"))
1516                usage("git submodule--helper <command>");
1517
1518        for (i = 0; i < ARRAY_SIZE(commands); i++) {
1519                if (!strcmp(argv[1], commands[i].cmd)) {
1520                        if (get_super_prefix() &&
1521                            !(commands[i].option & SUPPORT_SUPER_PREFIX))
1522                                die(_("%s doesn't support --super-prefix"),
1523                                    commands[i].cmd);
1524                        return commands[i].fn(argc - 1, argv + 1, prefix);
1525                }
1526        }
1527
1528        die(_("'%s' is not a valid submodule--helper "
1529              "subcommand"), argv[1]);
1530}