ce_smudge_racily_clean_entry: explain why it works.
This is a tricky code and warrants extra commenting. I wasted 30 minutes trying to break it until I realized why it works. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 20, 2005 at 14:18 UTC
4b3511b0f8422fd2c5b1b37c9655ae2ce904bca5
1 file changed
+25
-1
read-cache.c
+25
-1
@@ -616,7 +616,31 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
616
if (ce_match_stat_basic(ce, &st))
617
return;
618
if (ce_modified_check_fs(ce, &st)) {
619
- /* This is "racily clean"; smudge it */
619
+ /* This is "racily clean"; smudge it. Note that this
620
+ * is a tricky code. At first glance, it may appear
621
+ * that it can break with this sequence:
622
+ *
623
+ * $ echo xyzzy >frotz
624
+ * $ git-update-index --add frotz
625
+ * $ : >frotz
626
+ * $ sleep 3
627
+ * $ echo filfre >nitfol
628
+ * $ git-update-index --add nitfol
629
+ *
630
+ * but it does not. Whe the second update-index runs,
631
+ * it notices that the entry "frotz" has the same timestamp
632
+ * as index, and if we were to smudge it by resetting its
633
+ * size to zero here, then the object name recorded
634
+ * in index is the 6-byte file but the cached stat information
635
+ * becomes zero --- which would then match what we would
636
+ * obtain from the filesystem next time we stat("frotz").
637
+ *
638
+ * However, the second update-index, before calling
639
+ * this function, notices that the cached size is 6
640
+ * bytes and what is on the filesystem is an empty
641
+ * file, and never calls us, so the cached size information
642
+ * for "frotz" stays 6 which does not match the filesystem.
643
+ */
644
ce->ce_size = htonl(0);
645
}
646
}