Commit 5f4a61aea834fe25ce1596bc9c0e0b5e563aa98b

Russell Belfer 2012-08-09T19:43:25

Working implementation of git_submodule_status This is a big redesign of the git_submodule_status API and the implementation of the redesigned API. It also fixes a number of bugs that I found in other parts of the submodule API while writing the tests for the status part. This also fixes a couple of bugs in the iterators that had not been noticed before - one with iterating when there is a gitlink (i.e. separate-work-dir) and one where I was treating anything even vaguely submodule-like as a submodule, more aggressively than core git does.

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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
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
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 79ef7a4..088e1ec 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -391,6 +391,21 @@ GIT_EXTERN(int) git_diff_print_patch(
 	void *cb_data,
 	git_diff_data_fn print_cb);
 
+/**
+ * Query how many diff records are there in a diff list.
+ *
+ * You can optionally pass in a `git_delta_t` value if you want a count
+ * of just entries that match that delta type, or pass -1 for all delta
+ * records.
+ *
+ * @param diff A git_diff_list generated by one of the above functions
+ * @param delta_t A git_delta_t value to filter the count, or -1 for all records
+ * @return Count of number of deltas matching delta_t type
+ */
+GIT_EXTERN(int) git_diff_entrycount(
+	git_diff_list *diff,
+	int delta_t);
+
 /**@}*/
 
 
diff --git a/include/git2/oid.h b/include/git2/oid.h
index 887b33e..9e54a9f 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -185,6 +185,8 @@ GIT_EXTERN(int) git_oid_streq(const git_oid *a, const char *str);
 
 /**
  * Check is an oid is all zeros.
+ *
+ * @return 1 if all zeros, 0 otherwise.
  */
 GIT_EXTERN(int) git_oid_iszero(const git_oid *a);
 
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 6cd6646..fe7f26c 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -61,83 +61,67 @@ typedef enum {
 } git_submodule_ignore_t;
 
 /**
- * Status values for submodules.
- *
- * One of these values will be returned for the submodule in the index
- * relative to the HEAD tree, and one will be returned for the submodule in
- * the working directory relative to the index.  The value can be extracted
- * from the actual submodule status return value using one of the macros
- * below (see GIT_SUBMODULE_INDEX_STATUS and GIT_SUBMODULE_WD_STATUS).
- */
-enum {
-	GIT_SUBMODULE_STATUS_CLEAN = 0,
-	GIT_SUBMODULE_STATUS_ADDED = 1,
-	GIT_SUBMODULE_STATUS_REMOVED = 2,
-	GIT_SUBMODULE_STATUS_REMOVED_TYPE_CHANGE = 3,
-	GIT_SUBMODULE_STATUS_MODIFIED = 4,
-	GIT_SUBMODULE_STATUS_MODIFIED_AHEAD = 5,
-	GIT_SUBMODULE_STATUS_MODIFIED_BEHIND = 6
-};
-
-/**
  * Return codes for submodule status.
  *
- * A combination of these flags (and shifted values of the
- * GIT_SUBMODULE_STATUS codes above) will be returned to describe the status
- * of a submodule.
+ * A combination of these flags will be returned to describe the status of a
+ * submodule.  Depending on the "ignore" property of the submodule, some of
+ * the flags may never be returned because they indicate changes that are
+ * supposed to be ignored.
  *
  * Submodule info is contained in 4 places: the HEAD tree, the index, config
  * files (both .git/config and .gitmodules), and the working directory.  Any
  * or all of those places might be missing information about the submodule
- * depending on what state the repo is in.
- *
- * When you ask for submodule status, we consider all four places and return
- * a combination of the flags below.  Also, we also compare HEAD to index to
- * workdir, and return a relative status code (see above) for the
- * comparisons.  Use the GIT_SUBMODULE_INDEX_STATUS() and
- * GIT_SUBMODULE_WD_STATUS() macros to extract these status codes from the
- * results.  As an example, if the submodule exists in the HEAD and does not
- * exist in the index, then using GIT_SUBMODULE_INDEX_STATUS(st) will return
- * GIT_SUBMODULE_STATUS_REMOVED.
- *
- * The ignore settings for the submodule will control how much status info
- * you get about the working directory.  For example, with ignore ALL, the
- * workdir will always show as clean.  With any ignore level below NONE,
- * you will never get the WD_HAS_UNTRACKED value back.
- *
- * The other SUBMODULE_STATUS values you might see are:
- *
- * - IN_HEAD means submodule exists in HEAD tree
- * - IN_INDEX means submodule exists in index
- * - IN_CONFIG means submodule exists in config
- * - IN_WD means submodule exists in workdir and looks like a submodule
- * - WD_CHECKED_OUT means submodule in workdir has .git content
- * - WD_HAS_UNTRACKED means workdir contains untracked files.  This would
- *   only ever be returned for ignore value GIT_SUBMODULE_IGNORE_NONE.
- * - WD_MISSING_COMMITS means workdir repo is out of date and does not
- *   contain the SHAs from either the index or the HEAD tree
- */
-#define GIT_SUBMODULE_STATUS_IN_HEAD             (1u << 0)
-#define GIT_SUBMODULE_STATUS_IN_INDEX            (1u << 1)
-#define GIT_SUBMODULE_STATUS_IN_CONFIG           (1u << 2)
-#define GIT_SUBMODULE_STATUS_IN_WD               (1u << 3)
-#define GIT_SUBMODULE_STATUS_INDEX_DATA_OFFSET         (4)
-#define GIT_SUBMODULE_STATUS_WD_DATA_OFFSET            (7)
-#define GIT_SUBMODULE_STATUS_WD_CHECKED_OUT     (1u << 10)
-#define GIT_SUBMODULE_STATUS_WD_HAS_UNTRACKED   (1u << 11)
-#define GIT_SUBMODULE_STATUS_WD_MISSING_COMMITS (1u << 12)
-
-/**
- * Extract submodule status value for index from status mask.
- */
-#define GIT_SUBMODULE_INDEX_STATUS(s)  \
-	(((s) >> GIT_SUBMODULE_STATUS_INDEX_DATA_OFFSET) & 0x07)
-
-/**
- * Extract submodule status value for working directory from status mask.
+ * depending on what state the repo is in.  We consider all four places to
+ * build the combination of status flags.
+ *
+ * There are four values that are not really status, but give basic info
+ * about what sources of submodule data are available.  These will be
+ * returned even if ignore is set to "ALL".
+ *
+ * * IN_HEAD   - superproject head contains submodule
+ * * IN_INDEX  - superproject index contains submodule
+ * * IN_CONFIG - superproject gitmodules has submodule
+ * * IN_WD     - superproject workdir has submodule
+ *
+ * The following values will be returned so long as ignore is not "ALL".
+ *
+ * * INDEX_ADDED       - in index, not in head
+ * * INDEX_DELETED     - in head, not in index
+ * * INDEX_MODIFIED    - index and head don't match
+ * * WD_UNINITIALIZED  - workdir contains empty directory
+ * * WD_ADDED          - in workdir, not index
+ * * WD_DELETED        - in index, not workdir
+ * * WD_MODIFIED       - index and workdir head don't match
+ *
+ * The following can only be returned if ignore is "NONE" or "UNTRACKED".
+ *
+ * * WD_INDEX_MODIFIED - submodule workdir index is dirty
+ * * WD_WD_MODIFIED    - submodule workdir has modified files
+ *
+ * Lastly, the following will only be returned for ignore "NONE".
+ *
+ * * WD_UNTRACKED      - wd contains untracked files
  */
-#define GIT_SUBMODULE_WD_STATUS(s)     \
-	(((s) >> GIT_SUBMODULE_STATUS_WD_DATA_OFFSET) & 0x07)
+typedef enum {
+	 GIT_SUBMODULE_STATUS_IN_HEAD           = (1u << 0),
+	 GIT_SUBMODULE_STATUS_IN_INDEX          = (1u << 1),
+	 GIT_SUBMODULE_STATUS_IN_CONFIG         = (1u << 2),
+	 GIT_SUBMODULE_STATUS_IN_WD             = (1u << 3),
+	 GIT_SUBMODULE_STATUS_INDEX_ADDED       = (1u << 4),
+	 GIT_SUBMODULE_STATUS_INDEX_DELETED     = (1u << 5),
+	 GIT_SUBMODULE_STATUS_INDEX_MODIFIED    = (1u << 6),
+	 GIT_SUBMODULE_STATUS_WD_UNINITIALIZED  = (1u << 7),
+	 GIT_SUBMODULE_STATUS_WD_ADDED          = (1u << 8),
+	 GIT_SUBMODULE_STATUS_WD_DELETED        = (1u << 9),
+	 GIT_SUBMODULE_STATUS_WD_MODIFIED       = (1u << 10),
+	 GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED = (1u << 11),
+	 GIT_SUBMODULE_STATUS_WD_WD_MODIFIED    = (1u << 12),
+	 GIT_SUBMODULE_STATUS_WD_UNTRACKED      = (1u << 13),
+} git_submodule_status_t;
+
+#define GIT_SUBMODULE_STATUS_IS_UNMODIFIED(S) \
+	(((S) & ~(GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX | \
+	GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD)) == 0)
 
 /**
  * Lookup submodule information by name or path.
@@ -206,7 +190,7 @@ GIT_EXTERN(int) git_submodule_foreach(
  *
  * To fully emulate "git submodule add" call this function, then open the
  * submodule repo and perform the clone step as needed.  Lastly, call
- * `git_submodule_add_finalize` to wrap up adding the new submodule and
+ * `git_submodule_add_finalize()` to wrap up adding the new submodule and
  * .gitmodules to the index to be ready to commit.
  *
  * @param submodule The newly created submodule ready to open for clone
@@ -232,22 +216,33 @@ GIT_EXTERN(int) git_submodule_add_setup(
  * and done the clone of the submodule.  This adds the .gitmodules file
  * and the newly cloned submodule to the index to be ready to be committed
  * (but doesn't actually do the commit).
+ *
+ * @param submodule The submodule to finish adding.
  */
 GIT_EXTERN(int) git_submodule_add_finalize(git_submodule *submodule);
 
 /**
  * Add current submodule HEAD commit to index of superproject.
+ *
+ * @param submodule The submodule to add to the index
+ * @param write_index Boolean if this should immediately write the index
+ *            file.  If you pass this as false, you will have to get the
+ *            git_index and explicitly call `git_index_write()` on it to
+ *            save the change.
+ * @return 0 on success, <0 on failure
  */
-GIT_EXTERN(int) git_submodule_add_to_index(git_submodule *submodule);
+GIT_EXTERN(int) git_submodule_add_to_index(
+	git_submodule *submodule,
+	int write_index);
 
 /**
  * Write submodule settings to .gitmodules file.
  *
  * This commits any in-memory changes to the submodule to the gitmodules
- * file on disk.  You may also be interested in `git_submodule_init` which
+ * file on disk.  You may also be interested in `git_submodule_init()` which
  * writes submodule info to ".git/config" (which is better for local changes
- * to submodule settings) and/or `git_submodule_sync` which writes settings
- * about remotes to the actual submodule repository.
+ * to submodule settings) and/or `git_submodule_sync()` which writes
+ * settings about remotes to the actual submodule repository.
  *
  * @param submodule The submodule to write.
  * @return 0 on success, <0 on failure.
@@ -259,7 +254,7 @@ GIT_EXTERN(int) git_submodule_save(git_submodule *submodule);
  *
  * This returns a pointer to the repository that contains the submodule.
  * This is a just a reference to the repository that was passed to the
- * original `git_submodule_lookup` call, so if that repository has been
+ * original `git_submodule_lookup()` call, so if that repository has been
  * freed, then this may be a dangling reference.
  *
  * @param submodule Pointer to submodule object
@@ -300,8 +295,8 @@ GIT_EXTERN(const char *) git_submodule_url(git_submodule *submodule);
  * This sets the URL in memory for the submodule. This will be used for
  * any following submodule actions while this submodule data is in memory.
  *
- * After calling this, you may wish to call `git_submodule_save` to write
- * the changes back to the ".gitmodules" file and `git_submodule_sync` to
+ * After calling this, you may wish to call `git_submodule_save()` to write
+ * the changes back to the ".gitmodules" file and `git_submodule_sync()` to
  * write the changes to the checked out submodule repository.
  *
  * @param submodule Pointer to the submodule object
@@ -331,8 +326,8 @@ GIT_EXTERN(const git_oid *) git_submodule_head_oid(git_submodule *submodule);
  *
  * This returns the OID that corresponds to looking up 'HEAD' in the checked
  * out submodule.  If there are pending changes in the index or anything
- * else, this won't notice that.  You should call `git_submodule_status` for
- * a more complete picture about the state of the working directory.
+ * else, this won't notice that.  You should call `git_submodule_status()`
+ * for a more complete picture about the state of the working directory.
  *
  * @param submodule Pointer to submodule object
  * @return Pointer to git_oid or NULL if submodule is not checked out.
@@ -348,7 +343,7 @@ GIT_EXTERN(const git_oid *) git_submodule_wd_oid(git_submodule *submodule);
  *    of the submodule from a clean checkout to be dirty, including the
  *    addition of untracked files.  This is the default if unspecified.
  *  - **GIT_SUBMODULE_IGNORE_UNTRACKED** examines the contents of the
- *    working tree (i.e. call `git_status_foreach` on the submodule) but
+ *    working tree (i.e. call `git_status_foreach()` on the submodule) but
  *    UNTRACKED files will not count as making the submodule dirty.
  *  - **GIT_SUBMODULE_IGNORE_DIRTY** means to only check if the HEAD of the
  *    submodule has moved for status.  This is fast since it does not need to
@@ -364,12 +359,12 @@ GIT_EXTERN(git_submodule_ignore_t) git_submodule_ignore(
  * Set the ignore rule for the submodule.
  *
  * This sets the ignore rule in memory for the submodule.  This will be used
- * for any following actions (such as `git_submodule_status`) while the
- * submodule is in memory.  You should call `git_submodule_save` if you want
- * to persist the new ignore role.
+ * for any following actions (such as `git_submodule_status()`) while the
+ * submodule is in memory.  You should call `git_submodule_save()` if you
+ * want to persist the new ignore role.
  *
  * Calling this again with GIT_SUBMODULE_IGNORE_DEFAULT or calling
- * `git_submodule_reload` will revert the rule to the value that was in the
+ * `git_submodule_reload()` will revert the rule to the value that was in the
  * original config.
  *
  * @return old value for ignore
@@ -388,10 +383,10 @@ GIT_EXTERN(git_submodule_update_t) git_submodule_update(
  * Set the update rule for the submodule.
  *
  * This sets the update rule in memory for the submodule.  You should call
- * `git_submodule_save` if you want to persist the new update rule.
+ * `git_submodule_save()` if you want to persist the new update rule.
  *
  * Calling this again with GIT_SUBMODULE_UPDATE_DEFAULT or calling
- * `git_submodule_reload` will revert the rule to the value that was in the
+ * `git_submodule_reload()` will revert the rule to the value that was in the
  * original config.
  *
  * @return old value for update
@@ -429,7 +424,7 @@ GIT_EXTERN(int) git_submodule_sync(git_submodule *submodule);
  * Open the repository for a submodule.
  *
  * This is a newly opened repository object.  The caller is responsible for
- * calling `git_repository_free` on it when done.  Multiple calls to this
+ * calling `git_repository_free()` on it when done.  Multiple calls to this
  * function will return distinct `git_repository` objects.  This will only
  * work if the submodule is checked out into the working directory.
  *
@@ -462,10 +457,10 @@ GIT_EXTERN(int) git_submodule_reload_all(git_repository *repo);
  * This looks at a submodule and tries to determine the status.  It
  * will return a combination of the `GIT_SUBMODULE_STATUS` values above.
  * How deeply it examines the working directory to do this will depend
- * on the `git_submodule_ignore_t` value for the submodule (which can be
- * overridden with `git_submodule_set_ignore()`).
+ * on the `git_submodule_ignore_t` value for the submodule - which can be
+ * set either temporarily or permanently with `git_submodule_set_ignore()`.
  *
- * @param status Combination of GIT_SUBMODULE_STATUS values from above.
+ * @param status Combination of `GIT_SUBMODULE_STATUS` flags
  * @param submodule Submodule for which to get status
  * @return 0 on success, <0 on error
  */
diff --git a/src/diff_output.c b/src/diff_output.c
index d269a4c..2bf939f 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -718,6 +718,25 @@ int git_diff_print_patch(
 	return error;
 }
 
+int git_diff_entrycount(git_diff_list *diff, int delta_t)
+{
+	int count = 0;
+	unsigned int i;
+	git_diff_delta *delta;
+
+	assert(diff);
+
+	if (delta_t < 0)
+		return diff->deltas.length;
+
+	git_vector_foreach(&diff->deltas, i, delta) {
+		if (delta->status == (git_delta_t)delta_t)
+			count++;
+	}
+
+	return count;
+}
+
 int git_diff_blobs(
 	git_blob *old_blob,
 	git_blob *new_blob,
diff --git a/src/iterator.c b/src/iterator.c
index 819b0e2..92fe671 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -525,7 +525,9 @@ static int workdir_iterator__advance(
 	while ((wf = wi->stack) != NULL) {
 		next = git_vector_get(&wf->entries, ++wf->index);
 		if (next != NULL) {
-			if (strcmp(next->path, DOT_GIT "/") == 0)
+			/* match git's behavior of ignoring anything named ".git" */
+			if (strcmp(next->path, DOT_GIT "/") == 0 ||
+				strcmp(next->path, DOT_GIT) == 0)
 				continue;
 			/* else found a good entry */
 			break;
@@ -607,8 +609,8 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
 
 	wi->entry.path = ps->path;
 
-	/* skip over .git directory */
-	if (strcmp(ps->path, DOT_GIT "/") == 0)
+	/* skip over .git entry */
+	if (strcmp(ps->path, DOT_GIT "/") == 0 || strcmp(ps->path, DOT_GIT) == 0)
 		return workdir_iterator__advance((git_iterator *)wi, NULL);
 
 	/* if there is an error processing the entry, treat as ignored */
@@ -629,15 +631,10 @@ static int workdir_iterator__update_entry(workdir_iterator *wi)
 
 	/* detect submodules */
 	if (S_ISDIR(wi->entry.mode)) {
-		bool is_submodule = git_path_contains(&wi->path, DOT_GIT);
-
-		/* if there is no .git, still check submodules data */
-		if (!is_submodule) {
-			int res = git_submodule_lookup(NULL, wi->repo, wi->entry.path);
-			is_submodule = (res == 0);
-			if (res == GIT_ENOTFOUND)
-				giterr_clear();
-		}
+		int res = git_submodule_lookup(NULL, wi->repo, wi->entry.path);
+		bool is_submodule = (res == 0);
+		if (res == GIT_ENOTFOUND)
+			giterr_clear();
 
 		/* if submodule, mark as GITLINK and remove trailing slash */
 		if (is_submodule) {
diff --git a/src/repository.c b/src/repository.c
index 18788d1..c12df25 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -146,8 +146,13 @@ static int load_workdir(git_repository *repo, git_buf *parent_path)
 		return -1;
 
 	error = git_config_get_string(&worktree, config, "core.worktree");
-	if (!error && worktree != NULL)
-		repo->workdir = git__strdup(worktree);
+	if (!error && worktree != NULL) {
+		error = git_path_prettify_dir(
+			&worktree_buf, worktree, repo->path_repository);
+		if (error < 0)
+			return error;
+		repo->workdir = git_buf_detach(&worktree_buf);
+	}
 	else if (error != GIT_ENOTFOUND)
 		return error;
 	else {
diff --git a/src/submodule.c b/src/submodule.c
index 3ebb362..15501a1 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -42,7 +42,7 @@ static kh_inline khint_t str_hash_no_trailing_slash(const char *s)
 	khint_t h;
 
 	for (h = 0; *s; ++s)
-		if (s[1] || *s != '/')
+		if (s[1] != '\0' || *s != '/')
 			h = (h << 5) - h + *s;
 
 	return h;
@@ -53,9 +53,9 @@ static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b)
 	size_t alen = a ? strlen(a) : 0;
 	size_t blen = b ? strlen(b) : 0;
 
-	if (alen && a[alen] == '/')
+	if (alen > 0 && a[alen - 1] == '/')
 		alen--;
-	if (blen && b[blen] == '/')
+	if (blen > 0 && b[blen - 1] == '/')
 		blen--;
 
 	return (alen == blen && strncmp(a, b, alen) == 0);
@@ -65,24 +65,19 @@ __KHASH_IMPL(
 	str, static kh_inline, const char *, void *, 1,
 	str_hash_no_trailing_slash, str_equal_no_trailing_slash);
 
-static int load_submodule_config(
-	git_repository *repo, bool force);
-static git_config_file *open_gitmodules(
-	git_repository *, bool, const git_oid *);
-static int lookup_head_remote(
-	git_buf *url, git_repository *repo);
-static int submodule_get(
-	git_submodule **, git_repository *, const char *, const char *);
-static void submodule_release(
-	git_submodule *sm, int decr);
-static int submodule_load_from_index(
-	git_repository *, const git_index_entry *);
-static int submodule_load_from_head(
-	git_repository *, const char *, const git_oid *);
-static int submodule_load_from_config(
-	const char *, const char *, void *);
-static int submodule_update_config(
-	git_submodule *, const char *, const char *, bool,	bool);
+static int load_submodule_config(git_repository *repo, bool force);
+static git_config_file *open_gitmodules(git_repository *, bool, const git_oid *);
+static int lookup_head_remote(git_buf *url, git_repository *repo);
+static int submodule_get(git_submodule **, git_repository *, const char *, const char *);
+static void submodule_release(git_submodule *sm, int decr);
+static int submodule_load_from_index(git_repository *, const git_index_entry *);
+static int submodule_load_from_head(git_repository*, const char*, const git_oid*);
+static int submodule_load_from_config(const char *, const char *, void *);
+static int submodule_load_from_wd_lite(git_submodule *, const char *, void *);
+static int submodule_update_config(git_submodule *, const char *, const char *, bool, bool);
+static void submodule_mode_mismatch(git_repository *, const char *, unsigned int);
+static int submodule_index_status(unsigned int *status, git_submodule *sm);
+static int submodule_wd_status(unsigned int *status, git_submodule *sm);
 
 static int submodule_cmp(const void *a, const void *b)
 {
@@ -167,8 +162,10 @@ int git_submodule_foreach(
 				break;
 		}
 
-		if ((error = callback(sm, sm->name, payload)) < 0)
+		if (callback(sm, sm->name, payload)) {
+			error = GIT_EUSER;
 			break;
+		}
 	});
 
 	git_vector_free(&seen);
@@ -337,10 +334,10 @@ int git_submodule_add_finalize(git_submodule *sm)
 		(error = git_index_add(index, GIT_MODULES_FILE, 0)) < 0)
 		return error;
 
-	return git_submodule_add_to_index(sm);
+	return git_submodule_add_to_index(sm, true);
 }
 
-int git_submodule_add_to_index(git_submodule *sm)
+int git_submodule_add_to_index(git_submodule *sm, int write_index)
 {
 	int error;
 	git_repository *repo, *sm_repo;
@@ -354,6 +351,9 @@ int git_submodule_add_to_index(git_submodule *sm)
 
 	repo = sm->owner;
 
+	/* force reload of wd OID by git_submodule_open */
+	sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID;
+
 	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
 		(error = git_buf_joinpath(
 			&path, git_repository_workdir(repo), sm->path)) < 0 ||
@@ -367,6 +367,7 @@ int git_submodule_add_to_index(git_submodule *sm)
 		error = -1;
 		goto cleanup;
 	}
+	entry.path = sm->path;
 	git_index__init_entry_from_stat(&st, &entry);
 
 	/* calling git_submodule_open will have set sm->wd_oid if possible */
@@ -388,9 +389,17 @@ int git_submodule_add_to_index(git_submodule *sm)
 
 	git_commit_free(head);
 
-	/* now add it */
+	/* add it */
 	error = git_index_add2(index, &entry);
 
+	/* write it, if requested */
+	if (!error && write_index) {
+		error = git_index_write(index);
+
+		if (!error)
+			git_oid_cpy(&sm->index_oid, &sm->wd_oid);
+	}
+
 cleanup:
 	git_repository_free(sm_repo);
 	git_buf_free(&path);
@@ -501,7 +510,7 @@ int git_submodule_set_url(git_submodule *submodule, const char *url)
 	return 0;
 }
 
- const git_oid *git_submodule_index_oid(git_submodule *submodule)
+const git_oid *git_submodule_index_oid(git_submodule *submodule)
 {
 	assert(submodule);
 
@@ -531,6 +540,8 @@ const git_oid *git_submodule_wd_oid(git_submodule *submodule)
 		/* calling submodule open grabs the HEAD OID if possible */
 		if (!git_submodule_open(&subrepo, submodule))
 			git_repository_free(subrepo);
+		else
+			giterr_clear();
 	}
 
 	if (submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)
@@ -693,16 +704,21 @@ int git_submodule_reload(git_submodule *submodule)
 	if (git_repository_index__weakptr(&index, repo) < 0)
 		return -1;
 
+	submodule->flags = submodule->flags &
+		~(GIT_SUBMODULE_STATUS_IN_INDEX |
+		  GIT_SUBMODULE_STATUS__INDEX_OID_VALID);
+
 	pos = git_index_find(index, submodule->path);
 	if (pos >= 0) {
 		git_index_entry *entry = git_index_get(index, pos);
 
-		submodule->flags = submodule->flags &
-			~(GIT_SUBMODULE_STATUS_IN_INDEX |
-			  GIT_SUBMODULE_STATUS__INDEX_OID_VALID);
-
-		if ((error = submodule_load_from_index(repo, entry)) < 0)
-			return error;
+		if (S_ISGITLINK(entry->mode)) {
+			if ((error = submodule_load_from_index(repo, entry)) < 0)
+				return error;
+		} else {
+			submodule_mode_mismatch(
+				repo, entry->path, GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE);
+		}
 	}
 
 	/* refresh HEAD tree data */
@@ -715,7 +731,14 @@ int git_submodule_reload(git_submodule *submodule)
 			  GIT_SUBMODULE_STATUS__HEAD_OID_VALID);
 
 		if (!(error = git_tree_entry_bypath(&te, head, submodule->path))) {
-			error = submodule_load_from_head(repo, submodule->path, &te->oid);
+
+			if (S_ISGITLINK(te->attr)) {
+				error = submodule_load_from_head(repo, submodule->path, &te->oid);
+			} else {
+				submodule_mode_mismatch(
+					repo, submodule->path,
+					GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE);
+			}
 
 			git_tree_entry_free(te);
 		}
@@ -749,6 +772,16 @@ int git_submodule_reload(git_submodule *submodule)
 		git_config_file_free(mods);
 	}
 
+	if (error < 0)
+		return error;
+
+	/* refresh wd data */
+
+	submodule->flags = submodule->flags &
+		~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID);
+
+	error = submodule_load_from_wd_lite(submodule, submodule->path, NULL);
+
 	return error;
 }
 
@@ -756,16 +789,21 @@ int git_submodule_status(
 	unsigned int *status,
 	git_submodule *submodule)
 {
+	int error = 0;
+	unsigned int status_val;
+
 	assert(status && submodule);
 
-	GIT_UNUSED(status);
-	GIT_UNUSED(submodule);
+	status_val = GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(submodule->flags);
 
-	/* TODO: move status code from below and update */
+	if (submodule->ignore != GIT_SUBMODULE_IGNORE_ALL) {
+		if (!(error = submodule_index_status(&status_val, submodule)))
+			error = submodule_wd_status(&status_val, submodule);
+	}
 
-	*status = 0;
+	*status = status_val;
 
-	return 0;
+	return error;
 }
 
 /*
@@ -848,7 +886,7 @@ static int submodule_get(
 		/* insert value at name - if another thread beats us to it, then use
 		 * their record and release our own.
 		 */
-		pos = kh_put(str, smcfg, name, &error);
+		pos = kh_put(str, smcfg, sm->name, &error);
 
 		if (error < 0) {
 			submodule_release(sm, 1);
@@ -1037,6 +1075,18 @@ static int submodule_load_from_wd_lite(
 	return 0;
 }
 
+static void submodule_mode_mismatch(
+	git_repository *repo, const char *path, unsigned int flag)
+{
+	khiter_t pos = git_strmap_lookup_index(repo->submodules, path);
+
+	if (git_strmap_valid_index(repo->submodules, pos)) {
+		git_submodule *sm = git_strmap_value_at(repo->submodules, pos);
+
+		sm->flags |= flag;
+	}
+}
+
 static int load_submodule_config_from_index(
 	git_repository *repo, git_oid *gitmodules_oid)
 {
@@ -1055,8 +1105,13 @@ static int load_submodule_config_from_index(
 			error = submodule_load_from_index(repo, entry);
 			if (error < 0)
 				break;
-		} else if (strcmp(entry->path, GIT_MODULES_FILE) == 0)
-			git_oid_cpy(gitmodules_oid, &entry->oid);
+		} else {
+			submodule_mode_mismatch(
+				repo, entry->path, GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE);
+
+			if (strcmp(entry->path, GIT_MODULES_FILE) == 0)
+				git_oid_cpy(gitmodules_oid, &entry->oid);
+		}
 
 		error = git_iterator_advance(i, &entry);
 	}
@@ -1090,9 +1145,14 @@ static int load_submodule_config_from_head(
 			error = submodule_load_from_head(repo, entry->path, &entry->oid);
 			if (error < 0)
 				break;
-		} else if (strcmp(entry->path, GIT_MODULES_FILE) == 0 &&
-				   git_oid_iszero(gitmodules_oid))
-			git_oid_cpy(gitmodules_oid, &entry->oid);
+		} else {
+			submodule_mode_mismatch(
+				repo, entry->path, GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE);
+
+			if (strcmp(entry->path, GIT_MODULES_FILE) == 0 &&
+				git_oid_iszero(gitmodules_oid))
+				git_oid_cpy(gitmodules_oid, &entry->oid);
+		}
 
 		error = git_iterator_advance(i, &entry);
 	}
@@ -1303,183 +1363,108 @@ cleanup:
 	return error;
 }
 
-#if 0
-
-static int head_oid_for_submodule(
-	git_oid *oid,
-	git_repository *owner,
-	const char *path)
+static int submodule_index_status(unsigned int *status, git_submodule *sm)
 {
-	int error = 0;
-	git_oid head_oid;
-	git_tree *head_tree = NULL, *container_tree = NULL;
-	unsigned int pos;
-	const git_tree_entry *entry;
-
-	if (git_reference_name_to_oid(&head_oid, owner, GIT_HEAD_FILE) < 0 ||
-		git_tree_lookup(&head_tree, owner, &head_oid) < 0 ||
-		git_tree_resolve_path(&container_tree, &pos, head_tree, path) < 0 ||
-		(entry = git_tree_entry_byindex(container_tree, pos)) == NULL)
-	{
-		memset(oid, 0, sizeof(*oid));
-		error = GIT_ENOTFOUND;
-	}
-	else {
-		git_oid_cpy(oid, &entry->oid);
-	}
+	const git_oid *head_oid  = git_submodule_head_oid(sm);
+	const git_oid *index_oid = git_submodule_index_oid(sm);
 
-	git_tree_free(head_tree);
-	git_tree_free(container_tree);
+	if (!head_oid) {
+		if (index_oid)
+			*status |= GIT_SUBMODULE_STATUS_INDEX_ADDED;
+	}
+	else if (!index_oid)
+		*status |= GIT_SUBMODULE_STATUS_INDEX_DELETED;
+	else if (!git_oid_equal(head_oid, index_oid))
+		*status |= GIT_SUBMODULE_STATUS_INDEX_MODIFIED;
 
-	return error;
+	return 0;
 }
 
-int git_submodule_status(
-	unsigned int *status,
-	git_oid *head,
-	git_submodule *sm,
-	git_submodule_ignore_t ignore)
+static int submodule_wd_status(unsigned int *status, git_submodule *sm)
 {
-	int error;
-	const char *workdir;
-	git_repository *owner, *sm_repo = NULL;
-	git_oid owner_head, sm_head;
-
-	assert(submodule && status);
-
-	if (head == NULL)
-		head = &sm_head;
-
-	owner   = submodule->owner;
-	workdir = git_repository_workdir(owner);
-
-	if (ignore == GIT_SUBMODULE_IGNORE_DEFAULT)
-		ignore = sm->ignore;
-
-	/* if this is a bare repo or the submodule dir has no .git yet,
-	 * then it is not checked out and we'll just return index data.
-	 */
-	if (!workdir || (sm->flags & GIT_SUBMODULE_FLAG__HAS_DOTGIT) == 0) {
-		*status = GIT_SUBMODULE_STATUS_NOT_CHECKED_OUT;
-
-		if (sm->index_oid_valid)
-			git_oid_cpy(head, &sm->index_oid);
-		else
-			memset(head, 0, sizeof(git_oid));
-
-		if (git_oid_iszero(head)) {
-			if (sm->url)
-				*status = GIT_SUBMODULE_STATUS_NEW_SUBMODULE;
-		} else if (!sm->url) {
-			*status = GIT_SUBMODULE_STATUS_DELETED_SUBMODULE;
-		}
+	int error = 0;
+	const git_oid *wd_oid, *index_oid;
+	git_repository *sm_repo = NULL;
 
-		return 0;
+	/* open repo now if we need it (so wd_oid() call won't reopen) */
+	if ((sm->ignore == GIT_SUBMODULE_IGNORE_NONE ||
+		 sm->ignore == GIT_SUBMODULE_IGNORE_UNTRACKED) &&
+		(sm->flags & GIT_SUBMODULE_STATUS_IN_WD) != 0)
+	{
+		if ((error = git_submodule_open(&sm_repo, sm)) < 0)
+			return error;
 	}
 
-	/* look up submodule path in repo head to find if new or deleted */
-	if ((error = head_oid_for_submodule(&owner_head, owner, sm->path)) < 0) {
-		*status = GIT_SUBMODULE_STATUS_NEW_SUBMODULE;
-		/* ??? */
-	}
+	index_oid = git_submodule_index_oid(sm);
+	wd_oid    = git_submodule_wd_oid(sm);
 
-	if (ignore == GIT_SUBMODULE_IGNORE_ALL) {
-		*status = GIT_SUBMODULE_STATUS_CLEAN;
-		git_oid_cpy(head, &sm->oid);
-		return 0;
+	if (!index_oid) {
+		if (wd_oid)
+			*status |= GIT_SUBMODULE_STATUS_WD_ADDED;
 	}
-
-	if ((error = git_submodule_open(&sm_repo, sm)) < 0)
-		return error;
-
-	if ((error = git_reference_name_to_oid(head, sm_repo, GIT_HEAD_FILE)) < 0)
-		goto cleanup;
-
-	if (ignore == GIT_SUBMODULE_IGNORE_DIRTY &&
-		git_oid_cmp(head, &sm->oid) == 0)
-	{
-		*status = GIT_SUBMODULE_STATUS_CLEAN;
-		return 0;
+	else if (!wd_oid) {
+		if ((sm->flags & GIT_SUBMODULE_STATUS__WD_SCANNED) != 0 &&
+			(sm->flags & GIT_SUBMODULE_STATUS_IN_WD) == 0)
+			*status |= GIT_SUBMODULE_STATUS_WD_UNINITIALIZED;
+		else
+			*status |= GIT_SUBMODULE_STATUS_WD_DELETED;
 	}
+	else if (!git_oid_equal(index_oid, wd_oid))
+		*status |= GIT_SUBMODULE_STATUS_WD_MODIFIED;
 
-	/* look up submodule oid from index in repo to find if new commits or missing commits */
+	if (sm_repo != NULL) {
+		git_tree *sm_head;
+		git_diff_options opt;
+		git_diff_list *diff;
 
-	/* run a short status to find if modified or untracked content */
+		/* the diffs below could be optimized with an early termination
+		 * option to the git_diff functions, but for now this is sufficient
+		 * (and certainly no worse that what core git does).
+		 */
 
-#define GIT_SUBMODULE_STATUS_NEW_SUBMODULE       (1u << 2)
-#define GIT_SUBMODULE_STATUS_DELETED_SUBMODULE   (1u << 3)
-#define GIT_SUBMODULE_STATUS_NOT_CHECKED_OUT     (1u << 4)
-#define GIT_SUBMODULE_STATUS_NEW_COMMITS         (1u << 5)
-#define GIT_SUBMODULE_STATUS_MISSING_COMMITS     (1u << 6)
-#define GIT_SUBMODULE_STATUS_MODIFIED_CONTENT    (1u << 7)
-#define GIT_SUBMODULE_STATUS_UNTRACKED_CONTENT   (1u << 8)
+		/* perform head-to-index diff on submodule */
 
-cleanup:
-	git_repository_free(sm_repo);
-	git_tree_free(owner_tree);
+		if ((error = git_repository_head_tree(&sm_head, sm_repo)) < 0)
+			return error;
 
-	return error;
-}
+		memset(&opt, 0, sizeof(opt));
+		if (sm->ignore == GIT_SUBMODULE_IGNORE_NONE)
+			opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
 
-int git_submodule_status_for_path(
-	unsigned int *status,
-	git_oid *head,
-	git_repository *repo,
-	const char *submodule_path,
-	git_submodule_ignore_t ignore)
-{
-	int error;
-	git_submodule *sm;
-	const char *workdir;
-	git_buf path = GIT_BUF_INIT;
-	git_oid owner_head;
+		error = git_diff_index_to_tree(sm_repo, &opt, sm_head, &diff);
 
-	assert(repo && submodule_path && status);
+		if (!error) {
+			if (git_diff_entrycount(diff, -1) > 0)
+				*status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED;
 
-	if ((error = git_submodule_lookup(&sm, repo, submodule_path)) == 0)
-		return git_submodule_status(status, head, sm, ignore);
+			git_diff_list_free(diff);
+			diff = NULL;
+		}
 
-	/* if submodule still exists in HEAD, then it is DELETED */
-	if (!(error = head_oid_for_submodule(&owner_head, repo, submodule_path))) {
-		*status = GIT_SUBMODULE_STATUS_DELETED_SUBMODULE;
-		if (head)
-			git_oid_cmp(head, &owner_head);
-		return 0;
-	}
+		git_tree_free(sm_head);
 
-	/* submodule was not found - let's see what we can determine about it */
-	workdir = git_repository_workdir(repo);
+		if (error < 0)
+			return error;
 
-	if (error != GIT_ENOTFOUND || !workdir) {
-		*status = GIT_SUBMODULE_STATUS_NOT_A_SUBMODULE;
-		return error;
-	}
+		/* perform index-to-workdir diff on submodule */
 
-	giterr_clear();
-	error = 0;
+		error = git_diff_workdir_to_index(sm_repo, &opt, &diff);
 
-	/* figure out if this is NEW, NOT_CHECKED_OUT, or what */
-	if (git_buf_joinpath(&path, workdir, submodule_path) < 0)
-		return -1;
+		if (!error) {
+			int untracked = git_diff_entrycount(diff, GIT_DELTA_UNTRACKED);
 
-	if (git_path_contains(&path, DOT_GIT)) {
-		git_repository *sm_repo;
+			if (untracked > 0)
+				*status |= GIT_SUBMODULE_STATUS_WD_UNTRACKED;
 
-		*status = GIT_SUBMODULE_STATUS_UNTRACKED_SUBMODULE;
+			if (git_diff_entrycount(diff, -1) - untracked > 0)
+				*status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED;
 
-		/* only bother look up head if it was non-NULL */
-		if (head != NULL &&
-			!(error = git_repository_open(&sm_repo, path.ptr)))
-		{
-			error = git_reference_name_to_oid(head, sm_repo, GIT_HEAD_FILE);
-			git_repository_free(sm_repo);
+			git_diff_list_free(diff);
+			diff = NULL;
 		}
-	} else
-		*status = GIT_SUBMODULE_STATUS_NOT_A_SUBMODULE;
 
-	git_buf_free(&path);
+		git_repository_free(sm_repo);
+	}
 
 	return error;
 }
-
-#endif
diff --git a/src/submodule.h b/src/submodule.h
index 83bc7df..c7a6aaf 100644
--- a/src/submodule.h
+++ b/src/submodule.h
@@ -85,10 +85,18 @@ struct git_submodule {
 };
 
 /* Additional flags on top of public GIT_SUBMODULE_STATUS values */
-#define GIT_SUBMODULE_STATUS__WD_SCANNED      (1u << 15)
-#define GIT_SUBMODULE_STATUS__HEAD_OID_VALID  (1u << 16)
-#define GIT_SUBMODULE_STATUS__INDEX_OID_VALID (1u << 17)
-#define GIT_SUBMODULE_STATUS__WD_OID_VALID    (1u << 18)
-#define GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES (1u << 19)
+enum {
+	GIT_SUBMODULE_STATUS__WD_SCANNED          = (1u << 20),
+	GIT_SUBMODULE_STATUS__HEAD_OID_VALID      = (1u << 21),
+	GIT_SUBMODULE_STATUS__INDEX_OID_VALID     = (1u << 22),
+	GIT_SUBMODULE_STATUS__WD_OID_VALID        = (1u << 23),
+	GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE  = (1u << 24),
+	GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE = (1u << 25),
+	GIT_SUBMODULE_STATUS__WD_NOT_SUBMODULE    = (1u << 26),
+	GIT_SUBMODULE_STATUS__INDEX_MULTIPLE_ENTRIES = (1u << 27),
+};
+
+#define GIT_SUBMODULE_STATUS__CLEAR_INTERNAL(S) \
+	((S) & ~(0xFFFFFFFFu << 20))
 
 #endif
diff --git a/tests-clar/status/submodules.c b/tests-clar/status/submodules.c
index 3a69e0c..24dd660 100644
--- a/tests-clar/status/submodules.c
+++ b/tests-clar/status/submodules.c
@@ -50,7 +50,7 @@ void test_status_submodules__0(void)
 		git_status_foreach(g_repo, cb_status__count, &counts)
 	);
 
-	cl_assert(counts == 6);
+	cl_assert_equal_i(6, counts);
 }
 
 static const char *expected_files[] = {
@@ -95,12 +95,12 @@ void test_status_submodules__1(void)
 		git_status_foreach(g_repo, cb_status__match, &index)
 	);
 
-	cl_assert(index == 6);
+	cl_assert_equal_i(6, index);
 }
 
 void test_status_submodules__single_file(void)
 {
-	unsigned int status;
+	unsigned int status = 0;
 	cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
-	cl_assert(status == 0);
+	cl_assert(!status);
 }
diff --git a/tests-clar/submodule/status.c b/tests-clar/submodule/status.c
index e0c1e4c..d3a3923 100644
--- a/tests-clar/submodule/status.c
+++ b/tests-clar/submodule/status.c
@@ -2,6 +2,7 @@
 #include "posix.h"
 #include "path.h"
 #include "submodule_helpers.h"
+#include "fileops.h"
 
 static git_repository *g_repo = NULL;
 
@@ -25,20 +26,283 @@ void test_submodule_status__cleanup(void)
 
 void test_submodule_status__unchanged(void)
 {
-	/* make sure it really looks unchanged */
+	unsigned int status, expected;
+	git_submodule *sm;
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	expected = GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_IN_WD;
+
+	cl_assert(status == expected);
 }
 
-void test_submodule_status__changed(void)
+/* 4 values of GIT_SUBMODULE_IGNORE to check */
+
+void test_submodule_status__ignore_none(void)
 {
-	/* 4 values of GIT_SUBMODULE_IGNORE to check */
+	unsigned int status;
+	git_submodule *sm;
+	git_buf path = GIT_BUF_INIT;
+
+	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
+	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), GIT_DIRREMOVAL_FILES_AND_DIRS));
+
+	cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNTRACKED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);
+
+	/* removed sm_unchanged for deleted workdir */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
+
+	/* now mkdir sm_unchanged to test uninitialized */
+	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_reload(sm));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
+
+	/* update sm_changed_head in index */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_add_to_index(sm, true));
+	/* reload is not needed because add_to_index updates the submodule data */
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);
+
+	/* remove sm_changed_head from index */
+	{
+		git_index *index;
+		int pos;
+
+		cl_git_pass(git_repository_index(&index, g_repo));
+		pos = git_index_find(index, "sm_changed_head");
+		cl_assert(pos >= 0);
+		cl_git_pass(git_index_remove(index, pos));
+		cl_git_pass(git_index_write(index));
+
+		git_index_free(index);
+	}
 
-	/* 6 states of change:
-	 * - none, (handled in __unchanged above)
-	 * - dirty workdir file,
-	 * - dirty index,
-	 * - moved head,
-	 * - untracked file,
-	 * - missing commits (i.e. superproject commit is ahead of submodule)
-	 */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_reload(sm));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0);
+
+	git_buf_free(&path);
 }
 
+static int set_sm_ignore(git_submodule *sm, const char *name, void *payload)
+{
+	git_submodule_ignore_t ignore = *(git_submodule_ignore_t *)payload;
+	GIT_UNUSED(name);
+	git_submodule_set_ignore(sm, ignore);
+	return 0;
+}
+
+void test_submodule_status__ignore_untracked(void)
+{
+	unsigned int status;
+	git_submodule *sm;
+	git_buf path = GIT_BUF_INIT;
+	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED;
+
+	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
+	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), GIT_DIRREMOVAL_FILES_AND_DIRS));
+
+	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));
+
+	cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);
+
+	/* removed sm_unchanged for deleted workdir */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
+
+	/* now mkdir sm_unchanged to test uninitialized */
+	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_reload(sm));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
+
+	/* update sm_changed_head in index */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_add_to_index(sm, true));
+	/* reload is not needed because add_to_index updates the submodule data */
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);
+
+	git_buf_free(&path);
+}
+
+void test_submodule_status__ignore_dirty(void)
+{
+	unsigned int status;
+	git_submodule *sm;
+	git_buf path = GIT_BUF_INIT;
+	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_DIRTY;
+
+	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
+	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), GIT_DIRREMOVAL_FILES_AND_DIRS));
+
+	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));
+
+	cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0);
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0);
+
+	/* removed sm_unchanged for deleted workdir */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
+
+	/* now mkdir sm_unchanged to test uninitialized */
+	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_reload(sm));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
+
+	/* update sm_changed_head in index */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_add_to_index(sm, true));
+	/* reload is not needed because add_to_index updates the submodule data */
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0);
+
+	git_buf_free(&path);
+}
+
+void test_submodule_status__ignore_all(void)
+{
+	unsigned int status;
+	git_submodule *sm;
+	git_buf path = GIT_BUF_INIT;
+	git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_ALL;
+
+	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged"));
+	cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), GIT_DIRREMOVAL_FILES_AND_DIRS));
+
+	cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign));
+
+	cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	/* removed sm_unchanged for deleted workdir */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	/* now mkdir sm_unchanged to test uninitialized */
+	cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0));
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
+	cl_git_pass(git_submodule_reload(sm));
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	/* update sm_changed_head in index */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+	cl_git_pass(git_submodule_add_to_index(sm, true));
+	/* reload is not needed because add_to_index updates the submodule data */
+	cl_git_pass(git_submodule_status(&status, sm));
+	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
+
+	git_buf_free(&path);
+}