Skip to content

Commit 7ff4a4f

Browse files
committed
refactor!: split Handshake::fetch_or_extract_refmap()
1 parent d61a8fb commit 7ff4a4f

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

gitoxide-core/src/pack/receive.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,8 @@ where
8888
extra_refspecs: vec![],
8989
};
9090
let refmap = handshake
91-
.fetch_or_extract_refmap(
92-
&mut progress,
93-
&mut transport.inner,
94-
user_agent.clone(),
95-
trace_packetlines,
96-
true,
97-
context,
98-
)
91+
.fetch_or_extract_refmap(user_agent.clone(), true, context)?
92+
.fetch(&mut progress, &mut transport.inner, trace_packetlines)
9993
.await?;
10094

10195
if refmap.mappings.is_empty() && !refmap.remote_refs.is_empty() {

gix-protocol/src/handshake/mod.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,56 @@ pub(crate) mod hero {
7979
use gix_features::progress::Progress;
8080
use std::borrow::Cow;
8181

82-
impl Handshake {
83-
/// Obtain a [refmap](crate::fetch::RefMap) either from this instance, taking it out in the process, if the handshake was
84-
/// created from a V1 connection, or use `transport` to fetch the refmap as a separate command invocation.
82+
pub enum FetchRefMap<'a> {
83+
Map(RefMap),
84+
Command(crate::LsRefsCommand<'a>, crate::fetch::refmap::init::Context),
85+
}
86+
87+
impl FetchRefMap<'_> {
88+
/// Fetch the refmap, either by returning the existing one or invoking the command.
8589
#[allow(clippy::result_large_err)]
8690
#[maybe_async::maybe_async]
87-
pub async fn fetch_or_extract_refmap<T>(
88-
&mut self,
91+
pub async fn fetch(
92+
self,
8993
mut progress: impl Progress,
90-
transport: &mut T,
91-
user_agent: (&'static str, Option<Cow<'static, str>>),
94+
transport: &mut impl Transport,
9295
trace_packetlines: bool,
96+
) -> Result<crate::fetch::RefMap, crate::fetch::refmap::init::Error> {
97+
let (cmd, cx) = match self {
98+
FetchRefMap::Map(map) => return Ok(map),
99+
FetchRefMap::Command(cmd, cx) => (cmd, cx),
100+
};
101+
102+
let capabilities = cmd.capabilities;
103+
let remote_refs = cmd.invoke(transport, &mut progress, trace_packetlines).await?;
104+
RefMap::from_refs(remote_refs, capabilities, cx)
105+
}
106+
}
107+
108+
impl Handshake {
109+
/// Prepare fetching a [refmap](crate::fetch::RefMap) if not present in the handshake.
110+
#[allow(clippy::result_large_err)]
111+
pub fn fetch_or_extract_refmap(
112+
&mut self,
113+
user_agent: (&'static str, Option<Cow<'static, str>>),
93114
prefix_from_spec_as_filter_on_remote: bool,
94115
refmap_context: crate::fetch::refmap::init::Context,
95-
) -> Result<crate::fetch::RefMap, crate::fetch::refmap::init::Error>
96-
where
97-
T: Transport,
98-
{
116+
) -> Result<FetchRefMap<'_>, crate::fetch::refmap::init::Error> {
99117
if let Some(refs) = self.refs.take() {
100-
return crate::fetch::RefMap::from_refs(refs, &self.capabilities, refmap_context);
118+
return Ok(FetchRefMap::Map(crate::fetch::RefMap::from_refs(
119+
refs,
120+
&self.capabilities,
121+
refmap_context,
122+
)?));
101123
}
102124

103125
let _span = gix_trace::coarse!("gix_protocol::handshake::fetch_or_extract_refmap()");
104126
let all_refspecs = refmap_context.aggregate_refspecs();
105127
let prefix_refspecs = prefix_from_spec_as_filter_on_remote.then_some(&all_refspecs[..]);
106-
let remote_refs = crate::LsRefsCommand::new(prefix_refspecs, &self.capabilities, user_agent)
107-
.invoke(transport, &mut progress, trace_packetlines)
108-
.await?;
109-
RefMap::from_refs(remote_refs, &self.capabilities, refmap_context)
128+
Ok(FetchRefMap::Command(
129+
crate::LsRefsCommand::new(prefix_refspecs, &self.capabilities, user_agent),
130+
refmap_context,
131+
))
110132
}
111133
}
112134
}
@@ -146,6 +168,7 @@ mod error {
146168
}
147169
}
148170
}
171+
149172
#[cfg(feature = "handshake")]
150173
pub use error::Error;
151174

gix-protocol/src/ls_refs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) mod function {
5050

5151
/// A command to list references from a remote Git repository.
5252
pub struct LsRefsCommand<'a> {
53-
capabilities: &'a Capabilities,
53+
pub(crate) capabilities: &'a Capabilities,
5454
features: Vec<(&'static str, Option<Cow<'static, str>>)>,
5555
arguments: Vec<BString>,
5656
}

gix/src/remote/connection/ref_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ where
156156
fetch_refspecs: self.remote.fetch_specs.clone(),
157157
extra_refspecs,
158158
};
159+
159160
let ref_map = handshake
160161
.fetch_or_extract_refmap(
161-
progress,
162-
&mut self.transport.inner,
163162
self.remote.repo.config.user_agent_tuple(),
164-
self.trace,
165163
prefix_from_spec_as_filter_on_remote,
166164
context,
167-
)
165+
)?
166+
.fetch(progress, &mut self.transport.inner, self.trace)
168167
.await?;
168+
169169
self.handshake = Some(handshake);
170170
Ok(ref_map)
171171
}

0 commit comments

Comments
 (0)