3 test_description
='commit graph'
6 test_expect_success
'setup full repo' '
8 cd "$TRASH_DIRECTORY/full" &&
10 git config core.commitGraph true &&
11 objdir=".git/objects" &&
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
'exit with correct error on bad input to --stdin-packs' '
27 cd "$TRASH_DIRECTORY/full" &&
28 echo doesnotexist >in &&
29 test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr &&
30 test_i18ngrep "error adding pack" stderr
33 test_expect_success
'create commits and repack' '
34 cd "$TRASH_DIRECTORY/full" &&
35 for i in $(test_seq 3)
43 test_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
52 graph_git_two_modes
() {
53 git
-c core.commitGraph
=true
$1 >output
54 git
-c core.commitGraph
=false
$1 >expect
55 test_cmp expect output
58 graph_git_behavior
() {
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"
73 graph_git_behavior
'no graph' full commits
/3 commits
/1
81 NUM_CHUNKS
=$
((3 + $
(echo "$2" |
wc -w)))
84 header: 43475048 1 1 $NUM_CHUNKS 0
86 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
88 git commit-graph
read >output
&&
89 test_cmp expect output
92 test_expect_success
'write graph' '
93 cd "$TRASH_DIRECTORY/full" &&
94 git commit-graph write &&
95 test_path_is_file $objdir/info/commit-graph &&
99 graph_git_behavior
'graph exists' full commits
/3 commits
/1
101 test_expect_success
'Add more commits' '
102 cd "$TRASH_DIRECTORY/full" &&
103 git reset --hard commits/1 &&
104 for i in $(test_seq 4 5)
107 git branch commits/$i
109 git reset --hard commits/2 &&
110 for i in $(test_seq 6 7)
113 git branch commits/$i
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 &&
127 # Current graph structure:
137 test_expect_success
'write graph with merges' '
138 cd "$TRASH_DIRECTORY/full" &&
139 git commit-graph write &&
140 test_path_is_file $objdir/info/commit-graph &&
141 graph_read_expect "10" "extra_edges"
144 graph_git_behavior
'merge 1 vs 2' full merge
/1 merge
/2
145 graph_git_behavior
'merge 1 vs 3' full merge
/1 merge
/3
146 graph_git_behavior
'merge 2 vs 3' full merge
/2 merge
/3
148 test_expect_success
'Add one more commit' '
149 cd "$TRASH_DIRECTORY/full" &&
151 git branch commits/8 &&
152 ls $objdir/pack | grep idx >existing-idx &&
154 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
157 # Current graph structure:
169 graph_git_behavior
'mixed mode, commit 8 vs merge 1' full commits
/8 merge
/1
170 graph_git_behavior
'mixed mode, commit 8 vs merge 2' full commits
/8 merge
/2
172 test_expect_success
'write graph with new commit' '
173 cd "$TRASH_DIRECTORY/full" &&
174 git commit-graph write &&
175 test_path_is_file $objdir/info/commit-graph &&
176 graph_read_expect "11" "extra_edges"
179 graph_git_behavior
'full graph, commit 8 vs merge 1' full commits
/8 merge
/1
180 graph_git_behavior
'full graph, commit 8 vs merge 2' full commits
/8 merge
/2
182 test_expect_success
'write graph with nothing new' '
183 cd "$TRASH_DIRECTORY/full" &&
184 git commit-graph write &&
185 test_path_is_file $objdir/info/commit-graph &&
186 graph_read_expect "11" "extra_edges"
189 graph_git_behavior
'cleared graph, commit 8 vs merge 1' full commits
/8 merge
/1
190 graph_git_behavior
'cleared graph, commit 8 vs merge 2' full commits
/8 merge
/2
192 test_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 &&
196 graph_read_expect "9" "extra_edges"
199 graph_git_behavior
'graph from pack, commit 8 vs merge 1' full commits
/8 merge
/1
200 graph_git_behavior
'graph from pack, commit 8 vs merge 2' full commits
/8 merge
/2
202 test_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"
212 graph_git_behavior
'graph from commits, commit 8 vs merge 1' full commits
/8 merge
/1
213 graph_git_behavior
'graph from commits, commit 8 vs merge 2' full commits
/8 merge
/2
215 test_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 &&
219 graph_read_expect "10" "extra_edges"
222 graph_git_behavior
'append graph, commit 8 vs merge 1' full commits
/8 merge
/1
223 graph_git_behavior
'append graph, commit 8 vs merge 2' full commits
/8 merge
/2
225 test_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 &&
229 graph_read_expect "11" "extra_edges"
232 graph_git_behavior
'append graph, commit 8 vs merge 1' full commits
/8 merge
/1
233 graph_git_behavior
'append graph, commit 8 vs merge 2' full commits
/8 merge
/2
235 test_expect_success
'setup bare repo' '
236 cd "$TRASH_DIRECTORY" &&
237 git clone --bare --no-local full bare &&
239 git config core.commitGraph true &&
243 graph_git_behavior
'bare repo, commit 8 vs merge 1' bare commits
/8 merge
/1
244 graph_git_behavior
'bare repo, commit 8 vs merge 2' bare commits
/8 merge
/2
246 test_expect_success
'write graph in bare repo' '
247 cd "$TRASH_DIRECTORY/bare" &&
248 git commit-graph write &&
249 test_path_is_file $baredir/info/commit-graph &&
250 graph_read_expect "11" "extra_edges"
253 graph_git_behavior
'bare repo with graph, commit 8 vs merge 1' bare commits
/8 merge
/1
254 graph_git_behavior
'bare repo with graph, commit 8 vs merge 2' bare commits
/8 merge
/2
256 test_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
265 test_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 &&
273 cp $objdir/info/commit-graph commit-graph-after-gc &&
274 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
275 git commit-graph write --reachable &&
276 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
279 test_expect_success
'replace-objects invalidates commit-graph' '
280 cd "$TRASH_DIRECTORY" &&
281 test_when_finished rm -rf replace &&
282 git clone full 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
301 test_expect_success
'commit grafts invalidate commit-graph' '
302 cd "$TRASH_DIRECTORY" &&
303 test_when_finished rm -rf graft &&
304 git clone full 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
325 test_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 &&
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
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.
345 test_expect_success
'git commit-graph verify' '
346 cd "$TRASH_DIRECTORY/full" &&
347 git rev-parse commits/8 | git commit-graph write --stdin-commits &&
348 git commit-graph verify >output
353 HASH_LEN
="$(test_oid rawsz)"
356 GRAPH_BYTE_CHUNK_COUNT
=6
357 GRAPH_CHUNK_LOOKUP_OFFSET
=8
358 GRAPH_CHUNK_LOOKUP_WIDTH
=12
359 GRAPH_CHUNK_LOOKUP_ROWS
=5
360 GRAPH_BYTE_OID_FANOUT_ID
=$GRAPH_CHUNK_LOOKUP_OFFSET
361 GRAPH_BYTE_OID_LOOKUP_ID
=$
(($GRAPH_CHUNK_LOOKUP_OFFSET + \
362 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
363 GRAPH_BYTE_COMMIT_DATA_ID
=$
(($GRAPH_CHUNK_LOOKUP_OFFSET + \
364 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
365 GRAPH_FANOUT_OFFSET
=$
(($GRAPH_CHUNK_LOOKUP_OFFSET + \
366 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
367 GRAPH_BYTE_FANOUT1
=$
(($GRAPH_FANOUT_OFFSET + 4 * 4))
368 GRAPH_BYTE_FANOUT2
=$
(($GRAPH_FANOUT_OFFSET + 4 * 255))
369 GRAPH_OID_LOOKUP_OFFSET
=$
(($GRAPH_FANOUT_OFFSET + 4 * 256))
370 GRAPH_BYTE_OID_LOOKUP_ORDER
=$
(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
371 GRAPH_BYTE_OID_LOOKUP_MISSING
=$
(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
372 GRAPH_COMMIT_DATA_OFFSET
=$
(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
373 GRAPH_BYTE_COMMIT_TREE
=$GRAPH_COMMIT_DATA_OFFSET
374 GRAPH_BYTE_COMMIT_PARENT
=$
(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
375 GRAPH_BYTE_COMMIT_EXTRA_PARENT
=$
(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
376 GRAPH_BYTE_COMMIT_WRONG_PARENT
=$
(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
377 GRAPH_BYTE_COMMIT_GENERATION
=$
(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
378 GRAPH_BYTE_COMMIT_DATE
=$
(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
379 GRAPH_COMMIT_DATA_WIDTH
=$
(($HASH_LEN + 16))
380 GRAPH_OCTOPUS_DATA_OFFSET
=$
(($GRAPH_COMMIT_DATA_OFFSET + \
381 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
382 GRAPH_BYTE_OCTOPUS
=$
(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
383 GRAPH_BYTE_FOOTER
=$
(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
385 corrupt_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
391 corrupt_graph_verify
() {
393 test_must_fail git commit-graph verify
2>test_err
&&
394 grep -v "^+" test_err
>err
&&
395 test_i18ngrep
"$grepstr" err
&&
396 if test "$2" != "no-copy"
398 cp $objdir/info
/commit-graph commit-graph-pre-write-test
400 git status
--short &&
401 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD
=true git commit-graph
write &&
402 git commit-graph verify
405 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
406 # Manipulates the commit-graph file at the position
407 # by inserting the data, optionally zeroing the file
408 # starting at <zero_pos>, then runs 'git commit-graph verify'
409 # and places the output in the file 'err'. Test 'err' for
411 corrupt_graph_and_verify
() {
415 corrupt_graph_setup
&&
416 orig_size
=$
(wc -c < $objdir/info
/commit-graph
) &&
417 zero_pos
=${4:-${orig_size}} &&
418 printf "$data" |
dd of
="$objdir/info/commit-graph" bs
=1 seek
="$pos" conv
=notrunc
&&
419 dd of
="$objdir/info/commit-graph" bs
=1 seek
="$zero_pos" if=/dev
/null
&&
420 generate_zero_bytes $
(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" &&
421 corrupt_graph_verify
"$grepstr"
425 test_expect_success POSIXPERM
,SANITY
'detect permission problem' '
426 corrupt_graph_setup &&
427 chmod 000 $objdir/info/commit-graph &&
428 corrupt_graph_verify "Could not open" "no-copy"
431 test_expect_success
'detect too small' '
432 corrupt_graph_setup &&
433 echo "a small graph" >$objdir/info/commit-graph &&
434 corrupt_graph_verify "too small"
437 test_expect_success
'detect bad signature' '
438 corrupt_graph_and_verify 0 "\0" \
442 test_expect_success
'detect bad version' '
443 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
447 test_expect_success
'detect bad hash version' '
448 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
452 test_expect_success
'detect low chunk count' '
453 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
454 "missing the .* chunk"
457 test_expect_success
'detect missing OID fanout chunk' '
458 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
459 "missing the OID Fanout chunk"
462 test_expect_success
'detect missing OID lookup chunk' '
463 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
464 "missing the OID Lookup chunk"
467 test_expect_success
'detect missing commit data chunk' '
468 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
469 "missing the Commit Data chunk"
472 test_expect_success
'detect incorrect fanout' '
473 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
477 test_expect_success
'detect incorrect fanout final value' '
478 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
482 test_expect_success
'detect incorrect OID order' '
483 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
484 "incorrect OID order"
487 test_expect_success
'detect OID not in object database' '
488 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
489 "from object database"
492 test_expect_success
'detect incorrect tree OID' '
493 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
494 "root tree OID for commit"
497 test_expect_success
'detect incorrect parent int-id' '
498 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
502 test_expect_success
'detect extra parent int-id' '
503 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
507 test_expect_success
'detect wrong parent' '
508 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
509 "commit-graph parent for"
512 test_expect_success
'detect incorrect generation number' '
513 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
514 "generation for commit"
517 test_expect_success
'detect incorrect generation number' '
518 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
519 "non-zero generation number"
522 test_expect_success
'detect incorrect commit date' '
523 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
527 test_expect_success
'detect incorrect parent for octopus merge' '
528 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
532 test_expect_success
'detect invalid checksum hash' '
533 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
537 test_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
542 test_expect_success
'git fsck (checks commit-graph)' '
543 cd "$TRASH_DIRECTORY/full" &&
545 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
546 "incorrect checksum" &&
547 cp commit-graph-pre-write-test $objdir/info/commit-graph &&
548 test_must_fail git fsck
551 test_expect_success
'setup non-the_repository tests' '
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
561 test_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 &&
565 git -C repo log --pretty=format:"%ct " -1 &&
566 git -C repo rev-parse one
568 test_cmp expect actual &&
570 test-tool repository parse_commit_in_graph \
571 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
572 git -C repo log --pretty="%ct" -1 one >expect &&
573 test_cmp expect actual
576 test_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 &&
579 git -C repo rev-parse two^{tree} >expect &&
580 test_cmp expect actual &&
582 test-tool repository get_commit_tree_in_graph \
583 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
584 git -C repo rev-parse one^{tree} >expect &&
585 test_cmp expect actual