fix(monitor): sync pushed repos on branch mismatch

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 01:53:46 +00:00
parent f198db7c68
commit 1f6e10fd8e
2 changed files with 45 additions and 1 deletions

View file

@ -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))

View file

@ -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)