]> git.ipfire.org Git - thirdparty/git.git/blame - trace2/tr2_sysenv.c
Merge branch 'ws/git-push-doc-grammofix' into maint-2.42
[thirdparty/git.git] / trace2 / tr2_sysenv.c
CommitLineData
ac48adf4 1#include "git-compat-util.h"
bce9db6d
JH
2#include "config.h"
3#include "dir.h"
4#include "tr2_sysenv.h"
5
6/*
7 * Each entry represents a trace2 setting.
8 * See Documentation/technical/api-trace2.txt
9 */
10struct tr2_sysenv_entry {
11 const char *env_var_name;
12 const char *git_config_name;
13
14 char *value;
15 unsigned int getenv_called : 1;
16};
17
18/*
19 * This table must match "enum tr2_sysenv_variable" in tr2_sysenv.h.
20 *
21 * The strings in this table are constant and must match the published
22 * config and environment variable names as described in the documentation.
23 *
e4b75d6a 24 * We do not define entries for the GIT_TRACE2_PARENT_* environment
bce9db6d
JH
25 * variables because they are transient and used to pass information
26 * from parent to child git processes, rather than settings.
27 */
28/* clang-format off */
29static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
e4b75d6a 30 [TR2_SYSENV_CFG_PARAM] = { "GIT_TRACE2_CONFIG_PARAMS",
bce9db6d 31 "trace2.configparams" },
3d3adaad
JS
32 [TR2_SYSENV_ENV_VARS] = { "GIT_TRACE2_ENV_VARS",
33 "trace2.envvars" },
bce9db6d 34
e4b75d6a 35 [TR2_SYSENV_DST_DEBUG] = { "GIT_TRACE2_DST_DEBUG",
bce9db6d
JH
36 "trace2.destinationdebug" },
37
e4b75d6a 38 [TR2_SYSENV_NORMAL] = { "GIT_TRACE2",
bce9db6d 39 "trace2.normaltarget" },
e4b75d6a 40 [TR2_SYSENV_NORMAL_BRIEF] = { "GIT_TRACE2_BRIEF",
bce9db6d
JH
41 "trace2.normalbrief" },
42
e4b75d6a 43 [TR2_SYSENV_EVENT] = { "GIT_TRACE2_EVENT",
bce9db6d 44 "trace2.eventtarget" },
e4b75d6a 45 [TR2_SYSENV_EVENT_BRIEF] = { "GIT_TRACE2_EVENT_BRIEF",
bce9db6d 46 "trace2.eventbrief" },
e4b75d6a 47 [TR2_SYSENV_EVENT_NESTING] = { "GIT_TRACE2_EVENT_NESTING",
bce9db6d
JH
48 "trace2.eventnesting" },
49
e4b75d6a 50 [TR2_SYSENV_PERF] = { "GIT_TRACE2_PERF",
bce9db6d 51 "trace2.perftarget" },
e4b75d6a 52 [TR2_SYSENV_PERF_BRIEF] = { "GIT_TRACE2_PERF_BRIEF",
bce9db6d 53 "trace2.perfbrief" },
83e57b04
JS
54
55 [TR2_SYSENV_MAX_FILES] = { "GIT_TRACE2_MAX_FILES",
56 "trace2.maxfiles" },
bce9db6d
JH
57};
58/* clang-format on */
59
a4e7e317
GC
60static int tr2_sysenv_cb(const char *key, const char *value,
61 const struct config_context *ctx UNUSED, void *d)
bce9db6d
JH
62{
63 int k;
64
65 if (!starts_with(key, "trace2."))
66 return 0;
67
68 for (k = 0; k < ARRAY_SIZE(tr2_sysenv_settings); k++) {
69 if (!strcmp(key, tr2_sysenv_settings[k].git_config_name)) {
70 free(tr2_sysenv_settings[k].value);
71 tr2_sysenv_settings[k].value = xstrdup(value);
72 return 0;
73 }
74 }
75
76 return 0;
77}
78
79/*
80 * Load Trace2 settings from the system config (usually "/etc/gitconfig"
81 * unless we were built with a runtime-prefix). These are intended to
82 * define the default values for Trace2 as requested by the administrator.
83 *
84 * Then override with the Trace2 settings from the global config.
85 */
86void tr2_sysenv_load(void)
87{
88 if (ARRAY_SIZE(tr2_sysenv_settings) != TR2_SYSENV_MUST_BE_LAST)
89 BUG("tr2_sysenv_settings size is wrong");
90
91 read_very_early_config(tr2_sysenv_cb, NULL);
92}
93
94/*
95 * Return the value for the requested Trace2 setting from these sources:
96 * the system config, the global config, and the environment.
97 */
98const char *tr2_sysenv_get(enum tr2_sysenv_variable var)
99{
100 if (var >= TR2_SYSENV_MUST_BE_LAST)
101 BUG("tr2_sysenv_get invalid var '%d'", var);
102
103 if (!tr2_sysenv_settings[var].getenv_called) {
104 const char *v = getenv(tr2_sysenv_settings[var].env_var_name);
105 if (v && *v) {
106 free(tr2_sysenv_settings[var].value);
107 tr2_sysenv_settings[var].value = xstrdup(v);
108 }
109 tr2_sysenv_settings[var].getenv_called = 1;
110 }
111
112 return tr2_sysenv_settings[var].value;
113}
114
115/*
116 * Return a friendly name for this setting that is suitable for printing
117 * in an error messages.
118 */
119const char *tr2_sysenv_display_name(enum tr2_sysenv_variable var)
120{
121 if (var >= TR2_SYSENV_MUST_BE_LAST)
122 BUG("tr2_sysenv_get invalid var '%d'", var);
123
124 return tr2_sysenv_settings[var].env_var_name;
125}
126
127void tr2_sysenv_release(void)
128{
129 int k;
130
131 for (k = 0; k < ARRAY_SIZE(tr2_sysenv_settings); k++)
132 free(tr2_sysenv_settings[k].value);
133}