Skip to content

swtich to nearest surviving branch after modify#105

Draft
skarim wants to merge 1 commit into
skarim/add-existing-branchfrom
skarim/switch-after-modify
Draft

swtich to nearest surviving branch after modify#105
skarim wants to merge 1 commit into
skarim/add-existing-branchfrom
skarim/switch-after-modify

Conversation

@skarim
Copy link
Copy Markdown
Collaborator

@skarim skarim commented May 22, 2026

After gh stack modify applies changes, the user may end up on an
orphaned branch that is no longer part of the stack — for example, if
their checked-out branch was dropped, folded into another branch, or
renamed. Previously, the code blindly restored the original branch
regardless of whether it still existed in the stack.

Add a resolveCheckoutBranch helper that inspects the modify plan and
the post-modify stack to determine the best branch to check out:

  1. Still in stack → keep the original branch (no-op)
  2. Renamed → check out the new name
  3. Folded down → check out the fold target (branch below)
  4. Folded up → check out the fold target (branch above)
  5. Dropped → check out the nearest surviving neighbor
    (prefer above, fall back to below)
  6. Fallback → topmost branch in the stack

Both ApplyPlan and ContinueApply (the --continue path) now use
this helper instead of unconditionally restoring the original branch.
When the resolved branch differs from the original, a message is
printed so the user knows they've been switched.

The resolution uses the pre-modify snapshot (already persisted in the
state file) to determine original adjacency, so it works correctly even
when multiple branches are removed in the same operation.

Includes 12 new tests:

  • 9 unit tests for resolveCheckoutBranch covering all action types,
    edge cases (topmost dropped, multiple drops, empty stack), and
    the fallback path
  • 3 integration tests verifying ApplyPlan checks out the correct
    branch after drop, fold-down, and rename operations

Stack created with GitHub Stacks CLIGive Feedback 💬

After `gh stack modify` applies changes, the user may end up on an
orphaned branch that is no longer part of the stack — for example, if
their checked-out branch was dropped, folded into another branch, or
renamed. Previously, the code blindly restored the original branch
regardless of whether it still existed in the stack.

Add a `resolveCheckoutBranch` helper that inspects the modify plan and
the post-modify stack to determine the best branch to check out:

  1. Still in stack  → keep the original branch (no-op)
  2. Renamed         → check out the new name
  3. Folded down     → check out the fold target (branch below)
  4. Folded up       → check out the fold target (branch above)
  5. Dropped         → check out the nearest surviving neighbor
                       (prefer above, fall back to below)
  6. Fallback        → topmost branch in the stack

Both `ApplyPlan` and `ContinueApply` (the `--continue` path) now use
this helper instead of unconditionally restoring the original branch.
When the resolved branch differs from the original, a message is
printed so the user knows they've been switched.

The resolution uses the pre-modify snapshot (already persisted in the
state file) to determine original adjacency, so it works correctly even
when multiple branches are removed in the same operation.

Includes 12 new tests:
  - 9 unit tests for resolveCheckoutBranch covering all action types,
    edge cases (topmost dropped, multiple drops, empty stack), and
    the fallback path
  - 3 integration tests verifying ApplyPlan checks out the correct
    branch after drop, fold-down, and rename operations
@skarim skarim force-pushed the skarim/add-existing-branch branch from 1fdd772 to d522c91 Compare May 22, 2026 16:27
@skarim skarim force-pushed the skarim/switch-after-modify branch from f7e7a80 to f633f33 Compare May 22, 2026 16:27
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