Merge branch 'sg/subtree-signed-commits' into pu
[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         rm -rf .git &&
8         mkdir full &&
9         cd full &&
10         git init &&
11         git config core.commitGraph true &&
12         objdir=".git/objects"
13 '
14
15 test_expect_success 'write graph with no packs' '
16         git commit-graph write --object-dir . &&
17         test_path_is_missing info/graph-latest
18 '
19
20 test_expect_success 'create commits and repack' '
21         for i in $(test_seq 3)
22         do
23                 test_commit $i &&
24                 git branch commits/$i
25         done &&
26         git repack
27 '
28
29 graph_git_two_modes() {
30         git -c core.graph=true $1 >output
31         git -c core.graph=false $1 >expect
32         test_cmp output expect
33 }
34
35 graph_git_behavior() {
36         MSG=$1
37         BRANCH=$2
38         COMPARE=$3
39         test_expect_success "check normal git operations: $MSG" '
40                 graph_git_two_modes "log --oneline $BRANCH" &&
41                 graph_git_two_modes "log --topo-order $BRANCH" &&
42                 graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
43                 graph_git_two_modes "branch -vv" &&
44                 graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
45         '
46 }
47
48 graph_git_behavior 'no graph' commits/3 commits/1
49
50 graph_read_expect() {
51         OPTIONAL=""
52         NUM_CHUNKS=3
53         if [ ! -z $2 ]
54         then
55                 OPTIONAL=" $2"
56                 NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
57         fi
58         cat >expect <<- EOF
59         header: 43475048 1 1 $NUM_CHUNKS 0
60         num_commits: $1
61         chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
62         EOF
63 }
64
65 test_expect_success 'write graph' '
66         graph1=$(git commit-graph write) &&
67         test_path_is_file $objdir/info/$graph1 &&
68         test_path_is_missing $objdir/info/graph-latest &&
69         git commit-graph read --file=$graph1 >output &&
70         graph_read_expect "3" &&
71         test_cmp expect output
72 '
73
74 graph_git_behavior 'graph exists, no head' commits/3 commits/1
75
76 test_expect_success 'Add more commits' '
77         git reset --hard commits/1 &&
78         for i in $(test_seq 4 5)
79         do
80                 test_commit $i &&
81                 git branch commits/$i
82         done &&
83         git reset --hard commits/2 &&
84         for i in $(test_seq 6 7)
85         do
86                 test_commit $i &&
87                 git branch commits/$i
88         done &&
89         git reset --hard commits/2 &&
90         git merge commits/4 &&
91         git branch merge/1 &&
92         git reset --hard commits/4 &&
93         git merge commits/6 &&
94         git branch merge/2 &&
95         git reset --hard commits/3 &&
96         git merge commits/5 commits/7 &&
97         git branch merge/3 &&
98         git repack
99 '
100
101 # Current graph structure:
102 #
103 #   __M3___
104 #  /   |   \
105 # 3 M1 5 M2 7
106 # |/  \|/  \|
107 # 2    4    6
108 # |___/____/
109 # 1
110
111 test_expect_success 'write graph with merges' '
112         graph2=$(git commit-graph write --set-latest)&&
113         test_path_is_file $objdir/info/$graph2 &&
114         test_path_is_file $objdir/info/graph-latest &&
115         printf $graph2 >expect &&
116         test_cmp expect $objdir/info/graph-latest &&
117         git commit-graph read --file=$graph2 >output &&
118         graph_read_expect "10" "large_edges" &&
119         test_cmp expect output
120 '
121
122 graph_git_behavior 'merge 1 vs 2' merge/1 merge/2
123 graph_git_behavior 'merge 1 vs 3' merge/1 merge/3
124 graph_git_behavior 'merge 2 vs 3' merge/2 merge/3
125
126 test_expect_success 'Add one more commit' '
127         test_commit 8 &&
128         git branch commits/8 &&
129         ls $objdir/pack | grep idx >existing-idx &&
130         git repack &&
131         ls $objdir/pack| grep idx | grep -v --file=existing-idx >new-idx
132 '
133
134 # Current graph structure:
135 #
136 #      8
137 #      |
138 #   __M3___
139 #  /   |   \
140 # 3 M1 5 M2 7
141 # |/  \|/  \|
142 # 2    4    6
143 # |___/____/
144 # 1
145
146 graph_git_behavior 'mixed mode, commit 8 vs merge 1' commits/8 merge/1
147 graph_git_behavior 'mixed mode, commit 8 vs merge 2' commits/8 merge/2
148
149 test_expect_success 'write graph with new commit' '
150         graph3=$(git commit-graph write --set-latest --delete-expired) &&
151         test_path_is_file $objdir/info/$graph3 &&
152         test_path_is_file $objdir/info/$graph2 &&
153         test_path_is_missing $objdir/info/$graph1 &&
154         test_path_is_file $objdir/info/graph-latest &&
155         printf $graph3 >expect &&
156         test_cmp expect $objdir/info/graph-latest &&
157         git commit-graph read --file=$graph3 >output &&
158         graph_read_expect "11" "large_edges" &&
159         test_cmp expect output
160 '
161
162 graph_git_behavior 'full graph, commit 8 vs merge 1' commits/8 merge/1
163 graph_git_behavior 'full graph, commit 8 vs merge 2' commits/8 merge/2
164
165 test_expect_success 'write graph with nothing new' '
166         graph4=$(git commit-graph write --set-latest --delete-expired) &&
167         test_path_is_file $objdir/info/$graph4 &&
168         test_path_is_missing $objdir/info/$graph2 &&
169         printf $graph3 >expect &&
170         printf $graph4 >output &&
171         test_cmp expect output &&
172         test_path_is_file $objdir/info/graph-latest &&
173         printf $graph4 >expect &&
174         test_cmp expect $objdir/info/graph-latest &&
175         git commit-graph read --file=$graph4 >output &&
176         graph_read_expect "11" "large_edges" &&
177         test_cmp expect output
178 '
179
180 graph_git_behavior 'cleared graph, commit 8 vs merge 1' commits/8 merge/1
181 graph_git_behavior 'cleared graph, commit 8 vs merge 2' commits/8 merge/2
182
183 test_expect_success 'build graph from latest pack with closure' '
184         rm $objdir/info/graph-latest &&
185         graph5=$(cat new-idx | git commit-graph write --set-latest --delete-expired --stdin-packs) &&
186         test_path_is_file $objdir/info/$graph5 &&
187         test_path_is_missing $objdir/info/$graph4 &&
188         test_path_is_file $objdir/info/graph-latest &&
189         printf $graph5 >expect &&
190         test_cmp expect $objdir/info/graph-latest &&
191         git commit-graph read --file=$graph5 >output &&
192         graph_read_expect "9" "large_edges" &&
193         test_cmp expect output
194 '
195
196 graph_git_behavior 'graph from pack, commit 8 vs merge 1' commits/8 merge/1
197 graph_git_behavior 'graph from pack, commit 8 vs merge 2' commits/8 merge/2
198
199 test_expect_success 'build graph from commits with closure' '
200         git tag -a -m "merge" tag/merge merge/2 &&
201         git rev-parse tag/merge >commits-in &&
202         git rev-parse merge/1 >>commits-in &&
203         rm $objdir/info/graph-latest &&
204         graph6=$(cat commits-in | git commit-graph write --set-latest --delete-expired --stdin-commits) &&
205         test_path_is_file $objdir/info/$graph6 &&
206         test_path_is_missing $objdir/info/$graph5 &&
207         test_path_is_file $objdir/info/graph-latest &&
208         printf $graph6 >expect &&
209         test_cmp expect $objdir/info/graph-latest &&
210         git commit-graph read --file=$graph6 >output &&
211         graph_read_expect "6" &&
212         test_cmp expect output
213 '
214
215 graph_git_behavior 'graph from commits, commit 8 vs merge 1' commits/8 merge/1
216 graph_git_behavior 'graph from commits, commit 8 vs merge 2' commits/8 merge/2
217
218 test_expect_success 'setup bare repo' '
219         cd .. &&
220         git clone --bare --no-local full bare &&
221         cd bare &&
222         git config core.commitGraph true &&
223         baredir="./objects"
224 '
225
226 graph_git_behavior 'bare repo, commit 8 vs merge 1' commits/8 merge/1
227 graph_git_behavior 'bare repo, commit 8 vs merge 2' commits/8 merge/2
228
229 test_expect_success 'write graph in bare repo' '
230         graphbare=$(git commit-graph write --set-latest) &&
231         test_path_is_file $baredir/info/$graphbare &&
232         test_path_is_file $baredir/info/graph-latest &&
233         printf $graphbare >expect &&
234         test_cmp expect $baredir/info/graph-latest &&
235         git commit-graph read --file=$graphbare >output &&
236         graph_read_expect "11" "large_edges" &&
237         test_cmp expect output
238 '
239
240 graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' commits/8 merge/1
241 graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' commits/8 merge/2
242
243 test_done
244