Skip to content

Commit b9457d6

Browse files
committed
fixup! mingw: implement a platform-specific strbuf_realpath()
Let's be careful to make this function work near the drive root: to resolve the drive root path itself, we _need_ a trailing backslash: if a file handle to the path `C:` is created, it does not actually refer to the drive root. Instead, it refers to this very Windows-only concept of a "per-drive current directory". This also requires the code that wants to re-append the last component to be more careful and only append a slash _if necessary_. This commit fixes the problem with `scalar unregister C:/foo` that was reported at #4200. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 622f956 commit b9457d6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

compat/mingw.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,15 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
14101410

14111411
if (p != wpath && (last_component = find_last_dir_sep(path))) {
14121412
append = xstrdup(last_component + 1); /* skip directory separator */
1413-
*p = L'\0';
1413+
/*
1414+
* Do not strip the trailing slash at the drive root, otherwise
1415+
* the path would be e.g. `C:` (which resolves to the
1416+
* _current_ directory on that drive).
1417+
*/
1418+
if (p[-1] == L':')
1419+
p[1] = L'\0';
1420+
else
1421+
*p = L'\0';
14141422
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
14151423
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
14161424
NULL, OPEN_EXISTING,
@@ -1438,7 +1446,7 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
14381446

14391447
if (append) {
14401448
/* Use forward-slash, like `normalize_ntpath()` */
1441-
strbuf_addch(resolved, '/');
1449+
strbuf_complete(resolved, '/');
14421450
strbuf_addstr(resolved, append);
14431451
FREE_AND_NULL(append);
14441452
}

t/t0060-path-utils.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
237237
test "$sym" = "$(test-tool path-utils real_path "$dir2/syml")"
238238
'
239239

240+
test_expect_success MINGW 'real path works near drive root' '
241+
# we need a non-existing path at the drive root; simply skip if C:/xyz exists
242+
test -e C:/xyz ||
243+
test C:/xyz = $(test-tool path-utils real_path C:/xyz)
244+
'
245+
240246
test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
241247
ln -s target symlink &&
242248
test "$(test-tool path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"

0 commit comments

Comments
 (0)