Package diff3 implements a three-way merge algorithm Original version in Javascript by Bryan Housel @bhousel: https://github.com/bhousel/node-diff3, which in turn is based on project Synchrotron, created by Tony Garnock-Jones. For more detail please visit: http://homepages.kcbbs.gen.nz/tonyg/projects/synchrotron.html https://github.com/tonyg/synchrotron
Ported to go by Javier Peletier @jpeletier
Import the package and call Merge:
Merge(a, o, b io.Reader, detailed bool, labelA string, labelB string) (*MergeResult, error)
Where:
aandbare the current and incoming versions of contentois the common ancestordetailedspecifies whether you want the merge conflicts to be detailed.labelAandlabelBindicate a label to mark conflicts with, usually the branch names.
Returns a MergeResult that includes a flag indicating whether there were conflicts or not and a io.Reader stream with the result.
For callers that already have line or token slices, call the generic API directly:
result := diff3.Diff3Merge(aLines, oLines, bLines, true)The generic type parameter is inferred for common calls. If inference is not possible, specify it explicitly:
result := diff3.Diff3Merge[string](aLines, oLines, bLines, true)Diff3Merge accepts any comparable element type, so callers can merge token slices as well as string lines.
The default algorithm remains the original Hunt-McIlroy LCS implementation.
Use MergeWithOptions or Diff3MergeWithOptions to opt into Myers diff:
result, err := diff3.MergeWithOptions(a, o, b, diff3.MergeOptions{
Algorithm: diff3.DiffAlgorithmMyers,
ExcludeFalseConflicts: true,
Detailed: true,
LabelA: "current",
LabelB: "incoming",
})For slices:
result := diff3.Diff3MergeWithOptions(aLines, oLines, bLines, diff3.MergeOptions{
Algorithm: diff3.DiffAlgorithmMyers,
ExcludeFalseConflicts: true,
})Diff3Merge, Diff3MergeResult, and Conflict are now generic.
Before:
var result []*diff3.Diff3MergeResult
var conflict *diff3.ConflictAfter:
var result []*diff3.Diff3MergeResult[string]
var conflict *diff3.Conflict[string]The fields on Diff3MergeResult and Conflict are now exported and inspectable:
for _, item := range result {
if item.Ok != nil {
// merged non-conflict block
}
if item.Conflict != nil {
_ = item.Conflict.A
_ = item.Conflict.O
_ = item.Conflict.B
_ = item.Conflict.AIndex
_ = item.Conflict.OIndex
_ = item.Conflict.BIndex
}
}Direct calls to Diff3Merge with []string usually continue to compile through type inference. Code that names Diff3MergeResult or Conflict must add the type argument, usually [string].
The generic API, exported merge result fields, Myers diff implementation, and parallel diff computation are based on work by Kyungmin Bae and Devsisters in devsisters/go-diff3.