From 1f6e10fd8e1f66fcca315a1d25be1bea2ac06726 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 01:53:46 +0000 Subject: [PATCH] fix(monitor): sync pushed repos on branch mismatch Co-Authored-By: Virgil --- pkg/monitor/sync.go | 4 +++- pkg/monitor/sync_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/pkg/monitor/sync.go b/pkg/monitor/sync.go index 764b2d3..dd8e03e 100644 --- a/pkg/monitor/sync.go +++ b/pkg/monitor/sync.go @@ -127,7 +127,9 @@ func (m *Subsystem) syncWorkspacePush(repo, branch, org string) bool { currentBranch := m.detectBranch(repoDir) if currentBranch != "" && currentBranch != targetBranch { - return true + if !m.gitOK(repoDir, "checkout", "-B", targetBranch, core.Concat("origin/", targetBranch)) { + return false + } } return m.gitOK(repoDir, "reset", "--hard", core.Concat("origin/", targetBranch)) diff --git a/pkg/monitor/sync_test.go b/pkg/monitor/sync_test.go index 4dcec8a..93c5944 100644 --- a/pkg/monitor/sync_test.go +++ b/pkg/monitor/sync_test.go @@ -184,6 +184,48 @@ func TestSync_HandleWorkspacePushed_Good_ResetsTrackedRepo(t *testing.T) { assert.Equal(t, mon.gitOutput(tmpClone, "rev-parse", "HEAD"), mon.gitOutput(repoDir, "rev-parse", "HEAD")) } +func TestSync_HandleWorkspacePushed_Good_SwitchesTrackedRepoBranch(t *testing.T) { + remoteDir := core.JoinPath(t.TempDir(), "remote") + fs.EnsureDir(remoteDir) + run(t, remoteDir, "git", "init", "--bare") + + codeDir := t.TempDir() + orgDir := core.JoinPath(codeDir, "core") + fs.EnsureDir(orgDir) + repoDir := core.JoinPath(orgDir, "test-repo") + run(t, orgDir, "git", "clone", remoteDir, "test-repo") + run(t, repoDir, "git", "checkout", "-b", "main") + fs.Write(core.JoinPath(repoDir, "README.md"), "# test") + run(t, repoDir, "git", "add", ".") + run(t, repoDir, "git", "commit", "-m", "init") + run(t, repoDir, "git", "push", "-u", "origin", "main") + run(t, repoDir, "git", "checkout", "-b", "feature/wip") + + cloneParent := t.TempDir() + tmpClone := core.JoinPath(cloneParent, "clone2") + run(t, cloneParent, "git", "clone", remoteDir, "clone2") + run(t, tmpClone, "git", "checkout", "main") + fs.Write(core.JoinPath(tmpClone, "new.go"), "package main\n") + run(t, tmpClone, "git", "add", ".") + run(t, tmpClone, "git", "commit", "-m", "agent work") + run(t, tmpClone, "git", "push", "origin", "main") + + t.Setenv("CODE_PATH", codeDir) + + mon := New() + mon.ServiceRuntime = testMon.ServiceRuntime + + result := mon.HandleIPCEvents(mon.Core(), messages.WorkspacePushed{ + Repo: "test-repo", + Branch: "main", + Org: "core", + }) + assert.True(t, result.OK) + assert.Equal(t, "main", mon.gitOutput(repoDir, "rev-parse", "--abbrev-ref", "HEAD")) + assert.True(t, fs.Exists(core.JoinPath(repoDir, "new.go"))) + assert.Equal(t, mon.gitOutput(tmpClone, "rev-parse", "HEAD"), mon.gitOutput(repoDir, "rev-parse", "HEAD")) +} + func TestSync_SyncRepos_Good_NormalisesWindowsRepoPath(t *testing.T) { remoteDir := core.JoinPath(t.TempDir(), "remote") fs.EnsureDir(remoteDir)