Skip to content

Commit b9b5924

Browse files
committed
run-command config experiment
Signed-off-by: Jeff King <peff@peff.net>
1 parent 4d61fcd commit b9b5924

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

run-command.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ int start_command(struct child_process *cmd)
681681
int failed_errno;
682682
const char *str;
683683

684+
if (cmd->git_cmd)
685+
trace_config_for(the_repository, cmd->args.v[0], &cmd->env);
686+
684687
/*
685688
* In case of errors we must keep the promise to close FDs
686689
* that have been passed in via ->in and ->out.

trace.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "quote.h"
3030
#include "setup.h"
3131
#include "trace.h"
32+
#include "strvec.h"
33+
#include "config.h"
3234

3335
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
3436
struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
@@ -442,3 +444,47 @@ void trace_command_performance(const char **argv)
442444
sq_quote_argv_pretty(&command_line, argv);
443445
trace_performance_enter();
444446
}
447+
448+
struct trace_config_data {
449+
const char *want_cmd;
450+
struct strvec *out;
451+
};
452+
453+
static int trace_config_cb(const char *var, const char *value,
454+
const struct config_context *ctx UNUSED, void *vdata)
455+
{
456+
struct trace_config_data *data = vdata;
457+
const char *have_cmd, *key;
458+
size_t have_len;
459+
460+
if (!parse_config_key(var, "trace", &have_cmd, &have_len, &key) &&
461+
have_cmd &&
462+
!strncmp(data->want_cmd, have_cmd, have_len) &&
463+
data->want_cmd[have_len] == '\0') {
464+
struct strbuf buf = STRBUF_INIT;
465+
466+
strbuf_addstr(&buf, "GIT_TRACE_");
467+
while (*key)
468+
strbuf_addch(&buf, toupper(*key++));
469+
470+
/*
471+
* Environment always takes precedence over config, so do not
472+
* override existing variables. We cannot rely on setenv()'s
473+
* overwrite flag here, because we may pass the list off to
474+
* a spawn() implementation, which always overwrites.
475+
*/
476+
if (!getenv(buf.buf))
477+
strvec_pushf(data->out, "%s=%s", buf.buf, value);
478+
479+
strbuf_release(&buf);
480+
}
481+
return 0;
482+
}
483+
484+
void trace_config_for(struct repository *r, const char *cmd, struct strvec *out)
485+
{
486+
struct trace_config_data data;
487+
data.want_cmd = cmd;
488+
data.out = out;
489+
repo_config(r, trace_config_cb, &data);
490+
}

trace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include "strbuf.h"
55

6+
struct repository;
7+
struct strvec;
8+
69
/**
710
* The trace API can be used to print debug messages to stderr or a file. Trace
811
* code is inactive unless explicitly enabled by setting `GIT_TRACE*` environment
@@ -127,6 +130,13 @@ void trace_command_performance(const char **argv);
127130
void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
128131
uint64_t trace_performance_enter(void);
129132

133+
/**
134+
* Load any trace-related config for git command "cmd", and insert the matching
135+
* environment variables into "out", which is suitable for use by run-command
136+
* and friends.
137+
*/
138+
void trace_config_for(struct repository *r, const char *cmd, struct strvec *out);
139+
130140
/*
131141
* TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
132142
* default is __FILE__, as it is consistent with assert(), and static function

0 commit comments

Comments
 (0)