Skip to content

Add support for specifying custom headers on git push and fetch #711

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

Merged
merged 1 commit into from
May 24, 2021
Merged
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
45 changes: 36 additions & 9 deletions src/remote.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use libc;
use std::ffi::CString;
use raw::git_strarray;
use std::marker;
use std::mem;
use std::ops::Range;
use std::ptr;
use std::slice;
use std::str;
use std::{ffi::CString, os::raw::c_char};

use crate::string_array::StringArray;
use crate::util::Binding;
Expand Down Expand Up @@ -43,13 +44,17 @@ pub struct FetchOptions<'cb> {
prune: FetchPrune,
update_fetchhead: bool,
download_tags: AutotagOption,
custom_headers: Vec<CString>,
custom_headers_ptrs: Vec<*const c_char>,
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need the duplication?
also I think the best practice is to keep stuff in Option by default.

Copy link
Member

Choose a reason for hiding this comment

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

Nah I think this is ok, empty vecs don't need allocation and one here is the owned vector of strings and the other is what we pass to libgit2 itself

}

/// Options to control the behavior of a git push.
pub struct PushOptions<'cb> {
callbacks: Option<RemoteCallbacks<'cb>>,
proxy: Option<ProxyOptions<'cb>>,
pb_parallelism: u32,
custom_headers: Vec<CString>,
custom_headers_ptrs: Vec<*const c_char>,
}

/// Holds callbacks for a connection to a `Remote`. Disconnects when dropped
Expand Down Expand Up @@ -474,6 +479,8 @@ impl<'cb> FetchOptions<'cb> {
prune: FetchPrune::Unspecified,
update_fetchhead: true,
download_tags: AutotagOption::Unspecified,
custom_headers: Vec::new(),
custom_headers_ptrs: Vec::new(),
}
}

Expand Down Expand Up @@ -511,6 +518,16 @@ impl<'cb> FetchOptions<'cb> {
self.download_tags = opt;
self
}

/// Set extra headers for this fetch operation.
pub fn custom_headers(&mut self, custom_headers: &[&str]) -> &mut Self {
self.custom_headers = custom_headers
.iter()
.map(|&s| CString::new(s).unwrap())
.collect();
self.custom_headers_ptrs = self.custom_headers.iter().map(|s| s.as_ptr()).collect();
self
}
}

impl<'cb> Binding for FetchOptions<'cb> {
Expand All @@ -535,10 +552,9 @@ impl<'cb> Binding for FetchOptions<'cb> {
prune: crate::call::convert(&self.prune),
update_fetchhead: crate::call::convert(&self.update_fetchhead),
download_tags: crate::call::convert(&self.download_tags),
// TODO: expose this as a builder option
custom_headers: raw::git_strarray {
count: 0,
strings: ptr::null_mut(),
custom_headers: git_strarray {
count: self.custom_headers_ptrs.len(),
strings: self.custom_headers_ptrs.as_ptr() as *mut _,
},
}
}
Expand All @@ -557,6 +573,8 @@ impl<'cb> PushOptions<'cb> {
callbacks: None,
proxy: None,
pb_parallelism: 1,
custom_headers: Vec::new(),
custom_headers_ptrs: Vec::new(),
}
}

Expand All @@ -582,6 +600,16 @@ impl<'cb> PushOptions<'cb> {
self.pb_parallelism = parallel;
self
}

/// Set extra headers for this push operation.
pub fn custom_headers(&mut self, custom_headers: &[&str]) -> &mut Self {
self.custom_headers = custom_headers
.iter()
.map(|&s| CString::new(s).unwrap())
.collect();
self.custom_headers_ptrs = self.custom_headers.iter().map(|s| s.as_ptr()).collect();
self
}
}

impl<'cb> Binding for PushOptions<'cb> {
Expand All @@ -604,10 +632,9 @@ impl<'cb> Binding for PushOptions<'cb> {
.map(|m| m.raw())
.unwrap_or_else(|| ProxyOptions::new().raw()),
pb_parallelism: self.pb_parallelism as libc::c_uint,
// TODO: expose this as a builder option
custom_headers: raw::git_strarray {
count: 0,
strings: ptr::null_mut(),
custom_headers: git_strarray {
count: self.custom_headers_ptrs.len(),
strings: self.custom_headers_ptrs.as_ptr() as *mut _,
},
}
}
Expand Down