Commit 4c562347aec5427ddf78d271f7dc2947ef7aa49e

Carlos Martín Nieto 2011-09-22T21:34:46

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>

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;