]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1500-rev-parse.sh
t[01]*: adjust the references to the default branch name "main"
[thirdparty/git.git] / t / t1500-rev-parse.sh
1 #!/bin/sh
2
3 test_description='test git rev-parse'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
10 test_rev_parse () {
11 d=
12 bare=
13 gitdir=
14 while :
15 do
16 case "$1" in
17 -C) d="$2"; shift; shift ;;
18 -b) case "$2" in
19 [tfu]*) bare="$2"; shift; shift ;;
20 *) error "test_rev_parse: bogus core.bare value '$2'" ;;
21 esac ;;
22 -g) gitdir="$2"; shift; shift ;;
23 -*) error "test_rev_parse: unrecognized option '$1'" ;;
24 *) break ;;
25 esac
26 done
27
28 name=$1
29 shift
30
31 for o in --is-bare-repository \
32 --is-inside-git-dir \
33 --is-inside-work-tree \
34 --show-prefix \
35 --git-dir \
36 --absolute-git-dir
37 do
38 test $# -eq 0 && break
39 expect="$1"
40 test_expect_success "$name: $o" '
41 if test -n "$gitdir"
42 then
43 test_when_finished "unset GIT_DIR" &&
44 GIT_DIR="$gitdir" &&
45 export GIT_DIR
46 fi &&
47
48 case "$bare" in
49 t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
50 f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
51 u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
52 esac &&
53
54 echo "$expect" >expect &&
55 git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
56 test_cmp expect actual
57 '
58 shift
59 done
60 }
61
62 ROOT=$(pwd)
63
64 test_expect_success 'setup' '
65 mkdir -p sub/dir work &&
66 cp -R .git repo.git
67 '
68
69 test_rev_parse toplevel false false true '' .git "$ROOT/.git"
70
71 test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
72 test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
73
74 test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
75
76 test_rev_parse -b t 'core.bare = true' true false false
77
78 test_rev_parse -b u 'core.bare undefined' false false true
79
80
81 test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
82
83 test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
84
85 test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
86
87
88 test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git"
89
90 test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
91
92 test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
93
94 test_expect_success 'git-common-dir from worktree root' '
95 echo .git >expect &&
96 git rev-parse --git-common-dir >actual &&
97 test_cmp expect actual
98 '
99
100 test_expect_success 'git-common-dir inside sub-dir' '
101 mkdir -p path/to/child &&
102 test_when_finished "rm -rf path" &&
103 echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
104 git -C path/to/child rev-parse --git-common-dir >actual &&
105 test_cmp expect actual
106 '
107
108 test_expect_success 'git-path from worktree root' '
109 echo .git/objects >expect &&
110 git rev-parse --git-path objects >actual &&
111 test_cmp expect actual
112 '
113
114 test_expect_success 'git-path inside sub-dir' '
115 mkdir -p path/to/child &&
116 test_when_finished "rm -rf path" &&
117 echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
118 git -C path/to/child rev-parse --git-path objects >actual &&
119 test_cmp expect actual
120 '
121
122 test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
123 test_commit test_commit &&
124 echo true >expect &&
125 git clone --depth 1 --no-local . shallow &&
126 test_when_finished "rm -rf shallow" &&
127 git -C shallow rev-parse --is-shallow-repository >actual &&
128 test_cmp expect actual
129 '
130
131 test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
132 echo false >expect &&
133 git rev-parse --is-shallow-repository >actual &&
134 test_cmp expect actual
135 '
136
137 test_expect_success 'rev-parse --show-object-format in repo' '
138 echo "$(test_oid algo)" >expect &&
139 git rev-parse --show-object-format >actual &&
140 test_cmp expect actual &&
141 git rev-parse --show-object-format=storage >actual &&
142 test_cmp expect actual &&
143 git rev-parse --show-object-format=input >actual &&
144 test_cmp expect actual &&
145 git rev-parse --show-object-format=output >actual &&
146 test_cmp expect actual &&
147 test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
148 grep "unknown mode for --show-object-format: squeamish-ossifrage" err
149 '
150
151 test_expect_success '--show-toplevel from subdir of working tree' '
152 pwd >expect &&
153 git -C sub/dir rev-parse --show-toplevel >actual &&
154 test_cmp expect actual
155 '
156
157 test_expect_success '--show-toplevel from inside .git' '
158 test_must_fail git -C .git rev-parse --show-toplevel
159 '
160
161 test_expect_success 'showing the superproject correctly' '
162 git rev-parse --show-superproject-working-tree >out &&
163 test_must_be_empty out &&
164
165 test_create_repo super &&
166 test_commit -C super test_commit &&
167 test_create_repo sub &&
168 test_commit -C sub test_commit &&
169 git -C super submodule add ../sub dir/sub &&
170 echo $(pwd)/super >expect &&
171 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
172 test_cmp expect out &&
173
174 test_commit -C super submodule_add &&
175 git -C super checkout -b branch1 &&
176 git -C super/dir/sub checkout -b branch1 &&
177 test_commit -C super/dir/sub branch1_commit &&
178 git -C super add dir/sub &&
179 test_commit -C super branch1_commit &&
180 git -C super checkout -b branch2 main &&
181 git -C super/dir/sub checkout -b branch2 main &&
182 test_commit -C super/dir/sub branch2_commit &&
183 git -C super add dir/sub &&
184 test_commit -C super branch2_commit &&
185 test_must_fail git -C super merge branch1 &&
186
187 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
188 test_cmp expect out
189 '
190
191 test_done