Skip to content

Commit 597ac69

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 09af4d8 commit 597ac69

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

add-interactive.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ int init_add_i_state(struct repository *r, struct add_i_state *s)
6666
&s->interactive_diff_algorithm))
6767
s->interactive_diff_algorithm = NULL;
6868

69+
if (git_config_get_bool("interactive.singlekey",
70+
&s->use_single_key))
71+
s->use_single_key = 0;
72+
6973
return 0;
7074
}
7175

add-interactive.h

Lines changed: 2 additions & 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

@@ -31,6 +32,7 @@ enum color_add_i {
3132
const char *get_add_i_color(enum color_add_i ix);
3233
const char *get_interactive_diff_filter(void);
3334
const char *get_interactive_diff_algorithm(void);
35+
int get_interactive_use_single_key(void);
3436

3537
struct repository;
3638
struct pathspec;

add-patch.c

Lines changed: 17 additions & 4 deletions
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
@@ -1046,14 +1047,27 @@ static int run_apply_check(struct add_p_state *s,
10461047
return 0;
10471048
}
10481049

1050+
static int read_single_character(struct add_p_state *s)
1051+
{
1052+
if (s->s.use_single_key) {
1053+
int res = read_key_without_echo(&s->answer);
1054+
printf("%s\n", res == EOF ? "" : s->answer.buf);
1055+
return res;
1056+
}
1057+
1058+
if (strbuf_getline(&s->answer, stdin) == EOF)
1059+
return EOF;
1060+
strbuf_trim_trailing_newline(&s->answer);
1061+
return 0;
1062+
}
1063+
10491064
static int prompt_yesno(struct add_p_state *s, const char *prompt)
10501065
{
10511066
for (;;) {
10521067
color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
10531068
fflush(stdout);
1054-
if (strbuf_getline(&s->answer, stdin) == EOF)
1069+
if (read_single_character(s) == EOF)
10551070
return -1;
1056-
strbuf_trim_trailing_newline(&s->answer);
10571071
switch (tolower(s->answer.buf[0])) {
10581072
case 'n': return 0;
10591073
case 'y': return 1;
@@ -1289,9 +1303,8 @@ static int patch_update_file(struct add_p_state *s,
12891303
_(s->mode->prompt_mode[prompt_mode_type]),
12901304
s->buf.buf);
12911305
fflush(stdout);
1292-
if (strbuf_getline(&s->answer, stdin) == EOF)
1306+
if (read_single_character(s) == EOF)
12931307
break;
1294-
strbuf_trim_trailing_newline(&s->answer);
12951308

12961309
if (!s->answer.len)
12971310
continue;

0 commit comments

Comments
 (0)