//! Identity, verification, and configuration commands. use super::format::{short_id, signature_line, when}; use super::open; use anyhow::Result; use oxy_core::hash::ChangeHash; use oxy_core::repo::{self, IdentityScope}; pub fn create(name: Option, email: Option, local: bool) -> Result { let mut repo = open()?; let name = name.unwrap_or_else(|| repo.config.author_name.clone()); let email = email.unwrap_or_else(|| repo.config.author_email.clone()); let scope = if local { IdentityScope::Repo } else { IdentityScope::User }; let (key, _) = repo.create_identity(&name, &email, scope)?; println!("Created identity {name} <{email}>"); println!("KeyId: {key}"); println!("Secret key stored under {}", repo::default_keys_dir().display()); match scope { IdentityScope::User => println!("Recorded as your default author in {}", repo.user_config_path().display()), IdentityScope::Repo => println!("Recorded in this repository's .oxy/config only"), } if !repo.config.sign_changes { println!("Changes will carry this author; run `oxy config --user core.sign change` to sign each commit."); } Ok(0) } pub fn show() -> Result { let repo = open()?; let Some(key) = repo.config.author_key else { println!("No identity configured; run `oxy identity create`."); return Ok(0); }; let id = repo.identity(&key)?; println!("{} <{}>", id.name, id.email); println!("KeyId: {key}"); println!("Created: {}", when(id.created)); println!("Secret: {}", if repo.secret_key()?.is_some() { "available" } else { "NOT FOUND in key directory" }); println!("Signing: {}", if repo.config.sign_changes { "every change (core.sign = change)" } else { "at push via seals (core.sign = seal)" }); Ok(0) } pub fn verify(change: Option<&str>) -> Result { let repo = open()?; let hashes: Vec = match change { Some(c) => vec![repo.resolve_change(c)?], None => repo.log()?.into_iter().filter(|e| !e.is_working_copy).map(|e| e.hash).collect(), }; let mut bad = 0; for h in hashes { let c = repo.store().get_change(&h)?; let line = signature_line(&repo, &h)?; if line.starts_with("INVALID") { bad += 1; } println!("{} #{} {line}", short_id(&c), h.short(12)); } Ok(if bad > 0 { 1 } else { 0 }) } pub fn config(key: &str, value: &str, user: bool) -> Result { if user { let path = repo::default_user_config(); repo::Config::set_user(&path, key, value)?; println!("{key} = {value} (in {})", path.display()); } else { let mut repo = open()?; repo.config.set(key, value)?; repo.save_config()?; println!("{key} = {value} (in this repository)"); } Ok(0) }