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