mingw: do resolve symlinks in `getcwd()`

As pointed out in https://github.com/git-for-windows/git/issues/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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 9, 2026 at 20:05 UTC f0af8b4aae3397d6bbe68a3c09f558cab983eaff
1 file changed +7 -11
compat/mingw.c
+7 -11
@@ -1239,18 +1239,16 @@ char *mingw_getcwd(char *pointer, int len)
1239 {
1240 wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
1241 DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1242 + HANDLE hnd;
1243
1244 if (!ret || ret >= ARRAY_SIZE(cwd)) {
1245 errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
1246 return NULL;
1247 }
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) {
1252 ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
1253 CloseHandle(hnd);
1254 if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1259,13 +1257,11 @@ char *mingw_getcwd(char *pointer, int len)
1257 return NULL;
1258 return pointer;
1259 }
1262 - if (!ret || ret >= ARRAY_SIZE(wpointer))
1263 - return NULL;
1264 - if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1260 + if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
1261 errno = ENOENT;
1262 return NULL;
1263 }
1268 - if (xwcstoutf(pointer, wpointer, len) < 0)
1264 + if (xwcstoutf(pointer, cwd, len) < 0)
1265 return NULL;
1266 convert_slashes(pointer);
1267 return pointer;