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