t / t1501-worktree.shon commit Merge branch 'maint-1.6.0' into maint (9bfff3a)
   1#!/bin/sh
   2
   3test_description='test separate work tree'
   4. ./test-lib.sh
   5
   6test_rev_parse() {
   7        name=$1
   8        shift
   9
  10        test_expect_success "$name: is-bare-repository" \
  11        "test '$1' = \"\$(git rev-parse --is-bare-repository)\""
  12        shift
  13        [ $# -eq 0 ] && return
  14
  15        test_expect_success "$name: is-inside-git-dir" \
  16        "test '$1' = \"\$(git rev-parse --is-inside-git-dir)\""
  17        shift
  18        [ $# -eq 0 ] && return
  19
  20        test_expect_success "$name: is-inside-work-tree" \
  21        "test '$1' = \"\$(git rev-parse --is-inside-work-tree)\""
  22        shift
  23        [ $# -eq 0 ] && return
  24
  25        test_expect_success "$name: prefix" \
  26        "test '$1' = \"\$(git rev-parse --show-prefix)\""
  27        shift
  28        [ $# -eq 0 ] && return
  29}
  30
  31EMPTY_TREE=$(git write-tree)
  32mkdir -p work/sub/dir || exit 1
  33mv .git repo.git || exit 1
  34
  35say "core.worktree = relative path"
  36GIT_DIR=repo.git
  37GIT_CONFIG="$(pwd)"/$GIT_DIR/config
  38export GIT_DIR GIT_CONFIG
  39unset GIT_WORK_TREE
  40git config core.worktree ../work
  41test_rev_parse 'outside'      false false false
  42cd work || exit 1
  43GIT_DIR=../repo.git
  44GIT_CONFIG="$(pwd)"/$GIT_DIR/config
  45test_rev_parse 'inside'       false false true ''
  46cd sub/dir || exit 1
  47GIT_DIR=../../../repo.git
  48GIT_CONFIG="$(pwd)"/$GIT_DIR/config
  49test_rev_parse 'subdirectory' false false true sub/dir/
  50cd ../../.. || exit 1
  51
  52say "core.worktree = absolute path"
  53GIT_DIR=$(pwd)/repo.git
  54GIT_CONFIG=$GIT_DIR/config
  55git config core.worktree "$(pwd)/work"
  56test_rev_parse 'outside'      false false false
  57cd work || exit 1
  58test_rev_parse 'inside'       false false true ''
  59cd sub/dir || exit 1
  60test_rev_parse 'subdirectory' false false true sub/dir/
  61cd ../../.. || exit 1
  62
  63say "GIT_WORK_TREE=relative path (override core.worktree)"
  64GIT_DIR=$(pwd)/repo.git
  65GIT_CONFIG=$GIT_DIR/config
  66git config core.worktree non-existent
  67GIT_WORK_TREE=work
  68export GIT_WORK_TREE
  69test_rev_parse 'outside'      false false false
  70cd work || exit 1
  71GIT_WORK_TREE=.
  72test_rev_parse 'inside'       false false true ''
  73cd sub/dir || exit 1
  74GIT_WORK_TREE=../..
  75test_rev_parse 'subdirectory' false false true sub/dir/
  76cd ../../.. || exit 1
  77
  78mv work repo.git/work
  79
  80say "GIT_WORK_TREE=absolute path, work tree below git dir"
  81GIT_DIR=$(pwd)/repo.git
  82GIT_CONFIG=$GIT_DIR/config
  83GIT_WORK_TREE=$(pwd)/repo.git/work
  84test_rev_parse 'outside'              false false false
  85cd repo.git || exit 1
  86test_rev_parse 'in repo.git'              false true  false
  87cd objects || exit 1
  88test_rev_parse 'in repo.git/objects'      false true  false
  89cd ../work || exit 1
  90test_rev_parse 'in repo.git/work'         false true true ''
  91cd sub/dir || exit 1
  92test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
  93cd ../../../.. || exit 1
  94
  95test_expect_success 'repo finds its work tree' '
  96        (cd repo.git &&
  97         : > work/sub/dir/untracked &&
  98         test sub/dir/untracked = "$(git ls-files --others)")
  99'
 100
 101test_expect_success 'repo finds its work tree from work tree, too' '
 102        (cd repo.git/work/sub/dir &&
 103         : > tracked &&
 104         git --git-dir=../../.. add tracked &&
 105         cd ../../.. &&
 106         test sub/dir/tracked = "$(git ls-files)")
 107'
 108
 109test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
 110        (cd repo.git/work/sub/dir &&
 111        GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
 112                git diff --exit-code tracked &&
 113        echo changed > tracked &&
 114        ! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
 115                git diff --exit-code tracked)
 116'
 117cat > diff-index-cached.expected <<\EOF
 118:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A      sub/dir/tracked
 119EOF
 120cat > diff-index.expected <<\EOF
 121:000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A      sub/dir/tracked
 122EOF
 123
 124
 125test_expect_success 'git diff-index' '
 126        GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-index $EMPTY_TREE > result &&
 127        test_cmp diff-index.expected result &&
 128        GIT_DIR=repo.git git diff-index --cached $EMPTY_TREE > result &&
 129        test_cmp diff-index-cached.expected result
 130'
 131cat >diff-files.expected <<\EOF
 132:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M      sub/dir/tracked
 133EOF
 134
 135test_expect_success 'git diff-files' '
 136        GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-files > result &&
 137        test_cmp diff-files.expected result
 138'
 139
 140cat >diff-TREE.expected <<\EOF
 141diff --git a/sub/dir/tracked b/sub/dir/tracked
 142new file mode 100644
 143index 0000000..5ea2ed4
 144--- /dev/null
 145+++ b/sub/dir/tracked
 146@@ -0,0 +1 @@
 147+changed
 148EOF
 149cat >diff-TREE-cached.expected <<\EOF
 150diff --git a/sub/dir/tracked b/sub/dir/tracked
 151new file mode 100644
 152index 0000000..e69de29
 153EOF
 154cat >diff-FILES.expected <<\EOF
 155diff --git a/sub/dir/tracked b/sub/dir/tracked
 156index e69de29..5ea2ed4 100644
 157--- a/sub/dir/tracked
 158+++ b/sub/dir/tracked
 159@@ -0,0 +1 @@
 160+changed
 161EOF
 162
 163test_expect_success 'git diff' '
 164        GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff $EMPTY_TREE > result &&
 165        test_cmp diff-TREE.expected result &&
 166        GIT_DIR=repo.git git diff --cached $EMPTY_TREE > result &&
 167        test_cmp diff-TREE-cached.expected result &&
 168        GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff > result &&
 169        test_cmp diff-FILES.expected result
 170'
 171
 172test_expect_success 'git grep' '
 173        (cd repo.git/work/sub &&
 174        GIT_DIR=../.. GIT_WORK_TREE=.. git grep -l changed | grep dir/tracked)
 175'
 176
 177test_expect_success 'git commit' '
 178        (
 179                cd repo.git &&
 180                GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
 181        )
 182'
 183
 184test_expect_success 'absolute pathspec should fail gracefully' '
 185        (
 186                cd repo.git || exit 1
 187                git config --unset core.worktree
 188                test_must_fail git log HEAD -- /home
 189        )
 190'
 191
 192test_done