Skip to content

Commit 08d383f

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9fadedd commit 08d383f

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

add-interactive.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lockfile.h"
1010
#include "dir.h"
1111
#include "run-command.h"
12+
#include "prompt.h"
1213

1314
static void init_color(struct repository *r, struct add_i_state *s,
1415
const char *slot_name, char *dst,
@@ -289,13 +290,12 @@ static ssize_t list_and_choose(struct add_i_state *s,
289290
fputs(singleton ? "> " : ">> ", stdout);
290291
fflush(stdout);
291292

292-
if (strbuf_getline(&input, stdin) == EOF) {
293+
if (git_read_line_interactively(&input) == EOF) {
293294
putchar('\n');
294295
if (immediate)
295296
res = LIST_AND_CHOOSE_QUIT;
296297
break;
297298
}
298-
strbuf_trim(&input);
299299

300300
if (!input.len)
301301
break;

add-patch.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "color.h"
88
#include "diff.h"
99
#include "compat/terminal.h"
10+
#include "prompt.h"
1011

1112
enum prompt_mode_type {
1213
PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -1158,9 +1159,8 @@ static int read_single_character(struct add_p_state *s)
11581159
return res;
11591160
}
11601161

1161-
if (strbuf_getline(&s->answer, stdin) == EOF)
1162+
if (git_read_line_interactively(&s->answer) == EOF)
11621163
return EOF;
1163-
strbuf_trim_trailing_newline(&s->answer);
11641164
return 0;
11651165
}
11661166

builtin/clean.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "color.h"
1919
#include "pathspec.h"
2020
#include "help.h"
21+
#include "prompt.h"
2122

2223
static int force = -1; /* unset */
2324
static int interactive;
@@ -420,7 +421,6 @@ static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
420421
return found;
421422
}
422423

423-
424424
/*
425425
* Parse user input, and return choice(s) for menu (menu_stuff).
426426
*
@@ -580,9 +580,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
580580
clean_get_color(CLEAN_COLOR_RESET));
581581
}
582582

583-
if (strbuf_getline_lf(&choice, stdin) != EOF) {
584-
strbuf_trim(&choice);
585-
} else {
583+
if (git_read_line_interactively(&choice) == EOF) {
586584
eof = 1;
587585
break;
588586
}
@@ -662,9 +660,7 @@ static int filter_by_patterns_cmd(void)
662660
clean_print_color(CLEAN_COLOR_PROMPT);
663661
printf(_("Input ignore patterns>> "));
664662
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)
668664
putchar('\n');
669665

670666
/* quit filter_by_pattern mode if press ENTER or Ctrl-D */
@@ -760,9 +756,7 @@ static int ask_each_cmd(void)
760756
qname = quote_path_relative(item->string, NULL, &buf);
761757
/* TRANSLATORS: Make sure to keep [y/N] as is */
762758
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) {
766760
putchar('\n');
767761
eof = 1;
768762
}

prompt.c

+10
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ char *git_prompt(const char *prompt, int flags)
7474
}
7575
return r;
7676
}
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
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
char *git_prompt(const char *prompt, int flags);
88

9+
int git_read_line_interactively(struct strbuf *line);
10+
911
#endif /* PROMPT_H */

shell.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "strbuf.h"
55
#include "run-command.h"
66
#include "alias.h"
7+
#include "prompt.h"
78

89
#define COMMAND_DIR "git-shell-commands"
910
#define HELP_COMMAND COMMAND_DIR "/help"
@@ -76,12 +77,11 @@ static void run_shell(void)
7677
int count;
7778

7879
fprintf(stderr, "git> ");
79-
if (strbuf_getline_lf(&line, stdin) == EOF) {
80+
if (git_read_line_interactively(&line) == EOF) {
8081
fprintf(stderr, "\n");
8182
strbuf_release(&line);
8283
break;
8384
}
84-
strbuf_trim(&line);
8585
rawargs = strbuf_detach(&line, NULL);
8686
split_args = xstrdup(rawargs);
8787
count = split_cmdline(split_args, &argv);

0 commit comments

Comments
 (0)