ilusm.dev

dif3

Three-way merge for VCS-style conflict resolution - given a base text, an ours branch, and a theirs branch, produce a merged result. Conflict regions are surrounded by standard <<<<<<< ours / ======= / >>>>>>> theirs markers. Returns merged lines, merged text, a conflict list, and an ok flag.

Load with: use dif3

What this module does

dif3 implements a classic three-way text merge. It first diffs base→ours and base→theirs using the diff module, then reconciles the two change sets line by line. Where only one side changed, the change is accepted automatically. Where both sides changed the same line differently, a conflict block is emitted with the standard diff3 conflict markers.

Quick example

use dif3

base   = "line 1\nline 2\nline 3"
ours   = "line 1\nOUR CHANGE\nline 3"
theirs = "line 1\nline 2\nTHEIR CHANGE"

result = dif3m(base, ours, theirs)
prn(result.text)
# line 1
# OUR CHANGE         ← auto-accepted (theirs unchanged)
# THEIR CHANGE       ← auto-accepted (ours unchanged)

prn(result.ok)   # tru (no conflicts)

# With a real conflict
conflict_result = dif3m(base, "X changed line 2", "Y changed line 2")
prn(conflict_result.ok)    # fls
prn(conflict_result.text)  # contains <<<<<<< / ======= / >>>>>>>

# Convenience helpers
merged_str = dif3s(base, ours, theirs)
is_clean   = dif3ok(base, ours, theirs)

Functions

Merging

dif3m(base, ours, theirs) / dif3.mrg(b, o, t)

Performs a three-way merge of the three text strings. Returns {lines, text, conf, ok} where lines is the merged line list, text is the joined string, conf is the list of conflict records (each {bi, ours, theirs}), and ok is tru if there are no conflicts.

dif3s(base, ours, theirs) / dif3.str(b, o, t)

Returns just the merged text string (shorthand for dif3m(…).text).

dif3ok(base, ours, theirs) / dif3.ok(b, o, t)

Returns tru if the three-way merge has no conflicts.

Notes

  • Conflict markers: <<<<<<< ours, =======, >>>>>>> theirs.
  • Requires trl, txt, and diff.