iterator.c 31.9 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9 10 11
 *
 * 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 "iterator.h"
#include "tree.h"
#include "ignore.h"
#include "buffer.h"
12
#include "git2/submodule.h"
13
#include <ctype.h>
14

15 16 17
#define ITERATOR_SET_CB(P,NAME_LC) do { \
	(P)->cb.current = NAME_LC ## _iterator__current; \
	(P)->cb.advance = NAME_LC ## _iterator__advance; \
18
	(P)->cb.advance_into = NAME_LC ## _iterator__advance_into; \
19 20
	(P)->cb.seek    = NAME_LC ## _iterator__seek; \
	(P)->cb.reset   = NAME_LC ## _iterator__reset; \
21
	(P)->cb.at_end  = NAME_LC ## _iterator__at_end; \
22 23 24
	(P)->cb.free    = NAME_LC ## _iterator__free; \
	} while (0)

25 26 27
#define ITERATOR_CASE_FLAGS \
	(GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_IGNORE_CASE)

28
#define ITERATOR_BASE_INIT(P,NAME_LC,NAME_UC,REPO) do { \
29 30
	(P) = git__calloc(1, sizeof(NAME_LC ## _iterator)); \
	GITERR_CHECK_ALLOC(P); \
31
	(P)->base.type    = GIT_ITERATOR_TYPE_ ## NAME_UC; \
32
	(P)->base.cb      = &(P)->cb; \
33
	ITERATOR_SET_CB(P,NAME_LC); \
34
	(P)->base.repo    = (REPO); \
35 36
	(P)->base.start   = start ? git__strdup(start) : NULL; \
	(P)->base.end     = end ? git__strdup(end) : NULL; \
37 38
	if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
		git__free(P); return -1; } \
39
	(P)->base.prefixcomp = git__prefixcmp; \
40
	(P)->base.flags = flags & ~ITERATOR_CASE_FLAGS; \
41 42
	if ((P)->base.flags & GIT_ITERATOR_DONT_AUTOEXPAND) \
		(P)->base.flags |= GIT_ITERATOR_INCLUDE_TREES; \
43 44
	} while (0)

45 46 47 48 49
#define iterator__flag(I,F) ((((git_iterator *)(I))->flags & GIT_ITERATOR_ ## F) != 0)
#define iterator__ignore_case(I)     iterator__flag(I,IGNORE_CASE)
#define iterator__include_trees(I)   iterator__flag(I,INCLUDE_TREES)
#define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
#define iterator__do_autoexpand(I)   !iterator__flag(I,DONT_AUTOEXPAND)
50

51
#define iterator__end(I) ((git_iterator *)(I))->end
52 53 54 55
#define iterator__past_end(I,PATH) \
	(iterator__end(I) && ((git_iterator *)(I))->prefixcomp((PATH),iterator__end(I)) > 0)


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static int iterator__reset_range(
	git_iterator *iter, const char *start, const char *end)
{
	if (start) {
		if (iter->start)
			git__free(iter->start);
		iter->start = git__strdup(start);
		GITERR_CHECK_ALLOC(iter->start);
	}

	if (end) {
		if (iter->end)
			git__free(iter->end);
		iter->end = git__strdup(end);
		GITERR_CHECK_ALLOC(iter->end);
	}

	return 0;
}
75

76
static int iterator__update_ignore_case(
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	git_iterator *iter,
	git_iterator_flag_t flags)
{
	int error = 0, ignore_case = -1;

	if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
		ignore_case = true;
	else if ((flags & GIT_ITERATOR_DONT_IGNORE_CASE) != 0)
		ignore_case = false;
	else {
		git_index *index;

		if (!(error = git_repository_index__weakptr(&index, iter->repo)))
			ignore_case = (index->ignore_case != false);
	}

	if (ignore_case > 0)
		iter->flags = (iter->flags | GIT_ITERATOR_IGNORE_CASE);
	else if (ignore_case == 0)
		iter->flags = (iter->flags & ~GIT_ITERATOR_IGNORE_CASE);

98
	iter->prefixcomp = iterator__ignore_case(iter) ?
99 100
		git__prefixcmp_icase : git__prefixcmp;

101 102 103
	return error;
}

104
GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
105
{
106
	if (entry) *entry = NULL;
107 108 109 110 111 112 113
}


static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
{
	GIT_UNUSED(i);
	iterator__clear_entry(e);
114
	return 0;
115 116
}

117
static int empty_iterator__seek(git_iterator *i, const char *p)
118
{
119
	GIT_UNUSED(i); GIT_UNUSED(p);
120 121 122
	return -1;
}

123
static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
124
{
125
	GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
126 127 128
	return 0;
}

129
static int empty_iterator__at_end(git_iterator *i)
130
{
131
	GIT_UNUSED(i);
132 133 134
	return 1;
}

135
static void empty_iterator__free(git_iterator *i)
136
{
137
	GIT_UNUSED(i);
138 139
}

140 141 142 143 144
typedef struct {
	git_iterator base;
	git_iterator_callbacks cb;
} empty_iterator;

145 146 147 148 149
int git_iterator_for_nothing(
	git_iterator **iter,
	git_iterator_flag_t flags,
	const char *start,
	const char *end)
150
{
151
	empty_iterator *i;
152

153 154
#define empty_iterator__current empty_iterator__noop
#define empty_iterator__advance empty_iterator__noop
155
#define empty_iterator__advance_into empty_iterator__noop
156

157
	ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
158 159 160

	if ((flags & GIT_ITERATOR_IGNORE_CASE) != 0)
		i->base.flags |= GIT_ITERATOR_IGNORE_CASE;
161

162
	*iter = (git_iterator *)i;
163 164 165
	return 0;
}

166

167 168 169 170 171 172
typedef struct tree_iterator_entry tree_iterator_entry;
struct tree_iterator_entry {
	tree_iterator_entry *parent;
	const git_tree_entry *te;
	git_tree *tree;
};
173

174 175
typedef struct tree_iterator_frame tree_iterator_frame;
struct tree_iterator_frame {
176
	tree_iterator_frame *up, *down;
177 178 179 180 181

	size_t n_entries; /* items in this frame */
	size_t current;   /* start of currently active range in frame */
	size_t next;      /* start of next range in frame */

182
	const char *start;
183
	size_t startlen;
184

185
	tree_iterator_entry *entries[GIT_FLEX_ARRAY];
186
};
187 188

typedef struct {
189
	git_iterator base;
190
	git_iterator_callbacks cb;
191 192
	tree_iterator_frame *head, *root;
	git_pool pool;
193 194
	git_index_entry entry;
	git_buf path;
195
	int path_ambiguities;
196
	bool path_has_filename;
197
	int (*strncomp)(const char *a, const char *b, size_t sz);
198
} tree_iterator;
199

200 201 202 203 204 205
static char *tree_iterator__current_filename(
	tree_iterator *ti, const git_tree_entry *te)
{
	if (!ti->path_has_filename) {
		if (git_buf_joinpath(&ti->path, ti->path.ptr, te->filename) < 0)
			return NULL;
206 207 208 209

		if (git_tree_entry__is_tree(te) && git_buf_putc(&ti->path, '/') < 0)
			return NULL;

210 211 212 213 214 215
		ti->path_has_filename = true;
	}

	return ti->path.ptr;
}

216
static void tree_iterator__rewrite_filename(tree_iterator *ti)
217
{
218
	tree_iterator_entry *scan = ti->head->entries[ti->head->current];
219 220
	ssize_t strpos = ti->path.size;
	const git_tree_entry *te;
221

222 223 224
	if (strpos && ti->path.ptr[strpos - 1] == '/')
		strpos--;

225
	for (; scan && (te = scan->te); scan = scan->parent) {
226 227 228
		strpos -= te->filename_len;
		memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
		strpos -= 1; /* separator */
229
	}
230
}
231

232
static int tree_iterator__te_cmp(
233 234
	const git_tree_entry *a,
	const git_tree_entry *b,
235
	int (*compare)(const char *, const char *, size_t))
236
{
237 238 239 240
	return git_path_cmp(
		a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
		b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
		compare);
241 242
}

243
static int tree_iterator__ci_cmp(const void *a, const void *b, void *p)
244
{
245
	const tree_iterator_entry *ae = a, *be = b;
246
	int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp);
247 248

	if (!cmp) {
249 250 251 252 253
		/* stabilize sort order among equivalent names */
		if (!ae->parent->te || !be->parent->te)
			cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncmp);
		else
			cmp = tree_iterator__ci_cmp(ae->parent, be->parent, p);
254 255 256
	}

	return cmp;
257 258
}

259 260 261 262 263 264 265 266 267 268 269
static int tree_iterator__search_cmp(const void *key, const void *val, void *p)
{
	const tree_iterator_frame *tf = key;
	const git_tree_entry *te = ((tree_iterator_entry *)val)->te;

	return git_path_cmp(
		tf->start, tf->startlen, false,
		te->filename, te->filename_len, te->attr == GIT_FILEMODE_TREE,
		((tree_iterator *)p)->strncomp);
}

270
static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
271
{
272
	int error;
273
	const git_tree_entry *te, *last = NULL;
274

275
	tf->next = tf->current;
276

277 278 279 280
	for (; tf->next < tf->n_entries; tf->next++, last = te) {
		te = tf->entries[tf->next]->te;

		if (last && tree_iterator__te_cmp(last, te, ti->strncomp))
281
			break;
282

283
		/* load trees for items in [current,next) range */
284 285
		if (git_tree_entry__is_tree(te) &&
			(error = git_tree_lookup(
286 287
				&tf->entries[tf->next]->tree, ti->base.repo, &te->oid)) < 0)
			return error;
288
	}
289

290 291 292
	if (tf->next > tf->current + 1)
		ti->path_ambiguities++;

293
	if (last && !tree_iterator__current_filename(ti, last))
294
		return -1;
295

296
	return 0;
297 298
}

299
GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
300
{
301
	return (ti->head->current < ti->head->n_entries &&
302
			ti->head->entries[ti->head->current]->tree != NULL);
303 304
}

305
static int tree_iterator__push_frame(tree_iterator *ti)
306
{
307
	int error = 0;
308 309
	tree_iterator_frame *head = ti->head, *tf = NULL;
	size_t i, n_entries = 0;
310

311
	if (head->current >= head->n_entries || !head->entries[head->current]->tree)
312
		return 0;
313

314 315 316 317 318 319 320 321
	for (i = head->current; i < head->next; ++i)
		n_entries += git_tree_entrycount(head->entries[i]->tree);

	tf = git__calloc(sizeof(tree_iterator_frame) +
		n_entries * sizeof(tree_iterator_entry *), 1);
	GITERR_CHECK_ALLOC(tf);

	tf->n_entries = n_entries;
322

323 324 325
	tf->up     = head;
	head->down = tf;
	ti->head   = tf;
326

327 328
	for (i = head->current, n_entries = 0; i < head->next; ++i) {
		git_tree *tree = head->entries[i]->tree;
329 330 331
		size_t j, max_j = git_tree_entrycount(tree);

		for (j = 0; j < max_j; ++j) {
332 333 334 335 336 337 338 339
			tree_iterator_entry *entry = git_pool_malloc(&ti->pool, 1);
			GITERR_CHECK_ALLOC(entry);

			entry->parent = head->entries[i];
			entry->te     = git_tree_entry_byindex(tree, j);
			entry->tree   = NULL;

			tf->entries[n_entries++] = entry;
340 341
		}
	}
342

343 344
	/* if ignore_case, sort entries case insensitively */
	if (iterator__ignore_case(ti))
345 346 347 348 349 350 351 352 353 354 355
		git__tsort_r(
			(void **)tf->entries, tf->n_entries, tree_iterator__ci_cmp, tf);

	/* pick tf->current based on "start" (or start at zero) */
	if (head->startlen > 0) {
		git__bsearch_r((void **)tf->entries, tf->n_entries, head,
			tree_iterator__search_cmp, ti, &tf->current);

		while (tf->current &&
			   !tree_iterator__search_cmp(head, tf->entries[tf->current-1], ti))
			tf->current--;
356

357 358 359
		if ((tf->start = strchr(head->start, '/')) != NULL) {
			tf->start++;
			tf->startlen = strlen(tf->start);
360
		}
361 362
	}

363 364
	ti->path_has_filename = false;

365
	if ((error = tree_iterator__set_next(ti, tf)) < 0)
366
		return error;
367

368
	/* autoexpand as needed */
369 370
	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
		return tree_iterator__push_frame(ti);
371

372 373
	return 0;
}
374

375 376
static bool tree_iterator__move_to_next(
	tree_iterator *ti, tree_iterator_frame *tf)
377
{
378 379 380
	if (tf->next > tf->current + 1)
		ti->path_ambiguities--;

381 382 383 384 385
	if (!tf->up) { /* at root */
		tf->current = tf->next;
		return false;
	}

386
	for (; tf->current < tf->next; tf->current++) {
387 388
		git_tree_free(tf->entries[tf->current]->tree);
		tf->entries[tf->current]->tree = NULL;
389
	}
390

391
	return (tf->current < tf->n_entries);
392
}
393

394
static bool tree_iterator__pop_frame(tree_iterator *ti, bool final)
395
{
396
	tree_iterator_frame *tf = ti->head;
397

398
	if (!tf->up)
399
		return false;
400

401 402 403
	ti->head = tf->up;
	ti->head->down = NULL;

404
	tree_iterator__move_to_next(ti, tf);
405

406 407 408 409
	if (!final) { /* if final, don't bother to clean up */
		git_pool_free_array(&ti->pool, tf->n_entries, (void **)tf->entries);
		git_buf_rtruncate_at_char(&ti->path, '/');
	}
410

411
	git__free(tf);
412

413 414
	return true;
}
415

416 417 418 419 420 421 422 423 424 425 426 427 428
static int tree_iterator__pop_all(tree_iterator *ti, bool to_end, bool final)
{
	while (tree_iterator__pop_frame(ti, final)) /* pop to root */;

	if (!final) {
		ti->head->current = to_end ? ti->head->n_entries : 0;
		ti->path_ambiguities = 0;
		git_buf_clear(&ti->path);
	}

	return 0;
}

429 430 431 432 433 434
static int tree_iterator__current(
	const git_index_entry **entry, git_iterator *self)
{
	tree_iterator *ti = (tree_iterator *)self;
	tree_iterator_frame *tf = ti->head;
	const git_tree_entry *te;
435

436
	iterator__clear_entry(entry);
437

438
	if (tf->current >= tf->n_entries)
439
		return 0;
440
	te = tf->entries[tf->current]->te;
441

442 443 444 445
	ti->entry.mode = te->attr;
	git_oid_cpy(&ti->entry.oid, &te->oid);

	ti->entry.path = tree_iterator__current_filename(ti, te);
446
	GITERR_CHECK_ALLOC(ti->entry.path);
447

448 449 450
	if (ti->path_ambiguities > 0)
		tree_iterator__rewrite_filename(ti);

451 452
	if (iterator__past_end(ti, ti->entry.path))
		return tree_iterator__pop_all(ti, true, false);
453

454 455 456
	if (entry)
		*entry = &ti->entry;

457
	return 0;
458 459
}

460 461 462 463 464 465
static int tree_iterator__advance_into(
	const git_index_entry **entry, git_iterator *self)
{
	int error = 0;
	tree_iterator *ti = (tree_iterator *)self;

466
	iterator__clear_entry(entry);
467

468 469
	if (tree_iterator__at_tree(ti) &&
		!(error = tree_iterator__push_frame(ti)))
470 471 472 473 474
		error = tree_iterator__current(entry, self);

	return error;
}

475
static int tree_iterator__advance(
476
	const git_index_entry **entry, git_iterator *self)
477
{
478
	int error;
479
	tree_iterator *ti = (tree_iterator *)self;
480
	tree_iterator_frame *tf = ti->head;
481

482
	iterator__clear_entry(entry);
483

484 485 486 487 488
	if (tf->current > tf->n_entries)
		return 0;

	if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
		tree_iterator__at_tree(ti))
489 490
		return tree_iterator__advance_into(entry, self);

491
	if (ti->path_has_filename) {
492
		git_buf_rtruncate_at_char(&ti->path, '/');
493 494
		ti->path_has_filename = false;
	}
495

496
	/* scan forward and up, advancing in frame or popping frame when done */
497 498
	while (!tree_iterator__move_to_next(ti, tf) &&
		   tree_iterator__pop_frame(ti, false))
499
		tf = ti->head;
500

501 502 503
	/* find next and load trees */
	if ((error = tree_iterator__set_next(ti, tf)) < 0)
		return error;
504

505 506
	/* deal with include_trees / auto_expand as needed */
	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
507
		return tree_iterator__advance_into(entry, self);
508

509
	return tree_iterator__current(entry, self);
510 511
}

512 513
static int tree_iterator__seek(git_iterator *self, const char *prefix)
{
514
	GIT_UNUSED(self); GIT_UNUSED(prefix);
515 516 517
	return -1;
}

518 519
static int tree_iterator__reset(
	git_iterator *self, const char *start, const char *end)
520
{
521
	tree_iterator *ti = (tree_iterator *)self;
522

523
	tree_iterator__pop_all(ti, false, false);
524

525 526
	if (iterator__reset_range(self, start, end) < 0)
		return -1;
527

528
	return tree_iterator__push_frame(ti); /* re-expand root tree */
529 530
}

531
static int tree_iterator__at_end(git_iterator *self)
532 533
{
	tree_iterator *ti = (tree_iterator *)self;
534 535
	return (ti->head->current >= ti->head->n_entries);
}
536

537 538 539
static void tree_iterator__free(git_iterator *self)
{
	tree_iterator *ti = (tree_iterator *)self;
540

541
	tree_iterator__pop_all(ti, true, false);
542

543 544 545
	git_tree_free(ti->head->entries[0]->tree);
	git__free(ti->head);
	git_pool_clear(&ti->pool);
546
	git_buf_free(&ti->path);
547 548
}

549
static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree)
550 551
{
	size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
552 553
	tree_iterator_frame *root = git__calloc(sz, sizeof(char));
	GITERR_CHECK_ALLOC(root);
554

555 556 557 558 559 560 561
	root->n_entries  = 1;
	root->next       = 1;
	root->start      = ti->base.start;
	root->startlen   = root->start ? strlen(root->start) : 0;
	root->entries[0] = git_pool_mallocz(&ti->pool, 1);
	GITERR_CHECK_ALLOC(root->entries[0]);
	root->entries[0]->tree = tree;
562

563
	ti->head = ti->root = root;
564 565 566 567

	return 0;
}

568
int git_iterator_for_tree(
569 570
	git_iterator **iter,
	git_tree *tree,
571
	git_iterator_flag_t flags,
572 573
	const char *start,
	const char *end)
574 575
{
	int error;
576 577 578
	tree_iterator *ti;

	if (tree == NULL)
579
		return git_iterator_for_nothing(iter, flags, start, end);
580

581 582 583
	if ((error = git_tree__dup(&tree, tree)) < 0)
		return error;

584
	ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
585

586
	if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
587
		goto fail;
588
	ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
589

590 591 592
	if ((error = git_pool_init(&ti->pool, sizeof(tree_iterator_entry),0)) < 0 ||
		(error = tree_iterator__create_root_frame(ti, tree)) < 0 ||
		(error = tree_iterator__push_frame(ti)) < 0) /* expand root now */
593 594 595 596
		goto fail;

	*iter = (git_iterator *)ti;
	return 0;
597

598 599
fail:
	git_iterator_free((git_iterator *)ti);
600 601 602 603 604
	return error;
}


typedef struct {
605
	git_iterator base;
606
	git_iterator_callbacks cb;
607
	git_index *index;
608
	size_t current;
609 610 611 612 613
	/* when not in autoexpand mode, use these to represent "tree" state */
	git_buf partial;
	size_t partial_pos;
	char restore_terminator;
	git_index_entry tree_entry;
614
} index_iterator;
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 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
{
	const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);

	if (ie != NULL && iterator__past_end(ii, ie->path)) {
		ii->current = git_index_entrycount(ii->index);
		ie = NULL;
	}

	return ie;
}

static const git_index_entry *index_iterator__skip_conflicts(index_iterator *ii)
{
	const git_index_entry *ie;

	while ((ie = index_iterator__index_entry(ii)) != NULL &&
		   git_index_entry_stage(ie) != 0)
		ii->current++;

	return ie;
}

static void index_iterator__next_prefix_tree(index_iterator *ii)
{
	const char *slash;

	if (!iterator__include_trees(ii))
		return;

	slash = strchr(&ii->partial.ptr[ii->partial_pos], '/');

	if (slash != NULL) {
		ii->partial_pos = (slash - ii->partial.ptr) + 1;
		ii->restore_terminator = ii->partial.ptr[ii->partial_pos];
		ii->partial.ptr[ii->partial_pos] = '\0';
	} else {
		ii->partial_pos = ii->partial.size;
	}

	if (index_iterator__index_entry(ii) == NULL)
		ii->partial_pos = ii->partial.size;
}

static int index_iterator__first_prefix_tree(index_iterator *ii)
{
	const git_index_entry *ie = index_iterator__skip_conflicts(ii);
	const char *scan, *prior, *slash;

	if (!ie || !iterator__include_trees(ii))
		return 0;

	/* find longest common prefix with prior index entry */
	for (scan = slash = ie->path, prior = ii->partial.ptr;
		 *scan && *scan == *prior; ++scan, ++prior)
		if (*scan == '/')
			slash = scan;

	if (git_buf_sets(&ii->partial, ie->path) < 0)
		return -1;

	ii->partial_pos = (slash - ie->path) + 1;
	index_iterator__next_prefix_tree(ii);

	return 0;
}

#define index_iterator__at_tree(I) \
	(iterator__include_trees(I) && (I)->partial_pos < (I)->partial.size)

686
static int index_iterator__current(
687
	const git_index_entry **entry, git_iterator *self)
688
{
689
	index_iterator *ii = (index_iterator *)self;
Ben Straub committed
690
	const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
691

692 693 694 695 696
	if (ie != NULL && index_iterator__at_tree(ii)) {
		ii->tree_entry.path = ii->partial.ptr;
		ie = &ii->tree_entry;
	}

697 698 699
	if (entry)
		*entry = ie;

700
	return 0;
701 702
}

703
static int index_iterator__at_end(git_iterator *self)
704
{
705
	index_iterator *ii = (index_iterator *)self;
706 707 708
	return (ii->current >= git_index_entrycount(ii->index));
}

709 710
static int index_iterator__advance(
	const git_index_entry **entry, git_iterator *self)
711
{
712
	index_iterator *ii = (index_iterator *)self;
713
	size_t entrycount = git_index_entrycount(ii->index);
714 715 716 717 718 719 720
	const git_index_entry *ie;

	if (index_iterator__at_tree(ii)) {
		if (iterator__do_autoexpand(ii)) {
			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
			index_iterator__next_prefix_tree(ii);
		} else {
721
			/* advance to sibling tree (i.e. find entry with new prefix) */
722 723 724 725 726 727 728
			while (ii->current < entrycount) {
				ii->current++;

				if (!(ie = git_index_get_byindex(ii->index, ii->current)) ||
					ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
					break;
			}
729

730 731
			if (index_iterator__first_prefix_tree(ii) < 0)
				return -1;
732
		}
733 734 735
	} else {
		if (ii->current < entrycount)
			ii->current++;
736

737 738
		if (index_iterator__first_prefix_tree(ii) < 0)
			return -1;
739
	}
740 741

	return index_iterator__current(entry, self);
742 743
}

744
static int index_iterator__advance_into(
745
	const git_index_entry **entry, git_iterator *self)
746
{
747
	index_iterator *ii = (index_iterator *)self;
748
	const git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
749

750 751 752 753 754
	if (ie != NULL && index_iterator__at_tree(ii)) {
		if (ii->restore_terminator)
			ii->partial.ptr[ii->partial_pos] = ii->restore_terminator;
		index_iterator__next_prefix_tree(ii);
	}
755

756
	return index_iterator__current(entry, self);
757 758 759 760
}

static int index_iterator__seek(git_iterator *self, const char *prefix)
{
761
	GIT_UNUSED(self); GIT_UNUSED(prefix);
762
	return -1;
763 764
}

765 766
static int index_iterator__reset(
	git_iterator *self, const char *start, const char *end)
767 768
{
	index_iterator *ii = (index_iterator *)self;
769
	const git_index_entry *ie;
770

771 772
	if (iterator__reset_range(self, start, end) < 0)
		return -1;
773

774 775
	ii->current = ii->base.start ?
		git_index__prefix_position(ii->index, ii->base.start) : 0;
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
	if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
		return 0;

	if (git_buf_sets(&ii->partial, ie->path) < 0)
		return -1;

	ii->partial_pos = 0;

	if (ii->base.start) {
		size_t startlen = strlen(ii->base.start);

		ii->partial_pos = (startlen > ii->partial.size) ?
			ii->partial.size : startlen;
	}

	index_iterator__next_prefix_tree(ii);
793

794
	return 0;
795 796
}

797
static void index_iterator__free(git_iterator *self)
798
{
799
	index_iterator *ii = (index_iterator *)self;
800
	git_index_free(ii->index);
801
	ii->index = NULL;
802 803

	git_buf_free(&ii->partial);
804 805
}

806
int git_iterator_for_index(
807
	git_iterator **iter,
808
	git_index  *index,
809
	git_iterator_flag_t flags,
810 811
	const char *start,
	const char *end)
812
{
813
	index_iterator *ii;
814

815
	ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
816

817
	if (index->ignore_case) {
818
		ii->base.flags |= GIT_ITERATOR_IGNORE_CASE;
819 820
		ii->base.prefixcomp = git__prefixcmp_icase;
	}
821

822 823
	ii->index = index;
	GIT_REFCOUNT_INC(index);
824

825 826 827
	git_buf_init(&ii->partial, 0);
	ii->tree_entry.mode = GIT_FILEMODE_TREE;

828
	index_iterator__reset((git_iterator *)ii, NULL, NULL);
829

830 831 832
	*iter = (git_iterator *)ii;

	return 0;
833 834 835
}


836 837
#define WORKDIR_MAX_DEPTH 100

838 839 840 841
typedef struct workdir_iterator_frame workdir_iterator_frame;
struct workdir_iterator_frame {
	workdir_iterator_frame *next;
	git_vector entries;
842
	size_t index;
843 844
};

845
typedef struct {
846
	git_iterator base;
847
	git_iterator_callbacks cb;
848
	workdir_iterator_frame *stack;
849 850 851
	git_ignores ignores;
	git_index_entry entry;
	git_buf path;
852
	size_t root_len;
853
	int is_ignored;
854
	int depth;
855 856
} workdir_iterator;

857 858 859 860 861 862 863 864
GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps)
{
	if (!ps)
		return false;
	else {
		const char *path = ps->path;
		size_t len  = ps->path_len;

865 866 867 868 869 870 871 872 873 874
		if (len < 4)
			return false;
		if (path[len - 1] == '/')
			len--;
		if (tolower(path[len - 1]) != 't' ||
			tolower(path[len - 2]) != 'i' ||
			tolower(path[len - 3]) != 'g' ||
			tolower(path[len - 4]) != '.')
			return false;
		return (len == 4 || path[len - 5] == '/');
875 876 877
	}
}

878 879
static workdir_iterator_frame *workdir_iterator__alloc_frame(
	workdir_iterator *wi)
880 881
{
	workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
882
	git_vector_cmp entry_compare = CASESELECT(
883
		iterator__ignore_case(wi),
884
		git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
885

886
	if (wf == NULL)
887
		return NULL;
888

889
	if (git_vector_init(&wf->entries, 0, entry_compare) != 0) {
890 891 892
		git__free(wf);
		return NULL;
	}
893

894 895
	return wf;
}
896

897
static void workdir_iterator__free_frame(workdir_iterator_frame *wf)
898 899
{
	unsigned int i;
900
	git_path_with_stat *path;
901

902
	git_vector_foreach(&wf->entries, i, path)
903
		git__free(path);
904 905
	git_vector_free(&wf->entries);
	git__free(wf);
906 907
}

908
static int workdir_iterator__update_entry(workdir_iterator *wi);
909

910
static int workdir_iterator__entry_cmp(const void *i, const void *item)
911
{
912
	const workdir_iterator *wi = (const workdir_iterator *)i;
913
	const git_path_with_stat *ps = item;
914
	return wi->base.prefixcomp(wi->base.start, ps->path);
915 916 917 918 919 920 921 922 923
}

static void workdir_iterator__seek_frame_start(
	workdir_iterator *wi, workdir_iterator_frame *wf)
{
	if (!wf)
		return;

	if (wi->base.start)
924
		git_vector_bsearch2(
925
			&wf->index, &wf->entries, workdir_iterator__entry_cmp, wi);
926 927 928 929 930
	else
		wf->index = 0;

	if (path_is_dotgit(git_vector_get(&wf->entries, wf->index)))
		wf->index++;
931 932
}

933
static int workdir_iterator__expand_dir(workdir_iterator *wi)
934 935
{
	int error;
936 937 938
	workdir_iterator_frame *wf;

	wf = workdir_iterator__alloc_frame(wi);
939
	GITERR_CHECK_ALLOC(wf);
940

941
	error = git_path_dirload_with_stat(
942
		wi->path.ptr, wi->root_len, iterator__ignore_case(wi),
943 944
		wi->base.start, wi->base.end, &wf->entries);

945
	if (error < 0 || wf->entries.length == 0) {
946
		workdir_iterator__free_frame(wf);
947 948 949
		return GIT_ENOTFOUND;
	}

950 951 952 953 954 955 956
	if (++(wi->depth) > WORKDIR_MAX_DEPTH) {
		giterr_set(GITERR_REPOSITORY,
			"Working directory is too deep (%d)", wi->depth);
		workdir_iterator__free_frame(wf);
		return -1;
	}

957
	workdir_iterator__seek_frame_start(wi, wf);
958

959
	/* only push new ignores if this is not top level directory */
960
	if (wi->stack != NULL) {
961
		ssize_t slash_pos = git_buf_rfind_next(&wi->path, '/');
962 963 964
		(void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]);
	}

965 966 967
	wf->next  = wi->stack;
	wi->stack = wf;

968
	return workdir_iterator__update_entry(wi);
969 970
}

971
static int workdir_iterator__current(
972
	const git_index_entry **entry, git_iterator *self)
973
{
974
	workdir_iterator *wi = (workdir_iterator *)self;
975 976
	if (entry)
		*entry = (wi->entry.path == NULL) ? NULL : &wi->entry;
977
	return 0;
978 979
}

980
static int workdir_iterator__at_end(git_iterator *self)
981
{
982
	return (((workdir_iterator *)self)->entry.path == NULL);
983 984
}

985 986 987 988 989 990
static int workdir_iterator__advance_into(
	const git_index_entry **entry, git_iterator *iter)
{
	int error = 0;
	workdir_iterator *wi = (workdir_iterator *)iter;

991
	iterator__clear_entry(entry);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

	/* workdir iterator will allow you to explicitly advance into a
	 * commit/submodule (as well as a tree) to avoid some cases where an
	 * entry is mislabeled as a submodule in the working directory
	 */
	if (wi->entry.path != NULL &&
		(wi->entry.mode == GIT_FILEMODE_TREE ||
		 wi->entry.mode == GIT_FILEMODE_COMMIT))
		/* returns GIT_ENOTFOUND if the directory is empty */
		error = workdir_iterator__expand_dir(wi);

	if (!error && entry)
		error = workdir_iterator__current(entry, iter);

	return error;
}

1009
static int workdir_iterator__advance(
1010
	const git_index_entry **entry, git_iterator *self)
1011
{
1012
	int error = 0;
1013 1014
	workdir_iterator *wi = (workdir_iterator *)self;
	workdir_iterator_frame *wf;
1015
	git_path_with_stat *next;
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	/* given include_trees & autoexpand, we might have to go into a tree */
	if (iterator__do_autoexpand(wi) &&
		wi->entry.path != NULL &&
		wi->entry.mode == GIT_FILEMODE_TREE)
	{
		error = workdir_iterator__advance_into(entry, self);

		/* continue silently past empty directories if autoexpanding */
		if (error != GIT_ENOTFOUND)
			return error;
		giterr_clear();
		error = 0;
	}

1031
	if (entry != NULL)
1032 1033
		*entry = NULL;

1034
	while (wi->entry.path != NULL) {
1035
		wf   = wi->stack;
1036
		next = git_vector_get(&wf->entries, ++wf->index);
1037

1038
		if (next != NULL) {
1039
			/* match git's behavior of ignoring anything named ".git" */
1040
			if (path_is_dotgit(next))
1041 1042 1043 1044 1045
				continue;
			/* else found a good entry */
			break;
		}

1046 1047
		/* pop stack if anything is left to pop */
		if (!wf->next) {
1048
			memset(&wi->entry, 0, sizeof(wi->entry));
1049
			return 0;
1050
		}
1051 1052

		wi->stack = wf->next;
1053
		wi->depth--;
1054 1055
		workdir_iterator__free_frame(wf);
		git_ignore__pop_dir(&wi->ignores);
1056 1057
	}

1058
	error = workdir_iterator__update_entry(wi);
1059

1060
	if (!error && entry != NULL)
1061
		error = workdir_iterator__current(entry, self);
1062 1063

	return error;
1064 1065
}

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
static int workdir_iterator__seek(git_iterator *self, const char *prefix)
{
	GIT_UNUSED(self);
	GIT_UNUSED(prefix);
	/* pop stack until matching prefix */
	/* find prefix item in current frame */
	/* push subdirectories as deep as possible while matching */
	return 0;
}

1076 1077
static int workdir_iterator__reset(
	git_iterator *self, const char *start, const char *end)
1078 1079
{
	workdir_iterator *wi = (workdir_iterator *)self;
1080

1081 1082 1083 1084 1085 1086
	while (wi->stack != NULL && wi->stack->next != NULL) {
		workdir_iterator_frame *wf = wi->stack;
		wi->stack = wf->next;
		workdir_iterator__free_frame(wf);
		git_ignore__pop_dir(&wi->ignores);
	}
1087
	wi->depth = 0;
1088 1089 1090 1091 1092 1093 1094

	if (iterator__reset_range(self, start, end) < 0)
		return -1;

	workdir_iterator__seek_frame_start(wi, wi->stack);

	return workdir_iterator__update_entry(wi);
1095 1096
}

1097
static void workdir_iterator__free(git_iterator *self)
1098
{
1099
	workdir_iterator *wi = (workdir_iterator *)self;
1100

1101 1102 1103 1104
	while (wi->stack != NULL) {
		workdir_iterator_frame *wf = wi->stack;
		wi->stack = wf->next;
		workdir_iterator__free_frame(wf);
1105 1106 1107 1108 1109 1110
	}

	git_ignore__free(&wi->ignores);
	git_buf_free(&wi->path);
}

1111
static int workdir_iterator__update_entry(workdir_iterator *wi)
1112
{
1113
	int error = 0;
1114 1115
	git_path_with_stat *ps =
		git_vector_get(&wi->stack->entries, wi->stack->index);
1116

1117
	git_buf_truncate(&wi->path, wi->root_len);
1118 1119 1120 1121 1122
	memset(&wi->entry, 0, sizeof(wi->entry));

	if (!ps)
		return 0;

1123 1124 1125 1126
	/* skip over .git entries */
	if (path_is_dotgit(ps))
		return workdir_iterator__advance(NULL, (git_iterator *)wi);

1127 1128
	if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0)
		return -1;
1129

1130
	if (iterator__past_end(wi, wi->path.ptr + wi->root_len))
1131 1132
		return 0;

1133
	wi->entry.path = ps->path;
1134

1135
	wi->is_ignored = -1;
1136

1137
	git_index_entry__init_from_stat(&wi->entry, &ps->st);
1138 1139

	/* need different mode here to keep directories during iteration */
1140
	wi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
1141 1142

	/* if this is a file type we don't handle, treat as ignored */
1143 1144
	if (wi->entry.mode == 0) {
		wi->is_ignored = 1;
1145
		return 0;
1146
	}
1147

1148 1149 1150 1151
	/* if this isn't a tree, then we're done */
	if (wi->entry.mode != GIT_FILEMODE_TREE)
		return 0;

1152
	/* detect submodules */
1153 1154 1155 1156
	error = git_submodule_lookup(NULL, wi->base.repo, wi->entry.path);
	if (error == GIT_ENOTFOUND)
		giterr_clear();

1157 1158 1159
	if (error == GIT_EEXISTS) /* if contains .git, treat as untracked submod */
		error = 0;

1160 1161 1162 1163 1164 1165
	/* if submodule, mark as GITLINK and remove trailing slash */
	if (!error) {
		size_t len = strlen(wi->entry.path);
		assert(wi->entry.path[len - 1] == '/');
		wi->entry.path[len - 1] = '\0';
		wi->entry.mode = S_IFGITLINK;
1166
		return 0;
Russell Belfer committed
1167
	}
1168

1169 1170 1171
	if (iterator__include_trees(wi))
		return 0;

1172
	return workdir_iterator__advance(NULL, (git_iterator *)wi);
1173 1174
}

1175
int git_iterator_for_workdir(
1176 1177
	git_iterator **iter,
	git_repository *repo,
1178
	git_iterator_flag_t flags,
1179 1180
	const char *start,
	const char *end)
1181 1182
{
	int error;
1183
	workdir_iterator *wi;
1184

1185 1186
	assert(iter && repo);

1187
	if ((error = git_repository__ensure_not_bare(
1188
			 repo, "scan working directory")) < 0)
1189
		return error;
1190

1191
	ITERATOR_BASE_INIT(wi, workdir, WORKDIR, repo);
1192

1193
	if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0)
1194
		goto fail;
1195

1196 1197 1198 1199
	if (git_buf_sets(&wi->path, git_repository_workdir(repo)) < 0 ||
		git_path_to_dir(&wi->path) < 0 ||
		git_ignore__for_path(repo, "", &wi->ignores) < 0)
	{
1200
		git__free(wi);
1201
		return -1;
1202 1203 1204
	}
	wi->root_len = wi->path.size;

1205
	if ((error = workdir_iterator__expand_dir(wi)) < 0) {
1206 1207 1208
		if (error != GIT_ENOTFOUND)
			goto fail;
		giterr_clear();
1209 1210 1211
	}

	*iter = (git_iterator *)wi;
1212
	return 0;
1213

1214 1215
fail:
	git_iterator_free((git_iterator *)wi);
1216 1217 1218
	return error;
}

1219

1220
void git_iterator_free(git_iterator *iter)
1221
{
1222
	if (iter == NULL)
1223 1224
		return;

1225
	iter->cb->free(iter);
1226

1227 1228
	git__free(iter->start);
	git__free(iter->end);
1229

1230 1231 1232
	memset(iter, 0, sizeof(*iter));

	git__free(iter);
1233 1234
}

1235
int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
1236
{
1237
	bool desire_ignore_case  = (ignore_case != 0);
1238

1239
	if (iterator__ignore_case(iter) == desire_ignore_case)
1240
		return 0;
1241

1242
	if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
1243 1244 1245 1246 1247 1248 1249 1250
		if (desire_ignore_case)
			iter->flags |= GIT_ITERATOR_IGNORE_CASE;
		else
			iter->flags &= ~GIT_ITERATOR_IGNORE_CASE;
	} else {
		giterr_set(GITERR_INVALID,
			"Cannot currently set ignore case on non-empty iterators");
		return -1;
1251 1252 1253
	}

	return 0;
1254 1255
}

1256
git_index *git_iterator_get_index(git_iterator *iter)
1257
{
1258
	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
1259 1260 1261 1262
		return ((index_iterator *)iter)->index;
	return NULL;
}

1263
int git_iterator_current_tree_entry(
1264
	const git_tree_entry **tree_entry, git_iterator *iter)
1265
{
1266 1267 1268
	if (iter->type != GIT_ITERATOR_TYPE_TREE)
		*tree_entry = NULL;
	else {
1269
		tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
1270 1271
		*tree_entry = (tf->current < tf->n_entries) ?
			tf->entries[tf->current]->te : NULL;
1272 1273
	}

1274
	return 0;
1275 1276
}

1277
int git_iterator_current_parent_tree(
1278
	const git_tree **tree_ptr,
1279
	git_iterator *iter,
1280
	const char *parent_path)
1281 1282 1283 1284
{
	tree_iterator *ti = (tree_iterator *)iter;
	tree_iterator_frame *tf;
	const char *scan = parent_path;
1285
	const git_tree_entry *te;
1286

1287
	*tree_ptr = NULL;
1288

1289 1290
	if (iter->type != GIT_ITERATOR_TYPE_TREE)
		return 0;
1291

1292 1293 1294 1295
	for (tf = ti->root; *scan; ) {
		if (!(tf = tf->down) ||
			tf->current >= tf->n_entries ||
			!(te = tf->entries[tf->current]->te) ||
1296
			ti->strncomp(scan, te->filename, te->filename_len) != 0)
1297 1298 1299
			return 0;

		scan += te->filename_len;
1300
		if (*scan == '/')
1301 1302 1303
			scan++;
	}

1304
	*tree_ptr = tf->entries[tf->current]->tree;
1305 1306 1307
	return 0;
}

1308
bool git_iterator_current_is_ignored(git_iterator *iter)
1309
{
1310 1311
	workdir_iterator *wi = (workdir_iterator *)iter;

1312
	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
1313
		return false;
1314 1315

	if (wi->is_ignored != -1)
1316
		return (bool)(wi->is_ignored != 0);
1317 1318

	if (git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored) < 0)
1319
		wi->is_ignored = true;
1320

1321
	return (bool)wi->is_ignored;
1322 1323
}

1324
int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
1325 1326 1327 1328
{
	const git_index_entry *entry;

	/* a "done" iterator is after every prefix */
1329
	if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
1330 1331 1332 1333 1334 1335
		return 1;

	/* a NULL prefix is after any valid iterator */
	if (!path_prefix)
		return -1;

1336
	return iter->prefixcomp(entry->path, path_prefix);
1337 1338
}

1339
int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
1340 1341 1342
{
	workdir_iterator *wi = (workdir_iterator *)iter;

1343
	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->entry.path)
1344 1345 1346 1347 1348 1349
		*path = NULL;
	else
		*path = &wi->path;

	return 0;
}