From 2e8a70aa05b5b73efe714e9eb6cded072c730726 Mon Sep 17 00:00:00 2001 From: "sorokin.oleg.edu" Date: Fri, 11 Sep 2026 14:35:39 +0300 Subject: [PATCH] Add sync agent instructions script --- sync-agent-instructions.ps1 | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 sync-agent-instructions.ps1 diff --git a/sync-agent-instructions.ps1 b/sync-agent-instructions.ps1 new file mode 100644 index 00000000..153fc259 --- /dev/null +++ b/sync-agent-instructions.ps1 @@ -0,0 +1,79 @@ +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 "/" 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."