module documentation

Git merge implementation.

Class Merger Handles git merge operations.
Exception MergeConflict Raised when a merge conflict occurs.
Function make_merge3 Return a Merge3 object, or raise ImportError if merge3 is not installed.
Function merge_blobs Perform three-way merge on blob contents.
Function octopus_merge Perform an octopus merge of multiple commits.
Function recursive_merge Perform a recursive merge with multiple merge bases.
Function three_way_merge Perform a three-way merge between commits.
Function _can_merge_lines Check if lines can be merged without conflict.
Function _create_virtual_commit Create a virtual commit object for recursive merging.
Function _merge3_to_bytes Convert merge3 result to bytes with conflict markers.
Function _merge_lines Merge lines when possible.
def make_merge3(base: Sequence[bytes], a: Sequence[bytes], b: Sequence[bytes], is_cherrypick: bool = False, sequence_matcher: type[SequenceMatcherProtocol[bytes]] | None = None) -> merge3.Merge3[bytes]:

Return a Merge3 object, or raise ImportError if merge3 is not installed.

def merge_blobs(base_blob: Blob | None, ours_blob: Blob | None, theirs_blob: Blob | None, path: bytes | None = None, gitattributes: GitAttributes | None = None, config: Config | None = None) -> tuple[bytes, bool]:

Perform three-way merge on blob contents.

Parameters
base_blob:Blob | NoneCommon ancestor blob (can be None)
ours_blob:Blob | NoneOur version of the blob (can be None)
theirs_blob:Blob | NoneTheir version of the blob (can be None)
path:bytes | NoneOptional path of the file being merged
gitattributes:GitAttributes | NoneOptional GitAttributes object for checking merge drivers
config:Config | NoneOptional Config object for loading merge driver configuration
Returns
tuple[bytes, bool]Tuple of (merged_content, had_conflicts)
def octopus_merge(object_store: BaseObjectStore, merge_bases: list[ObjectID], head_commit: Commit, other_commits: list[Commit], gitattributes: GitAttributes | None = None, config: Config | None = None) -> tuple[Tree, list[bytes]]:

Perform an octopus merge of multiple commits.

The octopus merge strategy merges multiple branches sequentially into a single commit with multiple parents. It refuses to proceed if any merge would result in conflicts that require manual resolution.

Parameters
object_store:BaseObjectStoreObject store to read/write objects
merge_bases:list[ObjectID]List of common ancestor commit IDs for all commits
head_commit:CommitCurrent HEAD commit (ours)
other_commits:list[Commit]List of commits to merge (theirs)
gitattributes:GitAttributes | NoneOptional GitAttributes object for checking merge drivers
config:Config | NoneOptional Config object for loading merge driver configuration
Returns
tuple[Tree, list[bytes]]tuple of (merged_tree, list_of_conflicted_paths) If any conflicts occur during the sequential merges, the function returns early with the conflicts list populated.
Raises
TypeErrorIf any object is not of the expected type
def recursive_merge(object_store: BaseObjectStore, merge_bases: list[ObjectID], ours_commit: Commit, theirs_commit: Commit, gitattributes: GitAttributes | None = None, config: Config | None = None) -> tuple[Tree, list[bytes]]:

Perform a recursive merge with multiple merge bases.

This implements Git's recursive merge strategy, which handles cases where there are multiple common ancestors (criss-cross merges). The algorithm:

  1. If there's 0 or 1 merge base, perform a simple three-way merge
  2. If there are multiple merge bases, merge them recursively to create a virtual merge base, then use that for the final three-way merge
Parameters
object_store:BaseObjectStoreObject store to read/write objects
merge_bases:list[ObjectID]List of merge base commit IDs
ours_commit:CommitOur commit
theirs_commit:CommitTheir commit
gitattributes:GitAttributes | NoneOptional GitAttributes object for checking merge drivers
config:Config | NoneOptional Config object for loading merge driver configuration
Returns
tuple[Tree, list[bytes]]tuple of (merged_tree, list_of_conflicted_paths)
def three_way_merge(object_store: BaseObjectStore, base_commit: Commit | None, ours_commit: Commit, theirs_commit: Commit, gitattributes: GitAttributes | None = None, config: Config | None = None) -> tuple[Tree, list[bytes]]:

Perform a three-way merge between commits.

Parameters
object_store:BaseObjectStoreObject store to read/write objects
base_commit:Commit | NoneCommon ancestor commit (None if no common ancestor)
ours_commit:CommitOur commit
theirs_commit:CommitTheir commit
gitattributes:GitAttributes | NoneOptional GitAttributes object for checking merge drivers
config:Config | NoneOptional Config object for loading merge driver configuration
Returns
tuple[Tree, list[bytes]]tuple of (merged_tree, list_of_conflicted_paths)
def _can_merge_lines(base_lines: Sequence[bytes], a_lines: Sequence[bytes], b_lines: Sequence[bytes]) -> bool:

Check if lines can be merged without conflict.

def _create_virtual_commit(object_store: BaseObjectStore, tree: Tree, parents: list[ObjectID], message: bytes = b'Virtual merge base') -> Commit:

Create a virtual commit object for recursive merging.

Parameters
object_store:BaseObjectStoreObject store to add the commit to
tree:TreeTree object for the commit
parents:list[ObjectID]List of parent commit IDs
message:bytesCommit message
Returns
CommitThe created Commit object
def _merge3_to_bytes(m: merge3.Merge3[bytes]) -> bytes:

Convert merge3 result to bytes with conflict markers.

Parameters
m:merge3.Merge3[bytes]Merge3 object
Returns
bytesMerged content as bytes
def _merge_lines(base_lines: Sequence[bytes], a_lines: Sequence[bytes], b_lines: Sequence[bytes]) -> Sequence[bytes]:

Merge lines when possible.