From 202383add69e7667fb2043d55e17f8064bc658c9 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 4 Sep 2020 12:33:15 +0800 Subject: [PATCH] refactor --- git-transport/src/client/capabilities.rs | 48 +++++++++++++++++ git-transport/src/client/git.rs | 66 ++++-------------------- git-transport/src/client/http/mod.rs | 15 +++--- 3 files changed, 67 insertions(+), 62 deletions(-) diff --git a/git-transport/src/client/capabilities.rs b/git-transport/src/client/capabilities.rs index 19792540b15..24c09656b67 100644 --- a/git-transport/src/client/capabilities.rs +++ b/git-transport/src/client/capabilities.rs @@ -109,3 +109,51 @@ impl Capabilities { .map(|c| Capability(c.as_bstr())) } } + +pub(crate) mod recv { + use crate::{client, client::Capabilities, Protocol}; + use bstr::ByteSlice; + use std::io; + + pub struct Outcome<'a> { + pub capabilities: Capabilities, + pub refs: Option>, + pub protocol: Protocol, + } + + pub fn v1_or_v2_as_detected(rd: &mut git_packetline::Provider) -> Result { + rd.fail_on_err_lines(true); + let capabilities_or_version = rd + .peek_line() + .ok_or(client::Error::ExpectedLine("capabilities or version"))???; + let first_line = capabilities_or_version + .to_text() + .ok_or(client::Error::ExpectedLine("text"))?; + + let version = if first_line.as_bstr().starts_with_str("version ") { + if first_line.as_bstr().ends_with_str(" 1") { + Protocol::V1 + } else { + Protocol::V2 + } + } else { + Protocol::V1 + }; + match version { + Protocol::V1 => { + let (capabilities, delimiter_position) = Capabilities::from_bytes(first_line.0)?; + rd.peek_buffer_replace_and_truncate(delimiter_position, b'\n'); + Ok(Outcome { + capabilities, + refs: Some(Box::new(rd.as_read())), + protocol: Protocol::V1, + }) + } + Protocol::V2 => Ok(Outcome { + capabilities: Capabilities::from_lines(rd.as_read())?, + refs: None, + protocol: Protocol::V2, + }), + } + } +} diff --git a/git-transport/src/client/git.rs b/git-transport/src/client/git.rs index b546bd34674..44a6c71da4e 100644 --- a/git-transport/src/client/git.rs +++ b/git-transport/src/client/git.rs @@ -1,7 +1,14 @@ -use crate::{client, client::SetServiceResponse, Protocol, Service}; +use crate::{ + client::{self, capabilities, SetServiceResponse}, + Protocol, Service, +}; use bstr::BString; use git_packetline::PacketLine; -use std::{io, io::Write, net::TcpStream}; +use std::{ + io, + io::Write, + net::{TcpStream, ToSocketAddrs}, +}; pub mod message { use crate::{Protocol, Service}; @@ -38,56 +45,6 @@ pub mod message { out } } -pub(crate) mod recv { - use crate::{client, client::Capabilities, Protocol}; - use bstr::ByteSlice; - use std::io; - - pub struct Outcome<'a> { - pub capabilities: Capabilities, - pub refs: Option>, - pub protocol: Protocol, - } - - pub fn capabilities_and_possibly_refs( - rd: &mut git_packetline::Provider, - ) -> Result { - rd.fail_on_err_lines(true); - let capabilities_or_version = rd - .peek_line() - .ok_or(client::Error::ExpectedLine("capabilities or version"))???; - let first_line = capabilities_or_version - .to_text() - .ok_or(client::Error::ExpectedLine("text"))?; - - let version = if first_line.as_bstr().starts_with_str("version ") { - if first_line.as_bstr().ends_with_str(" 1") { - Protocol::V1 - } else { - Protocol::V2 - } - } else { - Protocol::V1 - }; - match version { - Protocol::V1 => { - let (capabilities, delimiter_position) = Capabilities::from_bytes(first_line.0)?; - rd.peek_buffer_replace_and_truncate(delimiter_position, b'\n'); - Ok(Outcome { - capabilities, - refs: Some(Box::new(rd.as_read())), - protocol: Protocol::V1, - }) - } - Protocol::V2 => Ok(Outcome { - capabilities: Capabilities::from_lines(rd.as_read())?, - refs: None, - protocol: Protocol::V2, - }), - } - } -} - #[derive(PartialEq, Eq, Clone, Copy)] pub enum ConnectMode { Daemon, @@ -121,11 +78,11 @@ where line_writer.flush()?; } - let recv::Outcome { + let capabilities::recv::Outcome { capabilities, refs, protocol: actual_protocol, - } = recv::capabilities_and_possibly_refs(&mut self.line_provider)?; + } = capabilities::recv::v1_or_v2_as_detected(&mut self.line_provider)?; Ok(SetServiceResponse { actual_protocol, capabilities, @@ -206,7 +163,6 @@ where } use quick_error::quick_error; -use std::net::ToSocketAddrs; quick_error! { #[derive(Debug)] pub enum Error { diff --git a/git-transport/src/client/http/mod.rs b/git-transport/src/client/http/mod.rs index 8fb671fe9d1..504618dfb90 100644 --- a/git-transport/src/client/http/mod.rs +++ b/git-transport/src/client/http/mod.rs @@ -1,17 +1,18 @@ -use crate::{client, client::git, Protocol, Service}; +use crate::{ + client::{self, capabilities, ExtendedBufRead, HandleProgress, RequestWriter}, + Protocol, Service, +}; +use git_packetline::PacketLine; use std::{ borrow::Cow, convert::Infallible, - io, - io::{BufRead, Read}, + io::{self, BufRead, Read}, }; #[cfg(feature = "http-client-curl")] pub(crate) mod curl; mod traits; -use crate::client::{ExtendedBufRead, HandleProgress, RequestWriter}; -use git_packetline::PacketLine; pub use traits::{Error, GetResponse, Http, PostResponse}; #[cfg(feature = "http-client-curl")] @@ -117,11 +118,11 @@ impl client::Transport for Transport { )))); } - let git::recv::Outcome { + let capabilities::recv::Outcome { capabilities, refs, protocol: actual_protocol, - } = git::recv::capabilities_and_possibly_refs(line_reader)?; + } = capabilities::recv::v1_or_v2_as_detected(line_reader)?; self.service = Some(service); Ok(client::SetServiceResponse { actual_protocol,