Skip to content

Commit

Permalink
refactor(dag): Remove legacy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonrcarter committed Nov 7, 2022
1 parent 41e9e72 commit 8d3d175
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 61 deletions.
48 changes: 0 additions & 48 deletions git-branchless-lib/src/core/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,28 +296,6 @@ impl Dag {
Ok(dag)
}

/// Get one of the merge-base OIDs for the given pair of OIDs. If there are
/// multiple possible merge-bases, one is arbitrarily returned.
#[instrument]
pub fn get_one_merge_base_oid(
&self,
effects: &Effects,
repo: &Repo,
lhs_oid: NonZeroOid,
rhs_oid: NonZeroOid,
) -> eyre::Result<Option<NonZeroOid>> {
let set = vec![CommitVertex::from(lhs_oid), CommitVertex::from(rhs_oid)];
let set = self
.inner
.sort(&CommitSet::from_static_names(set))
.wrap_err("Sorting DAG vertex set")?;
let vertex = self.inner.gca_one(set).wrap_err("Computing merge-base")?;
match vertex {
None => Ok(None),
Some(vertex) => Ok(Some(vertex.to_hex().parse()?)),
}
}

/// Get the parent OID for the given OID. Returns an error if the given OID
/// does not have exactly 1 parent.
#[instrument]
Expand Down Expand Up @@ -429,32 +407,6 @@ impl Dag {
})
}

/// Find a path from the provided head to its merge-base with the main
/// branch.
#[instrument]
pub fn find_path_to_main_branch(
&self,
effects: &Effects,
head: CommitSet,
) -> eyre::Result<Option<CommitSet>> {
// FIXME: this assumes that there is only one merge-base with the main branch.
let merge_base = {
let (_effects, _progress) = effects.start_operation(OperationType::GetMergeBase);
self.query().gca_one(self.main_branch_commit.union(&head))?
};
let merge_base = match merge_base {
Some(merge_base) => merge_base,
None => return Ok(None),
};

// FIXME: this assumes that there is only one path to the merge-base.
let path = {
let (_effects, _progress) = effects.start_operation(OperationType::FindPathToMergeBase);
self.query().range(CommitSet::from(merge_base), head)?
};
Ok(Some(path))
}

/// Given a CommitSet, return a list of CommitSets, each representing a
/// connected component of the set.
///
Expand Down
8 changes: 5 additions & 3 deletions git-branchless/src/commands/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::convert::TryFrom;
use std::fmt::Write;
use std::time::SystemTime;

use eden_dag::DagAlgorithm;
use eden_dag::{DagAlgorithm, VertexName};
use lib::core::repo_ext::RepoExt;
use lib::util::ExitCode;
use rayon::ThreadPoolBuilder;
Expand All @@ -32,7 +32,7 @@ use lib::git::{GitRunInfo, NonZeroOid, Repo};
#[instrument]
fn resolve_base_commit(
dag: &Dag,
merge_base_oid: Option<NonZeroOid>,
merge_base_oid: Option<VertexName>,
oid: NonZeroOid,
) -> eyre::Result<NonZeroOid> {
let bases = match merge_base_oid {
Expand Down Expand Up @@ -207,7 +207,9 @@ pub fn r#move(
let base_oids = {
let mut result = Vec::new();
for base_oid in commit_set_to_vec(&base_oids)? {
let merge_base_oid = dag.get_one_merge_base_oid(effects, &repo, base_oid, dest_oid)?;
let merge_base_oid = dag
.query()
.gca_one(vec![base_oid, dest_oid].into_iter().collect::<CommitSet>())?;
let base_commit_oid = resolve_base_commit(&dag, merge_base_oid, base_oid)?;
result.push(CommitSet::from(base_commit_oid))
}
Expand Down
29 changes: 19 additions & 10 deletions git-branchless/src/commands/smartlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,20 @@ mod graph {
// Find the nearest ancestor that is included in the graph and
// also on the same branch.

let nearest_branch_ancestor = match dag
.find_path_to_main_branch(effects, CommitSet::from(parent_oid))?
{
Some(path_to_main_branch) => dag.query().heads_ancestors(
path_to_main_branch.intersection(&graph_vertices),
)?,
let parent_set = CommitSet::from(parent_oid);
let merge_base = dag
.query()
.gca_one(dag.main_branch_commit.union(&parent_set))?;

let path_to_main_branch = match merge_base {
Some(merge_base) => {
dag.query().range(CommitSet::from(merge_base), parent_set)?
}
None => CommitSet::empty(),
};
let nearest_branch_ancestor = dag
.query()
.heads_ancestors(path_to_main_branch.intersection(&graph_vertices))?;

let ancestor_oids = commit_set_to_vec(&nearest_branch_ancestor)?;
for ancestor_oid in ancestor_oids.iter() {
Expand Down Expand Up @@ -414,6 +420,7 @@ mod graph {
mod render {
use std::cmp::Ordering;
use std::collections::HashSet;
use std::convert::TryFrom;

use cursive::theme::Effect;
use cursive::utils::markup::StyledString;
Expand All @@ -440,7 +447,6 @@ mod render {
/// Returns the list such that the topologically-earlier subgraphs are first in
/// the list (i.e. those that would be rendered at the bottom of the smartlog).
fn split_commit_graph_by_roots(
effects: &Effects,
repo: &Repo,
dag: &Dag,
graph: &SmartlogGraph,
Expand All @@ -465,10 +471,13 @@ mod render {
_ => return lhs_oid.cmp(rhs_oid),
};

let merge_base_oid = dag.get_one_merge_base_oid(effects, repo, *lhs_oid, *rhs_oid);
let merge_base_oid = dag
.query()
.gca_one(vec![*lhs_oid, *rhs_oid].into_iter().collect::<CommitSet>());
let merge_base_oid = match merge_base_oid {
Err(_) => return lhs_oid.cmp(rhs_oid),
Ok(merge_base_oid) => merge_base_oid,
Ok(None) => None,
Ok(Some(merge_base_oid)) => NonZeroOid::try_from(merge_base_oid).ok(),
};

match merge_base_oid {
Expand Down Expand Up @@ -698,7 +707,7 @@ mod render {
head_oid: Option<NonZeroOid>,
commit_descriptors: &mut [&mut dyn NodeDescriptor],
) -> eyre::Result<Vec<StyledString>> {
let root_oids = split_commit_graph_by_roots(effects, repo, dag, graph);
let root_oids = split_commit_graph_by_roots(repo, dag, graph);
let lines = get_output(
effects.get_glyphs(),
dag,
Expand Down

0 comments on commit 8d3d175

Please sign in to comment.