@matheusbsbs / matheus-bagliano / commits / 04f816b125
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 14, 2020 at 18:43 UTC 04f816b125dc2649e70aad686d79b05bdc1d1c61
3 files changed +20 -4
add-interactive.c
+2
@@ -60,6 +60,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
60 FREE_AND_NULL(s->interactive_diff_algorithm);
61 git_config_get_string("diff.algorithm",
62 &s->interactive_diff_algorithm);
63 +
64 + git_config_get_bool("interactive.singlekey", &s->use_single_key);
65 }
66
67 void clear_add_i_state(struct add_i_state *s)
add-interactive.h
+1
@@ -16,6 +16,7 @@ struct add_i_state {
16 char file_old_color[COLOR_MAXLEN];
17 char file_new_color[COLOR_MAXLEN];
18
19 + int use_single_key;
20 char *interactive_diff_filter, *interactive_diff_algorithm;
21 };
22
add-patch.c
+17 -4
@@ -6,6 +6,7 @@
6 #include "pathspec.h"
7 #include "color.h"
8 #include "diff.h"
9 +#include "compat/terminal.h"
10
11 enum prompt_mode_type {
12 PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -1149,14 +1150,27 @@ static int run_apply_check(struct add_p_state *s,
1150 return 0;
1151 }
1152
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 +
1167 static int prompt_yesno(struct add_p_state *s, const char *prompt)
1168 {
1169 for (;;) {
1170 color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
1171 fflush(stdout);
1157 - if (strbuf_getline(&s->answer, stdin) == EOF)
1172 + if (read_single_character(s) == EOF)
1173 return -1;
1159 - strbuf_trim_trailing_newline(&s->answer);
1174 switch (tolower(s->answer.buf[0])) {
1175 case 'n': return 0;
1176 case 'y': return 1;
@@ -1396,9 +1410,8 @@ static int patch_update_file(struct add_p_state *s,
1410 _(s->mode->prompt_mode[prompt_mode_type]),
1411 s->buf.buf);
1412 fflush(stdout);
1399 - if (strbuf_getline(&s->answer, stdin) == EOF)
1413 + if (read_single_character(s) == EOF)
1414 break;
1401 - strbuf_trim_trailing_newline(&s->answer);
1415
1416 if (!s->answer.len)
1417 continue;