From ce02fb4a0e5761d9993f9adaea3f434cdb2b3fba Mon Sep 17 00:00:00 2001
From: "Dax T. Games" <dtgames@kinggeek.org>
Date: Sat, 16 Apr 2022 10:51:26 -0400
Subject: [PATCH 1/4] fix find latest git

---
 vendor/init.bat        | 10 +++++-----
 vendor/lib/lib_git.cmd | 18 +++++++++++-------
 2 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/vendor/init.bat b/vendor/init.bat
index e5da902..2ee6a59 100644
--- a/vendor/init.bat
+++ b/vendor/init.bat
@@ -222,7 +222,7 @@ if defined GIT_INSTALL_ROOT (
     if exist "%GIT_INSTALL_ROOT%\cmd\git.exe" goto :SPECIFIED_GIT
 ) else if "%fast_init%" == "1" (
     if exist "%CMDER_ROOT%\vendor\git-for-windows\cmd\git.exe" (
-      %print_debug% "Skipping Git Auto-Detect!"
+      %print_debug% init.bat "Skipping Git Auto-Detect!"
       goto :VENDORED_GIT
     )
 )
@@ -249,22 +249,22 @@ for /F "delims=" %%F in ('where git.exe 2^>nul') do (
 :VENDORED_GIT
 if exist "%CMDER_ROOT%\vendor\git-for-windows" (
     set "GIT_INSTALL_ROOT=%CMDER_ROOT%\vendor\git-for-windows"
-    %print_debug% "Using vendored Git '%GIT_VERSION_VENDORED%'..."
+    %print_debug% init.bat "Using vendored Git '%GIT_VERSION_VENDORED%'..."
     goto :CONFIGURE_GIT
 ) else (
     goto :NO_GIT
 )
 
 :SPECIFIED_GIT
-%print_debug% "Using /GIT_INSTALL_ROOT..."
+%print_debug% init.bat "Using /GIT_INSTALL_ROOT..."
 goto :CONFIGURE_GIT
 
 :FOUND_GIT
-%print_debug% "Using found Git '%GIT_VERSION_USER%' from '%GIT_INSTALL_ROOT%..."
+%print_debug% init.bat "Using found Git '%GIT_VERSION_USER%' from '%GIT_INSTALL_ROOT%..."
 goto :CONFIGURE_GIT
 
 :CONFIGURE_GIT
-%print_debug% "Using Git from '%GIT_INSTALL_ROOT%..."
+%print_debug% init.bat "Using Git from '%GIT_INSTALL_ROOT%..."
 :: Add git to the path
 if exist "%GIT_INSTALL_ROOT%\cmd\git.exe" %lib_path% enhance_path "%GIT_INSTALL_ROOT%\cmd" ""
 
diff --git a/vendor/lib/lib_git.cmd b/vendor/lib/lib_git.cmd
index 19d2af7..eedef37 100644
--- a/vendor/lib/lib_git.cmd
+++ b/vendor/lib/lib_git.cmd
@@ -163,9 +163,9 @@ exit /b
     :: checks all major, minor, patch and build variables for the given arguments.
     :: whichever binary that has the most recent version will be used based on the return code.
 
-    %print_debug% Comparing:
-    %print_debug% %~1: %USER_MAJOR%.%USER_MINOR%.%USER_PATCH%.%USER_BUILD%
-    %print_debug% %~2: %VENDORED_MAJOR%.%VENDORED_MINOR%.%VENDORED_PATCH%.%VENDORED_BUILD%
+    %print_debug% ":compare_versions" "Comparing:"
+    %print_debug% ":compare_versions" "%~1: %USER_MAJOR%.%USER_MINOR%.%USER_PATCH%.%USER_BUILD%"
+    %print_debug% ":compare_versions" "%~2: %VENDORED_MAJOR%.%VENDORED_MINOR%.%VENDORED_PATCH%.%VENDORED_BUILD%"
 
     setlocal enabledelayedexpansion
     if !%~1_MAJOR! GTR !%~2_MAJOR! (endlocal & exit /b  1)
@@ -233,23 +233,26 @@ exit /b
     setlocal enabledelayedexpansion
     if ERRORLEVEL 0 (
         :: compare the user git version against the vendored version
-        !lib_git! compare_versions USER VENDORED
+        %lib_git% compare_versions USER VENDORED
+        set result=!ERRORLEVEL!
+        %print_debug% ":compare_git_versions" "campare versions_result: !result!"
 
         :: use the user provided git if its version is greater than, or equal to the vendored git
-        if ERRORLEVEL 0 (
+        if !result! geq 0 (
             if exist "!test_dir:~0,-4!\cmd\git.exe" (
                 set "GIT_INSTALL_ROOT=!test_dir:~0,-4!"
             ) else (
                 set "GIT_INSTALL_ROOT=!test_dir!"
             )
         ) else (
-            %print_verbose% "Found old !GIT_VERSION_USER! in !test_dir!, but not using..."
+            %print_debug% ":compare_git_versions" "Found old !GIT_VERSION_USER! in !test_dir!, but not using..."
         )
     ) else (
         :: compare the user git version against the vendored version
         :: if the user provided git executable is not found
         IF ERRORLEVEL -255 IF NOT ERRORLEVEL -254 (
-            %print_verbose% "No git at "!git_executable!" found."
+        :: if not exist "%git_executable%" (
+            %print_debug% ":compare_git_versions" "No git at "%git_executable%" found."
             set test_dir=
         )
     )
@@ -272,6 +275,7 @@ exit /b
 :get_user_git_version
     :: get the version information for the user provided git binary
     %lib_git% read_version USER "%test_dir%" 2>nul
+    %print_debug% ":get_user_git_version" "get_user_git_version GIT_VERSION_USER: %GIT_VERSION_USER%"
     %lib_git% validate_version USER %GIT_VERSION_USER%
     exit  /b
 

From 864b85f2549d42776bf1b870dfd835dc6e9e79dd Mon Sep 17 00:00:00 2001
From: "Dax T. Games" <dtgames@kinggeek.org>
Date: Sat, 16 Apr 2022 11:14:22 -0400
Subject: [PATCH 2/4] CHANGELOG.md

---
 CHANGELOG.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e071c36..c845a35 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,8 +2,9 @@
 
 ## [Unreleased](https://ci.appveyor.com/project/MartiUK/cmder/build/artifacts) (2022-03-17)
 
-### Changes
+### Fixes
 
+- Fix find and use latest Git install always using vendored Git.
 - Fix Git prompt branch when using Git worktree.
 
 ## [1.3.19](https://github.com/cmderdev/cmder/tree/v1.3.19) (2022-01-15)

From 6d313853f3e3b6317b01867a654d4d2f73e88882 Mon Sep 17 00:00:00 2001
From: "Dax T. Games" <dtgames@kinggeek.org>
Date: Sat, 16 Apr 2022 11:35:19 -0400
Subject: [PATCH 3/4] only look at \cmd\git.exe for found git

---
 vendor/init.bat | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/vendor/init.bat b/vendor/init.bat
index 2ee6a59..440cb9a 100644
--- a/vendor/init.bat
+++ b/vendor/init.bat
@@ -234,7 +234,7 @@ if defined GIT_INSTALL_ROOT (
 %lib_git% validate_version VENDORED %GIT_VERSION_VENDORED%
 
 :: check if git is in path...
-for /F "delims=" %%F in ('where git.exe 2^>nul') do (
+for /F "delims=" %%F in ('where git.exe 2^>nul ^| find "\cmd\git.exe"') do (
     :: get the absolute path to the user provided git binary
     %lib_git% is_git_shim "%%~dpF"
     %lib_git% get_user_git_version

From 36e13027f64b0ec7969898c681fd85fd44eac91e Mon Sep 17 00:00:00 2001
From: "Dax T. Games" <dtgames@kinggeek.org>
Date: Sat, 16 Apr 2022 11:42:27 -0400
Subject: [PATCH 4/4] CHANGELOG.md

---
 CHANGELOG.md | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3339c00..c6ca48a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,8 @@
 
 ### Fixes
 
+- Fix find and use latest Git install always using vendored Git.
+- Fix using Git from vendored Git and other Git for Windows tools from other Git in path.
 - Remove setting `term=cygwin` in `init.bat` to fix random `ABCD` characters when using arrow keys in `vim`.
   - See: [Sometimes pressing on arrow keys prints symbols #1691](https://github.com/Maximus5/ConEmu/issues/169)
 - Fix Git prompt branch when using Git worktree.