Module PatchSource

Patch - parsing and applying unified diffs in pure OCaml

Sourcetype hunk = {
  1. mine_start : int;
  2. mine_len : int;
  3. mine : string list;
  4. their_start : int;
  5. their_len : int;
  6. their : string list;
}

A hunk contains some difference between two files: each with a start line and length, and then the content as lists of string.

Sourcetype parse_error = {
  1. msg : string;
  2. lines : string list;
}
Sourceexception Parse_error of parse_error
Sourceval pp_hunk : mine_no_nl:bool -> their_no_nl:bool -> Format.formatter -> hunk -> unit

pp_hunk ppf hunk pretty-prints the hunk on ppf, the printing is in the same format as diff does.

Sourcetype git_ext =
  1. | Rename_only of string * string
  2. | Delete_only
  3. | Create_only
Sourcetype operation =
  1. | Edit of string * string
  2. | Delete of string
  3. | Create of string
  4. | Git_ext of string * string * git_ext
    (*

    The operation of a diff: in-place Edit, Delete, Create. And its git-extensions: Rename_only, Delete_only, Create_only. The parameters to the variants are filenames.

    Note that Edit also renames the given file under certain conditions and the file to use is driven by this POSIX rule: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/patch.html#tag_20_92_13_02

    Note also that the two filenames in Git_ext represent what would be in git --diff <filename1> <filename2> with their respective prefixes removed if parsed with parse ~p:1 or above.

    Warning: The two parameters of Rename_only represent the values of the rename from <filename1> and rename to <filename2> following the specs of the git extensions. Following the behaviour of GNU Patch which ignores these two lines, it is recommended to get the filenames from Git_ext instead of from Rename_only, which are used only for pretty-printing.

    *)
Sourceval pp_operation : Format.formatter -> operation -> unit

pp_operation ppf op pretty-prints the operation op on ppf.

Sourceval operation_eq : operation -> operation -> bool

operation_eq a b is true if a and b are equal.

Sourcetype t = {
  1. operation : operation;
  2. hunks : hunk list;
  3. mine_no_nl : bool;
  4. their_no_nl : bool;
}

The type of a diff: an operation, a list of hunks, and information whether a trailing newline exists on the left and right.

Sourceval pp : Format.formatter -> t -> unit

pp ppf t pretty-prints t on ppf.

Sourceval pp_list : Format.formatter -> t list -> unit

pp ppf diffs pretty-prints diffs on ppf.

Sourceval parse : p:int -> string -> t list

parse ~p data decodes data as a list of diffs.

  • parameter p

    denotes the expected prefix level of the filenames. For more information, see the option -p in your POSIX-complient patch.

  • raises Parse_error

    if a filename was unable to be parsed

Sourceval patch : cleanly:bool -> string option -> t -> string option

patch file_contents diff applies diff on file_contents, resulting in the new file contents (or None if deleted).

Sourceval diff : (string * string) option -> (string * string) option -> t option

diff (filename_a, content_a) (filename_b, content_b) creates a diff between content_a and content_b. Returns None if no changes could be detected.