74750b8d4cecd281bb7ec54bddc4a7f7ff9818b1
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
222test_done