radicle: add UserInfo to git

Add a type to capture user information that can be used for Git
configuration and signatures.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-09-06 10:56:26 +01:00 committed by cloudhead
parent ef6ae0fa84
commit dfdf2e20cc
No known key found for this signature in database
4 changed files with 38 additions and 0 deletions

View File

@ -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::*;

View File

@ -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;
}

View File

@ -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
}

View File

@ -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 {