View raw

1 (** Line-oriented diffs between two blobs. 2 3 Computes a Myers diff — the algorithm [git diff] uses by default — and 4 groups the result into hunks with the surrounding context. Cost scales 5 with the number of differences rather than with file size, so a small 6 change in a large file is cheap. Two files that differ in more than a 7 thousand lines fall back to a coarse wholesale-replacement diff. 8 9 The module is named for the granularity it works at: whole lines, matched 10 as opaque units, with no word- or character-level refinement. 11 12 It produces data only. Rendering it is {!module:Ui.Diff}'s job. *) 13 14 (** {1 Line-level results} *) 15 16 type line_kind = 17 | Context 18 | Addition 19 | Deletion (** Whether a line is unchanged, added, or removed. *) 20 21 type line = { 22 kind : line_kind; 23 old_number : int option; 24 new_number : int option; 25 text : string; 26 } 27 (** A single diff line with its position in the old and new files. *) 28 29 (** {1 Hunks} *) 30 31 type hunk = { 32 old_start : int; 33 old_count : int; 34 new_start : int; 35 new_count : int; 36 lines : line list; 37 } 38 (** A contiguous group of changes with surrounding context. *) 39 40 (** {1 Files} *) 41 42 type file = { 43 path : string; 44 old_hash : string option; 45 new_hash : string option; 46 old_mode : int option; 47 new_mode : int option; 48 binary : bool; 49 hunks : hunk list; 50 } 51 (** A single file's worth of changes, including metadata. *) 52 53 (** {1 Computing diffs} *) 54 55 val of_contents : string -> string -> line list 56 (** [of_contents old_content new_content] produces a flat list of diff lines 57 between two file contents, computed with Myers' O(ND) algorithm. The 58 common prefix and suffix are matched directly; when the remainder differs 59 in more than 1024 lines, it is listed as all deletions followed by all 60 additions rather than spending unbounded time on the search. *) 61 62 val hunks : ?context:int -> line list -> hunk list 63 (** Group diff lines into hunks with [context] lines of surrounding unchanged 64 content (default 3). *) 65