]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5318-commit-graph.sh
t5318: extract helpers to lib-commit-graph.sh
[thirdparty/git.git] / t / t5318-commit-graph.sh
CommitLineData
f237c8b6
DS
1#!/bin/sh
2
3test_description='commit graph'
4. ./test-lib.sh
5
d5b873c8
GS
6GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
7
6d209a01 8test_expect_success 'usage' '
367c5f36 9 test_expect_code 129 git commit-graph write blah 2>err &&
6d209a01
ÆAB
10 test_expect_code 129 git commit-graph write verify
11'
12
367c5f36
ÆAB
13test_expect_success 'usage shown without sub-command' '
14 test_expect_code 129 git commit-graph 2>err &&
15 ! grep error: err
16'
17
18test_expect_success 'usage shown with an error on unknown sub-command' '
19 cat >expect <<-\EOF &&
20 error: unrecognized subcommand: unknown
21 EOF
22 test_expect_code 129 git commit-graph unknown 2>stderr &&
23 grep error stderr >actual &&
24 test_cmp expect actual
25'
26
f237c8b6
DS
27test_expect_success 'setup full repo' '
28 mkdir full &&
29 cd "$TRASH_DIRECTORY/full" &&
30 git init &&
177722b3 31 git config core.commitGraph true &&
17925e06 32 objdir=".git/objects"
f237c8b6
DS
33'
34
1f9becae
TB
35test_expect_success POSIXPERM 'tweak umask for modebit tests' '
36 umask 022
37'
38
283e68c7
DS
39test_expect_success 'verify graph with no graph file' '
40 cd "$TRASH_DIRECTORY/full" &&
41 git commit-graph verify
42'
43
f237c8b6
DS
44test_expect_success 'write graph with no packs' '
45 cd "$TRASH_DIRECTORY/full" &&
1793280e
TB
46 git commit-graph write --object-dir $objdir &&
47 test_path_is_missing $objdir/info/commit-graph
f237c8b6
DS
48'
49
7c5c9b9c 50test_expect_success 'exit with correct error on bad input to --stdin-packs' '
e103f727
DS
51 cd "$TRASH_DIRECTORY/full" &&
52 echo doesnotexist >in &&
9916073b 53 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
e103f727
DS
54 test_i18ngrep "error adding pack" stderr
55'
56
f237c8b6
DS
57test_expect_success 'create commits and repack' '
58 cd "$TRASH_DIRECTORY/full" &&
59 for i in $(test_seq 3)
60 do
61 test_commit $i &&
d0fd9931 62 git branch commits/$i || return 1
f237c8b6
DS
63 done &&
64 git repack
65'
66
17925e06 67. "$TEST_DIRECTORY"/lib-commit-graph.sh
177722b3
DS
68
69graph_git_behavior 'no graph' full commits/3 commits/1
70
1f1304d4
TB
71test_expect_success 'exit with correct error on bad input to --stdin-commits' '
72 cd "$TRASH_DIRECTORY/full" &&
2f00c355
TB
73 # invalid, non-hex OID
74 echo HEAD >in &&
75 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
1f1304d4 76 test_i18ngrep "unexpected non-hex object ID: HEAD" stderr &&
2f00c355
TB
77 # non-existent OID
78 echo $ZERO_OID >in &&
79 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
80 test_i18ngrep "invalid object" stderr &&
81 # valid commit and tree OID
82 git rev-parse HEAD HEAD^{tree} >in &&
83 git commit-graph write --stdin-commits <in &&
e8b63005 84 graph_read_expect 3 generation_data
1f1304d4
TB
85'
86
f237c8b6
DS
87test_expect_success 'write graph' '
88 cd "$TRASH_DIRECTORY/full" &&
0b918b75 89 git commit-graph write &&
2a2e32bd 90 test_path_is_file $objdir/info/commit-graph &&
e8b63005 91 graph_read_expect "3" generation_data
f237c8b6
DS
92'
93
1f9becae
TB
94test_expect_success POSIXPERM 'write graph has correct permissions' '
95 test_path_is_file $objdir/info/commit-graph &&
96 echo "-r--r--r--" >expect &&
97 test_modebits $objdir/info/commit-graph >actual &&
98 test_cmp expect actual
99'
100
177722b3
DS
101graph_git_behavior 'graph exists' full commits/3 commits/1
102
f237c8b6
DS
103test_expect_success 'Add more commits' '
104 cd "$TRASH_DIRECTORY/full" &&
105 git reset --hard commits/1 &&
106 for i in $(test_seq 4 5)
107 do
108 test_commit $i &&
d0fd9931 109 git branch commits/$i || return 1
f237c8b6
DS
110 done &&
111 git reset --hard commits/2 &&
112 for i in $(test_seq 6 7)
113 do
114 test_commit $i &&
d0fd9931 115 git branch commits/$i || return 1
f237c8b6
DS
116 done &&
117 git reset --hard commits/2 &&
118 git merge commits/4 &&
119 git branch merge/1 &&
120 git reset --hard commits/4 &&
121 git merge commits/6 &&
122 git branch merge/2 &&
123 git reset --hard commits/3 &&
124 git merge commits/5 commits/7 &&
125 git branch merge/3 &&
126 git repack
127'
128
73716122
GS
129test_expect_success 'commit-graph write progress off for redirected stderr' '
130 cd "$TRASH_DIRECTORY/full" &&
131 git commit-graph write 2>err &&
6334c5ff 132 test_must_be_empty err
73716122
GS
133'
134
135test_expect_success 'commit-graph write force progress on for stderr' '
136 cd "$TRASH_DIRECTORY/full" &&
44a4693b 137 GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err &&
73716122
GS
138 test_file_not_empty err
139'
140
141test_expect_success 'commit-graph write with the --no-progress option' '
142 cd "$TRASH_DIRECTORY/full" &&
143 git commit-graph write --no-progress 2>err &&
6334c5ff 144 test_must_be_empty err
73716122
GS
145'
146
94fbd914
TB
147test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
148 cd "$TRASH_DIRECTORY/full" &&
149 git rev-parse commits/5 >in &&
150 git commit-graph write --stdin-commits <in 2>err &&
151 test_must_be_empty err
152'
153
154test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
155 cd "$TRASH_DIRECTORY/full" &&
156 git rev-parse commits/5 >in &&
157 GIT_PROGRESS_DELAY=0 git commit-graph write --stdin-commits --progress <in 2>err &&
158 test_i18ngrep "Collecting commits from input" err
159'
160
161test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
162 cd "$TRASH_DIRECTORY/full" &&
163 git rev-parse commits/5 >in &&
164 git commit-graph write --stdin-commits --no-progress <in 2>err &&
165 test_must_be_empty err
73716122
GS
166'
167
168test_expect_success 'commit-graph verify progress off for redirected stderr' '
169 cd "$TRASH_DIRECTORY/full" &&
170 git commit-graph verify 2>err &&
6334c5ff 171 test_must_be_empty err
73716122
GS
172'
173
174test_expect_success 'commit-graph verify force progress on for stderr' '
175 cd "$TRASH_DIRECTORY/full" &&
44a4693b 176 GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
73716122
GS
177 test_file_not_empty err
178'
179
180test_expect_success 'commit-graph verify with the --no-progress option' '
181 cd "$TRASH_DIRECTORY/full" &&
182 git commit-graph verify --no-progress 2>err &&
6334c5ff 183 test_must_be_empty err
73716122
GS
184'
185
f237c8b6
DS
186# Current graph structure:
187#
188# __M3___
189# / | \
190# 3 M1 5 M2 7
191# |/ \|/ \|
192# 2 4 6
193# |___/____/
194# 1
195
f237c8b6
DS
196test_expect_success 'write graph with merges' '
197 cd "$TRASH_DIRECTORY/full" &&
198 git commit-graph write &&
2a2e32bd 199 test_path_is_file $objdir/info/commit-graph &&
e8b63005 200 graph_read_expect "10" "generation_data extra_edges"
f237c8b6
DS
201'
202
177722b3
DS
203graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
204graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
205graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
206
f237c8b6
DS
207test_expect_success 'Add one more commit' '
208 cd "$TRASH_DIRECTORY/full" &&
209 test_commit 8 &&
210 git branch commits/8 &&
211 ls $objdir/pack | grep idx >existing-idx &&
212 git repack &&
4a3ed638 213 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
f237c8b6
DS
214'
215
216# Current graph structure:
217#
218# 8
219# |
220# __M3___
221# / | \
222# 3 M1 5 M2 7
223# |/ \|/ \|
224# 2 4 6
225# |___/____/
226# 1
227
177722b3
DS
228graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
229graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
230
f237c8b6
DS
231test_expect_success 'write graph with new commit' '
232 cd "$TRASH_DIRECTORY/full" &&
233 git commit-graph write &&
2a2e32bd 234 test_path_is_file $objdir/info/commit-graph &&
e8b63005 235 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
236'
237
177722b3
DS
238graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
239graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
240
f237c8b6
DS
241test_expect_success 'write graph with nothing new' '
242 cd "$TRASH_DIRECTORY/full" &&
243 git commit-graph write &&
2a2e32bd 244 test_path_is_file $objdir/info/commit-graph &&
e8b63005 245 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
246'
247
177722b3
DS
248graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
249graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
250
049d51a2
DS
251test_expect_success 'build graph from latest pack with closure' '
252 cd "$TRASH_DIRECTORY/full" &&
253 cat new-idx | git commit-graph write --stdin-packs &&
254 test_path_is_file $objdir/info/commit-graph &&
e8b63005 255 graph_read_expect "9" "generation_data extra_edges"
049d51a2
DS
256'
257
258graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
259graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
260
3d5df01b
DS
261test_expect_success 'build graph from commits with closure' '
262 cd "$TRASH_DIRECTORY/full" &&
263 git tag -a -m "merge" tag/merge merge/2 &&
264 git rev-parse tag/merge >commits-in &&
265 git rev-parse merge/1 >>commits-in &&
266 cat commits-in | git commit-graph write --stdin-commits &&
267 test_path_is_file $objdir/info/commit-graph &&
e8b63005 268 graph_read_expect "6" "generation_data"
3d5df01b
DS
269'
270
271graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
272graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
273
7547b95b
DS
274test_expect_success 'build graph from commits with append' '
275 cd "$TRASH_DIRECTORY/full" &&
276 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
277 test_path_is_file $objdir/info/commit-graph &&
e8b63005 278 graph_read_expect "10" "generation_data extra_edges"
7547b95b
DS
279'
280
281graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
282graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
283
59fb8770
DS
284test_expect_success 'build graph using --reachable' '
285 cd "$TRASH_DIRECTORY/full" &&
286 git commit-graph write --reachable &&
287 test_path_is_file $objdir/info/commit-graph &&
e8b63005 288 graph_read_expect "11" "generation_data extra_edges"
59fb8770
DS
289'
290
291graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
292graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
293
f237c8b6
DS
294test_expect_success 'setup bare repo' '
295 cd "$TRASH_DIRECTORY" &&
296 git clone --bare --no-local full bare &&
297 cd bare &&
177722b3 298 git config core.commitGraph true &&
f237c8b6
DS
299 baredir="./objects"
300'
301
177722b3
DS
302graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
303graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
304
f237c8b6
DS
305test_expect_success 'write graph in bare repo' '
306 cd "$TRASH_DIRECTORY/bare" &&
307 git commit-graph write &&
2a2e32bd 308 test_path_is_file $baredir/info/commit-graph &&
e8b63005 309 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
310'
311
177722b3
DS
312graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
313graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
314
7adf5266
DS
315test_expect_success 'perform fast-forward merge in full repo' '
316 cd "$TRASH_DIRECTORY/full" &&
317 git checkout -b merge-5-to-8 commits/5 &&
318 git merge commits/8 &&
319 git show-ref -s merge-5-to-8 >output &&
320 git show-ref -s commits/8 >expect &&
321 test_cmp expect output
322'
323
d5d5d7b6
DS
324test_expect_success 'check that gc computes commit-graph' '
325 cd "$TRASH_DIRECTORY/full" &&
326 git commit --allow-empty -m "blank" &&
327 git commit-graph write --reachable &&
328 cp $objdir/info/commit-graph commit-graph-before-gc &&
329 git reset --hard HEAD~1 &&
330 git config gc.writeCommitGraph true &&
331 git gc &&
332 cp $objdir/info/commit-graph commit-graph-after-gc &&
eb7cc5bc 333 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
d5d5d7b6 334 git commit-graph write --reachable &&
eb7cc5bc 335 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
d5d5d7b6
DS
336'
337
d6538246
DS
338test_expect_success 'replace-objects invalidates commit-graph' '
339 cd "$TRASH_DIRECTORY" &&
340 test_when_finished rm -rf replace &&
341 git clone full replace &&
342 (
343 cd replace &&
344 git commit-graph write --reachable &&
345 test_path_is_file .git/objects/info/commit-graph &&
346 git replace HEAD~1 HEAD~2 &&
095d112f 347 graph_git_two_modes "commit-graph verify" &&
d6538246
DS
348 git -c core.commitGraph=false log >expect &&
349 git -c core.commitGraph=true log >actual &&
350 test_cmp expect actual &&
351 git commit-graph write --reachable &&
352 git -c core.commitGraph=false --no-replace-objects log >expect &&
353 git -c core.commitGraph=true --no-replace-objects log >actual &&
354 test_cmp expect actual &&
355 rm -rf .git/objects/info/commit-graph &&
356 git commit-graph write --reachable &&
357 test_path_is_file .git/objects/info/commit-graph
358 )
359'
360
20fd6d57
DS
361test_expect_success 'commit grafts invalidate commit-graph' '
362 cd "$TRASH_DIRECTORY" &&
363 test_when_finished rm -rf graft &&
364 git clone full graft &&
365 (
366 cd graft &&
367 git commit-graph write --reachable &&
368 test_path_is_file .git/objects/info/commit-graph &&
369 H1=$(git rev-parse --verify HEAD~1) &&
370 H3=$(git rev-parse --verify HEAD~3) &&
371 echo "$H1 $H3" >.git/info/grafts &&
372 git -c core.commitGraph=false log >expect &&
373 git -c core.commitGraph=true log >actual &&
374 test_cmp expect actual &&
375 git commit-graph write --reachable &&
376 git -c core.commitGraph=false --no-replace-objects log >expect &&
377 git -c core.commitGraph=true --no-replace-objects log >actual &&
378 test_cmp expect actual &&
379 rm -rf .git/objects/info/commit-graph &&
380 git commit-graph write --reachable &&
381 test_path_is_missing .git/objects/info/commit-graph
382 )
383'
384
385test_expect_success 'replace-objects invalidates commit-graph' '
386 cd "$TRASH_DIRECTORY" &&
387 test_when_finished rm -rf shallow &&
388 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
389 (
390 cd shallow &&
391 git commit-graph write --reachable &&
392 test_path_is_missing .git/objects/info/commit-graph &&
393 git fetch origin --unshallow &&
394 git commit-graph write --reachable &&
395 test_path_is_file .git/objects/info/commit-graph
396 )
397'
398
665d70ad
DS
399test_expect_success 'warn on improper hash version' '
400 git init --object-format=sha1 sha1 &&
401 (
402 cd sha1 &&
403 test_commit 1 &&
404 git commit-graph write --reachable &&
405 mv .git/objects/info/commit-graph ../cg-sha1
406 ) &&
407 git init --object-format=sha256 sha256 &&
408 (
409 cd sha256 &&
410 test_commit 1 &&
411 git commit-graph write --reachable &&
412 mv .git/objects/info/commit-graph ../cg-sha256
413 ) &&
414 (
415 cd sha1 &&
416 mv ../cg-sha256 .git/objects/info/commit-graph &&
417 git log -1 2>err &&
418 test_i18ngrep "commit-graph hash version 2 does not match version 1" err
419 ) &&
420 (
421 cd sha256 &&
422 mv ../cg-sha1 .git/objects/info/commit-graph &&
423 git log -1 2>err &&
424 test_i18ngrep "commit-graph hash version 1 does not match version 2" err
425 )
426'
427
bc50d6c9 428test_expect_success 'lower layers have overflow chunk' '
90cb1c47
DS
429 cd "$TRASH_DIRECTORY/full" &&
430 UNIX_EPOCH_ZERO="@0 +0000" &&
431 FUTURE_DATE="@2147483646 +0000" &&
432 rm -f .git/objects/info/commit-graph &&
433 test_commit --date "$FUTURE_DATE" future-1 &&
434 test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
435 git commit-graph write --reachable &&
436 test_commit --date "$FUTURE_DATE" future-2 &&
437 test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
438 git commit-graph write --reachable --split=no-merge &&
439 test_commit extra &&
440 git commit-graph write --reachable --split=no-merge &&
441 git commit-graph write --reachable &&
442 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
443 mv .git/objects/info/commit-graph commit-graph-upgraded &&
444 git commit-graph write --reachable &&
445 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
446 test_cmp .git/objects/info/commit-graph commit-graph-upgraded
447'
448
d9b9f8a6
DS
449# the verify tests below expect the commit-graph to contain
450# exactly the commits reachable from the commits/8 branch.
451# If the file changes the set of commits in the list, then the
452# offsets into the binary file will result in different edits
453# and the tests will likely break.
454
283e68c7
DS
455test_expect_success 'git commit-graph verify' '
456 cd "$TRASH_DIRECTORY/full" &&
702110aa 457 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
e8b63005
AK
458 git commit-graph verify >output &&
459 graph_read_expect 9 extra_edges
283e68c7
DS
460'
461
96af91d4 462NUM_COMMITS=9
437787ae 463NUM_OCTOPUS_EDGES=2
ae0c89d4 464HASH_LEN="$(test_oid rawsz)"
d9b9f8a6
DS
465GRAPH_BYTE_VERSION=4
466GRAPH_BYTE_HASH=5
2bd0365f
DS
467GRAPH_BYTE_CHUNK_COUNT=6
468GRAPH_CHUNK_LOOKUP_OFFSET=8
469GRAPH_CHUNK_LOOKUP_WIDTH=12
470GRAPH_CHUNK_LOOKUP_ROWS=5
471GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
472GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
473 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
474GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
475 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
9bda8467
DS
476GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
477 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
478GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
479GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
480GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
481GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
96af91d4 482GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
2e3c0737
DS
483GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
484GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
53614b13
DS
485GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
486GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
487GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
1373e547 488GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
88968ebf 489GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
437787ae
DS
490GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
491GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
492 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
493GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
41df0e30 494GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
d9b9f8a6 495
f6761faa
ÆAB
496corrupt_graph_setup() {
497 cd "$TRASH_DIRECTORY/full" &&
498 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
1f9becae
TB
499 cp $objdir/info/commit-graph commit-graph-backup &&
500 chmod u+w $objdir/info/commit-graph
f6761faa
ÆAB
501}
502
503corrupt_graph_verify() {
504 grepstr=$1
505 test_must_fail git commit-graph verify 2>test_err &&
506 grep -v "^+" test_err >err &&
2ac138d5 507 test_i18ngrep "$grepstr" err &&
43d35618
ÆAB
508 if test "$2" != "no-copy"
509 then
510 cp $objdir/info/commit-graph commit-graph-pre-write-test
511 fi &&
512 git status --short &&
7b671f8c 513 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
1f9becae 514 chmod u+w $objdir/info/commit-graph &&
43d35618 515 git commit-graph verify
f6761faa
ÆAB
516}
517
d2b86fba 518# usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
d9b9f8a6 519# Manipulates the commit-graph file at the position
d2b86fba
JS
520# by inserting the data, optionally zeroing the file
521# starting at <zero_pos>, then runs 'git commit-graph verify'
d9b9f8a6
DS
522# and places the output in the file 'err'. Test 'err' for
523# the given string.
524corrupt_graph_and_verify() {
525 pos=$1
526 data="${2:-\0}"
527 grepstr=$3
f6761faa 528 corrupt_graph_setup &&
d2b86fba
JS
529 orig_size=$(wc -c < $objdir/info/commit-graph) &&
530 zero_pos=${4:-${orig_size}} &&
d9b9f8a6 531 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
b9cc4056 532 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
f3ad2bf4 533 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
f6761faa
ÆAB
534 corrupt_graph_verify "$grepstr"
535
d9b9f8a6
DS
536}
537
7b8ce9c6
ÆAB
538test_expect_success POSIXPERM,SANITY 'detect permission problem' '
539 corrupt_graph_setup &&
540 chmod 000 $objdir/info/commit-graph &&
43d35618 541 corrupt_graph_verify "Could not open" "no-copy"
7b8ce9c6
ÆAB
542'
543
945944ca
ÆAB
544test_expect_success 'detect too small' '
545 corrupt_graph_setup &&
546 echo "a small graph" >$objdir/info/commit-graph &&
547 corrupt_graph_verify "too small"
548'
549
d9b9f8a6
DS
550test_expect_success 'detect bad signature' '
551 corrupt_graph_and_verify 0 "\0" \
552 "graph signature"
553'
554
555test_expect_success 'detect bad version' '
556 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
557 "graph version"
558'
559
560test_expect_success 'detect bad hash version' '
1d86c8f0 561 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
d9b9f8a6
DS
562 "hash version"
563'
564
2bd0365f 565test_expect_success 'detect low chunk count' '
cb9daf16 566 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
2692c2f6 567 "final chunk has non-zero id"
2bd0365f
DS
568'
569
570test_expect_success 'detect missing OID fanout chunk' '
571 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
572 "missing the OID Fanout chunk"
573'
574
575test_expect_success 'detect missing OID lookup chunk' '
576 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
577 "missing the OID Lookup chunk"
578'
579
580test_expect_success 'detect missing commit data chunk' '
581 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
582 "missing the Commit Data chunk"
583'
584
9bda8467
DS
585test_expect_success 'detect incorrect fanout' '
586 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
587 "fanout value"
588'
589
590test_expect_success 'detect incorrect fanout final value' '
591 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
592 "fanout value"
593'
594
595test_expect_success 'detect incorrect OID order' '
596 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
597 "incorrect OID order"
598'
599
96af91d4
DS
600test_expect_success 'detect OID not in object database' '
601 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
602 "from object database"
603'
604
2e3c0737
DS
605test_expect_success 'detect incorrect tree OID' '
606 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
607 "root tree OID for commit"
608'
609
53614b13
DS
610test_expect_success 'detect incorrect parent int-id' '
611 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
612 "invalid parent"
613'
614
615test_expect_success 'detect extra parent int-id' '
616 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
617 "is too long"
618'
619
620test_expect_success 'detect wrong parent' '
621 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
622 "commit-graph parent for"
623'
624
1373e547
DS
625test_expect_success 'detect incorrect generation number' '
626 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
627 "generation for commit"
628'
629
630test_expect_success 'detect incorrect generation number' '
631 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
632 "non-zero generation number"
633'
634
88968ebf
DS
635test_expect_success 'detect incorrect commit date' '
636 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
637 "commit date"
638'
639
437787ae
DS
640test_expect_success 'detect incorrect parent for octopus merge' '
641 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
642 "invalid parent"
643'
644
41df0e30
DS
645test_expect_success 'detect invalid checksum hash' '
646 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
647 "incorrect checksum"
648'
649
d2b86fba
JS
650test_expect_success 'detect incorrect chunk count' '
651 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
2ad4f1a7
SG
652 "commit-graph file is too small to hold [0-9]* chunks" \
653 $GRAPH_CHUNK_LOOKUP_OFFSET
d2b86fba
JS
654'
655
f30e4d85 656test_expect_success 'git fsck (checks commit-graph when config set to true)' '
e0fd51e1
DS
657 cd "$TRASH_DIRECTORY/full" &&
658 git fsck &&
659 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
660 "incorrect checksum" &&
43d35618 661 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
f30e4d85
GC
662 test_must_fail git -c core.commitGraph=true fsck
663'
664
665test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
666 cd "$TRASH_DIRECTORY/full" &&
667 git fsck &&
668 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
669 "incorrect checksum" &&
670 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
671 git -c core.commitGraph=false fsck
672'
673
674test_expect_success 'git fsck (checks commit-graph when config unset)' '
675 cd "$TRASH_DIRECTORY/full" &&
676 test_when_finished "git config core.commitGraph true" &&
677
678 git fsck &&
679 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
680 "incorrect checksum" &&
681 test_unconfig core.commitGraph &&
682 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
e0fd51e1
DS
683 test_must_fail git fsck
684'
685
dade47c0
JT
686test_expect_success 'setup non-the_repository tests' '
687 rm -rf repo &&
688 git init repo &&
689 test_commit -C repo one &&
690 test_commit -C repo two &&
691 git -C repo config core.commitGraph true &&
692 git -C repo rev-parse two | \
693 git -C repo commit-graph write --stdin-commits
694'
695
696test_expect_success 'parse_commit_in_graph works for non-the_repository' '
697 test-tool repository parse_commit_in_graph \
698 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
3c458630
SG
699 {
700 git -C repo log --pretty=format:"%ct " -1 &&
701 git -C repo rev-parse one
702 } >expect &&
dade47c0
JT
703 test_cmp expect actual &&
704
705 test-tool repository parse_commit_in_graph \
706 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
3c458630 707 git -C repo log --pretty="%ct" -1 one >expect &&
dade47c0
JT
708 test_cmp expect actual
709'
710
711test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
712 test-tool repository get_commit_tree_in_graph \
713 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
3c458630 714 git -C repo rev-parse two^{tree} >expect &&
dade47c0
JT
715 test_cmp expect actual &&
716
717 test-tool repository get_commit_tree_in_graph \
718 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
3c458630 719 git -C repo rev-parse one^{tree} >expect &&
dade47c0
JT
720 test_cmp expect actual
721'
722
16749b8d 723test_expect_success 'corrupt commit-graph write (broken parent)' '
23424ea7
TB
724 rm -rf repo &&
725 git init repo &&
726 (
727 cd repo &&
728 empty="$(git mktree </dev/null)" &&
729 cat >broken <<-EOF &&
730 tree $empty
48c10cc0 731 parent $ZERO_OID
23424ea7
TB
732 author whatever <whatever@example.com> 1234 -0000
733 committer whatever <whatever@example.com> 1234 -0000
734
735 broken commit
736 EOF
737 broken="$(git hash-object -w -t commit --literally broken)" &&
738 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
739 test_must_fail git commit-graph write --stdin-commits \
740 <good 2>test_err &&
741 test_i18ngrep "unable to parse commit" test_err
742 )
743'
744
806278de 745test_expect_success 'corrupt commit-graph write (missing tree)' '
23424ea7
TB
746 rm -rf repo &&
747 git init repo &&
748 (
749 cd repo &&
750 tree="$(git mktree </dev/null)" &&
751 cat >broken <<-EOF &&
48c10cc0 752 parent $ZERO_OID
23424ea7
TB
753 author whatever <whatever@example.com> 1234 -0000
754 committer whatever <whatever@example.com> 1234 -0000
755
756 broken commit
757 EOF
758 broken="$(git hash-object -w -t commit --literally broken)" &&
759 git commit-tree -p "$broken" -m "good" "$tree" >good &&
760 test_must_fail git commit-graph write --stdin-commits \
761 <good 2>test_err &&
228c78fb 762 test_i18ngrep "unable to parse commit" test_err
23424ea7
TB
763 )
764'
765
e8b63005
AK
766# We test the overflow-related code with the following repo history:
767#
768# 4:F - 5:N - 6:U
769# / \
770# 1:U - 2:N - 3:U M:N
771# \ /
772# 7:N - 8:F - 9:N
773#
774# Here the commits denoted by U have committer date of zero seconds
775# since Unix epoch, the commits denoted by N have committer date
776# starting from 1112354055 seconds since Unix epoch (default committer
777# date for the test suite), and the commits denoted by F have committer
778# date of (2 ^ 31 - 2) seconds since Unix epoch.
779#
780# The largest offset observed is 2 ^ 31, just large enough to overflow.
781#
782
783test_expect_success 'set up and verify repo with generation data overflow chunk' '
784 objdir=".git/objects" &&
785 UNIX_EPOCH_ZERO="@0 +0000" &&
786 FUTURE_DATE="@2147483646 +0000" &&
e8b63005
AK
787 cd "$TRASH_DIRECTORY" &&
788 mkdir repo &&
789 cd repo &&
790 git init &&
791 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
792 test_commit 2 &&
793 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
794 git commit-graph write --reachable &&
795 graph_read_expect 3 generation_data &&
796 test_commit --date "$FUTURE_DATE" 4 &&
797 test_commit 5 &&
798 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
799 git branch left &&
800 git reset --hard 3 &&
801 test_commit 7 &&
802 test_commit --date "$FUTURE_DATE" 8 &&
803 test_commit 9 &&
804 git branch right &&
805 git reset --hard 3 &&
806 test_merge M left right &&
807 git commit-graph write --reachable &&
808 graph_read_expect 10 "generation_data generation_data_overflow" &&
809 git commit-graph verify
810'
811
812graph_git_behavior 'generation data overflow chunk repo' repo left right
813
f237c8b6 814test_done