Skip to content

Commit 84616c0

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 b2f97c8 commit 84616c0

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
@@ -1047,14 +1048,27 @@ static int run_apply_check(struct add_p_state *s,
10471048
return 0;
10481049
}
10491050

1051+
static int read_single_character(struct add_p_state *s)
1052+
{
1053+
if (s->s.use_single_key) {
1054+
int res = read_key_without_echo(&s->answer);
1055+
printf("%s\n", res == EOF ? "" : s->answer.buf);
1056+
return res;
1057+
}
1058+
1059+
if (strbuf_getline(&s->answer, stdin) == EOF)
1060+
return EOF;
1061+
strbuf_trim_trailing_newline(&s->answer);
1062+
return 0;
1063+
}
1064+
10501065
static int prompt_yesno(struct add_p_state *s, const char *prompt)
10511066
{
10521067
for (;;) {
10531068
color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
10541069
fflush(stdout);
1055-
if (strbuf_getline(&s->answer, stdin) == EOF)
1070+
if (read_single_character(s) == EOF)
10561071
return -1;
1057-
strbuf_trim_trailing_newline(&s->answer);
10581072
switch (tolower(s->answer.buf[0])) {
10591073
case 'n': return 0;
10601074
case 'y': return 1;
@@ -1290,9 +1304,8 @@ static int patch_update_file(struct add_p_state *s,
12901304
_(s->mode->prompt_mode[prompt_mode_type]),
12911305
s->buf.buf);
12921306
fflush(stdout);
1293-
if (strbuf_getline(&s->answer, stdin) == EOF)
1307+
if (read_single_character(s) == EOF)
12941308
break;
1295-
strbuf_trim_trailing_newline(&s->answer);
12961309

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

0 commit comments

Comments
 (0)