3 test_description='test git rev-parse'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14 echo "$expect" >expect &&
15 git -C "$dir" rev-parse "$@" >actual &&
16 test_cmp expect actual
19 # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
27 -C) d="$2"; shift; shift ;;
29 [tfu]*) bare="$2"; shift; shift ;;
30 *) error "test_rev_parse: bogus core.bare value '$2'" ;;
32 -g) gitdir="$2"; shift; shift ;;
33 -*) error "test_rev_parse: unrecognized option '$1'" ;;
41 for o in --is-bare-repository \
43 --is-inside-work-tree \
48 test $# -eq 0 && break
50 test_expect_success "$name: $o" '
53 test_when_finished "unset GIT_DIR" &&
59 t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
60 f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
61 u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
64 echo "$expect" >expect &&
65 git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
66 test_cmp expect actual
74 test_expect_success 'setup' '
75 mkdir -p sub/dir work &&
76 cp -R .git repo.git &&
77 git checkout -B main &&
79 git checkout -b side &&
82 git worktree add worktree side
85 test_rev_parse toplevel false false true '' .git "$ROOT/.git"
87 test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
88 test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
90 test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
92 test_rev_parse -b t 'core.bare = true' true false false
94 test_rev_parse -b u 'core.bare undefined' false false true
97 test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
99 test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
101 test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
104 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"
106 test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
108 test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
110 test_expect_success 'rev-parse --path-format=absolute' '
111 test_one "." "$ROOT/.git" --path-format=absolute --git-dir &&
112 test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir &&
113 test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir &&
114 test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir &&
115 test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir &&
116 test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir &&
117 test_one "." "$ROOT" --path-format=absolute --show-toplevel &&
118 test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects &&
119 test_one "." "$ROOT/.git/objects/foo/bar/baz" --path-format=absolute --git-path objects/foo/bar/baz
122 test_expect_success 'rev-parse --path-format=relative' '
123 test_one "." ".git" --path-format=relative --git-dir &&
124 test_one "." ".git" --path-format=relative --git-common-dir &&
125 test_one "sub/dir" "../../.git" --path-format=relative --git-dir &&
126 test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir &&
127 test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir &&
128 test_one "worktree" "../.git" --path-format=relative --git-common-dir &&
129 test_one "." "./" --path-format=relative --show-toplevel &&
130 test_one "." ".git/objects" --path-format=relative --git-path objects &&
131 test_one "." ".git/objects/foo/bar/baz" --path-format=relative --git-path objects/foo/bar/baz
134 test_expect_success '--path-format=relative does not affect --absolute-git-dir' '
135 git rev-parse --path-format=relative --absolute-git-dir >actual &&
136 echo "$ROOT/.git" >expect &&
137 test_cmp expect actual
140 test_expect_success '--path-format can change in the middle of the command line' '
141 git rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual &&
142 cat >expect <<-EOF &&
146 test_cmp expect actual
149 test_expect_success 'git-common-dir from worktree root' '
151 git rev-parse --git-common-dir >actual &&
152 test_cmp expect actual
155 test_expect_success 'git-common-dir inside sub-dir' '
156 mkdir -p path/to/child &&
157 test_when_finished "rm -rf path" &&
158 echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
159 git -C path/to/child rev-parse --git-common-dir >actual &&
160 test_cmp expect actual
163 test_expect_success 'git-path from worktree root' '
164 echo .git/objects >expect &&
165 git rev-parse --git-path objects >actual &&
166 test_cmp expect actual
169 test_expect_success 'git-path inside sub-dir' '
170 mkdir -p path/to/child &&
171 test_when_finished "rm -rf path" &&
172 echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
173 git -C path/to/child rev-parse --git-path objects >actual &&
174 test_cmp expect actual
177 test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
178 test_commit test_commit &&
180 git clone --depth 1 --no-local . shallow &&
181 test_when_finished "rm -rf shallow" &&
182 git -C shallow rev-parse --is-shallow-repository >actual &&
183 test_cmp expect actual
186 test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
187 echo false >expect &&
188 git rev-parse --is-shallow-repository >actual &&
189 test_cmp expect actual
192 test_expect_success 'rev-parse --show-object-format in repo' '
193 echo "$(test_oid algo)" >expect &&
194 git rev-parse --show-object-format >actual &&
195 test_cmp expect actual &&
196 git rev-parse --show-object-format=storage >actual &&
197 test_cmp expect actual &&
198 git rev-parse --show-object-format=input >actual &&
199 test_cmp expect actual &&
200 git rev-parse --show-object-format=output >actual &&
201 test_cmp expect actual &&
202 test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
203 grep "unknown mode for --show-object-format: squeamish-ossifrage" err
206 test_expect_success '--show-toplevel from subdir of working tree' '
208 git -C sub/dir rev-parse --show-toplevel >actual &&
209 test_cmp expect actual
212 test_expect_success '--show-toplevel from inside .git' '
213 test_must_fail git -C .git rev-parse --show-toplevel
216 test_expect_success 'showing the superproject correctly' '
217 git rev-parse --show-superproject-working-tree >out &&
218 test_must_be_empty out &&
220 test_create_repo super &&
221 test_commit -C super test_commit &&
222 test_create_repo sub &&
223 test_commit -C sub test_commit &&
224 git -C super submodule add ../sub dir/sub &&
225 echo $(pwd)/super >expect &&
226 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
227 test_cmp expect out &&
229 test_commit -C super submodule_add &&
230 git -C super checkout -b branch1 &&
231 git -C super/dir/sub checkout -b branch1 &&
232 test_commit -C super/dir/sub branch1_commit &&
233 git -C super add dir/sub &&
234 test_commit -C super branch1_commit &&
235 git -C super checkout -b branch2 main &&
236 git -C super/dir/sub checkout -b branch2 main &&
237 test_commit -C super/dir/sub branch2_commit &&
238 git -C super add dir/sub &&
239 test_commit -C super branch2_commit &&
240 test_must_fail git -C super merge branch1 &&
242 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&