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