David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2008 Johannes E. Schindelin |
| 4 | # |
| 5 | |
| 6 | test_description='prune' |
| 7 | . ./test-lib.sh |
| 8 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 9 | day=$((60*60*24)) |
| 10 | week=$(($day*7)) |
| 11 | |
| 12 | add_blob() { |
| 13 | before=$(git count-objects | sed "s/ .*//") && |
| 14 | BLOB=$(echo aleph_0 | git hash-object -w --stdin) && |
| 15 | BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 16 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 17 | test_path_is_file $BLOB_FILE && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 18 | test-tool chmtime =+0 $BLOB_FILE |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 19 | } |
| 20 | |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 21 | test_expect_success setup ' |
| 22 | |
| 23 | : > file && |
| 24 | git add file && |
| 25 | test_tick && |
| 26 | git commit -m initial && |
| 27 | git gc |
| 28 | |
| 29 | ' |
| 30 | |
| 31 | test_expect_success 'prune stale packs' ' |
| 32 | |
| 33 | orig_pack=$(echo .git/objects/pack/*.pack) && |
| 34 | : > .git/objects/tmp_1.pack && |
| 35 | : > .git/objects/tmp_2.pack && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 36 | test-tool chmtime =-86501 .git/objects/tmp_1.pack && |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 37 | git prune --expire 1.day && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 38 | test_path_is_file $orig_pack && |
| 39 | test_path_is_file .git/objects/tmp_2.pack && |
| 40 | test_path_is_missing .git/objects/tmp_1.pack |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 41 | |
| 42 | ' |
| 43 | |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 44 | test_expect_success 'prune --expire' ' |
| 45 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 46 | add_blob && |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 47 | git prune --expire=1.hour.ago && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 48 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 49 | test_path_is_file $BLOB_FILE && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 50 | test-tool chmtime =-86500 $BLOB_FILE && |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 51 | git prune --expire 1.day && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 52 | verbose test $before = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 53 | test_path_is_missing $BLOB_FILE |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 54 | |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'gc: implicit prune --expire' ' |
| 58 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 59 | add_blob && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 60 | test-tool chmtime =-$((2*$week-30)) $BLOB_FILE && |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 61 | git gc && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 62 | verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 63 | test_path_is_file $BLOB_FILE && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 64 | test-tool chmtime =-$((2*$week+1)) $BLOB_FILE && |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 65 | git gc && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 66 | verbose test $before = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 67 | test_path_is_missing $BLOB_FILE |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 68 | |
| 69 | ' |
| 70 | |
| 71 | test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' ' |
| 72 | |
| 73 | git config gc.pruneExpire invalid && |
| 74 | test_must_fail git gc |
| 75 | |
| 76 | ' |
| 77 | |
| 78 | test_expect_success 'gc: start with ok gc.pruneExpire' ' |
| 79 | |
| 80 | git config gc.pruneExpire 2.days.ago && |
| 81 | git gc |
| 82 | |
| 83 | ' |
| 84 | |
Michele Ballabio | 0c62705 | 2008-03-23 22:34:34 +0100 | [diff] [blame] | 85 | test_expect_success 'prune: prune nonsense parameters' ' |
| 86 | |
| 87 | test_must_fail git prune garbage && |
| 88 | test_must_fail git prune --- && |
| 89 | test_must_fail git prune --no-such-option |
| 90 | |
| 91 | ' |
| 92 | |
| 93 | test_expect_success 'prune: prune unreachable heads' ' |
| 94 | |
| 95 | git config core.logAllRefUpdates false && |
| 96 | mv .git/logs .git/logs.old && |
| 97 | : > file2 && |
| 98 | git add file2 && |
| 99 | git commit -m temporary && |
| 100 | tmp_head=$(git rev-list -1 HEAD) && |
| 101 | git reset HEAD^ && |
| 102 | git prune && |
| 103 | test_must_fail git reset $tmp_head -- |
| 104 | |
| 105 | ' |
| 106 | |
Max Kirillov | c40fdd0 | 2014-09-03 19:14:10 +0300 | [diff] [blame] | 107 | test_expect_success 'prune: do not prune detached HEAD with no reflog' ' |
| 108 | |
| 109 | git checkout --detach --quiet && |
| 110 | git commit --allow-empty -m "detached commit" && |
| 111 | # verify that there is no reflogs |
| 112 | # (should be removed and disabled by previous test) |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 113 | test_path_is_missing .git/logs && |
Max Kirillov | c40fdd0 | 2014-09-03 19:14:10 +0300 | [diff] [blame] | 114 | git prune -n >prune_actual && |
| 115 | : >prune_expected && |
| 116 | test_cmp prune_actual prune_expected |
| 117 | |
| 118 | ' |
| 119 | |
| 120 | test_expect_success 'prune: prune former HEAD after checking out branch' ' |
| 121 | |
| 122 | head_sha1=$(git rev-parse HEAD) && |
| 123 | git checkout --quiet master && |
| 124 | git prune -v >prune_actual && |
| 125 | grep "$head_sha1" prune_actual |
| 126 | |
| 127 | ' |
| 128 | |
Junio C Hamano | fe308f5 | 2008-03-24 23:20:51 -0700 | [diff] [blame] | 129 | test_expect_success 'prune: do not prune heads listed as an argument' ' |
Michele Ballabio | 0c62705 | 2008-03-23 22:34:34 +0100 | [diff] [blame] | 130 | |
| 131 | : > file2 && |
| 132 | git add file2 && |
| 133 | git commit -m temporary && |
| 134 | tmp_head=$(git rev-list -1 HEAD) && |
| 135 | git reset HEAD^ && |
| 136 | git prune -- $tmp_head && |
| 137 | git reset $tmp_head -- |
| 138 | |
| 139 | ' |
| 140 | |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 141 | test_expect_success 'gc --no-prune' ' |
| 142 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 143 | add_blob && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 144 | test-tool chmtime =-$((5001*$day)) $BLOB_FILE && |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 145 | git config gc.pruneExpire 2.days.ago && |
| 146 | git gc --no-prune && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 147 | verbose test 1 = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 148 | test_path_is_file $BLOB_FILE |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 149 | |
| 150 | ' |
| 151 | |
| 152 | test_expect_success 'gc respects gc.pruneExpire' ' |
| 153 | |
| 154 | git config gc.pruneExpire 5002.days.ago && |
| 155 | git gc && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 156 | test_path_is_file $BLOB_FILE && |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 157 | git config gc.pruneExpire 5000.days.ago && |
| 158 | git gc && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 159 | test_path_is_missing $BLOB_FILE |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 160 | |
| 161 | ' |
| 162 | |
| 163 | test_expect_success 'gc --prune=<date>' ' |
| 164 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 165 | add_blob && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 166 | test-tool chmtime =-$((5001*$day)) $BLOB_FILE && |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 167 | git gc --prune=5002.days.ago && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 168 | test_path_is_file $BLOB_FILE && |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 169 | git gc --prune=5000.days.ago && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 170 | test_path_is_missing $BLOB_FILE |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 171 | |
| 172 | ' |
| 173 | |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 174 | test_expect_success 'gc --prune=never' ' |
| 175 | |
| 176 | add_blob && |
| 177 | git gc --prune=never && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 178 | test_path_is_file $BLOB_FILE && |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 179 | git gc --prune=now && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 180 | test_path_is_missing $BLOB_FILE |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 181 | |
| 182 | ' |
| 183 | |
| 184 | test_expect_success 'gc respects gc.pruneExpire=never' ' |
| 185 | |
| 186 | git config gc.pruneExpire never && |
| 187 | add_blob && |
| 188 | git gc && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 189 | test_path_is_file $BLOB_FILE && |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 190 | git config gc.pruneExpire now && |
| 191 | git gc && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 192 | test_path_is_missing $BLOB_FILE |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 193 | |
| 194 | ' |
| 195 | |
| 196 | test_expect_success 'prune --expire=never' ' |
| 197 | |
| 198 | add_blob && |
| 199 | git prune --expire=never && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 200 | test_path_is_file $BLOB_FILE && |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 201 | git prune && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 202 | test_path_is_missing $BLOB_FILE |
Adam Simpkins | cbf731e | 2010-02-26 19:50:02 -0800 | [diff] [blame] | 203 | |
| 204 | ' |
| 205 | |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 206 | test_expect_success 'gc: prune old objects after local clone' ' |
| 207 | add_blob && |
Nguyễn Thái Ngọc Duy | 0e49649 | 2018-03-24 08:44:31 +0100 | [diff] [blame] | 208 | test-tool chmtime =-$((2*$week+1)) $BLOB_FILE && |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 209 | git clone --no-hardlinks . aclone && |
| 210 | ( |
| 211 | cd aclone && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 212 | verbose test 1 = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 213 | test_path_is_file $BLOB_FILE && |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 214 | git gc --prune && |
Jeff King | 8ad1652 | 2014-10-10 02:11:14 -0400 | [diff] [blame] | 215 | verbose test 0 = $(git count-objects | sed "s/ .*//") && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 216 | test_path_is_missing $BLOB_FILE |
Clemens Buchacher | 98df233 | 2009-09-13 12:49:45 +0200 | [diff] [blame] | 217 | ) |
| 218 | ' |
| 219 | |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 220 | test_expect_success 'garbage report in count-objects -v' ' |
Jeff King | f813e9e | 2015-06-22 06:40:50 -0400 | [diff] [blame] | 221 | test_when_finished "rm -f .git/objects/pack/fake*" && |
Doug Kelly | e6d65c9 | 2015-11-03 21:05:07 -0600 | [diff] [blame] | 222 | test_when_finished "rm -f .git/objects/pack/foo*" && |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 223 | : >.git/objects/pack/foo && |
| 224 | : >.git/objects/pack/foo.bar && |
| 225 | : >.git/objects/pack/foo.keep && |
| 226 | : >.git/objects/pack/foo.pack && |
| 227 | : >.git/objects/pack/fake.bar && |
| 228 | : >.git/objects/pack/fake.keep && |
| 229 | : >.git/objects/pack/fake.pack && |
| 230 | : >.git/objects/pack/fake.idx && |
| 231 | : >.git/objects/pack/fake2.keep && |
| 232 | : >.git/objects/pack/fake3.idx && |
| 233 | git count-objects -v 2>stderr && |
| 234 | grep "index file .git/objects/pack/fake.idx is too small" stderr && |
| 235 | grep "^warning:" stderr | sort >actual && |
| 236 | cat >expected <<\EOF && |
| 237 | warning: garbage found: .git/objects/pack/fake.bar |
| 238 | warning: garbage found: .git/objects/pack/foo |
| 239 | warning: garbage found: .git/objects/pack/foo.bar |
Justin Lebar | 235e8d5 | 2014-03-31 15:11:47 -0700 | [diff] [blame] | 240 | warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep |
Nguyễn Thái Ngọc Duy | 543c5ca | 2013-02-15 19:07:10 +0700 | [diff] [blame] | 241 | warning: no corresponding .idx: .git/objects/pack/foo.keep |
| 242 | warning: no corresponding .idx: .git/objects/pack/foo.pack |
| 243 | warning: no corresponding .pack: .git/objects/pack/fake3.idx |
| 244 | EOF |
| 245 | test_cmp expected actual |
| 246 | ' |
| 247 | |
Doug Kelly | 478f34d | 2015-11-03 21:05:08 -0600 | [diff] [blame] | 248 | test_expect_success 'clean pack garbage with gc' ' |
Doug Kelly | e6d65c9 | 2015-11-03 21:05:07 -0600 | [diff] [blame] | 249 | test_when_finished "rm -f .git/objects/pack/fake*" && |
| 250 | test_when_finished "rm -f .git/objects/pack/foo*" && |
| 251 | : >.git/objects/pack/foo.keep && |
| 252 | : >.git/objects/pack/foo.pack && |
| 253 | : >.git/objects/pack/fake.idx && |
| 254 | : >.git/objects/pack/fake2.keep && |
| 255 | : >.git/objects/pack/fake2.idx && |
| 256 | : >.git/objects/pack/fake3.keep && |
| 257 | git gc && |
| 258 | git count-objects -v 2>stderr && |
| 259 | grep "^warning:" stderr | sort >actual && |
| 260 | cat >expected <<\EOF && |
| 261 | warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep |
| 262 | warning: no corresponding .idx: .git/objects/pack/foo.keep |
| 263 | warning: no corresponding .idx: .git/objects/pack/foo.pack |
| 264 | EOF |
| 265 | test_cmp expected actual |
| 266 | ' |
| 267 | |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 268 | test_expect_success 'prune .git/shallow' ' |
Elia Pinto | 213ea11 | 2015-12-23 14:45:50 +0100 | [diff] [blame] | 269 | SHA1=$(echo hi|git commit-tree HEAD^{tree}) && |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 270 | echo $SHA1 >.git/shallow && |
| 271 | git prune --dry-run >out && |
| 272 | grep $SHA1 .git/shallow && |
| 273 | grep $SHA1 out && |
| 274 | git prune && |
Jeff King | f1dd90b | 2014-10-10 02:07:00 -0400 | [diff] [blame] | 275 | test_path_is_missing .git/shallow |
Nguyễn Thái Ngọc Duy | eab3296 | 2013-12-05 20:02:54 +0700 | [diff] [blame] | 276 | ' |
| 277 | |
Jonathon Mah | b0a4264 | 2015-02-08 20:15:39 -0500 | [diff] [blame] | 278 | test_expect_success 'prune: handle alternate object database' ' |
| 279 | test_create_repo A && |
| 280 | git -C A commit --allow-empty -m "initial commit" && |
| 281 | git clone --shared A B && |
| 282 | git -C B commit --allow-empty -m "next commit" && |
| 283 | git -C B prune |
| 284 | ' |
| 285 | |
Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 19:36:52 +0700 | [diff] [blame] | 286 | test_expect_success 'prune: handle index in multiple worktrees' ' |
| 287 | git worktree add second-worktree && |
| 288 | echo "new blob for second-worktree" >second-worktree/blob && |
| 289 | git -C second-worktree add blob && |
| 290 | git prune --expire=now && |
| 291 | git -C second-worktree show :blob >actual && |
| 292 | test_cmp second-worktree/blob actual |
| 293 | ' |
| 294 | |
Nguyễn Thái Ngọc Duy | d0c39a4 | 2017-08-23 19:36:59 +0700 | [diff] [blame] | 295 | test_expect_success 'prune: handle HEAD in multiple worktrees' ' |
| 296 | git worktree add --detach third-worktree && |
| 297 | echo "new blob for third-worktree" >third-worktree/blob && |
| 298 | git -C third-worktree add blob && |
| 299 | git -C third-worktree commit -m "third" && |
| 300 | rm .git/worktrees/third-worktree/index && |
| 301 | test_must_fail git -C third-worktree show :blob && |
| 302 | git prune --expire=now && |
| 303 | git -C third-worktree show HEAD:blob >actual && |
| 304 | test_cmp third-worktree/blob actual |
| 305 | ' |
| 306 | |
Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 19:37:01 +0700 | [diff] [blame] | 307 | test_expect_success 'prune: handle HEAD reflog in multiple worktrees' ' |
| 308 | git config core.logAllRefUpdates true && |
| 309 | echo "lost blob for third-worktree" >expected && |
| 310 | ( |
| 311 | cd third-worktree && |
| 312 | cat ../expected >blob && |
| 313 | git add blob && |
| 314 | git commit -m "second commit in third" && |
| 315 | git reset --hard HEAD^ |
| 316 | ) && |
| 317 | git prune --expire=now && |
| 318 | SHA1=`git hash-object expected` && |
| 319 | git -C third-worktree show "$SHA1" >actual && |
| 320 | test_cmp expected actual |
| 321 | ' |
| 322 | |
David Steven Tweed | 8464010 | 2008-02-07 02:55:14 +0000 | [diff] [blame] | 323 | test_done |