git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] mingw: ensure `getcwd()` reports the correct case

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 24, 2018, 09:19 UTC
Message-ID
<e13ae233822c8e4a4504e3294adf3e0de38a1990.1540372778.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.54.v2.git.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 compat/mingw.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 18caf2196..2c3e27ce9 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -917,8 +917,15 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
 
 char *mingw_getcwd(char *pointer, int len)
 {
-	wchar_t wpointer[MAX_PATH];
-	if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
+	wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
+	DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
+
+	if (!ret || ret >= ARRAY_SIZE(cwd)) {
+		errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
+		return NULL;
+	}
+	ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
+	if (!ret || ret >= ARRAY_SIZE(wpointer))
 		return NULL;
 	if (xwcstoutf(pointer, wpointer, len) < 0)
 		return NULL;
-- 
gitgitgadget
Previous: Anton Serbulov via GitGitGadgetNext: Junio C Hamano
Message 9 of 12 in “Work around case-insensitivity issues with cwd on Windows”
  1. 0/2 Work around case-insensitivity issues with cwd on WindowsJohannes Schindelin via GitGitGadget, Oct 23, 2018
  2. 1/2 mingw: ensure `getcwd()` reports the correct caseJohannes Schindelin via GitGitGadget, Oct 23, 2018
  3. 2/2 mingw: fix getcwd when the parent directory cannot be queriedAnton Serbulov via GitGitGadget, Oct 23, 2018
  4. Stephen & Linda SmithOct 23, 2018
  5. Stephen SmithOct 23, 2018
  6. Johannes SchindelinOct 24, 2018
  7. 0/2 Work around case-insensitivity issues with cwd on WindowsJohannes Schindelin via GitGitGadget, Oct 24, 2018
  8. 2/2 mingw: fix getcwd when the parent directory cannot be queriedAnton Serbulov via GitGitGadget, Oct 24, 2018
  9. 1/2 mingw: ensure `getcwd()` reports the correct caseJohannes Schindelin via GitGitGadget, Oct 24, 2018
  10. Junio C HamanoOct 24, 2018
  11. Johannes SchindelinOct 24, 2018
  12. Junio C HamanoOct 25, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.