1#!/bin/sh
   2test_description='Test Git when git repository is located at root
   4This test requires write access in root. Do not bother if you do not
   6have a throwaway chroot or VM.
   7Script t1509/prepare-chroot.sh may help you setup chroot, then you
   9can chroot in and execute this test from there.
  10'
  11. ./test-lib.sh
  13test_cmp_val() {
  15        echo "$1" > expected
  16        echo "$2" > result
  17        test_cmp expected result
  18}
  19test_vars() {
  21        test_expect_success "$1: gitdir" '
  22                test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
  23        '
  24        test_expect_success "$1: worktree" '
  26                test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
  27        '
  28        test_expect_success "$1: prefix" '
  30                test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
  31        '
  32}
  33test_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        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}
  56test_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        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}
  78test_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        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}
 100if ! test -w /
 102then
 103        skip_all="Test requiring writable / skipped. Read this test if you want to run it"
 104        test_done
 105fi
 106if  test -e /refs || test -e /objects || test -e /info || test -e /hooks ||
 108    test -e /.git || test -e /foo || test -e /me
 109then
 110        skip_all="Skip test that clobbers existing files in /"
 111        test_done
 112fi
 113if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
 115        skip_all="You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
 116        test_done
 117fi
 118if ! test_have_prereq NOT_ROOT
 120then
 121        skip_all="No you can't run this as root"
 122        test_done
 123fi
 124ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
 126test_expect_success 'setup' '
 128        rm -rf /foo &&
 129        mkdir /foo &&
 130        mkdir /foo/bar &&
 131        echo 1 > /foo/foome &&
 132        echo 1 > /foo/bar/barme &&
 133        echo 1 > /me
 134'
 135say "GIT_DIR absolute, GIT_WORK_TREE set"
 137test_expect_success 'go to /' 'cd /'
 139cat >ls.expected <<EOF
 141100644 $ONE_SHA1 0      foo/bar/barme
 142100644 $ONE_SHA1 0      foo/foome
 143100644 $ONE_SHA1 0      me
 144EOF
 145GIT_DIR="$TRASH_DIRECTORY/.git" && export GIT_DIR
 147GIT_WORK_TREE=/ && export GIT_WORK_TREE
 148test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
 150test_foobar_root
 151test_expect_success 'go to /foo' 'cd /foo'
 153test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
 155test_foobar_foo
 156test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 158test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
 160test_foobar_foobar
 161say "GIT_DIR relative, GIT_WORK_TREE set"
 163test_expect_success 'go to /' 'cd /'
 165GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
 167GIT_WORK_TREE=/ && export GIT_WORK_TREE
 168test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
 170test_foobar_root
 171test_expect_success 'go to /foo' 'cd /foo'
 173GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
 175GIT_WORK_TREE=/ && export GIT_WORK_TREE
 176test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
 178test_foobar_foo
 179test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 181GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
 183GIT_WORK_TREE=/ && export GIT_WORK_TREE
 184test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
 186test_foobar_foobar
 187say "GIT_DIR relative, GIT_WORK_TREE relative"
 189test_expect_success 'go to /' 'cd /'
 191GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
 193GIT_WORK_TREE=. && export GIT_WORK_TREE
 194test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
 196test_foobar_root
 197test_expect_success 'go to /' 'cd /foo'
 199GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
 201GIT_WORK_TREE=.. && export GIT_WORK_TREE
 202test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
 204test_foobar_foo
 205test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 207GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
 209GIT_WORK_TREE=../.. && export GIT_WORK_TREE
 210test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
 212test_foobar_foobar
 213say ".git at root"
 215unset GIT_DIR
 217unset GIT_WORK_TREE
 218test_expect_success 'go to /' 'cd /'
 220test_expect_success 'setup' '
 221        rm -rf /.git &&
 222        echo "Initialized empty Git repository in /.git/" > expected &&
 223        git init > result &&
 224        test_cmp expected result
 225'
 226test_vars 'auto gitdir, root' ".git" "/" ""
 228test_foobar_root
 229test_expect_success 'go to /foo' 'cd /foo'
 231test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
 232test_foobar_foo
 233test_expect_success 'go to /foo/bar' 'cd /foo/bar'
 235test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
 236test_foobar_foobar
 237test_expect_success 'cleanup' 'rm -rf /.git'
 239say "auto bare gitdir"
 241# DESTROYYYYY!!!!!
 243test_expect_success 'setup' '
 244        rm -rf /refs /objects /info /hooks &&
 245        rm -f /expected /ls.expected /me /result &&
 246        cd / &&
 247        echo "Initialized empty Git repository in /" > expected &&
 248        git init --bare > result &&
 249        test_cmp expected result
 250'
 251test_vars 'auto gitdir, root' "." "" ""
 253test_expect_success 'go to /foo' 'cd /foo'
 255test_vars 'auto gitdir, root' "/" "" ""
 257test_done