1#!/bin/sh
2
3test_description='Test ls-files recurse-submodules feature
4
5This test verifies the recurse-submodules feature correctly lists files from
6submodules.
7'
8
9. ./test-lib.sh
10
11test_expect_success 'setup directory structure and submodules' '
12	echo a >a &&
13	mkdir b &&
14	echo b >b/b &&
15	git add a b &&
16	git commit -m "add a and b" &&
17	git init submodule &&
18	echo c >submodule/c &&
19	git -C submodule add c &&
20	git -C submodule commit -m "add c" &&
21	git submodule add ./submodule &&
22	git commit -m "added submodule"
23'
24
25test_expect_success 'ls-files correctly outputs files in submodule' '
26	cat >expect <<-\EOF &&
27	.gitmodules
28	a
29	b/b
30	submodule/c
31	EOF
32
33	git ls-files --recurse-submodules >actual &&
34	test_cmp expect actual
35'
36
37test_expect_success 'ls-files correctly outputs files in submodule with -z' '
38	lf_to_nul >expect <<-\EOF &&
39	.gitmodules
40	a
41	b/b
42	submodule/c
43	EOF
44
45	git ls-files --recurse-submodules -z >actual &&
46	test_cmp expect actual
47'
48
49test_expect_success 'ls-files does not output files not added to a repo' '
50	cat >expect <<-\EOF &&
51	.gitmodules
52	a
53	b/b
54	submodule/c
55	EOF
56
57	echo a >not_added &&
58	echo b >b/not_added &&
59	echo c >submodule/not_added &&
60	git ls-files --recurse-submodules >actual &&
61	test_cmp expect actual
62'
63
64test_expect_success 'ls-files recurses more than 1 level' '
65	cat >expect <<-\EOF &&
66	.gitmodules
67	a
68	b/b
69	submodule/.gitmodules
70	submodule/c
71	submodule/subsub/d
72	EOF
73
74	git init submodule/subsub &&
75	echo d >submodule/subsub/d &&
76	git -C submodule/subsub add d &&
77	git -C submodule/subsub commit -m "add d" &&
78	git -C submodule submodule add ./subsub &&
79	git -C submodule commit -m "added subsub" &&
80	git submodule absorbgitdirs &&
81	git ls-files --recurse-submodules >actual &&
82	test_cmp expect actual
83'
84
85test_expect_success 'ls-files works with GIT_DIR' '
86	cat >expect <<-\EOF &&
87	.gitmodules
88	c
89	subsub/d
90	EOF
91
92	git --git-dir=submodule/.git ls-files --recurse-submodules >actual &&
93	test_cmp expect actual
94'
95
96test_expect_success '--recurse-submodules and pathspecs setup' '
97	echo e >submodule/subsub/e.txt &&
98	git -C submodule/subsub add e.txt &&
99	git -C submodule/subsub commit -m "adding e.txt" &&
100	echo f >submodule/f.TXT &&
101	echo g >submodule/g.txt &&
102	git -C submodule add f.TXT g.txt &&
103	git -C submodule commit -m "add f and g" &&
104	echo h >h.txt &&
105	mkdir sib &&
106	echo sib >sib/file &&
107	git add h.txt sib/file &&
108	git commit -m "add h and sib/file" &&
109	git init sub &&
110	echo sub >sub/file &&
111	git -C sub add file &&
112	git -C sub commit -m "add file" &&
113	git submodule add ./sub &&
114	git commit -m "added sub" &&
115
116	cat >expect <<-\EOF &&
117	.gitmodules
118	a
119	b/b
120	h.txt
121	sib/file
122	sub/file
123	submodule/.gitmodules
124	submodule/c
125	submodule/f.TXT
126	submodule/g.txt
127	submodule/subsub/d
128	submodule/subsub/e.txt
129	EOF
130
131	git ls-files --recurse-submodules >actual &&
132	test_cmp expect actual &&
133	git ls-files --recurse-submodules "*" >actual &&
134	test_cmp expect actual
135'
136
137test_expect_success 'inactive submodule' '
138	test_when_finished "git config --bool submodule.submodule.active true" &&
139	test_when_finished "git -C submodule config --bool submodule.subsub.active true" &&
140	git config --bool submodule.submodule.active "false" &&
141
142	cat >expect <<-\EOF &&
143	.gitmodules
144	a
145	b/b
146	h.txt
147	sib/file
148	sub/file
149	submodule
150	EOF
151
152	git ls-files --recurse-submodules >actual &&
153	test_cmp expect actual &&
154
155	git config --bool submodule.submodule.active "true" &&
156	git -C submodule config --bool submodule.subsub.active "false" &&
157
158	cat >expect <<-\EOF &&
159	.gitmodules
160	a
161	b/b
162	h.txt
163	sib/file
164	sub/file
165	submodule/.gitmodules
166	submodule/c
167	submodule/f.TXT
168	submodule/g.txt
169	submodule/subsub
170	EOF
171
172	git ls-files --recurse-submodules >actual &&
173	test_cmp expect actual
174'
175
176test_expect_success '--recurse-submodules and pathspecs' '
177	cat >expect <<-\EOF &&
178	h.txt
179	submodule/g.txt
180	submodule/subsub/e.txt
181	EOF
182
183	git ls-files --recurse-submodules "*.txt" >actual &&
184	test_cmp expect actual
185'
186
187test_expect_success '--recurse-submodules and pathspecs' '
188	cat >expect <<-\EOF &&
189	h.txt
190	submodule/f.TXT
191	submodule/g.txt
192	submodule/subsub/e.txt
193	EOF
194
195	git ls-files --recurse-submodules ":(icase)*.txt" >actual &&
196	test_cmp expect actual
197'
198
199test_expect_success '--recurse-submodules and pathspecs' '
200	cat >expect <<-\EOF &&
201	h.txt
202	submodule/f.TXT
203	submodule/g.txt
204	EOF
205
206	git ls-files --recurse-submodules ":(icase)*.txt" ":(exclude)submodule/subsub/*" >actual &&
207	test_cmp expect actual
208'
209
210test_expect_success '--recurse-submodules and pathspecs' '
211	cat >expect <<-\EOF &&
212	sub/file
213	EOF
214
215	git ls-files --recurse-submodules "sub" >actual &&
216	test_cmp expect actual &&
217	git ls-files --recurse-submodules "sub/" >actual &&
218	test_cmp expect actual &&
219	git ls-files --recurse-submodules "sub/file" >actual &&
220	test_cmp expect actual &&
221	git ls-files --recurse-submodules "su*/file" >actual &&
222	test_cmp expect actual &&
223	git ls-files --recurse-submodules "su?/file" >actual &&
224	test_cmp expect actual
225'
226
227test_expect_success '--recurse-submodules and pathspecs' '
228	cat >expect <<-\EOF &&
229	sib/file
230	sub/file
231	EOF
232
233	git ls-files --recurse-submodules "s??/file" >actual &&
234	test_cmp expect actual &&
235	git ls-files --recurse-submodules "s???file" >actual &&
236	test_cmp expect actual &&
237	git ls-files --recurse-submodules "s*file" >actual &&
238	test_cmp expect actual
239'
240
241test_expect_success '--recurse-submodules and relative paths' '
242	# From subdir
243	cat >expect <<-\EOF &&
244	b
245	EOF
246	git -C b ls-files --recurse-submodules >actual &&
247	test_cmp expect actual &&
248
249	# Relative path to top
250	cat >expect <<-\EOF &&
251	../.gitmodules
252	../a
253	b
254	../h.txt
255	../sib/file
256	../sub/file
257	../submodule/.gitmodules
258	../submodule/c
259	../submodule/f.TXT
260	../submodule/g.txt
261	../submodule/subsub/d
262	../submodule/subsub/e.txt
263	EOF
264	git -C b ls-files --recurse-submodules -- .. >actual &&
265	test_cmp expect actual &&
266
267	# Relative path to submodule
268	cat >expect <<-\EOF &&
269	../submodule/.gitmodules
270	../submodule/c
271	../submodule/f.TXT
272	../submodule/g.txt
273	../submodule/subsub/d
274	../submodule/subsub/e.txt
275	EOF
276	git -C b ls-files --recurse-submodules -- ../submodule >actual &&
277	test_cmp expect actual
278'
279
280test_expect_success '--recurse-submodules does not support --error-unmatch' '
281	test_must_fail git ls-files --recurse-submodules --error-unmatch 2>actual &&
282	test_i18ngrep "does not support --error-unmatch" actual
283'
284
285test_incompatible_with_recurse_submodules () {
286	test_expect_success "--recurse-submodules and $1 are incompatible" "
287		test_must_fail git ls-files --recurse-submodules $1 2>actual &&
288		test_i18ngrep 'unsupported mode' actual
289	"
290}
291
292test_incompatible_with_recurse_submodules --deleted
293test_incompatible_with_recurse_submodules --modified
294test_incompatible_with_recurse_submodules --others
295test_incompatible_with_recurse_submodules --stage
296test_incompatible_with_recurse_submodules --killed
297test_incompatible_with_recurse_submodules --unmerged
298
299test_done
300