diff --git a/src/patch.ml b/src/patch.ml index 63f4baf..221fb0e 100644 --- a/src/patch.ml +++ b/src/patch.ml @@ -261,8 +261,7 @@ let operation_of_strings git mine their = | Some a, Some b -> Edit (a, b) | None, None -> assert false (* ??!?? *) -(* parses a list of lines to a diff.t list *) -let to_diff data = +let parse_one data = (* first locate --- and +++ lines *) let rec find_start git ?hdr = function | [] -> hdr, [] @@ -288,11 +287,11 @@ let to_diff data = let to_lines = String.cuts '\n' -let to_diffs data = +let parse data = let lines = to_lines data in let rec doit acc = function | [] -> List.rev acc - | xs -> match to_diff xs with + | xs -> match parse_one xs with | None -> List.rev acc | Some (diff, rest) -> doit (diff :: acc) rest in diff --git a/src/patch.mli b/src/patch.mli index 24df4ba..7bce9d1 100644 --- a/src/patch.mli +++ b/src/patch.mli @@ -48,8 +48,8 @@ val pp_list : git:bool -> Format.formatter -> t list -> unit (** [pp ~git ppf diffs] pretty-prints [diffs] on [ppf]. If [git] is true, "git diff" style will be printed. *) -val to_diffs : string -> t list -(** [to_diffs data] decodes [data] as a list of diffs. *) +val parse : string -> t list +(** [parse data] decodes [data] as a list of diffs. *) val patch : string option -> t -> string option (** [patch file_contents diff] applies [diff] on [file_contents], resulting in diff --git a/src/patch_command.ml b/src/patch_command.ml index 5a8651a..cdd41a2 100644 --- a/src/patch_command.ml +++ b/src/patch_command.ml @@ -14,7 +14,7 @@ let exit_several_chunks = 3 let exit_patch_failure = 4 let run ~input ~diff = - match Patch.to_diffs diff with + match Patch.parse diff with | [] -> input | _::_::_ -> prerr_endline "Error: The diff contains several chunks,\n\ diff --git a/test/crowbar_test.ml b/test/crowbar_test.ml index 2756f94..01051f2 100644 --- a/test/crowbar_test.ml +++ b/test/crowbar_test.ml @@ -136,7 +136,7 @@ let get_diffs (file1 : file) (file2 : file) : file = let check_Patch file1 file2 = let text_diff = string_of_file (get_diffs file1 file2) in - match Patch.to_diffs text_diff with + match Patch.parse text_diff with | [] -> Crowbar.check_eq (string_of_file file1) (string_of_file file2) | _::_::_ -> Crowbar.fail "not a single diff!" | [diff] -> diff --git a/test/test.ml b/test/test.ml index 80d9479..4884330 100644 --- a/test/test.ml +++ b/test/test.ml @@ -295,7 +295,7 @@ foo ] let basic_parse diff exp () = - let diffs = Patch.to_diffs diff in + let diffs = Patch.parse diff in Alcotest.(check (list test_t) __LOC__ exp diffs) let parse_diffs = @@ -304,7 +304,7 @@ let parse_diffs = (List.combine basic_diffs basic_hunks) let basic_apply file diff exp () = - match Patch.to_diffs diff with + match Patch.parse diff with | [ diff ] -> let res = Patch.patch file diff in Alcotest.(check (option string) __LOC__ exp res) @@ -367,7 +367,7 @@ let multi_files = [ Some "bar" ; Some "baz" ; None ; Some "foobarbaz" ] let multi_exp = [ Some "foobar" ; None ; Some "baz" ; Some "foobar" ] let multi_apply () = - let diffs = Patch.to_diffs multi_diff in + let diffs = Patch.parse multi_diff in Alcotest.(check int __LOC__ (List.length multi_files) (List.length diffs)); Alcotest.(check int __LOC__ (List.length multi_exp) (List.length diffs)); List.iter2 (fun diff (input, expected) -> @@ -416,7 +416,7 @@ let op_test = Alcotest.testable (Patch.pp_operation ~git:false) Patch.operation_ let parse_real_diff_header file hdr () = let data = read (file ^ ".diff") in - let diffs = Patch.to_diffs data in + let diffs = Patch.parse data in Alcotest.(check int __LOC__ 1 (List.length diffs)); Alcotest.check op_test __LOC__ hdr (List.hd diffs).Patch.operation @@ -435,7 +435,7 @@ let regression_test name () = let old = opt_read (name ^ ".old") in let diff = read (name ^ ".diff") in let exp = opt_read (name ^ ".new") in - match Patch.to_diffs diff with + match Patch.parse diff with | [ diff ] -> let res = Patch.patch old diff in Alcotest.(check (option string) __LOC__ exp res)