From 09c3ae91830bd9c7543b401b19f8c65a15205d32 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 5 Mar 2026 15:03:37 +0000 Subject: fix(whoami): detect and fall back to system git config for nostr login Add GitSystem to SignerInfoSource so credentials stored in the system git config (/etc/gitconfig) are included in the priority fallback chain (local > global > system) and shown as a separate level in whoami output. --- src/bin/ngit/sub_commands/whoami.rs | 77 ++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 27 deletions(-) (limited to 'src/bin') diff --git a/src/bin/ngit/sub_commands/whoami.rs b/src/bin/ngit/sub_commands/whoami.rs index 19ce573..5c0a461 100644 --- a/src/bin/ngit/sub_commands/whoami.rs +++ b/src/bin/ngit/sub_commands/whoami.rs @@ -41,8 +41,10 @@ struct WhoamiJson { local: Option, #[serde(skip_serializing_if = "Option::is_none")] global: Option, + #[serde(skip_serializing_if = "Option::is_none")] + system: Option, /// The account that would be used for operations in the current context - /// (local takes priority over global). + /// (local > global > system, matching git's priority order). #[serde(skip_serializing_if = "Option::is_none")] active: Option, } @@ -62,7 +64,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { let signer_info = extract_signer_cli_arguments(args).unwrap_or(None); - // Try to load local login (silent, no prompts) + // Try to load login from each config level (silent, no prompts) let local = load_user_for_scope( git_repo.as_ref(), signer_info.as_ref(), @@ -71,7 +73,6 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { ) .await; - // Try to load global login (silent, no prompts) let global = load_user_for_scope( git_repo.as_ref(), signer_info.as_ref(), @@ -80,45 +81,67 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { ) .await; + let system = load_user_for_scope( + git_repo.as_ref(), + signer_info.as_ref(), + client.as_ref(), + SignerInfoSource::GitSystem, + ) + .await; + if let Some(client) = client { client.disconnect().await?; } + // Active account follows git's priority order: local > global > system + let active_scope = if local.is_some() { + Some("local") + } else if global.is_some() { + Some("global") + } else if system.is_some() { + Some("system") + } else { + None + }; + if command_args.json { - // active = local if present, else global - let active = local - .as_ref() - .map(|u| make_user_json(u, "local")) - .or_else(|| global.as_ref().map(|u| make_user_json(u, "global"))); + let active = active_scope.and_then(|scope| match scope { + "local" => local.as_ref().map(|u| make_user_json(u, scope)), + "global" => global.as_ref().map(|u| make_user_json(u, scope)), + "system" => system.as_ref().map(|u| make_user_json(u, scope)), + _ => None, + }); let output = WhoamiJson { local: local.as_ref().map(|u| make_user_json(u, "local")), global: global.as_ref().map(|u| make_user_json(u, "global")), + system: system.as_ref().map(|u| make_user_json(u, "system")), active, }; println!("{}", serde_json::to_string_pretty(&output)?); + } else if local.is_none() && global.is_none() && system.is_none() { + println!("not logged in"); + println!(); + println!("use `ngit account login` to log in"); } else { - match (local.as_ref(), global.as_ref()) { - (None, None) => { - println!("not logged in"); - println!(); - println!("use `ngit account login` to log in"); - } - (Some(u), None) => { - println!("logged in to local repository as:"); + type UserEntry = Option<(String, String, Option)>; + let entries: &[(&str, &UserEntry)] = + &[("local", &local), ("global", &global), ("system", &system)]; + let mut first = true; + for (scope, user) in entries { + if let Some(u) = user { + if !first { + println!(); + } + first = false; + let is_active = active_scope == Some(scope); + if is_active { + println!("{scope} (active):"); + } else { + println!("{scope}:"); + } print_user_human(u); } - (None, Some(u)) => { - println!("logged in globally as:"); - print_user_human(u); - } - (Some(local_u), Some(global_u)) => { - println!("local (active):"); - print_user_human(local_u); - println!(); - println!("global:"); - print_user_human(global_u); - } } } -- cgit v1.2.3