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