use git_protocol::transport::client::Transport;
use crate::{
bstr::BString,
remote,
remote::{
fetch::{DryRun, RefMap},
ref_map, Connection,
},
Progress,
};
mod error;
pub use error::Error;
use crate::remote::fetch::WritePackedRefs;
pub enum RefLogMessage {
Prefixed {
action: String,
},
Override {
message: BString,
},
}
impl RefLogMessage {
pub(crate) fn compose(&self, context: &str) -> BString {
match self {
RefLogMessage::Prefixed { action } => format!("{action}: {context}").into(),
RefLogMessage::Override { message } => message.to_owned(),
}
}
}
#[derive(Debug, Clone)]
pub enum Status {
NoPackReceived {
update_refs: refs::update::Outcome,
},
Change {
write_pack_bundle: git_pack::bundle::write::Outcome,
update_refs: refs::update::Outcome,
},
DryRun {
update_refs: refs::update::Outcome,
},
}
#[derive(Debug, Clone)]
pub struct Outcome {
pub ref_map: RefMap,
pub status: Status,
}
#[derive(Debug, Copy, Clone)]
pub enum ProgressId {
RemoteProgress,
}
impl From<ProgressId> for git_features::progress::Id {
fn from(v: ProgressId) -> Self {
match v {
ProgressId::RemoteProgress => *b"FERP",
}
}
}
pub mod negotiate;
pub mod prepare {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Cannot perform a meaningful fetch operation without any configured ref-specs")]
MissingRefSpecs,
#[error(transparent)]
RefMap(#[from] crate::remote::ref_map::Error),
}
impl git_protocol::transport::IsSpuriousError for Error {
fn is_spurious(&self) -> bool {
match self {
Error::RefMap(err) => err.is_spurious(),
_ => false,
}
}
}
}
impl<'remote, 'repo, T, P> Connection<'remote, 'repo, T, P>
where
T: Transport,
P: Progress,
{
#[allow(clippy::result_large_err)]
#[git_protocol::maybe_async::maybe_async]
pub async fn prepare_fetch(
mut self,
options: ref_map::Options,
) -> Result<Prepare<'remote, 'repo, T, P>, prepare::Error> {
if self.remote.refspecs(remote::Direction::Fetch).is_empty() {
return Err(prepare::Error::MissingRefSpecs);
}
let ref_map = self.ref_map_inner(options).await?;
Ok(Prepare {
con: Some(self),
ref_map,
dry_run: DryRun::No,
reflog_message: None,
write_packed_refs: WritePackedRefs::Never,
})
}
}
impl<'remote, 'repo, T, P> Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
pub fn ref_map(&self) -> &RefMap {
&self.ref_map
}
}
mod config;
mod receive_pack;
#[path = "update_refs/mod.rs"]
pub mod refs;
pub struct Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
con: Option<Connection<'remote, 'repo, T, P>>,
ref_map: RefMap,
dry_run: DryRun,
reflog_message: Option<RefLogMessage>,
write_packed_refs: WritePackedRefs,
}
impl<'remote, 'repo, T, P> Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
pub fn with_dry_run(mut self, enabled: bool) -> Self {
self.dry_run = if enabled { DryRun::Yes } else { DryRun::No };
self
}
pub fn with_write_packed_refs_only(mut self, enabled: bool) -> Self {
self.write_packed_refs = if enabled {
WritePackedRefs::Only
} else {
WritePackedRefs::Never
};
self
}
pub fn with_reflog_message(mut self, reflog_message: RefLogMessage) -> Self {
self.reflog_message = reflog_message.into();
self
}
}
impl<'remote, 'repo, T, P> Drop for Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
fn drop(&mut self) {
if let Some(mut con) = self.con.take() {
#[cfg(feature = "async-network-client")]
{
git_protocol::futures_lite::future::block_on(git_protocol::indicate_end_of_interaction(
&mut con.transport,
))
.ok();
}
#[cfg(not(feature = "async-network-client"))]
{
git_protocol::indicate_end_of_interaction(&mut con.transport).ok();
}
}
}
}