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__
16 * @brief Git config management routines
17 * @defgroup git_config Git config management routines
24 * Priority level of a config file.
25 * These priority levels correspond to the natural escalation logic
26 * (from higher to lower) when searching for config entries in git.git.
28 * git_config_open_default() and git_repository_config() honor those
29 * priority levels as well.
32 /** System-wide configuration file; /etc/gitconfig on Linux systems */
33 GIT_CONFIG_LEVEL_SYSTEM
= 1,
35 /** XDG compatible configuration file; typically ~/.config/git/config */
36 GIT_CONFIG_LEVEL_XDG
= 2,
38 /** User-specific configuration file (also called Global configuration
39 * file); typically ~/.gitconfig
41 GIT_CONFIG_LEVEL_GLOBAL
= 3,
43 /** Repository specific configuration file; $WORK_DIR/.git/config on
46 GIT_CONFIG_LEVEL_LOCAL
= 4,
48 /** Application specific configuration file; freely defined by applications
50 GIT_CONFIG_LEVEL_APP
= 5,
52 /** Represents the highest level available config file (i.e. the most
53 * specific config file available that actually is loaded)
55 GIT_CONFIG_HIGHEST_LEVEL
= -1,
61 git_config_level_t level
;
64 typedef int (*git_config_foreach_cb
)(const git_config_entry
*, void *);
65 typedef struct git_config_iterator git_config_iterator
;
76 const char *str_match
;
81 * Locate the path to the global configuration file
83 * The user or global configuration file is usually
84 * located in `$HOME/.gitconfig`.
86 * This method will try to guess the full path to that
87 * file, if the file exists. The returned path
88 * may be used on any `git_config` call to load the
89 * global configuration file.
91 * This method will not guess the path to the xdg compatible
92 * config file (.config/git/config).
94 * @param out Pointer to a user-allocated git_buf in which to store the path
95 * @return 0 if a global configuration file has been found. Its path will be stored in `out`.
97 GIT_EXTERN(int) git_config_find_global(git_buf
*out
);
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 Pointer to a user-allocated git_buf in which to store the path
111 * @return 0 if a xdg compatible configuration file has been
112 * found. Its path will be stored in `out`.
114 GIT_EXTERN(int) git_config_find_xdg(git_buf
*out
);
117 * Locate the path to the system configuration file
119 * If /etc/gitconfig doesn't exist, it will look for
120 * %PROGRAMFILES%\Git\etc\gitconfig.
122 * @param out Pointer to a user-allocated git_buf in which to store the path
123 * @return 0 if a system configuration file has been
124 * found. Its path will be stored in `out`.
126 GIT_EXTERN(int) git_config_find_system(git_buf
*out
);
129 * Open the global, XDG and system configuration files
131 * Utility wrapper that finds the global, XDG and system configuration files
132 * and opens them into a single prioritized config object that can be
133 * used when accessing default config data outside a repository.
135 * @param out Pointer to store the config instance
136 * @return 0 or an error code
138 GIT_EXTERN(int) git_config_open_default(git_config
**out
);
141 * Allocate a new configuration object
143 * This object is empty, so you have to add a file to it before you
144 * can do anything with it.
146 * @param out pointer to the new configuration
147 * @return 0 or an error code
149 GIT_EXTERN(int) git_config_new(git_config
**out
);
152 * Add an on-disk config file instance to an existing config
154 * The on-disk file pointed at by `path` will be opened and
155 * parsed; it's expected to be a native Git config file following
156 * the default Git config syntax (see man git-config).
158 * Note that the configuration object will free the file
161 * Further queries on this config object will access each
162 * of the config file instances in order (instances with
163 * a higher priority level will be accessed first).
165 * @param cfg the configuration to add the file to
166 * @param path path to the configuration file to add
167 * @param level the priority level of the backend
168 * @param force replace config file at the given priority level
169 * @return 0 on success, GIT_EEXISTS when adding more than one file
170 * for a given priority level (and force_replace set to 0),
171 * GIT_ENOTFOUND when the file doesn't exist or error code
173 GIT_EXTERN(int) git_config_add_file_ondisk(
176 git_config_level_t level
,
180 * Create a new config instance containing a single on-disk file
182 * This method is a simple utility wrapper for the following sequence
185 * - git_config_add_file_ondisk
187 * @param out The configuration instance to create
188 * @param path Path to the on-disk file to open
189 * @return 0 on success, GIT_ENOTFOUND when the file doesn't exist
192 GIT_EXTERN(int) git_config_open_ondisk(git_config
**out
, const char *path
);
195 * Build a single-level focused config object from a multi-level one.
197 * The returned config object can be used to perform get/set/delete operations
198 * on a single specific level.
200 * Getting several times the same level from the same parent multi-level config
201 * will return different config instances, but containing the same config_file
204 * @param out The configuration instance to create
205 * @param parent Multi-level config to search for the given level
206 * @param level Configuration level to search for
207 * @return 0, GIT_ENOTFOUND if the passed level cannot be found in the
208 * multi-level parent config, or an error code
210 GIT_EXTERN(int) git_config_open_level(
212 const git_config
*parent
,
213 git_config_level_t level
);
216 * Open the global/XDG configuration file according to git's rules
218 * Git allows you to store your global configuration at
219 * `$HOME/.config` or `$XDG_CONFIG_HOME/git/config`. For backwards
220 * compatability, the XDG file shouldn't be used unless the use has
221 * created it explicitly. With this function you'll open the correct
224 * @param out pointer in which to store the config object
225 * @param config the config object in which to look
227 GIT_EXTERN(int) git_config_open_global(git_config
**out
, git_config
*config
);
230 * Create a snapshot of the configuration
232 * Create a snapshot of the current state of a configuration, which
233 * allows you to look into a consistent view of the configuration for
234 * looking up complex values (e.g. a remote, submodule).
236 * The string returned when querying such a config object is valid
239 * @param out pointer in which to store the snapshot config object
240 * @param config configuration to snapshot
241 * @return 0 or an error code
243 GIT_EXTERN(int) git_config_snapshot(git_config
**out
, git_config
*config
);
247 * Reload changed config files
249 * A config file may be changed on disk out from under the in-memory
250 * config object. This function causes us to look for files that have
251 * been modified since we last loaded them and refresh the config with
252 * the latest information.
254 * @param cfg The configuration to refresh
255 * @return 0 or an error code
257 GIT_EXTERN(int) git_config_refresh(git_config
*cfg
);
260 * Free the configuration and its associated memory and files
262 * @param cfg the configuration to free
264 GIT_EXTERN(void) git_config_free(git_config
*cfg
);
267 * Get the git_config_entry of a config variable.
269 * The git_config_entry is owned by the config and should not be freed by the
272 * @param out pointer to the variable git_config_entry
273 * @param cfg where to look for the variable
274 * @param name the variable's name
275 * @return 0 or an error code
277 GIT_EXTERN(int) git_config_get_entry(
278 const git_config_entry
**out
,
279 const git_config
*cfg
,
283 * Get the value of an 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 occurence 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_int32(int32_t *out
, const git_config
*cfg
, const char *name
);
297 * Get the value of a long integer config variable.
299 * All config files will be looked into, in the order of their
300 * defined level. A higher level means a higher priority. The
301 * first occurrence of the variable will be returned here.
303 * @param out pointer to the variable where the value should be stored
304 * @param cfg where to look for the variable
305 * @param name the variable's name
306 * @return 0 or an error code
308 GIT_EXTERN(int) git_config_get_int64(int64_t *out
, const git_config
*cfg
, const char *name
);
311 * Get the value of a boolean config variable.
313 * This function uses the usual C convention of 0 being false and
314 * anything else true.
316 * All config files will be looked into, in the order of their
317 * defined level. A higher level means a higher priority. The
318 * first occurrence of the variable will be returned here.
320 * @param out pointer to the variable where the value should be stored
321 * @param cfg where to look for the variable
322 * @param name the variable's name
323 * @return 0 or an error code
325 GIT_EXTERN(int) git_config_get_bool(int *out
, const git_config
*cfg
, const char *name
);
328 * Get the value of a string config variable.
330 * The string is owned by the variable and should not be freed by the
331 * user. The pointer will be valid until the next operation on this
334 * All config files will be looked into, in the order of their
335 * defined level. A higher level means a higher priority. The
336 * first occurrence of the variable will be returned here.
338 * @param out pointer to the variable's value
339 * @param cfg where to look for the variable
340 * @param name the variable's name
341 * @return 0 or an error code
343 GIT_EXTERN(int) git_config_get_string(const char **out
, const git_config
*cfg
, const char *name
);
346 * Get each value of a multivar in a foreach callback
348 * The callback will be called on each variable found
350 * @param cfg where to look for the variable
351 * @param name the variable's name
352 * @param regexp regular expression to filter which variables we're
353 * interested in. Use NULL to indicate all
354 * @param callback the function to be called on each value of the variable
355 * @param payload opaque pointer to pass to the callback
357 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
);
360 * Get each value of a multivar
362 * @param out pointer to store the iterator
363 * @param cfg where to look for the variable
364 * @param name the variable's name
365 * @param regexp regular expression to filter which variables we're
366 * interested in. Use NULL to indicate all
368 GIT_EXTERN(int) git_config_multivar_iterator_new(git_config_iterator
**out
, const git_config
*cfg
, const char *name
, const char *regexp
);
371 * Return the current entry and advance the iterator
373 * The pointers returned by this function are valid until the iterator
376 * @param entry pointer to store the entry
377 * @param iter the iterator
378 * @return 0 or an error code. GIT_ITEROVER if the iteration has completed
380 GIT_EXTERN(int) git_config_next(git_config_entry
**entry
, git_config_iterator
*iter
);
383 * Free a config iterator
385 * @param iter the iterator to free
387 GIT_EXTERN(void) git_config_iterator_free(git_config_iterator
*iter
);
390 * Set the value of an integer config variable in the config file
391 * with the highest level (usually the local one).
393 * @param cfg where to look for the variable
394 * @param name the variable's name
395 * @param value Integer value for the variable
396 * @return 0 or an error code
398 GIT_EXTERN(int) git_config_set_int32(git_config
*cfg
, const char *name
, int32_t value
);
401 * Set the value of a long integer config variable in the config file
402 * with the highest level (usually the local one).
404 * @param cfg where to look for the variable
405 * @param name the variable's name
406 * @param value Long integer value for the variable
407 * @return 0 or an error code
409 GIT_EXTERN(int) git_config_set_int64(git_config
*cfg
, const char *name
, int64_t value
);
412 * Set the value of a boolean config variable in the config file
413 * with the highest level (usually the local one).
415 * @param cfg where to look for the variable
416 * @param name the variable's name
417 * @param value the value to store
418 * @return 0 or an error code
420 GIT_EXTERN(int) git_config_set_bool(git_config
*cfg
, const char *name
, int value
);
423 * Set the value of a string config variable in the config file
424 * with the highest level (usually the local one).
426 * A copy of the string is made and the user is free to use it
429 * @param cfg where to look for the variable
430 * @param name the variable's name
431 * @param value the string to store.
432 * @return 0 or an error code
434 GIT_EXTERN(int) git_config_set_string(git_config
*cfg
, const char *name
, const char *value
);
437 * Set a multivar in the local config file.
439 * @param cfg where to look for the variable
440 * @param name the variable's name
441 * @param regexp a regular expression to indicate which values to replace
442 * @param value the new value.
444 GIT_EXTERN(int) git_config_set_multivar(git_config
*cfg
, const char *name
, const char *regexp
, const char *value
);
447 * Delete a config variable from the config file
448 * with the highest level (usually the local one).
450 * @param cfg the configuration
451 * @param name the variable to delete
453 GIT_EXTERN(int) git_config_delete_entry(git_config
*cfg
, const char *name
);
456 * Deletes one or several entries from a multivar in the local config file.
458 * @param cfg where to look for the variables
459 * @param name the variable's name
460 * @param regexp a regular expression to indicate which values to delete
462 * @return 0 or an error code
464 GIT_EXTERN(int) git_config_delete_multivar(git_config
*cfg
, const char *name
, const char *regexp
);
467 * Perform an operation on each config variable.
469 * The callback receives the normalized name and value of each variable
470 * in the config backend, and the data pointer passed to this function.
471 * If the callback returns a non-zero value, the function stops iterating
472 * and returns that value to the caller.
474 * The pointers passed to the callback are only valid as long as the
475 * iteration is ongoing.
477 * @param cfg where to get the variables from
478 * @param callback the function to call on each variable
479 * @param payload the data to pass to the callback
480 * @return 0 on success, non-zero callback return value, or error code
482 GIT_EXTERN(int) git_config_foreach(
483 const git_config
*cfg
,
484 git_config_foreach_cb callback
,
488 * Iterate over all the config variables
490 * Use `git_config_next` to advance the iteration and
491 * `git_config_iterator_free` when done.
493 * @param out pointer to store the iterator
494 * @param cfg where to ge the variables from
496 GIT_EXTERN(int) git_config_iterator_new(git_config_iterator
**out
, const git_config
*cfg
);
499 * Iterate over all the config variables whose name matches a pattern
501 * Use `git_config_next` to advance the iteration and
502 * `git_config_iterator_free` when done.
504 * @param out pointer to store the iterator
505 * @param cfg where to ge the variables from
506 * @param regexp regular expression to match the names
508 GIT_EXTERN(int) git_config_iterator_glob_new(git_config_iterator
**out
, const git_config
*cfg
, const char *regexp
);
511 * Perform an operation on each config variable matching a regular expression.
513 * This behaviors like `git_config_foreach` with an additional filter of a
514 * regular expression that filters which config keys are passed to the
517 * The pointers passed to the callback are only valid as long as the
518 * iteration is ongoing.
520 * @param cfg where to get the variables from
521 * @param regexp regular expression to match against config names
522 * @param callback the function to call on each variable
523 * @param payload the data to pass to the callback
524 * @return 0 or the return value of the callback which didn't return 0
526 GIT_EXTERN(int) git_config_foreach_match(
527 const git_config
*cfg
,
529 git_config_foreach_cb callback
,
533 * Query the value of a config variable and return it mapped to
534 * an integer constant.
536 * This is a helper method to easily map different possible values
537 * to a variable to integer constants that easily identify them.
539 * A mapping array looks as follows:
541 * git_cvar_map autocrlf_mapping[] = {
542 * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
543 * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
544 * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT},
545 * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}};
547 * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the
548 * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter.
550 * The same thing applies for any "true" value such as "true", "yes" or "1", storing
551 * the `GIT_AUTO_CRLF_TRUE` variable.
553 * Otherwise, if the value matches the string "input" (with case insensitive comparison),
554 * the given constant will be stored in `out`, and likewise for "default".
556 * If not a single match can be made to store in `out`, an error code will be
559 * @param out place to store the result of the mapping
560 * @param cfg config file to get the variables from
561 * @param name name of the config variable to lookup
562 * @param maps array of `git_cvar_map` objects specifying the possible mappings
563 * @param map_n number of mapping objects in `maps`
564 * @return 0 on success, error code otherwise
566 GIT_EXTERN(int) git_config_get_mapped(
568 const git_config
*cfg
,
570 const git_cvar_map
*maps
,
574 * Maps a string value to an integer constant
576 * @param out place to store the result of the parsing
577 * @param maps array of `git_cvar_map` objects specifying the possible mappings
578 * @param map_n number of mapping objects in `maps`
579 * @param value value to parse
581 GIT_EXTERN(int) git_config_lookup_map_value(
583 const git_cvar_map
*maps
,
588 * Parse a string value as a bool.
590 * Valid values for true are: 'true', 'yes', 'on', 1 or any
591 * number different from 0
592 * Valid values for false are: 'false', 'no', 'off', 0
594 * @param out place to store the result of the parsing
595 * @param value value to parse
597 GIT_EXTERN(int) git_config_parse_bool(int *out
, const char *value
);
600 * Parse a string value as an int32.
602 * An optional value suffix of 'k', 'm', or 'g' will
603 * cause the value to be multiplied by 1024, 1048576,
604 * or 1073741824 prior to output.
606 * @param out place to store the result of the parsing
607 * @param value value to parse
609 GIT_EXTERN(int) git_config_parse_int32(int32_t *out
, const char *value
);
612 * Parse a string value as an int64.
614 * An optional value suffix of 'k', 'm', or 'g' will
615 * cause the value to be multiplied by 1024, 1048576,
616 * or 1073741824 prior to output.
618 * @param out place to store the result of the parsing
619 * @param value value to parse
621 GIT_EXTERN(int) git_config_parse_int64(int64_t *out
, const char *value
);
625 * Perform an operation on each config variable in given config backend
626 * matching a regular expression.
628 * This behaviors like `git_config_foreach_match` except instead of all config
629 * entries it just enumerates through the given backend entry.
631 * @param backend where to get the variables from
632 * @param regexp regular expression to match against config names (can be NULL)
633 * @param callback the function to call on each variable
634 * @param payload the data to pass to the callback
636 GIT_EXTERN(int) git_config_backend_foreach_match(
637 git_config_backend
*backend
,
639 git_config_foreach_cb callback
,