1#!/bin/sh
2
3test_description='git status for submodule'
4
5. ./test-lib.sh
6
7test_create_repo_with_commit () {
8 test_create_repo "$1" &&
9 (
10 cd "$1" &&
11 : >bar &&
12 git add bar &&
13 git commit -m " Add bar" &&
14 : >foo &&
15 git add foo &&
16 git commit -m " Add foo"
17 )
18}
19
20sanitize_output () {
21 sed -e "s/$OID_REGEX/HASH/" -e "s/$OID_REGEX/HASH/" output >output2 &&
22 mv output2 output
23}
24
25
26test_expect_success 'setup' '
27 test_create_repo_with_commit sub &&
28 echo output > .gitignore &&
29 git add sub .gitignore &&
30 git commit -m "Add submodule sub"
31'
32
33test_expect_success 'status clean' '
34 git status >output &&
35 test_i18ngrep "nothing to commit" output
36'
37
38test_expect_success 'commit --dry-run -a clean' '
39 test_must_fail git commit --dry-run -a >output &&
40 test_i18ngrep "nothing to commit" output
41'
42
43test_expect_success 'status with modified file in submodule' '
44 (cd sub && git reset --hard) &&
45 echo "changed" >sub/foo &&
46 git status >output &&
47 test_i18ngrep "modified: sub (modified content)" output
48'
49
50test_expect_success 'status with modified file in submodule (porcelain)' '
51 (cd sub && git reset --hard) &&
52 echo "changed" >sub/foo &&
53 git status --porcelain >output &&
54 diff output - <<-\EOF
55 M sub
56 EOF
57'
58
59test_expect_success 'status with modified file in submodule (short)' '
60 (cd sub && git reset --hard) &&
61 echo "changed" >sub/foo &&
62 git status --short >output &&
63 diff output - <<-\EOF
64 m sub
65 EOF
66'
67
68test_expect_success 'status with added file in submodule' '
69 (cd sub && git reset --hard && echo >foo && git add foo) &&
70 git status >output &&
71 test_i18ngrep "modified: sub (modified content)" output
72'
73
74test_expect_success 'status with added file in submodule (porcelain)' '
75 (cd sub && git reset --hard && echo >foo && git add foo) &&
76 git status --porcelain >output &&
77 diff output - <<-\EOF
78 M sub
79 EOF
80'
81
82test_expect_success 'status with added file in submodule (short)' '
83 (cd sub && git reset --hard && echo >foo && git add foo) &&
84 git status --short >output &&
85 diff output - <<-\EOF
86 m sub
87 EOF
88'
89
90test_expect_success 'status with untracked file in submodule' '
91 (cd sub && git reset --hard) &&
92 echo "content" >sub/new-file &&
93 git status >output &&
94 test_i18ngrep "modified: sub (untracked content)" output
95'
96
97test_expect_success 'status -uno with untracked file in submodule' '
98 git status -uno >output &&
99 test_i18ngrep "^nothing to commit" output
100'
101
102test_expect_success 'status with untracked file in submodule (porcelain)' '
103 git status --porcelain >output &&
104 diff output - <<-\EOF
105 M sub
106 EOF
107'
108
109test_expect_success 'status with untracked file in submodule (short)' '
110 git status --short >output &&
111 diff output - <<-\EOF
112 ? sub
113 EOF
114'
115
116test_expect_success 'status with added and untracked file in submodule' '
117 (cd sub && git reset --hard && echo >foo && git add foo) &&
118 echo "content" >sub/new-file &&
119 git status >output &&
120 test_i18ngrep "modified: sub (modified content, untracked content)" output
121'
122
123test_expect_success 'status with added and untracked file in submodule (porcelain)' '
124 (cd sub && git reset --hard && echo >foo && git add foo) &&
125 echo "content" >sub/new-file &&
126 git status --porcelain >output &&
127 diff output - <<-\EOF
128 M sub
129 EOF
130'
131
132test_expect_success 'status with modified file in modified submodule' '
133 (cd sub && git reset --hard) &&
134 rm sub/new-file &&
135 (cd sub && echo "next change" >foo && git commit -m "next change" foo) &&
136 echo "changed" >sub/foo &&
137 git status >output &&
138 test_i18ngrep "modified: sub (new commits, modified content)" output
139'
140
141test_expect_success 'status with modified file in modified submodule (porcelain)' '
142 (cd sub && git reset --hard) &&
143 echo "changed" >sub/foo &&
144 git status --porcelain >output &&
145 diff output - <<-\EOF
146 M sub
147 EOF
148'
149
150test_expect_success 'status with added file in modified submodule' '
151 (cd sub && git reset --hard && echo >foo && git add foo) &&
152 git status >output &&
153 test_i18ngrep "modified: sub (new commits, modified content)" output
154'
155
156test_expect_success 'status with added file in modified submodule (porcelain)' '
157 (cd sub && git reset --hard && echo >foo && git add foo) &&
158 git status --porcelain >output &&
159 diff output - <<-\EOF
160 M sub
161 EOF
162'
163
164test_expect_success 'status with untracked file in modified submodule' '
165 (cd sub && git reset --hard) &&
166 echo "content" >sub/new-file &&
167 git status >output &&
168 test_i18ngrep "modified: sub (new commits, untracked content)" output
169'
170
171test_expect_success 'status with untracked file in modified submodule (porcelain)' '
172 git status --porcelain >output &&
173 diff output - <<-\EOF
174 M sub
175 EOF
176'
177
178test_expect_success 'status with added and untracked file in modified submodule' '
179 (cd sub && git reset --hard && echo >foo && git add foo) &&
180 echo "content" >sub/new-file &&
181 git status >output &&
182 test_i18ngrep "modified: sub (new commits, modified content, untracked content)" output
183'
184
185test_expect_success 'status with added and untracked file in modified submodule (porcelain)' '
186 (cd sub && git reset --hard && echo >foo && git add foo) &&
187 echo "content" >sub/new-file &&
188 git status --porcelain >output &&
189 diff output - <<-\EOF
190 M sub
191 EOF
192'
193
194test_expect_success 'setup .git file for sub' '
195 (cd sub &&
196 rm -f new-file
197 REAL="$(pwd)/../.real" &&
198 mv .git "$REAL"
199 echo "gitdir: $REAL" >.git) &&
200 echo .real >>.gitignore &&
201 git commit -m "added .real to .gitignore" .gitignore
202'
203
204test_expect_success 'status with added file in modified submodule with .git file' '
205 (cd sub && git reset --hard && echo >foo && git add foo) &&
206 git status >output &&
207 test_i18ngrep "modified: sub (new commits, modified content)" output
208'
209
210test_expect_success 'status with a lot of untracked files in the submodule' '
211 (
212 cd sub
213 i=0 &&
214 while test $i -lt 1024
215 do
216 >some-file-$i
217 i=$(( $i + 1 ))
218 done
219 ) &&
220 git status --porcelain sub 2>err.actual &&
221 test_must_be_empty err.actual &&
222 rm err.actual
223'
224
225test_expect_success 'rm submodule contents' '
226 rm -rf sub &&
227 mkdir sub
228'
229
230test_expect_success 'status clean (empty submodule dir)' '
231 git status >output &&
232 test_i18ngrep "nothing to commit" output
233'
234
235test_expect_success 'status -a clean (empty submodule dir)' '
236 test_must_fail git commit --dry-run -a >output &&
237 test_i18ngrep "nothing to commit" output
238'
239
240cat >status_expect <<\EOF
241AA .gitmodules
242A sub1
243EOF
244
245test_expect_success 'status with merge conflict in .gitmodules' '
246 git clone . super &&
247 test_create_repo_with_commit sub1 &&
248 test_tick &&
249 test_create_repo_with_commit sub2 &&
250 (
251 cd super &&
252 prev=$(git rev-parse HEAD) &&
253 git checkout -b add_sub1 &&
254 git submodule add ../sub1 &&
255 git commit -m "add sub1" &&
256 git checkout -b add_sub2 $prev &&
257 git submodule add ../sub2 &&
258 git commit -m "add sub2" &&
259 git checkout -b merge_conflict_gitmodules &&
260 test_must_fail git merge add_sub1 &&
261 git status -s >../status_actual 2>&1
262 ) &&
263 test_cmp status_actual status_expect
264'
265
266sha1_merge_sub1=$(cd sub1 && git rev-parse HEAD)
267sha1_merge_sub2=$(cd sub2 && git rev-parse HEAD)
268short_sha1_merge_sub1=$(cd sub1 && git rev-parse --short HEAD)
269short_sha1_merge_sub2=$(cd sub2 && git rev-parse --short HEAD)
270cat >diff_expect <<\EOF
271diff --cc .gitmodules
272index badaa4c,44f999a..0000000
273--- a/.gitmodules
274+++ b/.gitmodules
275@@@ -1,3 -1,3 +1,9 @@@
276++<<<<<<< HEAD
277 +[submodule "sub2"]
278 + path = sub2
279 + url = ../sub2
280++=======
281+ [submodule "sub1"]
282+ path = sub1
283+ url = ../sub1
284++>>>>>>> add_sub1
285EOF
286
287cat >diff_submodule_expect <<\EOF
288diff --cc .gitmodules
289index badaa4c,44f999a..0000000
290--- a/.gitmodules
291+++ b/.gitmodules
292@@@ -1,3 -1,3 +1,9 @@@
293++<<<<<<< HEAD
294 +[submodule "sub2"]
295 + path = sub2
296 + url = ../sub2
297++=======
298+ [submodule "sub1"]
299+ path = sub1
300+ url = ../sub1
301++>>>>>>> add_sub1
302EOF
303
304test_expect_success 'diff with merge conflict in .gitmodules' '
305 (
306 cd super &&
307 git diff >../diff_actual 2>&1
308 ) &&
309 test_cmp diff_expect diff_actual
310'
311
312test_expect_success 'diff --submodule with merge conflict in .gitmodules' '
313 (
314 cd super &&
315 git diff --submodule >../diff_submodule_actual 2>&1
316 ) &&
317 test_cmp diff_submodule_expect diff_submodule_actual
318'
319
320# We'll setup different cases for further testing:
321# sub1 will contain a nested submodule,
322# sub2 will have an untracked file
323# sub3 will have an untracked repository
324test_expect_success 'setup superproject with untracked file in nested submodule' '
325 (
326 cd super &&
327 git clean -dfx &&
328 rm .gitmodules &&
329 git submodule add -f ./sub1 &&
330 git submodule add -f ./sub2 &&
331 git submodule add -f ./sub1 sub3 &&
332 git commit -a -m "messy merge in superproject" &&
333 (
334 cd sub1 &&
335 git submodule add ../sub2 &&
336 git commit -a -m "add sub2 to sub1"
337 ) &&
338 git add sub1 &&
339 git commit -a -m "update sub1 to contain nested sub"
340 ) &&
341 echo content >super/sub1/sub2/file &&
342 echo content >super/sub2/file &&
343 git -C super/sub3 clone ../../sub2 untracked_repository
344'
345
346test_expect_success 'status with untracked file in nested submodule (porcelain)' '
347 git -C super status --porcelain >output &&
348 diff output - <<-\EOF
349 M sub1
350 M sub2
351 M sub3
352 EOF
353'
354
355test_expect_success 'status with untracked file in nested submodule (porcelain=2)' '
356 git -C super status --porcelain=2 >output &&
357 sanitize_output output &&
358 diff output - <<-\EOF
359 1 .M S..U 160000 160000 160000 HASH HASH sub1
360 1 .M S..U 160000 160000 160000 HASH HASH sub2
361 1 .M S..U 160000 160000 160000 HASH HASH sub3
362 EOF
363'
364
365test_expect_success 'status with untracked file in nested submodule (short)' '
366 git -C super status --short >output &&
367 diff output - <<-\EOF
368 ? sub1
369 ? sub2
370 ? sub3
371 EOF
372'
373
374test_expect_success 'setup superproject with modified file in nested submodule' '
375 git -C super/sub1/sub2 add file &&
376 git -C super/sub2 add file
377'
378
379test_expect_success 'status with added file in nested submodule (porcelain)' '
380 git -C super status --porcelain >output &&
381 diff output - <<-\EOF
382 M sub1
383 M sub2
384 M sub3
385 EOF
386'
387
388test_expect_success 'status with added file in nested submodule (porcelain=2)' '
389 git -C super status --porcelain=2 >output &&
390 sanitize_output output &&
391 diff output - <<-\EOF
392 1 .M S.M. 160000 160000 160000 HASH HASH sub1
393 1 .M S.M. 160000 160000 160000 HASH HASH sub2
394 1 .M S..U 160000 160000 160000 HASH HASH sub3
395 EOF
396'
397
398test_expect_success 'status with added file in nested submodule (short)' '
399 git -C super status --short >output &&
400 diff output - <<-\EOF
401 m sub1
402 m sub2
403 ? sub3
404 EOF
405'
406
407test_done