Skip to content

Commit

Permalink
[transport] allow setting a custom URL in git::Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 27, 2021
1 parent a0d6756 commit f7437e0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
22 changes: 14 additions & 8 deletions git-transport/src/client/git/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ where
))
}
fn to_url(&self) -> String {
git_url::Url {
scheme: git_url::Scheme::File,
user: None,
host: None,
port: None,
path: self.path.clone(),
}
.to_string()
self.custom_url.as_ref().map_or_else(
|| {
git_url::Url {
scheme: git_url::Scheme::File,
user: None,
host: None,
port: None,
path: self.path.clone(),
}
.to_string()
},
|url| url.clone(),
)
}

/// We implement this in a paranoid and safe way, not allowing downgrade to V1 which
Expand Down Expand Up @@ -118,6 +123,7 @@ where
path: repository_path.into(),
virtual_host: virtual_host.map(|(h, p)| (h.into(), p)),
desired_version,
custom_url: None,
supported_versions: [desired_version],
mode,
}
Expand Down
1 change: 1 addition & 0 deletions git-transport/src/client/git/blocking_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ where
path: repository_path.into(),
virtual_host: virtual_host.map(|(h, p)| (h.into(), p)),
desired_version,
custom_url: None,
supported_versions: [desired_version],
mode,
}
Expand Down
15 changes: 13 additions & 2 deletions git-transport/src/client/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Connection<R, W> {
pub(in crate::client) virtual_host: Option<(String, Option<u16>)>,
pub(in crate::client) desired_version: Protocol,
supported_versions: [Protocol; 1],
custom_url: Option<String>,
pub(in crate::client) mode: ConnectMode,
}

Expand All @@ -29,6 +30,16 @@ impl<R, W> Connection<R, W> {
pub fn into_inner(self) -> (R, W) {
(self.line_provider.into_inner(), self.writer)
}

/// Optionally set the URL to be returned when asked for it if `Some` or calculate a default for `None`.
///
/// The URL is required as parameter for authentication helpers which are called in transports
/// that support authentication. Even though plain git transports don't support that, this
/// may well be the case in custom transports.
pub fn custom_url(mut self, url: Option<String>) -> Self {
self.custom_url = url;
self
}
}

pub(crate) mod message {
Expand Down Expand Up @@ -60,7 +71,7 @@ pub(crate) mod message {
// as extra lines in the reply, which we don't want to handle. Especially since an old server will not respond with that
// line (is what I assume, at least), so it's an optional part in the response to understand and handle. There is no value
// in that, so let's help V2 servers to respond in a way that assumes V1.
let needs_null_prefix = if version != Protocol::V1 {
let extra_params_need_null_prefix = if version != Protocol::V1 {
out.push(0);
out.push_str(format!("version={}", version as usize));
out.push(0);
Expand All @@ -70,7 +81,7 @@ pub(crate) mod message {
};

if !extra_parameters.is_empty() {
if needs_null_prefix {
if extra_params_need_null_prefix {
out.push(0);
}
for (key, value) in extra_parameters {
Expand Down
5 changes: 4 additions & 1 deletion git-transport/tests/client/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex};
async fn handshake_v1_and_request() -> crate::Result {
let mut out = Vec::new();
let server_response = fixture_bytes("v1/clone.response");
let mut c = git::Connection::new(
let c = git::Connection::new(
server_response.as_slice(),
&mut out,
Protocol::V1,
Expand All @@ -29,6 +29,9 @@ async fn handshake_v1_and_request() -> crate::Result {
git::ConnectMode::Daemon,
);
assert!(c.is_stateful(), "tcp connections are stateful");
let c = c.custom_url(Some("anything".into()));
assert_eq!(c.to_url(), "anything");
let mut c = c.custom_url(None);
assert_eq!(c.to_url(), "file:///foo.git");
let mut res = c.handshake(Service::UploadPack, &[]).await?;
assert_eq!(res.actual_protocol, Protocol::V1);
Expand Down

0 comments on commit f7437e0

Please sign in to comment.