Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2005 Junio C Hamano |
| 4 | # |
| 5 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 6 | test_description='git ls-files --others --exclude |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 7 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 8 | This test runs git ls-files --others and tests --exclude patterns. |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 9 | ' |
| 10 | |
Ævar Arnfjörð Bjarmason | 272f0a5 | 2021-10-07 12:01:36 +0200 | [diff] [blame] | 11 | TEST_PASSES_SANITIZE_LEAK=true |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 12 | . ./test-lib.sh |
| 13 | |
| 14 | rm -fr one three |
| 15 | for dir in . one one/two three |
| 16 | do |
| 17 | mkdir -p $dir && |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 18 | for i in 1 2 3 4 5 6 7 8 |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 19 | do |
| 20 | >$dir/a.$i |
| 21 | done |
| 22 | done |
Finn Arne Gangstad | dd482ee | 2009-02-10 15:20:17 +0100 | [diff] [blame] | 23 | >"#ignore1" |
| 24 | >"#ignore2" |
| 25 | >"#hidden" |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 26 | |
| 27 | cat >expect <<EOF |
| 28 | a.2 |
| 29 | a.4 |
| 30 | a.5 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 31 | a.8 |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 32 | one/a.3 |
| 33 | one/a.4 |
| 34 | one/a.5 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 35 | one/a.7 |
| 36 | one/two/a.2 |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 37 | one/two/a.3 |
| 38 | one/two/a.5 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 39 | one/two/a.7 |
| 40 | one/two/a.8 |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 41 | three/a.2 |
| 42 | three/a.3 |
| 43 | three/a.4 |
| 44 | three/a.5 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 45 | three/a.8 |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 46 | EOF |
| 47 | |
| 48 | echo '.gitignore |
Finn Arne Gangstad | dd482ee | 2009-02-10 15:20:17 +0100 | [diff] [blame] | 49 | \#ignore1 |
| 50 | \#ignore2* |
| 51 | \#hid*n |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 52 | output |
| 53 | expect |
| 54 | .gitignore |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 55 | *.7 |
| 56 | !*.8' >.git/ignore |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 57 | |
| 58 | echo '*.1 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 59 | /*.3 |
| 60 | !*.6' >.gitignore |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 61 | echo '*.2 |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 62 | two/*.4 |
| 63 | !*.7 |
| 64 | *.8' >one/.gitignore |
| 65 | echo '!*.2 |
| 66 | !*.8' >one/two/.gitignore |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 67 | |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 68 | allignores='.gitignore one/.gitignore one/two/.gitignore' |
| 69 | |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 70 | test_expect_success 'git ls-files --others with various exclude options.' ' |
| 71 | git ls-files --others \ |
Junio C Hamano | 1df092d | 2005-07-28 23:53:29 -0700 | [diff] [blame] | 72 | --exclude=\*.6 \ |
Junio C Hamano | f87f949 | 2005-07-24 15:26:09 -0700 | [diff] [blame] | 73 | --exclude-per-directory=.gitignore \ |
| 74 | --exclude-from=.git/ignore \ |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 75 | >output && |
| 76 | test_cmp expect output |
| 77 | ' |
Pavel Roskin | da7bc9b | 2005-08-10 22:15:02 -0400 | [diff] [blame] | 78 | |
Alex Riesen | d317e43 | 2005-11-02 14:05:45 +0100 | [diff] [blame] | 79 | # Test \r\n (MSDOS-like systems) |
Alex Riesen | 0dbc4e8 | 2006-02-12 19:05:34 +0100 | [diff] [blame] | 80 | printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore |
Alex Riesen | d317e43 | 2005-11-02 14:05:45 +0100 | [diff] [blame] | 81 | |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 82 | test_expect_success 'git ls-files --others with \r\n line endings.' ' |
| 83 | git ls-files --others \ |
Alex Riesen | d317e43 | 2005-11-02 14:05:45 +0100 | [diff] [blame] | 84 | --exclude=\*.6 \ |
| 85 | --exclude-per-directory=.gitignore \ |
| 86 | --exclude-from=.git/ignore \ |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 87 | >output && |
| 88 | test_cmp expect output |
| 89 | ' |
Alex Riesen | d317e43 | 2005-11-02 14:05:45 +0100 | [diff] [blame] | 90 | |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 91 | test_expect_success 'setup skip-worktree gitignore' ' |
| 92 | git add $allignores && |
| 93 | git update-index --skip-worktree $allignores && |
| 94 | rm $allignores |
| 95 | ' |
| 96 | |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 97 | test_expect_success 'git ls-files --others with various exclude options.' ' |
| 98 | git ls-files --others \ |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 99 | --exclude=\*.6 \ |
| 100 | --exclude-per-directory=.gitignore \ |
| 101 | --exclude-from=.git/ignore \ |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 102 | >output && |
| 103 | test_cmp expect output |
| 104 | ' |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 105 | |
Ævar Arnfjörð Bjarmason | 96ecf69 | 2022-07-28 01:13:40 +0200 | [diff] [blame] | 106 | test_expect_success 'restore gitignore' ' |
Nguyễn Thái Ngọc Duy | 08d595d | 2013-04-13 09:12:08 +1000 | [diff] [blame] | 107 | git checkout --ignore-skip-worktree-bits $allignores && |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 108 | rm .git/index |
| 109 | ' |
| 110 | |
Finn Arne Gangstad | dd482ee | 2009-02-10 15:20:17 +0100 | [diff] [blame] | 111 | cat > excludes-file <<\EOF |
Johannes Schindelin | 0ba956d | 2007-05-22 01:12:17 +0100 | [diff] [blame] | 112 | *.[1-8] |
| 113 | e* |
Finn Arne Gangstad | dd482ee | 2009-02-10 15:20:17 +0100 | [diff] [blame] | 114 | \#* |
Johannes Schindelin | 0ba956d | 2007-05-22 01:12:17 +0100 | [diff] [blame] | 115 | EOF |
| 116 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 117 | git config core.excludesFile excludes-file |
Johannes Schindelin | 0ba956d | 2007-05-22 01:12:17 +0100 | [diff] [blame] | 118 | |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 119 | git -c status.displayCommentPrefix=true status | grep "^# " > output |
Johannes Schindelin | 0ba956d | 2007-05-22 01:12:17 +0100 | [diff] [blame] | 120 | |
| 121 | cat > expect << EOF |
| 122 | # .gitignore |
| 123 | # a.6 |
| 124 | # one/ |
| 125 | # output |
| 126 | # three/ |
| 127 | EOF |
| 128 | |
Ævar Arnfjörð Bjarmason | 96ecf69 | 2022-07-28 01:13:40 +0200 | [diff] [blame] | 129 | test_expect_success 'git status honors core.excludesfile' \ |
Jeff King | 82ebb0b | 2008-03-12 17:36:36 -0400 | [diff] [blame] | 130 | 'test_cmp expect output' |
Johannes Schindelin | 0ba956d | 2007-05-22 01:12:17 +0100 | [diff] [blame] | 131 | |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 132 | test_expect_success 'trailing slash in exclude allows directory match(1)' ' |
| 133 | |
| 134 | git ls-files --others --exclude=one/ >output && |
| 135 | if grep "^one/" output |
| 136 | then |
| 137 | echo Ooops |
| 138 | false |
| 139 | else |
| 140 | : happy |
| 141 | fi |
| 142 | |
| 143 | ' |
| 144 | |
| 145 | test_expect_success 'trailing slash in exclude allows directory match (2)' ' |
| 146 | |
| 147 | git ls-files --others --exclude=one/two/ >output && |
| 148 | if grep "^one/two/" output |
| 149 | then |
| 150 | echo Ooops |
| 151 | false |
| 152 | else |
| 153 | : happy |
| 154 | fi |
| 155 | |
| 156 | ' |
| 157 | |
| 158 | test_expect_success 'trailing slash in exclude forces directory match (1)' ' |
| 159 | |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 160 | >two && |
Junio C Hamano | d6b8fc3 | 2008-01-31 01:17:48 -0800 | [diff] [blame] | 161 | git ls-files --others --exclude=two/ >output && |
| 162 | grep "^two" output |
| 163 | |
| 164 | ' |
| 165 | |
| 166 | test_expect_success 'trailing slash in exclude forces directory match (2)' ' |
| 167 | |
| 168 | git ls-files --others --exclude=one/a.1/ >output && |
| 169 | grep "^one/a.1" output |
| 170 | |
| 171 | ' |
| 172 | |
Michael J Gruber | 32738ed | 2008-12-18 18:11:18 +0100 | [diff] [blame] | 173 | test_expect_success 'negated exclude matches can override previous ones' ' |
| 174 | |
| 175 | git ls-files --others --exclude="a.*" --exclude="!a.1" >output && |
| 176 | grep "^a.1" output |
| 177 | ' |
| 178 | |
Junio C Hamano | 5cee349 | 2016-03-18 11:06:15 -0700 | [diff] [blame] | 179 | test_expect_success 'excluded directory overrides content patterns' ' |
Karsten Blees | c3c327d | 2013-05-29 22:32:36 +0200 | [diff] [blame] | 180 | |
| 181 | git ls-files --others --exclude="one" --exclude="!one/a.1" >output && |
Junio C Hamano | 5cee349 | 2016-03-18 11:06:15 -0700 | [diff] [blame] | 182 | if grep "^one/a.1" output |
| 183 | then |
| 184 | false |
| 185 | fi |
Karsten Blees | c3c327d | 2013-05-29 22:32:36 +0200 | [diff] [blame] | 186 | ' |
| 187 | |
| 188 | test_expect_success 'negated directory doesn'\''t affect content patterns' ' |
| 189 | |
| 190 | git ls-files --others --exclude="!one" --exclude="one/a.1" >output && |
| 191 | if grep "^one/a.1" output |
| 192 | then |
| 193 | false |
| 194 | fi |
| 195 | ' |
| 196 | |
Junio C Hamano | 472e746 | 2010-01-08 23:07:17 -0800 | [diff] [blame] | 197 | test_expect_success 'subdirectory ignore (setup)' ' |
| 198 | mkdir -p top/l1/l2 && |
| 199 | ( |
| 200 | cd top && |
| 201 | git init && |
| 202 | echo /.gitignore >.gitignore && |
| 203 | echo l1 >>.gitignore && |
| 204 | echo l2 >l1/.gitignore && |
| 205 | >l1/l2/l1 |
| 206 | ) |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'subdirectory ignore (toplevel)' ' |
| 210 | ( |
| 211 | cd top && |
| 212 | git ls-files -o --exclude-standard |
| 213 | ) >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 214 | test_must_be_empty actual |
Junio C Hamano | 472e746 | 2010-01-08 23:07:17 -0800 | [diff] [blame] | 215 | ' |
| 216 | |
| 217 | test_expect_success 'subdirectory ignore (l1/l2)' ' |
| 218 | ( |
| 219 | cd top/l1/l2 && |
| 220 | git ls-files -o --exclude-standard |
| 221 | ) >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 222 | test_must_be_empty actual |
Junio C Hamano | 472e746 | 2010-01-08 23:07:17 -0800 | [diff] [blame] | 223 | ' |
| 224 | |
Junio C Hamano | 48ffef9 | 2010-01-08 23:05:41 -0800 | [diff] [blame] | 225 | test_expect_success 'subdirectory ignore (l1)' ' |
Junio C Hamano | 472e746 | 2010-01-08 23:07:17 -0800 | [diff] [blame] | 226 | ( |
| 227 | cd top/l1 && |
| 228 | git ls-files -o --exclude-standard |
| 229 | ) >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 230 | test_must_be_empty actual |
Junio C Hamano | 472e746 | 2010-01-08 23:07:17 -0800 | [diff] [blame] | 231 | ' |
| 232 | |
Karsten Blees | 184d2a8 | 2013-04-15 21:08:02 +0200 | [diff] [blame] | 233 | test_expect_success 'show/hide empty ignored directory (setup)' ' |
| 234 | rm top/l1/l2/l1 && |
| 235 | rm top/l1/.gitignore |
| 236 | ' |
| 237 | |
| 238 | test_expect_success 'show empty ignored directory with --directory' ' |
| 239 | ( |
| 240 | cd top && |
| 241 | git ls-files -o -i --exclude l1 --directory |
| 242 | ) >actual && |
| 243 | echo l1/ >expect && |
| 244 | test_cmp expect actual |
| 245 | ' |
| 246 | |
| 247 | test_expect_success 'hide empty ignored directory with --no-empty-directory' ' |
| 248 | ( |
| 249 | cd top && |
| 250 | git ls-files -o -i --exclude l1 --directory --no-empty-directory |
| 251 | ) >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 252 | test_must_be_empty actual |
Karsten Blees | 184d2a8 | 2013-04-15 21:08:02 +0200 | [diff] [blame] | 253 | ' |
| 254 | |
Karsten Blees | 5bd8e2d | 2013-04-15 21:10:05 +0200 | [diff] [blame] | 255 | test_expect_success 'show/hide empty ignored sub-directory (setup)' ' |
| 256 | > top/l1/tracked && |
| 257 | ( |
| 258 | cd top && |
| 259 | git add -f l1/tracked |
| 260 | ) |
| 261 | ' |
| 262 | |
| 263 | test_expect_success 'show empty ignored sub-directory with --directory' ' |
| 264 | ( |
| 265 | cd top && |
| 266 | git ls-files -o -i --exclude l1 --directory |
| 267 | ) >actual && |
| 268 | echo l1/l2/ >expect && |
| 269 | test_cmp expect actual |
| 270 | ' |
| 271 | |
| 272 | test_expect_success 'hide empty ignored sub-directory with --no-empty-directory' ' |
| 273 | ( |
| 274 | cd top && |
| 275 | git ls-files -o -i --exclude l1 --directory --no-empty-directory |
| 276 | ) >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 277 | test_must_be_empty actual |
Karsten Blees | 5bd8e2d | 2013-04-15 21:10:05 +0200 | [diff] [blame] | 278 | ' |
| 279 | |
Nguyễn Thái Ngọc Duy | a3ea4d7 | 2012-10-15 13:24:36 +0700 | [diff] [blame] | 280 | test_expect_success 'pattern matches prefix completely' ' |
Nguyễn Thái Ngọc Duy | a3ea4d7 | 2012-10-15 13:24:36 +0700 | [diff] [blame] | 281 | git ls-files -i -o --exclude "/three/a.3[abc]" >actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 282 | test_must_be_empty actual |
Nguyễn Thái Ngọc Duy | a3ea4d7 | 2012-10-15 13:24:36 +0700 | [diff] [blame] | 283 | ' |
| 284 | |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 285 | test_expect_success 'ls-files with "**" patterns' ' |
Li Linchao | 18337d4 | 2022-07-03 15:49:09 +0000 | [diff] [blame] | 286 | cat <<-\EOF >expect && |
| 287 | a.1 |
| 288 | one/a.1 |
| 289 | one/two/a.1 |
| 290 | three/a.1 |
| 291 | EOF |
Jeff King | 60687de | 2015-03-20 06:06:44 -0400 | [diff] [blame] | 292 | git ls-files -o -i --exclude "**/a.1" >actual && |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 293 | test_cmp expect actual |
| 294 | ' |
| 295 | |
Elijah Newren | dd55fc0 | 2021-05-12 17:28:20 +0000 | [diff] [blame] | 296 | test_expect_success 'ls-files with "**" patterns and --directory' ' |
Elijah Newren | a97c7a8 | 2021-05-12 17:28:18 +0000 | [diff] [blame] | 297 | # Expectation same as previous test |
| 298 | git ls-files --directory -o -i --exclude "**/a.1" >actual && |
| 299 | test_cmp expect actual |
| 300 | ' |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 301 | |
| 302 | test_expect_success 'ls-files with "**" patterns and no slashes' ' |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 303 | git ls-files -o -i --exclude "one**a.1" >actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 304 | test_must_be_empty actual |
Nguyễn Thái Ngọc Duy | 237ec6e | 2012-10-15 13:26:02 +0700 | [diff] [blame] | 305 | ' |
| 306 | |
Pavel Roskin | da7bc9b | 2005-08-10 22:15:02 -0400 | [diff] [blame] | 307 | test_done |