Skip to content

Commit 79aec59

Browse files
peffdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5b94a77 commit 79aec59

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

builtin/submodule--helper.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -119,54 +119,6 @@ static int module_name(int argc, const char **argv, const char *prefix)
119119
return 0;
120120
}
121121

122-
/*
123-
* Rules to sanitize configuration variables that are Ok to be passed into
124-
* submodule operations from the parent project using "-c". Should only
125-
* include keys which are both (a) safe and (b) necessary for proper
126-
* operation.
127-
*/
128-
static int submodule_config_ok(const char *var)
129-
{
130-
if (starts_with(var, "credential."))
131-
return 1;
132-
return 0;
133-
}
134-
135-
static int sanitize_submodule_config(const char *var, const char *value, void *data)
136-
{
137-
struct strbuf *out = data;
138-
139-
if (submodule_config_ok(var)) {
140-
if (out->len)
141-
strbuf_addch(out, ' ');
142-
143-
if (value)
144-
sq_quotef(out, "%s=%s", var, value);
145-
else
146-
sq_quote_buf(out, var);
147-
}
148-
149-
return 0;
150-
}
151-
152-
static void prepare_submodule_repo_env(struct argv_array *out)
153-
{
154-
const char * const *var;
155-
156-
for (var = local_repo_env; *var; var++) {
157-
if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
158-
struct strbuf sanitized_config = STRBUF_INIT;
159-
git_config_from_parameters(sanitize_submodule_config,
160-
&sanitized_config);
161-
argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
162-
strbuf_release(&sanitized_config);
163-
} else {
164-
argv_array_push(out, *var);
165-
}
166-
}
167-
168-
}
169-
170122
static int clone_submodule(const char *path, const char *gitdir, const char *url,
171123
const char *depth, const char *reference, int quiet)
172124
{

submodule.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "argv-array.h"
1414
#include "blob.h"
1515
#include "thread-utils.h"
16+
#include "quote.h"
1617

1718
static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
1819
static struct string_list changed_submodule_paths;
@@ -1094,3 +1095,50 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
10941095
strbuf_release(&rel_path);
10951096
free((void *)real_work_tree);
10961097
}
1098+
/*
1099+
* Rules to sanitize configuration variables that are Ok to be passed into
1100+
* submodule operations from the parent project using "-c". Should only
1101+
* include keys which are both (a) safe and (b) necessary for proper
1102+
* operation.
1103+
*/
1104+
static int submodule_config_ok(const char *var)
1105+
{
1106+
if (starts_with(var, "credential."))
1107+
return 1;
1108+
return 0;
1109+
}
1110+
1111+
int sanitize_submodule_config(const char *var, const char *value, void *data)
1112+
{
1113+
struct strbuf *out = data;
1114+
1115+
if (submodule_config_ok(var)) {
1116+
if (out->len)
1117+
strbuf_addch(out, ' ');
1118+
1119+
if (value)
1120+
sq_quotef(out, "%s=%s", var, value);
1121+
else
1122+
sq_quote_buf(out, var);
1123+
}
1124+
1125+
return 0;
1126+
}
1127+
1128+
void prepare_submodule_repo_env(struct argv_array *out)
1129+
{
1130+
const char * const *var;
1131+
1132+
for (var = local_repo_env; *var; var++) {
1133+
if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
1134+
struct strbuf sanitized_config = STRBUF_INIT;
1135+
git_config_from_parameters(sanitize_submodule_config,
1136+
&sanitized_config);
1137+
argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
1138+
strbuf_release(&sanitized_config);
1139+
} else {
1140+
argv_array_push(out, *var);
1141+
}
1142+
}
1143+
1144+
}

submodule.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,20 @@ int find_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_nam
4343
int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);
4444
void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
4545

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+
4662
#endif

0 commit comments

Comments
 (0)