t / t3001-ls-files-others-exclude.shon commit Merge branch 'jc/check-x-z' (0db320d)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6test_description='git ls-files --others --exclude
   7
   8This test runs git ls-files --others and tests --exclude patterns.
   9'
  10
  11. ./test-lib.sh
  12
  13rm -fr one three
  14for dir in . one one/two three
  15do
  16  mkdir -p $dir &&
  17  for i in 1 2 3 4 5 6 7 8
  18  do
  19    >$dir/a.$i
  20  done
  21done
  22>"#ignore1"
  23>"#ignore2"
  24>"#hidden"
  25
  26cat >expect <<EOF
  27a.2
  28a.4
  29a.5
  30a.8
  31one/a.3
  32one/a.4
  33one/a.5
  34one/a.7
  35one/two/a.2
  36one/two/a.3
  37one/two/a.5
  38one/two/a.7
  39one/two/a.8
  40three/a.2
  41three/a.3
  42three/a.4
  43three/a.5
  44three/a.8
  45EOF
  46
  47echo '.gitignore
  48\#ignore1
  49\#ignore2*
  50\#hid*n
  51output
  52expect
  53.gitignore
  54*.7
  55!*.8' >.git/ignore
  56
  57echo '*.1
  58/*.3
  59!*.6' >.gitignore
  60echo '*.2
  61two/*.4
  62!*.7
  63*.8' >one/.gitignore
  64echo '!*.2
  65!*.8' >one/two/.gitignore
  66
  67allignores='.gitignore one/.gitignore one/two/.gitignore'
  68
  69test_expect_success \
  70    'git ls-files --others with various exclude options.' \
  71    'git ls-files --others \
  72       --exclude=\*.6 \
  73       --exclude-per-directory=.gitignore \
  74       --exclude-from=.git/ignore \
  75       >output &&
  76     test_cmp expect output'
  77
  78# Test \r\n (MSDOS-like systems)
  79printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
  80
  81test_expect_success \
  82    'git ls-files --others with \r\n line endings.' \
  83    'git ls-files --others \
  84       --exclude=\*.6 \
  85       --exclude-per-directory=.gitignore \
  86       --exclude-from=.git/ignore \
  87       >output &&
  88     test_cmp expect output'
  89
  90test_expect_success 'setup skip-worktree gitignore' '
  91        git add $allignores &&
  92        git update-index --skip-worktree $allignores &&
  93        rm $allignores
  94'
  95
  96test_expect_success \
  97    'git ls-files --others with various exclude options.' \
  98    'git ls-files --others \
  99       --exclude=\*.6 \
 100       --exclude-per-directory=.gitignore \
 101       --exclude-from=.git/ignore \
 102       >output &&
 103     test_cmp expect output'
 104
 105test_expect_success 'restore gitignore' '
 106        git checkout --ignore-skip-worktree-bits $allignores &&
 107        rm .git/index
 108'
 109
 110cat > excludes-file <<\EOF
 111*.[1-8]
 112e*
 113\#*
 114EOF
 115
 116git config core.excludesFile excludes-file
 117
 118git status | grep "^#   " > output
 119
 120cat > expect << EOF
 121#       .gitignore
 122#       a.6
 123#       one/
 124#       output
 125#       three/
 126EOF
 127
 128test_expect_success 'git status honors core.excludesfile' \
 129        'test_cmp expect output'
 130
 131test_expect_success 'trailing slash in exclude allows directory match(1)' '
 132
 133        git ls-files --others --exclude=one/ >output &&
 134        if grep "^one/" output
 135        then
 136                echo Ooops
 137                false
 138        else
 139                : happy
 140        fi
 141
 142'
 143
 144test_expect_success 'trailing slash in exclude allows directory match (2)' '
 145
 146        git ls-files --others --exclude=one/two/ >output &&
 147        if grep "^one/two/" output
 148        then
 149                echo Ooops
 150                false
 151        else
 152                : happy
 153        fi
 154
 155'
 156
 157test_expect_success 'trailing slash in exclude forces directory match (1)' '
 158
 159        >two &&
 160        git ls-files --others --exclude=two/ >output &&
 161        grep "^two" output
 162
 163'
 164
 165test_expect_success 'trailing slash in exclude forces directory match (2)' '
 166
 167        git ls-files --others --exclude=one/a.1/ >output &&
 168        grep "^one/a.1" output
 169
 170'
 171
 172test_expect_success 'negated exclude matches can override previous ones' '
 173
 174        git ls-files --others --exclude="a.*" --exclude="!a.1" >output &&
 175        grep "^a.1" output
 176'
 177
 178test_expect_success 'excluded directory overrides content patterns' '
 179
 180        git ls-files --others --exclude="one" --exclude="!one/a.1" >output &&
 181        if grep "^one/a.1" output
 182        then
 183                false
 184        fi
 185'
 186
 187test_expect_success 'negated directory doesn'\''t affect content patterns' '
 188
 189        git ls-files --others --exclude="!one" --exclude="one/a.1" >output &&
 190        if grep "^one/a.1" output
 191        then
 192                false
 193        fi
 194'
 195
 196test_expect_success 'subdirectory ignore (setup)' '
 197        mkdir -p top/l1/l2 &&
 198        (
 199                cd top &&
 200                git init &&
 201                echo /.gitignore >.gitignore &&
 202                echo l1 >>.gitignore &&
 203                echo l2 >l1/.gitignore &&
 204                >l1/l2/l1
 205        )
 206'
 207
 208test_expect_success 'subdirectory ignore (toplevel)' '
 209        (
 210                cd top &&
 211                git ls-files -o --exclude-standard
 212        ) >actual &&
 213        >expect &&
 214        test_cmp expect actual
 215'
 216
 217test_expect_success 'subdirectory ignore (l1/l2)' '
 218        (
 219                cd top/l1/l2 &&
 220                git ls-files -o --exclude-standard
 221        ) >actual &&
 222        >expect &&
 223        test_cmp expect actual
 224'
 225
 226test_expect_success 'subdirectory ignore (l1)' '
 227        (
 228                cd top/l1 &&
 229                git ls-files -o --exclude-standard
 230        ) >actual &&
 231        >expect &&
 232        test_cmp expect actual
 233'
 234
 235test_expect_success 'show/hide empty ignored directory (setup)' '
 236        rm top/l1/l2/l1 &&
 237        rm top/l1/.gitignore
 238'
 239
 240test_expect_success 'show empty ignored directory with --directory' '
 241        (
 242                cd top &&
 243                git ls-files -o -i --exclude l1 --directory
 244        ) >actual &&
 245        echo l1/ >expect &&
 246        test_cmp expect actual
 247'
 248
 249test_expect_success 'hide empty ignored directory with --no-empty-directory' '
 250        (
 251                cd top &&
 252                git ls-files -o -i --exclude l1 --directory --no-empty-directory
 253        ) >actual &&
 254        >expect &&
 255        test_cmp expect actual
 256'
 257
 258test_expect_success 'show/hide empty ignored sub-directory (setup)' '
 259        > top/l1/tracked &&
 260        (
 261                cd top &&
 262                git add -f l1/tracked
 263        )
 264'
 265
 266test_expect_success 'show empty ignored sub-directory with --directory' '
 267        (
 268                cd top &&
 269                git ls-files -o -i --exclude l1 --directory
 270        ) >actual &&
 271        echo l1/l2/ >expect &&
 272        test_cmp expect actual
 273'
 274
 275test_expect_success 'hide empty ignored sub-directory with --no-empty-directory' '
 276        (
 277                cd top &&
 278                git ls-files -o -i --exclude l1 --directory --no-empty-directory
 279        ) >actual &&
 280        >expect &&
 281        test_cmp expect actual
 282'
 283
 284test_expect_success 'pattern matches prefix completely' '
 285        : >expect &&
 286        git ls-files -i -o --exclude "/three/a.3[abc]" >actual &&
 287        test_cmp expect actual
 288'
 289
 290test_expect_success 'ls-files with "**" patterns' '
 291        cat <<\EOF >expect &&
 292a.1
 293one/a.1
 294one/two/a.1
 295three/a.1
 296EOF
 297        git ls-files -o -i --exclude "**/a.1" >actual
 298        test_cmp expect actual
 299'
 300
 301
 302test_expect_success 'ls-files with "**" patterns and no slashes' '
 303        : >expect &&
 304        git ls-files -o -i --exclude "one**a.1" >actual &&
 305        test_cmp expect actual
 306'
 307
 308test_done