param( [string]$RemoteName = "origin", [string]$Branch = "main" ) $ErrorActionPreference = "Stop" $projectRoot = Split-Path -Parent $PSScriptRoot $envFile = Join-Path $projectRoot ".env.local" if (-not (Test-Path -LiteralPath $envFile)) { throw "Missing .env.local with FORGEJO_URL, FORGEJO_USER, FORGEJO_TOKEN, and FORGEJO_REPO." } $values = @{} Get-Content -LiteralPath $envFile | ForEach-Object { if ($_ -match "^\s*([^#=]+?)\s*=\s*(.*)\s*$") { $values[$matches[1]] = $matches[2] } } foreach ($name in @("FORGEJO_URL", "FORGEJO_USER", "FORGEJO_TOKEN", "FORGEJO_REPO")) { if (-not $values.ContainsKey($name) -or [string]::IsNullOrWhiteSpace($values[$name])) { throw "Missing $name in .env.local." } } $repoUrl = "$($values.FORGEJO_URL.TrimEnd('/'))/$($values.FORGEJO_REPO).git" $remoteUrl = git -C $projectRoot remote get-url $RemoteName 2>$null if ($LASTEXITCODE -ne 0) { git -C $projectRoot remote add $RemoteName $repoUrl } elseif ($remoteUrl -ne $repoUrl) { git -C $projectRoot remote set-url $RemoteName $repoUrl } $pair = "$($values.FORGEJO_USER):$($values.FORGEJO_TOKEN)" $auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($pair)) git -C $projectRoot -c "http.extraHeader=Authorization: Basic $auth" push $RemoteName $Branch