A Go CLI extension (gh stack) for managing stacked branches and pull requests. Uses Cobra for commands, bubbletea/lipgloss for TUI, and stretchr/testify for tests.
Repository operations require Git 2.36+.
go mod download # install deps
go build -o gh-stack . # build
go vet ./... # static analysis. Always run before tests.
go run ./scripts/test -workers 4 # full race-enabled suite in isolated processesRun vet before tests. See CONTRIBUTING.md for canonical/targeted commands and runner details. Do not add t.Parallel to tests that change Git mocks, cwd, or environment variables.
No Makefile, no code generation, no external linter config. Standard Go toolchain only.
cmd/: One Cobra command per file. Each exports<Name>Cmd(cfg *config.Config)with logic inrun<Name>().internal/git/:Opsinterface wrapping git CLI.MockOpsfor tests. UseForWorktree(path)for scoped execution,CommonDir()for shared storage, andGitDir()for native per-worktree state. Never use productionos.ChdirorSetOpsto switch context.internal/github/:ClientOpsinterface (18 methods) for GitHub API.MockClientfor tests. Stack operations use the public Stacks REST API (/repos/{owner}/{repo}/stacks); merges use the async merge API (/repos/{owner}/{repo}/pulls/{n}/merge-async) with an explicitmerge_action(direct_mergeormerge_queue) chosen from the base branch's merge-queue detection.merge_actionis optional — omitting it (or sendingdefault) lets the server auto-route (merge queue if one is configured, else direct merge) — but the CLI sends it explicitly so a wrong detection fails loudly instead of silently merging directly.internal/config/:Configstruct passed to all commands. Holds I/O, colors, and test hooks (SelectFn,ConfirmFn,InputFn,GitHubClientOverride).internal/stack/: Shared catalog (<common-dir>/gh-stack, JSON), conservative legacy migration, atomic saves, and short catalog locks.internal/worktree/: Origin/owner identities, cleanliness preflight, scoped operations, and touched-ref recovery.internal/tui/: bubbletea views (stackview,modifyview).
- Return typed
ExitErrorsentinels (codes 1-10 incmd/utils.go) fromRunE. Never callos.Exit()directly. - Check errors with
var exitErr *ExitError; errors.As(err, &exitErr). - Table-driven tests with
t.Run()subtests. - Use
config.NewTestConfig()for test configs with captured I/O. - Mock git:
restore := git.SetOps(&git.MockOps{...}); defer restore(). Always defer restore. - Mock GitHub:
cfg.GitHubClientOverride = &github.MockClient{...}. - Mock prompts: set
cfg.SelectFn,cfg.ConfirmFn, orcfg.InputFn. - Load stack files with
stack.Load(dir)after writing to get correct checksums. - Use
stackStateDir(cfg)for application state andbeginStackMutationbefore mutation snapshots; defer cleanup. The clone-wide operation lock is separate from short catalog saves. - Recovery must match stack identity, execute in the recorded worktree, and retain journals on partial failures. Native Git markers stay per-worktree.
- Finish paused operations before switching preview stages or versions. Reject origin-only and unknown rebase execution modes before routing recovery; use the matching layer3 build in the recorded origin for origin-only journals.
- Mutation locks coordinate gh-stack only, not Git commands/editors. Keep affected worktrees quiescent during rewrites. Pass the snapshot SHA (or prior
Context.TouchedSHA) toContext.Startbefore ref mutations; never claim an external commit as this operation's work during continuation. - Distributed modify preflights action/cascade targets and executes in recorded owners; only the origin may switch for unoccupied branches. Preserve dropped/folded source worktrees and refs. Before native continuation, preflight other worktrees, not remaining branches in the intentionally busy pending worktree.
For full architecture details, see AGENTS.md in the repository root.