Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use clap::{Args, Parser, Subcommand};
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,

/// Show PR status (shortcut for `gw log --pr`)
#[arg(long, global = true)]
pub pr: bool,
}

#[derive(Subcommand)]
Expand Down
6 changes: 5 additions & 1 deletion src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Vec::new();
Expand Down
6 changes: 5 additions & 1 deletion src/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down
64 changes: 42 additions & 22 deletions src/gh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <branch>` 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<String, PrInfo> {
pub fn batch_pr_status(branches: &[&str]) -> HashMap<String, PrInfo> {
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<std::process::Child>)> = 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;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down