Skip to content

refactor: dedupe tracked-branch lookups, drop dead undo indirection#89

Merged
andyrewlee merged 1 commit into
mainfrom
refactor/engine-undo-simplify
Jun 21, 2026
Merged

refactor: dedupe tracked-branch lookups, drop dead undo indirection#89
andyrewlee merged 1 commit into
mainfrom
refactor/engine-undo-simplify

Conversation

@andyrewlee

Copy link
Copy Markdown
Owner

What

Two behaviour-preserving simplifications in internal/stack found during a maintainability audit.

1. Single source for the "not tracked" lookup. The pattern b, ok := s.Get(name); if !ok { return …"branch %q is not tracked" } appeared across engine.go and restack.go. Adds:

  • State.tracked(name) — the by-name lookup + canonical error (used by delete, untrack, RestackBranch, needsRestackAgainst).
  • currentTracked(g, s) — the CurrentBranch + tracked preamble shared by fold / squash / onto.

2. Drop a dead variable in Undo. checkoutAfterRestore was initialised to "" and never reassigned before its own checkoutAfterRestore == "" guard (always true), so it only obscured that the final checkout target is simply entry.CurrentBranch. Inlined.

Why it's safe

No behaviour change. For the trunk, currentTracked still returns the same "not tracked" error s.Get produced (the trunk isn't in Branches). The undo change removes an always-true subcondition and inlines the identical target. Covered by the engine unit tests, the model/invariant test (thousands of random op sequences), and e2e.

make ci green — stdlib-only, lint 0 issues, race + e2e, coverage 86.5%. Net −37/+46 lines (the helpers carry doc comments; call sites shrink).

No behaviour change; verified by the engine + model-invariant + e2e suites.

- Add State.tracked and currentTracked helpers and route the engine's
  eight duplicated "branch %q is not tracked" lookups through them:
  fold/squash/onto share the CurrentBranch+tracked preamble, and
  delete/untrack/RestackBranch/needsRestackAgainst share the by-name
  lookup. The message now has a single source of truth.
- Remove the checkoutAfterRestore variable in Undo: it was initialised to
  "" and never reassigned before its own always-true `== ""` guard, so it
  only obscured that the final checkout target is entry.CurrentBranch.
@andyrewlee andyrewlee merged commit 8c01a5b into main Jun 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant