1#!/bin/sh23test_description='git repack works correctly'45. ./test-lib.sh67test_expect_success 'objects in packs marked .keep are not repacked' '8echo content1 > file1 &&9echo content2 > file2 &&10git add . &&11test_tick &&12git commit -m initial_commit &&13# Create two packs14# The first pack will contain all of the objects except one15git rev-list --objects --all | grep -v file2 |16git pack-objects pack > /dev/null &&17# The second pack will contain the excluded object18packsha1=$(git rev-list --objects --all | grep file2 |19git pack-objects pack) &&20>pack-$packsha1.keep &&21objsha1=$(git verify-pack -v pack-$packsha1.idx | head -n 1 |22sed -e "s/^\([0-9a-f]\{40\}\).*/\1/") &&23mv pack-* .git/objects/pack/ &&24git repack --no-pack-kept-objects -A -d -l &&25git prune-packed &&26for p in .git/objects/pack/*.idx; do27idx=$(basename $p)28test "pack-$packsha1.idx" = "$idx" && continue29if git verify-pack -v $p | egrep "^$objsha1"; then30found_duplicate_object=131echo "DUPLICATE OBJECT FOUND"32break33fi34done &&35test -z "$found_duplicate_object"36'3738test_expect_success 'writing bitmaps can duplicate .keep objects' '39# build on $objsha1, $packsha1, and .keep state from previous40git repack -Adl &&41test_when_finished "found_duplicate_object=" &&42for p in .git/objects/pack/*.idx; do43idx=$(basename $p)44test "pack-$packsha1.idx" = "$idx" && continue45if git verify-pack -v $p | egrep "^$objsha1"; then46found_duplicate_object=147echo "DUPLICATE OBJECT FOUND"48break49fi50done &&51test "$found_duplicate_object" = 152'5354test_expect_success 'loose objects in alternate ODB are not repacked' '55mkdir alt_objects &&56echo `pwd`/alt_objects > .git/objects/info/alternates &&57echo content3 > file3 &&58objsha1=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&59git add file3 &&60test_tick &&61git commit -m commit_file3 &&62git repack -a -d -l &&63git prune-packed &&64for p in .git/objects/pack/*.idx; do65if git verify-pack -v $p | egrep "^$objsha1"; then66found_duplicate_object=167echo "DUPLICATE OBJECT FOUND"68break69fi70done &&71test -z "$found_duplicate_object"72'7374test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '75mkdir alt_objects/pack &&76mv .git/objects/pack/* alt_objects/pack &&77git repack -a &&78myidx=$(ls -1 .git/objects/pack/*.idx) &&79test -f "$myidx" &&80for p in alt_objects/pack/*.idx; do81git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"82done | while read sha1 rest; do83if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then84echo "Missing object in local pack: $sha1"85return 186fi87done88'8990test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '91rm -f .git/objects/pack/* &&92echo new_content >> file1 &&93git add file1 &&94test_tick &&95git commit -m more_content &&96git repack &&97git repack -a -d &&98myidx=$(ls -1 .git/objects/pack/*.idx) &&99test -f "$myidx" &&100for p in alt_objects/pack/*.idx; do101git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"102done | while read sha1 rest; do103if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then104echo "Missing object in local pack: $sha1"105return 1106fi107done108'109110test_expect_success 'packed obs in alternate ODB kept pack are repacked' '111# swap the .keep so the commit object is in the pack with .keep112for p in alt_objects/pack/*.pack113do114base_name=$(basename $p .pack) &&115if test -f alt_objects/pack/$base_name.keep116then117rm alt_objects/pack/$base_name.keep118else119touch alt_objects/pack/$base_name.keep120fi121done &&122git repack -a -d &&123myidx=$(ls -1 .git/objects/pack/*.idx) &&124test -f "$myidx" &&125for p in alt_objects/pack/*.idx; do126git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"127done | while read sha1 rest; do128if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then129echo "Missing object in local pack: $sha1"130return 1131fi132done133'134135test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '136rm -f alt_objects/pack/*.keep &&137mv .git/objects/pack/* alt_objects/pack/ &&138csha1=$(git rev-parse HEAD^{commit}) &&139git reset --hard HEAD^ &&140test_tick &&141git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&142# The pack-objects call on the next line is equivalent to143# git repack -A -d without the call to prune-packed144git pack-objects --honor-pack-keep --non-empty --all --reflog \145--unpack-unreachable </dev/null pack &&146rm -f .git/objects/pack/* &&147mv pack-* .git/objects/pack/ &&148test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |149egrep "^$csha1 " | sort | uniq | wc -l) &&150echo > .git/objects/info/alternates &&151test_must_fail git show $csha1152'153154test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '155echo `pwd`/alt_objects > .git/objects/info/alternates &&156echo "$csha1" | git pack-objects --non-empty --all --reflog pack &&157rm -f .git/objects/pack/* &&158mv pack-* .git/objects/pack/ &&159# The pack-objects call on the next line is equivalent to160# git repack -A -d without the call to prune-packed161git pack-objects --honor-pack-keep --non-empty --all --reflog \162--unpack-unreachable </dev/null pack &&163rm -f .git/objects/pack/* &&164mv pack-* .git/objects/pack/ &&165test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |166egrep "^$csha1 " | sort | uniq | wc -l) &&167echo > .git/objects/info/alternates &&168test_must_fail git show $csha1169'170171test_expect_success 'objects made unreachable by grafts only are kept' '172test_tick &&173git commit --allow-empty -m "commit 4" &&174H0=$(git rev-parse HEAD) &&175H1=$(git rev-parse HEAD^) &&176H2=$(git rev-parse HEAD^^) &&177echo "$H0 $H2" > .git/info/grafts &&178git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&179git repack -a -d &&180git cat-file -t $H1181'182183test_done184