Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: add UserInfo to git
Fintan Halpenny committed 2 years ago
commit dfdf2e20ccef961376d77f0563d04764332b2ad8
parent ef6ae0fa84b912c2cbfffae93d0b24269be3a860
4 files changed +38 -0
modified 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
+
    /// `<alias>@<public key>`.
+
    pub fn email(&self) -> String {
+
        format!("{}@{}", self.alias, self.key)
+
    }
+
}
+

#[cfg(test)]
mod test {
    use super::*;
modified 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;
}
modified 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
    }
modified 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 {