Skip to content

Commit

Permalink
Use UTF-8 code page when using native ANSI sequence processing (git-f…
Browse files Browse the repository at this point in the history
…or-windows#4968)

In git-for-windows#4700, I introduced a change in Git for Windows' behavior where it
would favor recent Windows 10 versions' native ANSI sequence processing
to [Git for Windows' home-grown
one](https://github.com/git-for-windows/git/blob/v2.45.1.windows.1/compat/winansi.c#L362-L439).

What I missed was that the home-grown processing _also_ ensured that
text written to the Win32 Console was carefully converted from UTF-8 to
UTF-16 encoding, while the native ANSI sequence processing would respect
the currently-set code page.

However, Git for Windows does not use the current code page at all,
always using UTF-8 encoded text internally. So let's make sure that the
code page is `CP_UTF8` when Git for Windows uses the native ANSI
sequence processing.

This fixes git-for-windows#4851.
  • Loading branch information
dscho authored May 26, 2024
2 parents f3b93ac + 45506bb commit 9cf5174
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,15 @@ static void detect_msys_tty(int fd)
#endif

static HANDLE std_console_handle;
static DWORD std_console_mode;
static DWORD std_console_mode = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
static UINT std_console_code_page = CP_UTF8;

static void reset_std_console_mode(void)
static void reset_std_console(void)
{
SetConsoleMode(std_console_handle, std_console_mode);
if (std_console_mode != ENABLE_VIRTUAL_TERMINAL_PROCESSING)
SetConsoleMode(std_console_handle, std_console_mode);
if (std_console_code_page != CP_UTF8)
SetConsoleOutputCP(std_console_code_page);
}

static int enable_virtual_processing(void)
Expand All @@ -613,6 +617,14 @@ static int enable_virtual_processing(void)
return 0;
}

std_console_code_page = GetConsoleOutputCP();
if (std_console_code_page != CP_UTF8)
SetConsoleOutputCP(CP_UTF8);
if (!std_console_code_page)
std_console_code_page = CP_UTF8;

atexit(reset_std_console);

if (std_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
return 1;

Expand All @@ -622,7 +634,6 @@ static int enable_virtual_processing(void)
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;

atexit(reset_std_console_mode);
return 1;
}

Expand Down

0 comments on commit 9cf5174

Please sign in to comment.