pub struct PatchDag { /* private fields */ }Expand description
The Patch-DAG — a directed acyclic graph of patches.
Internally stores:
- A HashMap of PatchId -> DagNode
- A HashMap of branch name -> target PatchId
- An ancestor cache (lazy, stable across mutations since adding new nodes never changes existing nodes’ ancestor sets)
Implementations§
Source§impl PatchDag
impl PatchDag
Sourcepub fn branch_exists(&self, name: &BranchName) -> bool
pub fn branch_exists(&self, name: &BranchName) -> bool
Check if a branch exists.
Sourcepub fn head(&self) -> Option<(String, PatchId)>
pub fn head(&self) -> Option<(String, PatchId)>
Get the current HEAD branch (the branch named “main”, or the first branch).
Sourcepub fn branch_divergence(
&self,
branch_a: &BranchName,
branch_b: &BranchName,
) -> Result<(usize, usize), DagError>
pub fn branch_divergence( &self, branch_a: &BranchName, branch_b: &BranchName, ) -> Result<(usize, usize), DagError>
Get the number of commits ahead/behind between two branches.
Returns (ahead, behind) where:
aheadis the number of patches onbranch_asince the LCAbehindis the number of patches onbranch_bsince the LCA
Computed by finding the Lowest Common Ancestor (LCA) and counting patches on each branch’s chain between the LCA and the branch tip.
Source§impl PatchDag
impl PatchDag
Sourcepub fn add_patch(
&mut self,
patch: Patch,
parent_ids: Vec<PatchId>,
) -> Result<PatchId, DagError>
pub fn add_patch( &mut self, patch: Patch, parent_ids: Vec<PatchId>, ) -> Result<PatchId, DagError>
Add a patch to the DAG.
§Arguments
patch- The patch to addparent_ids- Parent patch IDs (empty for root commit)
§Errors
DuplicatePatchif a patch with the same ID already existsParentNotFoundif a parent doesn’t exist (unless it’s a root commit)CycleDetectedif adding this edge would create a cycle
Sourcepub fn ancestors(&self, id: &PatchId) -> HashSet<PatchId>
pub fn ancestors(&self, id: &PatchId) -> HashSet<PatchId>
Get all transitive ancestors of a patch (excluding the patch itself).
Uses BFS traversal. Results are cached: the first call for a given patch
ID computes the ancestor set via BFS; subsequent calls return the cached
result in O(1). The cache is safe without invalidation because
add_patch() only creates new nodes — existing nodes’ ancestor sets
never change.
Sourcepub fn lca(&self, a: &PatchId, b: &PatchId) -> Option<PatchId>
pub fn lca(&self, a: &PatchId, b: &PatchId) -> Option<PatchId>
Find the Lowest Common Ancestor (LCA) of two patches.
The LCA is the most recent patch that is an ancestor of both.
Returns None if no common ancestor exists.
Uses generation numbers for O(1) depth comparison instead of BFS-based
ancestor_depth(), reducing complexity from O(n²) to O(n).
Sourcepub fn create_branch(
&mut self,
name: BranchName,
target: PatchId,
) -> Result<(), DagError>
pub fn create_branch( &mut self, name: BranchName, target: PatchId, ) -> Result<(), DagError>
Create a new branch pointing to a patch.
Sourcepub fn get_branch(&self, name: &BranchName) -> Option<PatchId>
pub fn get_branch(&self, name: &BranchName) -> Option<PatchId>
Get the target patch ID of a branch.
Sourcepub fn update_branch(
&mut self,
name: &BranchName,
target: PatchId,
) -> Result<(), DagError>
pub fn update_branch( &mut self, name: &BranchName, target: PatchId, ) -> Result<(), DagError>
Update a branch to point to a new patch.
Sourcepub fn delete_branch(&mut self, name: &BranchName) -> Result<(), DagError>
pub fn delete_branch(&mut self, name: &BranchName) -> Result<(), DagError>
Delete a branch.
Sourcepub fn list_branches(&self) -> Vec<(String, PatchId)>
pub fn list_branches(&self) -> Vec<(String, PatchId)>
List all branches.
Sourcepub fn patch_count(&self) -> usize
pub fn patch_count(&self) -> usize
Get the total number of patches in the DAG.
Sourcepub fn patch_chain(&self, id: &PatchId) -> Vec<PatchId> ⓘ
pub fn patch_chain(&self, id: &PatchId) -> Vec<PatchId> ⓘ
Get patches from a specific node back to root (inclusive).
Sourcepub fn reachable_patches(&self, id: &PatchId) -> Vec<Patch>
pub fn reachable_patches(&self, id: &PatchId) -> Vec<Patch>
Collect all patches reachable from a given patch ID (inclusive).
Source§impl PatchDag
impl PatchDag
Sourcepub fn merge_branches(
&self,
branch_a: &BranchName,
branch_b: &BranchName,
) -> Result<MergeResult, DagError>
pub fn merge_branches( &self, branch_a: &BranchName, branch_b: &BranchName, ) -> Result<MergeResult, DagError>
Compute a merge plan for two branches.
Returns a MergeResult containing the patches to merge and any conflicts.
This does NOT modify the DAG — it only computes the plan.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PatchDag
impl !RefUnwindSafe for PatchDag
impl Send for PatchDag
impl !Sync for PatchDag
impl Unpin for PatchDag
impl UnsafeUnpin for PatchDag
impl UnwindSafe for PatchDag
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more