Commit 6605f51d81a9ccfb1b5a1c1689a57cf3f5b2f5b3

Sven Strickroth 2012-09-24T18:50:37

Automatically detect msysgit installation path Do not hardcode the installation path of msysgit, but read installation path from registry. Also "%PROGRAMFILES%\Git\etc" won't work on x64 systems with 64-bit libgit2, because msysgit is x86 only and located in "%ProgramFiles(x86)%\Git\etc". Signed-off-by: Sven Strickroth <email@cs-ware.de>

diff --git a/src/fileops.c b/src/fileops.c
index cd0c055..b9044f0 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -423,12 +423,36 @@ static int win32_find_file(git_buf *path, const struct win32_path *root, const c
 int git_futils_find_system_file(git_buf *path, const char *filename)
 {
 #ifdef GIT_WIN32
+#ifndef _WIN64
+#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
+#else
+#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
+#endif
+
 	struct win32_path root;
 
-	if (win32_expand_path(&root, L"%PROGRAMFILES%\\Git\\etc\\") < 0 ||
-		root.path[0] == L'%') /* i.e. no expansion happened */
+	HKEY hKey;
+	DWORD dwType = REG_SZ;
+	DWORD dwSize = MAX_PATH;
+
+	root.len = 0;
+	if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
 	{
-		giterr_set(GITERR_OS, "Cannot locate the system's Program Files directory");
+		if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType,(LPBYTE)&root.path, &dwSize) == ERROR_SUCCESS)
+		{
+			// InstallLocation points to the root of the msysgit directory
+			if (wcscat_s(root.path, MAX_PATH, L"etc\\"))
+			{
+				giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long");
+				return -1;
+			}
+			root.len = (DWORD)wcslen(root.path) + 1;
+		}
+	}
+	RegCloseKey(hKey);
+
+	if (!root.len) {
+		giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory");
 		return -1;
 	}