Commit d8b903dab0ab1a9fa6d8515825fa5dd19de68fca

nulltoken 2011-09-11T18:46:08

status: enhance determination of statuses for a whole directory - Should increase performance through usage of a walker - No callback invocation for unaltered entries

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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
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
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
diff --git a/src/status.c b/src/status.c
index 72792a6..e2e9998 100644
--- a/src/status.c
+++ b/src/status.c
@@ -44,28 +44,7 @@ struct status_entry {
 	char path[GIT_FLEX_ARRAY]; /* more */
 };
 
-static int status_cmp(const void *a, const void *b)
-{
-	const struct status_entry *entry_a = (const struct status_entry *)(a);
-	const struct status_entry *entry_b = (const struct status_entry *)(b);
-
-	return strcmp(entry_a->path, entry_b->path);
-}
-
-static int status_srch(const void *key, const void *array_member)
-{
-	const char *path = (const char *)key;
-	const struct status_entry *entry = (const struct status_entry *)(array_member);
-
-	return strcmp(path, entry->path);
-}
-
-static int find_status_entry(git_vector *entries, const char *path)
-{
-	return git_vector_bsearch2(entries, status_srch, path);
-}
-
-static struct status_entry *new_status_entry(git_vector *entries, const char *path)
+static struct status_entry *status_entry_new(git_vector *entries, const char *path)
 {
 	struct status_entry *e = git__malloc(sizeof(*e) + strlen(path) + 1);
 	if (e == NULL)
@@ -81,114 +60,53 @@ static struct status_entry *new_status_entry(git_vector *entries, const char *pa
 	return e;
 }
 
-static void recurse_tree_entries(git_tree *tree, git_vector *entries, char *path)
+GIT_INLINE(void) status_entry_update_from_tree_entry(struct status_entry *e, const git_tree_entry *tree_entry)
 {
-	int i, cnt, idx;
-	struct status_entry *e;
-	char file_path[GIT_PATH_MAX];
-	git_tree *subtree;
-
-	cnt = git_tree_entrycount(tree);
-	for (i = 0; i < cnt; ++i) {
-		const git_tree_entry *tree_entry = git_tree_entry_byindex(tree, i);
-
-		git_path_join(file_path, path, tree_entry->filename);
-
-		if (git_tree_lookup(&subtree, tree->object.repo, &tree_entry->oid) == GIT_SUCCESS) {
-			recurse_tree_entries(subtree, entries, file_path);
-			git_tree_close(subtree);
-			continue;
-		}
+	assert(e && tree_entry);
 
-		if ((idx = find_status_entry(entries, file_path)) != GIT_ENOTFOUND)
-			e = (struct status_entry *)git_vector_get(entries, idx);
-		else
-			e = new_status_entry(entries, file_path);
-
-		git_oid_cpy(&e->head_oid, &tree_entry->oid);
-	}
+	git_oid_cpy(&e->head_oid, &tree_entry->oid);
 }
 
-static int recurse_tree_entry(git_tree *tree, struct status_entry *e, const char *path)
+GIT_INLINE(void) status_entry_update_from_index_entry(struct status_entry *e, const git_index_entry *index_entry)
 {
-	char *dir_sep;
-	const git_tree_entry *tree_entry;
-	git_tree *subtree;
-	int error = GIT_SUCCESS;
+	assert(e && index_entry);
 
-	dir_sep = strchr(path, '/');
-	if (!dir_sep) {
-		tree_entry = git_tree_entry_byname(tree, path);
-		if (tree_entry == NULL)
-			return GIT_SUCCESS;	/* The leaf doesn't exist in the tree*/
-
-		git_oid_cpy(&e->head_oid, &tree_entry->oid);
-		return GIT_SUCCESS;
-	}
+	git_oid_cpy(&e->index_oid, &index_entry->oid);
+	e->mtime = index_entry->mtime;
+}
 
-	/* Retrieve subtree name */
-	*dir_sep = '\0';
+static void status_entry_update_from_index(struct status_entry *e, git_index *index)
+{
+	int idx;
+	git_index_entry *index_entry;
 
-	tree_entry = git_tree_entry_byname(tree, path);
-	if (tree_entry == NULL)
-		return GIT_SUCCESS;	/* The subtree doesn't exist in the tree*/
+	assert(e && index);
 
-	*dir_sep = '/';
+	idx = git_index_find(index, e->path);
+	if (idx < 0)
+		return;
 
-	/* Retreive subtree */
-	if ((error = git_tree_lookup(&subtree, tree->object.repo, &tree_entry->oid)) < GIT_SUCCESS)
-		return git__throw(GIT_EOBJCORRUPTED, "Can't find tree object '%s'", &tree_entry->filename);
+	index_entry = git_index_get(index, idx);
 
-	error = recurse_tree_entry(subtree, e, dir_sep+1);
-	git_tree_close(subtree);
-	return error;
+	status_entry_update_from_index_entry(e, index_entry);
 }
 
-struct status_st {
-	union {
-		git_vector *vector;
-		struct status_entry *e;
-	} entry;
-
-	int workdir_path_len;
-};
-
-static int dirent_cb(void *state, char *full_path)
+static int status_entry_update_from_workdir(struct status_entry *e, char* full_path)
 {
-	int idx;
-	struct status_entry *e;
-	struct status_st *st = (struct status_st *)state;
-	char *file_path = full_path + st->workdir_path_len;
 	struct stat filest;
-	git_oid oid;
-
-	if ((git_futils_isdir(full_path) == GIT_SUCCESS) && (!strcmp(DOT_GIT, file_path)))
-		return 0;
-
-	if (git_futils_isdir(full_path) == GIT_SUCCESS)
-		return git_futils_direach(full_path, GIT_PATH_MAX, dirent_cb, state);
 
-	if ((idx = find_status_entry(st->entry.vector, file_path)) != GIT_ENOTFOUND) {
-		e = (struct status_entry *)git_vector_get(st->entry.vector, idx);
+	if (p_stat(full_path, &filest) < GIT_SUCCESS)
+		return git__throw(GIT_EOSERR, "Failed to determine status of file '%s'. Can't read file", full_path);
 
-		if (p_stat(full_path, &filest) < 0)
-			return git__throw(GIT_EOSERR, "Failed to read file %s", full_path);
+	if (e->mtime.seconds == (git_time_t)filest.st_mtime)
+		git_oid_cpy(&e->wt_oid, &e->index_oid);
+	else
+		git_odb_hashfile(&e->wt_oid, full_path, GIT_OBJ_BLOB);
 
-		if (e->mtime.seconds == (git_time_t)filest.st_mtime) {
-			git_oid_cpy(&e->wt_oid, &e->index_oid);
-			return 0;
-		}
-	} else {
-		e = new_status_entry(st->entry.vector, file_path);
-	}
-
-	git_odb_hashfile(&oid, full_path, GIT_OBJ_BLOB);
-	git_oid_cpy(&e->wt_oid, &oid);
-
-	return 0;
+	return GIT_SUCCESS;
 }
 
-static int set_status_flags(struct status_entry *e)
+static int status_entry_update_flags(struct status_entry *e)
 {
 	git_oid zero;
 	int head_zero, index_zero, wt_zero;
@@ -219,72 +137,18 @@ static int set_status_flags(struct status_entry *e)
 	return GIT_SUCCESS;
 }
 
-int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload)
-{
-	git_vector entries;
-	struct status_entry *e;
+struct status_st {
+	git_vector *vector;
 	git_index *index;
-	unsigned int i, cnt;
-	git_index_entry *index_entry;
-	char temp_path[GIT_PATH_MAX];
-	int error;
 	git_tree *tree;
-	struct status_st dirent_st;
-
-	git_reference *head_ref, *resolved_head_ref;
-	git_commit *head_commit;
-
-	git_repository_index(&index, repo);
-
-	cnt = git_index_entrycount(index);
-	git_vector_init(&entries, cnt, status_cmp);
-	for (i = 0; i < cnt; ++i) {
-		index_entry = git_index_get(index, i);
-
-		e = new_status_entry(&entries, index_entry->path);
-		git_oid_cpy(&e->index_oid, &index_entry->oid);
-		e->mtime = index_entry->mtime;
-	}
-	git_index_free(index);
-
-	git_reference_lookup(&head_ref, repo, GIT_HEAD_FILE);
-	git_reference_resolve(&resolved_head_ref, head_ref);
 
-	git_commit_lookup(&head_commit, repo, git_reference_oid(resolved_head_ref));
-
-	// recurse through tree entries
-	git_commit_tree(&tree, head_commit);
-	recurse_tree_entries(tree, &entries, "");
-	git_tree_close(tree);
-	git_commit_close(head_commit);
-
-	dirent_st.workdir_path_len = strlen(repo->path_workdir);
-	dirent_st.entry.vector = &entries;
-	strcpy(temp_path, repo->path_workdir);
-	git_futils_direach(temp_path, GIT_PATH_MAX, dirent_cb, &dirent_st);
-
-	for (i = 0; i < entries.length; ++i) {
-		e = (struct status_entry *)git_vector_get(&entries, i);
-
-		set_status_flags(e);
-	}
-
-
-	for (i = 0; i < entries.length; ++i) {
-		e = (struct status_entry *)git_vector_get(&entries, i);
-
-		if ((error = callback(e->path, e->status_flags, payload)) < GIT_SUCCESS)
-			return git__throw(error, "Failed to list statuses. User callback failed");
-	}
-
-	for (i = 0; i < entries.length; ++i) {
-		e = (struct status_entry *)git_vector_get(&entries, i);
-		free(e);
-	}
-	git_vector_free(&entries);
-
-	return GIT_SUCCESS;
-}
+	int workdir_path_len;
+	char* head_tree_relative_path;
+	int head_tree_relative_path_len;
+	unsigned int tree_position;
+	unsigned int index_position;
+	int is_dir:1;
+};
 
 static int retrieve_head_tree(git_tree **tree_out, git_repository *repo)
 {
@@ -322,15 +186,355 @@ exit:
 	return error;
 }
 
+#define GIT_STATUS_PATH_NULL	-2
+#define GIT_STATUS_PATH_IGNORE	-1
+#define GIT_STATUS_PATH_FILE	0
+#define GIT_STATUS_PATH_FOLDER	1
+
+static int dirent_cb(void *state, char *full_path);
+static int alphasorted_futils_direach(
+	char *path, size_t path_sz,
+	int (*fn)(void *, char *), void *arg);
+
+static int process_folder(struct status_st *st, const git_tree_entry *tree_entry, char *full_path, int path_type)
+{
+	git_object *subtree = NULL;
+	git_tree *pushed_tree = NULL;
+	int error, pushed_tree_position = 0;
+	git_otype tree_entry_type;
+
+	tree_entry_type = git_tree_entry_type(tree_entry);
+
+	switch (tree_entry_type) {
+	case GIT_OBJ_TREE:
+		error = git_tree_entry_2object(&subtree, ((git_object *)(st->tree))->repo, tree_entry);
+		pushed_tree = st->tree;
+		pushed_tree_position = st->tree_position;
+		st->tree = (git_tree *)subtree;
+		st->tree_position = 0;
+		st->head_tree_relative_path_len += 1 + tree_entry->filename_len; /* path + '/' + name */
+		break;
+
+	case GIT_OBJ_BLOB:
+		/* No op */
+		break;
+
+	default:
+		error = git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type");	/* TODO: How should we deal with submodules? */
+	}
+
+	if (full_path != NULL && path_type == GIT_STATUS_PATH_FOLDER)
+		error = alphasorted_futils_direach(full_path, GIT_PATH_MAX, dirent_cb, st);
+	else {
+		error = dirent_cb(st, NULL);
+	}
+
+	if (tree_entry_type == GIT_OBJ_TREE) {
+		git_object_close(subtree);
+		st->head_tree_relative_path_len -= 1 + tree_entry->filename_len;
+		st->tree = pushed_tree;
+		st->tree_position = pushed_tree_position;
+		st->tree_position++;
+	}
+
+	return error;
+}
+
+static int store_if_changed(struct status_st *st, struct status_entry *e)
+{
+	int error;
+	if ((error = status_entry_update_flags(e)) < GIT_SUCCESS)
+			return git__throw(error, "Failed to process the file '%s'. It doesn't exist in the workdir, in the HEAD nor in the index", e->path);
+
+	if (e->status_flags == GIT_STATUS_CURRENT) {
+		free(e);
+		return GIT_SUCCESS;
+	}
+
+	return git_vector_insert(st->vector, e);
+}
+
+static int determine_status(struct status_st *st,
+	int in_head, int in_index, int in_workdir,
+	const git_tree_entry *tree_entry,
+	const git_index_entry *index_entry,
+	char *full_path,
+	const char *status_path,
+	int path_type)
+{
+	struct status_entry *e;
+	int error = GIT_SUCCESS;
+	git_otype tree_entry_type = GIT_OBJ_BAD;
+
+	if (tree_entry != NULL)
+		tree_entry_type = git_tree_entry_type(tree_entry);
+
+	/* If we're dealing with a directory in the workdir, let's recursively tackle it first */
+	if (path_type == GIT_STATUS_PATH_FOLDER)
+		return process_folder(st, tree_entry, full_path, path_type);
+
+	/* Are we dealing with a file somewhere? */
+	if (in_workdir || in_index || (in_head && tree_entry_type == GIT_OBJ_BLOB)) {
+		e = status_entry_new(NULL, status_path);
+
+		if (in_head && tree_entry_type == GIT_OBJ_BLOB) {
+			status_entry_update_from_tree_entry(e, tree_entry);
+			st->tree_position++;
+		}
+
+		if (in_index) {
+			status_entry_update_from_index_entry(e, index_entry);
+			st->index_position++;
+		}
+
+		if (in_workdir)
+			if ((error = status_entry_update_from_workdir(e, full_path)) < GIT_SUCCESS)
+				return error;	/* The callee has already set the error message */
+
+		return store_if_changed(st, e);
+	}
+
+	/* Last option, we're dealing with a leftover folder tree entry */
+	assert(in_head && !in_index && !in_workdir && (tree_entry_type == GIT_OBJ_TREE));
+	return process_folder(st, tree_entry, full_path, path_type);
+}
+
+static int path_type_from(char *full_path, int is_dir)
+{
+	if (full_path == NULL)
+		return GIT_STATUS_PATH_NULL;
+
+	if (!is_dir)
+		return GIT_STATUS_PATH_FILE;
+
+	if (!git__suffixcmp(full_path, "/" DOT_GIT))
+		return GIT_STATUS_PATH_IGNORE;
+
+	return GIT_STATUS_PATH_FOLDER;
+}
+
+static const char *status_path(const char *first, const char *second, const char *third)
+{
+	/* At least one of them can not be NULL */
+	assert(first != NULL || second != NULL || third != NULL);
+
+	/* TODO: Fixme. Ensure that when non null, they're all equal */
+	if (first != NULL)
+		return first;
+
+	if (second != NULL)
+		return second;
+
+	return third;
+}
+
+static int compare(const char *left, const char *right)
+{
+	if (left == NULL && right == NULL)
+		return 0;
+
+	if (left == NULL)
+		return 1;
+
+	if (right == NULL)
+		return -1;
+
+	return strcmp(left, right);
+}
+
+/*
+ * Convenience method to enumerate a tree. Contrarily to the git_tree_entry_byindex()
+ * method, it allows the tree to be enumerated to be NULL. In this case, every returned
+ * tree entry will be NULL as well.
+ */
+static const git_tree_entry *git_tree_entry_bypos(git_tree *tree, unsigned int idx)
+{
+	if (tree == NULL)
+		return NULL;
+
+	return git_vector_get(&tree->entries, idx);
+}
+
+/*
+ * Convenience method to enumerate the index. This method is not supposed to be exposed
+ * as part of the index API because it precludes that the index will not be altered
+ * while the enumeration is being processed. Which wouldn't be very API friendly :)
+ */
+static const git_index_entry *git_index_entry_bypos(git_index *index, unsigned int idx)
+{
+	assert(index);
+	return git_vector_get(&index->entries, idx);
+}
+
+/* Greatly inspired from JGit IndexTreeWalker */
+/* https://github.com/spearce/jgit/blob/ed47e29c777accfa78c6f50685a5df2b8f5b8ff5/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java#L88 */
+
+static int dirent_cb(void *state, char *a)
+{
+	const git_tree_entry *m;
+	const git_index_entry *entry;
+	int path_type;
+	int cmpma, cmpmi, cmpai, error;
+	const char *pm, *pa, *pi;
+	const char *m_name, *i_name, *a_name;
+
+	struct status_st *st = (struct status_st *)state;
+
+	path_type = path_type_from(a, st->is_dir);
+
+	if (path_type == GIT_STATUS_PATH_IGNORE)
+		return GIT_SUCCESS;	/* Let's skip the ".git" directory */
+
+	a_name = (path_type != GIT_STATUS_PATH_NULL) ? a + st->workdir_path_len : NULL;
+
+	while (1) {
+		m = git_tree_entry_bypos(st->tree, st->tree_position);
+		entry = git_index_entry_bypos(st->index, st->index_position);
+
+		if ((m == NULL) && (a == NULL) && (entry == NULL))
+			return GIT_SUCCESS;
+
+		if (m != NULL) {
+			st->head_tree_relative_path[st->head_tree_relative_path_len] = '\0';
+			git_path_join(st->head_tree_relative_path, st->head_tree_relative_path, m->filename);
+			m_name = st->head_tree_relative_path;
+		} else
+			m_name = NULL;
+
+		i_name = (entry != NULL) ? entry->path : NULL;
+
+		cmpma = compare(m_name, a_name);
+		cmpmi = compare(m_name, i_name);
+		cmpai = compare(a_name, i_name);
+
+		pm = ((cmpma <= 0) && (cmpmi <= 0)) ? m_name : NULL;
+		pa = ((cmpma >= 0) && (cmpai <= 0)) ? a_name : NULL;
+		pi = ((cmpmi >= 0) && (cmpai >= 0)) ? i_name : NULL;
+
+		error = determine_status(st, pm != NULL, pi != NULL, pa != NULL, m, entry, a, status_path(pm, pi, pa), path_type);
+
+		if (pa != NULL)
+			return GIT_SUCCESS;
+	}
+}
+
+static int status_cmp(const void *a, const void *b)
+{
+	const struct status_entry *entry_a = (const struct status_entry *)(a);
+	const struct status_entry *entry_b = (const struct status_entry *)(b);
+
+	return strcmp(entry_a->path, entry_b->path);
+}
+
+#define DEFAULT_SIZE 16
+
+int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload)
+{
+	git_vector entries;
+	git_index *index = NULL;
+	char temp_path[GIT_PATH_MAX];
+	char tree_path[GIT_PATH_MAX] = "";
+	struct status_st dirent_st;
+	int error = GIT_SUCCESS;
+	unsigned int i;
+	git_tree *tree;
+	struct status_entry *e;
+
+	if ((error = git_repository_index(&index, repo)) < GIT_SUCCESS) {
+		return git__rethrow(error, "Failed to determine statuses. Index can't be opened");
+	}
+
+	if ((error = retrieve_head_tree(&tree, repo)) < GIT_SUCCESS) {
+		error = git__rethrow(error, "Failed to determine statuses");
+		goto exit;
+	}
+
+	git_vector_init(&entries, DEFAULT_SIZE, status_cmp);
+
+	dirent_st.workdir_path_len = strlen(repo->path_workdir);
+	dirent_st.tree_position = 0;
+	dirent_st.index_position = 0;
+	dirent_st.tree = tree;
+	dirent_st.index = index;
+	dirent_st.vector = &entries;
+	dirent_st.head_tree_relative_path = tree_path;
+	dirent_st.head_tree_relative_path_len = 0;
+	dirent_st.is_dir = 1;
+
+	strcpy(temp_path, repo->path_workdir);
+
+	if (git_futils_isdir(temp_path)) {
+		error = git__throw(GIT_EINVALIDPATH, "Failed to determine status of file '%s'. Provided path doesn't lead to a folder", temp_path);
+		goto exit;
+	}
+
+	if ((error = alphasorted_futils_direach(temp_path, sizeof(temp_path), dirent_cb, &dirent_st)) < GIT_SUCCESS)
+		error = git__rethrow(error, "Failed to determine statuses. An error occured while processing the working directory");
+
+	if ((error == GIT_SUCCESS) && ((error = dirent_cb(&dirent_st, NULL)) < GIT_SUCCESS))
+		error = git__rethrow(error, "Failed to determine statuses. An error occured while post-processing the HEAD tree and the index");
+
+	for (i = 0; i < entries.length; ++i) {
+		e = (struct status_entry *)git_vector_get(&entries, i);
+
+		if (error == GIT_SUCCESS) {
+			error = callback(e->path, e->status_flags, payload);
+			if (error < GIT_SUCCESS)
+				error = git__rethrow(error, "Failed to determine statuses. User callback failed");
+		}
+
+		free(e);
+	}
+
+exit:
+	git_vector_free(&entries);
+	git_tree_close(tree);
+	git_index_free(index);
+	return error;
+}
+
+static int recurse_tree_entry(git_tree *tree, struct status_entry *e, const char *path)
+{
+	char *dir_sep;
+	const git_tree_entry *tree_entry;
+	git_tree *subtree;
+	int error = GIT_SUCCESS;
+
+	dir_sep = strchr(path, '/');
+	if (!dir_sep) {
+		tree_entry = git_tree_entry_byname(tree, path);
+		if (tree_entry == NULL)
+			return GIT_SUCCESS;	/* The leaf doesn't exist in the tree*/
+
+		status_entry_update_from_tree_entry(e, tree_entry);
+		return GIT_SUCCESS;
+	}
+
+	/* Retrieve subtree name */
+	*dir_sep = '\0';
+
+	tree_entry = git_tree_entry_byname(tree, path);
+	if (tree_entry == NULL)
+		return GIT_SUCCESS;	/* The subtree doesn't exist in the tree*/
+
+	*dir_sep = '/';
+
+	/* Retreive subtree */
+	if ((error = git_tree_lookup(&subtree, tree->object.repo, &tree_entry->oid)) < GIT_SUCCESS)
+		return git__throw(GIT_EOBJCORRUPTED, "Can't find tree object '%s'", &tree_entry->filename);
+
+	error = recurse_tree_entry(subtree, e, dir_sep+1);
+	git_tree_close(subtree);
+	return error;
+}
+
 int git_status_file(unsigned int *status_flags, git_repository *repo, const char *path)
 {
 	struct status_entry *e;
 	git_index *index = NULL;
-	git_index_entry *index_entry;
 	char temp_path[GIT_PATH_MAX];
-	int idx, error = GIT_SUCCESS;
+	int error = GIT_SUCCESS;
 	git_tree *tree = NULL;
-	struct stat filest;
 
 	assert(status_flags && repo && path);
 
@@ -338,21 +542,14 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 	if (git_futils_isdir(temp_path) == GIT_SUCCESS)
 		return git__throw(GIT_EINVALIDPATH, "Failed to determine status of file '%s'. Provided path leads to a folder, not a file", path);
 
-	e = new_status_entry(NULL, path);
+	e = status_entry_new(NULL, path);
 	if (e == NULL)
 		return GIT_ENOMEM;
 
 	/* Find file in Workdir */
 	if (git_futils_exists(temp_path) == GIT_SUCCESS) {
-		if (p_stat(temp_path, &filest) < GIT_SUCCESS) {
-			error = git__throw(GIT_EOSERR, "Failed to determine status of file '%s'. Can't read file", temp_path);
-			goto exit;
-		}
-
-		if (e->mtime.seconds == (git_time_t)filest.st_mtime)
-			git_oid_cpy(&e->wt_oid, &e->index_oid);
-		else
-			git_odb_hashfile(&e->wt_oid, temp_path, GIT_OBJ_BLOB);
+		if ((error = status_entry_update_from_workdir(e, temp_path)) < GIT_SUCCESS)
+			goto exit;	/* The callee has already set the error message */
 	}
 
 	/* Find file in Index */
@@ -361,12 +558,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 		goto exit;
 	}
 
-	idx = git_index_find(index, path);
-	if (idx >= 0) {
-		index_entry = git_index_get(index, idx);
-		git_oid_cpy(&e->index_oid, &index_entry->oid);
-		e->mtime = index_entry->mtime;
-	}
+	status_entry_update_from_index(e, index);
 	git_index_free(index);
 
 	if ((error = retrieve_head_tree(&tree, repo)) < GIT_SUCCESS) {
@@ -386,7 +578,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 	}
 
 	/* Determine status */
-	if ((error = set_status_flags(e)) < GIT_SUCCESS) {
+	if ((error = status_entry_update_flags(e)) < GIT_SUCCESS) {
 		error = git__throw(error, "Nonexistent file");
 		goto exit;
 	}
@@ -398,3 +590,95 @@ exit:
 	free(e);
 	return error;
 }
+
+/*
+ * git_futils_direach is not supposed to return entries in an ordered manner.
+ * alphasorted_futils_direach wraps git_futils_direach and invokes the callback
+ * function by passing it alphabeticcally sorted paths parameters.
+ *
+ */
+
+struct alphasorted_dirent_info {
+	int is_dir;
+
+	char path[GIT_FLEX_ARRAY]; /* more */
+};
+
+static struct alphasorted_dirent_info *alphasorted_dirent_info_new(const char *path)
+{
+	int is_dir;
+	struct alphasorted_dirent_info *di;
+
+	is_dir = git_futils_isdir(path) == GIT_SUCCESS ? 1 : 0;
+
+	di = git__malloc(sizeof(*di) + (is_dir ? GIT_PATH_MAX : strlen(path)) + 1);
+	if (di == NULL)
+		return NULL;
+
+	memset(di, 0x0, sizeof(*di));
+
+	strcpy(di->path, path);
+	di->is_dir = is_dir;
+
+	return di;
+}
+
+int alphasorted_dirent_info_cmp(const void *a, const void *b)
+{
+	struct alphasorted_dirent_info *stra = (struct alphasorted_dirent_info *)a;
+	struct alphasorted_dirent_info *strb = (struct alphasorted_dirent_info *)b;
+
+	return strcmp(stra->path, strb->path);
+}
+
+static int alphasorted_dirent_cb(void *state, char *full_path)
+{
+	struct alphasorted_dirent_info *entry;
+	git_vector *entry_names;
+
+	entry_names = (git_vector *)state;
+	entry = alphasorted_dirent_info_new(full_path);
+
+	if (entry == NULL)
+		return GIT_ENOMEM;
+
+	if (git_vector_insert(entry_names, entry) < GIT_SUCCESS) {
+		free(entry);
+		return GIT_ENOMEM;
+	}
+
+	return GIT_SUCCESS;
+}
+
+static int alphasorted_futils_direach(
+	char *path,
+	size_t path_sz,
+	int (*fn)(void *, char *),
+	void *arg)
+{
+	struct alphasorted_dirent_info *entry;
+	git_vector entry_names;
+	unsigned int idx;
+	int error = GIT_SUCCESS;
+
+	if (git_vector_init(&entry_names, 16, alphasorted_dirent_info_cmp) < GIT_SUCCESS)
+		return GIT_ENOMEM;
+
+	error = git_futils_direach(path, path_sz, alphasorted_dirent_cb, &entry_names);
+
+	git_vector_sort(&entry_names);
+
+	for (idx = 0; idx < entry_names.length; ++idx) {
+		entry = (struct alphasorted_dirent_info *)git_vector_get(&entry_names, idx);
+
+		if (error == GIT_SUCCESS) {
+			((struct status_st *)arg)->is_dir = entry->is_dir;
+			error = fn(arg, entry->path);
+		}
+
+		free(entry);
+	}
+
+	git_vector_free(&entry_names);
+	return error;
+}
\ No newline at end of file
diff --git a/tests/t18-status.c b/tests/t18-status.c
index 774dab6..1995111 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -33,15 +33,26 @@ static const char *test_blob_oid = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a";
 #define STATUS_WORKDIR_FOLDER TEST_RESOURCES "/status/"
 #define STATUS_REPOSITORY_TEMP_FOLDER TEMP_REPO_FOLDER ".gitted/"
 
+static int file_create(const char *filename, const char *content)
+{
+	int fd;
+
+	fd = p_creat(filename, 0644);
+	if (fd == 0)
+		return GIT_ERROR;
+	if (p_write(fd, content, strlen(content)) != 0)
+		return GIT_ERROR;
+	if (p_close(fd) != 0)
+		return GIT_ERROR;
+
+	return GIT_SUCCESS;
+}
+
 BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
 	git_oid expected_id, actual_id;
 	char filename[] = "new_file";
-	int fd;
 
-	fd = p_creat(filename, 0644);
-	must_pass(fd);
-	must_pass(p_write(fd, "new_file\n", 9));
-	must_pass(p_close(fd));
+	must_pass(file_create(filename, "new_file\n\0"));
 
 	must_pass(git_odb_hashfile(&actual_id, filename, GIT_OBJ_BLOB));
 
@@ -51,8 +62,7 @@ BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
 	must_pass(p_unlink(filename));
 END_TEST
 
-static const char *entry_paths[] = {
-	"current_file",
+static const char *entry_paths0[] = {
 	"file_deleted",
 	"modified_file",
 	"new_file",
@@ -65,13 +75,12 @@ static const char *entry_paths[] = {
 	"staged_new_file_deleted_file",
 	"staged_new_file_modified_file",
 
-	"subdir/current_file",
 	"subdir/deleted_file",
 	"subdir/modified_file",
 	"subdir/new_file",
 };
-static const unsigned int entry_statuses[] = {
-	GIT_STATUS_CURRENT,
+
+static const unsigned int entry_statuses0[] = {
 	GIT_STATUS_WT_DELETED,
 	GIT_STATUS_WT_MODIFIED,
 	GIT_STATUS_WT_NEW,
@@ -84,38 +93,41 @@ static const unsigned int entry_statuses[] = {
 	GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_DELETED,
 	GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED,
 
-	GIT_STATUS_CURRENT,
 	GIT_STATUS_WT_DELETED,
 	GIT_STATUS_WT_MODIFIED,
 	GIT_STATUS_WT_NEW,
 };
-#define ENTRY_COUNT 16
-
-static unsigned int get_expected_entry_status(const char *path)
-{
-	int i;
 
-	for (i = 0; i < ENTRY_COUNT; ++i)
-		if (!strcmp(path, entry_paths[i]))
-			return entry_statuses[i];
-
-	return (unsigned int)-1;
-}
+#define ENTRY_COUNT0 14
 
 struct status_entry_counts {
 	int wrong_status_flags_count;
+	int wrong_sorted_path;
 	int entry_count;
+	const unsigned int* expected_statuses;
+	const char** expected_paths;
+	int expected_entry_count;
 };
 
 static int status_cb(const char *path, unsigned int status_flags, void *payload)
 {
-	unsigned int expected_status_flags = get_expected_entry_status(path);
 	struct status_entry_counts *counts = (struct status_entry_counts *)payload;
 
-	counts->entry_count++;
-	if (status_flags != expected_status_flags)
+	if (counts->entry_count > counts->expected_entry_count) {
+		counts->wrong_status_flags_count++;
+		goto exit;
+	}
+
+	if (strcmp(path, counts->expected_paths[counts->entry_count])) {
+		counts->wrong_sorted_path++;
+		goto exit;
+	}
+
+	if (status_flags != counts->expected_statuses[counts->entry_count])
 		counts->wrong_status_flags_count++;
 
+exit:
+	counts->entry_count++;
 	return GIT_SUCCESS;
 }
 
@@ -128,9 +140,195 @@ BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
 	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
 
 	memset(&counts, 0x0, sizeof(struct status_entry_counts));
+	counts.expected_entry_count = ENTRY_COUNT0;
+	counts.expected_paths = entry_paths0;
+	counts.expected_statuses = entry_statuses0;
+
+	git_status_foreach(repo, status_cb, &counts);
+	must_be_true(counts.entry_count == counts.expected_entry_count);
+	must_be_true(counts.wrong_status_flags_count == 0);
+	must_be_true(counts.wrong_sorted_path == 0);
+
+	git_repository_free(repo);
+
+	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
+END_TEST
+
+static int status_cb1(const char *path, unsigned int status_flags, void *payload)
+{
+	int *count = (int *)payload;;
+
+	GIT_UNUSED_ARG(path);
+	GIT_UNUSED_ARG(status_flags);
+
+	*count++;
+
+	return GIT_SUCCESS;
+}
+
+BEGIN_TEST(statuscb1, "test retrieving status for a worktree of an empty repository")
+	git_repository *repo;
+	int count = 0;
+
+	must_pass(copydir_recurs(EMPTY_REPOSITORY_FOLDER, TEST_STD_REPO_FOLDER));
+	must_pass(remove_placeholders(TEST_STD_REPO_FOLDER, "dummy-marker.txt"));
+	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
+
+	git_status_foreach(repo, status_cb1, &count);
+	must_be_true(count == 0);
+
+	git_repository_free(repo);
+
+	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
+END_TEST
+
+static const char *entry_paths2[] = {
+	"current_file",
+	"file_deleted",
+	"modified_file",
+	"staged_changes",
+	"staged_changes_file_deleted",
+	"staged_changes_modified_file",
+	"staged_delete_file_deleted",
+	"staged_delete_modified_file",
+	"staged_new_file",
+	"staged_new_file_deleted_file",
+	"staged_new_file_modified_file",
+	"subdir/current_file",
+	"subdir/deleted_file",
+	"subdir/modified_file",
+};
+
+static const unsigned int entry_statuses2[] = {
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_INDEX_DELETED,
+	GIT_STATUS_INDEX_DELETED,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+};
+
+#define ENTRY_COUNT2 14
+
+BEGIN_TEST(statuscb2, "test retrieving status for a purged worktree of an valid repository")
+	git_repository *repo;
+	struct status_entry_counts counts;
+
+	must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
+	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
+	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
+
+	/* Purging the working */
+	must_pass(p_unlink(TEMP_REPO_FOLDER "current_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "modified_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "new_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "staged_changes"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "staged_changes_modified_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "staged_delete_modified_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "staged_new_file"));
+	must_pass(p_unlink(TEMP_REPO_FOLDER "staged_new_file_modified_file"));
+	must_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER "subdir", 1));
+
+	memset(&counts, 0x0, sizeof(struct status_entry_counts));
+	counts.expected_entry_count = ENTRY_COUNT2;
+	counts.expected_paths = entry_paths2;
+	counts.expected_statuses = entry_statuses2;
+
+	git_status_foreach(repo, status_cb, &counts);
+	must_be_true(counts.entry_count == counts.expected_entry_count);
+	must_be_true(counts.wrong_status_flags_count == 0);
+	must_be_true(counts.wrong_sorted_path == 0);
+
+	git_repository_free(repo);
+
+	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
+END_TEST
+
+static const char *entry_paths3[] = {
+	".HEADER",
+	"42-is-not-prime.sigh",
+	"README.md",
+	"current_file",
+	"current_file/current_file",
+	"current_file/modified_file",
+	"current_file/new_file",
+	"file_deleted",
+	"modified_file",
+	"new_file",
+	"staged_changes",
+	"staged_changes_file_deleted",
+	"staged_changes_modified_file",
+	"staged_delete_file_deleted",
+	"staged_delete_modified_file",
+	"staged_new_file",
+	"staged_new_file_deleted_file",
+	"staged_new_file_modified_file",
+	"subdir",
+	"subdir/current_file",
+	"subdir/deleted_file",
+	"subdir/modified_file",
+};
+
+static const unsigned int entry_statuses3[] = {
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_MODIFIED,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED,
+	GIT_STATUS_INDEX_DELETED,
+	GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED,
+	GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW,
+	GIT_STATUS_WT_NEW,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+	GIT_STATUS_WT_DELETED,
+};
+
+#define ENTRY_COUNT3 22
+
+BEGIN_TEST(statuscb3, "test retrieving status for a worktree where a file and a subdir have been renamed and some files have been added")
+	git_repository *repo;
+	struct status_entry_counts counts;
+
+	must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
+	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
+	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
+
+	must_pass(git_futils_mv_atomic(TEMP_REPO_FOLDER "current_file", TEMP_REPO_FOLDER "swap"));
+	must_pass(git_futils_mv_atomic(TEMP_REPO_FOLDER "subdir", TEMP_REPO_FOLDER "current_file"));
+	must_pass(git_futils_mv_atomic(TEMP_REPO_FOLDER "swap", TEMP_REPO_FOLDER "subdir"));
+
+	must_pass(file_create(TEMP_REPO_FOLDER ".HEADER", "dummy"));
+	must_pass(file_create(TEMP_REPO_FOLDER "42-is-not-prime.sigh", "dummy"));
+	must_pass(file_create(TEMP_REPO_FOLDER "README.md", "dummy"));
+
+	memset(&counts, 0x0, sizeof(struct status_entry_counts));
+	counts.expected_entry_count = ENTRY_COUNT3;
+	counts.expected_paths = entry_paths3;
+	counts.expected_statuses = entry_statuses3;
+
 	git_status_foreach(repo, status_cb, &counts);
-	must_be_true(counts.entry_count == ENTRY_COUNT);
+	must_be_true(counts.entry_count == counts.expected_entry_count);
 	must_be_true(counts.wrong_status_flags_count == 0);
+	must_be_true(counts.wrong_sorted_path == 0);
 
 	git_repository_free(repo);
 
@@ -146,9 +344,9 @@ BEGIN_TEST(singlestatus0, "test retrieving status for single file")
 	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
 	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
 
-	for (i = 0; i < ENTRY_COUNT; ++i) {
-		must_pass(git_status_file(&status_flags, repo, entry_paths[i]));
-		must_be_true(status_flags == entry_statuses[i]);
+	for (i = 0; i < ENTRY_COUNT0; ++i) {
+		must_pass(git_status_file(&status_flags, repo, entry_paths0[i]));
+		must_be_true(status_flags == entry_statuses0[i]);
 	}
 
 	git_repository_free(repo);
@@ -238,6 +436,9 @@ BEGIN_SUITE(status)
 	ADD_TEST(file0);
 
 	ADD_TEST(statuscb0);
+	ADD_TEST(statuscb1);
+	ADD_TEST(statuscb2);
+	ADD_TEST(statuscb3);
 
 	ADD_TEST(singlestatus0);
 	ADD_TEST(singlestatus1);