a73x

3daf97c1

Surface staleness warning when patch base ref has moved ahead

a73x   2026-03-21 19:26

When running `patch show`, check whether the patch's base ref has
advanced since the patch was created. If so, print a warning with
the number of commits the base is ahead, helping reviewers decide
whether to request a rebase.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

diff --git a/src/lib.rs b/src/lib.rs
index 7d459b0..0edfa02 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -241,6 +241,44 @@ 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");
                        }
                    }
                }
                // Show revisions
                if !p.revisions.is_empty() {
                    println!("\n--- Revisions ---");