3 test_description
='Tests of cwd/prefix/worktree/gitdir setup in all cases'
8 # A few rules for repo setup:
10 # 1. GIT_DIR is relative to user's cwd. --git-dir is equivalent to
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
17 # 3. core.worktree is relative to git_dir.
19 # 4. GIT_WORK_TREE is relative to user's cwd. --work-tree is
20 # equivalent to GIT_WORK_TREE.
22 # 5. GIT_WORK_TREE/core.worktree is only effective if GIT_DIR is set
23 # Uneffective worktree settings should be warned.
25 # 6. Effective GIT_WORK_TREE overrides core.worktree and core.bare
27 # 7. Effective core.worktree conflicts with core.bare
29 # 8. If GIT_DIR is set but neither worktree nor bare setting is given,
30 # original cwd becomes worktree.
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.
35 # 10. If no worktree is available, cwd remains unchanged, prefix is
38 # 11. When user's cwd is outside worktree, cwd remains unchanged,
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 &&
48 GIT_TRACE
="`pwd`/trace" git symbolic-ref HEAD
>/dev
/null
&&
49 grep '^setup: ' trace
>result
&&
50 test_cmp expected result
54 # Bit 0 = GIT_WORK_TREE
56 # Bit 2 = core.worktree
57 # Bit 3 = .git is a file
59 # Case# = encoding of the above 5 bits
64 ############################################################
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
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
82 test_expect_success
'#0: setup' '
83 unset GIT_DIR GIT_WORK_TREE &&
85 cd 0 && git init && cd ..
88 test_expect_success
'#0: at root' '
89 cat >0/expected <<EOF &&
91 setup: worktree: $TRASH_DIRECTORY/0
92 setup: cwd: $TRASH_DIRECTORY/0
98 test_expect_success
'#0: in subdir' '
99 cat >0/sub/expected <<EOF &&
101 setup: worktree: $TRASH_DIRECTORY/0
102 setup: cwd: $TRASH_DIRECTORY/0
111 ############################################################
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
123 # GIT_WORK_TREE is ignored -> #0
125 test_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 &&
130 GIT_WORK_TREE=non-existent &&
131 export GIT_WORK_TREE &&
135 test_expect_failure
'#1: at root' '
136 cat >1/expected <<EOF &&
138 setup: worktree: $TRASH_DIRECTORY/1
139 setup: cwd: $TRASH_DIRECTORY/1
140 setup: prefix: (null)
145 test_expect_failure
'#1: in subdir' '
146 cat >1/sub/expected <<EOF &&
148 setup: worktree: $TRASH_DIRECTORY/1
149 setup: cwd: $TRASH_DIRECTORY/1
158 ############################################################
162 # - GIT_WORK_TREE is not set
164 # - core.worktree is not set
165 # - .git is a directory
166 # - core.bare is not set, cwd is outside .git
170 # - worktree is at original cwd
173 # - git_dir is set to $GIT_DIR
174 # - cwd can't be outside worktree
176 test_expect_success
'#2: setup' '
177 unset GIT_DIR GIT_WORK_TREE &&
179 cd 2 && git init && cd ..
182 test_expect_success
'#2: at root' '
183 cat >2/expected <<EOF &&
184 setup: git_dir: $TRASH_DIRECTORY/2/.git
185 setup: worktree: $TRASH_DIRECTORY/2
186 setup: cwd: $TRASH_DIRECTORY/2
187 setup: prefix: (null)
189 test_repo 2 "$TRASH_DIRECTORY/2/.git"
192 test_expect_success
'#2: in subdir' '
193 cat >2/sub/expected <<EOF &&
194 setup: git_dir: $TRASH_DIRECTORY/2/.git
195 setup: worktree: $TRASH_DIRECTORY/2/sub
196 setup: cwd: $TRASH_DIRECTORY/2/sub
197 setup: prefix: (null)
199 test_repo 2/sub "$TRASH_DIRECTORY/2/.git"
202 test_expect_success
'#2: relative GIT_DIR at root' '
203 cat >2/expected <<EOF &&
205 setup: worktree: $TRASH_DIRECTORY/2
206 setup: cwd: $TRASH_DIRECTORY/2
207 setup: prefix: (null)
212 test_expect_success
'#2: relative GIT_DIR in subdir' '
213 cat >2/sub/expected <<EOF &&
214 setup: git_dir: ../.git
215 setup: worktree: $TRASH_DIRECTORY/2/sub
216 setup: cwd: $TRASH_DIRECTORY/2/sub
217 setup: prefix: (null)
219 test_repo 2/sub ../.git
225 ############################################################
229 # - GIT_WORK_TREE is set
231 # - core.worktree is not set
232 # - .git is a directory
233 # - core.bare is not set, cwd is outside .git
237 # - worktree is set to $GIT_WORK_TREE
238 # - cwd is at worktree root
239 # - prefix is calculated
240 # - git_dir is set to $GIT_DIR
241 # - cwd can be outside worktree
243 test_expect_success
'#3: setup' '
244 unset GIT_DIR GIT_WORK_TREE &&
245 mkdir 3 3/sub 3/sub/sub 3.wt 3.wt/sub 3/wt 3/wt/sub &&
246 cd 3 && git init && cd ..
249 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=root at root' '
250 cat >3/expected <<EOF &&
252 setup: worktree: $TRASH_DIRECTORY/3
253 setup: cwd: $TRASH_DIRECTORY/3
254 setup: prefix: (null)
256 test_repo 3 .git "$TRASH_DIRECTORY/3"
259 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=root(rel) at root' '
260 cat >3/expected <<EOF &&
262 setup: worktree: $TRASH_DIRECTORY/3
263 setup: cwd: $TRASH_DIRECTORY/3
264 setup: prefix: (null)
269 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=root at root' '
270 cat >3/expected <<EOF &&
271 setup: git_dir: $TRASH_DIRECTORY/3/.git
272 setup: worktree: $TRASH_DIRECTORY/3
273 setup: cwd: $TRASH_DIRECTORY/3
274 setup: prefix: (null)
276 test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3"
279 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=root(rel) at root' '
280 cat >3/expected <<EOF &&
281 setup: git_dir: $TRASH_DIRECTORY/3/.git
282 setup: worktree: $TRASH_DIRECTORY/3
283 setup: cwd: $TRASH_DIRECTORY/3
284 setup: prefix: (null)
286 test_repo 3 "$TRASH_DIRECTORY/3/.git" .
289 test_expect_success
'#3: GIT_DIR(rel), GIT_WORKTREE=root in subdir' '
290 cat >3/sub/sub/expected <<EOF &&
291 setup: git_dir: $TRASH_DIRECTORY/3/.git
292 setup: worktree: $TRASH_DIRECTORY/3
293 setup: cwd: $TRASH_DIRECTORY/3
294 setup: prefix: sub/sub/
296 test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY/3"
299 test_expect_success
'#3: GIT_DIR(rel), GIT_WORKTREE=root(rel) in subdir' '
300 cat >3/sub/sub/expected <<EOF &&
301 setup: git_dir: $TRASH_DIRECTORY/3/.git
302 setup: worktree: $TRASH_DIRECTORY/3
303 setup: cwd: $TRASH_DIRECTORY/3
304 setup: prefix: sub/sub/
306 test_repo 3/sub/sub ../../.git ../..
309 test_expect_success
'#3: GIT_DIR, GIT_WORKTREE=root in subdir' '
310 cat >3/sub/expected <<EOF &&
311 setup: git_dir: $TRASH_DIRECTORY/3/.git
312 setup: worktree: $TRASH_DIRECTORY/3
313 setup: cwd: $TRASH_DIRECTORY/3
316 test_repo 3/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3"
319 test_expect_success
'#3: GIT_DIR, GIT_WORKTREE=root(rel) in subdir' '
320 cat >3/sub/sub/expected <<EOF &&
321 setup: git_dir: $TRASH_DIRECTORY/3/.git
322 setup: worktree: $TRASH_DIRECTORY/3
323 setup: cwd: $TRASH_DIRECTORY/3
324 setup: prefix: sub/sub/
326 test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../..
329 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=wt at root' '
330 cat >3/expected <<EOF &&
332 setup: worktree: $TRASH_DIRECTORY/3/wt
333 setup: cwd: $TRASH_DIRECTORY/3
334 setup: prefix: (null)
336 test_repo 3 .git "$TRASH_DIRECTORY/3/wt"
339 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) at root' '
340 cat >3/expected <<EOF &&
342 setup: worktree: $TRASH_DIRECTORY/3/wt
343 setup: cwd: $TRASH_DIRECTORY/3
344 setup: prefix: (null)
349 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=wt(rel) at root' '
350 cat >3/expected <<EOF &&
351 setup: git_dir: $TRASH_DIRECTORY/3/.git
352 setup: worktree: $TRASH_DIRECTORY/3/wt
353 setup: cwd: $TRASH_DIRECTORY/3
354 setup: prefix: (null)
356 test_repo 3 "$TRASH_DIRECTORY/3/.git" wt
359 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=wt at root' '
360 cat >3/expected <<EOF &&
361 setup: git_dir: $TRASH_DIRECTORY/3/.git
362 setup: worktree: $TRASH_DIRECTORY/3/wt
363 setup: cwd: $TRASH_DIRECTORY/3
364 setup: prefix: (null)
366 test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3/wt"
369 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=wt in subdir' '
370 cat >3/sub/sub/expected <<EOF &&
371 setup: git_dir: ../../.git
372 setup: worktree: $TRASH_DIRECTORY/3/wt
373 setup: cwd: $TRASH_DIRECTORY/3/sub/sub
374 setup: prefix: (null)
376 test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY/3/wt"
379 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=wt(rel) in subdir' '
380 cat >3/sub/sub/expected <<EOF &&
381 setup: git_dir: ../../.git
382 setup: worktree: $TRASH_DIRECTORY/3/wt
383 setup: cwd: $TRASH_DIRECTORY/3/sub/sub
384 setup: prefix: (null)
386 test_repo 3/sub/sub ../../.git ../../wt
389 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=wt(rel) in subdir' '
390 cat >3/sub/sub/expected <<EOF &&
391 setup: git_dir: $TRASH_DIRECTORY/3/.git
392 setup: worktree: $TRASH_DIRECTORY/3/wt
393 setup: cwd: $TRASH_DIRECTORY/3/sub/sub
394 setup: prefix: (null)
396 test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../../wt
399 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=wt in subdir' '
400 cat >3/sub/sub/expected <<EOF &&
401 setup: git_dir: $TRASH_DIRECTORY/3/.git
402 setup: worktree: $TRASH_DIRECTORY/3/wt
403 setup: cwd: $TRASH_DIRECTORY/3/sub/sub
404 setup: prefix: (null)
406 test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY/3/wt"
409 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=.. at root' '
410 cat >3/expected <<EOF &&
411 setup: git_dir: $TRASH_DIRECTORY/3/.git
412 setup: worktree: $TRASH_DIRECTORY
413 setup: cwd: $TRASH_DIRECTORY
416 test_repo 3 .git "$TRASH_DIRECTORY"
419 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=..(rel) at root' '
420 cat >3/expected <<EOF &&
421 setup: git_dir: $TRASH_DIRECTORY/3/.git
422 setup: worktree: $TRASH_DIRECTORY
423 setup: cwd: $TRASH_DIRECTORY
429 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=..(rel) at root' '
430 cat >3/expected <<EOF &&
431 setup: git_dir: $TRASH_DIRECTORY/3/.git
432 setup: worktree: $TRASH_DIRECTORY
433 setup: cwd: $TRASH_DIRECTORY
436 test_repo 3 "$TRASH_DIRECTORY/3/.git" ..
439 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=.. at root' '
440 cat >3/expected <<EOF &&
441 setup: git_dir: $TRASH_DIRECTORY/3/.git
442 setup: worktree: $TRASH_DIRECTORY
443 setup: cwd: $TRASH_DIRECTORY
446 test_repo 3 "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY"
449 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=.. in subdir' '
450 cat >3/sub/sub/expected <<EOF &&
451 setup: git_dir: $TRASH_DIRECTORY/3/.git
452 setup: worktree: $TRASH_DIRECTORY
453 setup: cwd: $TRASH_DIRECTORY
454 setup: prefix: 3/sub/sub/
456 test_repo 3/sub/sub ../../.git "$TRASH_DIRECTORY"
459 test_expect_success
'#3: GIT_DIR(rel), GIT_WORK_TREE=..(rel) in subdir' '
460 cat >3/sub/sub/expected <<EOF &&
461 setup: git_dir: $TRASH_DIRECTORY/3/.git
462 setup: worktree: $TRASH_DIRECTORY
463 setup: cwd: $TRASH_DIRECTORY
464 setup: prefix: 3/sub/sub/
466 test_repo 3/sub/sub ../../.git ../../..
469 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=..(rel) in subdir' '
470 cat >3/sub/sub/expected <<EOF &&
471 setup: git_dir: $TRASH_DIRECTORY/3/.git
472 setup: worktree: $TRASH_DIRECTORY
473 setup: cwd: $TRASH_DIRECTORY
474 setup: prefix: 3/sub/sub/
476 test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" ../../../
479 test_expect_success
'#3: GIT_DIR, GIT_WORK_TREE=.. in subdir' '
480 cat >3/sub/sub/expected <<EOF &&
481 setup: git_dir: $TRASH_DIRECTORY/3/.git
482 setup: worktree: $TRASH_DIRECTORY
483 setup: cwd: $TRASH_DIRECTORY
484 setup: prefix: 3/sub/sub/
486 test_repo 3/sub/sub "$TRASH_DIRECTORY/3/.git" "$TRASH_DIRECTORY"
492 ############################################################
496 # - GIT_WORK_TREE is not set
497 # - GIT_DIR is not set
498 # - core.worktree is set
499 # - .git is a directory
500 # - core.bare is not set, cwd is outside .git
504 # core.worktree is ignored -> #0
506 test_expect_success
'#4: setup' '
507 unset GIT_DIR GIT_WORK_TREE &&
511 git config core.worktree non-existent &&
515 test_expect_failure
'#4: at root' '
516 cat >4/expected <<EOF &&
518 setup: worktree: $TRASH_DIRECTORY/4
519 setup: cwd: $TRASH_DIRECTORY/4
520 setup: prefix: (null)
525 test_expect_failure
'#4: in subdir' '
526 cat >4/sub/expected <<EOF &&
528 setup: worktree: $TRASH_DIRECTORY/4
529 setup: cwd: $TRASH_DIRECTORY/4
538 ############################################################
542 # - GIT_WORK_TREE is set
543 # - GIT_DIR is not set
544 # - core.worktree is set
545 # - .git is a directory
546 # - core.bare is not set, cwd is outside .git
550 # GIT_WORK_TREE/core.worktree are ignored -> #0
552 test_expect_success
'#5: setup' '
553 unset GIT_DIR GIT_WORK_TREE &&
557 git config core.worktree non-existent &&
558 GIT_WORK_TREE=non-existent-too &&
559 export GIT_WORK_TREE &&
563 test_expect_failure
'#5: at root' '
564 cat >5/expected <<EOF &&
566 setup: worktree: $TRASH_DIRECTORY/5
567 setup: cwd: $TRASH_DIRECTORY/5
568 setup: prefix: (null)
573 test_expect_failure
'#5: in subdir' '
574 cat >5/sub/expected <<EOF &&
576 setup: worktree: $TRASH_DIRECTORY/5
577 setup: cwd: $TRASH_DIRECTORY/5