interactive: refactor code asking the user for interactive input
There are quite a few code locations (e.g. `git clean --interactive`) where Git asks the user for an answer. In preparation for fixing a bug shared by all of them, and also to DRY up the code, let's refactor it. Please note that most of these callers trimmed white-space both at the beginning and at the end of the answer, instead of trimming only the end (as the caller in `add-patch.c` does). Therefore, technically speaking, we change behavior in this patch. At the same time, it can be argued that this is actually a bug fix. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Apr 10, 2020 at 11:27 UTC
08d383f23e80e418c844952fcc4e2e635962c292
6 files changed
+22
-16
add-interactive.c
+2
-2
@@ -9,6 +9,7 @@
9
#include "lockfile.h"
10
#include "dir.h"
11
#include "run-command.h"
12
+#include "prompt.h"
13
14
static void init_color(struct repository *r, struct add_i_state *s,
15
const char *slot_name, char *dst,
@@ -289,13 +290,12 @@ static ssize_t list_and_choose(struct add_i_state *s,
290
fputs(singleton ? "> " : ">> ", stdout);
291
fflush(stdout);
292
292
- if (strbuf_getline(&input, stdin) == EOF) {
293
+ if (git_read_line_interactively(&input) == EOF) {
294
putchar('\n');
295
if (immediate)
296
res = LIST_AND_CHOOSE_QUIT;
297
break;
298
}
298
- strbuf_trim(&input);
299
300
if (!input.len)
301
break;
add-patch.c
+2
-2
@@ -7,6 +7,7 @@
7
#include "color.h"
8
#include "diff.h"
9
#include "compat/terminal.h"
10
+#include "prompt.h"
11
12
enum prompt_mode_type {
13
PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -1158,9 +1159,8 @@ static int read_single_character(struct add_p_state *s)
1159
return res;
1160
}
1161
1161
- if (strbuf_getline(&s->answer, stdin) == EOF)
1162
+ if (git_read_line_interactively(&s->answer) == EOF)
1163
return EOF;
1163
- strbuf_trim_trailing_newline(&s->answer);
1164
return 0;
1165
}
1166
builtin/clean.c
+4
-10
@@ -18,6 +18,7 @@
18
#include "color.h"
19
#include "pathspec.h"
20
#include "help.h"
21
+#include "prompt.h"
22
23
static int force = -1; /* unset */
24
static int interactive;
@@ -420,7 +421,6 @@ static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
421
return found;
422
}
423
423
-
424
/*
425
* Parse user input, and return choice(s) for menu (menu_stuff).
426
*
@@ -580,9 +580,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
580
clean_get_color(CLEAN_COLOR_RESET));
581
}
582
583
- if (strbuf_getline_lf(&choice, stdin) != EOF) {
584
- strbuf_trim(&choice);
585
- } else {
583
+ if (git_read_line_interactively(&choice) == EOF) {
584
eof = 1;
585
break;
586
}
@@ -662,9 +660,7 @@ static int filter_by_patterns_cmd(void)
660
clean_print_color(CLEAN_COLOR_PROMPT);
661
printf(_("Input ignore patterns>> "));
662
clean_print_color(CLEAN_COLOR_RESET);
665
- if (strbuf_getline_lf(&confirm, stdin) != EOF)
666
- strbuf_trim(&confirm);
667
- else
663
+ if (git_read_line_interactively(&confirm) == EOF)
664
putchar('\n');
665
666
/* quit filter_by_pattern mode if press ENTER or Ctrl-D */
@@ -760,9 +756,7 @@ static int ask_each_cmd(void)
756
qname = quote_path_relative(item->string, NULL, &buf);
757
/* TRANSLATORS: Make sure to keep [y/N] as is */
758
printf(_("Remove %s [y/N]? "), qname);
763
- if (strbuf_getline_lf(&confirm, stdin) != EOF) {
764
- strbuf_trim(&confirm);
765
- } else {
759
+ if (git_read_line_interactively(&confirm) == EOF) {
760
putchar('\n');
761
eof = 1;
762
}
prompt.c
+10
@@ -74,3 +74,13 @@ char *git_prompt(const char *prompt, int flags)
74
}
75
return r;
76
}
77
+
78
+int git_read_line_interactively(struct strbuf *line)
79
+{
80
+ int ret = strbuf_getline_lf(line, stdin);
81
+
82
+ if (ret != EOF)
83
+ strbuf_trim_trailing_newline(line);
84
+
85
+ return ret;
86
+}
prompt.h
+2
@@ -6,4 +6,6 @@
6
7
char *git_prompt(const char *prompt, int flags);
8
9
+int git_read_line_interactively(struct strbuf *line);
10
+
11
#endif /* PROMPT_H */
shell.c
+2
-2
@@ -4,6 +4,7 @@
4
#include "strbuf.h"
5
#include "run-command.h"
6
#include "alias.h"
7
+#include "prompt.h"
8
9
#define COMMAND_DIR "git-shell-commands"
10
#define HELP_COMMAND COMMAND_DIR "/help"
@@ -76,12 +77,11 @@ static void run_shell(void)
77
int count;
78
79
fprintf(stderr, "git> ");
79
- if (strbuf_getline_lf(&line, stdin) == EOF) {
80
+ if (git_read_line_interactively(&line) == EOF) {
81
fprintf(stderr, "\n");
82
strbuf_release(&line);
83
break;
84
}
84
- strbuf_trim(&line);
85
rawargs = strbuf_detach(&line, NULL);
86
split_args = xstrdup(rawargs);
87
count = split_cmdline(split_args, &argv);