Skip to content

Commit

Permalink
[clone] Support explicitly closing (v2) connections
Browse files Browse the repository at this point in the history
It's a no-op for everyone else or stateless transports like HTTP
  • Loading branch information
Byron committed Sep 2, 2020
1 parent 55cf167 commit 41e4cb2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions git-transport/src/client/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ impl client::Transport for SpawnProcessOnDemand {
.request(write_mode, on_drop)
}

fn close(mut self) -> Result<(), client::Error> {
if let Some(c) = self.connection.take() {
c.close()
} else {
Ok(())
}
}

fn to_url(&self) -> String {
self.url.to_string()
}
Expand Down
9 changes: 9 additions & 0 deletions git-transport/src/client/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ where
on_drop,
))
}

fn close(mut self) -> Result<(), client::Error> {
if self.version == Protocol::V2 {
git_packetline::encode::flush_to_write(&mut self.writer)?;
self.writer.flush()?;
}
Ok(())
}

fn to_url(&self) -> String {
git_url::Url {
scheme: git_url::Scheme::File,
Expand Down
5 changes: 5 additions & 0 deletions git-transport/src/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ impl<H: Http> client::Transport for Transport<H> {
on_drop,
))
}

fn close(self) -> Result<(), client::Error> {
Ok(())
}

fn to_url(&self) -> String {
self.url.to_owned()
}
Expand Down
3 changes: 3 additions & 0 deletions git-transport/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ pub trait Transport {
/// If `handle_progress` is not None, it's function passed a text line without trailing LF from which progress information can be parsed.
fn request(&mut self, write_mode: WriteMode, on_drop: Vec<MessageKind>) -> Result<RequestWriter, Error>;

/// Closes the connection to indicate no further requests will be made.
fn close(self) -> Result<(), Error>;

/// Returns the canonical URL pointing to the destination of this transport.
/// Please note that local paths may not be represented correctly, as they will go through a potentially lossy
/// unicode conversion.
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 @@ -143,6 +143,8 @@ fn handshake_v1_and_request() -> crate::Result {
drop(reader);
let sidebands = Rc::try_unwrap(messages).expect("no other handle").into_inner();
assert_eq!(sidebands.len(), 6, "…along with some status messages");
c.close()?;

assert_eq!(
out.as_slice().as_bstr(),
b"002egit-upload-pack /foo.git\0host=example.org\0000ahello\n\
Expand Down Expand Up @@ -281,6 +283,7 @@ fn handshake_v2_and_request() -> crate::Result {
drop(res);
let messages = Rc::try_unwrap(messages).expect("no other handle").into_inner();
assert_eq!(messages.len(), 4);
c.close()?;

assert_eq!(
out.as_slice().as_bstr(),
Expand All @@ -300,7 +303,7 @@ fn handshake_v2_and_request() -> crate::Result {
000eofs-delta
0032want 808e50d724f604f69ab93c6da2919c014667bedb
0009done
0000"
00000000"
.as_bstr(),
"it sends the correct request, including the adjusted version"
);
Expand Down
3 changes: 3 additions & 0 deletions git-transport/tests/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ fn handshake_v1() -> crate::Result {
"e8df6c1ffb7afa27aff9abbe11c7e4b80d19b61e refs/tags/v0.3.0^{}"
]
);
c.close()?;

assert_eq!(
server.received_as_string().lines().collect::<Vec<_>>(),
format!(
Expand Down Expand Up @@ -405,6 +407,7 @@ Accept: application/x-git-upload-pack-result
drop(res);
let messages = Rc::try_unwrap(messages).expect("no other handle").into_inner();
assert_eq!(messages.len(), 5);
c.close()?;

assert_eq!(
server.received_as_string().lines().collect::<Vec<_>>(),
Expand Down

0 comments on commit 41e4cb2

Please sign in to comment.