node: Start working on git-fetch

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-08-22 17:40:12 +02:00
parent 15eed71588
commit 38f4bb52d8
No known key found for this signature in database
6 changed files with 40 additions and 13 deletions

24
node/src/git.rs Normal file
View File

@ -0,0 +1,24 @@
use std::net;
use crate::identity::ProjId;
use crate::storage::{Error, WriteStorage};
/// Default port of the `git` transport protocol.
pub const PROTOCOL_PORT: u16 = 9418;
pub fn fetch<S: WriteStorage>(
_proj: &ProjId,
remote: &net::SocketAddr,
mut storage: S,
) -> Result<(), Error> {
let _repo = storage.repository();
let url = format!("git://{}", remote);
let refs: &[&str] = &[];
let mut remote = git2::Remote::create_detached(&url)?;
let mut opts = git2::FetchOptions::default();
remote.fetch(refs, Some(&mut opts), None)?;
Ok(())
}

View File

@ -6,6 +6,7 @@ mod address_manager;
mod clock; mod clock;
mod collections; mod collections;
mod decoder; mod decoder;
mod git;
mod hash; mod hash;
mod identity; mod identity;
mod logger; mod logger;

View File

@ -15,6 +15,7 @@ use crate::address_manager::AddressManager;
use crate::clock::RefClock; use crate::clock::RefClock;
use crate::collections::{HashMap, HashSet}; use crate::collections::{HashMap, HashSet};
use crate::decoder::Decoder; use crate::decoder::Decoder;
use crate::git;
use crate::identity::{ProjId, UserId}; use crate::identity::{ProjId, UserId};
use crate::storage; use crate::storage;
use crate::storage::{Inventory, ReadStorage, Remotes, Unverified, WriteStorage}; use crate::storage::{Inventory, ReadStorage, Remotes, Unverified, WriteStorage};
@ -38,6 +39,7 @@ pub const MAX_CONNECTION_ATTEMPTS: usize = 3;
#[derive(Debug)] #[derive(Debug)]
pub enum Command { pub enum Command {
Connect(net::SocketAddr), Connect(net::SocketAddr),
Fetch(ProjId, net::SocketAddr),
} }
/// Message envelope. All messages sent over the network are wrapped in this type. /// Message envelope. All messages sent over the network are wrapped in this type.
@ -351,7 +353,7 @@ impl<T: ReadStorage + WriteStorage, S: address_book::Store> Protocol<S, T> {
impl<S, T> nakamoto::Protocol for Protocol<S, T> impl<S, T> nakamoto::Protocol for Protocol<S, T>
where where
T: ReadStorage + WriteStorage, T: ReadStorage + WriteStorage + 'static,
S: address_book::Store, S: address_book::Store,
{ {
type Event = (); type Event = ();
@ -416,6 +418,9 @@ where
match cmd { match cmd {
Command::Connect(addr) => self.context.connect(addr), Command::Connect(addr) => self.context.connect(addr),
Command::Fetch(proj, remote) => {
git::fetch(&proj, &remote, &mut self.storage).unwrap();
}
} }
} }

View File

@ -1,7 +1,7 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::path::Path; use std::path::Path;
use std::{fmt, fs, io, net}; use std::{fmt, fs, io};
use git_ref_format::refspec; use git_ref_format::refspec;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -100,8 +100,7 @@ pub trait ReadStorage {
} }
pub trait WriteStorage { pub trait WriteStorage {
/// Fetch a project from a remote peer. fn repository(&mut self) -> &mut git2::Repository;
fn fetch(&mut self, proj: &ProjId, remote: &net::SocketAddr) -> Result<(), Error>;
} }
impl<T, S> ReadStorage for T impl<T, S> ReadStorage for T
@ -121,10 +120,10 @@ where
impl<T, S> WriteStorage for T impl<T, S> WriteStorage for T
where where
T: DerefMut<Target = S>, T: DerefMut<Target = S>,
S: WriteStorage, S: WriteStorage + 'static,
{ {
fn fetch(&mut self, proj: &ProjId, remote: &net::SocketAddr) -> Result<(), Error> { fn repository(&mut self) -> &mut git2::Repository {
self.deref_mut().fetch(proj, remote) self.deref_mut().repository()
} }
} }
@ -159,8 +158,8 @@ impl ReadStorage for Storage {
} }
impl WriteStorage for Storage { impl WriteStorage for Storage {
fn fetch(&mut self, _id: &ProjId, _remote: &net::SocketAddr) -> Result<(), Error> { fn repository(&mut self) -> &mut git2::Repository {
todo!() &mut self.backend
} }
} }

View File

@ -57,7 +57,7 @@ impl<S> DerefMut for Peer<S> {
impl<S> Peer<S> impl<S> Peer<S>
where where
S: ReadStorage + WriteStorage, S: ReadStorage + WriteStorage + 'static,
{ {
pub fn new(name: &'static str, ip: impl Into<net::IpAddr>, storage: S) -> Self { pub fn new(name: &'static str, ip: impl Into<net::IpAddr>, storage: S) -> Self {
Self::config( Self::config(

View File

@ -1,5 +1,3 @@
use std::net;
use crate::identity::ProjId; use crate::identity::ProjId;
use crate::storage::{Error, Inventory, ReadStorage, Remotes, Unverified, WriteStorage}; use crate::storage::{Error, Inventory, ReadStorage, Remotes, Unverified, WriteStorage};
@ -40,7 +38,7 @@ impl ReadStorage for MockStorage {
} }
impl WriteStorage for MockStorage { impl WriteStorage for MockStorage {
fn fetch(&mut self, _proj: &ProjId, _remote: &net::SocketAddr) -> Result<(), Error> { fn repository(&mut self) -> &mut git2::Repository {
todo!() todo!()
} }
} }