diff.c: pass sign_index to emit_line_ws_markup
[git.git] / xdiff-interface.c
blobe7af95db8654a88359b9abd31bc018fb1cb66b5f
1 #include "cache.h"
2 #include "config.h"
3 #include "object-store.h"
4 #include "xdiff-interface.h"
5 #include "xdiff/xtypes.h"
6 #include "xdiff/xdiffi.h"
7 #include "xdiff/xemit.h"
8 #include "xdiff/xmacros.h"
9 #include "xdiff/xutils.h"
11 struct xdiff_emit_state {
12 xdiff_emit_consume_fn consume;
13 void *consume_callback_data;
14 struct strbuf remainder;
17 static int parse_num(char **cp_p, int *num_p)
19 char *cp = *cp_p;
20 int num = 0;
22 while ('0' <= *cp && *cp <= '9')
23 num = num * 10 + *cp++ - '0';
24 if (!(cp - *cp_p))
25 return -1;
26 *cp_p = cp;
27 *num_p = num;
28 return 0;
31 int parse_hunk_header(char *line, int len,
32 int *ob, int *on,
33 int *nb, int *nn)
35 char *cp;
36 cp = line + 4;
37 if (parse_num(&cp, ob)) {
38 bad_line:
39 return error("malformed diff output: %s", line);
41 if (*cp == ',') {
42 cp++;
43 if (parse_num(&cp, on))
44 goto bad_line;
46 else
47 *on = 1;
48 if (*cp++ != ' ' || *cp++ != '+')
49 goto bad_line;
50 if (parse_num(&cp, nb))
51 goto bad_line;
52 if (*cp == ',') {
53 cp++;
54 if (parse_num(&cp, nn))
55 goto bad_line;
57 else
58 *nn = 1;
59 return -!!memcmp(cp, " @@", 3);
62 static void consume_one(void *priv_, char *s, unsigned long size)
64 struct xdiff_emit_state *priv = priv_;
65 char *ep;
66 while (size) {
67 unsigned long this_size;
68 ep = memchr(s, '\n', size);
69 this_size = (ep == NULL) ? size : (ep - s + 1);
70 priv->consume(priv->consume_callback_data, s, this_size);
71 size -= this_size;
72 s += this_size;
76 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
78 struct xdiff_emit_state *priv = priv_;
79 int i;
81 for (i = 0; i < nbuf; i++) {
82 if (mb[i].ptr[mb[i].size-1] != '\n') {
83 /* Incomplete line */
84 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
85 continue;
88 /* we have a complete line */
89 if (!priv->remainder.len) {
90 consume_one(priv, mb[i].ptr, mb[i].size);
91 continue;
93 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
94 consume_one(priv, priv->remainder.buf, priv->remainder.len);
95 strbuf_reset(&priv->remainder);
97 if (priv->remainder.len) {
98 consume_one(priv, priv->remainder.buf, priv->remainder.len);
99 strbuf_reset(&priv->remainder);
101 return 0;
105 * Trim down common substring at the end of the buffers,
106 * but end on a complete line.
108 static void trim_common_tail(mmfile_t *a, mmfile_t *b)
110 const int blk = 1024;
111 long trimmed = 0, recovered = 0;
112 char *ap = a->ptr + a->size;
113 char *bp = b->ptr + b->size;
114 long smaller = (a->size < b->size) ? a->size : b->size;
116 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
117 trimmed += blk;
118 ap -= blk;
119 bp -= blk;
122 while (recovered < trimmed)
123 if (ap[recovered++] == '\n')
124 break;
125 a->size -= trimmed - recovered;
126 b->size -= trimmed - recovered;
129 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
131 mmfile_t a = *mf1;
132 mmfile_t b = *mf2;
134 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
135 return -1;
137 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
138 trim_common_tail(&a, &b);
140 return xdl_diff(&a, &b, xpp, xecfg, xecb);
143 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
144 xdiff_emit_consume_fn fn, void *consume_callback_data,
145 xpparam_t const *xpp, xdemitconf_t const *xecfg)
147 int ret;
148 struct xdiff_emit_state state;
149 xdemitcb_t ecb;
151 memset(&state, 0, sizeof(state));
152 state.consume = fn;
153 state.consume_callback_data = consume_callback_data;
154 memset(&ecb, 0, sizeof(ecb));
155 ecb.outf = xdiff_outf;
156 ecb.priv = &state;
157 strbuf_init(&state.remainder, 0);
158 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
159 strbuf_release(&state.remainder);
160 return ret;
163 int read_mmfile(mmfile_t *ptr, const char *filename)
165 struct stat st;
166 FILE *f;
167 size_t sz;
169 if (stat(filename, &st))
170 return error_errno("Could not stat %s", filename);
171 if ((f = fopen(filename, "rb")) == NULL)
172 return error_errno("Could not open %s", filename);
173 sz = xsize_t(st.st_size);
174 ptr->ptr = xmalloc(sz ? sz : 1);
175 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
176 fclose(f);
177 return error("Could not read %s", filename);
179 fclose(f);
180 ptr->size = sz;
181 return 0;
184 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
186 unsigned long size;
187 enum object_type type;
189 if (oideq(oid, &null_oid)) {
190 ptr->ptr = xstrdup("");
191 ptr->size = 0;
192 return;
195 ptr->ptr = read_object_file(oid, &type, &size);
196 if (!ptr->ptr || type != OBJ_BLOB)
197 die("unable to read blob object %s", oid_to_hex(oid));
198 ptr->size = size;
201 #define FIRST_FEW_BYTES 8000
202 int buffer_is_binary(const char *ptr, unsigned long size)
204 if (FIRST_FEW_BYTES < size)
205 size = FIRST_FEW_BYTES;
206 return !!memchr(ptr, 0, size);
209 struct ff_regs {
210 int nr;
211 struct ff_reg {
212 regex_t re;
213 int negate;
214 } *array;
217 static long ff_regexp(const char *line, long len,
218 char *buffer, long buffer_size, void *priv)
220 struct ff_regs *regs = priv;
221 regmatch_t pmatch[2];
222 int i;
223 int result;
225 /* Exclude terminating newline (and cr) from matching */
226 if (len > 0 && line[len-1] == '\n') {
227 if (len > 1 && line[len-2] == '\r')
228 len -= 2;
229 else
230 len--;
233 for (i = 0; i < regs->nr; i++) {
234 struct ff_reg *reg = regs->array + i;
235 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
236 if (reg->negate)
237 return -1;
238 break;
241 if (regs->nr <= i)
242 return -1;
243 i = pmatch[1].rm_so >= 0 ? 1 : 0;
244 line += pmatch[i].rm_so;
245 result = pmatch[i].rm_eo - pmatch[i].rm_so;
246 if (result > buffer_size)
247 result = buffer_size;
248 while (result > 0 && (isspace(line[result - 1])))
249 result--;
250 memcpy(buffer, line, result);
251 return result;
254 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
256 int i;
257 struct ff_regs *regs;
259 xecfg->find_func = ff_regexp;
260 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
261 for (i = 0, regs->nr = 1; value[i]; i++)
262 if (value[i] == '\n')
263 regs->nr++;
264 ALLOC_ARRAY(regs->array, regs->nr);
265 for (i = 0; i < regs->nr; i++) {
266 struct ff_reg *reg = regs->array + i;
267 const char *ep = strchr(value, '\n'), *expression;
268 char *buffer = NULL;
270 reg->negate = (*value == '!');
271 if (reg->negate && i == regs->nr - 1)
272 die("Last expression must not be negated: %s", value);
273 if (*value == '!')
274 value++;
275 if (ep)
276 expression = buffer = xstrndup(value, ep - value);
277 else
278 expression = value;
279 if (regcomp(&reg->re, expression, cflags))
280 die("Invalid regexp to look for hunk header: %s", expression);
281 free(buffer);
282 value = ep + 1;
286 void xdiff_clear_find_func(xdemitconf_t *xecfg)
288 if (xecfg->find_func) {
289 int i;
290 struct ff_regs *regs = xecfg->find_func_priv;
292 for (i = 0; i < regs->nr; i++)
293 regfree(&regs->array[i].re);
294 free(regs->array);
295 free(regs);
296 xecfg->find_func = NULL;
297 xecfg->find_func_priv = NULL;
301 unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
303 return xdl_hash_record(&s, s + len, flags);
306 int xdiff_compare_lines(const char *l1, long s1,
307 const char *l2, long s2, long flags)
309 return xdl_recmatch(l1, s1, l2, s2, flags);
312 int git_xmerge_style = -1;
314 int git_xmerge_config(const char *var, const char *value, void *cb)
316 if (!strcmp(var, "merge.conflictstyle")) {
317 if (!value)
318 die("'%s' is not a boolean", var);
319 if (!strcmp(value, "diff3"))
320 git_xmerge_style = XDL_MERGE_DIFF3;
321 else if (!strcmp(value, "merge"))
322 git_xmerge_style = 0;
323 else
324 die("unknown style '%s' given for '%s'",
325 value, var);
326 return 0;
328 return git_default_config(var, value, cb);