79 lines
4.0 KiB
PowerShell
79 lines
4.0 KiB
PowerShell
<#
|
||
Keeps a local clone in sync with Gitea (e.g. every 5 minutes via Windows Task Scheduler).
|
||
Safe by design: it only fast-forwards. Nothing happens if there are local uncommitted changes, if the
|
||
branch has no upstream, or if local and remote both have new commits (that needs a human).
|
||
Log: storage/logs/git-autosync.log (not committed).
|
||
|
||
Register (once per machine, PowerShell):
|
||
scripts\git-autosync.ps1 -Register # every 5 minutes, as the current user
|
||
scripts\git-autosync.ps1 -Unregister
|
||
Run once by hand:
|
||
scripts\git-autosync.ps1
|
||
#>
|
||
param(
|
||
[switch] $Register,
|
||
[switch] $Unregister,
|
||
[int] $Minutes = 5
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$repo = Split-Path -Parent $PSScriptRoot
|
||
$taskName = 'FeWo Stommer Git-Sync'
|
||
$log = Join-Path $repo 'storage\logs\git-autosync.log'
|
||
|
||
if ($Register) {
|
||
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$PSCommandPath`""
|
||
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes $Minutes)
|
||
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -MultipleInstances IgnoreNew
|
||
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Description "git pull --ff-only für $repo (scripts/git-autosync.ps1)" -Force | Out-Null
|
||
Write-Output "Aufgabe '$taskName' eingerichtet: alle $Minutes Minuten. Protokoll: $log"
|
||
exit 0
|
||
}
|
||
|
||
if ($Unregister) {
|
||
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
|
||
Write-Output "Aufgabe '$taskName' entfernt."
|
||
exit 0
|
||
}
|
||
|
||
function Write-Log([string] $message) {
|
||
New-Item -ItemType Directory -Force -Path (Split-Path $log) | Out-Null
|
||
Add-Content -Path $log -Value ("{0:yyyy-MM-dd HH:mm:ss} {1}" -f (Get-Date), $message) -Encoding utf8
|
||
}
|
||
|
||
function Invoke-Git([string[]] $arguments) {
|
||
$output = & git.exe -C $repo @arguments 2>&1
|
||
if ($LASTEXITCODE -ne 0) { throw "git $($arguments -join ' '): $output" }
|
||
return $output
|
||
}
|
||
|
||
try {
|
||
Invoke-Git @('fetch', '--quiet', '--prune') | Out-Null
|
||
|
||
$branch = (Invoke-Git @('rev-parse', '--abbrev-ref', 'HEAD')).Trim()
|
||
$upstream = & git.exe -C $repo rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>$null
|
||
if ($LASTEXITCODE -ne 0 -or -not $upstream) { Write-Log "übersprungen: Branch $branch hat keinen Upstream"; exit 0 }
|
||
|
||
$counts = (Invoke-Git @('rev-list', '--left-right', '--count', "HEAD...$upstream")).Trim() -split '\s+'
|
||
$ahead = [int] $counts[0]; $behind = [int] $counts[1]
|
||
if ($behind -eq 0) { exit 0 } # nothing new – no log entry
|
||
|
||
if ((Invoke-Git @('status', '--porcelain', '--untracked-files=no')) ) { Write-Log "übersprungen: lokale Änderungen in $branch ($behind neue Commits auf $upstream warten)"; exit 0 }
|
||
if ($ahead -gt 0) { Write-Log "nicht synchronisiert: $branch hat $ahead eigene und $behind neue Commits – bitte von Hand zusammenführen (git pull)"; exit 0 }
|
||
|
||
$before = (Invoke-Git @('rev-parse', 'HEAD')).Trim()
|
||
Invoke-Git @('merge', '--ff-only', '--quiet', $upstream) | Out-Null
|
||
$changed = Invoke-Git @('diff', '--name-only', $before, 'HEAD')
|
||
$hints = @()
|
||
if ($changed -match '^(package\.json|package-lock\.json)$') { $hints += 'npm ci && npm run build' }
|
||
if ($changed -match '^composer\.(json|lock)$') { $hints += 'composer install' }
|
||
if ($changed -match '^database/migrations/') { $hints += 'php artisan migrate' }
|
||
if ($changed -match '^resources/(themes|css|js)/') { $hints += 'npm run build (Theme/Assets geändert)' }
|
||
$summary = (Invoke-Git @('log', '--oneline', "$before..HEAD")) -join ' | '
|
||
Write-Log ("aktualisiert $branch um $behind Commit(s): $summary" + ($(if ($hints) { " → bitte ausführen: " + (($hints | Select-Object -Unique) -join '; ') } else { '' })))
|
||
}
|
||
catch {
|
||
Write-Log "Fehler: $($_.Exception.Message)"
|
||
exit 1
|
||
}
|