Commit f64568ebcb8a5c5be9dcda2a016692edae42a507

Sven Strickroth 2019-06-28T16:49:58

Do not unconditionally remove the last 4 chars of the directory where git.exe was found Removal of the last 4 chars is only required for Git for Windows installations in order to find the "root" folder of the Git installation. Fixes issue #5127. Signed-off-by: Sven Strickroth <email@cs-ware.de>

diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index e31ff97..d5dba8c 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -93,9 +93,17 @@ static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wch
 			continue;
 		wcscpy(&root.path[root.len], gitexe);
 
-		if (_waccess(root.path, F_OK) == 0 && root.len > 5 && (root.len - 4 + wcslen(subdir) < MAX_PATH)) {
-			/* replace "bin\\" or "cmd\\" with subdir */
-			wcscpy(&root.path[root.len - 4], subdir);
+		if (!_waccess(root.path, F_OK)) {
+			/* replace "bin\\" or "cmd\\" of a Git for Windows installation with subdir OR append path */
+			if ((root.len > 5 && wcscmp(root.path - 4, L"cmd\\")) || wcscmp(root.path - 4, L"bin\\")) {
+				if (root.len - 4 + wcslen(subdir) >= MAX_PATH)
+					continue;
+				wcscpy(&root.path[root.len - 4], subdir);
+			} else {
+				if (root.len + wcslen(subdir) >= MAX_PATH)
+					continue;
+				wcscat(root.path, subdir);
+			}
 
 			win32_path_to_8(buf, root.path);
 			return 0;