fix(sync): stop reporting success when no branch was actually synced

hub sync silently dropped any local branch whose upstream it could not
resolve: it cleared its internal remote branch while leaving the "gone"
flag false, so neither the update nor the delete path ran. A run could
then print "Already up to date" having done nothing, which is why a
workspace-wide sync looked like it worked when it did not.

- report unmatched branches as UNTRACKED with the reason, so
  "Already up to date" is only said when it is true
- resolve the remote default branch via `git ls-remote --symref` when
  refs/remotes/<remote>/HEAD is missing, so merged branches whose
  upstream was deleted are still cleaned up
- make --create-missing opt-in, keeping hub's default of never creating
  a local branch that does not already exist
- summarise runs as changed/unchanged with per-action counts, and list
  deletions and failures in full instead of leaving them in scrollback
- restore per-command flag help, which the custom Usage had dropped
- add an in-place CLI progress line, drawn only to a terminal so piped
  and --json output stay free of cursor control
- group the TUI selection screen by outcome so blocked and dirty
  repositories are visible before a run starts

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-09-28 19:57:42 +01:00
parent 4f6e9c9991
commit f4286edff2
7 changed files with 885 additions and 72 deletions
+92 -5
View File
@@ -1,6 +1,8 @@
package main
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
@@ -22,7 +24,7 @@ func TestTUIOnlySelectsEligibleRepositories(t *testing.T) {
t.Fatalf("selected repositories = %d, want 2", got)
}
m.cursor = 2
m = focusRepo(t, m, "blocked")
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeySpace})
m = updated.(tuiModel)
if got := len(m.selectedRepos()); got != 2 {
@@ -47,7 +49,8 @@ func TestTUIRequiresExplicitConfirmation(t *testing.T) {
func TestTUIViewExplainsBlockedRepository(t *testing.T) {
m := newTUIModel("/workspace", testTUIRepos(), options{})
m.width, m.height, m.cursor = 100, 30, 2
m.width, m.height = 100, 30
m = focusRepo(t, m, "blocked")
view := m.View()
for _, want := range []string{"STACK SYNC", "detached HEAD", "1 blocked"} {
if !strings.Contains(view, want) {
@@ -56,9 +59,25 @@ func TestTUIViewExplainsBlockedRepository(t *testing.T) {
}
}
// focusRepo points the cursor at a repository by name. The selection screen is
// grouped by outcome, so list positions are not stable across changes to the
// grouping rules and tests must not depend on them.
func focusRepo(t *testing.T, m tuiModel, name string) tuiModel {
t.Helper()
for i, r := range m.repos {
if r.RelativePath == name {
m.cursor = i
return m
}
}
t.Fatalf("repository %q not found in %+v", name, m.repos)
return m
}
func TestTUIViewExplainsDirtyBranchProtection(t *testing.T) {
m := newTUIModel("/workspace", testTUIRepos(), options{})
m.width, m.height, m.cursor = 100, 30, 1
m.width, m.height = 100, 30
m = focusRepo(t, m, "dirty")
view := m.View()
for _, want := range []string{"ready (dirty)", "CHECKED-OUT BRANCH PROTECTED", "file.txt"} {
if !strings.Contains(view, want) {
@@ -71,6 +90,7 @@ func TestTUIViewShowsLastSyncOutputForFocusedRepository(t *testing.T) {
m := newTUIModel("/workspace", testTUIRepos(), options{})
m.width, m.height = 100, 30
m.lastResult = []syncResult{{Path: "clean", Success: true, Message: "Updated branch main", Duration: "120ms"}}
m = focusRepo(t, m, "clean")
view := m.View()
for _, want := range []string{"LAST SYNC", "120ms", "Updated branch main"} {
if !strings.Contains(view, want) {
@@ -100,6 +120,7 @@ func TestTUISyncProgressShowsLiveCountsAndLatestResult(t *testing.T) {
t.Fatalf("finish event did not update progress: done=%d ok=%d active=%+v", m.syncDone, m.syncOK, m.syncActive)
}
view := m.View()
// The live progress view counts finished repositories, not changed ones.
for _, want := range []string{"1/3", "1 synced", "0 failed", "Latest", "clean", "up to date"} {
if !strings.Contains(view, want) {
t.Fatalf("progress view missing %q:\n%s", want, view)
@@ -122,7 +143,9 @@ func TestTUICompletionSummarizesAndFocusesFirstIssue(t *testing.T) {
t.Fatalf("completion did not focus the first issue: mode=%v cursor=%d status=%q", m.mode, m.cursor, m.status)
}
view := m.View()
for _, want := range []string{"LAST RUN", "1 synced", "1 failed", "dirty", "authentication failed", "f next"} {
// "clean" only had a protected branch, so under the changed/unchanged
// accounting it is unchanged rather than changed.
for _, want := range []string{"LAST RUN", "0 changed", "1 unchanged", "1 failed", "dirty", "authentication failed", "f next"} {
if !strings.Contains(view, want) {
t.Fatalf("completion view missing %q:\n%s", want, view)
}
@@ -135,7 +158,7 @@ func TestTUIFocusNextIssueCyclesThroughAttentionResults(t *testing.T) {
{Path: "clean", Success: true, Branches: []branchSyncResult{{Branch: "main", Action: "protected", Message: "checked out elsewhere"}}},
{Path: "dirty", Message: "fetch failed"},
}
m.cursor = 0
m = focusRepo(t, m, "clean")
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'f'}})
m = updated.(tuiModel)
@@ -154,3 +177,67 @@ func TestTruncateMiddle(t *testing.T) {
t.Fatalf("truncateMiddle() = %q", got)
}
}
// Problems must be visible on the selection screen without starting a run.
func TestSelectionScreenGroupsRepositoriesByOutcome(t *testing.T) {
repos := []repo{
{Path: "/w/zeta", RelativePath: "zeta", Branch: "main", Eligible: true},
{Path: "/w/dirty-b", RelativePath: "dirty-b", Branch: "main", Dirty: []string{" M a"}, Eligible: true},
{Path: "/w/blocked", RelativePath: "blocked", BlockReason: "detached HEAD"},
{Path: "/w/alpha", RelativePath: "alpha", Branch: "main", Eligible: true},
{Path: "/w/dirty-a", RelativePath: "dirty-a", Branch: "main", Dirty: []string{" M b"}, Eligible: true},
}
m := newTUIModel("/w", repos, options{})
m.width, m.height = 100, 40
var order []string
for _, r := range m.repos {
order = append(order, r.RelativePath)
}
want := []string{"blocked", "dirty-a", "dirty-b", "alpha", "zeta"}
if !reflect.DeepEqual(order, want) {
t.Fatalf("grouped order = %v, want %v", order, want)
}
view := m.View()
for _, heading := range []string{"BLOCKED ·", "DIRTY ·", "READY"} {
if !strings.Contains(view, heading) {
t.Fatalf("view missing group heading %q:\n%s", heading, view)
}
}
// Headings must precede the repositories they describe.
if strings.Index(view, "DIRTY ·") > strings.Index(view, "dirty-a") {
t.Fatalf("DIRTY heading rendered after its repositories:\n%s", view)
}
}
// Group headings share the list height, so a tall list must still keep the
// cursor on screen and must not loop or stall.
func TestGroupedViewportKeepsCursorVisible(t *testing.T) {
var repos []repo
for i := range 40 {
relative := fmt.Sprintf("repo-%02d", i)
r := repo{Path: "/w/" + relative, RelativePath: relative, Branch: "main", Eligible: true}
if i%5 == 0 {
r.Dirty = []string{" M file"}
}
repos = append(repos, r)
}
m := newTUIModel("/w", repos, options{})
m.width, m.height = 100, 24
for _, cursor := range []int{0, 1, 7, 19, 39} {
m.cursor = cursor
m.clampViewport()
rows := m.visibleRows()
if m.cursor < rows.start || m.cursor >= rows.end {
t.Fatalf("cursor %d outside window %+v", m.cursor, rows)
}
if got := m.windowRows(rows.start, rows.end); got > m.listHeight() {
t.Fatalf("window uses %d rows, only %d available", got, m.listHeight())
}
if !strings.Contains(m.View(), m.repos[m.cursor].RelativePath) {
t.Fatalf("cursor %d (%s) not rendered", m.cursor, m.repos[m.cursor].RelativePath)
}
}
}