- OCaml 78%
- Python 19%
- Raku 2%
- Shell 1%
merge3 #
Myers diff and diff3 three-way merge for OCaml.
The diff is Myers' O(ND) algorithm. N is the number of lines in the two files together and D is the length of the shortest edit script, that is the number of lines inserted or deleted. When two files are almost the same, D is small and the diff runs in close to linear time.
On top of the diff sits the diff3 three-way merge, which prints
conflicts with the same markers as git. For values that are not text, the
library has merge combinators in the style of Irmin (pair, option,
alist), which build a merge function for a structured type out of merges on
its parts.
Installation #
Install with opam:
$ opam install merge3
If opam cannot find the package, it may not yet be released in the
public opam-repository. Add the overlay repository, then install
it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install merge3
Usage #
Line-level diff #
Merge3.diff takes two arrays of lines and returns a shortest edit script
from the first to the second:
# let a = [| "one"; "two"; "three" |]
and b = [| "one"; "TWO"; "three" |] in
Merge3.diff ~eq:String.equal a b
- : string Merge3.edit list =
[Merge3.Keep "one"; Merge3.Delete "two"; Merge3.Insert "TWO";
Merge3.Keep "three"]
Similarity #
Merge3.similarity scores two strings from 0 to 1 by the bytes their
matching blocks share, the Ratcliff/Obershelp measure. It gives exactly
the value of Python's difflib.SequenceMatcher(None, a, b).ratio(), autojunk
heuristic included, so scores computed by either agree:
# Merge3.similarity "abcd" "bcde"
- : float = 0.75
Three-way merge #
Merge3.merge runs diff3 over two versions and their common ancestor. A
region that merges cleanly comes back as Resolved. If both sides changed
the same lines in different ways, the region is a Conflict, and
Merge3.to_string prints it between git-style markers:
# let base = "a\nb\nc\n"
and ours = "a\nB1\nc\n"
and theirs = "a\nB2\nc\n" in
print_string (Merge3.to_string (Merge3.merge ~base ~ours ~theirs ()))
a
<<<<<<< ours
B1
=======
B2
>>>>>>> theirs
c
- : unit = ()
To check whether a merge went through cleanly without printing it, use
Merge3.has_conflicts:
# let chunks =
Merge3.merge ~base:"a\nb\nc\n"
~ours:"a\nb\nc\n"
~theirs:"a\nb\nC\n" () in
Merge3.has_conflicts chunks
- : bool = false
Merge combinators #
Here a merge on pairs is built from a merge on integers and a merge on optional integers:
# let merge_int = Merge3.default ~eq:Int.equal in
let merge_pair = Merge3.pair merge_int (Merge3.option merge_int) in
merge_pair ~old:(Some (1, None)) (1, Some 2) (1, Some 2)
- : (int * int option) Merge3.result = Merge3.Ok (1, Some 2)
Licence #
ISC