3 # Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
4 # Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
5 # Copyright (c) 2016 Jacob Keller (copy + convert to --submodule=diff)
8 test_description='Support for diff format verbose submodule difference in git diff
10 This test tries to verify the sanity of --submodule=diff option of git diff.
15 # Tested non-UTF-8 encoding
16 test_encoding="ISO8859-1"
18 # String "added" in German (translated with Google Translate), encoded in UTF-8,
19 # used in sample commit log messages in add_file() function below.
20 added=$(printf "hinzugef\303\274gt")
28 echo "$name" >"$name" &&
31 # "git commit -m" would break MinGW, as Windows refuse to pass
32 # $test_encoding encoded parameter to git.
33 echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
34 git -c "i18n.commitEncoding=$test_encoding" commit -F -
36 git rev-parse --short --verify HEAD
42 git commit "$@" -m "Commit $*" >/dev/null
48 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
49 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
50 "$i" >"$i.compare" || return 1
52 test_cmp "$1.compare" "$2.compare" &&
53 rm -f "$1.compare" "$2.compare"
56 test_expect_success 'setup repository' '
57 test_create_repo sm1 &&
59 head1=$(add_file sm1 foo1 foo2) &&
60 fullhead1=$(git -C sm1 rev-parse --verify HEAD)
63 test_expect_success 'added submodule' '
65 git diff-index -p --submodule=diff HEAD >actual &&
66 cat >expected <<-EOF &&
67 Submodule sm1 0000000...$head1 (new submodule)
68 diff --git a/sm1/foo1 b/sm1/foo1
70 index 0000000..1715acd
75 diff --git a/sm1/foo2 b/sm1/foo2
77 index 0000000..54b060e
83 diff_cmp expected actual
86 test_expect_success 'added submodule, set diff.submodule' '
87 test_config diff.submodule log &&
89 git diff-index -p --submodule=diff HEAD >actual &&
90 cat >expected <<-EOF &&
91 Submodule sm1 0000000...$head1 (new submodule)
92 diff --git a/sm1/foo1 b/sm1/foo1
94 index 0000000..1715acd
99 diff --git a/sm1/foo2 b/sm1/foo2
101 index 0000000..54b060e
107 diff_cmp expected actual
110 test_expect_success '--submodule=short overrides diff.submodule' '
111 test_config diff.submodule log &&
113 git diff --submodule=short --cached >actual &&
114 cat >expected <<-EOF &&
115 diff --git a/sm1 b/sm1
117 index 0000000..$head1
121 +Subproject commit $fullhead1
123 diff_cmp expected actual
126 test_expect_success 'diff.submodule does not affect plumbing' '
127 test_config diff.submodule log &&
128 git diff-index -p HEAD >actual &&
129 cat >expected <<-EOF &&
130 diff --git a/sm1 b/sm1
132 index 0000000..$head1
136 +Subproject commit $fullhead1
138 diff_cmp expected actual
142 head2=$(add_file sm1 foo3)
144 test_expect_success 'modified submodule(forward)' '
145 git diff-index -p --submodule=diff HEAD >actual &&
146 cat >expected <<-EOF &&
147 Submodule sm1 $head1..$head2:
148 diff --git a/sm1/foo3 b/sm1/foo3
150 index 0000000..c1ec6c6
156 diff_cmp expected actual
159 test_expect_success 'modified submodule(forward)' '
160 git diff --submodule=diff >actual &&
161 cat >expected <<-EOF &&
162 Submodule sm1 $head1..$head2:
163 diff --git a/sm1/foo3 b/sm1/foo3
165 index 0000000..c1ec6c6
171 diff_cmp expected actual
174 test_expect_success 'modified submodule(forward) --submodule' '
175 git diff --submodule >actual &&
176 cat >expected <<-EOF &&
177 Submodule sm1 $head1..$head2:
178 > Add foo3 ($added foo3)
180 diff_cmp expected actual
183 fullhead2=$(cd sm1; git rev-parse --verify HEAD)
184 test_expect_success 'modified submodule(forward) --submodule=short' '
185 git diff --submodule=short >actual &&
186 cat >expected <<-EOF &&
187 diff --git a/sm1 b/sm1
188 index $head1..$head2 160000
192 -Subproject commit $fullhead1
193 +Subproject commit $fullhead2
195 diff_cmp expected actual
201 git reset --hard HEAD~2 >/dev/null &&
202 git rev-parse --short --verify HEAD
205 test_expect_success 'modified submodule(backward)' '
206 git diff-index -p --submodule=diff HEAD >actual &&
207 cat >expected <<-EOF &&
208 Submodule sm1 $head2..$head3 (rewind):
209 diff --git a/sm1/foo2 b/sm1/foo2
210 deleted file mode 100644
211 index 54b060e..0000000
216 diff --git a/sm1/foo3 b/sm1/foo3
217 deleted file mode 100644
218 index c1ec6c6..0000000
224 diff_cmp expected actual
227 head4=$(add_file sm1 foo4 foo5)
228 test_expect_success 'modified submodule(backward and forward)' '
229 git diff-index -p --submodule=diff HEAD >actual &&
230 cat >expected <<-EOF &&
231 Submodule sm1 $head2...$head4:
232 diff --git a/sm1/foo2 b/sm1/foo2
233 deleted file mode 100644
234 index 54b060e..0000000
239 diff --git a/sm1/foo3 b/sm1/foo3
240 deleted file mode 100644
241 index c1ec6c6..0000000
246 diff --git a/sm1/foo4 b/sm1/foo4
248 index 0000000..a0016db
253 diff --git a/sm1/foo5 b/sm1/foo5
255 index 0000000..d6f2413
261 diff_cmp expected actual
267 head5=$(git hash-object sm1 | cut -c1-7) &&
272 test_expect_success 'typechanged submodule(submodule->blob), --cached' '
273 git diff --submodule=diff --cached >actual &&
274 cat >expected <<-EOF &&
275 Submodule sm1 $head4...0000000 (submodule deleted)
276 diff --git a/sm1/foo1 b/sm1/foo1
277 deleted file mode 100644
278 index 1715acd..0000000
283 diff --git a/sm1/foo4 b/sm1/foo4
284 deleted file mode 100644
285 index a0016db..0000000
290 diff --git a/sm1/foo5 b/sm1/foo5
291 deleted file mode 100644
292 index d6f2413..0000000
297 diff --git a/sm1 b/sm1
299 index 0000000..9da5fb8
305 diff_cmp expected actual
308 test_expect_success 'typechanged submodule(submodule->blob)' '
309 git diff --submodule=diff >actual &&
310 cat >expected <<-EOF &&
311 diff --git a/sm1 b/sm1
312 deleted file mode 100644
313 index 9da5fb8..0000000
318 Submodule sm1 0000000...$head4 (new submodule)
319 diff --git a/sm1/foo1 b/sm1/foo1
321 index 0000000..1715acd
326 diff --git a/sm1/foo4 b/sm1/foo4
328 index 0000000..a0016db
333 diff --git a/sm1/foo5 b/sm1/foo5
335 index 0000000..d6f2413
341 diff_cmp expected actual
345 git checkout-index sm1
346 test_expect_success 'typechanged submodule(submodule->blob)' '
347 git diff-index -p --submodule=diff HEAD >actual &&
348 cat >expected <<-EOF &&
349 Submodule sm1 $head4...0000000 (submodule deleted)
350 diff --git a/sm1 b/sm1
352 index 0000000..9da5fb8
358 diff_cmp expected actual
362 test_create_repo sm1 &&
363 head6=$(add_file sm1 foo6 foo7)
364 fullhead6=$(cd sm1; git rev-parse --verify HEAD)
365 test_expect_success 'nonexistent commit' '
366 git diff-index -p --submodule=diff HEAD >actual &&
367 cat >expected <<-EOF &&
368 Submodule sm1 $head4...$head6 (commits not present)
370 diff_cmp expected actual
374 test_expect_success 'typechanged submodule(blob->submodule)' '
375 git diff-index -p --submodule=diff HEAD >actual &&
376 cat >expected <<-EOF &&
377 diff --git a/sm1 b/sm1
378 deleted file mode 100644
379 index 9da5fb8..0000000
384 Submodule sm1 0000000...$head6 (new submodule)
385 diff --git a/sm1/foo6 b/sm1/foo6
387 index 0000000..462398b
392 diff --git a/sm1/foo7 b/sm1/foo7
394 index 0000000..6e9262c
400 diff_cmp expected actual
404 test_expect_success 'submodule is up to date' '
405 head7=$(git -C sm1 rev-parse --short --verify HEAD) &&
406 git diff-index -p --submodule=diff HEAD >actual &&
407 test_must_be_empty actual
410 test_expect_success 'submodule contains untracked content' '
411 echo new > sm1/new-file &&
412 git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
413 cat >expected <<-EOF &&
414 Submodule sm1 contains untracked content
416 diff_cmp expected actual
419 test_expect_success 'submodule contains untracked content (untracked ignored)' '
420 git diff-index -p --submodule=diff HEAD >actual &&
421 test_must_be_empty actual
424 test_expect_success 'submodule contains untracked content (dirty ignored)' '
425 git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
426 test_must_be_empty actual
429 test_expect_success 'submodule contains untracked content (all ignored)' '
430 git diff-index -p --ignore-submodules=all --submodule=diff HEAD >actual &&
431 test_must_be_empty actual
434 test_expect_success 'submodule contains untracked and modified content' '
435 echo new > sm1/foo6 &&
436 git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
437 cat >expected <<-EOF &&
438 Submodule sm1 contains untracked content
439 Submodule sm1 contains modified content
440 diff --git a/sm1/foo6 b/sm1/foo6
441 index 462398b..3e75765 100644
448 diff_cmp expected actual
452 test_expect_success 'submodule contains untracked and modified content (untracked ignored)' '
453 echo new > sm1/foo6 &&
454 git diff-index -p --submodule=diff HEAD >actual &&
455 cat >expected <<-EOF &&
456 Submodule sm1 contains modified content
457 diff --git a/sm1/foo6 b/sm1/foo6
458 index 462398b..3e75765 100644
465 diff_cmp expected actual
468 test_expect_success 'submodule contains untracked and modified content (dirty ignored)' '
469 echo new > sm1/foo6 &&
470 git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
471 test_must_be_empty actual
474 test_expect_success 'submodule contains untracked and modified content (all ignored)' '
475 echo new > sm1/foo6 &&
476 git diff-index -p --ignore-submodules --submodule=diff HEAD >actual &&
477 test_must_be_empty actual
480 test_expect_success 'submodule contains modified content' '
481 rm -f sm1/new-file &&
482 git diff-index -p --submodule=diff HEAD >actual &&
483 cat >expected <<-EOF &&
484 Submodule sm1 contains modified content
485 diff --git a/sm1/foo6 b/sm1/foo6
486 index 462398b..3e75765 100644
493 diff_cmp expected actual
496 (cd sm1; git commit -mchange foo6 >/dev/null) &&
497 head8=$(cd sm1; git rev-parse --short --verify HEAD) &&
498 test_expect_success 'submodule is modified' '
499 git diff-index -p --submodule=diff HEAD >actual &&
500 cat >expected <<-EOF &&
501 Submodule sm1 $head7..$head8:
502 diff --git a/sm1/foo6 b/sm1/foo6
503 index 462398b..3e75765 100644
510 diff_cmp expected actual
513 test_expect_success 'modified submodule contains untracked content' '
514 echo new > sm1/new-file &&
515 git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
516 cat >expected <<-EOF &&
517 Submodule sm1 contains untracked content
518 Submodule sm1 $head7..$head8:
519 diff --git a/sm1/foo6 b/sm1/foo6
520 index 462398b..3e75765 100644
527 diff_cmp expected actual
530 test_expect_success 'modified submodule contains untracked content (untracked ignored)' '
531 git diff-index -p --submodule=diff HEAD >actual &&
532 cat >expected <<-EOF &&
533 Submodule sm1 $head7..$head8:
534 diff --git a/sm1/foo6 b/sm1/foo6
535 index 462398b..3e75765 100644
542 diff_cmp expected actual
545 test_expect_success 'modified submodule contains untracked content (dirty ignored)' '
546 git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
547 cat >expected <<-EOF &&
548 Submodule sm1 $head7..$head8:
549 diff --git a/sm1/foo6 b/sm1/foo6
550 index 462398b..3e75765 100644
557 diff_cmp expected actual
560 test_expect_success 'modified submodule contains untracked content (all ignored)' '
561 git diff-index -p --ignore-submodules=all --submodule=diff HEAD >actual &&
562 test_must_be_empty actual
565 test_expect_success 'modified submodule contains untracked and modified content' '
566 echo modification >> sm1/foo6 &&
567 git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
568 cat >expected <<-EOF &&
569 Submodule sm1 contains untracked content
570 Submodule sm1 contains modified content
571 Submodule sm1 $head7..$head8:
572 diff --git a/sm1/foo6 b/sm1/foo6
573 index 462398b..dfda541 100644
581 diff_cmp expected actual
584 test_expect_success 'modified submodule contains untracked and modified content (untracked ignored)' '
585 echo modification >> sm1/foo6 &&
586 git diff-index -p --submodule=diff HEAD >actual &&
587 cat >expected <<-EOF &&
588 Submodule sm1 contains modified content
589 Submodule sm1 $head7..$head8:
590 diff --git a/sm1/foo6 b/sm1/foo6
591 index 462398b..e20e2d9 100644
600 diff_cmp expected actual
603 test_expect_success 'modified submodule contains untracked and modified content (dirty ignored)' '
604 echo modification >> sm1/foo6 &&
605 git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
606 cat >expected <<-EOF &&
607 Submodule sm1 $head7..$head8:
608 diff --git a/sm1/foo6 b/sm1/foo6
609 index 462398b..3e75765 100644
616 diff_cmp expected actual
619 test_expect_success 'modified submodule contains untracked and modified content (all ignored)' '
620 echo modification >> sm1/foo6 &&
621 git diff-index -p --ignore-submodules --submodule=diff HEAD >actual &&
622 test_must_be_empty actual
626 test_expect_success 'modified submodule contains modified content' '
627 rm -f sm1/new-file &&
628 git diff-index -p --submodule=diff HEAD >actual &&
629 cat >expected <<-EOF &&
630 Submodule sm1 contains modified content
631 Submodule sm1 $head7..$head8:
632 diff --git a/sm1/foo6 b/sm1/foo6
633 index 462398b..ac466ca 100644
644 diff_cmp expected actual
648 test_expect_success 'deleted submodule' '
649 git diff-index -p --submodule=diff HEAD >actual &&
650 cat >expected <<-EOF &&
651 Submodule sm1 $head7...0000000 (submodule deleted)
653 diff_cmp expected actual
656 test_expect_success 'create second submodule' '
657 test_create_repo sm2 &&
658 head9=$(add_file sm2 foo8 foo9) &&
662 test_expect_success 'multiple submodules' '
663 git diff-index -p --submodule=diff HEAD >actual &&
664 cat >expected <<-EOF &&
665 Submodule sm1 $head7...0000000 (submodule deleted)
666 Submodule sm2 0000000...$head9 (new submodule)
667 diff --git a/sm2/foo8 b/sm2/foo8
669 index 0000000..db9916b
674 diff --git a/sm2/foo9 b/sm2/foo9
676 index 0000000..9c3b4f6
682 diff_cmp expected actual
685 test_expect_success 'path filter' '
686 git diff-index -p --submodule=diff HEAD sm2 >actual &&
687 cat >expected <<-EOF &&
688 Submodule sm2 0000000...$head9 (new submodule)
689 diff --git a/sm2/foo8 b/sm2/foo8
691 index 0000000..db9916b
696 diff --git a/sm2/foo9 b/sm2/foo9
698 index 0000000..9c3b4f6
704 diff_cmp expected actual
708 test_expect_success 'given commit' '
709 git diff-index -p --submodule=diff HEAD^ >actual &&
710 cat >expected <<-EOF &&
711 Submodule sm1 $head7...0000000 (submodule deleted)
712 Submodule sm2 0000000...$head9 (new submodule)
713 diff --git a/sm2/foo8 b/sm2/foo8
715 index 0000000..db9916b
720 diff --git a/sm2/foo9 b/sm2/foo9
722 index 0000000..9c3b4f6
728 diff_cmp expected actual
731 test_expect_success 'setup .git file for sm2' '
733 REAL="$(pwd)/../.real" &&
735 echo "gitdir: $REAL" >.git)
738 test_expect_success 'diff --submodule=diff with .git file' '
739 git diff --submodule=diff HEAD^ >actual &&
740 cat >expected <<-EOF &&
741 Submodule sm1 $head7...0000000 (submodule deleted)
742 Submodule sm2 0000000...$head9 (new submodule)
743 diff --git a/sm2/foo8 b/sm2/foo8
745 index 0000000..db9916b
750 diff --git a/sm2/foo9 b/sm2/foo9
752 index 0000000..9c3b4f6
758 diff_cmp expected actual
761 test_expect_success 'setup nested submodule' '
762 git submodule add -f ./sm2 &&
763 git commit -a -m "add sm2" &&
764 git -C sm2 submodule add ../sm2 nested &&
765 git -C sm2 commit -a -m "nested sub" &&
766 head10=$(git -C sm2 rev-parse --short --verify HEAD)
769 test_expect_success 'move nested submodule HEAD' '
770 echo "nested content" >sm2/nested/file &&
771 git -C sm2/nested add file &&
772 git -C sm2/nested commit --allow-empty -m "new HEAD" &&
773 head11=$(git -C sm2/nested rev-parse --short --verify HEAD)
776 test_expect_success 'diff --submodule=diff with moved nested submodule HEAD' '
777 cat >expected <<-EOF &&
778 Submodule nested $head9..$head11:
779 diff --git a/nested/file b/nested/file
781 index 0000000..ca281f5
787 git -C sm2 diff --submodule=diff >actual 2>err &&
788 test_must_be_empty err &&
789 diff_cmp expected actual
792 test_expect_success 'diff --submodule=diff recurses into nested submodules' '
793 cat >expected <<-EOF &&
794 Submodule sm2 contains modified content
795 Submodule sm2 $head9..$head10:
796 diff --git a/sm2/.gitmodules b/sm2/.gitmodules
798 index 0000000..3a816b8
800 +++ b/sm2/.gitmodules
802 +[submodule "nested"]
805 Submodule nested 0000000...$head11 (new submodule)
806 diff --git a/sm2/nested/file b/sm2/nested/file
808 index 0000000..ca281f5
810 +++ b/sm2/nested/file
813 diff --git a/sm2/nested/foo8 b/sm2/nested/foo8
815 index 0000000..db9916b
817 +++ b/sm2/nested/foo8
820 diff --git a/sm2/nested/foo9 b/sm2/nested/foo9
822 index 0000000..9c3b4f6
824 +++ b/sm2/nested/foo9
828 git diff --submodule=diff >actual 2>err &&
829 test_must_be_empty err &&
830 diff_cmp expected actual