]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3210-pack-refs.sh
t9001: fix indentation in test_no_confirm()
[thirdparty/git.git] / t / t3210-pack-refs.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Amos Waterland
4 # Copyright (c) 2006 Christian Couder
5 #
6
7 test_description='git pack-refs should not change the branch semantic
8
9 This test runs git pack-refs and git show-ref and checks that the branch
10 semantic is still the same.
11 '
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
17
18 test_expect_success 'enable reflogs' '
19 git config core.logallrefupdates true
20 '
21
22 test_expect_success \
23 'prepare a trivial repository' \
24 'echo Hello > A &&
25 git update-index --add A &&
26 git commit -m "Initial commit." &&
27 HEAD=$(git rev-parse --verify HEAD)'
28
29 SHA1=
30
31 test_expect_success \
32 'see if git show-ref works as expected' \
33 'git branch a &&
34 SHA1=$(cat .git/refs/heads/a) &&
35 echo "$SHA1 refs/heads/a" >expect &&
36 git show-ref a >result &&
37 test_cmp expect result'
38
39 test_expect_success \
40 'see if a branch still exists when packed' \
41 'git branch b &&
42 git pack-refs --all &&
43 rm -f .git/refs/heads/b &&
44 echo "$SHA1 refs/heads/b" >expect &&
45 git show-ref b >result &&
46 test_cmp expect result'
47
48 test_expect_success 'git branch c/d should barf if branch c exists' '
49 git branch c &&
50 git pack-refs --all &&
51 rm -f .git/refs/heads/c &&
52 test_must_fail git branch c/d
53 '
54
55 test_expect_success \
56 'see if a branch still exists after git pack-refs --prune' \
57 'git branch e &&
58 git pack-refs --all --prune &&
59 echo "$SHA1 refs/heads/e" >expect &&
60 git show-ref e >result &&
61 test_cmp expect result'
62
63 test_expect_success 'see if git pack-refs --prune remove ref files' '
64 git branch f &&
65 git pack-refs --all --prune &&
66 ! test -f .git/refs/heads/f
67 '
68
69 test_expect_success 'see if git pack-refs --prune removes empty dirs' '
70 git branch r/s/t &&
71 git pack-refs --all --prune &&
72 ! test -e .git/refs/heads/r
73 '
74
75 test_expect_success \
76 'git branch g should work when git branch g/h has been deleted' \
77 'git branch g/h &&
78 git pack-refs --all --prune &&
79 git branch -d g/h &&
80 git branch g &&
81 git pack-refs --all &&
82 git branch -d g'
83
84 test_expect_success 'git branch i/j/k should barf if branch i exists' '
85 git branch i &&
86 git pack-refs --all --prune &&
87 test_must_fail git branch i/j/k
88 '
89
90 test_expect_success \
91 'test git branch k after branch k/l/m and k/lm have been deleted' \
92 'git branch k/l &&
93 git branch k/lm &&
94 git branch -d k/l &&
95 git branch k/l/m &&
96 git branch -d k/l/m &&
97 git branch -d k/lm &&
98 git branch k'
99
100 test_expect_success \
101 'test git branch n after some branch deletion and pruning' \
102 'git branch n/o &&
103 git branch n/op &&
104 git branch -d n/o &&
105 git branch n/o/p &&
106 git branch -d n/op &&
107 git pack-refs --all --prune &&
108 git branch -d n/o/p &&
109 git branch n'
110
111 test_expect_success \
112 'see if up-to-date packed refs are preserved' \
113 'git branch q &&
114 git pack-refs --all --prune &&
115 git update-ref refs/heads/q refs/heads/q &&
116 ! test -f .git/refs/heads/q'
117
118 test_expect_success 'pack, prune and repack' '
119 git tag foo &&
120 git pack-refs --all --prune &&
121 git show-ref >all-of-them &&
122 git pack-refs &&
123 git show-ref >again &&
124 test_cmp all-of-them again
125 '
126
127 test_expect_success 'explicit pack-refs with dangling packed reference' '
128 git commit --allow-empty -m "soon to be garbage-collected" &&
129 git pack-refs --all &&
130 git reset --hard HEAD^ &&
131 git reflog expire --expire=all --all &&
132 git prune --expire=all &&
133 git pack-refs --all 2>result &&
134 test_must_be_empty result
135 '
136
137 test_expect_success 'delete ref with dangling packed version' '
138 git checkout -b lamb &&
139 git commit --allow-empty -m "future garbage" &&
140 git pack-refs --all &&
141 git reset --hard HEAD^ &&
142 git checkout main &&
143 git reflog expire --expire=all --all &&
144 git prune --expire=all &&
145 git branch -d lamb 2>result &&
146 test_must_be_empty result
147 '
148
149 test_expect_success 'delete ref while another dangling packed ref' '
150 git branch lamb &&
151 git commit --allow-empty -m "future garbage" &&
152 git pack-refs --all &&
153 git reset --hard HEAD^ &&
154 git reflog expire --expire=all --all &&
155 git prune --expire=all &&
156 git branch -d lamb 2>result &&
157 test_must_be_empty result
158 '
159
160 test_expect_success 'pack ref directly below refs/' '
161 git update-ref refs/top HEAD &&
162 git pack-refs --all --prune &&
163 grep refs/top .git/packed-refs &&
164 test_path_is_missing .git/refs/top
165 '
166
167 test_expect_success 'do not pack ref in refs/bisect' '
168 git update-ref refs/bisect/local HEAD &&
169 git pack-refs --all --prune &&
170 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
171 test_path_is_file .git/refs/bisect/local
172 '
173
174 test_expect_success 'disable reflogs' '
175 git config core.logallrefupdates false &&
176 rm -rf .git/logs
177 '
178
179 test_expect_success 'create packed foo/bar/baz branch' '
180 git branch foo/bar/baz &&
181 git pack-refs --all --prune &&
182 test_path_is_missing .git/refs/heads/foo/bar/baz &&
183 test_must_fail git reflog exists refs/heads/foo/bar/baz
184 '
185
186 test_expect_success 'notice d/f conflict with existing directory' '
187 test_must_fail git branch foo &&
188 test_must_fail git branch foo/bar
189 '
190
191 test_expect_success 'existing directory reports concrete ref' '
192 test_must_fail git branch foo 2>stderr &&
193 test_i18ngrep refs/heads/foo/bar/baz stderr
194 '
195
196 test_expect_success 'notice d/f conflict with existing ref' '
197 test_must_fail git branch foo/bar/baz/extra &&
198 test_must_fail git branch foo/bar/baz/lots/of/extra/components
199 '
200
201 test_expect_success 'reject packed-refs with unterminated line' '
202 cp .git/packed-refs .git/packed-refs.bak &&
203 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
204 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
205 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
206 test_must_fail git for-each-ref >out 2>err &&
207 test_cmp expected_err err
208 '
209
210 test_expect_success 'reject packed-refs containing junk' '
211 cp .git/packed-refs .git/packed-refs.bak &&
212 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
213 printf "%s\n" "bogus content" >>.git/packed-refs &&
214 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
215 test_must_fail git for-each-ref >out 2>err &&
216 test_cmp expected_err err
217 '
218
219 test_expect_success 'reject packed-refs with a short SHA-1' '
220 cp .git/packed-refs .git/packed-refs.bak &&
221 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
222 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
223 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
224 test_must_fail git for-each-ref >out 2>err &&
225 test_cmp expected_err err
226 '
227
228 test_expect_success 'timeout if packed-refs.lock exists' '
229 LOCK=.git/packed-refs.lock &&
230 >"$LOCK" &&
231 test_when_finished "rm -f $LOCK" &&
232 test_must_fail git pack-refs --all --prune
233 '
234
235 test_expect_success 'retry acquiring packed-refs.lock' '
236 LOCK=.git/packed-refs.lock &&
237 >"$LOCK" &&
238 test_when_finished "wait && rm -f $LOCK" &&
239 {
240 ( sleep 1 && rm -f $LOCK ) &
241 } &&
242 git -c core.packedrefstimeout=3000 pack-refs --all --prune
243 '
244
245 test_expect_success SYMLINKS 'pack symlinked packed-refs' '
246 # First make sure that symlinking works when reading:
247 git update-ref refs/heads/lossy refs/heads/main &&
248 git for-each-ref >all-refs-before &&
249 mv .git/packed-refs .git/my-deviant-packed-refs &&
250 ln -s my-deviant-packed-refs .git/packed-refs &&
251 git for-each-ref >all-refs-linked &&
252 test_cmp all-refs-before all-refs-linked &&
253 git pack-refs --all --prune &&
254 git for-each-ref >all-refs-packed &&
255 test_cmp all-refs-before all-refs-packed &&
256 test -h .git/packed-refs &&
257 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
258 '
259
260 test_done