| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git repack works correctly' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | fsha1= |
| 11 | csha1= |
| 12 | tsha1= |
| 13 | |
| 14 | test_expect_success '-A with -d option leaves unreachable objects unpacked' ' |
| 15 | echo content > file1 && |
| 16 | git add . && |
| 17 | test_tick && |
| 18 | git commit -m initial_commit && |
| 19 | # create a transient branch with unique content |
| 20 | git checkout -b transient_branch && |
| 21 | echo more content >> file1 && |
| 22 | # record the objects created in the database for file, commit, tree |
| 23 | fsha1=$(git hash-object file1) && |
| 24 | test_tick && |
| 25 | git commit -a -m more_content && |
| 26 | csha1=$(git rev-parse HEAD^{commit}) && |
| 27 | tsha1=$(git rev-parse HEAD^{tree}) && |
| 28 | git checkout main && |
| 29 | echo even more content >> file1 && |
| 30 | test_tick && |
| 31 | git commit -a -m even_more_content && |
| 32 | # delete the transient branch |
| 33 | git branch -D transient_branch && |
| 34 | # pack the repo |
| 35 | git repack -A -d -l && |
| 36 | # verify objects are packed in repository |
| 37 | test 3 = $(git verify-pack -v -- .git/objects/pack/*.idx | |
| 38 | grep -E "^($fsha1|$csha1|$tsha1) " | |
| 39 | sort | uniq | wc -l) && |
| 40 | git show $fsha1 && |
| 41 | git show $csha1 && |
| 42 | git show $tsha1 && |
| 43 | # now expire the reflog, while keeping reachable ones but expiring |
| 44 | # unreachables immediately |
| 45 | test_tick && |
| 46 | sometimeago=$(( $test_tick - 10000 )) && |
| 47 | git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all && |
| 48 | # and repack |
| 49 | git repack -A -d -l && |
| 50 | # verify objects are retained unpacked |
| 51 | test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx | |
| 52 | grep -E "^($fsha1|$csha1|$tsha1) " | |
| 53 | sort | uniq | wc -l) && |
| 54 | git show $fsha1 && |
| 55 | git show $csha1 && |
| 56 | git show $tsha1 |
| 57 | ' |
| 58 | |
| 59 | compare_mtimes () |
| 60 | { |
| 61 | read tref && |
| 62 | while read t; do |
| 63 | test "$tref" = "$t" || return 1 |
| 64 | done |
| 65 | } |
| 66 | |
| 67 | test_expect_success '-A without -d option leaves unreachable objects packed' ' |
| 68 | fsha1path=$(echo "$fsha1" | sed -e "s|\(..\)|\1/|") && |
| 69 | fsha1path=".git/objects/$fsha1path" && |
| 70 | csha1path=$(echo "$csha1" | sed -e "s|\(..\)|\1/|") && |
| 71 | csha1path=".git/objects/$csha1path" && |
| 72 | tsha1path=$(echo "$tsha1" | sed -e "s|\(..\)|\1/|") && |
| 73 | tsha1path=".git/objects/$tsha1path" && |
| 74 | git branch transient_branch $csha1 && |
| 75 | git repack -a -d -l && |
| 76 | test ! -f "$fsha1path" && |
| 77 | test ! -f "$csha1path" && |
| 78 | test ! -f "$tsha1path" && |
| 79 | test 1 = $(ls -1 .git/objects/pack/pack-*.pack | wc -l) && |
| 80 | packfile=$(ls .git/objects/pack/pack-*.pack) && |
| 81 | git branch -D transient_branch && |
| 82 | test_tick && |
| 83 | git repack -A -l && |
| 84 | test ! -f "$fsha1path" && |
| 85 | test ! -f "$csha1path" && |
| 86 | test ! -f "$tsha1path" && |
| 87 | git show $fsha1 && |
| 88 | git show $csha1 && |
| 89 | git show $tsha1 |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'unpacked objects receive timestamp of pack file' ' |
| 93 | tmppack=".git/objects/pack/tmp_pack" && |
| 94 | ln "$packfile" "$tmppack" && |
| 95 | git repack -A -l -d && |
| 96 | test-tool chmtime --get "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \ |
| 97 | > mtimes && |
| 98 | compare_mtimes < mtimes |
| 99 | ' |
| 100 | |
| 101 | test_expect_success 'do not bother loosening old objects' ' |
| 102 | obj1=$(echo one | git hash-object -w --stdin) && |
| 103 | obj2=$(echo two | git hash-object -w --stdin) && |
| 104 | pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) && |
| 105 | pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) && |
| 106 | git prune-packed && |
| 107 | git cat-file -p $obj1 && |
| 108 | git cat-file -p $obj2 && |
| 109 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack && |
| 110 | git repack -A -d --unpack-unreachable=1.hour.ago && |
| 111 | git cat-file -p $obj1 && |
| 112 | test_must_fail git cat-file -p $obj2 |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'gc.recentObjectsHook' ' |
| 116 | obj1=$(echo one | git hash-object -w --stdin) && |
| 117 | obj2=$(echo two | git hash-object -w --stdin) && |
| 118 | obj3=$(echo three | git hash-object -w --stdin) && |
| 119 | pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) && |
| 120 | pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) && |
| 121 | pack3=$(echo $obj3 | git pack-objects .git/objects/pack/pack) && |
| 122 | git prune-packed && |
| 123 | |
| 124 | git cat-file -p $obj1 && |
| 125 | git cat-file -p $obj2 && |
| 126 | git cat-file -p $obj3 && |
| 127 | |
| 128 | # make an unreachable annotated tag object to ensure we rescue objects |
| 129 | # which are reachable from non-pruned unreachable objects |
| 130 | obj2_tag="$(git mktag <<-EOF |
| 131 | object $obj2 |
| 132 | type blob |
| 133 | tag obj2-tag |
| 134 | tagger T A Gger <tagger@example.com> 1234567890 -0000 |
| 135 | EOF |
| 136 | )" && |
| 137 | |
| 138 | obj2_tag_pack="$(echo $obj2_tag | git pack-objects .git/objects/pack/pack)" && |
| 139 | git prune-packed && |
| 140 | |
| 141 | write_script precious-objects <<-EOF && |
| 142 | echo $obj2_tag |
| 143 | EOF |
| 144 | git config gc.recentObjectsHook ./precious-objects && |
| 145 | |
| 146 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack && |
| 147 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack3.pack && |
| 148 | test-tool chmtime =-86400 .git/objects/pack/pack-$obj2_tag_pack.pack && |
| 149 | git repack -A -d --unpack-unreachable=1.hour.ago && |
| 150 | |
| 151 | git cat-file -p $obj1 && |
| 152 | git cat-file -p $obj2 && |
| 153 | git cat-file -p $obj2_tag && |
| 154 | test_must_fail git cat-file -p $obj3 |
| 155 | ' |
| 156 | |
| 157 | test_expect_success 'keep packed objects found only in index' ' |
| 158 | echo my-unique-content >file && |
| 159 | git add file && |
| 160 | git commit -m "make it reachable" && |
| 161 | git gc && |
| 162 | git reset HEAD^ && |
| 163 | git reflog expire --expire=now --all && |
| 164 | git add file && |
| 165 | test-tool chmtime =-86400 .git/objects/pack/* && |
| 166 | git gc --prune=1.hour.ago && |
| 167 | git cat-file blob :file |
| 168 | ' |
| 169 | |
| 170 | test_expect_success 'repack -k keeps unreachable packed objects' ' |
| 171 | # create packed-but-unreachable object |
| 172 | sha1=$(echo unreachable-packed | git hash-object -w --stdin) && |
| 173 | pack=$(echo $sha1 | git pack-objects .git/objects/pack/pack) && |
| 174 | git prune-packed && |
| 175 | |
| 176 | # -k should keep it |
| 177 | git repack -adk && |
| 178 | git cat-file -p $sha1 && |
| 179 | |
| 180 | # and double check that without -k it would have been removed |
| 181 | git repack -ad && |
| 182 | test_must_fail git cat-file -p $sha1 |
| 183 | ' |
| 184 | |
| 185 | test_expect_success 'repack -k packs unreachable loose objects' ' |
| 186 | # create loose unreachable object |
| 187 | sha1=$(echo would-be-deleted-loose | git hash-object -w --stdin) && |
| 188 | objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") && |
| 189 | test_path_is_file $objpath && |
| 190 | |
| 191 | # and confirm that the loose object goes away, but we can |
| 192 | # still access it (ergo, it is packed) |
| 193 | git repack -adk && |
| 194 | test_path_is_missing $objpath && |
| 195 | git cat-file -p $sha1 |
| 196 | ' |
| 197 | |
| 198 | test_expect_success 'repack -k packs unreachable loose objects without existing packfiles' ' |
| 199 | test_when_finished "rm -rf repo" && |
| 200 | git init repo && |
| 201 | ( |
| 202 | cd repo && |
| 203 | |
| 204 | oid=$(echo would-be-deleted-loose | git hash-object -w --stdin) && |
| 205 | objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") && |
| 206 | test_path_is_file $objpath && |
| 207 | |
| 208 | git repack -ad --keep-unreachable && |
| 209 | test_path_is_missing $objpath && |
| 210 | git cat-file -p $oid |
| 211 | ) |
| 212 | ' |
| 213 | |
| 214 | test_done |