config: add parsing and getter for paths · nodegit/libgit2@eac773d · GitHub
Skip to content

Commit eac773d

Browse files
committed
config: add parsing and getter for paths
1 parent 4d6f55a commit eac773d

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions

‎include/git2/config.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,24 @@ GIT_EXTERN(int) git_config_get_int64(int64_t *out, const git_config *cfg, const
319319
*/
320320
GIT_EXTERN(int) git_config_get_bool(int *out, const git_config *cfg, const char *name);
321321

322+
/**
323+
* Get the value of a path config variable.
324+
*
325+
* A leading '~' will be expanded to the global search path (which
326+
* defaults to the user's home directory but can be overridden via
327+
* `git_libgit2_opts()`.
328+
*
329+
* All config files will be looked into, in the order of their
330+
* defined level. A higher level means a higher priority. The
331+
* first occurrence of the variable will be returned here.
332+
*
333+
* @param out the buffer in which to store the result
334+
* @param cfg where to look for the variable
335+
* @param name the variable's name
336+
* @param 0 or an error code
337+
*/
338+
GIT_EXTERN(int) git_config_get_path(git_buf *out, const git_config *cfg, const char *name);
339+
322340
/**
323341
* Get the value of a string config variable.
324342
*
@@ -615,6 +633,20 @@ GIT_EXTERN(int) git_config_parse_int32(int32_t *out, const char *value);
615633
*/
616634
GIT_EXTERN(int) git_config_parse_int64(int64_t *out, const char *value);
617635

636+
/**
637+
* Parse a string value as a path.
638+
*
639+
* A leading '~' will be expanded to the global search path (which
640+
* defaults to the user's home directory but can be overridden via
641+
* `git_libgit2_opts()`.
642+
*
643+
* If the value does not begin with a tilde, the input will be
644+
* returned.
645+
*
646+
* @param out placae to store the result of parsing
647+
* @param value the path to evaluate
648+
*/
649+
GIT_EXTERN(int) git_config_parse_path(git_buf *out, const char *value);
618650

619651
/**
620652
* Perform an operation on each config variable in given config backend

‎src/config.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,17 @@ int git_config_get_bool(int *out, const git_config *cfg, const char *name)
785785
return git_config_parse_bool(out, entry->value);
786786
}
787787

788+
int git_config_get_path(git_buf *out, const git_config *cfg, const char *name)
789+
{
790+
const git_config_entry *entry;
791+
int error;
792+
793+
if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
794+
return error;
795+
796+
return git_config_parse_path(out, entry->value);
797+
}
798+
788799
int git_config_get_string(
789800
const char **out, const git_config *cfg, const char *name)
790801
{
@@ -1184,6 +1195,36 @@ int git_config_parse_int32(int32_t *out, const char *value)
11841195
return -1;
11851196
}
11861197

1198+
int git_config_parse_path(git_buf *out, const char *value)
1199+
{
1200+
int error = 0;
1201+
const git_buf *home;
1202+
1203+
assert(out && value);
1204+
1205+
git_buf_sanitize(out);
1206+
1207+
if (value[0] == '~') {
1208+
if (value[1] != '\0' && value[1] != '/') {
1209+
giterr_set(GITERR_CONFIG, "retrieving a homedir by name is not supported");
1210+
return -1;
1211+
}
1212+
1213+
if ((error = git_sysdir_get(&home, GIT_SYSDIR_GLOBAL)) < 0)
1214+
return error;
1215+
1216+
git_buf_sets(out, home->ptr);
1217+
git_buf_puts(out, value + 1);
1218+
1219+
if (git_buf_oom(out))
1220+
return -1;
1221+
1222+
return 0;
1223+
}
1224+
1225+
return git_buf_sets(out, value);
1226+
}
1227+
11871228
/* Take something the user gave us and make it nice for our hash function */
11881229
int git_config__normalize_name(const char *in, char **out)
11891230
{

‎tests/config/read.c‎

Lines changed: 46 additions & 0 deletions

0 commit comments

Comments
 (0)