A three-way merge compares both edited lists with the same original, so it can tell a one-sided edit from a disagreement.
dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff; using OneDiff.Merge; int[] basis = [10, 20, 30]; int[] ours = [11, 20, 31]; int[] theirs = [12, 20, 30]; var merge = Diff.Merge<int>(basis, ours, theirs); foreach (var r in merge.Regions) if (r.Kind == MergeRegionKind.Conflict) Console.WriteLine($"Conflict at {r.Base.Start}");
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
Conflict at 0The full program
using OneDiff; using OneDiff.Merge; List<int> basis = [10, 20, 30]; List<int> ours = [11, 20, 31]; List<int> theirs = [12, 20, 30]; var merge = Diff.Merge<int>( basis.ToArray(), ours.ToArray(), theirs.ToArray()); foreach (var r in merge.Regions) if (r.Kind == MergeRegionKind.Conflict) Console.WriteLine($"Conflict at {r.Base.Start}: " + $"{basis[r.Base.Start]} / " + $"{ours[r.Ours.Start]} / " + $"{theirs[r.Theirs.Start]}"); Console.WriteLine($"Open: {merge.ConflictCount}"); Console.WriteLine($"Merged items: {merge.Items is not null}");
dotnet run prints:
Conflict at 0: 10 / 11 / 12 Open: 1 Merged items: False
Diff.Merge<T> takes three ReadOnlySpan<T> inputs, so the program converts each List<int> to an array that can be passed as a span. The program prints the three values in base, ours, theirs order. It reads the first item in each conflicting range here because all three ranges are nonempty in this example. For other inputs, check Length before indexing: a range can be empty, for instance when one side deletes an item. With the default settings, Items is null while these conflicts are unresolved.
Variations
Take our side of every conflict while keeping one-sided edits from either input. Add this after the full program:
var resolved = merge.Resolve(ConflictResolution.Ours); Console.WriteLine(string.Join(", ", resolved!));
11, 20, 31Related recipes
The short answer
Use Diff.Merge<T> with the original list and both edited versions in that order. It compares typed items directly and returns a set of regions. Each conflict region gives you a range in all three inputs so you can show what the two editors changed. When there are no open conflicts, read Items for the merged sequence.
What is a three-way merge?
A two-list comparison tells you the lists differ, but not who edited which items. Start with [10, 20, 30]. If your edit changes 30 to 31 and the other copy still has 30, the base tells the merge that retaining 30 was not a competing edit. Neither concatenating the copies nor removing duplicates gives you that information. Three-way merge compares each version to the base before it combines the edits.
Order matters: this is a merge of sequences, not a join on a database key. By default it uses the type’s default equality comparer. If you supply a comparer, it decides which items count as equal. Comparing records only by ID, for instance, can make an edit to another field look like no change. For a merge that should notice those edits, include the fields that matter in equality.
What does a merge conflict mean for lists?
In the example, both edits replace the first item, but one replaces it with 11 and the other with 12. That’s a conflict. The output reports index 0 in the base. For inserts or deletes, the indices can differ between the inputs: use r.Base with the original, r.Ours with your version and r.Theirs with the other version. Each range starts at a zero-based index and has a length, which can be zero. Don’t use all three starts as unconditional subscripts.
The merge works with runs of changes, not separate decisions for every object. The merge rules explain why neighboring edits can land in one conflict even if they don’t replace the same individual item.
How do I fix merge conflicts in a typed list?
If one branch should win in every disagreement, use the Resolve call in Variations. It chooses that branch only for conflicts, not for the whole list. If the decision varies by conflict, use the three ranges to show the competing sequences and choose what to append for each region. Keep the regions in order, including those marked unchanged. That way, what you keep on either side is still a T, not a line of text you have to parse back into an item.