mirror of
https://github.com/github/gh-stack.git
synced 2026-09-14 20:26:28 +08:00
bf2358bf12
* Add `gh stack trunk` navigation command
Add a new navigation command that checks out the trunk branch of the
current stack.
The command is stack-aware: it requires the user to be on a branch that
is part of a stack, loads the stack metadata, and checks out `s.Trunk.Branch`.
If the user is already on the trunk branch, it prints a message and exits
without calling git checkout.
New files:
- cmd/trunk.go: TrunkCmd (cobra command) + runTrunk implementation
- cmd/trunk_test.go: 7 test cases covering happy path, already on
trunk, from top of stack, not in a stack, checkout failure, custom
trunk branch name, and positional argument rejection
Modified files:
- cmd/root.go: register TrunkCmd in the "nav" command group
- README.md: add `gh stack trunk` to the Navigation section
- docs/src/content/docs/reference/cli.md: add `gh stack trunk`
reference section
* address review comments
* increment skill version
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/github/gh-stack/internal/config"
|
|
"github.com/github/gh-stack/internal/git"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func TrunkCmd(cfg *config.Config) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "trunk",
|
|
Short: "Check out the trunk branch of the stack",
|
|
Long: `Check out the trunk branch of the current stack.
|
|
|
|
The trunk is the base branch that the stack is built on (e.g., main or develop).
|
|
You must be on a branch that is part of a stack.`,
|
|
Example: ` # Jump to the trunk branch
|
|
$ gh stack trunk`,
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return runTrunk(cfg)
|
|
},
|
|
}
|
|
}
|
|
|
|
func runTrunk(cfg *config.Config) error {
|
|
result, err := loadStack(cfg, "")
|
|
if err != nil {
|
|
if errors.Is(err, errInterrupt) {
|
|
return ErrSilent
|
|
}
|
|
return ErrNotInStack
|
|
}
|
|
s := result.Stack
|
|
currentBranch := result.CurrentBranch
|
|
trunk := s.Trunk.Branch
|
|
|
|
if currentBranch == trunk {
|
|
cfg.Printf("Already on trunk branch %s", trunk)
|
|
return nil
|
|
}
|
|
|
|
if err := git.CheckoutBranch(trunk); err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg.Successf("Switched to %s", trunk)
|
|
return nil
|
|
}
|