Make git_path_root() cope with windows network paths Fix libgit2/libgit2sharp#125
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
diff --git a/src/path.c b/src/path.c
index 0f45d71..45cc94e 100644
--- a/src/path.c
+++ b/src/path.c
@@ -171,6 +171,15 @@ int git_path_root(const char *path)
/* Does the root of the path look like a windows drive ? */
if (isalpha(path[0]) && (path[1] == ':'))
offset += 2;
+
+ /* Are we dealing with a network path? */
+ else if (path[0] == '/' && path[1] == '/') {
+ offset += 2;
+
+ /* Skip the computer name segment */
+ while (*(path + offset) && *(path + offset) != '/')
+ offset++;
+ }
#endif
if (*(path + offset) == '/')
diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c
index c07362f..c1e3ef2 100644
--- a/tests-clar/core/path.c
+++ b/tests-clar/core/path.c
@@ -388,3 +388,18 @@ void test_core_path__11_walkup(void)
git_buf_free(&p);
}
+
+void test_core_path__12_offset_to_path_root(void)
+{
+ cl_assert(git_path_root("non/rooted/path") == -1);
+ cl_assert(git_path_root("/rooted/path") == 0);
+
+#ifdef GIT_WIN32
+ /* Windows specific tests */
+ cl_assert(git_path_root("C:non/rooted/path") == -1);
+ cl_assert(git_path_root("C:/rooted/path") == 2);
+ cl_assert(git_path_root("//computername/sharefolder/resource") == 14);
+ cl_assert(git_path_root("//computername/sharefolder") == 14);
+ cl_assert(git_path_root("//computername") == -1);
+#endif
+}