t / t1510-repo-setup.shon commit t1510: setup case #19 (09327fd)
   1#!/bin/sh
   2
   3test_description='Tests of cwd/prefix/worktree/gitdir setup in all cases'
   4
   5. ./test-lib.sh
   6
   7#
   8# A few rules for repo setup:
   9#
  10# 1. GIT_DIR is relative to user's cwd. --git-dir is equivalent to
  11#    GIT_DIR.
  12#
  13# 2. .git file is relative to parent directory. .git file is basically
  14#    symlink in disguise. The directory where .git file points to will
  15#    become new git_dir.
  16#
  17# 3. core.worktree is relative to git_dir.
  18#
  19# 4. GIT_WORK_TREE is relative to user's cwd. --work-tree is
  20#    equivalent to GIT_WORK_TREE.
  21#
  22# 5. GIT_WORK_TREE/core.worktree is only effective if GIT_DIR is set
  23#    Uneffective worktree settings should be warned.
  24#
  25# 6. Effective GIT_WORK_TREE overrides core.worktree and core.bare
  26#
  27# 7. Effective core.worktree conflicts with core.bare
  28#
  29# 8. If GIT_DIR is set but neither worktree nor bare setting is given,
  30#    original cwd becomes worktree.
  31#
  32# 9. If .git discovery is done inside a repo, the repo becomes a bare
  33#    repo. .git discovery is performed if GIT_DIR is not set.
  34#
  35# 10. If no worktree is available, cwd remains unchanged, prefix is
  36#     NULL.
  37#
  38# 11. When user's cwd is outside worktree, cwd remains unchanged,
  39#     prefix is NULL.
  40#
  41
  42test_repo() {
  43        (
  44        cd "$1" &&
  45        if test -n "$2"; then GIT_DIR="$2" && export GIT_DIR; fi &&
  46        if test -n "$3"; then GIT_WORK_TREE="$3" && export GIT_WORK_TREE; fi &&
  47        rm -f trace &&
  48        GIT_TRACE="`pwd`/trace" git symbolic-ref HEAD >/dev/null &&
  49        grep '^setup: ' trace >result &&
  50        test_cmp expected result
  51        )
  52}
  53
  54# Bit 0 = GIT_WORK_TREE
  55# Bit 1 = GIT_DIR
  56# Bit 2 = core.worktree
  57# Bit 3 = .git is a file
  58# Bit 4 = bare repo
  59# Case# = encoding of the above 5 bits
  60
  61#
  62# Case #0
  63#
  64############################################################
  65#
  66# Input:
  67#
  68#  - GIT_WORK_TREE is not set
  69#  - GIT_DIR is not set
  70#  - core.worktree is not set
  71#  - .git is a directory
  72#  - core.bare is not set, cwd is outside .git
  73#
  74# Output:
  75#
  76#  - worktree is .git's parent directory
  77#  - cwd is at worktree root dir
  78#  - prefix is calculated
  79#  - git_dir is set to ".git"
  80#  - cwd can't be outside worktree
  81
  82test_expect_success '#0: setup' '
  83        unset GIT_DIR GIT_WORK_TREE &&
  84        mkdir 0 0/sub &&
  85        cd 0 && git init && cd ..
  86'
  87
  88test_expect_success '#0: at root' '
  89        cat >0/expected <<EOF &&
  90setup: git_dir: .git
  91setup: worktree: $TRASH_DIRECTORY/0
  92setup: cwd: $TRASH_DIRECTORY/0
  93setup: prefix: (null)
  94EOF
  95        test_repo 0
  96'
  97
  98test_expect_success '#0: in subdir' '
  99        cat >0/sub/expected <<EOF &&
 100setup: git_dir: .git
 101setup: worktree: $TRASH_DIRECTORY/0
 102setup: cwd: $TRASH_DIRECTORY/0
 103setup: prefix: sub/
 104EOF
 105        test_repo 0/sub
 106'
 107
 108#
 109# case #1
 110#
 111############################################################
 112#
 113# Input:
 114#
 115#  - GIT_WORK_TREE is set
 116#  - GIT_DIR is not set
 117#  - core.worktree is not set
 118#  - .git is a directory
 119#  - core.bare is not set, cwd is outside .git
 120#
 121# Output:
 122#
 123# GIT_WORK_TREE is ignored -> #0
 124
 125test_expect_success '#1: setup' '
 126        unset GIT_DIR GIT_WORK_TREE &&
 127        mkdir 1 1/sub 1.wt 1.wt/sub 1/wt 1/wt/sub &&
 128        cd 1 &&
 129        git init &&
 130        GIT_WORK_TREE=non-existent &&
 131        export GIT_WORK_TREE &&
 132        cd ..
 133'
 134
 135test_expect_failure '#1: at root' '
 136        cat >1/expected <<EOF &&
 137setup: git_dir: .git
 138setup: worktree: $TRASH_DIRECTORY/1
 139setup: cwd: $TRASH_DIRECTORY/1
 140setup: prefix: (null)
 141EOF
 142        test_repo 1
 143'
 144
 145test_expect_failure '#1: in subdir' '
 146        cat >1/sub/expected <<EOF &&
 147setup: git_dir: .git
 148setup: worktree: $TRASH_DIRECTORY/1
 149setup: cwd: $TRASH_DIRECTORY/1
 150setup: prefix: sub/
 151EOF
 152        test_repo 1/sub
 153'
 154
 155#
 156# case #2
 157#
 158############################################################
 159#
 160# Input:
 161#
 162#  - GIT_WORK_TREE is not set
 163#  - GIT_DIR is set
 164#  - core.worktree is not set
 165#  - .git is a directory
 166#  - core.bare is not set, cwd is outside .git
 167#
 168# Output:
 169#
 170#  - worktree is at original cwd
 171#  - cwd is unchanged
 172#  - prefix is NULL
 173#  - git_dir is set to $GIT_DIR
 174#  - cwd can't be outside worktree
 175
 176test_expect_success '#2: setup' '
 177        unset GIT_DIR GIT_WORK_TREE &&
 178        mkdir 2 2/sub &&
 179        cd 2 && git init && cd ..
 180'
 181
 182test_expect_success '#2: at root' '
 183        cat >2/expected <<EOF &&
 184setup: git_dir: $TRASH_DIRECTORY/2/.git
 185setup: worktree: $TRASH_DIRECTORY/2
 186setup: cwd: $TRASH_DIRECTORY/2
 187setup: prefix: (null)
 188EOF
 189        test_repo 2 "$TRASH_DIRECTORY/2/.git"
 190'
 191
 192test_expect_success '#2: in subdir' '
 193        cat >2/sub/expected <<EOF &&
 194setup: git_dir: $TRASH_DIRECTORY/2/.git
 195setup: worktree: $TRASH_DIRECTORY/2/sub
 196setup: cwd: $TRASH_DIRECTORY/2/sub
 197setup: prefix: (null)
 198EOF
 199        test_repo 2/sub "$TRASH_DIRECTORY/2/.git"
 200'
 201
 202test_expect_success '#2: relative GIT_DIR at root' '
 203        cat >2/expected <<EOF &&
 204setup: git_dir: .git
 205setup: worktree: $TRASH_DIRECTORY/2
 206setup: cwd: $TRASH_DIRECTORY/2
 207setup: prefix: (null)
 208EOF
 209        test_repo 2 .git
 210'
 211
 212test_expect_success '#2: relative GIT_DIR in subdir' '
 213        cat >2/sub/expected <<EOF &&
 214setup: git_dir: ../.git
 215setup: worktree: $TRASH_DIRECTORY/2/sub
 216setup: cwd: $TRASH_DIRECTORY/2/sub
 217setup: prefix: (null)
 218EOF
 219        test_repo 2/sub ../.git
 220'
 221
 222#
 223# case #3
 224#
 225############################################################
 226#
 227# Input:
 228#
 229#  - GIT_WORK_TREE is set
 230#  - GIT_DIR is set
 231#  - core.worktree is not set
 232#  - .git is a directory
 233#  - core.bare is not set, cwd is outside .git
 234#
 235# Output:
 236#
 237#  - worktree is set to $GIT_WORK_TREE
 238#  - cwd is at worktree root
 239#  - prefix is calculated
 240#  - git_dir is set to $GIT_DIR
 241#  - cwd can be outside worktree
 242
 243test_expect_success '#3: setup' '
 244        unset GIT_DIR GIT_WORK_TREE &&
 245        mkdir 3 3/sub 3/sub/sub 3.wt 3.wt/sub 3/wt 3/wt/sub &&
 246        cd 3 && git init && cd ..
 247'
 248
 249test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
 250        cat >3/expected <<EOF &&
 251setup: git_dir: .git
 252setup: worktree: $TRASH_DIRECTORY/3
 253setup: cwd: $TRASH_DIRECTORY/3
 254setup: prefix: (null)
 255EOF
 256        test_repo 3 .git "$TRASH_DIRECTORY/3"
 257'
 258
 259test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
 260        cat >3/expected <<EOF &&
 261setup: git_dir: .git
 262setup: worktree: $TRASH_DIRECTORY/3
 263setup: cwd: $TRASH_DIRECTORY/3
 264setup: prefix: (null)
 265EOF
 266        test_repo 3 .git .
 267'
 268
 269test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=root at root' '
 270        cat >3/expected <<EOF &&
 271setup: git_dir: $TRASH_DIRECTORY/3/.git
 272setup: worktree: $TRASH_DIRECTORY/3
 273setup: cwd: $TRASH_DIRECTORY/3
 274setup: prefix: (null)
 275EOF
 276        test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3"
 277'
 278
 279test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
 280        cat >3/expected <<EOF &&
 281setup: git_dir: $TRASH_DIRECTORY/3/.git
 282setup: worktree: $TRASH_DIRECTORY/3
 283setup: cwd: $TRASH_DIRECTORY/3
 284setup: prefix: (null)
 285EOF
 286        test_repo 3 "$TRASH_DIRECTORY/3/.git" .
 287'
 288
 289test_expect_success '#3: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
 290        cat >3/sub/sub/expected <<EOF &&
 291setup: git_dir: $TRASH_DIRECTORY/3/.git
 292setup: worktree: $TRASH_DIRECTORY/3
 293setup: cwd: $TRASH_DIRECTORY/3
 294setup: prefix: sub/sub/
 295EOF
 296        test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY/3"
 297'
 298
 299test_expect_success '#3: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
 300        cat >3/sub/sub/expected <<EOF &&
 301setup: git_dir: $TRASH_DIRECTORY/3/.git
 302setup: worktree: $TRASH_DIRECTORY/3
 303setup: cwd: $TRASH_DIRECTORY/3
 304setup: prefix: sub/sub/
 305EOF
 306        test_repo 3/sub/sub ../../.git ../..
 307'
 308
 309test_expect_success '#3: GIT_DIR, GIT_WORKTREE=root in subdir' '
 310        cat >3/sub/expected <<EOF &&
 311setup: git_dir: $TRASH_DIRECTORY/3/.git
 312setup: worktree: $TRASH_DIRECTORY/3
 313setup: cwd: $TRASH_DIRECTORY/3
 314setup: prefix: sub/
 315EOF
 316        test_repo 3/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3"
 317'
 318
 319test_expect_success '#3: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
 320        cat >3/sub/sub/expected <<EOF &&
 321setup: git_dir: $TRASH_DIRECTORY/3/.git
 322setup: worktree: $TRASH_DIRECTORY/3
 323setup: cwd: $TRASH_DIRECTORY/3
 324setup: prefix: sub/sub/
 325EOF
 326        test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../..
 327'
 328
 329test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
 330        cat >3/expected <<EOF &&
 331setup: git_dir: .git
 332setup: worktree: $TRASH_DIRECTORY/3/wt
 333setup: cwd: $TRASH_DIRECTORY/3
 334setup: prefix: (null)
 335EOF
 336        test_repo 3 .git "$TRASH_DIRECTORY/3/wt"
 337'
 338
 339test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
 340        cat >3/expected <<EOF &&
 341setup: git_dir: .git
 342setup: worktree: $TRASH_DIRECTORY/3/wt
 343setup: cwd: $TRASH_DIRECTORY/3
 344setup: prefix: (null)
 345EOF
 346        test_repo 3 .git wt
 347'
 348
 349test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
 350        cat >3/expected <<EOF &&
 351setup: git_dir: $TRASH_DIRECTORY/3/.git
 352setup: worktree: $TRASH_DIRECTORY/3/wt
 353setup: cwd: $TRASH_DIRECTORY/3
 354setup: prefix: (null)
 355EOF
 356        test_repo 3 "$TRASH_DIRECTORY/3/.git" wt
 357'
 358
 359test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=wt at root' '
 360        cat >3/expected <<EOF &&
 361setup: git_dir: $TRASH_DIRECTORY/3/.git
 362setup: worktree: $TRASH_DIRECTORY/3/wt
 363setup: cwd: $TRASH_DIRECTORY/3
 364setup: prefix: (null)
 365EOF
 366        test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3/wt"
 367'
 368
 369test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
 370        cat >3/sub/sub/expected <<EOF &&
 371setup: git_dir: ../../.git
 372setup: worktree: $TRASH_DIRECTORY/3/wt
 373setup: cwd: $TRASH_DIRECTORY/3/sub/sub
 374setup: prefix: (null)
 375EOF
 376        test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY/3/wt"
 377'
 378
 379test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
 380        cat >3/sub/sub/expected <<EOF &&
 381setup: git_dir: ../../.git
 382setup: worktree: $TRASH_DIRECTORY/3/wt
 383setup: cwd: $TRASH_DIRECTORY/3/sub/sub
 384setup: prefix: (null)
 385EOF
 386        test_repo 3/sub/sub ../../.git ../../wt
 387'
 388
 389test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
 390        cat >3/sub/sub/expected <<EOF &&
 391setup: git_dir: $TRASH_DIRECTORY/3/.git
 392setup: worktree: $TRASH_DIRECTORY/3/wt
 393setup: cwd: $TRASH_DIRECTORY/3/sub/sub
 394setup: prefix: (null)
 395EOF
 396        test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../../wt
 397'
 398
 399test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
 400        cat >3/sub/sub/expected <<EOF &&
 401setup: git_dir: $TRASH_DIRECTORY/3/.git
 402setup: worktree: $TRASH_DIRECTORY/3/wt
 403setup: cwd: $TRASH_DIRECTORY/3/sub/sub
 404setup: prefix: (null)
 405EOF
 406        test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3/wt"
 407'
 408
 409test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
 410        cat >3/expected <<EOF &&
 411setup: git_dir: $TRASH_DIRECTORY/3/.git
 412setup: worktree: $TRASH_DIRECTORY
 413setup: cwd: $TRASH_DIRECTORY
 414setup: prefix: 3/
 415EOF
 416        test_repo 3 .git "$TRASH_DIRECTORY"
 417'
 418
 419test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
 420        cat >3/expected <<EOF &&
 421setup: git_dir: $TRASH_DIRECTORY/3/.git
 422setup: worktree: $TRASH_DIRECTORY
 423setup: cwd: $TRASH_DIRECTORY
 424setup: prefix: 3/
 425EOF
 426        test_repo 3 .git ..
 427'
 428
 429test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
 430        cat >3/expected <<EOF &&
 431setup: git_dir: $TRASH_DIRECTORY/3/.git
 432setup: worktree: $TRASH_DIRECTORY
 433setup: cwd: $TRASH_DIRECTORY
 434setup: prefix: 3/
 435EOF
 436        test_repo 3 "$TRASH_DIRECTORY/3/.git" ..
 437'
 438
 439test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=.. at root' '
 440        cat >3/expected <<EOF &&
 441setup: git_dir: $TRASH_DIRECTORY/3/.git
 442setup: worktree: $TRASH_DIRECTORY
 443setup: cwd: $TRASH_DIRECTORY
 444setup: prefix: 3/
 445EOF
 446        test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY"
 447'
 448
 449test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
 450        cat >3/sub/sub/expected <<EOF &&
 451setup: git_dir: $TRASH_DIRECTORY/3/.git
 452setup: worktree: $TRASH_DIRECTORY
 453setup: cwd: $TRASH_DIRECTORY
 454setup: prefix: 3/sub/sub/
 455EOF
 456        test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY"
 457'
 458
 459test_expect_success '#3: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
 460        cat >3/sub/sub/expected <<EOF &&
 461setup: git_dir: $TRASH_DIRECTORY/3/.git
 462setup: worktree: $TRASH_DIRECTORY
 463setup: cwd: $TRASH_DIRECTORY
 464setup: prefix: 3/sub/sub/
 465EOF
 466        test_repo 3/sub/sub ../../.git ../../..
 467'
 468
 469test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
 470        cat >3/sub/sub/expected <<EOF &&
 471setup: git_dir: $TRASH_DIRECTORY/3/.git
 472setup: worktree: $TRASH_DIRECTORY
 473setup: cwd: $TRASH_DIRECTORY
 474setup: prefix: 3/sub/sub/
 475EOF
 476        test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../../../
 477'
 478
 479test_expect_success '#3: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
 480        cat >3/sub/sub/expected <<EOF &&
 481setup: git_dir: $TRASH_DIRECTORY/3/.git
 482setup: worktree: $TRASH_DIRECTORY
 483setup: cwd: $TRASH_DIRECTORY
 484setup: prefix: 3/sub/sub/
 485EOF
 486        test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY"
 487'
 488
 489#
 490# case #4
 491#
 492############################################################
 493#
 494# Input:
 495#
 496#  - GIT_WORK_TREE is not set
 497#  - GIT_DIR is not set
 498#  - core.worktree is set
 499#  - .git is a directory
 500#  - core.bare is not set, cwd is outside .git
 501#
 502# Output:
 503#
 504# core.worktree is ignored -> #0
 505
 506test_expect_success '#4: setup' '
 507        unset GIT_DIR GIT_WORK_TREE &&
 508        mkdir 4 4/sub &&
 509        cd 4 &&
 510        git init &&
 511        git config core.worktree non-existent &&
 512        cd ..
 513'
 514
 515test_expect_failure '#4: at root' '
 516        cat >4/expected <<EOF &&
 517setup: git_dir: .git
 518setup: worktree: $TRASH_DIRECTORY/4
 519setup: cwd: $TRASH_DIRECTORY/4
 520setup: prefix: (null)
 521EOF
 522        test_repo 4
 523'
 524
 525test_expect_failure '#4: in subdir' '
 526        cat >4/sub/expected <<EOF &&
 527setup: git_dir: .git
 528setup: worktree: $TRASH_DIRECTORY/4
 529setup: cwd: $TRASH_DIRECTORY/4
 530setup: prefix: sub/
 531EOF
 532        test_repo 4/sub
 533'
 534
 535#
 536# case #5
 537#
 538############################################################
 539#
 540# Input:
 541#
 542#  - GIT_WORK_TREE is set
 543#  - GIT_DIR is not set
 544#  - core.worktree is set
 545#  - .git is a directory
 546#  - core.bare is not set, cwd is outside .git
 547#
 548# Output:
 549#
 550# GIT_WORK_TREE/core.worktree are ignored -> #0
 551
 552test_expect_success '#5: setup' '
 553        unset GIT_DIR GIT_WORK_TREE &&
 554        mkdir 5 5/sub &&
 555        cd 5 &&
 556        git init &&
 557        git config core.worktree non-existent &&
 558        GIT_WORK_TREE=non-existent-too &&
 559        export GIT_WORK_TREE &&
 560        cd ..
 561'
 562
 563test_expect_failure '#5: at root' '
 564        cat >5/expected <<EOF &&
 565setup: git_dir: .git
 566setup: worktree: $TRASH_DIRECTORY/5
 567setup: cwd: $TRASH_DIRECTORY/5
 568setup: prefix: (null)
 569EOF
 570        test_repo 5
 571'
 572
 573test_expect_failure '#5: in subdir' '
 574        cat >5/sub/expected <<EOF &&
 575setup: git_dir: .git
 576setup: worktree: $TRASH_DIRECTORY/5
 577setup: cwd: $TRASH_DIRECTORY/5
 578setup: prefix: sub/
 579EOF
 580        test_repo 5/sub
 581'
 582
 583#
 584# case #6
 585#
 586############################################################
 587#
 588# Input:
 589#
 590#  - GIT_WORK_TREE is not set
 591#  - GIT_DIR is set
 592#  - core.worktree is set
 593#  - .git is a directory
 594#  - core.bare is not set, cwd is outside .git
 595#
 596# Output:
 597#
 598#  - worktree is at core.worktree
 599#  - cwd is at worktree root
 600#  - prefix is calculated
 601#  - git_dir is at $GIT_DIR
 602#  - cwd can be outside worktree
 603
 604test_expect_success '#6: setup' '
 605        unset GIT_DIR GIT_WORK_TREE &&
 606        mkdir 6 6/sub 6/sub/sub 6.wt 6.wt/sub 6/wt 6/wt/sub &&
 607        cd 6 && git init && cd ..
 608'
 609
 610test_expect_success '#6: GIT_DIR(rel), core.worktree=.. at root' '
 611        cat >6/expected <<EOF &&
 612setup: git_dir: .git
 613setup: worktree: $TRASH_DIRECTORY/6
 614setup: cwd: $TRASH_DIRECTORY/6
 615setup: prefix: (null)
 616EOF
 617        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6" &&
 618        test_repo 6 .git
 619'
 620
 621test_expect_success '#6: GIT_DIR(rel), core.worktree=..(rel) at root' '
 622        cat >6/expected <<EOF &&
 623setup: git_dir: .git
 624setup: worktree: $TRASH_DIRECTORY/6
 625setup: cwd: $TRASH_DIRECTORY/6
 626setup: prefix: (null)
 627EOF
 628        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree .. &&
 629        test_repo 6 .git
 630'
 631
 632test_expect_success '#6: GIT_DIR, core.worktree=.. at root' '
 633        cat >6/expected <<EOF &&
 634setup: git_dir: $TRASH_DIRECTORY/6/.git
 635setup: worktree: $TRASH_DIRECTORY/6
 636setup: cwd: $TRASH_DIRECTORY/6
 637setup: prefix: (null)
 638EOF
 639        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6" &&
 640        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 641'
 642
 643test_expect_success '#6: GIT_DIR, core.worktree=..(rel) at root' '
 644        cat >6/expected <<EOF &&
 645setup: git_dir: $TRASH_DIRECTORY/6/.git
 646setup: worktree: $TRASH_DIRECTORY/6
 647setup: cwd: $TRASH_DIRECTORY/6
 648setup: prefix: (null)
 649EOF
 650        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree .. &&
 651        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 652'
 653
 654test_expect_failure '#6: GIT_DIR(rel), core.worktree=.. in subdir' '
 655        cat >6/sub/sub/expected <<EOF &&
 656setup: git_dir: $TRASH_DIRECTORY/6/.git
 657setup: worktree: $TRASH_DIRECTORY/6
 658setup: cwd: $TRASH_DIRECTORY/6
 659setup: prefix: sub/sub/
 660EOF
 661        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6" &&
 662        test_repo 6/sub/sub ../../.git
 663'
 664
 665test_expect_failure '#6: GIT_DIR(rel), core.worktree=..(rel) in subdir' '
 666        cat >6/sub/sub/expected <<EOF &&
 667setup: git_dir: $TRASH_DIRECTORY/6/.git
 668setup: worktree: $TRASH_DIRECTORY/6
 669setup: cwd: $TRASH_DIRECTORY/6
 670setup: prefix: sub/sub/
 671EOF
 672        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree .. &&
 673        test_repo 6/sub/sub ../../.git
 674'
 675
 676test_expect_success '#6: GIT_DIR, core.worktree=.. in subdir' '
 677        cat >6/sub/expected <<EOF &&
 678setup: git_dir: $TRASH_DIRECTORY/6/.git
 679setup: worktree: $TRASH_DIRECTORY/6
 680setup: cwd: $TRASH_DIRECTORY/6
 681setup: prefix: sub/
 682EOF
 683        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6" &&
 684        test_repo 6/sub "$TRASH_DIRECTORY/6/.git"
 685'
 686
 687test_expect_success '#6: GIT_DIR, core.worktree=..(rel) in subdir' '
 688        cat >6/sub/sub/expected <<EOF &&
 689setup: git_dir: $TRASH_DIRECTORY/6/.git
 690setup: worktree: $TRASH_DIRECTORY/6
 691setup: cwd: $TRASH_DIRECTORY/6
 692setup: prefix: sub/sub/
 693EOF
 694        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree .. &&
 695        test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git"
 696'
 697
 698test_expect_success '#6: GIT_DIR(rel), core.worktree=../wt at root' '
 699        cat >6/expected <<EOF &&
 700setup: git_dir: .git
 701setup: worktree: $TRASH_DIRECTORY/6/wt
 702setup: cwd: $TRASH_DIRECTORY/6
 703setup: prefix: (null)
 704EOF
 705        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6/wt" &&
 706        test_repo 6 .git
 707'
 708
 709test_expect_success '#6: GIT_DIR(rel), core.worktree=../wt(rel) at root' '
 710        cat >6/expected <<EOF &&
 711setup: git_dir: .git
 712setup: worktree: $TRASH_DIRECTORY/6/wt
 713setup: cwd: $TRASH_DIRECTORY/6
 714setup: prefix: (null)
 715EOF
 716        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../wt &&
 717        test_repo 6 .git
 718'
 719
 720test_expect_success '#6: GIT_DIR, core.worktree=../wt(rel) at root' '
 721        cat >6/expected <<EOF &&
 722setup: git_dir: $TRASH_DIRECTORY/6/.git
 723setup: worktree: $TRASH_DIRECTORY/6/wt
 724setup: cwd: $TRASH_DIRECTORY/6
 725setup: prefix: (null)
 726EOF
 727        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../wt &&
 728        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 729'
 730
 731test_expect_success '#6: GIT_DIR, core.worktree=../wt at root' '
 732        cat >6/expected <<EOF &&
 733setup: git_dir: $TRASH_DIRECTORY/6/.git
 734setup: worktree: $TRASH_DIRECTORY/6/wt
 735setup: cwd: $TRASH_DIRECTORY/6
 736setup: prefix: (null)
 737EOF
 738        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6/wt" &&
 739        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 740'
 741
 742test_expect_success '#6: GIT_DIR(rel), core.worktree=../wt in subdir' '
 743        cat >6/sub/sub/expected <<EOF &&
 744setup: git_dir: ../../.git
 745setup: worktree: $TRASH_DIRECTORY/6/wt
 746setup: cwd: $TRASH_DIRECTORY/6/sub/sub
 747setup: prefix: (null)
 748EOF
 749        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6/wt" &&
 750        test_repo 6/sub/sub ../../.git
 751'
 752
 753test_expect_success '#6: GIT_DIR(rel), core.worktree=../wt(rel) in subdir' '
 754        cat >6/sub/sub/expected <<EOF &&
 755setup: git_dir: ../../.git
 756setup: worktree: $TRASH_DIRECTORY/6/wt
 757setup: cwd: $TRASH_DIRECTORY/6/sub/sub
 758setup: prefix: (null)
 759EOF
 760        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../wt &&
 761        test_repo 6/sub/sub ../../.git
 762'
 763
 764test_expect_success '#6: GIT_DIR, core.worktree=../wt(rel) in subdir' '
 765        cat >6/sub/sub/expected <<EOF &&
 766setup: git_dir: $TRASH_DIRECTORY/6/.git
 767setup: worktree: $TRASH_DIRECTORY/6/wt
 768setup: cwd: $TRASH_DIRECTORY/6/sub/sub
 769setup: prefix: (null)
 770EOF
 771        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../wt &&
 772        test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git"
 773'
 774
 775test_expect_success '#6: GIT_DIR, core.worktree=../wt in subdir' '
 776        cat >6/sub/sub/expected <<EOF &&
 777setup: git_dir: $TRASH_DIRECTORY/6/.git
 778setup: worktree: $TRASH_DIRECTORY/6/wt
 779setup: cwd: $TRASH_DIRECTORY/6/sub/sub
 780setup: prefix: (null)
 781EOF
 782        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY/6/wt" &&
 783        test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git"
 784'
 785
 786test_expect_failure '#6: GIT_DIR(rel), core.worktree=../.. at root' '
 787        cat >6/expected <<EOF &&
 788setup: git_dir: $TRASH_DIRECTORY/6/.git
 789setup: worktree: $TRASH_DIRECTORY
 790setup: cwd: $TRASH_DIRECTORY
 791setup: prefix: 6/
 792EOF
 793        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY" &&
 794        test_repo 6 .git
 795'
 796
 797test_expect_failure '#6: GIT_DIR(rel), core.worktree=../..(rel) at root' '
 798        cat >6/expected <<EOF &&
 799setup: git_dir: $TRASH_DIRECTORY/6/.git
 800setup: worktree: $TRASH_DIRECTORY
 801setup: cwd: $TRASH_DIRECTORY
 802setup: prefix: 6/
 803EOF
 804        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../../ &&
 805        test_repo 6 .git
 806'
 807
 808test_expect_success '#6: GIT_DIR, core.worktree=../..(rel) at root' '
 809        cat >6/expected <<EOF &&
 810setup: git_dir: $TRASH_DIRECTORY/6/.git
 811setup: worktree: $TRASH_DIRECTORY
 812setup: cwd: $TRASH_DIRECTORY
 813setup: prefix: 6/
 814EOF
 815        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../../ &&
 816        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 817'
 818
 819test_expect_success '#6: GIT_DIR, core.worktree=../.. at root' '
 820        cat >6/expected <<EOF &&
 821setup: git_dir: $TRASH_DIRECTORY/6/.git
 822setup: worktree: $TRASH_DIRECTORY
 823setup: cwd: $TRASH_DIRECTORY
 824setup: prefix: 6/
 825EOF
 826        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY" &&
 827        test_repo 6 "$TRASH_DIRECTORY/6/.git"
 828'
 829
 830test_expect_failure '#6: GIT_DIR(rel), core.worktree=../.. in subdir' '
 831        cat >6/sub/sub/expected <<EOF &&
 832setup: git_dir: $TRASH_DIRECTORY/6/.git
 833setup: worktree: $TRASH_DIRECTORY
 834setup: cwd: $TRASH_DIRECTORY
 835setup: prefix: 6/sub/sub/
 836EOF
 837        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY" &&
 838        test_repo 6/sub/sub ../../.git
 839'
 840
 841test_expect_failure '#6: GIT_DIR(rel), core.worktree=../..(rel) in subdir' '
 842        cat >6/sub/sub/expected <<EOF &&
 843setup: git_dir: $TRASH_DIRECTORY/6/.git
 844setup: worktree: $TRASH_DIRECTORY
 845setup: cwd: $TRASH_DIRECTORY
 846setup: prefix: 6/sub/sub/
 847EOF
 848        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../.. &&
 849        test_repo 6/sub/sub ../../.git
 850'
 851
 852test_expect_success '#6: GIT_DIR, core.worktree=../..(rel) in subdir' '
 853        cat >6/sub/sub/expected <<EOF &&
 854setup: git_dir: $TRASH_DIRECTORY/6/.git
 855setup: worktree: $TRASH_DIRECTORY
 856setup: cwd: $TRASH_DIRECTORY
 857setup: prefix: 6/sub/sub/
 858EOF
 859        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree ../.. &&
 860        test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git"
 861'
 862
 863test_expect_success '#6: GIT_DIR, core.worktree=../.. in subdir' '
 864        cat >6/sub/sub/expected <<EOF &&
 865setup: git_dir: $TRASH_DIRECTORY/6/.git
 866setup: worktree: $TRASH_DIRECTORY
 867setup: cwd: $TRASH_DIRECTORY
 868setup: prefix: 6/sub/sub/
 869EOF
 870        git config --file="$TRASH_DIRECTORY/6/.git/config" core.worktree "$TRASH_DIRECTORY" &&
 871        test_repo 6/sub/sub "$TRASH_DIRECTORY/6/.git"
 872'
 873
 874#
 875# case #7
 876#
 877############################################################
 878#
 879# Input:
 880#
 881#  - GIT_WORK_TREE is set
 882#  - GIT_DIR is set
 883#  - core.worktree is set
 884#  - .git is a directory
 885#  - core.bare is not set, cwd is outside .git
 886#
 887# Output:
 888#
 889# core.worktree is overridden by GIT_WORK_TREE -> #3
 890
 891test_expect_success '#7: setup' '
 892        unset GIT_DIR GIT_WORK_TREE &&
 893        mkdir 7 7/sub 7/sub/sub 7.wt 7.wt/sub 7/wt 7/wt/sub &&
 894        cd 7 &&
 895        git init &&
 896        git config core.worktree non-existent &&
 897        cd ..
 898'
 899
 900test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
 901        cat >7/expected <<EOF &&
 902setup: git_dir: .git
 903setup: worktree: $TRASH_DIRECTORY/7
 904setup: cwd: $TRASH_DIRECTORY/7
 905setup: prefix: (null)
 906EOF
 907        test_repo 7 .git "$TRASH_DIRECTORY/7"
 908'
 909
 910test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
 911        cat >7/expected <<EOF &&
 912setup: git_dir: .git
 913setup: worktree: $TRASH_DIRECTORY/7
 914setup: cwd: $TRASH_DIRECTORY/7
 915setup: prefix: (null)
 916EOF
 917        test_repo 7 .git .
 918'
 919
 920test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=root at root' '
 921        cat >7/expected <<EOF &&
 922setup: git_dir: $TRASH_DIRECTORY/7/.git
 923setup: worktree: $TRASH_DIRECTORY/7
 924setup: cwd: $TRASH_DIRECTORY/7
 925setup: prefix: (null)
 926EOF
 927        test_repo 7 "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY/7"
 928'
 929
 930test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
 931        cat >7/expected <<EOF &&
 932setup: git_dir: $TRASH_DIRECTORY/7/.git
 933setup: worktree: $TRASH_DIRECTORY/7
 934setup: cwd: $TRASH_DIRECTORY/7
 935setup: prefix: (null)
 936EOF
 937        test_repo 7 "$TRASH_DIRECTORY/7/.git" .
 938'
 939
 940test_expect_success '#7: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
 941        cat >7/sub/sub/expected <<EOF &&
 942setup: git_dir: $TRASH_DIRECTORY/7/.git
 943setup: worktree: $TRASH_DIRECTORY/7
 944setup: cwd: $TRASH_DIRECTORY/7
 945setup: prefix: sub/sub/
 946EOF
 947        test_repo 7/sub/sub ../../.git "$TRASH_DIRECTORY/7"
 948'
 949
 950test_expect_success '#7: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
 951        cat >7/sub/sub/expected <<EOF &&
 952setup: git_dir: $TRASH_DIRECTORY/7/.git
 953setup: worktree: $TRASH_DIRECTORY/7
 954setup: cwd: $TRASH_DIRECTORY/7
 955setup: prefix: sub/sub/
 956EOF
 957        test_repo 7/sub/sub ../../.git ../..
 958'
 959
 960test_expect_success '#7: GIT_DIR, GIT_WORKTREE=root in subdir' '
 961        cat >7/sub/expected <<EOF &&
 962setup: git_dir: $TRASH_DIRECTORY/7/.git
 963setup: worktree: $TRASH_DIRECTORY/7
 964setup: cwd: $TRASH_DIRECTORY/7
 965setup: prefix: sub/
 966EOF
 967        test_repo 7/sub "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY/7"
 968'
 969
 970test_expect_success '#7: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
 971        cat >7/sub/sub/expected <<EOF &&
 972setup: git_dir: $TRASH_DIRECTORY/7/.git
 973setup: worktree: $TRASH_DIRECTORY/7
 974setup: cwd: $TRASH_DIRECTORY/7
 975setup: prefix: sub/sub/
 976EOF
 977        test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" ../..
 978'
 979
 980test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
 981        cat >7/expected <<EOF &&
 982setup: git_dir: .git
 983setup: worktree: $TRASH_DIRECTORY/7/wt
 984setup: cwd: $TRASH_DIRECTORY/7
 985setup: prefix: (null)
 986EOF
 987        test_repo 7 .git "$TRASH_DIRECTORY/7/wt"
 988'
 989
 990test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
 991        cat >7/expected <<EOF &&
 992setup: git_dir: .git
 993setup: worktree: $TRASH_DIRECTORY/7/wt
 994setup: cwd: $TRASH_DIRECTORY/7
 995setup: prefix: (null)
 996EOF
 997        test_repo 7 .git wt
 998'
 999
1000test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
1001        cat >7/expected <<EOF &&
1002setup: git_dir: $TRASH_DIRECTORY/7/.git
1003setup: worktree: $TRASH_DIRECTORY/7/wt
1004setup: cwd: $TRASH_DIRECTORY/7
1005setup: prefix: (null)
1006EOF
1007        test_repo 7 "$TRASH_DIRECTORY/7/.git" wt
1008'
1009
1010test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=wt at root' '
1011        cat >7/expected <<EOF &&
1012setup: git_dir: $TRASH_DIRECTORY/7/.git
1013setup: worktree: $TRASH_DIRECTORY/7/wt
1014setup: cwd: $TRASH_DIRECTORY/7
1015setup: prefix: (null)
1016EOF
1017        test_repo 7 "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY/7/wt"
1018'
1019
1020test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
1021        cat >7/sub/sub/expected <<EOF &&
1022setup: git_dir: ../../.git
1023setup: worktree: $TRASH_DIRECTORY/7/wt
1024setup: cwd: $TRASH_DIRECTORY/7/sub/sub
1025setup: prefix: (null)
1026EOF
1027        test_repo 7/sub/sub ../../.git "$TRASH_DIRECTORY/7/wt"
1028'
1029
1030test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
1031        cat >7/sub/sub/expected <<EOF &&
1032setup: git_dir: ../../.git
1033setup: worktree: $TRASH_DIRECTORY/7/wt
1034setup: cwd: $TRASH_DIRECTORY/7/sub/sub
1035setup: prefix: (null)
1036EOF
1037        test_repo 7/sub/sub ../../.git ../../wt
1038'
1039
1040test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
1041        cat >7/sub/sub/expected <<EOF &&
1042setup: git_dir: $TRASH_DIRECTORY/7/.git
1043setup: worktree: $TRASH_DIRECTORY/7/wt
1044setup: cwd: $TRASH_DIRECTORY/7/sub/sub
1045setup: prefix: (null)
1046EOF
1047        test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" ../../wt
1048'
1049
1050test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
1051        cat >7/sub/sub/expected <<EOF &&
1052setup: git_dir: $TRASH_DIRECTORY/7/.git
1053setup: worktree: $TRASH_DIRECTORY/7/wt
1054setup: cwd: $TRASH_DIRECTORY/7/sub/sub
1055setup: prefix: (null)
1056EOF
1057        test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY/7/wt"
1058'
1059
1060test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
1061        cat >7/expected <<EOF &&
1062setup: git_dir: $TRASH_DIRECTORY/7/.git
1063setup: worktree: $TRASH_DIRECTORY
1064setup: cwd: $TRASH_DIRECTORY
1065setup: prefix: 7/
1066EOF
1067        test_repo 7 .git "$TRASH_DIRECTORY"
1068'
1069
1070test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
1071        cat >7/expected <<EOF &&
1072setup: git_dir: $TRASH_DIRECTORY/7/.git
1073setup: worktree: $TRASH_DIRECTORY
1074setup: cwd: $TRASH_DIRECTORY
1075setup: prefix: 7/
1076EOF
1077        test_repo 7 .git ..
1078'
1079
1080test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
1081        cat >7/expected <<EOF &&
1082setup: git_dir: $TRASH_DIRECTORY/7/.git
1083setup: worktree: $TRASH_DIRECTORY
1084setup: cwd: $TRASH_DIRECTORY
1085setup: prefix: 7/
1086EOF
1087        test_repo 7 "$TRASH_DIRECTORY/7/.git" ..
1088'
1089
1090test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=.. at root' '
1091        cat >7/expected <<EOF &&
1092setup: git_dir: $TRASH_DIRECTORY/7/.git
1093setup: worktree: $TRASH_DIRECTORY
1094setup: cwd: $TRASH_DIRECTORY
1095setup: prefix: 7/
1096EOF
1097        test_repo 7 "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY"
1098'
1099
1100test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
1101        cat >7/sub/sub/expected <<EOF &&
1102setup: git_dir: $TRASH_DIRECTORY/7/.git
1103setup: worktree: $TRASH_DIRECTORY
1104setup: cwd: $TRASH_DIRECTORY
1105setup: prefix: 7/sub/sub/
1106EOF
1107        test_repo 7/sub/sub ../../.git "$TRASH_DIRECTORY"
1108'
1109
1110test_expect_success '#7: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
1111        cat >7/sub/sub/expected <<EOF &&
1112setup: git_dir: $TRASH_DIRECTORY/7/.git
1113setup: worktree: $TRASH_DIRECTORY
1114setup: cwd: $TRASH_DIRECTORY
1115setup: prefix: 7/sub/sub/
1116EOF
1117        test_repo 7/sub/sub ../../.git ../../..
1118'
1119
1120test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
1121        cat >7/sub/sub/expected <<EOF &&
1122setup: git_dir: $TRASH_DIRECTORY/7/.git
1123setup: worktree: $TRASH_DIRECTORY
1124setup: cwd: $TRASH_DIRECTORY
1125setup: prefix: 7/sub/sub/
1126EOF
1127        test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" ../../../
1128'
1129
1130test_expect_success '#7: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
1131        cat >7/sub/sub/expected <<EOF &&
1132setup: git_dir: $TRASH_DIRECTORY/7/.git
1133setup: worktree: $TRASH_DIRECTORY
1134setup: cwd: $TRASH_DIRECTORY
1135setup: prefix: 7/sub/sub/
1136EOF
1137        test_repo 7/sub/sub "$TRASH_DIRECTORY/7/.git" "$TRASH_DIRECTORY"
1138'
1139
1140#
1141# case #8
1142#
1143############################################################
1144#
1145# Input:
1146#
1147#  - GIT_WORK_TREE is not set
1148#  - GIT_DIR is not set
1149#  - core.worktree is not set
1150#  - .git is a file
1151#  - core.bare is not set, cwd is outside .git
1152#
1153# Output:
1154#
1155# #0 except that git_dir is set by .git file
1156
1157test_expect_success '#8: setup' '
1158        unset GIT_DIR GIT_WORK_TREE &&
1159        mkdir 8 8/sub &&
1160        cd 8 &&
1161        git init &&
1162        mv .git ../8.git &&
1163        echo gitdir: ../8.git >.git &&
1164        cd ..
1165'
1166
1167test_expect_success '#8: at root' '
1168        cat >8/expected <<EOF &&
1169setup: git_dir: $TRASH_DIRECTORY/8.git
1170setup: worktree: $TRASH_DIRECTORY/8
1171setup: cwd: $TRASH_DIRECTORY/8
1172setup: prefix: (null)
1173EOF
1174        test_repo 8
1175'
1176
1177test_expect_success '#8: in subdir' '
1178        cat >8/sub/expected <<EOF &&
1179setup: git_dir: $TRASH_DIRECTORY/8.git
1180setup: worktree: $TRASH_DIRECTORY/8
1181setup: cwd: $TRASH_DIRECTORY/8
1182setup: prefix: sub/
1183EOF
1184        test_repo 8/sub
1185'
1186
1187#
1188# case #9
1189#
1190############################################################
1191#
1192# Input:
1193#
1194#  - GIT_WORK_TREE is set
1195#  - GIT_DIR is not set
1196#  - core.worktree is not set
1197#  - .git is a file
1198#  - core.bare is not set, cwd is outside .git
1199#
1200# Output:
1201#
1202# #1 except that git_dir is set by .git file
1203
1204test_expect_success '#9: setup' '
1205        unset GIT_DIR GIT_WORK_TREE &&
1206        mkdir 9 9/sub 9.wt 9.wt/sub 9/wt 9/wt/sub &&
1207        cd 9 &&
1208        git init &&
1209        mv .git ../9.git &&
1210        echo gitdir: ../9.git >.git &&
1211        GIT_WORK_TREE=non-existent &&
1212        export GIT_WORK_TREE &&
1213        cd ..
1214'
1215
1216test_expect_failure '#9: at root' '
1217        cat >9/expected <<EOF &&
1218setup: git_dir: $TRASH_DIRECTORY/9.git
1219setup: worktree: $TRASH_DIRECTORY/9
1220setup: cwd: $TRASH_DIRECTORY/9
1221setup: prefix: (null)
1222EOF
1223        test_repo 9
1224'
1225
1226test_expect_failure '#9: in subdir' '
1227        cat >9/sub/expected <<EOF &&
1228setup: git_dir: $TRASH_DIRECTORY/9.git
1229setup: worktree: $TRASH_DIRECTORY/9
1230setup: cwd: $TRASH_DIRECTORY/9
1231setup: prefix: sub/
1232EOF
1233        test_repo 9/sub
1234'
1235
1236#
1237# case #10
1238#
1239############################################################
1240#
1241# Input:
1242#
1243#  - GIT_WORK_TREE is not set
1244#  - GIT_DIR is set
1245#  - core.worktree is not set
1246#  - .git is a file
1247#  - core.bare is not set, cwd is outside .git
1248#
1249# Output:
1250#
1251# #2 except that git_dir is set by .git file
1252
1253test_expect_success '#10: setup' '
1254        unset GIT_DIR GIT_WORK_TREE &&
1255        mkdir 10 10/sub &&
1256        cd 10 &&
1257        git init &&
1258        mv .git ../10.git &&
1259        echo gitdir: ../10.git >.git &&
1260        cd ..
1261'
1262
1263test_expect_failure '#10: at root' '
1264        cat >10/expected <<EOF &&
1265setup: git_dir: $TRASH_DIRECTORY/10.git
1266setup: worktree: $TRASH_DIRECTORY/10
1267setup: cwd: $TRASH_DIRECTORY/10
1268setup: prefix: (null)
1269EOF
1270        test_repo 10 "$TRASH_DIRECTORY/10/.git"
1271'
1272
1273test_expect_failure '#10: in subdir' '
1274        cat >10/sub/expected <<EOF &&
1275setup: git_dir: $TRASH_DIRECTORY/10.git
1276setup: worktree: $TRASH_DIRECTORY/10/sub
1277setup: cwd: $TRASH_DIRECTORY/10/sub
1278setup: prefix: (null)
1279EOF
1280        test_repo 10/sub "$TRASH_DIRECTORY/10/.git"
1281'
1282
1283test_expect_failure '#10: relative GIT_DIR at root' '
1284        cat >10/expected <<EOF &&
1285setup: git_dir: $TRASH_DIRECTORY/10.git
1286setup: worktree: $TRASH_DIRECTORY/10
1287setup: cwd: $TRASH_DIRECTORY/10
1288setup: prefix: (null)
1289EOF
1290        test_repo 10 .git
1291'
1292
1293test_expect_failure '#10: relative GIT_DIR in subdir' '
1294        cat >10/sub/expected <<EOF &&
1295setup: git_dir: $TRASH_DIRECTORY/10.git
1296setup: worktree: $TRASH_DIRECTORY/10/sub
1297setup: cwd: $TRASH_DIRECTORY/10/sub
1298setup: prefix: (null)
1299EOF
1300        test_repo 10/sub ../.git
1301'
1302
1303#
1304# case #11
1305#
1306############################################################
1307#
1308# Input:
1309#
1310#  - GIT_WORK_TREE is set
1311#  - GIT_DIR is set
1312#  - core.worktree is not set
1313#  - .git is a file
1314#  - core.bare is not set, cwd is outside .git
1315#
1316# Output:
1317#
1318# #3 except that git_dir is set by .git file
1319
1320test_expect_success '#11: setup' '
1321        unset GIT_DIR GIT_WORK_TREE &&
1322        mkdir 11 11/sub 11/sub/sub 11.wt 11.wt/sub 11/wt 11/wt/sub &&
1323        cd 11 &&
1324        git init &&
1325        mv .git ../11.git &&
1326        echo gitdir: ../11.git >.git &&
1327        cd ..
1328'
1329
1330test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
1331        cat >11/expected <<EOF &&
1332setup: git_dir: $TRASH_DIRECTORY/11.git
1333setup: worktree: $TRASH_DIRECTORY/11
1334setup: cwd: $TRASH_DIRECTORY/11
1335setup: prefix: (null)
1336EOF
1337        test_repo 11 .git "$TRASH_DIRECTORY/11"
1338'
1339
1340test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
1341        cat >11/expected <<EOF &&
1342setup: git_dir: $TRASH_DIRECTORY/11.git
1343setup: worktree: $TRASH_DIRECTORY/11
1344setup: cwd: $TRASH_DIRECTORY/11
1345setup: prefix: (null)
1346EOF
1347        test_repo 11 .git .
1348'
1349
1350test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=root at root' '
1351        cat >11/expected <<EOF &&
1352setup: git_dir: $TRASH_DIRECTORY/11.git
1353setup: worktree: $TRASH_DIRECTORY/11
1354setup: cwd: $TRASH_DIRECTORY/11
1355setup: prefix: (null)
1356EOF
1357        test_repo 11 "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY/11"
1358'
1359
1360test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
1361        cat >11/expected <<EOF &&
1362setup: git_dir: $TRASH_DIRECTORY/11.git
1363setup: worktree: $TRASH_DIRECTORY/11
1364setup: cwd: $TRASH_DIRECTORY/11
1365setup: prefix: (null)
1366EOF
1367        test_repo 11 "$TRASH_DIRECTORY/11/.git" .
1368'
1369
1370test_expect_failure '#11: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
1371        cat >11/sub/sub/expected <<EOF &&
1372setup: git_dir: $TRASH_DIRECTORY/11.git
1373setup: worktree: $TRASH_DIRECTORY/11
1374setup: cwd: $TRASH_DIRECTORY/11
1375setup: prefix: sub/sub/
1376EOF
1377        test_repo 11/sub/sub ../../.git "$TRASH_DIRECTORY/11"
1378'
1379
1380test_expect_failure '#11: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
1381        cat >11/sub/sub/expected <<EOF &&
1382setup: git_dir: $TRASH_DIRECTORY/11.git
1383setup: worktree: $TRASH_DIRECTORY/11
1384setup: cwd: $TRASH_DIRECTORY/11
1385setup: prefix: sub/sub/
1386EOF
1387        test_repo 11/sub/sub ../../.git ../..
1388'
1389
1390test_expect_failure '#11: GIT_DIR, GIT_WORKTREE=root in subdir' '
1391        cat >11/sub/expected <<EOF &&
1392setup: git_dir: $TRASH_DIRECTORY/11.git
1393setup: worktree: $TRASH_DIRECTORY/11
1394setup: cwd: $TRASH_DIRECTORY/11
1395setup: prefix: sub/
1396EOF
1397        test_repo 11/sub "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY/11"
1398'
1399
1400test_expect_failure '#11: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
1401        cat >11/sub/sub/expected <<EOF &&
1402setup: git_dir: $TRASH_DIRECTORY/11.git
1403setup: worktree: $TRASH_DIRECTORY/11
1404setup: cwd: $TRASH_DIRECTORY/11
1405setup: prefix: sub/sub/
1406EOF
1407        test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" ../..
1408'
1409
1410test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
1411        cat >11/expected <<EOF &&
1412setup: git_dir: $TRASH_DIRECTORY/11.git
1413setup: worktree: $TRASH_DIRECTORY/11/wt
1414setup: cwd: $TRASH_DIRECTORY/11
1415setup: prefix: (null)
1416EOF
1417        test_repo 11 .git "$TRASH_DIRECTORY/11/wt"
1418'
1419
1420test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
1421        cat >11/expected <<EOF &&
1422setup: git_dir: $TRASH_DIRECTORY/11.git
1423setup: worktree: $TRASH_DIRECTORY/11/wt
1424setup: cwd: $TRASH_DIRECTORY/11
1425setup: prefix: (null)
1426EOF
1427        test_repo 11 .git wt
1428'
1429
1430test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
1431        cat >11/expected <<EOF &&
1432setup: git_dir: $TRASH_DIRECTORY/11.git
1433setup: worktree: $TRASH_DIRECTORY/11/wt
1434setup: cwd: $TRASH_DIRECTORY/11
1435setup: prefix: (null)
1436EOF
1437        test_repo 11 "$TRASH_DIRECTORY/11/.git" wt
1438'
1439
1440test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=wt at root' '
1441        cat >11/expected <<EOF &&
1442setup: git_dir: $TRASH_DIRECTORY/11.git
1443setup: worktree: $TRASH_DIRECTORY/11/wt
1444setup: cwd: $TRASH_DIRECTORY/11
1445setup: prefix: (null)
1446EOF
1447        test_repo 11 "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY/11/wt"
1448'
1449
1450test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
1451        cat >11/sub/sub/expected <<EOF &&
1452setup: git_dir: $TRASH_DIRECTORY/11.git
1453setup: worktree: $TRASH_DIRECTORY/11/wt
1454setup: cwd: $TRASH_DIRECTORY/11/sub/sub
1455setup: prefix: (null)
1456EOF
1457        test_repo 11/sub/sub ../../.git "$TRASH_DIRECTORY/11/wt"
1458'
1459
1460test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
1461        cat >11/sub/sub/expected <<EOF &&
1462setup: git_dir: $TRASH_DIRECTORY/11.git
1463setup: worktree: $TRASH_DIRECTORY/11/wt
1464setup: cwd: $TRASH_DIRECTORY/11/sub/sub
1465setup: prefix: (null)
1466EOF
1467        test_repo 11/sub/sub ../../.git ../../wt
1468'
1469
1470test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
1471        cat >11/sub/sub/expected <<EOF &&
1472setup: git_dir: $TRASH_DIRECTORY/11.git
1473setup: worktree: $TRASH_DIRECTORY/11/wt
1474setup: cwd: $TRASH_DIRECTORY/11/sub/sub
1475setup: prefix: (null)
1476EOF
1477        test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" ../../wt
1478'
1479
1480test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
1481        cat >11/sub/sub/expected <<EOF &&
1482setup: git_dir: $TRASH_DIRECTORY/11.git
1483setup: worktree: $TRASH_DIRECTORY/11/wt
1484setup: cwd: $TRASH_DIRECTORY/11/sub/sub
1485setup: prefix: (null)
1486EOF
1487        test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY/11/wt"
1488'
1489
1490test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
1491        cat >11/expected <<EOF &&
1492setup: git_dir: $TRASH_DIRECTORY/11.git
1493setup: worktree: $TRASH_DIRECTORY
1494setup: cwd: $TRASH_DIRECTORY
1495setup: prefix: 11/
1496EOF
1497        test_repo 11 .git "$TRASH_DIRECTORY"
1498'
1499
1500test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
1501        cat >11/expected <<EOF &&
1502setup: git_dir: $TRASH_DIRECTORY/11.git
1503setup: worktree: $TRASH_DIRECTORY
1504setup: cwd: $TRASH_DIRECTORY
1505setup: prefix: 11/
1506EOF
1507        test_repo 11 .git ..
1508'
1509
1510test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
1511        cat >11/expected <<EOF &&
1512setup: git_dir: $TRASH_DIRECTORY/11.git
1513setup: worktree: $TRASH_DIRECTORY
1514setup: cwd: $TRASH_DIRECTORY
1515setup: prefix: 11/
1516EOF
1517        test_repo 11 "$TRASH_DIRECTORY/11/.git" ..
1518'
1519
1520test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=.. at root' '
1521        cat >11/expected <<EOF &&
1522setup: git_dir: $TRASH_DIRECTORY/11.git
1523setup: worktree: $TRASH_DIRECTORY
1524setup: cwd: $TRASH_DIRECTORY
1525setup: prefix: 11/
1526EOF
1527        test_repo 11 "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY"
1528'
1529
1530test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
1531        cat >11/sub/sub/expected <<EOF &&
1532setup: git_dir: $TRASH_DIRECTORY/11.git
1533setup: worktree: $TRASH_DIRECTORY
1534setup: cwd: $TRASH_DIRECTORY
1535setup: prefix: 11/sub/sub/
1536EOF
1537        test_repo 11/sub/sub ../../.git "$TRASH_DIRECTORY"
1538'
1539
1540test_expect_failure '#11: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
1541        cat >11/sub/sub/expected <<EOF &&
1542setup: git_dir: $TRASH_DIRECTORY/11.git
1543setup: worktree: $TRASH_DIRECTORY
1544setup: cwd: $TRASH_DIRECTORY
1545setup: prefix: 11/sub/sub/
1546EOF
1547        test_repo 11/sub/sub ../../.git ../../..
1548'
1549
1550test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
1551        cat >11/sub/sub/expected <<EOF &&
1552setup: git_dir: $TRASH_DIRECTORY/11.git
1553setup: worktree: $TRASH_DIRECTORY
1554setup: cwd: $TRASH_DIRECTORY
1555setup: prefix: 11/sub/sub/
1556EOF
1557        test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" ../../../
1558'
1559
1560test_expect_failure '#11: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
1561        cat >11/sub/sub/expected <<EOF &&
1562setup: git_dir: $TRASH_DIRECTORY/11.git
1563setup: worktree: $TRASH_DIRECTORY
1564setup: cwd: $TRASH_DIRECTORY
1565setup: prefix: 11/sub/sub/
1566EOF
1567        test_repo 11/sub/sub "$TRASH_DIRECTORY/11/.git" "$TRASH_DIRECTORY"
1568'
1569
1570#
1571# case #12
1572#
1573############################################################
1574#
1575# Input:
1576#
1577#  - GIT_WORK_TREE is not set
1578#  - GIT_DIR is not set
1579#  - core.worktree is set
1580#  - .git is a file
1581#  - core.bare is not set, cwd is outside .git
1582#
1583# Output:
1584#
1585# #4 except that git_dir is set by .git file
1586
1587
1588test_expect_success '#12: setup' '
1589        unset GIT_DIR GIT_WORK_TREE &&
1590        mkdir 12 12/sub 12/sub/sub 12.wt 12.wt/sub 12/wt 12/wt/sub &&
1591        cd 12 &&
1592        git init &&
1593        git config core.worktree non-existent &&
1594        mv .git ../12.git &&
1595        echo gitdir: ../12.git >.git &&
1596        cd ..
1597'
1598
1599test_expect_failure '#12: at root' '
1600        cat >12/expected <<EOF &&
1601setup: git_dir: $TRASH_DIRECTORY/12.git
1602setup: worktree: $TRASH_DIRECTORY/12
1603setup: cwd: $TRASH_DIRECTORY/12
1604setup: prefix: (null)
1605EOF
1606        test_repo 12
1607'
1608
1609test_expect_failure '#12: in subdir' '
1610        cat >12/sub/expected <<EOF &&
1611setup: git_dir: $TRASH_DIRECTORY/12.git
1612setup: worktree: $TRASH_DIRECTORY/12
1613setup: cwd: $TRASH_DIRECTORY/12
1614setup: prefix: sub/
1615EOF
1616        test_repo 12/sub
1617'
1618
1619#
1620# case #13
1621#
1622############################################################
1623#
1624# Input:
1625#
1626#  - GIT_WORK_TREE is set
1627#  - GIT_DIR is not set
1628#  - core.worktree is set
1629#  - .git is a file
1630#  - core.bare is not set, cwd is outside .git
1631#
1632# Output:
1633#
1634# #5 except that git_dir is set by .git file
1635
1636test_expect_success '#13: setup' '
1637        unset GIT_DIR GIT_WORK_TREE &&
1638        mkdir 13 13/sub 13/sub/sub 13.wt 13.wt/sub 13/wt 13/wt/sub &&
1639        cd 13 &&
1640        git init &&
1641        git config core.worktree non-existent &&
1642        GIT_WORK_TREE=non-existent-too &&
1643        export GIT_WORK_TREE &&
1644        mv .git ../13.git &&
1645        echo gitdir: ../13.git >.git &&
1646        cd ..
1647'
1648
1649test_expect_failure '#13: at root' '
1650        cat >13/expected <<EOF &&
1651setup: git_dir: $TRASH_DIRECTORY/13.git
1652setup: worktree: $TRASH_DIRECTORY/13
1653setup: cwd: $TRASH_DIRECTORY/13
1654setup: prefix: (null)
1655EOF
1656        test_repo 13
1657'
1658
1659test_expect_failure '#13: in subdir' '
1660        cat >13/sub/expected <<EOF &&
1661setup: git_dir: $TRASH_DIRECTORY/13.git
1662setup: worktree: $TRASH_DIRECTORY/13
1663setup: cwd: $TRASH_DIRECTORY/13
1664setup: prefix: sub/
1665EOF
1666        test_repo 13/sub
1667'
1668
1669#
1670# case #14
1671#
1672############################################################
1673#
1674# Input:
1675#
1676#  - GIT_WORK_TREE is not set
1677#  - GIT_DIR is set
1678#  - core.worktree is set
1679#  - .git is a file
1680#  - core.bare is not set, cwd is outside .git
1681#
1682# Output:
1683#
1684# #6 except that git_dir is set by .git file
1685
1686test_expect_success '#14: setup' '
1687        unset GIT_DIR GIT_WORK_TREE &&
1688        mkdir 14 14/sub 14/sub/sub 14.wt 14.wt/sub 14/wt 14/wt/sub &&
1689        cd 14 &&
1690        git init &&
1691        mv .git ../14.git &&
1692        echo gitdir: ../14.git >.git &&
1693        cd ..
1694'
1695
1696test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14 at root' '
1697        cat >14/expected <<EOF &&
1698setup: git_dir: $TRASH_DIRECTORY/14.git
1699setup: worktree: $TRASH_DIRECTORY/14
1700setup: cwd: $TRASH_DIRECTORY/14
1701setup: prefix: (null)
1702EOF
1703        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14" &&
1704        test_repo 14 .git
1705'
1706
1707test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14(rel) at root' '
1708        cat >14/expected <<EOF &&
1709setup: git_dir: $TRASH_DIRECTORY/14.git
1710setup: worktree: $TRASH_DIRECTORY/14
1711setup: cwd: $TRASH_DIRECTORY/14
1712setup: prefix: (null)
1713EOF
1714        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14 &&
1715        test_repo 14 .git
1716'
1717
1718test_expect_failure '#14: GIT_DIR, core.worktree=../14 at root' '
1719        cat >14/expected <<EOF &&
1720setup: git_dir: $TRASH_DIRECTORY/14.git
1721setup: worktree: $TRASH_DIRECTORY/14
1722setup: cwd: $TRASH_DIRECTORY/14
1723setup: prefix: (null)
1724EOF
1725        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14" &&
1726        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1727'
1728
1729test_expect_failure '#14: GIT_DIR, core.worktree=../14(rel) at root' '
1730        cat >14/expected <<EOF &&
1731setup: git_dir: $TRASH_DIRECTORY/14.git
1732setup: worktree: $TRASH_DIRECTORY/14
1733setup: cwd: $TRASH_DIRECTORY/14
1734setup: prefix: (null)
1735EOF
1736        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14 &&
1737        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1738'
1739
1740test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14 in subdir' '
1741        cat >14/sub/sub/expected <<EOF &&
1742setup: git_dir: $TRASH_DIRECTORY/14.git
1743setup: worktree: $TRASH_DIRECTORY/14
1744setup: cwd: $TRASH_DIRECTORY/14
1745setup: prefix: sub/sub/
1746EOF
1747        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14" &&
1748        test_repo 14/sub/sub ../../.git
1749'
1750
1751test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14(rel) in subdir' '
1752        cat >14/sub/sub/expected <<EOF &&
1753setup: git_dir: $TRASH_DIRECTORY/14.git
1754setup: worktree: $TRASH_DIRECTORY/14
1755setup: cwd: $TRASH_DIRECTORY/14
1756setup: prefix: sub/sub/
1757EOF
1758        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14 &&
1759        test_repo 14/sub/sub ../../.git
1760'
1761
1762test_expect_failure '#14: GIT_DIR, core.worktree=../14 in subdir' '
1763        cat >14/sub/expected <<EOF &&
1764setup: git_dir: $TRASH_DIRECTORY/14.git
1765setup: worktree: $TRASH_DIRECTORY/14
1766setup: cwd: $TRASH_DIRECTORY/14
1767setup: prefix: sub/
1768EOF
1769        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14" &&
1770        test_repo 14/sub "$TRASH_DIRECTORY/14/.git"
1771'
1772
1773test_expect_failure '#14: GIT_DIR, core.worktree=../14(rel) in subdir' '
1774        cat >14/sub/sub/expected <<EOF &&
1775setup: git_dir: $TRASH_DIRECTORY/14.git
1776setup: worktree: $TRASH_DIRECTORY/14
1777setup: cwd: $TRASH_DIRECTORY/14
1778setup: prefix: sub/sub/
1779EOF
1780        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14 &&
1781        test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git"
1782'
1783
1784test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14/wt at root' '
1785        cat >14/expected <<EOF &&
1786setup: git_dir: $TRASH_DIRECTORY/14.git
1787setup: worktree: $TRASH_DIRECTORY/14/wt
1788setup: cwd: $TRASH_DIRECTORY/14
1789setup: prefix: (null)
1790EOF
1791        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14/wt" &&
1792        test_repo 14 .git
1793'
1794
1795test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14/wt(rel) at root' '
1796        cat >14/expected <<EOF &&
1797setup: git_dir: $TRASH_DIRECTORY/14.git
1798setup: worktree: $TRASH_DIRECTORY/14/wt
1799setup: cwd: $TRASH_DIRECTORY/14
1800setup: prefix: (null)
1801EOF
1802        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14/wt &&
1803        test_repo 14 .git
1804'
1805
1806test_expect_failure '#14: GIT_DIR, core.worktree=../14/wt(rel) at root' '
1807        cat >14/expected <<EOF &&
1808setup: git_dir: $TRASH_DIRECTORY/14.git
1809setup: worktree: $TRASH_DIRECTORY/14/wt
1810setup: cwd: $TRASH_DIRECTORY/14
1811setup: prefix: (null)
1812EOF
1813        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14/wt &&
1814        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1815'
1816
1817test_expect_failure '#14: GIT_DIR, core.worktree=../14/wt at root' '
1818        cat >14/expected <<EOF &&
1819setup: git_dir: $TRASH_DIRECTORY/14.git
1820setup: worktree: $TRASH_DIRECTORY/14/wt
1821setup: cwd: $TRASH_DIRECTORY/14
1822setup: prefix: (null)
1823EOF
1824        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14/wt" &&
1825        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1826'
1827
1828test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14/wt in subdir' '
1829        cat >14/sub/sub/expected <<EOF &&
1830setup: git_dir: $TRASH_DIRECTORY/14.git
1831setup: worktree: $TRASH_DIRECTORY/14/wt
1832setup: cwd: $TRASH_DIRECTORY/14/sub/sub
1833setup: prefix: (null)
1834EOF
1835        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14/wt" &&
1836        test_repo 14/sub/sub ../../.git
1837'
1838
1839test_expect_failure '#14: GIT_DIR(rel), core.worktree=../14/wt(rel) in subdir' '
1840        cat >14/sub/sub/expected <<EOF &&
1841setup: git_dir: $TRASH_DIRECTORY/14.git
1842setup: worktree: $TRASH_DIRECTORY/14/wt
1843setup: cwd: $TRASH_DIRECTORY/14/sub/sub
1844setup: prefix: (null)
1845EOF
1846        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14/wt &&
1847        test_repo 14/sub/sub ../../.git
1848'
1849
1850test_expect_failure '#14: GIT_DIR, core.worktree=../14/wt(rel) in subdir' '
1851        cat >14/sub/sub/expected <<EOF &&
1852setup: git_dir: $TRASH_DIRECTORY/14.git
1853setup: worktree: $TRASH_DIRECTORY/14/wt
1854setup: cwd: $TRASH_DIRECTORY/14/sub/sub
1855setup: prefix: (null)
1856EOF
1857        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree ../14/wt &&
1858        test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git"
1859'
1860
1861test_expect_failure '#14: GIT_DIR, core.worktree=../14/wt in subdir' '
1862        cat >14/sub/sub/expected <<EOF &&
1863setup: git_dir: $TRASH_DIRECTORY/14.git
1864setup: worktree: $TRASH_DIRECTORY/14/wt
1865setup: cwd: $TRASH_DIRECTORY/14/sub/sub
1866setup: prefix: (null)
1867EOF
1868        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY/14/wt" &&
1869        test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git"
1870'
1871
1872test_expect_failure '#14: GIT_DIR(rel), core.worktree=.. at root' '
1873        cat >14/expected <<EOF &&
1874setup: git_dir: $TRASH_DIRECTORY/14.git
1875setup: worktree: $TRASH_DIRECTORY
1876setup: cwd: $TRASH_DIRECTORY
1877setup: prefix: 14/
1878EOF
1879        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY" &&
1880        test_repo 14 .git
1881'
1882
1883test_expect_failure '#14: GIT_DIR(rel), core.worktree=..(rel) at root' '
1884        cat >14/expected <<EOF &&
1885setup: git_dir: $TRASH_DIRECTORY/14.git
1886setup: worktree: $TRASH_DIRECTORY
1887setup: cwd: $TRASH_DIRECTORY
1888setup: prefix: 14/
1889EOF
1890        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree .. &&
1891        test_repo 14 .git
1892'
1893
1894test_expect_failure '#14: GIT_DIR, core.worktree=..(rel) at root' '
1895        cat >14/expected <<EOF &&
1896setup: git_dir: $TRASH_DIRECTORY/14.git
1897setup: worktree: $TRASH_DIRECTORY
1898setup: cwd: $TRASH_DIRECTORY
1899setup: prefix: 14/
1900EOF
1901        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree .. &&
1902        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1903'
1904
1905test_expect_failure '#14: GIT_DIR, core.worktree=.. at root' '
1906        cat >14/expected <<EOF &&
1907setup: git_dir: $TRASH_DIRECTORY/14.git
1908setup: worktree: $TRASH_DIRECTORY
1909setup: cwd: $TRASH_DIRECTORY
1910setup: prefix: 14/
1911EOF
1912        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY" &&
1913        test_repo 14 "$TRASH_DIRECTORY/14/.git"
1914'
1915
1916test_expect_failure '#14: GIT_DIR(rel), core.worktree=.. in subdir' '
1917        cat >14/sub/sub/expected <<EOF &&
1918setup: git_dir: $TRASH_DIRECTORY/14.git
1919setup: worktree: $TRASH_DIRECTORY
1920setup: cwd: $TRASH_DIRECTORY
1921setup: prefix: 14/sub/sub/
1922EOF
1923        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY" &&
1924        test_repo 14/sub/sub ../../.git
1925'
1926
1927test_expect_failure '#14: GIT_DIR(rel), core.worktree=..(rel) in subdir' '
1928        cat >14/sub/sub/expected <<EOF &&
1929setup: git_dir: $TRASH_DIRECTORY/14.git
1930setup: worktree: $TRASH_DIRECTORY
1931setup: cwd: $TRASH_DIRECTORY
1932setup: prefix: 14/sub/sub/
1933EOF
1934        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree .. &&
1935        test_repo 14/sub/sub ../../.git
1936'
1937
1938test_expect_failure '#14: GIT_DIR, core.worktree=..(rel) in subdir' '
1939        cat >14/sub/sub/expected <<EOF &&
1940setup: git_dir: $TRASH_DIRECTORY/14.git
1941setup: worktree: $TRASH_DIRECTORY
1942setup: cwd: $TRASH_DIRECTORY
1943setup: prefix: 14/sub/sub/
1944EOF
1945        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree .. &&
1946        test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git"
1947'
1948
1949test_expect_failure '#14: GIT_DIR, core.worktree=.. in subdir' '
1950        cat >14/sub/sub/expected <<EOF &&
1951setup: git_dir: $TRASH_DIRECTORY/14.git
1952setup: worktree: $TRASH_DIRECTORY
1953setup: cwd: $TRASH_DIRECTORY
1954setup: prefix: 14/sub/sub/
1955EOF
1956        git config --file="$TRASH_DIRECTORY/14.git/config" core.worktree "$TRASH_DIRECTORY" &&
1957        test_repo 14/sub/sub "$TRASH_DIRECTORY/14/.git"
1958'
1959
1960#
1961# case #15
1962#
1963############################################################
1964#
1965# Input:
1966#
1967#  - GIT_WORK_TREE is set
1968#  - GIT_DIR is set
1969#  - core.worktree is set
1970#  - .git is a file
1971#  - core.bare is not set, cwd is outside .git
1972#
1973# Output:
1974#
1975# #7 except that git_dir is set by .git file
1976
1977test_expect_success '#15: setup' '
1978        unset GIT_DIR GIT_WORK_TREE &&
1979        mkdir 15 15/sub 15/sub/sub 15.wt 15.wt/sub 15/wt 15/wt/sub &&
1980        cd 15 &&
1981        git init &&
1982        git config core.worktree non-existent &&
1983        mv .git ../15.git &&
1984        echo gitdir: ../15.git >.git &&
1985        cd ..
1986'
1987
1988test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
1989        cat >15/expected <<EOF &&
1990setup: git_dir: $TRASH_DIRECTORY/15.git
1991setup: worktree: $TRASH_DIRECTORY/15
1992setup: cwd: $TRASH_DIRECTORY/15
1993setup: prefix: (null)
1994EOF
1995        test_repo 15 .git "$TRASH_DIRECTORY/15"
1996'
1997
1998test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
1999        cat >15/expected <<EOF &&
2000setup: git_dir: $TRASH_DIRECTORY/15.git
2001setup: worktree: $TRASH_DIRECTORY/15
2002setup: cwd: $TRASH_DIRECTORY/15
2003setup: prefix: (null)
2004EOF
2005        test_repo 15 .git .
2006'
2007
2008test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=root at root' '
2009        cat >15/expected <<EOF &&
2010setup: git_dir: $TRASH_DIRECTORY/15.git
2011setup: worktree: $TRASH_DIRECTORY/15
2012setup: cwd: $TRASH_DIRECTORY/15
2013setup: prefix: (null)
2014EOF
2015        test_repo 15 "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY/15"
2016'
2017
2018test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
2019        cat >15/expected <<EOF &&
2020setup: git_dir: $TRASH_DIRECTORY/15.git
2021setup: worktree: $TRASH_DIRECTORY/15
2022setup: cwd: $TRASH_DIRECTORY/15
2023setup: prefix: (null)
2024EOF
2025        test_repo 15 "$TRASH_DIRECTORY/15/.git" .
2026'
2027
2028test_expect_failure '#15: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
2029        cat >15/sub/sub/expected <<EOF &&
2030setup: git_dir: $TRASH_DIRECTORY/15.git
2031setup: worktree: $TRASH_DIRECTORY/15
2032setup: cwd: $TRASH_DIRECTORY/15
2033setup: prefix: sub/sub/
2034EOF
2035        test_repo 15/sub/sub ../../.git "$TRASH_DIRECTORY/15"
2036'
2037
2038test_expect_failure '#15: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
2039        cat >15/sub/sub/expected <<EOF &&
2040setup: git_dir: $TRASH_DIRECTORY/15.git
2041setup: worktree: $TRASH_DIRECTORY/15
2042setup: cwd: $TRASH_DIRECTORY/15
2043setup: prefix: sub/sub/
2044EOF
2045        test_repo 15/sub/sub ../../.git ../..
2046'
2047
2048test_expect_failure '#15: GIT_DIR, GIT_WORKTREE=root in subdir' '
2049        cat >15/sub/expected <<EOF &&
2050setup: git_dir: $TRASH_DIRECTORY/15.git
2051setup: worktree: $TRASH_DIRECTORY/15
2052setup: cwd: $TRASH_DIRECTORY/15
2053setup: prefix: sub/
2054EOF
2055        test_repo 15/sub "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY/15"
2056'
2057
2058test_expect_failure '#15: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
2059        cat >15/sub/sub/expected <<EOF &&
2060setup: git_dir: $TRASH_DIRECTORY/15.git
2061setup: worktree: $TRASH_DIRECTORY/15
2062setup: cwd: $TRASH_DIRECTORY/15
2063setup: prefix: sub/sub/
2064EOF
2065        test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" ../..
2066'
2067
2068test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
2069        cat >15/expected <<EOF &&
2070setup: git_dir: $TRASH_DIRECTORY/15.git
2071setup: worktree: $TRASH_DIRECTORY/15/wt
2072setup: cwd: $TRASH_DIRECTORY/15
2073setup: prefix: (null)
2074EOF
2075        test_repo 15 .git "$TRASH_DIRECTORY/15/wt"
2076'
2077
2078test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
2079        cat >15/expected <<EOF &&
2080setup: git_dir: $TRASH_DIRECTORY/15.git
2081setup: worktree: $TRASH_DIRECTORY/15/wt
2082setup: cwd: $TRASH_DIRECTORY/15
2083setup: prefix: (null)
2084EOF
2085        test_repo 15 .git wt
2086'
2087
2088test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
2089        cat >15/expected <<EOF &&
2090setup: git_dir: $TRASH_DIRECTORY/15.git
2091setup: worktree: $TRASH_DIRECTORY/15/wt
2092setup: cwd: $TRASH_DIRECTORY/15
2093setup: prefix: (null)
2094EOF
2095        test_repo 15 "$TRASH_DIRECTORY/15/.git" wt
2096'
2097
2098test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=wt at root' '
2099        cat >15/expected <<EOF &&
2100setup: git_dir: $TRASH_DIRECTORY/15.git
2101setup: worktree: $TRASH_DIRECTORY/15/wt
2102setup: cwd: $TRASH_DIRECTORY/15
2103setup: prefix: (null)
2104EOF
2105        test_repo 15 "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY/15/wt"
2106'
2107
2108test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
2109        cat >15/sub/sub/expected <<EOF &&
2110setup: git_dir: $TRASH_DIRECTORY/15.git
2111setup: worktree: $TRASH_DIRECTORY/15/wt
2112setup: cwd: $TRASH_DIRECTORY/15/sub/sub
2113setup: prefix: (null)
2114EOF
2115        test_repo 15/sub/sub ../../.git "$TRASH_DIRECTORY/15/wt"
2116'
2117
2118test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
2119        cat >15/sub/sub/expected <<EOF &&
2120setup: git_dir: $TRASH_DIRECTORY/15.git
2121setup: worktree: $TRASH_DIRECTORY/15/wt
2122setup: cwd: $TRASH_DIRECTORY/15/sub/sub
2123setup: prefix: (null)
2124EOF
2125        test_repo 15/sub/sub ../../.git ../../wt
2126'
2127
2128test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
2129        cat >15/sub/sub/expected <<EOF &&
2130setup: git_dir: $TRASH_DIRECTORY/15.git
2131setup: worktree: $TRASH_DIRECTORY/15/wt
2132setup: cwd: $TRASH_DIRECTORY/15/sub/sub
2133setup: prefix: (null)
2134EOF
2135        test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" ../../wt
2136'
2137
2138test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
2139        cat >15/sub/sub/expected <<EOF &&
2140setup: git_dir: $TRASH_DIRECTORY/15.git
2141setup: worktree: $TRASH_DIRECTORY/15/wt
2142setup: cwd: $TRASH_DIRECTORY/15/sub/sub
2143setup: prefix: (null)
2144EOF
2145        test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY/15/wt"
2146'
2147
2148test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
2149        cat >15/expected <<EOF &&
2150setup: git_dir: $TRASH_DIRECTORY/15.git
2151setup: worktree: $TRASH_DIRECTORY
2152setup: cwd: $TRASH_DIRECTORY
2153setup: prefix: 15/
2154EOF
2155        test_repo 15 .git "$TRASH_DIRECTORY"
2156'
2157
2158test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
2159        cat >15/expected <<EOF &&
2160setup: git_dir: $TRASH_DIRECTORY/15.git
2161setup: worktree: $TRASH_DIRECTORY
2162setup: cwd: $TRASH_DIRECTORY
2163setup: prefix: 15/
2164EOF
2165        test_repo 15 .git ..
2166'
2167
2168test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
2169        cat >15/expected <<EOF &&
2170setup: git_dir: $TRASH_DIRECTORY/15.git
2171setup: worktree: $TRASH_DIRECTORY
2172setup: cwd: $TRASH_DIRECTORY
2173setup: prefix: 15/
2174EOF
2175        test_repo 15 "$TRASH_DIRECTORY/15/.git" ..
2176'
2177
2178test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=.. at root' '
2179        cat >15/expected <<EOF &&
2180setup: git_dir: $TRASH_DIRECTORY/15.git
2181setup: worktree: $TRASH_DIRECTORY
2182setup: cwd: $TRASH_DIRECTORY
2183setup: prefix: 15/
2184EOF
2185        test_repo 15 "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY"
2186'
2187
2188test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
2189        cat >15/sub/sub/expected <<EOF &&
2190setup: git_dir: $TRASH_DIRECTORY/15.git
2191setup: worktree: $TRASH_DIRECTORY
2192setup: cwd: $TRASH_DIRECTORY
2193setup: prefix: 15/sub/sub/
2194EOF
2195        test_repo 15/sub/sub ../../.git "$TRASH_DIRECTORY"
2196'
2197
2198test_expect_failure '#15: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
2199        cat >15/sub/sub/expected <<EOF &&
2200setup: git_dir: $TRASH_DIRECTORY/15.git
2201setup: worktree: $TRASH_DIRECTORY
2202setup: cwd: $TRASH_DIRECTORY
2203setup: prefix: 15/sub/sub/
2204EOF
2205        test_repo 15/sub/sub ../../.git ../../..
2206'
2207
2208test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
2209        cat >15/sub/sub/expected <<EOF &&
2210setup: git_dir: $TRASH_DIRECTORY/15.git
2211setup: worktree: $TRASH_DIRECTORY
2212setup: cwd: $TRASH_DIRECTORY
2213setup: prefix: 15/sub/sub/
2214EOF
2215        test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" ../../../
2216'
2217
2218test_expect_failure '#15: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
2219        cat >15/sub/sub/expected <<EOF &&
2220setup: git_dir: $TRASH_DIRECTORY/15.git
2221setup: worktree: $TRASH_DIRECTORY
2222setup: cwd: $TRASH_DIRECTORY
2223setup: prefix: 15/sub/sub/
2224EOF
2225        test_repo 15/sub/sub "$TRASH_DIRECTORY/15/.git" "$TRASH_DIRECTORY"
2226'
2227
2228#
2229# case #16.1
2230#
2231############################################################
2232#
2233# Input:
2234#
2235#  - GIT_WORK_TREE is not set
2236#  - GIT_DIR is not set
2237#  - core.worktree is not set
2238#  - .git is a directory
2239#  - cwd is inside .git
2240#
2241# Output:
2242#
2243#  - no worktree
2244#  - cwd is unchanged
2245#  - prefix is NULL
2246#  - git_dir is set
2247#  - cwd can't be outside worktree
2248
2249test_expect_success '#16.1: setup' '
2250        unset GIT_DIR GIT_WORK_TREE &&
2251        mkdir 16 16/sub &&
2252        cd 16 &&
2253        git init &&
2254        mkdir .git/wt .git/wt/sub &&
2255        cd ..
2256'
2257
2258test_expect_success '#16.1: at .git' '
2259        cat >16/.git/expected <<EOF &&
2260setup: git_dir: .
2261setup: worktree: (null)
2262setup: cwd: $TRASH_DIRECTORY/16/.git
2263setup: prefix: (null)
2264EOF
2265        test_repo 16/.git
2266'
2267
2268test_expect_success '#16.1: in .git/wt' '
2269        cat >16/.git/wt/expected <<EOF &&
2270setup: git_dir: $TRASH_DIRECTORY/16/.git
2271setup: worktree: (null)
2272setup: cwd: $TRASH_DIRECTORY/16/.git/wt
2273setup: prefix: (null)
2274EOF
2275        test_repo 16/.git/wt
2276'
2277
2278test_expect_success '#16.1: in .git/wt/sub' '
2279        cat >16/.git/wt/sub/expected <<EOF &&
2280setup: git_dir: $TRASH_DIRECTORY/16/.git
2281setup: worktree: (null)
2282setup: cwd: $TRASH_DIRECTORY/16/.git/wt/sub
2283setup: prefix: (null)
2284EOF
2285        test_repo 16/.git/wt/sub
2286'
2287
2288#
2289# case #16.2
2290#
2291############################################################
2292#
2293# Input:
2294#
2295#  - GIT_WORK_TREE is not set
2296#  - GIT_DIR is not set
2297#  - core.worktree is not set
2298#  - .git is a directory
2299#  - core.bare is set
2300#
2301# Output:
2302#
2303#  - no worktree
2304#  - cwd is unchanged
2305#  - prefix is NULL
2306#  - git_dir is set
2307#  - cwd can't be outside worktree
2308
2309test_expect_success '#16.2: setup' '
2310        git config --file="$TRASH_DIRECTORY/16/.git/config" core.bare true
2311'
2312
2313test_expect_success '#16.2: at .git' '
2314        cat >16/.git/expected <<EOF &&
2315setup: git_dir: .
2316setup: worktree: (null)
2317setup: cwd: $TRASH_DIRECTORY/16/.git
2318setup: prefix: (null)
2319EOF
2320        test_repo 16/.git
2321'
2322
2323test_expect_success '#16.2: in .git/wt' '
2324        cat >16/.git/wt/expected <<EOF &&
2325setup: git_dir: $TRASH_DIRECTORY/16/.git
2326setup: worktree: (null)
2327setup: cwd: $TRASH_DIRECTORY/16/.git/wt
2328setup: prefix: (null)
2329EOF
2330        test_repo 16/.git/wt
2331'
2332
2333test_expect_success '#16.2: in .git/wt/sub' '
2334        cat >16/.git/wt/sub/expected <<EOF &&
2335setup: git_dir: $TRASH_DIRECTORY/16/.git
2336setup: worktree: (null)
2337setup: cwd: $TRASH_DIRECTORY/16/.git/wt/sub
2338setup: prefix: (null)
2339EOF
2340        test_repo 16/.git/wt/sub
2341'
2342
2343test_expect_success '#16.2: at root' '
2344        cat >16/expected <<EOF &&
2345setup: git_dir: .git
2346setup: worktree: (null)
2347setup: cwd: $TRASH_DIRECTORY/16
2348setup: prefix: (null)
2349EOF
2350        test_repo 16
2351'
2352
2353test_expect_failure '#16.2: in subdir' '
2354        cat >16/sub/expected <<EOF &&
2355setup: git_dir: $TRASH_DIRECTORY/16/.git
2356setup: worktree: (null)
2357setup: cwd: $TRASH_DIRECTORY/16/sub
2358setup: prefix: (null)
2359EOF
2360        test_repo 16/sub
2361'
2362
2363#
2364# case #17.1
2365#
2366############################################################
2367#
2368# Input:
2369#
2370#  - GIT_WORK_TREE is set
2371#  - GIT_DIR is not set
2372#  - core.worktree is not set
2373#  - .git is a directory
2374#  - cwd is inside .git
2375#
2376# Output:
2377#
2378# GIT_WORK_TREE is ignored -> #16.1 (with warnings perhaps)
2379
2380test_expect_success '#17.1: setup' '
2381        unset GIT_DIR GIT_WORK_TREE &&
2382        mkdir 17 17/sub &&
2383        cd 17 &&
2384        git init &&
2385        mkdir .git/wt .git/wt/sub &&
2386        GIT_WORK_TREE=non-existent &&
2387        export GIT_WORK_TREE &&
2388        cd ..
2389'
2390
2391test_expect_failure '#17.1: at .git' '
2392        cat >17/.git/expected <<EOF &&
2393setup: git_dir: .
2394setup: worktree: (null)
2395setup: cwd: $TRASH_DIRECTORY/17/.git
2396setup: prefix: (null)
2397EOF
2398        test_repo 17/.git
2399'
2400
2401test_expect_failure '#17.1: in .git/wt' '
2402        cat >17/.git/wt/expected <<EOF &&
2403setup: git_dir: $TRASH_DIRECTORY/17/.git
2404setup: worktree: (null)
2405setup: cwd: $TRASH_DIRECTORY/17/.git/wt
2406setup: prefix: (null)
2407EOF
2408        test_repo 17/.git/wt
2409'
2410
2411test_expect_failure '#17.1: in .git/wt/sub' '
2412        cat >17/.git/wt/sub/expected <<EOF &&
2413setup: git_dir: $TRASH_DIRECTORY/17/.git
2414setup: worktree: (null)
2415setup: cwd: $TRASH_DIRECTORY/17/.git/wt/sub
2416setup: prefix: (null)
2417EOF
2418        test_repo 17/.git/wt/sub
2419'
2420
2421#
2422# case #17.2
2423#
2424############################################################
2425#
2426# Input:
2427#
2428#  - GIT_WORK_TREE is set
2429#  - GIT_DIR is not set
2430#  - core.worktree is not set
2431#  - .git is a directory
2432#  - core.bare is set
2433#
2434# Output:
2435#
2436# GIT_WORK_TREE is ignored -> #16.2 (with warnings perhaps)
2437
2438test_expect_success '#17.2: setup' '
2439        git config --file="$TRASH_DIRECTORY/17/.git/config" core.bare true
2440'
2441
2442test_expect_failure '#17.2: at .git' '
2443        cat >17/.git/expected <<EOF &&
2444setup: git_dir: .
2445setup: worktree: (null)
2446setup: cwd: $TRASH_DIRECTORY/17/.git
2447setup: prefix: (null)
2448EOF
2449        test_repo 17/.git
2450'
2451
2452test_expect_failure '#17.2: in .git/wt' '
2453        cat >17/.git/wt/expected <<EOF &&
2454setup: git_dir: $TRASH_DIRECTORY/17/.git
2455setup: worktree: (null)
2456setup: cwd: $TRASH_DIRECTORY/17/.git/wt
2457setup: prefix: (null)
2458EOF
2459        test_repo 17/.git/wt
2460'
2461
2462test_expect_failure '#17.2: in .git/wt/sub' '
2463        cat >17/.git/wt/sub/expected <<EOF &&
2464setup: git_dir: $TRASH_DIRECTORY/17/.git
2465setup: worktree: (null)
2466setup: cwd: $TRASH_DIRECTORY/17/.git/wt/sub
2467setup: prefix: (null)
2468EOF
2469        test_repo 17/.git/wt/sub
2470'
2471
2472test_expect_failure '#17.2: at root' '
2473        cat >17/expected <<EOF &&
2474setup: git_dir: .git
2475setup: worktree: (null)
2476setup: cwd: $TRASH_DIRECTORY/17
2477setup: prefix: (null)
2478EOF
2479        test_repo 17
2480'
2481
2482test_expect_failure '#17.2: in subdir' '
2483        cat >17/sub/expected <<EOF &&
2484setup: git_dir: $TRASH_DIRECTORY/17/.git
2485setup: worktree: (null)
2486setup: cwd: $TRASH_DIRECTORY/17/sub
2487setup: prefix: (null)
2488EOF
2489        test_repo 17/sub
2490'
2491
2492#
2493# case #18
2494#
2495############################################################
2496#
2497# Input:
2498#
2499#  - GIT_WORK_TREE is not set
2500#  - GIT_DIR is set
2501#  - core.worktree is not set
2502#  - .git is a directory
2503#  - core.bare is set
2504#
2505# Output:
2506#
2507#  - no worktree (rule #8)
2508#  - cwd is unchanged
2509#  - prefix is NULL
2510#  - git_dir is set to $GIT_DIR
2511#  - cwd can't be outside worktree
2512
2513test_expect_success '#18: setup' '
2514        unset GIT_DIR GIT_WORK_TREE &&
2515        mkdir 18 18/sub &&
2516        cd 18 &&
2517        git init &&
2518        mkdir .git/wt .git/wt/sub &&
2519        git config core.bare true &&
2520        cd ..
2521'
2522
2523test_expect_success '#18: (rel) at root' '
2524        cat >18/expected <<EOF &&
2525setup: git_dir: .git
2526setup: worktree: (null)
2527setup: cwd: $TRASH_DIRECTORY/18
2528setup: prefix: (null)
2529EOF
2530         test_repo 18 .git
2531'
2532
2533test_expect_success '#18: at root' '
2534        cat >18/expected <<EOF &&
2535setup: git_dir: $TRASH_DIRECTORY/18/.git
2536setup: worktree: (null)
2537setup: cwd: $TRASH_DIRECTORY/18
2538setup: prefix: (null)
2539EOF
2540         test_repo 18 "$TRASH_DIRECTORY/18/.git"
2541'
2542
2543test_expect_success '#18: (rel) in subdir' '
2544        cat >18/sub/expected <<EOF &&
2545setup: git_dir: ../.git
2546setup: worktree: (null)
2547setup: cwd: $TRASH_DIRECTORY/18/sub
2548setup: prefix: (null)
2549EOF
2550        test_repo 18/sub ../.git
2551'
2552
2553test_expect_success '#18: in subdir' '
2554        cat >18/sub/expected <<EOF &&
2555setup: git_dir: $TRASH_DIRECTORY/18/.git
2556setup: worktree: (null)
2557setup: cwd: $TRASH_DIRECTORY/18/sub
2558setup: prefix: (null)
2559EOF
2560        test_repo 18/sub "$TRASH_DIRECTORY/18/.git"
2561'
2562
2563#
2564# case #19
2565#
2566############################################################
2567#
2568# Input:
2569#
2570#  - GIT_WORK_TREE is set
2571#  - GIT_DIR is set
2572#  - .git is a directory
2573#  - core.worktree is not set
2574#  - core.bare is set
2575#
2576# Output:
2577#
2578# bare repo is overridden by GIT_WORK_TREE -> #3
2579
2580test_expect_success '#19: setup' '
2581        unset GIT_DIR GIT_WORK_TREE &&
2582        mkdir 19 19/sub 19/sub/sub 19.wt 19.wt/sub 19/wt 19/wt/sub &&
2583        cd 19 &&
2584        git init &&
2585        git config core.bare true &&
2586        cd ..
2587'
2588
2589test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
2590        cat >19/expected <<EOF &&
2591setup: git_dir: .git
2592setup: worktree: $TRASH_DIRECTORY/19
2593setup: cwd: $TRASH_DIRECTORY/19
2594setup: prefix: (null)
2595EOF
2596        test_repo 19 .git "$TRASH_DIRECTORY/19"
2597'
2598
2599test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
2600        cat >19/expected <<EOF &&
2601setup: git_dir: .git
2602setup: worktree: $TRASH_DIRECTORY/19
2603setup: cwd: $TRASH_DIRECTORY/19
2604setup: prefix: (null)
2605EOF
2606        test_repo 19 .git .
2607'
2608
2609test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=root at root' '
2610        cat >19/expected <<EOF &&
2611setup: git_dir: $TRASH_DIRECTORY/19/.git
2612setup: worktree: $TRASH_DIRECTORY/19
2613setup: cwd: $TRASH_DIRECTORY/19
2614setup: prefix: (null)
2615EOF
2616        test_repo 19 "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY/19"
2617'
2618
2619test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
2620        cat >19/expected <<EOF &&
2621setup: git_dir: $TRASH_DIRECTORY/19/.git
2622setup: worktree: $TRASH_DIRECTORY/19
2623setup: cwd: $TRASH_DIRECTORY/19
2624setup: prefix: (null)
2625EOF
2626        test_repo 19 "$TRASH_DIRECTORY/19/.git" .
2627'
2628
2629test_expect_success '#19: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
2630        cat >19/sub/sub/expected <<EOF &&
2631setup: git_dir: $TRASH_DIRECTORY/19/.git
2632setup: worktree: $TRASH_DIRECTORY/19
2633setup: cwd: $TRASH_DIRECTORY/19
2634setup: prefix: sub/sub/
2635EOF
2636        test_repo 19/sub/sub ../../.git "$TRASH_DIRECTORY/19"
2637'
2638
2639test_expect_success '#19: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
2640        cat >19/sub/sub/expected <<EOF &&
2641setup: git_dir: $TRASH_DIRECTORY/19/.git
2642setup: worktree: $TRASH_DIRECTORY/19
2643setup: cwd: $TRASH_DIRECTORY/19
2644setup: prefix: sub/sub/
2645EOF
2646        test_repo 19/sub/sub ../../.git ../..
2647'
2648
2649test_expect_success '#19: GIT_DIR, GIT_WORKTREE=root in subdir' '
2650        cat >19/sub/expected <<EOF &&
2651setup: git_dir: $TRASH_DIRECTORY/19/.git
2652setup: worktree: $TRASH_DIRECTORY/19
2653setup: cwd: $TRASH_DIRECTORY/19
2654setup: prefix: sub/
2655EOF
2656        test_repo 19/sub "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY/19"
2657'
2658
2659test_expect_success '#19: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
2660        cat >19/sub/sub/expected <<EOF &&
2661setup: git_dir: $TRASH_DIRECTORY/19/.git
2662setup: worktree: $TRASH_DIRECTORY/19
2663setup: cwd: $TRASH_DIRECTORY/19
2664setup: prefix: sub/sub/
2665EOF
2666        test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" ../..
2667'
2668
2669test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
2670        cat >19/expected <<EOF &&
2671setup: git_dir: .git
2672setup: worktree: $TRASH_DIRECTORY/19/wt
2673setup: cwd: $TRASH_DIRECTORY/19
2674setup: prefix: (null)
2675EOF
2676        test_repo 19 .git "$TRASH_DIRECTORY/19/wt"
2677'
2678
2679test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
2680        cat >19/expected <<EOF &&
2681setup: git_dir: .git
2682setup: worktree: $TRASH_DIRECTORY/19/wt
2683setup: cwd: $TRASH_DIRECTORY/19
2684setup: prefix: (null)
2685EOF
2686        test_repo 19 .git wt
2687'
2688
2689test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
2690        cat >19/expected <<EOF &&
2691setup: git_dir: $TRASH_DIRECTORY/19/.git
2692setup: worktree: $TRASH_DIRECTORY/19/wt
2693setup: cwd: $TRASH_DIRECTORY/19
2694setup: prefix: (null)
2695EOF
2696        test_repo 19 "$TRASH_DIRECTORY/19/.git" wt
2697'
2698
2699test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=wt at root' '
2700        cat >19/expected <<EOF &&
2701setup: git_dir: $TRASH_DIRECTORY/19/.git
2702setup: worktree: $TRASH_DIRECTORY/19/wt
2703setup: cwd: $TRASH_DIRECTORY/19
2704setup: prefix: (null)
2705EOF
2706        test_repo 19 "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY/19/wt"
2707'
2708
2709test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
2710        cat >19/sub/sub/expected <<EOF &&
2711setup: git_dir: ../../.git
2712setup: worktree: $TRASH_DIRECTORY/19/wt
2713setup: cwd: $TRASH_DIRECTORY/19/sub/sub
2714setup: prefix: (null)
2715EOF
2716        test_repo 19/sub/sub ../../.git "$TRASH_DIRECTORY/19/wt"
2717'
2718
2719test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
2720        cat >19/sub/sub/expected <<EOF &&
2721setup: git_dir: ../../.git
2722setup: worktree: $TRASH_DIRECTORY/19/wt
2723setup: cwd: $TRASH_DIRECTORY/19/sub/sub
2724setup: prefix: (null)
2725EOF
2726        test_repo 19/sub/sub ../../.git ../../wt
2727'
2728
2729test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
2730        cat >19/sub/sub/expected <<EOF &&
2731setup: git_dir: $TRASH_DIRECTORY/19/.git
2732setup: worktree: $TRASH_DIRECTORY/19/wt
2733setup: cwd: $TRASH_DIRECTORY/19/sub/sub
2734setup: prefix: (null)
2735EOF
2736        test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" ../../wt
2737'
2738
2739test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
2740        cat >19/sub/sub/expected <<EOF &&
2741setup: git_dir: $TRASH_DIRECTORY/19/.git
2742setup: worktree: $TRASH_DIRECTORY/19/wt
2743setup: cwd: $TRASH_DIRECTORY/19/sub/sub
2744setup: prefix: (null)
2745EOF
2746        test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY/19/wt"
2747'
2748
2749test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
2750        cat >19/expected <<EOF &&
2751setup: git_dir: $TRASH_DIRECTORY/19/.git
2752setup: worktree: $TRASH_DIRECTORY
2753setup: cwd: $TRASH_DIRECTORY
2754setup: prefix: 19/
2755EOF
2756        test_repo 19 .git "$TRASH_DIRECTORY"
2757'
2758
2759test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
2760        cat >19/expected <<EOF &&
2761setup: git_dir: $TRASH_DIRECTORY/19/.git
2762setup: worktree: $TRASH_DIRECTORY
2763setup: cwd: $TRASH_DIRECTORY
2764setup: prefix: 19/
2765EOF
2766        test_repo 19 .git ..
2767'
2768
2769test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
2770        cat >19/expected <<EOF &&
2771setup: git_dir: $TRASH_DIRECTORY/19/.git
2772setup: worktree: $TRASH_DIRECTORY
2773setup: cwd: $TRASH_DIRECTORY
2774setup: prefix: 19/
2775EOF
2776        test_repo 19 "$TRASH_DIRECTORY/19/.git" ..
2777'
2778
2779test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=.. at root' '
2780        cat >19/expected <<EOF &&
2781setup: git_dir: $TRASH_DIRECTORY/19/.git
2782setup: worktree: $TRASH_DIRECTORY
2783setup: cwd: $TRASH_DIRECTORY
2784setup: prefix: 19/
2785EOF
2786        test_repo 19 "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY"
2787'
2788
2789test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
2790        cat >19/sub/sub/expected <<EOF &&
2791setup: git_dir: $TRASH_DIRECTORY/19/.git
2792setup: worktree: $TRASH_DIRECTORY
2793setup: cwd: $TRASH_DIRECTORY
2794setup: prefix: 19/sub/sub/
2795EOF
2796        test_repo 19/sub/sub ../../.git "$TRASH_DIRECTORY"
2797'
2798
2799test_expect_success '#19: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
2800        cat >19/sub/sub/expected <<EOF &&
2801setup: git_dir: $TRASH_DIRECTORY/19/.git
2802setup: worktree: $TRASH_DIRECTORY
2803setup: cwd: $TRASH_DIRECTORY
2804setup: prefix: 19/sub/sub/
2805EOF
2806        test_repo 19/sub/sub ../../.git ../../..
2807'
2808
2809test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
2810        cat >19/sub/sub/expected <<EOF &&
2811setup: git_dir: $TRASH_DIRECTORY/19/.git
2812setup: worktree: $TRASH_DIRECTORY
2813setup: cwd: $TRASH_DIRECTORY
2814setup: prefix: 19/sub/sub/
2815EOF
2816        test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" ../../../
2817'
2818
2819test_expect_success '#19: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
2820        cat >19/sub/sub/expected <<EOF &&
2821setup: git_dir: $TRASH_DIRECTORY/19/.git
2822setup: worktree: $TRASH_DIRECTORY
2823setup: cwd: $TRASH_DIRECTORY
2824setup: prefix: 19/sub/sub/
2825EOF
2826        test_repo 19/sub/sub "$TRASH_DIRECTORY/19/.git" "$TRASH_DIRECTORY"
2827'
2828
2829test_done