Merge orchestration (sequencer state, hooks, editors, messages) stays in grit-cli. The library exposes tree and index transforms embedders and porcelain reuse:
RevWalk::merge_basesandRevWalk::is_ancestor— common ancestors and ancestor checks.merge::merge_trees— rename-aware three-way tree merge (merge_trees_three_way), conflict-marker blobs, andTreeMergeOutput.porcelain::merge— fast-forward/octopus index composition,predict_merge(in-memory conflict check via the ODB overlay),write_merge_tree(stored merge tree), and checkout helpers the CLI calls.
When two commits have several merge bases (criss-cross history), predict_merge, write_merge_tree and integrate_branch merge the bases into one virtual base and merge against that, as Git's recursive and ort strategies do. The bases are taken oldest first, in the order RevWalk::merge_bases with MergeBaseMode::All reports them (equal commit dates keep Git's order). Content conflicts between the bases stay in the virtual base as nested conflict markers. Binary, modify/delete and symlink conflicts keep the older version, and the caller's MergeFavor applies only to the final merge. A single merge base is a plain three-way merge. merge_trees_three_way itself always takes one explicit base tree.
Pass explicit MergeFavor and WhitespaceMergeOptions into tree merges; pass identity and timestamps into commit-writing porcelain.
Predicting versus writing a merge
Both functions take two commits and MergePredictionOptions, never touch refs, the index or the working tree, and work in bare repositories.
| Function | Objects written | Returns |
|---|---|---|
predict_merge |
None | Up to date, fast-forward, clean (with the tree id), conflicts, or unrelated |
write_merge_tree |
Every new tree and blob of the merge | WrittenMergeTree: stored tree, conflicts, unmerged stages |
The tree id in MergePrediction::Clean is the id git merge-tree --write-tree prints, but the tree is not stored: committing, diffing or reading it fails with ObjectNotFound. Use predict_merge to answer "would this merge cleanly?" cheaply (merge queues, grit merge --check). Use write_merge_tree when you need the result itself: it is the library form of git merge-tree --write-tree. A clean result can be committed straight away with NewCommit::new(tree, ..).with_parents(vec![ours, theirs]). On conflicts the stored tree holds conflict markers in the conflicted files, and WrittenMergeTree::stages lists the mode, oid, stage and path rows Git prints after the tree id. Set ours_label and theirs_label to choose the conflict-marker labels.
write_merge_tree always merges (the fast-forward option is ignored), merges several merge bases into a virtual base first (stage 1 entries then come from it, as with git merge-tree), and refuses commits with no common ancestor with Error::UnrelatedHistories unless allow_unrelated_histories is set.
Directory renames
When one side moves every file out of a directory and the other side adds a file inside it, the new file most likely belongs in the directory's new home. Tree merges detect this the way Git's ort merge does, controlled by DirectoryRenameMode:
| Mode | merge.directoryRenames |
Result |
|---|---|---|
Conflict (default) |
conflict or unset |
The file moves to the new directory and stays unmerged at the adding side's stage (stage 3 when theirs added it), so the merge stops for confirmation. |
Apply |
true |
The file moves and the merge is clean. |
Off |
false |
The file stays in the old directory. |
A directory counts as renamed only when the side removed it entirely; the destination is the directory that received the most of its files, and a tie is a split that moves nothing. A destination path the adding side already uses, or that several new files would share, blocks the move; a file the base or the renaming side has at the destination is instead three-way merged with the moved file there. Splits and blocked moves are conflicts in Conflict and Apply mode even though no index entry is unmerged. All of them are listed in TreeMergeOutput::directory_rename_conflicts and reported by conflicts_from_tree_merge as the directory_rename_* ConflictKinds.
merge_trees_three_way takes the mode as an argument; read the configured one with DirectoryRenameMode::from_repo, which fails with ConfigError::InvalidDirectoryRenames for an unknown value. MergePredictionOptions::directory_renames and MergeOptions::directory_renames override the config for one merge; pick, revert, rebase and history edits always use the config.
Integrating a branch (integrate_branch)
integrate_branch merges one commit into a local branch and updates the index and work tree (or records merge-in-progress state on conflicts). Pass into_ref as either a full ref (for example refs/heads/main) or a short branch name (for example main or feature/topic); short names map to refs/heads/{name} and missing branches return BranchError::NotFound. The label argument names the other side in the default merge message (Merge {label} unless MergeOptions::message overrides it) and in branch reflog lines (merge {label}, or merge {label}: fast-forward on fast-forward).
Typed merge errors (MergeError)
Branch integration, continue_merge, and replay sequencing return Error::Merge wrapping MergeError. Stable kind_name slugs match the CLI --json error kind field (for example not_fast_forward, unrelated_histories, continue_unmerged_paths, operation_in_progress). Common variants:
| Variant | When |
|---|---|
NotFastForward |
FastForwardPolicy::Only but histories diverged |
UnrelatedHistories |
No merge base and allow_unrelated is false |
ContinueUnmergedPaths |
continue_merge with conflict stages still in the index |
NoMergeInProgress |
Continue/abort/skip with no active merge |
OperationInProgress |
Another paused operation (merge, pick, rebase, …) blocks the call |
These arrive as Error::Merge; repository read, object, and I/O failures arrive as their own area variants of the umbrella Error.
Integrating into detached HEAD (integrate_detached_head)
Merge::integrate with MergeRequest::Detached { into_oid } takes the current detached tip instead of a branch ref. Outcomes, conflict handling, hooks, and MergeOptions match Merge::integrate_into_branch, but only HEAD moves on a fast-forward or merge commit; no refs/heads/* ref is updated. Reflog lines use merge {label} / merge {label}: fast-forward on HEAD only.