]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/submodule--helper.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / submodule--helper.c
index 05f2c9bc9858d98ae855b566d6128030e002fa18..1f087d7bed2dfb9a82d80774337cd977a9af0e1f 100644 (file)
@@ -1,5 +1,10 @@
 #define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
+#include "abspath.h"
+#include "alloc.h"
+#include "environment.h"
+#include "gettext.h"
+#include "hex.h"
 #include "repository.h"
 #include "cache.h"
 #include "config.h"
@@ -7,6 +12,7 @@
 #include "quote.h"
 #include "pathspec.h"
 #include "dir.h"
+#include "setup.h"
 #include "submodule.h"
 #include "submodule-config.h"
 #include "string-list.h"
@@ -18,6 +24,7 @@
 #include "revision.h"
 #include "diffcore.h"
 #include "diff.h"
+#include "object-name.h"
 #include "object-store.h"
 #include "advice.h"
 #include "branch.h"
@@ -113,10 +120,9 @@ static char *resolve_relative_url(const char *rel_url, const char *up_path, int
 }
 
 /* the result should be freed by the caller. */
-static char *get_submodule_displaypath(const char *path, const char *prefix)
+static char *get_submodule_displaypath(const char *path, const char *prefix,
+                                      const char *super_prefix)
 {
-       const char *super_prefix = get_super_prefix();
-
        if (prefix && super_prefix) {
                BUG("cannot have prefix '%s' and superprefix '%s'",
                    prefix, super_prefix);
@@ -279,6 +285,7 @@ struct foreach_cb {
        int argc;
        const char **argv;
        const char *prefix;
+       const char *super_prefix;
        int quiet;
        int recursive;
 };
@@ -294,7 +301,8 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
        struct child_process cp = CHILD_PROCESS_INIT;
        char *displaypath;
 
-       displaypath = get_submodule_displaypath(path, info->prefix);
+       displaypath = get_submodule_displaypath(path, info->prefix,
+                                               info->super_prefix);
 
        sub = submodule_from_path(the_repository, null_oid(), path);
 
@@ -364,10 +372,10 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
                cpr.dir = path;
                prepare_submodule_repo_env(&cpr.env);
 
-               strvec_pushl(&cpr.args, "--super-prefix", NULL);
-               strvec_pushf(&cpr.args, "%s/", displaypath);
                strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
                             NULL);
+               strvec_pushl(&cpr.args, "--super-prefix", NULL);
+               strvec_pushf(&cpr.args, "%s/", displaypath);
 
                if (info->quiet)
                        strvec_push(&cpr.args, "--quiet");
@@ -391,6 +399,7 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
        struct pathspec pathspec = { 0 };
        struct module_list list = MODULE_LIST_INIT;
        struct option module_foreach_options[] = {
+               OPT__SUPER_PREFIX(&info.super_prefix),
                OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
                OPT_BOOL(0, "recursive", &info.recursive,
                         N_("recurse into nested submodules")),
@@ -435,11 +444,13 @@ static int starts_with_dot_dot_slash(const char *const path)
 
 struct init_cb {
        const char *prefix;
+       const char *super_prefix;
        unsigned int flags;
 };
 #define INIT_CB_INIT { 0 }
 
 static void init_submodule(const char *path, const char *prefix,
+                          const char *super_prefix,
                           unsigned int flags)
 {
        const struct submodule *sub;
@@ -447,7 +458,7 @@ static void init_submodule(const char *path, const char *prefix,
        const char *upd;
        char *url = NULL, *displaypath;
 
-       displaypath = get_submodule_displaypath(path, prefix);
+       displaypath = get_submodule_displaypath(path, prefix, super_prefix);
 
        sub = submodule_from_path(the_repository, null_oid(), path);
 
@@ -523,7 +534,8 @@ static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data
 {
        struct init_cb *info = cb_data;
 
-       init_submodule(list_item->name, info->prefix, info->flags);
+       init_submodule(list_item->name, info->prefix, info->super_prefix,
+                      info->flags);
 }
 
 static int module_init(int argc, const char **argv, const char *prefix)
@@ -570,6 +582,7 @@ cleanup:
 
 struct status_cb {
        const char *prefix;
+       const char *super_prefix;
        unsigned int flags;
 };
 #define STATUS_CB_INIT { 0 }
@@ -608,7 +621,7 @@ static int handle_submodule_head_ref(const char *refname UNUSED,
 
 static void status_submodule(const char *path, const struct object_id *ce_oid,
                             unsigned int ce_flags, const char *prefix,
-                            unsigned int flags)
+                            const char *super_prefix, unsigned int flags)
 {
        char *displaypath;
        struct strvec diff_files_args = STRVEC_INIT;
@@ -624,7 +637,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
                die(_("no submodule mapping found in .gitmodules for path '%s'"),
                      path);
 
-       displaypath = get_submodule_displaypath(path, prefix);
+       displaypath = get_submodule_displaypath(path, prefix, super_prefix);
 
        if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
                print_status(flags, 'U', path, null_oid(), displaypath);
@@ -682,10 +695,10 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
                cpr.dir = path;
                prepare_submodule_repo_env(&cpr.env);
 
-               strvec_push(&cpr.args, "--super-prefix");
-               strvec_pushf(&cpr.args, "%s/", displaypath);
                strvec_pushl(&cpr.args, "submodule--helper", "status",
                             "--recursive", NULL);
+               strvec_push(&cpr.args, "--super-prefix");
+               strvec_pushf(&cpr.args, "%s/", displaypath);
 
                if (flags & OPT_CACHED)
                        strvec_push(&cpr.args, "--cached");
@@ -709,7 +722,7 @@ static void status_submodule_cb(const struct cache_entry *list_item,
        struct status_cb *info = cb_data;
 
        status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
-                        info->prefix, info->flags);
+                        info->prefix, info->super_prefix, info->flags);
 }
 
 static int module_status(int argc, const char **argv, const char *prefix)
@@ -719,6 +732,7 @@ static int module_status(int argc, const char **argv, const char *prefix)
        struct module_list list = MODULE_LIST_INIT;
        int quiet = 0;
        struct option module_status_options[] = {
+               OPT__SUPER_PREFIX(&info.super_prefix),
                OPT__QUIET(&quiet, N_("suppress submodule status output")),
                OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
                OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
@@ -787,6 +801,7 @@ struct summary_cb {
        int argc;
        const char **argv;
        const char *prefix;
+       const char *super_prefix;
        unsigned int cached: 1;
        unsigned int for_status: 1;
        unsigned int files: 1;
@@ -948,7 +963,8 @@ static void generate_submodule_summary(struct summary_cb *info,
                dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
        }
 
-       displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
+       displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
+                                               info->super_prefix);
 
        if (!missing_src && !missing_dst) {
                struct child_process cp_rev_list = CHILD_PROCESS_INIT;
@@ -1043,7 +1059,7 @@ static void prepare_submodule_summary(struct summary_cb *info,
 }
 
 static void submodule_summary_callback(struct diff_queue_struct *q,
-                                      struct diff_options *options,
+                                      struct diff_options *options UNUSED,
                                       void *data)
 {
        int i;
@@ -1099,7 +1115,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
                strvec_pushv(&diff_args, info->argv);
 
        git_config(git_diff_basic_config, NULL);
-       init_revisions(&rev, info->prefix);
+       repo_init_revisions(the_repository, &rev, info->prefix);
        rev.abbrev = 0;
        precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
        setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
@@ -1165,7 +1181,7 @@ static int module_summary(int argc, const char **argv, const char *prefix)
        if (!summary_limit)
                return 0;
 
-       if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
+       if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
                if (argc) {
                        argv++;
                        argc--;
@@ -1178,7 +1194,7 @@ static int module_summary(int argc, const char **argv, const char *prefix)
                        argc--;
                }
        } else {
-               if (get_oid("HEAD", &head_oid))
+               if (repo_get_oid(the_repository, "HEAD", &head_oid))
                        die(_("could not fetch a revision for HEAD"));
        }
 
@@ -1203,12 +1219,13 @@ static int module_summary(int argc, const char **argv, const char *prefix)
 
 struct sync_cb {
        const char *prefix;
+       const char *super_prefix;
        unsigned int flags;
 };
 #define SYNC_CB_INIT { 0 }
 
 static void sync_submodule(const char *path, const char *prefix,
-                          unsigned int flags)
+                          const char *super_prefix, unsigned int flags)
 {
        const struct submodule *sub;
        char *remote_key = NULL;
@@ -1239,7 +1256,7 @@ static void sync_submodule(const char *path, const char *prefix,
                super_config_url = xstrdup("");
        }
 
-       displaypath = get_submodule_displaypath(path, prefix);
+       displaypath = get_submodule_displaypath(path, prefix, super_prefix);
 
        if (!(flags & OPT_QUIET))
                printf(_("Synchronizing submodule url for '%s'\n"),
@@ -1276,10 +1293,11 @@ static void sync_submodule(const char *path, const char *prefix,
                cpr.dir = path;
                prepare_submodule_repo_env(&cpr.env);
 
-               strvec_push(&cpr.args, "--super-prefix");
-               strvec_pushf(&cpr.args, "%s/", displaypath);
                strvec_pushl(&cpr.args, "submodule--helper", "sync",
                             "--recursive", NULL);
+               strvec_push(&cpr.args, "--super-prefix");
+               strvec_pushf(&cpr.args, "%s/", displaypath);
+
 
                if (flags & OPT_QUIET)
                        strvec_push(&cpr.args, "--quiet");
@@ -1302,7 +1320,8 @@ static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data
 {
        struct sync_cb *info = cb_data;
 
-       sync_submodule(list_item->name, info->prefix, info->flags);
+       sync_submodule(list_item->name, info->prefix, info->super_prefix,
+                      info->flags);
 }
 
 static int module_sync(int argc, const char **argv, const char *prefix)
@@ -1313,6 +1332,7 @@ static int module_sync(int argc, const char **argv, const char *prefix)
        int quiet = 0;
        int recursive = 0;
        struct option module_sync_options[] = {
+               OPT__SUPER_PREFIX(&info.super_prefix),
                OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
                OPT_BOOL(0, "recursive", &recursive,
                        N_("recurse into nested submodules")),
@@ -1365,7 +1385,7 @@ static void deinit_submodule(const char *path, const char *prefix,
        if (!sub || !sub->name)
                goto cleanup;
 
-       displaypath = get_submodule_displaypath(path, prefix);
+       displaypath = get_submodule_displaypath(path, prefix, NULL);
 
        /* remove the submodule work tree (unless the user already did it) */
        if (is_directory(path)) {
@@ -1379,7 +1399,7 @@ static void deinit_submodule(const char *path, const char *prefix,
                                          ".git file by using absorbgitdirs."),
                                        displaypath);
 
-                       absorb_git_dir_into_superproject(path);
+                       absorb_git_dir_into_superproject(path, NULL);
 
                }
 
@@ -1883,6 +1903,7 @@ static void submodule_update_clone_release(struct submodule_update_clone *suc)
 
 struct update_data {
        const char *prefix;
+       const char *super_prefix;
        char *displaypath;
        enum submodule_update_type update_default;
        struct object_id suboid;
@@ -1958,7 +1979,8 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
        enum submodule_update_type update_type;
        char *key;
        const struct update_data *ud = suc->update_data;
-       char *displaypath = get_submodule_displaypath(ce->name, ud->prefix);
+       char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
+                                                     ud->super_prefix);
        struct strbuf sb = STRBUF_INIT;
        int needs_cloning = 0;
        int need_free_url = 0;
@@ -2117,9 +2139,9 @@ static int update_clone_get_next_task(struct child_process *child,
        return 0;
 }
 
-static int update_clone_start_failure(struct strbuf *err,
+static int update_clone_start_failure(struct strbuf *err UNUSED,
                                      void *suc_cb,
-                                     void *idx_task_cb)
+                                     void *idx_task_cb UNUSED)
 {
        struct submodule_update_clone *suc = suc_cb;
 
@@ -2438,11 +2460,11 @@ static void update_data_to_args(const struct update_data *update_data,
 {
        enum submodule_update_type update_type = update_data->update_default;
 
+       strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
        if (update_data->displaypath) {
                strvec_push(args, "--super-prefix");
                strvec_pushf(args, "%s/", update_data->displaypath);
        }
-       strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
        strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
        if (update_data->quiet)
                strvec_push(args, "--quiet");
@@ -2608,7 +2630,8 @@ static int update_submodules(struct update_data *update_data)
                        goto fail;
 
                update_data->displaypath = get_submodule_displaypath(
-                       update_data->sm_path, update_data->prefix);
+                       update_data->sm_path, update_data->prefix,
+                       update_data->super_prefix);
                code = update_submodule(update_data);
                FREE_AND_NULL(update_data->displaypath);
 fail:
@@ -2634,6 +2657,7 @@ static int module_update(int argc, const char **argv, const char *prefix)
                LIST_OBJECTS_FILTER_INIT;
        int ret;
        struct option module_update_options[] = {
+               OPT__SUPER_PREFIX(&opt.super_prefix),
                OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
                OPT_BOOL(0, "init", &opt.init,
                         N_("initialize uninitialized submodules before update")),
@@ -2730,6 +2754,7 @@ static int module_update(int argc, const char **argv, const char *prefix)
                        module_list_active(&list);
 
                info.prefix = opt.prefix;
+               info.super_prefix = opt.super_prefix;
                if (opt.quiet)
                        info.flags |= OPT_QUIET;
 
@@ -2828,7 +2853,9 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
        int i;
        struct pathspec pathspec = { 0 };
        struct module_list list = MODULE_LIST_INIT;
+       const char *super_prefix = NULL;
        struct option embed_gitdir_options[] = {
+               OPT__SUPER_PREFIX(&super_prefix),
                OPT_END()
        };
        const char *const git_submodule_helper_usage[] = {
@@ -2844,7 +2871,8 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
                goto cleanup;
 
        for (i = 0; i < list.nr; i++)
-               absorb_git_dir_into_superproject(list.entries[i]->name);
+               absorb_git_dir_into_superproject(list.entries[i]->name,
+                                                super_prefix);
 
        ret = 0;
 cleanup:
@@ -2876,7 +2904,7 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
        config_name = xstrfmt("submodule.%s.url", path);
 
        config_set_in_gitmodules_file_gently(config_name, newurl);
-       sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+       sync_submodule(path, prefix, NULL, quiet ? OPT_QUIET : 0);
 
        free(config_name);
 
@@ -3198,9 +3226,8 @@ static void die_on_index_match(const char *path, int force)
                ensure_full_index(&the_index);
 
                /*
-                * Since there is only one pathspec, we just need
-                * need to check ps_matched[0] to know if a cache
-                * entry matched.
+                * Since there is only one pathspec, we just need to
+                * check ps_matched[0] to know if a cache entry matched.
                 */
                for (i = 0; i < the_index.cache_nr; i++) {
                        ce_path_match(&the_index, the_index.cache[i], &ps,
@@ -3353,8 +3380,6 @@ cleanup:
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
 {
-       const char *cmd = argv[0];
-       const char *subcmd;
        parse_opt_subcommand_fn *fn = NULL;
        const char *const usage[] = {
                N_("git submodule--helper <command>"),
@@ -3378,18 +3403,6 @@ int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
        argc = parse_options(argc, argv, prefix, options, usage, 0);
-       subcmd = argv[0];
-
-       if (strcmp(subcmd, "clone") && strcmp(subcmd, "update") &&
-           strcmp(subcmd, "foreach") && strcmp(subcmd, "status") &&
-           strcmp(subcmd, "sync") && strcmp(subcmd, "absorbgitdirs") &&
-           get_super_prefix())
-               /*
-                * xstrfmt() rather than "%s %s" to keep the translated
-                * string identical to git.c's.
-                */
-               die(_("%s doesn't support --super-prefix"),
-                   xstrfmt("'%s %s'", cmd, subcmd));
 
        return fn(argc, argv, prefix);
 }