Skip to content

Commit e8c7126

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 531d9f0 commit e8c7126

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
@@ -1097,14 +1098,27 @@ static int run_apply_check(struct add_p_state *s,
10971098
return 0;
10981099
}
10991100

1101+
static int read_single_character(struct add_p_state *s)
1102+
{
1103+
if (s->s.use_single_key) {
1104+
int res = read_key_without_echo(&s->answer);
1105+
printf("%s\n", res == EOF ? "" : s->answer.buf);
1106+
return res;
1107+
}
1108+
1109+
if (strbuf_getline(&s->answer, stdin) == EOF)
1110+
return EOF;
1111+
strbuf_trim_trailing_newline(&s->answer);
1112+
return 0;
1113+
}
1114+
11001115
static int prompt_yesno(struct add_p_state *s, const char *prompt)
11011116
{
11021117
for (;;) {
11031118
color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
11041119
fflush(stdout);
1105-
if (strbuf_getline(&s->answer, stdin) == EOF)
1120+
if (read_single_character(s) == EOF)
11061121
return -1;
1107-
strbuf_trim_trailing_newline(&s->answer);
11081122
switch (tolower(s->answer.buf[0])) {
11091123
case 'n': return 0;
11101124
case 'y': return 1;
@@ -1340,9 +1354,8 @@ static int patch_update_file(struct add_p_state *s,
13401354
_(s->mode->prompt_mode[prompt_mode_type]),
13411355
s->buf.buf);
13421356
fflush(stdout);
1343-
if (strbuf_getline(&s->answer, stdin) == EOF)
1357+
if (read_single_character(s) == EOF)
13441358
break;
1345-
strbuf_trim_trailing_newline(&s->answer);
13461359

13471360
if (!s->answer.len)
13481361
continue;

0 commit comments

Comments
 (0)