node: Simplify `init`

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-09-01 01:12:23 +02:00
parent 68099688e6
commit 993d88101c
No known key found for this signature in database
1 changed files with 6 additions and 12 deletions

View File

@ -5,7 +5,7 @@ use nonempty::NonEmpty;
use thiserror::Error; use thiserror::Error;
use crate::git; use crate::git;
use crate::identity::{ProjId, UserId}; use crate::identity::ProjId;
use crate::storage::git::RADICLE_ID_REF; use crate::storage::git::RADICLE_ID_REF;
use crate::storage::ReadRepository as _; use crate::storage::ReadRepository as _;
use crate::{identity, storage}; use crate::{identity, storage};
@ -35,13 +35,13 @@ pub fn init<S: storage::WriteStorage>(
repo: &git2::Repository, repo: &git2::Repository,
name: &str, name: &str,
description: &str, description: &str,
delegate: UserId,
storage: S, storage: S,
) -> Result<ProjId, InitError> { ) -> Result<ProjId, InitError> {
let user_id = storage.user_id();
let delegate = identity::Delegate { let delegate = identity::Delegate {
// TODO: Use actual user name. // TODO: Use actual user name.
name: String::from("anonymous"), name: String::from("anonymous"),
id: identity::Did::from(delegate), id: identity::Did::from(*user_id),
}; };
let head = repo.head()?; let head = repo.head()?;
@ -61,7 +61,7 @@ pub fn init<S: storage::WriteStorage>(
}; };
let sig = repo let sig = repo
.signature() .signature()
.or_else(|_| git2::Signature::now("anonymous", "anonymous@anonymous.xyz"))?; .or_else(|_| git2::Signature::now("anonymous", user_id.to_string().as_str()))?;
let filename = *identity::IDENTITY_PATH; let filename = *identity::IDENTITY_PATH;
let path = repo.workdir().ok_or(InitError::BareRepo)?.join(filename); let path = repo.workdir().ok_or(InitError::BareRepo)?.join(filename);
@ -99,7 +99,6 @@ pub fn init<S: storage::WriteStorage>(
..Url::default() ..Url::default()
}; };
let user_id = storage.user_id();
let fetch = format!("+refs/remotes/{user_id}/heads/*:refs/remotes/rad/*"); let fetch = format!("+refs/remotes/{user_id}/heads/*:refs/remotes/rad/*");
let push = format!("refs/heads/*:refs/remotes/{user_id}/heads/*"); let push = format!("refs/heads/*:refs/remotes/{user_id}/heads/*");
let mut remote = repo.remote_with_fetch(REMOTE_NAME, url.to_string().as_str(), &fetch)?; let mut remote = repo.remote_with_fetch(REMOTE_NAME, url.to_string().as_str(), &fetch)?;
@ -129,7 +128,6 @@ pub fn init<S: storage::WriteStorage>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::crypto::Signer;
use crate::git; use crate::git;
use crate::storage::git::Storage; use crate::storage::git::Storage;
use crate::test::crypto; use crate::test::crypto;
@ -143,12 +141,8 @@ mod tests {
let sig = git2::Signature::now("anonymous", "anonymous@radicle.xyz").unwrap(); let sig = git2::Signature::now("anonymous", "anonymous@radicle.xyz").unwrap();
let head = git::initial_commit(&repo, &sig).unwrap(); let head = git::initial_commit(&repo, &sig).unwrap();
let head = git::commit(&repo, &head, "Second commit", "anonymous").unwrap(); let head = git::commit(&repo, &head, "Second commit", "anonymous").unwrap();
let _branch = repo.branch("master", &head, false).unwrap();
repo.branch("master", &head, false).unwrap(); init(&repo, "acme", "Acme's repo", &mut storage).unwrap();
let signer = crypto::MockSigner::new(&mut fastrand::Rng::new());
let delegate = *signer.public_key();
init(&repo, "acme", "Acme's repo", delegate, &mut storage).unwrap();
} }
} }