t5411: add basic test cases for proc-receive hook
[git] / t / t5324-split-commit-graph.sh
1 #!/bin/sh
2
3 test_description='split commit graph'
4 . ./test-lib.sh
5
6 GIT_TEST_COMMIT_GRAPH=0
7 GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
8
9 test_expect_success 'setup repo' '
10         git init &&
11         git config core.commitGraph true &&
12         git config gc.writeCommitGraph false &&
13         infodir=".git/objects/info" &&
14         graphdir="$infodir/commit-graphs" &&
15         test_oid_init &&
16         test_oid_cache <<-EOM
17         shallow sha1:1760
18         shallow sha256:2064
19
20         base sha1:1376
21         base sha256:1496
22         EOM
23 '
24
25 graph_read_expect() {
26         NUM_BASE=0
27         if test ! -z $2
28         then
29                 NUM_BASE=$2
30         fi
31         cat >expect <<- EOF
32         header: 43475048 1 1 3 $NUM_BASE
33         num_commits: $1
34         chunks: oid_fanout oid_lookup commit_metadata
35         EOF
36         test-tool read-graph >output &&
37         test_cmp expect output
38 }
39
40 test_expect_success 'create commits and write commit-graph' '
41         for i in $(test_seq 3)
42         do
43                 test_commit $i &&
44                 git branch commits/$i || return 1
45         done &&
46         git commit-graph write --reachable &&
47         test_path_is_file $infodir/commit-graph &&
48         graph_read_expect 3
49 '
50
51 graph_git_two_modes() {
52         git -c core.commitGraph=true $1 >output
53         git -c core.commitGraph=false $1 >expect
54         test_cmp expect output
55 }
56
57 graph_git_behavior() {
58         MSG=$1
59         BRANCH=$2
60         COMPARE=$3
61         test_expect_success "check normal git operations: $MSG" '
62                 graph_git_two_modes "log --oneline $BRANCH" &&
63                 graph_git_two_modes "log --topo-order $BRANCH" &&
64                 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
65                 graph_git_two_modes "branch -vv" &&
66                 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
67         '
68 }
69
70 graph_git_behavior 'graph exists' commits/3 commits/1
71
72 verify_chain_files_exist() {
73         for hash in $(cat $1/commit-graph-chain)
74         do
75                 test_path_is_file $1/graph-$hash.graph || return 1
76         done
77 }
78
79 test_expect_success 'add more commits, and write a new base graph' '
80         git reset --hard commits/1 &&
81         for i in $(test_seq 4 5)
82         do
83                 test_commit $i &&
84                 git branch commits/$i || return 1
85         done &&
86         git reset --hard commits/2 &&
87         for i in $(test_seq 6 10)
88         do
89                 test_commit $i &&
90                 git branch commits/$i || return 1
91         done &&
92         git reset --hard commits/2 &&
93         git merge commits/4 &&
94         git branch merge/1 &&
95         git reset --hard commits/4 &&
96         git merge commits/6 &&
97         git branch merge/2 &&
98         git commit-graph write --reachable &&
99         graph_read_expect 12
100 '
101
102 test_expect_success 'fork and fail to base a chain on a commit-graph file' '
103         test_when_finished rm -rf fork &&
104         git clone . fork &&
105         (
106                 cd fork &&
107                 rm .git/objects/info/commit-graph &&
108                 echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
109                 test_commit new-commit &&
110                 git commit-graph write --reachable --split &&
111                 test_path_is_file $graphdir/commit-graph-chain &&
112                 test_line_count = 1 $graphdir/commit-graph-chain &&
113                 verify_chain_files_exist $graphdir
114         )
115 '
116
117 test_expect_success 'add three more commits, write a tip graph' '
118         git reset --hard commits/3 &&
119         git merge merge/1 &&
120         git merge commits/5 &&
121         git merge merge/2 &&
122         git branch merge/3 &&
123         git commit-graph write --reachable --split &&
124         test_path_is_missing $infodir/commit-graph &&
125         test_path_is_file $graphdir/commit-graph-chain &&
126         ls $graphdir/graph-*.graph >graph-files &&
127         test_line_count = 2 graph-files &&
128         verify_chain_files_exist $graphdir
129 '
130
131 graph_git_behavior 'split commit-graph: merge 3 vs 2' merge/3 merge/2
132
133 test_expect_success 'add one commit, write a tip graph' '
134         test_commit 11 &&
135         git branch commits/11 &&
136         git commit-graph write --reachable --split &&
137         test_path_is_missing $infodir/commit-graph &&
138         test_path_is_file $graphdir/commit-graph-chain &&
139         ls $graphdir/graph-*.graph >graph-files &&
140         test_line_count = 3 graph-files &&
141         verify_chain_files_exist $graphdir
142 '
143
144 graph_git_behavior 'three-layer commit-graph: commit 11 vs 6' commits/11 commits/6
145
146 test_expect_success 'add one commit, write a merged graph' '
147         test_commit 12 &&
148         git branch commits/12 &&
149         git commit-graph write --reachable --split &&
150         test_path_is_file $graphdir/commit-graph-chain &&
151         test_line_count = 2 $graphdir/commit-graph-chain &&
152         ls $graphdir/graph-*.graph >graph-files &&
153         test_line_count = 2 graph-files &&
154         verify_chain_files_exist $graphdir
155 '
156
157 graph_git_behavior 'merged commit-graph: commit 12 vs 6' commits/12 commits/6
158
159 test_expect_success 'create fork and chain across alternate' '
160         git clone . fork &&
161         (
162                 cd fork &&
163                 git config core.commitGraph true &&
164                 rm -rf $graphdir &&
165                 echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
166                 test_commit 13 &&
167                 git branch commits/13 &&
168                 git commit-graph write --reachable --split &&
169                 test_path_is_file $graphdir/commit-graph-chain &&
170                 test_line_count = 3 $graphdir/commit-graph-chain &&
171                 ls $graphdir/graph-*.graph >graph-files &&
172                 test_line_count = 1 graph-files &&
173                 git -c core.commitGraph=true  rev-list HEAD >expect &&
174                 git -c core.commitGraph=false rev-list HEAD >actual &&
175                 test_cmp expect actual &&
176                 test_commit 14 &&
177                 git commit-graph write --reachable --split --object-dir=.git/objects/ &&
178                 test_line_count = 3 $graphdir/commit-graph-chain &&
179                 ls $graphdir/graph-*.graph >graph-files &&
180                 test_line_count = 1 graph-files
181         )
182 '
183
184 graph_git_behavior 'alternate: commit 13 vs 6' commits/13 commits/6
185
186 test_expect_success 'test merge stragety constants' '
187         git clone . merge-2 &&
188         (
189                 cd merge-2 &&
190                 git config core.commitGraph true &&
191                 test_line_count = 2 $graphdir/commit-graph-chain &&
192                 test_commit 14 &&
193                 git commit-graph write --reachable --split --size-multiple=2 &&
194                 test_line_count = 3 $graphdir/commit-graph-chain
195
196         ) &&
197         git clone . merge-10 &&
198         (
199                 cd merge-10 &&
200                 git config core.commitGraph true &&
201                 test_line_count = 2 $graphdir/commit-graph-chain &&
202                 test_commit 14 &&
203                 git commit-graph write --reachable --split --size-multiple=10 &&
204                 test_line_count = 1 $graphdir/commit-graph-chain &&
205                 ls $graphdir/graph-*.graph >graph-files &&
206                 test_line_count = 1 graph-files
207         ) &&
208         git clone . merge-10-expire &&
209         (
210                 cd merge-10-expire &&
211                 git config core.commitGraph true &&
212                 test_line_count = 2 $graphdir/commit-graph-chain &&
213                 test_commit 15 &&
214                 touch $graphdir/to-delete.graph $graphdir/to-keep.graph &&
215                 test-tool chmtime =1546362000 $graphdir/to-delete.graph &&
216                 test-tool chmtime =1546362001 $graphdir/to-keep.graph &&
217                 git commit-graph write --reachable --split --size-multiple=10 \
218                         --expire-time="2019-01-01 12:00 -05:00" &&
219                 test_line_count = 1 $graphdir/commit-graph-chain &&
220                 test_path_is_missing $graphdir/to-delete.graph &&
221                 test_path_is_file $graphdir/to-keep.graph &&
222                 ls $graphdir/graph-*.graph >graph-files &&
223                 test_line_count = 3 graph-files
224         ) &&
225         git clone --no-hardlinks . max-commits &&
226         (
227                 cd max-commits &&
228                 git config core.commitGraph true &&
229                 test_line_count = 2 $graphdir/commit-graph-chain &&
230                 test_commit 16 &&
231                 test_commit 17 &&
232                 git commit-graph write --reachable --split --max-commits=1 &&
233                 test_line_count = 1 $graphdir/commit-graph-chain &&
234                 ls $graphdir/graph-*.graph >graph-files &&
235                 test_line_count = 1 graph-files
236         )
237 '
238
239 test_expect_success 'remove commit-graph-chain file after flattening' '
240         git clone . flatten &&
241         (
242                 cd flatten &&
243                 test_line_count = 2 $graphdir/commit-graph-chain &&
244                 git commit-graph write --reachable &&
245                 test_path_is_missing $graphdir/commit-graph-chain &&
246                 ls $graphdir >graph-files &&
247                 test_line_count = 0 graph-files
248         )
249 '
250
251 corrupt_file() {
252         file=$1
253         pos=$2
254         data="${3:-\0}"
255         chmod a+w "$file" &&
256         printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
257 }
258
259 test_expect_success 'verify hashes along chain, even in shallow' '
260         git clone --no-hardlinks . verify &&
261         (
262                 cd verify &&
263                 git commit-graph verify &&
264                 base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
265                 corrupt_file "$base_file" $(test_oid shallow) "\01" &&
266                 test_must_fail git commit-graph verify --shallow 2>test_err &&
267                 grep -v "^+" test_err >err &&
268                 test_i18ngrep "incorrect checksum" err
269         )
270 '
271
272 test_expect_success 'verify --shallow does not check base contents' '
273         git clone --no-hardlinks . verify-shallow &&
274         (
275                 cd verify-shallow &&
276                 git commit-graph verify &&
277                 base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
278                 corrupt_file "$base_file" 1000 "\01" &&
279                 git commit-graph verify --shallow &&
280                 test_must_fail git commit-graph verify 2>test_err &&
281                 grep -v "^+" test_err >err &&
282                 test_i18ngrep "incorrect checksum" err
283         )
284 '
285
286 test_expect_success 'warn on base graph chunk incorrect' '
287         git clone --no-hardlinks . base-chunk &&
288         (
289                 cd base-chunk &&
290                 git commit-graph verify &&
291                 base_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
292                 corrupt_file "$base_file" $(test_oid base) "\01" &&
293                 git commit-graph verify --shallow 2>test_err &&
294                 grep -v "^+" test_err >err &&
295                 test_i18ngrep "commit-graph chain does not match" err
296         )
297 '
298
299 test_expect_success 'verify after commit-graph-chain corruption' '
300         git clone --no-hardlinks . verify-chain &&
301         (
302                 cd verify-chain &&
303                 corrupt_file "$graphdir/commit-graph-chain" 60 "G" &&
304                 git commit-graph verify 2>test_err &&
305                 grep -v "^+" test_err >err &&
306                 test_i18ngrep "invalid commit-graph chain" err &&
307                 corrupt_file "$graphdir/commit-graph-chain" 60 "A" &&
308                 git commit-graph verify 2>test_err &&
309                 grep -v "^+" test_err >err &&
310                 test_i18ngrep "unable to find all commit-graph files" err
311         )
312 '
313
314 test_expect_success 'verify across alternates' '
315         git clone --no-hardlinks . verify-alt &&
316         (
317                 cd verify-alt &&
318                 rm -rf $graphdir &&
319                 altdir="$(pwd)/../.git/objects" &&
320                 echo "$altdir" >.git/objects/info/alternates &&
321                 git commit-graph verify --object-dir="$altdir/" &&
322                 test_commit extra &&
323                 git commit-graph write --reachable --split &&
324                 tip_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
325                 corrupt_file "$tip_file" 100 "\01" &&
326                 test_must_fail git commit-graph verify --shallow 2>test_err &&
327                 grep -v "^+" test_err >err &&
328                 test_i18ngrep "commit-graph has incorrect fanout value" err
329         )
330 '
331
332 test_expect_success 'add octopus merge' '
333         git reset --hard commits/10 &&
334         git merge commits/3 commits/4 &&
335         git branch merge/octopus &&
336         git commit-graph write --reachable --split &&
337         git commit-graph verify --progress 2>err &&
338         test_line_count = 3 err &&
339         test_i18ngrep ! warning err &&
340         test_line_count = 3 $graphdir/commit-graph-chain
341 '
342
343 graph_git_behavior 'graph exists' merge/octopus commits/12
344
345 test_expect_success 'split across alternate where alternate is not split' '
346         git commit-graph write --reachable &&
347         test_path_is_file .git/objects/info/commit-graph &&
348         cp .git/objects/info/commit-graph . &&
349         git clone --no-hardlinks . alt-split &&
350         (
351                 cd alt-split &&
352                 rm -f .git/objects/info/commit-graph &&
353                 echo "$(pwd)"/../.git/objects >.git/objects/info/alternates &&
354                 test_commit 18 &&
355                 git commit-graph write --reachable --split &&
356                 test_line_count = 1 $graphdir/commit-graph-chain
357         ) &&
358         test_cmp commit-graph .git/objects/info/commit-graph
359 '
360
361 test_expect_success '--split=no-merge always writes an incremental' '
362         test_when_finished rm -rf a b &&
363         rm -rf $graphdir $infodir/commit-graph &&
364         git reset --hard commits/2 &&
365         git rev-list HEAD~1 >a &&
366         git rev-list HEAD >b &&
367         git commit-graph write --split --stdin-commits <a &&
368         git commit-graph write --split=no-merge --stdin-commits <b &&
369         test_line_count = 2 $graphdir/commit-graph-chain
370 '
371
372 test_expect_success '--split=replace replaces the chain' '
373         rm -rf $graphdir $infodir/commit-graph &&
374         git reset --hard commits/3 &&
375         git rev-list -1 HEAD~2 >a &&
376         git rev-list -1 HEAD~1 >b &&
377         git rev-list -1 HEAD >c &&
378         git commit-graph write --split=no-merge --stdin-commits <a &&
379         git commit-graph write --split=no-merge --stdin-commits <b &&
380         git commit-graph write --split=no-merge --stdin-commits <c &&
381         test_line_count = 3 $graphdir/commit-graph-chain &&
382         git commit-graph write --stdin-commits --split=replace <b &&
383         test_path_is_missing $infodir/commit-graph &&
384         test_path_is_file $graphdir/commit-graph-chain &&
385         ls $graphdir/graph-*.graph >graph-files &&
386         test_line_count = 1 graph-files &&
387         verify_chain_files_exist $graphdir &&
388         graph_read_expect 2
389 '
390
391 test_expect_success ULIMIT_FILE_DESCRIPTORS 'handles file descriptor exhaustion' '
392         git init ulimit &&
393         (
394                 cd ulimit &&
395                 for i in $(test_seq 64)
396                 do
397                         test_commit $i &&
398                         test_might_fail run_with_limited_open_files git commit-graph write \
399                                 --split=no-merge --reachable || return 1
400                 done
401         )
402 '
403
404 test_done