Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 1 | #ifndef CONFIG_H |
| 2 | #define CONFIG_H |
| 3 | |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 4 | #include "hashmap.h" |
| 5 | #include "string-list.h" |
| 6 | |
| 7 | struct object_id; |
| 8 | |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 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 | |
| 22 | struct git_config_source { |
| 23 | unsigned int use_stdin:1; |
| 24 | const char *file; |
| 25 | const char *blob; |
| 26 | }; |
| 27 | |
| 28 | enum 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 | |
Johannes Schindelin | 8032cc4 | 2018-04-09 10:32:05 +0200 | [diff] [blame] | 36 | enum 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 | */ |
| 52 | typedef 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 | |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 56 | struct config_options { |
| 57 | unsigned int respect_includes : 1; |
Brandon Williams | a577fb5 | 2017-06-14 11:07:38 -0700 | [diff] [blame] | 58 | const char *commondir; |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 59 | const char *git_dir; |
Johannes Schindelin | 8032cc4 | 2018-04-09 10:32:05 +0200 | [diff] [blame] | 60 | config_parser_event_fn_t event_fn; |
| 61 | void *event_fn_data; |
Jeff King | 66f9722 | 2018-06-28 18:05:00 -0400 | [diff] [blame] | 62 | enum config_error_action { |
| 63 | CONFIG_ERROR_UNSET = 0, /* use source-specific default */ |
| 64 | CONFIG_ERROR_DIE, /* die() on error */ |
| 65 | CONFIG_ERROR_ERROR, /* error() on error, return -1 */ |
Jeff King | 6358320 | 2018-06-28 18:05:09 -0400 | [diff] [blame] | 66 | CONFIG_ERROR_SILENT, /* return -1 */ |
Jeff King | 66f9722 | 2018-06-28 18:05:00 -0400 | [diff] [blame] | 67 | } error_action; |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | typedef int (*config_fn_t)(const char *, const char *, void *); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 71 | int git_default_config(const char *, const char *, void *); |
| 72 | int git_config_from_file(config_fn_t fn, const char *, void *); |
| 73 | int git_config_from_file_with_options(config_fn_t fn, const char *, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 74 | void *, |
| 75 | const struct config_options *); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 76 | int git_config_from_mem(config_fn_t fn, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 77 | const enum config_origin_type, |
| 78 | const char *name, |
| 79 | const char *buf, size_t len, |
| 80 | void *data, const struct config_options *opts); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 81 | int git_config_from_blob_oid(config_fn_t fn, const char *name, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 82 | const struct object_id *oid, void *data); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 83 | void git_config_push_parameter(const char *text); |
| 84 | int git_config_from_parameters(config_fn_t fn, void *data); |
| 85 | void read_early_config(config_fn_t cb, void *data); |
| 86 | void git_config(config_fn_t fn, void *); |
| 87 | int config_with_options(config_fn_t fn, void *, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 88 | struct git_config_source *config_source, |
| 89 | const struct config_options *opts); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 90 | int git_parse_ssize_t(const char *, ssize_t *); |
| 91 | int git_parse_ulong(const char *, unsigned long *); |
| 92 | int git_parse_maybe_bool(const char *); |
| 93 | int git_config_int(const char *, const char *); |
| 94 | int64_t git_config_int64(const char *, const char *); |
| 95 | unsigned long git_config_ulong(const char *, const char *); |
| 96 | ssize_t git_config_ssize_t(const char *, const char *); |
| 97 | int git_config_bool_or_int(const char *, const char *, int *); |
| 98 | int git_config_bool(const char *, const char *); |
| 99 | int git_config_string(const char **, const char *, const char *); |
| 100 | int git_config_pathname(const char **, const char *, const char *); |
| 101 | int git_config_expiry_date(timestamp_t *, const char *, const char *); |
| 102 | int git_config_color(char *, const char *, const char *); |
| 103 | int git_config_set_in_file_gently(const char *, const char *, const char *); |
| 104 | void git_config_set_in_file(const char *, const char *, const char *); |
| 105 | int git_config_set_gently(const char *, const char *); |
| 106 | void git_config_set(const char *, const char *); |
| 107 | int git_config_parse_key(const char *, char **, int *); |
| 108 | int git_config_key_is_valid(const char *key); |
| 109 | int git_config_set_multivar_gently(const char *, const char *, const char *, int); |
| 110 | void git_config_set_multivar(const char *, const char *, const char *, int); |
| 111 | int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int); |
| 112 | void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int); |
| 113 | int git_config_rename_section(const char *, const char *); |
| 114 | int git_config_rename_section_in_file(const char *, const char *, const char *); |
| 115 | int git_config_copy_section(const char *, const char *); |
| 116 | int git_config_copy_section_in_file(const char *, const char *, const char *); |
| 117 | const char *git_etc_gitconfig(void); |
| 118 | int git_env_bool(const char *, int); |
| 119 | unsigned long git_env_ulong(const char *, unsigned long); |
| 120 | int git_config_system(void); |
| 121 | int config_error_nonbool(const char *); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 122 | #if defined(__GNUC__) |
| 123 | #define config_error_nonbool(s) (config_error_nonbool(s), const_error()) |
| 124 | #endif |
| 125 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 126 | int git_config_parse_parameter(const char *, config_fn_t fn, void *data); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 127 | |
| 128 | enum config_scope { |
| 129 | CONFIG_SCOPE_UNKNOWN = 0, |
| 130 | CONFIG_SCOPE_SYSTEM, |
| 131 | CONFIG_SCOPE_GLOBAL, |
| 132 | CONFIG_SCOPE_REPO, |
| 133 | CONFIG_SCOPE_CMDLINE, |
| 134 | }; |
| 135 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 136 | enum config_scope current_config_scope(void); |
| 137 | const char *current_config_origin_type(void); |
| 138 | const char *current_config_name(void); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 139 | |
| 140 | struct config_include_data { |
| 141 | int depth; |
| 142 | config_fn_t fn; |
| 143 | void *data; |
| 144 | const struct config_options *opts; |
| 145 | }; |
| 146 | #define CONFIG_INCLUDE_INIT { 0 } |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 147 | int git_config_include(const char *name, const char *value, void *data); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Match and parse a config key of the form: |
| 151 | * |
| 152 | * section.(subsection.)?key |
| 153 | * |
| 154 | * (i.e., what gets handed to a config_fn_t). The caller provides the section; |
| 155 | * we return -1 if it does not match, 0 otherwise. The subsection and key |
| 156 | * out-parameters are filled by the function (and *subsection is NULL if it is |
| 157 | * missing). |
| 158 | * |
| 159 | * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if |
| 160 | * there is no subsection at all. |
| 161 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 162 | int parse_config_key(const char *var, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 163 | const char *section, |
| 164 | const char **subsection, int *subsection_len, |
| 165 | const char **key); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 166 | |
| 167 | struct config_set_element { |
| 168 | struct hashmap_entry ent; |
| 169 | char *key; |
| 170 | struct string_list value_list; |
| 171 | }; |
| 172 | |
| 173 | struct configset_list_item { |
| 174 | struct config_set_element *e; |
| 175 | int value_index; |
| 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * the contents of the list are ordered according to their |
| 180 | * position in the config files and order of parsing the files. |
| 181 | * (i.e. key-value pair at the last position of .git/config will |
| 182 | * be at the last item of the list) |
| 183 | */ |
| 184 | struct configset_list { |
| 185 | struct configset_list_item *items; |
| 186 | unsigned int nr, alloc; |
| 187 | }; |
| 188 | |
| 189 | struct config_set { |
| 190 | struct hashmap config_hash; |
| 191 | int hash_initialized; |
| 192 | struct configset_list list; |
| 193 | }; |
| 194 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 195 | void git_configset_init(struct config_set *cs); |
| 196 | int git_configset_add_file(struct config_set *cs, const char *filename); |
| 197 | const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key); |
| 198 | void git_configset_clear(struct config_set *cs); |
Han-Wen Nienhuys | 8ad169c | 2018-08-06 16:33:12 +0200 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * These functions return 1 if not found, and 0 if found, leaving the found |
| 202 | * value in the 'dest' pointer. |
| 203 | */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 204 | int git_configset_get_value(struct config_set *cs, const char *key, const char **dest); |
| 205 | int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest); |
| 206 | int git_configset_get_string(struct config_set *cs, const char *key, char **dest); |
| 207 | int git_configset_get_int(struct config_set *cs, const char *key, int *dest); |
| 208 | int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest); |
| 209 | int git_configset_get_bool(struct config_set *cs, const char *key, int *dest); |
| 210 | int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest); |
| 211 | int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest); |
| 212 | int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 213 | |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 214 | /* Functions for reading a repository's config */ |
| 215 | struct repository; |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 216 | void repo_config(struct repository *repo, config_fn_t fn, void *data); |
| 217 | int repo_config_get_value(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 218 | const char *key, const char **value); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 219 | const struct string_list *repo_config_get_value_multi(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 220 | const char *key); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 221 | int repo_config_get_string_const(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 222 | const char *key, const char **dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 223 | int repo_config_get_string(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 224 | const char *key, char **dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 225 | int repo_config_get_int(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 226 | const char *key, int *dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 227 | int repo_config_get_ulong(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 228 | const char *key, unsigned long *dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 229 | int repo_config_get_bool(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 230 | const char *key, int *dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 231 | int repo_config_get_bool_or_int(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 232 | const char *key, int *is_bool, int *dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 233 | int repo_config_get_maybe_bool(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 234 | const char *key, int *dest); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 235 | int repo_config_get_pathname(struct repository *repo, |
Denton Liu | ad6dad0 | 2019-04-29 04:28:23 -0400 | [diff] [blame^] | 236 | const char *key, const char **dest); |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 237 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 238 | int git_config_get_value(const char *key, const char **value); |
| 239 | const struct string_list *git_config_get_value_multi(const char *key); |
| 240 | void git_config_clear(void); |
| 241 | int git_config_get_string_const(const char *key, const char **dest); |
| 242 | int git_config_get_string(const char *key, char **dest); |
| 243 | int git_config_get_int(const char *key, int *dest); |
| 244 | int git_config_get_ulong(const char *key, unsigned long *dest); |
| 245 | int git_config_get_bool(const char *key, int *dest); |
| 246 | int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest); |
| 247 | int git_config_get_maybe_bool(const char *key, int *dest); |
| 248 | int git_config_get_pathname(const char *key, const char **dest); |
| 249 | int git_config_get_index_threads(int *dest); |
| 250 | int git_config_get_untracked_cache(void); |
| 251 | int git_config_get_split_index(void); |
| 252 | int git_config_get_max_percent_split_change(void); |
| 253 | int git_config_get_fsmonitor(void); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 254 | |
| 255 | /* This dies if the configured or default date is in the future */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 256 | int git_config_get_expiry(const char *key, const char **output); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 257 | |
Junio C Hamano | 6e96cb5 | 2017-08-19 11:43:39 -0700 | [diff] [blame] | 258 | /* parse either "this many days" integer, or "5.days.ago" approxidate */ |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 259 | int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now); |
Junio C Hamano | 6e96cb5 | 2017-08-19 11:43:39 -0700 | [diff] [blame] | 260 | |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 261 | struct key_value_info { |
| 262 | const char *filename; |
| 263 | int linenr; |
| 264 | enum config_origin_type origin_type; |
| 265 | enum config_scope scope; |
| 266 | }; |
| 267 | |
Denton Liu | b199d71 | 2019-04-29 04:28:20 -0400 | [diff] [blame] | 268 | NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3))); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 269 | NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr); |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 270 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 271 | #define LOOKUP_CONFIG(mapping, var) \ |
| 272 | lookup_config(mapping, ARRAY_SIZE(mapping), var) |
| 273 | int lookup_config(const char **mapping, int nr_mapping, const char *var); |
| 274 | |
Brandon Williams | e67a57f | 2017-06-14 11:07:34 -0700 | [diff] [blame] | 275 | #endif /* CONFIG_H */ |