gix_revision/merge_base/
mod.rs

1bitflags::bitflags! {
2    /// The flags used in the graph for finding [merge bases](crate::merge_base()).
3    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
4    pub struct Flags: u8 {
5        /// The commit belongs to the graph reachable by the first commit
6        const COMMIT1 = 1 << 0;
7        /// The commit belongs to the graph reachable by all other commits.
8        const COMMIT2 = 1 << 1;
9
10        /// Marks the commit as done, it's reachable by both COMMIT1 and COMMIT2.
11        const STALE = 1 << 2;
12        /// The commit was already put ontto the results list.
13        const RESULT = 1 << 3;
14    }
15}
16
17/// The error returned by the [`merge_base()`][function::merge_base()] function.
18#[derive(Debug, thiserror::Error)]
19#[allow(missing_docs)]
20pub enum Error {
21    #[error("A commit could not be inserted into the graph")]
22    InsertCommit(#[from] gix_revwalk::graph::get_or_insert_default::Error),
23}
24
25pub(crate) mod function;
26
27mod octopus {
28    use crate::merge_base::{Error, Flags};
29    use gix_hash::ObjectId;
30    use gix_revwalk::{graph, Graph};
31
32    /// Given a commit at `first` id, traverse the commit `graph` and return *the best common ancestor* between it and `others`,
33    /// sorted from best to worst. Returns `None` if there is no common merge-base as `first` and `others` don't *all* share history.
34    /// If `others` is empty, `Some(first)` is returned.
35    ///
36    /// # Performance
37    ///
38    /// For repeated calls, be sure to re-use `graph` as its content will be kept and reused for a great speed-up. The contained flags
39    /// will automatically be cleared.
40    pub fn octopus(
41        mut first: ObjectId,
42        others: &[ObjectId],
43        graph: &mut Graph<'_, '_, graph::Commit<Flags>>,
44    ) -> Result<Option<ObjectId>, Error> {
45        for other in others {
46            if let Some(next) =
47                crate::merge_base(first, std::slice::from_ref(other), graph)?.and_then(|bases| bases.into_iter().next())
48            {
49                first = next;
50            } else {
51                return Ok(None);
52            }
53        }
54        Ok(Some(first))
55    }
56}
57pub use octopus::octopus;