upload-pack: move keepalive to upload_pack_data
[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_line_count = 0 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_line_count = 0 err
156 '
157
158 test_expect_success 'commit-graph verify progress off for redirected stderr' '
159         cd "$TRASH_DIRECTORY/full" &&
160         git commit-graph verify 2>err &&
161         test_line_count = 0 err
162 '
163
164 test_expect_success 'commit-graph verify force progress on for stderr' '
165         cd "$TRASH_DIRECTORY/full" &&
166         GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
167         test_file_not_empty err
168 '
169
170 test_expect_success 'commit-graph verify with the --no-progress option' '
171         cd "$TRASH_DIRECTORY/full" &&
172         git commit-graph verify --no-progress 2>err &&
173         test_line_count = 0 err
174 '
175
176 # Current graph structure:
177 #
178 #   __M3___
179 #  /   |   \
180 # 3 M1 5 M2 7
181 # |/  \|/  \|
182 # 2    4    6
183 # |___/____/
184 # 1
185
186 test_expect_success 'write graph with merges' '
187         cd "$TRASH_DIRECTORY/full" &&
188         git commit-graph write &&
189         test_path_is_file $objdir/info/commit-graph &&
190         graph_read_expect "10" "extra_edges"
191 '
192
193 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
194 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
195 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
196
197 test_expect_success 'Add one more commit' '
198         cd "$TRASH_DIRECTORY/full" &&
199         test_commit 8 &&
200         git branch commits/8 &&
201         ls $objdir/pack | grep idx >existing-idx &&
202         git repack &&
203         ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
204 '
205
206 # Current graph structure:
207 #
208 #      8
209 #      |
210 #   __M3___
211 #  /   |   \
212 # 3 M1 5 M2 7
213 # |/  \|/  \|
214 # 2    4    6
215 # |___/____/
216 # 1
217
218 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
219 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
220
221 test_expect_success 'write graph with new commit' '
222         cd "$TRASH_DIRECTORY/full" &&
223         git commit-graph write &&
224         test_path_is_file $objdir/info/commit-graph &&
225         graph_read_expect "11" "extra_edges"
226 '
227
228 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
229 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
230
231 test_expect_success 'write graph with nothing new' '
232         cd "$TRASH_DIRECTORY/full" &&
233         git commit-graph write &&
234         test_path_is_file $objdir/info/commit-graph &&
235         graph_read_expect "11" "extra_edges"
236 '
237
238 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
239 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
240
241 test_expect_success 'build graph from latest pack with closure' '
242         cd "$TRASH_DIRECTORY/full" &&
243         cat new-idx | git commit-graph write --stdin-packs &&
244         test_path_is_file $objdir/info/commit-graph &&
245         graph_read_expect "9" "extra_edges"
246 '
247
248 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
249 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
250
251 test_expect_success 'build graph from commits with closure' '
252         cd "$TRASH_DIRECTORY/full" &&
253         git tag -a -m "merge" tag/merge merge/2 &&
254         git rev-parse tag/merge >commits-in &&
255         git rev-parse merge/1 >>commits-in &&
256         cat commits-in | git commit-graph write --stdin-commits &&
257         test_path_is_file $objdir/info/commit-graph &&
258         graph_read_expect "6"
259 '
260
261 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
262 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
263
264 test_expect_success 'build graph from commits with append' '
265         cd "$TRASH_DIRECTORY/full" &&
266         git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
267         test_path_is_file $objdir/info/commit-graph &&
268         graph_read_expect "10" "extra_edges"
269 '
270
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
273
274 test_expect_success 'build graph using --reachable' '
275         cd "$TRASH_DIRECTORY/full" &&
276         git commit-graph write --reachable &&
277         test_path_is_file $objdir/info/commit-graph &&
278         graph_read_expect "11" "extra_edges"
279 '
280
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
283
284 test_expect_success 'setup bare repo' '
285         cd "$TRASH_DIRECTORY" &&
286         git clone --bare --no-local full bare &&
287         cd bare &&
288         git config core.commitGraph true &&
289         baredir="./objects"
290 '
291
292 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
293 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
294
295 test_expect_success 'write graph in bare repo' '
296         cd "$TRASH_DIRECTORY/bare" &&
297         git commit-graph write &&
298         test_path_is_file $baredir/info/commit-graph &&
299         graph_read_expect "11" "extra_edges"
300 '
301
302 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
303 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
304
305 test_expect_success 'perform fast-forward merge in full repo' '
306         cd "$TRASH_DIRECTORY/full" &&
307         git checkout -b merge-5-to-8 commits/5 &&
308         git merge commits/8 &&
309         git show-ref -s merge-5-to-8 >output &&
310         git show-ref -s commits/8 >expect &&
311         test_cmp expect output
312 '
313
314 test_expect_success 'check that gc computes commit-graph' '
315         cd "$TRASH_DIRECTORY/full" &&
316         git commit --allow-empty -m "blank" &&
317         git commit-graph write --reachable &&
318         cp $objdir/info/commit-graph commit-graph-before-gc &&
319         git reset --hard HEAD~1 &&
320         git config gc.writeCommitGraph true &&
321         git gc &&
322         cp $objdir/info/commit-graph commit-graph-after-gc &&
323         ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
324         git commit-graph write --reachable &&
325         test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
326 '
327
328 test_expect_success 'replace-objects invalidates commit-graph' '
329         cd "$TRASH_DIRECTORY" &&
330         test_when_finished rm -rf replace &&
331         git clone full replace &&
332         (
333                 cd replace &&
334                 git commit-graph write --reachable &&
335                 test_path_is_file .git/objects/info/commit-graph &&
336                 git replace HEAD~1 HEAD~2 &&
337                 git -c core.commitGraph=false log >expect &&
338                 git -c core.commitGraph=true log >actual &&
339                 test_cmp expect actual &&
340                 git commit-graph write --reachable &&
341                 git -c core.commitGraph=false --no-replace-objects log >expect &&
342                 git -c core.commitGraph=true --no-replace-objects log >actual &&
343                 test_cmp expect actual &&
344                 rm -rf .git/objects/info/commit-graph &&
345                 git commit-graph write --reachable &&
346                 test_path_is_file .git/objects/info/commit-graph
347         )
348 '
349
350 test_expect_success 'commit grafts invalidate commit-graph' '
351         cd "$TRASH_DIRECTORY" &&
352         test_when_finished rm -rf graft &&
353         git clone full graft &&
354         (
355                 cd graft &&
356                 git commit-graph write --reachable &&
357                 test_path_is_file .git/objects/info/commit-graph &&
358                 H1=$(git rev-parse --verify HEAD~1) &&
359                 H3=$(git rev-parse --verify HEAD~3) &&
360                 echo "$H1 $H3" >.git/info/grafts &&
361                 git -c core.commitGraph=false log >expect &&
362                 git -c core.commitGraph=true log >actual &&
363                 test_cmp expect actual &&
364                 git commit-graph write --reachable &&
365                 git -c core.commitGraph=false --no-replace-objects log >expect &&
366                 git -c core.commitGraph=true --no-replace-objects log >actual &&
367                 test_cmp expect actual &&
368                 rm -rf .git/objects/info/commit-graph &&
369                 git commit-graph write --reachable &&
370                 test_path_is_missing .git/objects/info/commit-graph
371         )
372 '
373
374 test_expect_success 'replace-objects invalidates commit-graph' '
375         cd "$TRASH_DIRECTORY" &&
376         test_when_finished rm -rf shallow &&
377         git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
378         (
379                 cd shallow &&
380                 git commit-graph write --reachable &&
381                 test_path_is_missing .git/objects/info/commit-graph &&
382                 git fetch origin --unshallow &&
383                 git commit-graph write --reachable &&
384                 test_path_is_file .git/objects/info/commit-graph
385         )
386 '
387
388 # the verify tests below expect the commit-graph to contain
389 # exactly the commits reachable from the commits/8 branch.
390 # If the file changes the set of commits in the list, then the
391 # offsets into the binary file will result in different edits
392 # and the tests will likely break.
393
394 test_expect_success 'git commit-graph verify' '
395         cd "$TRASH_DIRECTORY/full" &&
396         git rev-parse commits/8 | git commit-graph write --stdin-commits &&
397         git commit-graph verify >output
398 '
399
400 NUM_COMMITS=9
401 NUM_OCTOPUS_EDGES=2
402 HASH_LEN="$(test_oid rawsz)"
403 GRAPH_BYTE_VERSION=4
404 GRAPH_BYTE_HASH=5
405 GRAPH_BYTE_CHUNK_COUNT=6
406 GRAPH_CHUNK_LOOKUP_OFFSET=8
407 GRAPH_CHUNK_LOOKUP_WIDTH=12
408 GRAPH_CHUNK_LOOKUP_ROWS=5
409 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
410 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
411                             1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
412 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
413                              2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
414 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
415                        $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
416 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
417 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
418 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
419 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
420 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
421 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
422 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
423 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
424 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
425 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
426 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
427 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
428 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
429 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
430                              $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
431 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
432 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
433
434 corrupt_graph_setup() {
435         cd "$TRASH_DIRECTORY/full" &&
436         test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
437         cp $objdir/info/commit-graph commit-graph-backup &&
438         chmod u+w $objdir/info/commit-graph
439 }
440
441 corrupt_graph_verify() {
442         grepstr=$1
443         test_must_fail git commit-graph verify 2>test_err &&
444         grep -v "^+" test_err >err &&
445         test_i18ngrep "$grepstr" err &&
446         if test "$2" != "no-copy"
447         then
448                 cp $objdir/info/commit-graph commit-graph-pre-write-test
449         fi &&
450         git status --short &&
451         GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD=true git commit-graph write &&
452         chmod u+w $objdir/info/commit-graph &&
453         git commit-graph verify
454 }
455
456 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
457 # Manipulates the commit-graph file at the position
458 # by inserting the data, optionally zeroing the file
459 # starting at <zero_pos>, then runs 'git commit-graph verify'
460 # and places the output in the file 'err'. Test 'err' for
461 # the given string.
462 corrupt_graph_and_verify() {
463         pos=$1
464         data="${2:-\0}"
465         grepstr=$3
466         corrupt_graph_setup &&
467         orig_size=$(wc -c < $objdir/info/commit-graph) &&
468         zero_pos=${4:-${orig_size}} &&
469         printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
470         dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
471         generate_zero_bytes $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
472         corrupt_graph_verify "$grepstr"
473
474 }
475
476 test_expect_success POSIXPERM,SANITY 'detect permission problem' '
477         corrupt_graph_setup &&
478         chmod 000 $objdir/info/commit-graph &&
479         corrupt_graph_verify "Could not open" "no-copy"
480 '
481
482 test_expect_success 'detect too small' '
483         corrupt_graph_setup &&
484         echo "a small graph" >$objdir/info/commit-graph &&
485         corrupt_graph_verify "too small"
486 '
487
488 test_expect_success 'detect bad signature' '
489         corrupt_graph_and_verify 0 "\0" \
490                 "graph signature"
491 '
492
493 test_expect_success 'detect bad version' '
494         corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
495                 "graph version"
496 '
497
498 test_expect_success 'detect bad hash version' '
499         corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
500                 "hash version"
501 '
502
503 test_expect_success 'detect low chunk count' '
504         corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
505                 "missing the .* chunk"
506 '
507
508 test_expect_success 'detect missing OID fanout chunk' '
509         corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
510                 "missing the OID Fanout chunk"
511 '
512
513 test_expect_success 'detect missing OID lookup chunk' '
514         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
515                 "missing the OID Lookup chunk"
516 '
517
518 test_expect_success 'detect missing commit data chunk' '
519         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
520                 "missing the Commit Data chunk"
521 '
522
523 test_expect_success 'detect incorrect fanout' '
524         corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
525                 "fanout value"
526 '
527
528 test_expect_success 'detect incorrect fanout final value' '
529         corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
530                 "fanout value"
531 '
532
533 test_expect_success 'detect incorrect OID order' '
534         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
535                 "incorrect OID order"
536 '
537
538 test_expect_success 'detect OID not in object database' '
539         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
540                 "from object database"
541 '
542
543 test_expect_success 'detect incorrect tree OID' '
544         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
545                 "root tree OID for commit"
546 '
547
548 test_expect_success 'detect incorrect parent int-id' '
549         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
550                 "invalid parent"
551 '
552
553 test_expect_success 'detect extra parent int-id' '
554         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
555                 "is too long"
556 '
557
558 test_expect_success 'detect wrong parent' '
559         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
560                 "commit-graph parent for"
561 '
562
563 test_expect_success 'detect incorrect generation number' '
564         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
565                 "generation for commit"
566 '
567
568 test_expect_success 'detect incorrect generation number' '
569         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
570                 "non-zero generation number"
571 '
572
573 test_expect_success 'detect incorrect commit date' '
574         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
575                 "commit date"
576 '
577
578 test_expect_success 'detect incorrect parent for octopus merge' '
579         corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
580                 "invalid parent"
581 '
582
583 test_expect_success 'detect invalid checksum hash' '
584         corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
585                 "incorrect checksum"
586 '
587
588 test_expect_success 'detect incorrect chunk count' '
589         corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
590                 "chunk lookup table entry missing" $GRAPH_CHUNK_LOOKUP_OFFSET
591 '
592
593 test_expect_success 'git fsck (checks commit-graph)' '
594         cd "$TRASH_DIRECTORY/full" &&
595         git fsck &&
596         corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
597                 "incorrect checksum" &&
598         cp commit-graph-pre-write-test $objdir/info/commit-graph &&
599         test_must_fail git fsck
600 '
601
602 test_expect_success 'setup non-the_repository tests' '
603         rm -rf repo &&
604         git init repo &&
605         test_commit -C repo one &&
606         test_commit -C repo two &&
607         git -C repo config core.commitGraph true &&
608         git -C repo rev-parse two | \
609                 git -C repo commit-graph write --stdin-commits
610 '
611
612 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
613         test-tool repository parse_commit_in_graph \
614                 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
615         {
616                 git -C repo log --pretty=format:"%ct " -1 &&
617                 git -C repo rev-parse one
618         } >expect &&
619         test_cmp expect actual &&
620
621         test-tool repository parse_commit_in_graph \
622                 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
623         git -C repo log --pretty="%ct" -1 one >expect &&
624         test_cmp expect actual
625 '
626
627 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
628         test-tool repository get_commit_tree_in_graph \
629                 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
630         git -C repo rev-parse two^{tree} >expect &&
631         test_cmp expect actual &&
632
633         test-tool repository get_commit_tree_in_graph \
634                 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
635         git -C repo rev-parse one^{tree} >expect &&
636         test_cmp expect actual
637 '
638
639 test_expect_success 'corrupt commit-graph write (broken parent)' '
640         rm -rf repo &&
641         git init repo &&
642         (
643                 cd repo &&
644                 empty="$(git mktree </dev/null)" &&
645                 cat >broken <<-EOF &&
646                 tree $empty
647                 parent $ZERO_OID
648                 author whatever <whatever@example.com> 1234 -0000
649                 committer whatever <whatever@example.com> 1234 -0000
650
651                 broken commit
652                 EOF
653                 broken="$(git hash-object -w -t commit --literally broken)" &&
654                 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
655                 test_must_fail git commit-graph write --stdin-commits \
656                         <good 2>test_err &&
657                 test_i18ngrep "unable to parse commit" test_err
658         )
659 '
660
661 test_expect_success 'corrupt commit-graph write (missing tree)' '
662         rm -rf repo &&
663         git init repo &&
664         (
665                 cd repo &&
666                 tree="$(git mktree </dev/null)" &&
667                 cat >broken <<-EOF &&
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" "$tree" >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_done