Skip to content

Commit 5d59520

Browse files
committed
path: get correct dirname for Windows root
Getting the dirname of a filesystem root should return the filesystem root itself. E.g. the dirname of "/" is always "/". On Windows, we emulate this behavior and as such, we should return e.g. "C:/" if calling dirname on "C:/". But we currently fail to do so and instead return ".", as we do not check if we actually have a Windows prefix before stripping off the last directory component. Fix this by calling out to `win32_prefix_length` immediately after stripping trailing slashes, returning early if we have a prefix.
1 parent d8c0607 commit 5d59520

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/path.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ int git_path_dirname_r(git_buf *buffer, const char *path)
159159
while (endp > path && *endp == '/')
160160
endp--;
161161

162+
if ((len = win32_prefix_length(path, endp - path + 1)) > 0)
163+
goto Exit;
164+
162165
/* Find the start of the dir */
163166
while (endp > path && *endp != '/')
164167
endp--;

tests/core/path.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ void test_core_path__00_dirname(void)
8989
check_dirname(REP16("/abc"), REP15("/abc"));
9090

9191
#ifdef GIT_WIN32
92+
check_dirname("C:/", "C:/");
9293
check_dirname("C:/path/", "C:/");
9394
check_dirname("C:/path", "C:/");
95+
check_dirname("//computername/", "//computername/");
9496
check_dirname("//computername/path/", "//computername/");
9597
check_dirname("//computername/path", "//computername/");
9698
check_dirname("//computername/sub/path/", "//computername/sub");

0 commit comments

Comments
 (0)