gitoxide_core/repository/
merge_base.rs1use anyhow::bail;
2
3use crate::OutputFormat;
4
5pub fn merge_base(
6 mut repo: gix::Repository,
7 first: String,
8 others: Vec<String>,
9 mut out: impl std::io::Write,
10 format: OutputFormat,
11) -> anyhow::Result<()> {
12 if format != OutputFormat::Human {
13 bail!("Only 'human' format is currently supported");
14 }
15 repo.object_cache_size_if_unset(50 * 1024 * 1024);
16 let first_id = repo.rev_parse_single(first.as_str())?;
17 let other_ids: Vec<_> = others
18 .iter()
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}