sha1-file.c: don't freshen cruft packs

We don't bother to freshen objects stored in a cruft pack individually by updating the `.mtimes` file. This is because we can't portably `mmap` and write into the middle of a file (i.e., to update the mtime of just one object). Instead, we would have to rewrite the entire `.mtimes` file which may incur some wasted effort especially if there a lot of cruft objects and they are freshened infrequently. Instead, force the freshening code to avoid an optimizing write by writing out the object loose and letting it pick up a current mtime. This works because we prefer the mtime of the loose copy of an object when both a loose and packed one exist (whether or not the packed copy comes from a cruft pack or not). This could certainly do with a test and/or be included earlier in this series/PR, but I want to wait until after I have a chance to clean up the overly-repetitive nature of the cruft pack tests in general. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 20, 2022 at 19:18 UTC a613164257b46700ca583bdcab160c712ad392fe
2 files changed +27
object-file.c
+2
@@ -2035,6 +2035,8 @@ static int freshen_packed_object(const struct object_id *oid)
2035 struct pack_entry e;
2036 if (!find_pack_entry(the_repository, oid, &e))
2037 return 0;
2038 + if (e.p->is_cruft)
2039 + return 0;
2040 if (e.p->freshened)
2041 return 1;
2042 if (!freshen_file(e.p->pack_name))
t/t5329-pack-objects-cruft.sh
+25
@@ -711,4 +711,29 @@ test_expect_success 'MIDX bitmaps tolerate reachable cruft objects' '
711 )
712 '
713
714 +test_expect_success 'cruft objects are freshend via loose' '
715 + git init repo &&
716 + test_when_finished "rm -fr repo" &&
717 + (
718 + cd repo &&
719 +
720 + echo "cruft" >contents &&
721 + blob="$(git hash-object -w -t blob contents)" &&
722 + loose="$objdir/$(test_oid_to_path $blob)" &&
723 +
724 + test_commit base &&
725 +
726 + git repack --cruft -d &&
727 +
728 + test_path_is_missing "$loose" &&
729 + test-tool pack-mtimes "$(basename "$(ls $packdir/pack-*.mtimes)")" >cruft &&
730 + grep "$blob" cruft &&
731 +
732 + # write the same object again
733 + git hash-object -w -t blob contents &&
734 +
735 + test_path_is_file "$loose"
736 + )
737 +'
738 +
739 test_done