roosterwijs/scripts/push.ps1

53 lines
2 KiB
PowerShell

# Veilig committen + pushen vanaf de dev-machine (Windows / PowerShell).
# Gebruik: .\scripts\push.ps1 "mijn commit-bericht"
# Lost het terugkerende push-probleem op: ruimt een achtergebleven
# index.lock op, commit alleen als er iets te committen valt, pusht, en
# CONTROLEERT daarna of de remote echt is bijgewerkt.
param([string]$Bericht = "")
$ErrorActionPreference = "Continue"
# Ga naar de repo-root (map boven dit script).
Set-Location (Split-Path $PSScriptRoot -Parent)
# 1) Eventuele achtergebleven lock weghalen (oorzaak van mislukte commits).
if (Test-Path ".git\index.lock") {
Remove-Item ".git\index.lock" -Force
Write-Host "Verwijderd: achtergebleven .git\index.lock" -ForegroundColor Yellow
}
# 2) Huidige branch bepalen.
$branch = (git rev-parse --abbrev-ref HEAD).Trim()
Write-Host "Branch: $branch"
# 3) Alles toevoegen en committen (alleen als er wijzigingen zijn).
git add -A
$status = git status --porcelain
if ([string]::IsNullOrWhiteSpace($status)) {
Write-Host "Geen wijzigingen om te committen." -ForegroundColor Yellow
} else {
if ([string]::IsNullOrWhiteSpace($Bericht)) {
$Bericht = "Update " + (Get-Date -Format "yyyy-MM-dd HH:mm")
}
git commit -m $Bericht
if ($LASTEXITCODE -ne 0) { Write-Host "Commit MISLUKT." -ForegroundColor Red; exit 1 }
}
# 4) Pushen.
git push origin $branch
$pushExit = $LASTEXITCODE
# 5) Controleren of de remote echt gelijk is aan lokaal.
git fetch origin 2>$null | Out-Null
$lokaal = (git rev-parse HEAD).Trim()
$remote = (git rev-parse "origin/$branch").Trim()
Write-Host ""
if ($pushExit -eq 0 -and $lokaal -eq $remote) {
Write-Host "OK - origin/$branch is bijgewerkt naar $($lokaal.Substring(0,8))." -ForegroundColor Green
Write-Host "Draai nu op de test-server: ./scripts/deploy.sh"
} else {
Write-Host "LET OP - de push is NIET doorgekomen." -ForegroundColor Red
Write-Host " lokaal : $lokaal"
Write-Host " origin : $remote"
Write-Host "Bekijk de git-uitvoer hierboven (vaak een sleutel-/authenticatieprobleem)." -ForegroundColor Red
exit 1
}