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