From 9b6e2c8b98f6a382a6ee8d9110bfe93b1b48d6a7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 21 Jun 2021 16:57:58 +0000 Subject: pager: avoid setting COLUMNS when we're guessing its value We query `TIOCGWINSZ` in Git to determine the correct value for `COLUMNS`, and then set that environment variable. If `TIOCGWINSZ` is not available, we fall back to the hard-coded value 80 _and still_ set the environment variable. On Windows this is a problem. The reason is that Git for Windows uses a version of `less` that relies on the MSYS2 runtime to interact with the pseudo terminal (typically inside a MinTTY window, which is also aware of the MSYS2 runtime). Both MinTTY and `less.exe` interact with that pseudo terminal via `ioctl()` calls (which the MSYS2 runtime emulates even if there is no such thing on Windows). Since https://github.com/gwsw/less/commit/bb0ee4e76c2, `less` prefers the `COLUMNS` variable over asking ncurses itself. But `git.exe` itself is _not_ aware of the MSYS2 runtime, or for that matter of that pseudo terminal, and has no way to call `ioctl()` or `TIOCGWINSZ`. Therefore, `git.exe` will fall back to hard-coding 80 columns, no matter what the actual terminal size is. But `less.exe` is totally able to interact with the MSYS2 runtime and would not actually require Git's help (which actually makes things worse here). So let's not override `COLUMNS` on Windows. Let's just not set `COLUMNS` unless we managed to query the actual value from the terminal. This fixes https://github.com/git-for-windows/git/issues/3235 Co-authored-by: Junio C Hamano Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- pager.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'pager.c') diff --git a/pager.c b/pager.c index 3d37dd7ada..52f27a6765 100644 --- a/pager.c +++ b/pager.c @@ -11,6 +11,10 @@ static struct child_process pager_process = CHILD_PROCESS_INIT; static const char *pager_program; +/* Is the value coming back from term_columns() just a guess? */ +static int term_columns_guessed; + + static void close_pager_fds(void) { /* signal EOF to pager */ @@ -114,7 +118,8 @@ void setup_pager(void) { char buf[64]; xsnprintf(buf, sizeof(buf), "%d", term_columns()); - setenv("COLUMNS", buf, 0); + if (!term_columns_guessed) + setenv("COLUMNS", buf, 0); } setenv("GIT_PAGER_IN_USE", "true", 1); @@ -158,15 +163,20 @@ int term_columns(void) return term_columns_at_startup; term_columns_at_startup = 80; + term_columns_guessed = 1; col_string = getenv("COLUMNS"); - if (col_string && (n_cols = atoi(col_string)) > 0) + if (col_string && (n_cols = atoi(col_string)) > 0) { term_columns_at_startup = n_cols; + term_columns_guessed = 0; + } #ifdef TIOCGWINSZ else { struct winsize ws; - if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) + if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) { term_columns_at_startup = ws.ws_col; + term_columns_guessed = 0; + } } #endif -- cgit v1.2.3