2 #include "xdiff-interface.h"
5 struct xdiff_emit_state {
6 xdiff_emit_consume_fn consume;
7 void *consume_callback_data;
8 struct strbuf remainder;
11 static int parse_num(char **cp_p, int *num_p)
17 while ('0' <= *cp && *cp <= '9')
18 num = num * 10 + *cp++ - '0';
19 if (!(read_some = cp - *cp_p))
26 int parse_hunk_header(char *line, int len,
32 if (parse_num(&cp, ob)) {
34 return error("malformed diff output: %s", line);
38 if (parse_num(&cp, on))
43 if (*cp++ != ' ' || *cp++ != '+')
45 if (parse_num(&cp, nb))
49 if (parse_num(&cp, nn))
54 return -!!memcmp(cp, " @@", 3);
57 static void consume_one(void *priv_, char *s, unsigned long size)
59 struct xdiff_emit_state *priv = priv_;
62 unsigned long this_size;
63 ep = memchr(s, '\n', size);
64 this_size = (ep == NULL) ? size : (ep - s + 1);
65 priv->consume(priv->consume_callback_data, s, this_size);
71 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
73 struct xdiff_emit_state *priv = priv_;
76 for (i = 0; i < nbuf; i++) {
77 if (mb[i].ptr[mb[i].size-1] != '\n') {
79 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
83 /* we have a complete line */
84 if (!priv->remainder.len) {
85 consume_one(priv, mb[i].ptr, mb[i].size);
88 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
89 consume_one(priv, priv->remainder.buf, priv->remainder.len);
90 strbuf_reset(&priv->remainder);
92 if (priv->remainder.len) {
93 consume_one(priv, priv->remainder.buf, priv->remainder.len);
94 strbuf_reset(&priv->remainder);
100 * Trim down common substring at the end of the buffers,
101 * but leave at least ctx lines at the end.
103 static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
105 const int blk = 1024;
106 long trimmed = 0, recovered = 0;
107 char *ap = a->ptr + a->size;
108 char *bp = b->ptr + b->size;
109 long smaller = (a->size < b->size) ? a->size : b->size;
114 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
120 while (recovered < trimmed)
121 if (ap[recovered++] == '\n')
123 a->size -= trimmed - recovered;
124 b->size -= trimmed - recovered;
127 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
132 trim_common_tail(&a, &b, xecfg->ctxlen);
134 return xdl_diff(&a, &b, xpp, xecfg, xecb);
137 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
138 xdiff_emit_consume_fn fn, void *consume_callback_data,
139 xpparam_t const *xpp,
140 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
143 struct xdiff_emit_state state;
145 memset(&state, 0, sizeof(state));
147 state.consume_callback_data = consume_callback_data;
148 xecb->outf = xdiff_outf;
150 strbuf_init(&state.remainder, 0);
151 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
152 strbuf_release(&state.remainder);
156 int read_mmfile(mmfile_t *ptr, const char *filename)
162 if (stat(filename, &st))
163 return error("Could not stat %s", filename);
164 if ((f = fopen(filename, "rb")) == NULL)
165 return error("Could not open %s", filename);
166 sz = xsize_t(st.st_size);
167 ptr->ptr = xmalloc(sz ? sz : 1);
168 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
169 return error("Could not read %s", filename);
175 #define FIRST_FEW_BYTES 8000
176 int buffer_is_binary(const char *ptr, unsigned long size)
178 if (FIRST_FEW_BYTES < size)
179 size = FIRST_FEW_BYTES;
180 return !!memchr(ptr, 0, size);
191 static long ff_regexp(const char *line, long len,
192 char *buffer, long buffer_size, void *priv)
195 struct ff_regs *regs = priv;
196 regmatch_t pmatch[2];
200 /* Exclude terminating newline (and cr) from matching */
201 if (len > 0 && line[len-1] == '\n') {
202 if (len > 1 && line[len-2] == '\r')
208 line_buffer = xstrndup(line, len); /* make NUL terminated */
210 /* Exclude terminating newline (and cr) from matching */
211 if (len > 0 && line[len-1] == '\n') {
212 if (len > 1 && line[len-2] == '\r')
218 line_buffer = xstrndup(line, len); /* make NUL terminated */
220 for (i = 0; i < regs->nr; i++) {
221 struct ff_reg *reg = regs->array + i;
222 if (!regexec(®->re, line_buffer, 2, pmatch, 0)) {
230 i = pmatch[1].rm_so >= 0 ? 1 : 0;
231 line += pmatch[i].rm_so;
232 result = pmatch[i].rm_eo - pmatch[i].rm_so;
233 if (result > buffer_size)
234 result = buffer_size;
236 while (result > 0 && (isspace(line[result - 1])))
238 memcpy(buffer, line, result);
244 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
247 struct ff_regs *regs;
249 xecfg->find_func = ff_regexp;
250 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
251 for (i = 0, regs->nr = 1; value[i]; i++)
252 if (value[i] == '\n')
254 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
255 for (i = 0; i < regs->nr; i++) {
256 struct ff_reg *reg = regs->array + i;
257 const char *ep = strchr(value, '\n'), *expression;
260 reg->negate = (*value == '!');
261 if (reg->negate && i == regs->nr - 1)
262 die("Last expression must not be negated: %s", value);
266 expression = buffer = xstrndup(value, ep - value);
269 if (regcomp(®->re, expression, cflags))
270 die("Invalid regexp to look for hunk header: %s", expression);
276 int git_xmerge_style = -1;
278 int git_xmerge_config(const char *var, const char *value, void *cb)
280 if (!strcasecmp(var, "merge.conflictstyle")) {
282 die("'%s' is not a boolean", var);
283 if (!strcmp(value, "diff3"))
284 git_xmerge_style = XDL_MERGE_DIFF3;
285 else if (!strcmp(value, "merge"))
286 git_xmerge_style = 0;
288 die("unknown style '%s' given for '%s'",
292 return git_default_config(var, value, cb);