Raw
1 #ifndef XDIFF_INTERFACE_H
2 #define XDIFF_INTERFACE_H
3
4 #include "hash.h"
5 #include "xdiff/xdiff.h"
6
7 struct object_database;
8
9 /*
10 * Hunk descriptor for externally computed diffs, in xdiff's own
11 * coordinates: line numbers are 1-based and a hunk's start is the
12 * first line it covers. A caller translates any external "empty side"
13 * idiom (such as git diff's start-0/count-0) to a 1-based start before
14 * storing hunks in this struct.
15 */
16 struct xdl_hunk {
17 long old_start, old_count;
18 long new_start, new_count;
19 };
20
21 /*
22 * xdiff isn't equipped to handle content over a gigabyte;
23 * we make the cutoff 1GB - 1MB to give some breathing
24 * room for constant-sized additions (e.g., merge markers)
25 */
26 #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
27
28 /**
29 * The `xdiff_emit_line_fn` function can return 1 to abort early, or 0
30 * to continue processing. Note that doing so is an all-or-nothing
31 * affair, as returning 1 will return all the way to the top-level,
32 * e.g. the xdi_diff_outf() call to generate the diff.
33 *
34 * Thus returning 1 means you won't be getting any more diff lines. If
35 * you need something in-between those two options you'll to use
36 * `xdl_emit_hunk_consume_func_t` and implement your own version of
37 * xdl_emit_diff().
38 *
39 * We may extend the interface in the future to understand other more
40 * granular return values. While you should return 1 to exit early,
41 * doing so will currently make your early return indistinguishable
42 * from an error internal to xdiff, xdiff itself will see that
43 * non-zero return and translate it to -1.
44 *
45 * See "diff_grep" in diffcore-pickaxe.c and "quick_consume" in diff.c
46 * for a trick to work around this, i.e. using the "consume_callback_data"
47 * to note the desired early return.
48 */
49 typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
50 typedef void (*xdiff_emit_hunk_fn)(void *data,
51 long old_begin, long old_nr,
52 long new_begin, long new_nr,
53 const char *func, long funclen);
54
55 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
56 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
57 xdiff_emit_hunk_fn hunk_fn,
58 xdiff_emit_line_fn line_fn,
59 void *consume_callback_data,
60 xpparam_t const *xpp, xdemitconf_t const *xecfg);
61
62 struct range_set;
63 /*
64 * Like xdi_diff_outf(), but forwards only the lines within the given
65 * (post-image) line ranges to line_fn, as "git log -L" scopes its output.
66 * Returns line_fn's latched return value (so a consumer can signal a hit
67 * with a non-zero return), or non-zero on xdiff failure. Defined in
68 * diff.c (it reuses the line-range filter there).
69 */
70 int diff_emit_line_ranges(mmfile_t *mf1, mmfile_t *mf2,
71 const struct range_set *ranges,
72 xdiff_emit_line_fn line_fn, void *cb_data,
73 xpparam_t *xpp, xdemitconf_t *xecfg);
74 int read_mmfile(mmfile_t *ptr, const char *filename);
75 void read_mmblob(mmfile_t *ptr, struct object_database *odb,
76 const struct object_id *oid);
77 int buffer_is_binary(const char *ptr, unsigned long size);
78
79 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
80 void xdiff_clear_find_func(xdemitconf_t *xecfg);
81 struct config_context;
82 int parse_conflict_style_name(const char *value);
83 const char *conflict_style_name(int style);
84 int git_xmerge_config(const char *var, const char *value,
85 const struct config_context *ctx, void *cb);
86 extern int git_xmerge_style;
87
88 /*
89 * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
90 * Returns 1 if the strings are deemed equal, 0 otherwise.
91 * The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
92 * are treated for the comparison.
93 */
94 int xdiff_compare_lines(const char *l1, long s1,
95 const char *l2, long s2, long flags);
96
97 /*
98 * Returns a hash of the string s of length len.
99 * The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
100 * are treated for the hash.
101 */
102 unsigned long xdiff_hash_string(const char *s, size_t len, long flags);
103
104 struct strbuf;
105
106 /*
107 * Append a unified-diff hunk header to `out`, e.g.
108 * "@@ -<old> +<new> @@ func\n". The header comes from wrapping xdiff's
109 * own hunk-header emitter, so it matches what a normal diff would
110 * produce for these begins and counts. For a side with no lines
111 * (count 0) the begin is the line before the change, and a count of 1
112 * is omitted.
113 */
114 void xdiff_emit_hunk_header(struct strbuf *out,
115 long old_begin, long old_count,
116 long new_begin, long new_count,
117 const char *func, long funclen);
118
119 #endif