diff --git a/radicle/src/git.rs b/radicle/src/git.rs index 75d458b2..06f2721e 100644 --- a/radicle/src/git.rs +++ b/radicle/src/git.rs @@ -8,6 +8,7 @@ use once_cell::sync::Lazy; use crate::collections::RandomMap; use crate::crypto::PublicKey; +use crate::node::Alias; use crate::storage; use crate::storage::refs::Refs; use crate::storage::RemoteId; @@ -666,6 +667,29 @@ pub mod env { ]; } +/// The user information used for signing commits and configuring the +/// `name` and `email` fields in the Git config. +#[derive(Debug, Clone)] +pub struct UserInfo { + /// Alias of the local peer. + pub alias: Alias, + /// [`PublicKey`] of the local peer. + pub key: PublicKey, +} + +impl UserInfo { + /// The name of the user, i.e. the `alias`. + pub fn name(&self) -> Alias { + self.alias.clone() + } + + /// The "email" of the user, which is in the form + /// `@`. + pub fn email(&self) -> String { + format!("{}@{}", self.alias, self.key) + } +} + #[cfg(test)] mod test { use super::*; diff --git a/radicle/src/storage.rs b/radicle/src/storage.rs index c2664129..45776d61 100644 --- a/radicle/src/storage.rs +++ b/radicle/src/storage.rs @@ -24,6 +24,7 @@ use crate::identity::{Id, Identity}; use crate::storage::git::NAMESPACES_GLOB; use crate::storage::refs::Refs; +use self::git::UserInfo; use self::refs::SignedRefs; pub type BranchName = git::RefString; @@ -507,6 +508,8 @@ pub trait WriteRepository: ReadRepository + SignRepository { } /// Set the repository 'rad/id' to the given commit. fn set_identity_head_to(&self, commit: Oid) -> Result<(), RepositoryError>; + /// Set the user info of the Git repository. + fn set_user(&self, info: &UserInfo) -> Result<(), Error>; /// Get the underlying git repository. fn raw(&self) -> &git2::Repository; } diff --git a/radicle/src/storage/git.rs b/radicle/src/storage/git.rs index d24a1ea3..7a47547b 100644 --- a/radicle/src/storage/git.rs +++ b/radicle/src/storage/git.rs @@ -637,6 +637,13 @@ impl WriteRepository for Repository { Ok(()) } + fn set_user(&self, info: &UserInfo) -> Result<(), Error> { + let mut config = self.backend.config()?; + config.set_str("user.name", &info.name())?; + config.set_str("user.email", &info.email())?; + Ok(()) + } + fn raw(&self) -> &git2::Repository { &self.backend } diff --git a/radicle/src/test/storage.rs b/radicle/src/test/storage.rs index 8eaa72f7..1bf81284 100644 --- a/radicle/src/test/storage.rs +++ b/radicle/src/test/storage.rs @@ -263,6 +263,10 @@ impl WriteRepository for MockRepository { fn set_identity_head_to(&self, _commit: Oid) -> Result<(), RepositoryError> { todo!() } + + fn set_user(&self, _info: &git::UserInfo) -> Result<(), Error> { + todo!() + } } impl SignRepository for MockRepository {