t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / t / t4041-diff-submodule-option.sh
blob8fc40e75eb3c78d911cc48ca535abbd1c569cba9
1 #!/bin/sh
3 # Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
7 test_description='Support for verbose submodule differences in git diff
9 This test tries to verify the sanity of the --submodule option of git diff.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
18 # Tested non-UTF-8 encoding
19 test_encoding="ISO8859-1"
21 # String "added" in German (translated with Google Translate), encoded in UTF-8,
22 # used in sample commit log messages in add_file() function below.
23 added=$(printf "hinzugef\303\274gt")
24 add_file () {
26 cd "$1" &&
27 shift &&
28 for name
30 echo "$name" >"$name" &&
31 git add "$name" &&
32 test_tick &&
33 # "git commit -m" would break MinGW, as Windows refuse to pass
34 # $test_encoding encoded parameter to git.
35 echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
36 git -c "i18n.commitEncoding=$test_encoding" commit -F -
37 done >/dev/null &&
38 git rev-parse --short --verify HEAD
41 commit_file () {
42 test_tick &&
43 git commit "$@" -m "Commit $*" >/dev/null
46 test_create_repo sm1 &&
47 add_file . foo >/dev/null
49 head1=$(add_file sm1 foo1 foo2)
50 fullhead1=$(cd sm1; git rev-parse --verify HEAD)
52 test_expect_success 'added submodule' '
53 git add sm1 &&
54 git diff-index -p --submodule=log HEAD >actual &&
55 cat >expected <<-EOF &&
56 Submodule sm1 0000000...$head1 (new submodule)
57 EOF
58 test_cmp expected actual
61 test_expect_success 'added submodule, set diff.submodule' '
62 git config diff.submodule log &&
63 git add sm1 &&
64 git diff --cached >actual &&
65 cat >expected <<-EOF &&
66 Submodule sm1 0000000...$head1 (new submodule)
67 EOF
68 git config --unset diff.submodule &&
69 test_cmp expected actual
72 test_expect_success '--submodule=short overrides diff.submodule' '
73 test_config diff.submodule log &&
74 git add sm1 &&
75 git diff --submodule=short --cached >actual &&
76 cat >expected <<-EOF &&
77 diff --git a/sm1 b/sm1
78 new file mode 160000
79 index 0000000..$head1
80 --- /dev/null
81 +++ b/sm1
82 @@ -0,0 +1 @@
83 +Subproject commit $fullhead1
84 EOF
85 test_cmp expected actual
88 test_expect_success 'diff.submodule does not affect plumbing' '
89 test_config diff.submodule log &&
90 git diff-index -p HEAD >actual &&
91 cat >expected <<-EOF &&
92 diff --git a/sm1 b/sm1
93 new file mode 160000
94 index 0000000..$head1
95 --- /dev/null
96 +++ b/sm1
97 @@ -0,0 +1 @@
98 +Subproject commit $fullhead1
99 EOF
100 test_cmp expected actual
103 commit_file sm1 &&
104 head2=$(add_file sm1 foo3)
106 test_expect_success 'modified submodule(forward)' '
107 git diff-index -p --submodule=log HEAD >actual &&
108 cat >expected <<-EOF &&
109 Submodule sm1 $head1..$head2:
110 > Add foo3 ($added foo3)
112 test_cmp expected actual
115 test_expect_success 'modified submodule(forward)' '
116 git diff --submodule=log >actual &&
117 cat >expected <<-EOF &&
118 Submodule sm1 $head1..$head2:
119 > Add foo3 ($added foo3)
121 test_cmp expected actual
124 test_expect_success 'modified submodule(forward) --submodule' '
125 git diff --submodule >actual &&
126 cat >expected <<-EOF &&
127 Submodule sm1 $head1..$head2:
128 > Add foo3 ($added foo3)
130 test_cmp expected actual
133 fullhead2=$(cd sm1; git rev-parse --verify HEAD)
134 test_expect_success 'modified submodule(forward) --submodule=short' '
135 git diff --submodule=short >actual &&
136 cat >expected <<-EOF &&
137 diff --git a/sm1 b/sm1
138 index $head1..$head2 160000
139 --- a/sm1
140 +++ b/sm1
141 @@ -1 +1 @@
142 -Subproject commit $fullhead1
143 +Subproject commit $fullhead2
145 test_cmp expected actual
148 commit_file sm1 &&
149 head3=$(
150 cd sm1 &&
151 git reset --hard HEAD~2 >/dev/null &&
152 git rev-parse --short --verify HEAD
155 test_expect_success 'modified submodule(backward)' '
156 git diff-index -p --submodule=log HEAD >actual &&
157 cat >expected <<-EOF &&
158 Submodule sm1 $head2..$head3 (rewind):
159 < Add foo3 ($added foo3)
160 < Add foo2 ($added foo2)
162 test_cmp expected actual
165 head4=$(add_file sm1 foo4 foo5)
166 test_expect_success 'modified submodule(backward and forward)' '
167 git diff-index -p --submodule=log HEAD >actual &&
168 cat >expected <<-EOF &&
169 Submodule sm1 $head2...$head4:
170 > Add foo5 ($added foo5)
171 > Add foo4 ($added foo4)
172 < Add foo3 ($added foo3)
173 < Add foo2 ($added foo2)
175 test_cmp expected actual
178 commit_file sm1 &&
179 mv sm1 sm1-bak &&
180 echo sm1 >sm1 &&
181 head5=$(git hash-object sm1 | cut -c1-7) &&
182 git add sm1 &&
183 rm -f sm1 &&
184 mv sm1-bak sm1
186 test_expect_success 'typechanged submodule(submodule->blob), --cached' '
187 git diff --submodule=log --cached >actual &&
188 cat >expected <<-EOF &&
189 Submodule sm1 $head4...0000000 (submodule deleted)
190 diff --git a/sm1 b/sm1
191 new file mode 100644
192 index 0000000..$head5
193 --- /dev/null
194 +++ b/sm1
195 @@ -0,0 +1 @@
196 +sm1
198 test_cmp expected actual
201 test_expect_success 'typechanged submodule(submodule->blob)' '
202 git diff --submodule=log >actual &&
203 cat >expected <<-EOF &&
204 diff --git a/sm1 b/sm1
205 deleted file mode 100644
206 index $head5..0000000
207 --- a/sm1
208 +++ /dev/null
209 @@ -1 +0,0 @@
210 -sm1
211 Submodule sm1 0000000...$head4 (new submodule)
213 test_cmp expected actual
216 rm -rf sm1 &&
217 git checkout-index sm1
218 test_expect_success 'typechanged submodule(submodule->blob)' '
219 git diff-index -p --submodule=log HEAD >actual &&
220 cat >expected <<-EOF &&
221 Submodule sm1 $head4...0000000 (submodule deleted)
222 diff --git a/sm1 b/sm1
223 new file mode 100644
224 index 0000000..$head5
225 --- /dev/null
226 +++ b/sm1
227 @@ -0,0 +1 @@
228 +sm1
230 test_cmp expected actual
233 rm -f sm1 &&
234 test_create_repo sm1 &&
235 head6=$(add_file sm1 foo6 foo7)
236 fullhead6=$(cd sm1; git rev-parse --verify HEAD)
237 test_expect_success 'nonexistent commit' '
238 git diff-index -p --submodule=log HEAD >actual &&
239 cat >expected <<-EOF &&
240 Submodule sm1 $head4...$head6 (commits not present)
242 test_cmp expected actual
245 commit_file
246 test_expect_success 'typechanged submodule(blob->submodule)' '
247 git diff-index -p --submodule=log HEAD >actual &&
248 cat >expected <<-EOF &&
249 diff --git a/sm1 b/sm1
250 deleted file mode 100644
251 index $head5..0000000
252 --- a/sm1
253 +++ /dev/null
254 @@ -1 +0,0 @@
255 -sm1
256 Submodule sm1 0000000...$head6 (new submodule)
258 test_cmp expected actual
261 commit_file sm1 &&
262 test_expect_success 'submodule is up to date' '
263 git diff-index -p --submodule=log HEAD >actual &&
264 test_must_be_empty actual
267 test_expect_success 'submodule contains untracked content' '
268 echo new > sm1/new-file &&
269 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
270 cat >expected <<-EOF &&
271 Submodule sm1 contains untracked content
273 test_cmp expected actual
276 test_expect_success 'submodule contains untracked content (untracked ignored)' '
277 git diff-index -p --submodule=log HEAD >actual &&
278 test_must_be_empty actual
281 test_expect_success 'submodule contains untracked content (dirty ignored)' '
282 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
283 test_must_be_empty actual
286 test_expect_success 'submodule contains untracked content (all ignored)' '
287 git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
288 test_must_be_empty actual
291 test_expect_success 'submodule contains untracked and modified content' '
292 echo new > sm1/foo6 &&
293 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
294 cat >expected <<-EOF &&
295 Submodule sm1 contains untracked content
296 Submodule sm1 contains modified content
298 test_cmp expected actual
301 test_expect_success 'submodule contains untracked and modified content (untracked ignored)' '
302 echo new > sm1/foo6 &&
303 git diff-index -p --submodule=log HEAD >actual &&
304 cat >expected <<-EOF &&
305 Submodule sm1 contains modified content
307 test_cmp expected actual
310 test_expect_success 'submodule contains untracked and modified content (dirty ignored)' '
311 echo new > sm1/foo6 &&
312 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
313 test_must_be_empty actual
316 test_expect_success 'submodule contains untracked and modified content (all ignored)' '
317 echo new > sm1/foo6 &&
318 git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
319 test_must_be_empty actual
322 test_expect_success 'submodule contains modified content' '
323 rm -f sm1/new-file &&
324 git diff-index -p --submodule=log HEAD >actual &&
325 cat >expected <<-EOF &&
326 Submodule sm1 contains modified content
328 test_cmp expected actual
331 (cd sm1; git commit -mchange foo6 >/dev/null) &&
332 head8=$(cd sm1; git rev-parse --short --verify HEAD) &&
333 test_expect_success 'submodule is modified' '
334 git diff-index -p --submodule=log HEAD >actual &&
335 cat >expected <<-EOF &&
336 Submodule sm1 $head6..$head8:
337 > change
339 test_cmp expected actual
342 test_expect_success 'modified submodule contains untracked content' '
343 echo new > sm1/new-file &&
344 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
345 cat >expected <<-EOF &&
346 Submodule sm1 contains untracked content
347 Submodule sm1 $head6..$head8:
348 > change
350 test_cmp expected actual
353 test_expect_success 'modified submodule contains untracked content (untracked ignored)' '
354 git diff-index -p --submodule=log HEAD >actual &&
355 cat >expected <<-EOF &&
356 Submodule sm1 $head6..$head8:
357 > change
359 test_cmp expected actual
362 test_expect_success 'modified submodule contains untracked content (dirty ignored)' '
363 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
364 cat >expected <<-EOF &&
365 Submodule sm1 $head6..$head8:
366 > change
368 test_cmp expected actual
371 test_expect_success 'modified submodule contains untracked content (all ignored)' '
372 git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
373 test_must_be_empty actual
376 test_expect_success 'modified submodule contains untracked and modified content' '
377 echo modification >> sm1/foo6 &&
378 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
379 cat >expected <<-EOF &&
380 Submodule sm1 contains untracked content
381 Submodule sm1 contains modified content
382 Submodule sm1 $head6..$head8:
383 > change
385 test_cmp expected actual
388 test_expect_success 'modified submodule contains untracked and modified content (untracked ignored)' '
389 echo modification >> sm1/foo6 &&
390 git diff-index -p --submodule=log HEAD >actual &&
391 cat >expected <<-EOF &&
392 Submodule sm1 contains modified content
393 Submodule sm1 $head6..$head8:
394 > change
396 test_cmp expected actual
399 test_expect_success 'modified submodule contains untracked and modified content (dirty ignored)' '
400 echo modification >> sm1/foo6 &&
401 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
402 cat >expected <<-EOF &&
403 Submodule sm1 $head6..$head8:
404 > change
406 test_cmp expected actual
409 test_expect_success 'modified submodule contains untracked and modified content (all ignored)' '
410 echo modification >> sm1/foo6 &&
411 git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
412 test_must_be_empty actual
415 test_expect_success 'modified submodule contains modified content' '
416 rm -f sm1/new-file &&
417 git diff-index -p --submodule=log HEAD >actual &&
418 cat >expected <<-EOF &&
419 Submodule sm1 contains modified content
420 Submodule sm1 $head6..$head8:
421 > change
423 test_cmp expected actual
426 rm -rf sm1
427 test_expect_success 'deleted submodule' '
428 git diff-index -p --submodule=log HEAD >actual &&
429 cat >expected <<-EOF &&
430 Submodule sm1 $head6...0000000 (submodule deleted)
432 test_cmp expected actual
435 test_expect_success 'create second submodule' '
436 test_create_repo sm2 &&
437 head7=$(add_file sm2 foo8 foo9) &&
438 git add sm2
441 test_expect_success 'multiple submodules' '
442 git diff-index -p --submodule=log HEAD >actual &&
443 cat >expected <<-EOF &&
444 Submodule sm1 $head6...0000000 (submodule deleted)
445 Submodule sm2 0000000...$head7 (new submodule)
447 test_cmp expected actual
450 test_expect_success 'path filter' '
451 git diff-index -p --submodule=log HEAD sm2 >actual &&
452 cat >expected <<-EOF &&
453 Submodule sm2 0000000...$head7 (new submodule)
455 test_cmp expected actual
458 commit_file sm2
459 test_expect_success 'given commit' '
460 git diff-index -p --submodule=log HEAD^ >actual &&
461 cat >expected <<-EOF &&
462 Submodule sm1 $head6...0000000 (submodule deleted)
463 Submodule sm2 0000000...$head7 (new submodule)
465 test_cmp expected actual
468 test_expect_success 'given commit --submodule' '
469 git diff-index -p --submodule HEAD^ >actual &&
470 cat >expected <<-EOF &&
471 Submodule sm1 $head6...0000000 (submodule deleted)
472 Submodule sm2 0000000...$head7 (new submodule)
474 test_cmp expected actual
477 fullhead7=$(cd sm2; git rev-parse --verify HEAD)
479 test_expect_success 'given commit --submodule=short' '
480 git diff-index -p --submodule=short HEAD^ >actual &&
481 cat >expected <<-EOF &&
482 diff --git a/sm1 b/sm1
483 deleted file mode 160000
484 index $head6..0000000
485 --- a/sm1
486 +++ /dev/null
487 @@ -1 +0,0 @@
488 -Subproject commit $fullhead6
489 diff --git a/sm2 b/sm2
490 new file mode 160000
491 index 0000000..$head7
492 --- /dev/null
493 +++ b/sm2
494 @@ -0,0 +1 @@
495 +Subproject commit $fullhead7
497 test_cmp expected actual
500 test_expect_success 'setup .git file for sm2' '
501 (cd sm2 &&
502 REAL="$(pwd)/../.real" &&
503 mv .git "$REAL" &&
504 echo "gitdir: $REAL" >.git)
507 test_expect_success 'diff --submodule with .git file' '
508 git diff --submodule HEAD^ >actual &&
509 cat >expected <<-EOF &&
510 Submodule sm1 $head6...0000000 (submodule deleted)
511 Submodule sm2 0000000...$head7 (new submodule)
513 test_cmp expected actual
516 test_expect_success 'diff --submodule with objects referenced by alternates' '
517 mkdir sub_alt &&
518 (cd sub_alt &&
519 git init &&
520 echo a >a &&
521 git add a &&
522 git commit -m a
523 ) &&
524 mkdir super &&
525 (cd super &&
526 git clone -s ../sub_alt sub &&
527 git init &&
528 git add sub &&
529 git commit -m "sub a"
530 ) &&
531 (cd sub_alt &&
532 sha1_before=$(git rev-parse --short HEAD) &&
533 echo b >b &&
534 git add b &&
535 git commit -m b &&
536 sha1_after=$(git rev-parse --short HEAD) &&
538 echo "Submodule sub $sha1_before..$sha1_after:" &&
539 echo " > b"
540 } >../expected
541 ) &&
542 (cd super &&
543 (cd sub &&
544 git fetch &&
545 git checkout origin/main
546 ) &&
547 git diff --submodule > ../actual
548 ) &&
549 test_cmp expected actual
552 test_done