submodule--helper: move config-sanitizing to submodule.c
authorJeff King <peff@peff.net>
Thu, 28 Apr 2016 13:38:20 +0000 (09:38 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Apr 2016 19:15:21 +0000 (12:15 -0700)
These functions should be used by any code which spawns a
submodule process, which may happen in submodule.c (e.g.,
for spawning fetch). Let's move them there and make them
public so that submodule--helper can continue to use them.

Since they're now public, let's also provide a basic overview
of their intended use.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
submodule.c
submodule.h
index 3d37c3f1822db14d376a19afd443c3082e8c5a8e..16d6432f911fce621c6adceae84a1411ea4dabc8 100644 (file)
@@ -125,54 +125,6 @@ static int module_name(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
        return 0;
 }
 
-/*
- * Rules to sanitize configuration variables that are Ok to be passed into
- * submodule operations from the parent project using "-c". Should only
- * include keys which are both (a) safe and (b) necessary for proper
- * operation.
- */
-static int submodule_config_ok(const char *var)
-{
-       if (starts_with(var, "credential."))
-               return 1;
-       return 0;
-}
-
-static int sanitize_submodule_config(const char *var, const char *value, void *data)
-{
-       struct strbuf *out = data;
-
-       if (submodule_config_ok(var)) {
-               if (out->len)
-                       strbuf_addch(out, ' ');
-
-               if (value)
-                       sq_quotef(out, "%s=%s", var, value);
-               else
-                       sq_quote_buf(out, var);
-       }
-
-       return 0;
-}
-
-static void prepare_submodule_repo_env(struct argv_array *out)
-{
-       const char * const *var;
-
-       for (var = local_repo_env; *var; var++) {
-               if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
-                       struct strbuf sanitized_config = STRBUF_INIT;
-                       git_config_from_parameters(sanitize_submodule_config,
-                                                  &sanitized_config);
-                       argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
-                       strbuf_release(&sanitized_config);
-               } else {
-                       argv_array_push(out, *var);
-               }
-       }
-
-}
-
 static int clone_submodule(const char *path, const char *gitdir, const char *url,
                           const char *depth, const char *reference, int quiet)
 {
 static int clone_submodule(const char *path, const char *gitdir, const char *url,
                           const char *depth, const char *reference, int quiet)
 {
index b83939c294782ac59e5c78c8882064a1e272e309..24e8182f5937a2602b90ae73790eca04cb1ab51f 100644 (file)
@@ -13,6 +13,7 @@
 #include "argv-array.h"
 #include "blob.h"
 #include "thread-utils.h"
 #include "argv-array.h"
 #include "blob.h"
 #include "thread-utils.h"
+#include "quote.h"
 
 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
 static struct string_list changed_submodule_paths;
 
 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
 static struct string_list changed_submodule_paths;
@@ -1097,3 +1098,50 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
        strbuf_release(&rel_path);
        free((void *)real_work_tree);
 }
        strbuf_release(&rel_path);
        free((void *)real_work_tree);
 }
+/*
+ * Rules to sanitize configuration variables that are Ok to be passed into
+ * submodule operations from the parent project using "-c". Should only
+ * include keys which are both (a) safe and (b) necessary for proper
+ * operation.
+ */
+static int submodule_config_ok(const char *var)
+{
+       if (starts_with(var, "credential."))
+               return 1;
+       return 0;
+}
+
+int sanitize_submodule_config(const char *var, const char *value, void *data)
+{
+       struct strbuf *out = data;
+
+       if (submodule_config_ok(var)) {
+               if (out->len)
+                       strbuf_addch(out, ' ');
+
+               if (value)
+                       sq_quotef(out, "%s=%s", var, value);
+               else
+                       sq_quote_buf(out, var);
+       }
+
+       return 0;
+}
+
+void prepare_submodule_repo_env(struct argv_array *out)
+{
+       const char * const *var;
+
+       for (var = local_repo_env; *var; var++) {
+               if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
+                       struct strbuf sanitized_config = STRBUF_INIT;
+                       git_config_from_parameters(sanitize_submodule_config,
+                                                  &sanitized_config);
+                       argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
+                       strbuf_release(&sanitized_config);
+               } else {
+                       argv_array_push(out, *var);
+               }
+       }
+
+}
index e06eaa5ebb30e825fd0721c76e7d194b0b854706..48690b156a2ef9c97a5fad5cad6e79a231becf2d 100644 (file)
@@ -43,4 +43,20 @@ int find_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_nam
 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);
 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
 
 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);
 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
 
+/*
+ * This function is intended as a callback for use with
+ * git_config_from_parameters(). It ignores any config options which
+ * are not suitable for passing along to a submodule, and accumulates the rest
+ * in "data", which must be a pointer to a strbuf. The end result can
+ * be put into $GIT_CONFIG_PARAMETERS for passing to a sub-process.
+ */
+int sanitize_submodule_config(const char *var, const char *value, void *data);
+
+/*
+ * Prepare the "env_array" parameter of a "struct child_process" for executing
+ * a submodule by clearing any repo-specific envirionment variables, but
+ * retaining any config approved by sanitize_submodule_config().
+ */
+void prepare_submodule_repo_env(struct argv_array *out);
+
 #endif
 #endif