Skip to content

Commit af9e974

Browse files
committed
refactor!: split async and blocking implementations for Handshake::fetch_or_extract_refmap()
1 parent 7ff4a4f commit af9e974

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

gitoxide-core/src/pack/receive.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,17 @@ where
8787
fetch_refspecs: fetch_refspecs.clone(),
8888
extra_refspecs: vec![],
8989
};
90-
let refmap = handshake
91-
.fetch_or_extract_refmap(user_agent.clone(), true, context)?
92-
.fetch(&mut progress, &mut transport.inner, trace_packetlines)
90+
91+
let fetch_refmap = handshake.fetch_or_extract_refmap(user_agent.clone(), true, context)?;
92+
93+
#[cfg(feature = "async-client")]
94+
let refmap = fetch_refmap
95+
.fetch_async(&mut progress, &mut transport.inner, trace_packetlines)
9396
.await?;
9497

98+
#[cfg(feature = "blocking-client")]
99+
let refmap = fetch_refmap.fetch_blocking(&mut progress, &mut transport.inner, trace_packetlines)?;
100+
95101
if refmap.mappings.is_empty() && !refmap.remote_refs.is_empty() {
96102
return Err(Error::NoMapping {
97103
refspecs: refmap.refspecs.clone(),

gix-protocol/src/handshake/mod.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ pub(crate) mod hero {
7272
#[cfg(feature = "fetch")]
7373
mod fetch {
7474
#[cfg(feature = "async-client")]
75-
use crate::transport::client::async_io::Transport;
75+
use crate::transport::client::async_io;
7676
#[cfg(feature = "blocking-client")]
77-
use crate::transport::client::blocking_io::Transport;
77+
use crate::transport::client::blocking_io;
7878
use crate::{fetch::RefMap, Handshake};
7979
use gix_features::progress::Progress;
8080
use std::borrow::Cow;
@@ -86,12 +86,12 @@ pub(crate) mod hero {
8686

8787
impl FetchRefMap<'_> {
8888
/// Fetch the refmap, either by returning the existing one or invoking the command.
89+
#[cfg(feature = "async-client")]
8990
#[allow(clippy::result_large_err)]
90-
#[maybe_async::maybe_async]
91-
pub async fn fetch(
91+
pub async fn fetch_async(
9292
self,
9393
mut progress: impl Progress,
94-
transport: &mut impl Transport,
94+
transport: &mut impl async_io::Transport,
9595
trace_packetlines: bool,
9696
) -> Result<crate::fetch::RefMap, crate::fetch::refmap::init::Error> {
9797
let (cmd, cx) = match self {
@@ -100,7 +100,26 @@ pub(crate) mod hero {
100100
};
101101

102102
let capabilities = cmd.capabilities;
103-
let remote_refs = cmd.invoke(transport, &mut progress, trace_packetlines).await?;
103+
let remote_refs = cmd.invoke_async(transport, &mut progress, trace_packetlines).await?;
104+
RefMap::from_refs(remote_refs, capabilities, cx)
105+
}
106+
107+
/// Fetch the refmap, either by returning the existing one or invoking the command.
108+
#[cfg(feature = "blocking-client")]
109+
#[allow(clippy::result_large_err)]
110+
pub fn fetch_blocking(
111+
self,
112+
mut progress: impl Progress,
113+
transport: &mut impl blocking_io::Transport,
114+
trace_packetlines: bool,
115+
) -> Result<crate::fetch::RefMap, crate::fetch::refmap::init::Error> {
116+
let (cmd, cx) = match self {
117+
FetchRefMap::Map(map) => return Ok(map),
118+
FetchRefMap::Command(cmd, cx) => (cmd, cx),
119+
};
120+
121+
let capabilities = cmd.capabilities;
122+
let remote_refs = cmd.invoke_blocking(transport, &mut progress, trace_packetlines)?;
104123
RefMap::from_refs(remote_refs, capabilities, cx)
105124
}
106125
}

gix-protocol/src/ls_refs.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ pub(crate) mod function {
3636
use bstr::{BString, ByteVec};
3737
use gix_features::progress::Progress;
3838
use gix_transport::client::Capabilities;
39-
use maybe_async::maybe_async;
4039

4140
use super::Error;
4241
#[cfg(feature = "async-client")]
43-
use crate::transport::client::async_io::{Transport, TransportV2Ext};
42+
use crate::transport::client::async_io::{self, TransportV2Ext as _};
4443
#[cfg(feature = "blocking-client")]
45-
use crate::transport::client::blocking_io::{Transport, TransportV2Ext};
44+
use crate::transport::client::blocking_io::{self, TransportV2Ext as _};
4645
use crate::{
4746
handshake::{refs::from_v2_refs, Ref},
4847
Command,
@@ -103,10 +102,10 @@ pub(crate) mod function {
103102
///
104103
/// `progress` is used to provide feedback.
105104
/// If `trace` is `true`, all packetlines received or sent will be passed to the facilities of the `gix-trace` crate.
106-
#[maybe_async]
107-
pub async fn invoke(
105+
#[cfg(feature = "async-client")]
106+
pub async fn invoke_async(
108107
self,
109-
mut transport: impl Transport,
108+
mut transport: impl async_io::Transport,
110109
progress: &mut impl Progress,
111110
trace: bool,
112111
) -> Result<Vec<Ref>, Error> {
@@ -133,5 +132,38 @@ pub(crate) mod function {
133132
.await?;
134133
Ok(from_v2_refs(&mut remote_refs).await?)
135134
}
135+
136+
/// Invoke a ls-refs V2 command on `transport`.
137+
///
138+
/// `progress` is used to provide feedback.
139+
/// If `trace` is `true`, all packetlines received or sent will be passed to the facilities of the `gix-trace` crate.
140+
#[cfg(feature = "blocking-client")]
141+
pub fn invoke_blocking(
142+
self,
143+
mut transport: impl blocking_io::Transport,
144+
progress: &mut impl Progress,
145+
trace: bool,
146+
) -> Result<Vec<Ref>, Error> {
147+
Command::LsRefs.validate_argument_prefixes(
148+
gix_transport::Protocol::V2,
149+
self.capabilities,
150+
&self.arguments,
151+
&self.features,
152+
)?;
153+
154+
progress.step();
155+
progress.set_name("list refs".into());
156+
let mut remote_refs = transport.invoke(
157+
Command::LsRefs.as_str(),
158+
self.features.into_iter(),
159+
if self.arguments.is_empty() {
160+
None
161+
} else {
162+
Some(self.arguments.into_iter())
163+
},
164+
trace,
165+
)?;
166+
Ok(from_v2_refs(&mut remote_refs)?)
167+
}
136168
}
137169
}

gix-protocol/tests/protocol/fetch/_impl.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,18 @@ mod fetch_fn {
106106
None => match delegate.action() {
107107
Ok(RefsAction::Skip) => Vec::new(),
108108
Ok(RefsAction::Continue) => {
109-
LsRefsCommand::new(None, &capabilities, ("agent", Some(Cow::Owned(agent.clone()))))
110-
.invoke(&mut transport, &mut progress, trace)
111-
.await?
109+
#[cfg(feature = "async-client")]
110+
{
111+
LsRefsCommand::new(None, &capabilities, ("agent", Some(Cow::Owned(agent.clone()))))
112+
.invoke_async(&mut transport, &mut progress, trace)
113+
.await?
114+
}
115+
#[cfg(feature = "blocking-client")]
116+
{
117+
LsRefsCommand::new(None, &capabilities, ("agent", Some(Cow::Owned(agent.clone()))))
118+
.invoke_blocking(&mut transport, &mut progress, trace)
119+
.await?
120+
}
112121
}
113122
Err(err) => {
114123
indicate_end_of_interaction(transport, trace).await?;

gix/src/remote/connection/ref_map.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,20 @@ where
157157
extra_refspecs,
158158
};
159159

160-
let ref_map = handshake
161-
.fetch_or_extract_refmap(
162-
self.remote.repo.config.user_agent_tuple(),
163-
prefix_from_spec_as_filter_on_remote,
164-
context,
165-
)?
166-
.fetch(progress, &mut self.transport.inner, self.trace)
160+
let fetch_refmap = handshake.fetch_or_extract_refmap(
161+
self.remote.repo.config.user_agent_tuple(),
162+
prefix_from_spec_as_filter_on_remote,
163+
context,
164+
)?;
165+
166+
#[cfg(feature = "async-network-client")]
167+
let ref_map = fetch_refmap
168+
.fetch_async(progress, &mut self.transport.inner, self.trace)
167169
.await?;
168170

171+
#[cfg(feature = "blocking-network-client")]
172+
let ref_map = fetch_refmap.fetch_blocking(progress, &mut self.transport.inner, self.trace)?;
173+
169174
self.handshake = Some(handshake);
170175
Ok(ref_map)
171176
}

0 commit comments

Comments
 (0)