]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5318-commit-graph.sh
test-read-graph: include extra post-parse info
[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 &&
665d70ad
DS
32 objdir=".git/objects" &&
33
34 test_oid_cache <<-EOF
35 oid_version sha1:1
36 oid_version sha256:2
37 EOF
f237c8b6
DS
38'
39
1f9becae
TB
40test_expect_success POSIXPERM 'tweak umask for modebit tests' '
41 umask 022
42'
43
283e68c7
DS
44test_expect_success 'verify graph with no graph file' '
45 cd "$TRASH_DIRECTORY/full" &&
46 git commit-graph verify
47'
48
f237c8b6
DS
49test_expect_success 'write graph with no packs' '
50 cd "$TRASH_DIRECTORY/full" &&
1793280e
TB
51 git commit-graph write --object-dir $objdir &&
52 test_path_is_missing $objdir/info/commit-graph
f237c8b6
DS
53'
54
7c5c9b9c 55test_expect_success 'exit with correct error on bad input to --stdin-packs' '
e103f727
DS
56 cd "$TRASH_DIRECTORY/full" &&
57 echo doesnotexist >in &&
9916073b 58 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
e103f727
DS
59 test_i18ngrep "error adding pack" stderr
60'
61
f237c8b6
DS
62test_expect_success 'create commits and repack' '
63 cd "$TRASH_DIRECTORY/full" &&
64 for i in $(test_seq 3)
65 do
66 test_commit $i &&
d0fd9931 67 git branch commits/$i || return 1
f237c8b6
DS
68 done &&
69 git repack
70'
71
177722b3 72graph_git_two_modes() {
3247919a
ÆAB
73 git -c core.commitGraph=true $1 >output &&
74 git -c core.commitGraph=false $1 >expect &&
dcbaa0b3 75 test_cmp expect output
177722b3
DS
76}
77
78graph_git_behavior() {
79 MSG=$1
80 DIR=$2
81 BRANCH=$3
82 COMPARE=$4
83 test_expect_success "check normal git operations: $MSG" '
84 cd "$TRASH_DIRECTORY/$DIR" &&
85 graph_git_two_modes "log --oneline $BRANCH" &&
86 graph_git_two_modes "log --topo-order $BRANCH" &&
87 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
88 graph_git_two_modes "branch -vv" &&
89 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
90 '
91}
92
93graph_git_behavior 'no graph' full commits/3 commits/1
94
2a2e32bd
DS
95graph_read_expect() {
96 OPTIONAL=""
97 NUM_CHUNKS=3
e8b63005 98 if test ! -z "$2"
2a2e32bd
DS
99 then
100 OPTIONAL=" $2"
101 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
102 fi
103 cat >expect <<- EOF
665d70ad 104 header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
2a2e32bd
DS
105 num_commits: $1
106 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
c78c7a95 107 options:
2a2e32bd 108 EOF
4bd0593e 109 test-tool read-graph >output &&
2a2e32bd
DS
110 test_cmp expect output
111}
112
1f1304d4
TB
113test_expect_success 'exit with correct error on bad input to --stdin-commits' '
114 cd "$TRASH_DIRECTORY/full" &&
2f00c355
TB
115 # invalid, non-hex OID
116 echo HEAD >in &&
117 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
1f1304d4 118 test_i18ngrep "unexpected non-hex object ID: HEAD" stderr &&
2f00c355
TB
119 # non-existent OID
120 echo $ZERO_OID >in &&
121 test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr &&
122 test_i18ngrep "invalid object" stderr &&
123 # valid commit and tree OID
124 git rev-parse HEAD HEAD^{tree} >in &&
125 git commit-graph write --stdin-commits <in &&
e8b63005 126 graph_read_expect 3 generation_data
1f1304d4
TB
127'
128
f237c8b6
DS
129test_expect_success 'write graph' '
130 cd "$TRASH_DIRECTORY/full" &&
0b918b75 131 git commit-graph write &&
2a2e32bd 132 test_path_is_file $objdir/info/commit-graph &&
e8b63005 133 graph_read_expect "3" generation_data
f237c8b6
DS
134'
135
1f9becae
TB
136test_expect_success POSIXPERM 'write graph has correct permissions' '
137 test_path_is_file $objdir/info/commit-graph &&
138 echo "-r--r--r--" >expect &&
139 test_modebits $objdir/info/commit-graph >actual &&
140 test_cmp expect actual
141'
142
177722b3
DS
143graph_git_behavior 'graph exists' full commits/3 commits/1
144
f237c8b6
DS
145test_expect_success 'Add more commits' '
146 cd "$TRASH_DIRECTORY/full" &&
147 git reset --hard commits/1 &&
148 for i in $(test_seq 4 5)
149 do
150 test_commit $i &&
d0fd9931 151 git branch commits/$i || return 1
f237c8b6
DS
152 done &&
153 git reset --hard commits/2 &&
154 for i in $(test_seq 6 7)
155 do
156 test_commit $i &&
d0fd9931 157 git branch commits/$i || return 1
f237c8b6
DS
158 done &&
159 git reset --hard commits/2 &&
160 git merge commits/4 &&
161 git branch merge/1 &&
162 git reset --hard commits/4 &&
163 git merge commits/6 &&
164 git branch merge/2 &&
165 git reset --hard commits/3 &&
166 git merge commits/5 commits/7 &&
167 git branch merge/3 &&
168 git repack
169'
170
73716122
GS
171test_expect_success 'commit-graph write progress off for redirected stderr' '
172 cd "$TRASH_DIRECTORY/full" &&
173 git commit-graph write 2>err &&
6334c5ff 174 test_must_be_empty err
73716122
GS
175'
176
177test_expect_success 'commit-graph write force progress on for stderr' '
178 cd "$TRASH_DIRECTORY/full" &&
44a4693b 179 GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err &&
73716122
GS
180 test_file_not_empty err
181'
182
183test_expect_success 'commit-graph write with the --no-progress option' '
184 cd "$TRASH_DIRECTORY/full" &&
185 git commit-graph write --no-progress 2>err &&
6334c5ff 186 test_must_be_empty err
73716122
GS
187'
188
94fbd914
TB
189test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
190 cd "$TRASH_DIRECTORY/full" &&
191 git rev-parse commits/5 >in &&
192 git commit-graph write --stdin-commits <in 2>err &&
193 test_must_be_empty err
194'
195
196test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
197 cd "$TRASH_DIRECTORY/full" &&
198 git rev-parse commits/5 >in &&
199 GIT_PROGRESS_DELAY=0 git commit-graph write --stdin-commits --progress <in 2>err &&
200 test_i18ngrep "Collecting commits from input" err
201'
202
203test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
204 cd "$TRASH_DIRECTORY/full" &&
205 git rev-parse commits/5 >in &&
206 git commit-graph write --stdin-commits --no-progress <in 2>err &&
207 test_must_be_empty err
73716122
GS
208'
209
210test_expect_success 'commit-graph verify progress off for redirected stderr' '
211 cd "$TRASH_DIRECTORY/full" &&
212 git commit-graph verify 2>err &&
6334c5ff 213 test_must_be_empty err
73716122
GS
214'
215
216test_expect_success 'commit-graph verify force progress on for stderr' '
217 cd "$TRASH_DIRECTORY/full" &&
44a4693b 218 GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err &&
73716122
GS
219 test_file_not_empty err
220'
221
222test_expect_success 'commit-graph verify with the --no-progress option' '
223 cd "$TRASH_DIRECTORY/full" &&
224 git commit-graph verify --no-progress 2>err &&
6334c5ff 225 test_must_be_empty err
73716122
GS
226'
227
f237c8b6
DS
228# Current graph structure:
229#
230# __M3___
231# / | \
232# 3 M1 5 M2 7
233# |/ \|/ \|
234# 2 4 6
235# |___/____/
236# 1
237
f237c8b6
DS
238test_expect_success 'write graph with merges' '
239 cd "$TRASH_DIRECTORY/full" &&
240 git commit-graph write &&
2a2e32bd 241 test_path_is_file $objdir/info/commit-graph &&
e8b63005 242 graph_read_expect "10" "generation_data extra_edges"
f237c8b6
DS
243'
244
177722b3
DS
245graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
246graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
247graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
248
f237c8b6
DS
249test_expect_success 'Add one more commit' '
250 cd "$TRASH_DIRECTORY/full" &&
251 test_commit 8 &&
252 git branch commits/8 &&
253 ls $objdir/pack | grep idx >existing-idx &&
254 git repack &&
4a3ed638 255 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
f237c8b6
DS
256'
257
258# Current graph structure:
259#
260# 8
261# |
262# __M3___
263# / | \
264# 3 M1 5 M2 7
265# |/ \|/ \|
266# 2 4 6
267# |___/____/
268# 1
269
177722b3
DS
270graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
271graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
272
f237c8b6
DS
273test_expect_success 'write graph with new commit' '
274 cd "$TRASH_DIRECTORY/full" &&
275 git commit-graph write &&
2a2e32bd 276 test_path_is_file $objdir/info/commit-graph &&
e8b63005 277 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
278'
279
177722b3
DS
280graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
281graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
282
f237c8b6
DS
283test_expect_success 'write graph with nothing new' '
284 cd "$TRASH_DIRECTORY/full" &&
285 git commit-graph write &&
2a2e32bd 286 test_path_is_file $objdir/info/commit-graph &&
e8b63005 287 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
288'
289
177722b3
DS
290graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
291graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
292
049d51a2
DS
293test_expect_success 'build graph from latest pack with closure' '
294 cd "$TRASH_DIRECTORY/full" &&
295 cat new-idx | git commit-graph write --stdin-packs &&
296 test_path_is_file $objdir/info/commit-graph &&
e8b63005 297 graph_read_expect "9" "generation_data extra_edges"
049d51a2
DS
298'
299
300graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
301graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
302
3d5df01b
DS
303test_expect_success 'build graph from commits with closure' '
304 cd "$TRASH_DIRECTORY/full" &&
305 git tag -a -m "merge" tag/merge merge/2 &&
306 git rev-parse tag/merge >commits-in &&
307 git rev-parse merge/1 >>commits-in &&
308 cat commits-in | git commit-graph write --stdin-commits &&
309 test_path_is_file $objdir/info/commit-graph &&
e8b63005 310 graph_read_expect "6" "generation_data"
3d5df01b
DS
311'
312
313graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
314graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
315
7547b95b
DS
316test_expect_success 'build graph from commits with append' '
317 cd "$TRASH_DIRECTORY/full" &&
318 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
319 test_path_is_file $objdir/info/commit-graph &&
e8b63005 320 graph_read_expect "10" "generation_data extra_edges"
7547b95b
DS
321'
322
323graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
324graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
325
59fb8770
DS
326test_expect_success 'build graph using --reachable' '
327 cd "$TRASH_DIRECTORY/full" &&
328 git commit-graph write --reachable &&
329 test_path_is_file $objdir/info/commit-graph &&
e8b63005 330 graph_read_expect "11" "generation_data extra_edges"
59fb8770
DS
331'
332
333graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
334graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
335
f237c8b6
DS
336test_expect_success 'setup bare repo' '
337 cd "$TRASH_DIRECTORY" &&
338 git clone --bare --no-local full bare &&
339 cd bare &&
177722b3 340 git config core.commitGraph true &&
f237c8b6
DS
341 baredir="./objects"
342'
343
177722b3
DS
344graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
345graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
346
f237c8b6
DS
347test_expect_success 'write graph in bare repo' '
348 cd "$TRASH_DIRECTORY/bare" &&
349 git commit-graph write &&
2a2e32bd 350 test_path_is_file $baredir/info/commit-graph &&
e8b63005 351 graph_read_expect "11" "generation_data extra_edges"
f237c8b6
DS
352'
353
177722b3
DS
354graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
355graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
356
7adf5266
DS
357test_expect_success 'perform fast-forward merge in full repo' '
358 cd "$TRASH_DIRECTORY/full" &&
359 git checkout -b merge-5-to-8 commits/5 &&
360 git merge commits/8 &&
361 git show-ref -s merge-5-to-8 >output &&
362 git show-ref -s commits/8 >expect &&
363 test_cmp expect output
364'
365
d5d5d7b6
DS
366test_expect_success 'check that gc computes commit-graph' '
367 cd "$TRASH_DIRECTORY/full" &&
368 git commit --allow-empty -m "blank" &&
369 git commit-graph write --reachable &&
370 cp $objdir/info/commit-graph commit-graph-before-gc &&
371 git reset --hard HEAD~1 &&
372 git config gc.writeCommitGraph true &&
373 git gc &&
374 cp $objdir/info/commit-graph commit-graph-after-gc &&
eb7cc5bc 375 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
d5d5d7b6 376 git commit-graph write --reachable &&
eb7cc5bc 377 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
d5d5d7b6
DS
378'
379
d6538246
DS
380test_expect_success 'replace-objects invalidates commit-graph' '
381 cd "$TRASH_DIRECTORY" &&
382 test_when_finished rm -rf replace &&
383 git clone full replace &&
384 (
385 cd replace &&
386 git commit-graph write --reachable &&
387 test_path_is_file .git/objects/info/commit-graph &&
388 git replace HEAD~1 HEAD~2 &&
095d112f 389 graph_git_two_modes "commit-graph verify" &&
d6538246
DS
390 git -c core.commitGraph=false log >expect &&
391 git -c core.commitGraph=true log >actual &&
392 test_cmp expect actual &&
393 git commit-graph write --reachable &&
394 git -c core.commitGraph=false --no-replace-objects log >expect &&
395 git -c core.commitGraph=true --no-replace-objects log >actual &&
396 test_cmp expect actual &&
397 rm -rf .git/objects/info/commit-graph &&
398 git commit-graph write --reachable &&
399 test_path_is_file .git/objects/info/commit-graph
400 )
401'
402
20fd6d57
DS
403test_expect_success 'commit grafts invalidate commit-graph' '
404 cd "$TRASH_DIRECTORY" &&
405 test_when_finished rm -rf graft &&
406 git clone full graft &&
407 (
408 cd graft &&
409 git commit-graph write --reachable &&
410 test_path_is_file .git/objects/info/commit-graph &&
411 H1=$(git rev-parse --verify HEAD~1) &&
412 H3=$(git rev-parse --verify HEAD~3) &&
413 echo "$H1 $H3" >.git/info/grafts &&
414 git -c core.commitGraph=false log >expect &&
415 git -c core.commitGraph=true log >actual &&
416 test_cmp expect actual &&
417 git commit-graph write --reachable &&
418 git -c core.commitGraph=false --no-replace-objects log >expect &&
419 git -c core.commitGraph=true --no-replace-objects log >actual &&
420 test_cmp expect actual &&
421 rm -rf .git/objects/info/commit-graph &&
422 git commit-graph write --reachable &&
423 test_path_is_missing .git/objects/info/commit-graph
424 )
425'
426
427test_expect_success 'replace-objects invalidates commit-graph' '
428 cd "$TRASH_DIRECTORY" &&
429 test_when_finished rm -rf shallow &&
430 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
431 (
432 cd shallow &&
433 git commit-graph write --reachable &&
434 test_path_is_missing .git/objects/info/commit-graph &&
435 git fetch origin --unshallow &&
436 git commit-graph write --reachable &&
437 test_path_is_file .git/objects/info/commit-graph
438 )
439'
440
665d70ad
DS
441test_expect_success 'warn on improper hash version' '
442 git init --object-format=sha1 sha1 &&
443 (
444 cd sha1 &&
445 test_commit 1 &&
446 git commit-graph write --reachable &&
447 mv .git/objects/info/commit-graph ../cg-sha1
448 ) &&
449 git init --object-format=sha256 sha256 &&
450 (
451 cd sha256 &&
452 test_commit 1 &&
453 git commit-graph write --reachable &&
454 mv .git/objects/info/commit-graph ../cg-sha256
455 ) &&
456 (
457 cd sha1 &&
458 mv ../cg-sha256 .git/objects/info/commit-graph &&
459 git log -1 2>err &&
460 test_i18ngrep "commit-graph hash version 2 does not match version 1" err
461 ) &&
462 (
463 cd sha256 &&
464 mv ../cg-sha1 .git/objects/info/commit-graph &&
465 git log -1 2>err &&
466 test_i18ngrep "commit-graph hash version 1 does not match version 2" err
467 )
468'
469
bc50d6c9 470test_expect_success 'lower layers have overflow chunk' '
90cb1c47
DS
471 cd "$TRASH_DIRECTORY/full" &&
472 UNIX_EPOCH_ZERO="@0 +0000" &&
473 FUTURE_DATE="@2147483646 +0000" &&
474 rm -f .git/objects/info/commit-graph &&
475 test_commit --date "$FUTURE_DATE" future-1 &&
476 test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
477 git commit-graph write --reachable &&
478 test_commit --date "$FUTURE_DATE" future-2 &&
479 test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
480 git commit-graph write --reachable --split=no-merge &&
481 test_commit extra &&
482 git commit-graph write --reachable --split=no-merge &&
483 git commit-graph write --reachable &&
484 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
485 mv .git/objects/info/commit-graph commit-graph-upgraded &&
486 git commit-graph write --reachable &&
487 graph_read_expect 16 "generation_data generation_data_overflow extra_edges" &&
488 test_cmp .git/objects/info/commit-graph commit-graph-upgraded
489'
490
d9b9f8a6
DS
491# the verify tests below expect the commit-graph to contain
492# exactly the commits reachable from the commits/8 branch.
493# If the file changes the set of commits in the list, then the
494# offsets into the binary file will result in different edits
495# and the tests will likely break.
496
283e68c7
DS
497test_expect_success 'git commit-graph verify' '
498 cd "$TRASH_DIRECTORY/full" &&
702110aa 499 git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits &&
e8b63005
AK
500 git commit-graph verify >output &&
501 graph_read_expect 9 extra_edges
283e68c7
DS
502'
503
96af91d4 504NUM_COMMITS=9
437787ae 505NUM_OCTOPUS_EDGES=2
ae0c89d4 506HASH_LEN="$(test_oid rawsz)"
d9b9f8a6
DS
507GRAPH_BYTE_VERSION=4
508GRAPH_BYTE_HASH=5
2bd0365f
DS
509GRAPH_BYTE_CHUNK_COUNT=6
510GRAPH_CHUNK_LOOKUP_OFFSET=8
511GRAPH_CHUNK_LOOKUP_WIDTH=12
512GRAPH_CHUNK_LOOKUP_ROWS=5
513GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
514GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
515 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
516GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
517 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
9bda8467
DS
518GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
519 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
520GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
521GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
522GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
523GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
96af91d4 524GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
2e3c0737
DS
525GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
526GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
53614b13
DS
527GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
528GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
529GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
1373e547 530GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
88968ebf 531GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
437787ae
DS
532GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
533GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
534 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
535GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
41df0e30 536GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
d9b9f8a6 537
f6761faa
ÆAB
538corrupt_graph_setup() {
539 cd "$TRASH_DIRECTORY/full" &&
540 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
1f9becae
TB
541 cp $objdir/info/commit-graph commit-graph-backup &&
542 chmod u+w $objdir/info/commit-graph
f6761faa
ÆAB
543}
544
545corrupt_graph_verify() {
546 grepstr=$1
547 test_must_fail git commit-graph verify 2>test_err &&
548 grep -v "^+" test_err >err &&
2ac138d5 549 test_i18ngrep "$grepstr" err &&
43d35618
ÆAB
550 if test "$2" != "no-copy"
551 then
552 cp $objdir/info/commit-graph commit-graph-pre-write-test
553 fi &&
554 git status --short &&
7b671f8c 555 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write &&
1f9becae 556 chmod u+w $objdir/info/commit-graph &&
43d35618 557 git commit-graph verify
f6761faa
ÆAB
558}
559
d2b86fba 560# usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
d9b9f8a6 561# Manipulates the commit-graph file at the position
d2b86fba
JS
562# by inserting the data, optionally zeroing the file
563# starting at <zero_pos>, then runs 'git commit-graph verify'
d9b9f8a6
DS
564# and places the output in the file 'err'. Test 'err' for
565# the given string.
566corrupt_graph_and_verify() {
567 pos=$1
568 data="${2:-\0}"
569 grepstr=$3
f6761faa 570 corrupt_graph_setup &&
d2b86fba
JS
571 orig_size=$(wc -c < $objdir/info/commit-graph) &&
572 zero_pos=${4:-${orig_size}} &&
d9b9f8a6 573 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
b9cc4056 574 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
f3ad2bf4 575 test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
f6761faa
ÆAB
576 corrupt_graph_verify "$grepstr"
577
d9b9f8a6
DS
578}
579
7b8ce9c6
ÆAB
580test_expect_success POSIXPERM,SANITY 'detect permission problem' '
581 corrupt_graph_setup &&
582 chmod 000 $objdir/info/commit-graph &&
43d35618 583 corrupt_graph_verify "Could not open" "no-copy"
7b8ce9c6
ÆAB
584'
585
945944ca
ÆAB
586test_expect_success 'detect too small' '
587 corrupt_graph_setup &&
588 echo "a small graph" >$objdir/info/commit-graph &&
589 corrupt_graph_verify "too small"
590'
591
d9b9f8a6
DS
592test_expect_success 'detect bad signature' '
593 corrupt_graph_and_verify 0 "\0" \
594 "graph signature"
595'
596
597test_expect_success 'detect bad version' '
598 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
599 "graph version"
600'
601
602test_expect_success 'detect bad hash version' '
1d86c8f0 603 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
d9b9f8a6
DS
604 "hash version"
605'
606
2bd0365f 607test_expect_success 'detect low chunk count' '
cb9daf16 608 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
2692c2f6 609 "final chunk has non-zero id"
2bd0365f
DS
610'
611
612test_expect_success 'detect missing OID fanout chunk' '
613 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
614 "missing the OID Fanout chunk"
615'
616
617test_expect_success 'detect missing OID lookup chunk' '
618 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
619 "missing the OID Lookup chunk"
620'
621
622test_expect_success 'detect missing commit data chunk' '
623 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
624 "missing the Commit Data chunk"
625'
626
9bda8467
DS
627test_expect_success 'detect incorrect fanout' '
628 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
629 "fanout value"
630'
631
632test_expect_success 'detect incorrect fanout final value' '
633 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
634 "fanout value"
635'
636
637test_expect_success 'detect incorrect OID order' '
638 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
639 "incorrect OID order"
640'
641
96af91d4
DS
642test_expect_success 'detect OID not in object database' '
643 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
644 "from object database"
645'
646
2e3c0737
DS
647test_expect_success 'detect incorrect tree OID' '
648 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
649 "root tree OID for commit"
650'
651
53614b13
DS
652test_expect_success 'detect incorrect parent int-id' '
653 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
654 "invalid parent"
655'
656
657test_expect_success 'detect extra parent int-id' '
658 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
659 "is too long"
660'
661
662test_expect_success 'detect wrong parent' '
663 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
664 "commit-graph parent for"
665'
666
1373e547
DS
667test_expect_success 'detect incorrect generation number' '
668 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
669 "generation for commit"
670'
671
672test_expect_success 'detect incorrect generation number' '
673 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
674 "non-zero generation number"
675'
676
88968ebf
DS
677test_expect_success 'detect incorrect commit date' '
678 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
679 "commit date"
680'
681
437787ae
DS
682test_expect_success 'detect incorrect parent for octopus merge' '
683 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
684 "invalid parent"
685'
686
41df0e30
DS
687test_expect_success 'detect invalid checksum hash' '
688 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
689 "incorrect checksum"
690'
691
d2b86fba
JS
692test_expect_success 'detect incorrect chunk count' '
693 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
2ad4f1a7
SG
694 "commit-graph file is too small to hold [0-9]* chunks" \
695 $GRAPH_CHUNK_LOOKUP_OFFSET
d2b86fba
JS
696'
697
f30e4d85 698test_expect_success 'git fsck (checks commit-graph when config set to true)' '
e0fd51e1
DS
699 cd "$TRASH_DIRECTORY/full" &&
700 git fsck &&
701 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
702 "incorrect checksum" &&
43d35618 703 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
f30e4d85
GC
704 test_must_fail git -c core.commitGraph=true fsck
705'
706
707test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
708 cd "$TRASH_DIRECTORY/full" &&
709 git fsck &&
710 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
711 "incorrect checksum" &&
712 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
713 git -c core.commitGraph=false fsck
714'
715
716test_expect_success 'git fsck (checks commit-graph when config unset)' '
717 cd "$TRASH_DIRECTORY/full" &&
718 test_when_finished "git config core.commitGraph true" &&
719
720 git fsck &&
721 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
722 "incorrect checksum" &&
723 test_unconfig core.commitGraph &&
724 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
e0fd51e1
DS
725 test_must_fail git fsck
726'
727
dade47c0
JT
728test_expect_success 'setup non-the_repository tests' '
729 rm -rf repo &&
730 git init repo &&
731 test_commit -C repo one &&
732 test_commit -C repo two &&
733 git -C repo config core.commitGraph true &&
734 git -C repo rev-parse two | \
735 git -C repo commit-graph write --stdin-commits
736'
737
738test_expect_success 'parse_commit_in_graph works for non-the_repository' '
739 test-tool repository parse_commit_in_graph \
740 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
3c458630
SG
741 {
742 git -C repo log --pretty=format:"%ct " -1 &&
743 git -C repo rev-parse one
744 } >expect &&
dade47c0
JT
745 test_cmp expect actual &&
746
747 test-tool repository parse_commit_in_graph \
748 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
3c458630 749 git -C repo log --pretty="%ct" -1 one >expect &&
dade47c0
JT
750 test_cmp expect actual
751'
752
753test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
754 test-tool repository get_commit_tree_in_graph \
755 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
3c458630 756 git -C repo rev-parse two^{tree} >expect &&
dade47c0
JT
757 test_cmp expect actual &&
758
759 test-tool repository get_commit_tree_in_graph \
760 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
3c458630 761 git -C repo rev-parse one^{tree} >expect &&
dade47c0
JT
762 test_cmp expect actual
763'
764
16749b8d 765test_expect_success 'corrupt commit-graph write (broken parent)' '
23424ea7
TB
766 rm -rf repo &&
767 git init repo &&
768 (
769 cd repo &&
770 empty="$(git mktree </dev/null)" &&
771 cat >broken <<-EOF &&
772 tree $empty
48c10cc0 773 parent $ZERO_OID
23424ea7
TB
774 author whatever <whatever@example.com> 1234 -0000
775 committer whatever <whatever@example.com> 1234 -0000
776
777 broken commit
778 EOF
779 broken="$(git hash-object -w -t commit --literally broken)" &&
780 git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
781 test_must_fail git commit-graph write --stdin-commits \
782 <good 2>test_err &&
783 test_i18ngrep "unable to parse commit" test_err
784 )
785'
786
806278de 787test_expect_success 'corrupt commit-graph write (missing tree)' '
23424ea7
TB
788 rm -rf repo &&
789 git init repo &&
790 (
791 cd repo &&
792 tree="$(git mktree </dev/null)" &&
793 cat >broken <<-EOF &&
48c10cc0 794 parent $ZERO_OID
23424ea7
TB
795 author whatever <whatever@example.com> 1234 -0000
796 committer whatever <whatever@example.com> 1234 -0000
797
798 broken commit
799 EOF
800 broken="$(git hash-object -w -t commit --literally broken)" &&
801 git commit-tree -p "$broken" -m "good" "$tree" >good &&
802 test_must_fail git commit-graph write --stdin-commits \
803 <good 2>test_err &&
228c78fb 804 test_i18ngrep "unable to parse commit" test_err
23424ea7
TB
805 )
806'
807
e8b63005
AK
808# We test the overflow-related code with the following repo history:
809#
810# 4:F - 5:N - 6:U
811# / \
812# 1:U - 2:N - 3:U M:N
813# \ /
814# 7:N - 8:F - 9:N
815#
816# Here the commits denoted by U have committer date of zero seconds
817# since Unix epoch, the commits denoted by N have committer date
818# starting from 1112354055 seconds since Unix epoch (default committer
819# date for the test suite), and the commits denoted by F have committer
820# date of (2 ^ 31 - 2) seconds since Unix epoch.
821#
822# The largest offset observed is 2 ^ 31, just large enough to overflow.
823#
824
825test_expect_success 'set up and verify repo with generation data overflow chunk' '
826 objdir=".git/objects" &&
827 UNIX_EPOCH_ZERO="@0 +0000" &&
828 FUTURE_DATE="@2147483646 +0000" &&
829 test_oid_cache <<-EOF &&
830 oid_version sha1:1
831 oid_version sha256:2
832 EOF
833 cd "$TRASH_DIRECTORY" &&
834 mkdir repo &&
835 cd repo &&
836 git init &&
837 test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
838 test_commit 2 &&
839 test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
840 git commit-graph write --reachable &&
841 graph_read_expect 3 generation_data &&
842 test_commit --date "$FUTURE_DATE" 4 &&
843 test_commit 5 &&
844 test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
845 git branch left &&
846 git reset --hard 3 &&
847 test_commit 7 &&
848 test_commit --date "$FUTURE_DATE" 8 &&
849 test_commit 9 &&
850 git branch right &&
851 git reset --hard 3 &&
852 test_merge M left right &&
853 git commit-graph write --reachable &&
854 graph_read_expect 10 "generation_data generation_data_overflow" &&
855 git commit-graph verify
856'
857
858graph_git_behavior 'generation data overflow chunk repo' repo left right
859
f237c8b6 860test_done