cmder/vendor/lib/lib_git.cmd

283 lines
8.7 KiB
Batchfile
Raw Normal View History

@echo off
call "%~dp0lib_base.cmd"
call "%%~dp0lib_console.cmd"
set lib_git=call "%~dp0lib_git.cmd"
if "%~1" == "/h" (
%lib_base% help "%~0"
) else if "%1" neq "" (
call :%*
)
exit /b
:read_version
:::===============================================================================
2022-01-16 00:00:58 +08:00
:::read_version - Get the git.exe version
:::.
:::include:
:::.
::: call "lib_git.cmd"
:::.
:::usage:
:::.
::: %lib_git% read_version "[dir_path]"
:::.
:::required:
:::.
::: [GIT SCOPE] <in> USER | VENDORED
::: [GIT PATH] <in> Fully qualified path to the Git command root.
:::.
:::output:
:::.
::: GIT_VERSION_[GIT SCOPE] <out> Env variable containing Git semantic version string
:::-------------------------------------------------------------------------------
:: clear the variables
set GIT_VERSION_%~1=
:: set the executable path
set "git_executable=%~2\git.exe"
2020-12-08 03:41:11 +08:00
%print_debug% :read_version "Env Var - git_executable=%git_executable%"
:: check if the executable actually exists
2018-04-13 09:39:52 +08:00
if not exist "%git_executable%" (
2020-12-08 03:41:11 +08:00
%print_debug% :read_version "%git_executable% does not exist."
2018-04-13 09:39:52 +08:00
exit /b -255
)
:: get the git version in the provided directory
2020-09-13 20:27:10 +08:00
"%git_executable%" --version > "%temp%\git_version.txt"
2019-10-14 03:47:25 +08:00
setlocal enabledelayedexpansion
for /F "tokens=1,2,3 usebackq" %%A in (`type "%temp%\git_version.txt" 2^>nul`) do (
2018-04-13 09:39:52 +08:00
if /i "%%A %%B" == "git version" (
2018-09-16 23:18:10 +08:00
set "GIT_VERSION=%%C"
2018-04-13 09:39:52 +08:00
) else (
2022-01-16 00:00:58 +08:00
echo "'git --version' returned an improper version string!"
%print_debug% :read_version "returned string: '%%A %%B %%C' by executable path: %git_executable%"
2018-04-13 09:39:52 +08:00
pause
exit /b
)
)
2020-12-08 03:41:11 +08:00
endlocal & set "GIT_VERSION_%~1=%GIT_VERSION%" & %print_debug% :read_version "Env Var - GIT_VERSION_%~1=%GIT_VERSION%"
exit /b
:parse_version
:::===============================================================================
:::parse_version - Parse semantic version string 'x.x.x.x' and return the pieces
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% parse_version "[VERSION]"
:::.
:::required:
:::.
::: [SCOPE] <in> USER | VENDORED
::: [VERSION] <in> Semantic version String. Ex: 1.2.3.4
:::.
:::output:
:::.
::: [SCOPE]_MAJOR <out> Scoped Major version.
::: [SCOPE]_MINOR <out> Scoped Minor version.
::: [SCOPE]_PATCH <out> Scoped Patch version.
::: [SCOPE]_BUILD <out> Scoped Build version.
:::-------------------------------------------------------------------------------
:: process a `x.x.x.xxxx.x` formatted string
2020-12-08 03:41:11 +08:00
%print_debug% :parse_version "ARGV[1]=%~1, ARGV[2]=%~2"
2019-10-14 03:47:25 +08:00
setlocal enabledelayedexpansion
for /F "tokens=1-3* delims=.,-" %%A in ("%2") do (
set "%~1_MAJOR=%%A"
set "%~1_MINOR=%%B"
set "%~1_PATCH=%%C"
set "%~1_BUILD=%%D"
)
2019-10-14 03:47:25 +08:00
REM endlocal & set "%~1_MAJOR=!%~1_MAJOR!" & set "%~1_MINOR=!%~1_MINOR!" & set "%~1_PATCH=!%~1_PATCH!" & set "%~1_BUILD=!%~1_BUILD!"
if "%~1" == "VENDORED" (
endlocal & set "%~1_MAJOR=%VENDORED_MAJOR%" & set "%~1_MINOR=%VENDORED_MINOR%" & set "%~1_PATCH=%VENDORED_PATCH%" & set "%~1_BUILD=%VENDORED_BUILD%"
2019-10-14 03:47:25 +08:00
) else (
endlocal & set "%~1_MAJOR=%USER_MAJOR%" & set "%~1_MINOR=%USER_MINOR%" & set "%~1_PATCH=%USER_PATCH%" & set "%~1_BUILD=%USER_BUILD%"
2019-10-14 03:47:25 +08:00
)
exit /b
2019-10-14 03:47:25 +08:00
:endlocal_set_git_version
:validate_version
:::===============================================================================
:::validate_version - Validate semantic version string 'x.x.x.x'.
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% validate_version [SCOPE] [VERSION]
:::.
:::required:
:::.
::: [SCOPE] <in> Example: USER | VENDORED
::: [VERSION] <in> Semantic version String. Ex: 1.2.3.4
:::-------------------------------------------------------------------------------
:: now parse the version information into the corresponding variables
2020-12-08 03:41:11 +08:00
%print_debug% :validate_version "ARGV[1]=%~1, ARGV[2]=%~2"
2019-10-14 03:47:25 +08:00
call :parse_version %~1 %~2
:: ... and maybe display it, for debugging purposes.
2020-12-08 03:41:11 +08:00
REM %print_debug% :validate_version "Found Git Version for %~1: !%~1_MAJOR!.!%~1_MINOR!.!%~1_PATCH!.!%~1_BUILD!"
2019-10-14 03:47:25 +08:00
if "%~1" == "VENDORED" (
%print_debug% :validate_version "Found Git Version for %~1: %VENDORED_MAJOR%.%VENDORED_MINOR%.%VENDORED_PATCH%.%VENDORED_BUILD%"
2019-10-14 03:47:25 +08:00
) else (
%print_debug% :validate_version "Found Git Version for %~1: %USER_MAJOR%.%USER_MINOR%.%USER_PATCH%.%USER_BUILD%"
2019-10-14 03:47:25 +08:00
)
exit /b
:compare_versions
:::===============================================================================
:::compare_version - Compare semantic versions return latest version.
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% validate_version [SCOPE1] [SCOPE2]
:::.
:::required:
:::.
::: [SCOPE1] <in> Example: USER
::: [SCOPE2] <in> Example: VENDOR
:::-------------------------------------------------------------------------------
:: 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.
2022-04-16 22:51:26 +08:00
%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%"
2019-10-14 03:47:25 +08:00
setlocal enabledelayedexpansion
if !%~1_MAJOR! GTR !%~2_MAJOR! (endlocal & exit /b 1)
if !%~1_MAJOR! LSS !%~2_MAJOR! (endlocal & exit /b -1)
2019-10-14 03:47:25 +08:00
if !%~1_MINOR! GTR !%~2_MINOR! (endlocal & exit /b 1)
if !%~1_MINOR! LSS !%~2_MINOR! (endlocal & exit /b -1)
2019-10-14 03:47:25 +08:00
if !%~1_PATCH! GTR !%~2_PATCH! (endlocal & exit /b 1)
if !%~1_PATCH! LSS !%~2_PATCH! (endlocal & exit /b -1)
2019-10-14 03:47:25 +08:00
if !%~1_BUILD! GTR !%~2_BUILD! (endlocal & exit /b 1)
if !%~1_BUILD! LSS !%~2_BUILD! (endlocal & exit /b -1)
:: looks like we have the same versions.
2019-10-14 03:47:25 +08:00
endlocal & exit /b 0
2020-04-06 20:20:26 +08:00
:::===============================================================================
:::is_git_shim
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% is_git_shim [filepath]
:::.
:::required:
:::.
::: [filepath] <in>
:::-------------------------------------------------------------------------------
:is_git_shim
pushd "%~1"
:: check if there's shim - and if yes follow the path
setlocal enabledelayedexpansion
if exist git.shim (
for /F "tokens=2 delims== " %%I in (git.shim) do (
pushd %%~dpI
set "test_dir=!CD!"
popd
)
) else (
set "test_dir=!CD!"
)
endlocal & set "test_dir=%test_dir%"
popd
exit /b
:::===============================================================================
:::compare_git_versions
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% compare_git_versions
:::-------------------------------------------------------------------------------
:compare_git_versions
2020-08-25 08:27:56 +08:00
setlocal enabledelayedexpansion
if ERRORLEVEL 0 (
2020-04-06 20:20:26 +08:00
:: compare the user git version against the vendored version
2022-04-16 22:51:26 +08:00
%lib_git% compare_versions USER VENDORED
set result=!ERRORLEVEL!
%print_debug% ":compare_git_versions" "campare versions_result: !result!"
2020-04-06 20:20:26 +08:00
:: use the user provided git if its version is greater than, or equal to the vendored git
2022-04-16 22:51:26 +08:00
if !result! geq 0 (
2020-08-23 20:42:58 +08:00
if exist "!test_dir:~0,-4!\cmd\git.exe" (
set "GIT_INSTALL_ROOT=!test_dir:~0,-4!"
) else (
2020-08-23 20:42:58 +08:00
set "GIT_INSTALL_ROOT=!test_dir!"
)
2020-04-06 20:20:26 +08:00
) else (
2022-04-16 22:51:26 +08:00
%print_debug% ":compare_git_versions" "Found old !GIT_VERSION_USER! in !test_dir!, but not using..."
2020-04-06 20:20:26 +08:00
)
) 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 (
2022-04-16 22:51:26 +08:00
:: if not exist "%git_executable%" (
2024-07-27 08:24:12 +08:00
%print_debug% ":compare_git_versions" "No git at '%git_executable%' found."
2020-04-06 20:20:26 +08:00
set test_dir=
)
)
2020-08-25 08:27:56 +08:00
endlocal && set "GIT_INSTALL_ROOT=%GIT_INSTALL_ROOT%" && set test_dir=
2020-08-23 20:42:58 +08:00
2020-04-06 20:20:26 +08:00
exit /b
:::===============================================================================
:::get_user_git_version - get the version information for the user provided git binary
:::.
:::include:
:::.
::: call "$0"
:::.
:::usage:
:::.
::: %lib_git% get_user_git_version
:::-------------------------------------------------------------------------------
:get_user_git_version
:: get the version information for the user provided git binary
2020-09-13 20:27:10 +08:00
%lib_git% read_version USER "%test_dir%" 2>nul
2022-04-16 22:51:26 +08:00
%print_debug% ":get_user_git_version" "get_user_git_version GIT_VERSION_USER: %GIT_VERSION_USER%"
2020-04-06 20:20:26 +08:00
%lib_git% validate_version USER %GIT_VERSION_USER%
exit /b