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 .cloned()
20 .map(|other| repo.rev_parse_single(other.as_str()).map(gix::Id::detach))
21 .collect::<Result<_, _>>()?;
22
23 let cache = repo.commit_graph_if_enabled()?;
24 let mut graph = repo.revision_graph(cache.as_ref());
25 let bases = repo.merge_bases_many_with_graph(first_id, &other_ids, &mut graph)?;
26 if bases.is_empty() {
27 bail!("No base found for {first} and {others}", others = others.join(", "))
28 }
29 for id in bases {
30 writeln!(&mut out, "{id}")?;
31 }
32 Ok(())
33}