From 41033889bd4708b5271739abcafe347eeaed972a Mon Sep 17 00:00:00 2001 From: Jon Deibel Date: Mon, 9 Mar 2026 10:36:21 -0400 Subject: [PATCH 1/2] fix: query PR status per-branch instead of using global limit The old approach used gh pr list --limit 100 which missed PRs in repos with many open PRs. Now queries each tracked branch with gh pr list --head , spawned in parallel so latency is roughly one network round-trip regardless of branch count. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/commands/sync.rs | 6 ++++- src/commands/tree.rs | 6 ++++- src/gh.rs | 64 +++++++++++++++++++++++++++++--------------- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/commands/sync.rs b/src/commands/sync.rs index acdef63..0eedbd2 100644 --- a/src/commands/sync.rs +++ b/src/commands/sync.rs @@ -33,7 +33,11 @@ pub fn run(args: SyncArgs, ctx: &Ctx) -> Result<()> { let _ = ctx.git.run(&["fetch", "--prune", "origin"]); // Batch PR status once for all merge detection - let pr_map = gh::batch_pr_status(); + let branch_names: Vec<&str> = stacks + .iter() + .flat_map(|s| s.branches.iter().map(|b| b.name.as_str())) + .collect(); + let pr_map = gh::batch_pr_status(&branch_names); let mut synced_bases = std::collections::HashSet::new(); let mut branches_to_delete: Vec = Vec::new(); diff --git a/src/commands/tree.rs b/src/commands/tree.rs index 766dd05..ca214f0 100644 --- a/src/commands/tree.rs +++ b/src/commands/tree.rs @@ -167,7 +167,11 @@ pub fn run(ctx: &Ctx, show_pr: bool) -> Result<()> { // PR status (only when requested) let pr_status = if show_pr { - gh::batch_pr_status() + let branch_names: Vec<&str> = stacks + .iter() + .flat_map(|s| s.branches.iter().map(|b| b.name.as_str())) + .collect(); + gh::batch_pr_status(&branch_names) } else { HashMap::new() }; diff --git a/src/gh.rs b/src/gh.rs index 9d30770..cdf2fec 100644 --- a/src/gh.rs +++ b/src/gh.rs @@ -7,33 +7,53 @@ pub struct PrInfo { pub state: String, } -/// Batch-fetch PR status for all branches via a single gh CLI call. +/// Fetch PR status for the given branches via parallel gh CLI calls. +/// Each branch gets its own `gh pr list --head ` query, spawned +/// concurrently so the total latency is ~one network round-trip. /// Returns a map of branch_name -> PrInfo. /// Returns empty map if gh is not available or fails. -pub fn batch_pr_status() -> HashMap { +pub fn batch_pr_status(branches: &[&str]) -> HashMap { let mut result = HashMap::new(); + if branches.is_empty() { + return result; + } - let output = Command::new("gh") - .args([ - "pr", "list", "--state", "all", "--json", - "headRefName,number,state", "--limit", "100", - ]) - .output(); - - let output = match output { - Ok(o) if o.status.success() => o, - _ => return result, - }; - - let stdout = String::from_utf8_lossy(&output.stdout); + // Spawn all queries in parallel + let children: Vec<(&str, Option)> = branches + .iter() + .map(|&branch| { + let child = Command::new("gh") + .args([ + "pr", "list", "--head", branch, "--state", "all", "--json", + "number,state", "--limit", "1", + ]) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .ok(); + (branch, child) + }) + .collect(); - for chunk in stdout.split('{') { - if let (Some(branch), Some(number), Some(state)) = ( - extract_json_string(chunk, "headRefName"), - extract_json_number(chunk, "number"), - extract_json_string(chunk, "state"), - ) { - result.insert(branch, PrInfo { number, state }); + // Collect results + for (branch, child) in children { + let Some(child) = child else { continue }; + let Ok(output) = child.wait_with_output() else { + continue; + }; + if !output.status.success() { + continue; + } + let stdout = String::from_utf8_lossy(&output.stdout); + // Parse the first (and only) result from the JSON array + for chunk in stdout.split('{') { + if let (Some(number), Some(state)) = ( + extract_json_number(chunk, "number"), + extract_json_string(chunk, "state"), + ) { + result.insert(branch.to_string(), PrInfo { number, state }); + break; + } } } From 57ef7152c9feac50d5eb2124028f0669573916ca Mon Sep 17 00:00:00 2001 From: Jon Deibel Date: Mon, 9 Mar 2026 10:36:27 -0400 Subject: [PATCH 2/2] feat: add gw --pr shortcut for gw log --pr Co-Authored-By: Claude Opus 4.6 (1M context) --- src/cli.rs | 4 ++++ src/main.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index b9935dd..482c137 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,6 +6,10 @@ use clap::{Args, Parser, Subcommand}; pub struct Cli { #[command(subcommand)] pub command: Option, + + /// Show PR status (shortcut for `gw log --pr`) + #[arg(long, global = true)] + pub pr: bool, } #[derive(Subcommand)] diff --git a/src/main.rs b/src/main.rs index a6aa29a..cbe62b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn main() -> Result<()> { Some(cmd) => cmd, None => { let ctx = Ctx::discover()?; - return commands::tree::run(&ctx, false); + return commands::tree::run(&ctx, cli.pr); } };