Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t0008-ignores.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t0008-ignores.sh` 967 lines ยท 24806 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description=check-ignore 4 5 TEST_CREATE_REPO_NO_TEMPLATE=1 6 . ./test-lib.sh 7 8 init_vars () { 9 global_excludes="global-excludes" 10 } 11 12 enable_global_excludes () { 13 init_vars && 14 git config core.excludesfile "$global_excludes" 15 } 16 17 expect_in () { 18 dest="$HOME/expected-$1" text="$2" 19 if test -z "$text" 20 then 21 >"$dest" # avoid newline 22 else 23 echo "$text" >"$dest" 24 fi 25 } 26 27 expect () { 28 expect_in stdout "$1" 29 } 30 31 expect_from_stdin () { 32 cat >"$HOME/expected-stdout" 33 } 34 35 test_stderr () { 36 expected="$1" 37 expect_in stderr "$1" && 38 test_cmp "$HOME/expected-stderr" "$HOME/stderr" 39 } 40 41 broken_c_unquote () { 42 sed -e 's/^"//' -e 's/\\//' -e 's/"$//' "$1" | tr '\n' '\0' 43 } 44 45 broken_c_unquote_verbose () { 46 sed -e 's/ "/ /' -e 's/\\//' -e 's/"$//' "$1" | tr ':\t\n' '\000' 47 } 48 49 stderr_contains () { 50 regexp="$1" 51 if test_grep "$regexp" "$HOME/stderr" 52 then 53 return 0 54 else 55 echo "didn't find /$regexp/ in $HOME/stderr" 56 cat "$HOME/stderr" 57 return 1 58 fi 59 } 60 61 stderr_empty_on_success () { 62 expect_code="$1" 63 if test $expect_code = 0 64 then 65 test_stderr "" 66 else 67 # If we expect failure then stderr might or might not be empty 68 # due to --quiet - the caller can check its contents 69 return 0 70 fi 71 } 72 73 test_check_ignore () { 74 args="$1" expect_code="${2:-0}" global_args="$3" 75 76 init_vars && 77 rm -f "$HOME/stdout" "$HOME/stderr" "$HOME/cmd" && 78 echo git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \ 79 >"$HOME/cmd" && 80 echo "$expect_code" >"$HOME/expected-exit-code" && 81 test_expect_code "$expect_code" \ 82 git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \ 83 >"$HOME/stdout" 2>"$HOME/stderr" && 84 test_cmp "$HOME/expected-stdout" "$HOME/stdout" && 85 stderr_empty_on_success "$expect_code" 86 } 87 88 # Runs the same code with 4 different levels of output verbosity: 89 # 90 # 1. with -q / --quiet 91 # 2. with default verbosity 92 # 3. with -v / --verbose 93 # 4. with -v / --verbose, *and* -n / --non-matching 94 # 95 # expecting success each time. Takes advantage of the fact that 96 # check-ignore --verbose output is the same as normal output except 97 # for the extra first column. 98 # 99 # A parameter is used to determine if the tests are run with the 100 # normal case (using the index), or with the --no-index option. 101 # 102 # Arguments: 103 # - (optional) prereqs for this test, e.g. 'SYMLINKS' 104 # - test name 105 # - output to expect from the fourth verbosity mode (the output 106 # from the other verbosity modes is automatically inferred 107 # from this value) 108 # - code to run (should invoke test_check_ignore) 109 # - index option: --index or --no-index 110 test_expect_success_multiple () { 111 prereq= 112 if test $# -eq 5 113 then 114 prereq=$1 115 shift 116 fi 117 if test "$4" = "--index" 118 then 119 no_index_opt= 120 else 121 no_index_opt=$4 122 fi 123 testname="$1" expect_all="$2" code="$3" 124 125 expect_verbose=$(echo "$expect_all" | grep -v '^:: ' || :) 126 expect=$(echo "$expect_verbose" | sed -e 's/.* //') 127 128 test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" ' 129 expect "$expect" && 130 eval "$code" 131 ' 132 133 # --quiet is only valid when a single pattern is passed 134 if test $( echo "$expect_all" | wc -l ) = 1 135 then 136 for quiet_opt in '-q' '--quiet' 137 do 138 opts="${no_index_opt:+$no_index_opt }$quiet_opt" 139 test_expect_success $prereq "$testname${opts:+ with $opts}" " 140 expect '' && 141 $code 142 " 143 done 144 quiet_opt= 145 fi 146 147 for verbose_opt in '-v' '--verbose' 148 do 149 for non_matching_opt in '' '-n' '--non-matching' 150 do 151 if test -n "$non_matching_opt" 152 then 153 my_expect="$expect_all" 154 else 155 my_expect="$expect_verbose" 156 fi 157 158 test_code=" 159 expect '$my_expect' && 160 $code 161 " 162 opts="${no_index_opt:+$no_index_opt }$verbose_opt${non_matching_opt:+ $non_matching_opt}" 163 test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code" 164 done 165 done 166 verbose_opt= 167 non_matching_opt= 168 no_index_opt= 169 } 170 171 test_expect_success_multi () { 172 test_expect_success_multiple "$@" "--index" 173 } 174 175 test_expect_success_no_index_multi () { 176 test_expect_success_multiple "$@" "--no-index" 177 } 178 179 test_expect_success 'setup' ' 180 init_vars && 181 mkdir -p a/b/ignored-dir a/submodule b && 182 if test_have_prereq SYMLINKS 183 then 184 ln -s b a/symlink 185 fi && 186 ( 187 cd a/submodule && 188 git init && 189 echo a >a && 190 git add a && 191 git commit -m"commit in submodule" 192 ) && 193 git add a/submodule && 194 cat <<-\EOF >.gitignore && 195 one 196 ignored-* 197 top-level-dir/ 198 EOF 199 for dir in . a 200 do 201 : >$dir/not-ignored && 202 : >$dir/ignored-and-untracked && 203 : >$dir/ignored-but-in-index || return 1 204 done && 205 git add -f ignored-but-in-index a/ignored-but-in-index && 206 cat <<-\EOF >a/.gitignore && 207 two* 208 *three 209 EOF 210 cat <<-\EOF >a/b/.gitignore && 211 four 212 five 213 # this comment should affect the line numbers 214 six 215 ignored-dir/ 216 # and so should this blank line: 217 218 !on* 219 !two 220 EOF 221 echo "seven" >a/b/ignored-dir/.gitignore && 222 test -n "$HOME" && 223 cat <<-\EOF >"$global_excludes" && 224 globalone 225 !globaltwo 226 globalthree 227 EOF 228 mkdir .git/info && 229 cat <<-\EOF >.git/info/exclude 230 per-repo 231 EOF 232 ' 233 234 ############################################################################ 235 # 236 # test invalid inputs 237 238 test_expect_success_multi '. corner-case' ':: .' ' 239 test_check_ignore . 1 240 ' 241 242 test_expect_success_multi 'empty command line' '' ' 243 test_check_ignore "" 128 && 244 stderr_contains "fatal: no path specified" 245 ' 246 247 test_expect_success_multi '--stdin with empty STDIN' '' ' 248 test_check_ignore "--stdin" 1 stdin 584 one 585 not-ignored 586 a/one 587 a/not-ignored 588 a/b/on 589 a/b/one 590 a/b/one one 591 "a/b/one two" 592 "a/b/one\"three" 593 a/b/not-ignored 594 a/b/two 595 a/b/twooo 596 globaltwo 597 a/globaltwo 598 a/b/globaltwo 599 b/globaltwo 600 EOF 601 cat <<-\EOF >expected-default 602 one 603 a/one 604 a/b/twooo 605 EOF 606 cat <<-EOF >expected-verbose 607 .gitignore:1:one one 608 .gitignore:1:one a/one 609 a/b/.gitignore:8:!on* a/b/on 610 a/b/.gitignore:8:!on* a/b/one 611 a/b/.gitignore:8:!on* a/b/one one 612 a/b/.gitignore:8:!on* a/b/one two 613 a/b/.gitignore:8:!on* "a/b/one\\"three" 614 a/b/.gitignore:9:!two a/b/two 615 a/.gitignore:1:two* a/b/twooo 616 $global_excludes:2:!globaltwo globaltwo 617 $global_excludes:2:!globaltwo a/globaltwo 618 $global_excludes:2:!globaltwo a/b/globaltwo 619 $global_excludes:2:!globaltwo b/globaltwo 620 EOF 621 622 broken_c_unquote stdin >stdin0 623 624 broken_c_unquote expected-default >expected-default0 625 626 broken_c_unquote_verbose expected-verbose >expected-verbose0 627 628 test_expect_success '--stdin' ' 629 expect_from_stdin stdin 662 ../one 663 ../not-ignored 664 one 665 not-ignored 666 b/on 667 b/one 668 b/one one 669 "b/one two" 670 "b/one\"three" 671 b/two 672 b/not-ignored 673 b/twooo 674 ../globaltwo 675 globaltwo 676 b/globaltwo 677 ../b/globaltwo 678 c/not-ignored 679 EOF 680 # N.B. we deliberately end STDIN with a non-matching pattern in order 681 # to test that the exit code indicates that one or more of the 682 # provided paths is ignored - in other words, that it represents an 683 # aggregation of all the results, not just the final result. 684 685 cat <<-EOF >expected-all 686 .gitignore:1:one ../one 687 :: ../not-ignored 688 .gitignore:1:one one 689 :: not-ignored 690 a/b/.gitignore:8:!on* b/on 691 a/b/.gitignore:8:!on* b/one 692 a/b/.gitignore:8:!on* b/one one 693 a/b/.gitignore:8:!on* b/one two 694 a/b/.gitignore:8:!on* "b/one\\"three" 695 a/b/.gitignore:9:!two b/two 696 :: b/not-ignored 697 a/.gitignore:1:two* b/twooo 698 $global_excludes:2:!globaltwo ../globaltwo 699 $global_excludes:2:!globaltwo globaltwo 700 $global_excludes:2:!globaltwo b/globaltwo 701 $global_excludes:2:!globaltwo ../b/globaltwo 702 :: c/not-ignored 703 EOF 704 cat <<-EOF >expected-default 705 ../one 706 one 707 b/twooo 708 EOF 709 grep -v '^:: ' expected-all >expected-verbose 710 711 broken_c_unquote stdin >stdin0 712 713 broken_c_unquote expected-default >expected-default0 714 715 broken_c_unquote_verbose expected-verbose >expected-verbose0 716 717 test_expect_success '--stdin from subdirectory' ' 718 expect_from_stdin out &) && 763 764 # We cannot just "echo >in" because check-ignore would get EOF 765 # after echo exited; instead we open the descriptor in our 766 # shell, and then echo to the fd. We make sure to close it at 767 # the end, so that the subprocess does get EOF and dies 768 # properly. 769 # 770 # Similarly, we must keep "out" open so that check-ignore does 771 # not ever get SIGPIPE trying to write to us. Not only would that 772 # produce incorrect results, but then there would be no writer on the 773 # other end of the pipe, and we would potentially block forever trying 774 # to open it. 775 exec 9>in && 776 exec 8&-" && 778 test_when_finished "exec 8<&-" && 779 echo >&9 one && 780 read response <&8 && 781 echo "$response" | grep "^\.gitignore:1:one one" && 782 echo >&9 two && 783 read response <&8 && 784 echo "$response" | grep "^:: two" 785 ' 786 787 test_expect_success 'existing file and directory' ' 788 test_when_finished "rm one" && 789 test_when_finished "rmdir top-level-dir" && 790 >one && 791 mkdir top-level-dir && 792 git check-ignore one top-level-dir >actual && 793 test_grep one actual && 794 test_grep top-level-dir actual 795 ' 796 797 test_expect_success 'existing directory and file' ' 798 test_when_finished "rm one" && 799 test_when_finished "rmdir top-level-dir" && 800 >one && 801 mkdir top-level-dir && 802 git check-ignore top-level-dir one >actual && 803 test_grep one actual && 804 test_grep top-level-dir actual 805 ' 806 807 test_expect_success 'exact prefix matching (with root)' ' 808 test_when_finished rm -r a && 809 mkdir -p a/git a/git-foo && 810 touch a/git/foo a/git-foo/bar && 811 echo /git/ >a/.gitignore && 812 git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual && 813 cat >expect <<-\EOF && 814 a/git 815 a/git/foo 816 EOF 817 test_cmp expect actual 818 ' 819 820 test_expect_success 'exact prefix matching (without root)' ' 821 test_when_finished rm -r a && 822 mkdir -p a/git a/git-foo && 823 touch a/git/foo a/git-foo/bar && 824 echo git/ >a/.gitignore && 825 git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual && 826 cat >expect <<-\EOF && 827 a/git 828 a/git/foo 829 EOF 830 test_cmp expect actual 831 ' 832 833 test_expect_success 'directories and ** matches' ' 834 cat >.gitignore <<-\EOF && 835 data/** 836 !data/**/ 837 !data/**/*.txt 838 EOF 839 git check-ignore file \ 840 data/file data/data1/file1 data/data1/file1.txt \ 841 data/data2/file2 data/data2/file2.txt >actual && 842 cat >expect <<-\EOF && 843 data/file 844 data/data1/file1 845 data/data2/file2 846 EOF 847 test_cmp expect actual 848 ' 849 850 test_expect_success '** not confused by matching leading prefix' ' 851 cat >.gitignore <<-\EOF && 852 foo**/bar 853 EOF 854 git check-ignore foobar foo/bar >actual && 855 cat >expect <<-\EOF && 856 foo/bar 857 EOF 858 test_cmp expect actual 859 ' 860 861 ############################################################################ 862 # 863 # test whitespace handling 864 865 test_expect_success 'trailing whitespace is ignored' ' 866 mkdir whitespace && 867 >whitespace/trailing && 868 >whitespace/untracked && 869 echo "whitespace/trailing " >ignore && 870 cat >expect <actual 2>err && 874 test_cmp expect actual && 875 test_must_be_empty err 876 ' 877 878 test_expect_success !MINGW 'quoting allows trailing whitespace' ' 879 rm -rf whitespace && 880 mkdir whitespace && 881 >"whitespace/trailing " && 882 >whitespace/untracked && 883 echo "whitespace/trailing\\ \\ " >ignore && 884 echo whitespace/untracked >expect && 885 git ls-files -o -X ignore whitespace >actual 2>err && 886 test_cmp expect actual && 887 test_must_be_empty err 888 ' 889 890 test_expect_success !MINGW,!CYGWIN 'correct handling of backslashes' ' 891 rm -rf whitespace && 892 mkdir whitespace && 893 >"whitespace/trailing 1 " && 894 >"whitespace/trailing 2 \\\\" && 895 >"whitespace/trailing 3 \\\\" && 896 >"whitespace/trailing 4 \\ " && 897 >"whitespace/trailing 5 \\ \\ " && 898 >"whitespace/trailing 6 \\a\\" && 899 >whitespace/untracked && 900 sed -e "s/Z$//" >ignore <<-\EOF && 901 whitespace/trailing 1 \ Z 902 whitespace/trailing 2 \\\\Z 903 whitespace/trailing 3 \\\\ Z 904 whitespace/trailing 4 \\\ Z 905 whitespace/trailing 5 \\ \\\ Z 906 whitespace/trailing 6 \\a\\Z 907 EOF 908 echo whitespace/untracked >expect && 909 git ls-files -o -X ignore whitespace >actual 2>err && 910 test_cmp expect actual && 911 test_must_be_empty err 912 ' 913 914 test_expect_success 'info/exclude trumps core.excludesfile' ' 915 echo >>global-excludes usually-ignored && 916 echo >>.git/info/exclude "!usually-ignored" && 917 >usually-ignored && 918 echo "?? usually-ignored" >expect && 919 920 git status --porcelain usually-ignored >actual && 921 test_cmp expect actual 922 ' 923 924 test_expect_success SYMLINKS 'set up ignore file for symlink tests' ' 925 echo "*" >ignore && 926 rm -f .gitignore .git/info/exclude 927 ' 928 929 test_expect_success SYMLINKS 'symlinks respected in core.excludesFile' ' 930 test_when_finished "rm symlink" && 931 ln -s ignore symlink && 932 test_config core.excludesFile "$(pwd)/symlink" && 933 echo file >expect && 934 git check-ignore file >actual 2>err && 935 test_cmp expect actual && 936 test_must_be_empty err 937 ' 938 939 test_expect_success SYMLINKS 'symlinks respected in info/exclude' ' 940 test_when_finished "rm .git/info/exclude" && 941 ln -s ../../ignore .git/info/exclude && 942 echo file >expect && 943 git check-ignore file >actual 2>err && 944 test_cmp expect actual && 945 test_must_be_empty err 946 ' 947 948 test_expect_success SYMLINKS 'symlinks not respected in-tree' ' 949 test_when_finished "rm -rf subdir .gitignore err actual" && 950 ln -s ignore .gitignore && 951 mkdir subdir && 952 ln -s ignore subdir/.gitignore && 953 test_must_fail git check-ignore subdir/file >actual 2>err && 954 test_must_be_empty actual && 955 test_grep "unable to access.*gitignore" err 956 ' 957 958 test_expect_success EXPENSIVE 'large exclude file ignored in tree' ' 959 test_when_finished "rm .gitignore" && 960 find . -name .gitignore -exec rm "{}" ";" && 961 dd if=/dev/zero of=.gitignore bs=101M count=1 && 962 git ls-files -o --exclude-standard 2>err && 963 echo "warning: ignoring excessively large pattern file: .gitignore" >expect && 964 test_cmp expect err 965 ' 966 967 test_done