Skip to content

Commit b673a5c

Browse files
authored
Add support for specifying additional custom headers on git push and fetch operations (#711)
1 parent e16e16a commit b673a5c

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/remote.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use libc;
2-
use std::ffi::CString;
2+
use raw::git_strarray;
33
use std::marker;
44
use std::mem;
55
use std::ops::Range;
66
use std::ptr;
77
use std::slice;
88
use std::str;
9+
use std::{ffi::CString, os::raw::c_char};
910

1011
use crate::string_array::StringArray;
1112
use crate::util::Binding;
@@ -43,13 +44,17 @@ pub struct FetchOptions<'cb> {
4344
prune: FetchPrune,
4445
update_fetchhead: bool,
4546
download_tags: AutotagOption,
47+
custom_headers: Vec<CString>,
48+
custom_headers_ptrs: Vec<*const c_char>,
4649
}
4750

4851
/// Options to control the behavior of a git push.
4952
pub struct PushOptions<'cb> {
5053
callbacks: Option<RemoteCallbacks<'cb>>,
5154
proxy: Option<ProxyOptions<'cb>>,
5255
pb_parallelism: u32,
56+
custom_headers: Vec<CString>,
57+
custom_headers_ptrs: Vec<*const c_char>,
5358
}
5459

5560
/// Holds callbacks for a connection to a `Remote`. Disconnects when dropped
@@ -474,6 +479,8 @@ impl<'cb> FetchOptions<'cb> {
474479
prune: FetchPrune::Unspecified,
475480
update_fetchhead: true,
476481
download_tags: AutotagOption::Unspecified,
482+
custom_headers: Vec::new(),
483+
custom_headers_ptrs: Vec::new(),
477484
}
478485
}
479486

@@ -511,6 +518,16 @@ impl<'cb> FetchOptions<'cb> {
511518
self.download_tags = opt;
512519
self
513520
}
521+
522+
/// Set extra headers for this fetch operation.
523+
pub fn custom_headers(&mut self, custom_headers: &[&str]) -> &mut Self {
524+
self.custom_headers = custom_headers
525+
.iter()
526+
.map(|&s| CString::new(s).unwrap())
527+
.collect();
528+
self.custom_headers_ptrs = self.custom_headers.iter().map(|s| s.as_ptr()).collect();
529+
self
530+
}
514531
}
515532

516533
impl<'cb> Binding for FetchOptions<'cb> {
@@ -535,10 +552,9 @@ impl<'cb> Binding for FetchOptions<'cb> {
535552
prune: crate::call::convert(&self.prune),
536553
update_fetchhead: crate::call::convert(&self.update_fetchhead),
537554
download_tags: crate::call::convert(&self.download_tags),
538-
// TODO: expose this as a builder option
539-
custom_headers: raw::git_strarray {
540-
count: 0,
541-
strings: ptr::null_mut(),
555+
custom_headers: git_strarray {
556+
count: self.custom_headers_ptrs.len(),
557+
strings: self.custom_headers_ptrs.as_ptr() as *mut _,
542558
},
543559
}
544560
}
@@ -557,6 +573,8 @@ impl<'cb> PushOptions<'cb> {
557573
callbacks: None,
558574
proxy: None,
559575
pb_parallelism: 1,
576+
custom_headers: Vec::new(),
577+
custom_headers_ptrs: Vec::new(),
560578
}
561579
}
562580

@@ -582,6 +600,16 @@ impl<'cb> PushOptions<'cb> {
582600
self.pb_parallelism = parallel;
583601
self
584602
}
603+
604+
/// Set extra headers for this push operation.
605+
pub fn custom_headers(&mut self, custom_headers: &[&str]) -> &mut Self {
606+
self.custom_headers = custom_headers
607+
.iter()
608+
.map(|&s| CString::new(s).unwrap())
609+
.collect();
610+
self.custom_headers_ptrs = self.custom_headers.iter().map(|s| s.as_ptr()).collect();
611+
self
612+
}
585613
}
586614

587615
impl<'cb> Binding for PushOptions<'cb> {
@@ -604,10 +632,9 @@ impl<'cb> Binding for PushOptions<'cb> {
604632
.map(|m| m.raw())
605633
.unwrap_or_else(|| ProxyOptions::new().raw()),
606634
pb_parallelism: self.pb_parallelism as libc::c_uint,
607-
// TODO: expose this as a builder option
608-
custom_headers: raw::git_strarray {
609-
count: 0,
610-
strings: ptr::null_mut(),
635+
custom_headers: git_strarray {
636+
count: self.custom_headers_ptrs.len(),
637+
strings: self.custom_headers_ptrs.as_ptr() as *mut _,
611638
},
612639
}
613640
}

0 commit comments

Comments
 (0)