2 #include "xdiff-interface.h"
3 #include "xdiff/xtypes.h"
4 #include "xdiff/xdiffi.h"
5 #include "xdiff/xemit.h"
6 #include "xdiff/xmacros.h"
8 struct xdiff_emit_state {
9 xdiff_emit_consume_fn consume;
10 void *consume_callback_data;
11 struct strbuf remainder;
14 static int parse_num(char **cp_p, int *num_p)
20 while ('0' <= *cp && *cp <= '9')
21 num = num * 10 + *cp++ - '0';
22 if (!(read_some = cp - *cp_p))
29 int parse_hunk_header(char *line, int len,
35 if (parse_num(&cp, ob)) {
37 return error("malformed diff output: %s", line);
41 if (parse_num(&cp, on))
46 if (*cp++ != ' ' || *cp++ != '+')
48 if (parse_num(&cp, nb))
52 if (parse_num(&cp, nn))
57 return -!!memcmp(cp, " @@", 3);
60 static void consume_one(void *priv_, char *s, unsigned long size)
62 struct xdiff_emit_state *priv = priv_;
65 unsigned long this_size;
66 ep = memchr(s, '\n', size);
67 this_size = (ep == NULL) ? size : (ep - s + 1);
68 priv->consume(priv->consume_callback_data, s, this_size);
74 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
76 struct xdiff_emit_state *priv = priv_;
79 for (i = 0; i < nbuf; i++) {
80 if (mb[i].ptr[mb[i].size-1] != '\n') {
82 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
86 /* we have a complete line */
87 if (!priv->remainder.len) {
88 consume_one(priv, mb[i].ptr, mb[i].size);
91 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
92 consume_one(priv, priv->remainder.buf, priv->remainder.len);
93 strbuf_reset(&priv->remainder);
95 if (priv->remainder.len) {
96 consume_one(priv, priv->remainder.buf, priv->remainder.len);
97 strbuf_reset(&priv->remainder);
103 * Trim down common substring at the end of the buffers,
104 * but leave at least ctx lines at the end.
106 static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
108 const int blk = 1024;
109 long trimmed = 0, recovered = 0;
110 char *ap = a->ptr + a->size;
111 char *bp = b->ptr + b->size;
112 long smaller = (a->size < b->size) ? a->size : b->size;
117 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
123 while (recovered < trimmed)
124 if (ap[recovered++] == '\n')
126 a->size -= trimmed - recovered;
127 b->size -= trimmed - recovered;
130 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
135 trim_common_tail(&a, &b, xecfg->ctxlen);
137 return xdl_diff(&a, &b, xpp, xecfg, xecb);
140 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
141 xdiff_emit_consume_fn fn, void *consume_callback_data,
142 xpparam_t const *xpp,
143 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
146 struct xdiff_emit_state state;
148 memset(&state, 0, sizeof(state));
150 state.consume_callback_data = consume_callback_data;
151 xecb->outf = xdiff_outf;
153 strbuf_init(&state.remainder, 0);
154 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
155 strbuf_release(&state.remainder);
159 struct xdiff_emit_hunk_state {
160 xdiff_emit_hunk_consume_fn consume;
161 void *consume_callback_data;
164 static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
165 xdemitconf_t const *xecfg)
167 long s1, s2, same, p_next, t_next;
168 xdchange_t *xch, *xche;
169 struct xdiff_emit_hunk_state *state = ecb->priv;
170 xdiff_emit_hunk_consume_fn fn = state->consume;
171 void *consume_callback_data = state->consume_callback_data;
173 for (xch = xscr; xch; xch = xche->next) {
174 xche = xdl_get_hunk(xch, xecfg);
176 s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
177 s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
178 same = s2 + XDL_MAX(xch->i1 - s1, 0);
179 p_next = xche->i1 + xche->chg1;
180 t_next = xche->i2 + xche->chg2;
182 fn(consume_callback_data, same, p_next, t_next);
187 int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
188 xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
189 xpparam_t const *xpp, xdemitconf_t *xecfg)
191 struct xdiff_emit_hunk_state state;
194 memset(&state, 0, sizeof(state));
195 memset(&ecb, 0, sizeof(ecb));
197 state.consume_callback_data = consume_callback_data;
198 xecfg->emit_func = (void (*)())process_diff;
200 return xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
203 int read_mmfile(mmfile_t *ptr, const char *filename)
209 if (stat(filename, &st))
210 return error("Could not stat %s", filename);
211 if ((f = fopen(filename, "rb")) == NULL)
212 return error("Could not open %s", filename);
213 sz = xsize_t(st.st_size);
214 ptr->ptr = xmalloc(sz ? sz : 1);
215 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
216 return error("Could not read %s", filename);
222 #define FIRST_FEW_BYTES 8000
223 int buffer_is_binary(const char *ptr, unsigned long size)
225 if (FIRST_FEW_BYTES < size)
226 size = FIRST_FEW_BYTES;
227 return !!memchr(ptr, 0, size);
238 static long ff_regexp(const char *line, long len,
239 char *buffer, long buffer_size, void *priv)
242 struct ff_regs *regs = priv;
243 regmatch_t pmatch[2];
247 /* Exclude terminating newline (and cr) from matching */
248 if (len > 0 && line[len-1] == '\n') {
249 if (len > 1 && line[len-2] == '\r')
255 line_buffer = xstrndup(line, len); /* make NUL terminated */
257 for (i = 0; i < regs->nr; i++) {
258 struct ff_reg *reg = regs->array + i;
259 if (!regexec(®->re, line_buffer, 2, pmatch, 0)) {
267 i = pmatch[1].rm_so >= 0 ? 1 : 0;
268 line += pmatch[i].rm_so;
269 result = pmatch[i].rm_eo - pmatch[i].rm_so;
270 if (result > buffer_size)
271 result = buffer_size;
273 while (result > 0 && (isspace(line[result - 1])))
275 memcpy(buffer, line, result);
281 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
284 struct ff_regs *regs;
286 xecfg->find_func = ff_regexp;
287 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
288 for (i = 0, regs->nr = 1; value[i]; i++)
289 if (value[i] == '\n')
291 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
292 for (i = 0; i < regs->nr; i++) {
293 struct ff_reg *reg = regs->array + i;
294 const char *ep = strchr(value, '\n'), *expression;
297 reg->negate = (*value == '!');
298 if (reg->negate && i == regs->nr - 1)
299 die("Last expression must not be negated: %s", value);
303 expression = buffer = xstrndup(value, ep - value);
306 if (regcomp(®->re, expression, cflags))
307 die("Invalid regexp to look for hunk header: %s", expression);
313 int git_xmerge_style = -1;
315 int git_xmerge_config(const char *var, const char *value, void *cb)
317 if (!strcasecmp(var, "merge.conflictstyle")) {
319 die("'%s' is not a boolean", var);
320 if (!strcmp(value, "diff3"))
321 git_xmerge_style = XDL_MERGE_DIFF3;
322 else if (!strcmp(value, "merge"))
323 git_xmerge_style = 0;
325 die("unknown style '%s' given for '%s'",
329 return git_default_config(var, value, cb);