Public Access
111 lines
4.5 KiB
PowerShell
111 lines
4.5 KiB
PowerShell
# 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 is called directly and $LASTEXITCODE checked straight afterwards. Wrapping
|
|
# it in a function does not work: a PowerShell function returns everything written
|
|
# to the output stream, so the caller receives git's console output as well as the
|
|
# exit code, and comparing that array against 0 reports failure every time.
|
|
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'
|
|
}
|
|
|
|
git config --global core.autocrlf input | Out-Null
|
|
|
|
# git refuses to commit without an identity, and says so in a way that is easy to
|
|
# miss among its other output.
|
|
$who = git config user.email
|
|
if (-not $who) { $who = git config --global user.email }
|
|
if (-not $who) {
|
|
Write-Host 'Git does not know who you are. Set that once:' -ForegroundColor Yellow
|
|
Write-Host ' git config --global user.email "you@example.com"'
|
|
Write-Host ' git config --global user.name "Your Name"'
|
|
Fail 'Nothing was committed.'
|
|
}
|
|
|
|
if (-not (Test-Path '.git')) {
|
|
Write-Host 'Setting up a new local repository...'
|
|
git init -b main
|
|
if ($LASTEXITCODE -ne 0) { Fail 'git init failed.' }
|
|
}
|
|
|
|
if ((git remote) -match '^origin$') { git remote set-url origin $Remote }
|
|
else { git remote add origin $Remote }
|
|
|
|
# Whatever branch is checked out, not a hard-coded one. Pushing 'main' while the
|
|
# work sits on 'deploy' reports "Everything up-to-date" and sends nothing.
|
|
$branch = (git rev-parse --abbrev-ref HEAD).Trim()
|
|
if (-not $branch -or $branch -eq 'HEAD') { Fail 'No branch is checked out here.' }
|
|
Write-Host "Branch: $branch" -ForegroundColor Cyan
|
|
|
|
# ------------------------------------------- an unfinished rebase blocks everything
|
|
|
|
$gitDir = git rev-parse --git-dir 2>$null
|
|
if ($gitDir) {
|
|
$stuck = @('rebase-merge', 'rebase-apply', 'MERGE_HEAD', 'CHERRY_PICK_HEAD') |
|
|
Where-Object { Test-Path (Join-Path $gitDir $_) }
|
|
if ($stuck) {
|
|
Write-Host ''
|
|
Write-Host 'There is an unfinished rebase or merge here.' -ForegroundColor Red
|
|
Write-Host ' git rebase --abort throw it away, back to how things were'
|
|
Write-Host ' git status see which files need attention'
|
|
Write-Host ' git rebase --continue after fixing those files'
|
|
Fail 'Nothing was done.'
|
|
}
|
|
}
|
|
|
|
# ---------------------------------------------------------------- commit
|
|
|
|
git add -A
|
|
if (git status --porcelain) {
|
|
$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')" }
|
|
git commit -m $message
|
|
if ($LASTEXITCODE -ne 0) { Fail 'git commit failed. The message above says why.' }
|
|
Write-Host 'Committed.' -ForegroundColor Green
|
|
} else {
|
|
Write-Host 'Nothing new to commit.' -ForegroundColor Yellow
|
|
}
|
|
|
|
# ------------------------------------------------------- catch up, then push
|
|
|
|
git fetch origin
|
|
if ($LASTEXITCODE -ne 0) { Fail 'Could not reach Gitea. Check the network and your sign in details.' }
|
|
|
|
if (git ls-remote --heads origin $branch) {
|
|
$behind = git rev-list --count "HEAD..origin/$branch" 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..."
|
|
git pull --rebase origin $branch
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host ''
|
|
Write-Host 'The two histories could not be joined automatically.' -ForegroundColor Red
|
|
Write-Host " git log --oneline HEAD..origin/$branch what is on the server"
|
|
Write-Host " git push --force-with-lease origin $branch if this folder is the good copy"
|
|
Fail 'Nothing was sent.'
|
|
}
|
|
}
|
|
} else {
|
|
Write-Host "Branch '$branch' is not on the server yet; it will be created."
|
|
}
|
|
|
|
git push -u origin $branch
|
|
if ($LASTEXITCODE -ne 0) { Fail 'The push was rejected. The message above says why. Nothing was sent.' }
|
|
|
|
Write-Host ''
|
|
Write-Host "Pushed $branch successfully." -ForegroundColor Green
|
|
git log --oneline -1
|
|
Write-Host 'https://gitea.hideawaygaming.com.au/jessikitty/visitor-signin'
|