t / t3700-add.shon commit Merge branch 'sl/autoconf' (ebf91ab)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Carl D. Worth
   4#
   5
   6test_description='Test of git add, including the -- option.'
   7
   8. ./test-lib.sh
   9
  10test_expect_success \
  11    'Test of git add' \
  12    'touch foo && git add foo'
  13
  14test_expect_success \
  15    'Post-check that foo is in the index' \
  16    'git ls-files foo | grep foo'
  17
  18test_expect_success \
  19    'Test that "git add -- -q" works' \
  20    'touch -- -q && git add -- -q'
  21
  22test_expect_success \
  23        'git add: Test that executable bit is not used if core.filemode=0' \
  24        'git config core.filemode 0 &&
  25         echo foo >xfoo1 &&
  26         chmod 755 xfoo1 &&
  27         git add xfoo1 &&
  28         case "`git ls-files --stage xfoo1`" in
  29         100644" "*xfoo1) echo pass;;
  30         *) echo fail; git ls-files --stage xfoo1; (exit 1);;
  31         esac'
  32
  33test_expect_success SYMLINKS 'git add: filemode=0 should not get confused by symlink' '
  34        rm -f xfoo1 &&
  35        ln -s foo xfoo1 &&
  36        git add xfoo1 &&
  37        case "`git ls-files --stage xfoo1`" in
  38        120000" "*xfoo1) echo pass;;
  39        *) echo fail; git ls-files --stage xfoo1; (exit 1);;
  40        esac
  41'
  42
  43test_expect_success \
  44        'git update-index --add: Test that executable bit is not used...' \
  45        'git config core.filemode 0 &&
  46         echo foo >xfoo2 &&
  47         chmod 755 xfoo2 &&
  48         git update-index --add xfoo2 &&
  49         case "`git ls-files --stage xfoo2`" in
  50         100644" "*xfoo2) echo pass;;
  51         *) echo fail; git ls-files --stage xfoo2; (exit 1);;
  52         esac'
  53
  54test_expect_success SYMLINKS 'git add: filemode=0 should not get confused by symlink' '
  55        rm -f xfoo2 &&
  56        ln -s foo xfoo2 &&
  57        git update-index --add xfoo2 &&
  58        case "`git ls-files --stage xfoo2`" in
  59        120000" "*xfoo2) echo pass;;
  60        *) echo fail; git ls-files --stage xfoo2; (exit 1);;
  61        esac
  62'
  63
  64test_expect_success SYMLINKS \
  65        'git update-index --add: Test that executable bit is not used...' \
  66        'git config core.filemode 0 &&
  67         ln -s xfoo2 xfoo3 &&
  68         git update-index --add xfoo3 &&
  69         case "`git ls-files --stage xfoo3`" in
  70         120000" "*xfoo3) echo pass;;
  71         *) echo fail; git ls-files --stage xfoo3; (exit 1);;
  72         esac'
  73
  74test_expect_success '.gitignore test setup' '
  75        echo "*.ig" >.gitignore &&
  76        mkdir c.if d.ig &&
  77        >a.ig && >b.if &&
  78        >c.if/c.if && >c.if/c.ig &&
  79        >d.ig/d.if && >d.ig/d.ig
  80'
  81
  82test_expect_success '.gitignore is honored' '
  83        git add . &&
  84        ! (git ls-files | grep "\\.ig")
  85'
  86
  87test_expect_success 'error out when attempting to add ignored ones without -f' '
  88        test_must_fail git add a.?? &&
  89        ! (git ls-files | grep "\\.ig")
  90'
  91
  92test_expect_success 'error out when attempting to add ignored ones without -f' '
  93        test_must_fail git add d.?? &&
  94        ! (git ls-files | grep "\\.ig")
  95'
  96
  97test_expect_success 'add ignored ones with -f' '
  98        git add -f a.?? &&
  99        git ls-files --error-unmatch a.ig
 100'
 101
 102test_expect_success 'add ignored ones with -f' '
 103        git add -f d.??/* &&
 104        git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
 105'
 106
 107test_expect_success 'add ignored ones with -f' '
 108        rm -f .git/index &&
 109        git add -f d.?? &&
 110        git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
 111'
 112
 113test_expect_success '.gitignore with subdirectory' '
 114
 115        rm -f .git/index &&
 116        mkdir -p sub/dir &&
 117        echo "!dir/a.*" >sub/.gitignore &&
 118        >sub/a.ig &&
 119        >sub/dir/a.ig &&
 120        git add sub/dir &&
 121        git ls-files --error-unmatch sub/dir/a.ig &&
 122        rm -f .git/index &&
 123        (
 124                cd sub/dir &&
 125                git add .
 126        ) &&
 127        git ls-files --error-unmatch sub/dir/a.ig
 128'
 129
 130mkdir 1 1/2 1/3
 131touch 1/2/a 1/3/b 1/2/c
 132test_expect_success 'check correct prefix detection' '
 133        rm -f .git/index &&
 134        git add 1/2/a 1/3/b 1/2/c
 135'
 136
 137test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
 138        for s in 1 2 3
 139        do
 140                echo $s > stage$s
 141                echo "100755 $(git hash-object -w stage$s) $s   file"
 142                echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s       symlink"
 143        done | git update-index --index-info &&
 144        git config core.filemode 0 &&
 145        git config core.symlinks 0 &&
 146        echo new > file &&
 147        echo new > symlink &&
 148        git add file symlink &&
 149        git ls-files --stage | grep "^100755 .* 0       file$" &&
 150        git ls-files --stage | grep "^120000 .* 0       symlink$"
 151'
 152
 153test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
 154        git rm --cached -f file symlink &&
 155        (
 156                echo "100644 $(git hash-object -w stage1) 1     file"
 157                echo "100755 $(git hash-object -w stage2) 2     file"
 158                echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink"
 159                echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
 160        ) | git update-index --index-info &&
 161        git config core.filemode 0 &&
 162        git config core.symlinks 0 &&
 163        echo new > file &&
 164        echo new > symlink &&
 165        git add file symlink &&
 166        git ls-files --stage | grep "^100755 .* 0       file$" &&
 167        git ls-files --stage | grep "^120000 .* 0       symlink$"
 168'
 169
 170test_expect_success 'git add --refresh' '
 171        >foo && git add foo && git commit -a -m "commit all" &&
 172        test -z "`git diff-index HEAD -- foo`" &&
 173        git read-tree HEAD &&
 174        case "`git diff-index HEAD -- foo`" in
 175        :100644" "*"M   foo") echo pass;;
 176        *) echo fail; (exit 1);;
 177        esac &&
 178        git add --refresh -- foo &&
 179        test -z "`git diff-index HEAD -- foo`"
 180'
 181
 182test_expect_success 'git add --refresh with pathspec' '
 183        git reset --hard &&
 184        echo >foo && echo >bar && echo >baz &&
 185        git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
 186        echo "100644 $H 3       foo" | git update-index --index-info &&
 187        test-chmtime -60 bar baz &&
 188        >expect &&
 189        git add --refresh bar >actual &&
 190        test_cmp expect actual &&
 191
 192        git diff-files --name-only >actual &&
 193        ! grep bar actual&&
 194        grep baz actual
 195'
 196
 197test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
 198        git reset --hard &&
 199        date >foo1 &&
 200        date >foo2 &&
 201        chmod 0 foo2 &&
 202        test_must_fail git add --verbose . &&
 203        ! ( git ls-files foo1 | grep foo1 )
 204'
 205
 206rm -f foo2
 207
 208test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
 209        git reset --hard &&
 210        date >foo1 &&
 211        date >foo2 &&
 212        chmod 0 foo2 &&
 213        test_must_fail git add --verbose --ignore-errors . &&
 214        git ls-files foo1 | grep foo1
 215'
 216
 217rm -f foo2
 218
 219test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
 220        git config add.ignore-errors 1 &&
 221        git reset --hard &&
 222        date >foo1 &&
 223        date >foo2 &&
 224        chmod 0 foo2 &&
 225        test_must_fail git add --verbose . &&
 226        git ls-files foo1 | grep foo1
 227'
 228rm -f foo2
 229
 230test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
 231        git config add.ignore-errors 0 &&
 232        git reset --hard &&
 233        date >foo1 &&
 234        date >foo2 &&
 235        chmod 0 foo2 &&
 236        test_must_fail git add --verbose . &&
 237        ! ( git ls-files foo1 | grep foo1 )
 238'
 239rm -f foo2
 240
 241test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
 242       git config add.ignore-errors 1 &&
 243       git reset --hard &&
 244       date >foo1 &&
 245       date >foo2 &&
 246       chmod 0 foo2 &&
 247       test_must_fail git add --verbose --no-ignore-errors . &&
 248       ! ( git ls-files foo1 | grep foo1 ) &&
 249       git config add.ignore-errors 0
 250'
 251rm -f foo2
 252
 253test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
 254        git reset --hard &&
 255        touch fo\[ou\]bar foobar &&
 256        git add '\''fo\[ou\]bar'\'' &&
 257        git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
 258        ! ( git ls-files foobar | grep foobar )
 259'
 260
 261test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
 262        git reset --hard &&
 263        H=$(git rev-parse :1/2/a) &&
 264        (
 265                echo "100644 $H 1       track-this"
 266                echo "100644 $H 3       track-this"
 267        ) | git update-index --index-info &&
 268        echo track-this >>.gitignore &&
 269        echo resolved >track-this &&
 270        git add track-this
 271'
 272
 273test_expect_success '"add non-existent" should fail' '
 274        test_must_fail git add non-existent &&
 275        ! (git ls-files | grep "non-existent")
 276'
 277
 278test_expect_success 'git add --dry-run of existing changed file' "
 279        echo new >>track-this &&
 280        git add --dry-run track-this >actual 2>&1 &&
 281        echo \"add 'track-this'\" | test_cmp - actual
 282"
 283
 284test_expect_success 'git add --dry-run of non-existing file' "
 285        echo ignored-file >>.gitignore &&
 286        test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
 287"
 288
 289test_expect_success 'git add --dry-run of an existing file output' "
 290        echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
 291        test_i18ncmp expect actual
 292"
 293
 294cat >expect.err <<\EOF
 295The following paths are ignored by one of your .gitignore files:
 296ignored-file
 297Use -f if you really want to add them.
 298fatal: no files added
 299EOF
 300cat >expect.out <<\EOF
 301add 'track-this'
 302EOF
 303
 304test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
 305        test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
 306'
 307
 308test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
 309        test_i18ncmp expect.out actual.out &&
 310        test_i18ncmp expect.err actual.err
 311'
 312
 313test_done