Git 2.23-rc1
[git.git] / t / t5318-commit-graph.sh
blob22cb9d66430410f726e821e906fa79587f43c3e8
1 #!/bin/sh
3 test_description='commit graph'
4 . ./test-lib.sh
6 test_expect_success 'setup full repo' '
7 mkdir full &&
8 cd "$TRASH_DIRECTORY/full" &&
9 git init &&
10 git config core.commitGraph true &&
11 objdir=".git/objects" &&
12 test_oid_init
15 test_expect_success 'verify graph with no graph file' '
16 cd "$TRASH_DIRECTORY/full" &&
17 git commit-graph verify
20 test_expect_success 'write graph with no packs' '
21 cd "$TRASH_DIRECTORY/full" &&
22 git commit-graph write --object-dir . &&
23 test_path_is_missing info/commit-graph
26 test_expect_success 'close with correct error on bad input' '
27 cd "$TRASH_DIRECTORY/full" &&
28 echo doesnotexist >in &&
29 { git commit-graph write --stdin-packs <in 2>stderr; ret=$?; } &&
30 test "$ret" = 1 &&
31 test_i18ngrep "error adding pack" stderr
34 test_expect_success 'create commits and repack' '
35 cd "$TRASH_DIRECTORY/full" &&
36 for i in $(test_seq 3)
38 test_commit $i &&
39 git branch commits/$i
40 done &&
41 git repack
44 graph_git_two_modes() {
45 git -c core.commitGraph=true $1 >output
46 git -c core.commitGraph=false $1 >expect
47 test_cmp expect output
50 graph_git_behavior() {
51 MSG=$1
52 DIR=$2
53 BRANCH=$3
54 COMPARE=$4
55 test_expect_success "check normal git operations: $MSG" '
56 cd "$TRASH_DIRECTORY/$DIR" &&
57 graph_git_two_modes "log --oneline $BRANCH" &&
58 graph_git_two_modes "log --topo-order $BRANCH" &&
59 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
60 graph_git_two_modes "branch -vv" &&
61 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
65 graph_git_behavior 'no graph' full commits/3 commits/1
67 graph_read_expect() {
68 OPTIONAL=""
69 NUM_CHUNKS=3
70 if test ! -z $2
71 then
72 OPTIONAL=" $2"
73 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
75 cat >expect <<- EOF
76 header: 43475048 1 1 $NUM_CHUNKS 0
77 num_commits: $1
78 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
79 EOF
80 git commit-graph read >output &&
81 test_cmp expect output
84 test_expect_success 'write graph' '
85 cd "$TRASH_DIRECTORY/full" &&
86 git commit-graph write &&
87 test_path_is_file $objdir/info/commit-graph &&
88 graph_read_expect "3"
91 graph_git_behavior 'graph exists' full commits/3 commits/1
93 test_expect_success 'Add more commits' '
94 cd "$TRASH_DIRECTORY/full" &&
95 git reset --hard commits/1 &&
96 for i in $(test_seq 4 5)
98 test_commit $i &&
99 git branch commits/$i
100 done &&
101 git reset --hard commits/2 &&
102 for i in $(test_seq 6 7)
104 test_commit $i &&
105 git branch commits/$i
106 done &&
107 git reset --hard commits/2 &&
108 git merge commits/4 &&
109 git branch merge/1 &&
110 git reset --hard commits/4 &&
111 git merge commits/6 &&
112 git branch merge/2 &&
113 git reset --hard commits/3 &&
114 git merge commits/5 commits/7 &&
115 git branch merge/3 &&
116 git repack
119 # Current graph structure:
121 # __M3___
122 # / | \
123 # 3 M1 5 M2 7
124 # |/ \|/ \|
125 # 2 4 6
126 # |___/____/
129 test_expect_success 'write graph with merges' '
130 cd "$TRASH_DIRECTORY/full" &&
131 git commit-graph write &&
132 test_path_is_file $objdir/info/commit-graph &&
133 graph_read_expect "10" "extra_edges"
136 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
137 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
138 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
140 test_expect_success 'Add one more commit' '
141 cd "$TRASH_DIRECTORY/full" &&
142 test_commit 8 &&
143 git branch commits/8 &&
144 ls $objdir/pack | grep idx >existing-idx &&
145 git repack &&
146 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
149 # Current graph structure:
153 # __M3___
154 # / | \
155 # 3 M1 5 M2 7
156 # |/ \|/ \|
157 # 2 4 6
158 # |___/____/
161 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
162 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
164 test_expect_success 'write graph with new commit' '
165 cd "$TRASH_DIRECTORY/full" &&
166 git commit-graph write &&
167 test_path_is_file $objdir/info/commit-graph &&
168 graph_read_expect "11" "extra_edges"
171 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
172 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
174 test_expect_success 'write graph with nothing new' '
175 cd "$TRASH_DIRECTORY/full" &&
176 git commit-graph write &&
177 test_path_is_file $objdir/info/commit-graph &&
178 graph_read_expect "11" "extra_edges"
181 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
182 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
184 test_expect_success 'build graph from latest pack with closure' '
185 cd "$TRASH_DIRECTORY/full" &&
186 cat new-idx | git commit-graph write --stdin-packs &&
187 test_path_is_file $objdir/info/commit-graph &&
188 graph_read_expect "9" "extra_edges"
191 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
192 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
194 test_expect_success 'build graph from commits with closure' '
195 cd "$TRASH_DIRECTORY/full" &&
196 git tag -a -m "merge" tag/merge merge/2 &&
197 git rev-parse tag/merge >commits-in &&
198 git rev-parse merge/1 >>commits-in &&
199 cat commits-in | git commit-graph write --stdin-commits &&
200 test_path_is_file $objdir/info/commit-graph &&
201 graph_read_expect "6"
204 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
205 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
207 test_expect_success 'build graph from commits with append' '
208 cd "$TRASH_DIRECTORY/full" &&
209 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
210 test_path_is_file $objdir/info/commit-graph &&
211 graph_read_expect "10" "extra_edges"
214 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
215 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
217 test_expect_success 'build graph using --reachable' '
218 cd "$TRASH_DIRECTORY/full" &&
219 git commit-graph write --reachable &&
220 test_path_is_file $objdir/info/commit-graph &&
221 graph_read_expect "11" "extra_edges"
224 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
225 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
227 test_expect_success 'setup bare repo' '
228 cd "$TRASH_DIRECTORY" &&
229 git clone --bare --no-local full bare &&
230 cd bare &&
231 git config core.commitGraph true &&
232 baredir="./objects"
235 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
236 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
238 test_expect_success 'write graph in bare repo' '
239 cd "$TRASH_DIRECTORY/bare" &&
240 git commit-graph write &&
241 test_path_is_file $baredir/info/commit-graph &&
242 graph_read_expect "11" "extra_edges"
245 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
246 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
248 test_expect_success 'perform fast-forward merge in full repo' '
249 cd "$TRASH_DIRECTORY/full" &&
250 git checkout -b merge-5-to-8 commits/5 &&
251 git merge commits/8 &&
252 git show-ref -s merge-5-to-8 >output &&
253 git show-ref -s commits/8 >expect &&
254 test_cmp expect output
257 test_expect_success 'check that gc computes commit-graph' '
258 cd "$TRASH_DIRECTORY/full" &&
259 git commit --allow-empty -m "blank" &&
260 git commit-graph write --reachable &&
261 cp $objdir/info/commit-graph commit-graph-before-gc &&
262 git reset --hard HEAD~1 &&
263 git config gc.writeCommitGraph true &&
264 git gc &&
265 cp $objdir/info/commit-graph commit-graph-after-gc &&
266 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
267 git commit-graph write --reachable &&
268 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
271 test_expect_success 'replace-objects invalidates commit-graph' '
272 cd "$TRASH_DIRECTORY" &&
273 test_when_finished rm -rf replace &&
274 git clone full replace &&
276 cd replace &&
277 git commit-graph write --reachable &&
278 test_path_is_file .git/objects/info/commit-graph &&
279 git replace HEAD~1 HEAD~2 &&
280 git -c core.commitGraph=false log >expect &&
281 git -c core.commitGraph=true log >actual &&
282 test_cmp expect actual &&
283 git commit-graph write --reachable &&
284 git -c core.commitGraph=false --no-replace-objects log >expect &&
285 git -c core.commitGraph=true --no-replace-objects log >actual &&
286 test_cmp expect actual &&
287 rm -rf .git/objects/info/commit-graph &&
288 git commit-graph write --reachable &&
289 test_path_is_file .git/objects/info/commit-graph
293 test_expect_success 'commit grafts invalidate commit-graph' '
294 cd "$TRASH_DIRECTORY" &&
295 test_when_finished rm -rf graft &&
296 git clone full graft &&
298 cd graft &&
299 git commit-graph write --reachable &&
300 test_path_is_file .git/objects/info/commit-graph &&
301 H1=$(git rev-parse --verify HEAD~1) &&
302 H3=$(git rev-parse --verify HEAD~3) &&
303 echo "$H1 $H3" >.git/info/grafts &&
304 git -c core.commitGraph=false log >expect &&
305 git -c core.commitGraph=true log >actual &&
306 test_cmp expect actual &&
307 git commit-graph write --reachable &&
308 git -c core.commitGraph=false --no-replace-objects log >expect &&
309 git -c core.commitGraph=true --no-replace-objects log >actual &&
310 test_cmp expect actual &&
311 rm -rf .git/objects/info/commit-graph &&
312 git commit-graph write --reachable &&
313 test_path_is_missing .git/objects/info/commit-graph
317 test_expect_success 'replace-objects invalidates commit-graph' '
318 cd "$TRASH_DIRECTORY" &&
319 test_when_finished rm -rf shallow &&
320 git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
322 cd shallow &&
323 git commit-graph write --reachable &&
324 test_path_is_missing .git/objects/info/commit-graph &&
325 git fetch origin --unshallow &&
326 git commit-graph write --reachable &&
327 test_path_is_file .git/objects/info/commit-graph
331 # the verify tests below expect the commit-graph to contain
332 # exactly the commits reachable from the commits/8 branch.
333 # If the file changes the set of commits in the list, then the
334 # offsets into the binary file will result in different edits
335 # and the tests will likely break.
337 test_expect_success 'git commit-graph verify' '
338 cd "$TRASH_DIRECTORY/full" &&
339 git rev-parse commits/8 | git commit-graph write --stdin-commits &&
340 git commit-graph verify >output
343 NUM_COMMITS=9
344 NUM_OCTOPUS_EDGES=2
345 HASH_LEN="$(test_oid rawsz)"
346 GRAPH_BYTE_VERSION=4
347 GRAPH_BYTE_HASH=5
348 GRAPH_BYTE_CHUNK_COUNT=6
349 GRAPH_CHUNK_LOOKUP_OFFSET=8
350 GRAPH_CHUNK_LOOKUP_WIDTH=12
351 GRAPH_CHUNK_LOOKUP_ROWS=5
352 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
353 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
354 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
355 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
356 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
357 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
358 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
359 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
360 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
361 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
362 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
363 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
364 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
365 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
366 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
367 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
368 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
369 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
370 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
371 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
372 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
373 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
374 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
375 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
377 corrupt_graph_setup() {
378 cd "$TRASH_DIRECTORY/full" &&
379 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
380 cp $objdir/info/commit-graph commit-graph-backup
383 corrupt_graph_verify() {
384 grepstr=$1
385 test_must_fail git commit-graph verify 2>test_err &&
386 grep -v "^+" test_err >err &&
387 test_i18ngrep "$grepstr" err &&
388 if test "$2" != "no-copy"
389 then
390 cp $objdir/info/commit-graph commit-graph-pre-write-test
391 fi &&
392 git status --short &&
393 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD=true git commit-graph write &&
394 git commit-graph verify
397 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
398 # Manipulates the commit-graph file at the position
399 # by inserting the data, optionally zeroing the file
400 # starting at <zero_pos>, then runs 'git commit-graph verify'
401 # and places the output in the file 'err'. Test 'err' for
402 # the given string.
403 corrupt_graph_and_verify() {
404 pos=$1
405 data="${2:-\0}"
406 grepstr=$3
407 corrupt_graph_setup &&
408 orig_size=$(wc -c < $objdir/info/commit-graph) &&
409 zero_pos=${4:-${orig_size}} &&
410 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
411 dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
412 generate_zero_bytes $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
413 corrupt_graph_verify "$grepstr"
417 test_expect_success POSIXPERM,SANITY 'detect permission problem' '
418 corrupt_graph_setup &&
419 chmod 000 $objdir/info/commit-graph &&
420 corrupt_graph_verify "Could not open" "no-copy"
423 test_expect_success 'detect too small' '
424 corrupt_graph_setup &&
425 echo "a small graph" >$objdir/info/commit-graph &&
426 corrupt_graph_verify "too small"
429 test_expect_success 'detect bad signature' '
430 corrupt_graph_and_verify 0 "\0" \
431 "graph signature"
434 test_expect_success 'detect bad version' '
435 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
436 "graph version"
439 test_expect_success 'detect bad hash version' '
440 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
441 "hash version"
444 test_expect_success 'detect low chunk count' '
445 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
446 "missing the .* chunk"
449 test_expect_success 'detect missing OID fanout chunk' '
450 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
451 "missing the OID Fanout chunk"
454 test_expect_success 'detect missing OID lookup chunk' '
455 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
456 "missing the OID Lookup chunk"
459 test_expect_success 'detect missing commit data chunk' '
460 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
461 "missing the Commit Data chunk"
464 test_expect_success 'detect incorrect fanout' '
465 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
466 "fanout value"
469 test_expect_success 'detect incorrect fanout final value' '
470 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
471 "fanout value"
474 test_expect_success 'detect incorrect OID order' '
475 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
476 "incorrect OID order"
479 test_expect_success 'detect OID not in object database' '
480 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
481 "from object database"
484 test_expect_success 'detect incorrect tree OID' '
485 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
486 "root tree OID for commit"
489 test_expect_success 'detect incorrect parent int-id' '
490 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
491 "invalid parent"
494 test_expect_success 'detect extra parent int-id' '
495 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
496 "is too long"
499 test_expect_success 'detect wrong parent' '
500 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
501 "commit-graph parent for"
504 test_expect_success 'detect incorrect generation number' '
505 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
506 "generation for commit"
509 test_expect_success 'detect incorrect generation number' '
510 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
511 "non-zero generation number"
514 test_expect_success 'detect incorrect commit date' '
515 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
516 "commit date"
519 test_expect_success 'detect incorrect parent for octopus merge' '
520 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
521 "invalid parent"
524 test_expect_success 'detect invalid checksum hash' '
525 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
526 "incorrect checksum"
529 test_expect_success 'detect incorrect chunk count' '
530 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
531 "chunk lookup table entry missing" $GRAPH_CHUNK_LOOKUP_OFFSET
534 test_expect_success 'git fsck (checks commit-graph)' '
535 cd "$TRASH_DIRECTORY/full" &&
536 git fsck &&
537 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
538 "incorrect checksum" &&
539 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
540 test_must_fail git fsck
543 test_expect_success 'setup non-the_repository tests' '
544 rm -rf repo &&
545 git init repo &&
546 test_commit -C repo one &&
547 test_commit -C repo two &&
548 git -C repo config core.commitGraph true &&
549 git -C repo rev-parse two | \
550 git -C repo commit-graph write --stdin-commits
553 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
554 test-tool repository parse_commit_in_graph \
555 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
557 git -C repo log --pretty=format:"%ct " -1 &&
558 git -C repo rev-parse one
559 } >expect &&
560 test_cmp expect actual &&
562 test-tool repository parse_commit_in_graph \
563 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
564 git -C repo log --pretty="%ct" -1 one >expect &&
565 test_cmp expect actual
568 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
569 test-tool repository get_commit_tree_in_graph \
570 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
571 git -C repo rev-parse two^{tree} >expect &&
572 test_cmp expect actual &&
574 test-tool repository get_commit_tree_in_graph \
575 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
576 git -C repo rev-parse one^{tree} >expect &&
577 test_cmp expect actual
580 test_done