xdiff-interface.c 7.3 KB
Newer Older
1
#include "cache.h"
2
#include "config.h"
3
#include "object-store.h"
4
#include "xdiff-interface.h"
5 6 7 8
#include "xdiff/xtypes.h"
#include "xdiff/xdiffi.h"
#include "xdiff/xemit.h"
#include "xdiff/xmacros.h"
9
#include "xdiff/xutils.h"
10 11 12 13 14 15

struct xdiff_emit_state {
	xdiff_emit_consume_fn consume;
	void *consume_callback_data;
	struct strbuf remainder;
};
16

17
static int parse_num(char **cp_p, int *num_p)
18 19
{
	char *cp = *cp_p;
20
	int num = 0;
21 22 23

	while ('0' <= *cp && *cp <= '9')
		num = num * 10 + *cp++ - '0';
24
	if (!(cp - *cp_p))
25 26 27 28 29 30 31
		return -1;
	*cp_p = cp;
	*num_p = num;
	return 0;
}

int parse_hunk_header(char *line, int len,
32 33
		      int *ob, int *on,
		      int *nb, int *nn)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	char *cp;
	cp = line + 4;
	if (parse_num(&cp, ob)) {
	bad_line:
		return error("malformed diff output: %s", line);
	}
	if (*cp == ',') {
		cp++;
		if (parse_num(&cp, on))
			goto bad_line;
	}
	else
		*on = 1;
	if (*cp++ != ' ' || *cp++ != '+')
		goto bad_line;
	if (parse_num(&cp, nb))
		goto bad_line;
	if (*cp == ',') {
		cp++;
		if (parse_num(&cp, nn))
			goto bad_line;
	}
	else
		*nn = 1;
	return -!!memcmp(cp, " @@", 3);
}

62 63 64 65 66 67 68 69
static void consume_one(void *priv_, char *s, unsigned long size)
{
	struct xdiff_emit_state *priv = priv_;
	char *ep;
	while (size) {
		unsigned long this_size;
		ep = memchr(s, '\n', size);
		this_size = (ep == NULL) ? size : (ep - s + 1);
70
		priv->consume(priv->consume_callback_data, s, this_size);
71 72 73 74 75
		size -= this_size;
		s += this_size;
	}
}

76
static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
77 78 79 80 81 82 83
{
	struct xdiff_emit_state *priv = priv_;
	int i;

	for (i = 0; i < nbuf; i++) {
		if (mb[i].ptr[mb[i].size-1] != '\n') {
			/* Incomplete line */
84
			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
85 86 87 88
			continue;
		}

		/* we have a complete line */
89
		if (!priv->remainder.len) {
90 91 92
			consume_one(priv, mb[i].ptr, mb[i].size);
			continue;
		}
93 94 95
		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
		consume_one(priv, priv->remainder.buf, priv->remainder.len);
		strbuf_reset(&priv->remainder);
96
	}
97 98 99
	if (priv->remainder.len) {
		consume_one(priv, priv->remainder.buf, priv->remainder.len);
		strbuf_reset(&priv->remainder);
100 101 102
	}
	return 0;
}
103

104 105
/*
 * Trim down common substring at the end of the buffers,
106
 * but end on a complete line.
107
 */
108
static void trim_common_tail(mmfile_t *a, mmfile_t *b)
109 110
{
	const int blk = 1024;
111
	long trimmed = 0, recovered = 0;
112 113 114 115 116 117 118 119 120 121
	char *ap = a->ptr + a->size;
	char *bp = b->ptr + b->size;
	long smaller = (a->size < b->size) ? a->size : b->size;

	while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
		trimmed += blk;
		ap -= blk;
		bp -= blk;
	}

122
	while (recovered < trimmed)
123
		if (ap[recovered++] == '\n')
124 125 126
			break;
	a->size -= trimmed - recovered;
	b->size -= trimmed - recovered;
127 128
}

129 130
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
{
131 132 133
	mmfile_t a = *mf1;
	mmfile_t b = *mf2;

134 135 136
	if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
		return -1;

137 138
	if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
		trim_common_tail(&a, &b);
139 140

	return xdl_diff(&a, &b, xpp, xecfg, xecb);
141 142
}

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)
146 147
{
	int ret;
148
	struct xdiff_emit_state state;
149
	xdemitcb_t ecb;
150 151 152 153

	memset(&state, 0, sizeof(state));
	state.consume = fn;
	state.consume_callback_data = consume_callback_data;
154 155 156
	memset(&ecb, 0, sizeof(ecb));
	ecb.outf = xdiff_outf;
	ecb.priv = &state;
157
	strbuf_init(&state.remainder, 0);
158
	ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
159
	strbuf_release(&state.remainder);
160 161 162
	return ret;
}

163 164 165 166
int read_mmfile(mmfile_t *ptr, const char *filename)
{
	struct stat st;
	FILE *f;
167
	size_t sz;
168 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 176
	if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
		fclose(f);
177
		return error("Could not read %s", filename);
178
	}
179
	fclose(f);
180
	ptr->size = sz;
181 182 183
	return 0;
}

184
void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
185 186 187 188
{
	unsigned long size;
	enum object_type type;

189
	if (!oidcmp(oid, &null_oid)) {
190 191 192 193 194
		ptr->ptr = xstrdup("");
		ptr->size = 0;
		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 199 200
	ptr->size = size;
}

201 202 203 204 205 206 207
#define FIRST_FEW_BYTES 8000
int buffer_is_binary(const char *ptr, unsigned long size)
{
	if (FIRST_FEW_BYTES < size)
		size = FIRST_FEW_BYTES;
	return !!memchr(ptr, 0, size);
}
208 209 210 211 212 213 214 215 216 217 218 219 220 221

struct ff_regs {
	int nr;
	struct ff_reg {
		regex_t re;
		int negate;
	} *array;
};

static long ff_regexp(const char *line, long len,
		char *buffer, long buffer_size, void *priv)
{
	struct ff_regs *regs = priv;
	regmatch_t pmatch[2];
222
	int i;
223
	int result;
224

225 226
	/* Exclude terminating newline (and cr) from matching */
	if (len > 0 && line[len-1] == '\n') {
Junio C Hamano's avatar
Junio C Hamano committed
227 228 229 230 231 232
		if (len > 1 && line[len-2] == '\r')
			len -= 2;
		else
			len--;
	}

233 234
	for (i = 0; i < regs->nr; i++) {
		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;
239 240
		}
	}
241
	if (regs->nr <= i)
242
		return -1;
243 244 245 246 247
	i = pmatch[1].rm_so >= 0 ? 1 : 0;
	line += pmatch[i].rm_so;
	result = pmatch[i].rm_eo - pmatch[i].rm_so;
	if (result > buffer_size)
		result = buffer_size;
248 249
	while (result > 0 && (isspace(line[result - 1])))
		result--;
250 251 252 253
	memcpy(buffer, line, result);
	return result;
}

254
void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
255 256 257 258 259 260 261 262 263
{
	int i;
	struct ff_regs *regs;

	xecfg->find_func = ff_regexp;
	regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
	for (i = 0, regs->nr = 1; value[i]; i++)
		if (value[i] == '\n')
			regs->nr++;
264
	ALLOC_ARRAY(regs->array, regs->nr);
265 266 267 268 269 270 271 272 273 274 275 276 277 278
	for (i = 0; i < regs->nr; i++) {
		struct ff_reg *reg = regs->array + i;
		const char *ep = strchr(value, '\n'), *expression;
		char *buffer = NULL;

		reg->negate = (*value == '!');
		if (reg->negate && i == regs->nr - 1)
			die("Last expression must not be negated: %s", value);
		if (*value == '!')
			value++;
		if (ep)
			expression = buffer = xstrndup(value, ep - value);
		else
			expression = value;
279
		if (regcomp(&reg->re, expression, cflags))
280
			die("Invalid regexp to look for hunk header: %s", expression);
281
		free(buffer);
282 283 284
		value = ep + 1;
	}
}
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
void xdiff_clear_find_func(xdemitconf_t *xecfg)
{
	if (xecfg->find_func) {
		int i;
		struct ff_regs *regs = xecfg->find_func_priv;

		for (i = 0; i < regs->nr; i++)
			regfree(&regs->array[i].re);
		free(regs->array);
		free(regs);
		xecfg->find_func = NULL;
		xecfg->find_func_priv = NULL;
	}
}

301 302 303 304 305 306 307 308 309 310 311
unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
{
	return xdl_hash_record(&s, s + len, flags);
}

int xdiff_compare_lines(const char *l1, long s1,
			const char *l2, long s2, long flags)
{
	return xdl_recmatch(l1, s1, l2, s2, flags);
}

312 313 314 315
int git_xmerge_style = -1;

int git_xmerge_config(const char *var, const char *value, void *cb)
{
316
	if (!strcmp(var, "merge.conflictstyle")) {
317 318 319 320 321 322 323 324 325 326 327 328 329
		if (!value)
			die("'%s' is not a boolean", var);
		if (!strcmp(value, "diff3"))
			git_xmerge_style = XDL_MERGE_DIFF3;
		else if (!strcmp(value, "merge"))
			git_xmerge_style = 0;
		else
			die("unknown style '%s' given for '%s'",
			    value, var);
		return 0;
	}
	return git_default_config(var, value, cb);
}