1 use std::mem;
2 
3 use crate::build::CheckoutBuilder;
4 use crate::merge::MergeOptions;
5 use crate::raw;
6 use std::ptr;
7 
8 /// Options to specify when cherry picking
9 pub struct CherrypickOptions<'cb> {
10     mainline: u32,
11     checkout_builder: Option<CheckoutBuilder<'cb>>,
12     merge_opts: Option<MergeOptions>,
13 }
14 
15 impl<'cb> CherrypickOptions<'cb> {
16     /// Creates a default set of cherrypick options
new() -> CherrypickOptions<'cb>17     pub fn new() -> CherrypickOptions<'cb> {
18         CherrypickOptions {
19             mainline: 0,
20             checkout_builder: None,
21             merge_opts: None,
22         }
23     }
24 
25     /// Set the mainline value
26     ///
27     /// For merge commits, the "mainline" is treated as the parent.
mainline(&mut self, mainline: u32) -> &mut Self28     pub fn mainline(&mut self, mainline: u32) -> &mut Self {
29         self.mainline = mainline;
30         self
31     }
32 
33     /// Set the checkout builder
checkout_builder(&mut self, cb: CheckoutBuilder<'cb>) -> &mut Self34     pub fn checkout_builder(&mut self, cb: CheckoutBuilder<'cb>) -> &mut Self {
35         self.checkout_builder = Some(cb);
36         self
37     }
38 
39     /// Set the merge options
merge_opts(&mut self, merge_opts: MergeOptions) -> &mut Self40     pub fn merge_opts(&mut self, merge_opts: MergeOptions) -> &mut Self {
41         self.merge_opts = Some(merge_opts);
42         self
43     }
44 
45     /// Obtain the raw struct
raw(&mut self) -> raw::git_cherrypick_options46     pub fn raw(&mut self) -> raw::git_cherrypick_options {
47         unsafe {
48             let mut checkout_opts: raw::git_checkout_options = mem::zeroed();
49             raw::git_checkout_init_options(&mut checkout_opts, raw::GIT_CHECKOUT_OPTIONS_VERSION);
50             if let Some(ref mut cb) = self.checkout_builder {
51                 cb.configure(&mut checkout_opts);
52             }
53 
54             let mut merge_opts: raw::git_merge_options = mem::zeroed();
55             raw::git_merge_init_options(&mut merge_opts, raw::GIT_MERGE_OPTIONS_VERSION);
56             if let Some(ref opts) = self.merge_opts {
57                 ptr::copy(opts.raw(), &mut merge_opts, 1);
58             }
59 
60             let mut cherrypick_opts: raw::git_cherrypick_options = mem::zeroed();
61             raw::git_cherrypick_init_options(
62                 &mut cherrypick_opts,
63                 raw::GIT_CHERRYPICK_OPTIONS_VERSION,
64             );
65             cherrypick_opts.mainline = self.mainline;
66             cherrypick_opts.checkout_opts = checkout_opts;
67             cherrypick_opts.merge_opts = merge_opts;
68 
69             cherrypick_opts
70         }
71     }
72 }
73