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
33mkdir -p work2 || exit 1
34mv .git repo.git || exit 1
35
36say "core.worktree = relative path"
37GIT_DIR=repo.git
38GIT_CONFIG="$(pwd)"/$GIT_DIR/config
39export GIT_DIR GIT_CONFIG
40unset GIT_WORK_TREE
41git config core.worktree ../work
42test_rev_parse 'outside' false false false
43cd work || exit 1
44GIT_DIR=../repo.git
45GIT_CONFIG="$(pwd)"/$GIT_DIR/config
46test_rev_parse 'inside' false false true ''
47cd sub/dir || exit 1
48GIT_DIR=../../../repo.git
49GIT_CONFIG="$(pwd)"/$GIT_DIR/config
50test_rev_parse 'subdirectory' false false true sub/dir/
51cd ../../.. || exit 1
52
53say "core.worktree = absolute path"
54GIT_DIR=$(pwd)/repo.git
55GIT_CONFIG=$GIT_DIR/config
56git config core.worktree "$(pwd)/work"
57test_rev_parse 'outside' false false false
58cd work2
59test_rev_parse 'outside2' false false false
60cd ../work || exit 1
61test_rev_parse 'inside' false false true ''
62cd sub/dir || exit 1
63test_rev_parse 'subdirectory' false false true sub/dir/
64cd ../../.. || exit 1
65
66say "GIT_WORK_TREE=relative path (override core.worktree)"
67GIT_DIR=$(pwd)/repo.git
68GIT_CONFIG=$GIT_DIR/config
69git config core.worktree non-existent
70GIT_WORK_TREE=work
71export GIT_WORK_TREE
72test_rev_parse 'outside' false false false
73cd work2
74test_rev_parse 'outside' false false false
75cd ../work || exit 1
76GIT_WORK_TREE=.
77test_rev_parse 'inside' false false true ''
78cd sub/dir || exit 1
79GIT_WORK_TREE=../..
80test_rev_parse 'subdirectory' false false true sub/dir/
81cd ../../.. || exit 1
82
83mv work repo.git/work
84mv work2 repo.git/work2
85
86say "GIT_WORK_TREE=absolute path, work tree below git dir"
87GIT_DIR=$(pwd)/repo.git
88GIT_CONFIG=$GIT_DIR/config
89GIT_WORK_TREE=$(pwd)/repo.git/work
90test_rev_parse 'outside' false false false
91cd repo.git || exit 1
92test_rev_parse 'in repo.git' false true false
93cd objects || exit 1
94test_rev_parse 'in repo.git/objects' false true false
95cd ../work2 || exit 1
96test_rev_parse 'in repo.git/work2' false true false
97cd ../work || exit 1
98test_rev_parse 'in repo.git/work' false true true ''
99cd sub/dir || exit 1
100test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
101cd ../../../.. || exit 1
102
103test_expect_success 'repo finds its work tree' '
104 (cd repo.git &&
105 : > work/sub/dir/untracked &&
106 test sub/dir/untracked = "$(git ls-files --others)")
107'
108
109test_expect_success 'repo finds its work tree from work tree, too' '
110 (cd repo.git/work/sub/dir &&
111 : > tracked &&
112 git --git-dir=../../.. add tracked &&
113 cd ../../.. &&
114 test sub/dir/tracked = "$(git ls-files)")
115'
116
117test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
118 (cd repo.git/work/sub/dir &&
119 GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
120 git diff --exit-code tracked &&
121 echo changed > tracked &&
122 ! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
123 git diff --exit-code tracked)
124'
125cat > diff-index-cached.expected <<\EOF
126:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A sub/dir/tracked
127EOF
128cat > diff-index.expected <<\EOF
129:000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A sub/dir/tracked
130EOF
131
132
133test_expect_success 'git diff-index' '
134 GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-index $EMPTY_TREE > result &&
135 test_cmp diff-index.expected result &&
136 GIT_DIR=repo.git git diff-index --cached $EMPTY_TREE > result &&
137 test_cmp diff-index-cached.expected result
138'
139cat >diff-files.expected <<\EOF
140:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M sub/dir/tracked
141EOF
142
143test_expect_success 'git diff-files' '
144 GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-files > result &&
145 test_cmp diff-files.expected result
146'
147
148cat >diff-TREE.expected <<\EOF
149diff --git a/sub/dir/tracked b/sub/dir/tracked
150new file mode 100644
151index 0000000..5ea2ed4
152--- /dev/null
153+++ b/sub/dir/tracked
154@@ -0,0 +1 @@
155+changed
156EOF
157cat >diff-TREE-cached.expected <<\EOF
158diff --git a/sub/dir/tracked b/sub/dir/tracked
159new file mode 100644
160index 0000000..e69de29
161EOF
162cat >diff-FILES.expected <<\EOF
163diff --git a/sub/dir/tracked b/sub/dir/tracked
164index e69de29..5ea2ed4 100644
165--- a/sub/dir/tracked
166+++ b/sub/dir/tracked
167@@ -0,0 +1 @@
168+changed
169EOF
170
171test_expect_success 'git diff' '
172 GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff $EMPTY_TREE > result &&
173 test_cmp diff-TREE.expected result &&
174 GIT_DIR=repo.git git diff --cached $EMPTY_TREE > result &&
175 test_cmp diff-TREE-cached.expected result &&
176 GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff > result &&
177 test_cmp diff-FILES.expected result
178'
179
180test_expect_success 'git grep' '
181 (cd repo.git/work/sub &&
182 GIT_DIR=../.. GIT_WORK_TREE=.. git grep -l changed | grep dir/tracked)
183'
184
185test_expect_success 'git commit' '
186 (
187 cd repo.git &&
188 GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
189 )
190'
191
192test_expect_success 'absolute pathspec should fail gracefully' '
193 (
194 cd repo.git || exit 1
195 git config --unset core.worktree
196 test_must_fail git log HEAD -- /home
197 )
198'
199
200test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
201 : > dummy_file
202 echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
203 git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
204'
205
206test_done