xdiff-interface.c 7.35 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

struct xdiff_emit_state {
12 13
	xdiff_emit_hunk_fn hunk_fn;
	xdiff_emit_line_fn line_fn;
14 15 16
	void *consume_callback_data;
	struct strbuf remainder;
};
17

18 19 20 21
static int xdiff_out_hunk(void *priv_,
			  long old_begin, long old_nr,
			  long new_begin, long new_nr,
			  const char *func, long funclen)
22
{
23
	struct xdiff_emit_state *priv = priv_;
24

25 26
	if (priv->remainder.len)
		BUG("xdiff emitted hunk in the middle of a line");
27

28 29 30 31
	priv->hunk_fn(priv->consume_callback_data,
		      old_begin, old_nr, new_begin, new_nr,
		      func, funclen);
	return 0;
32 33
}

34 35 36 37 38 39 40 41
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);
42
		priv->line_fn(priv->consume_callback_data, s, this_size);
43 44 45 46 47
		size -= this_size;
		s += this_size;
	}
}

48
static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
49 50 51 52
{
	struct xdiff_emit_state *priv = priv_;
	int i;

53 54 55
	if (!priv->line_fn)
		return 0;

56 57 58
	for (i = 0; i < nbuf; i++) {
		if (mb[i].ptr[mb[i].size-1] != '\n') {
			/* Incomplete line */
59
			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
60 61 62 63
			continue;
		}

		/* we have a complete line */
64
		if (!priv->remainder.len) {
65 66 67
			consume_one(priv, mb[i].ptr, mb[i].size);
			continue;
		}
68 69 70
		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
		consume_one(priv, priv->remainder.buf, priv->remainder.len);
		strbuf_reset(&priv->remainder);
71
	}
72 73 74
	if (priv->remainder.len) {
		consume_one(priv, priv->remainder.buf, priv->remainder.len);
		strbuf_reset(&priv->remainder);
75 76 77
	}
	return 0;
}
78

79 80
/*
 * Trim down common substring at the end of the buffers,
81
 * but end on a complete line.
82
 */
83
static void trim_common_tail(mmfile_t *a, mmfile_t *b)
84 85
{
	const int blk = 1024;
86
	long trimmed = 0, recovered = 0;
87 88 89 90 91 92 93 94 95 96
	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;
	}

97
	while (recovered < trimmed)
98
		if (ap[recovered++] == '\n')
99 100 101
			break;
	a->size -= trimmed - recovered;
	b->size -= trimmed - recovered;
102 103
}

104 105
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
{
106 107 108
	mmfile_t a = *mf1;
	mmfile_t b = *mf2;

109 110 111
	if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
		return -1;

112 113
	if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
		trim_common_tail(&a, &b);
114 115

	return xdl_diff(&a, &b, xpp, xecfg, xecb);
116 117
}

118 119 120 121 122 123
void discard_hunk_line(void *priv,
		       long ob, long on, long nb, long nn,
		       const char *func, long funclen)
{
}

124
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
125 126 127
		  xdiff_emit_hunk_fn hunk_fn,
		  xdiff_emit_line_fn line_fn,
		  void *consume_callback_data,
128
		  xpparam_t const *xpp, xdemitconf_t const *xecfg)
129 130
{
	int ret;
131
	struct xdiff_emit_state state;
132
	xdemitcb_t ecb;
133 134

	memset(&state, 0, sizeof(state));
135 136
	state.hunk_fn = hunk_fn;
	state.line_fn = line_fn;
137
	state.consume_callback_data = consume_callback_data;
138
	memset(&ecb, 0, sizeof(ecb));
139 140
	if (hunk_fn)
		ecb.out_hunk = xdiff_out_hunk;
141
	ecb.out_line = xdiff_outf;
142
	ecb.priv = &state;
143
	strbuf_init(&state.remainder, 0);
144
	ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
145
	strbuf_release(&state.remainder);
146 147 148
	return ret;
}

149 150 151 152
int read_mmfile(mmfile_t *ptr, const char *filename)
{
	struct stat st;
	FILE *f;
153
	size_t sz;
154 155

	if (stat(filename, &st))
156
		return error_errno("Could not stat %s", filename);
157
	if ((f = fopen(filename, "rb")) == NULL)
158
		return error_errno("Could not open %s", filename);
159
	sz = xsize_t(st.st_size);
160
	ptr->ptr = xmalloc(sz ? sz : 1);
161 162
	if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
		fclose(f);
163
		return error("Could not read %s", filename);
164
	}
165
	fclose(f);
166
	ptr->size = sz;
167 168 169
	return 0;
}

170
void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
171 172 173 174
{
	unsigned long size;
	enum object_type type;

175
	if (oideq(oid, &null_oid)) {
176 177 178 179 180
		ptr->ptr = xstrdup("");
		ptr->size = 0;
		return;
	}

181
	ptr->ptr = read_object_file(oid, &type, &size);
182
	if (!ptr->ptr || type != OBJ_BLOB)
183
		die("unable to read blob object %s", oid_to_hex(oid));
184 185 186
	ptr->size = size;
}

187 188 189 190 191 192 193
#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);
}
194 195 196 197 198 199 200 201 202 203 204 205 206 207

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];
208
	int i;
209
	int result;
210

211 212
	/* Exclude terminating newline (and cr) from matching */
	if (len > 0 && line[len-1] == '\n') {
Junio C Hamano's avatar
Junio C Hamano committed
213 214 215 216 217 218
		if (len > 1 && line[len-2] == '\r')
			len -= 2;
		else
			len--;
	}

219 220
	for (i = 0; i < regs->nr; i++) {
		struct ff_reg *reg = regs->array + i;
221
		if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
222
			if (reg->negate)
223
				return -1;
224
			break;
225 226
		}
	}
227
	if (regs->nr <= i)
228
		return -1;
229 230 231 232 233
	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;
234 235
	while (result > 0 && (isspace(line[result - 1])))
		result--;
236 237 238 239
	memcpy(buffer, line, result);
	return result;
}

240
void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
241 242 243 244 245 246 247 248 249
{
	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++;
250
	ALLOC_ARRAY(regs->array, regs->nr);
251 252 253 254 255 256 257 258 259 260 261 262 263 264
	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;
265
		if (regcomp(&reg->re, expression, cflags))
266
			die("Invalid regexp to look for hunk header: %s", expression);
267
		free(buffer);
268 269 270
		value = ep + 1;
	}
}
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
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;
	}
}

287 288 289 290 291 292 293 294 295 296 297
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);
}

298 299 300 301
int git_xmerge_style = -1;

int git_xmerge_config(const char *var, const char *value, void *cb)
{
302
	if (!strcmp(var, "merge.conflictstyle")) {
303 304 305 306 307 308
		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;
309 310 311 312
		/*
		 * Please update _git_checkout() in
		 * git-completion.bash when you add new merge config
		 */
313 314 315 316 317 318 319
		else
			die("unknown style '%s' given for '%s'",
			    value, var);
		return 0;
	}
	return git_default_config(var, value, cb);
}