Skip to content

Commit

Permalink
Add ps::private::cherry_picking module
Browse files Browse the repository at this point in the history
Add ps::private::cherry_picking module so that we have a place to house
Patch Stack specific cherry picking concerns. To start we have added the
map_range_for_cherry_pick() function that translates patch indexs to
struct ready to be used with the git modules cherry pick functions.

<!-- ps-id: c0921e7b-2d0c-429a-86fa-bac397a665c3 -->
  • Loading branch information
drewdeponte committed Mar 22, 2023
1 parent d63ad32 commit f42e67b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/ps/private/cherry_picking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::super::super::ps;
use std::result::Result;

pub struct CherryPickRange {
pub root_oid: git2::Oid,
pub leaf_oid: Option<git2::Oid>,
}

#[derive(Debug)]
pub enum MapRangeForCherryPickError {
StartIndexNotFound,
EndIndexNotFound,
}

pub fn map_range_for_cherry_pick(
patches_vec: &Vec<ps::ListPatch>,
start_patch_index: usize,
end_patch_index: Option<usize>,
) -> Result<CherryPickRange, MapRangeForCherryPickError> {
let start_patch_oid = patches_vec
.get(start_patch_index)
.ok_or(MapRangeForCherryPickError::StartIndexNotFound)?
.oid;

Ok(match (start_patch_index, end_patch_index) {
(si, Some(ei)) if (si < ei) => {
let end_patch_oid = patches_vec
.get(ei)
.ok_or(MapRangeForCherryPickError::EndIndexNotFound)?
.oid;
CherryPickRange {
root_oid: start_patch_oid,
leaf_oid: Some(end_patch_oid),
}
}
(si, Some(ei)) if (si > ei) => {
let end_patch_oid = patches_vec
.get(ei)
.ok_or(MapRangeForCherryPickError::EndIndexNotFound)?
.oid;
CherryPickRange {
root_oid: end_patch_oid,
leaf_oid: Some(start_patch_oid),
}
}
_ => CherryPickRange {
root_oid: start_patch_oid,
leaf_oid: None,
},
})
}
1 change: 1 addition & 0 deletions src/ps/private/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod checkout;
pub mod cherry_picking;
pub mod commit_is_behind;
pub mod config;
pub mod git;
Expand Down

0 comments on commit f42e67b

Please sign in to comment.