Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make transport::Service Debug and add some HTTP constants from libgit2. #1069

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitflags = "2.1.0"
libc = "0.2"
log = "0.4.8"
libgit2-sys = { path = "libgit2-sys", version = "0.17.0" }
http = "1.1"

[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]
openssl-sys = { version = "0.9.45", optional = true }
Expand Down
47 changes: 46 additions & 1 deletion src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait SmartSubtransport: Send + 'static {
}

/// Actions that a smart transport can ask a subtransport to perform
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
vcfxb marked this conversation as resolved.
Show resolved Hide resolved
#[allow(missing_docs)]
pub enum Service {
UploadPackLs,
Expand All @@ -63,6 +63,51 @@ pub enum Service {
ReceivePack,
}

/// HTTP implementation related info for various services.
///
/// This information was pulled from
/// <https://github.com/libgit2/libgit2/blob/2ecc8586f7eec4063b5da1563d0a33f9e9f9fcf7/src/libgit2/transports/http.c#L68-L95>.
impl Service {
/// The HTTP Method used by libgit2's implementation for http(s) transport.
pub const fn http_method(self) -> http::Method {
use http::Method;

match self {
Service::UploadPackLs | Service::ReceivePackLs => Method::GET,
Service::UploadPack | Service::ReceivePack => Method::POST,
}
}

/// The value of the HTTP "Accept" header that is used by libgit2's implementation for http(s) transport.
pub const fn http_accept_header(self) -> &'static str {
match self {
Service::UploadPackLs => "application/x-git-upload-pack-advertisement",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a public interface exposed by libgit2? If not I tend not to add this. We don't really want to get into troubles that there is no stability guarantee in libgit2 while git2-rs exposes it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire Service type is native to git2-rs -- libgit2 doesn't have anything really matching it. The closest thing is https://github.com/libgit2/libgit2/blob/main/src/libgit2/transports/http.c but that is not publicly exposed. I can definitely understand not merging this because of that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really appreciate your understanding. Yeah we need to weight on that.

Anyway, as GitHub keeps diff in a pull request indefinitely, this can be used as a reference when people need such a glue code, which is great!

Per discussion, I am going to close this and thank you again!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! You're welcome!

Service::ReceivePackLs => "application/x-git-receive-pack-advertisement",
Service::UploadPack => "application/x-git-upload-pack-result",
Service::ReceivePack => "application/x-git-receive-pack-result",
}
}

/// The value of the HTTP "Content-Type" header that is used by libgit2's implementation for http(s) transport.
pub const fn http_content_type_header(self) -> Option<&'static str> {
match self {
Service::UploadPackLs | Service::ReceivePackLs => None,
Service::ReceivePack => Some("application/x-git-receive-pack-request"),
Service::UploadPack => Some("application/x-git-upload-pack-request"),
}
}

/// The HTTP Url path and query suffix used by libgit2's implementation for http(s) transport.
pub const fn http_path_and_query(self) -> &'static str {
match self {
Service::UploadPackLs => "/info/refs?service=git-upload-pack",
Service::UploadPack => "/git-upload-pack",
Service::ReceivePackLs => "/info/refs?service=git-receive-pack",
Service::ReceivePack => "/git-receive-pack",
}
}
}

/// An instance of a stream over which a smart transport will communicate with a
/// remote.
///
Expand Down