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