{"record":{"id":"70ddb8cfdb104528","repo":"Hmbown/CodeWhale","slug":"gh-pr-checkout-number-failed-stderr","errorCode":null,"errorMessage":"gh pr checkout #{number} failed: {stderr}","messagePattern":"gh pr checkout #(.+?) failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/lib.rs","lineNumber":8300,"sourceCode":"        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();\n        bail!(\"gh pr diff #{number} failed: {stderr}\");\n    }\n    Ok(String::from_utf8_lossy(&output.stdout).to_string())\n}\n\nfn run_gh_pr_checkout(number: u32, repo: Option<&str>) -> Result<()> {\n    let mut cmd = crate::dependencies::Gh::command()\n        .ok_or_else(|| anyhow::anyhow!(\"gh not found on PATH\"))?;\n    cmd.arg(\"pr\").arg(\"checkout\").arg(number.to_string());\n    if let Some(r) = repo {\n        cmd.arg(\"--repo\").arg(r);\n    }\n    let output = cmd\n        .output()\n        .map_err(|e| anyhow::anyhow!(\"Failed to run `gh pr checkout`: {e}\"))?;\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();\n        bail!(\"gh pr checkout #{number} failed: {stderr}\");\n    }\n    Ok(())\n}\n\n/// Format the PR review prompt that lands in the composer. Caps the\n/// diff at 200 KiB so a massive PR doesn't blow the model's context\n/// window before the user even hits Enter — they can always ask the\n/// model to fetch more via `gh pr diff #N` from inside the session.\nfn format_pr_prompt(number: u32, view: &GhPullRequest, diff: &str) -> String {\n    const MAX_DIFF_BYTES: usize = 200 * 1024;\n    let diff_section = if diff.len() > MAX_DIFF_BYTES {\n        let cut = (0..=MAX_DIFF_BYTES)\n            .rev()\n            .find(|&i| diff.is_char_boundary(i))\n            .unwrap_or(0);\n        format!(\n            \"{}\\n\\n[…diff truncated at {} KiB; ask me to fetch more if needed]\\n\",\n            &diff[..cut],","sourceCodeStart":8282,"sourceCodeEnd":8318,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/lib.rs#L8282-L8318","documentation":"run_gh_pr_checkout runs `gh pr checkout <number>` (optionally with --repo) and bails with gh's trimmed stderr on non-zero exit. Checkout mutates local git state, so besides auth/PR problems the failure can come from the local work tree: conflicts, dirty files, or an existing branch name collision.","triggerScenarios":"Review flow attempting PR checkout with uncommitted local changes that conflict; gh auth expired; PR number not in the target repo; a local branch already using the name gh wants to create.","commonSituations":"Developer forgets to stash before reviewing; fork PRs whose head branch was deleted; running checkout from a detached HEAD; CI runners with no gh credentials.","solutions":["Commit or stash local changes (`git stash -u`) and retry","Run `gh auth status`; re-login if expired","Confirm the PR exists: `gh pr view <number>`","If a local branch name collides, remove/rename it first","Read the embedded gh stderr for the exact refusal"],"exampleFix":"# before\nrun_gh_pr_checkout   # gh pr checkout #4242 failed: Your local changes would be overwritten\n\n# after\ngit stash -u && gh pr checkout 4242   # then retry the review flow","handlingStrategy":"validation","validationCode":"git diff --quiet && git diff --cached --quiet || { echo 'dirty tree -- stashing'; git stash -u; }\ngh auth status >/dev/null 2>&1 || { echo 'gh not authenticated'; exit 1; }\ngh pr view \"$PR\" >/dev/null 2>&1 || { echo \"PR $PR not visible\"; exit 1; }","typeGuard":null,"tryCatchPattern":"match run_gh_pr_checkout(number, repo.as_deref()) {\n    Ok(()) => { /* proceed with review */ }\n    Err(e) if e.to_string().contains(\"would be overwritten\") => { git_stash_and_retry(); }\n    Err(e) => eprintln!(\"gh pr checkout failed: {e:#}\"),\n}","preventionTips":["Start PR review flows from a clean work tree (stash or commit first)","Keep gh authenticated; scripts should check `gh auth status` before checkout steps","Pull the target branch before checkout to reduce conflict surfaces"],"tags":["gh-cli","subprocess","pull-request","git-checkout","dirty-worktree"],"backgroundTag":"subprocess-nonzero-exit","analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}