Skip to content

Commit

Permalink
Refactor early config parsing
Browse files Browse the repository at this point in the history
The Trace2 machinery wishes to parse the system and the global config
early. To this end, it taps into the `common-main` framework to run
first thing.

There are more Git features that could benefit from such a handling,
most notably the Windows-specific `core.longPaths` setting: If a user
has worktrees whose path already requires long paths support, we cannot
wait until we parse the the config settings in the usual way because
the gitdir discovery needs to happen first and would fail because any
`core.longPaths` setting in, say, `~/.gitconfig` would not have been
parsed yet.

To that end, let's refactor Trace2's early config parsing so that other
users can tap into it, too.

Signed-off-by: Kevin Worm <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
kevin-worm authored and dscho committed May 30, 2022
1 parent 590ade5 commit 3b059de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
10 changes: 10 additions & 0 deletions common-main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "cache.h"
#include "config.h"
#include "exec-cmd.h"
#include "attr.h"
#include "trace2/tr2_sysenv.h"

/*
* Many parts of Git have subprograms communicate via pipe, expect the
Expand All @@ -23,6 +25,11 @@ static void restore_sigpipe_to_default(void)
signal(SIGPIPE, SIG_DFL);
}

static int read_very_early_config_cb(const char *key, const char *value, void *d)
{
return tr2_sysenv_cb(key, value, d);
}

int main(int argc, const char **argv)
{
int result;
Expand All @@ -46,7 +53,10 @@ int main(int argc, const char **argv)

attr_start();

read_very_early_config(read_very_early_config_cb, NULL);

trace2_initialize();

trace2_cmd_start(argv);
trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);

Expand Down
2 changes: 1 addition & 1 deletion trace2.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void trace2_initialize_fl(const char *file, int line)
if (trace2_enabled)
return;

tr2_sysenv_load();
tr2_sysenv_check_size();

if (!tr2_tgt_want_builtins())
return;
Expand Down
20 changes: 9 additions & 11 deletions trace2/tr2_sysenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
};
/* clang-format on */

static int tr2_sysenv_cb(const char *key, const char *value, void *d)
/*
* Load Trace2 settings from the system config (usually "/etc/gitconfig"
* unless we were built with a runtime-prefix). These are intended to
* define the default values for Trace2 as requested by the administrator.
*
* Then override with the Trace2 settings from the global config.
*/
int tr2_sysenv_cb(const char *key, const char *value, void *d)
{
int k;

Expand All @@ -75,19 +82,10 @@ static int tr2_sysenv_cb(const char *key, const char *value, void *d)
return 0;
}

/*
* Load Trace2 settings from the system config (usually "/etc/gitconfig"
* unless we were built with a runtime-prefix). These are intended to
* define the default values for Trace2 as requested by the administrator.
*
* Then override with the Trace2 settings from the global config.
*/
void tr2_sysenv_load(void)
void tr2_sysenv_check_size(void)
{
if (ARRAY_SIZE(tr2_sysenv_settings) != TR2_SYSENV_MUST_BE_LAST)
BUG("tr2_sysenv_settings size is wrong");

read_very_early_config(tr2_sysenv_cb, NULL);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion trace2/tr2_sysenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum tr2_sysenv_variable {
TR2_SYSENV_MUST_BE_LAST
};

void tr2_sysenv_load(void);
int tr2_sysenv_cb(const char *key, const char *value, void *d);
void tr2_sysenv_check_size(void);

const char *tr2_sysenv_get(enum tr2_sysenv_variable);
const char *tr2_sysenv_display_name(enum tr2_sysenv_variable var);
Expand Down

0 comments on commit 3b059de

Please sign in to comment.