The twelfth batch
[git/gitster.git] / t / t5318-commit-graph.sh
blob2916c07e3c2490e7af5eb05cfe62f90e4c7c359a
1 #!/bin/sh
3 test_description='commit graph'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-chunk.sh
9 GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
11 test_expect_success 'usage' '
12 test_expect_code 129 git commit-graph write blah 2>err &&
13 test_expect_code 129 git commit-graph write verify
16 test_expect_success 'usage shown without sub-command' '
17 test_expect_code 129 git commit-graph 2>err &&
18 grep usage: err
21 test_expect_success 'usage shown with an error on unknown sub-command' '
22 cat >expect <<-\EOF &&
23 error: unknown subcommand: `unknown'\''
24 EOF
25 test_expect_code 129 git commit-graph unknown 2>stderr &&
26 grep error stderr >actual &&
27 test_cmp expect actual
30 objdir=".git/objects"
32 test_expect_success 'setup full repo' '
33 git init full
36 test_expect_success POSIXPERM 'tweak umask for modebit tests' '
37 umask 022
40 test_expect_success 'verify graph with no graph file' '
41 git -C full commit-graph verify
44 test_expect_success 'write graph with no packs' '
45 git -C full commit-graph write --object-dir $objdir &&
46 test_path_is_missing full/$objdir/info/commit-graph
49 test_expect_success 'exit with correct error on bad input to --stdin-packs' '
50 echo doesnotexist >in &&
51 test_expect_code 1 git -C full commit-graph write --stdin-packs \
52 <in 2>stderr &&
53 test_grep "error adding pack" stderr
56 test_expect_success 'create commits and repack' '
57 for i in $(test_seq 3)
59 test_commit -C full $i &&
60 git -C full branch commits/$i || return 1
61 done &&
62 git -C full repack
65 . "$TEST_DIRECTORY"/lib-commit-graph.sh
67 graph_git_behavior 'no graph' full commits/3 commits/1
69 test_expect_success 'exit with correct error on bad input to --stdin-commits' '
70 # invalid, non-hex OID
71 echo HEAD | test_expect_code 1 git -C full commit-graph write \
72 --stdin-commits 2>stderr &&
73 test_grep "unexpected non-hex object ID: HEAD" stderr &&
74 # non-existent OID
75 echo $ZERO_OID | test_expect_code 1 git -C full commit-graph write \
76 --stdin-commits 2>stderr &&
77 test_grep "invalid object" stderr &&
78 # valid commit and tree OID
79 git -C full rev-parse HEAD HEAD^{tree} >in &&
80 git -C full commit-graph write --stdin-commits <in &&
81 graph_read_expect -C full 3 generation_data
84 test_expect_success 'write graph' '
85 git -C full commit-graph write &&
86 test_path_is_file full/$objdir/info/commit-graph &&
87 graph_read_expect -C full 3 generation_data
90 test_expect_success POSIXPERM 'write graph has correct permissions' '
91 test_path_is_file full/$objdir/info/commit-graph &&
92 echo "-r--r--r--" >expect &&
93 test_modebits full/$objdir/info/commit-graph >actual &&
94 test_cmp expect actual
97 graph_git_behavior 'graph exists' full commits/3 commits/1
99 test_expect_success 'Add more commits' '
100 git -C full reset --hard commits/1 &&
101 for i in $(test_seq 4 5)
103 test_commit -C full $i &&
104 git -C full branch commits/$i || return 1
105 done &&
106 git -C full reset --hard commits/2 &&
107 for i in $(test_seq 6 7)
109 test_commit -C full $i &&
110 git -C full branch commits/$i || return 1
111 done &&
112 git -C full reset --hard commits/2 &&
113 git -C full merge commits/4 &&
114 git -C full branch merge/1 &&
115 git -C full reset --hard commits/4 &&
116 git -C full merge commits/6 &&
117 git -C full branch merge/2 &&
118 git -C full reset --hard commits/3 &&
119 git -C full merge commits/5 commits/7 &&
120 git -C full branch merge/3 &&
121 git -C full repack
124 test_expect_success 'commit-graph write progress off for redirected stderr' '
125 git -C full commit-graph write 2>err &&
126 test_must_be_empty err
129 test_expect_success 'commit-graph write force progress on for stderr' '
130 GIT_PROGRESS_DELAY=0 git -C full commit-graph write --progress 2>err &&
131 test_file_not_empty err
134 test_expect_success 'commit-graph write with the --no-progress option' '
135 git -C full commit-graph write --no-progress 2>err &&
136 test_must_be_empty err
139 test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
140 git -C full rev-parse commits/5 >in &&
141 git -C full commit-graph write --stdin-commits <in 2>err &&
142 test_must_be_empty err
145 test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
146 git -C full rev-parse commits/5 >in &&
147 GIT_PROGRESS_DELAY=0 git -C full commit-graph write --stdin-commits \
148 --progress <in 2>err &&
149 test_grep "Collecting commits from input" err
152 test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
153 git -C full rev-parse commits/5 >in &&
154 git -C full commit-graph write --stdin-commits --no-progress <in 2>err &&
155 test_must_be_empty err
158 test_expect_success 'commit-graph verify progress off for redirected stderr' '
159 git -C full commit-graph verify 2>err &&
160 test_must_be_empty err
163 test_expect_success 'commit-graph verify force progress on for stderr' '
164 GIT_PROGRESS_DELAY=0 git -C full commit-graph verify --progress 2>err &&
165 test_file_not_empty err
168 test_expect_success 'commit-graph verify with the --no-progress option' '
169 git -C full commit-graph verify --no-progress 2>err &&
170 test_must_be_empty err
173 # Current graph structure:
175 # __M3___
176 # / | \
177 # 3 M1 5 M2 7
178 # |/ \|/ \|
179 # 2 4 6
180 # |___/____/
183 test_expect_success 'write graph with merges' '
184 git -C full commit-graph write &&
185 test_path_is_file full/$objdir/info/commit-graph &&
186 graph_read_expect -C full 10 "generation_data extra_edges"
189 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
190 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
191 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
193 test_expect_success 'Add one more commit' '
194 test_commit -C full 8 &&
195 git -C full branch commits/8 &&
196 ls full/$objdir/pack | grep idx >existing-idx &&
197 git -C full repack &&
198 ls full/$objdir/pack| grep idx | grep -v -f existing-idx >new-idx
201 # Current graph structure:
205 # __M3___
206 # / | \
207 # 3 M1 5 M2 7
208 # |/ \|/ \|
209 # 2 4 6
210 # |___/____/
213 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
214 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
216 test_expect_success 'write graph with new commit' '
217 git -C full commit-graph write &&
218 test_path_is_file full/$objdir/info/commit-graph &&
219 graph_read_expect -C full 11 "generation_data extra_edges"
222 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
223 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
225 test_expect_success 'write graph with nothing new' '
226 git -C full commit-graph write &&
227 test_path_is_file full/$objdir/info/commit-graph &&
228 graph_read_expect -C full 11 "generation_data extra_edges"
231 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
232 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
234 test_expect_success 'build graph from latest pack with closure' '
235 git -C full commit-graph write --stdin-packs <new-idx &&
236 test_path_is_file full/$objdir/info/commit-graph &&
237 graph_read_expect -C full 9 "generation_data extra_edges"
240 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
241 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
243 test_expect_success 'build graph from commits with closure' '
244 git -C full tag -a -m "merge" tag/merge merge/2 &&
245 git -C full rev-parse tag/merge >commits-in &&
246 git -C full rev-parse merge/1 >>commits-in &&
247 git -C full commit-graph write --stdin-commits <commits-in &&
248 test_path_is_file full/$objdir/info/commit-graph &&
249 graph_read_expect -C full 6 "generation_data"
252 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
253 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
255 test_expect_success 'build graph from commits with append' '
256 git -C full rev-parse merge/3 >in &&
257 git -C full commit-graph write --stdin-commits --append <in &&
258 test_path_is_file full/$objdir/info/commit-graph &&
259 graph_read_expect -C full 10 "generation_data extra_edges"
262 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
263 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
265 test_expect_success 'build graph using --reachable' '
266 git -C full commit-graph write --reachable &&
267 test_path_is_file full/$objdir/info/commit-graph &&
268 graph_read_expect -C full 11 "generation_data extra_edges"
271 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
272 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
274 test_expect_success 'setup bare repo' '
275 git clone --bare --no-local full bare
278 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
279 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
281 test_expect_success 'write graph in bare repo' '
282 git -C bare commit-graph write &&
283 test_path_is_file bare/objects/info/commit-graph &&
284 graph_read_expect -C bare 11 "generation_data extra_edges"
287 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
288 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
290 test_expect_success 'perform fast-forward merge in full repo' '
291 git -C full checkout -b merge-5-to-8 commits/5 &&
292 git -C full merge commits/8 &&
293 git -C full show-ref -s merge-5-to-8 >output &&
294 git -C full show-ref -s commits/8 >expect &&
295 test_cmp expect output
298 test_expect_success 'check that gc computes commit-graph' '
299 test_commit -C full --no-tag blank &&
300 git -C full commit-graph write --reachable &&
301 cp full/$objdir/info/commit-graph commit-graph-before-gc &&
302 git -C full reset --hard HEAD~1 &&
303 test_config -C full gc.writeCommitGraph true &&
304 git -C full gc &&
305 cp full/$objdir/info/commit-graph commit-graph-after-gc &&
306 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
307 git -C full commit-graph write --reachable &&
308 test_cmp_bin commit-graph-after-gc full/$objdir/info/commit-graph
311 test_expect_success 'replace-objects invalidates commit-graph' '
312 test_when_finished rm -rf replace &&
313 git clone full replace &&
315 cd replace &&
316 git commit-graph write --reachable &&
317 test_path_is_file .git/objects/info/commit-graph &&
318 git replace HEAD~1 HEAD~2 &&
319 graph_git_two_modes "commit-graph verify" &&
320 git -c core.commitGraph=false log >expect &&
321 git -c core.commitGraph=true log >actual &&
322 test_cmp expect actual &&
323 git commit-graph write --reachable &&
324 git -c core.commitGraph=false --no-replace-objects log >expect &&
325 git -c core.commitGraph=true --no-replace-objects log >actual &&
326 test_cmp expect actual &&
327 rm -rf .git/objects/info/commit-graph &&
328 git commit-graph write --reachable &&
329 test_path_is_file .git/objects/info/commit-graph
333 test_expect_success 'commit grafts invalidate commit-graph' '
334 test_when_finished rm -rf graft &&
335 git clone --template= full graft &&
337 cd graft &&
338 git commit-graph write --reachable &&
339 test_path_is_file .git/objects/info/commit-graph &&
340 H1=$(git rev-parse --verify HEAD~1) &&
341 H3=$(git rev-parse --verify HEAD~3) &&
342 mkdir .git/info &&
343 echo "$H1 $H3" >.git/info/grafts &&
344 git -c core.commitGraph=false log >expect &&
345 git -c core.commitGraph=true log >actual &&
346 test_cmp expect actual &&
347 git commit-graph write --reachable &&
348 git -c core.commitGraph=false --no-replace-objects log >expect &&
349 git -c core.commitGraph=true --no-replace-objects log >actual &&
350 test_cmp expect actual &&
351 rm -rf .git/objects/info/commit-graph &&
352 git commit-graph write --reachable &&
353 test_path_is_missing .git/objects/info/commit-graph
357 test_expect_success 'replace-objects invalidates commit-graph' '
358 test_when_finished rm -rf shallow &&
359 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
361 cd shallow &&
362 git commit-graph write --reachable &&
363 test_path_is_missing .git/objects/info/commit-graph &&
364 git fetch origin --unshallow &&
365 git commit-graph write --reachable &&
366 test_path_is_file .git/objects/info/commit-graph
370 test_expect_success 'warn on improper hash version' '
371 git init --object-format=sha1 sha1 &&
373 cd sha1 &&
374 test_commit 1 &&
375 git commit-graph write --reachable &&
376 mv .git/objects/info/commit-graph ../cg-sha1
377 ) &&
378 git init --object-format=sha256 sha256 &&
380 cd sha256 &&
381 test_commit 1 &&
382 git commit-graph write --reachable &&
383 mv .git/objects/info/commit-graph ../cg-sha256
384 ) &&
386 cd sha1 &&
387 mv ../cg-sha256 .git/objects/info/commit-graph &&
388 git log -1 2>err &&
389 test_grep "commit-graph hash version 2 does not match version 1" err
390 ) &&
392 cd sha256 &&
393 mv ../cg-sha1 .git/objects/info/commit-graph &&
394 git log -1 2>err &&
395 test_grep "commit-graph hash version 1 does not match version 2" err
399 test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow chunk' '
400 UNIX_EPOCH_ZERO="@0 +0000" &&
401 FUTURE_DATE="@4147483646 +0000" &&
402 rm -f full/.git/objects/info/commit-graph &&
403 test_commit -C full --date "$FUTURE_DATE" future-1 &&
404 test_commit -C full --date "$UNIX_EPOCH_ZERO" old-1 &&
405 git -C full commit-graph write --reachable &&
406 test_commit -C full --date "$FUTURE_DATE" future-2 &&
407 test_commit -C full --date "$UNIX_EPOCH_ZERO" old-2 &&
408 git -C full commit-graph write --reachable --split=no-merge &&
409 test_commit -C full extra &&
410 git -C full commit-graph write --reachable --split=no-merge &&
411 git -C full commit-graph write --reachable &&
412 graph_read_expect -C full 16 \
413 "generation_data generation_data_overflow extra_edges" &&
414 mv full/.git/objects/info/commit-graph commit-graph-upgraded &&
415 git -C full commit-graph write --reachable &&
416 graph_read_expect -C full 16 \
417 "generation_data generation_data_overflow extra_edges" &&
418 test_cmp full/.git/objects/info/commit-graph commit-graph-upgraded
421 # the verify tests below expect the commit-graph to contain
422 # exactly the commits reachable from the commits/8 branch.
423 # If the file changes the set of commits in the list, then the
424 # offsets into the binary file will result in different edits
425 # and the tests will likely break.
427 test_expect_success 'git commit-graph verify' '
428 git -C full rev-parse commits/8 >in &&
429 git -C full -c commitGraph.generationVersion=1 commit-graph write \
430 --stdin-commits <in &&
431 git -C full commit-graph verify >output &&
432 graph_read_expect -C full 9 extra_edges 1
435 NUM_COMMITS=9
436 NUM_OCTOPUS_EDGES=2
437 HASH_LEN="$(test_oid rawsz)"
438 GRAPH_BYTE_VERSION=4
439 GRAPH_BYTE_HASH=5
440 GRAPH_BYTE_CHUNK_COUNT=6
441 GRAPH_CHUNK_LOOKUP_OFFSET=8
442 GRAPH_CHUNK_LOOKUP_WIDTH=12
443 GRAPH_CHUNK_LOOKUP_ROWS=5
444 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
445 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
446 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
447 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
448 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
449 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
450 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
451 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
452 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
453 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
454 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
455 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
456 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
457 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
458 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
459 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
460 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
461 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
462 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
463 GRAPH_BYTE_COMMIT_GENERATION_LAST=$(($GRAPH_BYTE_COMMIT_GENERATION + $(($NUM_COMMITS - 1)) * $GRAPH_COMMIT_DATA_WIDTH))
464 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
465 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
466 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
467 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
468 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
470 corrupt_graph_setup() {
471 test_when_finished mv commit-graph-backup full/$objdir/info/commit-graph &&
472 cp full/$objdir/info/commit-graph commit-graph-backup &&
473 chmod u+w full/$objdir/info/commit-graph
476 corrupt_graph_verify() {
477 grepstr=$1
478 test_must_fail git -C full commit-graph verify 2>test_err &&
479 grep -v "^+" test_err >err &&
480 test_grep "$grepstr" err &&
481 if test "$2" != "no-copy"
482 then
483 cp full/$objdir/info/commit-graph commit-graph-pre-write-test
484 fi &&
485 git -C full status --short &&
486 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git -C full commit-graph write &&
487 chmod u+w full/$objdir/info/commit-graph &&
488 git -C full commit-graph verify
491 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
492 # Manipulates the commit-graph file at the position
493 # by inserting the data, optionally zeroing the file
494 # starting at <zero_pos>, then runs 'git commit-graph verify'
495 # and places the output in the file 'err'. Test 'err' for
496 # the given string.
497 corrupt_graph_and_verify() {
498 pos=$1
499 data="${2:-\0}"
500 grepstr=$3
501 corrupt_graph_setup &&
502 orig_size=$(wc -c <full/$objdir/info/commit-graph) &&
503 zero_pos=${4:-${orig_size}} &&
504 printf "$data" | dd of="full/$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
505 dd of="full/$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
506 test-tool genzeros $(($orig_size - $zero_pos)) >>"full/$objdir/info/commit-graph" &&
507 corrupt_graph_verify "$grepstr"
511 test_expect_success POSIXPERM,SANITY 'detect permission problem' '
512 corrupt_graph_setup &&
513 chmod 000 full/$objdir/info/commit-graph &&
514 corrupt_graph_verify "Could not open" "no-copy"
517 test_expect_success 'detect too small' '
518 corrupt_graph_setup &&
519 echo "a small graph" >full/$objdir/info/commit-graph &&
520 corrupt_graph_verify "too small"
523 test_expect_success 'detect bad signature' '
524 corrupt_graph_and_verify 0 "\0" \
525 "graph signature"
528 test_expect_success 'detect bad version' '
529 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
530 "graph version"
533 test_expect_success 'detect bad hash version' '
534 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
535 "hash version"
538 test_expect_success 'detect low chunk count' '
539 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
540 "final chunk has non-zero id"
543 test_expect_success 'detect missing OID fanout chunk' '
544 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
545 "commit-graph required OID fanout chunk missing or corrupted"
548 test_expect_success 'detect missing OID lookup chunk' '
549 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
550 "commit-graph required OID lookup chunk missing or corrupted"
553 test_expect_success 'detect missing commit data chunk' '
554 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
555 "commit-graph required commit data chunk missing or corrupted"
558 test_expect_success 'detect incorrect fanout' '
559 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
560 "fanout value"
563 test_expect_success 'detect incorrect fanout final value' '
564 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
565 "OID lookup chunk is the wrong size"
568 test_expect_success 'detect incorrect OID order' '
569 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
570 "incorrect OID order"
573 test_expect_success 'detect OID not in object database' '
574 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
575 "from object database"
578 test_expect_success 'detect incorrect tree OID' '
579 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
580 "root tree OID for commit"
583 test_expect_success 'detect incorrect parent int-id' '
584 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
585 "invalid parent"
588 test_expect_success 'detect extra parent int-id' '
589 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
590 "is too long"
593 test_expect_success 'detect wrong parent' '
594 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
595 "commit-graph parent for"
598 test_expect_success 'detect incorrect generation number' '
599 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
600 "generation for commit"
603 test_expect_success 'detect incorrect commit date' '
604 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
605 "commit date"
608 test_expect_success 'detect incorrect parent for octopus merge' '
609 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
610 "invalid parent"
613 test_expect_success 'detect invalid checksum hash' '
614 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
615 "incorrect checksum"
618 test_expect_success 'detect incorrect chunk count' '
619 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
620 "commit-graph file is too small to hold [0-9]* chunks" \
621 $GRAPH_CHUNK_LOOKUP_OFFSET
624 test_expect_success 'detect mixed generation numbers (non-zero to zero)' '
625 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION_LAST "\0\0\0\0" \
626 "both zero and non-zero generations"
629 test_expect_success 'detect mixed generation numbers (zero to non-zero)' '
630 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\0\0\0\0" \
631 "both zero and non-zero generations"
634 test_expect_success 'git fsck (checks commit-graph when config set to true)' '
635 git -C full fsck &&
636 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
637 "incorrect checksum" &&
638 cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
639 test_must_fail git -C full -c core.commitGraph=true fsck
642 test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
643 git -C full fsck &&
644 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
645 "incorrect checksum" &&
646 cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
647 git -C full -c core.commitGraph=false fsck
650 test_expect_success 'git fsck (checks commit-graph when config unset)' '
651 test_when_finished "git -C full config core.commitGraph true" &&
653 git -C full fsck &&
654 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
655 "incorrect checksum" &&
656 test_unconfig -C full core.commitGraph &&
657 cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
658 test_must_fail git -C full fsck
661 test_expect_success 'git fsck shows commit-graph output with --progress' '
662 git -C "$TRASH_DIRECTORY/full" fsck --progress 2>err &&
663 grep "Verifying commits in commit graph" err
666 test_expect_success 'git fsck suppresses commit-graph output with --no-progress' '
667 git -C "$TRASH_DIRECTORY/full" fsck --no-progress 2>err &&
668 ! grep "Verifying commits in commit graph" err
671 test_expect_success 'setup non-the_repository tests' '
672 rm -rf repo &&
673 git init repo &&
674 test_commit -C repo one &&
675 test_commit -C repo two &&
676 git -C repo config core.commitGraph true &&
677 git -C repo rev-parse two | \
678 git -C repo commit-graph write --stdin-commits
681 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
682 test-tool repository parse_commit_in_graph \
683 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
685 git -C repo log --pretty=format:"%ct " -1 &&
686 git -C repo rev-parse one
687 } >expect &&
688 test_cmp expect actual &&
690 test-tool repository parse_commit_in_graph \
691 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
692 git -C repo log --pretty="%ct" -1 one >expect &&
693 test_cmp expect actual
696 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
697 test-tool repository get_commit_tree_in_graph \
698 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
699 git -C repo rev-parse two^{tree} >expect &&
700 test_cmp expect actual &&
702 test-tool repository get_commit_tree_in_graph \
703 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
704 git -C repo rev-parse one^{tree} >expect &&
705 test_cmp expect actual
708 test_expect_success 'corrupt commit-graph write (broken parent)' '
709 rm -rf repo &&
710 git init repo &&
712 cd repo &&
713 empty="$(git mktree </dev/null)" &&
714 cat >broken <<-EOF &&
715 tree $empty
716 parent $ZERO_OID
717 author whatever <whatever@example.com> 1234 -0000
718 committer whatever <whatever@example.com> 1234 -0000
720 broken commit
722 broken="$(git hash-object -w -t commit --literally broken)" &&
723 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
724 test_must_fail git commit-graph write --stdin-commits \
725 <good 2>test_err &&
726 test_grep "unable to parse commit" test_err
730 test_expect_success 'corrupt commit-graph write (missing tree)' '
731 rm -rf repo &&
732 git init repo &&
734 cd repo &&
735 tree="$(git mktree </dev/null)" &&
736 cat >broken <<-EOF &&
737 parent $ZERO_OID
738 author whatever <whatever@example.com> 1234 -0000
739 committer whatever <whatever@example.com> 1234 -0000
741 broken commit
743 broken="$(git hash-object -w -t commit --literally broken)" &&
744 git commit-tree -p "$broken" -m "good" "$tree" >good &&
745 test_must_fail git commit-graph write --stdin-commits \
746 <good 2>test_err &&
747 test_grep "unable to parse commit" test_err
751 # We test the overflow-related code with the following repo history:
753 # 4:F - 5:N - 6:U
754 # / \
755 # 1:U - 2:N - 3:U M:N
756 # \ /
757 # 7:N - 8:F - 9:N
759 # Here the commits denoted by U have committer date of zero seconds
760 # since Unix epoch, the commits denoted by N have committer date
761 # starting from 1112354055 seconds since Unix epoch (default committer
762 # date for the test suite), and the commits denoted by F have committer
763 # date of (2 ^ 31 - 2) seconds since Unix epoch.
765 # The largest offset observed is 2 ^ 31, just large enough to overflow.
768 test_expect_success 'set up and verify repo with generation data overflow chunk' '
769 UNIX_EPOCH_ZERO="@0 +0000" &&
770 FUTURE_DATE="@2147483646 +0000" &&
772 git init repo &&
774 cd repo &&
776 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
777 test_commit 2 &&
778 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
779 git commit-graph write --reachable &&
780 graph_read_expect 3 generation_data &&
781 test_commit --date "$FUTURE_DATE" 4 &&
782 test_commit 5 &&
783 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
784 git branch left &&
785 git reset --hard 3 &&
786 test_commit 7 &&
787 test_commit --date "$FUTURE_DATE" 8 &&
788 test_commit 9 &&
789 git branch right &&
790 git reset --hard 3 &&
791 test_merge M left right &&
792 git commit-graph write --reachable &&
793 graph_read_expect 10 "generation_data generation_data_overflow" &&
794 git commit-graph verify
798 graph_git_behavior 'generation data overflow chunk repo' repo left right
800 test_expect_success 'overflow during generation version upgrade' '
801 git init overflow-v2-upgrade &&
803 cd overflow-v2-upgrade &&
805 # This commit will have a date at two seconds past the Epoch,
806 # and a (v1) generation number of 1, since it is a root commit.
808 # The offset will then be computed as 1-2, which will underflow
809 # to 2^31, which is greater than the v2 offset small limit of
810 # 2^31-1.
812 # This is sufficient to need a large offset table for the v2
813 # generation numbers.
814 test_commit --date "@2 +0000" base &&
815 git repack -d &&
817 # Test that upgrading from generation v1 to v2 correctly
818 # produces the overflow table.
819 git -c commitGraph.generationVersion=1 commit-graph write &&
820 git -c commitGraph.generationVersion=2 commit-graph write \
821 --changed-paths &&
823 git rev-list --all
827 corrupt_chunk () {
828 graph=full/.git/objects/info/commit-graph &&
829 test_when_finished "rm -rf $graph" &&
830 git -C full commit-graph write --reachable &&
831 corrupt_chunk_file $graph "$@"
834 check_corrupt_chunk () {
835 corrupt_chunk "$@" &&
836 git -C full -c core.commitGraph=false log >expect.out &&
837 git -C full -c core.commitGraph=true log >out 2>err &&
838 test_cmp expect.out out
841 test_expect_success 'reader notices too-small oid fanout chunk' '
842 # make it big enough that the graph file is plausible,
843 # otherwise we hit an earlier check
844 check_corrupt_chunk OIDF clear $(printf "000000%02x" $(test_seq 250)) &&
845 cat >expect.err <<-\EOF &&
846 error: commit-graph oid fanout chunk is wrong size
847 error: commit-graph required OID fanout chunk missing or corrupted
849 test_cmp expect.err err
852 test_expect_success 'reader notices fanout/lookup table mismatch' '
853 check_corrupt_chunk OIDF 1020 "FFFFFFFF" &&
854 cat >expect.err <<-\EOF &&
855 error: commit-graph OID lookup chunk is the wrong size
856 error: commit-graph required OID lookup chunk missing or corrupted
858 test_cmp expect.err err
861 test_expect_success 'reader notices out-of-bounds fanout' '
862 # Rather than try to corrupt a specific hash, we will just
863 # wreck them all. But we cannot just set them all to 0xFFFFFFFF or
864 # similar, as they are used for hi/lo starts in a binary search (so if
865 # they are identical, that indicates that the search should abort
866 # immediately). Instead, we will give them high values that differ by
867 # 2^24, ensuring that any that are used would cause an out-of-bounds
868 # read.
869 check_corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
870 cat >expect.err <<-\EOF &&
871 error: commit-graph fanout values out of order
872 error: commit-graph required OID fanout chunk missing or corrupted
874 test_cmp expect.err err
877 test_expect_success 'reader notices too-small commit data chunk' '
878 check_corrupt_chunk CDAT clear 00000000 &&
879 cat >expect.err <<-\EOF &&
880 error: commit-graph commit data chunk is wrong size
881 error: commit-graph required commit data chunk missing or corrupted
883 test_cmp expect.err err
886 test_expect_success 'reader notices out-of-bounds extra edge' '
887 check_corrupt_chunk EDGE clear &&
888 cat >expect.err <<-\EOF &&
889 error: commit-graph extra-edges pointer out of bounds
891 test_cmp expect.err err
894 test_expect_success 'reader notices too-small generations chunk' '
895 check_corrupt_chunk GDA2 clear 00000000 &&
896 cat >expect.err <<-\EOF &&
897 error: commit-graph generations chunk is wrong size
899 test_cmp expect.err err
902 test_expect_success 'stale commit cannot be parsed when given directly' '
903 test_when_finished "rm -rf repo" &&
904 git init repo &&
906 cd repo &&
907 test_commit A &&
908 test_commit B &&
909 git commit-graph write --reachable &&
911 oid=$(git rev-parse B) &&
912 rm .git/objects/"$(test_oid_to_path "$oid")" &&
914 # Verify that it is possible to read the commit from the
915 # commit graph when not being paranoid, ...
916 git rev-list B &&
917 # ... but parsing the commit when double checking that
918 # it actually exists in the object database should fail.
919 test_must_fail env GIT_COMMIT_GRAPH_PARANOIA=true git rev-list -1 B
923 test_expect_success 'stale commit cannot be parsed when traversing graph' '
924 test_when_finished "rm -rf repo" &&
925 git init repo &&
927 cd repo &&
929 test_commit A &&
930 test_commit B &&
931 test_commit C &&
932 git commit-graph write --reachable &&
934 # Corrupt the repository by deleting the intermediate commit
935 # object. Commands should notice that this object is absent and
936 # thus that the repository is corrupt even if the commit graph
937 # exists.
938 oid=$(git rev-parse B) &&
939 rm .git/objects/"$(test_oid_to_path "$oid")" &&
941 # Again, we should be able to parse the commit when not
942 # being paranoid about commit graph staleness...
943 git rev-parse HEAD~2 &&
944 # ... but fail when we are paranoid.
945 test_must_fail env GIT_COMMIT_GRAPH_PARANOIA=true git rev-parse HEAD~2 2>error &&
946 grep "error: commit $oid exists in commit-graph but not in the object database" error
950 test_done