editor: save and reset terminal after calling EDITOR

When EDITOR is invoked to modify a commit message, it will likely change the terminal settings, and if it misbehaves will leave the terminal output damaged as shown in a recent report from Windows Terminal[1] Instead use the functions provided by compat/terminal to save the settings and recover safely. [1] https://github.com/microsoft/terminal/issues/9359 Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlo Marcelo Arenas Belón committed Oct 5, 2021 at 00:46 UTC 3d411afabc9a96f41d47c07d6af6edda3d29ec92
1 file changed +8
editor.c
+8
@@ -3,6 +3,7 @@
3 #include "strbuf.h"
4 #include "run-command.h"
5 #include "sigchain.h"
6 +#include "compat/terminal.h"
7
8 #ifndef DEFAULT_EDITOR
9 #define DEFAULT_EDITOR "vi"
@@ -50,6 +51,8 @@ const char *git_sequence_editor(void)
51 static int launch_specified_editor(const char *editor, const char *path,
52 struct strbuf *buffer, const char *const *env)
53 {
54 + int term_fail;
55 +
56 if (!editor)
57 return error("Terminal is dumb, but EDITOR unset");
58
@@ -83,7 +86,10 @@ static int launch_specified_editor(const char *editor, const char *path,
86 p.env = env;
87 p.use_shell = 1;
88 p.trace2_child_class = "editor";
89 + term_fail = save_term(1);
90 if (start_command(&p) < 0) {
91 + if (!term_fail)
92 + restore_term();
93 strbuf_release(&realpath);
94 return error("unable to start editor '%s'", editor);
95 }
@@ -91,6 +97,8 @@ static int launch_specified_editor(const char *editor, const char *path,
97 sigchain_push(SIGINT, SIG_IGN);
98 sigchain_push(SIGQUIT, SIG_IGN);
99 ret = finish_command(&p);
100 + if (!term_fail)
101 + restore_term();
102 strbuf_release(&realpath);
103 sig = ret - 128;
104 sigchain_pop(SIGINT);