Compute and present the opam file differences as a unified diff #7

Merged
reynir merged 3 commits from opam-diff-as-diff into main 2025-01-08 14:16:05 +00:00
2 changed files with 16 additions and 39 deletions
Showing only changes of commit 420d049ff0 - Show all commits

View file

@ -172,18 +172,6 @@ type opam_diff = {
diff : string ; diff : string ;
} }
let commands_to_strings (l, r) =
let v a =
OpamPrinter.FullPos.value (OpamPp.print OpamFormat.V.command a)
in
List.map v l, List.map v r
let opt_url_to_string (l, r) =
let url_to_s = function
| None -> "" | Some u -> OpamFile.URL.write_to_string u
in
url_to_s l, url_to_s r
let pp_opam_diff ppf { pkg ; effectively_equal ; _ } = let pp_opam_diff ppf { pkg ; effectively_equal ; _ } =
Format.fprintf ppf "%a%s" Format.fprintf ppf "%a%s"
pp_opampackage pkg pp_opampackage pkg
@ -268,7 +256,7 @@ let compare left right =
in in
(opam_diff, version_diff, left_pkgs, right_pkgs, duniverse_ret) (opam_diff, version_diff, left_pkgs, right_pkgs, duniverse_ret)
let compare_to_json let compare_to_json
(opam_diff, version_diff, left_pkgs, right_pkgs, duniverse_diff) : Yojson.Basic.t = (opam_diff, version_diff, left_pkgs, right_pkgs, duniverse_diff) : Yojson.Basic.t =
let version_diff_to_json lst = let version_diff_to_json lst =
`List (List.map (fun { name; version_left; version_right } -> `List (List.map (fun { name; version_left; version_right } ->
@ -276,17 +264,15 @@ let compare left right =
("name", `String (OpamPackage.Name.to_string name)); ("name", `String (OpamPackage.Name.to_string name));
("version_left", `String (OpamPackage.Version.to_string version_left)); ("version_left", `String (OpamPackage.Version.to_string version_left));
("version_right", `String (OpamPackage.Version.to_string version_right)) ("version_right", `String (OpamPackage.Version.to_string version_right))
] ]) lst)
) lst)
in in
let package_set_to_json set = let package_set_to_json set =
`List (Set.fold (fun p acc -> `List (Set.fold (fun p acc ->
let json = `Assoc [ let json = `Assoc [
("name", `String (OpamPackage.Name.to_string p.OpamPackage.name)); ("name", `String (OpamPackage.Name.to_string p.OpamPackage.name));
("version", `String (OpamPackage.Version.to_string p.OpamPackage.version)) ("version", `String (OpamPackage.Version.to_string p.OpamPackage.version))
] in ] in
json :: acc json :: acc) set [])
) set [])
in in
let opam_diff_to_json opam_diff = let opam_diff_to_json opam_diff =
`List (List.map (fun (diff : opam_diff) -> `List (List.map (fun (diff : opam_diff) ->
@ -294,23 +280,20 @@ let compare left right =
("package_version", `String (OpamPackage.to_string diff.pkg)); ("package_version", `String (OpamPackage.to_string diff.pkg));
("effectively_equal", `Bool diff.effectively_equal); ("effectively_equal", `Bool diff.effectively_equal);
("diff", `String diff.diff); ("diff", `String diff.diff);
] ]) opam_diff)
) opam_diff)
in in
let duniverse_to_json = function let duniverse_to_json = function
| Ok (left, right, detailed_diff) -> | Ok (left, right, detailed_diff) ->
`Assoc [ `Assoc [
("left", `List (List.map (fun (k, v) -> `Assoc [("name", `String k); ("value", `String v)]) left)); ("left", `List (List.map (fun (k, v) -> `Assoc [("name", `String k); ("value", `String v)]) left));
("right", `List (List.map (fun (k, v) -> `Assoc [("name", `String k); ("value", `String v)]) right)); ("right", `List (List.map (fun (k, v) -> `Assoc [("name", `String k); ("value", `String v)]) right));
("detailed_diff",`List (List.map (fun (diff : duniverse_diff) -> ("detailed_diff",`List (List.map (fun (diff : duniverse_diff) ->
`Assoc [ `Assoc [
("name", `String diff.name); ("name", `String diff.name);
]) detailed_diff)) ]) detailed_diff))
] ]
| Error (`Msg msg) ->
| Error (`Msg msg) -> `String msg
`String msg
in in
`Assoc [ `Assoc [
("opam_diff", opam_diff_to_json opam_diff); ("opam_diff", opam_diff_to_json opam_diff);

View file

@ -26,15 +26,9 @@ val pp_duniverse_dir : Format.formatter -> string * string -> unit
val pp_opam_diff : Format.formatter -> opam_diff -> unit val pp_opam_diff : Format.formatter -> opam_diff -> unit
val commands_to_strings : OpamTypes.command list * OpamTypes.command list -> string list * string list
val opt_url_to_string : OpamFile.URL.t option * OpamFile.URL.t option -> string * string
val compare : OpamFile.SwitchExport.t -> val compare : OpamFile.SwitchExport.t ->
OpamFile.SwitchExport.t -> OpamFile.SwitchExport.t ->
opam_diff list * version_diff list * OpamPackage.Set.t * OpamPackage.Set.t * ((string * string) list * (string * string) list * duniverse_diff list, [> `Msg of string ]) result opam_diff list * version_diff list * OpamPackage.Set.t * OpamPackage.Set.t * ((string * string) list * (string * string) list * duniverse_diff list, [> `Msg of string ]) result
val compare_to_json : opam_diff list * version_diff list * OpamPackage.Set.t * OpamPackage.Set.t * val compare_to_json : opam_diff list * version_diff list * OpamPackage.Set.t * OpamPackage.Set.t *
((string * string) list * (string * string) list * duniverse_diff list, [< `Msg of string ]) result -> Yojson.Basic.t ((string * string) list * (string * string) list * duniverse_diff list, [< `Msg of string ]) result -> Yojson.Basic.t