Skip to content

Commit 4e44062

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
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 0cd9a6c commit 4e44062

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
@@ -1234,18 +1234,16 @@ char *mingw_getcwd(char *pointer, int len)
12341234
{
12351235
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
12361236
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1237+
HANDLE hnd;
12371238

12381239
if (!ret || ret >= ARRAY_SIZE(cwd)) {
12391240
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
12401241
return NULL;
12411242
}
1242-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1243-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1244-
HANDLE hnd = CreateFileW(cwd, 0,
1245-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1246-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1247-
if (hnd == INVALID_HANDLE_VALUE)
1248-
return NULL;
1243+
hnd = CreateFileW(cwd, 0,
1244+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1245+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1246+
if (hnd != INVALID_HANDLE_VALUE) {
12491247
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
12501248
CloseHandle(hnd);
12511249
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1254,13 +1252,11 @@ char *mingw_getcwd(char *pointer, int len)
12541252
return NULL;
12551253
return pointer;
12561254
}
1257-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1258-
return NULL;
1259-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1255+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
12601256
errno = ENOENT;
12611257
return NULL;
12621258
}
1263-
if (xwcstoutf(pointer, wpointer, len) < 0)
1259+
if (xwcstoutf(pointer, cwd, len) < 0)
12641260
return NULL;
12651261
convert_slashes(pointer);
12661262
return pointer;

0 commit comments

Comments
 (0)