Merge branch 'cm/save-restore-terminal'

An editor session launched during a Git operation (e.g. during 'git
commit') can leave the terminal in a funny state.  The code path
has updated to save the terminal state before, and restore it
after, it spawns an editor.

* cm/save-restore-terminal:
  editor: save and reset terminal after calling EDITOR
  terminal: teach git how to save/restore its terminal settings
This commit is contained in:
Junio C Hamano 2021-10-18 15:47:57 -07:00
Родитель a4b9fb6a5c 3d411afabc
Коммит 853ec9aa9b
3 изменённых файлов: 71 добавлений и 15 удалений

Просмотреть файл

@ -8,8 +8,6 @@
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
static void restore_term(void);
static void restore_term_on_signal(int sig)
{
restore_term();
@ -25,7 +23,7 @@ static void restore_term_on_signal(int sig)
static int term_fd = -1;
static struct termios old_term;
static void restore_term(void)
void restore_term(void)
{
if (term_fd < 0)
return;
@ -35,15 +33,22 @@ static void restore_term(void)
term_fd = -1;
}
int save_term(int full_duplex)
{
if (term_fd < 0)
term_fd = open("/dev/tty", O_RDWR);
return (term_fd < 0) ? -1 : tcgetattr(term_fd, &old_term);
}
static int disable_bits(tcflag_t bits)
{
struct termios t;
term_fd = open("/dev/tty", O_RDWR);
if (tcgetattr(term_fd, &t) < 0)
if (save_term(0) < 0)
goto error;
old_term = t;
t = old_term;
sigchain_push_common(restore_term_on_signal);
t.c_lflag &= ~bits;
@ -75,9 +80,10 @@ static int enable_non_canonical(void)
static int use_stty = 1;
static struct string_list stty_restore = STRING_LIST_INIT_DUP;
static HANDLE hconin = INVALID_HANDLE_VALUE;
static DWORD cmode;
static HANDLE hconout = INVALID_HANDLE_VALUE;
static DWORD cmode_in, cmode_out;
static void restore_term(void)
void restore_term(void)
{
if (use_stty) {
int i;
@ -97,9 +103,42 @@ static void restore_term(void)
if (hconin == INVALID_HANDLE_VALUE)
return;
SetConsoleMode(hconin, cmode);
SetConsoleMode(hconin, cmode_in);
CloseHandle(hconin);
if (cmode_out) {
assert(hconout != INVALID_HANDLE_VALUE);
SetConsoleMode(hconout, cmode_out);
CloseHandle(hconout);
}
hconin = hconout = INVALID_HANDLE_VALUE;
}
int save_term(int full_duplex)
{
hconin = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hconin == INVALID_HANDLE_VALUE)
return -1;
if (full_duplex) {
hconout = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hconout == INVALID_HANDLE_VALUE)
goto error;
GetConsoleMode(hconout, &cmode_out);
}
GetConsoleMode(hconin, &cmode_in);
use_stty = 0;
return 0;
error:
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
return -1;
}
static int disable_bits(DWORD bits)
@ -135,15 +174,11 @@ static int disable_bits(DWORD bits)
use_stty = 0;
}
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hconin == INVALID_HANDLE_VALUE)
if (save_term(0) < 0)
return -1;
GetConsoleMode(hconin, &cmode);
sigchain_push_common(restore_term_on_signal);
if (!SetConsoleMode(hconin, cmode & ~bits)) {
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
return -1;
@ -361,6 +396,16 @@ int read_key_without_echo(struct strbuf *buf)
#else
int save_term(int full_duplex)
{
/* full_duplex == 1, but no support available */
return -full_duplex;
}
void restore_term(void)
{
}
char *git_terminal_prompt(const char *prompt, int echo)
{
return getpass(prompt);

Просмотреть файл

@ -1,6 +1,9 @@
#ifndef COMPAT_TERMINAL_H
#define COMPAT_TERMINAL_H
int save_term(int full_duplex);
void restore_term(void);
char *git_terminal_prompt(const char *prompt, int echo);
/* Read a single keystroke, without echoing it to the terminal */

Просмотреть файл

@ -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"
@ -50,6 +51,8 @@ const char *git_sequence_editor(void)
static int launch_specified_editor(const char *editor, const char *path,
struct strbuf *buffer, const char *const *env)
{
int term_fail;
if (!editor)
return error("Terminal is dumb, but EDITOR unset");
@ -83,7 +86,10 @@ static int launch_specified_editor(const char *editor, const char *path,
p.env = env;
p.use_shell = 1;
p.trace2_child_class = "editor";
term_fail = save_term(1);
if (start_command(&p) < 0) {
if (!term_fail)
restore_term();
strbuf_release(&realpath);
return error("unable to start editor '%s'", editor);
}
@ -91,6 +97,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 (!term_fail)
restore_term();
strbuf_release(&realpath);
sig = ret - 128;
sigchain_pop(SIGINT);