summaryrefslogtreecommitdiff
path: root/run-command.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2018-01-18 09:45:11 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-01-19 18:49:20 (GMT)
commitc61a975df1d3d5e448622f951e6dd584ee21ebf3 (patch)
tree375821474938a1f288c2c40e07b49ed0590ea7cc /run-command.c
parent21dfc5e08fec74ded0ab4206bca0927a38feb882 (diff)
downloadgit-c61a975df1d3d5e448622f951e6dd584ee21ebf3.zip
git-c61a975df1d3d5e448622f951e6dd584ee21ebf3.tar.gz
git-c61a975df1d3d5e448622f951e6dd584ee21ebf3.tar.bz2
run-command.c: print env vars in trace_run_command()
Occasionally submodule code could execute new commands with GIT_DIR set to some submodule. GIT_TRACE prints just the command line which makes it hard to tell that it's not really executed on this repository. Print the env delta (compared to parent environment) in this case. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'run-command.c')
-rw-r--r--run-command.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/run-command.c b/run-command.c
index 326c33e..1301b87 100644
--- a/run-command.c
+++ b/run-command.c
@@ -557,6 +557,63 @@ static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
return code;
}
+static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
+{
+ struct string_list envs = STRING_LIST_INIT_DUP;
+ const char *const *e;
+ int i;
+ int printed_unset = 0;
+
+ /* Last one wins, see run-command.c:prep_childenv() for context */
+ for (e = deltaenv; e && *e; e++) {
+ struct strbuf key = STRBUF_INIT;
+ char *equals = strchr(*e, '=');
+
+ if (equals) {
+ strbuf_add(&key, *e, equals - *e);
+ string_list_insert(&envs, key.buf)->util = equals + 1;
+ } else {
+ string_list_insert(&envs, *e)->util = NULL;
+ }
+ strbuf_release(&key);
+ }
+
+ /* "unset X Y...;" */
+ for (i = 0; i < envs.nr; i++) {
+ const char *var = envs.items[i].string;
+ const char *val = envs.items[i].util;
+
+ if (val || !getenv(var))
+ continue;
+
+ if (!printed_unset) {
+ strbuf_addstr(dst, " unset");
+ printed_unset = 1;
+ }
+ strbuf_addf(dst, " %s", var);
+ }
+ if (printed_unset)
+ strbuf_addch(dst, ';');
+
+ /* ... followed by "A=B C=D ..." */
+ for (i = 0; i < envs.nr; i++) {
+ const char *var = envs.items[i].string;
+ const char *val = envs.items[i].util;
+ const char *oldval;
+
+ if (!val)
+ continue;
+
+ oldval = getenv(var);
+ if (oldval && !strcmp(val, oldval))
+ continue;
+
+ strbuf_addf(dst, " %s=", var);
+ sq_quote_buf_pretty(dst, val);
+ }
+ string_list_clear(&envs, 0);
+}
+
static void trace_run_command(const struct child_process *cp)
{
struct strbuf buf = STRBUF_INIT;
@@ -565,6 +622,12 @@ static void trace_run_command(const struct child_process *cp)
return;
strbuf_addf(&buf, "trace: run_command:");
+ /*
+ * The caller is responsible for initializing cp->env from
+ * cp->env_array if needed. We only check one place.
+ */
+ if (cp->env)
+ trace_add_env(&buf, cp->env);
if (cp->git_cmd)
strbuf_addstr(&buf, " git");
sq_quote_argv_pretty(&buf, cp->argv);