rewrote git installer . bat to be more smart, force install if the variable is defined, and log everything.

This commit is contained in:
Augustus
2025-07-02 03:47:41 +03:00
parent 9f8589a306
commit 63670451b2

View File

@@ -1,49 +1,104 @@
@echo off @echo off
setlocal enabledelayedexpansion setlocal
:: Define variables :: Set this to 1 to force download/install even if git is in PATH
set "FORCE_DOWNLOAD=1"
:: Git download URL
set "GIT_URL=https://github.com/git-for-windows/git/releases/download/v2.50.0.windows.2/PortableGit-2.50.0.2-64-bit.7z.exe"
:: Extract archive name from URL
for %%A in ("%GIT_URL%") do (
for %%B in (%%~nxA) do set "GIT_ARCHIVE=%%~B"
)
:: Paths
set "SCRIPT_DIR=%~dp0" set "SCRIPT_DIR=%~dp0"
set "EXTRACT_DIR=%SCRIPT_DIR%gitportable" set "EXTRACT_DIR=%SCRIPT_DIR%gitportable"
set "GIT_ARCHIVE=PortableGit.7z.exe" set "LOG_FILE=%SCRIPT_DIR%git_setup.log"
set "API_URL=https://api.github.com/repos/git-for-windows/git/releases/latest"
:: Check if Git is available in the PATH :: Redirect output to log
call :main >> "%LOG_FILE%" 2>&1
goto :end
:main
echo ====================================================
echo Script started at %DATE% %TIME%
echo ====================================================
echo.
:: Check if git is in PATH unless forced
where git >nul 2>nul where git >nul 2>nul
if %errorlevel%==0 ( if %errorlevel%==0 (
echo Git is already installed and available in PATH. if "%FORCE_DOWNLOAD%"=="0" (
goto :EOF echo Git found in PATH, skipping download and extraction.
goto :done
) else (
echo Git found in PATH but FORCE_DOWNLOAD=1, continuing anyway.
)
) )
:: Check if gitportable folder already exists :: Check if extraction folder exists
if exist "%EXTRACT_DIR%\" ( if exist "%EXTRACT_DIR%\" (
echo 'gitportable' folder already exists. Skipping download. echo 'gitportable' folder already exists.
goto :EOF if "%FORCE_DOWNLOAD%"=="0" (
echo Skipping download and extraction.
goto :done
) else (
echo FORCE_DOWNLOAD=1, continuing with download and extraction.
)
) )
:: Use PowerShell to get the download URL dynamically :: Download Git
echo Fetching latest Portable Git release info... echo Downloading Git...
for /f "usebackq delims=" %%i in (`powershell -NoProfile -Command ^ powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%GIT_URL%' -OutFile '%SCRIPT_DIR%%GIT_ARCHIVE%'"
"(Invoke-RestMethod -Uri '%API_URL%').assets | Where-Object { $_.name -like '*64-bit.7z.exe' } | Select-Object -First 1 -ExpandProperty browser_download_url"`) do ( if %errorlevel% neq 0 (
set "GIT_URL=%%i" echo Error: Download failed.
goto :done
) )
echo Download completed.
if not defined GIT_URL ( :: Clean extraction directory if forced
echo Failed to find download URL from GitHub API. if "%FORCE_DOWNLOAD%"=="1" if exist "%EXTRACT_DIR%\" (
exit /b 1 echo Removing existing '%EXTRACT_DIR%' folder before extraction...
rmdir /s /q "%EXTRACT_DIR%"
) )
echo Downloading Portable Git from: %GIT_URL%
powershell -NoProfile -Command "Invoke-WebRequest -Uri '%GIT_URL%' -OutFile '%SCRIPT_DIR%%GIT_ARCHIVE%'"
:: Create extraction directory :: Create extraction directory
echo Preparing extraction folder...
mkdir "%EXTRACT_DIR%" mkdir "%EXTRACT_DIR%"
if %errorlevel% neq 0 (
echo Error: Failed to create extraction directory.
goto :done
)
:: Extract Portable Git :: Extract Git silently
echo Extracting Portable Git... echo Extracting Git...
"%SCRIPT_DIR%%GIT_ARCHIVE%" -o"%EXTRACT_DIR%" -y "%SCRIPT_DIR%%GIT_ARCHIVE%" -o"%EXTRACT_DIR%" -y >nul 2>&1
if %errorlevel% neq 0 (
echo Error: Extraction failed.
goto :done
)
echo Extraction completed.
:: Cleanup :: Cleanup
echo Cleaning up archive file...
del "%SCRIPT_DIR%%GIT_ARCHIVE%" del "%SCRIPT_DIR%%GIT_ARCHIVE%"
if %errorlevel% neq 0 (
echo Warning: Failed to delete archive file.
)
echo Done. Portable Git is ready in: %EXTRACT_DIR% echo.
pause echo Git Portable is ready in: %EXTRACT_DIR%
echo ====================================================
echo Script ended at %DATE% %TIME%
echo ====================================================
:done
exit /b
:end
echo.
echo Log saved to: %LOG_FILE%
echo Press Enter to close this window...
pause >nul