merge-ort: initialize repo in index state
[git/gitster.git] / trace2 / tr2_cfg.c
blob78cfc15d52dd5800ea5bd610e5db7b0e73b4f8af
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "strbuf.h"
4 #include "trace2.h"
5 #include "trace2/tr2_cfg.h"
6 #include "trace2/tr2_sysenv.h"
8 static struct strbuf **tr2_cfg_patterns;
9 static int tr2_cfg_count_patterns;
10 static int tr2_cfg_loaded;
12 static struct strbuf **tr2_cfg_env_vars;
13 static int tr2_cfg_env_vars_count;
14 static int tr2_cfg_env_vars_loaded;
17 * Parse a string containing a comma-delimited list of config keys
18 * or wildcard patterns into a list of strbufs.
20 static int tr2_cfg_load_patterns(void)
22 struct strbuf **s;
23 const char *envvar;
25 if (tr2_cfg_loaded)
26 return tr2_cfg_count_patterns;
27 tr2_cfg_loaded = 1;
29 envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
30 if (!envvar || !*envvar)
31 return tr2_cfg_count_patterns;
33 tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
34 for (s = tr2_cfg_patterns; *s; s++) {
35 struct strbuf *buf = *s;
37 if (buf->len && buf->buf[buf->len - 1] == ',')
38 strbuf_setlen(buf, buf->len - 1);
39 strbuf_trim_trailing_newline(*s);
40 strbuf_trim(*s);
43 tr2_cfg_count_patterns = s - tr2_cfg_patterns;
44 return tr2_cfg_count_patterns;
47 void tr2_cfg_free_patterns(void)
49 if (tr2_cfg_patterns)
50 strbuf_list_free(tr2_cfg_patterns);
51 tr2_cfg_count_patterns = 0;
52 tr2_cfg_loaded = 0;
56 * Parse a string containing a comma-delimited list of environment variable
57 * names into a list of strbufs.
59 static int tr2_load_env_vars(void)
61 struct strbuf **s;
62 const char *varlist;
64 if (tr2_cfg_env_vars_loaded)
65 return tr2_cfg_env_vars_count;
66 tr2_cfg_env_vars_loaded = 1;
68 varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
69 if (!varlist || !*varlist)
70 return tr2_cfg_env_vars_count;
72 tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
73 for (s = tr2_cfg_env_vars; *s; s++) {
74 struct strbuf *buf = *s;
76 if (buf->len && buf->buf[buf->len - 1] == ',')
77 strbuf_setlen(buf, buf->len - 1);
78 strbuf_trim_trailing_newline(*s);
79 strbuf_trim(*s);
82 tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
83 return tr2_cfg_env_vars_count;
86 void tr2_cfg_free_env_vars(void)
88 if (tr2_cfg_env_vars)
89 strbuf_list_free(tr2_cfg_env_vars);
90 tr2_cfg_env_vars_count = 0;
91 tr2_cfg_env_vars_loaded = 0;
94 struct tr2_cfg_data {
95 const char *file;
96 int line;
100 * See if the given config key matches any of our patterns of interest.
102 static int tr2_cfg_cb(const char *key, const char *value, void *d)
104 struct strbuf **s;
105 struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
107 for (s = tr2_cfg_patterns; *s; s++) {
108 struct strbuf *buf = *s;
109 int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
110 if (wm == WM_MATCH) {
111 trace2_def_param_fl(data->file, data->line, key, value);
112 return 0;
116 return 0;
119 void tr2_cfg_list_config_fl(const char *file, int line)
121 struct tr2_cfg_data data = { file, line };
123 if (tr2_cfg_load_patterns() > 0)
124 read_early_config(tr2_cfg_cb, &data);
127 void tr2_list_env_vars_fl(const char *file, int line)
129 struct strbuf **s;
131 if (tr2_load_env_vars() <= 0)
132 return;
134 for (s = tr2_cfg_env_vars; *s; s++) {
135 struct strbuf *buf = *s;
136 const char *val = getenv(buf->buf);
137 if (val && *val)
138 trace2_def_param_fl(file, line, buf->buf, val);
142 void tr2_cfg_set_fl(const char *file, int line, const char *key,
143 const char *value)
145 struct tr2_cfg_data data = { file, line };
147 if (tr2_cfg_load_patterns() > 0)
148 tr2_cfg_cb(key, value, &data);