t / t1509-root-worktree.shon commit Merge branch 'cp/add-u-pathspec' (e25ccff)
   1#!/bin/sh
   2
   3test_description='Test Git when git repository is located at root
   4
   5This test requires write access in root. Do not bother if you do not
   6have a throwaway chroot or VM.
   7
   8Script t1509/prepare-chroot.sh may help you setup chroot, then you
   9can chroot in and execute this test from there.
  10'
  11
  12. ./test-lib.sh
  13
  14test_cmp_val() {
  15        echo "$1" > expected
  16        echo "$2" > result
  17        test_cmp expected result
  18}
  19
  20test_vars() {
  21        test_expect_success "$1: gitdir" '
  22                test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
  23        '
  24
  25        test_expect_success "$1: worktree" '
  26                test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
  27        '
  28
  29        test_expect_success "$1: prefix" '
  30                test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
  31        '
  32}
  33
  34test_foobar_root() {
  35        test_expect_success 'add relative' '
  36                test -z "$(cd / && git ls-files)" &&
  37                git add foo/foome &&
  38                git add foo/bar/barme &&
  39                git add me &&
  40                ( cd / && git ls-files --stage ) > result &&
  41                test_cmp /ls.expected result &&
  42                rm "$(git rev-parse --git-dir)/index"
  43        '
  44
  45        test_expect_success 'add absolute' '
  46                test -z "$(cd / && git ls-files)" &&
  47                git add /foo/foome &&
  48                git add /foo/bar/barme &&
  49                git add /me &&
  50                ( cd / && git ls-files --stage ) > result &&
  51                test_cmp /ls.expected result &&
  52                rm "$(git rev-parse --git-dir)/index"
  53        '
  54
  55}
  56
  57test_foobar_foo() {
  58        test_expect_success 'add relative' '
  59                test -z "$(cd / && git ls-files)" &&
  60                git add foome &&
  61                git add bar/barme &&
  62                git add ../me &&
  63                ( cd / && git ls-files --stage ) > result &&
  64                test_cmp /ls.expected result &&
  65                rm "$(git rev-parse --git-dir)/index"
  66        '
  67
  68        test_expect_success 'add absolute' '
  69                test -z "$(cd / && git ls-files)" &&
  70                git add /foo/foome &&
  71                git add /foo/bar/barme &&
  72                git add /me &&
  73                ( cd / && git ls-files --stage ) > result &&
  74                test_cmp /ls.expected result &&
  75                rm "$(git rev-parse --git-dir)/index"
  76        '
  77}
  78
  79test_foobar_foobar() {
  80        test_expect_success 'add relative' '
  81                test -z "$(cd / && git ls-files)" &&
  82                git add ../foome &&
  83                git add barme &&
  84                git add ../../me &&
  85                ( cd / && git ls-files --stage ) > result &&
  86                test_cmp /ls.expected result &&
  87                rm "$(git rev-parse --git-dir)/index"
  88        '
  89
  90        test_expect_success 'add absolute' '
  91                test -z "$(cd / && git ls-files)" &&
  92                git add /foo/foome &&
  93                git add /foo/bar/barme &&
  94                git add /me &&
  95                ( cd / && git ls-files --stage ) > result &&
  96                test_cmp /ls.expected result &&
  97                rm "$(git rev-parse --git-dir)/index"
  98        '
  99}
 100
 101if ! test_have_prereq POSIXPERM || ! [ -w / ]; then
 102        say "Dangerous test skipped. Read this test if you want to execute it"
 103        test_done
 104fi
 105
 106if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
 107        say "You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
 108        test_done
 109fi
 110
 111if [ "$UID" = 0 ]; then
 112        say "No you can't run this with root"
 113        test_done
 114fi
 115
 116ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
 117
 118test_expect_success 'setup' '
 119        rm -rf /foo
 120        mkdir /foo &&
 121        mkdir /foo/bar &&
 122        echo 1 > /foo/foome &&
 123        echo 1 > /foo/bar/barme &&
 124        echo 1 > /me
 125'
 126
 127say "GIT_DIR absolute, GIT_WORK_TREE set"
 128
 129test_expect_success 'go to /' 'cd /'
 130
 131cat >ls.expected <<EOF
 132100644 $ONE_SHA1 0      foo/bar/barme
 133100644 $ONE_SHA1 0      foo/foome
 134100644 $ONE_SHA1 0      me
 135EOF
 136
 137export GIT_DIR="$TRASH_DIRECTORY/.git"
 138export GIT_WORK_TREE=/
 139
 140test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
 141test_foobar_root
 142
 143test_expect_success 'go to /foo' 'cd /foo'
 144
 145test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
 146test_foobar_foo
 147
 148test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 149
 150test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
 151test_foobar_foobar
 152
 153say "GIT_DIR relative, GIT_WORK_TREE set"
 154
 155test_expect_success 'go to /' 'cd /'
 156
 157export GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git"
 158export GIT_WORK_TREE=/
 159
 160test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
 161test_foobar_root
 162
 163test_expect_success 'go to /foo' 'cd /foo'
 164
 165export GIT_DIR="../$TRASH_DIRECTORY/.git"
 166export GIT_WORK_TREE=/
 167
 168test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
 169test_foobar_foo
 170
 171test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 172
 173export GIT_DIR="../../$TRASH_DIRECTORY/.git"
 174export GIT_WORK_TREE=/
 175
 176test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
 177test_foobar_foobar
 178
 179say "GIT_DIR relative, GIT_WORK_TREE relative"
 180
 181test_expect_success 'go to /' 'cd /'
 182
 183export GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git"
 184export GIT_WORK_TREE=.
 185
 186test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
 187test_foobar_root
 188
 189test_expect_success 'go to /' 'cd /foo'
 190
 191export GIT_DIR="../$TRASH_DIRECTORY/.git"
 192export GIT_WORK_TREE=..
 193
 194test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
 195test_foobar_foo
 196
 197test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 198
 199export GIT_DIR="../../$TRASH_DIRECTORY/.git"
 200export GIT_WORK_TREE=../..
 201
 202test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
 203test_foobar_foobar
 204
 205say ".git at root"
 206
 207unset GIT_DIR
 208unset GIT_WORK_TREE
 209
 210test_expect_success 'go to /' 'cd /'
 211test_expect_success 'setup' '
 212        rm -rf /.git
 213        echo "Initialized empty Git repository in /.git/" > expected &&
 214        git init > result &&
 215        test_cmp expected result
 216'
 217
 218test_vars 'auto gitdir, root' ".git" "/" ""
 219test_foobar_root
 220
 221test_expect_success 'go to /foo' 'cd /foo'
 222test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
 223test_foobar_foo
 224
 225test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 226test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
 227test_foobar_foobar
 228
 229test_expect_success 'cleanup' 'rm -rf /.git'
 230
 231say "auto bare gitdir"
 232
 233# DESTROYYYYY!!!!!
 234test_expect_success 'setup' '
 235        rm -rf /refs /objects /info /hooks
 236        rm /*
 237        cd / &&
 238        echo "Initialized empty Git repository in /" > expected &&
 239        git init --bare > result &&
 240        test_cmp expected result
 241'
 242
 243test_vars 'auto gitdir, root' "." "" ""
 244
 245test_expect_success 'go to /foo' 'cd /foo'
 246
 247test_vars 'auto gitdir, root' "/" "" ""
 248
 249test_done