config: rename `git_etc_config()`
[git] / config.h
1 #ifndef CONFIG_H
2 #define CONFIG_H
3
4 #include "hashmap.h"
5 #include "string-list.h"
6
7
8 /**
9  * The config API gives callers a way to access Git configuration files
10  * (and files which have the same syntax).
11  *
12  * General Usage
13  * -------------
14  *
15  * Config files are parsed linearly, and each variable found is passed to a
16  * caller-provided callback function. The callback function is responsible
17  * for any actions to be taken on the config option, and is free to ignore
18  * some options. It is not uncommon for the configuration to be parsed
19  * several times during the run of a Git program, with different callbacks
20  * picking out different variables useful to themselves.
21  */
22
23 struct object_id;
24
25 /* git_config_parse_key() returns these negated: */
26 #define CONFIG_INVALID_KEY 1
27 #define CONFIG_NO_SECTION_OR_NAME 2
28 /* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */
29 #define CONFIG_NO_LOCK -1
30 #define CONFIG_INVALID_FILE 3
31 #define CONFIG_NO_WRITE 4
32 #define CONFIG_NOTHING_SET 5
33 #define CONFIG_INVALID_PATTERN 6
34 #define CONFIG_GENERIC_ERROR 7
35
36 #define CONFIG_REGEX_NONE ((void *)1)
37
38 enum config_scope {
39         CONFIG_SCOPE_UNKNOWN = 0,
40         CONFIG_SCOPE_SYSTEM,
41         CONFIG_SCOPE_GLOBAL,
42         CONFIG_SCOPE_LOCAL,
43         CONFIG_SCOPE_WORKTREE,
44         CONFIG_SCOPE_COMMAND,
45         CONFIG_SCOPE_SUBMODULE,
46 };
47 const char *config_scope_name(enum config_scope scope);
48
49 struct git_config_source {
50         unsigned int use_stdin:1;
51         const char *file;
52         const char *blob;
53         enum config_scope scope;
54 };
55
56 enum config_origin_type {
57         CONFIG_ORIGIN_BLOB,
58         CONFIG_ORIGIN_FILE,
59         CONFIG_ORIGIN_STDIN,
60         CONFIG_ORIGIN_SUBMODULE_BLOB,
61         CONFIG_ORIGIN_CMDLINE
62 };
63
64 enum config_event_t {
65         CONFIG_EVENT_SECTION,
66         CONFIG_EVENT_ENTRY,
67         CONFIG_EVENT_WHITESPACE,
68         CONFIG_EVENT_COMMENT,
69         CONFIG_EVENT_EOF,
70         CONFIG_EVENT_ERROR
71 };
72
73 /*
74  * The parser event function (if not NULL) is called with the event type and
75  * the begin/end offsets of the parsed elements.
76  *
77  * Note: for CONFIG_EVENT_ENTRY (i.e. config variables), the trailing newline
78  * character is considered part of the element.
79  */
80 typedef int (*config_parser_event_fn_t)(enum config_event_t type,
81                                         size_t begin_offset, size_t end_offset,
82                                         void *event_fn_data);
83
84 struct config_options {
85         unsigned int respect_includes : 1;
86         unsigned int ignore_repo : 1;
87         unsigned int ignore_worktree : 1;
88         unsigned int ignore_cmdline : 1;
89         unsigned int system_gently : 1;
90         const char *commondir;
91         const char *git_dir;
92         config_parser_event_fn_t event_fn;
93         void *event_fn_data;
94         enum config_error_action {
95                 CONFIG_ERROR_UNSET = 0, /* use source-specific default */
96                 CONFIG_ERROR_DIE, /* die() on error */
97                 CONFIG_ERROR_ERROR, /* error() on error, return -1 */
98                 CONFIG_ERROR_SILENT, /* return -1 */
99         } error_action;
100 };
101
102 /**
103  * A config callback function takes three parameters:
104  *
105  * - the name of the parsed variable. This is in canonical "flat" form: the
106  *   section, subsection, and variable segments will be separated by dots,
107  *   and the section and variable segments will be all lowercase. E.g.,
108  *   `core.ignorecase`, `diff.SomeType.textconv`.
109  *
110  * - the value of the found variable, as a string. If the variable had no
111  *   value specified, the value will be NULL (typically this means it
112  *   should be interpreted as boolean true).
113  *
114  * - a void pointer passed in by the caller of the config API; this can
115  *   contain callback-specific data
116  *
117  * A config callback should return 0 for success, or -1 if the variable
118  * could not be parsed properly.
119  */
120 typedef int (*config_fn_t)(const char *, const char *, void *);
121
122 int git_default_config(const char *, const char *, void *);
123
124 /**
125  * Read a specific file in git-config format.
126  * This function takes the same callback and data parameters as `git_config`.
127  */
128 int git_config_from_file(config_fn_t fn, const char *, void *);
129
130 int git_config_from_file_with_options(config_fn_t fn, const char *,
131                                       void *,
132                                       const struct config_options *);
133 int git_config_from_mem(config_fn_t fn,
134                         const enum config_origin_type,
135                         const char *name,
136                         const char *buf, size_t len,
137                         void *data, const struct config_options *opts);
138 int git_config_from_blob_oid(config_fn_t fn, const char *name,
139                              const struct object_id *oid, void *data);
140 void git_config_push_parameter(const char *text);
141 void git_config_push_env(const char *spec);
142 int git_config_from_parameters(config_fn_t fn, void *data);
143 void read_early_config(config_fn_t cb, void *data);
144 void read_very_early_config(config_fn_t cb, void *data);
145
146 /**
147  * Most programs will simply want to look up variables in all config files
148  * that Git knows about, using the normal precedence rules. To do this,
149  * call `git_config` with a callback function and void data pointer.
150  *
151  * `git_config` will read all config sources in order of increasing
152  * priority. Thus a callback should typically overwrite previously-seen
153  * entries with new ones (e.g., if both the user-wide `~/.gitconfig` and
154  * repo-specific `.git/config` contain `color.ui`, the config machinery
155  * will first feed the user-wide one to the callback, and then the
156  * repo-specific one; by overwriting, the higher-priority repo-specific
157  * value is left at the end).
158  */
159 void git_config(config_fn_t fn, void *);
160
161 /**
162  * Lets the caller examine config while adjusting some of the default
163  * behavior of `git_config`. It should almost never be used by "regular"
164  * Git code that is looking up configuration variables.
165  * It is intended for advanced callers like `git-config`, which are
166  * intentionally tweaking the normal config-lookup process.
167  * It takes two extra parameters:
168  *
169  * - `config_source`
170  * If this parameter is non-NULL, it specifies the source to parse for
171  * configuration, rather than looking in the usual files. See `struct
172  * git_config_source` in `config.h` for details. Regular `git_config` defaults
173  * to `NULL`.
174  *
175  * - `opts`
176  * Specify options to adjust the behavior of parsing config files. See `struct
177  * config_options` in `config.h` for details. As an example: regular `git_config`
178  * sets `opts.respect_includes` to `1` by default.
179  */
180 int config_with_options(config_fn_t fn, void *,
181                         struct git_config_source *config_source,
182                         const struct config_options *opts);
183
184 /**
185  * Value Parsing Helpers
186  * ---------------------
187  *
188  * The following helper functions aid in parsing string values
189  */
190
191 int git_parse_ssize_t(const char *, ssize_t *);
192 int git_parse_ulong(const char *, unsigned long *);
193
194 /**
195  * Same as `git_config_bool`, except that it returns -1 on error rather
196  * than dying.
197  */
198 int git_parse_maybe_bool(const char *);
199
200 /**
201  * Parse the string to an integer, including unit factors. Dies on error;
202  * otherwise, returns the parsed result.
203  */
204 int git_config_int(const char *, const char *);
205
206 int64_t git_config_int64(const char *, const char *);
207
208 /**
209  * Identical to `git_config_int`, but for unsigned longs.
210  */
211 unsigned long git_config_ulong(const char *, const char *);
212
213 ssize_t git_config_ssize_t(const char *, const char *);
214
215 /**
216  * Same as `git_config_bool`, except that integers are returned as-is, and
217  * an `is_bool` flag is unset.
218  */
219 int git_config_bool_or_int(const char *, const char *, int *);
220
221 /**
222  * Parse a string into a boolean value, respecting keywords like "true" and
223  * "false". Integer values are converted into true/false values (when they
224  * are non-zero or zero, respectively). Other values cause a die(). If
225  * parsing is successful, the return value is the result.
226  */
227 int git_config_bool(const char *, const char *);
228
229 /**
230  * Allocates and copies the value string into the `dest` parameter; if no
231  * string is given, prints an error message and returns -1.
232  */
233 int git_config_string(const char **, const char *, const char *);
234
235 /**
236  * Similar to `git_config_string`, but expands `~` or `~user` into the
237  * user's home directory when found at the beginning of the path.
238  */
239 int git_config_pathname(const char **, const char *, const char *);
240
241 int git_config_expiry_date(timestamp_t *, const char *, const char *);
242 int git_config_color(char *, const char *, const char *);
243 int git_config_set_in_file_gently(const char *, const char *, const char *);
244
245 /**
246  * write config values to a specific config file, takes a key/value pair as
247  * parameter.
248  */
249 void git_config_set_in_file(const char *, const char *, const char *);
250
251 int git_config_set_gently(const char *, const char *);
252
253 /**
254  * write config values to `.git/config`, takes a key/value pair as parameter.
255  */
256 void git_config_set(const char *, const char *);
257
258 int git_config_parse_key(const char *, char **, size_t *);
259 int git_config_key_is_valid(const char *key);
260
261 /*
262  * The following macros specify flag bits that alter the behavior
263  * of the git_config_set_multivar*() methods.
264  */
265
266 /*
267  * When CONFIG_FLAGS_MULTI_REPLACE is specified, all matching key/values
268  * are removed before a single new pair is written. If the flag is not
269  * present, then set operations replace only the first match.
270  */
271 #define CONFIG_FLAGS_MULTI_REPLACE (1 << 0)
272
273 /*
274  * When CONFIG_FLAGS_FIXED_VALUE is specified, match key/value pairs
275  * by string comparison (not regex match) to the provided value_pattern
276  * parameter.
277  */
278 #define CONFIG_FLAGS_FIXED_VALUE (1 << 1)
279
280 int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned);
281 void git_config_set_multivar(const char *, const char *, const char *, unsigned);
282 int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned);
283
284 /**
285  * takes four parameters:
286  *
287  * - the name of the file, as a string, to which key/value pairs will be written.
288  *
289  * - the name of key, as a string. This is in canonical "flat" form: the section,
290  *   subsection, and variable segments will be separated by dots, and the section
291  *   and variable segments will be all lowercase.
292  *   E.g., `core.ignorecase`, `diff.SomeType.textconv`.
293  *
294  * - the value of the variable, as a string. If value is equal to NULL, it will
295  *   remove the matching key from the config file.
296  *
297  * - the value regex, as a string. It will disregard key/value pairs where value
298  *   does not match.
299  *
300  * - a flags value with bits corresponding to the CONFIG_FLAG_* macros.
301  *
302  * It returns 0 on success.
303  */
304 void git_config_set_multivar_in_file(const char *config_filename,
305                                      const char *key,
306                                      const char *value,
307                                      const char *value_pattern,
308                                      unsigned flags);
309
310 /**
311  * rename or remove sections in the config file
312  * parameters `old_name` and `new_name`
313  * If NULL is passed through `new_name` parameter,
314  * the section will be removed from the config file.
315  */
316 int git_config_rename_section(const char *, const char *);
317
318 int git_config_rename_section_in_file(const char *, const char *, const char *);
319 int git_config_copy_section(const char *, const char *);
320 int git_config_copy_section_in_file(const char *, const char *, const char *);
321 int git_env_bool(const char *, int);
322 unsigned long git_env_ulong(const char *, unsigned long);
323 int git_config_system(void);
324 int config_error_nonbool(const char *);
325 #if defined(__GNUC__)
326 #define config_error_nonbool(s) (config_error_nonbool(s), const_error())
327 #endif
328
329 char *git_system_config(void);
330
331 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
332
333 enum config_scope current_config_scope(void);
334 const char *current_config_origin_type(void);
335 const char *current_config_name(void);
336 int current_config_line(void);
337
338 /**
339  * Include Directives
340  * ------------------
341  *
342  * By default, the config parser does not respect include directives.
343  * However, a caller can use the special `git_config_include` wrapper
344  * callback to support them. To do so, you simply wrap your "real" callback
345  * function and data pointer in a `struct config_include_data`, and pass
346  * the wrapper to the regular config-reading functions. For example:
347  *
348  * -------------------------------------------
349  * int read_file_with_include(const char *file, config_fn_t fn, void *data)
350  * {
351  * struct config_include_data inc = CONFIG_INCLUDE_INIT;
352  * inc.fn = fn;
353  * inc.data = data;
354  * return git_config_from_file(git_config_include, file, &inc);
355  * }
356  * -------------------------------------------
357  *
358  * `git_config` respects includes automatically. The lower-level
359  * `git_config_from_file` does not.
360  *
361  */
362 struct config_include_data {
363         int depth;
364         config_fn_t fn;
365         void *data;
366         const struct config_options *opts;
367 };
368 #define CONFIG_INCLUDE_INIT { 0 }
369 int git_config_include(const char *name, const char *value, void *data);
370
371 /*
372  * Match and parse a config key of the form:
373  *
374  *   section.(subsection.)?key
375  *
376  * (i.e., what gets handed to a config_fn_t). The caller provides the section;
377  * we return -1 if it does not match, 0 otherwise. The subsection and key
378  * out-parameters are filled by the function (and *subsection is NULL if it is
379  * missing).
380  *
381  * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
382  * there is no subsection at all.
383  */
384 int parse_config_key(const char *var,
385                      const char *section,
386                      const char **subsection, size_t *subsection_len,
387                      const char **key);
388
389 /**
390  * Custom Configsets
391  * -----------------
392  *
393  * A `config_set` can be used to construct an in-memory cache for
394  * config-like files that the caller specifies (i.e., files like `.gitmodules`,
395  * `~/.gitconfig` etc.). For example,
396  *
397  * ----------------------------------------
398  * struct config_set gm_config;
399  * git_configset_init(&gm_config);
400  * int b;
401  * //we add config files to the config_set
402  * git_configset_add_file(&gm_config, ".gitmodules");
403  * git_configset_add_file(&gm_config, ".gitmodules_alt");
404  *
405  * if (!git_configset_get_bool(gm_config, "submodule.frotz.ignore", &b)) {
406  * //hack hack hack
407  * }
408  *
409  * when we are done with the configset:
410  * git_configset_clear(&gm_config);
411  * ----------------------------------------
412  *
413  * Configset API provides functions for the above mentioned work flow
414  */
415
416 struct config_set_element {
417         struct hashmap_entry ent;
418         char *key;
419         struct string_list value_list;
420 };
421
422 struct configset_list_item {
423         struct config_set_element *e;
424         int value_index;
425 };
426
427 /*
428  * the contents of the list are ordered according to their
429  * position in the config files and order of parsing the files.
430  * (i.e. key-value pair at the last position of .git/config will
431  * be at the last item of the list)
432  */
433 struct configset_list {
434         struct configset_list_item *items;
435         unsigned int nr, alloc;
436 };
437
438 struct config_set {
439         struct hashmap config_hash;
440         int hash_initialized;
441         struct configset_list list;
442 };
443
444 /**
445  * Initializes the config_set `cs`.
446  */
447 void git_configset_init(struct config_set *cs);
448
449 /**
450  * Parses the file and adds the variable-value pairs to the `config_set`,
451  * dies if there is an error in parsing the file. Returns 0 on success, or
452  * -1 if the file does not exist or is inaccessible. The user has to decide
453  * if he wants to free the incomplete configset or continue using it when
454  * the function returns -1.
455  */
456 int git_configset_add_file(struct config_set *cs, const char *filename);
457
458 /**
459  * Finds and returns the value list, sorted in order of increasing priority
460  * for the configuration variable `key` and config set `cs`. When the
461  * configuration variable `key` is not found, returns NULL. The caller
462  * should not free or modify the returned pointer, as it is owned by the cache.
463  */
464 const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
465
466 /**
467  * Clears `config_set` structure, removes all saved variable-value pairs.
468  */
469 void git_configset_clear(struct config_set *cs);
470
471 /*
472  * These functions return 1 if not found, and 0 if found, leaving the found
473  * value in the 'dest' pointer.
474  */
475
476 /*
477  * Finds the highest-priority value for the configuration variable `key`
478  * and config set `cs`, stores the pointer to it in `value` and returns 0.
479  * When the configuration variable `key` is not found, returns 1 without
480  * touching `value`. The caller should not free or modify `value`, as it
481  * is owned by the cache.
482  */
483 int git_configset_get_value(struct config_set *cs, const char *key, const char **dest);
484
485 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
486 int git_configset_get_string_tmp(struct config_set *cs, const char *key, const char **dest);
487 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
488 int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
489 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
490 int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
491 int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
492 int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
493
494 /* Functions for reading a repository's config */
495 struct repository;
496 void repo_config(struct repository *repo, config_fn_t fn, void *data);
497 int repo_config_get_value(struct repository *repo,
498                           const char *key, const char **value);
499 const struct string_list *repo_config_get_value_multi(struct repository *repo,
500                                                       const char *key);
501 int repo_config_get_string(struct repository *repo,
502                            const char *key, char **dest);
503 int repo_config_get_string_tmp(struct repository *repo,
504                                const char *key, const char **dest);
505 int repo_config_get_int(struct repository *repo,
506                         const char *key, int *dest);
507 int repo_config_get_ulong(struct repository *repo,
508                           const char *key, unsigned long *dest);
509 int repo_config_get_bool(struct repository *repo,
510                          const char *key, int *dest);
511 int repo_config_get_bool_or_int(struct repository *repo,
512                                 const char *key, int *is_bool, int *dest);
513 int repo_config_get_maybe_bool(struct repository *repo,
514                                const char *key, int *dest);
515 int repo_config_get_pathname(struct repository *repo,
516                              const char *key, const char **dest);
517
518 /**
519  * Querying For Specific Variables
520  * -------------------------------
521  *
522  * For programs wanting to query for specific variables in a non-callback
523  * manner, the config API provides two functions `git_config_get_value`
524  * and `git_config_get_value_multi`. They both read values from an internal
525  * cache generated previously from reading the config files.
526  */
527
528 /**
529  * Finds the highest-priority value for the configuration variable `key`,
530  * stores the pointer to it in `value` and returns 0. When the
531  * configuration variable `key` is not found, returns 1 without touching
532  * `value`. The caller should not free or modify `value`, as it is owned
533  * by the cache.
534  */
535 int git_config_get_value(const char *key, const char **value);
536
537 /**
538  * Finds and returns the value list, sorted in order of increasing priority
539  * for the configuration variable `key`. When the configuration variable
540  * `key` is not found, returns NULL. The caller should not free or modify
541  * the returned pointer, as it is owned by the cache.
542  */
543 const struct string_list *git_config_get_value_multi(const char *key);
544
545 /**
546  * Resets and invalidates the config cache.
547  */
548 void git_config_clear(void);
549
550 /**
551  * Allocates and copies the retrieved string into the `dest` parameter for
552  * the configuration variable `key`; if NULL string is given, prints an
553  * error message and returns -1. When the configuration variable `key` is
554  * not found, returns 1 without touching `dest`.
555  */
556 int git_config_get_string(const char *key, char **dest);
557
558 /**
559  * Similar to `git_config_get_string`, but does not allocate any new
560  * memory; on success `dest` will point to memory owned by the config
561  * machinery, which could be invalidated if it is discarded and reloaded.
562  */
563 int git_config_get_string_tmp(const char *key, const char **dest);
564
565 /**
566  * Finds and parses the value to an integer for the configuration variable
567  * `key`. Dies on error; otherwise, stores the value of the parsed integer in
568  * `dest` and returns 0. When the configuration variable `key` is not found,
569  * returns 1 without touching `dest`.
570  */
571 int git_config_get_int(const char *key, int *dest);
572
573 /**
574  * Similar to `git_config_get_int` but for unsigned longs.
575  */
576 int git_config_get_ulong(const char *key, unsigned long *dest);
577
578 /**
579  * Finds and parses the value into a boolean value, for the configuration
580  * variable `key` respecting keywords like "true" and "false". Integer
581  * values are converted into true/false values (when they are non-zero or
582  * zero, respectively). Other values cause a die(). If parsing is successful,
583  * stores the value of the parsed result in `dest` and returns 0. When the
584  * configuration variable `key` is not found, returns 1 without touching
585  * `dest`.
586  */
587 int git_config_get_bool(const char *key, int *dest);
588
589 /**
590  * Similar to `git_config_get_bool`, except that integers are copied as-is,
591  * and `is_bool` flag is unset.
592  */
593 int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
594
595 /**
596  * Similar to `git_config_get_bool`, except that it returns -1 on error
597  * rather than dying.
598  */
599 int git_config_get_maybe_bool(const char *key, int *dest);
600
601 /**
602  * Similar to `git_config_get_string`, but expands `~` or `~user` into
603  * the user's home directory when found at the beginning of the path.
604  */
605 int git_config_get_pathname(const char *key, const char **dest);
606
607 int git_config_get_index_threads(int *dest);
608 int git_config_get_untracked_cache(void);
609 int git_config_get_split_index(void);
610 int git_config_get_max_percent_split_change(void);
611 int git_config_get_fsmonitor(void);
612
613 /* This dies if the configured or default date is in the future */
614 int git_config_get_expiry(const char *key, const char **output);
615
616 /* parse either "this many days" integer, or "5.days.ago" approxidate */
617 int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
618
619 struct key_value_info {
620         const char *filename;
621         int linenr;
622         enum config_origin_type origin_type;
623         enum config_scope scope;
624 };
625
626 /**
627  * First prints the error message specified by the caller in `err` and then
628  * dies printing the line number and the file name of the highest priority
629  * value for the configuration variable `key`.
630  */
631 NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
632
633 /**
634  * Helper function which formats the die error message according to the
635  * parameters entered. Used by `git_die_config()`. It can be used by callers
636  * handling `git_config_get_value_multi()` to print the correct error message
637  * for the desired value.
638  */
639 NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
640
641 #define LOOKUP_CONFIG(mapping, var) \
642         lookup_config(mapping, ARRAY_SIZE(mapping), var)
643 int lookup_config(const char **mapping, int nr_mapping, const char *var);
644
645 #endif /* CONFIG_H */