Git 2.30.9
[git.git] / t / t0060-path-utils.sh
blobf538264cdd33826ca1bad474256b438e5606fa56
1 #!/bin/sh
3 # Copyright (c) 2008 David Reiss
6 test_description='Test various path utilities'
8 . ./test-lib.sh
10 norm_path() {
11 expected=$(test-tool path-utils print_path "$2")
12 test_expect_success $3 "normalize path: $1 => $2" \
13 "test \"\$(test-tool path-utils normalize_path_copy '$1')\" = '$expected'"
16 relative_path() {
17 expected=$(test-tool path-utils print_path "$3")
18 test_expect_success $4 "relative path: $1 $2 => $3" \
19 "test \"\$(test-tool path-utils relative_path '$1' '$2')\" = '$expected'"
22 test_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'
29 test_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
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:
40 rootoff=$(test-tool path-utils normalize_path_copy / | wc -c)
41 # Account for the trailing LF:
42 if test $rootoff = 2; then
43 rootoff= # we are on Unix
44 else
45 rootoff=$(($rootoff-1))
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-tool path-utils print_path /)" in
50 */) rootslash=1;;
51 *) rootslash=0;;
52 esac
55 ancestor() {
56 # We do some math with the expected ancestor length.
57 expected=$3
58 case "$rootoff,$expected,$2" in
59 *,*,//*) ;; # leave UNC paths alone
60 [0-9]*,[0-9]*,/*)
61 # On Windows, expect MSYS2 pseudo root translation for
62 # Unix-style absolute paths
63 expected=$(($expected-$rootslash+$rootoff))
65 esac
66 test_expect_success $4 "longest ancestor: $1 $2 => $expected" \
67 "actual=\$(test-tool path-utils longest_ancestor_length '$1' '$2') &&
68 test \"\$actual\" = '$expected'"
71 # Some absolute path tests should be skipped on Windows due to path mangling
72 # on POSIX-style absolute paths
73 case $(uname -s) in
74 *MINGW*)
76 *CYGWIN*)
79 test_set_prereq POSIX
81 esac
83 test_expect_success basename 'test-tool path-utils basename'
84 test_expect_success dirname 'test-tool path-utils dirname'
86 norm_path "" ""
87 norm_path . ""
88 norm_path ./ ""
89 norm_path ./. ""
90 norm_path ./.. ++failed++
91 norm_path ../. ++failed++
92 norm_path ./../.// ++failed++
93 norm_path dir/.. ""
94 norm_path dir/sub/../.. ""
95 norm_path dir/sub/../../.. ++failed++
96 norm_path dir dir
97 norm_path dir// dir/
98 norm_path ./dir dir
99 norm_path dir/. dir/
100 norm_path dir///./ dir/
101 norm_path dir//sub/.. dir/
102 norm_path dir/sub/../ dir/
103 norm_path dir/sub/../. dir/
104 norm_path dir/s1/../s2/ dir/s2/
105 norm_path d1/s1///s2/..//../s3/ d1/s3/
106 norm_path d1/s1//../s2/../../d2 d2
107 norm_path d1/.../d2 d1/.../d2
108 norm_path d1/..././../d2 d1/d2
110 norm_path / /
111 norm_path // / POSIX
112 norm_path /// / POSIX
113 norm_path /. /
114 norm_path /./ / POSIX
115 norm_path /./.. ++failed++ POSIX
116 norm_path /../. ++failed++
117 norm_path /./../.// ++failed++ POSIX
118 norm_path /dir/.. / POSIX
119 norm_path /dir/sub/../.. / POSIX
120 norm_path /dir/sub/../../.. ++failed++ POSIX
121 norm_path /dir /dir
122 norm_path /dir// /dir/
123 norm_path /./dir /dir
124 norm_path /dir/. /dir/
125 norm_path /dir///./ /dir/
126 norm_path /dir//sub/.. /dir/
127 norm_path /dir/sub/../ /dir/
128 norm_path //dir/sub/../. /dir/ POSIX
129 norm_path /dir/s1/../s2/ /dir/s2/
130 norm_path /d1/s1///s2/..//../s3/ /d1/s3/
131 norm_path /d1/s1//../s2/../../d2 /d2
132 norm_path /d1/.../d2 /d1/.../d2
133 norm_path /d1/..././../d2 /d1/d2
135 ancestor / / -1
136 ancestor /foo / 0
137 ancestor /foo /fo -1
138 ancestor /foo /foo -1
139 ancestor /foo /bar -1
140 ancestor /foo /foo/bar -1
141 ancestor /foo /foo:/bar -1
142 ancestor /foo /:/foo:/bar 0
143 ancestor /foo /foo:/:/bar 0
144 ancestor /foo /:/bar:/foo 0
145 ancestor /foo/bar / 0
146 ancestor /foo/bar /fo -1
147 ancestor /foo/bar /foo 4
148 ancestor /foo/bar /foo/ba -1
149 ancestor /foo/bar /:/fo 0
150 ancestor /foo/bar /foo:/foo/ba 4
151 ancestor /foo/bar /bar -1
152 ancestor /foo/bar /fo -1
153 ancestor /foo/bar /foo:/bar 4
154 ancestor /foo/bar /:/foo:/bar 4
155 ancestor /foo/bar /foo:/:/bar 4
156 ancestor /foo/bar /:/bar:/fo 0
157 ancestor /foo/bar /:/bar 0
158 ancestor /foo/bar /foo 4
159 ancestor /foo/bar /foo:/bar 4
160 ancestor /foo/bar /bar -1
162 # Windows-specific: DOS drives, network shares
163 ancestor C:/Users/me C:/ 2 MINGW
164 ancestor D:/Users/me C:/ -1 MINGW
165 ancestor //server/share/my-directory //server/share/ 14 MINGW
167 test_expect_success 'strip_path_suffix' '
168 test c:/msysgit = $(test-tool path-utils strip_path_suffix \
169 c:/msysgit/libexec//git-core libexec/git-core)
172 test_expect_success 'absolute path rejects the empty string' '
173 test_must_fail test-tool path-utils absolute_path ""
176 test_expect_success MINGW '<drive-letter>:\\abc is an absolute path' '
177 for letter in : \" C Z 1 ä
179 path=$letter:\\abc &&
180 absolute="$(test-tool path-utils absolute_path "$path")" &&
181 test "$path" = "$absolute" || return 1
182 done
185 test_expect_success 'real path rejects the empty string' '
186 test_must_fail test-tool path-utils real_path ""
189 test_expect_success POSIX 'real path works on absolute paths 1' '
190 nopath="hopefully-absent-path" &&
191 test "/" = "$(test-tool path-utils real_path "/")" &&
192 test "/$nopath" = "$(test-tool path-utils real_path "/$nopath")"
195 test_expect_success 'real path works on absolute paths 2' '
196 nopath="hopefully-absent-path" &&
197 # Find an existing top-level directory for the remaining tests:
198 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
199 test "$d" = "$(test-tool path-utils real_path "$d")" &&
200 test "$d/$nopath" = "$(test-tool path-utils real_path "$d/$nopath")"
203 test_expect_success POSIX 'real path removes extra leading slashes' '
204 nopath="hopefully-absent-path" &&
205 test "/" = "$(test-tool path-utils real_path "///")" &&
206 test "/$nopath" = "$(test-tool path-utils real_path "///$nopath")" &&
207 # Find an existing top-level directory for the remaining tests:
208 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
209 test "$d" = "$(test-tool path-utils real_path "//$d")" &&
210 test "$d/$nopath" = "$(test-tool path-utils real_path "//$d/$nopath")"
213 test_expect_success 'real path removes other extra slashes' '
214 nopath="hopefully-absent-path" &&
215 # Find an existing top-level directory for the remaining tests:
216 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
217 test "$d" = "$(test-tool path-utils real_path "$d///")" &&
218 test "$d/$nopath" = "$(test-tool path-utils real_path "$d///$nopath")"
221 test_expect_success SYMLINKS 'real path works on symlinks' '
222 mkdir first &&
223 ln -s ../.git first/.git &&
224 mkdir second &&
225 ln -s ../first second/other &&
226 mkdir third &&
227 dir="$(cd .git; pwd -P)" &&
228 dir2=third/../second/other/.git &&
229 test "$dir" = "$(test-tool path-utils real_path $dir2)" &&
230 file="$dir"/index &&
231 test "$file" = "$(test-tool path-utils real_path $dir2/index)" &&
232 basename=blub &&
233 test "$dir/$basename" = "$(cd .git && test-tool path-utils real_path "$basename")" &&
234 ln -s ../first/file .git/syml &&
235 sym="$(cd first; pwd -P)"/file &&
236 test "$sym" = "$(test-tool path-utils real_path "$dir2/syml")"
239 test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
240 ln -s target symlink &&
241 test "$(test-tool path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
244 test_expect_success 'prefix_path works with only absolute path to work tree' '
245 echo "" >expected &&
246 test-tool path-utils prefix_path prefix "$(pwd)" >actual &&
247 test_cmp expected actual
250 test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
251 test_must_fail test-tool path-utils prefix_path prefix "$(pwd)a"
254 test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
255 git init repo &&
256 ln -s repo repolink &&
257 test "a" = "$(cd repo && test-tool path-utils prefix_path prefix "$(pwd)/../repolink/a")"
260 relative_path /foo/a/b/c/ /foo/a/b/ c/
261 relative_path /foo/a/b/c/ /foo/a/b c/
262 relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
263 relative_path /foo/a/b /foo/a/b ./
264 relative_path /foo/a/b/ /foo/a/b ./
265 relative_path /foo/a /foo/a/b ../
266 relative_path / /foo/a/b/ ../../../
267 relative_path /foo/a/c /foo/a/b/ ../c
268 relative_path /foo/a/c /foo/a/b ../c
269 relative_path /foo/x/y /foo/a/b/ ../../x/y
270 relative_path /foo/a/b "<empty>" /foo/a/b
271 relative_path /foo/a/b "<null>" /foo/a/b
272 relative_path foo/a/b/c/ foo/a/b/ c/
273 relative_path foo/a/b/c/ foo/a/b c/
274 relative_path foo/a/b//c foo/a//b c
275 relative_path foo/a/b/ foo/a/b/ ./
276 relative_path foo/a/b/ foo/a/b ./
277 relative_path foo/a foo/a/b ../
278 relative_path foo/x/y foo/a/b ../../x/y
279 relative_path foo/a/c foo/a/b ../c
280 relative_path foo/a/b /foo/x/y foo/a/b
281 relative_path /foo/a/b foo/x/y /foo/a/b
282 relative_path d:/a/b D:/a/c ../b MINGW
283 relative_path C:/a/b D:/a/c C:/a/b MINGW
284 relative_path foo/a/b "<empty>" foo/a/b
285 relative_path foo/a/b "<null>" foo/a/b
286 relative_path "<empty>" /foo/a/b ./
287 relative_path "<empty>" "<empty>" ./
288 relative_path "<empty>" "<null>" ./
289 relative_path "<null>" "<empty>" ./
290 relative_path "<null>" "<null>" ./
291 relative_path "<null>" /foo/a/b ./
293 test_git_path A=B info/grafts .git/info/grafts
294 test_git_path GIT_GRAFT_FILE=foo info/grafts foo
295 test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
296 test_git_path GIT_INDEX_FILE=foo index foo
297 test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
298 test_git_path GIT_INDEX_FILE=foo index2 .git/index2
299 test_expect_success 'setup fake objects directory foo' 'mkdir foo'
300 test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
301 test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
302 test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
303 test_expect_success 'setup common repository' 'git --git-dir=bar init'
304 test_git_path GIT_COMMON_DIR=bar index .git/index
305 test_git_path GIT_COMMON_DIR=bar index.lock .git/index.lock
306 test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
307 test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
308 test_git_path GIT_COMMON_DIR=bar logs/HEAD.lock .git/logs/HEAD.lock
309 test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
310 test_git_path GIT_COMMON_DIR=bar logs/refs bar/logs/refs
311 test_git_path GIT_COMMON_DIR=bar logs/refs/ bar/logs/refs/
312 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
313 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
314 test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
315 test_git_path GIT_COMMON_DIR=bar objects bar/objects
316 test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
317 test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
318 test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
319 test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
320 test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
321 test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
322 test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
323 test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
324 test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
325 test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
326 test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
327 test_git_path GIT_COMMON_DIR=bar config bar/config
328 test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
329 test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
330 test_git_path GIT_COMMON_DIR=bar common bar/common
331 test_git_path GIT_COMMON_DIR=bar common/file bar/common/file
333 # In the tests below, $(pwd) must be used because it is a native path on
334 # Windows and avoids MSYS's path mangling (which simplifies "foo/../bar" and
335 # strips the dot from trailing "/.").
337 test_submodule_relative_url "../" "../foo" "../submodule" "../../submodule"
338 test_submodule_relative_url "../" "../foo/bar" "../submodule" "../../foo/submodule"
339 test_submodule_relative_url "../" "../foo/submodule" "../submodule" "../../foo/submodule"
340 test_submodule_relative_url "../" "./foo" "../submodule" "../submodule"
341 test_submodule_relative_url "../" "./foo/bar" "../submodule" "../foo/submodule"
342 test_submodule_relative_url "../../../" "../foo/bar" "../sub/a/b/c" "../../../../foo/sub/a/b/c"
343 test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
344 test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
345 test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
347 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
348 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
349 test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
350 test_submodule_relative_url "(null)" "../foo/bar" "../submodule" "../foo/submodule"
351 test_submodule_relative_url "(null)" "../foo/submodule" "../submodule" "../foo/submodule"
352 test_submodule_relative_url "(null)" "../foo" "../submodule" "../submodule"
353 test_submodule_relative_url "(null)" "./foo/bar" "../submodule" "foo/submodule"
354 test_submodule_relative_url "(null)" "./foo" "../submodule" "submodule"
355 test_submodule_relative_url "(null)" "//somewhere else/repo" "../subrepo" "//somewhere else/subrepo"
356 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../subrepo" "//subrepo"
357 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../subrepo" "/subrepo"
358 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../../subrepo" "subrepo"
359 test_submodule_relative_url "(null)" "$(pwd)/subsuper_update_r" "../subsubsuper_update_r" "$(pwd)/subsubsuper_update_r"
360 test_submodule_relative_url "(null)" "$(pwd)/super_update_r2" "../subsuper_update_r" "$(pwd)/subsuper_update_r"
361 test_submodule_relative_url "(null)" "$(pwd)/." "../." "$(pwd)/."
362 test_submodule_relative_url "(null)" "$(pwd)" "./." "$(pwd)/."
363 test_submodule_relative_url "(null)" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
364 test_submodule_relative_url "(null)" "$(pwd)" "./å äö" "$(pwd)/å äö"
365 test_submodule_relative_url "(null)" "$(pwd)/." "../submodule" "$(pwd)/submodule"
366 test_submodule_relative_url "(null)" "$(pwd)/submodule" "../submodule" "$(pwd)/submodule"
367 test_submodule_relative_url "(null)" "$(pwd)/home2/../remote" "../bundle1" "$(pwd)/home2/../bundle1"
368 test_submodule_relative_url "(null)" "$(pwd)/submodule_update_repo" "./." "$(pwd)/submodule_update_repo/."
369 test_submodule_relative_url "(null)" "file:///tmp/repo" "../subrepo" "file:///tmp/subrepo"
370 test_submodule_relative_url "(null)" "foo/bar" "../submodule" "foo/submodule"
371 test_submodule_relative_url "(null)" "foo" "../submodule" "submodule"
372 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../subrepo" "helper:://hostname/subrepo"
373 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../subrepo" "helper:://subrepo"
374 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../subrepo" "helper::/subrepo"
375 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../subrepo" "helper::subrepo"
376 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../subrepo" "helper:subrepo"
377 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../../subrepo" ".:subrepo"
378 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../subrepo" "ssh://hostname/subrepo"
379 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../subrepo" "ssh://subrepo"
380 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../subrepo" "ssh:/subrepo"
381 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../subrepo" "ssh:subrepo"
382 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../../subrepo" ".:subrepo"
383 test_submodule_relative_url "(null)" "ssh://hostname:22/repo" "../subrepo" "ssh://hostname:22/subrepo"
384 test_submodule_relative_url "(null)" "user@host:path/to/repo" "../subrepo" "user@host:path/to/subrepo"
385 test_submodule_relative_url "(null)" "user@host:repo" "../subrepo" "user@host:subrepo"
386 test_submodule_relative_url "(null)" "user@host:repo" "../../subrepo" ".:subrepo"
388 test_expect_success 'match .gitmodules' '
389 test-tool path-utils is_dotgitmodules \
390 .gitmodules \
392 .git${u200c}modules \
394 .Gitmodules \
395 .gitmoduleS \
397 ".gitmodules " \
398 ".gitmodules." \
399 ".gitmodules " \
400 ".gitmodules. " \
401 ".gitmodules ." \
402 ".gitmodules.." \
403 ".gitmodules " \
404 ".gitmodules. " \
405 ".gitmodules . " \
406 ".gitmodules ." \
408 ".Gitmodules " \
409 ".Gitmodules." \
410 ".Gitmodules " \
411 ".Gitmodules. " \
412 ".Gitmodules ." \
413 ".Gitmodules.." \
414 ".Gitmodules " \
415 ".Gitmodules. " \
416 ".Gitmodules . " \
417 ".Gitmodules ." \
419 GITMOD~1 \
420 gitmod~1 \
421 GITMOD~2 \
422 gitmod~3 \
423 GITMOD~4 \
425 "GITMOD~1 " \
426 "gitmod~2." \
427 "GITMOD~3 " \
428 "gitmod~4. " \
429 "GITMOD~1 ." \
430 "gitmod~2 " \
431 "GITMOD~3. " \
432 "gitmod~4 . " \
434 GI7EBA~1 \
435 gi7eba~9 \
437 GI7EB~10 \
438 GI7EB~11 \
439 GI7EB~99 \
440 GI7EB~10 \
441 GI7E~100 \
442 GI7E~101 \
443 GI7E~999 \
444 ~1000000 \
445 ~9999999 \
447 .gitmodules:\$DATA \
448 "gitmod~4 . :\$DATA" \
450 --not \
451 ".gitmodules x" \
452 ".gitmodules .x" \
454 " .gitmodules" \
456 ..gitmodules \
458 gitmodules \
460 .gitmodule \
462 ".gitmodules x " \
463 ".gitmodules .x" \
465 GI7EBA~ \
466 GI7EBA~0 \
467 GI7EBA~~1 \
468 GI7EBA~X \
469 Gx7EBA~1 \
470 GI7EBX~1 \
472 GI7EB~1 \
473 GI7EB~01 \
474 GI7EB~1X \
476 .gitmodules,:\$DATA
479 test_expect_success MINGW 'is_valid_path() on Windows' '
480 test-tool path-utils is_valid_path \
481 win32 \
482 "win32 x" \
483 ../hello.txt \
484 C:\\git \
485 comm \
486 conout.c \
487 com0.c \
488 lptN \
490 --not \
491 "win32 " \
492 "win32 /x " \
493 "win32." \
494 "win32 . ." \
495 .../hello.txt \
496 colon:test \
497 "AUX.c" \
498 "abc/conOut\$ .xyz/test" \
499 lpt8 \
500 com9.c \
501 "lpt*" \
502 Nul \
503 "PRN./abc"
506 test_done