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