Ace-Step-Munk / check_update.bat
OnyxlMunkey's picture
Add ACE-Step 1.5 Docker app
e961681
@echo off
REM Git Update Check Utility
REM This script checks for updates from GitHub and optionally updates the repository
setlocal enabledelayedexpansion
REM Configuration
set TIMEOUT_SECONDS=10
set GIT_PORTABLE_PATH=%~dp0PortableGit\bin\git.exe
set GIT_PATH=
set REPO_PATH=%~dp0
set PROXY_CONFIG_FILE=%~dp0proxy_config.txt
echo ========================================
echo ACE-Step Update Check
echo ========================================
echo.
REM Check for Git: first try PortableGit, then system Git
if exist "%GIT_PORTABLE_PATH%" (
set "GIT_PATH=%GIT_PORTABLE_PATH%"
echo [Git] Using PortableGit
) else (
REM Try to find git in system PATH
where git >nul 2>&1
if !ERRORLEVEL! EQU 0 (
for /f "tokens=*" %%i in ('where git 2^>nul') do (
if not defined GIT_PATH set "GIT_PATH=%%i"
)
echo [Git] Using system Git: !GIT_PATH!
) else (
echo [Error] Git not found.
echo - PortableGit not found at: %GIT_PORTABLE_PATH%
echo - System Git not found in PATH
echo.
echo Please either:
echo 1. Install PortableGit in the PortableGit folder, or
echo 2. Install Git and add it to your system PATH
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
)
echo.
REM Check if this is a git repository
cd /d "%REPO_PATH%"
"!GIT_PATH!" rev-parse --git-dir >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo [Error] Not a git repository.
echo This folder does not appear to be a git repository.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
REM Load proxy configuration if exists
set PROXY_ENABLED=0
set PROXY_URL=
if exist "%PROXY_CONFIG_FILE%" (
for /f "usebackq tokens=1,* delims==" %%a in ("%PROXY_CONFIG_FILE%") do (
if /i "%%a"=="PROXY_ENABLED" set PROXY_ENABLED=%%b
if /i "%%a"=="PROXY_URL" set PROXY_URL=%%b
)
if "!PROXY_ENABLED!"=="1" (
if not "!PROXY_URL!"=="" (
echo [Proxy] Using proxy server: !PROXY_URL!
"!GIT_PATH!" config --local http.proxy "!PROXY_URL!"
"!GIT_PATH!" config --local https.proxy "!PROXY_URL!"
echo.
)
)
)
echo [1/4] Checking current version...
REM Get current branch
for /f "tokens=*" %%i in ('"!GIT_PATH!" rev-parse --abbrev-ref HEAD 2^>nul') do set CURRENT_BRANCH=%%i
if "%CURRENT_BRANCH%"=="" set CURRENT_BRANCH=main
REM Get current commit
for /f "tokens=*" %%i in ('"!GIT_PATH!" rev-parse --short HEAD 2^>nul') do set CURRENT_COMMIT=%%i
echo Branch: %CURRENT_BRANCH%
echo Commit: %CURRENT_COMMIT%
echo.
echo [2/4] Checking for updates (timeout: %TIMEOUT_SECONDS%s)...
echo Connecting to GitHub...
:FetchRetry
REM Fetch remote with timeout (stderr visible so "Bad credentials" etc. are shown)
set FETCH_SUCCESS=0
"!GIT_PATH!" fetch origin --quiet
if %ERRORLEVEL% EQU 0 (
set FETCH_SUCCESS=1
)
if !FETCH_SUCCESS! EQU 1 goto :FetchDone
REM Try with timeout using a temp marker file
set TEMP_MARKER=%TEMP%\acestep_git_fetch_%RANDOM%.tmp
REM Start fetch in background
set "FETCH_CMD=!GIT_PATH! fetch origin --quiet"
start /b "" cmd /c "!FETCH_CMD! >nul 2>&1 && echo SUCCESS > "!TEMP_MARKER!""
REM Wait with timeout
set /a COUNTER=0
:WaitLoop
if exist "!TEMP_MARKER!" (
set FETCH_SUCCESS=1
del "!TEMP_MARKER!" >nul 2>&1
goto :FetchDone
)
timeout /t 1 /nobreak >nul
set /a COUNTER+=1
if !COUNTER! LSS %TIMEOUT_SECONDS% goto :WaitLoop
REM Timeout reached
echo [Timeout] Could not connect to GitHub within %TIMEOUT_SECONDS% seconds.
:FetchDone
if %FETCH_SUCCESS% EQU 0 (
echo [Failed] Could not fetch from GitHub.
echo If the error above is "Bad credentials", update or clear stored Git credentials.
echo This repo is public and does not require login: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git
echo Otherwise check your internet connection or proxy.
echo.
REM Ask if user wants to configure proxy
set /p PROXY_CHOICE="Do you want to configure a proxy server to retry? (Y/N): "
if /i "!PROXY_CHOICE!"=="Y" (
call :ConfigureProxy
if !ERRORLEVEL! EQU 0 (
echo.
echo [Proxy] Retrying with proxy configuration...
echo.
goto :FetchRetry
)
)
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 2
)
echo [Success] Fetched latest information from GitHub.
echo.
echo [3/4] Comparing versions...
REM Get remote commit
for /f "tokens=*" %%i in ('"!GIT_PATH!" rev-parse --short origin/%CURRENT_BRANCH% 2^>nul') do set REMOTE_COMMIT=%%i
if "%REMOTE_COMMIT%"=="" (
echo [Warning] Remote branch 'origin/%CURRENT_BRANCH%' not found.
echo.
echo Your current branch '%CURRENT_BRANCH%' does not exist on the remote repository.
echo This might be a local development branch.
echo.
REM Try to get main branch instead
set FALLBACK_BRANCH=main
echo Checking main branch instead...
for /f "tokens=*" %%i in ('"!GIT_PATH!" rev-parse --short origin/!FALLBACK_BRANCH! 2^>nul') do set REMOTE_COMMIT=%%i
if "!REMOTE_COMMIT!"=="" (
echo [Error] Could not find remote main branch either.
echo Please ensure you are connected to the correct repository.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
echo Found main branch: !REMOTE_COMMIT!
echo.
echo Recommendation: Switch to main branch to check for official updates.
echo Command: git checkout main
echo.
set /p SWITCH_BRANCH="Do you want to switch to main branch now? (Y/N): "
if /i "!SWITCH_BRANCH!"=="Y" (
echo.
echo Switching to main branch...
"!GIT_PATH!" checkout main
if !ERRORLEVEL! EQU 0 (
echo [Success] Switched to main branch.
echo.
echo Please run this script again to check for updates.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
) else (
echo [Error] Failed to switch branch.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
) else (
echo.
echo Staying on branch '%CURRENT_BRANCH%'. No update performed.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
)
)
echo Local: %CURRENT_COMMIT%
echo Remote: %REMOTE_COMMIT%
echo.
REM Compare commits
if "%CURRENT_COMMIT%"=="%REMOTE_COMMIT%" (
echo [4/4] Result: Already up to date!
echo You have the latest version.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
) else (
echo [4/4] Result: Update available!
REM Check if local is behind remote
"!GIT_PATH!" merge-base --is-ancestor HEAD origin/%CURRENT_BRANCH% 2>nul
if !ERRORLEVEL! EQU 0 (
echo A new version is available on GitHub.
echo.
REM Show commits behind (do not suppress stderr so ref/encoding errors are visible)
echo New commits:
"!GIT_PATH!" --no-pager log --oneline --graph --decorate "HEAD..origin/!CURRENT_BRANCH!"
if !ERRORLEVEL! NEQ 0 (
echo [Could not show commit log. Check branch name and network.]
)
echo.
REM Ask if user wants to update
set /p UPDATE_CHOICE="Do you want to update now? (Y/N): "
if /i "!UPDATE_CHOICE!"=="Y" (
echo.
echo Updating...
REM First, refresh the index to avoid false positives from line ending changes
"!GIT_PATH!" update-index --refresh >nul 2>&1
REM Check for uncommitted changes
"!GIT_PATH!" diff-index --quiet HEAD -- 2>nul
if !ERRORLEVEL! NEQ 0 (
echo.
echo [Info] Checking for potential conflicts...
REM Get list of locally modified files
set TEMP_LOCAL_CHANGES=%TEMP%\acestep_local_changes_%RANDOM%.txt
"!GIT_PATH!" diff --name-only HEAD 2>nul > "!TEMP_LOCAL_CHANGES!"
REM Get list of files changed in remote
set TEMP_REMOTE_CHANGES=%TEMP%\acestep_remote_changes_%RANDOM%.txt
"!GIT_PATH!" diff --name-only HEAD..origin/%CURRENT_BRANCH% 2>nul > "!TEMP_REMOTE_CHANGES!"
REM Check for conflicts
set HAS_CONFLICTS=0
REM Use wmic to get locale-independent date/time format (YYYYMMDDHHMMSS)
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value 2^>nul') do set "DATETIME=%%a"
set "BACKUP_DIR=%~dp0.update_backup_!DATETIME:~0,8!_!DATETIME:~8,6!"
REM Find conflicting files
for /f "usebackq delims=" %%a in ("!TEMP_LOCAL_CHANGES!") do (
findstr /x /c:"%%a" "!TEMP_REMOTE_CHANGES!" >nul 2>&1
if !ERRORLEVEL! EQU 0 (
REM Found a conflict
set HAS_CONFLICTS=1
REM Create backup directory if not exists
if not exist "!BACKUP_DIR!" (
mkdir "!BACKUP_DIR!"
echo.
echo [Backup] Creating backup directory: !BACKUP_DIR!
)
REM Backup the file
echo [Backup] Backing up: %%a
set FILE_PATH=%%a
set FILE_DIR=
for %%i in ("!FILE_PATH!") do set FILE_DIR=%%~dpi
REM Create subdirectories in backup if needed
if not "!FILE_DIR!"=="" (
if not "!FILE_DIR!"=="." (
if not exist "!BACKUP_DIR!\!FILE_DIR!" (
mkdir "!BACKUP_DIR!\!FILE_DIR!" 2>nul
)
)
)
REM Copy file to backup
copy "%%a" "!BACKUP_DIR!\%%a" >nul 2>&1
)
)
REM Clean up temp files
del "!TEMP_LOCAL_CHANGES!" >nul 2>&1
del "!TEMP_REMOTE_CHANGES!" >nul 2>&1
if !HAS_CONFLICTS! EQU 1 (
echo.
echo ========================================
echo [Warning] Potential conflicts detected!
echo ========================================
echo.
echo Your modified files may conflict with remote updates.
echo Your changes have been backed up to:
echo !BACKUP_DIR!
echo.
echo Update will restore these files to the remote version.
echo You can manually merge your changes later.
echo.
set /p CONFLICT_CHOICE="Continue with update? (Y/N): "
if /i "!CONFLICT_CHOICE!"=="Y" (
echo.
echo [Restore] Proceeding with update...
echo [Restore] Files will be updated to remote version.
) else (
echo.
echo Update cancelled.
echo Your backup remains at: !BACKUP_DIR!
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
)
) else (
echo.
echo [Info] No conflicts detected. Safe to stash and update.
echo.
set /p STASH_CHOICE="Stash your changes and continue? (Y/N): "
if /i "!STASH_CHOICE!"=="Y" (
echo Stashing changes...
"!GIT_PATH!" stash push -m "Auto-stash before update - %date% %time%"
) else (
echo.
echo Update cancelled.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
)
)
)
REM Check for untracked files that could be overwritten
set STASHED_UNTRACKED=0
set TEMP_UNTRACKED=%TEMP%\acestep_untracked_%RANDOM%.txt
"!GIT_PATH!" ls-files --others --exclude-standard 2>nul > "!TEMP_UNTRACKED!"
REM Check if there are any untracked files
set HAS_UNTRACKED=0
for /f "usebackq delims=" %%u in ("!TEMP_UNTRACKED!") do set HAS_UNTRACKED=1
if !HAS_UNTRACKED! EQU 1 (
REM Get files added in remote
set TEMP_REMOTE_ADDED=%TEMP%\acestep_remote_added_%RANDOM%.txt
"!GIT_PATH!" diff --name-only --diff-filter=A HEAD..origin/%CURRENT_BRANCH% 2>nul > "!TEMP_REMOTE_ADDED!"
set HAS_UNTRACKED_CONFLICTS=0
for /f "usebackq delims=" %%u in ("!TEMP_UNTRACKED!") do (
findstr /x /c:"%%u" "!TEMP_REMOTE_ADDED!" >nul 2>&1
if !ERRORLEVEL! EQU 0 (
if !HAS_UNTRACKED_CONFLICTS! EQU 0 (
echo.
echo ========================================
echo [Warning] Untracked files conflict with update!
echo ========================================
echo.
echo The following untracked files would be overwritten:
)
set HAS_UNTRACKED_CONFLICTS=1
echo %%u
)
)
del "!TEMP_REMOTE_ADDED!" >nul 2>&1
if !HAS_UNTRACKED_CONFLICTS! EQU 1 (
echo.
set /p STASH_UNTRACKED_CHOICE="Stash untracked files before updating? (Y/N): "
if /i "!STASH_UNTRACKED_CHOICE!"=="Y" (
echo Stashing all changes including untracked files...
"!GIT_PATH!" stash push --include-untracked -m "pre-update-%RANDOM%" >nul 2>&1
if !ERRORLEVEL! EQU 0 (
set STASHED_UNTRACKED=1
echo [Stash] Changes stashed successfully.
) else (
echo [Error] Failed to stash changes. Update aborted.
del "!TEMP_UNTRACKED!" >nul 2>&1
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
) else (
echo.
echo Update cancelled. Please move or remove the conflicting files manually.
del "!TEMP_UNTRACKED!" >nul 2>&1
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
echo.
)
)
del "!TEMP_UNTRACKED!" >nul 2>&1
REM Pull changes
echo Pulling latest changes...
REM Force update by resetting to remote branch (discards any remaining local changes)
"!GIT_PATH!" reset --hard origin/%CURRENT_BRANCH% >nul 2>&1
if !ERRORLEVEL! EQU 0 (
echo.
echo ========================================
echo Update completed successfully!
echo ========================================
echo.
REM Check if backup was created
if defined BACKUP_DIR (
if exist "!BACKUP_DIR!" (
echo [Important] Your modified files were backed up to:
echo !BACKUP_DIR!
echo.
echo To restore your changes:
echo 1. Run merge_config.bat to compare and merge files
echo 2. Or manually compare backup with new version
echo.
echo Backed up files:
set "BACKUP_DIR_DISPLAY=!BACKUP_DIR!"
for /f "delims=" %%f in ('dir /b /s "!BACKUP_DIR!\*.*" 2^>nul') do (
set "FILEPATH=%%f"
REM Use call to safely handle the string replacement
call set "FILEPATH=%%FILEPATH:!BACKUP_DIR_DISPLAY!\=%%"
echo - !FILEPATH!
)
echo.
)
)
if !STASHED_UNTRACKED! EQU 1 (
echo [Stash] Untracked files were stashed before the update.
echo To restore them: git stash pop
echo To discard them: git stash drop
echo.
echo Note: 'git stash pop' may produce merge conflicts if
echo the update modified the same files. Resolve manually.
echo.
)
echo Please restart the application to use the new version.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
) else (
echo.
echo [Error] Update failed.
echo Please check the error messages above.
if !STASHED_UNTRACKED! EQU 1 (
echo.
echo [Stash] Restoring stashed changes...
"!GIT_PATH!" stash pop >nul 2>&1
if !ERRORLEVEL! EQU 0 (
echo [Stash] Changes restored successfully.
) else (
echo [Stash] Could not auto-restore. Run 'git stash pop' manually.
)
)
REM If backup exists, mention it
if defined BACKUP_DIR (
if exist "!BACKUP_DIR!" (
echo.
echo Your backup is still available at: !BACKUP_DIR!
)
)
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 1
)
) else (
echo.
echo Update skipped.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
)
) else (
echo [Warning] Local version has diverged from remote.
echo This might be because you have local commits.
echo Please update manually or consult the documentation.
echo.
echo ========================================
echo Press any key to close...
echo ========================================
pause >nul
exit /b 0
)
)
REM ========================================
REM Function: ConfigureProxy
REM Configure proxy server for git
REM ========================================
:ConfigureProxy
echo.
echo ========================================
echo Proxy Server Configuration
echo ========================================
echo.
echo Please enter your proxy server URL.
echo.
echo Examples:
echo - HTTP proxy: http://127.0.0.1:7890
echo - HTTPS proxy: https://proxy.example.com:8080
echo - SOCKS5: socks5://127.0.0.1:1080
echo.
echo Leave empty to disable proxy.
echo.
set /p NEW_PROXY_URL="Proxy URL: "
if "!NEW_PROXY_URL!"=="" (
echo.
echo [Proxy] Disabling proxy...
REM Remove proxy configuration
"!GIT_PATH!" config --local --unset http.proxy 2>nul
"!GIT_PATH!" config --local --unset https.proxy 2>nul
REM Update config file
(
echo PROXY_ENABLED=0
echo PROXY_URL=
) > "%PROXY_CONFIG_FILE%"
echo [Proxy] Proxy disabled.
exit /b 0
) else (
echo.
echo [Proxy] Configuring proxy: !NEW_PROXY_URL!
REM Apply proxy to git
"!GIT_PATH!" config --local http.proxy "!NEW_PROXY_URL!"
"!GIT_PATH!" config --local https.proxy "!NEW_PROXY_URL!"
REM Save to config file
(
echo PROXY_ENABLED=1
echo PROXY_URL=!NEW_PROXY_URL!
) > "%PROXY_CONFIG_FILE%"
echo [Proxy] Proxy configured successfully.
echo [Proxy] Configuration saved to: %PROXY_CONFIG_FILE%
exit /b 0
)
endlocal