submodule--helper: move config-sanitizing to submodule.c

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>

Jeff King committed Apr 28, 2016 at 09:38 UTC 4638728c632e59715b7346ddeca83528d37a4894
3 files changed +64 -48
builtin/submodule--helper.c
-48
@@ -125,54 +125,6 @@ static int module_name(int argc, const char **argv, const char *prefix)
125 return 0;
126 }
127
128 -/*
129 - * Rules to sanitize configuration variables that are Ok to be passed into
130 - * submodule operations from the parent project using "-c". Should only
131 - * include keys which are both (a) safe and (b) necessary for proper
132 - * operation.
133 - */
134 -static int submodule_config_ok(const char *var)
135 -{
136 - if (starts_with(var, "credential."))
137 - return 1;
138 - return 0;
139 -}
140 -
141 -static int sanitize_submodule_config(const char *var, const char *value, void *data)
142 -{
143 - struct strbuf *out = data;
144 -
145 - if (submodule_config_ok(var)) {
146 - if (out->len)
147 - strbuf_addch(out, ' ');
148 -
149 - if (value)
150 - sq_quotef(out, "%s=%s", var, value);
151 - else
152 - sq_quote_buf(out, var);
153 - }
154 -
155 - return 0;
156 -}
157 -
158 -static void prepare_submodule_repo_env(struct argv_array *out)
159 -{
160 - const char * const *var;
161 -
162 - for (var = local_repo_env; *var; var++) {
163 - if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
164 - struct strbuf sanitized_config = STRBUF_INIT;
165 - git_config_from_parameters(sanitize_submodule_config,
166 - &sanitized_config);
167 - argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
168 - strbuf_release(&sanitized_config);
169 - } else {
170 - argv_array_push(out, *var);
171 - }
172 - }
173 -
174 -}
175 -
128 static int clone_submodule(const char *path, const char *gitdir, const char *url,
129 const char *depth, const char *reference, int quiet)
130 {
submodule.c
+48
@@ -13,6 +13,7 @@
13 #include "argv-array.h"
14 #include "blob.h"
15 #include "thread-utils.h"
16 +#include "quote.h"
17
18 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
19 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)
1098 strbuf_release(&rel_path);
1099 free((void *)real_work_tree);
1100 }
1101 +/*
1102 + * Rules to sanitize configuration variables that are Ok to be passed into
1103 + * submodule operations from the parent project using "-c". Should only
1104 + * include keys which are both (a) safe and (b) necessary for proper
1105 + * operation.
1106 + */
1107 +static int submodule_config_ok(const char *var)
1108 +{
1109 + if (starts_with(var, "credential."))
1110 + return 1;
1111 + return 0;
1112 +}
1113 +
1114 +int sanitize_submodule_config(const char *var, const char *value, void *data)
1115 +{
1116 + struct strbuf *out = data;
1117 +
1118 + if (submodule_config_ok(var)) {
1119 + if (out->len)
1120 + strbuf_addch(out, ' ');
1121 +
1122 + if (value)
1123 + sq_quotef(out, "%s=%s", var, value);
1124 + else
1125 + sq_quote_buf(out, var);
1126 + }
1127 +
1128 + return 0;
1129 +}
1130 +
1131 +void prepare_submodule_repo_env(struct argv_array *out)
1132 +{
1133 + const char * const *var;
1134 +
1135 + for (var = local_repo_env; *var; var++) {
1136 + if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
1137 + struct strbuf sanitized_config = STRBUF_INIT;
1138 + git_config_from_parameters(sanitize_submodule_config,
1139 + &sanitized_config);
1140 + argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
1141 + strbuf_release(&sanitized_config);
1142 + } else {
1143 + argv_array_push(out, *var);
1144 + }
1145 + }
1146 +
1147 +}
submodule.h
+16
@@ -43,4 +43,20 @@ int find_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_nam
43 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);
44 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
45
46 +/*
47 + * This function is intended as a callback for use with
48 + * git_config_from_parameters(). It ignores any config options which
49 + * are not suitable for passing along to a submodule, and accumulates the rest
50 + * in "data", which must be a pointer to a strbuf. The end result can
51 + * be put into $GIT_CONFIG_PARAMETERS for passing to a sub-process.
52 + */
53 +int sanitize_submodule_config(const char *var, const char *value, void *data);
54 +
55 +/*
56 + * Prepare the "env_array" parameter of a "struct child_process" for executing
57 + * a submodule by clearing any repo-specific envirionment variables, but
58 + * retaining any config approved by sanitize_submodule_config().
59 + */
60 +void prepare_submodule_repo_env(struct argv_array *out);
61 +
62 #endif