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