Skip to content

Commit

Permalink
Merge branch 'paulyoung/scheme-ext'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 7, 2022
2 parents a47ad84 + 93e6d71 commit 3e27550
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion git-repository/tests/fixtures/make_remote_repos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ git init --bare bad-url-rewriting
[remote "origin"]
pushUrl = "file://dev/null"
[url "foo://"]
[url "invalid:://"]
pushInsteadOf = "file://"
[url "https://github.com/byron/"]
Expand Down
3 changes: 1 addition & 2 deletions git-repository/tests/repository/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod find_remote {
"…but is able to replace the fetch url successfully"
);

let expected_err_msg = "The rewritten push url \"foo://dev/null\" failed to parse";
let expected_err_msg = "The rewritten push url \"invalid:://dev/null\" failed to parse";
assert_eq!(
repo.find_remote("origin").unwrap_err().to_string(),
expected_err_msg,
Expand All @@ -237,7 +237,6 @@ mod find_remote {
"it can rewrite a single url like git can"
);
}
assert_eq!(remote.url(Direction::Push).unwrap().to_bstring(), "file://dev/null",);
assert_eq!(
remote.rewrite_urls().unwrap_err().to_string(),
expected_err_msg,
Expand Down
2 changes: 1 addition & 1 deletion git-transport/src/client/blocking_io/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn connect(
if desired_version != Protocol::V1 {
let mut args = vec![Cow::from("-o"), "SendEnv=GIT_PROTOCOL".into()];
if let Some(port) = port {
args.push(format!("-p={}", port).into());
args.push(format!("-p{}", port).into());
}
Some((
args,
Expand Down
15 changes: 4 additions & 11 deletions git-url/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
borrow::Cow,
convert::{Infallible, TryFrom},
};
use std::{borrow::Cow, convert::Infallible};

pub use bstr;
use bstr::{BStr, ByteSlice};
Expand All @@ -16,8 +13,6 @@ pub enum Error {
Utf8(#[from] std::str::Utf8Error),
#[error(transparent)]
Url(#[from] url::ParseError),
#[error("Protocol {protocol:?} is not supported")]
UnsupportedProtocol { protocol: String },
#[error("Paths cannot be empty")]
EmptyPath,
#[error("Relative URLs are not permitted: {url:?}")]
Expand All @@ -30,10 +25,8 @@ impl From<Infallible> for Error {
}
}

fn str_to_protocol(s: &str) -> Result<Scheme, Error> {
Scheme::try_from(s).map_err(|invalid| Error::UnsupportedProtocol {
protocol: invalid.into(),
})
fn str_to_protocol(s: &str) -> Scheme {
Scheme::from(s)
}

fn guess_protocol(url: &[u8]) -> &str {
Expand Down Expand Up @@ -67,7 +60,7 @@ fn try_strip_file_protocol(url: &[u8]) -> Option<&[u8]> {
fn to_owned_url(url: url::Url) -> Result<crate::Url, Error> {
Ok(crate::Url {
serialize_alternative_form: false,
scheme: str_to_protocol(url.scheme())?,
scheme: str_to_protocol(url.scheme()),
user: if url.username().is_empty() {
None
} else {
Expand Down
27 changes: 16 additions & 11 deletions git-url/src/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
use std::convert::TryFrom;

/// A scheme for use in a [`Url`][crate::Url].
/// A scheme or protocol for use in a [`Url`][crate::Url].
///
/// It defines how to talk to a given repository.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Scheme {
/// A local resource that is accessible on the current host.
File,
/// A git daemon, like `File` over TCP/IP.
Git,
/// Launch `git-upload-pack` through an `ssh` tunnel.
Ssh,
/// Use the HTTP protocol to talk to git servers.
Http,
/// Use the HTTPS protocol to talk to git servers.
Https,
/// Any other protocol or transport that isn't known at compile time.
///
/// It's used to support plug-in transports.
Ext(String),
}

impl<'a> TryFrom<&'a str> for Scheme {
type Error = &'a str;

fn try_from(value: &'a str) -> Result<Self, Self::Error> {
Ok(match value {
impl<'a> From<&'a str> for Scheme {
fn from(value: &'a str) -> Self {
match value {
"ssh" => Scheme::Ssh,
"file" => Scheme::File,
"git" => Scheme::Git,
"http" => Scheme::Http,
"https" => Scheme::Https,
"rad" => Scheme::Ext("rad".into()),
unknown => return Err(unknown),
})
unknown => Scheme::Ext(unknown.into()),
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions git-url/tests/parse/invalid.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::parse::assert_failure;

#[test]
fn unknown_protocol() {
assert_failure("foo://host.xz/path/to/repo.git/", "Protocol \"foo\" is not supported")
fn relative_path_due_to_double_colon() {
assert_failure(
"invalid:://host.xz/path/to/repo.git/",
"Relative URLs are not permitted: \"invalid:://host.xz/path/to/repo.git/\"",
)
}

#[test]
Expand Down
14 changes: 14 additions & 0 deletions git-url/tests/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,17 @@ mod git {
)
}
}

mod unknown {
use git_url::Scheme;

use crate::parse::{assert_url_roundtrip, url};

#[test]
fn any_protocol_is_supported_via_the_ext_scheme() -> crate::Result {
assert_url_roundtrip(
"abc://example.com/~byron/hello",
url(Scheme::Ext("abc".into()), None, "example.com", None, b"/~byron/hello"),
)
}
}

0 comments on commit 3e27550

Please sign in to comment.