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
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 "EXTRACT_DIR=%SCRIPT_DIR%gitportable"
set "GIT_ARCHIVE=PortableGit.7z.exe"
set "API_URL=https://api.github.com/repos/git-for-windows/git/releases/latest"
set "LOG_FILE=%SCRIPT_DIR%git_setup.log"
:: 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
if %errorlevel%==0 (
echo Git is already installed and available in PATH.
goto :EOF
if "%FORCE_DOWNLOAD%"=="0" (
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%\" (
echo 'gitportable' folder already exists. Skipping download.
goto :EOF
echo 'gitportable' folder already exists.
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
echo Fetching latest Portable Git release info...
for /f "usebackq delims=" %%i in (`powershell -NoProfile -Command ^
"(Invoke-RestMethod -Uri '%API_URL%').assets | Where-Object { $_.name -like '*64-bit.7z.exe' } | Select-Object -First 1 -ExpandProperty browser_download_url"`) do (
set "GIT_URL=%%i"
:: Download Git
echo Downloading Git...
powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%GIT_URL%' -OutFile '%SCRIPT_DIR%%GIT_ARCHIVE%'"
if %errorlevel% neq 0 (
echo Error: Download failed.
goto :done
)
echo Download completed.
if not defined GIT_URL (
echo Failed to find download URL from GitHub API.
exit /b 1
:: Clean extraction directory if forced
if "%FORCE_DOWNLOAD%"=="1" if exist "%EXTRACT_DIR%\" (
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
echo Preparing extraction folder...
mkdir "%EXTRACT_DIR%"
if %errorlevel% neq 0 (
echo Error: Failed to create extraction directory.
goto :done
)
:: Extract Portable Git
echo Extracting Portable Git...
"%SCRIPT_DIR%%GIT_ARCHIVE%" -o"%EXTRACT_DIR%" -y
:: Extract Git silently
echo Extracting Git...
"%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
echo Cleaning up archive file...
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%
pause
echo.
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