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
31mkdir -p work/sub/dir || exit 1
32mv .git repo.git || exit 1
33
34say "core.worktree = relative path"
35GIT_DIR=repo.git
36GIT_CONFIG="$(pwd)"/$GIT_DIR/config
37export GIT_DIR GIT_CONFIG
38unset GIT_WORK_TREE
39git config core.worktree ../work
40test_rev_parse 'outside' false false false
41cd work || exit 1
42GIT_DIR=../repo.git
43GIT_CONFIG="$(pwd)"/$GIT_DIR/config
44test_rev_parse 'inside' false false true ''
45cd sub/dir || exit 1
46GIT_DIR=../../../repo.git
47GIT_CONFIG="$(pwd)"/$GIT_DIR/config
48test_rev_parse 'subdirectory' false false true sub/dir/
49cd ../../.. || exit 1
50
51say "core.worktree = absolute path"
52GIT_DIR=$(pwd)/repo.git
53GIT_CONFIG=$GIT_DIR/config
54git config core.worktree "$(pwd)/work"
55test_rev_parse 'outside' false false false
56cd work || exit 1
57test_rev_parse 'inside' false false true ''
58cd sub/dir || exit 1
59test_rev_parse 'subdirectory' false false true sub/dir/
60cd ../../.. || exit 1
61
62say "GIT_WORK_TREE=relative path (override core.worktree)"
63GIT_DIR=$(pwd)/repo.git
64GIT_CONFIG=$GIT_DIR/config
65git config core.worktree non-existent
66GIT_WORK_TREE=work
67export GIT_WORK_TREE
68test_rev_parse 'outside' false false false
69cd work || exit 1
70GIT_WORK_TREE=.
71test_rev_parse 'inside' false false true ''
72cd sub/dir || exit 1
73GIT_WORK_TREE=../..
74test_rev_parse 'subdirectory' false false true sub/dir/
75cd ../../.. || exit 1
76
77mv work repo.git/work
78
79say "GIT_WORK_TREE=absolute path, work tree below git dir"
80GIT_DIR=$(pwd)/repo.git
81GIT_CONFIG=$GIT_DIR/config
82GIT_WORK_TREE=$(pwd)/repo.git/work
83test_rev_parse 'outside' false false false
84cd repo.git || exit 1
85test_rev_parse 'in repo.git' false true false
86cd objects || exit 1
87test_rev_parse 'in repo.git/objects' false true false
88cd ../work || exit 1
89test_rev_parse 'in repo.git/work' false true true ''
90cd sub/dir || exit 1
91test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
92cd ../../../.. || exit 1
93
94test_expect_success 'repo finds its work tree' '
95 (cd repo.git &&
96 : > work/sub/dir/untracked &&
97 test sub/dir/untracked = "$(git ls-files --others)")
98'
99
100test_expect_success 'repo finds its work tree from work tree, too' '
101 (cd repo.git/work/sub/dir &&
102 : > tracked &&
103 git --git-dir=../../.. add tracked &&
104 cd ../../.. &&
105 test sub/dir/tracked = "$(git ls-files)")
106'
107
108test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
109 cd repo.git/work/sub/dir &&
110 GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
111 git diff --exit-code tracked &&
112 echo changed > tracked &&
113 ! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
114 git diff --exit-code tracked
115'
116
117test_done