-
Notifications
You must be signed in to change notification settings - Fork 62
checkout: detect remote stack from current branch #475
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,10 +44,12 @@ it simply switches to the branch. | |
| When a branch name is provided, the command resolves it against | ||
| locally tracked stacks only. | ||
|
|
||
| When run without arguments, opens an interactive picker listing every | ||
| stack available to you — both the stacks tracked locally and the stacks | ||
| that exist only on GitHub — so you can search, filter, and check one out. | ||
| Fully merged stacks are omitted.`, | ||
| When run without arguments, first checks whether the current branch belongs | ||
| to a stack on remote that is not tracked locally, and offers to check | ||
| it out. Otherwise, it opens an interactive picker listing every stack available | ||
| to you — both the stacks tracked locally and the stacks that exist only on | ||
| GitHub — so you can search, filter, and check one out. Fully merged stacks are | ||
| omitted.`, | ||
| Example: ` # Check out a stack by its stack number | ||
| $ gh stack checkout 7 | ||
|
|
||
|
|
@@ -643,7 +645,22 @@ func interactiveCheckout(cfg *config.Config, sf *stack.StackFile, gitDir string) | |
| return nil, "", fmt.Errorf("no target specified; provide a branch name or PR number, or run interactively to select a stack") | ||
| } | ||
|
|
||
| rows := gatherCheckoutRows(cfg, sf) | ||
| rows, remoteStacks := gatherCheckoutRows(cfg, sf) | ||
| if currentBranch, branchErr := git.CurrentBranch(); branchErr == nil { | ||
| stackNumber, confirmed, confirmErr := offerRemoteStackForBranch(cfg, sf, remoteStacks, currentBranch) | ||
| if confirmErr != nil { | ||
| // The confirmation helper only returns an error for an explicit | ||
| // interrupt and has already printed the friendly message. | ||
| return nil, "", ErrSilent | ||
| } | ||
| if confirmed { | ||
| return resolveCheckoutSelection(cfg, sf, gitDir, checkoutview.StackRow{ | ||
| Number: stackNumber, | ||
| Type: checkoutview.TypeRemote, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if len(rows) == 0 { | ||
| cfg.Infof("No stacks available to check out") | ||
| cfg.Printf("Create a stack with `%s` or check out a stack by number with `%s`", | ||
|
|
@@ -668,14 +685,89 @@ func interactiveCheckout(cfg *config.Config, sf *stack.StackFile, gitDir string) | |
| // with the local stacks into the picker's rows. Any GitHub failure (stacks not | ||
| // enabled for the repo, no auth, network error) gracefully degrades to a | ||
| // local-only list. | ||
| func gatherCheckoutRows(cfg *config.Config, sf *stack.StackFile) []checkoutview.StackRow { | ||
| func gatherCheckoutRows(cfg *config.Config, sf *stack.StackFile) ([]checkoutview.StackRow, []github.RemoteStack) { | ||
| var remote []github.RemoteStack | ||
| if client, err := cfg.GitHubClient(); err == nil { | ||
| if stacks, err := client.ListStacks(); err == nil { | ||
| remote = stacks | ||
| } | ||
| } | ||
| return checkoutview.BuildRows(sf.Stacks, remote) | ||
| return checkoutview.BuildRows(sf.Stacks, remote), remote | ||
| } | ||
|
|
||
| // offerRemoteStackForBranch asks to check out the unique active remote stack | ||
| // containing branch when the branch is not already associated with a local | ||
| // stack. Every outcome except confirmation or Ctrl+C falls through to the | ||
| // existing picker. | ||
| func offerRemoteStackForBranch(cfg *config.Config, sf *stack.StackFile, remote []github.RemoteStack, branch string) (int, bool, error) { | ||
| if branch == "" || len(sf.FindAllStacksForBranch(branch)) > 0 { | ||
| return 0, false, nil | ||
| } | ||
|
|
||
| matches := matchingRemoteStacksForBranch(remote, branch) | ||
| if len(matches) != 1 { | ||
| return 0, false, nil | ||
| } | ||
|
|
||
| stackNumber := matches[0].Number | ||
| prompt := fmt.Sprintf("Found stack #%d that includes branch %q. Check out stack #%d?", stackNumber, branch, stackNumber) | ||
| confirmed, err := confirmRemoteStackCheckout(cfg, prompt) | ||
| if err != nil { | ||
| if errors.Is(err, errInterrupt) { | ||
| return 0, false, err | ||
| } | ||
| return 0, false, nil | ||
| } | ||
| if !confirmed { | ||
| return 0, false, nil | ||
|
skarim marked this conversation as resolved.
|
||
| } | ||
| return stackNumber, true, nil | ||
| } | ||
|
|
||
| // matchingRemoteStacksForBranch returns picker-eligible remote stacks that | ||
| // contain branch exactly once per stack. Empty and fully merged stacks are not | ||
| // actionable and are omitted, matching the picker. | ||
| func matchingRemoteStacksForBranch(remote []github.RemoteStack, branch string) []*github.RemoteStack { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A branch can only belong to one stack, right? 🤔 Or does this cover the default branch as well, which is the one branch that can match many stacks?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically the limitation is only at a PR-level to only have a PR in one stack. It is possible to reuse a branch name, which is how you'd end up with multiple stacks matching for a branch. This can happen if a PR was closed and then opened for the same head branch, or if you open a new PR with a different base-head combo. |
||
| var matches []*github.RemoteStack | ||
| for i := range remote { | ||
| rs := &remote[i] | ||
| if rs.Number <= 0 || len(rs.PRDetails) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| containsBranch := false | ||
| hasUnmergedPR := false | ||
| for _, pr := range rs.PRDetails { | ||
| if pr.Head.Ref == branch { | ||
| containsBranch = true | ||
| } | ||
| if !pr.IsMerged() { | ||
| hasUnmergedPR = true | ||
| } | ||
| } | ||
| if containsBranch && hasUnmergedPR { | ||
| matches = append(matches, rs) | ||
| } | ||
| } | ||
| return matches | ||
| } | ||
|
|
||
| func confirmRemoteStackCheckout(cfg *config.Config, prompt string) (bool, error) { | ||
| var ( | ||
| confirmed bool | ||
| err error | ||
| ) | ||
| if cfg.ConfirmFn != nil { | ||
| confirmed, err = cfg.ConfirmFn(prompt, true) | ||
| } else { | ||
| p := prompter.New(cfg.In, cfg.Out, cfg.Err) | ||
| confirmed, err = p.Confirm(prompt, true) | ||
| } | ||
| if isInterruptError(err) { | ||
| printInterrupt(cfg) | ||
| return false, errInterrupt | ||
| } | ||
| return confirmed, err | ||
| } | ||
|
|
||
| // resolveCheckoutSelection resolves a picker selection to a local stack and the | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.