]> git.proxmox.com Git - libgit2.git/blame - include/git2/config.h
Merge pull request #1635 from arrbee/simplify-mkdir
[libgit2.git] / include / git2 / config.h
CommitLineData
5d4cd003 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
5d4cd003 3 *
bb742ede
VM
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.
5d4cd003
CMN
6 */
7#ifndef INCLUDE_git_config_h__
8#define INCLUDE_git_config_h__
9
10#include "common.h"
9a3c5e55 11#include "types.h"
5d4cd003
CMN
12
13/**
9a3c5e55 14 * @file git2/config.h
5d4cd003 15 * @brief Git config management routines
9a3c5e55 16 * @defgroup git_config Git config management routines
5d4cd003
CMN
17 * @ingroup Git
18 * @{
19 */
20GIT_BEGIN_DECL
21
a1abe66a 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 */
16adc9fa
RB
30typedef 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 /** Represents the highest level available config file (i.e. the most
48 * specific config file available that actually is loaded)
49 */
50 GIT_CONFIG_HIGHEST_LEVEL = -1,
51} git_config_level_t;
a1abe66a 52
53typedef struct {
54 const char *name;
55 const char *value;
16adc9fa 56 git_config_level_t level;
a1abe66a 57} git_config_entry;
58
54b2a37a
BS
59typedef int (*git_config_foreach_cb)(const git_config_entry *, void *);
60
c5e94482
VM
61typedef enum {
62 GIT_CVAR_FALSE = 0,
63 GIT_CVAR_TRUE = 1,
64 GIT_CVAR_INT32,
65 GIT_CVAR_STRING
66} git_cvar_t;
67
68typedef struct {
69 git_cvar_t cvar_type;
70 const char *str_match;
71 int map_value;
72} git_cvar_map;
73
19cb6857
VM
74/**
75 * Locate the path to the global configuration file
76 *
77 * The user or global configuration file is usually
78 * located in `$HOME/.gitconfig`.
79 *
80 * This method will try to guess the full path to that
81 * file, if the file exists. The returned path
82 * may be used on any `git_config` call to load the
83 * global configuration file.
84 *
4258d483 85 * This method will not guess the path to the xdg compatible
8b4f9b17
SS
86 * config file (.config/git/config).
87 *
54b2a37a
BS
88 * @param out Buffer to store the path in
89 * @param length size of the buffer in bytes
90 * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`.
19cb6857 91 */
54b2a37a 92GIT_EXTERN(int) git_config_find_global(char *out, size_t length);
19cb6857 93
8b4f9b17 94/**
4258d483 95 * Locate the path to the global xdg compatible configuration file
8b4f9b17 96 *
4258d483 97 * The xdg compatible configuration file is usually
8b4f9b17
SS
98 * located in `$HOME/.config/git/config`.
99 *
100 * This method will try to guess the full path to that
101 * file, if the file exists. The returned path
102 * may be used on any `git_config` call to load the
4258d483 103 * xdg compatible configuration file.
8b4f9b17 104 *
54b2a37a
BS
105 * @param out Buffer to store the path in
106 * @param length size of the buffer in bytes
4258d483 107 * @return 0 if a xdg compatible configuration file has been
8b4f9b17
SS
108 * found. Its path will be stored in `buffer`.
109 */
54b2a37a 110GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length);
8b4f9b17 111
4c562347
CMN
112/**
113 * Locate the path to the system configuration file
114 *
115 * If /etc/gitconfig doesn't exist, it will look for
116 * %PROGRAMFILES%\Git\etc\gitconfig.
117
54b2a37a
BS
118 * @param global_config_path Buffer to store the path in
119 * @param length size of the buffer in bytes
e172cf08 120 * @return 0 if a system configuration file has been
4c562347
CMN
121 * found. Its path will be stored in `buffer`.
122 */
54b2a37a 123GIT_EXTERN(int) git_config_find_system(char *out, size_t length);
4c562347 124
ca1b6e54 125/**
a1abe66a 126 * Open the global, XDG and system configuration files
ca1b6e54 127 *
a1abe66a 128 * Utility wrapper that finds the global, XDG and system configuration files
ca1b6e54 129 * and opens them into a single prioritized config object that can be
85bd1746 130 * used when accessing default config data outside a repository.
ca1b6e54
RB
131 *
132 * @param out Pointer to store the config instance
133 * @return 0 or an error code
134 */
85bd1746 135GIT_EXTERN(int) git_config_open_default(git_config **out);
ca1b6e54 136
c0335005 137/**
a2a305fc
CMN
138 * Allocate a new configuration object
139 *
140 * This object is empty, so you have to add a file to it before you
141 * can do anything with it.
142 *
143 * @param out pointer to the new configuration
e172cf08 144 * @return 0 or an error code
c0335005
CMN
145 */
146GIT_EXTERN(int) git_config_new(git_config **out);
147
c0335005 148/**
07ff8817
VM
149 * Add an on-disk config file instance to an existing config
150 *
151 * The on-disk file pointed at by `path` will be opened and
152 * parsed; it's expected to be a native Git config file following
153 * the default Git config syntax (see man git-config).
c0335005 154 *
a2a305fc
CMN
155 * Note that the configuration object will free the file
156 * automatically.
f44cbec4 157 *
07ff8817
VM
158 * Further queries on this config object will access each
159 * of the config file instances in order (instances with
a1abe66a 160 * a higher priority level will be accessed first).
07ff8817 161 *
ce78f39e 162 * @param cfg the configuration to add the file to
83041c71 163 * @param path path to the configuration file to add
a1abe66a 164 * @param level the priority level of the backend
83041c71 165 * @param force replace config file at the given priority level
a1abe66a 166 * @return 0 on success, GIT_EEXISTS when adding more than one file
270160b9 167 * for a given priority level (and force_replace set to 0),
168 * GIT_ENOTFOUND when the file doesn't exist or error code
c0335005 169 */
a1abe66a 170GIT_EXTERN(int) git_config_add_file_ondisk(
171 git_config *cfg,
172 const char *path,
16adc9fa 173 git_config_level_t level,
a1abe66a 174 int force);
07ff8817 175
07ff8817
VM
176/**
177 * Create a new config instance containing a single on-disk file
178 *
179 * This method is a simple utility wrapper for the following sequence
180 * of calls:
181 * - git_config_new
182 * - git_config_add_file_ondisk
183 *
270160b9 184 * @param out The configuration instance to create
07ff8817 185 * @param path Path to the on-disk file to open
270160b9 186 * @return 0 on success, GIT_ENOTFOUND when the file doesn't exist
187 * or an error code
07ff8817 188 */
270160b9 189GIT_EXTERN(int) git_config_open_ondisk(git_config **out, const char *path);
5d4cd003 190
a1abe66a 191/**
192 * Build a single-level focused config object from a multi-level one.
193 *
194 * The returned config object can be used to perform get/set/delete operations
195 * on a single specific level.
196 *
197 * Getting several times the same level from the same parent multi-level config
198 * will return different config instances, but containing the same config_file
199 * instance.
200 *
54b2a37a
BS
201 * @param out The configuration instance to create
202 * @param parent Multi-level config to search for the given level
203 * @param level Configuration level to search for
a1abe66a 204 * @return 0, GIT_ENOTFOUND if the passed level cannot be found in the
205 * multi-level parent config, or an error code
206 */
207GIT_EXTERN(int) git_config_open_level(
0cb16fe9
L
208 git_config **out,
209 const git_config *parent,
16adc9fa 210 git_config_level_t level);
a1abe66a 211
5d831887
CMN
212/**
213 * Open the global/XDG configuration file according to git's rules
214 *
215 * Git allows you to store your global configuration at
216 * `$HOME/.config` or `$XDG_CONFIG_HOME/git/config`. For backwards
217 * compatability, the XDG file shouldn't be used unless the use has
218 * created it explicitly. With this function you'll open the correct
219 * one to write to.
220 *
221 * @param out pointer in which to store the config object
222 * @param config the config object in which to look
223 */
224GIT_EXTERN(int) git_config_open_global(git_config **out, git_config *config);
225
226
744cc03e
RB
227/**
228 * Reload changed config files
229 *
230 * A config file may be changed on disk out from under the in-memory
231 * config object. This function causes us to look for files that have
232 * been modified since we last loaded them and refresh the config with
233 * the latest information.
234 *
235 * @param cfg The configuration to refresh
236 * @return 0 or an error code
237 */
238GIT_EXTERN(int) git_config_refresh(git_config *cfg);
239
5d4cd003 240/**
a2a305fc 241 * Free the configuration and its associated memory and files
9a3c5e55
CMN
242 *
243 * @param cfg the configuration to free
5d4cd003
CMN
244 */
245GIT_EXTERN(void) git_config_free(git_config *cfg);
246
a1abe66a 247/**
248 * Get the git_config_entry of a config variable.
249 *
250 * The git_config_entry is owned by the config and should not be freed by the
251 * user.
252
253 * @param out pointer to the variable git_config_entry
254 * @param cfg where to look for the variable
255 * @param name the variable's name
256 * @return 0 or an error code
257 */
54b2a37a 258GIT_EXTERN(int) git_config_get_entry(
16adc9fa 259 const git_config_entry **out,
54b2a37a
BS
260 const git_config *cfg,
261 const char *name);
a1abe66a 262
9a3c5e55
CMN
263/**
264 * Get the value of an integer config variable.
265 *
a1abe66a 266 * All config files will be looked into, in the order of their
267 * defined level. A higher level means a higher priority. The
268 * first occurence of the variable will be returned here.
269 *
29e948de 270 * @param out pointer to the variable where the value should be stored
9a3c5e55
CMN
271 * @param cfg where to look for the variable
272 * @param name the variable's name
e172cf08 273 * @return 0 or an error code
9a3c5e55 274 */
54b2a37a 275GIT_EXTERN(int) git_config_get_int32(int32_t *out, const git_config *cfg, const char *name);
9a3c5e55 276
e69ac243
CMN
277/**
278 * Get the value of a long integer config variable.
279 *
a1abe66a 280 * All config files will be looked into, in the order of their
281 * defined level. A higher level means a higher priority. The
7eb222fc 282 * first occurrence of the variable will be returned here.
a1abe66a 283 *
29e948de 284 * @param out pointer to the variable where the value should be stored
e69ac243
CMN
285 * @param cfg where to look for the variable
286 * @param name the variable's name
e172cf08 287 * @return 0 or an error code
e69ac243 288 */
54b2a37a 289GIT_EXTERN(int) git_config_get_int64(int64_t *out, const git_config *cfg, const char *name);
e69ac243 290
9a3c5e55
CMN
291/**
292 * Get the value of a boolean config variable.
293 *
2974aa94
CMN
294 * This function uses the usual C convention of 0 being false and
295 * anything else true.
296 *
a1abe66a 297 * All config files will be looked into, in the order of their
298 * defined level. A higher level means a higher priority. The
7eb222fc 299 * first occurrence of the variable will be returned here.
a1abe66a 300 *
29e948de 301 * @param out pointer to the variable where the value should be stored
9a3c5e55
CMN
302 * @param cfg where to look for the variable
303 * @param name the variable's name
e172cf08 304 * @return 0 or an error code
9a3c5e55 305 */
54b2a37a 306GIT_EXTERN(int) git_config_get_bool(int *out, const git_config *cfg, const char *name);
9a3c5e55
CMN
307
308/**
309 * Get the value of a string config variable.
310 *
311 * The string is owned by the variable and should not be freed by the
312 * user.
313 *
a1abe66a 314 * All config files will be looked into, in the order of their
315 * defined level. A higher level means a higher priority. The
7eb222fc 316 * first occurrence of the variable will be returned here.
a1abe66a 317 *
29e948de 318 * @param out pointer to the variable's value
9a3c5e55
CMN
319 * @param cfg where to look for the variable
320 * @param name the variable's name
e172cf08 321 * @return 0 or an error code
9a3c5e55 322 */
54b2a37a 323GIT_EXTERN(int) git_config_get_string(const char **out, const git_config *cfg, const char *name);
9a3c5e55 324
5e0dc4af 325/**
d9da4cca
CMN
326 * Get each value of a multivar.
327 *
328 * The callback will be called on each variable found
329 *
330 * @param cfg where to look for the variable
331 * @param name the variable's name
332 * @param regexp regular expression to filter which variables we're
333 * interested in. Use NULL to indicate all
334 * @param fn the function to be called on each value of the variable
335 * @param data opaque pointer to pass to the callback
5e0dc4af 336 */
54b2a37a 337GIT_EXTERN(int) git_config_get_multivar(const git_config *cfg, const char *name, const char *regexp, git_config_foreach_cb callback, void *payload);
5e0dc4af 338
9a3c5e55 339/**
a1abe66a 340 * Set the value of an integer config variable in the config file
341 * with the highest level (usually the local one).
9a3c5e55
CMN
342 *
343 * @param cfg where to look for the variable
344 * @param name the variable's name
d144c569 345 * @param value Integer value for the variable
e172cf08 346 * @return 0 or an error code
9a3c5e55 347 */
fafd4710 348GIT_EXTERN(int) git_config_set_int32(git_config *cfg, const char *name, int32_t value);
9a3c5e55 349
e69ac243 350/**
a1abe66a 351 * Set the value of a long integer config variable in the config file
352 * with the highest level (usually the local one).
e69ac243
CMN
353 *
354 * @param cfg where to look for the variable
355 * @param name the variable's name
d144c569 356 * @param value Long integer value for the variable
e172cf08 357 * @return 0 or an error code
e69ac243 358 */
fafd4710 359GIT_EXTERN(int) git_config_set_int64(git_config *cfg, const char *name, int64_t value);
e69ac243 360
9a3c5e55 361/**
a1abe66a 362 * Set the value of a boolean config variable in the config file
363 * with the highest level (usually the local one).
9a3c5e55
CMN
364 *
365 * @param cfg where to look for the variable
366 * @param name the variable's name
367 * @param value the value to store
e172cf08 368 * @return 0 or an error code
9a3c5e55 369 */
2974aa94 370GIT_EXTERN(int) git_config_set_bool(git_config *cfg, const char *name, int value);
9a3c5e55
CMN
371
372/**
a1abe66a 373 * Set the value of a string config variable in the config file
374 * with the highest level (usually the local one).
9a3c5e55
CMN
375 *
376 * A copy of the string is made and the user is free to use it
377 * afterwards.
378 *
379 * @param cfg where to look for the variable
380 * @param name the variable's name
381 * @param value the string to store.
e172cf08 382 * @return 0 or an error code
9a3c5e55
CMN
383 */
384GIT_EXTERN(int) git_config_set_string(git_config *cfg, const char *name, const char *value);
385
3005855f 386/**
a1abe66a 387 * Set a multivar in the local config file.
d9da4cca
CMN
388 *
389 * @param cfg where to look for the variable
390 * @param name the variable's name
391 * @param regexp a regular expression to indicate which values to replace
392 * @param value the new value.
3005855f
CMN
393 */
394GIT_EXTERN(int) git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value);
395
9a3c5e55 396/**
a1abe66a 397 * Delete a config variable from the config file
398 * with the highest level (usually the local one).
2601fcfc
CMN
399 *
400 * @param cfg the configuration
401 * @param name the variable to delete
402 */
54b2a37a 403GIT_EXTERN(int) git_config_delete_entry(git_config *cfg, const char *name);
2601fcfc
CMN
404
405/**
406 * Perform an operation on each config variable.
9a3c5e55 407 *
cfef5fb7
VM
408 * The callback receives the normalized name and value of each variable
409 * in the config backend, and the data pointer passed to this function.
410 * As soon as one of the callback functions returns something other than 0,
5dca2010 411 * this function stops iterating and returns `GIT_EUSER`.
9a3c5e55
CMN
412 *
413 * @param cfg where to get the variables from
414 * @param callback the function to call on each variable
e0fc39da 415 * @param payload the data to pass to the callback
5dca2010 416 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
9a3c5e55 417 */
cfef5fb7 418GIT_EXTERN(int) git_config_foreach(
54b2a37a
BS
419 const git_config *cfg,
420 git_config_foreach_cb callback,
cfef5fb7 421 void *payload);
9a3c5e55 422
b3ff1dab
RB
423/**
424 * Perform an operation on each config variable matching a regular expression.
425 *
426 * This behaviors like `git_config_foreach` with an additional filter of a
427 * regular expression that filters which config keys are passed to the
428 * callback.
429 *
430 * @param cfg where to get the variables from
431 * @param regexp regular expression to match against config names
432 * @param callback the function to call on each variable
433 * @param payload the data to pass to the callback
434 * @return 0 or the return value of the callback which didn't return 0
435 */
436GIT_EXTERN(int) git_config_foreach_match(
54b2a37a 437 const git_config *cfg,
b3ff1dab 438 const char *regexp,
54b2a37a 439 git_config_foreach_cb callback,
b3ff1dab 440 void *payload);
c5e94482
VM
441
442/**
443 * Query the value of a config variable and return it mapped to
444 * an integer constant.
445 *
446 * This is a helper method to easily map different possible values
447 * to a variable to integer constants that easily identify them.
448 *
449 * A mapping array looks as follows:
450 *
b90202bb 451 * git_cvar_map autocrlf_mapping[] = {
c5e94482
VM
452 * {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
453 * {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
454 * {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT},
455 * {GIT_CVAR_STRING, "default", GIT_AUTO_CRLF_DEFAULT}};
456 *
457 * On any "false" value for the variable (e.g. "false", "FALSE", "no"), the
458 * mapping will store `GIT_AUTO_CRLF_FALSE` in the `out` parameter.
459 *
460 * The same thing applies for any "true" value such as "true", "yes" or "1", storing
461 * the `GIT_AUTO_CRLF_TRUE` variable.
462 *
463 * Otherwise, if the value matches the string "input" (with case insensitive comparison),
464 * the given constant will be stored in `out`, and likewise for "default".
465 *
466 * If not a single match can be made to store in `out`, an error code will be
467 * returned.
468 *
29e948de 469 * @param out place to store the result of the mapping
c5e94482
VM
470 * @param cfg config file to get the variables from
471 * @param name name of the config variable to lookup
472 * @param maps array of `git_cvar_map` objects specifying the possible mappings
473 * @param map_n number of mapping objects in `maps`
e172cf08 474 * @return 0 on success, error code otherwise
c5e94482 475 */
54b2a37a 476GIT_EXTERN(int) git_config_get_mapped(
0cb16fe9
L
477 int *out,
478 const git_config *cfg,
479 const char *name,
480 const git_cvar_map *maps,
481 size_t map_n);
c5e94482 482
a1abe66a 483/**
484 * Maps a string value to an integer constant
485 *
486 * @param out place to store the result of the parsing
487 * @param maps array of `git_cvar_map` objects specifying the possible mappings
488 * @param map_n number of mapping objects in `maps`
489 * @param value value to parse
490 */
491GIT_EXTERN(int) git_config_lookup_map_value(
492 int *out,
54b2a37a 493 const git_cvar_map *maps,
a1abe66a 494 size_t map_n,
495 const char *value);
496
497/**
498 * Parse a string value as a bool.
499 *
500 * Valid values for true are: 'true', 'yes', 'on', 1 or any
501 * number different from 0
502 * Valid values for false are: 'false', 'no', 'off', 0
503 *
504 * @param out place to store the result of the parsing
505 * @param value value to parse
506 */
507GIT_EXTERN(int) git_config_parse_bool(int *out, const char *value);
508
509/**
54b2a37a 510 * Parse a string value as an int32.
a1abe66a 511 *
512 * An optional value suffix of 'k', 'm', or 'g' will
513 * cause the value to be multiplied by 1024, 1048576,
514 * or 1073741824 prior to output.
515 *
516 * @param out place to store the result of the parsing
517 * @param value value to parse
518 */
54b2a37a 519GIT_EXTERN(int) git_config_parse_int32(int32_t *out, const char *value);
a1abe66a 520
521/**
54b2a37a 522 * Parse a string value as an int64.
a1abe66a 523 *
524 * An optional value suffix of 'k', 'm', or 'g' will
525 * cause the value to be multiplied by 1024, 1048576,
526 * or 1073741824 prior to output.
527 *
528 * @param out place to store the result of the parsing
529 * @param value value to parse
530 */
54b2a37a 531GIT_EXTERN(int) git_config_parse_int64(int64_t *out, const char *value);
a1abe66a 532
533
5d4cd003
CMN
534/** @} */
535GIT_END_DECL
536#endif