From 4c562347aec5427ddf78d271f7dc2947ef7aa49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 22 Sep 2011 21:34:46 +0200 Subject: [PATCH] Add git_config_find_system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows the library to guess where the system configuration file should be located. Signed-off-by: Carlos Martín Nieto --- include/git2/config.h | 12 +++++++++ src/config.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/git2/config.h b/include/git2/config.h index 83ed2f91c..bafac6804 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -51,6 +51,18 @@ struct git_config_file { */ GIT_EXTERN(int) git_config_find_global(char *global_config_path); +/** + * Locate the path to the system configuration file + * + * If /etc/gitconfig doesn't exist, it will look for + * %PROGRAMFILES%\Git\etc\gitconfig. + + * @param system_config_path Buffer of GIT_PATH_MAX length to store the path + * @return GIT_SUCCESS if a system configuration file has been + * found. Its path will be stored in `buffer`. + */ +GIT_EXTERN(int) git_config_find_system(char *system_config_path); + /** * Open the global configuration file * diff --git a/src/config.c b/src/config.c index 0ec710036..a4445cac0 100644 --- a/src/config.c +++ b/src/config.c @@ -11,6 +11,9 @@ #include "config.h" #include "git2/config.h" #include "vector.h" +#if GIT_WIN32 +# include +#endif #include @@ -332,6 +335,62 @@ int git_config_find_global(char *global_config_path) return GIT_SUCCESS; } + + +#if GIT_WIN32 +static int win32_find_system(char *system_config_path) +{ + const wchar_t *query = L"%PROGRAMFILES%\\Git\\etc\\gitconfig"; + wchar_t *apphome_utf16; + char *apphome_utf8; + DWORD size, ret; + + size = ExpandEnvironmentStringsW(query, NULL, 0); + /* The function gave us the full size of the buffer in chars, including NUL */ + apphome_utf16 = git__malloc(size * sizeof(wchar_t)); + if (apphome_utf16 == NULL) + return GIT_ENOMEM; + + ret = ExpandEnvironmentStringsW(query, apphome_utf16, size); + free(query_utf16); + if (ret == 0 || ret >= size) + return git__throw(GIT_ERROR, "Failed to expand environment strings"); + + if (_waccess(apphome_utf16, F_OK) < 0) { + free(apphome_utf16); + return GIT_ENOTFOUND; + } + + apphome_utf8 = conv_utf16_to_utf8(apphome_utf16); + free(apphome_utf16); + + if (strlen(apphome_utf8) >= GIT_PATH_MAX) { + free(apphome_utf8); + return git__throw(GIT_ESHORTBUFFER, "Path is too long"); + } + + strcpy(system_config_path, apphome_utf8); + free(apphome_utf8); + return GIT_SUCCESS; +} +#endif + +int git_config_find_system(char *system_config_path) +{ + const char *etc = "/etc/gitconfig"; + + if (git_futils_exists(etc) == GIT_SUCCESS) { + memcpy(system_config_path, etc, strlen(etc) + 1); + return GIT_SUCCESS; + } + +#if GIT_WIN32 + return win32_find_system(system_config_path); +#else + return GIT_ENOTFOUND; +#endif +} + int git_config_open_global(git_config **out) { int error;