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
);
231 * Reload changed config files
233 * A config file may be changed on disk out from under the in-memory
234 * config object. This function causes us to look for files that have
235 * been modified since we last loaded them and refresh the config with
236 * the latest information.
238 * @param cfg The configuration to refresh
239 * @return 0 or an error code
241 GIT_EXTERN(int) git_config_refresh(git_config
*cfg
);
244 * Free the configuration and its associated memory and files
246 * @param cfg the configuration to free
248 GIT_EXTERN(void) git_config_free(git_config
*cfg
);
251 * Get the git_config_entry of a config variable.
253 * The git_config_entry is owned by the config and should not be freed by the
256 * @param out pointer to the variable git_config_entry
257 * @param cfg where to look for the variable
258 * @param name the variable's name
259 * @return 0 or an error code
261 GIT_EXTERN(int) git_config_get_entry(
262 const git_config_entry
**out
,
263 const git_config
*cfg
,
267 * Get the value of an integer config variable.
269 * All config files will be looked into, in the order of their
270 * defined level. A higher level means a higher priority. The
271 * first occurence of the variable will be returned here.
273 * @param out pointer to the variable where the value should be stored
274 * @param cfg where to look for the variable
275 * @param name the variable's name
276 * @return 0 or an error code
278 GIT_EXTERN(int) git_config_get_int32(int32_t *out
, const git_config
*cfg
, const char *name
);
281 * Get the value of a long integer config variable.
283 * All config files will be looked into, in the order of their
284 * defined level. A higher level means a higher priority. The
285 * first occurrence of the variable will be returned here.
287 * @param out pointer to the variable where the value should be stored
288 * @param cfg where to look for the variable
289 * @param name the variable's name
290 * @return 0 or an error code
292 GIT_EXTERN(int) git_config_get_int64(int64_t *out
, const git_config
*cfg
, const char *name
);
295 * Get the value of a boolean config variable.
297 * This function uses the usual C convention of 0 being false and
298 * anything else true.
300 * All config files will be looked into, in the order of their
301 * defined level. A higher level means a higher priority. The
302 * first occurrence of the variable will be returned here.
304 * @param out pointer to the variable where the value should be stored
305 * @param cfg where to look for the variable
306 * @param name the variable's name
307 * @return 0 or an error code
309 GIT_EXTERN(int) git_config_get_bool(int *out
, const git_config
*cfg
, const char *name
);
312 * Get the value of a string config variable.
314 * The string is owned by the variable and should not be freed by the
317 * All config files will be looked into, in the order of their
318 * defined level. A higher level means a higher priority. The
319 * first occurrence of the variable will be returned here.
321 * @param out pointer to the variable's value
322 * @param cfg where to look for the variable
323 * @param name the variable's name
324 * @return 0 or an error code
326 GIT_EXTERN(int) git_config_get_string(const char **out
, const git_config
*cfg
, const char *name
);
329 * Get each value of a multivar in a foreach callback
331 * The callback will be called on each variable found
333 * @param cfg where to look for the variable
334 * @param name the variable's name
335 * @param regexp regular expression to filter which variables we're
336 * interested in. Use NULL to indicate all
337 * @param callback the function to be called on each value of the variable
338 * @param payload opaque pointer to pass to the callback
340 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
);
343 * Get each value of a multivar
345 * @param out pointer to store the iterator
346 * @param cfg where to look for the variable
347 * @param name the variable's name
348 * @param regexp regular expression to filter which variables we're
349 * interested in. Use NULL to indicate all
351 GIT_EXTERN(int) git_config_multivar_iterator_new(git_config_iterator
**out
, const git_config
*cfg
, const char *name
, const char *regexp
);
354 * Return the current entry and advance the iterator
356 * @param entry pointer to store the entry
357 * @param iter the iterator
358 * @return 0 or an error code. GIT_ITEROVER if the iteration has completed
360 GIT_EXTERN(int) git_config_next(git_config_entry
**entry
, git_config_iterator
*iter
);
363 * Free a config iterator
365 * @param iter the iterator to free
367 GIT_EXTERN(void) git_config_iterator_free(git_config_iterator
*iter
);
370 * Set the value of an integer config variable in the config file
371 * with the highest level (usually the local one).
373 * @param cfg where to look for the variable
374 * @param name the variable's name
375 * @param value Integer value for the variable
376 * @return 0 or an error code
378 GIT_EXTERN(int) git_config_set_int32(git_config
*cfg
, const char *name
, int32_t value
);
381 * Set the value of a long integer config variable in the config file
382 * with the highest level (usually the local one).
384 * @param cfg where to look for the variable
385 * @param name the variable's name
386 * @param value Long integer value for the variable
387 * @return 0 or an error code
389 GIT_EXTERN(int) git_config_set_int64(git_config
*cfg
, const char *name
, int64_t value
);
392 * Set the value of a boolean config variable in the config file
393 * with the highest level (usually the local one).
395 * @param cfg where to look for the variable
396 * @param name the variable's name
397 * @param value the value to store
398 * @return 0 or an error code
400 GIT_EXTERN(int) git_config_set_bool(git_config
*cfg
, const char *name
, int value
);
403 * Set the value of a string config variable in the config file
404 * with the highest level (usually the local one).
406 * A copy of the string is made and the user is free to use it
409 * @param cfg where to look for the variable
410 * @param name the variable's name
411 * @param value the string to store.
412 * @return 0 or an error code
414 GIT_EXTERN(int) git_config_set_string(git_config
*cfg
, const char *name
, const char *value
);
417 * Set a multivar in the local config file.
419 * @param cfg where to look for the variable
420 * @param name the variable's name
421 * @param regexp a regular expression to indicate which values to replace
422 * @param value the new value.
424 GIT_EXTERN(int) git_config_set_multivar(git_config
*cfg
, const char *name
, const char *regexp
, const char *value
);
427 * Delete a config variable from the config file
428 * with the highest level (usually the local one).
430 * @param cfg the configuration
431 * @param name the variable to delete
433 GIT_EXTERN(int) git_config_delete_entry(git_config
*cfg
, const char *name
);
436 * Deletes one or several entries from a multivar in the local config file.
438 * @param cfg where to look for the variables
439 * @param name the variable's name
440 * @param regexp a regular expression to indicate which values to delete
442 * @return 0 or an error code
444 GIT_EXTERN(int) git_config_delete_multivar(git_config
*cfg
, const char *name
, const char *regexp
);
447 * Perform an operation on each config variable.
449 * The callback receives the normalized name and value of each variable
450 * in the config backend, and the data pointer passed to this function.
451 * If the callback returns a non-zero value, the function stops iterating
452 * and returns that value to the caller.
454 * @param cfg where to get the variables from
455 * @param callback the function to call on each variable
456 * @param payload the data to pass to the callback
457 * @return 0 on success, non-zero callback return value, or error code
459 GIT_EXTERN(int) git_config_foreach(
460 const git_config
*cfg
,
461 git_config_foreach_cb callback
,
465 * Iterate over all the config variables
467 * Use `git_config_next` to advance the iteration and
468 * `git_config_iterator_free` when done.
470 * @param out pointer to store the iterator
471 * @param cfg where to ge the variables from
473 GIT_EXTERN(int) git_config_iterator_new(git_config_iterator
**out
, const git_config
*cfg
);
476 * Iterate over all the config variables whose name matches a pattern
478 * Use `git_config_next` to advance the iteration and
479 * `git_config_iterator_free` when done.
481 * @param out pointer to store the iterator
482 * @param cfg where to ge the variables from
483 * @param regexp regular expression to match the names
485 GIT_EXTERN(int) git_config_iterator_glob_new(git_config_iterator
**out
, const git_config
*cfg
, const char *regexp
);
488 * Perform an operation on each config variable matching a regular expression.
490 * This behaviors like `git_config_foreach` with an additional filter of a
491 * regular expression that filters which config keys are passed to the
494 * @param cfg where to get the variables from
495 * @param regexp regular expression to match against config names
496 * @param callback the function to call on each variable
497 * @param payload the data to pass to the callback
498 * @return 0 or the return value of the callback which didn't return 0
500 GIT_EXTERN(int) git_config_foreach_match(
501 const git_config
*cfg
,
503 git_config_foreach_cb callback
,
507 * Query the value of a config variable and return it mapped to
508 * an integer constant.
510 * This is a helper method to easily map different possible values
511 * to a variable to integer constants that easily identify them.
513 * A mapping array looks as follows:
515 * git_cvar_map autocrlf_mapping[] = {
516 * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
517 * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
518 * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT},
519 * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}};
521 * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the
522 * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter.
524 * The same thing applies for any "true" value such as "true", "yes" or "1", storing
525 * the `GIT_AUTO_CRLF_TRUE` variable.
527 * Otherwise, if the value matches the string "input" (with case insensitive comparison),
528 * the given constant will be stored in `out`, and likewise for "default".
530 * If not a single match can be made to store in `out`, an error code will be
533 * @param out place to store the result of the mapping
534 * @param cfg config file to get the variables from
535 * @param name name of the config variable to lookup
536 * @param maps array of `git_cvar_map` objects specifying the possible mappings
537 * @param map_n number of mapping objects in `maps`
538 * @return 0 on success, error code otherwise
540 GIT_EXTERN(int) git_config_get_mapped(
542 const git_config
*cfg
,
544 const git_cvar_map
*maps
,
548 * Maps a string value to an integer constant
550 * @param out place to store the result of the parsing
551 * @param maps array of `git_cvar_map` objects specifying the possible mappings
552 * @param map_n number of mapping objects in `maps`
553 * @param value value to parse
555 GIT_EXTERN(int) git_config_lookup_map_value(
557 const git_cvar_map
*maps
,
562 * Parse a string value as a bool.
564 * Valid values for true are: 'true', 'yes', 'on', 1 or any
565 * number different from 0
566 * Valid values for false are: 'false', 'no', 'off', 0
568 * @param out place to store the result of the parsing
569 * @param value value to parse
571 GIT_EXTERN(int) git_config_parse_bool(int *out
, const char *value
);
574 * Parse a string value as an int32.
576 * An optional value suffix of 'k', 'm', or 'g' will
577 * cause the value to be multiplied by 1024, 1048576,
578 * or 1073741824 prior to output.
580 * @param out place to store the result of the parsing
581 * @param value value to parse
583 GIT_EXTERN(int) git_config_parse_int32(int32_t *out
, const char *value
);
586 * Parse a string value as an int64.
588 * An optional value suffix of 'k', 'm', or 'g' will
589 * cause the value to be multiplied by 1024, 1048576,
590 * or 1073741824 prior to output.
592 * @param out place to store the result of the parsing
593 * @param value value to parse
595 GIT_EXTERN(int) git_config_parse_int64(int64_t *out
, const char *value
);
599 * Perform an operation on each config variable in given config backend
600 * matching a regular expression.
602 * This behaviors like `git_config_foreach_match` except instead of all config
603 * entries it just enumerates through the given backend entry.
605 * @param backend where to get the variables from
606 * @param regexp regular expression to match against config names (can be NULL)
607 * @param callback the function to call on each variable
608 * @param payload the data to pass to the callback
610 GIT_EXTERN(int) git_config_backend_foreach_match(
611 git_config_backend
*backend
,
613 git_config_foreach_cb callback
,