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

[PATCH v4 08/10] built-in add -p: handle Escape sequences in interactive.singlekey mode

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 14, 2020, 18:43 UTC
Message-ID
<cdc609f8fa349b9ffada68dfd6fd183365222ba7.1579027433.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.175.v4.git.1579027433.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

This recapitulates part of b5cc003253c8 (add -i: ignore terminal escape sequences, 2011-05-17):

    add -i: ignore terminal escape sequences
    On the author's terminal, the up-arrow input sequence is ^[[A, and
    thus fat-fingering an up-arrow into 'git checkout -p' is quite
    dangerous: git-add--interactive.perl will ignore the ^[ and [
    characters and happily treat A as "discard everything".
    As a band-aid fix, use Term::Cap to get all terminal capabilities.
    Then use the heuristic that any capability value that starts with ^[
    (i.e., \e in perl) must be a key input sequence.  Finally, given an
    input that starts with ^[, read more characters until we have read a
    full escape sequence, then return that to the caller.  We use a
    timeout of 0.5 seconds on the subsequent reads to avoid getting stuck
    if the user actually input a lone ^[.
    Since none of the currently recognized keys start with ^[, the net
    result is that the sequence as a whole will be ignored and the help
    displayed.

Note that we leave part for later which uses "Term::Cap to get all terminal capabilities", for several reasons:

1. it is actually not really necessary, as the timeout of 0.5 seconds
   should be plenty sufficient to catch Escape sequences,
2. it is cleaner to keep the change to special-case Escape sequences
   separate from the change that reads all terminal capabilities to
   speed things up, and
3. in practice, relying on the terminal capabilities is a bit overrated,
   as the information could be incomplete, or plain wrong. For example,
   in this developer's tmux sessions, the terminal capabilities claim
   that the "cursor up" sequence is ^[M, but the actual sequence
   produced by the "cursor up" key is ^[[A.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/terminal.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)
diff --git a/compat/terminal.c b/compat/terminal.c
index 1b2564042a..b7f58d1781 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -161,6 +161,37 @@ static int enable_non_canonical(void)
 	return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
 }
 
+/*
+ * Override `getchar()`, as the default implementation does not use
+ * `ReadFile()`.
+ *
+ * This poses a problem when we want to see whether the standard
+ * input has more characters, as the default of Git for Windows is to start the
+ * Bash in a MinTTY, which uses a named pipe to emulate a pty, in which case
+ * our `poll()` emulation calls `PeekNamedPipe()`, which seems to require
+ * `ReadFile()` to be called first to work properly (it only reports 0
+ * available bytes, otherwise).
+ *
+ * So let's just override `getchar()` with a version backed by `ReadFile()` and
+ * go our merry ways from here.
+ */
+static int mingw_getchar(void)
+{
+	DWORD read = 0;
+	unsigned char ch;
+
+	if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL))
+		return EOF;
+
+	if (!read) {
+		error("Unexpected 0 read");
+		return EOF;
+	}
+
+	return ch;
+}
+#define getchar mingw_getchar
+
 #endif
 
 #ifndef FORCE_TEXT
@@ -228,8 +259,31 @@ int read_key_without_echo(struct strbuf *buf)
 		restore_term();
 		return EOF;
 	}
-
 	strbuf_addch(buf, ch);
+
+	if (ch == '\033' /* ESC */) {
+		/*
+		 * We are most likely looking at an Escape sequence. Let's try
+		 * to read more bytes, waiting at most half a second, assuming
+		 * that the sequence is complete if we did not receive any byte
+		 * within that time.
+		 *
+		 * Start by replacing the Escape byte with ^[ */
+		strbuf_splice(buf, buf->len - 1, 1, "^[", 2);
+
+		for (;;) {
+			struct pollfd pfd = { .fd = 0, .events = POLLIN };
+
+			if (poll(&pfd, 1, 500) < 1)
+				break;
+
+			ch = getchar();
+			if (ch == EOF)
+				return 0;
+			strbuf_addch(buf, ch);
+		}
+	}
+
 	restore_term();
 	return 0;
 }
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 57 of 63 in “built-in add -p: add support for the same config settings as the Perl version”
  1. 0/9 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Dec 21, 2019
  2. 1/9 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Dec 21, 2019
  3. 2/9 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Dec 21, 2019
  4. 6/9 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Dec 21, 2019
  5. 8/9 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Dec 21, 2019
  6. 4/9 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Dec 21, 2019
  7. 3/9 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Dec 21, 2019
  8. 7/9 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Dec 21, 2019
  9. 5/9 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Dec 21, 2019
  10. 9/9 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Dec 21, 2019
  11. SZEDER GáborDec 21, 2019
  12. Johannes SchindelinDec 25, 2019
  13. Junio C HamanoDec 22, 2019
  14. Johannes SchindelinDec 25, 2019
  15. Junio C HamanoDec 24, 2019
  16. Junio C HamanoDec 24, 2019
  17. Simon RuderichDec 25, 2019
  18. Johannes SchindelinDec 25, 2019
  19. Johannes SchindelinDec 25, 2019
  20. 0/9 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Dec 25, 2019
  21. 1/9 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Dec 25, 2019
  22. SZEDER GáborJan 7, 2020
  23. Johannes SchindelinJan 13, 2020
  24. 2/9 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Dec 25, 2019
  25. 3/9 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Dec 25, 2019
  26. 4/9 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Dec 25, 2019
  27. 6/9 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Dec 25, 2019
  28. 8/9 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Dec 25, 2019
  29. 9/9 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Dec 25, 2019
  30. Derrick StoleeDec 26, 2019
  31. Johannes SchindelinJan 1, 2020
  32. 7/9 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Dec 25, 2019
  33. 5/9 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Dec 25, 2019
  34. Junio C HamanoDec 26, 2019
  35. 00/10 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Jan 13, 2020
  36. 01/10 built-in add -i/-p: treat SIGPIPE as EOFJohannes Schindelin via GitGitGadget, Jan 13, 2020
  37. SZEDER GáborJan 13, 2020
  38. Jeff KingJan 13, 2020
  39. Junio C HamanoJan 15, 2020
  40. Jeff KingJan 15, 2020
  41. Johannes SchindelinJan 14, 2020
  42. SZEDER GáborJan 17, 2020
  43. Jeff KingJan 17, 2020
  44. 02/10 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Jan 13, 2020
  45. 03/10 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Jan 13, 2020
  46. 05/10 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Jan 13, 2020
  47. 04/10 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Jan 13, 2020
  48. 06/10 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Jan 13, 2020
  49. 07/10 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Jan 13, 2020
  50. 10/10 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Jan 13, 2020
  51. 09/10 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Jan 13, 2020
  52. 08/10 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Jan 13, 2020
  53. 00/10 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Jan 14, 2020
  54. 01/10 t3701: adjust difffilter testJohannes Schindelin via GitGitGadget, Jan 14, 2020
  55. 02/10 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Jan 14, 2020
  56. 05/10 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Jan 14, 2020
  57. 08/10 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Jan 14, 2020
  58. 10/10 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Jan 14, 2020
  59. 07/10 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Jan 14, 2020
  60. 03/10 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Jan 14, 2020
  61. 06/10 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Jan 14, 2020
  62. 04/10 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Jan 14, 2020
  63. 09/10 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Jan 14, 2020

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.