Skip to content

Commit efb8392

Browse files
dschoGit for Windows Build Agent
authored andcommitted
mingw: do resolve symlinks in getcwd()
As pointed out in #1676, the `git rev-parse --is-inside-work-tree` command currently fails when the current directory's path contains symbolic links. The underlying reason for this bug is that `getcwd()` is supposed to resolve symbolic links, but our `mingw_getcwd()` implementation did not. We do have all the building blocks for that, though: the `GetFinalPathByHandleW()` function will resolve symbolic links. However, we only called that function if `GetLongPathNameW()` failed, for historical reasons: the latter function was supported for a long time, but the former API function was introduced only with Windows Vista, and we used to support also Windows XP. With that support having been dropped, we are free to call the symbolic link-resolving function right away. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6a5a0dd commit efb8392

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

compat/mingw.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,18 +1051,16 @@ char *mingw_getcwd(char *pointer, int len)
10511051
{
10521052
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
10531053
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1054+
HANDLE hnd;
10541055

10551056
if (!ret || ret >= ARRAY_SIZE(cwd)) {
10561057
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
10571058
return NULL;
10581059
}
1059-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1060-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1061-
HANDLE hnd = CreateFileW(cwd, 0,
1062-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1063-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1064-
if (hnd == INVALID_HANDLE_VALUE)
1065-
return NULL;
1060+
hnd = CreateFileW(cwd, 0,
1061+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1062+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1063+
if (hnd != INVALID_HANDLE_VALUE) {
10661064
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
10671065
CloseHandle(hnd);
10681066
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1071,9 +1069,7 @@ char *mingw_getcwd(char *pointer, int len)
10711069
return NULL;
10721070
return pointer;
10731071
}
1074-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1075-
return NULL;
1076-
if (xwcstoutf(pointer, wpointer, len) < 0)
1072+
if (xwcstoutf(pointer, cwd, len) < 0)
10771073
return NULL;
10781074
convert_slashes(pointer);
10791075
return pointer;

0 commit comments

Comments
 (0)