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