t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / trace2 / tr2_cfg.c
blob22a99a0682a4985d43cfb2bc4ae26c8954e09de4
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "strbuf.h"
6 #include "trace2.h"
7 #include "trace2/tr2_cfg.h"
8 #include "trace2/tr2_sysenv.h"
9 #include "wildmatch.h"
11 static struct strbuf **tr2_cfg_patterns;
12 static int tr2_cfg_count_patterns;
13 static int tr2_cfg_loaded;
15 static struct strbuf **tr2_cfg_env_vars;
16 static int tr2_cfg_env_vars_count;
17 static int tr2_cfg_env_vars_loaded;
20 * Parse a string containing a comma-delimited list of config keys
21 * or wildcard patterns into a list of strbufs.
23 static int tr2_cfg_load_patterns(void)
25 struct strbuf **s;
26 const char *envvar;
28 if (tr2_cfg_loaded)
29 return tr2_cfg_count_patterns;
30 tr2_cfg_loaded = 1;
32 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
33 if (!envvar || !*envvar)
34 return tr2_cfg_count_patterns;
36 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
37 for (s = tr2_cfg_patterns; *s; s++) {
38 struct strbuf *buf = *s;
40 if (buf->len && buf->buf[buf->len - 1] == ',')
41 strbuf_setlen(buf, buf->len - 1);
42 strbuf_trim_trailing_newline(*s);
43 strbuf_trim(*s);
46 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
47 return tr2_cfg_count_patterns;
50 void tr2_cfg_free_patterns(void)
52 if (tr2_cfg_patterns)
53 strbuf_list_free(tr2_cfg_patterns);
54 tr2_cfg_count_patterns = 0;
55 tr2_cfg_loaded = 0;
59 * Parse a string containing a comma-delimited list of environment variable
60 * names into a list of strbufs.
62 static int tr2_load_env_vars(void)
64 struct strbuf **s;
65 const char *varlist;
67 if (tr2_cfg_env_vars_loaded)
68 return tr2_cfg_env_vars_count;
69 tr2_cfg_env_vars_loaded = 1;
71 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
72 if (!varlist || !*varlist)
73 return tr2_cfg_env_vars_count;
75 tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
76 for (s = tr2_cfg_env_vars; *s; s++) {
77 struct strbuf *buf = *s;
79 if (buf->len && buf->buf[buf->len - 1] == ',')
80 strbuf_setlen(buf, buf->len - 1);
81 strbuf_trim_trailing_newline(*s);
82 strbuf_trim(*s);
85 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
86 return tr2_cfg_env_vars_count;
89 void tr2_cfg_free_env_vars(void)
91 if (tr2_cfg_env_vars)
92 strbuf_list_free(tr2_cfg_env_vars);
93 tr2_cfg_env_vars_count = 0;
94 tr2_cfg_env_vars_loaded = 0;
97 struct tr2_cfg_data {
98 const char *file;
99 int line;
103 * See if the given config key matches any of our patterns of interest.
105 static int tr2_cfg_cb(const char *key, const char *value,
106 const struct config_context *ctx, void *d)
108 struct strbuf **s;
109 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
111 for (s = tr2_cfg_patterns; *s; s++) {
112 struct strbuf *buf = *s;
113 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
114 if (wm == WM_MATCH) {
115 trace2_def_param_fl(data->file, data->line, key, value,
116 ctx->kvi);
117 return 0;
121 return 0;
124 void tr2_cfg_list_config_fl(const char *file, int line)
126 struct tr2_cfg_data data = { file, line };
128 if (tr2_cfg_load_patterns() > 0)
129 read_early_config(the_repository, tr2_cfg_cb, &data);
132 void tr2_list_env_vars_fl(const char *file, int line)
134 struct key_value_info kvi = KVI_INIT;
135 struct strbuf **s;
137 kvi_from_param(&kvi);
138 if (tr2_load_env_vars() <= 0)
139 return;
141 for (s = tr2_cfg_env_vars; *s; s++) {
142 struct strbuf *buf = *s;
143 const char *val = getenv(buf->buf);
144 if (val && *val)
145 trace2_def_param_fl(file, line, buf->buf, val, &kvi);
149 void tr2_cfg_set_fl(const char *file, int line, const char *key,
150 const char *value)
152 struct key_value_info kvi = KVI_INIT;
153 struct config_context ctx = {
154 .kvi = &kvi,
156 struct tr2_cfg_data data = { file, line };
158 if (tr2_cfg_load_patterns() > 0)
159 tr2_cfg_cb(key, value, &ctx, &data);