The eleventh batch
[git/gitster.git] / t / t1500-rev-parse.sh
blob30c31918fde6539d52800e18dfbb3423b5b73491
1 #!/bin/sh
3 test_description='test git rev-parse'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 test_one () {
11 dir="$1" &&
12 expect="$2" &&
13 shift &&
14 shift &&
15 echo "$expect" >expect &&
16 git -C "$dir" rev-parse "$@" >actual &&
17 test_cmp expect actual
20 # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
21 test_rev_parse () {
23 bare=
24 gitdir=
25 while :
27 case "$1" in
28 -C) d="$2"; shift; shift ;;
29 -b) case "$2" in
30 [tfu]*) bare="$2"; shift; shift ;;
31 *) error "test_rev_parse: bogus core.bare value '$2'" ;;
32 esac ;;
33 -g) gitdir="$2"; shift; shift ;;
34 -*) error "test_rev_parse: unrecognized option '$1'" ;;
35 *) break ;;
36 esac
37 done
39 name=$1
40 shift
42 for o in --is-bare-repository \
43 --is-inside-git-dir \
44 --is-inside-work-tree \
45 --show-prefix \
46 --git-dir \
47 --absolute-git-dir
49 test $# -eq 0 && break
50 expect="$1"
51 test_expect_success "$name: $o" '
52 if test -n "$gitdir"
53 then
54 test_when_finished "unset GIT_DIR" &&
55 GIT_DIR="$gitdir" &&
56 export GIT_DIR
57 fi &&
59 case "$bare" in
60 t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
61 f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
62 u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
63 esac &&
65 echo "$expect" >expect &&
66 git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
67 test_cmp expect actual
69 shift
70 done
73 ROOT=$(pwd)
75 test_expect_success 'setup' '
76 mkdir -p sub/dir work &&
77 cp -R .git repo.git &&
78 git checkout -B main &&
79 test_commit abc &&
80 git checkout -b side &&
81 test_commit def &&
82 git checkout main &&
83 git worktree add worktree side
86 test_rev_parse toplevel false false true '' .git "$ROOT/.git"
88 test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
89 test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
91 test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
93 test_rev_parse -b t 'core.bare = true' true false false
95 test_rev_parse -b u 'core.bare undefined' false false true
98 test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
100 test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
102 test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
105 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"
107 test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
109 test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
111 test_expect_success 'rev-parse --path-format=absolute' '
112 test_one "." "$ROOT/.git" --path-format=absolute --git-dir &&
113 test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir &&
114 test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir &&
115 test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir &&
116 test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir &&
117 test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir &&
118 test_one "." "$ROOT" --path-format=absolute --show-toplevel &&
119 test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects &&
120 test_one "." "$ROOT/.git/objects/foo/bar/baz" --path-format=absolute --git-path objects/foo/bar/baz
123 test_expect_success 'rev-parse --path-format=relative' '
124 test_one "." ".git" --path-format=relative --git-dir &&
125 test_one "." ".git" --path-format=relative --git-common-dir &&
126 test_one "sub/dir" "../../.git" --path-format=relative --git-dir &&
127 test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir &&
128 test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir &&
129 test_one "worktree" "../.git" --path-format=relative --git-common-dir &&
130 test_one "." "./" --path-format=relative --show-toplevel &&
131 test_one "." ".git/objects" --path-format=relative --git-path objects &&
132 test_one "." ".git/objects/foo/bar/baz" --path-format=relative --git-path objects/foo/bar/baz
135 test_expect_success '--path-format=relative does not affect --absolute-git-dir' '
136 git rev-parse --path-format=relative --absolute-git-dir >actual &&
137 echo "$ROOT/.git" >expect &&
138 test_cmp expect actual
141 test_expect_success '--path-format can change in the middle of the command line' '
142 git rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual &&
143 cat >expect <<-EOF &&
144 $ROOT/.git
145 .git/objects/foo/bar
147 test_cmp expect actual
150 test_expect_success '--path-format does not segfault without an argument' '
151 test_must_fail git rev-parse --path-format
154 test_expect_success 'git-common-dir from worktree root' '
155 echo .git >expect &&
156 git rev-parse --git-common-dir >actual &&
157 test_cmp expect actual
160 test_expect_success 'git-common-dir inside sub-dir' '
161 mkdir -p path/to/child &&
162 test_when_finished "rm -rf path" &&
163 echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
164 git -C path/to/child rev-parse --git-common-dir >actual &&
165 test_cmp expect actual
168 test_expect_success 'git-path from worktree root' '
169 echo .git/objects >expect &&
170 git rev-parse --git-path objects >actual &&
171 test_cmp expect actual
174 test_expect_success 'git-path inside sub-dir' '
175 mkdir -p path/to/child &&
176 test_when_finished "rm -rf path" &&
177 echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
178 git -C path/to/child rev-parse --git-path objects >actual &&
179 test_cmp expect actual
182 test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
183 test_commit test_commit &&
184 echo true >expect &&
185 git clone --depth 1 --no-local . shallow &&
186 test_when_finished "rm -rf shallow" &&
187 git -C shallow rev-parse --is-shallow-repository >actual &&
188 test_cmp expect actual
191 test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
192 echo false >expect &&
193 git rev-parse --is-shallow-repository >actual &&
194 test_cmp expect actual
197 test_expect_success 'rev-parse --show-object-format in repo' '
198 test_oid algo >expect &&
199 git rev-parse --show-object-format >actual &&
200 test_cmp expect actual &&
201 git rev-parse --show-object-format=storage >actual &&
202 test_cmp expect actual &&
203 git rev-parse --show-object-format=input >actual &&
204 test_cmp expect actual &&
205 git rev-parse --show-object-format=output >actual &&
206 test_cmp expect actual &&
207 test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
208 grep "unknown mode for --show-object-format: squeamish-ossifrage" err
211 test_expect_success 'rev-parse --show-ref-format' '
212 test_detect_ref_format >expect &&
213 git rev-parse --show-ref-format >actual &&
214 test_cmp expect actual
217 test_expect_success 'rev-parse --show-ref-format with invalid storage' '
218 test_when_finished "rm -rf repo" &&
219 git init repo &&
221 cd repo &&
222 git config extensions.refstorage broken &&
223 test_must_fail git rev-parse --show-ref-format 2>err &&
224 grep "error: invalid value for ${SQ}extensions.refstorage${SQ}: ${SQ}broken${SQ}" err
228 test_expect_success '--show-toplevel from subdir of working tree' '
229 pwd >expect &&
230 git -C sub/dir rev-parse --show-toplevel >actual &&
231 test_cmp expect actual
234 test_expect_success '--show-toplevel from inside .git' '
235 test_must_fail git -C .git rev-parse --show-toplevel
238 test_expect_success 'showing the superproject correctly' '
239 git rev-parse --show-superproject-working-tree >out &&
240 test_must_be_empty out &&
242 test_create_repo super &&
243 test_commit -C super test_commit &&
244 test_create_repo sub &&
245 test_commit -C sub test_commit &&
246 git -c protocol.file.allow=always \
247 -C super submodule add ../sub dir/sub &&
248 echo $(pwd)/super >expect &&
249 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
250 test_cmp expect out &&
252 test_commit -C super submodule_add &&
253 git -C super checkout -b branch1 &&
254 git -C super/dir/sub checkout -b branch1 &&
255 test_commit -C super/dir/sub branch1_commit &&
256 git -C super add dir/sub &&
257 test_commit -C super branch1_commit &&
258 git -C super checkout -b branch2 main &&
259 git -C super/dir/sub checkout -b branch2 main &&
260 test_commit -C super/dir/sub branch2_commit &&
261 git -C super add dir/sub &&
262 test_commit -C super branch2_commit &&
263 test_must_fail git -C super merge branch1 &&
265 git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
266 test_cmp expect out
269 # at least one external project depends on this behavior:
270 test_expect_success 'rev-parse --since= unsqueezed ordering' '
271 x1=--since=1970-01-01T00:00:01Z &&
272 x2=--since=1970-01-01T00:00:02Z &&
273 x3=--since=1970-01-01T00:00:03Z &&
274 git rev-parse $x1 $x1 $x3 $x2 >actual &&
275 cat >expect <<-EOF &&
276 --max-age=1
277 --max-age=1
278 --max-age=3
279 --max-age=2
281 test_cmp expect actual
284 test_expect_success 'rev-parse --bisect includes bad, excludes good' '
285 test_commit_bulk 6 &&
287 git update-ref refs/bisect/bad-1 HEAD~1 &&
288 git update-ref refs/bisect/b HEAD~2 &&
289 git update-ref refs/bisect/bad-3 HEAD~3 &&
290 git update-ref refs/bisect/good-3 HEAD~3 &&
291 git update-ref refs/bisect/bad-4 HEAD~4 &&
292 git update-ref refs/bisect/go HEAD~4 &&
294 # Note: refs/bisect/b and refs/bisect/go should be ignored because they
295 # do not match the refs/bisect/bad or refs/bisect/good prefixes.
296 cat >expect <<-EOF &&
297 refs/bisect/bad-1
298 refs/bisect/bad-3
299 refs/bisect/bad-4
300 ^refs/bisect/good-3
303 git rev-parse --symbolic-full-name --bisect >actual &&
304 test_cmp expect actual
307 test_expect_success '--short= truncates to the actual hash length' '
308 git rev-parse HEAD >expect &&
309 git rev-parse --short=100 HEAD >actual &&
310 test_cmp expect actual
313 test_done