gitdataai/lib/git/cmd/merge/merge_base_many.rs
2026-05-30 01:38:40 +08:00

37 lines
996 B
Rust

use crate::{
bare::GitBare,
cmd::oid::ObjectId,
errors::{GitError, GitResult},
};
impl GitBare {
pub fn merge_base_many(
&self,
commits: Vec<ObjectId>,
) -> GitResult<ObjectId> {
if commits.len() < 2 {
return Err(GitError::ParseError(
"merge_base_many requires at least 2 commits".to_string(),
));
}
let repo = self.gix_repo()?;
let gix_commits: Vec<gix::hash::ObjectId> = commits
.iter()
.map(|id| id.try_into())
.collect::<Result<Vec<_>, _>>()?;
let first = gix_commits[0];
let others = &gix_commits[1..];
let bases = repo.merge_bases_many(first, others)?;
let base = bases.first().ok_or_else(|| {
GitError::ObjectNotFound(
"no common merge base found for the given commits".to_string(),
)
})?;
Ok(ObjectId::new(base.detach().to_hex().to_string()))
}
}