Skip to content

Commit 3bc5801

Browse files
committed
refactor!: stop taking a callback for test-only uses
1 parent e549841 commit 3bc5801

File tree

5 files changed

+62
-73
lines changed

5 files changed

+62
-73
lines changed

gix-protocol/src/fetch/refmap/init.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ impl RefMap {
8080
let remote_refs = crate::ls_refs(
8181
transport,
8282
capabilities,
83-
|_capabilities| Ok(crate::ls_refs::RefsAction::Continue),
8483
push_prefix_arguments(prefix_from_spec_as_filter_on_remote, &all_refspecs),
8584
&mut progress,
8685
trace_packetlines,

gix-protocol/src/ls_refs.rs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ mod error {
2929
#[cfg(any(feature = "blocking-client", feature = "async-client"))]
3030
pub use error::Error;
3131

32-
/// What to do after preparing ls-refs in [`ls_refs()`][crate::ls_refs()].
33-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
34-
pub enum RefsAction {
35-
/// Continue by sending a 'ls-refs' command.
36-
Continue,
37-
/// Skip 'ls-refs' entirely.
38-
///
39-
/// This is useful if the `ref-in-want` capability is taken advantage of. When fetching, one must must then send
40-
/// `want-ref`s during the negotiation phase.
41-
Skip,
42-
}
43-
4432
#[cfg(any(feature = "blocking-client", feature = "async-client"))]
4533
pub(crate) mod function {
4634
use std::borrow::Cow;
@@ -50,14 +38,14 @@ pub(crate) mod function {
5038
use gix_transport::client::Capabilities;
5139
use maybe_async::maybe_async;
5240

53-
use super::{Error, RefsAction};
41+
use super::Error;
5442
#[cfg(feature = "async-client")]
5543
use crate::transport::client::async_io::{Transport, TransportV2Ext};
5644
#[cfg(feature = "blocking-client")]
5745
use crate::transport::client::blocking_io::{Transport, TransportV2Ext};
5846
use crate::{
5947
handshake::{refs::from_v2_refs, Ref},
60-
indicate_end_of_interaction, Command,
48+
Command,
6149
};
6250

6351
/// Invoke a ls-refs V2 command on `transport`, which requires a prior handshake that yielded
@@ -70,7 +58,6 @@ pub(crate) mod function {
7058
pub async fn ls_refs(
7159
mut transport: impl Transport,
7260
capabilities: &Capabilities,
73-
prepare_ls_refs: impl FnOnce(&Capabilities) -> std::io::Result<RefsAction>,
7461
extra_args: Vec<BString>,
7562
progress: &mut impl Progress,
7663
trace: bool,
@@ -90,37 +77,22 @@ pub(crate) mod function {
9077
}
9178

9279
ls_args.extend(extra_args);
93-
let refs = match prepare_ls_refs(capabilities) {
94-
Ok(RefsAction::Skip) => Vec::new(),
95-
Ok(RefsAction::Continue) => {
96-
ls_refs.validate_argument_prefixes(
97-
gix_transport::Protocol::V2,
98-
capabilities,
99-
&ls_args,
100-
&ls_features,
101-
)?;
80+
ls_refs.validate_argument_prefixes(gix_transport::Protocol::V2, capabilities, &ls_args, &ls_features)?;
10281

103-
progress.step();
104-
progress.set_name("list refs".into());
105-
let mut remote_refs = transport
106-
.invoke(
107-
ls_refs.as_str(),
108-
ls_features.into_iter(),
109-
if ls_args.is_empty() {
110-
None
111-
} else {
112-
Some(ls_args.into_iter())
113-
},
114-
trace,
115-
)
116-
.await?;
117-
from_v2_refs(&mut remote_refs).await?
118-
}
119-
Err(err) => {
120-
indicate_end_of_interaction(transport, trace).await?;
121-
return Err(err.into());
122-
}
123-
};
124-
Ok(refs)
82+
progress.step();
83+
progress.set_name("list refs".into());
84+
let mut remote_refs = transport
85+
.invoke(
86+
ls_refs.as_str(),
87+
ls_features.into_iter(),
88+
if ls_args.is_empty() {
89+
None
90+
} else {
91+
Some(ls_args.into_iter())
92+
},
93+
trace,
94+
)
95+
.await?;
96+
Ok(from_v2_refs(&mut remote_refs).await?)
12597
}
12698
}

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

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/// What to do after preparing ls-refs.
2+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
3+
pub enum RefsAction {
4+
/// Continue by sending a 'ls-refs' command.
5+
Continue,
6+
/// Skip 'ls-refs' entirely.
7+
///
8+
/// This is useful if the `ref-in-want` capability is taken advantage of. When fetching, one
9+
/// must then send `want-ref`s during the negotiation phase.
10+
Skip,
11+
}
12+
113
mod fetch_fn {
214
use std::borrow::Cow;
315

@@ -13,7 +25,7 @@ mod fetch_fn {
1325
use gix_transport::client::blocking_io::{ExtendedBufRead, HandleProgress, Transport};
1426
use maybe_async::maybe_async;
1527

16-
use super::{Action, Delegate};
28+
use super::{Action, Delegate, RefsAction};
1729
use crate::fetch::Error;
1830

1931
/// A way to indicate how to treat the connection underlying the transport, potentially allowing to reuse it.
@@ -91,18 +103,24 @@ mod fetch_fn {
91103
let agent = gix_protocol::agent(agent);
92104
let refs = match refs {
93105
Some(refs) => refs,
94-
None => {
95-
gix_protocol::ls_refs(
96-
&mut transport,
97-
&capabilities,
98-
|a| delegate.prepare_ls_refs(a),
99-
Vec::new(),
100-
&mut progress,
101-
trace,
102-
("agent", Some(Cow::Owned(agent.clone()))),
103-
)
104-
.await?
105-
}
106+
None => match delegate.action() {
107+
Ok(RefsAction::Skip) => Vec::new(),
108+
Ok(RefsAction::Continue) => {
109+
gix_protocol::ls_refs(
110+
&mut transport,
111+
&capabilities,
112+
Vec::new(),
113+
&mut progress,
114+
trace,
115+
("agent", Some(Cow::Owned(agent.clone()))),
116+
)
117+
.await?
118+
}
119+
Err(err) => {
120+
indicate_end_of_interaction(transport, trace).await?;
121+
return Err(err.into());
122+
}
123+
},
106124
};
107125

108126
let fetch = Command::Fetch;
@@ -199,10 +217,11 @@ mod delegate {
199217
use gix_protocol::{
200218
fetch::{Arguments, Response},
201219
handshake::Ref,
202-
ls_refs::{self, RefsAction},
203220
};
204221
use gix_transport::client::Capabilities;
205222

223+
use super::RefsAction;
224+
206225
/// Defines what to do next after certain [`Delegate`] operations.
207226
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
208227
pub enum Action {
@@ -231,10 +250,10 @@ mod delegate {
231250
/// Note that some arguments are preset based on typical use, and `features` are preset to maximize options.
232251
/// The `server` capabilities can be used to see which additional capabilities the server supports as per the handshake which happened prior.
233252
///
234-
/// If the delegate returns [`ls_refs::Action::Skip`], no `ls-refs` command is sent to the server.
253+
/// If the delegate returns [`RefsAction::Skip`], no `ls-refs` command is sent to the server.
235254
///
236255
/// Note that this is called only if we are using protocol version 2.
237-
fn prepare_ls_refs(&mut self, _server: &Capabilities) -> std::io::Result<RefsAction> {
256+
fn action(&mut self) -> std::io::Result<RefsAction> {
238257
Ok(RefsAction::Continue)
239258
}
240259

@@ -299,8 +318,8 @@ mod delegate {
299318
self.deref().handshake_extra_parameters()
300319
}
301320

302-
fn prepare_ls_refs(&mut self, _server: &Capabilities) -> io::Result<RefsAction> {
303-
self.deref_mut().prepare_ls_refs(_server)
321+
fn action(&mut self) -> io::Result<RefsAction> {
322+
self.deref_mut().action()
304323
}
305324

306325
fn prepare_fetch(
@@ -328,8 +347,8 @@ mod delegate {
328347
self.deref().handshake_extra_parameters()
329348
}
330349

331-
fn prepare_ls_refs(&mut self, _server: &Capabilities) -> io::Result<RefsAction> {
332-
self.deref_mut().prepare_ls_refs(_server)
350+
fn action(&mut self) -> io::Result<RefsAction> {
351+
self.deref_mut().action()
333352
}
334353

335354
fn prepare_fetch(

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ use bstr::{BString, ByteSlice};
44
use gix_protocol::{
55
fetch::{Arguments, Response},
66
handshake,
7-
ls_refs::{self, RefsAction},
87
};
98
use gix_transport::client::Capabilities;
109

1110
use crate::fixture_bytes;
1211

1312
pub(super) mod _impl;
14-
use _impl::{Action, DelegateBlocking};
13+
use _impl::{Action, DelegateBlocking, RefsAction};
1514

1615
mod error {
1716
use std::io;
@@ -103,7 +102,7 @@ pub struct CloneRefInWantDelegate {
103102
}
104103

105104
impl DelegateBlocking for CloneRefInWantDelegate {
106-
fn prepare_ls_refs(&mut self, _server: &Capabilities) -> io::Result<RefsAction> {
105+
fn action(&mut self) -> io::Result<RefsAction> {
107106
Ok(RefsAction::Skip)
108107
}
109108

@@ -142,7 +141,7 @@ impl DelegateBlocking for LsRemoteDelegate {
142141
fn handshake_extra_parameters(&self) -> Vec<(String, Option<String>)> {
143142
vec![("value-only".into(), None), ("key".into(), Some("value".into()))]
144143
}
145-
fn prepare_ls_refs(&mut self, _server: &Capabilities) -> std::io::Result<RefsAction> {
144+
fn action(&mut self) -> std::io::Result<RefsAction> {
146145
match self.abort_with.take() {
147146
Some(err) => Err(err),
148147
None => Ok(RefsAction::Continue),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bstr::ByteSlice;
22
use gix_features::progress;
3-
use gix_protocol::{handshake, ls_refs};
3+
use gix_protocol::handshake;
44
use gix_transport::Protocol;
55

66
use crate::fetch::{
@@ -143,7 +143,7 @@ async fn ls_remote_abort_in_prep_ls_refs() -> crate::Result {
143143
b"0044git-upload-pack does/not/matter\x00\x00version=2\x00value-only\x00key=value\x000000".as_bstr()
144144
);
145145
match err {
146-
Error::LsRefs(ls_refs::Error::Io(err)) => {
146+
Error::Io(err) => {
147147
assert_eq!(err.kind(), std::io::ErrorKind::Other);
148148
assert_eq!(err.get_ref().expect("other error").to_string(), "hello world");
149149
}

0 commit comments

Comments
 (0)