blob: 3968b47ed53b7e55eab26fdf8e34aa1472a1bf78 [file] [log] [blame]
Jeff Kingd3038d22014-10-15 18:41:35 -04001#!/bin/sh
2#
3# This test covers the handling of objects which might have old
4# mtimes in the filesystem (because they were used previously)
5# and are just now becoming referenced again.
6#
7# We're going to do two things that are a little bit "fake" to
8# help make our simulation easier:
9#
10# 1. We'll turn off reflogs. You can still run into
11# problems with reflogs on, but your objects
12# don't get pruned until both the reflog expiration
13# has passed on their references, _and_ they are out
14# of prune's expiration period. Dropping reflogs
15# means we only have to deal with one variable in our tests,
16# but the results generalize.
17#
18# 2. We'll use a temporary index file to create our
19# works-in-progress. Most workflows would mention
20# referenced objects in the index, which prune takes
21# into account. However, many operations don't. For
22# example, a partial commit with "git commit foo"
23# will use a temporary index. Or they may not need
24# an index at all (e.g., creating a new commit
25# to refer to an existing tree).
26
27test_description='check pruning of dependent objects'
Johannes Schindelin5902f5f2020-11-18 23:44:38 +000028GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000029export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
30
Ævar Arnfjörð Bjarmasonb2e5d752023-02-07 00:07:36 +010031TEST_PASSES_SANITIZE_LEAK=true
Jeff Kingd3038d22014-10-15 18:41:35 -040032. ./test-lib.sh
33
34# We care about reachability, so we do not want to use
35# the normal test_commit, which creates extra tags.
36add () {
37 echo "$1" >"$1" &&
38 git add "$1"
39}
40commit () {
41 test_tick &&
42 add "$1" &&
43 git commit -m "$1"
44}
45
Jeff Kingabcb8652014-10-15 18:42:09 -040046maybe_repack () {
Jeff King2ba582b2021-04-28 11:42:43 -040047 case "$title" in
48 loose)
49 : skip repack
50 ;;
51 repack)
Jeff Kingabcb8652014-10-15 18:42:09 -040052 git repack -ad
Jeff King2ba582b2021-04-28 11:42:43 -040053 ;;
54 bitmap)
55 git repack -adb
56 ;;
57 *)
58 echo >&2 "unknown test type in maybe_repack"
59 return 1
60 ;;
61 esac
Jeff Kingabcb8652014-10-15 18:42:09 -040062}
Jeff Kingd3038d22014-10-15 18:41:35 -040063
Jeff King2ba582b2021-04-28 11:42:43 -040064for title in loose repack bitmap
65do
Jeff Kingabcb8652014-10-15 18:42:09 -040066 test_expect_success "make repo completely empty ($title)" '
67 rm -rf .git &&
68 git init
69 '
Jeff Kingd3038d22014-10-15 18:41:35 -040070
Jeff Kingabcb8652014-10-15 18:42:09 -040071 test_expect_success "disable reflogs ($title)" '
72 git config core.logallrefupdates false &&
David Turnerd0ab0582015-07-27 18:57:08 -040073 git reflog expire --expire=all --all
Jeff Kingabcb8652014-10-15 18:42:09 -040074 '
Jeff Kingd3038d22014-10-15 18:41:35 -040075
Jeff Kingabcb8652014-10-15 18:42:09 -040076 test_expect_success "setup basic history ($title)" '
77 commit base
78 '
Jeff Kingd3038d22014-10-15 18:41:35 -040079
Jeff Kingabcb8652014-10-15 18:42:09 -040080 test_expect_success "create and abandon some objects ($title)" '
81 git checkout -b experiment &&
82 commit abandon &&
83 maybe_repack &&
Johannes Schindelin5902f5f2020-11-18 23:44:38 +000084 git checkout main &&
Jeff Kingabcb8652014-10-15 18:42:09 -040085 git branch -D experiment
86 '
Jeff Kingd3038d22014-10-15 18:41:35 -040087
Jeff Kingabcb8652014-10-15 18:42:09 -040088 test_expect_success "simulate time passing ($title)" '
Junio C Hamanodeb98452018-04-25 13:29:00 +090089 test-tool chmtime --get -86400 $(find .git/objects -type f)
Jeff Kingabcb8652014-10-15 18:42:09 -040090 '
Jeff Kingd3038d22014-10-15 18:41:35 -040091
Jeff Kingabcb8652014-10-15 18:42:09 -040092 test_expect_success "start writing new commit with old blob ($title)" '
93 tree=$(
94 GIT_INDEX_FILE=index.tmp &&
95 export GIT_INDEX_FILE &&
96 git read-tree HEAD &&
97 add unrelated &&
98 add abandon &&
99 git write-tree
100 )
101 '
102
103 test_expect_success "simultaneous gc ($title)" '
104 git gc --prune=12.hours.ago
105 '
106
107 test_expect_success "finish writing out commit ($title)" '
108 commit=$(echo foo | git commit-tree -p HEAD $tree) &&
109 git update-ref HEAD $commit
110 '
111
112 # "abandon" blob should have been rescued by reference from new tree
113 test_expect_success "repository passes fsck ($title)" '
114 git fsck
115 '
Jeff King33d42212014-10-15 18:42:22 -0400116
117 test_expect_success "abandon objects again ($title)" '
118 git reset --hard HEAD^ &&
Junio C Hamanodeb98452018-04-25 13:29:00 +0900119 test-tool chmtime --get -86400 $(find .git/objects -type f)
Jeff King33d42212014-10-15 18:42:22 -0400120 '
121
122 test_expect_success "start writing new commit with same tree ($title)" '
123 tree=$(
124 GIT_INDEX_FILE=index.tmp &&
125 export GIT_INDEX_FILE &&
126 git read-tree HEAD &&
127 add abandon &&
128 add unrelated &&
129 git write-tree
130 )
131 '
132
133 test_expect_success "simultaneous gc ($title)" '
134 git gc --prune=12.hours.ago
135 '
136
137 # tree should have been refreshed by write-tree
138 test_expect_success "finish writing out commit ($title)" '
139 commit=$(echo foo | git commit-tree -p HEAD $tree) &&
140 git update-ref HEAD $commit
141 '
Jeff Kingabcb8652014-10-15 18:42:09 -0400142done
Jeff Kingd3038d22014-10-15 18:41:35 -0400143
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400144test_expect_success 'do not complain about existing broken links (commit)' '
brian m. carlson252a4ee2020-07-29 23:13:58 +0000145 cat >broken-commit <<-EOF &&
146 tree $(test_oid 001)
147 parent $(test_oid 002)
Jeff Kingdaf7d862015-06-01 05:56:37 -0400148 author whatever <whatever@example.com> 1234 -0000
149 committer whatever <whatever@example.com> 1234 -0000
150
151 some message
152 EOF
153 commit=$(git hash-object -t commit -w broken-commit) &&
Derrick Stoleeb068d9a2019-08-13 11:37:45 -0700154 git gc -q 2>stderr &&
Jeff Kingdaf7d862015-06-01 05:56:37 -0400155 verbose git cat-file -e $commit &&
156 test_must_be_empty stderr
157'
158
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400159test_expect_success 'do not complain about existing broken links (tree)' '
brian m. carlson252a4ee2020-07-29 23:13:58 +0000160 cat >broken-tree <<-EOF &&
161 100644 blob $(test_oid 003) foo
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400162 EOF
163 tree=$(git mktree --missing <broken-tree) &&
Derrick Stoleeb068d9a2019-08-13 11:37:45 -0700164 git gc -q 2>stderr &&
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400165 git cat-file -e $tree &&
166 test_must_be_empty stderr
167'
168
169test_expect_success 'do not complain about existing broken links (tag)' '
brian m. carlson252a4ee2020-07-29 23:13:58 +0000170 cat >broken-tag <<-EOF &&
171 object $(test_oid 004)
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400172 type commit
173 tag broken
174 tagger whatever <whatever@example.com> 1234 -0000
175
176 this is a broken tag
177 EOF
178 tag=$(git hash-object -t tag -w broken-tag) &&
Derrick Stoleeb068d9a2019-08-13 11:37:45 -0700179 git gc -q 2>stderr &&
Jeff Kinga3ba6bf2017-05-20 04:30:25 -0400180 git cat-file -e $tag &&
181 test_must_be_empty stderr
182'
183
Jeff Kingd3038d22014-10-15 18:41:35 -0400184test_done