]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/submodule--helper.c
Merge branch 'jk/loose-object-cache'
[thirdparty/git.git] / builtin / submodule--helper.c
index 5e6f2db4cdbcf9e88ddfc1890359f7f2f86b2cd2..b45514be317eafb765a8943241b5d880f65e3117 100644 (file)
@@ -584,6 +584,26 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static char *compute_submodule_clone_url(const char *rel_url)
+{
+       char *remoteurl, *relurl;
+       char *remote = get_default_remote();
+       struct strbuf remotesb = STRBUF_INIT;
+
+       strbuf_addf(&remotesb, "remote.%s.url", remote);
+       if (git_config_get_string(remotesb.buf, &remoteurl)) {
+               warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
+               remoteurl = xgetcwd();
+       }
+       relurl = relative_url(remoteurl, rel_url, NULL);
+
+       free(remote);
+       free(remoteurl);
+       strbuf_release(&remotesb);
+
+       return relurl;
+}
+
 struct init_cb {
        const char *prefix;
        unsigned int flags;
@@ -634,21 +654,9 @@ static void init_submodule(const char *path, const char *prefix,
                /* Possibly a url relative to parent */
                if (starts_with_dot_dot_slash(url) ||
                    starts_with_dot_slash(url)) {
-                       char *remoteurl, *relurl;
-                       char *remote = get_default_remote();
-                       struct strbuf remotesb = STRBUF_INIT;
-                       strbuf_addf(&remotesb, "remote.%s.url", remote);
-                       free(remote);
-
-                       if (git_config_get_string(remotesb.buf, &remoteurl)) {
-                               warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
-                               remoteurl = xgetcwd();
-                       }
-                       relurl = relative_url(remoteurl, url, NULL);
-                       strbuf_release(&remotesb);
-                       free(remoteurl);
-                       free(url);
-                       url = relurl;
+                       char *oldurl = url;
+                       url = compute_submodule_clone_url(oldurl);
+                       free(oldurl);
                }
 
                if (git_config_set_gently(sb.buf, url))
@@ -792,7 +800,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
                         path, NULL);
 
        git_config(git_diff_basic_config, NULL);
-       init_revisions(&rev, prefix);
+       repo_init_revisions(the_repository, &rev, prefix);
        rev.abbrev = 0;
        diff_files_args.argc = setup_revisions(diff_files_args.argc,
                                               diff_files_args.argv,
@@ -1257,19 +1265,20 @@ struct submodule_alternate_setup {
        SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
 
 static int add_possible_reference_from_superproject(
-               struct alternate_object_database *alt, void *sas_cb)
+               struct object_directory *odb, void *sas_cb)
 {
        struct submodule_alternate_setup *sas = sas_cb;
+       size_t len;
 
        /*
         * If the alternate object store is another repository, try the
         * standard layout with .git/(modules/<name>)+/objects
         */
-       if (ends_with(alt->path, "/objects")) {
+       if (strip_suffix(odb->path, "/objects", &len)) {
                char *sm_alternate;
                struct strbuf sb = STRBUF_INIT;
                struct strbuf err = STRBUF_INIT;
-               strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
+               strbuf_add(&sb, odb->path, len);
 
                /*
                 * We need to end the new path with '/' to mark it as a dir,
@@ -1277,7 +1286,7 @@ static int add_possible_reference_from_superproject(
                 * as the last part of a missing submodule reference would
                 * be taken as a file name.
                 */
-               strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
+               strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
 
                sm_alternate = compute_alternate_path(sb.buf, &err);
                if (sm_alternate) {
@@ -1444,6 +1453,70 @@ static int module_clone(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static void determine_submodule_update_strategy(struct repository *r,
+                                               int just_cloned,
+                                               const char *path,
+                                               const char *update,
+                                               struct submodule_update_strategy *out)
+{
+       const struct submodule *sub = submodule_from_path(r, &null_oid, path);
+       char *key;
+       const char *val;
+
+       key = xstrfmt("submodule.%s.update", sub->name);
+
+       if (update) {
+               if (parse_submodule_update_strategy(update, out) < 0)
+                       die(_("Invalid update mode '%s' for submodule path '%s'"),
+                               update, path);
+       } else if (!repo_config_get_string_const(r, key, &val)) {
+               if (parse_submodule_update_strategy(val, out) < 0)
+                       die(_("Invalid update mode '%s' configured for submodule path '%s'"),
+                               val, path);
+       } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
+               out->type = sub->update_strategy.type;
+               out->command = sub->update_strategy.command;
+       } else
+               out->type = SM_UPDATE_CHECKOUT;
+
+       if (just_cloned &&
+           (out->type == SM_UPDATE_MERGE ||
+            out->type == SM_UPDATE_REBASE ||
+            out->type == SM_UPDATE_NONE))
+               out->type = SM_UPDATE_CHECKOUT;
+
+       free(key);
+}
+
+static int module_update_module_mode(int argc, const char **argv, const char *prefix)
+{
+       const char *path, *update = NULL;
+       int just_cloned;
+       struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
+
+       if (argc < 3 || argc > 4)
+               die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
+
+       just_cloned = git_config_int("just_cloned", argv[1]);
+       path = argv[2];
+
+       if (argc == 4)
+               update = argv[3];
+
+       determine_submodule_update_strategy(the_repository,
+                                           just_cloned, path, update,
+                                           &update_strategy);
+       fputs(submodule_strategy_to_string(&update_strategy), stdout);
+
+       return 0;
+}
+
+struct update_clone_data {
+       const struct submodule *sub;
+       struct object_id oid;
+       unsigned just_cloned;
+};
+
 struct submodule_update_clone {
        /* index into 'list', the list of submodules to look into for cloning */
        int current;
@@ -1463,8 +1536,9 @@ struct submodule_update_clone {
        const char *recursive_prefix;
        const char *prefix;
 
-       /* Machine-readable status lines to be consumed by git-submodule.sh */
-       struct string_list projectlines;
+       /* to be consumed by git-submodule.sh */
+       struct update_clone_data *update_clone;
+       int update_clone_nr; int update_clone_alloc;
 
        /* If we want to stop as fast as possible and return an error */
        unsigned quickstop : 1;
@@ -1472,11 +1546,13 @@ struct submodule_update_clone {
        /* failed clones to be retried again */
        const struct cache_entry **failed_clones;
        int failed_clones_nr, failed_clones_alloc;
+
+       int max_jobs;
 };
 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
        SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
        NULL, NULL, NULL, \
-       STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
+       NULL, 0, 0, 0, NULL, 0, 0, 0}
 
 
 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
@@ -1515,6 +1591,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
        struct strbuf sb = STRBUF_INIT;
        const char *displaypath = NULL;
        int needs_cloning = 0;
+       int need_free_url = 0;
 
        if (ce_stage(ce)) {
                if (suc->recursive_prefix)
@@ -1563,18 +1640,25 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
 
        strbuf_reset(&sb);
        strbuf_addf(&sb, "submodule.%s.url", sub->name);
-       if (repo_config_get_string_const(the_repository, sb.buf, &url))
-               url = sub->url;
+       if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
+               if (starts_with_dot_slash(sub->url) ||
+                   starts_with_dot_dot_slash(sub->url)) {
+                       url = compute_submodule_clone_url(sub->url);
+                       need_free_url = 1;
+               } else
+                       url = sub->url;
+       }
 
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/.git", ce->name);
        needs_cloning = !file_exists(sb.buf);
 
-       strbuf_reset(&sb);
-       strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
-                       oid_to_hex(&ce->oid), ce_stage(ce),
-                       needs_cloning, ce->name);
-       string_list_append(&suc->projectlines, sb.buf);
+       ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
+                  suc->update_clone_alloc);
+       oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
+       suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
+       suc->update_clone[suc->update_clone_nr].sub = sub;
+       suc->update_clone_nr++;
 
        if (!needs_cloning)
                goto cleanup;
@@ -1609,6 +1693,8 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
 cleanup:
        strbuf_reset(&displaypath_sb);
        strbuf_reset(&sb);
+       if (need_free_url)
+               free((void*)url);
 
        return needs_cloning;
 }
@@ -1715,11 +1801,44 @@ static int git_update_clone_config(const char *var, const char *value,
        return 0;
 }
 
+static void update_submodule(struct update_clone_data *ucd)
+{
+       fprintf(stdout, "dummy %s %d\t%s\n",
+               oid_to_hex(&ucd->oid),
+               ucd->just_cloned,
+               ucd->sub->path);
+}
+
+static int update_submodules(struct submodule_update_clone *suc)
+{
+       int i;
+
+       run_processes_parallel(suc->max_jobs,
+                              update_clone_get_next_task,
+                              update_clone_start_failure,
+                              update_clone_task_finished,
+                              suc);
+
+       /*
+        * We saved the output and put it out all at once now.
+        * That means:
+        * - the listener does not have to interleave their (checkout)
+        *   work with our fetching.  The writes involved in a
+        *   checkout involve more straightforward sequential I/O.
+        * - the listener can avoid doing any work if fetching failed.
+        */
+       if (suc->quickstop)
+               return 1;
+
+       for (i = 0; i < suc->update_clone_nr; i++)
+               update_submodule(&suc->update_clone[i]);
+
+       return 0;
+}
+
 static int update_clone(int argc, const char **argv, const char *prefix)
 {
        const char *update = NULL;
-       int max_jobs = 1;
-       struct string_list_item *item;
        struct pathspec pathspec;
        struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
 
@@ -1741,7 +1860,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)
                OPT_STRING(0, "depth", &suc.depth, "<depth>",
                           N_("Create a shallow clone truncated to the "
                              "specified number of revisions")),
-               OPT_INTEGER('j', "jobs", &max_jobs,
+               OPT_INTEGER('j', "jobs", &suc.max_jobs,
                            N_("parallel jobs")),
                OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
                            N_("whether the initial clone should follow the shallow recommendation")),
@@ -1757,8 +1876,8 @@ static int update_clone(int argc, const char **argv, const char *prefix)
        };
        suc.prefix = prefix;
 
-       update_clone_config_from_gitmodules(&max_jobs);
-       git_config(git_update_clone_config, &max_jobs);
+       update_clone_config_from_gitmodules(&suc.max_jobs);
+       git_config(git_update_clone_config, &suc.max_jobs);
 
        argc = parse_options(argc, argv, prefix, module_update_clone_options,
                             git_submodule_helper_usage, 0);
@@ -1773,27 +1892,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)
        if (pathspec.nr)
                suc.warn_if_uninitialized = 1;
 
-       run_processes_parallel(max_jobs,
-                              update_clone_get_next_task,
-                              update_clone_start_failure,
-                              update_clone_task_finished,
-                              &suc);
-
-       /*
-        * We saved the output and put it out all at once now.
-        * That means:
-        * - the listener does not have to interleave their (checkout)
-        *   work with our fetching.  The writes involved in a
-        *   checkout involve more straightforward sequential I/O.
-        * - the listener can avoid doing any work if fetching failed.
-        */
-       if (suc.quickstop)
-               return 1;
-
-       for_each_string_list_item(item, &suc.projectlines)
-               fprintf(stdout, "%s", item->string);
-
-       return 0;
+       return update_submodules(&suc);
 }
 
 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
@@ -1939,6 +2038,45 @@ static int push_check(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
+{
+       const struct submodule *sub;
+       const char *path;
+       char *cw;
+       struct repository subrepo;
+
+       if (argc != 2)
+               BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
+
+       path = argv[1];
+
+       sub = submodule_from_path(the_repository, &null_oid, path);
+       if (!sub)
+               BUG("We could get the submodule handle before?");
+
+       if (repo_submodule_init(&subrepo, the_repository, path))
+               die(_("could not get a repository handle for submodule '%s'"), path);
+
+       if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
+               char *cfg_file, *abs_path;
+               const char *rel_path;
+               struct strbuf sb = STRBUF_INIT;
+
+               cfg_file = repo_git_path(&subrepo, "config");
+
+               abs_path = absolute_pathdup(path);
+               rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
+
+               git_config_set_in_file(cfg_file, "core.worktree", rel_path);
+
+               free(cfg_file);
+               free(abs_path);
+               strbuf_release(&sb);
+       }
+
+       return 0;
+}
+
 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
 {
        int i;
@@ -2004,6 +2142,45 @@ static int check_name(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static int module_config(int argc, const char **argv, const char *prefix)
+{
+       enum {
+               CHECK_WRITEABLE = 1
+       } command = 0;
+
+       struct option module_config_options[] = {
+               OPT_CMDMODE(0, "check-writeable", &command,
+                           N_("check if it is safe to write to the .gitmodules file"),
+                           CHECK_WRITEABLE),
+               OPT_END()
+       };
+       const char *const git_submodule_helper_usage[] = {
+               N_("git submodule--helper config name [value]"),
+               N_("git submodule--helper config --check-writeable"),
+               NULL
+       };
+
+       argc = parse_options(argc, argv, prefix, module_config_options,
+                            git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
+
+       if (argc == 1 && command == CHECK_WRITEABLE)
+               return is_writing_gitmodules_ok() ? 0 : -1;
+
+       /* Equivalent to ACTION_GET in builtin/config.c */
+       if (argc == 2)
+               return print_config_from_gitmodules(the_repository, argv[1]);
+
+       /* Equivalent to ACTION_SET in builtin/config.c */
+       if (argc == 3) {
+               if (!is_writing_gitmodules_ok())
+                       die(_("please make sure that the .gitmodules file is in the working tree"));
+
+               return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
+       }
+
+       usage_with_options(git_submodule_helper_usage, module_config_options);
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -2016,7 +2193,9 @@ static struct cmd_struct commands[] = {
        {"list", module_list, 0},
        {"name", module_name, 0},
        {"clone", module_clone, 0},
+       {"update-module-mode", module_update_module_mode, 0},
        {"update-clone", update_clone, 0},
+       {"ensure-core-worktree", ensure_core_worktree, 0},
        {"relative-path", resolve_relative_path, 0},
        {"resolve-relative-url", resolve_relative_url, 0},
        {"resolve-relative-url-test", resolve_relative_url_test, 0},
@@ -2031,6 +2210,7 @@ static struct cmd_struct commands[] = {
        {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
        {"is-active", is_active, 0},
        {"check-name", check_name, 0},
+       {"config", module_config, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)