commit-graph: not compatible with replace objects
[git/git.git] / t / t5318-commit-graph.sh
CommitLineData
f237c8b6
DS
1#!/bin/sh
2
3test_description='commit graph'
4. ./test-lib.sh
5
6test_expect_success 'setup full repo' '
7 mkdir full &&
8 cd "$TRASH_DIRECTORY/full" &&
9 git init &&
177722b3 10 git config core.commitGraph true &&
f237c8b6
DS
11 objdir=".git/objects"
12'
13
283e68c7
DS
14test_expect_success 'verify graph with no graph file' '
15 cd "$TRASH_DIRECTORY/full" &&
16 git commit-graph verify
17'
18
f237c8b6
DS
19test_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
23'
24
25test_expect_success 'create commits and repack' '
26 cd "$TRASH_DIRECTORY/full" &&
27 for i in $(test_seq 3)
28 do
29 test_commit $i &&
30 git branch commits/$i
31 done &&
32 git repack
33'
34
177722b3 35graph_git_two_modes() {
55abcb41
DS
36 git -c core.commitGraph=true $1 >output
37 git -c core.commitGraph=false $1 >expect
177722b3
DS
38 test_cmp output expect
39}
40
41graph_git_behavior() {
42 MSG=$1
43 DIR=$2
44 BRANCH=$3
45 COMPARE=$4
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"
53 '
54}
55
56graph_git_behavior 'no graph' full commits/3 commits/1
57
2a2e32bd
DS
58graph_read_expect() {
59 OPTIONAL=""
60 NUM_CHUNKS=3
61 if test ! -z $2
62 then
63 OPTIONAL=" $2"
64 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
65 fi
66 cat >expect <<- EOF
67 header: 43475048 1 1 $NUM_CHUNKS 0
68 num_commits: $1
69 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
70 EOF
71 git commit-graph read >output &&
72 test_cmp expect output
73}
74
f237c8b6
DS
75test_expect_success 'write graph' '
76 cd "$TRASH_DIRECTORY/full" &&
77 graph1=$(git commit-graph write) &&
2a2e32bd
DS
78 test_path_is_file $objdir/info/commit-graph &&
79 graph_read_expect "3"
f237c8b6
DS
80'
81
177722b3
DS
82graph_git_behavior 'graph exists' full commits/3 commits/1
83
f237c8b6
DS
84test_expect_success 'Add more commits' '
85 cd "$TRASH_DIRECTORY/full" &&
86 git reset --hard commits/1 &&
87 for i in $(test_seq 4 5)
88 do
89 test_commit $i &&
90 git branch commits/$i
91 done &&
92 git reset --hard commits/2 &&
93 for i in $(test_seq 6 7)
94 do
95 test_commit $i &&
96 git branch commits/$i
97 done &&
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 &&
107 git repack
108'
109
110# Current graph structure:
111#
112# __M3___
113# / | \
114# 3 M1 5 M2 7
115# |/ \|/ \|
116# 2 4 6
117# |___/____/
118# 1
119
f237c8b6
DS
120test_expect_success 'write graph with merges' '
121 cd "$TRASH_DIRECTORY/full" &&
122 git commit-graph write &&
2a2e32bd
DS
123 test_path_is_file $objdir/info/commit-graph &&
124 graph_read_expect "10" "large_edges"
f237c8b6
DS
125'
126
177722b3
DS
127graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
128graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
129graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
130
f237c8b6
DS
131test_expect_success 'Add one more commit' '
132 cd "$TRASH_DIRECTORY/full" &&
133 test_commit 8 &&
134 git branch commits/8 &&
135 ls $objdir/pack | grep idx >existing-idx &&
136 git repack &&
137 ls $objdir/pack| grep idx | grep -v --file=existing-idx >new-idx
138'
139
140# Current graph structure:
141#
142# 8
143# |
144# __M3___
145# / | \
146# 3 M1 5 M2 7
147# |/ \|/ \|
148# 2 4 6
149# |___/____/
150# 1
151
177722b3
DS
152graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
153graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
154
f237c8b6
DS
155test_expect_success 'write graph with new commit' '
156 cd "$TRASH_DIRECTORY/full" &&
157 git commit-graph write &&
2a2e32bd
DS
158 test_path_is_file $objdir/info/commit-graph &&
159 graph_read_expect "11" "large_edges"
f237c8b6
DS
160'
161
177722b3
DS
162graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
163graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
164
f237c8b6
DS
165test_expect_success 'write graph with nothing new' '
166 cd "$TRASH_DIRECTORY/full" &&
167 git commit-graph write &&
2a2e32bd
DS
168 test_path_is_file $objdir/info/commit-graph &&
169 graph_read_expect "11" "large_edges"
f237c8b6
DS
170'
171
177722b3
DS
172graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
173graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
174
049d51a2
DS
175test_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"
180'
181
182graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
183graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
184
3d5df01b
DS
185test_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"
193'
194
195graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
196graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
197
7547b95b
DS
198test_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"
203'
204
205graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
206graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
207
59fb8770
DS
208test_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"
213'
214
215graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
216graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
217
f237c8b6
DS
218test_expect_success 'setup bare repo' '
219 cd "$TRASH_DIRECTORY" &&
220 git clone --bare --no-local full bare &&
221 cd bare &&
177722b3 222 git config core.commitGraph true &&
f237c8b6
DS
223 baredir="./objects"
224'
225
177722b3
DS
226graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
227graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
228
f237c8b6
DS
229test_expect_success 'write graph in bare repo' '
230 cd "$TRASH_DIRECTORY/bare" &&
231 git commit-graph write &&
2a2e32bd
DS
232 test_path_is_file $baredir/info/commit-graph &&
233 graph_read_expect "11" "large_edges"
f237c8b6
DS
234'
235
177722b3
DS
236graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
237graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
238
7adf5266
DS
239test_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
246'
247
d5d5d7b6
DS
248test_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 &&
255 git gc &&
256 cp $objdir/info/commit-graph commit-graph-after-gc &&
257 ! test_cmp commit-graph-before-gc commit-graph-after-gc &&
258 git commit-graph write --reachable &&
259 test_cmp commit-graph-after-gc $objdir/info/commit-graph
260'
261
d6538246
DS
262test_expect_success 'replace-objects invalidates commit-graph' '
263 cd "$TRASH_DIRECTORY" &&
264 test_when_finished rm -rf replace &&
265 git clone full replace &&
266 (
267 cd replace &&
268 git commit-graph write --reachable &&
269 test_path_is_file .git/objects/info/commit-graph &&
270 git replace HEAD~1 HEAD~2 &&
271 git -c core.commitGraph=false log >expect &&
272 git -c core.commitGraph=true log >actual &&
273 test_cmp expect actual &&
274 git commit-graph write --reachable &&
275 git -c core.commitGraph=false --no-replace-objects log >expect &&
276 git -c core.commitGraph=true --no-replace-objects log >actual &&
277 test_cmp expect actual &&
278 rm -rf .git/objects/info/commit-graph &&
279 git commit-graph write --reachable &&
280 test_path_is_file .git/objects/info/commit-graph
281 )
282'
283
d9b9f8a6
DS
284# the verify tests below expect the commit-graph to contain
285# exactly the commits reachable from the commits/8 branch.
286# If the file changes the set of commits in the list, then the
287# offsets into the binary file will result in different edits
288# and the tests will likely break.
289
283e68c7
DS
290test_expect_success 'git commit-graph verify' '
291 cd "$TRASH_DIRECTORY/full" &&
d9b9f8a6 292 git rev-parse commits/8 | git commit-graph write --stdin-commits &&
283e68c7
DS
293 git commit-graph verify >output
294'
295
96af91d4 296NUM_COMMITS=9
437787ae 297NUM_OCTOPUS_EDGES=2
9bda8467 298HASH_LEN=20
d9b9f8a6
DS
299GRAPH_BYTE_VERSION=4
300GRAPH_BYTE_HASH=5
2bd0365f
DS
301GRAPH_BYTE_CHUNK_COUNT=6
302GRAPH_CHUNK_LOOKUP_OFFSET=8
303GRAPH_CHUNK_LOOKUP_WIDTH=12
304GRAPH_CHUNK_LOOKUP_ROWS=5
305GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
306GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
307 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
308GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
309 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
9bda8467
DS
310GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
311 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
312GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
313GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
314GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
315GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
96af91d4 316GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
2e3c0737
DS
317GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
318GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
53614b13
DS
319GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
320GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
321GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
1373e547 322GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
88968ebf 323GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
437787ae
DS
324GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
325GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
326 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
327GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
41df0e30 328GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
d9b9f8a6
DS
329
330# usage: corrupt_graph_and_verify <position> <data> <string>
331# Manipulates the commit-graph file at the position
332# by inserting the data, then runs 'git commit-graph verify'
333# and places the output in the file 'err'. Test 'err' for
334# the given string.
335corrupt_graph_and_verify() {
336 pos=$1
337 data="${2:-\0}"
338 grepstr=$3
339 cd "$TRASH_DIRECTORY/full" &&
340 test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
341 cp $objdir/info/commit-graph commit-graph-backup &&
342 printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
343 test_must_fail git commit-graph verify 2>test_err &&
344 grep -v "^+" test_err >err
345 test_i18ngrep "$grepstr" err
346}
347
348test_expect_success 'detect bad signature' '
349 corrupt_graph_and_verify 0 "\0" \
350 "graph signature"
351'
352
353test_expect_success 'detect bad version' '
354 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
355 "graph version"
356'
357
358test_expect_success 'detect bad hash version' '
359 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
360 "hash version"
361'
362
2bd0365f
DS
363test_expect_success 'detect low chunk count' '
364 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
365 "missing the .* chunk"
366'
367
368test_expect_success 'detect missing OID fanout chunk' '
369 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
370 "missing the OID Fanout chunk"
371'
372
373test_expect_success 'detect missing OID lookup chunk' '
374 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
375 "missing the OID Lookup chunk"
376'
377
378test_expect_success 'detect missing commit data chunk' '
379 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
380 "missing the Commit Data chunk"
381'
382
9bda8467
DS
383test_expect_success 'detect incorrect fanout' '
384 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
385 "fanout value"
386'
387
388test_expect_success 'detect incorrect fanout final value' '
389 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
390 "fanout value"
391'
392
393test_expect_success 'detect incorrect OID order' '
394 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
395 "incorrect OID order"
396'
397
96af91d4
DS
398test_expect_success 'detect OID not in object database' '
399 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
400 "from object database"
401'
402
2e3c0737
DS
403test_expect_success 'detect incorrect tree OID' '
404 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
405 "root tree OID for commit"
406'
407
53614b13
DS
408test_expect_success 'detect incorrect parent int-id' '
409 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
410 "invalid parent"
411'
412
413test_expect_success 'detect extra parent int-id' '
414 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
415 "is too long"
416'
417
418test_expect_success 'detect wrong parent' '
419 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
420 "commit-graph parent for"
421'
422
1373e547
DS
423test_expect_success 'detect incorrect generation number' '
424 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
425 "generation for commit"
426'
427
428test_expect_success 'detect incorrect generation number' '
429 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
430 "non-zero generation number"
431'
432
88968ebf
DS
433test_expect_success 'detect incorrect commit date' '
434 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
435 "commit date"
436'
437
437787ae
DS
438test_expect_success 'detect incorrect parent for octopus merge' '
439 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
440 "invalid parent"
441'
442
41df0e30
DS
443test_expect_success 'detect invalid checksum hash' '
444 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
445 "incorrect checksum"
446'
447
e0fd51e1
DS
448test_expect_success 'git fsck (checks commit-graph)' '
449 cd "$TRASH_DIRECTORY/full" &&
450 git fsck &&
451 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
452 "incorrect checksum" &&
453 test_must_fail git fsck
454'
455
dade47c0
JT
456test_expect_success 'setup non-the_repository tests' '
457 rm -rf repo &&
458 git init repo &&
459 test_commit -C repo one &&
460 test_commit -C repo two &&
461 git -C repo config core.commitGraph true &&
462 git -C repo rev-parse two | \
463 git -C repo commit-graph write --stdin-commits
464'
465
466test_expect_success 'parse_commit_in_graph works for non-the_repository' '
467 test-tool repository parse_commit_in_graph \
468 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
469 echo $(git -C repo log --pretty="%ct" -1) \
470 $(git -C repo rev-parse one) >expect &&
471 test_cmp expect actual &&
472
473 test-tool repository parse_commit_in_graph \
474 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
475 echo $(git -C repo log --pretty="%ct" -1 one) >expect &&
476 test_cmp expect actual
477'
478
479test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
480 test-tool repository get_commit_tree_in_graph \
481 repo/.git repo "$(git -C repo rev-parse two)" >actual &&
482 echo $(git -C repo rev-parse two^{tree}) >expect &&
483 test_cmp expect actual &&
484
485 test-tool repository get_commit_tree_in_graph \
486 repo/.git repo "$(git -C repo rev-parse one)" >actual &&
487 echo $(git -C repo rev-parse one^{tree}) >expect &&
488 test_cmp expect actual
489'
490
f237c8b6 491test_done