Skip to content

Commit 9cf5174

Browse files
authored
Use UTF-8 code page when using native ANSI sequence processing (git-for-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.
2 parents f3b93ac + 45506bb commit 9cf5174

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

compat/winansi.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,15 @@ static void detect_msys_tty(int fd)
595595
#endif
596596

597597
static HANDLE std_console_handle;
598-
static DWORD std_console_mode;
598+
static DWORD std_console_mode = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
599+
static UINT std_console_code_page = CP_UTF8;
599600

600-
static void reset_std_console_mode(void)
601+
static void reset_std_console(void)
601602
{
602-
SetConsoleMode(std_console_handle, std_console_mode);
603+
if (std_console_mode != ENABLE_VIRTUAL_TERMINAL_PROCESSING)
604+
SetConsoleMode(std_console_handle, std_console_mode);
605+
if (std_console_code_page != CP_UTF8)
606+
SetConsoleOutputCP(std_console_code_page);
603607
}
604608

605609
static int enable_virtual_processing(void)
@@ -613,6 +617,14 @@ static int enable_virtual_processing(void)
613617
return 0;
614618
}
615619

620+
std_console_code_page = GetConsoleOutputCP();
621+
if (std_console_code_page != CP_UTF8)
622+
SetConsoleOutputCP(CP_UTF8);
623+
if (!std_console_code_page)
624+
std_console_code_page = CP_UTF8;
625+
626+
atexit(reset_std_console);
627+
616628
if (std_console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
617629
return 1;
618630

@@ -622,7 +634,6 @@ static int enable_virtual_processing(void)
622634
ENABLE_VIRTUAL_TERMINAL_PROCESSING))
623635
return 0;
624636

625-
atexit(reset_std_console_mode);
626637
return 1;
627638
}
628639

0 commit comments

Comments
 (0)