commit-graph: use string-list API for input
[git] / t / t5318-commit-graph.sh
1 #!/bin/sh
2
3 test_description='commit graph'
4 . ./test-lib.sh
5
6 test_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 '
13
14 test_expect_success 'verify graph with no graph file' '
15         cd "$TRASH_DIRECTORY/full" &&
16         git commit-graph verify
17 '
18
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
23 '
24
25 test_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
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
39 }
40
41 graph_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
56 graph_git_behavior 'no graph' full commits/3 commits/1
57
58 graph_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
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 &&
79         graph_read_expect "3"
80 '
81
82 graph_git_behavior 'graph exists' full commits/3 commits/1
83
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)
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
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"
125 '
126
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
130
131 test_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
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
154
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"
160 '
161
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
164
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"
170 '
171
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
174
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"
180 '
181
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
184
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"
193 '
194
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
197
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"
203 '
204
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
207
208 test_expect_success 'setup bare repo' '
209         cd "$TRASH_DIRECTORY" &&
210         git clone --bare --no-local full bare &&
211         cd bare &&
212         git config core.commitGraph true &&
213         baredir="./objects"
214 '
215
216 graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
217 graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
218
219 test_expect_success 'write graph in bare repo' '
220         cd "$TRASH_DIRECTORY/bare" &&
221         git commit-graph write &&
222         test_path_is_file $baredir/info/commit-graph &&
223         graph_read_expect "11" "large_edges"
224 '
225
226 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
227 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
228
229 test_expect_success 'perform fast-forward merge in full repo' '
230         cd "$TRASH_DIRECTORY/full" &&
231         git checkout -b merge-5-to-8 commits/5 &&
232         git merge commits/8 &&
233         git show-ref -s merge-5-to-8 >output &&
234         git show-ref -s commits/8 >expect &&
235         test_cmp expect output
236 '
237
238 # the verify tests below expect the commit-graph to contain
239 # exactly the commits reachable from the commits/8 branch.
240 # If the file changes the set of commits in the list, then the
241 # offsets into the binary file will result in different edits
242 # and the tests will likely break.
243
244 test_expect_success 'git commit-graph verify' '
245         cd "$TRASH_DIRECTORY/full" &&
246         git rev-parse commits/8 | git commit-graph write --stdin-commits &&
247         git commit-graph verify >output
248 '
249
250 NUM_COMMITS=9
251 NUM_OCTOPUS_EDGES=2
252 HASH_LEN=20
253 GRAPH_BYTE_VERSION=4
254 GRAPH_BYTE_HASH=5
255 GRAPH_BYTE_CHUNK_COUNT=6
256 GRAPH_CHUNK_LOOKUP_OFFSET=8
257 GRAPH_CHUNK_LOOKUP_WIDTH=12
258 GRAPH_CHUNK_LOOKUP_ROWS=5
259 GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
260 GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
261                             1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
262 GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
263                              2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
264 GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
265                        $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
266 GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
267 GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
268 GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
269 GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
270 GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
271 GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
272 GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
273 GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
274 GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
275 GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
276 GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
277 GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
278 GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
279 GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
280                              $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
281 GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
282 GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
283
284 # usage: corrupt_graph_and_verify <position> <data> <string>
285 # Manipulates the commit-graph file at the position
286 # by inserting the data, then runs 'git commit-graph verify'
287 # and places the output in the file 'err'. Test 'err' for
288 # the given string.
289 corrupt_graph_and_verify() {
290         pos=$1
291         data="${2:-\0}"
292         grepstr=$3
293         cd "$TRASH_DIRECTORY/full" &&
294         test_when_finished mv commit-graph-backup $objdir/info/commit-graph &&
295         cp $objdir/info/commit-graph commit-graph-backup &&
296         printf "$data" | dd of="$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
297         test_must_fail git commit-graph verify 2>test_err &&
298         grep -v "^+" test_err >err
299         test_i18ngrep "$grepstr" err
300 }
301
302 test_expect_success 'detect bad signature' '
303         corrupt_graph_and_verify 0 "\0" \
304                 "graph signature"
305 '
306
307 test_expect_success 'detect bad version' '
308         corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
309                 "graph version"
310 '
311
312 test_expect_success 'detect bad hash version' '
313         corrupt_graph_and_verify $GRAPH_BYTE_HASH "\02" \
314                 "hash version"
315 '
316
317 test_expect_success 'detect low chunk count' '
318         corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\02" \
319                 "missing the .* chunk"
320 '
321
322 test_expect_success 'detect missing OID fanout chunk' '
323         corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
324                 "missing the OID Fanout chunk"
325 '
326
327 test_expect_success 'detect missing OID lookup chunk' '
328         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
329                 "missing the OID Lookup chunk"
330 '
331
332 test_expect_success 'detect missing commit data chunk' '
333         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
334                 "missing the Commit Data chunk"
335 '
336
337 test_expect_success 'detect incorrect fanout' '
338         corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
339                 "fanout value"
340 '
341
342 test_expect_success 'detect incorrect fanout final value' '
343         corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
344                 "fanout value"
345 '
346
347 test_expect_success 'detect incorrect OID order' '
348         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
349                 "incorrect OID order"
350 '
351
352 test_expect_success 'detect OID not in object database' '
353         corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
354                 "from object database"
355 '
356
357 test_expect_success 'detect incorrect tree OID' '
358         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
359                 "root tree OID for commit"
360 '
361
362 test_expect_success 'detect incorrect parent int-id' '
363         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
364                 "invalid parent"
365 '
366
367 test_expect_success 'detect extra parent int-id' '
368         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
369                 "is too long"
370 '
371
372 test_expect_success 'detect wrong parent' '
373         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
374                 "commit-graph parent for"
375 '
376
377 test_expect_success 'detect incorrect generation number' '
378         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
379                 "generation for commit"
380 '
381
382 test_expect_success 'detect incorrect generation number' '
383         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
384                 "non-zero generation number"
385 '
386
387 test_expect_success 'detect incorrect commit date' '
388         corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
389                 "commit date"
390 '
391
392 test_expect_success 'detect incorrect parent for octopus merge' '
393         corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
394                 "invalid parent"
395 '
396
397 test_expect_success 'detect invalid checksum hash' '
398         corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
399                 "incorrect checksum"
400 '
401
402 test_expect_success 'git fsck (checks commit-graph)' '
403         cd "$TRASH_DIRECTORY/full" &&
404         git fsck &&
405         corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
406                 "incorrect checksum" &&
407         test_must_fail git fsck
408 '
409
410 test_done