# Pushes this folder to the Gitea repo. # # Run from PowerShell inside the visitor-signin folder: # .\push-to-gitea.ps1 # If Windows blocks it: # powershell -ExecutionPolicy Bypass -File .\push-to-gitea.ps1 $Remote = 'https://gitea.hideawaygaming.com.au/jessikitty/visitor-signin.git' # git reports failure through its exit code, not as a PowerShell error, so every # call has to be checked. Without this the script reports success after a # rejected push, which is exactly what it used to do. function Invoke-Git { param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments) & git @Arguments return $LASTEXITCODE } function Fail($message) { Write-Host '' Write-Host $message -ForegroundColor Red exit 1 } if (-not (Test-Path 'package.json')) { Fail 'Run this from inside the visitor-signin folder.' } if (-not (Get-Command git -ErrorAction SilentlyContinue)) { Fail 'Git is not installed or not on PATH. Get it from https://git-scm.com/download/win' } # Keeps line endings sane between Windows and the Ubuntu docker host. git config --global core.autocrlf input | Out-Null if (-not (Test-Path '.git')) { Write-Host 'Setting up a new local repository...' if ((Invoke-Git init -b main) -ne 0) { Fail 'git init failed.' } } if ((git remote) -match '^origin$') { git remote set-url origin $Remote } else { git remote add origin $Remote } # ---------------------------------------------------------------- commit git add -A $pending = git status --porcelain if ($pending) { $message = Read-Host 'Describe this change (press enter for a dated default)' if (-not $message) { $message = "Update $(Get-Date -Format 'yyyy-MM-dd HH:mm')" } if ((Invoke-Git commit -m $message) -ne 0) { Fail 'git commit failed.' } Write-Host 'Committed.' -ForegroundColor Green } else { Write-Host 'No file changes to commit. Checking for anything unpushed...' -ForegroundColor Yellow } # ------------------------------------------------------- catch up, then push Write-Host 'Checking what is on the server...' if ((Invoke-Git fetch origin) -ne 0) { Fail 'Could not reach Gitea. Check the network and your sign in details.' } if (git ls-remote --heads origin main) { $behind = (git rev-list --count HEAD..origin/main 2>$null) if ($behind -and [int]$behind -gt 0) { Write-Host "The server has $behind commit(s) this folder does not. Replaying your work on top..." if ((Invoke-Git pull --rebase origin main) -ne 0) { Write-Host '' Write-Host 'The two histories could not be joined automatically.' -ForegroundColor Red Write-Host '' Write-Host 'See what is on the server that you do not have:' -ForegroundColor Yellow Write-Host ' git log --oneline HEAD..origin/main' Write-Host '' Write-Host 'If that is nothing you need, and this folder is the good copy:' -ForegroundColor Yellow Write-Host ' git push --force-with-lease origin main' exit 1 } } } if ((Invoke-Git push -u origin main) -ne 0) { Fail 'The push was rejected. Read the message above. Nothing was sent.' } Write-Host '' Write-Host 'Pushed successfully.' -ForegroundColor Green Write-Host 'https://gitea.hideawaygaming.com.au/jessikitty/visitor-signin'