feat: implement a basic backend #1
Generated
+1051
-75
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -28,7 +28,7 @@ nostr-gossip-memory = { git = "https://github.com/rust-nostr/nostr", rev = "d0a1
|
||||
nostr-connect = { git = "https://github.com/rust-nostr/nostr", rev = "d0a1d67d3c9e5cf9710807a6a414c155a5f47215" }
|
||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "d0a1d67d3c9e5cf9710807a6a414c155a5f47215" }
|
||||
|
||||
git2 = { version = "0.20", features = ["vendored-libgit2"] }
|
||||
gix = { version = "0.86", default-features = false, features = ["sha1", "blocking-network-client", "blocking-http-transport-reqwest-rust-tls", "worktree-mutation"] }
|
||||
|
||||
chrono = { version = "0.4.38", features = ["wasmbind"] }
|
||||
smol = "2"
|
||||
|
||||
@@ -8,5 +8,5 @@ publish.workspace = true
|
||||
signed_core = { path = "../signed_core" }
|
||||
|
||||
nostr.workspace = true
|
||||
git2.workspace = true
|
||||
gix.workspace = true
|
||||
anyhow.workspace = true
|
||||
|
||||
@@ -7,7 +7,8 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use git2::Repository;
|
||||
use gix::interrupt::IS_INTERRUPTED;
|
||||
use gix::progress::Discard;
|
||||
use signed_core::RepoAddr;
|
||||
|
||||
/// On-disk cache of cloned repositories, keyed by owner pubkey / repo id.
|
||||
@@ -29,18 +30,19 @@ impl GitCache {
|
||||
}
|
||||
|
||||
/// Open an existing clone.
|
||||
pub fn open(&self, addr: &RepoAddr) -> Result<Option<Repository>> {
|
||||
pub fn open(&self, addr: &RepoAddr) -> Result<Option<gix::Repository>> {
|
||||
let path = self.repo_path(addr);
|
||||
match Repository::open(&path) {
|
||||
match gix::open(&path) {
|
||||
Ok(repo) => Ok(Some(repo)),
|
||||
Err(e) if e.code() == git2::ErrorCode::NotFound => Ok(None),
|
||||
Err(gix::open::Error::NotARepository { .. }) => Ok(None),
|
||||
Err(gix::open::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Open the local clone if it exists (fetching first), otherwise clone
|
||||
/// from the first working URL in `clone_urls` (the announcement's `clone` tag).
|
||||
pub fn ensure_clone(&self, addr: &RepoAddr, clone_urls: &[String]) -> Result<Repository> {
|
||||
pub fn ensure_clone(&self, addr: &RepoAddr, clone_urls: &[String]) -> Result<gix::Repository> {
|
||||
let path = self.repo_path(addr);
|
||||
|
||||
if let Some(repo) = self.open(addr)? {
|
||||
@@ -53,9 +55,10 @@ impl GitCache {
|
||||
.with_context(|| format!("failed to create {}", parent.display()))?;
|
||||
}
|
||||
|
||||
let mut last_err: Option<git2::Error> = None;
|
||||
let mut last_err: Option<anyhow::Error> = None;
|
||||
|
||||
for url in clone_urls {
|
||||
match Repository::clone(url, &path) {
|
||||
match clone(url, &path) {
|
||||
Ok(repo) => return Ok(repo),
|
||||
Err(e) => last_err = Some(e),
|
||||
}
|
||||
@@ -68,14 +71,12 @@ impl GitCache {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch all heads from `origin`.
|
||||
pub fn fetch_all(repo: &Repository) -> Result<()> {
|
||||
let mut remote = repo.find_remote("origin")?;
|
||||
remote.fetch(
|
||||
&["+refs/heads/*:refs/remotes/origin/*", "+refs/tags/*:refs/tags/*"],
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
/// Fetch all configured refspecs from `origin`.
|
||||
pub fn fetch_all(repo: &gix::Repository) -> Result<()> {
|
||||
repo.find_remote("origin")?
|
||||
.connect(gix::remote::Direction::Fetch)?
|
||||
.prepare_fetch(Discard, Default::default())?
|
||||
.receive(Discard, &IS_INTERRUPTED)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -105,6 +106,16 @@ pub fn apply_patch(repo_path: &Path, patch: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clone(url: &str, path: &Path) -> Result<gix::Repository> {
|
||||
let url = gix::url::parse(url).context("invalid clone URL")?;
|
||||
|
||||
let mut prepare = gix::prepare_clone(url, path)?;
|
||||
let (mut checkout, _fetch) = prepare.fetch_then_checkout(Discard, &IS_INTERRUPTED)?;
|
||||
let (repo, _checkout) = checkout.main_worktree(Discard, &IS_INTERRUPTED)?;
|
||||
|
||||
Ok(repo)
|
||||
}
|
||||
|
||||
fn sanitize_path_component(id: &str) -> String {
|
||||
id.chars()
|
||||
.map(|c| {
|
||||
|
||||
Reference in New Issue
Block a user