submodule: port submodule subcommand 'summary' from shell to C
[git] / t / t7401-submodule-summary.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Ping Yin
4 #
5
6 test_description='Summary support for submodules
7
8 This test tries to verify the sanity of summary subcommand of git submodule.
9 '
10 # NOTE: This test script uses 'git add' instead of 'git submodule add' to add
11 # submodules to the superproject. Some submodule subcommands such as init and
12 # deinit might not work as expected in this script. t7421 does not have this
13 # caveat.
14
15 . ./test-lib.sh
16
17 add_file () {
18         sm=$1
19         shift
20         owd=$(pwd)
21         cd "$sm"
22         for name; do
23                 echo "$name" > "$name" &&
24                 git add "$name" &&
25                 test_tick &&
26                 git commit -m "Add $name"
27         done >/dev/null
28         git rev-parse --verify HEAD | cut -c1-7
29         cd "$owd"
30 }
31 commit_file () {
32         test_tick &&
33         git commit "$@" -m "Commit $*" >/dev/null
34 }
35
36 test_create_repo sm1 &&
37 add_file . foo >/dev/null
38
39 head1=$(add_file sm1 foo1 foo2)
40
41 test_expect_success 'added submodule' "
42         git add sm1 &&
43         git submodule summary >actual &&
44         cat >expected <<-EOF &&
45 * sm1 0000000...$head1 (2):
46   > Add foo2
47
48 EOF
49         test_cmp expected actual
50 "
51
52 test_expect_success 'added submodule (subdirectory)' "
53         mkdir sub &&
54         (
55                 cd sub &&
56                 git submodule summary >../actual
57         ) &&
58         cat >expected <<-EOF &&
59 * ../sm1 0000000...$head1 (2):
60   > Add foo2
61
62 EOF
63         test_cmp expected actual
64 "
65
66 test_expect_success 'added submodule (subdirectory only)' "
67         (
68                 cd sub &&
69                 git submodule summary . >../actual
70         ) &&
71         test_must_be_empty actual
72 "
73
74 test_expect_success 'added submodule (subdirectory with explicit path)' "
75         (
76                 cd sub &&
77                 git submodule summary ../sm1 >../actual
78         ) &&
79         cat >expected <<-EOF &&
80 * ../sm1 0000000...$head1 (2):
81   > Add foo2
82
83 EOF
84         test_cmp expected actual
85 "
86
87 commit_file sm1 &&
88 head2=$(add_file sm1 foo3)
89
90 test_expect_success 'modified submodule(forward)' "
91         git submodule summary >actual &&
92         cat >expected <<-EOF &&
93 * sm1 $head1...$head2 (1):
94   > Add foo3
95
96 EOF
97         test_cmp expected actual
98 "
99
100 test_expect_success 'modified submodule(forward), --files' "
101         git submodule summary --files >actual &&
102         cat >expected <<-EOF &&
103 * sm1 $head1...$head2 (1):
104   > Add foo3
105
106 EOF
107         test_cmp expected actual
108 "
109
110 test_expect_success 'no ignore=all setting has any effect' "
111         git config -f .gitmodules submodule.sm1.path sm1 &&
112         git config -f .gitmodules submodule.sm1.ignore all &&
113         git config submodule.sm1.ignore all &&
114         git config diff.ignoreSubmodules all &&
115         git submodule summary >actual &&
116         cat >expected <<-EOF &&
117 * sm1 $head1...$head2 (1):
118   > Add foo3
119
120 EOF
121         test_cmp expected actual &&
122         git config --unset diff.ignoreSubmodules &&
123         git config --remove-section submodule.sm1 &&
124         git config -f .gitmodules --remove-section submodule.sm1
125 "
126
127
128 commit_file sm1 &&
129 head3=$(
130         cd sm1 &&
131         git reset --hard HEAD~2 >/dev/null &&
132         git rev-parse --verify HEAD | cut -c1-7
133 )
134
135 test_expect_success 'modified submodule(backward)' "
136         git submodule summary >actual &&
137         cat >expected <<-EOF &&
138 * sm1 $head2...$head3 (2):
139   < Add foo3
140   < Add foo2
141
142 EOF
143         test_cmp expected actual
144 "
145
146 head4=$(add_file sm1 foo4 foo5) &&
147 head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD)
148 test_expect_success 'modified submodule(backward and forward)' "
149         git submodule summary >actual &&
150         cat >expected <<-EOF &&
151 * sm1 $head2...$head4 (4):
152   > Add foo5
153   > Add foo4
154   < Add foo3
155   < Add foo2
156
157 EOF
158         test_cmp expected actual
159 "
160
161 test_expect_success '--summary-limit' "
162         git submodule summary -n 3 >actual &&
163         cat >expected <<-EOF &&
164 * sm1 $head2...$head4 (4):
165   > Add foo5
166   > Add foo4
167   < Add foo3
168
169 EOF
170         test_cmp expected actual
171 "
172
173 commit_file sm1 &&
174 mv sm1 sm1-bak &&
175 echo sm1 >sm1 &&
176 head5=$(git hash-object sm1 | cut -c1-7) &&
177 git add sm1 &&
178 rm -f sm1 &&
179 mv sm1-bak sm1
180
181 test_expect_success 'typechanged submodule(submodule->blob), --cached' "
182         git submodule summary --cached >actual &&
183         cat >expected <<-EOF &&
184 * sm1 $head4(submodule)->$head5(blob) (3):
185   < Add foo5
186
187 EOF
188         test_i18ncmp actual expected
189 "
190
191 test_expect_success 'typechanged submodule(submodule->blob), --files' "
192         git submodule summary --files >actual &&
193         cat >expected <<-EOF &&
194 * sm1 $head5(blob)->$head4(submodule) (3):
195   > Add foo5
196
197 EOF
198         test_i18ncmp actual expected
199 "
200
201 rm -rf sm1 &&
202 git checkout-index sm1
203 test_expect_success 'typechanged submodule(submodule->blob)' "
204         git submodule summary >actual &&
205         cat >expected <<-EOF &&
206 * sm1 $head4(submodule)->$head5(blob):
207
208 EOF
209         test_i18ncmp actual expected
210 "
211
212 rm -f sm1 &&
213 test_create_repo sm1 &&
214 head6=$(add_file sm1 foo6 foo7)
215 test_expect_success 'nonexistent commit' "
216         git submodule summary >actual &&
217         cat >expected <<-EOF &&
218 * sm1 $head4...$head6:
219   Warn: sm1 doesn't contain commit $head4_full
220
221 EOF
222         test_i18ncmp actual expected
223 "
224
225 commit_file
226 test_expect_success 'typechanged submodule(blob->submodule)' "
227         git submodule summary >actual &&
228         cat >expected <<-EOF &&
229 * sm1 $head5(blob)->$head6(submodule) (2):
230   > Add foo7
231
232 EOF
233         test_i18ncmp expected actual
234 "
235
236 commit_file sm1 &&
237 rm -rf sm1
238 test_expect_success 'deleted submodule' "
239         git submodule summary >actual &&
240         cat >expected <<-EOF &&
241 * sm1 $head6...0000000:
242
243 EOF
244         test_cmp expected actual
245 "
246
247 test_expect_success 'create second submodule' '
248         test_create_repo sm2 &&
249         head7=$(add_file sm2 foo8 foo9) &&
250         git add sm2
251 '
252
253 test_expect_success 'multiple submodules' "
254         git submodule summary >actual &&
255         cat >expected <<-EOF &&
256 * sm1 $head6...0000000:
257
258 * sm2 0000000...$head7 (2):
259   > Add foo9
260
261 EOF
262         test_cmp expected actual
263 "
264
265 test_expect_success 'path filter' "
266         git submodule summary sm2 >actual &&
267         cat >expected <<-EOF &&
268 * sm2 0000000...$head7 (2):
269   > Add foo9
270
271 EOF
272         test_cmp expected actual
273 "
274
275 commit_file sm2
276 test_expect_success 'given commit' "
277         git submodule summary HEAD^ >actual &&
278         cat >expected <<-EOF &&
279 * sm1 $head6...0000000:
280
281 * sm2 0000000...$head7 (2):
282   > Add foo9
283
284 EOF
285         test_cmp expected actual
286 "
287
288 test_expect_success '--for-status' "
289         git submodule summary --for-status HEAD^ >actual &&
290         test_i18ncmp actual - <<EOF
291 * sm1 $head6...0000000:
292
293 * sm2 0000000...$head7 (2):
294   > Add foo9
295
296 EOF
297 "
298
299 test_expect_success 'fail when using --files together with --cached' "
300         test_must_fail git submodule summary --files --cached
301 "
302
303 test_expect_success 'should not fail in an empty repo' "
304         git init xyzzy &&
305         cd xyzzy &&
306         git submodule summary >output 2>&1 &&
307         test_must_be_empty output
308 "
309
310 test_done