factor out refresh_and_write_cache function
[git/git.git] / config.h
CommitLineData
e67a57fc
BW
1#ifndef CONFIG_H
2#define CONFIG_H
3
ef3ca954
EN
4#include "hashmap.h"
5#include "string-list.h"
6
7struct object_id;
8
e67a57fc
BW
9/* git_config_parse_key() returns these negated: */
10#define CONFIG_INVALID_KEY 1
11#define CONFIG_NO_SECTION_OR_NAME 2
12/* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */
13#define CONFIG_NO_LOCK -1
14#define CONFIG_INVALID_FILE 3
15#define CONFIG_NO_WRITE 4
16#define CONFIG_NOTHING_SET 5
17#define CONFIG_INVALID_PATTERN 6
18#define CONFIG_GENERIC_ERROR 7
19
20#define CONFIG_REGEX_NONE ((void *)1)
21
22struct git_config_source {
23 unsigned int use_stdin:1;
24 const char *file;
25 const char *blob;
26};
27
28enum config_origin_type {
29 CONFIG_ORIGIN_BLOB,
30 CONFIG_ORIGIN_FILE,
31 CONFIG_ORIGIN_STDIN,
32 CONFIG_ORIGIN_SUBMODULE_BLOB,
33 CONFIG_ORIGIN_CMDLINE
34};
35
8032cc44
JS
36enum config_event_t {
37 CONFIG_EVENT_SECTION,
38 CONFIG_EVENT_ENTRY,
39 CONFIG_EVENT_WHITESPACE,
40 CONFIG_EVENT_COMMENT,
41 CONFIG_EVENT_EOF,
42 CONFIG_EVENT_ERROR
43};
44
45/*
46 * The parser event function (if not NULL) is called with the event type and
47 * the begin/end offsets of the parsed elements.
48 *
49 * Note: for CONFIG_EVENT_ENTRY (i.e. config variables), the trailing newline
50 * character is considered part of the element.
51 */
52typedef int (*config_parser_event_fn_t)(enum config_event_t type,
53 size_t begin_offset, size_t end_offset,
54 void *event_fn_data);
55
e67a57fc
BW
56struct config_options {
57 unsigned int respect_includes : 1;
800a7f99
JH
58 unsigned int ignore_repo : 1;
59 unsigned int ignore_worktree : 1;
60 unsigned int ignore_cmdline : 1;
f672deec 61 unsigned int system_gently : 1;
a577fb5f 62 const char *commondir;
e67a57fc 63 const char *git_dir;
8032cc44
JS
64 config_parser_event_fn_t event_fn;
65 void *event_fn_data;
66f97228
JK
66 enum config_error_action {
67 CONFIG_ERROR_UNSET = 0, /* use source-specific default */
68 CONFIG_ERROR_DIE, /* die() on error */
69 CONFIG_ERROR_ERROR, /* error() on error, return -1 */
63583203 70 CONFIG_ERROR_SILENT, /* return -1 */
66f97228 71 } error_action;
e67a57fc
BW
72};
73
74typedef int (*config_fn_t)(const char *, const char *, void *);
55454427
DL
75int git_default_config(const char *, const char *, void *);
76int git_config_from_file(config_fn_t fn, const char *, void *);
77int git_config_from_file_with_options(config_fn_t fn, const char *,
ad6dad09
DL
78 void *,
79 const struct config_options *);
55454427 80int git_config_from_mem(config_fn_t fn,
ad6dad09
DL
81 const enum config_origin_type,
82 const char *name,
83 const char *buf, size_t len,
84 void *data, const struct config_options *opts);
55454427 85int git_config_from_blob_oid(config_fn_t fn, const char *name,
ad6dad09 86 const struct object_id *oid, void *data);
55454427
DL
87void git_config_push_parameter(const char *text);
88int git_config_from_parameters(config_fn_t fn, void *data);
89void read_early_config(config_fn_t cb, void *data);
4aeeef37 90void read_very_early_config(config_fn_t cb, void *data);
55454427
DL
91void git_config(config_fn_t fn, void *);
92int config_with_options(config_fn_t fn, void *,
ad6dad09
DL
93 struct git_config_source *config_source,
94 const struct config_options *opts);
55454427
DL
95int git_parse_ssize_t(const char *, ssize_t *);
96int git_parse_ulong(const char *, unsigned long *);
97int git_parse_maybe_bool(const char *);
98int git_config_int(const char *, const char *);
99int64_t git_config_int64(const char *, const char *);
100unsigned long git_config_ulong(const char *, const char *);
101ssize_t git_config_ssize_t(const char *, const char *);
102int git_config_bool_or_int(const char *, const char *, int *);
103int git_config_bool(const char *, const char *);
104int git_config_string(const char **, const char *, const char *);
105int git_config_pathname(const char **, const char *, const char *);
106int git_config_expiry_date(timestamp_t *, const char *, const char *);
107int git_config_color(char *, const char *, const char *);
108int git_config_set_in_file_gently(const char *, const char *, const char *);
109void git_config_set_in_file(const char *, const char *, const char *);
110int git_config_set_gently(const char *, const char *);
111void git_config_set(const char *, const char *);
112int git_config_parse_key(const char *, char **, int *);
113int git_config_key_is_valid(const char *key);
114int git_config_set_multivar_gently(const char *, const char *, const char *, int);
115void git_config_set_multivar(const char *, const char *, const char *, int);
116int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int);
117void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
118int git_config_rename_section(const char *, const char *);
119int git_config_rename_section_in_file(const char *, const char *, const char *);
120int git_config_copy_section(const char *, const char *);
121int git_config_copy_section_in_file(const char *, const char *, const char *);
122const char *git_etc_gitconfig(void);
123int git_env_bool(const char *, int);
124unsigned long git_env_ulong(const char *, unsigned long);
125int git_config_system(void);
126int config_error_nonbool(const char *);
e67a57fc
BW
127#if defined(__GNUC__)
128#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
129#endif
130
55454427 131int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
e67a57fc
BW
132
133enum config_scope {
134 CONFIG_SCOPE_UNKNOWN = 0,
135 CONFIG_SCOPE_SYSTEM,
136 CONFIG_SCOPE_GLOBAL,
137 CONFIG_SCOPE_REPO,
138 CONFIG_SCOPE_CMDLINE,
139};
140
55454427
DL
141enum config_scope current_config_scope(void);
142const char *current_config_origin_type(void);
143const char *current_config_name(void);
e67a57fc
BW
144
145struct config_include_data {
146 int depth;
147 config_fn_t fn;
148 void *data;
149 const struct config_options *opts;
150};
151#define CONFIG_INCLUDE_INIT { 0 }
55454427 152int git_config_include(const char *name, const char *value, void *data);
e67a57fc
BW
153
154/*
155 * Match and parse a config key of the form:
156 *
157 * section.(subsection.)?key
158 *
159 * (i.e., what gets handed to a config_fn_t). The caller provides the section;
160 * we return -1 if it does not match, 0 otherwise. The subsection and key
161 * out-parameters are filled by the function (and *subsection is NULL if it is
162 * missing).
163 *
164 * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
165 * there is no subsection at all.
166 */
55454427 167int parse_config_key(const char *var,
ad6dad09
DL
168 const char *section,
169 const char **subsection, int *subsection_len,
170 const char **key);
e67a57fc
BW
171
172struct config_set_element {
173 struct hashmap_entry ent;
174 char *key;
175 struct string_list value_list;
176};
177
178struct configset_list_item {
179 struct config_set_element *e;
180 int value_index;
181};
182
183/*
184 * the contents of the list are ordered according to their
185 * position in the config files and order of parsing the files.
186 * (i.e. key-value pair at the last position of .git/config will
187 * be at the last item of the list)
188 */
189struct configset_list {
190 struct configset_list_item *items;
191 unsigned int nr, alloc;
192};
193
194struct config_set {
195 struct hashmap config_hash;
196 int hash_initialized;
197 struct configset_list list;
198};
199
55454427
DL
200void git_configset_init(struct config_set *cs);
201int git_configset_add_file(struct config_set *cs, const char *filename);
202const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
203void git_configset_clear(struct config_set *cs);
8ad169c4
HWN
204
205/*
206 * These functions return 1 if not found, and 0 if found, leaving the found
207 * value in the 'dest' pointer.
208 */
55454427
DL
209int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
210int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest);
211int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
212int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
213int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
214int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
215int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
216int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
217int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
e67a57fc 218
3b256228
BW
219/* Functions for reading a repository's config */
220struct repository;
55454427
DL
221void repo_config(struct repository *repo, config_fn_t fn, void *data);
222int repo_config_get_value(struct repository *repo,
ad6dad09 223 const char *key, const char **value);
55454427 224const struct string_list *repo_config_get_value_multi(struct repository *repo,
ad6dad09 225 const char *key);
55454427 226int repo_config_get_string_const(struct repository *repo,
ad6dad09 227 const char *key, const char **dest);
55454427 228int repo_config_get_string(struct repository *repo,
ad6dad09 229 const char *key, char **dest);
55454427 230int repo_config_get_int(struct repository *repo,
ad6dad09 231 const char *key, int *dest);
55454427 232int repo_config_get_ulong(struct repository *repo,
ad6dad09 233 const char *key, unsigned long *dest);
55454427 234int repo_config_get_bool(struct repository *repo,
ad6dad09 235 const char *key, int *dest);
55454427 236int repo_config_get_bool_or_int(struct repository *repo,
ad6dad09 237 const char *key, int *is_bool, int *dest);
55454427 238int repo_config_get_maybe_bool(struct repository *repo,
3b256228 239 const char *key, int *dest);
55454427 240int repo_config_get_pathname(struct repository *repo,
ad6dad09 241 const char *key, const char **dest);
3b256228 242
55454427
DL
243int git_config_get_value(const char *key, const char **value);
244const struct string_list *git_config_get_value_multi(const char *key);
245void git_config_clear(void);
246int git_config_get_string_const(const char *key, const char **dest);
247int git_config_get_string(const char *key, char **dest);
248int git_config_get_int(const char *key, int *dest);
249int git_config_get_ulong(const char *key, unsigned long *dest);
250int git_config_get_bool(const char *key, int *dest);
251int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
252int git_config_get_maybe_bool(const char *key, int *dest);
253int git_config_get_pathname(const char *key, const char **dest);
254int git_config_get_index_threads(int *dest);
255int git_config_get_untracked_cache(void);
256int git_config_get_split_index(void);
257int git_config_get_max_percent_split_change(void);
258int git_config_get_fsmonitor(void);
e67a57fc
BW
259
260/* This dies if the configured or default date is in the future */
55454427 261int git_config_get_expiry(const char *key, const char **output);
e67a57fc 262
6e96cb52 263/* parse either "this many days" integer, or "5.days.ago" approxidate */
55454427 264int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
6e96cb52 265
e67a57fc
BW
266struct key_value_info {
267 const char *filename;
268 int linenr;
269 enum config_origin_type origin_type;
270 enum config_scope scope;
271};
272
b199d714 273NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
55454427 274NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
e67a57fc 275
a73b3680
NTND
276#define LOOKUP_CONFIG(mapping, var) \
277 lookup_config(mapping, ARRAY_SIZE(mapping), var)
278int lookup_config(const char **mapping, int nr_mapping, const char *var);
279
e67a57fc 280#endif /* CONFIG_H */