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