|
| 1 | +// cmd/submit.go |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/boneskull/gh-stack/internal/config" |
| 9 | + "github.com/boneskull/gh-stack/internal/git" |
| 10 | + "github.com/boneskull/gh-stack/internal/github" |
| 11 | + "github.com/boneskull/gh-stack/internal/state" |
| 12 | + "github.com/boneskull/gh-stack/internal/tree" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var submitCmd = &cobra.Command{ |
| 17 | + Use: "submit", |
| 18 | + Short: "Cascade, push, and create/update PRs for current branch and descendants", |
| 19 | + Long: `Submit rebases the current branch and its descendants onto their parents, |
| 20 | +pushes all affected branches, and creates or updates pull requests. |
| 21 | +
|
| 22 | +This is the typical workflow command after making changes in a stack: |
| 23 | +1. Cascade: rebase current branch + descendants onto their parents |
| 24 | +2. Push: force-push all affected branches (with --force-with-lease) |
| 25 | +3. PR: create PRs for branches without them, update PR bases for those that have them |
| 26 | +
|
| 27 | +If a rebase conflict occurs, resolve it and run 'gh stack continue'.`, |
| 28 | + RunE: runSubmit, |
| 29 | +} |
| 30 | + |
| 31 | +var ( |
| 32 | + submitDryRunFlag bool |
| 33 | + submitCurrentOnlyFlag bool |
| 34 | + submitUpdateOnlyFlag bool |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + submitCmd.Flags().BoolVar(&submitDryRunFlag, "dry-run", false, "show what would be done without doing it") |
| 39 | + submitCmd.Flags().BoolVar(&submitCurrentOnlyFlag, "current-only", false, "only submit current branch, not descendants") |
| 40 | + submitCmd.Flags().BoolVar(&submitUpdateOnlyFlag, "update-only", false, "only update existing PRs, don't create new ones") |
| 41 | + rootCmd.AddCommand(submitCmd) |
| 42 | +} |
| 43 | + |
| 44 | +func runSubmit(cmd *cobra.Command, args []string) error { |
| 45 | + cwd, err := os.Getwd() |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + cfg, err := config.Load(cwd) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + g := git.New(cwd) |
| 56 | + |
| 57 | + // Check for dirty working tree |
| 58 | + dirty, err := g.IsDirty() |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if dirty { |
| 63 | + return fmt.Errorf("working tree has uncommitted changes; commit or stash first") |
| 64 | + } |
| 65 | + |
| 66 | + // Check if operation already in progress |
| 67 | + if state.Exists(g.GetGitDir()) { |
| 68 | + return fmt.Errorf("operation already in progress; use 'gh stack continue' or 'gh stack abort'") |
| 69 | + } |
| 70 | + |
| 71 | + currentBranch, err := g.CurrentBranch() |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + // Build tree |
| 77 | + root, err := tree.Build(cfg) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + node := tree.FindNode(root, currentBranch) |
| 83 | + if node == nil { |
| 84 | + return fmt.Errorf("branch %q is not tracked", currentBranch) |
| 85 | + } |
| 86 | + |
| 87 | + // Collect branches to submit (current + descendants) |
| 88 | + var branches []*tree.Node |
| 89 | + branches = append(branches, node) |
| 90 | + if !submitCurrentOnlyFlag { |
| 91 | + branches = append(branches, tree.GetDescendants(node)...) |
| 92 | + } |
| 93 | + |
| 94 | + // Phase 1: Cascade |
| 95 | + fmt.Println("=== Phase 1: Cascade ===") |
| 96 | + if err := doCascadeWithState(g, cfg, branches, submitDryRunFlag, state.OperationSubmit, submitUpdateOnlyFlag); err != nil { |
| 97 | + return err // Conflict or error - state saved, user can continue |
| 98 | + } |
| 99 | + |
| 100 | + // Phases 2 & 3 |
| 101 | + return doSubmitPushAndPR(g, cfg, root, branches, submitDryRunFlag, submitUpdateOnlyFlag) |
| 102 | +} |
| 103 | + |
| 104 | +// doSubmitPushAndPR handles push and PR creation/update phases. |
| 105 | +// This is called after cascade succeeds (or from continue after conflict resolution). |
| 106 | +func doSubmitPushAndPR(g *git.Git, cfg *config.Config, root *tree.Node, branches []*tree.Node, dryRun, updateOnly bool) error { |
| 107 | + // Phase 2: Push all branches |
| 108 | + fmt.Println("\n=== Phase 2: Push ===") |
| 109 | + for _, b := range branches { |
| 110 | + if dryRun { |
| 111 | + fmt.Printf("Would push %s -> origin/%s (forced)\n", b.Name, b.Name) |
| 112 | + } else { |
| 113 | + fmt.Printf("Pushing %s -> origin/%s (forced)... ", b.Name, b.Name) |
| 114 | + if err := g.Push(b.Name, true); err != nil { |
| 115 | + fmt.Println("failed") |
| 116 | + return fmt.Errorf("failed to push %s: %w", b.Name, err) |
| 117 | + } |
| 118 | + fmt.Println("ok") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Phase 3: Create/update PRs |
| 123 | + return doSubmitPRs(cfg, root, branches, dryRun, updateOnly) |
| 124 | +} |
| 125 | + |
| 126 | +// doSubmitPRs handles PR creation/update for all branches. |
| 127 | +func doSubmitPRs(cfg *config.Config, root *tree.Node, branches []*tree.Node, dryRun, updateOnly bool) error { |
| 128 | + fmt.Println("\n=== Phase 3: PRs ===") |
| 129 | + |
| 130 | + trunk, err := cfg.GetTrunk() |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + // In dry-run mode, we don't need a GitHub client |
| 136 | + var ghClient *github.Client |
| 137 | + if !dryRun { |
| 138 | + var clientErr error |
| 139 | + ghClient, clientErr = github.NewClient() |
| 140 | + if clientErr != nil { |
| 141 | + return clientErr |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + for _, b := range branches { |
| 146 | + parent, _ := cfg.GetParent(b.Name) //nolint:errcheck // empty is fine |
| 147 | + if parent == "" { |
| 148 | + parent = trunk |
| 149 | + } |
| 150 | + |
| 151 | + existingPR, _ := cfg.GetPR(b.Name) //nolint:errcheck // 0 is fine |
| 152 | + |
| 153 | + if existingPR > 0 { |
| 154 | + // Update existing PR |
| 155 | + if dryRun { |
| 156 | + fmt.Printf("Would update PR #%d base to %q\n", existingPR, parent) |
| 157 | + } else { |
| 158 | + fmt.Printf("Updating PR #%d for %s (base: %s)... ", existingPR, b.Name, parent) |
| 159 | + if err := ghClient.UpdatePRBase(existingPR, parent); err != nil { |
| 160 | + fmt.Println("failed") |
| 161 | + fmt.Printf("Warning: failed to update PR #%d base: %v\n", existingPR, err) |
| 162 | + } else { |
| 163 | + fmt.Println("ok") |
| 164 | + } |
| 165 | + // Update stack comment |
| 166 | + if err := ghClient.GenerateAndPostStackComment(root, b.Name, trunk, existingPR); err != nil { |
| 167 | + fmt.Printf("Warning: failed to update stack comment for PR #%d: %v\n", existingPR, err) |
| 168 | + } |
| 169 | + } |
| 170 | + } else if !updateOnly { |
| 171 | + // Create new PR |
| 172 | + if dryRun { |
| 173 | + fmt.Printf("Would create PR for %s (base: %s)\n", b.Name, parent) |
| 174 | + } else { |
| 175 | + prNum, err := createPRForBranch(ghClient, cfg, root, b.Name, parent, trunk) |
| 176 | + if err != nil { |
| 177 | + fmt.Printf("Warning: failed to create PR for %s: %v\n", b.Name, err) |
| 178 | + } else { |
| 179 | + fmt.Printf("Created PR #%d for %s (%s)\n", prNum, b.Name, ghClient.PRURL(prNum)) |
| 180 | + } |
| 181 | + } |
| 182 | + } else { |
| 183 | + fmt.Printf("Skipping %s (no existing PR, --update-only)\n", b.Name) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +// createPRForBranch creates a PR for the given branch and stores the PR number. |
| 191 | +func createPRForBranch(ghClient *github.Client, cfg *config.Config, root *tree.Node, branch, base, trunk string) (int, error) { |
| 192 | + // Determine if draft (not targeting trunk = middle of stack) |
| 193 | + draft := base != trunk |
| 194 | + |
| 195 | + pr, err := ghClient.CreateSubmitPR(branch, base, draft) |
| 196 | + if err != nil { |
| 197 | + return 0, err |
| 198 | + } |
| 199 | + |
| 200 | + // Store PR number in config |
| 201 | + if err := cfg.SetPR(branch, pr.Number); err != nil { |
| 202 | + return pr.Number, fmt.Errorf("PR created but failed to store number: %w", err) |
| 203 | + } |
| 204 | + |
| 205 | + // Update the tree node's PR number so stack comments render correctly |
| 206 | + if node := tree.FindNode(root, branch); node != nil { |
| 207 | + node.PR = pr.Number |
| 208 | + } |
| 209 | + |
| 210 | + // Add stack navigation comment |
| 211 | + if err := ghClient.GenerateAndPostStackComment(root, branch, trunk, pr.Number); err != nil { |
| 212 | + fmt.Printf("Warning: failed to add stack comment to PR #%d: %v\n", pr.Number, err) |
| 213 | + } |
| 214 | + |
| 215 | + return pr.Number, nil |
| 216 | +} |
0 commit comments