git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 1/2] mingw: ensure `getcwd()` reports the correct case
Date: Tue, 23 Oct 2018 03:52:48 -0700 (PDT)	[thread overview]
Message-ID: <e13ae233822c8e4a4504e3294adf3e0de38a1990.1540291965.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.54.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


  reply	other threads:[~2018-10-23 10:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-23 10:52 [PATCH 0/2] Work around case-insensitivity issues with cwd on Windows Johannes Schindelin via GitGitGadget
2018-10-23 10:52 ` Johannes Schindelin via GitGitGadget [this message]
2018-10-23 10:52 ` [PATCH 2/2] mingw: fix getcwd when the parent directory cannot be queried Anton Serbulov via GitGitGadget
2018-10-23 13:16   ` Stephen & Linda Smith
2018-10-23 13:20     ` Stephen Smith
2018-10-24  9:19     ` Johannes Schindelin
2018-10-24  9:19 ` [PATCH v2 0/2] Work around case-insensitivity issues with cwd on Windows Johannes Schindelin via GitGitGadget
2018-10-24  9:19   ` [PATCH v2 1/2] mingw: ensure `getcwd()` reports the correct case Johannes Schindelin via GitGitGadget
2018-10-24  9:19   ` [PATCH v2 2/2] mingw: fix getcwd when the parent directory cannot be queried Anton Serbulov via GitGitGadget
2018-10-24 10:22   ` [PATCH v2 0/2] Work around case-insensitivity issues with cwd on Windows Junio C Hamano
2018-10-24 15:20     ` Johannes Schindelin
2018-10-25  4:11       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e13ae233822c8e4a4504e3294adf3e0de38a1990.1540291965.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).