Skip to content

Commit f7bb4cc

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 9c4d1d1 commit f7bb4cc

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

compat/mingw.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,18 +1239,16 @@ char *mingw_getcwd(char *pointer, int len)
12391239
{
12401240
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12411241
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1242+
HANDLE hnd;
12421243

12431244
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12441245
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12451246
return NULL;
12461247
}
1247-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1248-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1249-
HANDLE hnd = CreateFileW(cwd, 0,
1250-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1251-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1252-
if (hnd == INVALID_HANDLE_VALUE)
1253-
return NULL;
1248+
hnd = CreateFileW(cwd, 0,
1249+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1250+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1251+
if (hnd != INVALID_HANDLE_VALUE) {
12541252
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12551253
CloseHandle(hnd);
12561254
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1259,13 +1257,11 @@ char *mingw_getcwd(char *pointer, int len)
12591257
return NULL;
12601258
return pointer;
12611259
}
1262-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1263-
return NULL;
1264-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1260+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12651261
errno = ENOENT;
12661262
return NULL;
12671263
}
1268-
if (xwcstoutf(pointer, wpointer, len) < 0)
1264+
if (xwcstoutf(pointer, cwd, len) < 0)
12691265
return NULL;
12701266
convert_slashes(pointer);
12711267
return pointer;

0 commit comments

Comments
 (0)