]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5318-commit-graph.sh
builtin/bundle.c: let parse-options parse subcommands
[thirdparty/git.git] / t / t5318-commit-graph.sh
CommitLineData
f237c8b6
DS
1#!/bin/sh
2
3test_description='commit graph'
4. ./test-lib.sh
5
d5b873c8
GS
6GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
7
6d209a01 8test_expect_success 'usage' '
367c5f36 9 test_expect_code 129 git commit-graph write blah 2>err &&
6d209a01
ÆAB
10 test_expect_code 129 git commit-graph write verify
11'
12
367c5f36
ÆAB
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
f237c8b6
DS
27test_expect_success 'setup full repo' '
28 mkdir full &&
29 cd "$TRASH_DIRECTORY/full" &&
30 git init &&
177722b3 31 git config core.commitGraph true &&
17925e06 32 objdir=".git/objects"
f237c8b6
DS
33'
34
1f9becae
TB
35test_expect_success POSIXPERM 'tweak umask for modebit tests' '
36 umask 022
37'
38
283e68c7
DS
39test_expect_success 'verify graph with no graph file' '
40 cd "$TRASH_DIRECTORY/full" &&
41 git commit-graph verify
42'
43
f237c8b6
DS
44test_expect_success 'write graph with no packs' '
45 cd "$TRASH_DIRECTORY/full" &&
1793280e
TB
46 git commit-graph write --object-dir $objdir &&
47 test_path_is_missing $objdir/info/commit-graph
f237c8b6
DS
48'
49
7c5c9b9c 50test_expect_success 'exit with correct error on bad input to --stdin-packs' '
e103f727
DS
51 cd "$TRASH_DIRECTORY/full" &&
52 echo doesnotexist >in &&
9916073b 53 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
e103f727
DS
54 test_i18ngrep "error adding pack" stderr
55'
56
f237c8b6
DS
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 &&
d0fd9931 62 git branch commits/$i || return 1
f237c8b6
DS
63 done &&
64 git repack
65'
66
17925e06 67. "$TEST_DIRECTORY"/lib-commit-graph.sh
177722b3
DS
68
69graph_git_behavior 'no graph' full commits/3 commits/1
70
1f1304d4
TB
71test_expect_success 'exit with correct error on bad input to --stdin-commits' '
72 cd "$TRASH_DIRECTORY/full" &&
2f00c355
TB
73 # invalid, non-hex OID
74 echo HEAD >in &&
75 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
1f1304d4 76 test_i18ngrep "unexpected non-hex object ID: HEAD" stderr &&
2f00c355
TB
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 &&
e8b63005 84 graph_read_expect 3 generation_data
1f1304d4
TB
85'
86
f237c8b6
DS
87test_expect_success 'write graph' '
88 cd "$TRASH_DIRECTORY/full" &&
0b918b75 89 git commit-graph write &&
2a2e32bd 90 test_path_is_file $objdir/info/commit-graph &&
e8b63005 91 graph_read_expect "3" generation_data
f237c8b6
DS
92'
93
1f9becae
TB
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
177722b3
DS
101graph_git_behavior 'graph exists' full commits/3 commits/1
102
f237c8b6
DS
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 &&
d0fd9931 109 git branch commits/$i || return 1
f237c8b6
DS
110 done &&
111 git reset --hard commits/2 &&
112 for i in $(test_seq 6 7)
113 do
114 test_commit $i &&
d0fd9931 115 git branch commits/$i || return 1
f237c8b6
DS
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
73716122
GS
129test_expect_success 'commit-graph write progress off for redirected stderr' '
130 cd "$TRASH_DIRECTORY/full" &&
131 git commit-graph write 2>err &&
6334c5ff 132 test_must_be_empty err
73716122
GS
133'
134
135test_expect_success 'commit-graph write force progress on for stderr' '
136 cd "$TRASH_DIRECTORY/full" &&
44a4693b 137 GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err &&
73716122
GS
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 &&
6334c5ff 144 test_must_be_empty err
73716122
GS
145'
146
94fbd914
TB
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
73716122
GS
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 &&
6334c5ff 171 test_must_be_empty err
73716122
GS
172'
173
174test_expect_success 'commit-graph verify force progress on for stderr' '
175 cd "$TRASH_DIRECTORY/full" &&
44a4693b 176 GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
73716122
GS
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 &&
6334c5ff 183 test_must_be_empty err
73716122
GS
184'
185
f237c8b6
DS
186# Current graph structure:
187#
188# __M3___
189# / | \
190# 3 M1 5 M2 7
191# |/ \|/ \|
192# 2 4 6
193# |___/____/
194# 1
195
f237c8b6
DS
196test_expect_success 'write graph with merges' '
197 cd "$TRASH_DIRECTORY/full" &&
198 git commit-graph write &&
2a2e32bd 199 test_path_is_file $objdir/info/commit-graph &&
e8b63005 200 graph_read_expect "10" "generation_data extra_edges"
f237c8b6
DS
201'
202
177722b3
DS
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
f237c8b6
DS
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 &&
4a3ed638 213 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
f237c8b6
DS
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
177722b3
DS
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
f237c8b6
DS
231test_expect_success 'write graph with new commit' '
232 cd "$TRASH_DIRECTORY/full" &&
233 git commit-graph write &&
2a2e32bd 234 test_path_is_file $objdir/info/commit-graph &&
e8b63005 235 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
236'
237
177722b3
DS
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
f237c8b6
DS
241test_expect_success 'write graph with nothing new' '
242 cd "$TRASH_DIRECTORY/full" &&
243 git commit-graph write &&
2a2e32bd 244 test_path_is_file $objdir/info/commit-graph &&
e8b63005 245 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
246'
247
177722b3
DS
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
049d51a2
DS
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 &&
e8b63005 255 graph_read_expect "9" "generation_data extra_edges"
049d51a2
DS
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
3d5df01b
DS
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 &&
e8b63005 268 graph_read_expect "6" "generation_data"
3d5df01b
DS
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
7547b95b
DS
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 &&
e8b63005 278 graph_read_expect "10" "generation_data extra_edges"
7547b95b
DS
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
59fb8770
DS
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 &&
e8b63005 288 graph_read_expect "11" "generation_data extra_edges"
59fb8770
DS
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
f237c8b6
DS
294test_expect_success 'setup bare repo' '
295 cd "$TRASH_DIRECTORY" &&
296 git clone --bare --no-local full bare &&
297 cd bare &&
177722b3 298 git config core.commitGraph true &&
f237c8b6
DS
299 baredir="./objects"
300'
301
177722b3
DS
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
f237c8b6
DS
305test_expect_success 'write graph in bare repo' '
306 cd "$TRASH_DIRECTORY/bare" &&
307 git commit-graph write &&
2a2e32bd 308 test_path_is_file $baredir/info/commit-graph &&
e8b63005 309 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
310'
311
177722b3
DS
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
7adf5266
DS
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
d5d5d7b6
DS
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 &&
eb7cc5bc 333 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
d5d5d7b6 334 git commit-graph write --reachable &&
eb7cc5bc 335 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
d5d5d7b6
DS
336'
337
d6538246
DS
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 &&
095d112f 347 graph_git_two_modes "commit-graph verify" &&
d6538246
DS
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
20fd6d57
DS
361test_expect_success 'commit grafts invalidate commit-graph' '
362 cd "$TRASH_DIRECTORY" &&
363 test_when_finished rm -rf graft &&
93e02b6e 364 git clone --template= full graft &&
20fd6d57
DS
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) &&
93e02b6e 371 mkdir .git/info &&
20fd6d57
DS
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
665d70ad
DS
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
75979d94 429test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow chunk' '
90cb1c47
DS
430 cd "$TRASH_DIRECTORY/full" &&
431 UNIX_EPOCH_ZERO="@0 +0000" &&
75979d94 432 FUTURE_DATE="@4147483646 +0000" &&
90cb1c47
DS
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
d9b9f8a6
DS
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
283e68c7
DS
456test_expect_success 'git commit-graph verify' '
457 cd "$TRASH_DIRECTORY/full" &&
702110aa 458 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
e8b63005 459 git commit-graph verify >output &&
3b0199d4 460 graph_read_expect 9 extra_edges 1
283e68c7
DS
461'
462
96af91d4 463NUM_COMMITS=9
437787ae 464NUM_OCTOPUS_EDGES=2
ae0c89d4 465HASH_LEN="$(test_oid rawsz)"
d9b9f8a6
DS
466GRAPH_BYTE_VERSION=4
467GRAPH_BYTE_HASH=5
2bd0365f
DS
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))
9bda8467
DS
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))
96af91d4 483GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
2e3c0737
DS
484GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
485GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
53614b13
DS
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))
1373e547 489GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
88968ebf 490GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
437787ae
DS
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))
41df0e30 495GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
d9b9f8a6 496
f6761faa
ÆAB
497corrupt_graph_setup() {
498 cd "$TRASH_DIRECTORY/full" &&
499 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
1f9becae
TB
500 cp $objdir/info/commit-graph commit-graph-backup &&
501 chmod u+w $objdir/info/commit-graph
f6761faa
ÆAB
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 &&
2ac138d5 508 test_i18ngrep "$grepstr" err &&
43d35618
ÆAB
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 &&
7b671f8c 514 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
1f9becae 515 chmod u+w $objdir/info/commit-graph &&
43d35618 516 git commit-graph verify
f6761faa
ÆAB
517}
518
d2b86fba 519# usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
d9b9f8a6 520# Manipulates the commit-graph file at the position
d2b86fba
JS
521# by inserting the data, optionally zeroing the file
522# starting at <zero_pos>, then runs 'git commit-graph verify'
d9b9f8a6
DS
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
f6761faa 529 corrupt_graph_setup &&
d2b86fba
JS
530 orig_size=$(wc -c < $objdir/info/commit-graph) &&
531 zero_pos=${4:-${orig_size}} &&
d9b9f8a6 532 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
b9cc4056 533 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
f3ad2bf4 534 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
f6761faa
ÆAB
535 corrupt_graph_verify "$grepstr"
536
d9b9f8a6
DS
537}
538
7b8ce9c6
ÆAB
539test_expect_success POSIXPERM,SANITY 'detect permission problem' '
540 corrupt_graph_setup &&
541 chmod 000 $objdir/info/commit-graph &&
43d35618 542 corrupt_graph_verify "Could not open" "no-copy"
7b8ce9c6
ÆAB
543'
544
945944ca
ÆAB
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
d9b9f8a6
DS
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' '
1d86c8f0 562 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
d9b9f8a6
DS
563 "hash version"
564'
565
2bd0365f 566test_expect_success 'detect low chunk count' '
cb9daf16 567 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
2692c2f6 568 "final chunk has non-zero id"
2bd0365f
DS
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
9bda8467
DS
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
96af91d4
DS
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
2e3c0737
DS
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
53614b13
DS
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
1373e547
DS
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
88968ebf
DS
636test_expect_success 'detect incorrect commit date' '
637 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
638 "commit date"
639'
640
437787ae
DS
641test_expect_success 'detect incorrect parent for octopus merge' '
642 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
643 "invalid parent"
644'
645
41df0e30
DS
646test_expect_success 'detect invalid checksum hash' '
647 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
648 "incorrect checksum"
649'
650
d2b86fba
JS
651test_expect_success 'detect incorrect chunk count' '
652 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
2ad4f1a7
SG
653 "commit-graph file is too small to hold [0-9]* chunks" \
654 $GRAPH_CHUNK_LOOKUP_OFFSET
d2b86fba
JS
655'
656
f30e4d85 657test_expect_success 'git fsck (checks commit-graph when config set to true)' '
e0fd51e1
DS
658 cd "$TRASH_DIRECTORY/full" &&
659 git fsck &&
660 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
661 "incorrect checksum" &&
43d35618 662 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
f30e4d85
GC
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 &&
e0fd51e1
DS
684 test_must_fail git fsck
685'
686
dade47c0
JT
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 &&
3c458630
SG
700 {
701 git -C repo log --pretty=format:"%ct " -1 &&
702 git -C repo rev-parse one
703 } >expect &&
dade47c0
JT
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 &&
3c458630 708 git -C repo log --pretty="%ct" -1 one >expect &&
dade47c0
JT
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 &&
3c458630 715 git -C repo rev-parse two^{tree} >expect &&
dade47c0
JT
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 &&
3c458630 720 git -C repo rev-parse one^{tree} >expect &&
dade47c0
JT
721 test_cmp expect actual
722'
723
16749b8d 724test_expect_success 'corrupt commit-graph write (broken parent)' '
23424ea7
TB
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
48c10cc0 732 parent $ZERO_OID
23424ea7
TB
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
806278de 746test_expect_success 'corrupt commit-graph write (missing tree)' '
23424ea7
TB
747 rm -rf repo &&
748 git init repo &&
749 (
750 cd repo &&
751 tree="$(git mktree </dev/null)" &&
752 cat >broken <<-EOF &&
48c10cc0 753 parent $ZERO_OID
23424ea7
TB
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 &&
228c78fb 763 test_i18ngrep "unable to parse commit" test_err
23424ea7
TB
764 )
765'
766
e8b63005
AK
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" &&
e8b63005
AK
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
f237c8b6 815test_done