Commit 91e7d26303b17c7ebc45ba565247e968aaa20848

Russell Belfer 2012-12-10T15:29:44

Fix iterator reset and add reset ranges The `git_iterator_reset` command has not been working in all cases particularly when there is a start and end range. This fixes it and adds tests for it, and also extends it with the ability to update the start/end range strings when an iterator is reset.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
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
663
664
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
diff --git a/src/iterator.c b/src/iterator.c
index e2bf4cf..7061067 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -29,6 +29,25 @@
 		return -1; \
 	} while (0)
 
+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;
+}
 
 static int empty_iterator__no_item(
 	git_iterator *iter, const git_index_entry **entry)
@@ -44,16 +63,16 @@ static int empty_iterator__at_end(git_iterator *iter)
 	return 1;
 }
 
-static int empty_iterator__noop(git_iterator *iter)
+static int empty_iterator__reset(
+	git_iterator *iter, const char *start, const char *end)
 {
-	GIT_UNUSED(iter);
+	GIT_UNUSED(iter); GIT_UNUSED(start); GIT_UNUSED(end);
 	return 0;
 }
 
 static int empty_iterator__seek(git_iterator *iter, const char *prefix)
 {
-	GIT_UNUSED(iter);
-	GIT_UNUSED(prefix);
+	GIT_UNUSED(iter); GIT_UNUSED(prefix);
 	return -1;
 }
 
@@ -72,7 +91,7 @@ int git_iterator_for_nothing(git_iterator **iter)
 	i->at_end  = empty_iterator__at_end;
 	i->advance = empty_iterator__no_item;
 	i->seek    = empty_iterator__seek;
-	i->reset   = empty_iterator__noop;
+	i->reset   = empty_iterator__reset;
 	i->free    = empty_iterator__free;
 
 	*iter = i;
@@ -97,10 +116,9 @@ typedef struct {
 	bool path_has_filename;
 } tree_iterator;
 
-static const git_tree_entry *tree_iterator__tree_entry(tree_iterator *ti)
+GIT_INLINE(const git_tree_entry *)tree_iterator__tree_entry(tree_iterator *ti)
 {
-	return (ti->stack == NULL) ? NULL :
-		git_tree_entry_byindex(ti->stack->tree, ti->stack->index);
+	return git_tree_entry_byindex(ti->stack->tree, ti->stack->index);
 }
 
 static char *tree_iterator__current_filename(
@@ -115,25 +133,34 @@ static char *tree_iterator__current_filename(
 	return ti->path.ptr;
 }
 
-static void tree_iterator__pop_frame(tree_iterator *ti)
+static void tree_iterator__free_frame(tree_iterator_frame *tf)
 {
-	tree_iterator_frame *tf = ti->stack;
-	ti->stack = tf->next;
-	if (ti->stack != NULL) {
-		git_tree_free(tf->tree); /* don't free the initial tree */
-		ti->stack->prev = NULL;  /* disconnect prev */
-	}
+	if (!tf)
+		return;
+	git_tree_free(tf->tree);
 	git__free(tf);
 }
 
-static int tree_iterator__to_end(tree_iterator *ti)
+static bool tree_iterator__pop_frame(tree_iterator *ti)
 {
-	while (ti->stack && ti->stack->next)
-		tree_iterator__pop_frame(ti);
+	tree_iterator_frame *tf = ti->stack;
 
-	if (ti->stack)
-		ti->stack->index = git_tree_entrycount(ti->stack->tree);
+	/* don't free the initial tree/frame */
+	if (!tf->next)
+		return false;
+
+	ti->stack = tf->next;
+	ti->stack->prev = NULL;
+
+	tree_iterator__free_frame(tf);
+
+	return true;
+}
 
+static int tree_iterator__to_end(tree_iterator *ti)
+{
+	while (tree_iterator__pop_frame(ti)) /* pop all */;
+	ti->stack->index = git_tree_entrycount(ti->stack->tree);
 	return 0;
 }
 
@@ -246,12 +273,13 @@ static int tree_iterator__advance(
 		ti->path_has_filename = false;
 	}
 
-	while (ti->stack != NULL) {
+	while (1) {
 		te = git_tree_entry_byindex(ti->stack->tree, ++ti->stack->index);
 		if (te != NULL)
 			break;
 
-		tree_iterator__pop_frame(ti);
+		if (!tree_iterator__pop_frame(ti))
+			break; /* no frames left to pop */
 
 		git_buf_rtruncate_at_char(&ti->path, '/');
 	}
@@ -278,23 +306,30 @@ static int tree_iterator__seek(git_iterator *self, const char *prefix)
 static void tree_iterator__free(git_iterator *self)
 {
 	tree_iterator *ti = (tree_iterator *)self;
-	while (ti->stack != NULL)
-		tree_iterator__pop_frame(ti);
+
+	while (tree_iterator__pop_frame(ti)) /* pop all */;
+
+	tree_iterator__free_frame(ti->stack);
+	ti->stack = ti->tail = NULL;
+
 	git_buf_free(&ti->path);
 }
 
-static int tree_iterator__reset(git_iterator *self)
+static int tree_iterator__reset(
+	git_iterator *self, const char *start, const char *end)
 {
 	tree_iterator *ti = (tree_iterator *)self;
 
-	while (ti->stack && ti->stack->next)
-		tree_iterator__pop_frame(ti);
+	while (tree_iterator__pop_frame(ti)) /* pop all */;
 
-	if (ti->stack)
-		ti->stack->index =
-			git_tree__prefix_position(ti->stack->tree, ti->base.start);
+	if (iterator__reset_range(self, start, end) < 0)
+		return -1;
+
+	ti->stack->index =
+		git_tree__prefix_position(ti->stack->tree, ti->base.start);
 
 	git_buf_clear(&ti->path);
+	ti->path_has_filename = false;
 
 	return tree_iterator__expand_tree(ti);
 }
@@ -311,6 +346,9 @@ int git_iterator_for_tree_range(
 	if (tree == NULL)
 		return git_iterator_for_nothing(iter);
 
+	if ((error = git_tree__dup(&tree, tree)) < 0)
+		return error;
+
 	ITERATOR_BASE_INIT(ti, tree, TREE);
 
 	ti->base.repo = git_tree_owner(tree);
@@ -394,9 +432,12 @@ static int index_iterator__seek(git_iterator *self, const char *prefix)
 	return -1;
 }
 
-static int index_iterator__reset(git_iterator *self)
+static int index_iterator__reset(
+	git_iterator *self, const char *start, const char *end)
 {
 	index_iterator *ii = (index_iterator *)self;
+	if (iterator__reset_range(self, start, end) < 0)
+		return -1;
 	ii->current = ii->base.start ?
 		git_index__prefix_position(ii->index, ii->base.start) : 0;
 	index_iterator__skip_conflicts(ii);
@@ -425,7 +466,7 @@ int git_iterator_for_index_range(
 	ii->base.repo = git_index_owner(index);
 	ii->base.ignore_case = ii->index->ignore_case;
 
-	index_iterator__reset((git_iterator *)ii);
+	index_iterator__reset((git_iterator *)ii, NULL, NULL);
 
 	*iter = (git_iterator *)ii;
 
@@ -455,35 +496,19 @@ struct workdir_iterator_frame {
 	workdir_iterator_frame *next;
 	git_vector entries;
 	size_t index;
-	char *start;
 };
 
 typedef struct {
 	git_iterator base;
-	size_t root_len;
 	workdir_iterator_frame *stack;
+	int (*entrycmp)(const void *pfx, const void *item);
 	git_ignores ignores;
 	git_index_entry entry;
 	git_buf path;
+	size_t root_len;
 	int is_ignored;
 } workdir_iterator;
 
-static int git_path_with_stat_cmp_case(const void *a, const void *b)
-{
-	const git_path_with_stat *path_with_stat_a = a;
-	const git_path_with_stat *path_with_stat_b = b;
-
-	return strcmp(path_with_stat_a->path, path_with_stat_b->path);
-}
-
-static int git_path_with_stat_cmp_icase(const void *a, const void *b)
-{
-	const git_path_with_stat *path_with_stat_a = a;
-	const git_path_with_stat *path_with_stat_b = b;
-
-	return strcasecmp(path_with_stat_a->path, path_with_stat_b->path);
-}
-
 GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps)
 {
 	if (!ps)
@@ -492,26 +517,34 @@ GIT_INLINE(bool) path_is_dotgit(const git_path_with_stat *ps)
 		const char *path = ps->path;
 		size_t len  = ps->path_len;
 
-		return len >= 4 &&
-			tolower(path[len - 1]) == 't' &&
-			tolower(path[len - 2]) == 'i' &&
-			tolower(path[len - 3]) == 'g' &&
-			path[len - 4] == '.' &&
-			(len == 4 || path[len - 5] == '/');
+		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] == '/');
 	}
 }
 
-static workdir_iterator_frame *workdir_iterator__alloc_frame(workdir_iterator *wi)
+static workdir_iterator_frame *workdir_iterator__alloc_frame(
+	workdir_iterator *wi)
 {
 	workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
-	git_vector_cmp entry_compare = CASESELECT(wi->base.ignore_case, git_path_with_stat_cmp_icase, git_path_with_stat_cmp_case);
+	git_vector_cmp entry_compare = CASESELECT(wi->base.ignore_case,
+		git_path_with_stat_cmp_icase, git_path_with_stat_cmp);
 
 	if (wf == NULL)
 		return NULL;
+
 	if (git_vector_init(&wf->entries, 0, entry_compare) != 0) {
 		git__free(wf);
 		return NULL;
 	}
+
 	return wf;
 }
 
@@ -528,16 +561,32 @@ static void workdir_iterator__free_frame(workdir_iterator_frame *wf)
 
 static int workdir_iterator__update_entry(workdir_iterator *wi);
 
-static int workdir_iterator__entry_cmp_case(const void *prefix, const void *item)
+static int workdir_iterator__entry_cmp_case(const void *pfx, const void *item)
 {
 	const git_path_with_stat *ps = item;
-	return git__prefixcmp((const char *)prefix, ps->path);
+	return git__prefixcmp((const char *)pfx, ps->path);
 }
 
-static int workdir_iterator__entry_cmp_icase(const void *prefix, const void *item)
+static int workdir_iterator__entry_cmp_icase(const void *pfx, const void *item)
 {
 	const git_path_with_stat *ps = item;
-	return git__prefixcmp_icase((const char *)prefix, ps->path);
+	return git__prefixcmp_icase((const char *)pfx, ps->path);
+}
+
+static void workdir_iterator__seek_frame_start(
+	workdir_iterator *wi, workdir_iterator_frame *wf)
+{
+	if (!wf)
+		return;
+
+	if (wi->base.start)
+		git_vector_bsearch3(
+			&wf->index, &wf->entries, wi->entrycmp, wi->base.start);
+	else
+		wf->index = 0;
+
+	if (path_is_dotgit(git_vector_get(&wf->entries, wf->index)))
+		wf->index++;
 }
 
 static int workdir_iterator__expand_dir(workdir_iterator *wi)
@@ -546,39 +595,26 @@ static int workdir_iterator__expand_dir(workdir_iterator *wi)
 	workdir_iterator_frame *wf = workdir_iterator__alloc_frame(wi);
 	GITERR_CHECK_ALLOC(wf);
 
-	error = git_path_dirload_with_stat(wi->path.ptr, wi->root_len, &wf->entries);
+	error = git_path_dirload_with_stat(
+		wi->path.ptr, wi->root_len, wi->base.ignore_case,
+		wi->base.start, wi->base.end, &wf->entries);
+
 	if (error < 0 || wf->entries.length == 0) {
 		workdir_iterator__free_frame(wf);
 		return GIT_ENOTFOUND;
 	}
 
-	git_vector_sort(&wf->entries);
-
-	if (!wi->stack)
-		wf->start = wi->base.start;
-	else if (wi->stack->start &&
-		ITERATOR_PREFIXCMP(wi->base, wi->stack->start, wi->path.ptr + wi->root_len) == 0)
-		wf->start = wi->stack->start;
-
-	if (wf->start)
-		git_vector_bsearch3(
-			&wf->index,
-			&wf->entries,
-			CASESELECT(wi->base.ignore_case, workdir_iterator__entry_cmp_icase, workdir_iterator__entry_cmp_case),
-			wf->start);
-
-	if (path_is_dotgit(git_vector_get(&wf->entries, wf->index)))
-		wf->index++;
-
-	wf->next  = wi->stack;
-	wi->stack = wf;
+	workdir_iterator__seek_frame_start(wi, wf);
 
 	/* only push new ignores if this is not top level directory */
-	if (wi->stack->next != NULL) {
+	if (wi->stack != NULL) {
 		ssize_t slash_pos = git_buf_rfind_next(&wi->path, '/');
 		(void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]);
 	}
 
+	wf->next  = wi->stack;
+	wi->stack = wf;
+
 	return workdir_iterator__update_entry(wi);
 }
 
@@ -609,8 +645,10 @@ static int workdir_iterator__advance(
 	if (wi->entry.path == NULL)
 		return 0;
 
-	while ((wf = wi->stack) != NULL) {
+	while (1) {
+		wf   = wi->stack;
 		next = git_vector_get(&wf->entries, ++wf->index);
+
 		if (next != NULL) {
 			/* match git's behavior of ignoring anything named ".git" */
 			if (path_is_dotgit(next))
@@ -619,15 +657,15 @@ static int workdir_iterator__advance(
 			break;
 		}
 
-		/* pop workdir directory stack */
-		wi->stack = wf->next;
-		workdir_iterator__free_frame(wf);
-		git_ignore__pop_dir(&wi->ignores);
-
-		if (wi->stack == NULL) {
+		/* pop stack if anything is left to pop */
+		if (!wf->next) {
 			memset(&wi->entry, 0, sizeof(wi->entry));
 			return 0;
 		}
+
+		wi->stack = wf->next;
+		workdir_iterator__free_frame(wf);
+		git_ignore__pop_dir(&wi->ignores);
 	}
 
 	error = workdir_iterator__update_entry(wi);
@@ -648,18 +686,24 @@ static int workdir_iterator__seek(git_iterator *self, const char *prefix)
 	return 0;
 }
 
-static int workdir_iterator__reset(git_iterator *self)
+static int workdir_iterator__reset(
+	git_iterator *self, const char *start, const char *end)
 {
 	workdir_iterator *wi = (workdir_iterator *)self;
+
 	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);
 	}
-	if (wi->stack)
-		wi->stack->index = 0;
-	return 0;
+
+	if (iterator__reset_range(self, start, end) < 0)
+		return -1;
+
+	workdir_iterator__seek_frame_start(wi, wi->stack);
+
+	return workdir_iterator__update_entry(wi);
 }
 
 static void workdir_iterator__free(git_iterator *self)
@@ -678,7 +722,8 @@ static void workdir_iterator__free(git_iterator *self)
 
 static int workdir_iterator__update_entry(workdir_iterator *wi)
 {
-	git_path_with_stat *ps = git_vector_get(&wi->stack->entries, wi->stack->index);
+	git_path_with_stat *ps =
+		git_vector_get(&wi->stack->entries, wi->stack->index);
 
 	git_buf_truncate(&wi->path, wi->root_len);
 	memset(&wi->entry, 0, sizeof(wi->entry));
@@ -689,8 +734,8 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
 	if (git_buf_put(&wi->path, ps->path, ps->path_len) < 0)
 		return -1;
 
-	if (wi->base.end &&
-		ITERATOR_PREFIXCMP(wi->base, wi->path.ptr + wi->root_len, wi->base.end) > 0)
+	if (wi->base.end && ITERATOR_PREFIXCMP(
+			wi->base, wi->path.ptr + wi->root_len, wi->base.end) > 0)
 		return 0;
 
 	wi->entry.path = ps->path;
@@ -751,7 +796,7 @@ int git_iterator_for_workdir_range(
 	wi->base.repo = repo;
 
 	if ((error = git_repository_index__weakptr(&index, repo)) < 0) {
-		git__free(wi);
+		git_iterator_free((git_iterator *)wi);
 		return error;
 	}
 
@@ -767,6 +812,8 @@ int git_iterator_for_workdir_range(
 	}
 
 	wi->root_len = wi->path.size;
+	wi->entrycmp = wi->base.ignore_case ?
+		workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case;
 
 	if ((error = workdir_iterator__expand_dir(wi)) < 0) {
 		if (error == GIT_ENOTFOUND)
@@ -835,10 +882,13 @@ static int spoolandsort_iterator__seek(git_iterator *self, const char *prefix)
 	return -1;
 }
 
-static int spoolandsort_iterator__reset(git_iterator *self)
+static int spoolandsort_iterator__reset(
+	git_iterator *self, const char *start, const char *end)
 {
 	spoolandsort_iterator *si = (spoolandsort_iterator *)self;
 
+	GIT_UNUSED(start); GIT_UNUSED(end);
+
 	si->position = 0;
 
 	return 0;
diff --git a/src/iterator.h b/src/iterator.h
index 07cce5a..9fe6844 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -37,7 +37,7 @@ struct git_iterator {
 	int (*at_end)(git_iterator *);
 	int (*advance)(git_iterator *, const git_index_entry **);
 	int (*seek)(git_iterator *, const char *prefix);
-	int (*reset)(git_iterator *);
+	int (*reset)(git_iterator *, const char *start, const char *end);
 	void (*free)(git_iterator *);
 };
 
@@ -126,9 +126,10 @@ GIT_INLINE(int) git_iterator_seek(
 	return iter->seek(iter, prefix);
 }
 
-GIT_INLINE(int) git_iterator_reset(git_iterator *iter)
+GIT_INLINE(int) git_iterator_reset(
+	git_iterator *iter, const char *start, const char *end)
 {
-	return iter->reset(iter);
+	return iter->reset(iter, start, end);
 }
 
 GIT_INLINE(void) git_iterator_free(git_iterator *iter)
diff --git a/src/path.c b/src/path.c
index 87eded3..569101c 100644
--- a/src/path.c
+++ b/src/path.c
@@ -770,18 +770,30 @@ int git_path_dirload(
 int git_path_with_stat_cmp(const void *a, const void *b)
 {
 	const git_path_with_stat *psa = a, *psb = b;
-	return git__strcmp_cb(psa->path, psb->path);
+	return strcmp(psa->path, psb->path);
+}
+
+int git_path_with_stat_cmp_icase(const void *a, const void *b)
+{
+	const git_path_with_stat *psa = a, *psb = b;
+	return strcasecmp(psa->path, psb->path);
 }
 
 int git_path_dirload_with_stat(
 	const char *path,
 	size_t prefix_len,
+	bool ignore_case,
+	const char *start_stat,
+	const char *end_stat,
 	git_vector *contents)
 {
 	int error;
 	unsigned int i;
 	git_path_with_stat *ps;
 	git_buf full = GIT_BUF_INIT;
+	int (*strncomp)(const char *a, const char *b, size_t sz);
+	size_t start_len = start_stat ? strlen(start_stat) : 0;
+	size_t end_len = end_stat ? strlen(end_stat) : 0, cmp_len;
 
 	if (git_buf_set(&full, path, prefix_len) < 0)
 		return -1;
@@ -793,11 +805,23 @@ int git_path_dirload_with_stat(
 		return error;
 	}
 
+	strncomp = ignore_case ? git__strncasecmp : git__strncmp;
+
+	/* stat struct at start of git_path_with_stat, so shift path text */
 	git_vector_foreach(contents, i, ps) {
 		size_t path_len = strlen((char *)ps);
-
 		memmove(ps->path, ps, path_len + 1);
 		ps->path_len = path_len;
+	}
+
+	git_vector_foreach(contents, i, ps) {
+		/* skip if before start_stat or after end_stat */
+		cmp_len = min(start_len, ps->path_len);
+		if (cmp_len && strncomp(ps->path, start_stat, cmp_len) < 0)
+			continue;
+		cmp_len = min(end_len, ps->path_len);
+		if (cmp_len && strncomp(ps->path, end_stat, cmp_len) > 0)
+			continue;
 
 		if ((error = git_buf_joinpath(&full, full.ptr, ps->path)) < 0 ||
 			(error = git_path_lstat(full.ptr, &ps->st)) < 0)
@@ -806,11 +830,14 @@ int git_path_dirload_with_stat(
 		git_buf_truncate(&full, prefix_len);
 
 		if (S_ISDIR(ps->st.st_mode)) {
-			ps->path[path_len] = '/';
-			ps->path[path_len + 1] = '\0';
+			ps->path[ps->path_len++] = '/';
+			ps->path[ps->path_len] = '\0';
 		}
 	}
 
+	/* sort now that directory suffix is added */
+	git_vector_sort(contents);
+
 	git_buf_free(&full);
 
 	return error;
diff --git a/src/path.h b/src/path.h
index b629227..db260d8 100644
--- a/src/path.h
+++ b/src/path.h
@@ -321,18 +321,33 @@ typedef struct {
 } git_path_with_stat;
 
 extern int git_path_with_stat_cmp(const void *a, const void *b);
+extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
 
 /**
  * Load all directory entries along with stat info into a vector.
  *
- * This is just like git_path_dirload except that each entry in the
- * vector is a git_path_with_stat structure that contains both the
- * path and the stat info, plus directories will have a / suffixed
- * to their path name.
+ * This adds four things on top of plain `git_path_dirload`:
+ *
+ * 1. Each entry in the vector is a `git_path_with_stat` struct that
+ *    contains both the path and the stat info
+ * 2. The entries will be sorted alphabetically
+ * 3. Entries that are directories will be suffixed with a '/'
+ * 4. Optionally, you can be a start and end prefix and only elements
+ *    after the start and before the end (inclusively) will be stat'ed.
+ *
+ * @param path The directory to read from
+ * @param prefix_len The trailing part of path to prefix to entry paths
+ * @param ignore_case How to sort and compare paths with start/end limits
+ * @param start_stat As optimization, only stat values after this prefix
+ * @param end_stat As optimization, only stat values before this prefix
+ * @param contents Vector to fill with git_path_with_stat structures
  */
 extern int git_path_dirload_with_stat(
 	const char *path,
 	size_t prefix_len,
+	bool ignore_case,
+	const char *start_stat,
+	const char *end_stat,
 	git_vector *contents);
 
 #endif
diff --git a/src/tree.c b/src/tree.c
index 944992f..7f1b9fe 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -301,6 +301,9 @@ int git_tree__prefix_position(git_tree *tree, const char *path)
 	struct tree_key_search ksearch;
 	size_t at_pos;
 
+	if (!path)
+		return 0;
+
 	ksearch.filename = path;
 	ksearch.filename_len = strlen(path);
 
diff --git a/src/tree.h b/src/tree.h
index e0bcd6a..c28523d 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -29,6 +29,10 @@ struct git_treebuilder {
 	git_vector entries;
 };
 
+GIT_INLINE(int) git_tree__dup(git_tree **dest, git_tree *source)
+{
+	return git_object__dup((git_object **)dest, (git_object *)source);
+}
 
 GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e)
 {
diff --git a/src/util.c b/src/util.c
index 9813eb6..831b073 100644
--- a/src/util.c
+++ b/src/util.c
@@ -199,9 +199,17 @@ int git__strncmp(const char *a, const char *b, size_t sz)
 
 int git__strncasecmp(const char *a, const char *b, size_t sz)
 {
-	while (sz && *a && *b && tolower(*a) == tolower(*b))
+	int al, bl;
+
+	while (sz && *a && *b) {
+		al = (unsigned char)tolower(*a);
+		bl = (unsigned char)tolower(*b);
+		if (al != bl)
+			break;
 		--sz, ++a, ++b;
-	return !sz ? 0 : (tolower(*a) - tolower(*b));
+	}
+
+	return !sz ? 0 : al - bl;
 }
 
 void git__strntolower(char *str, size_t len)
diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c
index c97e09a..b579063 100644
--- a/tests-clar/diff/iterator.c
+++ b/tests-clar/diff/iterator.c
@@ -31,25 +31,35 @@ static void tree_iterator_test(
 	git_tree *t;
 	git_iterator *i;
 	const git_index_entry *entry;
-	int count = 0;
+	int count = 0, count_post_reset = 0;
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
 	cl_assert(t = resolve_commit_oid_to_tree(repo, treeish));
 	cl_git_pass(git_iterator_for_tree_range(&i, t, start, end));
-	cl_git_pass(git_iterator_current(i, &entry));
 
+	/* test loop */
+	cl_git_pass(git_iterator_current(i, &entry));
 	while (entry != NULL) {
 		if (expected_values != NULL)
 			cl_assert_equal_s(expected_values[count], entry->path);
-
 		count++;
+		cl_git_pass(git_iterator_advance(i, &entry));
+	}
 
+	/* test reset */
+	cl_git_pass(git_iterator_reset(i, NULL, NULL));
+	cl_git_pass(git_iterator_current(i, &entry));
+	while (entry != NULL) {
+		if (expected_values != NULL)
+			cl_assert_equal_s(expected_values[count_post_reset], entry->path);
+		count_post_reset++;
 		cl_git_pass(git_iterator_advance(i, &entry));
 	}
 
 	git_iterator_free(i);
 
-	cl_assert(expected_count == count);
+	cl_assert_equal_i(expected_count, count);
+	cl_assert_equal_i(count, count_post_reset);
 
 	git_tree_free(t);
 }
@@ -520,7 +530,7 @@ static void workdir_iterator_test(
 {
 	git_iterator *i;
 	const git_index_entry *entry;
-	int count = 0, count_all = 0;
+	int count = 0, count_all = 0, count_all_post_reset = 0;
 	git_repository *repo = cl_git_sandbox_init(sandbox);
 
 	cl_git_pass(git_iterator_for_workdir_range(&i, repo, start, end));
@@ -547,10 +557,26 @@ static void workdir_iterator_test(
 		cl_git_pass(git_iterator_advance(i, &entry));
 	}
 
+	cl_git_pass(git_iterator_reset(i, NULL, NULL));
+	cl_git_pass(git_iterator_current(i, &entry));
+
+	while (entry != NULL) {
+		if (S_ISDIR(entry->mode)) {
+			cl_git_pass(git_iterator_advance_into_directory(i, &entry));
+			continue;
+		}
+		if (expected_names != NULL)
+			cl_assert_equal_s(
+				expected_names[count_all_post_reset], entry->path);
+		count_all_post_reset++;
+		cl_git_pass(git_iterator_advance(i, &entry));
+	}
+
 	git_iterator_free(i);
 
-	cl_assert_equal_i(expected_count,count);
+	cl_assert_equal_i(expected_count, count);
 	cl_assert_equal_i(expected_count + expected_ignores, count_all);
+	cl_assert_equal_i(count_all, count_all_post_reset);
 }
 
 void test_diff_iterator__workdir_0(void)