]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - t/t5318-commit-graph.sh
builtin/bundle.c: let parse-options parse subcommands
[thirdparty/git.git] / t / t5318-commit-graph.sh
... / ...
CommitLineData
1#!/bin/sh
2
3test_description='commit graph'
4. ./test-lib.sh
5
6GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
7
8test_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
11'
12
13test_expect_success 'usage shown without sub-command' '
14 test_expect_code 129 git commit-graph 2>err &&
15 ! grep error: err
16'
17
18test_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
25'
26
27test_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"
33'
34
35test_expect_success POSIXPERM 'tweak umask for modebit tests' '
36 umask 022
37'
38
39test_expect_success 'verify graph with no graph file' '
40 cd "$TRASH_DIRECTORY/full" &&
41 git commit-graph verify
42'
43
44test_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
48'
49
50test_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
55'
56
57test_expect_success 'create commits and repack' '
58 cd "$TRASH_DIRECTORY/full" &&
59 for i in $(test_seq 3)
60 do
61 test_commit $i &&
62 git branch commits/$i || return 1
63 done &&
64 git repack
65'
66
67. "$TEST_DIRECTORY"/lib-commit-graph.sh
68
69graph_git_behavior 'no graph' full commits/3 commits/1
70
71test_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
85'
86
87test_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
92'
93
94test_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
99'
100
101graph_git_behavior 'graph exists' full commits/3 commits/1
102
103test_expect_success 'Add more commits' '
104 cd "$TRASH_DIRECTORY/full" &&
105 git reset --hard commits/1 &&
106 for i in $(test_seq 4 5)
107 do
108 test_commit $i &&
109 git branch commits/$i || return 1
110 done &&
111 git reset --hard commits/2 &&
112 for i in $(test_seq 6 7)
113 do
114 test_commit $i &&
115 git branch commits/$i || 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
127'
128
129test_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
133'
134
135test_expect_success 'commit-graph write force progress on for stderr' '
136 cd "$TRASH_DIRECTORY/full" &&
137 GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err &&
138 test_file_not_empty err
139'
140
141test_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
145'
146
147test_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
152'
153
154test_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
159'
160
161test_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
166'
167
168test_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
172'
173
174test_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
178'
179
180test_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
184'
185
186# Current graph structure:
187#
188# __M3___
189# / | \
190# 3 M1 5 M2 7
191# |/ \|/ \|
192# 2 4 6
193# |___/____/
194# 1
195
196test_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"
201'
202
203graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
204graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
205graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
206
207test_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
214'
215
216# Current graph structure:
217#
218# 8
219# |
220# __M3___
221# / | \
222# 3 M1 5 M2 7
223# |/ \|/ \|
224# 2 4 6
225# |___/____/
226# 1
227
228graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
229graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
230
231test_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"
236'
237
238graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
239graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
240
241test_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"
246'
247
248graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
249graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
250
251test_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"
256'
257
258graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
259graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
260
261test_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"
269'
270
271graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
272graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
273
274test_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"
279'
280
281graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
282graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
283
284test_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"
289'
290
291graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
292graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
293
294test_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"
300'
301
302graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
303graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
304
305test_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"
310'
311
312graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
313graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
314
315test_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
322'
323
324test_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
336'
337
338test_expect_success 'replace-objects invalidates commit-graph' '
339 cd "$TRASH_DIRECTORY" &&
340 test_when_finished rm -rf replace &&
341 git clone full replace &&
342 (
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
358 )
359'
360
361test_expect_success 'commit grafts invalidate commit-graph' '
362 cd "$TRASH_DIRECTORY" &&
363 test_when_finished rm -rf graft &&
364 git clone --template= full graft &&
365 (
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 mkdir .git/info &&
372 echo "$H1 $H3" >.git/info/grafts &&
373 git -c core.commitGraph=false log >expect &&
374 git -c core.commitGraph=true log >actual &&
375 test_cmp expect actual &&
376 git commit-graph write --reachable &&
377 git -c core.commitGraph=false --no-replace-objects log >expect &&
378 git -c core.commitGraph=true --no-replace-objects log >actual &&
379 test_cmp expect actual &&
380 rm -rf .git/objects/info/commit-graph &&
381 git commit-graph write --reachable &&
382 test_path_is_missing .git/objects/info/commit-graph
383 )
384'
385
386test_expect_success 'replace-objects invalidates commit-graph' '
387 cd "$TRASH_DIRECTORY" &&
388 test_when_finished rm -rf shallow &&
389 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
390 (
391 cd shallow &&
392 git commit-graph write --reachable &&
393 test_path_is_missing .git/objects/info/commit-graph &&
394 git fetch origin --unshallow &&
395 git commit-graph write --reachable &&
396 test_path_is_file .git/objects/info/commit-graph
397 )
398'
399
400test_expect_success 'warn on improper hash version' '
401 git init --object-format=sha1 sha1 &&
402 (
403 cd sha1 &&
404 test_commit 1 &&
405 git commit-graph write --reachable &&
406 mv .git/objects/info/commit-graph ../cg-sha1
407 ) &&
408 git init --object-format=sha256 sha256 &&
409 (
410 cd sha256 &&
411 test_commit 1 &&
412 git commit-graph write --reachable &&
413 mv .git/objects/info/commit-graph ../cg-sha256
414 ) &&
415 (
416 cd sha1 &&
417 mv ../cg-sha256 .git/objects/info/commit-graph &&
418 git log -1 2>err &&
419 test_i18ngrep "commit-graph hash version 2 does not match version 1" err
420 ) &&
421 (
422 cd sha256 &&
423 mv ../cg-sha1 .git/objects/info/commit-graph &&
424 git log -1 2>err &&
425 test_i18ngrep "commit-graph hash version 1 does not match version 2" err
426 )
427'
428
429test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow chunk' '
430 cd "$TRASH_DIRECTORY/full" &&
431 UNIX_EPOCH_ZERO="@0 +0000" &&
432 FUTURE_DATE="@4147483646 +0000" &&
433 rm -f .git/objects/info/commit-graph &&
434 test_commit --date "$FUTURE_DATE" future-1 &&
435 test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
436 git commit-graph write --reachable &&
437 test_commit --date "$FUTURE_DATE" future-2 &&
438 test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
439 git commit-graph write --reachable --split=no-merge &&
440 test_commit extra &&
441 git commit-graph write --reachable --split=no-merge &&
442 git commit-graph write --reachable &&
443 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
444 mv .git/objects/info/commit-graph commit-graph-upgraded &&
445 git commit-graph write --reachable &&
446 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
447 test_cmp .git/objects/info/commit-graph commit-graph-upgraded
448'
449
450# the verify tests below expect the commit-graph to contain
451# exactly the commits reachable from the commits/8 branch.
452# If the file changes the set of commits in the list, then the
453# offsets into the binary file will result in different edits
454# and the tests will likely break.
455
456test_expect_success 'git commit-graph verify' '
457 cd "$TRASH_DIRECTORY/full" &&
458 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
459 git commit-graph verify >output &&
460 graph_read_expect 9 extra_edges 1
461'
462
463NUM_COMMITS=9
464NUM_OCTOPUS_EDGES=2
465HASH_LEN="$(test_oid rawsz)"
466GRAPH_BYTE_VERSION=4
467GRAPH_BYTE_HASH=5
468GRAPH_BYTE_CHUNK_COUNT=6
469GRAPH_CHUNK_LOOKUP_OFFSET=8
470GRAPH_CHUNK_LOOKUP_WIDTH=12
471GRAPH_CHUNK_LOOKUP_ROWS=5
472GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
473GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
474 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
475GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
476 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
477GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
478 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
479GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
480GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
481GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
482GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
483GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
484GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
485GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
486GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
487GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
488GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
489GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
490GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
491GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
492GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
493 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
494GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
495GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
496
497corrupt_graph_setup() {
498 cd "$TRASH_DIRECTORY/full" &&
499 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
500 cp $objdir/info/commit-graph commit-graph-backup &&
501 chmod u+w $objdir/info/commit-graph
502}
503
504corrupt_graph_verify() {
505 grepstr=$1
506 test_must_fail git commit-graph verify 2>test_err &&
507 grep -v "^+" test_err >err &&
508 test_i18ngrep "$grepstr" err &&
509 if test "$2" != "no-copy"
510 then
511 cp $objdir/info/commit-graph commit-graph-pre-write-test
512 fi &&
513 git status --short &&
514 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
515 chmod u+w $objdir/info/commit-graph &&
516 git commit-graph verify
517}
518
519# usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
520# Manipulates the commit-graph file at the position
521# by inserting the data, optionally zeroing the file
522# starting at <zero_pos>, then runs 'git commit-graph verify'
523# and places the output in the file 'err'. Test 'err' for
524# the given string.
525corrupt_graph_and_verify() {
526 pos=$1
527 data="${2:-\0}"
528 grepstr=$3
529 corrupt_graph_setup &&
530 orig_size=$(wc -c < $objdir/info/commit-graph) &&
531 zero_pos=${4:-${orig_size}} &&
532 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
533 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
534 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
535 corrupt_graph_verify "$grepstr"
536
537}
538
539test_expect_success POSIXPERM,SANITY 'detect permission problem' '
540 corrupt_graph_setup &&
541 chmod 000 $objdir/info/commit-graph &&
542 corrupt_graph_verify "Could not open" "no-copy"
543'
544
545test_expect_success 'detect too small' '
546 corrupt_graph_setup &&
547 echo "a small graph" >$objdir/info/commit-graph &&
548 corrupt_graph_verify "too small"
549'
550
551test_expect_success 'detect bad signature' '
552 corrupt_graph_and_verify 0 "\0" \
553 "graph signature"
554'
555
556test_expect_success 'detect bad version' '
557 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
558 "graph version"
559'
560
561test_expect_success 'detect bad hash version' '
562 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
563 "hash version"
564'
565
566test_expect_success 'detect low chunk count' '
567 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
568 "final chunk has non-zero id"
569'
570
571test_expect_success 'detect missing OID fanout chunk' '
572 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
573 "missing the OID Fanout chunk"
574'
575
576test_expect_success 'detect missing OID lookup chunk' '
577 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
578 "missing the OID Lookup chunk"
579'
580
581test_expect_success 'detect missing commit data chunk' '
582 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
583 "missing the Commit Data chunk"
584'
585
586test_expect_success 'detect incorrect fanout' '
587 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
588 "fanout value"
589'
590
591test_expect_success 'detect incorrect fanout final value' '
592 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
593 "fanout value"
594'
595
596test_expect_success 'detect incorrect OID order' '
597 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
598 "incorrect OID order"
599'
600
601test_expect_success 'detect OID not in object database' '
602 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
603 "from object database"
604'
605
606test_expect_success 'detect incorrect tree OID' '
607 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
608 "root tree OID for commit"
609'
610
611test_expect_success 'detect incorrect parent int-id' '
612 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
613 "invalid parent"
614'
615
616test_expect_success 'detect extra parent int-id' '
617 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
618 "is too long"
619'
620
621test_expect_success 'detect wrong parent' '
622 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
623 "commit-graph parent for"
624'
625
626test_expect_success 'detect incorrect generation number' '
627 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
628 "generation for commit"
629'
630
631test_expect_success 'detect incorrect generation number' '
632 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
633 "non-zero generation number"
634'
635
636test_expect_success 'detect incorrect commit date' '
637 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
638 "commit date"
639'
640
641test_expect_success 'detect incorrect parent for octopus merge' '
642 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
643 "invalid parent"
644'
645
646test_expect_success 'detect invalid checksum hash' '
647 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
648 "incorrect checksum"
649'
650
651test_expect_success 'detect incorrect chunk count' '
652 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
653 "commit-graph file is too small to hold [0-9]* chunks" \
654 $GRAPH_CHUNK_LOOKUP_OFFSET
655'
656
657test_expect_success 'git fsck (checks commit-graph when config set to true)' '
658 cd "$TRASH_DIRECTORY/full" &&
659 git fsck &&
660 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
661 "incorrect checksum" &&
662 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
663 test_must_fail git -c core.commitGraph=true fsck
664'
665
666test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
667 cd "$TRASH_DIRECTORY/full" &&
668 git fsck &&
669 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
670 "incorrect checksum" &&
671 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
672 git -c core.commitGraph=false fsck
673'
674
675test_expect_success 'git fsck (checks commit-graph when config unset)' '
676 cd "$TRASH_DIRECTORY/full" &&
677 test_when_finished "git config core.commitGraph true" &&
678
679 git fsck &&
680 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
681 "incorrect checksum" &&
682 test_unconfig core.commitGraph &&
683 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
684 test_must_fail git fsck
685'
686
687test_expect_success 'setup non-the_repository tests' '
688 rm -rf repo &&
689 git init repo &&
690 test_commit -C repo one &&
691 test_commit -C repo two &&
692 git -C repo config core.commitGraph true &&
693 git -C repo rev-parse two | \
694 git -C repo commit-graph write --stdin-commits
695'
696
697test_expect_success 'parse_commit_in_graph works for non-the_repository' '
698 test-tool repository parse_commit_in_graph \
699 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
700 {
701 git -C repo log --pretty=format:"%ct " -1 &&
702 git -C repo rev-parse one
703 } >expect &&
704 test_cmp expect actual &&
705
706 test-tool repository parse_commit_in_graph \
707 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
708 git -C repo log --pretty="%ct" -1 one >expect &&
709 test_cmp expect actual
710'
711
712test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
713 test-tool repository get_commit_tree_in_graph \
714 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
715 git -C repo rev-parse two^{tree} >expect &&
716 test_cmp expect actual &&
717
718 test-tool repository get_commit_tree_in_graph \
719 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
720 git -C repo rev-parse one^{tree} >expect &&
721 test_cmp expect actual
722'
723
724test_expect_success 'corrupt commit-graph write (broken parent)' '
725 rm -rf repo &&
726 git init repo &&
727 (
728 cd repo &&
729 empty="$(git mktree </dev/null)" &&
730 cat >broken <<-EOF &&
731 tree $empty
732 parent $ZERO_OID
733 author whatever <whatever@example.com> 1234 -0000
734 committer whatever <whatever@example.com> 1234 -0000
735
736 broken commit
737 EOF
738 broken="$(git hash-object -w -t commit --literally broken)" &&
739 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
740 test_must_fail git commit-graph write --stdin-commits \
741 <good 2>test_err &&
742 test_i18ngrep "unable to parse commit" test_err
743 )
744'
745
746test_expect_success 'corrupt commit-graph write (missing tree)' '
747 rm -rf repo &&
748 git init repo &&
749 (
750 cd repo &&
751 tree="$(git mktree </dev/null)" &&
752 cat >broken <<-EOF &&
753 parent $ZERO_OID
754 author whatever <whatever@example.com> 1234 -0000
755 committer whatever <whatever@example.com> 1234 -0000
756
757 broken commit
758 EOF
759 broken="$(git hash-object -w -t commit --literally broken)" &&
760 git commit-tree -p "$broken" -m "good" "$tree" >good &&
761 test_must_fail git commit-graph write --stdin-commits \
762 <good 2>test_err &&
763 test_i18ngrep "unable to parse commit" test_err
764 )
765'
766
767# We test the overflow-related code with the following repo history:
768#
769# 4:F - 5:N - 6:U
770# / \
771# 1:U - 2:N - 3:U M:N
772# \ /
773# 7:N - 8:F - 9:N
774#
775# Here the commits denoted by U have committer date of zero seconds
776# since Unix epoch, the commits denoted by N have committer date
777# starting from 1112354055 seconds since Unix epoch (default committer
778# date for the test suite), and the commits denoted by F have committer
779# date of (2 ^ 31 - 2) seconds since Unix epoch.
780#
781# The largest offset observed is 2 ^ 31, just large enough to overflow.
782#
783
784test_expect_success 'set up and verify repo with generation data overflow chunk' '
785 objdir=".git/objects" &&
786 UNIX_EPOCH_ZERO="@0 +0000" &&
787 FUTURE_DATE="@2147483646 +0000" &&
788 cd "$TRASH_DIRECTORY" &&
789 mkdir repo &&
790 cd repo &&
791 git init &&
792 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
793 test_commit 2 &&
794 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
795 git commit-graph write --reachable &&
796 graph_read_expect 3 generation_data &&
797 test_commit --date "$FUTURE_DATE" 4 &&
798 test_commit 5 &&
799 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
800 git branch left &&
801 git reset --hard 3 &&
802 test_commit 7 &&
803 test_commit --date "$FUTURE_DATE" 8 &&
804 test_commit 9 &&
805 git branch right &&
806 git reset --hard 3 &&
807 test_merge M left right &&
808 git commit-graph write --reachable &&
809 graph_read_expect 10 "generation_data generation_data_overflow" &&
810 git commit-graph verify
811'
812
813graph_git_behavior 'generation data overflow chunk repo' repo left right
814
815test_done