]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/sh | |
2 | ||
3 | test_description='commit graph' | |
4 | . ./test-lib.sh | |
5 | ||
6 | GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0 | |
7 | ||
8 | test_expect_success 'usage' ' | |
9 | test_expect_code 129 git commit-graph write blah 2>err && | |
10 | test_expect_code 129 git commit-graph write verify | |
11 | ' | |
12 | ||
13 | test_expect_success 'usage shown without sub-command' ' | |
14 | test_expect_code 129 git commit-graph 2>err && | |
15 | ! grep error: err | |
16 | ' | |
17 | ||
18 | test_expect_success 'usage shown with an error on unknown sub-command' ' | |
19 | cat >expect <<-\EOF && | |
20 | error: unrecognized subcommand: unknown | |
21 | EOF | |
22 | test_expect_code 129 git commit-graph unknown 2>stderr && | |
23 | grep error stderr >actual && | |
24 | test_cmp expect actual | |
25 | ' | |
26 | ||
27 | test_expect_success 'setup full repo' ' | |
28 | mkdir full && | |
29 | cd "$TRASH_DIRECTORY/full" && | |
30 | git init && | |
31 | git config core.commitGraph true && | |
32 | objdir=".git/objects" && | |
33 | ||
34 | test_oid_cache <<-EOF | |
35 | oid_version sha1:1 | |
36 | oid_version sha256:2 | |
37 | EOF | |
38 | ' | |
39 | ||
40 | test_expect_success POSIXPERM 'tweak umask for modebit tests' ' | |
41 | umask 022 | |
42 | ' | |
43 | ||
44 | test_expect_success 'verify graph with no graph file' ' | |
45 | cd "$TRASH_DIRECTORY/full" && | |
46 | git commit-graph verify | |
47 | ' | |
48 | ||
49 | test_expect_success 'write graph with no packs' ' | |
50 | cd "$TRASH_DIRECTORY/full" && | |
51 | git commit-graph write --object-dir $objdir && | |
52 | test_path_is_missing $objdir/info/commit-graph | |
53 | ' | |
54 | ||
55 | test_expect_success 'exit with correct error on bad input to --stdin-packs' ' | |
56 | cd "$TRASH_DIRECTORY/full" && | |
57 | echo doesnotexist >in && | |
58 | test_expect_code 1 git commit-graph write --stdin-packs <in 2>stderr && | |
59 | test_i18ngrep "error adding pack" stderr | |
60 | ' | |
61 | ||
62 | test_expect_success 'create commits and repack' ' | |
63 | cd "$TRASH_DIRECTORY/full" && | |
64 | for i in $(test_seq 3) | |
65 | do | |
66 | test_commit $i && | |
67 | git branch commits/$i || return 1 | |
68 | done && | |
69 | git repack | |
70 | ' | |
71 | ||
72 | graph_git_two_modes() { | |
73 | git -c core.commitGraph=true $1 >output && | |
74 | git -c core.commitGraph=false $1 >expect && | |
75 | test_cmp expect output | |
76 | } | |
77 | ||
78 | graph_git_behavior() { | |
79 | MSG=$1 | |
80 | DIR=$2 | |
81 | BRANCH=$3 | |
82 | COMPARE=$4 | |
83 | test_expect_success "check normal git operations: $MSG" ' | |
84 | cd "$TRASH_DIRECTORY/$DIR" && | |
85 | graph_git_two_modes "log --oneline $BRANCH" && | |
86 | graph_git_two_modes "log --topo-order $BRANCH" && | |
87 | graph_git_two_modes "log --graph $COMPARE..$BRANCH" && | |
88 | graph_git_two_modes "branch -vv" && | |
89 | graph_git_two_modes "merge-base -a $BRANCH $COMPARE" | |
90 | ' | |
91 | } | |
92 | ||
93 | graph_git_behavior 'no graph' full commits/3 commits/1 | |
94 | ||
95 | graph_read_expect() { | |
96 | OPTIONAL="" | |
97 | NUM_CHUNKS=3 | |
98 | if test ! -z "$2" | |
99 | then | |
100 | OPTIONAL=" $2" | |
101 | NUM_CHUNKS=$((3 + $(echo "$2" | wc -w))) | |
102 | fi | |
103 | cat >expect <<- EOF | |
104 | header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0 | |
105 | num_commits: $1 | |
106 | chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL | |
107 | options: | |
108 | EOF | |
109 | test-tool read-graph >output && | |
110 | test_cmp expect output | |
111 | } | |
112 | ||
113 | test_expect_success 'exit with correct error on bad input to --stdin-commits' ' | |
114 | cd "$TRASH_DIRECTORY/full" && | |
115 | # invalid, non-hex OID | |
116 | echo HEAD >in && | |
117 | test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr && | |
118 | test_i18ngrep "unexpected non-hex object ID: HEAD" stderr && | |
119 | # non-existent OID | |
120 | echo $ZERO_OID >in && | |
121 | test_expect_code 1 git commit-graph write --stdin-commits <in 2>stderr && | |
122 | test_i18ngrep "invalid object" stderr && | |
123 | # valid commit and tree OID | |
124 | git rev-parse HEAD HEAD^{tree} >in && | |
125 | git commit-graph write --stdin-commits <in && | |
126 | graph_read_expect 3 generation_data | |
127 | ' | |
128 | ||
129 | test_expect_success 'write graph' ' | |
130 | cd "$TRASH_DIRECTORY/full" && | |
131 | git commit-graph write && | |
132 | test_path_is_file $objdir/info/commit-graph && | |
133 | graph_read_expect "3" generation_data | |
134 | ' | |
135 | ||
136 | test_expect_success POSIXPERM 'write graph has correct permissions' ' | |
137 | test_path_is_file $objdir/info/commit-graph && | |
138 | echo "-r--r--r--" >expect && | |
139 | test_modebits $objdir/info/commit-graph >actual && | |
140 | test_cmp expect actual | |
141 | ' | |
142 | ||
143 | graph_git_behavior 'graph exists' full commits/3 commits/1 | |
144 | ||
145 | test_expect_success 'Add more commits' ' | |
146 | cd "$TRASH_DIRECTORY/full" && | |
147 | git reset --hard commits/1 && | |
148 | for i in $(test_seq 4 5) | |
149 | do | |
150 | test_commit $i && | |
151 | git branch commits/$i || return 1 | |
152 | done && | |
153 | git reset --hard commits/2 && | |
154 | for i in $(test_seq 6 7) | |
155 | do | |
156 | test_commit $i && | |
157 | git branch commits/$i || return 1 | |
158 | done && | |
159 | git reset --hard commits/2 && | |
160 | git merge commits/4 && | |
161 | git branch merge/1 && | |
162 | git reset --hard commits/4 && | |
163 | git merge commits/6 && | |
164 | git branch merge/2 && | |
165 | git reset --hard commits/3 && | |
166 | git merge commits/5 commits/7 && | |
167 | git branch merge/3 && | |
168 | git repack | |
169 | ' | |
170 | ||
171 | test_expect_success 'commit-graph write progress off for redirected stderr' ' | |
172 | cd "$TRASH_DIRECTORY/full" && | |
173 | git commit-graph write 2>err && | |
174 | test_must_be_empty err | |
175 | ' | |
176 | ||
177 | test_expect_success 'commit-graph write force progress on for stderr' ' | |
178 | cd "$TRASH_DIRECTORY/full" && | |
179 | GIT_PROGRESS_DELAY=0 git commit-graph write --progress 2>err && | |
180 | test_file_not_empty err | |
181 | ' | |
182 | ||
183 | test_expect_success 'commit-graph write with the --no-progress option' ' | |
184 | cd "$TRASH_DIRECTORY/full" && | |
185 | git commit-graph write --no-progress 2>err && | |
186 | test_must_be_empty err | |
187 | ' | |
188 | ||
189 | test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' ' | |
190 | cd "$TRASH_DIRECTORY/full" && | |
191 | git rev-parse commits/5 >in && | |
192 | git commit-graph write --stdin-commits <in 2>err && | |
193 | test_must_be_empty err | |
194 | ' | |
195 | ||
196 | test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' ' | |
197 | cd "$TRASH_DIRECTORY/full" && | |
198 | git rev-parse commits/5 >in && | |
199 | GIT_PROGRESS_DELAY=0 git commit-graph write --stdin-commits --progress <in 2>err && | |
200 | test_i18ngrep "Collecting commits from input" err | |
201 | ' | |
202 | ||
203 | test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' ' | |
204 | cd "$TRASH_DIRECTORY/full" && | |
205 | git rev-parse commits/5 >in && | |
206 | git commit-graph write --stdin-commits --no-progress <in 2>err && | |
207 | test_must_be_empty err | |
208 | ' | |
209 | ||
210 | test_expect_success 'commit-graph verify progress off for redirected stderr' ' | |
211 | cd "$TRASH_DIRECTORY/full" && | |
212 | git commit-graph verify 2>err && | |
213 | test_must_be_empty err | |
214 | ' | |
215 | ||
216 | test_expect_success 'commit-graph verify force progress on for stderr' ' | |
217 | cd "$TRASH_DIRECTORY/full" && | |
218 | GIT_PROGRESS_DELAY=0 git commit-graph verify --progress 2>err && | |
219 | test_file_not_empty err | |
220 | ' | |
221 | ||
222 | test_expect_success 'commit-graph verify with the --no-progress option' ' | |
223 | cd "$TRASH_DIRECTORY/full" && | |
224 | git commit-graph verify --no-progress 2>err && | |
225 | test_must_be_empty err | |
226 | ' | |
227 | ||
228 | # Current graph structure: | |
229 | # | |
230 | # __M3___ | |
231 | # / | \ | |
232 | # 3 M1 5 M2 7 | |
233 | # |/ \|/ \| | |
234 | # 2 4 6 | |
235 | # |___/____/ | |
236 | # 1 | |
237 | ||
238 | test_expect_success 'write graph with merges' ' | |
239 | cd "$TRASH_DIRECTORY/full" && | |
240 | git commit-graph write && | |
241 | test_path_is_file $objdir/info/commit-graph && | |
242 | graph_read_expect "10" "generation_data extra_edges" | |
243 | ' | |
244 | ||
245 | graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2 | |
246 | graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3 | |
247 | graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3 | |
248 | ||
249 | test_expect_success 'Add one more commit' ' | |
250 | cd "$TRASH_DIRECTORY/full" && | |
251 | test_commit 8 && | |
252 | git branch commits/8 && | |
253 | ls $objdir/pack | grep idx >existing-idx && | |
254 | git repack && | |
255 | ls $objdir/pack| grep idx | grep -v -f existing-idx >new-idx | |
256 | ' | |
257 | ||
258 | # Current graph structure: | |
259 | # | |
260 | # 8 | |
261 | # | | |
262 | # __M3___ | |
263 | # / | \ | |
264 | # 3 M1 5 M2 7 | |
265 | # |/ \|/ \| | |
266 | # 2 4 6 | |
267 | # |___/____/ | |
268 | # 1 | |
269 | ||
270 | graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1 | |
271 | graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2 | |
272 | ||
273 | test_expect_success 'write graph with new commit' ' | |
274 | cd "$TRASH_DIRECTORY/full" && | |
275 | git commit-graph write && | |
276 | test_path_is_file $objdir/info/commit-graph && | |
277 | graph_read_expect "11" "generation_data extra_edges" | |
278 | ' | |
279 | ||
280 | graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1 | |
281 | graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2 | |
282 | ||
283 | test_expect_success 'write graph with nothing new' ' | |
284 | cd "$TRASH_DIRECTORY/full" && | |
285 | git commit-graph write && | |
286 | test_path_is_file $objdir/info/commit-graph && | |
287 | graph_read_expect "11" "generation_data extra_edges" | |
288 | ' | |
289 | ||
290 | graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1 | |
291 | graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2 | |
292 | ||
293 | test_expect_success 'build graph from latest pack with closure' ' | |
294 | cd "$TRASH_DIRECTORY/full" && | |
295 | cat new-idx | git commit-graph write --stdin-packs && | |
296 | test_path_is_file $objdir/info/commit-graph && | |
297 | graph_read_expect "9" "generation_data extra_edges" | |
298 | ' | |
299 | ||
300 | graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1 | |
301 | graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2 | |
302 | ||
303 | test_expect_success 'build graph from commits with closure' ' | |
304 | cd "$TRASH_DIRECTORY/full" && | |
305 | git tag -a -m "merge" tag/merge merge/2 && | |
306 | git rev-parse tag/merge >commits-in && | |
307 | git rev-parse merge/1 >>commits-in && | |
308 | cat commits-in | git commit-graph write --stdin-commits && | |
309 | test_path_is_file $objdir/info/commit-graph && | |
310 | graph_read_expect "6" "generation_data" | |
311 | ' | |
312 | ||
313 | graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1 | |
314 | graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2 | |
315 | ||
316 | test_expect_success 'build graph from commits with append' ' | |
317 | cd "$TRASH_DIRECTORY/full" && | |
318 | git rev-parse merge/3 | git commit-graph write --stdin-commits --append && | |
319 | test_path_is_file $objdir/info/commit-graph && | |
320 | graph_read_expect "10" "generation_data extra_edges" | |
321 | ' | |
322 | ||
323 | graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1 | |
324 | graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2 | |
325 | ||
326 | test_expect_success 'build graph using --reachable' ' | |
327 | cd "$TRASH_DIRECTORY/full" && | |
328 | git commit-graph write --reachable && | |
329 | test_path_is_file $objdir/info/commit-graph && | |
330 | graph_read_expect "11" "generation_data extra_edges" | |
331 | ' | |
332 | ||
333 | graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1 | |
334 | graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2 | |
335 | ||
336 | test_expect_success 'setup bare repo' ' | |
337 | cd "$TRASH_DIRECTORY" && | |
338 | git clone --bare --no-local full bare && | |
339 | cd bare && | |
340 | git config core.commitGraph true && | |
341 | baredir="./objects" | |
342 | ' | |
343 | ||
344 | graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1 | |
345 | graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2 | |
346 | ||
347 | test_expect_success 'write graph in bare repo' ' | |
348 | cd "$TRASH_DIRECTORY/bare" && | |
349 | git commit-graph write && | |
350 | test_path_is_file $baredir/info/commit-graph && | |
351 | graph_read_expect "11" "generation_data extra_edges" | |
352 | ' | |
353 | ||
354 | graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1 | |
355 | graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2 | |
356 | ||
357 | test_expect_success 'perform fast-forward merge in full repo' ' | |
358 | cd "$TRASH_DIRECTORY/full" && | |
359 | git checkout -b merge-5-to-8 commits/5 && | |
360 | git merge commits/8 && | |
361 | git show-ref -s merge-5-to-8 >output && | |
362 | git show-ref -s commits/8 >expect && | |
363 | test_cmp expect output | |
364 | ' | |
365 | ||
366 | test_expect_success 'check that gc computes commit-graph' ' | |
367 | cd "$TRASH_DIRECTORY/full" && | |
368 | git commit --allow-empty -m "blank" && | |
369 | git commit-graph write --reachable && | |
370 | cp $objdir/info/commit-graph commit-graph-before-gc && | |
371 | git reset --hard HEAD~1 && | |
372 | git config gc.writeCommitGraph true && | |
373 | git gc && | |
374 | cp $objdir/info/commit-graph commit-graph-after-gc && | |
375 | ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc && | |
376 | git commit-graph write --reachable && | |
377 | test_cmp_bin commit-graph-after-gc $objdir/info/commit-graph | |
378 | ' | |
379 | ||
380 | test_expect_success 'replace-objects invalidates commit-graph' ' | |
381 | cd "$TRASH_DIRECTORY" && | |
382 | test_when_finished rm -rf replace && | |
383 | git clone full replace && | |
384 | ( | |
385 | cd replace && | |
386 | git commit-graph write --reachable && | |
387 | test_path_is_file .git/objects/info/commit-graph && | |
388 | git replace HEAD~1 HEAD~2 && | |
389 | graph_git_two_modes "commit-graph verify" && | |
390 | git -c core.commitGraph=false log >expect && | |
391 | git -c core.commitGraph=true log >actual && | |
392 | test_cmp expect actual && | |
393 | git commit-graph write --reachable && | |
394 | git -c core.commitGraph=false --no-replace-objects log >expect && | |
395 | git -c core.commitGraph=true --no-replace-objects log >actual && | |
396 | test_cmp expect actual && | |
397 | rm -rf .git/objects/info/commit-graph && | |
398 | git commit-graph write --reachable && | |
399 | test_path_is_file .git/objects/info/commit-graph | |
400 | ) | |
401 | ' | |
402 | ||
403 | test_expect_success 'commit grafts invalidate commit-graph' ' | |
404 | cd "$TRASH_DIRECTORY" && | |
405 | test_when_finished rm -rf graft && | |
406 | git clone full graft && | |
407 | ( | |
408 | cd graft && | |
409 | git commit-graph write --reachable && | |
410 | test_path_is_file .git/objects/info/commit-graph && | |
411 | H1=$(git rev-parse --verify HEAD~1) && | |
412 | H3=$(git rev-parse --verify HEAD~3) && | |
413 | echo "$H1 $H3" >.git/info/grafts && | |
414 | git -c core.commitGraph=false log >expect && | |
415 | git -c core.commitGraph=true log >actual && | |
416 | test_cmp expect actual && | |
417 | git commit-graph write --reachable && | |
418 | git -c core.commitGraph=false --no-replace-objects log >expect && | |
419 | git -c core.commitGraph=true --no-replace-objects log >actual && | |
420 | test_cmp expect actual && | |
421 | rm -rf .git/objects/info/commit-graph && | |
422 | git commit-graph write --reachable && | |
423 | test_path_is_missing .git/objects/info/commit-graph | |
424 | ) | |
425 | ' | |
426 | ||
427 | test_expect_success 'replace-objects invalidates commit-graph' ' | |
428 | cd "$TRASH_DIRECTORY" && | |
429 | test_when_finished rm -rf shallow && | |
430 | git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow && | |
431 | ( | |
432 | cd shallow && | |
433 | git commit-graph write --reachable && | |
434 | test_path_is_missing .git/objects/info/commit-graph && | |
435 | git fetch origin --unshallow && | |
436 | git commit-graph write --reachable && | |
437 | test_path_is_file .git/objects/info/commit-graph | |
438 | ) | |
439 | ' | |
440 | ||
441 | test_expect_success 'warn on improper hash version' ' | |
442 | git init --object-format=sha1 sha1 && | |
443 | ( | |
444 | cd sha1 && | |
445 | test_commit 1 && | |
446 | git commit-graph write --reachable && | |
447 | mv .git/objects/info/commit-graph ../cg-sha1 | |
448 | ) && | |
449 | git init --object-format=sha256 sha256 && | |
450 | ( | |
451 | cd sha256 && | |
452 | test_commit 1 && | |
453 | git commit-graph write --reachable && | |
454 | mv .git/objects/info/commit-graph ../cg-sha256 | |
455 | ) && | |
456 | ( | |
457 | cd sha1 && | |
458 | mv ../cg-sha256 .git/objects/info/commit-graph && | |
459 | git log -1 2>err && | |
460 | test_i18ngrep "commit-graph hash version 2 does not match version 1" err | |
461 | ) && | |
462 | ( | |
463 | cd sha256 && | |
464 | mv ../cg-sha1 .git/objects/info/commit-graph && | |
465 | git log -1 2>err && | |
466 | test_i18ngrep "commit-graph hash version 1 does not match version 2" err | |
467 | ) | |
468 | ' | |
469 | ||
470 | test_expect_success 'lower layers have overflow chunk' ' | |
471 | cd "$TRASH_DIRECTORY/full" && | |
472 | UNIX_EPOCH_ZERO="@0 +0000" && | |
473 | FUTURE_DATE="@2147483646 +0000" && | |
474 | rm -f .git/objects/info/commit-graph && | |
475 | test_commit --date "$FUTURE_DATE" future-1 && | |
476 | test_commit --date "$UNIX_EPOCH_ZERO" old-1 && | |
477 | git commit-graph write --reachable && | |
478 | test_commit --date "$FUTURE_DATE" future-2 && | |
479 | test_commit --date "$UNIX_EPOCH_ZERO" old-2 && | |
480 | git commit-graph write --reachable --split=no-merge && | |
481 | test_commit extra && | |
482 | git commit-graph write --reachable --split=no-merge && | |
483 | git commit-graph write --reachable && | |
484 | graph_read_expect 16 "generation_data generation_data_overflow extra_edges" && | |
485 | mv .git/objects/info/commit-graph commit-graph-upgraded && | |
486 | git commit-graph write --reachable && | |
487 | graph_read_expect 16 "generation_data generation_data_overflow extra_edges" && | |
488 | test_cmp .git/objects/info/commit-graph commit-graph-upgraded | |
489 | ' | |
490 | ||
491 | # the verify tests below expect the commit-graph to contain | |
492 | # exactly the commits reachable from the commits/8 branch. | |
493 | # If the file changes the set of commits in the list, then the | |
494 | # offsets into the binary file will result in different edits | |
495 | # and the tests will likely break. | |
496 | ||
497 | test_expect_success 'git commit-graph verify' ' | |
498 | cd "$TRASH_DIRECTORY/full" && | |
499 | git rev-parse commits/8 | git -c commitGraph.generationVersion=1 commit-graph write --stdin-commits && | |
500 | git commit-graph verify >output && | |
501 | graph_read_expect 9 extra_edges | |
502 | ' | |
503 | ||
504 | NUM_COMMITS=9 | |
505 | NUM_OCTOPUS_EDGES=2 | |
506 | HASH_LEN="$(test_oid rawsz)" | |
507 | GRAPH_BYTE_VERSION=4 | |
508 | GRAPH_BYTE_HASH=5 | |
509 | GRAPH_BYTE_CHUNK_COUNT=6 | |
510 | GRAPH_CHUNK_LOOKUP_OFFSET=8 | |
511 | GRAPH_CHUNK_LOOKUP_WIDTH=12 | |
512 | GRAPH_CHUNK_LOOKUP_ROWS=5 | |
513 | GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET | |
514 | GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \ | |
515 | 1 * $GRAPH_CHUNK_LOOKUP_WIDTH)) | |
516 | GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \ | |
517 | 2 * $GRAPH_CHUNK_LOOKUP_WIDTH)) | |
518 | GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \ | |
519 | $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS)) | |
520 | GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4)) | |
521 | GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255)) | |
522 | GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256)) | |
523 | GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8)) | |
524 | GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10)) | |
525 | GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS)) | |
526 | GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET | |
527 | GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN)) | |
528 | GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4)) | |
529 | GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3)) | |
530 | GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11)) | |
531 | GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12)) | |
532 | GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16)) | |
533 | GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \ | |
534 | $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS)) | |
535 | GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4)) | |
536 | GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES)) | |
537 | ||
538 | corrupt_graph_setup() { | |
539 | cd "$TRASH_DIRECTORY/full" && | |
540 | test_when_finished mv commit-graph-backup $objdir/info/commit-graph && | |
541 | cp $objdir/info/commit-graph commit-graph-backup && | |
542 | chmod u+w $objdir/info/commit-graph | |
543 | } | |
544 | ||
545 | corrupt_graph_verify() { | |
546 | grepstr=$1 | |
547 | test_must_fail git commit-graph verify 2>test_err && | |
548 | grep -v "^+" test_err >err && | |
549 | test_i18ngrep "$grepstr" err && | |
550 | if test "$2" != "no-copy" | |
551 | then | |
552 | cp $objdir/info/commit-graph commit-graph-pre-write-test | |
553 | fi && | |
554 | git status --short && | |
555 | GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git commit-graph write && | |
556 | chmod u+w $objdir/info/commit-graph && | |
557 | git commit-graph verify | |
558 | } | |
559 | ||
560 | # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>] | |
561 | # Manipulates the commit-graph file at the position | |
562 | # by inserting the data, optionally zeroing the file | |
563 | # starting at <zero_pos>, then runs 'git commit-graph verify' | |
564 | # and places the output in the file 'err'. Test 'err' for | |
565 | # the given string. | |
566 | corrupt_graph_and_verify() { | |
567 | pos=$1 | |
568 | data="${2:-\0}" | |
569 | grepstr=$3 | |
570 | corrupt_graph_setup && | |
571 | orig_size=$(wc -c < $objdir/info/commit-graph) && | |
572 | zero_pos=${4:-${orig_size}} && | |
573 | printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc && | |
574 | dd of="$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null && | |
575 | test-tool genzeros $(($orig_size - $zero_pos)) >>"$objdir/info/commit-graph" && | |
576 | corrupt_graph_verify "$grepstr" | |
577 | ||
578 | } | |
579 | ||
580 | test_expect_success POSIXPERM,SANITY 'detect permission problem' ' | |
581 | corrupt_graph_setup && | |
582 | chmod 000 $objdir/info/commit-graph && | |
583 | corrupt_graph_verify "Could not open" "no-copy" | |
584 | ' | |
585 | ||
586 | test_expect_success 'detect too small' ' | |
587 | corrupt_graph_setup && | |
588 | echo "a small graph" >$objdir/info/commit-graph && | |
589 | corrupt_graph_verify "too small" | |
590 | ' | |
591 | ||
592 | test_expect_success 'detect bad signature' ' | |
593 | corrupt_graph_and_verify 0 "\0" \ | |
594 | "graph signature" | |
595 | ' | |
596 | ||
597 | test_expect_success 'detect bad version' ' | |
598 | corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \ | |
599 | "graph version" | |
600 | ' | |
601 | ||
602 | test_expect_success 'detect bad hash version' ' | |
603 | corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \ | |
604 | "hash version" | |
605 | ' | |
606 | ||
607 | test_expect_success 'detect low chunk count' ' | |
608 | corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \ | |
609 | "final chunk has non-zero id" | |
610 | ' | |
611 | ||
612 | test_expect_success 'detect missing OID fanout chunk' ' | |
613 | corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \ | |
614 | "missing the OID Fanout chunk" | |
615 | ' | |
616 | ||
617 | test_expect_success 'detect missing OID lookup chunk' ' | |
618 | corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \ | |
619 | "missing the OID Lookup chunk" | |
620 | ' | |
621 | ||
622 | test_expect_success 'detect missing commit data chunk' ' | |
623 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \ | |
624 | "missing the Commit Data chunk" | |
625 | ' | |
626 | ||
627 | test_expect_success 'detect incorrect fanout' ' | |
628 | corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \ | |
629 | "fanout value" | |
630 | ' | |
631 | ||
632 | test_expect_success 'detect incorrect fanout final value' ' | |
633 | corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \ | |
634 | "fanout value" | |
635 | ' | |
636 | ||
637 | test_expect_success 'detect incorrect OID order' ' | |
638 | corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \ | |
639 | "incorrect OID order" | |
640 | ' | |
641 | ||
642 | test_expect_success 'detect OID not in object database' ' | |
643 | corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \ | |
644 | "from object database" | |
645 | ' | |
646 | ||
647 | test_expect_success 'detect incorrect tree OID' ' | |
648 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \ | |
649 | "root tree OID for commit" | |
650 | ' | |
651 | ||
652 | test_expect_success 'detect incorrect parent int-id' ' | |
653 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \ | |
654 | "invalid parent" | |
655 | ' | |
656 | ||
657 | test_expect_success 'detect extra parent int-id' ' | |
658 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \ | |
659 | "is too long" | |
660 | ' | |
661 | ||
662 | test_expect_success 'detect wrong parent' ' | |
663 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \ | |
664 | "commit-graph parent for" | |
665 | ' | |
666 | ||
667 | test_expect_success 'detect incorrect generation number' ' | |
668 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \ | |
669 | "generation for commit" | |
670 | ' | |
671 | ||
672 | test_expect_success 'detect incorrect generation number' ' | |
673 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \ | |
674 | "non-zero generation number" | |
675 | ' | |
676 | ||
677 | test_expect_success 'detect incorrect commit date' ' | |
678 | corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \ | |
679 | "commit date" | |
680 | ' | |
681 | ||
682 | test_expect_success 'detect incorrect parent for octopus merge' ' | |
683 | corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \ | |
684 | "invalid parent" | |
685 | ' | |
686 | ||
687 | test_expect_success 'detect invalid checksum hash' ' | |
688 | corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \ | |
689 | "incorrect checksum" | |
690 | ' | |
691 | ||
692 | test_expect_success 'detect incorrect chunk count' ' | |
693 | corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \ | |
694 | "commit-graph file is too small to hold [0-9]* chunks" \ | |
695 | $GRAPH_CHUNK_LOOKUP_OFFSET | |
696 | ' | |
697 | ||
698 | test_expect_success 'git fsck (checks commit-graph when config set to true)' ' | |
699 | cd "$TRASH_DIRECTORY/full" && | |
700 | git fsck && | |
701 | corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \ | |
702 | "incorrect checksum" && | |
703 | cp commit-graph-pre-write-test $objdir/info/commit-graph && | |
704 | test_must_fail git -c core.commitGraph=true fsck | |
705 | ' | |
706 | ||
707 | test_expect_success 'git fsck (ignores commit-graph when config set to false)' ' | |
708 | cd "$TRASH_DIRECTORY/full" && | |
709 | git fsck && | |
710 | corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \ | |
711 | "incorrect checksum" && | |
712 | cp commit-graph-pre-write-test $objdir/info/commit-graph && | |
713 | git -c core.commitGraph=false fsck | |
714 | ' | |
715 | ||
716 | test_expect_success 'git fsck (checks commit-graph when config unset)' ' | |
717 | cd "$TRASH_DIRECTORY/full" && | |
718 | test_when_finished "git config core.commitGraph true" && | |
719 | ||
720 | git fsck && | |
721 | corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \ | |
722 | "incorrect checksum" && | |
723 | test_unconfig core.commitGraph && | |
724 | cp commit-graph-pre-write-test $objdir/info/commit-graph && | |
725 | test_must_fail git fsck | |
726 | ' | |
727 | ||
728 | test_expect_success 'setup non-the_repository tests' ' | |
729 | rm -rf repo && | |
730 | git init repo && | |
731 | test_commit -C repo one && | |
732 | test_commit -C repo two && | |
733 | git -C repo config core.commitGraph true && | |
734 | git -C repo rev-parse two | \ | |
735 | git -C repo commit-graph write --stdin-commits | |
736 | ' | |
737 | ||
738 | test_expect_success 'parse_commit_in_graph works for non-the_repository' ' | |
739 | test-tool repository parse_commit_in_graph \ | |
740 | repo/.git repo "$(git -C repo rev-parse two)" >actual && | |
741 | { | |
742 | git -C repo log --pretty=format:"%ct " -1 && | |
743 | git -C repo rev-parse one | |
744 | } >expect && | |
745 | test_cmp expect actual && | |
746 | ||
747 | test-tool repository parse_commit_in_graph \ | |
748 | repo/.git repo "$(git -C repo rev-parse one)" >actual && | |
749 | git -C repo log --pretty="%ct" -1 one >expect && | |
750 | test_cmp expect actual | |
751 | ' | |
752 | ||
753 | test_expect_success 'get_commit_tree_in_graph works for non-the_repository' ' | |
754 | test-tool repository get_commit_tree_in_graph \ | |
755 | repo/.git repo "$(git -C repo rev-parse two)" >actual && | |
756 | git -C repo rev-parse two^{tree} >expect && | |
757 | test_cmp expect actual && | |
758 | ||
759 | test-tool repository get_commit_tree_in_graph \ | |
760 | repo/.git repo "$(git -C repo rev-parse one)" >actual && | |
761 | git -C repo rev-parse one^{tree} >expect && | |
762 | test_cmp expect actual | |
763 | ' | |
764 | ||
765 | test_expect_success 'corrupt commit-graph write (broken parent)' ' | |
766 | rm -rf repo && | |
767 | git init repo && | |
768 | ( | |
769 | cd repo && | |
770 | empty="$(git mktree </dev/null)" && | |
771 | cat >broken <<-EOF && | |
772 | tree $empty | |
773 | parent $ZERO_OID | |
774 | author whatever <whatever@example.com> 1234 -0000 | |
775 | committer whatever <whatever@example.com> 1234 -0000 | |
776 | ||
777 | broken commit | |
778 | EOF | |
779 | broken="$(git hash-object -w -t commit --literally broken)" && | |
780 | git commit-tree -p "$broken" -m "good commit" "$empty" >good && | |
781 | test_must_fail git commit-graph write --stdin-commits \ | |
782 | <good 2>test_err && | |
783 | test_i18ngrep "unable to parse commit" test_err | |
784 | ) | |
785 | ' | |
786 | ||
787 | test_expect_success 'corrupt commit-graph write (missing tree)' ' | |
788 | rm -rf repo && | |
789 | git init repo && | |
790 | ( | |
791 | cd repo && | |
792 | tree="$(git mktree </dev/null)" && | |
793 | cat >broken <<-EOF && | |
794 | parent $ZERO_OID | |
795 | author whatever <whatever@example.com> 1234 -0000 | |
796 | committer whatever <whatever@example.com> 1234 -0000 | |
797 | ||
798 | broken commit | |
799 | EOF | |
800 | broken="$(git hash-object -w -t commit --literally broken)" && | |
801 | git commit-tree -p "$broken" -m "good" "$tree" >good && | |
802 | test_must_fail git commit-graph write --stdin-commits \ | |
803 | <good 2>test_err && | |
804 | test_i18ngrep "unable to parse commit" test_err | |
805 | ) | |
806 | ' | |
807 | ||
808 | # We test the overflow-related code with the following repo history: | |
809 | # | |
810 | # 4:F - 5:N - 6:U | |
811 | # / \ | |
812 | # 1:U - 2:N - 3:U M:N | |
813 | # \ / | |
814 | # 7:N - 8:F - 9:N | |
815 | # | |
816 | # Here the commits denoted by U have committer date of zero seconds | |
817 | # since Unix epoch, the commits denoted by N have committer date | |
818 | # starting from 1112354055 seconds since Unix epoch (default committer | |
819 | # date for the test suite), and the commits denoted by F have committer | |
820 | # date of (2 ^ 31 - 2) seconds since Unix epoch. | |
821 | # | |
822 | # The largest offset observed is 2 ^ 31, just large enough to overflow. | |
823 | # | |
824 | ||
825 | test_expect_success 'set up and verify repo with generation data overflow chunk' ' | |
826 | objdir=".git/objects" && | |
827 | UNIX_EPOCH_ZERO="@0 +0000" && | |
828 | FUTURE_DATE="@2147483646 +0000" && | |
829 | test_oid_cache <<-EOF && | |
830 | oid_version sha1:1 | |
831 | oid_version sha256:2 | |
832 | EOF | |
833 | cd "$TRASH_DIRECTORY" && | |
834 | mkdir repo && | |
835 | cd repo && | |
836 | git init && | |
837 | test_commit --date "$UNIX_EPOCH_ZERO" 1 && | |
838 | test_commit 2 && | |
839 | test_commit --date "$UNIX_EPOCH_ZERO" 3 && | |
840 | git commit-graph write --reachable && | |
841 | graph_read_expect 3 generation_data && | |
842 | test_commit --date "$FUTURE_DATE" 4 && | |
843 | test_commit 5 && | |
844 | test_commit --date "$UNIX_EPOCH_ZERO" 6 && | |
845 | git branch left && | |
846 | git reset --hard 3 && | |
847 | test_commit 7 && | |
848 | test_commit --date "$FUTURE_DATE" 8 && | |
849 | test_commit 9 && | |
850 | git branch right && | |
851 | git reset --hard 3 && | |
852 | test_merge M left right && | |
853 | git commit-graph write --reachable && | |
854 | graph_read_expect 10 "generation_data generation_data_overflow" && | |
855 | git commit-graph verify | |
856 | ' | |
857 | ||
858 | graph_git_behavior 'generation data overflow chunk repo' repo left right | |
859 | ||
860 | test_done |