3 test_description='commit graph'
6 test_expect_success 'setup full repo' '
8 cd "$TRASH_DIRECTORY/full" &&
10 git config core.commitGraph true &&
14 test_expect_success 'verify graph with no graph file' '
15 cd "$TRASH_DIRECTORY/full" &&
16 git commit-graph verify
19 test_expect_success 'write graph with no packs' '
20 cd "$TRASH_DIRECTORY/full" &&
21 git commit-graph write --object-dir . &&
22 test_path_is_file info/commit-graph
25 test_expect_success 'create commits and repack' '
26 cd "$TRASH_DIRECTORY/full" &&
27 for i in $(test_seq 3)
35 graph_git_two_modes() {
36 git -c core.commitGraph=true $1 >output
37 git -c core.commitGraph=false $1 >expect
38 test_cmp output expect
41 graph_git_behavior() {
46 test_expect_success "check normal git operations: $MSG" '
47 cd "$TRASH_DIRECTORY/$DIR" &&
48 graph_git_two_modes "log --oneline $BRANCH" &&
49 graph_git_two_modes "log --topo-order $BRANCH" &&
50 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
51 graph_git_two_modes "branch -vv" &&
52 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
56 graph_git_behavior 'no graph' full commits/3 commits/1
64 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
67 header: 43475048 1 1 $NUM_CHUNKS 0
69 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
71 git commit-graph read >output &&
72 test_cmp expect output
75 test_expect_success 'write graph' '
76 cd "$TRASH_DIRECTORY/full" &&
77 graph1=$(git commit-graph write) &&
78 test_path_is_file $objdir/info/commit-graph &&
82 graph_git_behavior 'graph exists' full commits/3 commits/1
84 test_expect_success 'Add more commits' '
85 cd "$TRASH_DIRECTORY/full" &&
86 git reset --hard commits/1 &&
87 for i in $(test_seq 4 5)
92 git reset --hard commits/2 &&
93 for i in $(test_seq 6 7)
98 git reset --hard commits/2 &&
99 git merge commits/4 &&
100 git branch merge/1 &&
101 git reset --hard commits/4 &&
102 git merge commits/6 &&
103 git branch merge/2 &&
104 git reset --hard commits/3 &&
105 git merge commits/5 commits/7 &&
106 git branch merge/3 &&
110 # Current graph structure:
120 test_expect_success 'write graph with merges' '
121 cd "$TRASH_DIRECTORY/full" &&
122 git commit-graph write &&
123 test_path_is_file $objdir/info/commit-graph &&
124 graph_read_expect "10" "large_edges"
127 graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
128 graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
129 graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
131 test_expect_success 'Add one more commit' '
132 cd "$TRASH_DIRECTORY/full" &&
134 git branch commits/8 &&
135 ls $objdir/pack | grep idx >existing-idx &&
137 ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx
140 # Current graph structure:
152 graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
153 graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
155 test_expect_success 'write graph with new commit' '
156 cd "$TRASH_DIRECTORY/full" &&
157 git commit-graph write &&
158 test_path_is_file $objdir/info/commit-graph &&
159 graph_read_expect "11" "large_edges"
162 graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
163 graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
165 test_expect_success 'write graph with nothing new' '
166 cd "$TRASH_DIRECTORY/full" &&
167 git commit-graph write &&
168 test_path_is_file $objdir/info/commit-graph &&
169 graph_read_expect "11" "large_edges"
172 graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
173 graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
175 test_expect_success 'build graph from latest pack with closure' '
176 cd "$TRASH_DIRECTORY/full" &&
177 cat new-idx | git commit-graph write --stdin-packs &&
178 test_path_is_file $objdir/info/commit-graph &&
179 graph_read_expect "9" "large_edges"
182 graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
183 graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
185 test_expect_success 'build graph from commits with closure' '
186 cd "$TRASH_DIRECTORY/full" &&
187 git tag -a -m "merge" tag/merge merge/2 &&
188 git rev-parse tag/merge >commits-in &&
189 git rev-parse merge/1 >>commits-in &&
190 cat commits-in | git commit-graph write --stdin-commits &&
191 test_path_is_file $objdir/info/commit-graph &&
192 graph_read_expect "6"
195 graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
196 graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
198 test_expect_success 'build graph from commits with append' '
199 cd "$TRASH_DIRECTORY/full" &&
200 git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
201 test_path_is_file $objdir/info/commit-graph &&
202 graph_read_expect "10" "large_edges"
205 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
206 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
208 test_expect_success 'build graph using --reachable' '
209 cd "$TRASH_DIRECTORY/full" &&
210 git commit-graph write --reachable &&
211 test_path_is_file $objdir/info/commit-graph &&
212 graph_read_expect "11" "large_edges"
215 graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
216 graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
218 test_expect_success 'setup bare repo' '
219 cd "$TRASH_DIRECTORY" &&
220 git clone --bare --no-local full bare &&
222 git config core.commitGraph true &&
226 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
227 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
229 test_expect_success 'write graph in bare repo' '
230 cd "$TRASH_DIRECTORY/bare" &&
231 git commit-graph write &&
232 test_path_is_file $baredir/info/commit-graph &&
233 graph_read_expect "11" "large_edges"
236 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
237 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
239 test_expect_success 'perform fast-forward merge in full repo' '
240 cd "$TRASH_DIRECTORY/full" &&
241 git checkout -b merge-5-to-8 commits/5 &&
242 git merge commits/8 &&
243 git show-ref -s merge-5-to-8 >output &&
244 git show-ref -s commits/8 >expect &&
245 test_cmp expect output
248 test_expect_success 'check that gc computes commit-graph' '
249 cd "$TRASH_DIRECTORY/full" &&
250 git commit --allow-empty -m "blank" &&
251 git commit-graph write --reachable &&
252 cp $objdir/info/commit-graph commit-graph-before-gc &&
253 git reset --hard HEAD~1 &&
254 git config gc.writeCommitGraph true &&
256 cp $objdir/info/commit-graph commit-graph-after-gc &&
257 ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
258 git commit-graph write --reachable &&
259 test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph
262 # the verify tests below expect the commit-graph to contain
263 # exactly the commits reachable from the commits/8 branch.
264 # If the file changes the set of commits in the list, then the
265 # offsets into the binary file will result in different edits
266 # and the tests will likely break.
268 test_expect_success 'git commit-graph verify' '
269 cd "$TRASH_DIRECTORY/full" &&
270 git rev-parse commits/8 | git commit-graph write --stdin-commits &&
271 git commit-graph verify >output
279 GRAPH_BYTE_CHUNK_COUNT=6
280 GRAPH_CHUNK_LOOKUP_OFFSET=8
281 GRAPH_CHUNK_LOOKUP_WIDTH=12
282 GRAPH_CHUNK_LOOKUP_ROWS=5
283 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
284 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
285 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
286 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
287 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
288 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
289 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
290 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
291 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
292 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
293 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
294 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
295 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
296 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
297 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
298 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
299 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
300 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
301 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
302 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
303 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
304 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
305 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
306 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
308 # usage: corrupt_graph_and_verify <position> <data> <string>
309 # Manipulates the commit-graph file at the position
310 # by inserting the data, then runs 'git commit-graph verify'
311 # and places the output in the file 'err'. Test 'err' for
313 corrupt_graph_and_verify() {
317 cd "$TRASH_DIRECTORY/full" &&
318 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
319 cp $objdir/info/commit-graph commit-graph-backup &&
320 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
321 test_must_fail git commit-graph verify 2>test_err &&
322 grep -v "^+" test_err >err
323 test_i18ngrep "$grepstr" err
326 test_expect_success 'detect bad signature' '
327 corrupt_graph_and_verify 0 "\0" \
331 test_expect_success 'detect bad version' '
332 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
336 test_expect_success 'detect bad hash version' '
337 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
341 test_expect_success 'detect low chunk count' '
342 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
343 "missing the .* chunk"
346 test_expect_success 'detect missing OID fanout chunk' '
347 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
348 "missing the OID Fanout chunk"
351 test_expect_success 'detect missing OID lookup chunk' '
352 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
353 "missing the OID Lookup chunk"
356 test_expect_success 'detect missing commit data chunk' '
357 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
358 "missing the Commit Data chunk"
361 test_expect_success 'detect incorrect fanout' '
362 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
366 test_expect_success 'detect incorrect fanout final value' '
367 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
371 test_expect_success 'detect incorrect OID order' '
372 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
373 "incorrect OID order"
376 test_expect_success 'detect OID not in object database' '
377 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
378 "from object database"
381 test_expect_success 'detect incorrect tree OID' '
382 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
383 "root tree OID for commit"
386 test_expect_success 'detect incorrect parent int-id' '
387 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
391 test_expect_success 'detect extra parent int-id' '
392 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
396 test_expect_success 'detect wrong parent' '
397 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
398 "commit-graph parent for"
401 test_expect_success 'detect incorrect generation number' '
402 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
403 "generation for commit"
406 test_expect_success 'detect incorrect generation number' '
407 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
408 "non-zero generation number"
411 test_expect_success 'detect incorrect commit date' '
412 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
416 test_expect_success 'detect incorrect parent for octopus merge' '
417 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
421 test_expect_success 'detect invalid checksum hash' '
422 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
426 test_expect_success 'git fsck (checks commit-graph)' '
427 cd "$TRASH_DIRECTORY/full" &&
429 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
430 "incorrect checksum" &&
431 test_must_fail git fsck
434 test_expect_success 'setup non-the_repository tests' '
437 test_commit -C repo one &&
438 test_commit -C repo two &&
439 git -C repo config core.commitGraph true &&
440 git -C repo rev-parse two | \
441 git -C repo commit-graph write --stdin-commits
444 test_expect_success 'parse_commit_in_graph works for non-the_repository' '
445 test-tool repository parse_commit_in_graph \
446 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
448 git -C repo log --pretty=format:"%ct " -1 &&
449 git -C repo rev-parse one
451 test_cmp expect actual &&
453 test-tool repository parse_commit_in_graph \
454 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
455 git -C repo log --pretty="%ct" -1 one >expect &&
456 test_cmp expect actual
459 test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
460 test-tool repository get_commit_tree_in_graph \
461 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
462 git -C repo rev-parse two^{tree} >expect &&
463 test_cmp expect actual &&
465 test-tool repository get_commit_tree_in_graph \
466 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
467 git -C repo rev-parse one^{tree} >expect &&
468 test_cmp expect actual