| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test separate work tree' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success 'setup' ' |
| 8 | EMPTY_TREE=$(git write-tree) && |
| 9 | EMPTY_BLOB=$(git hash-object -t blob --stdin </dev/null) && |
| 10 | CHANGED_BLOB=$(echo changed | git hash-object -t blob --stdin) && |
| 11 | EMPTY_BLOB7=$(echo $EMPTY_BLOB | sed "s/\(.......\).*/\1/") && |
| 12 | CHANGED_BLOB7=$(echo $CHANGED_BLOB | sed "s/\(.......\).*/\1/") && |
| 13 | |
| 14 | mkdir -p work/sub/dir && |
| 15 | mkdir -p work2 && |
| 16 | mv .git repo.git |
| 17 | ' |
| 18 | |
| 19 | test_expect_success 'setup: helper for testing rev-parse' ' |
| 20 | test_rev_parse() { |
| 21 | echo $1 >expected.bare && |
| 22 | echo $2 >expected.inside-git && |
| 23 | echo $3 >expected.inside-worktree && |
| 24 | if test $# -ge 4 |
| 25 | then |
| 26 | echo $4 >expected.prefix |
| 27 | fi && |
| 28 | |
| 29 | git rev-parse --is-bare-repository >actual.bare && |
| 30 | git rev-parse --is-inside-git-dir >actual.inside-git && |
| 31 | git rev-parse --is-inside-work-tree >actual.inside-worktree && |
| 32 | if test $# -ge 4 |
| 33 | then |
| 34 | git rev-parse --show-prefix >actual.prefix |
| 35 | fi && |
| 36 | |
| 37 | test_cmp expected.bare actual.bare && |
| 38 | test_cmp expected.inside-git actual.inside-git && |
| 39 | test_cmp expected.inside-worktree actual.inside-worktree && |
| 40 | if test $# -ge 4 |
| 41 | then |
| 42 | # rev-parse --show-prefix should output |
| 43 | # a single newline when at the top of the work tree, |
| 44 | # but we test for that separately. |
| 45 | test -z "$4" && test_must_be_empty actual.prefix || |
| 46 | test_cmp expected.prefix actual.prefix |
| 47 | fi |
| 48 | } |
| 49 | ' |
| 50 | |
| 51 | test_expect_success 'setup: core.worktree = relative path' ' |
| 52 | sane_unset GIT_WORK_TREE && |
| 53 | GIT_DIR=repo.git && |
| 54 | GIT_CONFIG="$(pwd)"/$GIT_DIR/config && |
| 55 | export GIT_DIR GIT_CONFIG && |
| 56 | git config core.worktree ../work |
| 57 | ' |
| 58 | |
| 59 | test_expect_success 'outside' ' |
| 60 | test_rev_parse false false false |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'inside work tree' ' |
| 64 | ( |
| 65 | cd work && |
| 66 | GIT_DIR=../repo.git && |
| 67 | GIT_CONFIG="$(pwd)"/$GIT_DIR/config && |
| 68 | test_rev_parse false false true "" |
| 69 | ) |
| 70 | ' |
| 71 | |
| 72 | test_expect_success 'empty prefix is actually written out' ' |
| 73 | echo >expected && |
| 74 | ( |
| 75 | cd work && |
| 76 | GIT_DIR=../repo.git && |
| 77 | GIT_CONFIG="$(pwd)"/$GIT_DIR/config && |
| 78 | git rev-parse --show-prefix >../actual |
| 79 | ) && |
| 80 | test_cmp expected actual |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'subdir of work tree' ' |
| 84 | ( |
| 85 | cd work/sub/dir && |
| 86 | GIT_DIR=../../../repo.git && |
| 87 | GIT_CONFIG="$(pwd)"/$GIT_DIR/config && |
| 88 | test_rev_parse false false true sub/dir/ |
| 89 | ) |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'setup: core.worktree = absolute path' ' |
| 93 | sane_unset GIT_WORK_TREE && |
| 94 | GIT_DIR=$(pwd)/repo.git && |
| 95 | GIT_CONFIG=$GIT_DIR/config && |
| 96 | export GIT_DIR GIT_CONFIG && |
| 97 | git config core.worktree "$(pwd)/work" |
| 98 | ' |
| 99 | |
| 100 | test_expect_success 'outside' ' |
| 101 | test_rev_parse false false false && |
| 102 | ( |
| 103 | cd work2 && |
| 104 | test_rev_parse false false false |
| 105 | ) |
| 106 | ' |
| 107 | |
| 108 | test_expect_success 'inside work tree' ' |
| 109 | ( |
| 110 | cd work && |
| 111 | test_rev_parse false false true "" |
| 112 | ) |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'subdir of work tree' ' |
| 116 | ( |
| 117 | cd work/sub/dir && |
| 118 | test_rev_parse false false true sub/dir/ |
| 119 | ) |
| 120 | ' |
| 121 | |
| 122 | test_expect_success 'setup: GIT_WORK_TREE=relative (override core.worktree)' ' |
| 123 | GIT_DIR=$(pwd)/repo.git && |
| 124 | GIT_CONFIG=$GIT_DIR/config && |
| 125 | git config core.worktree non-existent && |
| 126 | GIT_WORK_TREE=work && |
| 127 | export GIT_DIR GIT_CONFIG GIT_WORK_TREE |
| 128 | ' |
| 129 | |
| 130 | test_expect_success 'outside' ' |
| 131 | test_rev_parse false false false && |
| 132 | ( |
| 133 | cd work2 && |
| 134 | test_rev_parse false false false |
| 135 | ) |
| 136 | ' |
| 137 | |
| 138 | test_expect_success 'inside work tree' ' |
| 139 | ( |
| 140 | cd work && |
| 141 | GIT_WORK_TREE=. && |
| 142 | test_rev_parse false false true "" |
| 143 | ) |
| 144 | ' |
| 145 | |
| 146 | test_expect_success 'subdir of work tree' ' |
| 147 | ( |
| 148 | cd work/sub/dir && |
| 149 | GIT_WORK_TREE=../.. && |
| 150 | test_rev_parse false false true sub/dir/ |
| 151 | ) |
| 152 | ' |
| 153 | |
| 154 | test_expect_success 'setup: GIT_WORK_TREE=absolute, below git dir' ' |
| 155 | mv work repo.git/work && |
| 156 | mv work2 repo.git/work2 && |
| 157 | GIT_DIR=$(pwd)/repo.git && |
| 158 | GIT_CONFIG=$GIT_DIR/config && |
| 159 | GIT_WORK_TREE=$(pwd)/repo.git/work && |
| 160 | export GIT_DIR GIT_CONFIG GIT_WORK_TREE |
| 161 | ' |
| 162 | |
| 163 | test_expect_success 'outside' ' |
| 164 | echo outside && |
| 165 | test_rev_parse false false false |
| 166 | ' |
| 167 | |
| 168 | test_expect_success 'in repo.git' ' |
| 169 | ( |
| 170 | cd repo.git && |
| 171 | test_rev_parse false true false |
| 172 | ) && |
| 173 | ( |
| 174 | cd repo.git/objects && |
| 175 | test_rev_parse false true false |
| 176 | ) && |
| 177 | ( |
| 178 | cd repo.git/work2 && |
| 179 | test_rev_parse false true false |
| 180 | ) |
| 181 | ' |
| 182 | |
| 183 | test_expect_success 'inside work tree' ' |
| 184 | ( |
| 185 | cd repo.git/work && |
| 186 | test_rev_parse false true true "" |
| 187 | ) |
| 188 | ' |
| 189 | |
| 190 | test_expect_success 'subdir of work tree' ' |
| 191 | ( |
| 192 | cd repo.git/work/sub/dir && |
| 193 | test_rev_parse false true true sub/dir/ |
| 194 | ) |
| 195 | ' |
| 196 | |
| 197 | test_expect_success 'find work tree from repo' ' |
| 198 | echo sub/dir/untracked >expected && |
| 199 | cat <<-\EOF >repo.git/work/.gitignore && |
| 200 | expected.* |
| 201 | actual.* |
| 202 | .gitignore |
| 203 | EOF |
| 204 | >repo.git/work/sub/dir/untracked && |
| 205 | ( |
| 206 | cd repo.git && |
| 207 | git ls-files --others --exclude-standard >../actual |
| 208 | ) && |
| 209 | test_cmp expected actual |
| 210 | ' |
| 211 | |
| 212 | test_expect_success 'find work tree from work tree' ' |
| 213 | echo sub/dir/tracked >expected && |
| 214 | >repo.git/work/sub/dir/tracked && |
| 215 | ( |
| 216 | cd repo.git/work/sub/dir && |
| 217 | git --git-dir=../../.. add tracked |
| 218 | ) && |
| 219 | ( |
| 220 | cd repo.git && |
| 221 | git ls-files >../actual |
| 222 | ) && |
| 223 | test_cmp expected actual |
| 224 | ' |
| 225 | |
| 226 | test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' ' |
| 227 | ( |
| 228 | cd repo.git/work/sub/dir && |
| 229 | GIT_DIR=../../.. && |
| 230 | GIT_WORK_TREE=../.. && |
| 231 | GIT_PAGER= && |
| 232 | export GIT_DIR GIT_WORK_TREE GIT_PAGER && |
| 233 | |
| 234 | git diff --exit-code tracked && |
| 235 | echo changed >tracked && |
| 236 | test_must_fail git diff --exit-code tracked |
| 237 | ) |
| 238 | ' |
| 239 | |
| 240 | test_expect_success 'diff-index respects work tree under .git dir' ' |
| 241 | cat >diff-index-cached.expected <<-EOF && |
| 242 | :000000 100644 $ZERO_OID $EMPTY_BLOB A sub/dir/tracked |
| 243 | EOF |
| 244 | cat >diff-index.expected <<-EOF && |
| 245 | :000000 100644 $ZERO_OID $ZERO_OID A sub/dir/tracked |
| 246 | EOF |
| 247 | |
| 248 | ( |
| 249 | GIT_DIR=repo.git && |
| 250 | GIT_WORK_TREE=repo.git/work && |
| 251 | export GIT_DIR GIT_WORK_TREE && |
| 252 | git diff-index $EMPTY_TREE >diff-index.actual && |
| 253 | git diff-index --cached $EMPTY_TREE >diff-index-cached.actual |
| 254 | ) && |
| 255 | test_cmp diff-index.expected diff-index.actual && |
| 256 | test_cmp diff-index-cached.expected diff-index-cached.actual |
| 257 | ' |
| 258 | |
| 259 | test_expect_success 'diff-files respects work tree under .git dir' ' |
| 260 | cat >diff-files.expected <<-EOF && |
| 261 | :100644 100644 $EMPTY_BLOB $ZERO_OID M sub/dir/tracked |
| 262 | EOF |
| 263 | |
| 264 | ( |
| 265 | GIT_DIR=repo.git && |
| 266 | GIT_WORK_TREE=repo.git/work && |
| 267 | export GIT_DIR GIT_WORK_TREE && |
| 268 | git diff-files >diff-files.actual |
| 269 | ) && |
| 270 | test_cmp diff-files.expected diff-files.actual |
| 271 | ' |
| 272 | |
| 273 | test_expect_success 'git diff respects work tree under .git dir' ' |
| 274 | cat >diff-TREE.expected <<-EOF && |
| 275 | diff --git a/sub/dir/tracked b/sub/dir/tracked |
| 276 | new file mode 100644 |
| 277 | index 0000000..$CHANGED_BLOB7 |
| 278 | --- /dev/null |
| 279 | +++ b/sub/dir/tracked |
| 280 | @@ -0,0 +1 @@ |
| 281 | +changed |
| 282 | EOF |
| 283 | cat >diff-TREE-cached.expected <<-EOF && |
| 284 | diff --git a/sub/dir/tracked b/sub/dir/tracked |
| 285 | new file mode 100644 |
| 286 | index 0000000..$EMPTY_BLOB7 |
| 287 | EOF |
| 288 | cat >diff-FILES.expected <<-EOF && |
| 289 | diff --git a/sub/dir/tracked b/sub/dir/tracked |
| 290 | index $EMPTY_BLOB7..$CHANGED_BLOB7 100644 |
| 291 | --- a/sub/dir/tracked |
| 292 | +++ b/sub/dir/tracked |
| 293 | @@ -0,0 +1 @@ |
| 294 | +changed |
| 295 | EOF |
| 296 | |
| 297 | ( |
| 298 | GIT_DIR=repo.git && |
| 299 | GIT_WORK_TREE=repo.git/work && |
| 300 | export GIT_DIR GIT_WORK_TREE && |
| 301 | git diff $EMPTY_TREE >diff-TREE.actual && |
| 302 | git diff --cached $EMPTY_TREE >diff-TREE-cached.actual && |
| 303 | git diff >diff-FILES.actual |
| 304 | ) && |
| 305 | test_cmp diff-TREE.expected diff-TREE.actual && |
| 306 | test_cmp diff-TREE-cached.expected diff-TREE-cached.actual && |
| 307 | test_cmp diff-FILES.expected diff-FILES.actual |
| 308 | ' |
| 309 | |
| 310 | test_expect_success 'git grep' ' |
| 311 | echo dir/tracked >expected.grep && |
| 312 | ( |
| 313 | cd repo.git/work/sub && |
| 314 | GIT_DIR=../.. && |
| 315 | GIT_WORK_TREE=.. && |
| 316 | export GIT_DIR GIT_WORK_TREE && |
| 317 | git grep -l changed >../../../actual.grep |
| 318 | ) && |
| 319 | test_cmp expected.grep actual.grep |
| 320 | ' |
| 321 | |
| 322 | test_expect_success 'git commit' ' |
| 323 | ( |
| 324 | cd repo.git && |
| 325 | GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done |
| 326 | ) |
| 327 | ' |
| 328 | |
| 329 | test_expect_success 'absolute pathspec should fail gracefully' ' |
| 330 | ( |
| 331 | cd repo.git && |
| 332 | test_might_fail git config --unset core.worktree && |
| 333 | test_must_fail git log HEAD -- /home |
| 334 | ) |
| 335 | ' |
| 336 | |
| 337 | test_expect_success 'make_relative_path handles double slashes in GIT_DIR' ' |
| 338 | >dummy_file && |
| 339 | echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file && |
| 340 | git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file |
| 341 | ' |
| 342 | |
| 343 | test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' ' |
| 344 | GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \ |
| 345 | test-tool subprocess --setup-work-tree rev-parse --show-toplevel >actual && |
| 346 | echo "$(pwd)/repo.git/work" >expected && |
| 347 | test_cmp expected actual |
| 348 | ' |
| 349 | |
| 350 | test_expect_success 'Multi-worktree setup' ' |
| 351 | mkdir work && |
| 352 | mkdir -p repo.git/repos/foo && |
| 353 | cp repo.git/HEAD repo.git/index repo.git/repos/foo && |
| 354 | { cp repo.git/sharedindex.* repo.git/repos/foo || :; } && |
| 355 | sane_unset GIT_DIR GIT_CONFIG GIT_WORK_TREE |
| 356 | ' |
| 357 | |
| 358 | test_expect_success 'GIT_DIR set (1)' ' |
| 359 | echo "gitdir: repo.git/repos/foo" >gitfile && |
| 360 | echo ../.. >repo.git/repos/foo/commondir && |
| 361 | ( |
| 362 | cd work && |
| 363 | GIT_DIR=../gitfile git rev-parse --git-common-dir >actual && |
| 364 | test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect && |
| 365 | test_cmp expect actual |
| 366 | ) |
| 367 | ' |
| 368 | |
| 369 | test_expect_success 'GIT_DIR set (2)' ' |
| 370 | echo "gitdir: repo.git/repos/foo" >gitfile && |
| 371 | echo "$(pwd)/repo.git" >repo.git/repos/foo/commondir && |
| 372 | ( |
| 373 | cd work && |
| 374 | GIT_DIR=../gitfile git rev-parse --git-common-dir >actual && |
| 375 | test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect && |
| 376 | test_cmp expect actual |
| 377 | ) |
| 378 | ' |
| 379 | |
| 380 | test_expect_success 'Auto discovery' ' |
| 381 | echo "gitdir: repo.git/repos/foo" >.git && |
| 382 | echo ../.. >repo.git/repos/foo/commondir && |
| 383 | ( |
| 384 | cd work && |
| 385 | git rev-parse --git-common-dir >actual && |
| 386 | test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect && |
| 387 | test_cmp expect actual && |
| 388 | echo haha >data1 && |
| 389 | git add data1 && |
| 390 | git ls-files --full-name :/ | grep data1 >actual && |
| 391 | echo work/data1 >expect && |
| 392 | test_cmp expect actual |
| 393 | ) |
| 394 | ' |
| 395 | |
| 396 | test_expect_success '$GIT_DIR/common overrides core.worktree' ' |
| 397 | mkdir elsewhere && |
| 398 | git --git-dir=repo.git config core.worktree "$TRASH_DIRECTORY/elsewhere" && |
| 399 | echo "gitdir: repo.git/repos/foo" >.git && |
| 400 | echo ../.. >repo.git/repos/foo/commondir && |
| 401 | ( |
| 402 | cd work && |
| 403 | git rev-parse --git-common-dir >actual && |
| 404 | test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect && |
| 405 | test_cmp expect actual && |
| 406 | echo haha >data2 && |
| 407 | git add data2 && |
| 408 | git ls-files --full-name :/ | grep data2 >actual && |
| 409 | echo work/data2 >expect && |
| 410 | test_cmp expect actual |
| 411 | ) |
| 412 | ' |
| 413 | |
| 414 | test_expect_success '$GIT_WORK_TREE overrides $GIT_DIR/common' ' |
| 415 | echo "gitdir: repo.git/repos/foo" >.git && |
| 416 | echo ../.. >repo.git/repos/foo/commondir && |
| 417 | ( |
| 418 | cd work && |
| 419 | echo haha >data3 && |
| 420 | git --git-dir=../.git --work-tree=. add data3 && |
| 421 | git ls-files --full-name -- :/ | grep data3 >actual && |
| 422 | echo data3 >expect && |
| 423 | test_cmp expect actual |
| 424 | ) |
| 425 | ' |
| 426 | |
| 427 | test_expect_success 'error out gracefully on invalid $GIT_WORK_TREE' ' |
| 428 | ( |
| 429 | GIT_WORK_TREE=/.invalid/work/tree && |
| 430 | export GIT_WORK_TREE && |
| 431 | test_expect_code 128 git rev-parse |
| 432 | ) |
| 433 | ' |
| 434 | |
| 435 | test_expect_success 'refs work with relative gitdir and work tree' ' |
| 436 | git init relative && |
| 437 | git -C relative commit --allow-empty -m one && |
| 438 | git -C relative commit --allow-empty -m two && |
| 439 | |
| 440 | GIT_DIR=relative/.git GIT_WORK_TREE=relative git reset HEAD^ && |
| 441 | |
| 442 | git -C relative log -1 --format=%s >actual && |
| 443 | echo one >expect && |
| 444 | test_cmp expect actual |
| 445 | ' |
| 446 | |
| 447 | test_done |