use crate::{
    bare::GitBare,
    cmd::oid::ObjectId,
    errors::{GitError, GitResult},
};

impl GitBare {
    pub fn merge_base(
        &self,
        one: ObjectId,
        two: ObjectId,
    ) -> GitResult<ObjectId> {
        let repo = self.gix_repo()?;
        let gix_one: gix::hash::ObjectId = (&one).try_into()?;
        let gix_two: gix::hash::ObjectId = (&two).try_into()?;

        let base = repo.merge_base(gix_one, gix_two).map_err(|_| {
            GitError::ObjectNotFound(format!(
                "no merge base found between {} and {}",
                one, two
            ))
        })?;

        Ok(ObjectId::new(base.detach().to_hex().to_string()))
    }
}
