The eleventh batch
[alt-git.git] / t / t4041-diff-submodule-option.sh
blobaa149e0085ec991046b1a56e21444fbc89a69de0
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 # Test non-UTF-8 encoding in case iconv is available.
19 if test_have_prereq ICONV
20 then
21 test_encoding="ISO8859-1"
22 # String "added" in German (translated with Google Translate), encoded in UTF-8,
23 # used in sample commit log messages in add_file() function below.
24 added=$(printf "hinzugef\303\274gt")
25 else
26 test_encoding="UTF-8"
27 added="added"
30 add_file () {
32 cd "$1" &&
33 shift &&
34 for name
36 echo "$name" >"$name" &&
37 git add "$name" &&
38 test_tick &&
39 # "git commit -m" would break MinGW, as Windows refuse to pass
40 # $test_encoding encoded parameter to git.
41 echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
42 git -c "i18n.commitEncoding=$test_encoding" commit -F -
43 done >/dev/null &&
44 git rev-parse --short --verify HEAD
47 commit_file () {
48 test_tick &&
49 git commit "$@" -m "Commit $*" >/dev/null
52 test_create_repo sm1 &&
53 add_file . foo >/dev/null
55 head1=$(add_file sm1 foo1 foo2)
56 fullhead1=$(cd sm1; git rev-parse --verify HEAD)
58 test_expect_success 'added submodule' '
59 git add sm1 &&
60 git diff-index -p --submodule=log HEAD >actual &&
61 cat >expected <<-EOF &&
62 Submodule sm1 0000000...$head1 (new submodule)
63 EOF
64 test_cmp expected actual
67 test_expect_success 'added submodule, set diff.submodule' '
68 git config diff.submodule log &&
69 git add sm1 &&
70 git diff --cached >actual &&
71 cat >expected <<-EOF &&
72 Submodule sm1 0000000...$head1 (new submodule)
73 EOF
74 git config --unset diff.submodule &&
75 test_cmp expected actual
78 test_expect_success '--submodule=short overrides diff.submodule' '
79 test_config diff.submodule log &&
80 git add sm1 &&
81 git diff --submodule=short --cached >actual &&
82 cat >expected <<-EOF &&
83 diff --git a/sm1 b/sm1
84 new file mode 160000
85 index 0000000..$head1
86 --- /dev/null
87 +++ b/sm1
88 @@ -0,0 +1 @@
89 +Subproject commit $fullhead1
90 EOF
91 test_cmp expected actual
94 test_expect_success 'diff.submodule does not affect plumbing' '
95 test_config diff.submodule log &&
96 git diff-index -p HEAD >actual &&
97 cat >expected <<-EOF &&
98 diff --git a/sm1 b/sm1
99 new file mode 160000
100 index 0000000..$head1
101 --- /dev/null
102 +++ b/sm1
103 @@ -0,0 +1 @@
104 +Subproject commit $fullhead1
106 test_cmp expected actual
109 commit_file sm1 &&
110 head2=$(add_file sm1 foo3)
112 test_expect_success 'modified submodule(forward)' '
113 git diff-index -p --submodule=log HEAD >actual &&
114 cat >expected <<-EOF &&
115 Submodule sm1 $head1..$head2:
116 > Add foo3 ($added foo3)
118 test_cmp expected actual
121 test_expect_success 'modified submodule(forward)' '
122 git diff --submodule=log >actual &&
123 cat >expected <<-EOF &&
124 Submodule sm1 $head1..$head2:
125 > Add foo3 ($added foo3)
127 test_cmp expected actual
130 test_expect_success 'modified submodule(forward) --submodule' '
131 git diff --submodule >actual &&
132 cat >expected <<-EOF &&
133 Submodule sm1 $head1..$head2:
134 > Add foo3 ($added foo3)
136 test_cmp expected actual
139 fullhead2=$(cd sm1; git rev-parse --verify HEAD)
140 test_expect_success 'modified submodule(forward) --submodule=short' '
141 git diff --submodule=short >actual &&
142 cat >expected <<-EOF &&
143 diff --git a/sm1 b/sm1
144 index $head1..$head2 160000
145 --- a/sm1
146 +++ b/sm1
147 @@ -1 +1 @@
148 -Subproject commit $fullhead1
149 +Subproject commit $fullhead2
151 test_cmp expected actual
154 commit_file sm1 &&
155 head3=$(
156 cd sm1 &&
157 git reset --hard HEAD~2 >/dev/null &&
158 git rev-parse --short --verify HEAD
161 test_expect_success 'modified submodule(backward)' '
162 git diff-index -p --submodule=log HEAD >actual &&
163 cat >expected <<-EOF &&
164 Submodule sm1 $head2..$head3 (rewind):
165 < Add foo3 ($added foo3)
166 < Add foo2 ($added foo2)
168 test_cmp expected actual
171 head4=$(add_file sm1 foo4 foo5)
172 test_expect_success 'modified submodule(backward and forward)' '
173 git diff-index -p --submodule=log HEAD >actual &&
174 cat >expected <<-EOF &&
175 Submodule sm1 $head2...$head4:
176 > Add foo5 ($added foo5)
177 > Add foo4 ($added foo4)
178 < Add foo3 ($added foo3)
179 < Add foo2 ($added foo2)
181 test_cmp expected actual
184 commit_file sm1 &&
185 mv sm1 sm1-bak &&
186 echo sm1 >sm1 &&
187 head5=$(git hash-object sm1 | cut -c1-7) &&
188 git add sm1 &&
189 rm -f sm1 &&
190 mv sm1-bak sm1
192 test_expect_success 'typechanged submodule(submodule->blob), --cached' '
193 git diff --submodule=log --cached >actual &&
194 cat >expected <<-EOF &&
195 Submodule sm1 $head4...0000000 (submodule deleted)
196 diff --git a/sm1 b/sm1
197 new file mode 100644
198 index 0000000..$head5
199 --- /dev/null
200 +++ b/sm1
201 @@ -0,0 +1 @@
202 +sm1
204 test_cmp expected actual
207 test_expect_success 'typechanged submodule(submodule->blob)' '
208 git diff --submodule=log >actual &&
209 cat >expected <<-EOF &&
210 diff --git a/sm1 b/sm1
211 deleted file mode 100644
212 index $head5..0000000
213 --- a/sm1
214 +++ /dev/null
215 @@ -1 +0,0 @@
216 -sm1
217 Submodule sm1 0000000...$head4 (new submodule)
219 test_cmp expected actual
222 rm -rf sm1 &&
223 git checkout-index sm1
224 test_expect_success 'typechanged submodule(submodule->blob)' '
225 git diff-index -p --submodule=log HEAD >actual &&
226 cat >expected <<-EOF &&
227 Submodule sm1 $head4...0000000 (submodule deleted)
228 diff --git a/sm1 b/sm1
229 new file mode 100644
230 index 0000000..$head5
231 --- /dev/null
232 +++ b/sm1
233 @@ -0,0 +1 @@
234 +sm1
236 test_cmp expected actual
239 rm -f sm1 &&
240 test_create_repo sm1 &&
241 head6=$(add_file sm1 foo6 foo7)
242 fullhead6=$(cd sm1; git rev-parse --verify HEAD)
243 test_expect_success 'nonexistent commit' '
244 git diff-index -p --submodule=log HEAD >actual &&
245 cat >expected <<-EOF &&
246 Submodule sm1 $head4...$head6 (commits not present)
248 test_cmp expected actual
251 commit_file
252 test_expect_success 'typechanged submodule(blob->submodule)' '
253 git diff-index -p --submodule=log HEAD >actual &&
254 cat >expected <<-EOF &&
255 diff --git a/sm1 b/sm1
256 deleted file mode 100644
257 index $head5..0000000
258 --- a/sm1
259 +++ /dev/null
260 @@ -1 +0,0 @@
261 -sm1
262 Submodule sm1 0000000...$head6 (new submodule)
264 test_cmp expected actual
267 commit_file sm1 &&
268 test_expect_success 'submodule is up to date' '
269 git diff-index -p --submodule=log HEAD >actual &&
270 test_must_be_empty actual
273 test_expect_success 'submodule contains untracked content' '
274 echo new > sm1/new-file &&
275 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
276 cat >expected <<-EOF &&
277 Submodule sm1 contains untracked content
279 test_cmp expected actual
282 test_expect_success 'submodule contains untracked content (untracked ignored)' '
283 git diff-index -p --submodule=log HEAD >actual &&
284 test_must_be_empty actual
287 test_expect_success 'submodule contains untracked content (dirty ignored)' '
288 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
289 test_must_be_empty actual
292 test_expect_success 'submodule contains untracked content (all ignored)' '
293 git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
294 test_must_be_empty actual
297 test_expect_success 'submodule contains untracked and modified content' '
298 echo new > sm1/foo6 &&
299 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
300 cat >expected <<-EOF &&
301 Submodule sm1 contains untracked content
302 Submodule sm1 contains modified content
304 test_cmp expected actual
307 test_expect_success 'submodule contains untracked and modified content (untracked ignored)' '
308 echo new > sm1/foo6 &&
309 git diff-index -p --submodule=log HEAD >actual &&
310 cat >expected <<-EOF &&
311 Submodule sm1 contains modified content
313 test_cmp expected actual
316 test_expect_success 'submodule contains untracked and modified content (dirty ignored)' '
317 echo new > sm1/foo6 &&
318 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
319 test_must_be_empty actual
322 test_expect_success 'submodule contains untracked and modified content (all ignored)' '
323 echo new > sm1/foo6 &&
324 git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
325 test_must_be_empty actual
328 test_expect_success 'submodule contains modified content' '
329 rm -f sm1/new-file &&
330 git diff-index -p --submodule=log HEAD >actual &&
331 cat >expected <<-EOF &&
332 Submodule sm1 contains modified content
334 test_cmp expected actual
337 (cd sm1; git commit -mchange foo6 >/dev/null) &&
338 head8=$(cd sm1; git rev-parse --short --verify HEAD) &&
339 test_expect_success 'submodule is modified' '
340 git diff-index -p --submodule=log HEAD >actual &&
341 cat >expected <<-EOF &&
342 Submodule sm1 $head6..$head8:
343 > change
345 test_cmp expected actual
348 test_expect_success 'modified submodule contains untracked content' '
349 echo new > sm1/new-file &&
350 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
351 cat >expected <<-EOF &&
352 Submodule sm1 contains untracked content
353 Submodule sm1 $head6..$head8:
354 > change
356 test_cmp expected actual
359 test_expect_success 'modified submodule contains untracked content (untracked ignored)' '
360 git diff-index -p --submodule=log HEAD >actual &&
361 cat >expected <<-EOF &&
362 Submodule sm1 $head6..$head8:
363 > change
365 test_cmp expected actual
368 test_expect_success 'modified submodule contains untracked content (dirty ignored)' '
369 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
370 cat >expected <<-EOF &&
371 Submodule sm1 $head6..$head8:
372 > change
374 test_cmp expected actual
377 test_expect_success 'modified submodule contains untracked content (all ignored)' '
378 git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
379 test_must_be_empty actual
382 test_expect_success 'modified submodule contains untracked and modified content' '
383 echo modification >> sm1/foo6 &&
384 git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
385 cat >expected <<-EOF &&
386 Submodule sm1 contains untracked content
387 Submodule sm1 contains modified content
388 Submodule sm1 $head6..$head8:
389 > change
391 test_cmp expected actual
394 test_expect_success 'modified submodule contains untracked and modified content (untracked ignored)' '
395 echo modification >> sm1/foo6 &&
396 git diff-index -p --submodule=log HEAD >actual &&
397 cat >expected <<-EOF &&
398 Submodule sm1 contains modified content
399 Submodule sm1 $head6..$head8:
400 > change
402 test_cmp expected actual
405 test_expect_success 'modified submodule contains untracked and modified content (dirty ignored)' '
406 echo modification >> sm1/foo6 &&
407 git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
408 cat >expected <<-EOF &&
409 Submodule sm1 $head6..$head8:
410 > change
412 test_cmp expected actual
415 test_expect_success 'modified submodule contains untracked and modified content (all ignored)' '
416 echo modification >> sm1/foo6 &&
417 git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
418 test_must_be_empty actual
421 test_expect_success 'modified submodule contains modified content' '
422 rm -f sm1/new-file &&
423 git diff-index -p --submodule=log HEAD >actual &&
424 cat >expected <<-EOF &&
425 Submodule sm1 contains modified content
426 Submodule sm1 $head6..$head8:
427 > change
429 test_cmp expected actual
432 rm -rf sm1
433 test_expect_success 'deleted submodule' '
434 git diff-index -p --submodule=log HEAD >actual &&
435 cat >expected <<-EOF &&
436 Submodule sm1 $head6...0000000 (submodule deleted)
438 test_cmp expected actual
441 test_expect_success 'create second submodule' '
442 test_create_repo sm2 &&
443 head7=$(add_file sm2 foo8 foo9) &&
444 git add sm2
447 test_expect_success 'multiple submodules' '
448 git diff-index -p --submodule=log HEAD >actual &&
449 cat >expected <<-EOF &&
450 Submodule sm1 $head6...0000000 (submodule deleted)
451 Submodule sm2 0000000...$head7 (new submodule)
453 test_cmp expected actual
456 test_expect_success 'path filter' '
457 git diff-index -p --submodule=log HEAD sm2 >actual &&
458 cat >expected <<-EOF &&
459 Submodule sm2 0000000...$head7 (new submodule)
461 test_cmp expected actual
464 commit_file sm2
465 test_expect_success 'given commit' '
466 git diff-index -p --submodule=log HEAD^ >actual &&
467 cat >expected <<-EOF &&
468 Submodule sm1 $head6...0000000 (submodule deleted)
469 Submodule sm2 0000000...$head7 (new submodule)
471 test_cmp expected actual
474 test_expect_success 'given commit --submodule' '
475 git diff-index -p --submodule HEAD^ >actual &&
476 cat >expected <<-EOF &&
477 Submodule sm1 $head6...0000000 (submodule deleted)
478 Submodule sm2 0000000...$head7 (new submodule)
480 test_cmp expected actual
483 fullhead7=$(cd sm2; git rev-parse --verify HEAD)
485 test_expect_success 'given commit --submodule=short' '
486 git diff-index -p --submodule=short HEAD^ >actual &&
487 cat >expected <<-EOF &&
488 diff --git a/sm1 b/sm1
489 deleted file mode 160000
490 index $head6..0000000
491 --- a/sm1
492 +++ /dev/null
493 @@ -1 +0,0 @@
494 -Subproject commit $fullhead6
495 diff --git a/sm2 b/sm2
496 new file mode 160000
497 index 0000000..$head7
498 --- /dev/null
499 +++ b/sm2
500 @@ -0,0 +1 @@
501 +Subproject commit $fullhead7
503 test_cmp expected actual
506 test_expect_success 'setup .git file for sm2' '
507 (cd sm2 &&
508 REAL="$(pwd)/../.real" &&
509 mv .git "$REAL" &&
510 echo "gitdir: $REAL" >.git)
513 test_expect_success 'diff --submodule with .git file' '
514 git diff --submodule HEAD^ >actual &&
515 cat >expected <<-EOF &&
516 Submodule sm1 $head6...0000000 (submodule deleted)
517 Submodule sm2 0000000...$head7 (new submodule)
519 test_cmp expected actual
522 test_expect_success 'diff --submodule with objects referenced by alternates' '
523 mkdir sub_alt &&
524 (cd sub_alt &&
525 git init &&
526 echo a >a &&
527 git add a &&
528 git commit -m a
529 ) &&
530 mkdir super &&
531 (cd super &&
532 git clone -s ../sub_alt sub &&
533 git init &&
534 git add sub &&
535 git commit -m "sub a"
536 ) &&
537 (cd sub_alt &&
538 sha1_before=$(git rev-parse --short HEAD) &&
539 echo b >b &&
540 git add b &&
541 git commit -m b &&
542 sha1_after=$(git rev-parse --short HEAD) &&
544 echo "Submodule sub $sha1_before..$sha1_after:" &&
545 echo " > b"
546 } >../expected
547 ) &&
548 (cd super &&
549 (cd sub &&
550 git fetch &&
551 git checkout origin/main
552 ) &&
553 git diff --submodule > ../actual
554 ) &&
555 test_cmp expected actual
558 test_done