]> git.proxmox.com Git - libgit2.git/blob - include/git2/config.h
Merge branch 'development'
[libgit2.git] / include / git2 / config.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7 #ifndef INCLUDE_git_config_h__
8 #define INCLUDE_git_config_h__
9
10 #include "common.h"
11 #include "types.h"
12
13 /**
14 * @file git2/config.h
15 * @brief Git config management routines
16 * @defgroup git_config Git config management routines
17 * @ingroup Git
18 * @{
19 */
20 GIT_BEGIN_DECL
21
22 /**
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.
26 *
27 * git_config_open_default() and git_repository_config() honor those
28 * priority levels as well.
29 */
30 typedef enum {
31 /** System-wide configuration file; /etc/gitconfig on Linux systems */
32 GIT_CONFIG_LEVEL_SYSTEM = 1,
33
34 /** XDG compatible configuration file; typically ~/.config/git/config */
35 GIT_CONFIG_LEVEL_XDG = 2,
36
37 /** User-specific configuration file (also called Global configuration
38 * file); typically ~/.gitconfig
39 */
40 GIT_CONFIG_LEVEL_GLOBAL = 3,
41
42 /** Repository specific configuration file; $WORK_DIR/.git/config on
43 * non-bare repos
44 */
45 GIT_CONFIG_LEVEL_LOCAL = 4,
46
47 /** Application specific configuration file; freely defined by applications
48 */
49 GIT_CONFIG_LEVEL_APP = 5,
50
51 /** Represents the highest level available config file (i.e. the most
52 * specific config file available that actually is loaded)
53 */
54 GIT_CONFIG_HIGHEST_LEVEL = -1,
55 } git_config_level_t;
56
57 typedef struct {
58 const char *name;
59 const char *value;
60 git_config_level_t level;
61 } git_config_entry;
62
63 typedef int (*git_config_foreach_cb)(const git_config_entry *, void *);
64 typedef struct git_config_iterator git_config_iterator;
65
66 typedef enum {
67 GIT_CVAR_FALSE = 0,
68 GIT_CVAR_TRUE = 1,
69 GIT_CVAR_INT32,
70 GIT_CVAR_STRING
71 } git_cvar_t;
72
73 typedef struct {
74 git_cvar_t cvar_type;
75 const char *str_match;
76 int map_value;
77 } git_cvar_map;
78
79 /**
80 * Locate the path to the global configuration file
81 *
82 * The user or global configuration file is usually
83 * located in `$HOME/.gitconfig`.
84 *
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.
89 *
90 * This method will not guess the path to the xdg compatible
91 * config file (.config/git/config).
92 *
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`.
96 */
97 GIT_EXTERN(int) git_config_find_global(char *out, size_t length);
98
99 /**
100 * Locate the path to the global xdg compatible configuration file
101 *
102 * The xdg compatible configuration file is usually
103 * located in `$HOME/.config/git/config`.
104 *
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.
109 *
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`.
114 */
115 GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length);
116
117 /**
118 * Locate the path to the system configuration file
119 *
120 * If /etc/gitconfig doesn't exist, it will look for
121 * %PROGRAMFILES%\Git\etc\gitconfig.
122
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`.
127 */
128 GIT_EXTERN(int) git_config_find_system(char *out, size_t length);
129
130 /**
131 * Open the global, XDG and system configuration files
132 *
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.
136 *
137 * @param out Pointer to store the config instance
138 * @return 0 or an error code
139 */
140 GIT_EXTERN(int) git_config_open_default(git_config **out);
141
142 /**
143 * Allocate a new configuration object
144 *
145 * This object is empty, so you have to add a file to it before you
146 * can do anything with it.
147 *
148 * @param out pointer to the new configuration
149 * @return 0 or an error code
150 */
151 GIT_EXTERN(int) git_config_new(git_config **out);
152
153 /**
154 * Add an on-disk config file instance to an existing config
155 *
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).
159 *
160 * Note that the configuration object will free the file
161 * automatically.
162 *
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).
166 *
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
174 */
175 GIT_EXTERN(int) git_config_add_file_ondisk(
176 git_config *cfg,
177 const char *path,
178 git_config_level_t level,
179 int force);
180
181 /**
182 * Create a new config instance containing a single on-disk file
183 *
184 * This method is a simple utility wrapper for the following sequence
185 * of calls:
186 * - git_config_new
187 * - git_config_add_file_ondisk
188 *
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
192 * or an error code
193 */
194 GIT_EXTERN(int) git_config_open_ondisk(git_config **out, const char *path);
195
196 /**
197 * Build a single-level focused config object from a multi-level one.
198 *
199 * The returned config object can be used to perform get/set/delete operations
200 * on a single specific level.
201 *
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
204 * instance.
205 *
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
211 */
212 GIT_EXTERN(int) git_config_open_level(
213 git_config **out,
214 const git_config *parent,
215 git_config_level_t level);
216
217 /**
218 * Open the global/XDG configuration file according to git's rules
219 *
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
224 * one to write to.
225 *
226 * @param out pointer in which to store the config object
227 * @param config the config object in which to look
228 */
229 GIT_EXTERN(int) git_config_open_global(git_config **out, git_config *config);
230
231
232 /**
233 * Reload changed config files
234 *
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.
239 *
240 * @param cfg The configuration to refresh
241 * @return 0 or an error code
242 */
243 GIT_EXTERN(int) git_config_refresh(git_config *cfg);
244
245 /**
246 * Free the configuration and its associated memory and files
247 *
248 * @param cfg the configuration to free
249 */
250 GIT_EXTERN(void) git_config_free(git_config *cfg);
251
252 /**
253 * Get the git_config_entry of a config variable.
254 *
255 * The git_config_entry is owned by the config and should not be freed by the
256 * user.
257
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
262 */
263 GIT_EXTERN(int) git_config_get_entry(
264 const git_config_entry **out,
265 const git_config *cfg,
266 const char *name);
267
268 /**
269 * Get the value of an integer config variable.
270 *
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.
274 *
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
279 */
280 GIT_EXTERN(int) git_config_get_int32(int32_t *out, const git_config *cfg, const char *name);
281
282 /**
283 * Get the value of a long integer config variable.
284 *
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.
288 *
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
293 */
294 GIT_EXTERN(int) git_config_get_int64(int64_t *out, const git_config *cfg, const char *name);
295
296 /**
297 * Get the value of a boolean config variable.
298 *
299 * This function uses the usual C convention of 0 being false and
300 * anything else true.
301 *
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.
305 *
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
310 */
311 GIT_EXTERN(int) git_config_get_bool(int *out, const git_config *cfg, const char *name);
312
313 /**
314 * Get the value of a string config variable.
315 *
316 * The string is owned by the variable and should not be freed by the
317 * user.
318 *
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.
322 *
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
327 */
328 GIT_EXTERN(int) git_config_get_string(const char **out, const git_config *cfg, const char *name);
329
330 /**
331 * Get each value of a multivar in a foreach callback
332 *
333 * The callback will be called on each variable found
334 *
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
341 */
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);
343
344 /**
345 * Get each value of a multivar
346 *
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
352 */
353 GIT_EXTERN(int) git_config_multivar_iterator_new(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp);
354
355 /**
356 * Return the current entry and advance the iterator
357 *
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
361 */
362 GIT_EXTERN(int) git_config_next(git_config_entry **entry, git_config_iterator *iter);
363
364 /**
365 * Free a config iterator
366 *
367 * @param iter the iterator to free
368 */
369 GIT_EXTERN(void) git_config_iterator_free(git_config_iterator *iter);
370
371 /**
372 * Set the value of an integer config variable in the config file
373 * with the highest level (usually the local one).
374 *
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
379 */
380 GIT_EXTERN(int) git_config_set_int32(git_config *cfg, const char *name, int32_t value);
381
382 /**
383 * Set the value of a long integer config variable in the config file
384 * with the highest level (usually the local one).
385 *
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
390 */
391 GIT_EXTERN(int) git_config_set_int64(git_config *cfg, const char *name, int64_t value);
392
393 /**
394 * Set the value of a boolean config variable in the config file
395 * with the highest level (usually the local one).
396 *
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
401 */
402 GIT_EXTERN(int) git_config_set_bool(git_config *cfg, const char *name, int value);
403
404 /**
405 * Set the value of a string config variable in the config file
406 * with the highest level (usually the local one).
407 *
408 * A copy of the string is made and the user is free to use it
409 * afterwards.
410 *
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
415 */
416 GIT_EXTERN(int) git_config_set_string(git_config *cfg, const char *name, const char *value);
417
418 /**
419 * Set a multivar in the local config file.
420 *
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.
425 */
426 GIT_EXTERN(int) git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value);
427
428 /**
429 * Delete a config variable from the config file
430 * with the highest level (usually the local one).
431 *
432 * @param cfg the configuration
433 * @param name the variable to delete
434 */
435 GIT_EXTERN(int) git_config_delete_entry(git_config *cfg, const char *name);
436
437 /**
438 * Deletes one or several entries from a multivar in the local config file.
439 *
440 * @param cfg where to look for the variables
441 * @param name the variable's name
442 * @param regexp a regular expression to indicate which values to delete
443 *
444 * @return 0 or an error code
445 */
446 GIT_EXTERN(int) git_config_delete_multivar(git_config *cfg, const char *name, const char *regexp);
447
448 /**
449 * Perform an operation on each config variable.
450 *
451 * The callback receives the normalized name and value of each variable
452 * in the config backend, and the data pointer passed to this function.
453 * As soon as one of the callback functions returns something other than 0,
454 * this function stops iterating and returns `GIT_EUSER`.
455 *
456 * @param cfg where to get the variables from
457 * @param callback the function to call on each variable
458 * @param payload the data to pass to the callback
459 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
460 */
461 GIT_EXTERN(int) git_config_foreach(
462 const git_config *cfg,
463 git_config_foreach_cb callback,
464 void *payload);
465
466 /**
467 * Iterate over all the config variables
468 *
469 * Use `git_config_next` to advance the iteration and
470 * `git_config_iterator_free` when done.
471 *
472 * @param out pointer to store the iterator
473 * @param cfg where to ge the variables from
474 */
475 GIT_EXTERN(int) git_config_iterator_new(git_config_iterator **out, const git_config *cfg);
476
477 /**
478 * Iterate over all the config variables whose name matches a pattern
479 *
480 * Use `git_config_next` to advance the iteration and
481 * `git_config_iterator_free` when done.
482 *
483 * @param out pointer to store the iterator
484 * @param cfg where to ge the variables from
485 * @param regexp regular expression to match the names
486 */
487 GIT_EXTERN(int) git_config_iterator_glob_new(git_config_iterator **out, const git_config *cfg, const char *regexp);
488
489 /**
490 * Perform an operation on each config variable matching a regular expression.
491 *
492 * This behaviors like `git_config_foreach` with an additional filter of a
493 * regular expression that filters which config keys are passed to the
494 * callback.
495 *
496 * @param cfg where to get the variables from
497 * @param regexp regular expression to match against config names
498 * @param callback the function to call on each variable
499 * @param payload the data to pass to the callback
500 * @return 0 or the return value of the callback which didn't return 0
501 */
502 GIT_EXTERN(int) git_config_foreach_match(
503 const git_config *cfg,
504 const char *regexp,
505 git_config_foreach_cb callback,
506 void *payload);
507
508 /**
509 * Query the value of a config variable and return it mapped to
510 * an integer constant.
511 *
512 * This is a helper method to easily map different possible values
513 * to a variable to integer constants that easily identify them.
514 *
515 * A mapping array looks as follows:
516 *
517 * git_cvar_map autocrlf_mapping[] = {
518 * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
519 * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
520 * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT},
521 * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}};
522 *
523 * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the
524 * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter.
525 *
526 * The same thing applies for any "true" value such as "true", "yes" or "1", storing
527 * the `GIT_AUTO_CRLF_TRUE` variable.
528 *
529 * Otherwise, if the value matches the string "input" (with case insensitive comparison),
530 * the given constant will be stored in `out`, and likewise for "default".
531 *
532 * If not a single match can be made to store in `out`, an error code will be
533 * returned.
534 *
535 * @param out place to store the result of the mapping
536 * @param cfg config file to get the variables from
537 * @param name name of the config variable to lookup
538 * @param maps array of `git_cvar_map` objects specifying the possible mappings
539 * @param map_n number of mapping objects in `maps`
540 * @return 0 on success, error code otherwise
541 */
542 GIT_EXTERN(int) git_config_get_mapped(
543 int *out,
544 const git_config *cfg,
545 const char *name,
546 const git_cvar_map *maps,
547 size_t map_n);
548
549 /**
550 * Maps a string value to an integer constant
551 *
552 * @param out place to store the result of the parsing
553 * @param maps array of `git_cvar_map` objects specifying the possible mappings
554 * @param map_n number of mapping objects in `maps`
555 * @param value value to parse
556 */
557 GIT_EXTERN(int) git_config_lookup_map_value(
558 int *out,
559 const git_cvar_map *maps,
560 size_t map_n,
561 const char *value);
562
563 /**
564 * Parse a string value as a bool.
565 *
566 * Valid values for true are: 'true', 'yes', 'on', 1 or any
567 * number different from 0
568 * Valid values for false are: 'false', 'no', 'off', 0
569 *
570 * @param out place to store the result of the parsing
571 * @param value value to parse
572 */
573 GIT_EXTERN(int) git_config_parse_bool(int *out, const char *value);
574
575 /**
576 * Parse a string value as an int32.
577 *
578 * An optional value suffix of 'k', 'm', or 'g' will
579 * cause the value to be multiplied by 1024, 1048576,
580 * or 1073741824 prior to output.
581 *
582 * @param out place to store the result of the parsing
583 * @param value value to parse
584 */
585 GIT_EXTERN(int) git_config_parse_int32(int32_t *out, const char *value);
586
587 /**
588 * Parse a string value as an int64.
589 *
590 * An optional value suffix of 'k', 'm', or 'g' will
591 * cause the value to be multiplied by 1024, 1048576,
592 * or 1073741824 prior to output.
593 *
594 * @param out place to store the result of the parsing
595 * @param value value to parse
596 */
597 GIT_EXTERN(int) git_config_parse_int64(int64_t *out, const char *value);
598
599
600 /**
601 * Perform an operation on each config variable in given config backend
602 * matching a regular expression.
603 *
604 * This behaviors like `git_config_foreach_match` except instead of all config
605 * entries it just enumerates through the given backend entry.
606 *
607 * @param backend where to get the variables from
608 * @param regexp regular expression to match against config names (can be NULL)
609 * @param callback the function to call on each variable
610 * @param payload the data to pass to the callback
611 */
612 GIT_EXTERN(int) git_config_backend_foreach_match(
613 git_config_backend *backend,
614 const char *regexp,
615 int (*fn)(const git_config_entry *, void *),
616 void *data);
617
618
619 /** @} */
620 GIT_END_DECL
621 #endif