Skip to content

Commit 04f816b

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5e46e6 commit 04f816b

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

add-interactive.c

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
6060
FREE_AND_NULL(s->interactive_diff_algorithm);
6161
git_config_get_string("diff.algorithm",
6262
&s->interactive_diff_algorithm);
63+
64+
git_config_get_bool("interactive.singlekey", &s->use_single_key);
6365
}
6466

6567
void clear_add_i_state(struct add_i_state *s)

add-interactive.h

+1
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pathspec.h"
77
#include "color.h"
88
#include "diff.h"
9+
#include "compat/terminal.h"
910

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

1153+
static int read_single_character(struct add_p_state *s)
1154+
{
1155+
if (s->s.use_single_key) {
1156+
int res = read_key_without_echo(&s->answer);
1157+
printf("%s\n", res == EOF ? "" : s->answer.buf);
1158+
return res;
1159+
}
1160+
1161+
if (strbuf_getline(&s->answer, stdin) == EOF)
1162+
return EOF;
1163+
strbuf_trim_trailing_newline(&s->answer);
1164+
return 0;
1165+
}
1166+
11521167
static int prompt_yesno(struct add_p_state *s, const char *prompt)
11531168
{
11541169
for (;;) {
11551170
color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
11561171
fflush(stdout);
1157-
if (strbuf_getline(&s->answer, stdin) == EOF)
1172+
if (read_single_character(s) == EOF)
11581173
return -1;
1159-
strbuf_trim_trailing_newline(&s->answer);
11601174
switch (tolower(s->answer.buf[0])) {
11611175
case 'n': return 0;
11621176
case 'y': return 1;
@@ -1396,9 +1410,8 @@ static int patch_update_file(struct add_p_state *s,
13961410
_(s->mode->prompt_mode[prompt_mode_type]),
13971411
s->buf.buf);
13981412
fflush(stdout);
1399-
if (strbuf_getline(&s->answer, stdin) == EOF)
1413+
if (read_single_character(s) == EOF)
14001414
break;
1401-
strbuf_trim_trailing_newline(&s->answer);
14021415

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

0 commit comments

Comments
 (0)