blob: deae916707f0c1fea99c929ad0863b4aabf86457 [file] [log] [blame]
Matthias Lederhoferdace6e42007-06-06 09:01:21 +02001#!/bin/sh
2
3test_description='test git rev-parse'
Johannes Schindelin06d53142020-11-18 23:44:21 +00004GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00005export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
Matthias Lederhoferdace6e42007-06-06 09:01:21 +02007. ./test-lib.sh
8
brian m. carlsonfac60b82020-12-13 00:25:29 +00009test_one () {
10 dir="$1" &&
11 expect="$2" &&
12 shift &&
13 shift &&
14 echo "$expect" >expect &&
15 git -C "$dir" rev-parse "$@" >actual &&
16 test_cmp expect actual
17}
18
SZEDER Gábora2f5a872017-02-03 03:48:23 +010019# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
Eric Sunshine12f75262016-05-18 16:15:42 -040020test_rev_parse () {
Eric Sunshine1e043cf2016-05-18 16:15:43 -040021 d=
Eric Sunshine1dea0dc2016-05-18 16:15:44 -040022 bare=
Eric Sunshinee6273f42016-05-18 16:15:45 -040023 gitdir=
Eric Sunshine1e043cf2016-05-18 16:15:43 -040024 while :
25 do
26 case "$1" in
27 -C) d="$2"; shift; shift ;;
Eric Sunshine1dea0dc2016-05-18 16:15:44 -040028 -b) case "$2" in
29 [tfu]*) bare="$2"; shift; shift ;;
30 *) error "test_rev_parse: bogus core.bare value '$2'" ;;
31 esac ;;
Eric Sunshinee6273f42016-05-18 16:15:45 -040032 -g) gitdir="$2"; shift; shift ;;
Eric Sunshine1e043cf2016-05-18 16:15:43 -040033 -*) error "test_rev_parse: unrecognized option '$1'" ;;
34 *) break ;;
35 esac
36 done
37
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020038 name=$1
39 shift
40
Eric Sunshine12f75262016-05-18 16:15:42 -040041 for o in --is-bare-repository \
42 --is-inside-git-dir \
43 --is-inside-work-tree \
44 --show-prefix \
SZEDER Gábora2f5a872017-02-03 03:48:23 +010045 --git-dir \
46 --absolute-git-dir
Eric Sunshine12f75262016-05-18 16:15:42 -040047 do
48 test $# -eq 0 && break
49 expect="$1"
50 test_expect_success "$name: $o" '
Eric Sunshinee6273f42016-05-18 16:15:45 -040051 if test -n "$gitdir"
52 then
53 test_when_finished "unset GIT_DIR" &&
54 GIT_DIR="$gitdir" &&
55 export GIT_DIR
56 fi &&
57
Eric Sunshine1dea0dc2016-05-18 16:15:44 -040058 case "$bare" in
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 ;;
62 esac &&
63
Eric Sunshine12f75262016-05-18 16:15:42 -040064 echo "$expect" >expect &&
Eric Sunshine1e043cf2016-05-18 16:15:43 -040065 git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
Eric Sunshine12f75262016-05-18 16:15:42 -040066 test_cmp expect actual
67 '
68 shift
69 done
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020070}
71
SZEDER Gábor8fb3c002009-02-14 17:16:28 +010072ROOT=$(pwd)
Johannes Schindelin7efeb8f2007-08-05 14:12:53 +010073
Eric Sunshined66f68f2016-05-17 15:36:26 -040074test_expect_success 'setup' '
Eric Sunshined66f68f2016-05-17 15:36:26 -040075 mkdir -p sub/dir work &&
brian m. carlsonfac60b82020-12-13 00:25:29 +000076 cp -R .git repo.git &&
77 git checkout -B main &&
78 test_commit abc &&
79 git checkout -b side &&
80 test_commit def &&
81 git checkout main &&
82 git worktree add worktree side
Eric Sunshined66f68f2016-05-17 15:36:26 -040083'
84
SZEDER Gábora2f5a872017-02-03 03:48:23 +010085test_rev_parse toplevel false false true '' .git "$ROOT/.git"
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020086
SZEDER Gábora2f5a872017-02-03 03:48:23 +010087test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
88test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020089
SZEDER Gábora2f5a872017-02-03 03:48:23 +010090test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020091
Eric Sunshine1dea0dc2016-05-18 16:15:44 -040092test_rev_parse -b t 'core.bare = true' true false false
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020093
Eric Sunshine1dea0dc2016-05-18 16:15:44 -040094test_rev_parse -b u 'core.bare undefined' false false true
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020095
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020096
SZEDER Gábora2f5a872017-02-03 03:48:23 +010097test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
Matthias Lederhoferdace6e42007-06-06 09:01:21 +020098
Eric Sunshinee6273f42016-05-18 16:15:45 -040099test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200100
Eric Sunshinee6273f42016-05-18 16:15:45 -0400101test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200102
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200103
SZEDER Gábora2f5a872017-02-03 03:48:23 +0100104test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git"
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200105
Eric Sunshinee6273f42016-05-18 16:15:45 -0400106test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200107
Eric Sunshinee6273f42016-05-18 16:15:45 -0400108test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200109
brian m. carlsonfac60b82020-12-13 00:25:29 +0000110test_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
120'
121
122test_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
132'
133
134test_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
138'
139
140test_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 &&
143 $ROOT/.git
144 .git/objects/foo/bar
145 EOF
146 test_cmp expect actual
147'
148
Michael Rappazzo5de8a542017-02-17 17:59:02 +0100149test_expect_success 'git-common-dir from worktree root' '
150 echo .git >expect &&
151 git rev-parse --git-common-dir >actual &&
152 test_cmp expect actual
153'
154
Johannes Schindelin098aa862017-02-17 17:59:06 +0100155test_expect_success 'git-common-dir inside sub-dir' '
Michael Rappazzo5de8a542017-02-17 17:59:02 +0100156 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
161'
162
163test_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
167'
168
Johannes Schindelin098aa862017-02-17 17:59:06 +0100169test_expect_success 'git-path inside sub-dir' '
Michael Rappazzo5de8a542017-02-17 17:59:02 +0100170 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
175'
176
Øystein Walle417abfd2017-09-18 19:04:29 +0200177test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
178 test_commit test_commit &&
179 echo true >expect &&
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
184'
185
186test_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
190'
191
brian m. carlson2eabd382019-10-28 00:58:55 +0000192test_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
204'
205
Jeff King2d92ab32019-11-19 03:05:43 -0500206test_expect_success '--show-toplevel from subdir of working tree' '
207 pwd >expect &&
208 git -C sub/dir rev-parse --show-toplevel >actual &&
209 test_cmp expect actual
210'
211
212test_expect_success '--show-toplevel from inside .git' '
213 test_must_fail git -C .git rev-parse --show-toplevel
214'
215
Stefan Bellerbf0231c2017-03-08 15:07:42 -0800216test_expect_success 'showing the superproject correctly' '
217 git rev-parse --show-superproject-working-tree >out &&
218 test_must_be_empty out &&
219
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 &&
Sam McKelviec5cbb272018-09-27 11:10:54 -0700227 test_cmp expect out &&
228
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 &&
Johannes Schindelin06d53142020-11-18 23:44:21 +0000235 git -C super checkout -b branch2 main &&
236 git -C super/dir/sub checkout -b branch2 main &&
Sam McKelviec5cbb272018-09-27 11:10:54 -0700237 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 &&
241
242 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
Stefan Bellerbf0231c2017-03-08 15:07:42 -0800243 test_cmp expect out
244'
245
Eric Wonga5cdca42021-02-10 21:55:43 +0000246# at least one external project depends on this behavior:
247test_expect_success 'rev-parse --since= unsqueezed ordering' '
248 x1=--since=1970-01-01T00:00:01Z &&
249 x2=--since=1970-01-01T00:00:02Z &&
250 x3=--since=1970-01-01T00:00:03Z &&
251 git rev-parse $x1 $x1 $x3 $x2 >actual &&
252 cat >expect <<-EOF &&
253 --max-age=1
254 --max-age=1
255 --max-age=3
256 --max-age=2
257 EOF
258 test_cmp expect actual
259'
260
Matthias Lederhoferdace6e42007-06-06 09:01:21 +0200261test_done