Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Nanako Shiraishi | 47a528a | 2008-09-03 17:59:33 +0900 | [diff] [blame] | 3 | test_description='git repack works correctly' |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 4 | |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
Ævar Arnfjörð Bjarmason | b2e5d75 | 2023-02-07 00:07:36 +0100 | [diff] [blame] | 8 | TEST_PASSES_SANITIZE_LEAK=true |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 9 | . ./test-lib.sh |
| 10 | |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 11 | fsha1= |
| 12 | csha1= |
| 13 | tsha1= |
| 14 | |
Brandon Casey | 83d0289 | 2008-11-13 14:11:46 -0600 | [diff] [blame] | 15 | test_expect_success '-A with -d option leaves unreachable objects unpacked' ' |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 16 | echo content > file1 && |
| 17 | git add . && |
Junio C Hamano | 713c79e | 2010-04-14 15:09:57 -0700 | [diff] [blame] | 18 | test_tick && |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 19 | git commit -m initial_commit && |
| 20 | # create a transient branch with unique content |
| 21 | git checkout -b transient_branch && |
| 22 | echo more content >> file1 && |
| 23 | # record the objects created in the database for file, commit, tree |
| 24 | fsha1=$(git hash-object file1) && |
Junio C Hamano | 713c79e | 2010-04-14 15:09:57 -0700 | [diff] [blame] | 25 | test_tick && |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 26 | git commit -a -m more_content && |
| 27 | csha1=$(git rev-parse HEAD^{commit}) && |
| 28 | tsha1=$(git rev-parse HEAD^{tree}) && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 29 | git checkout main && |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 30 | echo even more content >> file1 && |
Junio C Hamano | 713c79e | 2010-04-14 15:09:57 -0700 | [diff] [blame] | 31 | test_tick && |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 32 | git commit -a -m even_more_content && |
| 33 | # delete the transient branch |
| 34 | git branch -D transient_branch && |
| 35 | # pack the repo |
| 36 | git repack -A -d -l && |
| 37 | # verify objects are packed in repository |
| 38 | test 3 = $(git verify-pack -v -- .git/objects/pack/*.idx | |
Đoàn Trần Công Danh | 81580fa | 2022-09-21 20:02:31 +0700 | [diff] [blame] | 39 | grep -E "^($fsha1|$csha1|$tsha1) " | |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 40 | sort | uniq | wc -l) && |
| 41 | git show $fsha1 && |
| 42 | git show $csha1 && |
| 43 | git show $tsha1 && |
Junio C Hamano | 713c79e | 2010-04-14 15:09:57 -0700 | [diff] [blame] | 44 | # now expire the reflog, while keeping reachable ones but expiring |
| 45 | # unreachables immediately |
| 46 | test_tick && |
| 47 | sometimeago=$(( $test_tick - 10000 )) && |
| 48 | git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all && |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 49 | # and repack |
| 50 | git repack -A -d -l && |
| 51 | # verify objects are retained unpacked |
| 52 | test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx | |
Đoàn Trần Công Danh | 81580fa | 2022-09-21 20:02:31 +0700 | [diff] [blame] | 53 | grep -E "^($fsha1|$csha1|$tsha1) " | |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 54 | sort | uniq | wc -l) && |
| 55 | git show $fsha1 && |
| 56 | git show $csha1 && |
| 57 | git show $tsha1 |
| 58 | ' |
| 59 | |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 60 | compare_mtimes () |
| 61 | { |
Paul-Sebastian Ungureanu | decf711 | 2018-04-07 01:19:47 +0300 | [diff] [blame] | 62 | read tref && |
| 63 | while read t; do |
Jeff King | 76e057d | 2015-03-25 01:29:10 -0400 | [diff] [blame] | 64 | test "$tref" = "$t" || return 1 |
Johannes Sixt | d040993 | 2009-01-28 10:52:26 +0100 | [diff] [blame] | 65 | done |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Brandon Casey | 83d0289 | 2008-11-13 14:11:46 -0600 | [diff] [blame] | 68 | test_expect_success '-A without -d option leaves unreachable objects packed' ' |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 69 | fsha1path=$(echo "$fsha1" | sed -e "s|\(..\)|\1/|") && |
| 70 | fsha1path=".git/objects/$fsha1path" && |
| 71 | csha1path=$(echo "$csha1" | sed -e "s|\(..\)|\1/|") && |
| 72 | csha1path=".git/objects/$csha1path" && |
| 73 | tsha1path=$(echo "$tsha1" | sed -e "s|\(..\)|\1/|") && |
| 74 | tsha1path=".git/objects/$tsha1path" && |
| 75 | git branch transient_branch $csha1 && |
| 76 | git repack -a -d -l && |
| 77 | test ! -f "$fsha1path" && |
| 78 | test ! -f "$csha1path" && |
| 79 | test ! -f "$tsha1path" && |
| 80 | test 1 = $(ls -1 .git/objects/pack/pack-*.pack | wc -l) && |
| 81 | packfile=$(ls .git/objects/pack/pack-*.pack) && |
| 82 | git branch -D transient_branch && |
Junio C Hamano | 713c79e | 2010-04-14 15:09:57 -0700 | [diff] [blame] | 83 | test_tick && |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 84 | git repack -A -l && |
Brandon Casey | 83d0289 | 2008-11-13 14:11:46 -0600 | [diff] [blame] | 85 | test ! -f "$fsha1path" && |
| 86 | test ! -f "$csha1path" && |
| 87 | test ! -f "$tsha1path" && |
| 88 | git show $fsha1 && |
| 89 | git show $csha1 && |
| 90 | git show $tsha1 |
| 91 | ' |
| 92 | |
| 93 | test_expect_success 'unpacked objects receive timestamp of pack file' ' |
| 94 | tmppack=".git/objects/pack/tmp_pack" && |
| 95 | ln "$packfile" "$tmppack" && |
| 96 | git repack -A -l -d && |
Junio C Hamano | deb9845 | 2018-04-25 13:29:00 +0900 | [diff] [blame] | 97 | test-tool chmtime --get "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \ |
Johannes Sixt | d040993 | 2009-01-28 10:52:26 +0100 | [diff] [blame] | 98 | > mtimes && |
| 99 | compare_mtimes < mtimes |
Brandon Casey | e896912 | 2008-06-28 19:25:05 -0500 | [diff] [blame] | 100 | ' |
| 101 | |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 102 | test_expect_success 'do not bother loosening old objects' ' |
| 103 | obj1=$(echo one | git hash-object -w --stdin) && |
| 104 | obj2=$(echo two | git hash-object -w --stdin) && |
| 105 | pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) && |
| 106 | pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) && |
| 107 | git prune-packed && |
| 108 | git cat-file -p $obj1 && |
| 109 | git cat-file -p $obj2 && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 110 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack && |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 111 | git repack -A -d --unpack-unreachable=1.hour.ago && |
| 112 | git cat-file -p $obj1 && |
| 113 | test_must_fail git cat-file -p $obj2 |
| 114 | ' |
| 115 | |
Taylor Blau | 4dc16e2 | 2023-06-07 18:58:17 -0400 | [diff] [blame] | 116 | test_expect_success 'gc.recentObjectsHook' ' |
| 117 | obj1=$(echo one | git hash-object -w --stdin) && |
| 118 | obj2=$(echo two | git hash-object -w --stdin) && |
| 119 | obj3=$(echo three | git hash-object -w --stdin) && |
| 120 | pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) && |
| 121 | pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) && |
| 122 | pack3=$(echo $obj3 | git pack-objects .git/objects/pack/pack) && |
| 123 | git prune-packed && |
| 124 | |
| 125 | git cat-file -p $obj1 && |
| 126 | git cat-file -p $obj2 && |
| 127 | git cat-file -p $obj3 && |
| 128 | |
Taylor Blau | 25d5952 | 2023-06-24 10:33:47 -0400 | [diff] [blame] | 129 | # make an unreachable annotated tag object to ensure we rescue objects |
| 130 | # which are reachable from non-pruned unreachable objects |
| 131 | obj2_tag="$(git mktag <<-EOF |
| 132 | object $obj2 |
| 133 | type blob |
| 134 | tag obj2-tag |
| 135 | tagger T A Gger <tagger@example.com> 1234567890 -0000 |
| 136 | EOF |
| 137 | )" && |
| 138 | |
| 139 | obj2_tag_pack="$(echo $obj2_tag | git pack-objects .git/objects/pack/pack)" && |
| 140 | git prune-packed && |
Taylor Blau | 4dc16e2 | 2023-06-07 18:58:17 -0400 | [diff] [blame] | 141 | |
| 142 | write_script precious-objects <<-EOF && |
| 143 | echo $obj2_tag |
| 144 | EOF |
| 145 | git config gc.recentObjectsHook ./precious-objects && |
| 146 | |
| 147 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack && |
| 148 | test-tool chmtime =-86400 .git/objects/pack/pack-$pack3.pack && |
Taylor Blau | 25d5952 | 2023-06-24 10:33:47 -0400 | [diff] [blame] | 149 | test-tool chmtime =-86400 .git/objects/pack/pack-$obj2_tag_pack.pack && |
Taylor Blau | 4dc16e2 | 2023-06-07 18:58:17 -0400 | [diff] [blame] | 150 | git repack -A -d --unpack-unreachable=1.hour.ago && |
| 151 | |
| 152 | git cat-file -p $obj1 && |
| 153 | git cat-file -p $obj2 && |
| 154 | git cat-file -p $obj2_tag && |
| 155 | test_must_fail git cat-file -p $obj3 |
| 156 | ' |
| 157 | |
Jeff King | c90f9e1 | 2014-10-16 20:44:49 -0400 | [diff] [blame] | 158 | test_expect_success 'keep packed objects found only in index' ' |
| 159 | echo my-unique-content >file && |
| 160 | git add file && |
| 161 | git commit -m "make it reachable" && |
| 162 | git gc && |
| 163 | git reset HEAD^ && |
| 164 | git reflog expire --expire=now --all && |
| 165 | git add file && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 166 | test-tool chmtime =-86400 .git/objects/pack/* && |
Jeff King | c90f9e1 | 2014-10-16 20:44:49 -0400 | [diff] [blame] | 167 | git gc --prune=1.hour.ago && |
| 168 | git cat-file blob :file |
| 169 | ' |
| 170 | |
Jeff King | 905f27b | 2016-06-13 00:36:28 -0400 | [diff] [blame] | 171 | test_expect_success 'repack -k keeps unreachable packed objects' ' |
| 172 | # create packed-but-unreachable object |
| 173 | sha1=$(echo unreachable-packed | git hash-object -w --stdin) && |
| 174 | pack=$(echo $sha1 | git pack-objects .git/objects/pack/pack) && |
| 175 | git prune-packed && |
| 176 | |
| 177 | # -k should keep it |
| 178 | git repack -adk && |
| 179 | git cat-file -p $sha1 && |
| 180 | |
| 181 | # and double check that without -k it would have been removed |
| 182 | git repack -ad && |
| 183 | test_must_fail git cat-file -p $sha1 |
| 184 | ' |
| 185 | |
Jeff King | e26a8c4 | 2016-06-13 00:38:04 -0400 | [diff] [blame] | 186 | test_expect_success 'repack -k packs unreachable loose objects' ' |
| 187 | # create loose unreachable object |
| 188 | sha1=$(echo would-be-deleted-loose | git hash-object -w --stdin) && |
| 189 | objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") && |
| 190 | test_path_is_file $objpath && |
| 191 | |
| 192 | # and confirm that the loose object goes away, but we can |
| 193 | # still access it (ergo, it is packed) |
| 194 | git repack -adk && |
| 195 | test_path_is_missing $objpath && |
| 196 | git cat-file -p $sha1 |
| 197 | ' |
| 198 | |
Brandon Casey | ccc1297 | 2008-05-09 23:01:55 -0500 | [diff] [blame] | 199 | test_done |