2024-05-03 17:50:50 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2024-05-04 19:57:50 +01:00
|
|
|
use tracing::{debug, info};
|
2024-05-03 17:50:50 +01:00
|
|
|
|
|
|
|
use crate::server::config::RepoDetails;
|
|
|
|
|
|
|
|
#[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 {}
|
|
|
|
|
2024-05-04 19:57:50 +01:00
|
|
|
#[tracing::instrument(skip_all, fields(repo = %repo_details))]
|
2024-05-03 17:50:50 +01:00
|
|
|
pub fn fetch(repo_details: &RepoDetails) -> Result<(), Error> {
|
|
|
|
// INFO: gitdir validate tests that the default fetch remote matches the configured remote
|
|
|
|
let repository = gix::ThreadSafeRepository::open(repo_details.gitdir.deref())
|
|
|
|
.map_err(Box::new)?
|
|
|
|
.to_thread_local();
|
2024-05-04 19:57:50 +01:00
|
|
|
debug!(?repository, "opened repo");
|
2024-05-03 17:50:50 +01:00
|
|
|
let Some(remote) = repository.find_default_remote(gix::remote::Direction::Fetch) else {
|
|
|
|
return Err(Error::NoFetchRemoteFound);
|
|
|
|
};
|
2024-05-04 19:57:50 +01:00
|
|
|
debug!(?remote, "fetch remote");
|
2024-05-03 17:50:50 +01:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|