clone: recursive and reference option triggers submodule alternates

When `--recursive` and `--reference` is given, it is reasonable to expect that the submodules are created with references to the submodules of the given alternate for the superproject. An initial attempt to do this was presented to the mailing list, which used flags that are passed around ("--super-reference") that instructed the submodule clone to look for a reference in the submodules of the referenced superproject. This is not well thought out, as any further `submodule update` should also respect the initial setup. When a new submodule is added to the superproject and the alternate of the superproject does not know about that submodule yet, we rather error out informing the user instead of being unclear if we did or did not use a submodules alternate. To solve this problem introduce new options that store the configuration for what the user wanted originally. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Aug 17, 2016 at 15:45 UTC 31224cbdc723d78a310b4cdbbd5abcfcbd44a4e5
4 files changed +176
Documentation/config.txt
+12
@@ -2837,6 +2837,18 @@ submodule.fetchJobs::
2837 in parallel. A value of 0 will give some reasonable default.
2838 If unset, it defaults to 1.
2839
2840 +submodule.alternateLocation::
2841 + Specifies how the submodules obtain alternates when submodules are
2842 + cloned. Possible values are `no`, `superproject`.
2843 + By default `no` is assumed, which doesn't add references. When the
2844 + value is set to `superproject` the submodule to be cloned computes
2845 + its alternates location relative to the superprojects alternate.
2846 +
2847 +submodule.alternateErrorStrategy
2848 + Specifies how to treat errors with the alternates for a submodule
2849 + as computed via `submodule.alternateLocation`. Possible values are
2850 + `ignore`, `info`, `die`. Default is `die`.
2851 +
2852 tag.forceSignAnnotated::
2853 A boolean to specify whether annotated tags created should be GPG signed.
2854 If `--annotate` is specified on the command line, it takes
builtin/clone.c
+19
@@ -947,6 +947,25 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
947 else
948 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
949 }
950 +
951 + if (option_recursive) {
952 + if (option_required_reference.nr &&
953 + option_optional_reference.nr)
954 + die(_("clone --recursive is not compatible with "
955 + "both --reference and --reference-if-able"));
956 + else if (option_required_reference.nr) {
957 + string_list_append(&option_config,
958 + "submodule.alternateLocation=superproject");
959 + string_list_append(&option_config,
960 + "submodule.alternateErrorStrategy=die");
961 + } else if (option_optional_reference.nr) {
962 + string_list_append(&option_config,
963 + "submodule.alternateLocation=superproject");
964 + string_list_append(&option_config,
965 + "submodule.alternateErrorStrategy=info");
966 + }
967 + }
968 +
969 init_db(option_template, INIT_DB_QUIET);
970 write_config(&option_config);
971
builtin/submodule--helper.c
+102
@@ -472,6 +472,105 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
472 return run_command(&cp);
473 }
474
475 +struct submodule_alternate_setup {
476 + const char *submodule_name;
477 + enum SUBMODULE_ALTERNATE_ERROR_MODE {
478 + SUBMODULE_ALTERNATE_ERROR_DIE,
479 + SUBMODULE_ALTERNATE_ERROR_INFO,
480 + SUBMODULE_ALTERNATE_ERROR_IGNORE
481 + } error_mode;
482 + struct string_list *reference;
483 +};
484 +#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
485 + SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
486 +
487 +static int add_possible_reference_from_superproject(
488 + struct alternate_object_database *alt, void *sas_cb)
489 +{
490 + struct submodule_alternate_setup *sas = sas_cb;
491 +
492 + /* directory name, minus trailing slash */
493 + size_t namelen = alt->name - alt->base - 1;
494 + struct strbuf name = STRBUF_INIT;
495 + strbuf_add(&name, alt->base, namelen);
496 +
497 + /*
498 + * If the alternate object store is another repository, try the
499 + * standard layout with .git/modules/<name>/objects
500 + */
501 + if (ends_with(name.buf, ".git/objects")) {
502 + char *sm_alternate;
503 + struct strbuf sb = STRBUF_INIT;
504 + struct strbuf err = STRBUF_INIT;
505 + strbuf_add(&sb, name.buf, name.len - strlen("objects"));
506 + /*
507 + * We need to end the new path with '/' to mark it as a dir,
508 + * otherwise a submodule name containing '/' will be broken
509 + * as the last part of a missing submodule reference would
510 + * be taken as a file name.
511 + */
512 + strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
513 +
514 + sm_alternate = compute_alternate_path(sb.buf, &err);
515 + if (sm_alternate) {
516 + string_list_append(sas->reference, xstrdup(sb.buf));
517 + free(sm_alternate);
518 + } else {
519 + switch (sas->error_mode) {
520 + case SUBMODULE_ALTERNATE_ERROR_DIE:
521 + die(_("submodule '%s' cannot add alternate: %s"),
522 + sas->submodule_name, err.buf);
523 + case SUBMODULE_ALTERNATE_ERROR_INFO:
524 + fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
525 + sas->submodule_name, err.buf);
526 + case SUBMODULE_ALTERNATE_ERROR_IGNORE:
527 + ; /* nothing */
528 + }
529 + }
530 + strbuf_release(&sb);
531 + }
532 +
533 + strbuf_release(&name);
534 + return 0;
535 +}
536 +
537 +static void prepare_possible_alternates(const char *sm_name,
538 + struct string_list *reference)
539 +{
540 + char *sm_alternate = NULL, *error_strategy = NULL;
541 + struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
542 +
543 + git_config_get_string("submodule.alternateLocation", &sm_alternate);
544 + if (!sm_alternate)
545 + return;
546 +
547 + git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
548 +
549 + if (!error_strategy)
550 + error_strategy = xstrdup("die");
551 +
552 + sas.submodule_name = sm_name;
553 + sas.reference = reference;
554 + if (!strcmp(error_strategy, "die"))
555 + sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
556 + else if (!strcmp(error_strategy, "info"))
557 + sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
558 + else if (!strcmp(error_strategy, "ignore"))
559 + sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
560 + else
561 + die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
562 +
563 + if (!strcmp(sm_alternate, "superproject"))
564 + foreach_alt_odb(add_possible_reference_from_superproject, &sas);
565 + else if (!strcmp(sm_alternate, "no"))
566 + ; /* do nothing */
567 + else
568 + die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
569 +
570 + free(sm_alternate);
571 + free(error_strategy);
572 +}
573 +
574 static int module_clone(int argc, const char **argv, const char *prefix)
575 {
576 const char *name = NULL, *url = NULL, *depth = NULL;
@@ -532,6 +631,9 @@ static int module_clone(int argc, const char **argv, const char *prefix)
631 if (!file_exists(sm_gitdir)) {
632 if (safe_create_leading_directories_const(sm_gitdir) < 0)
633 die(_("could not create directory '%s'"), sm_gitdir);
634 +
635 + prepare_possible_alternates(name, &reference);
636 +
637 if (clone_submodule(path, sm_gitdir, url, depth, &reference, quiet))
638 die(_("clone of '%s' into submodule path '%s' failed"),
639 url, path);
t/t7408-submodule-reference.sh
+43
@@ -82,4 +82,47 @@ test_expect_success 'updating superproject keeps alternates' '
82 test_alternate_is_used super-clone/.git/modules/sub/objects/info/alternates super-clone/sub
83 '
84
85 +test_expect_success 'submodules use alternates when cloning a superproject' '
86 + test_when_finished "rm -rf super-clone" &&
87 + git clone --reference super --recursive super super-clone &&
88 + (
89 + cd super-clone &&
90 + # test superproject has alternates setup correctly
91 + test_alternate_is_used .git/objects/info/alternates . &&
92 + # test submodule has correct setup
93 + test_alternate_is_used .git/modules/sub/objects/info/alternates sub
94 + )
95 +'
96 +
97 +test_expect_success 'missing submodule alternate fails clone and submodule update' '
98 + test_when_finished "rm -rf super-clone" &&
99 + git clone super super2 &&
100 + test_must_fail git clone --recursive --reference super2 super2 super-clone &&
101 + (
102 + cd super-clone &&
103 + # test superproject has alternates setup correctly
104 + test_alternate_is_used .git/objects/info/alternates . &&
105 + # update of the submodule succeeds
106 + test_must_fail git submodule update --init &&
107 + # and we have no alternates:
108 + test_must_fail test_alternate_is_used .git/modules/sub/objects/info/alternates sub &&
109 + test_must_fail test_path_is_file sub/file1
110 + )
111 +'
112 +
113 +test_expect_success 'ignoring missing submodule alternates passes clone and submodule update' '
114 + test_when_finished "rm -rf super-clone" &&
115 + git clone --reference-if-able super2 --recursive super2 super-clone &&
116 + (
117 + cd super-clone &&
118 + # test superproject has alternates setup correctly
119 + test_alternate_is_used .git/objects/info/alternates . &&
120 + # update of the submodule succeeds
121 + git submodule update --init &&
122 + # and we have no alternates:
123 + test_must_fail test_alternate_is_used .git/modules/sub/objects/info/alternates sub &&
124 + test_path_is_file sub/file1
125 + )
126 +'
127 +
128 test_done