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