In a three-way merge, read each conflict directly instead of parsing markers.
dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff; // Arguments: base, ours, theirs. var merge = Diff.Merge("on\n","off\n","auto\n"); foreach (var c in merge.Conflicts) Console.Write($"Line {c.BaseLine}\n" + $"Base: {c.BaseText}" + $"Ours: {c.OursText}" + $"Theirs: {c.TheirsText}");
Unzip the example, then dotnet run.
Prints:
Line 1 Base: on Ours: off Theirs: auto
merge.Conflicts is empty when the two edits merge without a disagreement. The program reads the conflicts without parsing merge.Text, which still contains markers by default when there is an unresolved conflict.
Variations
Use the edited versions’ line numbers. Add braces around the loop body and insert this line inside it to get the starting lines for a side-by-side view. All three line numbers are one-based.
Console.WriteLine($"Ours: {c.OursLine}, theirs: {c.TheirsLine}");Produce marker-free text by explicitly taking ours. Replace the merge call with this version. merge.Text is now off\n, but merge.Conflicts still contains the disagreement for review.
var merge = Diff.Merge("on\n", "off\n", "auto\n", new OneDiff.Merge.MergeSettings { Resolution = OneDiff.Merge.ConflictResolution.Ours });
Related recipes
The short answer
Iterate Diff.Merge(baseText, ours, theirs).Conflicts to get MergeConflict objects. Each gives you the three pieces of text with their line endings, a starting line for each version, and a Region with the underlying ranges. You can use these fields in a review tool even when you never display the merged text with markers.
How do you build a three-way merge view without parsing markers?
For a list of conflicts, loop over Conflicts in the order returned and show the base, ours, and theirs as separate fields. Use the corresponding line number for each field rather than using BaseLine to locate all three. Insertions or deletions earlier in an edited file can shift its later lines relative to the base. Keep the original line endings if you will use a selection to reconstruct text; trim them only for a display label.
Use Regions instead if the view also needs the ranges between conflicts, including the parts the library could merge without review. The structured result describes how the regions cover all three inputs. In a conflict split into several pieces, the first conflicting piece carries the base range, so don’t assume that each displayed piece has a separate span of original text.
When do three-way merge conflicts occur?
The merge compares each edit against the same base, so a change on just one side does not ordinarily need a decision. Identical changes to the same range are taken once. In the example, both sides replace the same value in different ways, so the result has one conflict. Even edits whose ranges only touch can conflict in this merge. Check the result rather than guessing from the sets of changed line numbers.
ConflictCount counts unresolved conflicts. If your application supplies a resolution policy, it can drop to zero even when the Conflicts list is nonempty. Use the count to decide whether output is still marked for review, and use the list if you need to report what was disputed even after automatic resolution.