Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Test Git when git repository is located at root |
| 4 | |
| 5 | This test requires write access in root. Do not bother if you do not |
| 6 | have a throwaway chroot or VM. |
| 7 | |
| 8 | Script t1509/prepare-chroot.sh may help you setup chroot, then you |
| 9 | can chroot in and execute this test from there. |
| 10 | ' |
| 11 | |
| 12 | . ./test-lib.sh |
| 13 | |
| 14 | test_cmp_val() { |
| 15 | echo "$1" > expected |
| 16 | echo "$2" > result |
| 17 | test_cmp expected result |
| 18 | } |
| 19 | |
| 20 | test_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 | |
| 34 | test_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 | |
| 57 | test_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 | |
| 79 | test_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 | |
Junio C Hamano | eae6953 | 2015-01-16 10:32:09 -0800 | [diff] [blame] | 101 | if ! test -w / |
| 102 | then |
| 103 | skip_all="Test requiring writable / skipped. Read this test if you want to run it" |
| 104 | test_done |
| 105 | fi |
| 106 | |
| 107 | if test -e /refs || test -e /objects || test -e /info || test -e /hooks || |
| 108 | test -e /.git || test -e /foo || test -e /me |
| 109 | then |
| 110 | skip_all="Skip test that clobbers existing files in /" |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 111 | test_done |
| 112 | fi |
| 113 | |
| 114 | if [ "$IKNOWWHATIAMDOING" != "YES" ]; then |
Ævar Arnfjörð Bjarmason | fadb515 | 2010-06-24 17:44:48 +0000 | [diff] [blame] | 115 | skip_all="You must set env var IKNOWWHATIAMDOING=YES in order to run this test" |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 116 | test_done |
| 117 | fi |
| 118 | |
Junio C Hamano | eae6953 | 2015-01-16 10:32:09 -0800 | [diff] [blame] | 119 | if ! test_have_prereq NOT_ROOT |
| 120 | then |
| 121 | skip_all="No you can't run this as root" |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 122 | test_done |
| 123 | fi |
| 124 | |
| 125 | ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d |
| 126 | |
| 127 | test_expect_success 'setup' ' |
Patrick Steinhardt | 24ca45f | 2015-08-05 11:43:50 +0200 | [diff] [blame] | 128 | rm -rf /foo && |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 129 | mkdir /foo && |
| 130 | mkdir /foo/bar && |
| 131 | echo 1 > /foo/foome && |
| 132 | echo 1 > /foo/bar/barme && |
| 133 | echo 1 > /me |
| 134 | ' |
| 135 | |
| 136 | say "GIT_DIR absolute, GIT_WORK_TREE set" |
| 137 | |
| 138 | test_expect_success 'go to /' 'cd /' |
| 139 | |
| 140 | cat >ls.expected <<EOF |
| 141 | 100644 $ONE_SHA1 0 foo/bar/barme |
| 142 | 100644 $ONE_SHA1 0 foo/foome |
| 143 | 100644 $ONE_SHA1 0 me |
| 144 | EOF |
| 145 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 146 | GIT_DIR="$TRASH_DIRECTORY/.git" && export GIT_DIR |
| 147 | GIT_WORK_TREE=/ && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 148 | |
| 149 | test_vars 'abs gitdir, root' "$GIT_DIR" "/" "" |
| 150 | test_foobar_root |
| 151 | |
| 152 | test_expect_success 'go to /foo' 'cd /foo' |
| 153 | |
| 154 | test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/" |
| 155 | test_foobar_foo |
| 156 | |
| 157 | test_expect_success 'go to /foo/bar' 'cd /foo/bar' |
| 158 | |
| 159 | test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/" |
| 160 | test_foobar_foobar |
| 161 | |
| 162 | say "GIT_DIR relative, GIT_WORK_TREE set" |
| 163 | |
| 164 | test_expect_success 'go to /' 'cd /' |
| 165 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 166 | GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR |
| 167 | GIT_WORK_TREE=/ && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 168 | |
| 169 | test_vars 'rel gitdir, root' "$GIT_DIR" "/" "" |
| 170 | test_foobar_root |
| 171 | |
| 172 | test_expect_success 'go to /foo' 'cd /foo' |
| 173 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 174 | GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR |
| 175 | GIT_WORK_TREE=/ && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 176 | |
| 177 | test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/" |
| 178 | test_foobar_foo |
| 179 | |
| 180 | test_expect_success 'go to /foo/bar' 'cd /foo/bar' |
| 181 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 182 | GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR |
| 183 | GIT_WORK_TREE=/ && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 184 | |
| 185 | test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/" |
| 186 | test_foobar_foobar |
| 187 | |
| 188 | say "GIT_DIR relative, GIT_WORK_TREE relative" |
| 189 | |
| 190 | test_expect_success 'go to /' 'cd /' |
| 191 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 192 | GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR |
| 193 | GIT_WORK_TREE=. && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 194 | |
| 195 | test_vars 'rel gitdir, root' "$GIT_DIR" "/" "" |
| 196 | test_foobar_root |
| 197 | |
| 198 | test_expect_success 'go to /' 'cd /foo' |
| 199 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 200 | GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR |
| 201 | GIT_WORK_TREE=.. && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 202 | |
| 203 | test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/" |
| 204 | test_foobar_foo |
| 205 | |
| 206 | test_expect_success 'go to /foo/bar' 'cd /foo/bar' |
| 207 | |
Junio C Hamano | 69ae92b | 2010-10-13 11:36:36 -0700 | [diff] [blame] | 208 | GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR |
| 209 | GIT_WORK_TREE=../.. && export GIT_WORK_TREE |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 210 | |
| 211 | test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/" |
| 212 | test_foobar_foobar |
| 213 | |
| 214 | say ".git at root" |
| 215 | |
| 216 | unset GIT_DIR |
| 217 | unset GIT_WORK_TREE |
| 218 | |
| 219 | test_expect_success 'go to /' 'cd /' |
| 220 | test_expect_success 'setup' ' |
Patrick Steinhardt | 24ca45f | 2015-08-05 11:43:50 +0200 | [diff] [blame] | 221 | rm -rf /.git && |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 222 | echo "Initialized empty Git repository in /.git/" > expected && |
| 223 | git init > result && |
Eric Sunshine | 7790b8c | 2022-11-21 03:00:19 +0000 | [diff] [blame] | 224 | test_cmp expected result && |
| 225 | git config --global --add safe.directory / |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 226 | ' |
| 227 | |
| 228 | test_vars 'auto gitdir, root' ".git" "/" "" |
| 229 | test_foobar_root |
| 230 | |
| 231 | test_expect_success 'go to /foo' 'cd /foo' |
| 232 | test_vars 'auto gitdir, foo' "/.git" "/" "foo/" |
| 233 | test_foobar_foo |
| 234 | |
| 235 | test_expect_success 'go to /foo/bar' 'cd /foo/bar' |
| 236 | test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/" |
| 237 | test_foobar_foobar |
| 238 | |
| 239 | test_expect_success 'cleanup' 'rm -rf /.git' |
| 240 | |
| 241 | say "auto bare gitdir" |
| 242 | |
| 243 | # DESTROYYYYY!!!!! |
| 244 | test_expect_success 'setup' ' |
Patrick Steinhardt | 24ca45f | 2015-08-05 11:43:50 +0200 | [diff] [blame] | 245 | rm -rf /refs /objects /info /hooks && |
Eric Sunshine | ce153b8 | 2022-11-21 03:00:20 +0000 | [diff] [blame] | 246 | rm -f /HEAD /expected /ls.expected /me /result && |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 247 | cd / && |
| 248 | echo "Initialized empty Git repository in /" > expected && |
| 249 | git init --bare > result && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 250 | test_cmp expected result |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 251 | ' |
| 252 | |
| 253 | test_vars 'auto gitdir, root' "." "" "" |
| 254 | |
| 255 | test_expect_success 'go to /foo' 'cd /foo' |
| 256 | |
| 257 | test_vars 'auto gitdir, root' "/" "" "" |
| 258 | |
Eric Sunshine | 35c194d | 2022-11-21 03:00:21 +0000 | [diff] [blame] | 259 | test_expect_success 'cleanup root' ' |
| 260 | rm -rf /.git /refs /objects /info /hooks /branches /foo && |
| 261 | rm -f /HEAD /config /description /expected /ls.expected /me /result |
| 262 | ' |
| 263 | |
Nguyễn Thái Ngọc Duy | 3719b2f | 2010-02-14 22:44:45 +0700 | [diff] [blame] | 264 | test_done |