Add git_config_find_system This allows the library to guess where the system configuration file should be located. Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/include/git2/config.h b/include/git2/config.h
index 83ed2f9..bafac68 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -52,6 +52,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
*
* Utility wrapper that calls `git_config_find_global`
diff --git a/src/config.c b/src/config.c
index 0ec7100..a4445ca 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 <windows.h>
+#endif
#include <ctype.h>
@@ -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;