Files

80 lines
2.0 KiB
PowerShell
Raw Permalink Normal View History

2026-09-11 14:35:39 +03:00
param(
[string]$Remote = "gitea",
[string]$MainBranch = "main"
)
$ErrorActionPreference = "Stop"
# Verify remote exists.
$remotes = git remote
if ($remotes -notcontains $Remote) {
throw "Git remote '$Remote' does not exist. Available remotes: $($remotes -join ', ')"
}
Write-Host "Using remote: $Remote"
Write-Host "Canonical branch: $MainBranch"
# Update remote references.
git fetch $Remote --prune
# Update local main from the selected remote.
git switch $MainBranch
git pull --ff-only $Remote $MainBranch
# Get all branches on this specific remote.
$remotePrefix = "$Remote/"
$branches = git for-each-ref `
--format="%(refname:short)" `
"refs/remotes/$Remote" |
Where-Object {
$_ -ne "$Remote/$MainBranch" -and
$_ -ne "$Remote/HEAD"
}
foreach ($remoteBranch in $branches) {
# Remove "<remote>/" prefix while preserving branch names such as
# agent/foo/bar.
$branch = $remoteBranch.Substring($remotePrefix.Length)
Write-Host ""
Write-Host "Updating $branch..."
# Check whether the local branch already exists.
git show-ref --verify --quiet "refs/heads/$branch"
if ($LASTEXITCODE -eq 0) {
git switch $branch
# Make sure the local branch reflects the latest remote branch
# before modifying the instruction files.
git pull --ff-only $Remote $branch
}
else {
git switch --track -c $branch "$Remote/$branch"
}
# Force these files to exactly match canonical main.
git restore `
--source "$MainBranch" `
-- AGENTS.md PROJECT_GOAL.md
git add AGENTS.md PROJECT_GOAL.md
# Commit only when one of the files actually changed.
git diff --cached --quiet
if ($LASTEXITCODE -ne 0) {
git commit -m "Sync agent instructions from $MainBranch"
git push $Remote $branch
}
else {
Write-Host "Already up to date."
}
}
git switch $MainBranch
Write-Host ""
Write-Host "Finished synchronizing AGENTS.md and PROJECT_GOAL.md."