diff_driver.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#include "common.h"

#include "git2/attr.h"

#include "diff.h"
#include "diff_driver.h"
#include "strmap.h"
#include "map.h"
#include "buf_text.h"
16
#include "config.h"
17
#include "repository.h"
18 19 20

typedef enum {
	DIFF_DRIVER_AUTO = 0,
21 22 23
	DIFF_DRIVER_BINARY = 1,
	DIFF_DRIVER_TEXT = 2,
	DIFF_DRIVER_PATTERNLIST = 3,
24 25
} git_diff_driver_t;

26 27 28 29 30
typedef struct {
	regex_t re;
	int flags;
} git_diff_driver_pattern;

31
enum {
32
	REG_NEGATE = (1 << 15) /* get out of the way of existing flags */
33 34 35 36 37
};

/* data for finding function context for a given file type */
struct git_diff_driver {
	git_diff_driver_t type;
38 39
	uint32_t binary_flags;
	uint32_t other_flags;
40
	git_array_t(git_diff_driver_pattern) fn_patterns;
41
	regex_t  word_pattern;
42
	char name[GIT_FLEX_ARRAY];
43 44
};

45
#include "userdiff.h"
46

47 48 49 50
struct git_diff_driver_registry {
	git_strmap *drivers;
};

51 52
#define FORCE_DIFFABLE (GIT_DIFF_FORCE_TEXT | GIT_DIFF_FORCE_BINARY)

53
static git_diff_driver global_drivers[3] = {
54 55 56
	{ DIFF_DRIVER_AUTO,   0, 0, },
	{ DIFF_DRIVER_BINARY, GIT_DIFF_FORCE_BINARY, 0 },
	{ DIFF_DRIVER_TEXT,   GIT_DIFF_FORCE_TEXT, 0 },
57 58
};

59
git_diff_driver_registry *git_diff_driver_registry_new(void)
60
{
61 62 63 64 65
	git_diff_driver_registry *reg =
		git__calloc(1, sizeof(git_diff_driver_registry));
	if (!reg)
		return NULL;

66
	if (git_strmap_alloc(&reg->drivers) < 0) {
67 68 69 70 71
		git_diff_driver_registry_free(reg);
		return NULL;
	}

	return reg;
72 73 74 75
}

void git_diff_driver_registry_free(git_diff_driver_registry *reg)
{
76 77
	git_diff_driver *drv;

78 79 80
	if (!reg)
		return;

81
	git_strmap_foreach_value(reg->drivers, drv, git_diff_driver_free(drv));
82
	git_strmap_free(reg->drivers);
83 84 85
	git__free(reg);
}

86 87
static int diff_driver_add_patterns(
	git_diff_driver *drv, const char *regex_str, int regex_flags)
88
{
89 90 91 92 93 94 95 96
	int error = 0;
	const char *scan, *end;
	git_diff_driver_pattern *pat = NULL;
	git_buf buf = GIT_BUF_INIT;

	for (scan = regex_str; scan; scan = end) {
		/* get pattern to fill in */
		if ((pat = git_array_alloc(drv->fn_patterns)) == NULL) {
97
			return -1;
98
		}
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		pat->flags = regex_flags;
		if (*scan == '!') {
			pat->flags |= REG_NEGATE;
			++scan;
		}

		if ((end = strchr(scan, '\n')) != NULL) {
			error = git_buf_set(&buf, scan, end - scan);
			end++;
		} else {
			error = git_buf_sets(&buf, scan);
		}
		if (error < 0)
			break;

115
		if ((error = p_regcomp(&pat->re, buf.ptr, regex_flags)) != 0) {
116 117 118
			/*
			 * TODO: issue a warning
			 */
119
		}
120 121
	}

122 123 124
	if (error && pat != NULL)
		(void)git_array_pop(drv->fn_patterns); /* release last item */
	git_buf_free(&buf);
125

126 127
	/* We want to ignore bad patterns, so return success regardless */
	return 0;
128 129 130 131
}

static int diff_driver_xfuncname(const git_config_entry *entry, void *payload)
{
132
	return diff_driver_add_patterns(payload, entry->value, REG_EXTENDED);
133 134 135 136
}

static int diff_driver_funcname(const git_config_entry *entry, void *payload)
{
137
	return diff_driver_add_patterns(payload, entry->value, 0);
138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

static git_diff_driver_registry *git_repository_driver_registry(
	git_repository *repo)
{
	if (!repo->diff_drivers) {
		git_diff_driver_registry *reg = git_diff_driver_registry_new();
		reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);

		if (reg != NULL) /* if we race, free losing allocation */
			git_diff_driver_registry_free(reg);
	}

	if (!repo->diff_drivers)
152
		giterr_set(GITERR_REPOSITORY, "unable to create diff driver registry");
153 154 155 156

	return repo->diff_drivers;
}

157 158 159 160 161
static int diff_driver_alloc(
	git_diff_driver **out, size_t *namelen_out, const char *name)
{
	git_diff_driver *driver;
	size_t driverlen = sizeof(git_diff_driver),
162 163
		namelen = strlen(name),
		alloclen;
164

165 166
	GITERR_CHECK_ALLOC_ADD(&alloclen, driverlen, namelen);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
167

168
	driver = git__calloc(1, alloclen);
169 170 171 172 173 174 175 176 177 178 179 180
	GITERR_CHECK_ALLOC(driver);

	memcpy(driver->name, name, namelen);

	*out = driver;

	if (namelen_out)
		*namelen_out = namelen;

	return 0;
}

181 182 183 184 185 186 187 188
static int git_diff_driver_builtin(
	git_diff_driver **out,
	git_diff_driver_registry *reg,
	const char *driver_name)
{
	int error = 0;
	git_diff_driver_definition *ddef = NULL;
	git_diff_driver *drv = NULL;
189
	size_t idx;
190 191 192 193 194 195 196 197 198 199

	for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
		if (!strcasecmp(driver_name, builtin_defs[idx].name)) {
			ddef = &builtin_defs[idx];
			break;
		}
	}
	if (!ddef)
		goto done;

200 201
	if ((error = diff_driver_alloc(&drv, NULL, ddef->name)) < 0)
		goto done;
202 203 204 205 206 207 208 209 210

	drv->type = DIFF_DRIVER_PATTERNLIST;

	if (ddef->fns &&
		(error = diff_driver_add_patterns(
			drv, ddef->fns, ddef->flags | REG_EXTENDED)) < 0)
		goto done;

	if (ddef->words &&
211
		(error = p_regcomp(
212 213 214 215 216 217
			&drv->word_pattern, ddef->words, ddef->flags | REG_EXTENDED)))
	{
		error = giterr_set_regex(&drv->word_pattern, error);
		goto done;
	}

218
	git_strmap_insert(reg->drivers, drv->name, drv, &error);
219 220
	if (error > 0)
		error = 0;
221 222

done:
223
	if (error && drv)
224
		git_diff_driver_free(drv);
225
	else
226 227 228 229 230
		*out = drv;

	return error;
}

231
static int git_diff_driver_load(
232
	git_diff_driver **out, git_repository *repo, const char *driver_name)
233
{
234
	int error = 0;
235
	git_diff_driver_registry *reg;
236
	git_diff_driver *drv = NULL;
237
	size_t namelen;
238
	khiter_t pos;
239
	git_config *cfg = NULL;
240
	git_buf name = GIT_BUF_INIT;
241
	git_config_entry *ce = NULL;
242
	bool found_driver = false;
243

244
	if ((reg = git_repository_driver_registry(repo)) == NULL)
245
		return -1;
246 247 248 249 250

	pos = git_strmap_lookup_index(reg->drivers, driver_name);
	if (git_strmap_valid_index(reg->drivers, pos)) {
		*out = git_strmap_value_at(reg->drivers, pos);
		return 0;
251 252
	}

253 254 255
	if ((error = diff_driver_alloc(&drv, &namelen, driver_name)) < 0)
		goto done;

256 257
	drv->type = DIFF_DRIVER_AUTO;

258
	/* if you can't read config for repo, just use default driver */
259
	if (git_repository_config_snapshot(&cfg, repo) < 0) {
260
		giterr_clear();
261
		goto done;
262 263 264
	}

	if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
265
		goto done;
266 267 268

	switch (git_config__get_bool_force(cfg, name.ptr, -1)) {
	case true:
269 270
		/* if diff.<driver>.binary is true, just return the binary driver */
		*out = &global_drivers[DIFF_DRIVER_BINARY];
271
		goto done;
272
	case false:
273 274 275
		/* if diff.<driver>.binary is false, force binary checks off */
		/* but still may have custom function context patterns, etc. */
		drv->binary_flags = GIT_DIFF_FORCE_TEXT;
276
		found_driver = true;
277 278
		break;
	default:
279
		/* diff.<driver>.binary unspecified or "auto", so just continue */
280
		break;
281 282 283 284
	}

	/* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */

285 286
	git_buf_truncate(&name, namelen + strlen("diff.."));
	git_buf_put(&name, "xfuncname", strlen("xfuncname"));
287
	if ((error = git_config_get_multivar_foreach(
288 289
			cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) {
		if (error != GIT_ENOTFOUND)
290
			goto done;
291
		giterr_clear(); /* no diff.<driver>.xfuncname, so just continue */
292
	}
293

294 295
	git_buf_truncate(&name, namelen + strlen("diff.."));
	git_buf_put(&name, "funcname", strlen("funcname"));
296
	if ((error = git_config_get_multivar_foreach(
297 298
			cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) {
		if (error != GIT_ENOTFOUND)
299
			goto done;
300
		giterr_clear(); /* no diff.<driver>.funcname, so just continue */
301 302 303
	}

	/* if we found any patterns, set driver type to use correct callback */
304
	if (git_array_size(drv->fn_patterns) > 0) {
305
		drv->type = DIFF_DRIVER_PATTERNLIST;
306 307
		found_driver = true;
	}
308

309 310
	git_buf_truncate(&name, namelen + strlen("diff.."));
	git_buf_put(&name, "wordregex", strlen("wordregex"));
311
	if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0)
312
		goto done;
313 314
	if (!ce || !ce->value)
		/* no diff.<driver>.wordregex, so just continue */;
315
	else if (!(error = p_regcomp(&drv->word_pattern, ce->value, REG_EXTENDED)))
316
		found_driver = true;
317 318 319 320
	else {
		/* TODO: warn about bad regex instead of failure */
		error = giterr_set_regex(&drv->word_pattern, error);
		goto done;
321 322 323 324 325 326
	}

	/* TODO: look up diff.<driver>.algorithm to turn on minimal / patience
	 * diff in drv->other_flags
	 */

327
	/* if no driver config found at all, fall back on AUTO driver */
328
	if (!found_driver)
329
		goto done;
330 331

	/* store driver in registry */
332
	git_strmap_insert(reg->drivers, drv->name, drv, &error);
333
	if (error < 0)
334
		goto done;
335
	error = 0;
336

337 338
	*out = drv;

339
done:
340
	git_config_entry_free(ce);
341
	git_buf_free(&name);
342
	git_config_free(cfg);
343

344 345 346 347 348
	if (!*out) {
		int error2 = git_diff_driver_builtin(out, reg, driver_name);
		if (!error)
			error = error2;
	}
349 350 351 352

	if (drv && drv != *out)
		git_diff_driver_free(drv);

353
	return error;
354 355
}

356 357 358
int git_diff_driver_lookup(
	git_diff_driver **out, git_repository *repo, const char *path)
{
359
	int error = 0;
360 361 362
	const char *value;

	assert(out);
363
	*out = NULL;
364 365

	if (!repo || !path || !strlen(path))
366 367 368 369
		/* just use the auto value */;
	else if ((error = git_attr_get(&value, repo, 0, path, "diff")) < 0)
		/* return error below */;
	else if (GIT_ATTR_UNSPECIFIED(value))
370 371
		/* just use the auto value */;
	else if (GIT_ATTR_FALSE(value))
372
		*out = &global_drivers[DIFF_DRIVER_BINARY];
373
	else if (GIT_ATTR_TRUE(value))
374
		*out = &global_drivers[DIFF_DRIVER_TEXT];
375 376

	/* otherwise look for driver information in config and build driver */
377
	else if ((error = git_diff_driver_load(out, repo, value)) < 0) {
378 379
		if (error == GIT_ENOTFOUND) {
			error = 0;
380
			giterr_clear();
381
		}
382
	}
383

384 385 386
	if (!*out)
		*out = &global_drivers[DIFF_DRIVER_AUTO];

387
	return error;
388 389 390 391
}

void git_diff_driver_free(git_diff_driver *driver)
{
392 393 394 395 396
	size_t i;

	if (!driver)
		return;

397
	for (i = 0; i < git_array_size(driver->fn_patterns); ++i)
398
		regfree(& git_array_get(driver->fn_patterns, i)->re);
399 400 401 402 403
	git_array_clear(driver->fn_patterns);

	regfree(&driver->word_pattern);

	git__free(driver);
404 405
}

406 407
void git_diff_driver_update_options(
	uint32_t *option_flags, git_diff_driver *driver)
408
{
409 410 411 412
	if ((*option_flags & FORCE_DIFFABLE) == 0)
		*option_flags |= driver->binary_flags;

	*option_flags |= driver->other_flags;
413 414 415 416 417
}

int git_diff_driver_content_is_binary(
	git_diff_driver *driver, const char *content, size_t content_len)
{
418
	git_buf search = GIT_BUF_INIT;
419 420 421

	GIT_UNUSED(driver);

422 423 424
	git_buf_attach_notowned(&search, content,
		min(content_len, GIT_FILTER_BYTES_TO_CHECK_NUL));

425 426 427 428 429 430 431 432 433 434 435 436
	/* TODO: provide encoding / binary detection callbacks that can
	 * be UTF-8 aware, etc.  For now, instead of trying to be smart,
	 * let's just use the simple NUL-byte detection that core git uses.
	 */

	/* previously was: if (git_buf_text_is_binary(&search)) */
	if (git_buf_text_contains_nul(&search))
		return 1;

	return 0;
}

437
static int diff_context_line__simple(
438
	git_diff_driver *driver, git_buf *line)
439
{
440
	char firstch = line->ptr[0];
441
	GIT_UNUSED(driver);
442
	return (git__isalpha(firstch) || firstch == '_' || firstch == '$');
443 444 445
}

static int diff_context_line__pattern_match(
446
	git_diff_driver *driver, git_buf *line)
447
{
448
	size_t i, maxi = git_array_size(driver->fn_patterns);
449
	regmatch_t pmatch[2];
450

451
	for (i = 0; i < maxi; ++i) {
452 453 454 455 456
		git_diff_driver_pattern *pat = git_array_get(driver->fn_patterns, i);

		if (!regexec(&pat->re, line->ptr, 2, pmatch, 0)) {
			if (pat->flags & REG_NEGATE)
				return false;
457 458 459 460 461

			/* use pmatch data to trim line data */
			i = (pmatch[1].rm_so >= 0) ? 1 : 0;
			git_buf_consume(line, git_buf_cstr(line) + pmatch[i].rm_so);
			git_buf_truncate(line, pmatch[i].rm_eo - pmatch[i].rm_so);
462
			git_buf_rtrim(line);
463

464
			return true;
465
		}
466 467 468 469 470
	}

	return false;
}

471 472 473 474 475 476 477
static long diff_context_find(
	const char *line,
	long line_len,
	char *out,
	long out_size,
	void *payload)
{
478
	git_diff_find_context_payload *ctxt = payload;
479

480
	if (git_buf_set(&ctxt->line, line, (size_t)line_len) < 0)
481
		return -1;
482
	git_buf_rtrim(&ctxt->line);
483

484
	if (!ctxt->line.size)
485 486
		return -1;

487
	if (!ctxt->match_line || !ctxt->match_line(ctxt->driver, &ctxt->line))
488
		return -1;
489

490
	if (out_size > (long)ctxt->line.size)
Russell Belfer committed
491
		out_size = (long)ctxt->line.size;
492
	memcpy(out, ctxt->line.ptr, (size_t)out_size);
493

494
	return out_size;
495
}
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510
void git_diff_find_context_init(
	git_diff_find_context_fn *findfn_out,
	git_diff_find_context_payload *payload_out,
	git_diff_driver *driver)
{
	*findfn_out = driver ? diff_context_find : NULL;

	memset(payload_out, 0, sizeof(*payload_out));
	if (driver) {
		payload_out->driver = driver;
		payload_out->match_line = (driver->type == DIFF_DRIVER_PATTERNLIST) ?
			diff_context_line__pattern_match : diff_context_line__simple;
		git_buf_init(&payload_out->line, 0);
	}
511 512
}

513
void git_diff_find_context_clear(git_diff_find_context_payload *payload)
514
{
515 516 517 518
	if (payload) {
		git_buf_free(&payload->line);
		payload->driver = NULL;
	}
519
}