gitoxide_core/repository/
merge_base.rs

1use crate::OutputFormat;
2use anyhow::bail;
3
4pub fn merge_base(
5    mut repo: gix::Repository,
6    first: String,
7    others: Vec<String>,
8    mut out: impl std::io::Write,
9    format: OutputFormat,
10) -> anyhow::Result<()> {
11    if format != OutputFormat::Human {
12        bail!("Only 'human' format is currently supported");
13    }
14    repo.object_cache_size_if_unset(50 * 1024 * 1024);
15    let first_id = repo.rev_parse_single(first.as_str())?;
16    let other_ids: Vec<_> = others
17        .iter()
18        .cloned()
19        .map(|other| repo.rev_parse_single(other.as_str()).map(gix::Id::detach))
20        .collect::<Result<_, _>>()?;
21
22    let cache = repo.commit_graph_if_enabled()?;
23    let mut graph = repo.revision_graph(cache.as_ref());
24    let bases = repo.merge_bases_many_with_graph(first_id, &other_ids, &mut graph)?;
25    if bases.is_empty() {
26        bail!("No base found for {first} and {others}", others = others.join(", "))
27    }
28    for id in bases {
29        writeln!(&mut out, "{id}")?;
30    }
31    Ok(())
32}