commit-graph.c: ensure graph layers respect core.sharedRepository
[git] / t / t5324-split-commit-graph.sh
1 #!/bin/sh
2
3 test_description='split commit graph'
4 . ./test-lib.sh
5
6 GIT_TEST_COMMIT_GRAPH=0
7
8 test_expect_success 'setup repo' '
9         git init &&
10         git config core.commitGraph true &&
11         git config gc.writeCommitGraph false &&
12         infodir=".git/objects/info" &&
13         graphdir="$infodir/commit-graphs" &&
14         test_oid_init &&
15         test_oid_cache <<-EOM
16         shallow sha1:1760
17         shallow sha256:2064
18
19         base sha1:1376
20         base sha256:1496
21         EOM
22 '
23
24 graph_read_expect() {
25         NUM_BASE=0
26         if test ! -z $2
27         then
28                 NUM_BASE=$2
29         fi
30         cat >expect <<- EOF
31         header: 43475048 1 1 3 $NUM_BASE
32         num_commits: $1
33         chunks: oid_fanout oid_lookup commit_metadata
34         EOF
35         test-tool read-graph >output &&
36         test_cmp expect output
37 }
38
39 test_expect_success POSIXPERM 'tweak umask for modebit tests' '
40         umask 022
41 '
42
43 test_expect_success 'create commits and write commit-graph' '
44         for i in $(test_seq 3)
45         do
46                 test_commit $i &&
47                 git branch commits/$i || return 1
48         done &&
49         git commit-graph write --reachable &&
50         test_path_is_file $infodir/commit-graph &&
51         graph_read_expect 3
52 '
53
54 graph_git_two_modes() {
55         git -c core.commitGraph=true $1 >output
56         git -c core.commitGraph=false $1 >expect
57         test_cmp expect output
58 }
59
60 graph_git_behavior() {
61         MSG=$1
62         BRANCH=$2
63         COMPARE=$3
64         test_expect_success "check normal git operations: $MSG" '
65                 graph_git_two_modes "log --oneline $BRANCH" &&
66                 graph_git_two_modes "log --topo-order $BRANCH" &&
67                 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
68                 graph_git_two_modes "branch -vv" &&
69                 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
70         '
71 }
72
73 graph_git_behavior 'graph exists' commits/3 commits/1
74
75 verify_chain_files_exist() {
76         for hash in $(cat $1/commit-graph-chain)
77         do
78                 test_path_is_file $1/graph-$hash.graph || return 1
79         done
80 }
81
82 test_expect_success 'add more commits, and write a new base graph' '
83         git reset --hard commits/1 &&
84         for i in $(test_seq 4 5)
85         do
86                 test_commit $i &&
87                 git branch commits/$i || return 1
88         done &&
89         git reset --hard commits/2 &&
90         for i in $(test_seq 6 10)
91         do
92                 test_commit $i &&
93                 git branch commits/$i || return 1
94         done &&
95         git reset --hard commits/2 &&
96         git merge commits/4 &&
97         git branch merge/1 &&
98         git reset --hard commits/4 &&
99         git merge commits/6 &&
100         git branch merge/2 &&
101         git commit-graph write --reachable &&
102         graph_read_expect 12
103 '
104
105 test_expect_success 'fork and fail to base a chain on a commit-graph file' '
106         test_when_finished rm -rf fork &&
107         git clone . fork &&
108         (
109                 cd fork &&
110                 rm .git/objects/info/commit-graph &&
111                 echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
112                 test_commit new-commit &&
113                 git commit-graph write --reachable --split &&
114                 test_path_is_file $graphdir/commit-graph-chain &&
115                 test_line_count = 1 $graphdir/commit-graph-chain &&
116                 verify_chain_files_exist $graphdir
117         )
118 '
119
120 test_expect_success 'add three more commits, write a tip graph' '
121         git reset --hard commits/3 &&
122         git merge merge/1 &&
123         git merge commits/5 &&
124         git merge merge/2 &&
125         git branch merge/3 &&
126         git commit-graph write --reachable --split &&
127         test_path_is_missing $infodir/commit-graph &&
128         test_path_is_file $graphdir/commit-graph-chain &&
129         ls $graphdir/graph-*.graph >graph-files &&
130         test_line_count = 2 graph-files &&
131         verify_chain_files_exist $graphdir
132 '
133
134 graph_git_behavior 'split commit-graph: merge 3 vs 2' merge/3 merge/2
135
136 test_expect_success 'add one commit, write a tip graph' '
137         test_commit 11 &&
138         git branch commits/11 &&
139         git commit-graph write --reachable --split &&
140         test_path_is_missing $infodir/commit-graph &&
141         test_path_is_file $graphdir/commit-graph-chain &&
142         ls $graphdir/graph-*.graph >graph-files &&
143         test_line_count = 3 graph-files &&
144         verify_chain_files_exist $graphdir
145 '
146
147 graph_git_behavior 'three-layer commit-graph: commit 11 vs 6' commits/11 commits/6
148
149 test_expect_success 'add one commit, write a merged graph' '
150         test_commit 12 &&
151         git branch commits/12 &&
152         git commit-graph write --reachable --split &&
153         test_path_is_file $graphdir/commit-graph-chain &&
154         test_line_count = 2 $graphdir/commit-graph-chain &&
155         ls $graphdir/graph-*.graph >graph-files &&
156         test_line_count = 2 graph-files &&
157         verify_chain_files_exist $graphdir
158 '
159
160 graph_git_behavior 'merged commit-graph: commit 12 vs 6' commits/12 commits/6
161
162 test_expect_success 'create fork and chain across alternate' '
163         git clone . fork &&
164         (
165                 cd fork &&
166                 git config core.commitGraph true &&
167                 rm -rf $graphdir &&
168                 echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
169                 test_commit 13 &&
170                 git branch commits/13 &&
171                 git commit-graph write --reachable --split &&
172                 test_path_is_file $graphdir/commit-graph-chain &&
173                 test_line_count = 3 $graphdir/commit-graph-chain &&
174                 ls $graphdir/graph-*.graph >graph-files &&
175                 test_line_count = 1 graph-files &&
176                 git -c core.commitGraph=true  rev-list HEAD >expect &&
177                 git -c core.commitGraph=false rev-list HEAD >actual &&
178                 test_cmp expect actual &&
179                 test_commit 14 &&
180                 git commit-graph write --reachable --split --object-dir=.git/objects/ &&
181                 test_line_count = 3 $graphdir/commit-graph-chain &&
182                 ls $graphdir/graph-*.graph >graph-files &&
183                 test_line_count = 1 graph-files
184         )
185 '
186
187 graph_git_behavior 'alternate: commit 13 vs 6' commits/13 commits/6
188
189 test_expect_success 'test merge stragety constants' '
190         git clone . merge-2 &&
191         (
192                 cd merge-2 &&
193                 git config core.commitGraph true &&
194                 test_line_count = 2 $graphdir/commit-graph-chain &&
195                 test_commit 14 &&
196                 git commit-graph write --reachable --split --size-multiple=2 &&
197                 test_line_count = 3 $graphdir/commit-graph-chain
198
199         ) &&
200         git clone . merge-10 &&
201         (
202                 cd merge-10 &&
203                 git config core.commitGraph true &&
204                 test_line_count = 2 $graphdir/commit-graph-chain &&
205                 test_commit 14 &&
206                 git commit-graph write --reachable --split --size-multiple=10 &&
207                 test_line_count = 1 $graphdir/commit-graph-chain &&
208                 ls $graphdir/graph-*.graph >graph-files &&
209                 test_line_count = 1 graph-files
210         ) &&
211         git clone . merge-10-expire &&
212         (
213                 cd merge-10-expire &&
214                 git config core.commitGraph true &&
215                 test_line_count = 2 $graphdir/commit-graph-chain &&
216                 test_commit 15 &&
217                 git commit-graph write --reachable --split --size-multiple=10 --expire-time=1980-01-01 &&
218                 test_line_count = 1 $graphdir/commit-graph-chain &&
219                 ls $graphdir/graph-*.graph >graph-files &&
220                 test_line_count = 3 graph-files
221         ) &&
222         git clone --no-hardlinks . max-commits &&
223         (
224                 cd max-commits &&
225                 git config core.commitGraph true &&
226                 test_line_count = 2 $graphdir/commit-graph-chain &&
227                 test_commit 16 &&
228                 test_commit 17 &&
229                 git commit-graph write --reachable --split --max-commits=1 &&
230                 test_line_count = 1 $graphdir/commit-graph-chain &&
231                 ls $graphdir/graph-*.graph >graph-files &&
232                 test_line_count = 1 graph-files
233         )
234 '
235
236 test_expect_success 'remove commit-graph-chain file after flattening' '
237         git clone . flatten &&
238         (
239                 cd flatten &&
240                 test_line_count = 2 $graphdir/commit-graph-chain &&
241                 git commit-graph write --reachable &&
242                 test_path_is_missing $graphdir/commit-graph-chain &&
243                 ls $graphdir >graph-files &&
244                 test_line_count = 0 graph-files
245         )
246 '
247
248 corrupt_file() {
249         file=$1
250         pos=$2
251         data="${3:-\0}"
252         chmod a+w "$file" &&
253         printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
254 }
255
256 test_expect_success 'verify hashes along chain, even in shallow' '
257         git clone --no-hardlinks . verify &&
258         (
259                 cd verify &&
260                 git commit-graph verify &&
261                 base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
262                 corrupt_file "$base_file" $(test_oid shallow) "\01" &&
263                 test_must_fail git commit-graph verify --shallow 2>test_err &&
264                 grep -v "^+" test_err >err &&
265                 test_i18ngrep "incorrect checksum" err
266         )
267 '
268
269 test_expect_success 'verify --shallow does not check base contents' '
270         git clone --no-hardlinks . verify-shallow &&
271         (
272                 cd verify-shallow &&
273                 git commit-graph verify &&
274                 base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
275                 corrupt_file "$base_file" 1000 "\01" &&
276                 git commit-graph verify --shallow &&
277                 test_must_fail git commit-graph verify 2>test_err &&
278                 grep -v "^+" test_err >err &&
279                 test_i18ngrep "incorrect checksum" err
280         )
281 '
282
283 test_expect_success 'warn on base graph chunk incorrect' '
284         git clone --no-hardlinks . base-chunk &&
285         (
286                 cd base-chunk &&
287                 git commit-graph verify &&
288                 base_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
289                 corrupt_file "$base_file" $(test_oid base) "\01" &&
290                 git commit-graph verify --shallow 2>test_err &&
291                 grep -v "^+" test_err >err &&
292                 test_i18ngrep "commit-graph chain does not match" err
293         )
294 '
295
296 test_expect_success 'verify after commit-graph-chain corruption' '
297         git clone --no-hardlinks . verify-chain &&
298         (
299                 cd verify-chain &&
300                 corrupt_file "$graphdir/commit-graph-chain" 60 "G" &&
301                 git commit-graph verify 2>test_err &&
302                 grep -v "^+" test_err >err &&
303                 test_i18ngrep "invalid commit-graph chain" err &&
304                 corrupt_file "$graphdir/commit-graph-chain" 60 "A" &&
305                 git commit-graph verify 2>test_err &&
306                 grep -v "^+" test_err >err &&
307                 test_i18ngrep "unable to find all commit-graph files" err
308         )
309 '
310
311 test_expect_success 'verify across alternates' '
312         git clone --no-hardlinks . verify-alt &&
313         (
314                 cd verify-alt &&
315                 rm -rf $graphdir &&
316                 altdir="$(pwd)/../.git/objects" &&
317                 echo "$altdir" >.git/objects/info/alternates &&
318                 git commit-graph verify --object-dir="$altdir/" &&
319                 test_commit extra &&
320                 git commit-graph write --reachable --split &&
321                 tip_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
322                 corrupt_file "$tip_file" 100 "\01" &&
323                 test_must_fail git commit-graph verify --shallow 2>test_err &&
324                 grep -v "^+" test_err >err &&
325                 test_i18ngrep "commit-graph has incorrect fanout value" err
326         )
327 '
328
329 test_expect_success 'add octopus merge' '
330         git reset --hard commits/10 &&
331         git merge commits/3 commits/4 &&
332         git branch merge/octopus &&
333         git commit-graph write --reachable --split &&
334         git commit-graph verify --progress 2>err &&
335         test_line_count = 3 err &&
336         test_i18ngrep ! warning err &&
337         test_line_count = 3 $graphdir/commit-graph-chain
338 '
339
340 graph_git_behavior 'graph exists' merge/octopus commits/12
341
342 test_expect_success 'split across alternate where alternate is not split' '
343         git commit-graph write --reachable &&
344         test_path_is_file .git/objects/info/commit-graph &&
345         cp .git/objects/info/commit-graph . &&
346         git clone --no-hardlinks . alt-split &&
347         (
348                 cd alt-split &&
349                 rm -f .git/objects/info/commit-graph &&
350                 echo "$(pwd)"/../.git/objects >.git/objects/info/alternates &&
351                 test_commit 18 &&
352                 git commit-graph write --reachable --split &&
353                 test_line_count = 1 $graphdir/commit-graph-chain
354         ) &&
355         test_cmp commit-graph .git/objects/info/commit-graph
356 '
357
358 while read mode modebits
359 do
360         test_expect_success POSIXPERM "split commit-graph respects core.sharedrepository $mode" '
361                 rm -rf $graphdir $infodir/commit-graph &&
362                 git reset --hard commits/1 &&
363                 test_config core.sharedrepository "$mode" &&
364                 git commit-graph write --split --reachable &&
365                 ls $graphdir/graph-*.graph >graph-files &&
366                 test_line_count = 1 graph-files &&
367                 echo "$modebits" >expect &&
368                 test_modebits $graphdir/graph-*.graph >actual &&
369                 test_cmp expect actual
370         '
371 done <<\EOF
372 0666 -r--r--r--
373 0600 -r--------
374 EOF
375
376 test_done