Commit f7c6795f by Patrick Steinhardt

path: only treat paths starting with '\' as absolute on Win32

Windows-based systems treat paths starting with '\' as absolute,
either referring to the current drive's root (e.g. "\foo" might
refer to "C:\foo") or to a network path (e.g. "\\host\foo"). On
the other hand, (most?) systems that are not based on Win32
accept backslashes as valid characters that may be part of the
filename, and thus we cannot treat them to identify absolute
paths.

Change the logic to only paths starting with '\' as absolute on
the Win32 platform. Add tests to avoid regressions and document
behaviour.
parent 01dda5ff
......@@ -276,9 +276,12 @@ int git_path_root(const char *path)
while (path[offset] && path[offset] != '/' && path[offset] != '\\')
offset++;
}
if (path[offset] == '\\')
return offset;
#endif
if (path[offset] == '/' || path[offset] == '\\')
if (path[offset] == '/')
return offset;
return -1; /* Not a real error - signals that path is not rooted */
......
......@@ -343,6 +343,16 @@ void test_path_core__join_unrooted(void)
test_join_unrooted("c:/foo", 2, "c:/foo", "c:/asdf");
test_join_unrooted("c:/foo/bar", 2, "c:/foo/bar", "c:/asdf");
#ifdef GIT_WIN32
/* Paths starting with '\\' are absolute */
test_join_unrooted("\\bar", 0, "\\bar", "c:/foo/");
test_join_unrooted("\\\\network\\bar", 9, "\\\\network\\bar", "c:/foo/");
#else
/* Paths starting with '\\' are not absolute on non-Windows systems */
test_join_unrooted("/foo/\\bar", 4, "\\bar", "/foo");
test_join_unrooted("c:/foo/\\bar", 7, "\\bar", "c:/foo/");
#endif
/* Base is returned when it's provided and is the prefix */
test_join_unrooted("c:/foo/bar/foobar", 6, "c:/foo/bar/foobar", "c:/foo");
test_join_unrooted("c:/foo/bar/foobar", 10, "c:/foo/bar/foobar", "c:/foo/bar");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment