Merge branch 'ma/header-dup-cleanup'
[git/debian.git] / t / t5318-commit-graph.sh
blobedb728f77c3583cecde2d87555a4547464db7b96
1 #!/bin/sh
3 test_description='commit graph'
4 . ./test-lib.sh
6 GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
8 test_expect_success 'usage' '
9 test_expect_code 129 git commit-graph write blah 2>err &&
10 test_expect_code 129 git commit-graph write verify
13 test_expect_success 'usage shown without sub-command' '
14 test_expect_code 129 git commit-graph 2>err &&
15 ! grep error: err
18 test_expect_success 'usage shown with an error on unknown sub-command' '
19 cat >expect <<-\EOF &&
20 error: unrecognized subcommand: unknown
21 EOF
22 test_expect_code 129 git commit-graph unknown 2>stderr &&
23 grep error stderr >actual &&
24 test_cmp expect actual
27 test_expect_success 'setup full repo' '
28 mkdir full &&
29 cd "$TRASH_DIRECTORY/full" &&
30 git init &&
31 git config core.commitGraph true &&
32 objdir=".git/objects" &&
34 test_oid_cache <<-EOF
35 oid_version sha1:1
36 oid_version sha256:2
37 EOF
40 test_expect_success POSIXPERM 'tweak umask for modebit tests' '
41 umask 022
44 test_expect_success 'verify graph with no graph file' '
45 cd "$TRASH_DIRECTORY/full" &&
46 git commit-graph verify
49 test_expect_success 'write graph with no packs' '
50 cd "$TRASH_DIRECTORY/full" &&
51 git commit-graph write --object-dir $objdir &&
52 test_path_is_missing $objdir/info/commit-graph
55 test_expect_success 'exit with correct error on bad input to --stdin-packs' '
56 cd "$TRASH_DIRECTORY/full" &&
57 echo doesnotexist >in &&
58 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
59 test_i18ngrep "error adding pack" stderr
62 test_expect_success 'create commits and repack' '
63 cd "$TRASH_DIRECTORY/full" &&
64 for i in $(test_seq 3)
66 test_commit $i &&
67 git branch commits/$i || return 1
68 done &&
69 git repack
72 graph_git_two_modes() {
73 git -c core.commitGraph=true $1 >output &&
74 git -c core.commitGraph=false $1 >expect &&
75 test_cmp expect output
78 graph_git_behavior() {
79 MSG=$1
80 DIR=$2
81 BRANCH=$3
82 COMPARE=$4
83 test_expect_success "check normal git operations: $MSG" '
84 cd "$TRASH_DIRECTORY/$DIR" &&
85 graph_git_two_modes "log --oneline $BRANCH" &&
86 graph_git_two_modes "log --topo-order $BRANCH" &&
87 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
88 graph_git_two_modes "branch -vv" &&
89 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
93 graph_git_behavior 'no graph' full commits/3 commits/1
95 graph_read_expect() {
96 OPTIONAL=""
97 NUM_CHUNKS=3
98 if test ! -z "$2"
99 then
100 OPTIONAL=" $2"
101 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
103 cat >expect <<- EOF
104 header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
105 num_commits: $1
106 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
108 test-tool read-graph >output &&
109 test_cmp expect output
112 test_expect_success 'exit with correct error on bad input to --stdin-commits' '
113 cd "$TRASH_DIRECTORY/full" &&
114 # invalid, non-hex OID
115 echo HEAD >in &&
116 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
117 test_i18ngrep "unexpected non-hex object ID: HEAD" stderr &&
118 # non-existent OID
119 echo $ZERO_OID >in &&
120 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
121 test_i18ngrep "invalid object" stderr &&
122 # valid commit and tree OID
123 git rev-parse HEAD HEAD^{tree} >in &&
124 git commit-graph write --stdin-commits <in &&
125 graph_read_expect 3 generation_data
128 test_expect_success 'write graph' '
129 cd "$TRASH_DIRECTORY/full" &&
130 git commit-graph write &&
131 test_path_is_file $objdir/info/commit-graph &&
132 graph_read_expect "3" generation_data
135 test_expect_success POSIXPERM 'write graph has correct permissions' '
136 test_path_is_file $objdir/info/commit-graph &&
137 echo "-r--r--r--" >expect &&
138 test_modebits $objdir/info/commit-graph >actual &&
139 test_cmp expect actual
142 graph_git_behavior 'graph exists' full commits/3 commits/1
144 test_expect_success 'Add more commits' '
145 cd "$TRASH_DIRECTORY/full" &&
146 git reset --hard commits/1 &&
147 for i in $(test_seq 4 5)
149 test_commit $i &&
150 git branch commits/$i || return 1
151 done &&
152 git reset --hard commits/2 &&
153 for i in $(test_seq 6 7)
155 test_commit $i &&
156 git branch commits/$i || return 1
157 done &&
158 git reset --hard commits/2 &&
159 git merge commits/4 &&
160 git branch merge/1 &&
161 git reset --hard commits/4 &&
162 git merge commits/6 &&
163 git branch merge/2 &&
164 git reset --hard commits/3 &&
165 git merge commits/5 commits/7 &&
166 git branch merge/3 &&
167 git repack
170 test_expect_success 'commit-graph write progress off for redirected stderr' '
171 cd "$TRASH_DIRECTORY/full" &&
172 git commit-graph write 2>err &&
173 test_must_be_empty err
176 test_expect_success 'commit-graph write force progress on for stderr' '
177 cd "$TRASH_DIRECTORY/full" &&
178 GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err &&
179 test_file_not_empty err
182 test_expect_success 'commit-graph write with the --no-progress option' '
183 cd "$TRASH_DIRECTORY/full" &&
184 git commit-graph write --no-progress 2>err &&
185 test_must_be_empty err
188 test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
189 cd "$TRASH_DIRECTORY/full" &&
190 git rev-parse commits/5 >in &&
191 git commit-graph write --stdin-commits <in 2>err &&
192 test_must_be_empty err
195 test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
196 cd "$TRASH_DIRECTORY/full" &&
197 git rev-parse commits/5 >in &&
198 GIT_PROGRESS_DELAY=0 git commit-graph write --stdin-commits --progress <in 2>err &&
199 test_i18ngrep "Collecting commits from input" err
202 test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
203 cd "$TRASH_DIRECTORY/full" &&
204 git rev-parse commits/5 >in &&
205 git commit-graph write --stdin-commits --no-progress <in 2>err &&
206 test_must_be_empty err
209 test_expect_success 'commit-graph verify progress off for redirected stderr' '
210 cd "$TRASH_DIRECTORY/full" &&
211 git commit-graph verify 2>err &&
212 test_must_be_empty err
215 test_expect_success 'commit-graph verify force progress on for stderr' '
216 cd "$TRASH_DIRECTORY/full" &&
217 GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
218 test_file_not_empty err
221 test_expect_success 'commit-graph verify with the --no-progress option' '
222 cd "$TRASH_DIRECTORY/full" &&
223 git commit-graph verify --no-progress 2>err &&
224 test_must_be_empty err
227 # Current graph structure:
229 # __M3___
230 # / | \
231 # 3 M1 5 M2 7
232 # |/ \|/ \|
233 # 2 4 6
234 # |___/____/
237 test_expect_success 'write graph with merges' '
238 cd "$TRASH_DIRECTORY/full" &&
239 git commit-graph write &&
240 test_path_is_file $objdir/info/commit-graph &&
241 graph_read_expect "10" "generation_data extra_edges"
244 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
245 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
246 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
248 test_expect_success 'Add one more commit' '
249 cd "$TRASH_DIRECTORY/full" &&
250 test_commit 8 &&
251 git branch commits/8 &&
252 ls $objdir/pack | grep idx >existing-idx &&
253 git repack &&
254 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
257 # Current graph structure:
261 # __M3___
262 # / | \
263 # 3 M1 5 M2 7
264 # |/ \|/ \|
265 # 2 4 6
266 # |___/____/
269 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
270 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
272 test_expect_success 'write graph with new commit' '
273 cd "$TRASH_DIRECTORY/full" &&
274 git commit-graph write &&
275 test_path_is_file $objdir/info/commit-graph &&
276 graph_read_expect "11" "generation_data extra_edges"
279 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
280 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
282 test_expect_success 'write graph with nothing new' '
283 cd "$TRASH_DIRECTORY/full" &&
284 git commit-graph write &&
285 test_path_is_file $objdir/info/commit-graph &&
286 graph_read_expect "11" "generation_data extra_edges"
289 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
290 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
292 test_expect_success 'build graph from latest pack with closure' '
293 cd "$TRASH_DIRECTORY/full" &&
294 cat new-idx | git commit-graph write --stdin-packs &&
295 test_path_is_file $objdir/info/commit-graph &&
296 graph_read_expect "9" "generation_data extra_edges"
299 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
300 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
302 test_expect_success 'build graph from commits with closure' '
303 cd "$TRASH_DIRECTORY/full" &&
304 git tag -a -m "merge" tag/merge merge/2 &&
305 git rev-parse tag/merge >commits-in &&
306 git rev-parse merge/1 >>commits-in &&
307 cat commits-in | git commit-graph write --stdin-commits &&
308 test_path_is_file $objdir/info/commit-graph &&
309 graph_read_expect "6" "generation_data"
312 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
313 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
315 test_expect_success 'build graph from commits with append' '
316 cd "$TRASH_DIRECTORY/full" &&
317 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
318 test_path_is_file $objdir/info/commit-graph &&
319 graph_read_expect "10" "generation_data extra_edges"
322 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
323 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
325 test_expect_success 'build graph using --reachable' '
326 cd "$TRASH_DIRECTORY/full" &&
327 git commit-graph write --reachable &&
328 test_path_is_file $objdir/info/commit-graph &&
329 graph_read_expect "11" "generation_data extra_edges"
332 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
333 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
335 test_expect_success 'setup bare repo' '
336 cd "$TRASH_DIRECTORY" &&
337 git clone --bare --no-local full bare &&
338 cd bare &&
339 git config core.commitGraph true &&
340 baredir="./objects"
343 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
344 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
346 test_expect_success 'write graph in bare repo' '
347 cd "$TRASH_DIRECTORY/bare" &&
348 git commit-graph write &&
349 test_path_is_file $baredir/info/commit-graph &&
350 graph_read_expect "11" "generation_data extra_edges"
353 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
354 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
356 test_expect_success 'perform fast-forward merge in full repo' '
357 cd "$TRASH_DIRECTORY/full" &&
358 git checkout -b merge-5-to-8 commits/5 &&
359 git merge commits/8 &&
360 git show-ref -s merge-5-to-8 >output &&
361 git show-ref -s commits/8 >expect &&
362 test_cmp expect output
365 test_expect_success 'check that gc computes commit-graph' '
366 cd "$TRASH_DIRECTORY/full" &&
367 git commit --allow-empty -m "blank" &&
368 git commit-graph write --reachable &&
369 cp $objdir/info/commit-graph commit-graph-before-gc &&
370 git reset --hard HEAD~1 &&
371 git config gc.writeCommitGraph true &&
372 git gc &&
373 cp $objdir/info/commit-graph commit-graph-after-gc &&
374 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
375 git commit-graph write --reachable &&
376 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
379 test_expect_success 'replace-objects invalidates commit-graph' '
380 cd "$TRASH_DIRECTORY" &&
381 test_when_finished rm -rf replace &&
382 git clone full replace &&
384 cd replace &&
385 git commit-graph write --reachable &&
386 test_path_is_file .git/objects/info/commit-graph &&
387 git replace HEAD~1 HEAD~2 &&
388 graph_git_two_modes "commit-graph verify" &&
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_file .git/objects/info/commit-graph
402 test_expect_success 'commit grafts invalidate commit-graph' '
403 cd "$TRASH_DIRECTORY" &&
404 test_when_finished rm -rf graft &&
405 git clone full graft &&
407 cd graft &&
408 git commit-graph write --reachable &&
409 test_path_is_file .git/objects/info/commit-graph &&
410 H1=$(git rev-parse --verify HEAD~1) &&
411 H3=$(git rev-parse --verify HEAD~3) &&
412 echo "$H1 $H3" >.git/info/grafts &&
413 git -c core.commitGraph=false log >expect &&
414 git -c core.commitGraph=true log >actual &&
415 test_cmp expect actual &&
416 git commit-graph write --reachable &&
417 git -c core.commitGraph=false --no-replace-objects log >expect &&
418 git -c core.commitGraph=true --no-replace-objects log >actual &&
419 test_cmp expect actual &&
420 rm -rf .git/objects/info/commit-graph &&
421 git commit-graph write --reachable &&
422 test_path_is_missing .git/objects/info/commit-graph
426 test_expect_success 'replace-objects invalidates commit-graph' '
427 cd "$TRASH_DIRECTORY" &&
428 test_when_finished rm -rf shallow &&
429 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
431 cd shallow &&
432 git commit-graph write --reachable &&
433 test_path_is_missing .git/objects/info/commit-graph &&
434 git fetch origin --unshallow &&
435 git commit-graph write --reachable &&
436 test_path_is_file .git/objects/info/commit-graph
440 test_expect_success 'warn on improper hash version' '
441 git init --object-format=sha1 sha1 &&
443 cd sha1 &&
444 test_commit 1 &&
445 git commit-graph write --reachable &&
446 mv .git/objects/info/commit-graph ../cg-sha1
447 ) &&
448 git init --object-format=sha256 sha256 &&
450 cd sha256 &&
451 test_commit 1 &&
452 git commit-graph write --reachable &&
453 mv .git/objects/info/commit-graph ../cg-sha256
454 ) &&
456 cd sha1 &&
457 mv ../cg-sha256 .git/objects/info/commit-graph &&
458 git log -1 2>err &&
459 test_i18ngrep "commit-graph hash version 2 does not match version 1" err
460 ) &&
462 cd sha256 &&
463 mv ../cg-sha1 .git/objects/info/commit-graph &&
464 git log -1 2>err &&
465 test_i18ngrep "commit-graph hash version 1 does not match version 2" err
469 test_expect_success 'lower layers have overflow chunk' '
470 cd "$TRASH_DIRECTORY/full" &&
471 UNIX_EPOCH_ZERO="@0 +0000" &&
472 FUTURE_DATE="@2147483646 +0000" &&
473 rm -f .git/objects/info/commit-graph &&
474 test_commit --date "$FUTURE_DATE" future-1 &&
475 test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
476 git commit-graph write --reachable &&
477 test_commit --date "$FUTURE_DATE" future-2 &&
478 test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
479 git commit-graph write --reachable --split=no-merge &&
480 test_commit extra &&
481 git commit-graph write --reachable --split=no-merge &&
482 git commit-graph write --reachable &&
483 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
484 mv .git/objects/info/commit-graph commit-graph-upgraded &&
485 git commit-graph write --reachable &&
486 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
487 test_cmp .git/objects/info/commit-graph commit-graph-upgraded
490 # the verify tests below expect the commit-graph to contain
491 # exactly the commits reachable from the commits/8 branch.
492 # If the file changes the set of commits in the list, then the
493 # offsets into the binary file will result in different edits
494 # and the tests will likely break.
496 test_expect_success 'git commit-graph verify' '
497 cd "$TRASH_DIRECTORY/full" &&
498 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
499 git commit-graph verify >output &&
500 graph_read_expect 9 extra_edges
503 NUM_COMMITS=9
504 NUM_OCTOPUS_EDGES=2
505 HASH_LEN="$(test_oid rawsz)"
506 GRAPH_BYTE_VERSION=4
507 GRAPH_BYTE_HASH=5
508 GRAPH_BYTE_CHUNK_COUNT=6
509 GRAPH_CHUNK_LOOKUP_OFFSET=8
510 GRAPH_CHUNK_LOOKUP_WIDTH=12
511 GRAPH_CHUNK_LOOKUP_ROWS=5
512 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
513 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
514 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
515 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
516 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
517 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
518 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
519 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
520 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
521 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
522 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
523 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
524 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
525 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
526 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
527 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
528 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
529 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
530 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
531 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
532 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
533 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
534 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
535 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
537 corrupt_graph_setup() {
538 cd "$TRASH_DIRECTORY/full" &&
539 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
540 cp $objdir/info/commit-graph commit-graph-backup &&
541 chmod u+w $objdir/info/commit-graph
544 corrupt_graph_verify() {
545 grepstr=$1
546 test_must_fail git commit-graph verify 2>test_err &&
547 grep -v "^+" test_err >err &&
548 test_i18ngrep "$grepstr" err &&
549 if test "$2" != "no-copy"
550 then
551 cp $objdir/info/commit-graph commit-graph-pre-write-test
552 fi &&
553 git status --short &&
554 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
555 chmod u+w $objdir/info/commit-graph &&
556 git commit-graph verify
559 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
560 # Manipulates the commit-graph file at the position
561 # by inserting the data, optionally zeroing the file
562 # starting at <zero_pos>, then runs 'git commit-graph verify'
563 # and places the output in the file 'err'. Test 'err' for
564 # the given string.
565 corrupt_graph_and_verify() {
566 pos=$1
567 data="${2:-\0}"
568 grepstr=$3
569 corrupt_graph_setup &&
570 orig_size=$(wc -c < $objdir/info/commit-graph) &&
571 zero_pos=${4:-${orig_size}} &&
572 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
573 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
574 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
575 corrupt_graph_verify "$grepstr"
579 test_expect_success POSIXPERM,SANITY 'detect permission problem' '
580 corrupt_graph_setup &&
581 chmod 000 $objdir/info/commit-graph &&
582 corrupt_graph_verify "Could not open" "no-copy"
585 test_expect_success 'detect too small' '
586 corrupt_graph_setup &&
587 echo "a small graph" >$objdir/info/commit-graph &&
588 corrupt_graph_verify "too small"
591 test_expect_success 'detect bad signature' '
592 corrupt_graph_and_verify 0 "\0" \
593 "graph signature"
596 test_expect_success 'detect bad version' '
597 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
598 "graph version"
601 test_expect_success 'detect bad hash version' '
602 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
603 "hash version"
606 test_expect_success 'detect low chunk count' '
607 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
608 "final chunk has non-zero id"
611 test_expect_success 'detect missing OID fanout chunk' '
612 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
613 "missing the OID Fanout chunk"
616 test_expect_success 'detect missing OID lookup chunk' '
617 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
618 "missing the OID Lookup chunk"
621 test_expect_success 'detect missing commit data chunk' '
622 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
623 "missing the Commit Data chunk"
626 test_expect_success 'detect incorrect fanout' '
627 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
628 "fanout value"
631 test_expect_success 'detect incorrect fanout final value' '
632 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
633 "fanout value"
636 test_expect_success 'detect incorrect OID order' '
637 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
638 "incorrect OID order"
641 test_expect_success 'detect OID not in object database' '
642 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
643 "from object database"
646 test_expect_success 'detect incorrect tree OID' '
647 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
648 "root tree OID for commit"
651 test_expect_success 'detect incorrect parent int-id' '
652 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
653 "invalid parent"
656 test_expect_success 'detect extra parent int-id' '
657 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
658 "is too long"
661 test_expect_success 'detect wrong parent' '
662 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
663 "commit-graph parent for"
666 test_expect_success 'detect incorrect generation number' '
667 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
668 "generation for commit"
671 test_expect_success 'detect incorrect generation number' '
672 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
673 "non-zero generation number"
676 test_expect_success 'detect incorrect commit date' '
677 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
678 "commit date"
681 test_expect_success 'detect incorrect parent for octopus merge' '
682 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
683 "invalid parent"
686 test_expect_success 'detect invalid checksum hash' '
687 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
688 "incorrect checksum"
691 test_expect_success 'detect incorrect chunk count' '
692 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
693 "commit-graph file is too small to hold [0-9]* chunks" \
694 $GRAPH_CHUNK_LOOKUP_OFFSET
697 test_expect_success 'git fsck (checks commit-graph when config set to true)' '
698 cd "$TRASH_DIRECTORY/full" &&
699 git fsck &&
700 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
701 "incorrect checksum" &&
702 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
703 test_must_fail git -c core.commitGraph=true fsck
706 test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
707 cd "$TRASH_DIRECTORY/full" &&
708 git fsck &&
709 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
710 "incorrect checksum" &&
711 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
712 git -c core.commitGraph=false fsck
715 test_expect_success 'git fsck (checks commit-graph when config unset)' '
716 cd "$TRASH_DIRECTORY/full" &&
717 test_when_finished "git config core.commitGraph true" &&
719 git fsck &&
720 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
721 "incorrect checksum" &&
722 test_unconfig core.commitGraph &&
723 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
724 test_must_fail git fsck
727 test_expect_success 'setup non-the_repository tests' '
728 rm -rf repo &&
729 git init repo &&
730 test_commit -C repo one &&
731 test_commit -C repo two &&
732 git -C repo config core.commitGraph true &&
733 git -C repo rev-parse two | \
734 git -C repo commit-graph write --stdin-commits
737 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
738 test-tool repository parse_commit_in_graph \
739 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
741 git -C repo log --pretty=format:"%ct " -1 &&
742 git -C repo rev-parse one
743 } >expect &&
744 test_cmp expect actual &&
746 test-tool repository parse_commit_in_graph \
747 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
748 git -C repo log --pretty="%ct" -1 one >expect &&
749 test_cmp expect actual
752 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
753 test-tool repository get_commit_tree_in_graph \
754 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
755 git -C repo rev-parse two^{tree} >expect &&
756 test_cmp expect actual &&
758 test-tool repository get_commit_tree_in_graph \
759 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
760 git -C repo rev-parse one^{tree} >expect &&
761 test_cmp expect actual
764 test_expect_success 'corrupt commit-graph write (broken parent)' '
765 rm -rf repo &&
766 git init repo &&
768 cd repo &&
769 empty="$(git mktree </dev/null)" &&
770 cat >broken <<-EOF &&
771 tree $empty
772 parent $ZERO_OID
773 author whatever <whatever@example.com> 1234 -0000
774 committer whatever <whatever@example.com> 1234 -0000
776 broken commit
778 broken="$(git hash-object -w -t commit --literally broken)" &&
779 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
780 test_must_fail git commit-graph write --stdin-commits \
781 <good 2>test_err &&
782 test_i18ngrep "unable to parse commit" test_err
786 test_expect_success 'corrupt commit-graph write (missing tree)' '
787 rm -rf repo &&
788 git init repo &&
790 cd repo &&
791 tree="$(git mktree </dev/null)" &&
792 cat >broken <<-EOF &&
793 parent $ZERO_OID
794 author whatever <whatever@example.com> 1234 -0000
795 committer whatever <whatever@example.com> 1234 -0000
797 broken commit
799 broken="$(git hash-object -w -t commit --literally broken)" &&
800 git commit-tree -p "$broken" -m "good" "$tree" >good &&
801 test_must_fail git commit-graph write --stdin-commits \
802 <good 2>test_err &&
803 test_i18ngrep "unable to parse commit" test_err
807 # We test the overflow-related code with the following repo history:
809 # 4:F - 5:N - 6:U
810 # / \
811 # 1:U - 2:N - 3:U M:N
812 # \ /
813 # 7:N - 8:F - 9:N
815 # Here the commits denoted by U have committer date of zero seconds
816 # since Unix epoch, the commits denoted by N have committer date
817 # starting from 1112354055 seconds since Unix epoch (default committer
818 # date for the test suite), and the commits denoted by F have committer
819 # date of (2 ^ 31 - 2) seconds since Unix epoch.
821 # The largest offset observed is 2 ^ 31, just large enough to overflow.
824 test_expect_success 'set up and verify repo with generation data overflow chunk' '
825 objdir=".git/objects" &&
826 UNIX_EPOCH_ZERO="@0 +0000" &&
827 FUTURE_DATE="@2147483646 +0000" &&
828 test_oid_cache <<-EOF &&
829 oid_version sha1:1
830 oid_version sha256:2
832 cd "$TRASH_DIRECTORY" &&
833 mkdir repo &&
834 cd repo &&
835 git init &&
836 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
837 test_commit 2 &&
838 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
839 git commit-graph write --reachable &&
840 graph_read_expect 3 generation_data &&
841 test_commit --date "$FUTURE_DATE" 4 &&
842 test_commit 5 &&
843 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
844 git branch left &&
845 git reset --hard 3 &&
846 test_commit 7 &&
847 test_commit --date "$FUTURE_DATE" 8 &&
848 test_commit 9 &&
849 git branch right &&
850 git reset --hard 3 &&
851 test_merge M left right &&
852 git commit-graph write --reachable &&
853 graph_read_expect 10 "generation_data generation_data_overflow" &&
854 git commit-graph verify
857 graph_git_behavior 'generation data overflow chunk repo' repo left right
859 test_done