mingw: ensure `getcwd()` reports the correct case

When switching the current working directory, say, in PowerShell, it is quite possible to use a different capitalization than the one that is recorded on disk. While doing the same in `cmd.exe` adjusts the capitalization magically, that does not happen in PowerShell so that `getcwd()` returns the current directory in a different way than is recorded on disk. Typically this creates no problems except when you call git log . in a subdirectory called, say, "GIT/" but you switched to "Git/" and your `getcwd()` reports the latter, then Git won't understand that you wanted to see the history as per the `GIT/` subdirectory but it thinks you wanted to see the history of some directory that may have existed in the past (but actually never did). So let's be extra careful to adjust the capitalization of the current directory before working with it. Reported by a few PowerShell power users ;-) Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 23, 2018 at 03:52 UTC 937974fc65f517a67bfb09086b3726e3498d65b2
1 file changed +9 -2
compat/mingw.c
+9 -2
@@ -917,8 +917,15 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
917
918 char *mingw_getcwd(char *pointer, int len)
919 {
920 - wchar_t wpointer[MAX_PATH];
921 - if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
920 + wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
921 + DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
922 +
923 + if (!ret || ret >= ARRAY_SIZE(cwd)) {
924 + errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
925 + return NULL;
926 + }
927 + ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
928 + if (!ret || ret >= ARRAY_SIZE(wpointer))
929 return NULL;
930 if (xwcstoutf(pointer, wpointer, len) < 0)
931 return NULL;