updateLocalBranch fetches and pulls the latest changes from GitHub after PR merge. It switches to the default branch before pulling so that the working tree contains the merged workflow files, which are required when offering to run the workflow.
()
| 202 | // It switches to the default branch before pulling so that the working tree contains |
| 203 | // the merged workflow files, which are required when offering to run the workflow. |
| 204 | func (c *AddInteractiveConfig) updateLocalBranch() error { |
| 205 | addInteractiveLog.Print("Updating local branch with merged changes") |
| 206 | |
| 207 | // Get the default branch name using gh |
| 208 | output, err := workflow.RunGHCombined("Getting default branch...", "repo", "view", "--repo", c.RepoOverride, "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name") |
| 209 | defaultBranch := "" |
| 210 | if err == nil { |
| 211 | defaultBranch = strings.TrimSpace(string(output)) |
| 212 | } |
| 213 | |
| 214 | // Fallback: query the local origin remote directly (works even when gh repo |
| 215 | // view fails, e.g. forks without a default remote set). |
| 216 | if defaultBranch == "" { |
| 217 | addInteractiveLog.Print("gh repo view failed, trying git ls-remote to detect default branch") |
| 218 | lsCmd := exec.Command("git", "ls-remote", "--symref", "origin", "HEAD") |
| 219 | lsOutput, lsErr := lsCmd.CombinedOutput() |
| 220 | if lsErr == nil { |
| 221 | defaultBranch = parseDefaultBranchFromLsRemote(string(lsOutput)) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if defaultBranch == "" { |
| 226 | defaultBranch = "main" |
| 227 | } |
| 228 | addInteractiveLog.Printf("Default branch: %s", defaultBranch) |
| 229 | |
| 230 | // Fetch the latest changes from origin |
| 231 | if c.Verbose { |
| 232 | fmt.Fprintln(os.Stderr, console.FormatProgressMessage("Fetching latest changes from GitHub...")) |
| 233 | } |
| 234 | |
| 235 | fetchCmd := exec.Command("git", "fetch", "origin", defaultBranch) |
| 236 | fetchOutput, err := fetchCmd.CombinedOutput() |
| 237 | if err != nil { |
| 238 | return fmt.Errorf("git fetch failed: %w (output: %s)", err, string(fetchOutput)) |
| 239 | } |
| 240 | |
| 241 | // Switch to the default branch so the working tree contains the merged workflow |
| 242 | // files. Without this, users on a feature branch won't have the files locally and |
| 243 | // the subsequent "run workflow" step will fail with "workflow file not found". |
| 244 | currentBranch, err := getCurrentBranch() |
| 245 | if err != nil { |
| 246 | addInteractiveLog.Printf("Could not determine current branch: %v", err) |
| 247 | currentBranch = "" |
| 248 | } |
| 249 | |
| 250 | if currentBranch != defaultBranch { |
| 251 | addInteractiveLog.Printf("Switching from %q to default branch %q", currentBranch, defaultBranch) |
| 252 | if err := switchBranch(defaultBranch, c.Verbose); err != nil { |
| 253 | return fmt.Errorf("failed to switch to default branch %s: %w", defaultBranch, err) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | pullCmd := exec.Command("git", "pull", "origin", defaultBranch) |
| 258 | pullOutput, err := pullCmd.CombinedOutput() |
| 259 | if err != nil { |
| 260 | return fmt.Errorf("git pull failed: %w (output: %s)", err, string(pullOutput)) |
| 261 | } |
no test coverage detected