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