1#!/bin/sh
2
3test_description='"-C <path>" option and its effects on other path-related options'
4
5. ./test-lib.sh
6
7test_expect_success '"git -C <path>" runs git from the directory <path>' '
8	test_create_repo dir1 &&
9	echo 1 >dir1/a.txt &&
10	msg="initial in dir1" &&
11	(cd dir1 && git add a.txt && git commit -m "$msg") &&
12	echo "$msg" >expected &&
13	git -C dir1 log --format=%s >actual &&
14	test_cmp expected actual
15'
16
17test_expect_success '"git -C <path>" with an empty <path> is a no-op' '
18	(
19		mkdir -p dir1/subdir &&
20		cd dir1/subdir &&
21		git -C "" rev-parse --show-prefix >actual &&
22		echo subdir/ >expect &&
23		test_cmp expect actual
24	)
25'
26
27test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
28	test_create_repo dir1/dir2 &&
29	echo 1 >dir1/dir2/b.txt &&
30	git -C dir1/dir2 add b.txt &&
31	msg="initial in dir1/dir2" &&
32	echo "$msg" >expected &&
33	git -C dir1/dir2 commit -m "$msg" &&
34	git -C dir1 -C dir2 log --format=%s >actual &&
35	test_cmp expected actual
36'
37
38test_expect_success 'Effect on --git-dir option: "-C c --git-dir=a.git" is equivalent to "--git-dir c/a.git"' '
39	mkdir c &&
40	mkdir c/a &&
41	mkdir c/a.git &&
42	(cd c/a.git && git init --bare) &&
43	echo 1 >c/a/a.txt &&
44	git --git-dir c/a.git --work-tree=c/a add a.txt &&
45	git --git-dir c/a.git --work-tree=c/a commit -m "initial" &&
46	git --git-dir=c/a.git log -1 --format=%s >expected &&
47	git -C c --git-dir=a.git log -1 --format=%s >actual &&
48	test_cmp expected actual
49'
50
51test_expect_success 'Order should not matter: "--git-dir=a.git -C c" is equivalent to "-C c --git-dir=a.git"' '
52	git -C c --git-dir=a.git log -1 --format=%s >expected &&
53	git --git-dir=a.git -C c log -1 --format=%s >actual &&
54	test_cmp expected actual
55'
56
57test_expect_success 'Effect on --work-tree option: "-C c/a.git --work-tree=../a"  is equivalent to "--work-tree=c/a --git-dir=c/a.git"' '
58	rm c/a/a.txt &&
59	git --git-dir=c/a.git --work-tree=c/a status >expected &&
60	git -C c/a.git --work-tree=../a status >actual &&
61	test_cmp expected actual
62'
63
64test_expect_success 'Order should not matter: "--work-tree=../a -C c/a.git" is equivalent to "-C c/a.git --work-tree=../a"' '
65	git -C c/a.git --work-tree=../a status >expected &&
66	git --work-tree=../a -C c/a.git status >actual &&
67	test_cmp expected actual
68'
69
70test_expect_success 'Effect on --git-dir and --work-tree options - "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=c/a.git --work-tree=c/a"' '
71	git --git-dir=c/a.git --work-tree=c/a status >expected &&
72	git -C c --git-dir=a.git --work-tree=a status >actual &&
73	test_cmp expected actual
74'
75
76test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git -C c --work-tree=a"' '
77	git -C c --git-dir=a.git --work-tree=a status >expected &&
78	git --git-dir=a.git -C c --work-tree=a status >actual &&
79	test_cmp expected actual
80'
81
82test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git --work-tree=a -C c"' '
83	git -C c --git-dir=a.git --work-tree=a status >expected &&
84	git --git-dir=a.git --work-tree=a -C c status >actual &&
85	test_cmp expected actual
86'
87
88test_expect_success 'Relative followed by fullpath: "-C ./here -C /there" is equivalent to "-C /there"' '
89	echo "initial in dir1/dir2" >expected &&
90	git -C dir1 -C "$(pwd)/dir1/dir2" log --format=%s >actual &&
91	test_cmp expected actual
92'
93
94test_done
95