-
Notifications
You must be signed in to change notification settings - Fork 13
trunk command #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+282
−1
Merged
trunk command #108
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/github/gh-stack/internal/config" | ||
| "github.com/github/gh-stack/internal/git" | ||
| "github.com/github/gh-stack/internal/stack" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestTrunk_FromMiddleBranch(t *testing.T) { | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "main"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}, {Branch: "b3"}}, | ||
| } | ||
|
|
||
| var checkedOut []string | ||
| tmpDir := t.TempDir() | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "b2", nil }, | ||
| CheckoutBranchFn: func(name string) error { | ||
| checkedOut = append(checkedOut, name) | ||
| return nil | ||
| }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, []string{"main"}, checkedOut) | ||
| } | ||
|
|
||
| func TestTrunk_AlreadyOnTrunk(t *testing.T) { | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "main"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, | ||
| } | ||
|
|
||
| var checkedOut []string | ||
| tmpDir := t.TempDir() | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "main", nil }, | ||
| CheckoutBranchFn: func(name string) error { | ||
| checkedOut = append(checkedOut, name) | ||
| return nil | ||
| }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, outR, errR := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| output := readCfgOutput(cfg, outR, errR) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Empty(t, checkedOut, "should not checkout any branch") | ||
| assert.Contains(t, output, "Already on trunk branch main") | ||
| } | ||
|
|
||
| func TestTrunk_FromTopOfStack(t *testing.T) { | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "main"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}, {Branch: "b3"}}, | ||
| } | ||
|
|
||
| var checkedOut []string | ||
| tmpDir := t.TempDir() | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "b3", nil }, | ||
| CheckoutBranchFn: func(name string) error { | ||
| checkedOut = append(checkedOut, name) | ||
| return nil | ||
| }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, []string{"main"}, checkedOut) | ||
| } | ||
|
|
||
| func TestTrunk_NotInStack(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| // No stack file written — empty git dir | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "some-branch", nil }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.ErrorIs(t, err, ErrNotInStack) | ||
| } | ||
|
|
||
| func TestTrunk_CheckoutFailure(t *testing.T) { | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "main"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, | ||
| } | ||
|
|
||
| tmpDir := t.TempDir() | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "b1", nil }, | ||
| CheckoutBranchFn: func(name string) error { | ||
| return fmt.Errorf("checkout failed: uncommitted changes") | ||
| }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.Error(t, err) | ||
| assert.ErrorContains(t, err, "checkout failed") | ||
| } | ||
|
skarim marked this conversation as resolved.
|
||
|
|
||
| func TestTrunk_CustomTrunkBranch(t *testing.T) { | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "develop"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, | ||
| } | ||
|
|
||
| var checkedOut []string | ||
| tmpDir := t.TempDir() | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "b1", nil }, | ||
| CheckoutBranchFn: func(name string) error { | ||
| checkedOut = append(checkedOut, name) | ||
| return nil | ||
| }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, []string{"develop"}, checkedOut) | ||
| } | ||
|
|
||
| func TestTrunk_RejectsArgs(t *testing.T) { | ||
| // Ensure trunk does not accept arguments | ||
| tmpDir := t.TempDir() | ||
| s := stack.Stack{ | ||
| Trunk: stack.BranchRef{Branch: "main"}, | ||
| Branches: []stack.BranchRef{{Branch: "b1"}}, | ||
| } | ||
| writeStackFile(t, tmpDir, s) | ||
|
|
||
| mock := &git.MockOps{ | ||
| GitDirFn: func() (string, error) { return tmpDir, nil }, | ||
| CurrentBranchFn: func() (string, error) { return "b1", nil }, | ||
| } | ||
| restore := git.SetOps(mock) | ||
| defer restore() | ||
|
|
||
| cfg, _, _ := config.NewTestConfig() | ||
| cmd := TrunkCmd(cfg) | ||
| cmd.SetArgs([]string{"unexpected-arg"}) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
| err := cmd.Execute() | ||
|
|
||
| assert.Error(t, err, "should reject positional arguments") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.