Skip to content

Commit 214919b

Browse files
committed
Merge branch 'pw/single-key-interactive'
The single-key interactive operation used by "git add -p" has been made more robust. * pw/single-key-interactive: add -p: disable stdin buffering when interactive.singlekey is set terminal: set VMIN and VTIME in non-canonical mode terminal: pop signal handler when terminal is restored terminal: always reset terminal when reading without echo
2 parents 7391ecd + ac618c4 commit 214919b

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

add-interactive.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
7070
&s->interactive_diff_algorithm);
7171

7272
git_config_get_bool("interactive.singlekey", &s->use_single_key);
73+
if (s->use_single_key)
74+
setbuf(stdin, NULL);
7375
}
7476

7577
void clear_add_i_state(struct add_i_state *s)

compat/terminal.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
static void restore_term_on_signal(int sig)
1212
{
1313
restore_term();
14-
sigchain_pop(sig);
14+
/* restore_term calls sigchain_pop_common */
1515
raise(sig);
1616
}
1717

@@ -31,14 +31,20 @@ void restore_term(void)
3131
tcsetattr(term_fd, TCSAFLUSH, &old_term);
3232
close(term_fd);
3333
term_fd = -1;
34+
sigchain_pop_common();
3435
}
3536

3637
int save_term(int full_duplex)
3738
{
3839
if (term_fd < 0)
3940
term_fd = open("/dev/tty", O_RDWR);
41+
if (term_fd < 0)
42+
return -1;
43+
if (tcgetattr(term_fd, &old_term) < 0)
44+
return -1;
45+
sigchain_push_common(restore_term_on_signal);
4046

41-
return (term_fd < 0) ? -1 : tcgetattr(term_fd, &old_term);
47+
return 0;
4248
}
4349

4450
static int disable_bits(tcflag_t bits)
@@ -49,12 +55,16 @@ static int disable_bits(tcflag_t bits)
4955
goto error;
5056

5157
t = old_term;
52-
sigchain_push_common(restore_term_on_signal);
5358

5459
t.c_lflag &= ~bits;
60+
if (bits & ICANON) {
61+
t.c_cc[VMIN] = 1;
62+
t.c_cc[VTIME] = 0;
63+
}
5564
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
5665
return 0;
5766

67+
sigchain_pop_common();
5868
error:
5969
close(term_fd);
6070
term_fd = -1;
@@ -100,6 +110,8 @@ void restore_term(void)
100110
return;
101111
}
102112

113+
sigchain_pop_common();
114+
103115
if (hconin == INVALID_HANDLE_VALUE)
104116
return;
105117

@@ -134,6 +146,7 @@ int save_term(int full_duplex)
134146

135147
GetConsoleMode(hconin, &cmode_in);
136148
use_stty = 0;
149+
sigchain_push_common(restore_term_on_signal);
137150
return 0;
138151
error:
139152
CloseHandle(hconin);
@@ -150,7 +163,11 @@ static int disable_bits(DWORD bits)
150163

151164
if (bits & ENABLE_LINE_INPUT) {
152165
string_list_append(&stty_restore, "icanon");
153-
strvec_push(&cp.args, "-icanon");
166+
/*
167+
* POSIX allows VMIN and VTIME to overlap with VEOF and
168+
* VEOL - let's hope that is not the case on windows.
169+
*/
170+
strvec_pushl(&cp.args, "-icanon", "min", "1", "time", "0", NULL);
154171
}
155172

156173
if (bits & ENABLE_ECHO_INPUT) {
@@ -177,10 +194,10 @@ static int disable_bits(DWORD bits)
177194
if (save_term(0) < 0)
178195
return -1;
179196

180-
sigchain_push_common(restore_term_on_signal);
181197
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
182198
CloseHandle(hconin);
183199
hconin = INVALID_HANDLE_VALUE;
200+
sigchain_pop_common();
184201
return -1;
185202
}
186203

@@ -385,7 +402,7 @@ int read_key_without_echo(struct strbuf *buf)
385402

386403
ch = getchar();
387404
if (ch == EOF)
388-
return 0;
405+
break;
389406
strbuf_addch(buf, ch);
390407
}
391408
}

compat/terminal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#ifndef COMPAT_TERMINAL_H
22
#define COMPAT_TERMINAL_H
33

4+
/*
5+
* Save the terminal attributes so they can be restored later by a
6+
* call to restore_term(). Note that every successful call to
7+
* save_term() must be matched by a call to restore_term() even if the
8+
* attributes have not been changed. Returns 0 on success, -1 on
9+
* failure.
10+
*/
411
int save_term(int full_duplex);
12+
/* Restore the terminal attributes that were saved with save_term() */
513
void restore_term(void);
614

715
char *git_terminal_prompt(const char *prompt, int echo);

0 commit comments

Comments
 (0)