Skip to content

Commit 197fe1e

Browse files
committed
built-in add -p: respect the interactive.singlekey config setting
The Perl version of `git add -p` supports this config setting to allow users to input commands via single characters (as opposed to having to press the <Enter> key afterwards). This is an opt-in feature because it requires Perl packages (Term::ReadKey and Term::Cap, where it tries to handle an absence of the latter package gracefully) to work. Note that at least on Ubuntu, that Perl package is not installed by default (it needs to be installed via `sudo apt-get install libterm-readkey-perl`), so this feature is probably not used a whole lot. In C, we obviously do not have these packages available, but we just introduced `read_single_keystroke()` that is similar to what Term::ReadKey provides, and we use that here. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 74593b5 commit 197fe1e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

add-interactive.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
6161
FREE_AND_NULL(s->interactive_diff_algorithm);
6262
git_config_get_string("diff.algorithm",
6363
&s->interactive_diff_algorithm);
64+
65+
git_config_get_bool("interactive.singlekey", &s->use_single_key);
6466
}
6567

6668
void clear_add_i_state(struct add_i_state *s)

add-interactive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct add_i_state {
1616
char file_old_color[COLOR_MAXLEN];
1717
char file_new_color[COLOR_MAXLEN];
1818

19+
int use_single_key;
1920
char *interactive_diff_filter, *interactive_diff_algorithm;
2021
};
2122

add-patch.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "color.h"
88
#include "diff.h"
99
#include "sigchain.h"
10+
#include "compat/terminal.h"
1011

1112
enum prompt_mode_type {
1213
PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -1150,14 +1151,27 @@ static int run_apply_check(struct add_p_state *s,
11501151
return 0;
11511152
}
11521153

1154+
static int read_single_character(struct add_p_state *s)
1155+
{
1156+
if (s->s.use_single_key) {
1157+
int res = read_key_without_echo(&s->answer);
1158+
printf("%s\n", res == EOF ? "" : s->answer.buf);
1159+
return res;
1160+
}
1161+
1162+
if (strbuf_getline(&s->answer, stdin) == EOF)
1163+
return EOF;
1164+
strbuf_trim_trailing_newline(&s->answer);
1165+
return 0;
1166+
}
1167+
11531168
static int prompt_yesno(struct add_p_state *s, const char *prompt)
11541169
{
11551170
for (;;) {
11561171
color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
11571172
fflush(stdout);
1158-
if (strbuf_getline(&s->answer, stdin) == EOF)
1173+
if (read_single_character(s) == EOF)
11591174
return -1;
1160-
strbuf_trim_trailing_newline(&s->answer);
11611175
switch (tolower(s->answer.buf[0])) {
11621176
case 'n': return 0;
11631177
case 'y': return 1;
@@ -1397,9 +1411,8 @@ static int patch_update_file(struct add_p_state *s,
13971411
_(s->mode->prompt_mode[prompt_mode_type]),
13981412
s->buf.buf);
13991413
fflush(stdout);
1400-
if (strbuf_getline(&s->answer, stdin) == EOF)
1414+
if (read_single_character(s) == EOF)
14011415
break;
1402-
strbuf_trim_trailing_newline(&s->answer);
14031416

14041417
if (!s->answer.len)
14051418
continue;

0 commit comments

Comments
 (0)