From f84b658530c0c0eaaaa0473add8c8c359fa6b09e Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 16 Feb 2026 22:39:51 +0000 Subject: feat: add --offline flag to list, checkout, apply commands Skip network fetch when --offline is set, using only local cache --- src/bin/ngit/cli.rs | 9 +++++++++ src/bin/ngit/main.rs | 19 ++++++++++++++----- src/bin/ngit/sub_commands/apply.rs | 12 +++++++----- src/bin/ngit/sub_commands/checkout.rs | 12 +++++++----- src/bin/ngit/sub_commands/list.rs | 16 +++++++++------- 5 files changed, 46 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/bin/ngit/cli.rs b/src/bin/ngit/cli.rs index c77e719..85c74cd 100644 --- a/src/bin/ngit/cli.rs +++ b/src/bin/ngit/cli.rs @@ -117,11 +117,17 @@ pub enum Commands { json: bool, /// Show details for specific proposal (event-id or nevent) id: Option, + /// Use local cache only, skip network fetch + #[arg(long)] + offline: bool, }, /// checkout a proposal branch by event-id or nevent Checkout { /// Proposal event-id (hex) or nevent (bech32) id: String, + /// Use local cache only, skip network fetch + #[arg(long)] + offline: bool, }, /// apply proposal patches to current branch Apply { @@ -130,6 +136,9 @@ pub enum Commands { /// Output patches to stdout instead of applying #[arg(long)] stdout: bool, + /// Use local cache only, skip network fetch + #[arg(long)] + offline: bool, }, /// update repo git servers to reflect nostr state (add, update or delete /// remote refs) diff --git a/src/bin/ngit/main.rs b/src/bin/ngit/main.rs index 77ef955..5458251 100644 --- a/src/bin/ngit/main.rs +++ b/src/bin/ngit/main.rs @@ -49,13 +49,22 @@ async fn main() { } }, Commands::Init(args) => sub_commands::init::launch(&cli, args).await, - Commands::List { status, json, id } => { - sub_commands::list::launch(status.clone(), *json, id.clone()).await - } + Commands::List { + status, + json, + id, + offline, + } => sub_commands::list::launch(status.clone(), *json, id.clone(), *offline).await, Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, Commands::Sync(args) => sub_commands::sync::launch(args).await, - Commands::Checkout { id } => sub_commands::checkout::launch(id).await, - Commands::Apply { id, stdout } => sub_commands::apply::launch(id, *stdout).await, + Commands::Checkout { id, offline } => { + sub_commands::checkout::launch(id, *offline).await + } + Commands::Apply { + id, + stdout, + offline, + } => sub_commands::apply::launch(id, *stdout, *offline).await, } } else { // Handle the case where no command is provided diff --git a/src/bin/ngit/sub_commands/apply.rs b/src/bin/ngit/sub_commands/apply.rs index 2d5d8d5..4b13975 100644 --- a/src/bin/ngit/sub_commands/apply.rs +++ b/src/bin/ngit/sub_commands/apply.rs @@ -73,7 +73,7 @@ fn run_git_fetch(remote_name: &str) -> Result<()> { Ok(()) } -pub async fn launch(id: &str, stdout: bool) -> Result<()> { +pub async fn launch(id: &str, stdout: bool, offline: bool) -> Result<()> { let event_id = parse_event_id(id)?; let git_repo = Repo::discover().context("failed to find a git repository")?; @@ -91,10 +91,12 @@ pub async fn launch(id: &str, stdout: bool) -> Result<()> { .ok() .flatten(); - if let Some((remote_name, _)) = &nostr_remote { - run_git_fetch(remote_name)?; - } else { - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + if !offline { + if let Some((remote_name, _)) = &nostr_remote { + run_git_fetch(remote_name)?; + } else { + fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + } } let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await?; diff --git a/src/bin/ngit/sub_commands/checkout.rs b/src/bin/ngit/sub_commands/checkout.rs index 2fc9a09..19e39d0 100644 --- a/src/bin/ngit/sub_commands/checkout.rs +++ b/src/bin/ngit/sub_commands/checkout.rs @@ -28,7 +28,7 @@ use crate::{ repo_ref::get_repo_coordinates_when_remote_unknown, }; -pub async fn launch(id: &str) -> Result<()> { +pub async fn launch(id: &str, offline: bool) -> Result<()> { let event_id = parse_event_id(id)?; let git_repo = Repo::discover().context("failed to find a git repository")?; @@ -44,10 +44,12 @@ pub async fn launch(id: &str) -> Result<()> { .ok() .flatten(); - if let Some((remote_name, _)) = &nostr_remote { - run_git_fetch(remote_name)?; - } else { - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + if !offline { + if let Some((remote_name, _)) = &nostr_remote { + run_git_fetch(remote_name)?; + } else { + fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + } } let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await?; diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs index fc0883a..80eec21 100644 --- a/src/bin/ngit/sub_commands/list.rs +++ b/src/bin/ngit/sub_commands/list.rs @@ -95,7 +95,7 @@ fn run_git_fetch(remote_name: &str) -> Result<()> { } #[allow(clippy::too_many_lines)] -pub async fn launch(status: String, json: bool, id: Option) -> Result<()> { +pub async fn launch(status: String, json: bool, id: Option, offline: bool) -> Result<()> { if std::env::var("NGIT_INTERACTIVE_MODE").is_ok() { return launch_interactive().await; } @@ -113,14 +113,16 @@ pub async fn launch(status: String, json: bool, id: Option) -> Result<() .ok() .flatten(); - if let Some((remote_name, _)) = &nostr_remote { - if std::env::var("NGITTEST").is_ok() { - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + if !offline { + if let Some((remote_name, _)) = &nostr_remote { + if std::env::var("NGITTEST").is_ok() { + fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; + } else { + run_git_fetch(remote_name)?; + } } else { - run_git_fetch(remote_name)?; + fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; } - } else { - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; } let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await?; -- cgit v1.2.3