From c72ff8f9527c845a91d0c728b2b76df738179af2 Mon Sep 17 00:00:00 2001 From: Jon Deibel Date: Sat, 7 Mar 2026 23:55:50 -0500 Subject: [PATCH] feat: add drift commit counts and fix behind-remote bugs in status/tree Enhance gw status to show commit counts for drift detection instead of boolean flags. Fix two bugs in both status.rs and tree.rs where branches that are only behind remote or have a [gone] remote incorrectly report as up-to-date. Changes: - Show "N behind parent, needs rebase" instead of "needs rebase" - Show "diverged (N ahead, M behind remote)" instead of "diverged" - Fix behind-remote-only bug (was falling through to UpToDate) - Handle [gone] tracking state as NoRemote - Add Behind variant to tree.rs RemoteStatus for consistency - Consolidate rev_parse + boolean into rev_list_count (net 0 new git calls) - Add parse_count_after helper with unit tests Co-Authored-By: Claude Opus 4.6 --- Cargo.lock | 98 ++++++------------------------------------ src/commands/status.rs | 72 +++++++++++++++++++++++++------ src/commands/tree.rs | 6 +++ 3 files changed, 77 insertions(+), 99 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aea63b2..839f06f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -896,12 +896,12 @@ checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] name = "socket2" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -1274,16 +1274,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets 0.53.5", + "windows-targets", ] [[package]] @@ -1301,31 +1292,14 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm 0.53.1", - "windows_aarch64_msvc 0.53.1", - "windows_i686_gnu 0.53.1", - "windows_i686_gnullvm 0.53.1", - "windows_i686_msvc 0.53.1", - "windows_x86_64_gnu 0.53.1", - "windows_x86_64_gnullvm 0.53.1", - "windows_x86_64_msvc 0.53.1", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] @@ -1334,96 +1308,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" - [[package]] name = "winnow" version = "0.7.15" diff --git a/src/commands/status.rs b/src/commands/status.rs index 76dfa01..b0ab533 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -30,8 +30,13 @@ pub fn run(ctx: &Ctx) -> Result<()> { // Gather git info let merge_base = ctx.git.merge_base(&parent, ¤t)?; let commits = ctx.git.log_oneline(&merge_base, "HEAD", 100)?; - let parent_sha = ctx.git.rev_parse(&parent)?; - let needs_rebase = merge_base != parent_sha; + let behind_parent: usize = ctx + .git + .run(&["rev-list", "--count", &format!("{merge_base}..{parent}")]) + .ok() + .and_then(|s| s.trim().parse().ok()) + .unwrap_or(0); + let needs_rebase = behind_parent > 0; // Remote status let remote_status = remote_info(ctx, ¤t); @@ -127,8 +132,15 @@ pub fn run(ctx: &Ctx) -> Result<()> { RemoteStatus::NeedsPush(ahead) => { flags.push(format!("{}", format!("{ahead} ahead, needs push").yellow())); } - RemoteStatus::Diverged => { - flags.push(format!("{}", "diverged from remote".yellow())); + RemoteStatus::Behind(behind) => { + flags.push(format!("{}", format!("{behind} behind remote").yellow())); + } + RemoteStatus::Diverged { ahead, behind } => { + flags.push( + format!("diverged ({ahead} ahead, {behind} behind remote)") + .yellow() + .to_string(), + ); } RemoteStatus::NoRemote => { flags.push(format!("{}", "not pushed".dimmed())); @@ -136,7 +148,11 @@ pub fn run(ctx: &Ctx) -> Result<()> { } if needs_rebase { - flags.push(format!("{}", "needs rebase".yellow())); + flags.push( + format!("{behind_parent} behind parent, needs rebase") + .yellow() + .to_string(), + ); } for flag in &flags { @@ -196,7 +212,8 @@ fn working_tree_info(ctx: &Ctx) -> WorkingTree { enum RemoteStatus { UpToDate, NeedsPush(usize), - Diverged, + Behind(usize), + Diverged { ahead: usize, behind: usize }, NoRemote, } @@ -224,19 +241,48 @@ fn remote_info(ctx: &Ctx, branch: &str) -> RemoteStatus { return RemoteStatus::UpToDate; } + if track.contains("gone") { + return RemoteStatus::NoRemote; + } + if track.contains("ahead") && track.contains("behind") { - return RemoteStatus::Diverged; + let ahead = parse_count_after(track, "ahead "); + let behind = parse_count_after(track, "behind "); + return RemoteStatus::Diverged { ahead, behind }; } if track.contains("ahead") { - // Parse the number from "[ahead N]" - let ahead = track - .split("ahead ") - .nth(1) - .and_then(|s| s.trim_end_matches(']').parse::().ok()) - .unwrap_or(1); + let ahead = parse_count_after(track, "ahead "); return RemoteStatus::NeedsPush(ahead); } + if track.contains("behind") { + let behind = parse_count_after(track, "behind "); + return RemoteStatus::Behind(behind); + } + RemoteStatus::UpToDate } + +fn parse_count_after(s: &str, prefix: &str) -> usize { + s.split(prefix) + .nth(1) + .and_then(|rest| rest.split(|c: char| !c.is_ascii_digit()).next()) + .and_then(|n| n.parse().ok()) + .unwrap_or(1) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_count_after() { + assert_eq!(parse_count_after("[ahead 3]", "ahead "), 3); + assert_eq!(parse_count_after("[behind 7]", "behind "), 7); + assert_eq!(parse_count_after("[ahead 2, behind 5]", "ahead "), 2); + assert_eq!(parse_count_after("[ahead 2, behind 5]", "behind "), 5); + assert_eq!(parse_count_after("[ahead 100]", "ahead "), 100); + assert_eq!(parse_count_after("garbage", "ahead "), 1); // fallback + } +} diff --git a/src/commands/tree.rs b/src/commands/tree.rs index 02abbef..b917898 100644 --- a/src/commands/tree.rs +++ b/src/commands/tree.rs @@ -402,6 +402,7 @@ fn format_branch_line( match ri.remote_status { RemoteStatus::Diverged => tags.push("diverged".yellow().to_string()), RemoteStatus::NeedsPush => tags.push("needs push".yellow().to_string()), + RemoteStatus::Behind => tags.push("behind remote".yellow().to_string()), _ => {} } } @@ -425,6 +426,7 @@ fn format_branch_line( enum RemoteStatus { UpToDate, NeedsPush, + Behind, Diverged, NoRemote, } @@ -460,10 +462,14 @@ fn batch_ref_info(ctx: &Ctx) -> HashMap { RemoteStatus::NoRemote } else if track.is_empty() { RemoteStatus::UpToDate + } else if track.contains("gone") { + RemoteStatus::NoRemote } else if track.contains("ahead") && track.contains("behind") { RemoteStatus::Diverged } else if track.contains("ahead") { RemoteStatus::NeedsPush + } else if track.contains("behind") { + RemoteStatus::Behind } else { RemoteStatus::UpToDate };