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

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

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

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

44 45 46 47 48
#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)
49

50 51 52
#define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)

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


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);
	}

75 76
	iter->flags &= ~GIT_ITERATOR_FIRST_ACCESS;

77 78
	return 0;
}
79

80
static int iterator__update_ignore_case(
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	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);

102
	iter->prefixcomp = iterator__ignore_case(iter) ?
103 104
		git__prefixcmp_icase : git__prefixcmp;

105 106 107
	return error;
}

108
GIT_INLINE(void) iterator__clear_entry(const git_index_entry **entry)
109
{
110
	if (entry) *entry = NULL;
111 112 113 114 115 116 117
}


static int empty_iterator__noop(const git_index_entry **e, git_iterator *i)
{
	GIT_UNUSED(i);
	iterator__clear_entry(e);
118
	return GIT_ITEROVER;
119 120
}

121
static int empty_iterator__seek(git_iterator *i, const char *p)
122
{
123
	GIT_UNUSED(i); GIT_UNUSED(p);
124 125 126
	return -1;
}

127
static int empty_iterator__reset(git_iterator *i, const char *s, const char *e)
128
{
129
	GIT_UNUSED(i); GIT_UNUSED(s); GIT_UNUSED(e);
130 131 132
	return 0;
}

133
static int empty_iterator__at_end(git_iterator *i)
134
{
135
	GIT_UNUSED(i);
136 137 138
	return 1;
}

139
static void empty_iterator__free(git_iterator *i)
140
{
141
	GIT_UNUSED(i);
142 143
}

144 145 146 147 148
typedef struct {
	git_iterator base;
	git_iterator_callbacks cb;
} empty_iterator;

149 150 151 152 153
int git_iterator_for_nothing(
	git_iterator **iter,
	git_iterator_flag_t flags,
	const char *start,
	const char *end)
154
{
155 156
	empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
	GITERR_CHECK_ALLOC(i);
157

158 159
#define empty_iterator__current empty_iterator__noop
#define empty_iterator__advance empty_iterator__noop
160
#define empty_iterator__advance_into empty_iterator__noop
161

162
	ITERATOR_BASE_INIT(i, empty, EMPTY, NULL);
163 164 165

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

167
	*iter = (git_iterator *)i;
168 169 170
	return 0;
}

171

172 173 174 175 176 177
typedef struct tree_iterator_entry tree_iterator_entry;
struct tree_iterator_entry {
	tree_iterator_entry *parent;
	const git_tree_entry *te;
	git_tree *tree;
};
178

179 180
typedef struct tree_iterator_frame tree_iterator_frame;
struct tree_iterator_frame {
181
	tree_iterator_frame *up, *down;
182 183 184 185 186

	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 */

187
	const char *start;
188
	size_t startlen;
189

190
	tree_iterator_entry *entries[GIT_FLEX_ARRAY];
191
};
192 193

typedef struct {
194
	git_iterator base;
195
	git_iterator_callbacks cb;
196 197
	tree_iterator_frame *head, *root;
	git_pool pool;
198 199
	git_index_entry entry;
	git_buf path;
200
	int path_ambiguities;
201
	bool path_has_filename;
202
	bool entry_is_current;
203
	int (*strncomp)(const char *a, const char *b, size_t sz);
204
} tree_iterator;
205

206 207 208 209 210 211
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;
212 213 214 215

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

216 217 218 219 220 221
		ti->path_has_filename = true;
	}

	return ti->path.ptr;
}

222
static void tree_iterator__rewrite_filename(tree_iterator *ti)
223
{
224
	tree_iterator_entry *scan = ti->head->entries[ti->head->current];
225 226
	ssize_t strpos = ti->path.size;
	const git_tree_entry *te;
227

228 229 230
	if (strpos && ti->path.ptr[strpos - 1] == '/')
		strpos--;

231
	for (; scan && (te = scan->te); scan = scan->parent) {
232 233 234
		strpos -= te->filename_len;
		memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
		strpos -= 1; /* separator */
235
	}
236
}
237

238
static int tree_iterator__te_cmp(
239 240
	const git_tree_entry *a,
	const git_tree_entry *b,
241
	int (*compare)(const char *, const char *, size_t))
242
{
243 244 245 246
	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);
247 248
}

249
static int tree_iterator__ci_cmp(const void *a, const void *b, void *p)
250
{
251
	const tree_iterator_entry *ae = a, *be = b;
252
	int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp);
253 254

	if (!cmp) {
255 256 257 258 259
		/* 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);
260 261 262
	}

	return cmp;
263 264
}

265 266 267 268 269 270 271 272 273 274 275
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);
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static bool tree_iterator__move_to_next(
	tree_iterator *ti, tree_iterator_frame *tf)
{
	if (tf->next > tf->current + 1)
		ti->path_ambiguities--;

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

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

	return (tf->current < tf->n_entries);
}

295
static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
296
{
297
	int error = 0;
298
	const git_tree_entry *te, *last = NULL;
299

300
	tf->next = tf->current;
301

302 303 304 305
	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))
306
			break;
307

308 309 310 311
		/* try to load trees for items in [current,next) range */
		if (!error && git_tree_entry__is_tree(te))
			error = git_tree_lookup(
				&tf->entries[tf->next]->tree, ti->base.repo, &te->oid);
312
	}
313

314 315 316
	if (tf->next > tf->current + 1)
		ti->path_ambiguities++;

317 318 319 320 321 322
	/* if a tree lookup failed, advance over this span and return failure */
	if (error < 0) {
		tree_iterator__move_to_next(ti, tf);
		return error;
	}

323
	if (last && !tree_iterator__current_filename(ti, last))
324
		return -1; /* must have been allocation failure */
325

326
	return 0;
327 328
}

329
GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
330
{
331
	return (ti->head->current < ti->head->n_entries &&
332
			ti->head->entries[ti->head->current]->tree != NULL);
333 334
}

335
static int tree_iterator__push_frame(tree_iterator *ti)
336
{
337
	int error = 0;
338
	tree_iterator_frame *head = ti->head, *tf = NULL;
339
	size_t i, n_entries = 0, alloclen;
340

341
	if (head->current >= head->n_entries || !head->entries[head->current]->tree)
342
		return GIT_ITEROVER;
343

344 345 346
	for (i = head->current; i < head->next; ++i)
		n_entries += git_tree_entrycount(head->entries[i]->tree);

347 348
	GITERR_CHECK_ALLOC_MULTIPLY(&alloclen, sizeof(tree_iterator_entry *), n_entries);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, sizeof(tree_iterator_frame));
349 350

	tf = git__calloc(1, alloclen);
351 352 353
	GITERR_CHECK_ALLOC(tf);

	tf->n_entries = n_entries;
354

355 356 357
	tf->up     = head;
	head->down = tf;
	ti->head   = tf;
358

359 360
	for (i = head->current, n_entries = 0; i < head->next; ++i) {
		git_tree *tree = head->entries[i]->tree;
361 362 363
		size_t j, max_j = git_tree_entrycount(tree);

		for (j = 0; j < max_j; ++j) {
364 365 366 367 368 369 370 371
			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;
372 373
		}
	}
374

375 376
	/* if ignore_case, sort entries case insensitively */
	if (iterator__ignore_case(ti))
377 378 379 380 381 382 383 384 385 386 387
		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--;
388

389 390 391
		if ((tf->start = strchr(head->start, '/')) != NULL) {
			tf->start++;
			tf->startlen = strlen(tf->start);
392
		}
393 394
	}

395
	ti->path_has_filename = ti->entry_is_current = false;
396

397
	if ((error = tree_iterator__set_next(ti, tf)) < 0)
398
		return error;
399

400
	/* autoexpand as needed */
401 402
	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
		return tree_iterator__push_frame(ti);
403

404 405
	return 0;
}
406

407
static bool tree_iterator__pop_frame(tree_iterator *ti, bool final)
408
{
409
	tree_iterator_frame *tf = ti->head;
410

411
	if (!tf->up)
412
		return false;
413

414 415 416
	ti->head = tf->up;
	ti->head->down = NULL;

417
	tree_iterator__move_to_next(ti, tf);
418

419 420 421 422
	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, '/');
	}
423

424
	git__free(tf);
425

426 427
	return true;
}
428

429
static void tree_iterator__pop_all(tree_iterator *ti, bool to_end, bool final)
430 431 432 433 434 435 436 437 438 439
{
	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);
	}
}

440
static int tree_iterator__update_entry(tree_iterator *ti)
441
{
442 443
	tree_iterator_frame *tf;
    const git_tree_entry *te;
444

445 446
	if (ti->entry_is_current)
        return 0;
447

448 449
	tf = ti->head;
    te = tf->entries[tf->current]->te;
450

451
	ti->entry.mode = te->attr;
452
	git_oid_cpy(&ti->entry.id, &te->oid);
453 454

	ti->entry.path = tree_iterator__current_filename(ti, te);
455
	GITERR_CHECK_ALLOC(ti->entry.path);
456

457 458 459
	if (ti->path_ambiguities > 0)
		tree_iterator__rewrite_filename(ti);

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	if (iterator__past_end(ti, ti->entry.path)) {
		tree_iterator__pop_all(ti, true, false);
		return GIT_ITEROVER;
	}

	ti->entry_is_current = true;

	return 0;
}

static int tree_iterator__current(
	const git_index_entry **entry, git_iterator *self)
{
	int error;
	tree_iterator *ti = (tree_iterator *)self;
	tree_iterator_frame *tf = ti->head;

	iterator__clear_entry(entry);

	if (tf->current >= tf->n_entries)
		return GIT_ITEROVER;

	if ((error = tree_iterator__update_entry(ti)) < 0)
		return error;
484

485 486 487
	if (entry)
		*entry = &ti->entry;

488 489
	ti->base.flags |= GIT_ITERATOR_FIRST_ACCESS;

490
	return 0;
491 492
}

493 494 495 496 497 498
static int tree_iterator__advance_into(
	const git_index_entry **entry, git_iterator *self)
{
	int error = 0;
	tree_iterator *ti = (tree_iterator *)self;

499
	iterator__clear_entry(entry);
500

501 502 503 504
	if (tree_iterator__at_tree(ti))
		error = tree_iterator__push_frame(ti);

	if (!error && entry)
505 506 507 508 509
		error = tree_iterator__current(entry, self);

	return error;
}

510
static int tree_iterator__advance(
511
	const git_index_entry **entry, git_iterator *self)
512
{
513
	int error;
514
	tree_iterator *ti = (tree_iterator *)self;
515
	tree_iterator_frame *tf = ti->head;
516

517
	iterator__clear_entry(entry);
518

519 520 521 522 523
	if (tf->current >= tf->n_entries)
		return GIT_ITEROVER;

	if (!iterator__has_been_accessed(ti))
		return tree_iterator__current(entry, self);
524 525 526

	if (iterator__do_autoexpand(ti) && iterator__include_trees(ti) &&
		tree_iterator__at_tree(ti))
527 528
		return tree_iterator__advance_into(entry, self);

529
	if (ti->path_has_filename) {
530
		git_buf_rtruncate_at_char(&ti->path, '/');
531
		ti->path_has_filename = ti->entry_is_current = false;
532
	}
533

534
	/* scan forward and up, advancing in frame or popping frame when done */
535 536
	while (!tree_iterator__move_to_next(ti, tf) &&
		   tree_iterator__pop_frame(ti, false))
537
		tf = ti->head;
538

539 540 541
	/* find next and load trees */
	if ((error = tree_iterator__set_next(ti, tf)) < 0)
		return error;
542

543 544
	/* deal with include_trees / auto_expand as needed */
	if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
545
		return tree_iterator__advance_into(entry, self);
546

547
	return tree_iterator__current(entry, self);
548 549
}

550 551
static int tree_iterator__seek(git_iterator *self, const char *prefix)
{
552
	GIT_UNUSED(self); GIT_UNUSED(prefix);
553 554 555
	return -1;
}

556 557
static int tree_iterator__reset(
	git_iterator *self, const char *start, const char *end)
558
{
559
	tree_iterator *ti = (tree_iterator *)self;
560

561
	tree_iterator__pop_all(ti, false, false);
562

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

566
	return tree_iterator__push_frame(ti); /* re-expand root tree */
567 568
}

569
static int tree_iterator__at_end(git_iterator *self)
570 571
{
	tree_iterator *ti = (tree_iterator *)self;
572 573
	return (ti->head->current >= ti->head->n_entries);
}
574

575 576 577
static void tree_iterator__free(git_iterator *self)
{
	tree_iterator *ti = (tree_iterator *)self;
578

579
	tree_iterator__pop_all(ti, true, false);
580

581 582 583
	git_tree_free(ti->head->entries[0]->tree);
	git__free(ti->head);
	git_pool_clear(&ti->pool);
584
	git_buf_free(&ti->path);
585 586
}

587
static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree)
588 589
{
	size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
590 591
	tree_iterator_frame *root = git__calloc(sz, sizeof(char));
	GITERR_CHECK_ALLOC(root);
592

593 594 595 596 597 598 599
	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;
600

601
	ti->head = ti->root = root;
602 603 604 605

	return 0;
}

606
int git_iterator_for_tree(
607 608
	git_iterator **iter,
	git_tree *tree,
609
	git_iterator_flag_t flags,
610 611
	const char *start,
	const char *end)
612 613
{
	int error;
614 615 616
	tree_iterator *ti;

	if (tree == NULL)
617
		return git_iterator_for_nothing(iter, flags, start, end);
618

619
	if ((error = git_object_dup((git_object **)&tree, (git_object *)tree)) < 0)
620 621
		return error;

622 623 624
	ti = git__calloc(1, sizeof(tree_iterator));
	GITERR_CHECK_ALLOC(ti);

625
	ITERATOR_BASE_INIT(ti, tree, TREE, git_tree_owner(tree));
626

627
	if ((error = iterator__update_ignore_case((git_iterator *)ti, flags)) < 0)
628
		goto fail;
629
	ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
630

631 632 633
	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 */
634 635 636 637
		goto fail;

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

639 640
fail:
	git_iterator_free((git_iterator *)ti);
641 642 643 644 645
	return error;
}


typedef struct {
646
	git_iterator base;
647
	git_iterator_callbacks cb;
648
	git_index *index;
649 650
	git_vector entries;
	git_vector_cmp entry_srch;
651
	size_t current;
652 653 654 655 656
	/* 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;
657
} index_iterator;
658

659 660
static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
{
661
	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
662 663

	if (ie != NULL && iterator__past_end(ii, ie->path)) {
664
		ii->current = git_vector_length(&ii->entries);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		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)

729
static int index_iterator__current(
730
	const git_index_entry **entry, git_iterator *self)
731
{
732
	index_iterator *ii = (index_iterator *)self;
733
	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
734

735 736 737 738 739
	if (ie != NULL && index_iterator__at_tree(ii)) {
		ii->tree_entry.path = ii->partial.ptr;
		ie = &ii->tree_entry;
	}

740 741 742
	if (entry)
		*entry = ie;

743 744 745
	ii->base.flags |= GIT_ITERATOR_FIRST_ACCESS;

	return (ie != NULL) ? 0 : GIT_ITEROVER;
746 747
}

748
static int index_iterator__at_end(git_iterator *self)
749
{
750
	index_iterator *ii = (index_iterator *)self;
751
	return (ii->current >= git_vector_length(&ii->entries));
752 753
}

754 755
static int index_iterator__advance(
	const git_index_entry **entry, git_iterator *self)
756
{
757
	index_iterator *ii = (index_iterator *)self;
758
	size_t entrycount = git_vector_length(&ii->entries);
759 760
	const git_index_entry *ie;

761 762 763
	if (!iterator__has_been_accessed(ii))
		return index_iterator__current(entry, self);

764 765 766 767 768
	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 {
769
			/* advance to sibling tree (i.e. find entry with new prefix) */
770 771 772
			while (ii->current < entrycount) {
				ii->current++;

773
				if (!(ie = git_vector_get(&ii->entries, ii->current)) ||
774 775 776
					ii->base.prefixcomp(ie->path, ii->partial.ptr) != 0)
					break;
			}
777

778 779
			if (index_iterator__first_prefix_tree(ii) < 0)
				return -1;
780
		}
781 782 783
	} else {
		if (ii->current < entrycount)
			ii->current++;
784

785 786
		if (index_iterator__first_prefix_tree(ii) < 0)
			return -1;
787
	}
788 789

	return index_iterator__current(entry, self);
790 791
}

792
static int index_iterator__advance_into(
793
	const git_index_entry **entry, git_iterator *self)
794
{
795
	index_iterator *ii = (index_iterator *)self;
796
	const git_index_entry *ie = git_vector_get(&ii->entries, ii->current);
797

798 799 800 801 802
	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);
	}
803

804
	return index_iterator__current(entry, self);
805 806 807 808
}

static int index_iterator__seek(git_iterator *self, const char *prefix)
{
809
	GIT_UNUSED(self); GIT_UNUSED(prefix);
810
	return -1;
811 812
}

813 814
static int index_iterator__reset(
	git_iterator *self, const char *start, const char *end)
815 816
{
	index_iterator *ii = (index_iterator *)self;
817
	const git_index_entry *ie;
818

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

822 823 824
	ii->current = 0;

	if (ii->base.start)
825
		git_index_snapshot_find(
826
			&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
827

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	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);
844

845
	return 0;
846 847
}

848
static void index_iterator__free(git_iterator *self)
849
{
850
	index_iterator *ii = (index_iterator *)self;
851
	git_index_snapshot_release(&ii->entries, ii->index);
852
	ii->index = NULL;
853
	git_buf_free(&ii->partial);
854 855
}

856
int git_iterator_for_index(
857
	git_iterator **iter,
858
	git_index  *index,
859
	git_iterator_flag_t flags,
860 861
	const char *start,
	const char *end)
862
{
863
	int error = 0;
864 865
	index_iterator *ii = git__calloc(1, sizeof(index_iterator));
	GITERR_CHECK_ALLOC(ii);
866

867
	if ((error = git_index_snapshot_new(&ii->entries, index)) < 0) {
868 869 870 871 872
		git__free(ii);
		return error;
	}
	ii->index = index;

873
	ITERATOR_BASE_INIT(ii, index, INDEX, git_index_owner(index));
874

875 876 877
	if ((error = iterator__update_ignore_case((git_iterator *)ii, flags)) < 0) {
		git_iterator_free((git_iterator *)ii);
		return error;
878
	}
879

880 881 882 883 884 885
	ii->entry_srch = iterator__ignore_case(ii) ?
		git_index_entry_isrch : git_index_entry_srch;

	git_vector_set_cmp(&ii->entries, iterator__ignore_case(ii) ?
		git_index_entry_icmp : git_index_entry_cmp);
	git_vector_sort(&ii->entries);
886

887 888 889
	git_buf_init(&ii->partial, 0);
	ii->tree_entry.mode = GIT_FILEMODE_TREE;

890
	index_iterator__reset((git_iterator *)ii, NULL, NULL);
891

892 893
	*iter = (git_iterator *)ii;
	return 0;
894 895 896
}


897 898 899 900 901
typedef struct fs_iterator_frame fs_iterator_frame;
struct fs_iterator_frame {
	fs_iterator_frame *next;
	git_vector entries;
	size_t index;
902
	int is_ignored;
903 904
};

905 906
typedef struct fs_iterator fs_iterator;
struct fs_iterator {
907 908 909 910 911 912
	git_iterator base;
	git_iterator_callbacks cb;
	fs_iterator_frame *stack;
	git_index_entry entry;
	git_buf path;
	size_t root_len;
913
	uint32_t dirload_flags;
914
	int depth;
915

916 917 918
	int (*enter_dir_cb)(fs_iterator *self);
	int (*leave_dir_cb)(fs_iterator *self);
	int (*update_entry_cb)(fs_iterator *self);
919
};
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

#define FS_MAX_DEPTH 100

static fs_iterator_frame *fs_iterator__alloc_frame(fs_iterator *fi)
{
	fs_iterator_frame *ff = git__calloc(1, sizeof(fs_iterator_frame));
	git_vector_cmp entry_compare = CASESELECT(
		iterator__ignore_case(fi),
		git_path_with_stat_cmp_icase, git_path_with_stat_cmp);

	if (ff && git_vector_init(&ff->entries, 0, entry_compare) < 0) {
		git__free(ff);
		ff = NULL;
	}

	return ff;
}

938
static void fs_iterator__free_frame(fs_iterator_frame *ff)
939
{
940
	git_vector_free_deep(&ff->entries);
941
	git__free(ff);
942
}
943

944 945 946 947 948 949 950 951 952 953 954
static void fs_iterator__pop_frame(
	fs_iterator *fi, fs_iterator_frame *ff, bool pop_last)
{
	if (fi && fi->stack == ff) {
		if (!ff->next && !pop_last) {
			memset(&fi->entry, 0, sizeof(fi->entry));
			return;
		}

		if (fi->leave_dir_cb)
			(void)fi->leave_dir_cb(fi);
955

956 957 958
		fi->stack = ff->next;
		fi->depth--;
	}
959

960
	fs_iterator__free_frame(ff);
961 962 963
}

static int fs_iterator__update_entry(fs_iterator *fi);
964 965
static int fs_iterator__advance_over(
	const git_index_entry **entry, git_iterator *self);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

static int fs_iterator__entry_cmp(const void *i, const void *item)
{
	const fs_iterator *fi = (const fs_iterator *)i;
	const git_path_with_stat *ps = item;
	return fi->base.prefixcomp(fi->base.start, ps->path);
}

static void fs_iterator__seek_frame_start(
	fs_iterator *fi, fs_iterator_frame *ff)
{
	if (!ff)
		return;

	if (fi->base.start)
		git_vector_bsearch2(
			&ff->index, &ff->entries, fs_iterator__entry_cmp, fi);
	else
		ff->index = 0;
}

static int fs_iterator__expand_dir(fs_iterator *fi)
{
	int error;
	fs_iterator_frame *ff;

992 993 994 995 996 997
	if (fi->depth > FS_MAX_DEPTH) {
		giterr_set(GITERR_REPOSITORY,
			"Directory nesting is too deep (%d)", fi->depth);
		return -1;
	}

998 999 1000 1001
	ff = fs_iterator__alloc_frame(fi);
	GITERR_CHECK_ALLOC(ff);

	error = git_path_dirload_with_stat(
1002
		fi->path.ptr, fi->root_len, fi->dirload_flags,
1003 1004
		fi->base.start, fi->base.end, &ff->entries);

1005
	if (error < 0) {
1006 1007
		git_error_state last_error = { 0 };
		giterr_capture(&last_error, error);
1008 1009

		/* these callbacks may clear the error message */
1010 1011
		fs_iterator__free_frame(ff);
		fs_iterator__advance_over(NULL, (git_iterator *)fi);
1012 1013 1014
		/* next time return value we skipped to */
		fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;

1015
		return giterr_restore(&last_error);
1016 1017 1018
	}

	if (ff->entries.length == 0) {
1019
		fs_iterator__free_frame(ff);
1020 1021
		return GIT_ENOTFOUND;
	}
1022
	fi->base.stat_calls += ff->entries.length;
1023

1024 1025 1026 1027
	fs_iterator__seek_frame_start(fi, ff);

	ff->next  = fi->stack;
	fi->stack = ff;
1028
	fi->depth++;
1029

1030
	if (fi->enter_dir_cb && (error = fi->enter_dir_cb(fi)) < 0)
1031 1032
		return error;

1033 1034 1035 1036 1037 1038 1039
	return fs_iterator__update_entry(fi);
}

static int fs_iterator__current(
	const git_index_entry **entry, git_iterator *self)
{
	fs_iterator *fi = (fs_iterator *)self;
1040 1041
	const git_index_entry *fe = (fi->entry.path == NULL) ? NULL : &fi->entry;

1042
	if (entry)
1043 1044 1045 1046 1047
		*entry = fe;

	fi->base.flags |= GIT_ITERATOR_FIRST_ACCESS;

	return (fe != NULL) ? 0 : GIT_ITEROVER;
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
}

static int fs_iterator__at_end(git_iterator *self)
{
	return (((fs_iterator *)self)->entry.path == NULL);
}

static int fs_iterator__advance_into(
	const git_index_entry **entry, git_iterator *iter)
{
	int error = 0;
	fs_iterator *fi = (fs_iterator *)iter;

	iterator__clear_entry(entry);

	/* Allow you to explicitly advance into a commit/submodule (as well as a
	 * tree) to avoid cases where an entry is mislabeled as a submodule in
	 * the working directory.  The fs iterator will never have COMMMIT
	 * entries on it's own, but a wrapper might add them.
	 */
	if (fi->entry.path != NULL &&
		(fi->entry.mode == GIT_FILEMODE_TREE ||
		 fi->entry.mode == GIT_FILEMODE_COMMIT))
		/* returns GIT_ENOTFOUND if the directory is empty */
		error = fs_iterator__expand_dir(fi);

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

1077 1078 1079
	if (!error && !fi->entry.path)
		error = GIT_ITEROVER;

1080 1081 1082
	return error;
}

1083
static int fs_iterator__advance_over(
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	const git_index_entry **entry, git_iterator *self)
{
	int error = 0;
	fs_iterator *fi = (fs_iterator *)self;
	fs_iterator_frame *ff;
	git_path_with_stat *next;

	if (entry != NULL)
		*entry = NULL;

	while (fi->entry.path != NULL) {
		ff   = fi->stack;
		next = git_vector_get(&ff->entries, ++ff->index);

		if (next != NULL)
			break;

1101
		fs_iterator__pop_frame(fi, ff, false);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	}

	error = fs_iterator__update_entry(fi);

	if (!error && entry != NULL)
		error = fs_iterator__current(entry, self);

	return error;
}

1112 1113 1114 1115 1116
static int fs_iterator__advance(
	const git_index_entry **entry, git_iterator *self)
{
	fs_iterator *fi = (fs_iterator *)self;

1117 1118 1119
	if (!iterator__has_been_accessed(fi))
		return fs_iterator__current(entry, self);

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	/* given include_trees & autoexpand, we might have to go into a tree */
	if (iterator__do_autoexpand(fi) &&
		fi->entry.path != NULL &&
		fi->entry.mode == GIT_FILEMODE_TREE)
	{
		int error = fs_iterator__advance_into(entry, self);
		if (error != GIT_ENOTFOUND)
			return error;
		/* continue silently past empty directories if autoexpanding */
		giterr_clear();
	}

	return fs_iterator__advance_over(entry, self);
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static int fs_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;
}

static int fs_iterator__reset(
	git_iterator *self, const char *start, const char *end)
{
1148
	int error;
1149 1150
	fs_iterator *fi = (fs_iterator *)self;

1151 1152
	while (fi->stack != NULL && fi->stack->next != NULL)
		fs_iterator__pop_frame(fi, fi->stack, false);
1153 1154
	fi->depth = 0;

1155 1156
	if ((error = iterator__reset_range(self, start, end)) < 0)
		return error;
1157 1158 1159

	fs_iterator__seek_frame_start(fi, fi->stack);

1160 1161 1162 1163 1164
	error = fs_iterator__update_entry(fi);
	if (error == GIT_ITEROVER)
		error = 0;

	return error;
1165 1166 1167 1168 1169 1170
}

static void fs_iterator__free(git_iterator *self)
{
	fs_iterator *fi = (fs_iterator *)self;

1171 1172
	while (fi->stack != NULL)
		fs_iterator__pop_frame(fi, fi->stack, true);
1173 1174 1175 1176 1177 1178

	git_buf_free(&fi->path);
}

static int fs_iterator__update_entry(fs_iterator *fi)
{
1179
	git_path_with_stat *ps;
1180 1181 1182

	memset(&fi->entry, 0, sizeof(fi->entry));

1183 1184 1185 1186
	if (!fi->stack)
		return GIT_ITEROVER;

	ps = git_vector_get(&fi->stack->entries, fi->stack->index);
1187
	if (!ps)
1188 1189 1190
		return GIT_ITEROVER;

	git_buf_truncate(&fi->path, fi->root_len);
1191 1192
	if (git_buf_put(&fi->path, ps->path, ps->path_len) < 0)
		return -1;
1193

1194
	if (iterator__past_end(fi, fi->path.ptr + fi->root_len))
1195
		return GIT_ITEROVER;
1196 1197

	fi->entry.path = ps->path;
1198
	git_index_entry__init_from_stat(&fi->entry, &ps->st, true);
1199 1200 1201 1202

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

1203
	/* allow wrapper to check/update the entry (can force skip) */
1204 1205
	if (fi->update_entry_cb &&
		fi->update_entry_cb(fi) == GIT_ENOTFOUND)
1206
		return fs_iterator__advance_over(NULL, (git_iterator *)fi);
1207

1208
	/* if this is a tree and trees aren't included, then skip */
1209 1210 1211 1212 1213 1214 1215
	if (fi->entry.mode == GIT_FILEMODE_TREE && !iterator__include_trees(fi)) {
		int error = fs_iterator__advance_into(NULL, (git_iterator *)fi);
		if (error != GIT_ENOTFOUND)
			return error;
		giterr_clear();
		return fs_iterator__advance_over(NULL, (git_iterator *)fi);
	}
1216

1217
	return 0;
1218 1219
}

1220 1221
static int fs_iterator__initialize(
	git_iterator **out, fs_iterator *fi, const char *root)
1222 1223 1224 1225 1226 1227 1228 1229 1230
{
	int error;

	if (git_buf_sets(&fi->path, root) < 0 || git_path_to_dir(&fi->path) < 0) {
		git__free(fi);
		return -1;
	}
	fi->root_len = fi->path.size;

1231 1232 1233 1234 1235
	fi->dirload_flags =
		(iterator__ignore_case(fi) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
		(iterator__flag(fi, PRECOMPOSE_UNICODE) ?
			GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);

1236 1237 1238 1239 1240 1241 1242 1243
	if ((error = fs_iterator__expand_dir(fi)) < 0) {
		if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) {
			giterr_clear();
			error = 0;
		} else {
			git_iterator_free((git_iterator *)fi);
			fi = NULL;
		}
1244 1245 1246 1247 1248 1249
	}

	*out = (git_iterator *)fi;
	return error;
}

1250 1251 1252 1253 1254 1255 1256 1257 1258
int git_iterator_for_filesystem(
	git_iterator **out,
	const char *root,
	git_iterator_flag_t flags,
	const char *start,
	const char *end)
{
	fs_iterator *fi = git__calloc(1, sizeof(fs_iterator));
	GITERR_CHECK_ALLOC(fi);
1259

1260 1261 1262 1263 1264 1265 1266
	ITERATOR_BASE_INIT(fi, fs, FS, NULL);

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

	return fs_iterator__initialize(out, fi, root);
}
1267

1268

1269
typedef struct {
1270
	fs_iterator fi;
1271 1272
	git_ignores ignores;
	int is_ignored;
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

	/*
	 * We may have a tree or the index+snapshot to compare against
	 * when checking for submodules.
	 */
	git_tree *tree;
	git_index *index;
	git_vector index_snapshot;
	git_vector_cmp entry_srch;

1283 1284
} workdir_iterator;

1285
GIT_INLINE(bool) workdir_path_is_dotgit(const git_buf *path)
1286
{
1287
	size_t len;
1288

1289 1290
	if (!path || (len = path->size) < 4)
		return false;
1291

1292 1293
	if (path->ptr[len - 1] == '/')
		len--;
1294

1295 1296 1297 1298 1299
	if (tolower(path->ptr[len - 1]) != 't' ||
		tolower(path->ptr[len - 2]) != 'i' ||
		tolower(path->ptr[len - 3]) != 'g' ||
		tolower(path->ptr[len - 4]) != '.')
		return false;
1300

1301
	return (len == 4 || path->ptr[len - 5] == '/');
1302 1303
}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
/**
 * Figure out if an entry is a submodule.
 *
 * We consider it a submodule if the path is listed as a submodule in
 * either the tree or the index.
 */
static int is_submodule(workdir_iterator *wi, git_path_with_stat *ie)
{
	int error, is_submodule = 0;

	if (wi->tree) {
		git_tree_entry *e;

		/* remove the trailing slash for finding */
		ie->path[ie->path_len-1] = '\0';
		error = git_tree_entry_bypath(&e, wi->tree, ie->path);
		ie->path[ie->path_len-1] = '/';
		if (error < 0 && error != GIT_ENOTFOUND)
			return 0;
		if (!error) {
			is_submodule = e->attr == GIT_FILEMODE_COMMIT;
			git_tree_entry_free(e);
		}
	}

	if (!is_submodule && wi->index) {
		git_index_entry *e;
		size_t pos;

		error = git_index_snapshot_find(&pos, &wi->index_snapshot, wi->entry_srch, ie->path, ie->path_len-1, 0);
		if (error < 0 && error != GIT_ENOTFOUND)
			return 0;

		if (!error) {
			e = git_vector_get(&wi->index_snapshot, pos);

			is_submodule = e->mode == GIT_FILEMODE_COMMIT;
		}
	}

	return is_submodule;
}

1347
static int workdir_iterator__enter_dir(fs_iterator *fi)
1348
{
1349
	workdir_iterator *wi = (workdir_iterator *)fi;
1350 1351 1352 1353 1354
	fs_iterator_frame *ff = fi->stack;
	size_t pos;
	git_path_with_stat *entry;
	bool found_submodules = false;

1355 1356 1357 1358 1359 1360 1361 1362
	/* check if this directory is ignored */
	if (git_ignore__lookup(
			&ff->is_ignored, &wi->ignores, fi->path.ptr + fi->root_len) < 0) {
		giterr_clear();
		ff->is_ignored = GIT_IGNORE_NOTFOUND;
	}

	/* if this is not the top level directory... */
1363
	if (ff->next != NULL) {
1364
		ssize_t slash_pos = git_buf_rfind_next(&fi->path, '/');
1365

1366 1367 1368 1369 1370
		/* inherit ignored from parent if no rule specified */
		if (ff->is_ignored <= GIT_IGNORE_NOTFOUND)
			ff->is_ignored = ff->next->is_ignored;

		/* push new ignores for files in this directory */
1371 1372
		(void)git_ignore__push_dir(&wi->ignores, &fi->path.ptr[slash_pos + 1]);
	}
1373

1374 1375
	/* convert submodules to GITLINK and remove trailing slashes */
	git_vector_foreach(&ff->entries, pos, entry) {
1376
		if (!S_ISDIR(entry->st.st_mode) || !strcmp(GIT_DIR, entry->path))
1377 1378
			continue;

1379
		if (is_submodule(wi, entry)) {
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
			entry->st.st_mode = GIT_FILEMODE_COMMIT;
			entry->path_len--;
			entry->path[entry->path_len] = '\0';
			found_submodules = true;
		}
	}

	/* if we renamed submodules, re-sort and re-seek to start */
	if (found_submodules) {
		git_vector_set_sorted(&ff->entries, 0);
		git_vector_sort(&ff->entries);
		fs_iterator__seek_frame_start(fi, ff);
	}

1394
	return 0;
1395 1396
}

1397
static int workdir_iterator__leave_dir(fs_iterator *fi)
1398
{
1399 1400 1401
	workdir_iterator *wi = (workdir_iterator *)fi;
	git_ignore__pop_dir(&wi->ignores);
	return 0;
1402 1403
}

1404
static int workdir_iterator__update_entry(fs_iterator *fi)
1405
{
1406
	workdir_iterator *wi = (workdir_iterator *)fi;
1407

1408 1409 1410
	/* skip over .git entries */
	if (workdir_path_is_dotgit(&fi->path))
		return GIT_ENOTFOUND;
1411

1412
	/* reset is_ignored since we haven't checked yet */
1413
	wi->is_ignored = GIT_IGNORE_UNCHECKED;
1414

1415 1416 1417
	return 0;
}

1418
static void workdir_iterator__free(git_iterator *self)
1419
{
1420
	workdir_iterator *wi = (workdir_iterator *)self;
1421 1422
	if (wi->index)
		git_index_snapshot_release(&wi->index_snapshot, wi->index);
Carlos Martín Nieto committed
1423
	git_tree_free(wi->tree);
1424
	fs_iterator__free(self);
1425 1426 1427
	git_ignore__free(&wi->ignores);
}

1428
int git_iterator_for_workdir_ext(
1429
	git_iterator **out,
1430
	git_repository *repo,
1431
	const char *repo_workdir,
1432 1433
	git_index *index,
	git_tree *tree,
1434
	git_iterator_flag_t flags,
1435 1436
	const char *start,
	const char *end)
1437
{
1438
	int error, precompose = 0;
1439
	workdir_iterator *wi;
1440

1441 1442 1443 1444 1445
	if (!repo_workdir) {
		if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
			return GIT_EBAREREPO;
		repo_workdir = git_repository_workdir(repo);
	}
1446

1447
	/* initialize as an fs iterator then do overrides */
1448 1449
	wi = git__calloc(1, sizeof(workdir_iterator));
	GITERR_CHECK_ALLOC(wi);
1450
	ITERATOR_BASE_INIT((&wi->fi), fs, FS, repo);
1451

1452 1453 1454 1455 1456
	wi->fi.base.type = GIT_ITERATOR_TYPE_WORKDIR;
	wi->fi.cb.free = workdir_iterator__free;
	wi->fi.enter_dir_cb = workdir_iterator__enter_dir;
	wi->fi.leave_dir_cb = workdir_iterator__leave_dir;
	wi->fi.update_entry_cb = workdir_iterator__update_entry;
1457

1458
	if ((error = iterator__update_ignore_case((git_iterator *)wi, flags)) < 0 ||
1459
		(error = git_ignore__for_path(repo, ".gitignore", &wi->ignores)) < 0)
1460
	{
1461 1462
		git_iterator_free((git_iterator *)wi);
		return error;
1463 1464
	}

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	if (tree && (error = git_object_dup((git_object **)&wi->tree, (git_object *)tree)) < 0)
		return error;

	wi->index = index;
	if (index && (error = git_index_snapshot_new(&wi->index_snapshot, index)) < 0) {
		git_iterator_free((git_iterator *)wi);
		return error;
	}
	wi->entry_srch = iterator__ignore_case(wi) ?
		git_index_entry_isrch : git_index_entry_srch;


1477
	/* try to look up precompose and set flag if appropriate */
1478 1479 1480 1481
	if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
		giterr_clear();
	else if (precompose)
		wi->fi.base.flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
1482

1483
	return fs_iterator__initialize(out, &wi->fi, repo_workdir);
1484 1485
}

1486

1487
void git_iterator_free(git_iterator *iter)
1488
{
1489
	if (iter == NULL)
1490 1491
		return;

1492
	iter->cb->free(iter);
1493

1494 1495
	git__free(iter->start);
	git__free(iter->end);
1496

1497 1498 1499
	memset(iter, 0, sizeof(*iter));

	git__free(iter);
1500 1501
}

1502
int git_iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
1503
{
1504
	bool desire_ignore_case  = (ignore_case != 0);
1505

1506
	if (iterator__ignore_case(iter) == desire_ignore_case)
1507
		return 0;
1508

1509
	if (iter->type == GIT_ITERATOR_TYPE_EMPTY) {
1510 1511 1512 1513 1514 1515 1516 1517
		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;
1518 1519 1520
	}

	return 0;
1521 1522
}

1523
git_index *git_iterator_get_index(git_iterator *iter)
1524
{
1525
	if (iter->type == GIT_ITERATOR_TYPE_INDEX)
1526 1527 1528 1529
		return ((index_iterator *)iter)->index;
	return NULL;
}

1530
int git_iterator_current_tree_entry(
1531
	const git_tree_entry **tree_entry, git_iterator *iter)
1532
{
1533 1534 1535
	if (iter->type != GIT_ITERATOR_TYPE_TREE)
		*tree_entry = NULL;
	else {
1536
		tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
1537 1538
		*tree_entry = (tf->current < tf->n_entries) ?
			tf->entries[tf->current]->te : NULL;
1539 1540
	}

1541
	return 0;
1542 1543
}

1544
int git_iterator_current_parent_tree(
1545
	const git_tree **tree_ptr,
1546
	git_iterator *iter,
1547
	const char *parent_path)
1548 1549 1550 1551
{
	tree_iterator *ti = (tree_iterator *)iter;
	tree_iterator_frame *tf;
	const char *scan = parent_path;
1552
	const git_tree_entry *te;
1553

1554
	*tree_ptr = NULL;
1555

1556 1557
	if (iter->type != GIT_ITERATOR_TYPE_TREE)
		return 0;
1558

1559 1560 1561 1562
	for (tf = ti->root; *scan; ) {
		if (!(tf = tf->down) ||
			tf->current >= tf->n_entries ||
			!(te = tf->entries[tf->current]->te) ||
1563
			ti->strncomp(scan, te->filename, te->filename_len) != 0)
1564 1565 1566
			return 0;

		scan += te->filename_len;
1567
		if (*scan == '/')
1568 1569 1570
			scan++;
	}

1571
	*tree_ptr = tf->entries[tf->current]->tree;
1572 1573 1574
	return 0;
}

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
static void workdir_iterator_update_is_ignored(workdir_iterator *wi)
{
	if (git_ignore__lookup(
			&wi->is_ignored, &wi->ignores, wi->fi.entry.path) < 0) {
		giterr_clear();
		wi->is_ignored = GIT_IGNORE_NOTFOUND;
	}

	/* use ignore from containing frame stack */
	if (wi->is_ignored <= GIT_IGNORE_NOTFOUND)
		wi->is_ignored = wi->fi.stack->is_ignored;
}

1588
bool git_iterator_current_is_ignored(git_iterator *iter)
1589
{
1590 1591
	workdir_iterator *wi = (workdir_iterator *)iter;

1592
	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
1593
		return false;
1594

1595 1596
	if (wi->is_ignored != GIT_IGNORE_UNCHECKED)
		return (bool)(wi->is_ignored == GIT_IGNORE_TRUE);
1597

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
	workdir_iterator_update_is_ignored(wi);

	return (bool)(wi->is_ignored == GIT_IGNORE_TRUE);
}

bool git_iterator_current_tree_is_ignored(git_iterator *iter)
{
	workdir_iterator *wi = (workdir_iterator *)iter;

	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
		return false;
1609

1610
	return (bool)(wi->fi.stack->is_ignored == GIT_IGNORE_TRUE);
1611 1612
}

1613
int git_iterator_cmp(git_iterator *iter, const char *path_prefix)
1614 1615 1616 1617
{
	const git_index_entry *entry;

	/* a "done" iterator is after every prefix */
1618
	if (git_iterator_current(&entry, iter) < 0 || entry == NULL)
1619 1620 1621 1622 1623 1624
		return 1;

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

1625
	return iter->prefixcomp(entry->path, path_prefix);
1626 1627
}

1628
int git_iterator_current_workdir_path(git_buf **path, git_iterator *iter)
1629 1630 1631
{
	workdir_iterator *wi = (workdir_iterator *)iter;

1632
	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR || !wi->fi.entry.path)
1633 1634
		*path = NULL;
	else
1635
		*path = &wi->fi.path;
1636 1637 1638

	return 0;
}
1639

1640 1641 1642 1643
int git_iterator_advance_over_with_status(
	const git_index_entry **entryptr,
	git_iterator_status_t *status,
	git_iterator *iter)
1644 1645 1646 1647 1648 1649
{
	int error = 0;
	workdir_iterator *wi = (workdir_iterator *)iter;
	char *base = NULL;
	const git_index_entry *entry;

1650
	*status = GIT_ITERATOR_STATUS_NORMAL;
1651 1652 1653 1654 1655 1656 1657

	if (iter->type != GIT_ITERATOR_TYPE_WORKDIR)
		return git_iterator_advance(entryptr, iter);
	if ((error = git_iterator_current(&entry, iter)) < 0)
		return error;

	if (!S_ISDIR(entry->mode)) {
1658 1659
		workdir_iterator_update_is_ignored(wi);
		if (wi->is_ignored == GIT_IGNORE_TRUE)
1660
			*status = GIT_ITERATOR_STATUS_IGNORED;
1661 1662 1663
		return git_iterator_advance(entryptr, iter);
	}

1664
	*status = GIT_ITERATOR_STATUS_EMPTY;
1665 1666 1667 1668 1669 1670

	base = git__strdup(entry->path);
	GITERR_CHECK_ALLOC(base);

	/* scan inside directory looking for a non-ignored item */
	while (entry && !iter->prefixcomp(entry->path, base)) {
1671
		workdir_iterator_update_is_ignored(wi);
1672

1673 1674 1675
		/* if we found an explicitly ignored item, then update from
		 * EMPTY to IGNORED
		 */
1676
		if (wi->is_ignored == GIT_IGNORE_TRUE)
1677 1678
			*status = GIT_ITERATOR_STATUS_IGNORED;
		else if (S_ISDIR(entry->mode)) {
1679 1680 1681 1682 1683 1684
			error = git_iterator_advance_into(&entry, iter);

			if (!error)
				continue;
			else if (error == GIT_ENOTFOUND) {
				error = 0;
1685
				wi->is_ignored = GIT_IGNORE_TRUE; /* mark empty dirs ignored */
1686 1687
			} else
				break; /* real error, stop here */
1688 1689
		} else {
			/* we found a non-ignored item, treat parent as untracked */
1690
			*status = GIT_ITERATOR_STATUS_NORMAL;
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
			break;
		}

		if ((error = git_iterator_advance(&entry, iter)) < 0)
			break;
	}

	/* wrap up scan back to base directory */
	while (entry && !iter->prefixcomp(entry->path, base))
		if ((error = git_iterator_advance(&entry, iter)) < 0)
			break;

	*entryptr = entry;
	git__free(base);

	return error;
}