Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e55b43e

Browse files
committed
A first sketch on how connections could be working (#450)
I see that `into_connection()` is just the underlying layer, and that convenience methods will be added on `Remote` at some point to do fetches, pushes and `ls-refs` possibly with less boilerplate (but also with less control).
1 parent ad101ef commit e55b43e

5 files changed

Lines changed: 33 additions & 27 deletions

File tree

git-protocol/src/fetch_fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ pub enum FetchConnection {
2121
/// When indicating the end-of-fetch, this flag is only relevant in protocol V2.
2222
/// Generally it only applies when using persistent transports.
2323
///
24-
/// In most explicit client side failures modes the end-of-operation' notification will be sent to the server automatically.
24+
/// In most explicit client side failure modes the end-of-operation' notification will be sent to the server automatically.
2525
TerminateOnSuccessfulCompletion,
2626

27-
/// Indicate that persistent transport connections can be reused by not sending an 'end-of-operation' notification to the server.
27+
/// Indicate that persistent transport connections can be reused by _not_ sending an 'end-of-operation' notification to the server.
2828
/// This is useful if multiple `fetch(…)` calls are used in succession.
2929
///
3030
/// Note that this has no effect in case of non-persistent connections, like the ones over HTTP.

git-repository/src/remote/connect.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::remote::Connection;
44
use crate::{remote, Remote};
5-
// use git_protocol::transport;
5+
use git_protocol::transport;
66
use git_protocol::transport::client::Transport;
77

88
mod error {
@@ -28,7 +28,7 @@ impl<'repo> Remote<'repo> {
2828
///
2929
/// Note that this method expects the `transport` to be created by the user, which would involve the [`url()`][Self::url()].
3030
/// It's meant to be used when async operation is needed with runtimes of the user's choice.
31-
pub fn into_connection<T>(self, transport: T, direction: remote::Direction) -> Connection<'repo, T>
31+
pub fn into_connection_with_transport<T>(self, transport: T, direction: remote::Direction) -> Connection<'repo, T>
3232
where
3333
T: Transport,
3434
{
@@ -41,9 +41,12 @@ impl<'repo> Remote<'repo> {
4141

4242
/// Connect to the url suitable for `direction` and return a handle through which operations can be performed.
4343
#[cfg(feature = "blocking-network-client")]
44-
pub fn connect(&self, direction: remote::Direction) -> Result<Connection<'repo, Box<dyn Transport + Send>>, Error> {
44+
pub fn into_connection(
45+
self,
46+
direction: remote::Direction,
47+
) -> Result<Connection<'repo, Box<dyn Transport + Send>>, Error> {
4548
use git_protocol::transport::Protocol;
46-
let _protocol = self
49+
let protocol = self
4750
.repo
4851
.config
4952
.resolved
@@ -61,11 +64,9 @@ impl<'repo> Remote<'repo> {
6164
}
6265
})
6366
})?;
64-
let _url = self.url(direction).ok_or(Error::MissingUrl { direction })?;
65-
todo!()
66-
// transport::connect(
67-
// url ,
68-
// protocol,
69-
// )
67+
68+
let url = self.url(direction).ok_or(Error::MissingUrl { direction })?.to_owned();
69+
let transport = transport::connect(url, protocol)?;
70+
Ok(self.into_connection_with_transport(transport, direction))
7071
}
7172
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[cfg(feature = "blocking-network-client")]
2+
mod blocking {
3+
use crate::remote;
4+
use git_repository::remote::Direction::Fetch;
5+
6+
#[test]
7+
fn ls_refs() {
8+
let repo = remote::repo("clone");
9+
let remote = repo.find_remote("origin").unwrap();
10+
let _connection = remote.into_connection(Fetch).unwrap();
11+
}
12+
}

git-repository/tests/remote/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,4 @@ pub(crate) fn cow_str(s: &str) -> Cow<str> {
1111
Cow::Borrowed(s)
1212
}
1313

14-
mod connect {
15-
#[cfg(feature = "blocking-network-client")]
16-
mod blocking {
17-
use crate::remote;
18-
use git_repository::remote::Direction::Fetch;
19-
20-
#[test]
21-
#[ignore]
22-
fn ls_refs() {
23-
let repo = remote::repo("clone");
24-
let remote = repo.find_remote("origin").unwrap();
25-
let _connection = remote.connect(Fetch).unwrap();
26-
}
27-
}
28-
}
14+
mod connect;

git-url/src/parse.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::convert::Infallible;
23

34
use bstr::{BStr, ByteSlice};
45

@@ -21,6 +22,12 @@ pub enum Error {
2122
RelativeUrl { url: String },
2223
}
2324

25+
impl From<Infallible> for Error {
26+
fn from(_: Infallible) -> Self {
27+
unreachable!("Cannot actually happen, but it seems there can't be a blanket impl for this")
28+
}
29+
}
30+
2431
fn str_to_protocol(s: &str) -> Result<Scheme, Error> {
2532
Ok(match s {
2633
"ssh" => Scheme::Ssh,

0 commit comments

Comments
 (0)