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 &&
10 git config core.commitGraph true &&
11 objdir=".git/objects" &&
12 test_oid_init
13'
14
15test_expect_success 'verify graph with no graph file' '
16 cd "$TRASH_DIRECTORY/full" &&
17 git commit-graph verify
18'
19
20test_expect_success 'write graph with no packs' '
21 cd "$TRASH_DIRECTORY/full" &&
22 git commit-graph write --object-dir . &&
23 test_path_is_file info/commit-graph
24'
25
26test_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
32'
33
34test_expect_success 'create commits and repack' '
35 cd "$TRASH_DIRECTORY/full" &&
36 for i in $(test_seq 3)
37 do
38 test_commit $i &&
39 git branch commits/$i
40 done &&
41 git repack
42'
43
44graph_git_two_modes() {
45 git -c core.commitGraph=true $1 >output
46 git -c core.commitGraph=false $1 >expect
47 test_cmp expect output
48}
49
50graph_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"
62 '
63}
64
65graph_git_behavior 'no graph' full commits/3 commits/1
66
67graph_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)))
74 fi
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
82}
83
84test_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"
89'
90
91graph_git_behavior 'graph exists' full commits/3 commits/1
92
93test_expect_success 'Add more commits' '
94 cd "$TRASH_DIRECTORY/full" &&
95 git reset --hard commits/1 &&
96 for i in $(test_seq 4 5)
97 do
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)
103 do
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
117'
118
119# Current graph structure:
120#
121# __M3___
122# / | \
123# 3 M1 5 M2 7
124# |/ \|/ \|
125# 2 4 6
126# |___/____/
127# 1
128
129test_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"
134'
135
136graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
137graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
138graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
139
140test_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
147'
148
149# Current graph structure:
150#
151# 8
152# |
153# __M3___
154# / | \
155# 3 M1 5 M2 7
156# |/ \|/ \|
157# 2 4 6
158# |___/____/
159# 1
160
161graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
162graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
163
164test_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"
169'
170
171graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
172graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
173
174test_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"
179'
180
181graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
182graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
183
184test_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"
189'
190
191graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
192graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
193
194test_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"
202'
203
204graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
205graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
206
207test_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"
212'
213
214graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
215graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
216
217test_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"
222'
223
224graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
225graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
226
227test_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"
233'
234
235graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
236graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
237
238test_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"
243'
244
245graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
246graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
247
248test_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
255'
256
257test_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
269'
270
271test_expect_success 'replace-objects invalidates commit-graph' '
272 cd "$TRASH_DIRECTORY" &&
273 test_when_finished rm -rf replace &&
274 git clone full replace &&
275 (
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
290 )
291'
292
293test_expect_success 'commit grafts invalidate commit-graph' '
294 cd "$TRASH_DIRECTORY" &&
295 test_when_finished rm -rf graft &&
296 git clone full graft &&
297 (
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
314 )
315'
316
317test_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 &&
321 (
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
328 )
329'
330
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.
336
337test_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
341'
342
343NUM_COMMITS=9
344NUM_OCTOPUS_EDGES=2
345HASH_LEN="$(test_oid rawsz)"
346GRAPH_BYTE_VERSION=4
347GRAPH_BYTE_HASH=5
348GRAPH_BYTE_CHUNK_COUNT=6
349GRAPH_CHUNK_LOOKUP_OFFSET=8
350GRAPH_CHUNK_LOOKUP_WIDTH=12
351GRAPH_CHUNK_LOOKUP_ROWS=5
352GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
353GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
354 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
355GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
356 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
357GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
358 $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
359GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
360GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
361GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
362GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
363GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
364GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
365GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
366GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
367GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
368GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
369GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
370GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
371GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
372GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
373 $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
374GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
375GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
376
377corrupt_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
381}
382
383corrupt_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
395}
396
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.
403corrupt_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"
414
415}
416
417test_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"
421'
422
423test_expect_success 'detect too small' '
424 corrupt_graph_setup &&
425 echo "a small graph" >$objdir/info/commit-graph &&
426 corrupt_graph_verify "too small"
427'
428
429test_expect_success 'detect bad signature' '
430 corrupt_graph_and_verify 0 "\0" \
431 "graph signature"
432'
433
434test_expect_success 'detect bad version' '
435 corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
436 "graph version"
437'
438
439test_expect_success 'detect bad hash version' '
440 corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
441 "hash version"
442'
443
444test_expect_success 'detect low chunk count' '
445 corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
446 "missing the .* chunk"
447'
448
449test_expect_success 'detect missing OID fanout chunk' '
450 corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
451 "missing the OID Fanout chunk"
452'
453
454test_expect_success 'detect missing OID lookup chunk' '
455 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
456 "missing the OID Lookup chunk"
457'
458
459test_expect_success 'detect missing commit data chunk' '
460 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
461 "missing the Commit Data chunk"
462'
463
464test_expect_success 'detect incorrect fanout' '
465 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
466 "fanout value"
467'
468
469test_expect_success 'detect incorrect fanout final value' '
470 corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
471 "fanout value"
472'
473
474test_expect_success 'detect incorrect OID order' '
475 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
476 "incorrect OID order"
477'
478
479test_expect_success 'detect OID not in object database' '
480 corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
481 "from object database"
482'
483
484test_expect_success 'detect incorrect tree OID' '
485 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
486 "root tree OID for commit"
487'
488
489test_expect_success 'detect incorrect parent int-id' '
490 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
491 "invalid parent"
492'
493
494test_expect_success 'detect extra parent int-id' '
495 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
496 "is too long"
497'
498
499test_expect_success 'detect wrong parent' '
500 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
501 "commit-graph parent for"
502'
503
504test_expect_success 'detect incorrect generation number' '
505 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
506 "generation for commit"
507'
508
509test_expect_success 'detect incorrect generation number' '
510 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
511 "non-zero generation number"
512'
513
514test_expect_success 'detect incorrect commit date' '
515 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
516 "commit date"
517'
518
519test_expect_success 'detect incorrect parent for octopus merge' '
520 corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
521 "invalid parent"
522'
523
524test_expect_success 'detect invalid checksum hash' '
525 corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
526 "incorrect checksum"
527'
528
529test_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
532'
533
534test_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
541'
542
543test_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
551'
552
553test_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 &&
556 {
557 git -C repo log --pretty=format:"%ct " -1 &&
558 git -C repo rev-parse one
559 } >expect &&
560 test_cmp expect actual &&
561
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
566'
567
568test_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 &&
573
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
578'
579
580test_done