submodule: add a helper to check if it is safe to write to .gitmodules
[gitweb.git] / builtin / submodule--helper.c
index 2bcc70fdfe2608ccbbab02b6f593dd81781ada41..8699515e81c26c6e70b3f1de95a35ab118ac3dd6 100644 (file)
@@ -542,7 +542,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
                argv_array_pushv(&cpr.args, info->argv);
 
                if (run_command(&cpr))
-                       die(_("run_command returned non-zero status while"
+                       die(_("run_command returned non-zero status while "
                                "recursing in the nested submodules of %s\n."),
                                displaypath);
        }
@@ -1123,8 +1123,6 @@ static void deinit_submodule(const char *path, const char *prefix,
                if (!(flags & OPT_QUIET))
                        printf(format, displaypath);
 
-               submodule_unset_core_worktree(sub);
-
                strbuf_release(&sb_rm);
        }
 
@@ -2005,27 +2003,39 @@ static int check_name(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
-static int connect_gitdir_workingtree(int argc, const char **argv, const char *prefix)
+static int module_config(int argc, const char **argv, const char *prefix)
 {
-       struct strbuf sb = STRBUF_INIT;
-       const char *name, *path;
-       char *sm_gitdir;
-
-       if (argc != 3)
-               BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
+       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
+       };
 
-       name = argv[1];
-       path = argv[2];
+       argc = parse_options(argc, argv, prefix, module_config_options,
+                            git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
 
-       strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
-       sm_gitdir = absolute_pathdup(sb.buf);
+       if (argc == 1 && command == CHECK_WRITEABLE)
+               return is_writing_gitmodules_ok() ? 0 : -1;
 
-       connect_work_tree_and_git_dir(path, sm_gitdir, 0);
+       /* Equivalent to ACTION_GET in builtin/config.c */
+       if (argc == 2)
+               return print_config_from_gitmodules(the_repository, argv[1]);
 
-       strbuf_release(&sb);
-       free(sm_gitdir);
+       /* Equivalent to ACTION_SET in builtin/config.c */
+       if (argc == 3)
+               return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
 
-       return 0;
+       usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
 #define SUPPORT_SUPER_PREFIX (1<<0)
@@ -2041,7 +2051,6 @@ static struct cmd_struct commands[] = {
        {"name", module_name, 0},
        {"clone", module_clone, 0},
        {"update-clone", update_clone, 0},
-       {"connect-gitdir-workingtree", connect_gitdir_workingtree, 0},
        {"relative-path", resolve_relative_path, 0},
        {"resolve-relative-url", resolve_relative_url, 0},
        {"resolve-relative-url-test", resolve_relative_url_test, 0},
@@ -2056,6 +2065,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)