use kernel::TimeMs;
use crate::checkpoint::Provenance;
use crate::error::StorageError;
use super::trees::Worktrees;
pub struct Landing<'a> {
pub t: TimeMs,
pub of: &'a Provenance,
pub subject: &'a str,
pub reviewed_by_person: bool,
}
pub struct PlannedMerge<'a> {
pub(super) trees: &'a Worktrees,
pub(super) target: git2::Oid,
}
impl std::fmt::Debug for PlannedMerge<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PlannedMerge")
.field("commit", &self.target)
.finish()
}
}
impl PlannedMerge<'_> {
pub fn commit(&self) -> String {
self.target.to_string()
}
pub fn apply(self, landing: &Landing<'_>) -> Result<(), StorageError> {
self.trees.land_merge(self.target, landing)
}
}
#[derive(Clone, Copy)]
pub(super) enum CheckoutRun {
DryRun,
Write,
}
pub(super) fn check_out(
repo: &git2::Repository,
tree: &git2::Tree<'_>,
run: CheckoutRun,
) -> Result<(), StorageError> {
let mut paths = Vec::new();
let mut checkout = git2::build::CheckoutBuilder::new();
checkout.safe();
match run {
CheckoutRun::DryRun => checkout.dry_run(),
CheckoutRun::Write => &mut checkout,
};
checkout.notify_on(git2::CheckoutNotificationType::CONFLICT);
checkout.notify(|_, path, _, _, _| {
paths.extend(path.map(|p| p.to_string_lossy().into_owned()));
true
});
let outcome = repo.checkout_tree(tree.as_object(), Some(&mut checkout));
drop(checkout);
if !paths.is_empty() {
return Err(StorageError::MergeWouldDiscard { paths });
}
outcome.map_err(|err| StorageError::Worktree {
op: "check out the merged trunk",
detail: err.to_string(),
})
}