2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_git_config_h__
8 #define INCLUDE_git_config_h__
15 * @brief Git config management routines
16 * @defgroup git_config Git config management routines
23 * Priority level of a config file.
24 * These priority levels correspond to the natural escalation logic
25 * (from higher to lower) when searching for config entries in git.git.
27 * git_config_open_default() and git_repository_config() honor those
28 * priority levels as well.
31 /** System-wide configuration file; /etc/gitconfig on Linux systems */
32 GIT_CONFIG_LEVEL_SYSTEM
= 1,
34 /** XDG compatible configuration file; typically ~/.config/git/config */
35 GIT_CONFIG_LEVEL_XDG
= 2,
37 /** User-specific configuration file (also called Global configuration
38 * file); typically ~/.gitconfig
40 GIT_CONFIG_LEVEL_GLOBAL
= 3,
42 /** Repository specific configuration file; $WORK_DIR/.git/config on
45 GIT_CONFIG_LEVEL_LOCAL
= 4,
47 /** Application specific configuration file; freely defined by applications
49 GIT_CONFIG_LEVEL_APP
= 5,
51 /** Represents the highest level available config file (i.e. the most
52 * specific config file available that actually is loaded)
54 GIT_CONFIG_HIGHEST_LEVEL
= -1,
60 git_config_level_t level
;
63 typedef int (*git_config_foreach_cb
)(const git_config_entry
*, void *);
64 typedef struct git_config_iterator git_config_iterator
;
75 const char *str_match
;
80 * Locate the path to the global configuration file
82 * The user or global configuration file is usually
83 * located in `$HOME/.gitconfig`.
85 * This method will try to guess the full path to that
86 * file, if the file exists. The returned path
87 * may be used on any `git_config` call to load the
88 * global configuration file.
90 * This method will not guess the path to the xdg compatible
91 * config file (.config/git/config).
93 * @param out Buffer to store the path in
94 * @param length size of the buffer in bytes
95 * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`.
97 GIT_EXTERN(int) git_config_find_global(char *out
, size_t length
);
100 * Locate the path to the global xdg compatible configuration file
102 * The xdg compatible configuration file is usually
103 * located in `$HOME/.config/git/config`.
105 * This method will try to guess the full path to that
106 * file, if the file exists. The returned path
107 * may be used on any `git_config` call to load the
108 * xdg compatible configuration file.
110 * @param out Buffer to store the path in
111 * @param length size of the buffer in bytes
112 * @return 0 if a xdg compatible configuration file has been
113 * found. Its path will be stored in `buffer`.
115 GIT_EXTERN(int) git_config_find_xdg(char *out
, size_t length
);
118 * Locate the path to the system configuration file
120 * If /etc/gitconfig doesn't exist, it will look for
121 * %PROGRAMFILES%\Git\etc\gitconfig.
123 * @param out Buffer to store the path in
124 * @param length size of the buffer in bytes
125 * @return 0 if a system configuration file has been
126 * found. Its path will be stored in `buffer`.
128 GIT_EXTERN(int) git_config_find_system(char *out
, size_t length
);
131 * Open the global, XDG and system configuration files
133 * Utility wrapper that finds the global, XDG and system configuration files
134 * and opens them into a single prioritized config object that can be
135 * used when accessing default config data outside a repository.
137 * @param out Pointer to store the config instance
138 * @return 0 or an error code
140 GIT_EXTERN(int) git_config_open_default(git_config
**out
);
143 * Allocate a new configuration object
145 * This object is empty, so you have to add a file to it before you
146 * can do anything with it.
148 * @param out pointer to the new configuration
149 * @return 0 or an error code
151 GIT_EXTERN(int) git_config_new(git_config
**out
);
154 * Add an on-disk config file instance to an existing config
156 * The on-disk file pointed at by `path` will be opened and
157 * parsed; it's expected to be a native Git config file following
158 * the default Git config syntax (see man git-config).
160 * Note that the configuration object will free the file
163 * Further queries on this config object will access each
164 * of the config file instances in order (instances with
165 * a higher priority level will be accessed first).
167 * @param cfg the configuration to add the file to
168 * @param path path to the configuration file to add
169 * @param level the priority level of the backend
170 * @param force replace config file at the given priority level
171 * @return 0 on success, GIT_EEXISTS when adding more than one file
172 * for a given priority level (and force_replace set to 0),
173 * GIT_ENOTFOUND when the file doesn't exist or error code
175 GIT_EXTERN(int) git_config_add_file_ondisk(
178 git_config_level_t level
,
182 * Create a new config instance containing a single on-disk file
184 * This method is a simple utility wrapper for the following sequence
187 * - git_config_add_file_ondisk
189 * @param out The configuration instance to create
190 * @param path Path to the on-disk file to open
191 * @return 0 on success, GIT_ENOTFOUND when the file doesn't exist
194 GIT_EXTERN(int) git_config_open_ondisk(git_config
**out
, const char *path
);
197 * Build a single-level focused config object from a multi-level one.
199 * The returned config object can be used to perform get/set/delete operations
200 * on a single specific level.
202 * Getting several times the same level from the same parent multi-level config
203 * will return different config instances, but containing the same config_file
206 * @param out The configuration instance to create
207 * @param parent Multi-level config to search for the given level
208 * @param level Configuration level to search for
209 * @return 0, GIT_ENOTFOUND if the passed level cannot be found in the
210 * multi-level parent config, or an error code
212 GIT_EXTERN(int) git_config_open_level(
214 const git_config
*parent
,
215 git_config_level_t level
);
218 * Open the global/XDG configuration file according to git's rules
220 * Git allows you to store your global configuration at
221 * `$HOME/.config` or `$XDG_CONFIG_HOME/git/config`. For backwards
222 * compatability, the XDG file shouldn't be used unless the use has
223 * created it explicitly. With this function you'll open the correct
226 * @param out pointer in which to store the config object
227 * @param config the config object in which to look
229 GIT_EXTERN(int) git_config_open_global(git_config
**out
, git_config
*config
);
233 * Reload changed config files
235 * A config file may be changed on disk out from under the in-memory
236 * config object. This function causes us to look for files that have
237 * been modified since we last loaded them and refresh the config with
238 * the latest information.
240 * @param cfg The configuration to refresh
241 * @return 0 or an error code
243 GIT_EXTERN(int) git_config_refresh(git_config
*cfg
);
246 * Free the configuration and its associated memory and files
248 * @param cfg the configuration to free
250 GIT_EXTERN(void) git_config_free(git_config
*cfg
);
253 * Get the git_config_entry of a config variable.
255 * The git_config_entry is owned by the config and should not be freed by the
258 * @param out pointer to the variable git_config_entry
259 * @param cfg where to look for the variable
260 * @param name the variable's name
261 * @return 0 or an error code
263 GIT_EXTERN(int) git_config_get_entry(
264 const git_config_entry
**out
,
265 const git_config
*cfg
,
269 * Get the value of an integer config variable.
271 * All config files will be looked into, in the order of their
272 * defined level. A higher level means a higher priority. The
273 * first occurence of the variable will be returned here.
275 * @param out pointer to the variable where the value should be stored
276 * @param cfg where to look for the variable
277 * @param name the variable's name
278 * @return 0 or an error code
280 GIT_EXTERN(int) git_config_get_int32(int32_t *out
, const git_config
*cfg
, const char *name
);
283 * Get the value of a long integer config variable.
285 * All config files will be looked into, in the order of their
286 * defined level. A higher level means a higher priority. The
287 * first occurrence of the variable will be returned here.
289 * @param out pointer to the variable where the value should be stored
290 * @param cfg where to look for the variable
291 * @param name the variable's name
292 * @return 0 or an error code
294 GIT_EXTERN(int) git_config_get_int64(int64_t *out
, const git_config
*cfg
, const char *name
);
297 * Get the value of a boolean config variable.
299 * This function uses the usual C convention of 0 being false and
300 * anything else true.
302 * All config files will be looked into, in the order of their
303 * defined level. A higher level means a higher priority. The
304 * first occurrence of the variable will be returned here.
306 * @param out pointer to the variable where the value should be stored
307 * @param cfg where to look for the variable
308 * @param name the variable's name
309 * @return 0 or an error code
311 GIT_EXTERN(int) git_config_get_bool(int *out
, const git_config
*cfg
, const char *name
);
314 * Get the value of a string config variable.
316 * The string is owned by the variable and should not be freed by the
319 * All config files will be looked into, in the order of their
320 * defined level. A higher level means a higher priority. The
321 * first occurrence of the variable will be returned here.
323 * @param out pointer to the variable's value
324 * @param cfg where to look for the variable
325 * @param name the variable's name
326 * @return 0 or an error code
328 GIT_EXTERN(int) git_config_get_string(const char **out
, const git_config
*cfg
, const char *name
);
331 * Get each value of a multivar in a foreach callback
333 * The callback will be called on each variable found
335 * @param cfg where to look for the variable
336 * @param name the variable's name
337 * @param regexp regular expression to filter which variables we're
338 * interested in. Use NULL to indicate all
339 * @param callback the function to be called on each value of the variable
340 * @param payload opaque pointer to pass to the callback
342 GIT_EXTERN(int) git_config_get_multivar_foreach(const git_config
*cfg
, const char *name
, const char *regexp
, git_config_foreach_cb callback
, void *payload
);
345 * Get each value of a multivar
347 * @param out pointer to store the iterator
348 * @param cfg where to look for the variable
349 * @param name the variable's name
350 * @param regexp regular expression to filter which variables we're
351 * interested in. Use NULL to indicate all
353 GIT_EXTERN(int) git_config_get_multivar(git_config_iterator
**out
, const git_config
*cfg
, const char *name
, const char *regexp
);
356 * Return the current entry and advance the iterator
358 * @param entry pointer to store the entry
359 * @param iter the iterator
360 * @return 0 or an error code. GIT_ITEROVER if the iteration has completed
362 GIT_EXTERN(int) git_config_next(git_config_entry
**entry
, git_config_iterator
*iter
);
365 * Free a config iterator
367 * @param iter the iterator to free
369 GIT_EXTERN(void) git_config_iterator_free(git_config_iterator
*iter
);
372 * Set the value of an integer config variable in the config file
373 * with the highest level (usually the local one).
375 * @param cfg where to look for the variable
376 * @param name the variable's name
377 * @param value Integer value for the variable
378 * @return 0 or an error code
380 GIT_EXTERN(int) git_config_set_int32(git_config
*cfg
, const char *name
, int32_t value
);
383 * Set the value of a long integer config variable in the config file
384 * with the highest level (usually the local one).
386 * @param cfg where to look for the variable
387 * @param name the variable's name
388 * @param value Long integer value for the variable
389 * @return 0 or an error code
391 GIT_EXTERN(int) git_config_set_int64(git_config
*cfg
, const char *name
, int64_t value
);
394 * Set the value of a boolean config variable in the config file
395 * with the highest level (usually the local one).
397 * @param cfg where to look for the variable
398 * @param name the variable's name
399 * @param value the value to store
400 * @return 0 or an error code
402 GIT_EXTERN(int) git_config_set_bool(git_config
*cfg
, const char *name
, int value
);
405 * Set the value of a string config variable in the config file
406 * with the highest level (usually the local one).
408 * A copy of the string is made and the user is free to use it
411 * @param cfg where to look for the variable
412 * @param name the variable's name
413 * @param value the string to store.
414 * @return 0 or an error code
416 GIT_EXTERN(int) git_config_set_string(git_config
*cfg
, const char *name
, const char *value
);
419 * Set a multivar in the local config file.
421 * @param cfg where to look for the variable
422 * @param name the variable's name
423 * @param regexp a regular expression to indicate which values to replace
424 * @param value the new value.
426 GIT_EXTERN(int) git_config_set_multivar(git_config
*cfg
, const char *name
, const char *regexp
, const char *value
);
429 * Delete a config variable from the config file
430 * with the highest level (usually the local one).
432 * @param cfg the configuration
433 * @param name the variable to delete
435 GIT_EXTERN(int) git_config_delete_entry(git_config
*cfg
, const char *name
);
438 * Perform an operation on each config variable.
440 * The callback receives the normalized name and value of each variable
441 * in the config backend, and the data pointer passed to this function.
442 * As soon as one of the callback functions returns something other than 0,
443 * this function stops iterating and returns `GIT_EUSER`.
445 * @param cfg where to get the variables from
446 * @param callback the function to call on each variable
447 * @param payload the data to pass to the callback
448 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
450 GIT_EXTERN(int) git_config_foreach(
451 const git_config
*cfg
,
452 git_config_foreach_cb callback
,
456 * Iterate over all the config variables
458 * Use `git_config_next` to advance the iteration and
459 * `git_config_iterator_free` when done.
461 * @param out pointer to store the iterator
462 * @param cfg where to ge the variables from
464 GIT_EXTERN(int) git_config_iterator_new(git_config_iterator
**out
, const git_config
*cfg
);
467 * Perform an operation on each config variable matching a regular expression.
469 * This behaviors like `git_config_foreach` with an additional filter of a
470 * regular expression that filters which config keys are passed to the
473 * @param cfg where to get the variables from
474 * @param regexp regular expression to match against config names
475 * @param callback the function to call on each variable
476 * @param payload the data to pass to the callback
477 * @return 0 or the return value of the callback which didn't return 0
479 GIT_EXTERN(int) git_config_foreach_match(
480 const git_config
*cfg
,
482 git_config_foreach_cb callback
,
486 * Query the value of a config variable and return it mapped to
487 * an integer constant.
489 * This is a helper method to easily map different possible values
490 * to a variable to integer constants that easily identify them.
492 * A mapping array looks as follows:
494 * git_cvar_map autocrlf_mapping[] = {
495 * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
496 * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
497 * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT},
498 * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}};
500 * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the
501 * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter.
503 * The same thing applies for any "true" value such as "true", "yes" or "1", storing
504 * the `GIT_AUTO_CRLF_TRUE` variable.
506 * Otherwise, if the value matches the string "input" (with case insensitive comparison),
507 * the given constant will be stored in `out`, and likewise for "default".
509 * If not a single match can be made to store in `out`, an error code will be
512 * @param out place to store the result of the mapping
513 * @param cfg config file to get the variables from
514 * @param name name of the config variable to lookup
515 * @param maps array of `git_cvar_map` objects specifying the possible mappings
516 * @param map_n number of mapping objects in `maps`
517 * @return 0 on success, error code otherwise
519 GIT_EXTERN(int) git_config_get_mapped(
521 const git_config
*cfg
,
523 const git_cvar_map
*maps
,
527 * Maps a string value to an integer constant
529 * @param out place to store the result of the parsing
530 * @param maps array of `git_cvar_map` objects specifying the possible mappings
531 * @param map_n number of mapping objects in `maps`
532 * @param value value to parse
534 GIT_EXTERN(int) git_config_lookup_map_value(
536 const git_cvar_map
*maps
,
541 * Parse a string value as a bool.
543 * Valid values for true are: 'true', 'yes', 'on', 1 or any
544 * number different from 0
545 * Valid values for false are: 'false', 'no', 'off', 0
547 * @param out place to store the result of the parsing
548 * @param value value to parse
550 GIT_EXTERN(int) git_config_parse_bool(int *out
, const char *value
);
553 * Parse a string value as an int32.
555 * An optional value suffix of 'k', 'm', or 'g' will
556 * cause the value to be multiplied by 1024, 1048576,
557 * or 1073741824 prior to output.
559 * @param out place to store the result of the parsing
560 * @param value value to parse
562 GIT_EXTERN(int) git_config_parse_int32(int32_t *out
, const char *value
);
565 * Parse a string value as an int64.
567 * An optional value suffix of 'k', 'm', or 'g' will
568 * cause the value to be multiplied by 1024, 1048576,
569 * or 1073741824 prior to output.
571 * @param out place to store the result of the parsing
572 * @param value value to parse
574 GIT_EXTERN(int) git_config_parse_int64(int64_t *out
, const char *value
);
578 * Perform an operation on each config variable in given config backend
579 * matching a regular expression.
581 * This behaviors like `git_config_foreach_match` except instead of all config
582 * entries it just enumerates through the given backend entry.
584 * @param backend where to get the variables from
585 * @param regexp regular expression to match against config names (can be NULL)
586 * @param callback the function to call on each variable
587 * @param payload the data to pass to the callback
589 GIT_EXTERN(int) git_config_backend_foreach_match(
590 git_config_backend
*backend
,
592 int (*fn
)(const git_config_entry
*, void *),