pathspec.c 16.8 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "pathspec.h"

10
#include "git2/pathspec.h"
11
#include "git2/diff.h"
12
#include "attr_file.h"
13 14 15
#include "iterator.h"
#include "repository.h"
#include "index.h"
16 17
#include "bitvec.h"
#include "diff.h"
18
#include "wildmatch.h"
19 20 21 22

/* what is the common non-wildcard prefix for all items in the pathspec */
char *git_pathspec_prefix(const git_strarray *pathspec)
{
23
	git_str prefix = GIT_STR_INIT;
24 25 26
	const char *scan;

	if (!pathspec || !pathspec->count ||
27
		git_str_common_prefix(&prefix, pathspec->strings, pathspec->count) < 0)
28 29 30 31 32 33 34 35
		return NULL;

	/* diff prefix will only be leading non-wildcards */
	for (scan = prefix.ptr; *scan; ++scan) {
		if (git__iswildcard(*scan) &&
			(scan == prefix.ptr || (*(scan - 1) != '\\')))
			break;
	}
36
	git_str_truncate(&prefix, scan - prefix.ptr);
37 38

	if (prefix.size <= 0) {
39
		git_str_dispose(&prefix);
40 41 42
		return NULL;
	}

43
	git_str_unescape(&prefix);
44

45
	return git_str_detach(&prefix);
46 47 48
}

/* is there anything in the spec that needs to be filtered on */
49
bool git_pathspec_is_empty(const git_strarray *pathspec)
50
{
51
	size_t i;
52

53
	if (pathspec == NULL)
54 55
		return true;

56 57 58 59 60 61 62
	for (i = 0; i < pathspec->count; ++i) {
		const char *str = pathspec->strings[i];

		if (str && str[0])
			return false;
	}

63 64 65 66
	return true;
}

/* build a vector of fnmatch patterns to evaluate efficiently */
67
int git_pathspec__vinit(
68 69 70 71 72 73
	git_vector *vspec, const git_strarray *strspec, git_pool *strpool)
{
	size_t i;

	memset(vspec, 0, sizeof(*vspec));

74
	if (git_pathspec_is_empty(strspec))
75 76 77 78 79 80 81 82 83 84 85 86
		return 0;

	if (git_vector_init(vspec, strspec->count, NULL) < 0)
		return -1;

	for (i = 0; i < strspec->count; ++i) {
		int ret;
		const char *pattern = strspec->strings[i];
		git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch));
		if (!match)
			return -1;

87
		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG;
88

89
		ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern);
90 91 92
		if (ret == GIT_ENOTFOUND) {
			git__free(match);
			continue;
93 94
		} else if (ret < 0) {
			git__free(match);
95
			return ret;
96
		}
97 98 99 100 101 102 103 104 105

		if (git_vector_insert(vspec, match) < 0)
			return -1;
	}

	return 0;
}

/* free data from the pathspec vector */
106
void git_pathspec__vfree(git_vector *vspec)
107
{
108
	git_vector_free_deep(vspec);
109 110
}

111
struct pathspec_match_context {
112
	int wildmatch_flags;
113 114 115 116 117 118 119 120 121 122
	int (*strcomp)(const char *, const char *);
	int (*strncomp)(const char *, const char *, size_t);
};

static void pathspec_match_context_init(
	struct pathspec_match_context *ctxt,
	bool disable_fnmatch,
	bool casefold)
{
	if (disable_fnmatch)
123
		ctxt->wildmatch_flags = -1;
124
	else if (casefold)
125
		ctxt->wildmatch_flags = WM_CASEFOLD;
126
	else
127
		ctxt->wildmatch_flags = 0;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

	if (casefold) {
		ctxt->strcomp  = git__strcasecmp;
		ctxt->strncomp = git__strncasecmp;
	} else {
		ctxt->strcomp  = git__strcmp;
		ctxt->strncomp = git__strncmp;
	}
}

static int pathspec_match_one(
	const git_attr_fnmatch *match,
	struct pathspec_match_context *ctxt,
	const char *path)
{
143
	int result = (match->flags & GIT_ATTR_FNMATCH_MATCH_ALL) ? 0 : WM_NOMATCH;
144

145 146
	if (result == WM_NOMATCH)
		result = ctxt->strcomp(match->pattern, path) ? WM_NOMATCH : 0;
147

148 149
	if (ctxt->wildmatch_flags >= 0 && result == WM_NOMATCH)
		result = wildmatch(match->pattern, path, ctxt->wildmatch_flags);
150 151

	/* if we didn't match, look for exact dirname prefix match */
152
	if (result == WM_NOMATCH &&
153 154 155 156 157
		(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
		ctxt->strncomp(path, match->pattern, match->length) == 0 &&
		path[match->length] == '/')
		result = 0;

158 159 160
	/* if we didn't match and this is a negative match, check for exact
	 * match of filename with leading '!'
	 */
161
	if (result == WM_NOMATCH &&
162 163
		(match->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 &&
		*path == '!' &&
164 165
		ctxt->strncomp(path + 1, match->pattern, match->length) == 0 &&
		(!path[match->length + 1] || path[match->length + 1] == '/'))
166 167
		return 1;

168 169 170 171 172
	if (result == 0)
		return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? 0 : 1;
	return -1;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static int git_pathspec__match_at(
	size_t *matched_at,
	const git_vector *vspec,
	struct pathspec_match_context *ctxt,
	const char *path0,
	const char *path1)
{
	int result = GIT_ENOTFOUND;
	size_t i = 0;
	const git_attr_fnmatch *match;

	git_vector_foreach(vspec, i, match) {
		if (path0 && (result = pathspec_match_one(match, ctxt, path0)) >= 0)
			break;
		if (path1 && (result = pathspec_match_one(match, ctxt, path1)) >= 0)
			break;
	}

	*matched_at = i;
	return result;
}

195
/* match a path against the vectorized pathspec */
196 197
bool git_pathspec__match(
	const git_vector *vspec,
198 199 200
	const char *path,
	bool disable_fnmatch,
	bool casefold,
201 202
	const char **matched_pathspec,
	size_t *matched_at)
203
{
204 205
	int result;
	size_t pos;
206
	struct pathspec_match_context ctxt;
207

208 209
	if (matched_pathspec)
		*matched_pathspec = NULL;
210 211
	if (matched_at)
		*matched_at = GIT_PATHSPEC_NOMATCH;
212

213 214 215
	if (!vspec || !vspec->length)
		return true;

216
	pathspec_match_context_init(&ctxt, disable_fnmatch, casefold);
217

218 219 220 221 222
	result = git_pathspec__match_at(&pos, vspec, &ctxt, path, NULL);
	if (result >= 0) {
		if (matched_pathspec) {
			const git_attr_fnmatch *match = git_vector_get(vspec, pos);
			*matched_pathspec = match->pattern;
223
		}
224 225 226

		if (matched_at)
			*matched_at = pos;
227 228
	}

229
	return (result > 0);
230
}
nulltoken committed
231

232

233 234 235
int git_pathspec__init(git_pathspec *ps, const git_strarray *paths)
{
	int error = 0;
236

237
	memset(ps, 0, sizeof(*ps));
238

239 240
	ps->prefix = git_pathspec_prefix(paths);

241 242
	if ((error = git_pool_init(&ps->pool, 1)) < 0 ||
	    (error = git_pathspec__vinit(&ps->pathspec, paths, &ps->pool)) < 0)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		git_pathspec__clear(ps);

	return error;
}

void git_pathspec__clear(git_pathspec *ps)
{
	git__free(ps->prefix);
	git_pathspec__vfree(&ps->pathspec);
	git_pool_clear(&ps->pool);
	memset(ps, 0, sizeof(*ps));
}

int git_pathspec_new(git_pathspec **out, const git_strarray *pathspec)
{
	int error = 0;
	git_pathspec *ps = git__malloc(sizeof(git_pathspec));
260
	GIT_ERROR_CHECK_ALLOC(ps);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

	if ((error = git_pathspec__init(ps, pathspec)) < 0) {
		git__free(ps);
		return error;
	}

	GIT_REFCOUNT_INC(ps);
	*out = ps;
	return 0;
}

static void pathspec_free(git_pathspec *ps)
{
	git_pathspec__clear(ps);
	git__free(ps);
}

void git_pathspec_free(git_pathspec *ps)
{
	if (!ps)
		return;
	GIT_REFCOUNT_DEC(ps, pathspec_free);
}
284

285 286 287 288 289 290
int git_pathspec_matches_path(
	const git_pathspec *ps, uint32_t flags, const char *path)
{
	bool no_fnmatch = (flags & GIT_PATHSPEC_NO_GLOB) != 0;
	bool casefold =  (flags & GIT_PATHSPEC_IGNORE_CASE) != 0;

291 292
	GIT_ASSERT_ARG(ps);
	GIT_ASSERT_ARG(path);
293 294 295 296 297 298 299

	return (0 != git_pathspec__match(
		&ps->pathspec, path, no_fnmatch, casefold, NULL, NULL));
}

static void pathspec_match_free(git_pathspec_match_list *m)
{
300 301 302
	if (!m)
		return;

303 304 305 306 307 308 309 310 311
	git_pathspec_free(m->pathspec);
	m->pathspec = NULL;

	git_array_clear(m->matches);
	git_array_clear(m->failures);
	git_pool_clear(&m->pool);
	git__free(m);
}

312 313
static git_pathspec_match_list *pathspec_match_alloc(
	git_pathspec *ps, int datatype)
314 315
{
	git_pathspec_match_list *m = git__calloc(1, sizeof(git_pathspec_match_list));
316 317 318
	if (!m)
		return NULL;

319 320
	if (git_pool_init(&m->pool, 1) < 0)
		return NULL;
321

322 323 324 325 326 327
	/* need to keep reference to pathspec and increment refcount because
	 * failures array stores pointers to the pattern strings of the
	 * pathspec that had no matches
	 */
	GIT_REFCOUNT_INC(ps);
	m->pathspec = ps;
328
	m->datatype = datatype;
329 330 331 332

	return m;
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
GIT_INLINE(size_t) pathspec_mark_pattern(git_bitvec *used, size_t pos)
{
	if (!git_bitvec_get(used, pos)) {
		git_bitvec_set(used, pos, true);
		return 1;
	}

	return 0;
}

static size_t pathspec_mark_remaining(
	git_bitvec *used,
	git_vector *patterns,
	struct pathspec_match_context *ctxt,
	size_t start,
	const char *path0,
	const char *path1)
{
	size_t count = 0;

	if (path1 == path0)
		path1 = NULL;

	for (; start < patterns->length; ++start) {
		const git_attr_fnmatch *pat = git_vector_get(patterns, start);

		if (git_bitvec_get(used, start))
			continue;

		if (path0 && pathspec_match_one(pat, ctxt, path0) > 0)
			count += pathspec_mark_pattern(used, start);
		else if (path1 && pathspec_match_one(pat, ctxt, path1) > 0)
			count += pathspec_mark_pattern(used, start);
	}

	return count;
}

static int pathspec_build_failure_array(
	git_pathspec_string_array_t *failures,
	git_vector *patterns,
	git_bitvec *used,
	git_pool *pool)
376
{
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	size_t pos;
	char **failed;
	const git_attr_fnmatch *pat;

	for (pos = 0; pos < patterns->length; ++pos) {
		if (git_bitvec_get(used, pos))
			continue;

		if ((failed = git_array_alloc(*failures)) == NULL)
			return -1;

		pat = git_vector_get(patterns, pos);

		if ((*failed = git_pool_strdup(pool, pat->pattern)) == NULL)
			return -1;
392
	}
393 394

	return 0;
395 396 397 398 399 400 401 402 403
}

static int pathspec_match_from_iterator(
	git_pathspec_match_list **out,
	git_iterator *iter,
	uint32_t flags,
	git_pathspec *ps)
{
	int error = 0;
404
	git_pathspec_match_list *m = NULL;
405 406 407
	const git_index_entry *entry = NULL;
	struct pathspec_match_context ctxt;
	git_vector *patterns = &ps->pathspec;
408 409
	bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0;
	bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0;
410 411
	size_t pos, used_ct = 0, found_files = 0;
	git_index *index = NULL;
412
	git_bitvec used_patterns;
413 414
	char **file;

415 416 417
	if (git_bitvec_init(&used_patterns, patterns->length) < 0)
		return -1;

418
	if (out) {
419
		*out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_STRINGS);
420
		GIT_ERROR_CHECK_ALLOC(m);
421
	}
422

423
	if ((error = git_iterator_reset_range(iter, ps->prefix, ps->prefix)) < 0)
424 425
		goto done;

426
	if (git_iterator_type(iter) == GIT_ITERATOR_WORKDIR &&
427 428 429 430
		(error = git_repository_index__weakptr(
			&index, git_iterator_owner(iter))) < 0)
		goto done;

431 432 433
	pathspec_match_context_init(
		&ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0,
		git_iterator_ignore_case(iter));
434 435

	while (!(error = git_iterator_advance(&entry, iter))) {
436 437 438
		/* search for match with entry->path */
		int result = git_pathspec__match_at(
			&pos, patterns, &ctxt, entry->path, NULL);
439 440 441 442 443 444 445

		/* no matches for this path */
		if (result < 0)
			continue;

		/* if result was a negative pattern match, then don't list file */
		if (!result) {
446
			used_ct += pathspec_mark_pattern(&used_patterns, pos);
447 448 449
			continue;
		}

450
		/* check if path is ignored and untracked */
451 452
		if (index != NULL &&
			git_iterator_current_is_ignored(iter) &&
453
			git_index__find_pos(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
454 455 456
			continue;

		/* mark the matched pattern as used */
457
		used_ct += pathspec_mark_pattern(&used_patterns, pos);
458 459 460
		++found_files;

		/* if find_failures is on, check if any later patterns also match */
461 462 463
		if (find_failures && used_ct < patterns->length)
			used_ct += pathspec_mark_remaining(
				&used_patterns, patterns, &ctxt, pos + 1, entry->path, NULL);
464 465

		/* if only looking at failures, exit early or just continue */
466
		if (failures_only || !out) {
467 468 469 470 471 472
			if (used_ct == patterns->length)
				break;
			continue;
		}

		/* insert matched path into matches array */
473
		if ((file = (char **)git_array_alloc(m->matches)) == NULL ||
474 475 476
			(*file = git_pool_strdup(&m->pool, entry->path)) == NULL) {
			error = -1;
			goto done;
477
		}
478 479
	}

480 481 482 483 484
	if (error < 0 && error != GIT_ITEROVER)
		goto done;
	error = 0;

	/* insert patterns that had no matches into failures array */
485 486 487 488
	if (find_failures && used_ct < patterns->length &&
		(error = pathspec_build_failure_array(
			&m->failures, patterns, &used_patterns, &m->pool)) < 0)
		goto done;
489 490 491

	/* if every pattern failed to match, then we have failed */
	if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_files) {
492
		git_error_set(GIT_ERROR_INVALID, "no matching files were found");
493 494 495 496
		error = GIT_ENOTFOUND;
	}

done:
497
	git_bitvec_free(&used_patterns);
498 499 500

	if (error < 0) {
		pathspec_match_free(m);
501
		if (out) *out = NULL;
502 503 504
	}

	return error;
505 506
}

507 508 509 510 511 512 513 514
static git_iterator_flag_t pathspec_match_iter_flags(uint32_t flags)
{
	git_iterator_flag_t f = 0;

	if ((flags & GIT_PATHSPEC_IGNORE_CASE) != 0)
		f |= GIT_ITERATOR_IGNORE_CASE;
	else if ((flags & GIT_PATHSPEC_USE_CASE) != 0)
		f |= GIT_ITERATOR_DONT_IGNORE_CASE;
515

516 517 518 519 520 521 522 523
	return f;
}

int git_pathspec_match_workdir(
	git_pathspec_match_list **out,
	git_repository *repo,
	uint32_t flags,
	git_pathspec *ps)
524
{
525
	git_iterator *iter;
526 527
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
	int error = 0;
528

529
	GIT_ASSERT_ARG(repo);
530

531
	iter_opts.flags = pathspec_match_iter_flags(flags);
532

533
	if (!(error = git_iterator_for_workdir(&iter, repo, NULL, NULL, &iter_opts))) {
534 535 536
		error = pathspec_match_from_iterator(out, iter, flags, ps);
		git_iterator_free(iter);
	}
537 538 539 540

	return error;
}

541 542 543 544 545 546 547
int git_pathspec_match_index(
	git_pathspec_match_list **out,
	git_index *index,
	uint32_t flags,
	git_pathspec *ps)
{
	git_iterator *iter;
548 549
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
	int error = 0;
550

551
	GIT_ASSERT_ARG(index);
552

553
	iter_opts.flags = pathspec_match_iter_flags(flags);
554

555
	if (!(error = git_iterator_for_index(&iter, git_index_owner(index), index, &iter_opts))) {
556 557 558 559 560 561 562 563 564 565 566 567 568 569
		error = pathspec_match_from_iterator(out, iter, flags, ps);
		git_iterator_free(iter);
	}

	return error;
}

int git_pathspec_match_tree(
	git_pathspec_match_list **out,
	git_tree *tree,
	uint32_t flags,
	git_pathspec *ps)
{
	git_iterator *iter;
570 571
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
	int error = 0;
572

573
	GIT_ASSERT_ARG(tree);
574

575
	iter_opts.flags = pathspec_match_iter_flags(flags);
576

577
	if (!(error = git_iterator_for_tree(&iter, tree, &iter_opts))) {
578 579 580 581 582 583 584
		error = pathspec_match_from_iterator(out, iter, flags, ps);
		git_iterator_free(iter);
	}

	return error;
}

585 586
int git_pathspec_match_diff(
	git_pathspec_match_list **out,
587
	git_diff *diff,
588 589 590 591 592 593 594 595 596 597 598 599 600
	uint32_t flags,
	git_pathspec *ps)
{
	int error = 0;
	git_pathspec_match_list *m = NULL;
	struct pathspec_match_context ctxt;
	git_vector *patterns = &ps->pathspec;
	bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0;
	bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0;
	size_t i, pos, used_ct = 0, found_deltas = 0;
	const git_diff_delta *delta, **match;
	git_bitvec used_patterns;

601
	GIT_ASSERT_ARG(diff);
602 603 604 605 606 607

	if (git_bitvec_init(&used_patterns, patterns->length) < 0)
		return -1;

	if (out) {
		*out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_DIFF);
608
		GIT_ERROR_CHECK_ALLOC(m);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	}

	pathspec_match_context_init(
		&ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0,
		git_diff_is_sorted_icase(diff));

	git_vector_foreach(&diff->deltas, i, delta) {
		/* search for match with delta */
		int result = git_pathspec__match_at(
			&pos, patterns, &ctxt, delta->old_file.path, delta->new_file.path);

		/* no matches for this path */
		if (result < 0)
			continue;

		/* mark the matched pattern as used */
		used_ct += pathspec_mark_pattern(&used_patterns, pos);

		/* if result was a negative pattern match, then don't list file */
		if (!result)
			continue;

		++found_deltas;

		/* if find_failures is on, check if any later patterns also match */
		if (find_failures && used_ct < patterns->length)
			used_ct += pathspec_mark_remaining(
				&used_patterns, patterns, &ctxt, pos + 1,
				delta->old_file.path, delta->new_file.path);

		/* if only looking at failures, exit early or just continue */
		if (failures_only || !out) {
			if (used_ct == patterns->length)
				break;
			continue;
		}

		/* insert matched delta into matches array */
		if (!(match = (const git_diff_delta **)git_array_alloc(m->matches))) {
			error = -1;
			goto done;
		} else {
			*match = delta;
		}
	}

	/* insert patterns that had no matches into failures array */
	if (find_failures && used_ct < patterns->length &&
		(error = pathspec_build_failure_array(
			&m->failures, patterns, &used_patterns, &m->pool)) < 0)
		goto done;

	/* if every pattern failed to match, then we have failed */
	if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_deltas) {
663
		git_error_set(GIT_ERROR_INVALID, "no matching deltas were found");
664 665 666 667 668 669 670 671 672 673 674 675 676 677
		error = GIT_ENOTFOUND;
	}

done:
	git_bitvec_free(&used_patterns);

	if (error < 0) {
		pathspec_match_free(m);
		if (out) *out = NULL;
	}

	return error;
}

678 679
void git_pathspec_match_list_free(git_pathspec_match_list *m)
{
680 681
	if (m)
		pathspec_match_free(m);
682 683 684 685 686
}

size_t git_pathspec_match_list_entrycount(
	const git_pathspec_match_list *m)
{
687
	return m ? git_array_size(m->matches) : 0;
688 689 690 691 692
}

const char *git_pathspec_match_list_entry(
	const git_pathspec_match_list *m, size_t pos)
{
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	if (!m || m->datatype != PATHSPEC_DATATYPE_STRINGS ||
		!git_array_valid_index(m->matches, pos))
		return NULL;

	return *((const char **)git_array_get(m->matches, pos));
}

const git_diff_delta *git_pathspec_match_list_diff_entry(
	const git_pathspec_match_list *m, size_t pos)
{
	if (!m || m->datatype != PATHSPEC_DATATYPE_DIFF ||
		!git_array_valid_index(m->matches, pos))
		return NULL;

	return *((const git_diff_delta **)git_array_get(m->matches, pos));
708 709 710 711 712
}

size_t git_pathspec_match_list_failed_entrycount(
	const git_pathspec_match_list *m)
{
713
	return m ? git_array_size(m->failures) : 0;
714 715 716 717
}

const char * git_pathspec_match_list_failed_entry(
	const git_pathspec_match_list *m, size_t pos)
718
{
719 720
	char **entry = m ? git_array_get(m->failures, pos) : NULL;

721
	return entry ? *entry : NULL;
722
}