Skip to content

Commit 723b463

Browse files
committed
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 <[email protected]>
1 parent 9c92915 commit 723b463

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

compat/mingw.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,13 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
938938
char *mingw_getcwd(char *pointer, int len)
939939
{
940940
int i;
941-
wchar_t wpointer[MAX_PATH];
942-
if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
941+
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
942+
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
943+
944+
if (ret < 0 || ret >= ARRAY_SIZE(cwd))
945+
return NULL;
946+
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
947+
if (!ret || ret >= ARRAY_SIZE(wpointer))
943948
return NULL;
944949
if (xwcstoutf(pointer, wpointer, len) < 0)
945950
return NULL;

0 commit comments

Comments
 (0)