From 557a5d98c6ea373db117d6fe19489086b4461aa7 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 5 Aug 2024 14:52:36 +0100 Subject: feat(remote): improve `list` warning copy so it is clearer and easier to read --- src/git_remote_helper.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index 8180175..12477da 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs @@ -21,7 +21,7 @@ use client::{ use git::RepoActions; use git2::{Oid, Repository}; use nostr::nips::nip01::Coordinate; -use nostr_sdk::{EventBuilder, PublicKey, Tag, Url}; +use nostr_sdk::{hashes::sha1::Hash as Sha1Hash, EventBuilder, PublicKey, Tag, Url}; use nostr_signer::NostrSigner; use repo_ref::RepoRef; use repo_state::RepoState; @@ -237,13 +237,22 @@ async fn list( if let Some(remote_value) = remote_state.get(name) { if value.ne(remote_value) { term.write_line( - format!("WARNING: {url} {name} out of sync with nostr ").as_str(), + format!( + "WARNING: {url} {name} is {} nostr ", + if let Ok((ahead, behind)) = + get_ahead_behind(git_repo, value, remote_value) + { + format!("{} ahead {} behind", ahead.len(), behind.len()) + } else { + "out of sync with".to_string() + } + ) + .as_str(), )?; } } else { term.write_line( - format!("WARNING: {url} is missing {name} which is tracked on nostr") - .as_str(), + format!("WARNING: {url} {name} is missing but tracked on nostr").as_str(), )?; } } @@ -319,6 +328,16 @@ fn list_from_remote( Ok(state) } +fn get_ahead_behind( + git_repo: &Repo, + base_ref_or_oid: &str, + latest_ref_or_oid: &str, +) -> Result<(Vec, Vec)> { + let base = git_repo.get_commit_or_tip_of_reference(base_ref_or_oid)?; + let latest = git_repo.get_commit_or_tip_of_reference(latest_ref_or_oid)?; + git_repo.get_commits_ahead_behind(&base, &latest) +} + fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, oid: &str) -> Result<()> { let oids = get_oids_from_fetch_batch(stdin, oid)?; -- cgit v1.2.3