submodule.c: stricter checking for submodules in is_submodule_modified
[git] / t / t7506-status-submodule.sh
1 #!/bin/sh
2
3 test_description='git status for submodule'
4
5 . ./test-lib.sh
6
7 test_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
20 test_expect_success 'setup' '
21         test_create_repo_with_commit sub &&
22         echo output > .gitignore &&
23         git add sub .gitignore &&
24         git commit -m "Add submodule sub"
25 '
26
27 test_expect_success 'status clean' '
28         git status >output &&
29         test_i18ngrep "nothing to commit" output
30 '
31
32 test_expect_success 'commit --dry-run -a clean' '
33         test_must_fail git commit --dry-run -a >output &&
34         test_i18ngrep "nothing to commit" output
35 '
36
37 test_expect_success 'status with modified file in submodule' '
38         (cd sub && git reset --hard) &&
39         echo "changed" >sub/foo &&
40         git status >output &&
41         test_i18ngrep "modified:   sub (modified content)" output
42 '
43
44 test_expect_success 'status with modified file in submodule (porcelain)' '
45         (cd sub && git reset --hard) &&
46         echo "changed" >sub/foo &&
47         git status --porcelain >output &&
48         diff output - <<-\EOF
49          M sub
50         EOF
51 '
52
53 test_expect_success 'status with added file in submodule' '
54         (cd sub && git reset --hard && echo >foo && git add foo) &&
55         git status >output &&
56         test_i18ngrep "modified:   sub (modified content)" output
57 '
58
59 test_expect_success 'status with added file in submodule (porcelain)' '
60         (cd sub && git reset --hard && echo >foo && git add foo) &&
61         git status --porcelain >output &&
62         diff output - <<-\EOF
63          M sub
64         EOF
65 '
66
67 test_expect_success 'status with untracked file in submodule' '
68         (cd sub && git reset --hard) &&
69         echo "content" >sub/new-file &&
70         git status >output &&
71         test_i18ngrep "modified:   sub (untracked content)" output
72 '
73
74 test_expect_success 'status -uno with untracked file in submodule' '
75         git status -uno >output &&
76         test_i18ngrep "^nothing to commit" output
77 '
78
79 test_expect_success 'status with untracked file in submodule (porcelain)' '
80         git status --porcelain >output &&
81         diff output - <<-\EOF
82          M sub
83         EOF
84 '
85
86 test_expect_success 'status with added and untracked file in submodule' '
87         (cd sub && git reset --hard && echo >foo && git add foo) &&
88         echo "content" >sub/new-file &&
89         git status >output &&
90         test_i18ngrep "modified:   sub (modified content, untracked content)" output
91 '
92
93 test_expect_success 'status with added and untracked file in submodule (porcelain)' '
94         (cd sub && git reset --hard && echo >foo && git add foo) &&
95         echo "content" >sub/new-file &&
96         git status --porcelain >output &&
97         diff output - <<-\EOF
98          M sub
99         EOF
100 '
101
102 test_expect_success 'status with modified file in modified submodule' '
103         (cd sub && git reset --hard) &&
104         rm sub/new-file &&
105         (cd sub && echo "next change" >foo && git commit -m "next change" foo) &&
106         echo "changed" >sub/foo &&
107         git status >output &&
108         test_i18ngrep "modified:   sub (new commits, modified content)" output
109 '
110
111 test_expect_success 'status with modified file in modified submodule (porcelain)' '
112         (cd sub && git reset --hard) &&
113         echo "changed" >sub/foo &&
114         git status --porcelain >output &&
115         diff output - <<-\EOF
116          M sub
117         EOF
118 '
119
120 test_expect_success 'status with added file in modified submodule' '
121         (cd sub && git reset --hard && echo >foo && git add foo) &&
122         git status >output &&
123         test_i18ngrep "modified:   sub (new commits, modified content)" output
124 '
125
126 test_expect_success 'status with added file in modified submodule (porcelain)' '
127         (cd sub && git reset --hard && echo >foo && git add foo) &&
128         git status --porcelain >output &&
129         diff output - <<-\EOF
130          M sub
131         EOF
132 '
133
134 test_expect_success 'status with untracked file in modified submodule' '
135         (cd sub && git reset --hard) &&
136         echo "content" >sub/new-file &&
137         git status >output &&
138         test_i18ngrep "modified:   sub (new commits, untracked content)" output
139 '
140
141 test_expect_success 'status with untracked file in modified submodule (porcelain)' '
142         git status --porcelain >output &&
143         diff output - <<-\EOF
144          M sub
145         EOF
146 '
147
148 test_expect_success 'status with added and untracked file in modified submodule' '
149         (cd sub && git reset --hard && echo >foo && git add foo) &&
150         echo "content" >sub/new-file &&
151         git status >output &&
152         test_i18ngrep "modified:   sub (new commits, modified content, untracked content)" output
153 '
154
155 test_expect_success 'status with added and untracked file in modified submodule (porcelain)' '
156         (cd sub && git reset --hard && echo >foo && git add foo) &&
157         echo "content" >sub/new-file &&
158         git status --porcelain >output &&
159         diff output - <<-\EOF
160          M sub
161         EOF
162 '
163
164 test_expect_success 'setup .git file for sub' '
165         (cd sub &&
166          rm -f new-file
167          REAL="$(pwd)/../.real" &&
168          mv .git "$REAL"
169          echo "gitdir: $REAL" >.git) &&
170          echo .real >>.gitignore &&
171          git commit -m "added .real to .gitignore" .gitignore
172 '
173
174 test_expect_success 'status with added file in modified submodule with .git file' '
175         (cd sub && git reset --hard && echo >foo && git add foo) &&
176         git status >output &&
177         test_i18ngrep "modified:   sub (new commits, modified content)" output
178 '
179
180 test_expect_success 'status with a lot of untracked files in the submodule' '
181         (
182                 cd sub
183                 i=0 &&
184                 while test $i -lt 1024
185                 do
186                         >some-file-$i
187                         i=$(( $i + 1 ))
188                 done
189         ) &&
190         git status --porcelain sub 2>err.actual &&
191         test_must_be_empty err.actual &&
192         rm err.actual
193 '
194
195 test_expect_success 'rm submodule contents' '
196         rm -rf sub &&
197         mkdir sub
198 '
199
200 test_expect_success 'status clean (empty submodule dir)' '
201         git status >output &&
202         test_i18ngrep "nothing to commit" output
203 '
204
205 test_expect_success 'status -a clean (empty submodule dir)' '
206         test_must_fail git commit --dry-run -a >output &&
207         test_i18ngrep "nothing to commit" output
208 '
209
210 cat >status_expect <<\EOF
211 AA .gitmodules
212 A  sub1
213 EOF
214
215 test_expect_success 'status with merge conflict in .gitmodules' '
216         git clone . super &&
217         test_create_repo_with_commit sub1 &&
218         test_tick &&
219         test_create_repo_with_commit sub2 &&
220         (
221                 cd super &&
222                 prev=$(git rev-parse HEAD) &&
223                 git checkout -b add_sub1 &&
224                 git submodule add ../sub1 &&
225                 git commit -m "add sub1" &&
226                 git checkout -b add_sub2 $prev &&
227                 git submodule add ../sub2 &&
228                 git commit -m "add sub2" &&
229                 git checkout -b merge_conflict_gitmodules &&
230                 test_must_fail git merge add_sub1 &&
231                 git status -s >../status_actual 2>&1
232         ) &&
233         test_cmp status_actual status_expect
234 '
235
236 sha1_merge_sub1=$(cd sub1 && git rev-parse HEAD)
237 sha1_merge_sub2=$(cd sub2 && git rev-parse HEAD)
238 short_sha1_merge_sub1=$(cd sub1 && git rev-parse --short HEAD)
239 short_sha1_merge_sub2=$(cd sub2 && git rev-parse --short HEAD)
240 cat >diff_expect <<\EOF
241 diff --cc .gitmodules
242 index badaa4c,44f999a..0000000
243 --- a/.gitmodules
244 +++ b/.gitmodules
245 @@@ -1,3 -1,3 +1,9 @@@
246 ++<<<<<<< HEAD
247  +[submodule "sub2"]
248  +      path = sub2
249  +      url = ../sub2
250 ++=======
251 + [submodule "sub1"]
252 +       path = sub1
253 +       url = ../sub1
254 ++>>>>>>> add_sub1
255 EOF
256
257 cat >diff_submodule_expect <<\EOF
258 diff --cc .gitmodules
259 index badaa4c,44f999a..0000000
260 --- a/.gitmodules
261 +++ b/.gitmodules
262 @@@ -1,3 -1,3 +1,9 @@@
263 ++<<<<<<< HEAD
264  +[submodule "sub2"]
265  +      path = sub2
266  +      url = ../sub2
267 ++=======
268 + [submodule "sub1"]
269 +       path = sub1
270 +       url = ../sub1
271 ++>>>>>>> add_sub1
272 EOF
273
274 test_expect_success 'diff with merge conflict in .gitmodules' '
275         (
276                 cd super &&
277                 git diff >../diff_actual 2>&1
278         ) &&
279         test_cmp diff_actual diff_expect
280 '
281
282 test_expect_success 'diff --submodule with merge conflict in .gitmodules' '
283         (
284                 cd super &&
285                 git diff --submodule >../diff_submodule_actual 2>&1
286         ) &&
287         test_cmp diff_submodule_actual diff_submodule_expect
288 '
289
290 test_done