rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t5318-commit-graph.sh
blobfbf0d64578cde0544251020cf0df5ea6b90c4a2e
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"
35 test_expect_success POSIXPERM 'tweak umask for modebit tests' '
36 umask 022
39 test_expect_success 'verify graph with no graph file' '
40 cd "$TRASH_DIRECTORY/full" &&
41 git commit-graph verify
44 test_expect_success 'write graph with no packs' '
45 cd "$TRASH_DIRECTORY/full" &&
46 git commit-graph write --object-dir $objdir &&
47 test_path_is_missing $objdir/info/commit-graph
50 test_expect_success 'exit with correct error on bad input to --stdin-packs' '
51 cd "$TRASH_DIRECTORY/full" &&
52 echo doesnotexist >in &&
53 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
54 test_i18ngrep "error adding pack" stderr
57 test_expect_success 'create commits and repack' '
58 cd "$TRASH_DIRECTORY/full" &&
59 for i in $(test_seq 3)
61 test_commit $i &&
62 git branch commits/$i || return 1
63 done &&
64 git repack
67 . "$TEST_DIRECTORY"/lib-commit-graph.sh
69 graph_git_behavior 'no graph' full commits/3 commits/1
71 test_expect_success 'exit with correct error on bad input to --stdin-commits' '
72 cd "$TRASH_DIRECTORY/full" &&
73 # invalid, non-hex OID
74 echo HEAD >in &&
75 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
76 test_i18ngrep "unexpected non-hex object ID: HEAD" stderr &&
77 # non-existent OID
78 echo $ZERO_OID >in &&
79 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
80 test_i18ngrep "invalid object" stderr &&
81 # valid commit and tree OID
82 git rev-parse HEAD HEAD^{tree} >in &&
83 git commit-graph write --stdin-commits <in &&
84 graph_read_expect 3 generation_data
87 test_expect_success 'write graph' '
88 cd "$TRASH_DIRECTORY/full" &&
89 git commit-graph write &&
90 test_path_is_file $objdir/info/commit-graph &&
91 graph_read_expect "3" generation_data
94 test_expect_success POSIXPERM 'write graph has correct permissions' '
95 test_path_is_file $objdir/info/commit-graph &&
96 echo "-r--r--r--" >expect &&
97 test_modebits $objdir/info/commit-graph >actual &&
98 test_cmp expect actual
101 graph_git_behavior 'graph exists' full commits/3 commits/1
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)
108 test_commit $i &&
109 git branch commits/$i || return 1
110 done &&
111 git reset --hard commits/2 &&
112 for i in $(test_seq 6 7)
114 test_commit $i &&
115 git branch commits/$i || return 1
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
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_must_be_empty err
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
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_must_be_empty err
147 test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
148 cd "$TRASH_DIRECTORY/full" &&
149 git rev-parse commits/5 >in &&
150 git commit-graph write --stdin-commits <in 2>err &&
151 test_must_be_empty err
154 test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
155 cd "$TRASH_DIRECTORY/full" &&
156 git rev-parse commits/5 >in &&
157 GIT_PROGRESS_DELAY=0 git commit-graph write --stdin-commits --progress <in 2>err &&
158 test_i18ngrep "Collecting commits from input" err
161 test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
162 cd "$TRASH_DIRECTORY/full" &&
163 git rev-parse commits/5 >in &&
164 git commit-graph write --stdin-commits --no-progress <in 2>err &&
165 test_must_be_empty err
168 test_expect_success 'commit-graph verify progress off for redirected stderr' '
169 cd "$TRASH_DIRECTORY/full" &&
170 git commit-graph verify 2>err &&
171 test_must_be_empty err
174 test_expect_success 'commit-graph verify force progress on for stderr' '
175 cd "$TRASH_DIRECTORY/full" &&
176 GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
177 test_file_not_empty err
180 test_expect_success 'commit-graph verify with the --no-progress option' '
181 cd "$TRASH_DIRECTORY/full" &&
182 git commit-graph verify --no-progress 2>err &&
183 test_must_be_empty err
186 # Current graph structure:
188 # __M3___
189 # / | \
190 # 3 M1 5 M2 7
191 # |/ \|/ \|
192 # 2 4 6
193 # |___/____/
196 test_expect_success 'write graph with merges' '
197 cd "$TRASH_DIRECTORY/full" &&
198 git commit-graph write &&
199 test_path_is_file $objdir/info/commit-graph &&
200 graph_read_expect "10" "generation_data extra_edges"
203 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
204 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
205 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
207 test_expect_success 'Add one more commit' '
208 cd "$TRASH_DIRECTORY/full" &&
209 test_commit 8 &&
210 git branch commits/8 &&
211 ls $objdir/pack | grep idx >existing-idx &&
212 git repack &&
213 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
216 # Current graph structure:
220 # __M3___
221 # / | \
222 # 3 M1 5 M2 7
223 # |/ \|/ \|
224 # 2 4 6
225 # |___/____/
228 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
229 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
231 test_expect_success 'write graph with new commit' '
232 cd "$TRASH_DIRECTORY/full" &&
233 git commit-graph write &&
234 test_path_is_file $objdir/info/commit-graph &&
235 graph_read_expect "11" "generation_data extra_edges"
238 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
239 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
241 test_expect_success 'write graph with nothing new' '
242 cd "$TRASH_DIRECTORY/full" &&
243 git commit-graph write &&
244 test_path_is_file $objdir/info/commit-graph &&
245 graph_read_expect "11" "generation_data extra_edges"
248 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
249 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
251 test_expect_success 'build graph from latest pack with closure' '
252 cd "$TRASH_DIRECTORY/full" &&
253 cat new-idx | git commit-graph write --stdin-packs &&
254 test_path_is_file $objdir/info/commit-graph &&
255 graph_read_expect "9" "generation_data extra_edges"
258 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
259 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
261 test_expect_success 'build graph from commits with closure' '
262 cd "$TRASH_DIRECTORY/full" &&
263 git tag -a -m "merge" tag/merge merge/2 &&
264 git rev-parse tag/merge >commits-in &&
265 git rev-parse merge/1 >>commits-in &&
266 cat commits-in | git commit-graph write --stdin-commits &&
267 test_path_is_file $objdir/info/commit-graph &&
268 graph_read_expect "6" "generation_data"
271 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
272 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
274 test_expect_success 'build graph from commits with append' '
275 cd "$TRASH_DIRECTORY/full" &&
276 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
277 test_path_is_file $objdir/info/commit-graph &&
278 graph_read_expect "10" "generation_data extra_edges"
281 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
282 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
284 test_expect_success 'build graph using --reachable' '
285 cd "$TRASH_DIRECTORY/full" &&
286 git commit-graph write --reachable &&
287 test_path_is_file $objdir/info/commit-graph &&
288 graph_read_expect "11" "generation_data extra_edges"
291 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
292 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
294 test_expect_success 'setup bare repo' '
295 cd "$TRASH_DIRECTORY" &&
296 git clone --bare --no-local full bare &&
297 cd bare &&
298 git config core.commitGraph true &&
299 baredir="./objects"
302 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
303 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
305 test_expect_success 'write graph in bare repo' '
306 cd "$TRASH_DIRECTORY/bare" &&
307 git commit-graph write &&
308 test_path_is_file $baredir/info/commit-graph &&
309 graph_read_expect "11" "generation_data extra_edges"
312 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
313 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
315 test_expect_success 'perform fast-forward merge in full repo' '
316 cd "$TRASH_DIRECTORY/full" &&
317 git checkout -b merge-5-to-8 commits/5 &&
318 git merge commits/8 &&
319 git show-ref -s merge-5-to-8 >output &&
320 git show-ref -s commits/8 >expect &&
321 test_cmp expect output
324 test_expect_success 'check that gc computes commit-graph' '
325 cd "$TRASH_DIRECTORY/full" &&
326 git commit --allow-empty -m "blank" &&
327 git commit-graph write --reachable &&
328 cp $objdir/info/commit-graph commit-graph-before-gc &&
329 git reset --hard HEAD~1 &&
330 git config gc.writeCommitGraph true &&
331 git gc &&
332 cp $objdir/info/commit-graph commit-graph-after-gc &&
333 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
334 git commit-graph write --reachable &&
335 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
338 test_expect_success 'replace-objects invalidates commit-graph' '
339 cd "$TRASH_DIRECTORY" &&
340 test_when_finished rm -rf replace &&
341 git clone full replace &&
343 cd replace &&
344 git commit-graph write --reachable &&
345 test_path_is_file .git/objects/info/commit-graph &&
346 git replace HEAD~1 HEAD~2 &&
347 graph_git_two_modes "commit-graph verify" &&
348 git -c core.commitGraph=false log >expect &&
349 git -c core.commitGraph=true log >actual &&
350 test_cmp expect actual &&
351 git commit-graph write --reachable &&
352 git -c core.commitGraph=false --no-replace-objects log >expect &&
353 git -c core.commitGraph=true --no-replace-objects log >actual &&
354 test_cmp expect actual &&
355 rm -rf .git/objects/info/commit-graph &&
356 git commit-graph write --reachable &&
357 test_path_is_file .git/objects/info/commit-graph
361 test_expect_success 'commit grafts invalidate commit-graph' '
362 cd "$TRASH_DIRECTORY" &&
363 test_when_finished rm -rf graft &&
364 git clone full graft &&
366 cd graft &&
367 git commit-graph write --reachable &&
368 test_path_is_file .git/objects/info/commit-graph &&
369 H1=$(git rev-parse --verify HEAD~1) &&
370 H3=$(git rev-parse --verify HEAD~3) &&
371 echo "$H1 $H3" >.git/info/grafts &&
372 git -c core.commitGraph=false log >expect &&
373 git -c core.commitGraph=true log >actual &&
374 test_cmp expect actual &&
375 git commit-graph write --reachable &&
376 git -c core.commitGraph=false --no-replace-objects log >expect &&
377 git -c core.commitGraph=true --no-replace-objects log >actual &&
378 test_cmp expect actual &&
379 rm -rf .git/objects/info/commit-graph &&
380 git commit-graph write --reachable &&
381 test_path_is_missing .git/objects/info/commit-graph
385 test_expect_success 'replace-objects invalidates commit-graph' '
386 cd "$TRASH_DIRECTORY" &&
387 test_when_finished rm -rf shallow &&
388 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
390 cd shallow &&
391 git commit-graph write --reachable &&
392 test_path_is_missing .git/objects/info/commit-graph &&
393 git fetch origin --unshallow &&
394 git commit-graph write --reachable &&
395 test_path_is_file .git/objects/info/commit-graph
399 test_expect_success 'warn on improper hash version' '
400 git init --object-format=sha1 sha1 &&
402 cd sha1 &&
403 test_commit 1 &&
404 git commit-graph write --reachable &&
405 mv .git/objects/info/commit-graph ../cg-sha1
406 ) &&
407 git init --object-format=sha256 sha256 &&
409 cd sha256 &&
410 test_commit 1 &&
411 git commit-graph write --reachable &&
412 mv .git/objects/info/commit-graph ../cg-sha256
413 ) &&
415 cd sha1 &&
416 mv ../cg-sha256 .git/objects/info/commit-graph &&
417 git log -1 2>err &&
418 test_i18ngrep "commit-graph hash version 2 does not match version 1" err
419 ) &&
421 cd sha256 &&
422 mv ../cg-sha1 .git/objects/info/commit-graph &&
423 git log -1 2>err &&
424 test_i18ngrep "commit-graph hash version 1 does not match version 2" err
428 test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow chunk' '
429 cd "$TRASH_DIRECTORY/full" &&
430 UNIX_EPOCH_ZERO="@0 +0000" &&
431 FUTURE_DATE="@4147483646 +0000" &&
432 rm -f .git/objects/info/commit-graph &&
433 test_commit --date "$FUTURE_DATE" future-1 &&
434 test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
435 git commit-graph write --reachable &&
436 test_commit --date "$FUTURE_DATE" future-2 &&
437 test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
438 git commit-graph write --reachable --split=no-merge &&
439 test_commit extra &&
440 git commit-graph write --reachable --split=no-merge &&
441 git commit-graph write --reachable &&
442 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
443 mv .git/objects/info/commit-graph commit-graph-upgraded &&
444 git commit-graph write --reachable &&
445 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
446 test_cmp .git/objects/info/commit-graph commit-graph-upgraded
449 # the verify tests below expect the commit-graph to contain
450 # exactly the commits reachable from the commits/8 branch.
451 # If the file changes the set of commits in the list, then the
452 # offsets into the binary file will result in different edits
453 # and the tests will likely break.
455 test_expect_success 'git commit-graph verify' '
456 cd "$TRASH_DIRECTORY/full" &&
457 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
458 git commit-graph verify >output &&
459 graph_read_expect 9 extra_edges 1
462 NUM_COMMITS=9
463 NUM_OCTOPUS_EDGES=2
464 HASH_LEN="$(test_oid rawsz)"
465 GRAPH_BYTE_VERSION=4
466 GRAPH_BYTE_HASH=5
467 GRAPH_BYTE_CHUNK_COUNT=6
468 GRAPH_CHUNK_LOOKUP_OFFSET=8
469 GRAPH_CHUNK_LOOKUP_WIDTH=12
470 GRAPH_CHUNK_LOOKUP_ROWS=5
471 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
472 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
473 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
474 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
475 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
476 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
477 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
478 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
479 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
480 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
481 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
482 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
483 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
484 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
485 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
486 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
487 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
488 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
489 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
490 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
491 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
492 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
493 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
494 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
496 corrupt_graph_setup() {
497 cd "$TRASH_DIRECTORY/full" &&
498 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
499 cp $objdir/info/commit-graph commit-graph-backup &&
500 chmod u+w $objdir/info/commit-graph
503 corrupt_graph_verify() {
504 grepstr=$1
505 test_must_fail git commit-graph verify 2>test_err &&
506 grep -v "^+" test_err >err &&
507 test_i18ngrep "$grepstr" err &&
508 if test "$2" != "no-copy"
509 then
510 cp $objdir/info/commit-graph commit-graph-pre-write-test
511 fi &&
512 git status --short &&
513 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
514 chmod u+w $objdir/info/commit-graph &&
515 git commit-graph verify
518 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
519 # Manipulates the commit-graph file at the position
520 # by inserting the data, optionally zeroing the file
521 # starting at <zero_pos>, then runs 'git commit-graph verify'
522 # and places the output in the file 'err'. Test 'err' for
523 # the given string.
524 corrupt_graph_and_verify() {
525 pos=$1
526 data="${2:-\0}"
527 grepstr=$3
528 corrupt_graph_setup &&
529 orig_size=$(wc -c < $objdir/info/commit-graph) &&
530 zero_pos=${4:-${orig_size}} &&
531 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
532 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
533 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
534 corrupt_graph_verify "$grepstr"
538 test_expect_success POSIXPERM,SANITY 'detect permission problem' '
539 corrupt_graph_setup &&
540 chmod 000 $objdir/info/commit-graph &&
541 corrupt_graph_verify "Could not open" "no-copy"
544 test_expect_success 'detect too small' '
545 corrupt_graph_setup &&
546 echo "a small graph" >$objdir/info/commit-graph &&
547 corrupt_graph_verify "too small"
550 test_expect_success 'detect bad signature' '
551 corrupt_graph_and_verify 0 "\0" \
552 "graph signature"
555 test_expect_success 'detect bad version' '
556 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
557 "graph version"
560 test_expect_success 'detect bad hash version' '
561 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
562 "hash version"
565 test_expect_success 'detect low chunk count' '
566 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
567 "final chunk has non-zero id"
570 test_expect_success 'detect missing OID fanout chunk' '
571 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
572 "missing the OID Fanout chunk"
575 test_expect_success 'detect missing OID lookup chunk' '
576 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
577 "missing the OID Lookup chunk"
580 test_expect_success 'detect missing commit data chunk' '
581 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
582 "missing the Commit Data chunk"
585 test_expect_success 'detect incorrect fanout' '
586 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
587 "fanout value"
590 test_expect_success 'detect incorrect fanout final value' '
591 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
592 "fanout value"
595 test_expect_success 'detect incorrect OID order' '
596 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
597 "incorrect OID order"
600 test_expect_success 'detect OID not in object database' '
601 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
602 "from object database"
605 test_expect_success 'detect incorrect tree OID' '
606 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
607 "root tree OID for commit"
610 test_expect_success 'detect incorrect parent int-id' '
611 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
612 "invalid parent"
615 test_expect_success 'detect extra parent int-id' '
616 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
617 "is too long"
620 test_expect_success 'detect wrong parent' '
621 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
622 "commit-graph parent for"
625 test_expect_success 'detect incorrect generation number' '
626 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
627 "generation for commit"
630 test_expect_success 'detect incorrect generation number' '
631 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
632 "non-zero generation number"
635 test_expect_success 'detect incorrect commit date' '
636 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
637 "commit date"
640 test_expect_success 'detect incorrect parent for octopus merge' '
641 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
642 "invalid parent"
645 test_expect_success 'detect invalid checksum hash' '
646 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
647 "incorrect checksum"
650 test_expect_success 'detect incorrect chunk count' '
651 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
652 "commit-graph file is too small to hold [0-9]* chunks" \
653 $GRAPH_CHUNK_LOOKUP_OFFSET
656 test_expect_success 'git fsck (checks commit-graph when config set to true)' '
657 cd "$TRASH_DIRECTORY/full" &&
658 git fsck &&
659 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
660 "incorrect checksum" &&
661 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
662 test_must_fail git -c core.commitGraph=true fsck
665 test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
666 cd "$TRASH_DIRECTORY/full" &&
667 git fsck &&
668 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
669 "incorrect checksum" &&
670 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
671 git -c core.commitGraph=false fsck
674 test_expect_success 'git fsck (checks commit-graph when config unset)' '
675 cd "$TRASH_DIRECTORY/full" &&
676 test_when_finished "git config core.commitGraph true" &&
678 git fsck &&
679 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
680 "incorrect checksum" &&
681 test_unconfig core.commitGraph &&
682 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
683 test_must_fail git fsck
686 test_expect_success 'setup non-the_repository tests' '
687 rm -rf repo &&
688 git init repo &&
689 test_commit -C repo one &&
690 test_commit -C repo two &&
691 git -C repo config core.commitGraph true &&
692 git -C repo rev-parse two | \
693 git -C repo commit-graph write --stdin-commits
696 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
697 test-tool repository parse_commit_in_graph \
698 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
700 git -C repo log --pretty=format:"%ct " -1 &&
701 git -C repo rev-parse one
702 } >expect &&
703 test_cmp expect actual &&
705 test-tool repository parse_commit_in_graph \
706 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
707 git -C repo log --pretty="%ct" -1 one >expect &&
708 test_cmp expect actual
711 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
712 test-tool repository get_commit_tree_in_graph \
713 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
714 git -C repo rev-parse two^{tree} >expect &&
715 test_cmp expect actual &&
717 test-tool repository get_commit_tree_in_graph \
718 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
719 git -C repo rev-parse one^{tree} >expect &&
720 test_cmp expect actual
723 test_expect_success 'corrupt commit-graph write (broken parent)' '
724 rm -rf repo &&
725 git init repo &&
727 cd repo &&
728 empty="$(git mktree </dev/null)" &&
729 cat >broken <<-EOF &&
730 tree $empty
731 parent $ZERO_OID
732 author whatever <whatever@example.com> 1234 -0000
733 committer whatever <whatever@example.com> 1234 -0000
735 broken commit
737 broken="$(git hash-object -w -t commit --literally broken)" &&
738 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
739 test_must_fail git commit-graph write --stdin-commits \
740 <good 2>test_err &&
741 test_i18ngrep "unable to parse commit" test_err
745 test_expect_success 'corrupt commit-graph write (missing tree)' '
746 rm -rf repo &&
747 git init repo &&
749 cd repo &&
750 tree="$(git mktree </dev/null)" &&
751 cat >broken <<-EOF &&
752 parent $ZERO_OID
753 author whatever <whatever@example.com> 1234 -0000
754 committer whatever <whatever@example.com> 1234 -0000
756 broken commit
758 broken="$(git hash-object -w -t commit --literally broken)" &&
759 git commit-tree -p "$broken" -m "good" "$tree" >good &&
760 test_must_fail git commit-graph write --stdin-commits \
761 <good 2>test_err &&
762 test_i18ngrep "unable to parse commit" test_err
766 # We test the overflow-related code with the following repo history:
768 # 4:F - 5:N - 6:U
769 # / \
770 # 1:U - 2:N - 3:U M:N
771 # \ /
772 # 7:N - 8:F - 9:N
774 # Here the commits denoted by U have committer date of zero seconds
775 # since Unix epoch, the commits denoted by N have committer date
776 # starting from 1112354055 seconds since Unix epoch (default committer
777 # date for the test suite), and the commits denoted by F have committer
778 # date of (2 ^ 31 - 2) seconds since Unix epoch.
780 # The largest offset observed is 2 ^ 31, just large enough to overflow.
783 test_expect_success 'set up and verify repo with generation data overflow chunk' '
784 objdir=".git/objects" &&
785 UNIX_EPOCH_ZERO="@0 +0000" &&
786 FUTURE_DATE="@2147483646 +0000" &&
787 cd "$TRASH_DIRECTORY" &&
788 mkdir repo &&
789 cd repo &&
790 git init &&
791 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
792 test_commit 2 &&
793 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
794 git commit-graph write --reachable &&
795 graph_read_expect 3 generation_data &&
796 test_commit --date "$FUTURE_DATE" 4 &&
797 test_commit 5 &&
798 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
799 git branch left &&
800 git reset --hard 3 &&
801 test_commit 7 &&
802 test_commit --date "$FUTURE_DATE" 8 &&
803 test_commit 9 &&
804 git branch right &&
805 git reset --hard 3 &&
806 test_merge M left right &&
807 git commit-graph write --reachable &&
808 graph_read_expect 10 "generation_data generation_data_overflow" &&
809 git commit-graph verify
812 graph_git_behavior 'generation data overflow chunk repo' repo left right
814 test_done