Skip to main content

PatchDag

Struct PatchDag 

Source
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

Source

pub fn branch_exists(&self, name: &BranchName) -> bool

Check if a branch exists.

Source

pub fn head(&self) -> Option<(String, PatchId)>

Get the current HEAD branch (the branch named “main”, or the first branch).

Source

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:

  • ahead is the number of patches on branch_a since the LCA
  • behind is the number of patches on branch_b since 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

Source

pub fn new() -> Self

Create a new empty DAG.

Source

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 add
  • parent_ids - Parent patch IDs (empty for root commit)
§Errors
  • DuplicatePatch if a patch with the same ID already exists
  • ParentNotFound if a parent doesn’t exist (unless it’s a root commit)
  • CycleDetected if adding this edge would create a cycle
Source

pub fn get_patch(&self, id: &PatchId) -> Option<&Patch>

Get a patch by ID.

Source

pub fn get_node(&self, id: &PatchId) -> Option<&DagNode>

Get a node by ID.

Source

pub fn has_patch(&self, id: &PatchId) -> bool

Check if a patch exists.

Source

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.

Source

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).

Source

pub fn create_branch( &mut self, name: BranchName, target: PatchId, ) -> Result<(), DagError>

Create a new branch pointing to a patch.

Source

pub fn get_branch(&self, name: &BranchName) -> Option<PatchId>

Get the target patch ID of a branch.

Source

pub fn update_branch( &mut self, name: &BranchName, target: PatchId, ) -> Result<(), DagError>

Update a branch to point to a new patch.

Source

pub fn delete_branch(&mut self, name: &BranchName) -> Result<(), DagError>

Delete a branch.

Source

pub fn list_branches(&self) -> Vec<(String, PatchId)>

List all branches.

Source

pub fn patch_count(&self) -> usize

Get the total number of patches in the DAG.

Source

pub fn patch_ids(&self) -> Vec<PatchId>

Get all patch IDs in the DAG.

Source

pub fn patch_chain(&self, id: &PatchId) -> Vec<PatchId>

Get patches from a specific node back to root (inclusive).

Source

pub fn reachable_patches(&self, id: &PatchId) -> Vec<Patch>

Collect all patches reachable from a given patch ID (inclusive).

Source§

impl PatchDag

Source

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§

Source§

impl Default for PatchDag

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more