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