From 3b059de54227e1fb0628f9cc47f2783e69c04401 Mon Sep 17 00:00:00 2001 From: Kevin Worm Date: Thu, 12 May 2022 10:33:33 +0200 Subject: [PATCH] Refactor early config parsing 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 Signed-off-by: Johannes Schindelin --- common-main.c | 10 ++++++++++ trace2.c | 2 +- trace2/tr2_sysenv.c | 20 +++++++++----------- trace2/tr2_sysenv.h | 3 ++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/common-main.c b/common-main.c index 29fb7452f8a0b7..269bb96bf7836c 100644 --- a/common-main.c +++ b/common-main.c @@ -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 @@ -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; @@ -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); diff --git a/trace2.c b/trace2.c index e01cf77f1a894e..80b805074a6f1f 100644 --- a/trace2.c +++ b/trace2.c @@ -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; diff --git a/trace2/tr2_sysenv.c b/trace2/tr2_sysenv.c index a380dcf9105e8d..b7c371caf1fa7f 100644 --- a/trace2/tr2_sysenv.c +++ b/trace2/tr2_sysenv.c @@ -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; @@ -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); } /* diff --git a/trace2/tr2_sysenv.h b/trace2/tr2_sysenv.h index 3292ee15bc9676..685ba5d6ae0b92 100644 --- a/trace2/tr2_sysenv.h +++ b/trace2/tr2_sysenv.h @@ -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);