]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0060-path-utils.sh
submodule: ignore trailing slash on superproject URL
[thirdparty/git.git] / t / t0060-path-utils.sh
CommitLineData
ae299be0
DR
1#!/bin/sh
2#
3# Copyright (c) 2008 David Reiss
4#
5
6test_description='Test various path utilities'
7
8. ./test-lib.sh
9
2718e852 10norm_path() {
7ffd18fc 11 expected=$(test-path-utils print_path "$2")
2718e852 12 test_expect_success $3 "normalize path: $1 => $2" \
abd4284b 13 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
ae299be0
DR
14}
15
203439b2 16relative_path() {
7ffd18fc 17 expected=$(test-path-utils print_path "$3")
203439b2 18 test_expect_success $4 "relative path: $1 $2 => $3" \
abd4284b 19 "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
203439b2
JX
20}
21
63e95beb
SB
22test_submodule_relative_url() {
23 test_expect_success "test_submodule_relative_url: $1 $2 $3 => $4" "
24 actual=\$(git submodule--helper resolve-relative-url-test '$1' '$2' '$3') &&
25 test \"\$actual\" = '$4'
26 "
27}
28
557bd833
NTND
29test_git_path() {
30 test_expect_success "git-path $1 $2 => $3" "
31 $1 git rev-parse --git-path $2 >actual &&
32 echo $3 >expect &&
33 test_cmp expect actual
34 "
35}
36
2718e852
JS
37# On Windows, we are using MSYS's bash, which mangles the paths.
38# Absolute paths are anchored at the MSYS installation directory,
39# which means that the path / accounts for this many characters:
40rootoff=$(test-path-utils normalize_path_copy / | wc -c)
41# Account for the trailing LF:
28baf82e 42if test $rootoff = 2; then
2718e852
JS
43 rootoff= # we are on Unix
44else
45 rootoff=$(($rootoff-1))
fc56c7b3
JS
46 # In MSYS2, the root directory "/" is translated into a Windows
47 # directory *with* trailing slash. Let's test for that and adjust
48 # our expected longest ancestor length accordingly.
49 case "$(test-path-utils print_path /)" in
50 */) rootslash=1;;
51 *) rootslash=0;;
52 esac
2718e852
JS
53fi
54
0454dd93 55ancestor() {
2718e852
JS
56 # We do some math with the expected ancestor length.
57 expected=$3
58 if test -n "$rootoff" && test "x$expected" != x-1; then
fc56c7b3
JS
59 expected=$(($expected-$rootslash))
60 test $expected -lt 0 ||
2718e852
JS
61 expected=$(($expected+$rootoff))
62 fi
63 test_expect_success "longest ancestor: $1 $2 => $expected" \
64 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
65 test \"\$actual\" = '$expected'"
0454dd93
DR
66}
67
abd4284b
JX
68# Some absolute path tests should be skipped on Windows due to path mangling
69# on POSIX-style absolute paths
2718e852
JS
70case $(uname -s) in
71*MINGW*)
72 ;;
73*)
74 test_set_prereq POSIX
75 ;;
76esac
77
7d1aaa68
JS
78test_expect_success basename 'test-path-utils basename'
79test_expect_success dirname 'test-path-utils dirname'
80
2718e852
JS
81norm_path "" ""
82norm_path . ""
83norm_path ./ ""
84norm_path ./. ""
85norm_path ./.. ++failed++
86norm_path ../. ++failed++
87norm_path ./../.// ++failed++
88norm_path dir/.. ""
89norm_path dir/sub/../.. ""
90norm_path dir/sub/../../.. ++failed++
91norm_path dir dir
92norm_path dir// dir/
93norm_path ./dir dir
94norm_path dir/. dir/
95norm_path dir///./ dir/
96norm_path dir//sub/.. dir/
97norm_path dir/sub/../ dir/
98norm_path dir/sub/../. dir/
99norm_path dir/s1/../s2/ dir/s2/
100norm_path d1/s1///s2/..//../s3/ d1/s3/
101norm_path d1/s1//../s2/../../d2 d2
102norm_path d1/.../d2 d1/.../d2
103norm_path d1/..././../d2 d1/d2
104
abd4284b 105norm_path / /
2718e852
JS
106norm_path // / POSIX
107norm_path /// / POSIX
abd4284b 108norm_path /. /
2718e852
JS
109norm_path /./ / POSIX
110norm_path /./.. ++failed++ POSIX
abd4284b 111norm_path /../. ++failed++
2718e852
JS
112norm_path /./../.// ++failed++ POSIX
113norm_path /dir/.. / POSIX
114norm_path /dir/sub/../.. / POSIX
115norm_path /dir/sub/../../.. ++failed++ POSIX
abd4284b
JX
116norm_path /dir /dir
117norm_path /dir// /dir/
118norm_path /./dir /dir
119norm_path /dir/. /dir/
120norm_path /dir///./ /dir/
121norm_path /dir//sub/.. /dir/
122norm_path /dir/sub/../ /dir/
2718e852 123norm_path //dir/sub/../. /dir/ POSIX
abd4284b
JX
124norm_path /dir/s1/../s2/ /dir/s2/
125norm_path /d1/s1///s2/..//../s3/ /d1/s3/
126norm_path /d1/s1//../s2/../../d2 /d2
127norm_path /d1/.../d2 /d1/.../d2
128norm_path /d1/..././../d2 /d1/d2
ae299be0 129
0454dd93 130ancestor / / -1
0454dd93
DR
131ancestor /foo / 0
132ancestor /foo /fo -1
133ancestor /foo /foo -1
0454dd93 134ancestor /foo /bar -1
0454dd93 135ancestor /foo /foo/bar -1
9e2326c7
MH
136ancestor /foo /foo:/bar -1
137ancestor /foo /:/foo:/bar 0
138ancestor /foo /foo:/:/bar 0
139ancestor /foo /:/bar:/foo 0
0454dd93
DR
140ancestor /foo/bar / 0
141ancestor /foo/bar /fo -1
0454dd93 142ancestor /foo/bar /foo 4
0454dd93
DR
143ancestor /foo/bar /foo/ba -1
144ancestor /foo/bar /:/fo 0
145ancestor /foo/bar /foo:/foo/ba 4
146ancestor /foo/bar /bar -1
9e2326c7
MH
147ancestor /foo/bar /fo -1
148ancestor /foo/bar /foo:/bar 4
149ancestor /foo/bar /:/foo:/bar 4
150ancestor /foo/bar /foo:/:/bar 4
151ancestor /foo/bar /:/bar:/fo 0
152ancestor /foo/bar /:/bar 0
153ancestor /foo/bar /foo 4
154ancestor /foo/bar /foo:/bar 4
155ancestor /foo/bar /bar -1
0454dd93 156
4fcc86b0
JS
157test_expect_success 'strip_path_suffix' '
158 test c:/msysgit = $(test-path-utils strip_path_suffix \
159 c:/msysgit/libexec//git-core libexec/git-core)
160'
8da650b4 161
a0601dc1 162test_expect_success 'absolute path rejects the empty string' '
17264bcc
MH
163 test_must_fail test-path-utils absolute_path ""
164'
165
3efe5d1d 166test_expect_success 'real path rejects the empty string' '
a5c45218
MH
167 test_must_fail test-path-utils real_path ""
168'
169
bacca785 170test_expect_success POSIX 'real path works on absolute paths 1' '
7bcf48da
MH
171 nopath="hopefully-absent-path" &&
172 test "/" = "$(test-path-utils real_path "/")" &&
bacca785
JS
173 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
174'
175
176test_expect_success 'real path works on absolute paths 2' '
177 nopath="hopefully-absent-path" &&
7bcf48da
MH
178 # Find an existing top-level directory for the remaining tests:
179 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
180 test "$d" = "$(test-path-utils real_path "$d")" &&
181 test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
182'
183
379a03ad
MH
184test_expect_success POSIX 'real path removes extra leading slashes' '
185 nopath="hopefully-absent-path" &&
186 test "/" = "$(test-path-utils real_path "///")" &&
187 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
188 # Find an existing top-level directory for the remaining tests:
189 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
190 test "$d" = "$(test-path-utils real_path "//$d")" &&
191 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
192'
193
194test_expect_success 'real path removes other extra slashes' '
195 nopath="hopefully-absent-path" &&
196 # Find an existing top-level directory for the remaining tests:
197 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
198 test "$d" = "$(test-path-utils real_path "$d///")" &&
199 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
200'
201
7bcf48da 202test_expect_success SYMLINKS 'real path works on symlinks' '
8da650b4
MH
203 mkdir first &&
204 ln -s ../.git first/.git &&
205 mkdir second &&
206 ln -s ../first second/other &&
207 mkdir third &&
208 dir="$(cd .git; pwd -P)" &&
209 dir2=third/../second/other/.git &&
210 test "$dir" = "$(test-path-utils real_path $dir2)" &&
211 file="$dir"/index &&
212 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
213 basename=blub &&
214 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
215 ln -s ../first/file .git/syml &&
216 sym="$(cd first; pwd -P)"/file &&
217 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
218'
219
655ee9ea 220test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
74af95d6
MEW
221 ln -s target symlink &&
222 test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
223'
224
e5aa1fc4
MEW
225test_expect_success 'prefix_path works with only absolute path to work tree' '
226 echo "" >expected &&
227 test-path-utils prefix_path prefix "$(pwd)" >actual &&
228 test_cmp expected actual
229'
230
e131daa4
MEW
231test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
232 test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
233'
234
235test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
236 git init repo &&
237 ln -s repo repolink &&
238 test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
239'
240
daf19a80
JX
241relative_path /foo/a/b/c/ /foo/a/b/ c/
242relative_path /foo/a/b/c/ /foo/a/b c/
243relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
244relative_path /foo/a/b /foo/a/b ./
245relative_path /foo/a/b/ /foo/a/b ./
246relative_path /foo/a /foo/a/b ../
247relative_path / /foo/a/b/ ../../../
248relative_path /foo/a/c /foo/a/b/ ../c
249relative_path /foo/a/c /foo/a/b ../c
250relative_path /foo/x/y /foo/a/b/ ../../x/y
251relative_path /foo/a/b "<empty>" /foo/a/b
252relative_path /foo/a/b "<null>" /foo/a/b
253relative_path foo/a/b/c/ foo/a/b/ c/
254relative_path foo/a/b/c/ foo/a/b c/
255relative_path foo/a/b//c foo/a//b c
256relative_path foo/a/b/ foo/a/b/ ./
257relative_path foo/a/b/ foo/a/b ./
258relative_path foo/a foo/a/b ../
259relative_path foo/x/y foo/a/b ../../x/y
260relative_path foo/a/c foo/a/b ../c
7fbd4221
JX
261relative_path foo/a/b /foo/x/y foo/a/b
262relative_path /foo/a/b foo/x/y /foo/a/b
263relative_path d:/a/b D:/a/c ../b MINGW
264relative_path C:/a/b D:/a/c C:/a/b MINGW
daf19a80
JX
265relative_path foo/a/b "<empty>" foo/a/b
266relative_path foo/a/b "<null>" foo/a/b
267relative_path "<empty>" /foo/a/b ./
268relative_path "<empty>" "<empty>" ./
269relative_path "<empty>" "<null>" ./
270relative_path "<null>" "<empty>" ./
271relative_path "<null>" "<null>" ./
272relative_path "<null>" /foo/a/b ./
203439b2 273
557bd833
NTND
274test_git_path A=B info/grafts .git/info/grafts
275test_git_path GIT_GRAFT_FILE=foo info/grafts foo
276test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
277test_git_path GIT_INDEX_FILE=foo index foo
278test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
279test_git_path GIT_INDEX_FILE=foo index2 .git/index2
280test_expect_success 'setup fake objects directory foo' 'mkdir foo'
281test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
282test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
283test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
c7b3a3d2
NTND
284test_expect_success 'setup common repository' 'git --git-dir=bar init'
285test_git_path GIT_COMMON_DIR=bar index .git/index
286test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
287test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
ce414b33
DT
288test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
289test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
290test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
291test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
c7b3a3d2
NTND
292test_git_path GIT_COMMON_DIR=bar objects bar/objects
293test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
294test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
295test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
6cfbdcb2 296test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
4e09cf2a 297test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
c7b3a3d2
NTND
298test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
299test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
300test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
301test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
ce414b33 302test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
c7b3a3d2
NTND
303test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
304test_git_path GIT_COMMON_DIR=bar config bar/config
305test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
306test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
557bd833 307
63e95beb
SB
308# In the tests below, the distinction between $PWD and $(pwd) is important:
309# on Windows, $PWD is POSIX style (/c/foo), $(pwd) has drive letter (c:/foo).
310
311test_submodule_relative_url "../" "../foo" "../submodule" "../../submodule"
312test_submodule_relative_url "../" "../foo/bar" "../submodule" "../../foo/submodule"
313test_submodule_relative_url "../" "../foo/submodule" "../submodule" "../../foo/submodule"
314test_submodule_relative_url "../" "./foo" "../submodule" "../submodule"
315test_submodule_relative_url "../" "./foo/bar" "../submodule" "../foo/submodule"
316test_submodule_relative_url "../../../" "../foo/bar" "../sub/a/b/c" "../../../../foo/sub/a/b/c"
317test_submodule_relative_url "../" "$PWD/addtest" "../repo" "$(pwd)/repo"
318test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
319test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
320
321test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
08788504 322test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
63e95beb
SB
323test_submodule_relative_url "(null)" "../foo/bar" "../submodule" "../foo/submodule"
324test_submodule_relative_url "(null)" "../foo/submodule" "../submodule" "../foo/submodule"
325test_submodule_relative_url "(null)" "../foo" "../submodule" "../submodule"
326test_submodule_relative_url "(null)" "./foo/bar" "../submodule" "foo/submodule"
327test_submodule_relative_url "(null)" "./foo" "../submodule" "submodule"
328test_submodule_relative_url "(null)" "//somewhere else/repo" "../subrepo" "//somewhere else/subrepo"
329test_submodule_relative_url "(null)" "$PWD/subsuper_update_r" "../subsubsuper_update_r" "$(pwd)/subsubsuper_update_r"
330test_submodule_relative_url "(null)" "$PWD/super_update_r2" "../subsuper_update_r" "$(pwd)/subsuper_update_r"
331test_submodule_relative_url "(null)" "$PWD/." "../." "$(pwd)/."
332test_submodule_relative_url "(null)" "$PWD" "./." "$(pwd)/."
333test_submodule_relative_url "(null)" "$PWD/addtest" "../repo" "$(pwd)/repo"
334test_submodule_relative_url "(null)" "$PWD" "./å äö" "$(pwd)/å äö"
335test_submodule_relative_url "(null)" "$PWD/." "../submodule" "$(pwd)/submodule"
336test_submodule_relative_url "(null)" "$PWD/submodule" "../submodule" "$(pwd)/submodule"
337test_submodule_relative_url "(null)" "$PWD/home2/../remote" "../bundle1" "$(pwd)/home2/../bundle1"
338test_submodule_relative_url "(null)" "$PWD/submodule_update_repo" "./." "$(pwd)/submodule_update_repo/."
339test_submodule_relative_url "(null)" "file:///tmp/repo" "../subrepo" "file:///tmp/subrepo"
340test_submodule_relative_url "(null)" "foo/bar" "../submodule" "foo/submodule"
341test_submodule_relative_url "(null)" "foo" "../submodule" "submodule"
342test_submodule_relative_url "(null)" "helper:://hostname/repo" "../subrepo" "helper:://hostname/subrepo"
343test_submodule_relative_url "(null)" "ssh://hostname/repo" "../subrepo" "ssh://hostname/subrepo"
344test_submodule_relative_url "(null)" "ssh://hostname:22/repo" "../subrepo" "ssh://hostname:22/subrepo"
345test_submodule_relative_url "(null)" "user@host:path/to/repo" "../subrepo" "user@host:path/to/subrepo"
346test_submodule_relative_url "(null)" "user@host:repo" "../subrepo" "user@host:subrepo"
347
ae299be0 348test_done