node: Use `Url` type
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
parent
f18afb793c
commit
45d6de80e8
|
|
@ -4,6 +4,7 @@ use crate::collections::HashMap;
|
||||||
use crate::identity::UserId;
|
use crate::identity::UserId;
|
||||||
use crate::storage::{Remote, Remotes, Unverified};
|
use crate::storage::{Remote, Remotes, Unverified};
|
||||||
use git_ref_format as format;
|
use git_ref_format as format;
|
||||||
|
use git_url::Url;
|
||||||
|
|
||||||
/// Default port of the `git` transport protocol.
|
/// Default port of the `git` transport protocol.
|
||||||
pub const PROTOCOL_PORT: u16 = 9418;
|
pub const PROTOCOL_PORT: u16 = 9418;
|
||||||
|
|
@ -25,9 +26,10 @@ pub enum ListRefsError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List remote refs of a project, given the remote URL.
|
/// List remote refs of a project, given the remote URL.
|
||||||
pub fn list_remotes(url: &str) -> Result<Remotes<Unverified>, ListRefsError> {
|
pub fn list_remotes(url: &Url) -> Result<Remotes<Unverified>, ListRefsError> {
|
||||||
|
let url = url.to_string();
|
||||||
let mut remotes = HashMap::default();
|
let mut remotes = HashMap::default();
|
||||||
let mut remote = git2::Remote::create_detached(url)?;
|
let mut remote = git2::Remote::create_detached(&url)?;
|
||||||
|
|
||||||
remote.connect(git2::Direction::Fetch)?;
|
remote.connect(git2::Direction::Fetch)?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -454,7 +454,14 @@ where
|
||||||
self.storage
|
self.storage
|
||||||
.repository(&proj)
|
.repository(&proj)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.fetch(&format!("git://{}", remote))
|
.fetch(&Url {
|
||||||
|
scheme: git_url::Scheme::Git,
|
||||||
|
host: Some(remote.ip().to_string()),
|
||||||
|
port: Some(remote.port()),
|
||||||
|
// TODO: Fix upstream crate so that it adds a `/` when needed.
|
||||||
|
path: format!("/{}", proj).into(),
|
||||||
|
..Url::default()
|
||||||
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use std::marker::PhantomData;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use git_url::Url;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
@ -130,7 +131,7 @@ pub trait ReadRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WriteRepository {
|
pub trait WriteRepository {
|
||||||
fn fetch(&mut self, url: &str) -> Result<(), git2::Error>;
|
fn fetch(&mut self, url: &Url) -> Result<(), git2::Error>;
|
||||||
fn namespace(&mut self, user: &UserId) -> Result<&mut git2::Repository, git2::Error>;
|
fn namespace(&mut self, user: &UserId) -> Result<&mut git2::Repository, git2::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::{fmt, fs};
|
use std::{fmt, fs};
|
||||||
|
|
||||||
use git_ref_format::refspec;
|
use git_ref_format::refspec;
|
||||||
|
use git_url::Url;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use radicle_git_ext as git_ext;
|
use radicle_git_ext as git_ext;
|
||||||
|
|
||||||
|
|
@ -174,22 +175,21 @@ impl ReadRepository for Repository {
|
||||||
|
|
||||||
impl WriteRepository for Repository {
|
impl WriteRepository for Repository {
|
||||||
/// Fetch all remotes of a project from the given URL.
|
/// Fetch all remotes of a project from the given URL.
|
||||||
fn fetch(&mut self, url: &str) -> Result<(), git2::Error> {
|
fn fetch(&mut self, url: &Url) -> Result<(), git2::Error> {
|
||||||
// TODO: Use `Url` type?
|
|
||||||
// TODO: Have function to fetch specific remotes.
|
// TODO: Have function to fetch specific remotes.
|
||||||
// TODO: Return meaningful info on success.
|
// TODO: Return meaningful info on success.
|
||||||
//
|
//
|
||||||
// Repository layout should look like this:
|
// Repository layout should look like this:
|
||||||
//
|
//
|
||||||
// /refs/namespaces/<project>
|
// /refs/namespaces/<remote>
|
||||||
// /refs/namespaces/<remote>
|
// /heads
|
||||||
// /heads
|
// /master
|
||||||
// /master
|
// /tags
|
||||||
// /tags
|
// ...
|
||||||
// ...
|
|
||||||
//
|
//
|
||||||
|
let url = url.to_string();
|
||||||
let refs: &[&str] = &["refs/namespaces/*:refs/namespaces/*"];
|
let refs: &[&str] = &["refs/namespaces/*:refs/namespaces/*"];
|
||||||
let mut remote = self.backend.remote_anonymous(url)?;
|
let mut remote = self.backend.remote_anonymous(&url)?;
|
||||||
let mut opts = git2::FetchOptions::default();
|
let mut opts = git2::FetchOptions::default();
|
||||||
|
|
||||||
remote.fetch(refs, Some(&mut opts), None)?;
|
remote.fetch(refs, Some(&mut opts), None)?;
|
||||||
|
|
@ -219,6 +219,7 @@ mod tests {
|
||||||
use crate::git;
|
use crate::git;
|
||||||
use crate::storage::{ReadStorage, WriteRepository};
|
use crate::storage::{ReadStorage, WriteRepository};
|
||||||
use crate::test::fixtures;
|
use crate::test::fixtures;
|
||||||
|
use git_url::Url;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_remotes() {
|
fn test_list_remotes() {
|
||||||
|
|
@ -226,10 +227,12 @@ mod tests {
|
||||||
let storage = fixtures::storage(dir.path());
|
let storage = fixtures::storage(dir.path());
|
||||||
let inv = storage.inventory().unwrap();
|
let inv = storage.inventory().unwrap();
|
||||||
let (proj, _) = inv.first().unwrap();
|
let (proj, _) = inv.first().unwrap();
|
||||||
let refs = git::list_remotes(&format!(
|
let refs = git::list_remotes(&Url {
|
||||||
"file://{}",
|
host: Some(dir.path().to_string_lossy().to_string()),
|
||||||
dir.path().join(&proj.to_string()).display(),
|
scheme: git_url::Scheme::File,
|
||||||
))
|
path: format!("/{}", proj).into(),
|
||||||
|
..Url::default()
|
||||||
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let remotes = storage.repository(proj).unwrap().remotes().unwrap();
|
let remotes = storage.repository(proj).unwrap().remotes().unwrap();
|
||||||
|
|
@ -249,10 +252,12 @@ mod tests {
|
||||||
// Have Bob fetch Alice's refs.
|
// Have Bob fetch Alice's refs.
|
||||||
bob.repository(proj)
|
bob.repository(proj)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.fetch(&format!(
|
.fetch(&Url {
|
||||||
"file://{}",
|
host: Some(alice.path().to_string_lossy().to_string()),
|
||||||
alice.path().join(&proj.to_string()).display()
|
scheme: git_url::Scheme::File,
|
||||||
))
|
path: format!("/{}", proj).into(),
|
||||||
|
..Url::default()
|
||||||
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
for remote in remotes.values() {
|
for remote in remotes.values() {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use git_url::Url;
|
||||||
|
|
||||||
use crate::identity::ProjId;
|
use crate::identity::ProjId;
|
||||||
use crate::storage::{
|
use crate::storage::{
|
||||||
Error, Inventory, ReadStorage, Remotes, Unverified, WriteRepository, WriteStorage,
|
Error, Inventory, ReadStorage, Remotes, Unverified, WriteRepository, WriteStorage,
|
||||||
|
|
@ -50,7 +52,7 @@ impl WriteStorage for MockStorage {
|
||||||
pub struct MockRepository {}
|
pub struct MockRepository {}
|
||||||
|
|
||||||
impl WriteRepository for MockRepository {
|
impl WriteRepository for MockRepository {
|
||||||
fn fetch(&mut self, _url: &str) -> Result<(), git2::Error> {
|
fn fetch(&mut self, _url: &Url) -> Result<(), git2::Error> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue