Skip to content

Commit

Permalink
editor: allow for saving/restoring terminal state
Browse files Browse the repository at this point in the history
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 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
instructed by the user through a new configuration "editor.rogue"
and the terminal is fully interactive.

[1] microsoft/terminal#9359

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
  • Loading branch information
carenas committed Nov 30, 2021
1 parent 4438e93 commit 910c158
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ include::config/diff.txt[]

include::config/difftool.txt[]

include::config/editor.txt[]

include::config/extensions.txt[]

include::config/fastimport.txt[]
Expand Down
4 changes: 4 additions & 0 deletions Documentation/config/editor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
editor.rogue::
A boolean to indicate you might be using an editor
that can't be trusted to clean its terminal state
reliably. Defaults to false.
16 changes: 16 additions & 0 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -54,6 +55,7 @@ static int launch_specified_editor(const char *editor, const char *path,
return error("Terminal is dumb, but EDITOR unset");

if (strcmp(editor, ":")) {
int save_and_restore_term = 0;
struct strbuf realpath = STRBUF_INIT;
const char *args[] = { editor, NULL, NULL };
struct child_process p = CHILD_PROCESS_INIT;
Expand All @@ -76,21 +78,35 @@ static int launch_specified_editor(const char *editor, const char *path,
fflush(stderr);
}

if (git_config_get_bool("editor.rogue", &save_and_restore_term) == 0)
{
/* disable unless we are fully interactive */
if (save_and_restore_term && !isatty(2))
save_and_restore_term = 0;
}

strbuf_realpath(&realpath, path, 1);
args[1] = realpath.buf;

p.argv = args;
p.env = env;
p.use_shell = 1;
p.trace2_child_class = "editor";
if (save_and_restore_term)
save_and_restore_term = !save_term(1);

if (start_command(&p) < 0) {
if (save_and_restore_term)
restore_term();
strbuf_release(&realpath);
return error("unable to start editor '%s'", editor);
}

sigchain_push(SIGINT, SIG_IGN);
sigchain_push(SIGQUIT, SIG_IGN);
ret = finish_command(&p);
if (save_and_restore_term)
restore_term();
strbuf_release(&realpath);
sig = ret - 128;
sigchain_pop(SIGINT);
Expand Down

0 comments on commit 910c158

Please sign in to comment.