diff mbox series

[RFC,2/3] editor: allow for saving/restoring terminal state

Message ID 20211202035446.1154-3-carenas@gmail.com (mailing list archive)
State New, archived
Headers show
Series editor: teach it to protect itself from rogue editors | expand

Commit Message

Carlo Marcelo Arenas Belón Dec. 2, 2021, 3:54 a.m. UTC
From: Junio C Hamano <gitster@pobox.com>

When EDITOR is invoked to modify a commit message (or do some
other editing), and it is a terminal mode editor, it will need to
change the terminal settings, and if it misbehaves could leave the
terminal output damaged as shown in recent reports from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings of the terminal and recover safely, but only do so if
the editor is known to have issues and unless the user has told
us through a boolean configuration "editor.stty" that it is safe
not to do so.

[1] https://github.com/microsoft/terminal/issues/9359
---
The commit message was contributed by me, but the rest comes from:
https://lore.kernel.org/git/xmqq7dcnyh5o.fsf@gitster.g/
Signed-of-by is missing as I didn't know what would be preferred
or if the Author will be ok with my grammar (ginger free grammar
check seems to approve, otherwise)

 editor.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

Comments

Junio C Hamano Dec. 2, 2021, 6:35 a.m. UTC | #1
Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:

> From: Junio C Hamano <gitster@pobox.com>
>
> When EDITOR is invoked to modify a commit message (or do some
> other editing), and it is a terminal mode editor, it will need to
> change the terminal settings, and if it misbehaves could leave the
> terminal output damaged as shown in recent reports from Windows
> Terminal[1]
>
> Instead use the functions provided by compat/terminal to save the
> settings of the terminal and recover safely, but only do so if
> the editor is known to have issues and unless the user has told
> us through a boolean configuration "editor.stty" that it is safe
> not to do so.
>
> [1] https://github.com/microsoft/terminal/issues/9359
> ---
> The commit message was contributed by me, but the rest comes from:
> https://lore.kernel.org/git/xmqq7dcnyh5o.fsf@gitster.g/
> Signed-of-by is missing as I didn't know what would be preferred
> or if the Author will be ok with my grammar (ginger free grammar
> check seems to approve, otherwise)

It was merely an illustration patch, without 3/3 it would not be
complete, and most importantly, finishing the last mile like 3/3 did
is 80% of the work.  I'd rather see you take the authorship of the
commit that results from the squashing of 2 and 3.

Any patch I send here is by default signed off by me.

Thanks.
diff mbox series

Patch

diff --git a/editor.c b/editor.c
index fdd3eeafa9..70d3f80966 100644
--- a/editor.c
+++ b/editor.c
@@ -3,6 +3,7 @@ 
 #include "strbuf.h"
 #include "run-command.h"
 #include "sigchain.h"
+#include "compat/terminal.h"
 
 #ifndef DEFAULT_EDITOR
 #define DEFAULT_EDITOR "vi"
@@ -47,6 +48,16 @@  const char *git_sequence_editor(void)
 	return editor;
 }
 
+static int prepare_term(const char *editor)
+{
+	int need_saverestore = !strcmp(editor, "vi");
+
+	git_config_get_bool("editor.stty", &need_saverestore);
+	if (need_saverestore)
+		return save_term(1);
+	return 0;
+}
+
 static int launch_specified_editor(const char *editor, const char *path,
 				   struct strbuf *buffer, const char *const *env)
 {
@@ -57,7 +68,7 @@  static int launch_specified_editor(const char *editor, const char *path,
 		struct strbuf realpath = STRBUF_INIT;
 		const char *args[] = { editor, NULL, NULL };
 		struct child_process p = CHILD_PROCESS_INIT;
-		int ret, sig;
+		int ret, sig, need_restore = 0;
 		int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
 
 		if (print_waiting_for_editor) {
@@ -83,7 +94,10 @@  static int launch_specified_editor(const char *editor, const char *path,
 		p.env = env;
 		p.use_shell = 1;
 		p.trace2_child_class = "editor";
+		need_restore = prepare_term(editor);
 		if (start_command(&p) < 0) {
+			if (need_restore)
+				restore_term();
 			strbuf_release(&realpath);
 			return error("unable to start editor '%s'", editor);
 		}
@@ -91,6 +105,8 @@  static int launch_specified_editor(const char *editor, const char *path,
 		sigchain_push(SIGINT, SIG_IGN);
 		sigchain_push(SIGQUIT, SIG_IGN);
 		ret = finish_command(&p);
+		if (need_restore)
+			restore_term();
 		strbuf_release(&realpath);
 		sig = ret - 128;
 		sigchain_pop(SIGINT);