Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/patch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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, []
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/patch.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/patch_command.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down
2 changes: 1 addition & 1 deletion test/crowbar_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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] ->
Expand Down
10 changes: 5 additions & 5 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down