(* Copyright (c) 2026 Thomas Gazagnaire Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) (** Interop: {!Git.Diff.patch} against git's patches ([git diff-tree -p]) of the trees of the merge corpus. Traces generated by: the git named in [traces/git-version]. Regenerate: [REGEN=1 dune build @traces]. For each case of the merge corpus ([../merge/traces]) git wrote the patch of its base tree into ours and into theirs, with [--full-index], under [--diff-algorithm=myers] (git diff's default) and [--diff-algorithm=histogram], the indent heuristic on as [diff.indentHeuristic] leaves it. The library's patch of the same trees, under the same algorithm and with object names in full, must be git's byte for byte. *) let trace path = Filename.concat "traces" path let corpus path = Filename.concat "../merge/traces" path let read_file path = In_channel.with_open_bin path (fun ic -> In_channel.input_all ic) let write_file path data = Out_channel.with_open_bin path (fun oc -> Out_channel.output_string oc data) type case = { name : string; base : Git.Hash.t; ours : Git.Hash.t; theirs : Git.Hash.t; } (* The cases of the merge corpus: their names and trees. *) let cases = let s = Csv.Field.string in let codec = Csv.( Row.( obj (fun name _ _ base ours theirs _ _ _ _ _ _ _ _ -> { name; base = Git.Hash.of_hex base; ours = Git.Hash.of_hex ours; theirs = Git.Hash.of_hex theirs; }) |> col "name" s ~enc:(fun c -> c.name) |> col "kind" s ~enc:(fun _ -> "") |> col "config" s ~enc:(fun _ -> "") |> col "base" s ~enc:(fun c -> Git.Hash.to_hex c.base) |> col "ours" s ~enc:(fun c -> Git.Hash.to_hex c.ours) |> col "theirs" s ~enc:(fun c -> Git.Hash.to_hex c.theirs) |> col "git_tree" s ~enc:(fun _ -> "") |> col "git_clean" s ~enc:(fun _ -> "") |> col "git_conflicts" s ~enc:(fun _ -> "") |> col "git_types" s ~enc:(fun _ -> "") |> col "myers_tree" s ~enc:(fun _ -> "") |> col "myers_conflicts" s ~enc:(fun _ -> "") |> col "no_renames_tree" s ~enc:(fun _ -> "") |> col "no_renames_conflicts" s ~enc:(fun _ -> "") |> finish)) in match Csv.of_file codec (corpus "cases.csv") with | Ok rows -> rows | Error e -> Fmt.failwith "cases.csv: %a" Csv.Error.pp e (* The records of a patches file: "\t\t\t\t\n" then the patch, keyed by the two trees, which name a patch where two cases can share a name. *) let records file = let data = read_file (trace file) in let rec go acc i = if i >= String.length data then List.rev acc else let j = String.index_from data i '\n' in match String.split_on_char '\t' (String.sub data i (j - i)) with | [ _name; _side; old_tree; new_tree; n ] -> let n = int_of_string n in go (((old_tree, new_tree), String.sub data (j + 1) n) :: acc) (j + 1 + n) | _ -> Fmt.failwith "%s: a record header with no five fields" file in go [] 0 let myers = records "myers.patches" let histogram = records "histogram.patches" (* Each case's patches of base into ours and into theirs, in a repository holding the corpus. *) let patches ~algorithm = Test_helpers.with_temp_repo @@ fun sw fs dir -> ignore (Git_eio.Repository.init ~sw ~fs dir); let pack_dir = Fpath.(to_string (dir / ".git" / "objects" / "pack")) in if not (Sys.file_exists pack_dir) then Sys.mkdir pack_dir 0o755; write_file (Filename.concat pack_dir "pack-corpus.pack") (read_file (corpus "corpus.pack")); write_file (Filename.concat pack_dir "pack-corpus.idx") (read_file (corpus "corpus.idx")); let repo = Git_eio.Repository.open_repo ~sw ~fs dir in let read h = match Git_eio.Repository.read repo h with | Ok (Git.Value.Blob b) -> Git.Blob.to_string b | _ -> Fmt.failwith "%a is no blob" Git.Hash.pp h in List.concat_map (fun c -> List.map (fun tree -> match Git_eio.Diff.trees repo ~old_tree:c.base ~new_tree:tree with | Ok changes -> ( (Git.Hash.to_hex c.base, Git.Hash.to_hex tree), Git.Diff.patch ~algorithm ~abbrev:Git.Hash.to_hex ~read changes ) | Error (`Msg m) -> Fmt.failwith "%s: %s" c.name m) [ c.ours; c.theirs ]) cases (* The patches that differ from git's, by their trees. *) let check ~algorithm ~expected () = let ours = patches ~algorithm in let differ = List.filter_map (fun (key, patch) -> match List.assoc_opt key ours with | Some p when String.equal p patch -> None | _ -> Some (fst key ^ ".." ^ snd key)) expected in Alcotest.(check int) "patches" (List.length ours) (List.length expected); Alcotest.(check (list string)) "patches other than git's" [] differ let () = Alcotest.run "diff interop" [ ( "patches", [ Alcotest.test_case "myers" `Quick (check ~algorithm:Git.Xdiff.Myers ~expected:myers); Alcotest.test_case "histogram" `Quick (check ~algorithm:Git.Xdiff.Histogram ~expected: (List.map (fun (key, patch) -> ( key, Option.value ~default:patch (List.assoc_opt key histogram) )) myers)); ] ); ]