|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/cli/go-gh/v2/pkg/prompter" |
4 | 8 | "github.com/github/gh-stack/internal/config" |
| 9 | + "github.com/github/gh-stack/internal/git" |
| 10 | + "github.com/github/gh-stack/internal/stack" |
5 | 11 | "github.com/spf13/cobra" |
6 | 12 | ) |
7 | 13 |
|
8 | 14 | type checkoutOptions struct { |
9 | | - target string |
10 | | - noSwitch bool |
| 15 | + target string |
11 | 16 | } |
12 | 17 |
|
13 | 18 | func CheckoutCmd(cfg *config.Config) *cobra.Command { |
14 | 19 | opts := &checkoutOptions{} |
15 | 20 |
|
16 | 21 | cmd := &cobra.Command{ |
17 | | - Use: "checkout <pr-or-branch>", |
| 22 | + Use: "checkout [<pr-or-branch>]", |
18 | 23 | Short: "Checkout a stack from a PR number or branch name", |
19 | | - Long: "Discover and check out an entire stack from a pull request number, URL, or branch name.", |
20 | | - Args: cobra.ExactArgs(1), |
| 24 | + Long: `Check out a stack from a pull request number or branch name. |
| 25 | +
|
| 26 | +Currently resolves stacks from local tracking only (.git/gh-stack). |
| 27 | +Accepts a PR number (e.g. 42) or a branch name that belongs to |
| 28 | +a locally tracked stack. When run without arguments, shows a menu of |
| 29 | +all locally available stacks to choose from. |
| 30 | +
|
| 31 | +Server-side stack discovery will be added in a future release.`, |
| 32 | + Args: cobra.MaximumNArgs(1), |
21 | 33 | RunE: func(cmd *cobra.Command, args []string) error { |
22 | | - opts.target = args[0] |
| 34 | + if len(args) > 0 { |
| 35 | + opts.target = args[0] |
| 36 | + } |
23 | 37 | return runCheckout(cfg, opts) |
24 | 38 | }, |
25 | 39 | } |
26 | 40 |
|
27 | | - cmd.Flags().BoolVar(&opts.noSwitch, "no-switch", false, "Fetch and track the stack without switching branches") |
28 | | - |
29 | 41 | return cmd |
30 | 42 | } |
31 | 43 |
|
32 | | -// runCheckout is a placeholder for the stack checkout workflow. |
| 44 | +// runCheckout resolves a stack from local tracking and checks out the target branch. |
33 | 45 | // |
34 | | -// The intended behavior is: |
35 | | -// 1. Resolve the target (PR number, URL, or branch name) to a PR |
| 46 | +// Future behavior (once the server API is available): |
| 47 | +// 1. Resolve the target (PR number, URL, or branch name) to a PR via the API |
36 | 48 | // 2. If the PR is part of a stack, discover the full set of PRs in the stack |
37 | 49 | // 3. Fetch and create local tracking branches for every branch in the stack |
38 | 50 | // 4. Save the stack to local tracking (.git/gh-stack, similar to gh stack init --adopt) |
39 | | -// 5. Switch to the target branch (unless --no-switch is set) |
| 51 | +// 5. Switch to the target branch |
40 | 52 | func runCheckout(cfg *config.Config, opts *checkoutOptions) error { |
41 | | - cfg.Warningf("gh stack checkout is not yet implemented") |
| 53 | + gitDir, err := git.GitDir() |
| 54 | + if err != nil { |
| 55 | + cfg.Errorf("not a git repository") |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + sf, err := stack.Load(gitDir) |
| 60 | + if err != nil { |
| 61 | + cfg.Errorf("failed to load stack state: %s", err) |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + var s *stack.Stack |
| 66 | + var targetBranch string |
| 67 | + |
| 68 | + if opts.target == "" { |
| 69 | + // Interactive picker mode |
| 70 | + s, err = interactiveStackPicker(cfg, sf) |
| 71 | + if err != nil { |
| 72 | + cfg.Errorf("%s", err) |
| 73 | + return nil |
| 74 | + } |
| 75 | + if s == nil { |
| 76 | + return nil |
| 77 | + } |
| 78 | + // Check out the top active branch of the selected stack |
| 79 | + if idx := s.FirstActiveBranchIndex(); idx >= 0 { |
| 80 | + targetBranch = s.Branches[len(s.Branches)-1].Branch |
| 81 | + } else { |
| 82 | + targetBranch = s.Branches[len(s.Branches)-1].Branch |
| 83 | + } |
| 84 | + } else { |
| 85 | + // Resolve target against local stacks |
| 86 | + s, targetBranch, err = findStackByTarget(sf, opts.target) |
| 87 | + if err != nil { |
| 88 | + cfg.Errorf("%s", err) |
| 89 | + return nil |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + currentBranch, _ := git.CurrentBranch() |
| 94 | + if targetBranch == currentBranch { |
| 95 | + cfg.Infof("Already on %s", targetBranch) |
| 96 | + cfg.Printf("Stack: %s", s.DisplayName()) |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + if err := git.CheckoutBranch(targetBranch); err != nil { |
| 101 | + cfg.Errorf("failed to checkout %s: %v", targetBranch, err) |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + cfg.Successf("Switched to %s", targetBranch) |
| 106 | + cfg.Printf("Stack: %s", s.DisplayName()) |
42 | 107 | return nil |
43 | 108 | } |
| 109 | + |
| 110 | +// findStackByTarget resolves a target string against locally tracked stacks. |
| 111 | +// It tries PR number first (integer), then branch name. |
| 112 | +func findStackByTarget(sf *stack.StackFile, target string) (*stack.Stack, string, error) { |
| 113 | + // Try parsing as a PR number |
| 114 | + if prNumber, err := strconv.Atoi(target); err == nil && prNumber > 0 { |
| 115 | + s, b := sf.FindStackByPRNumber(prNumber) |
| 116 | + if s != nil && b != nil { |
| 117 | + return s, b.Branch, nil |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Try matching as a branch name |
| 122 | + stacks := sf.FindAllStacksForBranch(target) |
| 123 | + if len(stacks) == 1 { |
| 124 | + return stacks[0], target, nil |
| 125 | + } |
| 126 | + if len(stacks) > 1 { |
| 127 | + // Target is in multiple stacks (e.g. a trunk branch) — return the first one. |
| 128 | + // A future improvement could prompt for disambiguation here. |
| 129 | + return stacks[0], target, nil |
| 130 | + } |
| 131 | + |
| 132 | + return nil, "", fmt.Errorf( |
| 133 | + "no locally tracked stack found for %q\n"+ |
| 134 | + "This command currently only works with stacks created locally.\n"+ |
| 135 | + "Server-side stack discovery will be available in a future release.", |
| 136 | + target, |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +// interactiveStackPicker shows a menu of all locally tracked stacks and returns |
| 141 | +// the one the user selects. Returns nil, nil if the user has no stacks. |
| 142 | +func interactiveStackPicker(cfg *config.Config, sf *stack.StackFile) (*stack.Stack, error) { |
| 143 | + if !cfg.IsInteractive() { |
| 144 | + return nil, fmt.Errorf("no target specified; provide a branch name or PR number, or run interactively to select a stack") |
| 145 | + } |
| 146 | + |
| 147 | + if len(sf.Stacks) == 0 { |
| 148 | + cfg.Infof("No locally tracked stacks found") |
| 149 | + cfg.Printf("Create a stack with %s or check out a specific branch/PR once server-side discovery is available.", cfg.ColorCyan("gh stack init")) |
| 150 | + return nil, nil |
| 151 | + } |
| 152 | + |
| 153 | + options := make([]string, len(sf.Stacks)) |
| 154 | + for i := range sf.Stacks { |
| 155 | + options[i] = sf.Stacks[i].DisplayName() |
| 156 | + } |
| 157 | + |
| 158 | + p := prompter.New(cfg.In, cfg.Out, cfg.Err) |
| 159 | + selected, err := p.Select( |
| 160 | + "Select a stack to check out (showing locally tracked stacks only)", |
| 161 | + "", |
| 162 | + options, |
| 163 | + ) |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("stack selection: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + return &sf.Stacks[selected], nil |
| 169 | +} |
0 commit comments