git-next/crates/server/src/gitforge/forgejo/branch/fetch.rs

37 lines
1.1 KiB
Rust
Raw Normal View History

use std::ops::Deref;
use tracing::{debug, info};
2024-05-11 18:57:18 +01:00
use crate::{config::RepoDetails, gitforge::Repository};
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
UnableToOpenRepo(Box<gix::open::Error>),
NoFetchRemoteFound,
Connect(Box<gix::remote::connect::Error>),
Fetch(String),
}
impl std::error::Error for Error {}
#[tracing::instrument(skip_all, fields(repo = %repo_details))]
pub fn fetch(repository: &Repository, repo_details: &RepoDetails) -> Result<(), Error> {
let repository = repository.deref().to_thread_local();
let Some(remote) = repository.find_default_remote(gix::remote::Direction::Fetch) else {
return Err(Error::NoFetchRemoteFound);
};
debug!(?remote, "fetch remote");
remote
.map_err(|e| Error::Fetch(e.to_string()))?
.connect(gix::remote::Direction::Fetch)
.map_err(Box::new)?
.prepare_fetch(gix::progress::Discard, Default::default())
.map_err(|e| Error::Fetch(e.to_string()))?
.receive(gix::progress::Discard, &Default::default())
.map_err(|e| Error::Fetch(e.to_string()))?;
info!("fetched");
Ok(())
}