15b1ee6b
Extract staleness_warning helper, use latest revision not first
a73x 2026-03-21 19:27
Flatten 5-level nesting into Option-chaining helper. Compare against latest revision's commit (where the branch is now) not the first. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
diff --git a/src/lib.rs b/src/lib.rs index 0edfa02..ce4f804 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,27 @@ use event::ReviewVerdict; use git2::Repository; use state::{IssueStatus, PatchStatus}; /// Check if the reviewer's base ref has moved ahead of the patch's latest revision. fn staleness_warning(repo: &Repository, patch: &state::PatchState) -> Option<String> { let latest_commit = &patch.revisions.last()?.commit; let commit_oid = git2::Oid::from_str(latest_commit).ok()?; let base_ref = format!("refs/heads/{}", patch.base_ref); let base_tip = repo.refname_to_id(&base_ref).ok()?; let merge_base = repo.merge_base(commit_oid, base_tip).ok()?; if merge_base == base_tip { return None; } let (ahead, _) = repo.graph_ahead_behind(base_tip, merge_base).ok()?; if ahead == 0 { return None; } let mb_short = &merge_base.to_string()[..8]; Some(format!( "\u{26a0} Based on {}@{} ({} commits behind your {})", patch.base_ref, mb_short, ahead, patch.base_ref )) } pub fn run(cli: cli::Cli, repo: &Repository) -> Result<(), error::Error> { match cli.command { Commands::Init => sync::init(repo), @@ -241,43 +262,9 @@ pub fn run(cli: cli::Cli, repo: &Repository) -> Result<(), error::Error> { if let Some(ref fixes) = p.fixes { println!("Fixes: {:.8}", fixes); } // Staleness warning: check if base ref has moved ahead of patch's original commit if !p.revisions.is_empty() { let commit_oid_str = &p.revisions[0].commit; match git2::Oid::from_str(commit_oid_str) { Ok(commit_oid) => { let base_ref_name = format!("refs/heads/{}", p.base_ref); match repo.refname_to_id(&base_ref_name) { Ok(base_tip) => { match repo.merge_base(commit_oid, base_tip) { Ok(merge_base) => { if merge_base != base_tip { let (ahead, _behind) = repo .graph_ahead_behind(base_tip, merge_base) .unwrap_or((0, 0)); if ahead > 0 { let mb_short = &merge_base.to_string()[..8]; eprintln!( "\u{26a0} Based on {}@{} ({} commits behind your {})", p.base_ref, mb_short, ahead, p.base_ref ); } } } Err(_) => { eprintln!("\u{26a0} Patch base commit not found locally"); } } } Err(_) => { // Base ref doesn't exist locally, skip warning } } } Err(_) => { eprintln!("\u{26a0} Patch base commit not found locally"); } } // Staleness warning if let Some(warning) = staleness_warning(repo, &p) { eprintln!("{}", warning); } // Show revisions if !p.revisions.is_empty() {