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