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
|
|
|
|
2024-05-11 18:57:18 +01:00
|
|
|
use crate::{config::RepoDetails, gitforge::Repository};
|
2024-05-03 17:50:50 +01:00
|
|
|
|
|
|
|
#[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-09 21:53:50 +01:00
|
|
|
pub fn fetch(repository: &Repository, repo_details: &RepoDetails) -> Result<(), Error> {
|
|
|
|
let repository = repository.deref().to_thread_local();
|
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(())
|
|
|
|
}
|